Wargame-Programming.com
Progamming Tips













Home

News | Projects | Downloads | Blitz Code | Progamming Tips





General tips for the budding wargame programmer.
















*Add a version number to your data files: Data files that the user can create (e.g. map files, game save) need to include a version number in the header data. This is in case you latter change the file format, you detect and take corrective action if the user tries to load a old file format into the new version of your program. It also a better way to trap improper file formats than relying on file extensions. Do give the user the option of loading the file at his own risk in any case.

*Be cautious with recursion: Recursion is the simplest way to solve many common programming task, but recursion functions are also great at causing stack overflows and locking-up computers. If your recursive routine uses a counter, always check the initial counter to make sure it has a reasonable value. Put a timer or counter check into the recursive routine to abort it automatically if a preset value is exceeded. Aborts based on a key press generally don't work in my experience.


* Organize Your Code: Name functions and subroutines using a noun_verb format. This makes it easy to find related functions. For example;

Map_Load()
Map_Save()
Map_Draw()
Map_Update()

The next step of course is to put all the related routines into a include file and give it a name such as "Map Functions.bb". And while I am on the subject of include files, at least in regards to Blitz Basic, include files should only contain functions. All global, dimensioning, and constant statements should be left intact but commented out. Copy those statements to the beginning of the main file and put them under a meaningful heading, e.g.;

;****** Map Functions Globals ***************
;Const MaxMapX=100, MaxMapY=100
;Dim Map(MaxMapX, MaxMapY)
;Global MapX,MapY

* Use Staggered squares: Everything you can do with hexagons can be accomplished with a lot less trouble using rows of squares where every other row is offset by half a square (like a brick wall). When everything is working you can draw a hexagon grid over the top to make it look pretty. Download Hexmap for a working scheme.

* Design/use generic interface tools: If you need a menu, message box, scroll bar in one sub or program, chances are you going to need one some where else to. Take a little extra time up front and make it generic. Essentially this means passing the key variables as parameters and not hardwiring any screen coordinates into the routine (i.e. use variables to define the upper right corner of your message box and position other screen elements as offsets to them.

* How to draw a Hexagon Presuming the underlying structure is a staggered square of width w in pixels with its upper left hand corner located at 0,0 in screen coordinates, lines need to be drawn from;
x1,y1 to x2,y2
0,+k to w/2, -k
w/2, -k to w,+k
w,+k to w, w-k
w, w-k to w/2, w+k
w/2, w+k to 0, w-k
0, w-k to 0, +k

where the value of k is selected such as to yield a attractive hexagon. For a perfect hexagon; k = w/4/SQRT(3) although I prefer the flatter hexagon obtained by setting k one or two units less than the ideal value. Of course, to draw a field of hexagons you only to draw three sides of each hexagon as long as you add an extra row and column worth of hexes to supply the missing sides to the edge hexagons.

* Order Terrain: Consider carefully the sequence of values you use to designate different terrain types in your map array. For example all terrain that tanks are not permitted to enter could have a terrain value of 10 or greater where all terrain where airdrops are permissible could have a value of less than 5.