Tag: Lloyds Relaxation Algorithm

Map Generation, Voronoi Debugging

After a solid week of debugging and reworking a php library I had found that generated a Voronoi diagram, I finally managed to hammer out the last kinks. It turns out that the two itterations of the Lloyds relaxation algorithm I used were sometimes coming up with sites that lay outside of the bounding box, which ended up causing all kinds of trouble. I now finally have a Voronoi polygon generator that a) I understand, and b) is performing fast enough for me to continue with.

Recreating Randomness
In order to properly debug the problem, I had to be able to recreate the problem properly. Initially, I used the PHP rand() function to create 1000 random points within a 1024×768 canvas. Each time the problem came up and I reloaded the script, it would come up with another 1000 random points, different each time. After first coming up with a half-arsed solution whereby I also outputting the coordinates for the 1000 points, I found out that you can set a seed with PHP’s srand() function. This allowed me to either generate a random number based on the system time, or just supply a number of my own. I made a little form that allowed me to generate a diagram based on a supplied number or generate one randomly and display the seed for that diagram. That way I could flip through random diagrams until I found one that was broken and simply keep generating that same diagram with the given seed. That made things much, much easier.

In the meantime, I also incorporated some code that wrote all the relevant information for the diagram into an XML file with which I could also once again recreate the diagram, without having to calculate everything again.

Up Next
Now that I’ve finally been able to find the flaws in the PHP library that I was working with and the code that I was using to call the library, I will continue exploring the finished code a bit, seeing if I can make some tweaks and do some cleaning up. I will also try to post the finish product of what I ended up making, perhaps with some more interactivity (the ability to set the canvas size, adjust the number of random sites, and perhaps also the amount of Lloyd iterations it does) so that people can play with it.

Then I’ll start looking at using a Perlin library to start creating a rudimentary shape for the map that I want to eventually create. Once I’m happy with that, elevation and moisture will come into play, which in turn means we’ll be able to make individual biomes.

I’m sure there’s still much more to do, but I have to say that even though the going was tough the last week — to the point that I was even dreaming about Voronoi polygons — it’s really rewarding to achieve breakthroughs and to persist into success.

Anyway,  I’ll try to get my script up tomorrow.