Using recursive fill to count maps
In the Empire game (yes, just one last mention!) during the map generation the program counts up the size of individual islands and sea areas. This is don by recursion and quite useful. Otherwise, the usual place you see recursion mentioned is in calculating factorials and Fibonacci sequences.
The map struct holds not only the terrain type and other information but a continent number. This is set initially to 0. After the land and seas have been generated, the program picks a random land point on the map (just keep picking a random location until it has land with a continent number of 0).
It then counts by calling itself 8 times. It calls itself recursively at location y,x with directions (y-1,x-1) that’s NorthWest, (y-1,x) and so on round to West (y,x-1). But it only does it as long as each of those locations is land type with a continent number of 0. And as it does it, it sets the continent numbers in each location to 1 for the first continent and so on.
This recursive algorithm means that every contiguous land location gets reached. A similar thing is done with seas and lakes but you have to be careful that the stack is large enough.
void FillIn(int x,int y,int locis) {
if onMap(x,y) {
if (locis==map[x][y].locis && map[x]
[y].continent==UNALLOCATED) {
map[x][y].continent=numconts;
allconts[numconts].count++;
FillIn(x-1,y,locis);
FillIn(x+1,y,locis);
FillIn(x,y-1,locis);
FillIn(x,y+1,locis);
FillIn(x-1,y-1,locis);
FillIn(x+1,y-1,locis);
FillIn(x+1,y+1,locis);
FillIn(x-1,y+1,locis);
}
}
}
This is used for both land and sea, so locis can be either. It’s an efficient algorithm and quite quick.
Those graphics? They were the initial hexagon graphics…
This post refers to the c sources in the AboutEmpire.zip file in the
Nuklear

Back in 2012/2013 I was writing the C/C++/C# column for About.com and I was doing games tutorials with SDL. This is an Empire type game, much like the Z80 game I mentioned in yesterday’s post except coded in C and with hexagons instead of squares.
Part of the reason is SDL which is a wonderful library and whose potential I have barely touched. I used to be an 8-bit games developer back in the 1980s, writing games for CBM-64, ZX Spectrum, MSX and Amstrad CPC-464.
When I wrote the Windows version of the book I used Visual Studio and it was quite excellent. But Visual Studio Code (VSC), the free cross-platform IDE is also very impressive.
Without these, I’d get compile errors like __timespec redefined errors.