lauantai 21. heinäkuuta 2012

Map generation..

I've been pondering about automatic map generation lately.. I started to approach map generation few months ago, at first through creating QT based tool, that would run lua scripts and generate world. The approach was to create 'workspace' xml file, that contains all the worlds setup properties (width, depth, height, water height... ), list of available lua scripts, and run configuration of scripts aswell as how many iterations should the script pipeline run. In theory this approach is sound, and all.. but the hazzle with QT Xml parsing made me loose my will to live/code/develop/'to remain inspired' and the project became doomed because of it.

After that I've been developing my game engine framework and during this the map generation thing has been itching me.. Yesterday I started sketching about generating the maps, with set of tiny command line executables. Now where to start? creation of the 'grid' of course.. err.. wait.. grids have this bad property of having non uniform distances between points, the hypotenuse in the middle is not same distance away from each neighbour vertex in a primitive shape (thinking this in 2D flat plane).. So, lets try triangles, they seem nice, they seem easy to use too, and should LOD into groups as nicely as quads (so if I zoom away from the triangle 'grid' I can take 4 triangles to make 1 bigger triangle)..
Triangle points are uniform distance away from eachother.
Why is this important? well as the points/vertexes are the data points in the map, I think of them as 'sensor' spots, and to measure area properties in each spot fairly, those measurement spots should be equal distance away from eachother, always. With quads, that is impossible. Also, I feel that this should reduce distortions with the map in the end, as the only source for distortions at the time will come from adding the height dimension to the grid.

So triangle grid, creation of it requires length of the side of the big triangle, aswell as length of small triangle sides.. the command line executable should then be:
  • grid -create -bigsize 1000 ...
oh wait, what is 1000 ? meters centimeters ? millimeters? feets?
lets define it to be meters!
  • grid -create -bigsize 100000 -size 0.5
Still missing something.. selected algorithm ? and parameters for that? min,max?
  • grid -create perlin -bigsize 100000 -size 0.5 -dimension 256 -min 0.0 -max 10.0
Ok so now we have specified that we want to use perlin noise function to create the terrain and for the seed we use 256 sized random texture (256x256).
Initially I tried to create a triangular texture algorithm for this, but that just was too much work and inventing a new wheel, where just a regular rectangular texture would suffice. I decided that it is too complicated and does not really contribute to the project to invent triangle textures.
Also about the data organization in the file, I've decided that it will be 'sharp end upwards' approach, so that the highest point where the width of the structure is 1 is first. The datatype is 32bit floats, using the computers architecture (I am not going to bother with endianess etc. issues) as the theory goes that this is just seed data, the final map, will use this as a resource on this machine, and the output that it produces, is something different, that can take all the funny technical fubar things into account (json?xml?).
First results with parameters:
  • grid -create perlin -bigsize 10 -size 1.0 -dimension 2 -min 0.0 -max 10
 Running create.
Generating perlin BigSize: 10 Size: 1 Min: 0 Max: 10 Dimension: 2
3.48882
3.21913 6.07814
-0.416768 3.48882 2.15854
6.07814 3.21913 6.07814 6.53172
3.48882 -0.416768 3.48882 2.15854 2.15854
3.21913 6.07814 3.21913 6.07814 6.53172 6.07814
-0.416768 3.48882 -0.416768 3.48882 2.15854 2.15854 2.15854
6.07814 3.21913 6.07814 3.21913 6.07814 6.53172 6.07814 6.53172
3.48882 -0.416768 3.48882 -0.416768 3.48882 2.15854 2.15854 2.15854 2.15854
3.21913 6.07814 3.21913 6.07814 3.21913 6.07814 6.53172 6.07814 6.53172 6.07814

not really what I expected, the negative values shouldnt be there.. Few fixes maybe needed.

update:
I had few bugs in the perlin functions, it added too many times, scaled wrongly and all.. after fixing that, the generation is more healthy looking:
Running create.
Generating perlin BigSize: 10 Size: 1 Min: 0 Max: 10 Dimension: 2
created random seeds: 5.44206 4.15723 9.79308 0.682089
5.10193
5.42672 4.76487
4.7618 5.10193 5.02163
4.20711 5.42672 4.76487 4.76487
5.10193 4.7618 5.10193 5.02163 5.10193
5.42672 4.20711 5.42672 4.76487 4.76487 4.76487
4.7618 5.10193 4.7618 5.10193 5.02163 5.10193 5.02163
4.20711 5.42672 4.20711 5.42672 4.76487 4.76487 4.76487 4.76487
5.10193 4.7618 5.10193 4.7618 5.10193 5.02163 5.10193 5.02163 5.10193
5.42672 4.20711 5.42672 4.20711 5.42672 4.76487 4.76487 4.76487 4.76487 4.76487
except, for random seed to be 9 and 0.6, i dont see much of those..

Continued the editing, and the perlin noise map looks a bit, odd xD

A bit buggy generation..
Buggy fixed, Still couple oddities.