Archive for January, 2009

Aircraft and Unit Stacking

January 24, 2009

I have been working on adding aircraft units to the game today. Adding airplanes is more work than for other unit types, because aircraft units can be placed on top of other units. That means I have to write code for arranging stacked units in a way that is both pleasing to the eye and lets players refer to individual units within a hexagon unambiguously.

screenshot-090124

I am not done yet, but it seems to work pretty well now. The screenshot shows German divebombers stacked on top of other units. One of the units is always minimized when two units share the same hex. The unit that is not minimized is the unit that a click on that hex will refer to.

Right now, you cannot select a minimized unit without moving the other unit in that hex somewhere else first, but I plan to make it possible to select the minimized unit simpy by clicking on it. Now mouse clicks are interpreted as referring to hexagons and not to individual units, which worked as long as there was never more than one unit in any hex.

Work left:

  • Bombers can not attack other units yet.
  • Make it possible to refer to minimized units
  • Remove any overlap between units

Battleships

January 10, 2009

Today I completed support for another unit type–now there are battleships too!

An obvious difference between tanks and ships is the type of hexes they can move to, but another difference is that ships can fire at enemies farther away. So now all units have a range field that controls how far they can shoot. The ship in the middle of the screenshot has a range of two, which makes it able to attack the tank to the right of it.

screenshot-0901101

I have done a lot of refactoring in the Scheme part of the code lately. I’m turning more and more towards an object-oriented style because it fits many of the problems in the game like a glove, but I still use a functional style where it is appropriate.

There are still some problems left. For instance, if a ship attacks a tank two hexes away, the tank is able to return the fire and damage the ship even though it is out of the tank’s range. The tank can not initiate a fight with the ship, but it can defend itself at any range.

Next, I want to add some visual indication of which enemies a unit can attack. For instance, a cross hair could appear around an attackable unit when the user moves the mouse pointer over it.

Terrain-Sensitive Movement

January 4, 2009

I had planned to work more on the game during the Christmas holidays, but I did not start until a few days ago. But I made some clear progress these last few days.

The algorithm that calculates the set of hexagons a unit can reach now takes terrain into account. For instance, crossing a forested hexagon costs more movement points than passing through a hexagon representing farm land.

The screenshot shows the hexagons the blueish tank near the bottom-left corner can move to. The forest hexes at the top were previously reachable.

screenshot-0901041

At first, I thought all I had to do was associate costs with every edge and sum them up as I traversed the graph, but after a while I realized that breadth-first search (which is what I used before) cannot be used  with weighted graphs to solve this problem (unless all edges have the same weight). So I had to throw away the old search code and implement Dijkstra’s algorithm instead. Took me longer than I expected, but now the code is cleaner and handles weighted graphs properly.

I also simplified the rules for how close to an enemy a unit is allowed to move without having to stop. The new rule is simply that you cannot pass through a hex that is adjacent to an enemy unit. That is all that is needed, really. Thanks Peter E. for making a comment that made me realize how pointless the (relative) complexity of the old method was!