2009. július 22., szerda

Map Generator


Problem/Question/Abstract:

How to implement a map generator in Delphi whioch produces rectangular maps with land or sea.

Answer:

The algorithm is very simple- I throw a few land points onto an empty map constraining the initial points so they don't drop near the edge. I then throw a few sea points down as well. Next I draw circles around each point (one circle round all the land points then repeat for the sea points), and repeat up to 12 layers (the constant Max-Layers defines this).

Circle data is defined in the data file Data.pas. This contains data for 35 concentric circular layers about the point at coordinates 0,0. The array layerpoints holds the number of points in each layer, offset indicates where a layer starts in the array C (Yeah stupid name- ah well) which holds all the points as offsets - ie the first layer has 8 points and the first 8 points at offset 0 in C are -1,-1,0,-1,1,-1,-1,0,1,0,-1,1,0,1,1,1, ie (-1,-1), (0,-1) (1,-1) etc up to (1,1) . Just add these to the point to give the coordinates of all points round it- these are set to land or sea as long as they are empty- so when land hits sea it defines the coast. An optimisation here is that each original point is switched off when the object attempts to grow a layer of points around it. If it succeeds in placing one new point then the original point is switched back on.

Once the initial map has been produced it is tidied by removing small continents (any below a threshold size defined in mincontsize). Island and sea sizes are counted by using a recursive fill algorithm. Each terrain square has a continent number which is set during the count- basically the refill routine sets the continent number if it is land then calls itself for all 8 squares around it.

The final step is to output the count of those continents that haven't been removed due to being too small and then plonk the continent number on the map in the approx centre of the continent (I calculate the average centre coordinate).


Component Download: MapGen.zip

Nincsenek megjegyzések:

Megjegyzés küldése