thefoggiest.dev

Fractal map creation

For a small msx adventure game I’m creating, I needed a map with three islands. A big one and two smaller ones. I’m not going to tell you what kind of game it is and if  I will ever finish it (because chances are I won’t), but that’s not the point here. The point it, how did I create the maps.

Using a small Java program called Fractalzoom, which I wrote yesterday evening. Fractalzoom is a small cli tool that takes a square gif image and randomly replaces its pixels with their neighbours. The function (I always call static java methods functions, because that’s what they really are) that does the work is really simple:

public static void process(Gif gif, Options options) throws Exception {
if (options.isVerbose())
System.out.print ("Processing: ");

for (int n = 0; n < options.getIterations(); n++) {
    if (options.isVerbose())
    System.out.print (".");
    for (int i = 1; i < gif.getHeight()-1; i++) {
      for (int j = 1; j < gif.getWidth()-1; j++) {

        int[] choices = new int[4];

        choices[0] = gif.getPaletteIndex(gif.getPixel(i, j-1)); // left
        choices[1] = gif.getPaletteIndex(gif.getPixel(i, j+1)); // right
        choices[2] = gif.getPaletteIndex(gif.getPixel(i-1, j)); // up
        choices[3] = gif.getPaletteIndex(gif.getPixel(i+1, j)); // down

        int choice = (int)Math.round(Math.random()*(choices.length-1));
        gif.setPixel(i, j, gif.getColour(choices[choice]));
      }
    }
  }
}

Gif is a wrapper class I created to facilitate using Wen Yu’s gifcodec, which lets me edit gif files. You should know that the gif format stores 256 colours in a palette and then uses their indexes throughout the rest of the file for each pixel. (Actually, it uses compression, but that’s not important here). What the code above does is visit every pixel in the file, find out the indexes of its neighbouring pixels, and then replaces it with one of them.

To use it, I create an image of for example 10 x 10 pixels. I use the Gimp. I fill it entirely with a dark blue colour, and then make a few pixels green, so that I have the rough outlines of my three islands. Then I scale it to 1000 percent, using no interpolation (that is important: I want 10×10 squares for each original pixel, and no anti-aliasing).

UPDATE: Instead of using Gimp for scaling, you can use ImageMagick to do it from a shell, which is nice if you have a lot of tiles:

# convert infile.gif -scale 1000% outfile.gif

# mogrify -scale 1000% *.gif

The resulting image is this:

stage1.gif: the original 10x10 pixels image, scaled up ten times.

Then I unleash Fractalzoom:

# fractalzoom stage1.gif

I get an image just as big, but with rigged coastlines:

stage1-zoomed

After that, I open it again in the Gimp, scaled it up 10 times again (again: no interpolation), and add elevated terrain using brown and white:

stage2

Next, I use Fractalzoom again, same as before. The image is larger, so it does more iterations. The result is this:

stage2-zoomed: the final version of my map, with rigged elevated terrain.

The result is a usable and realistic looking map (well, at least for my purposes). My islands have rigged coastal lines, bays, lakes, small islands of their coasts  and mountains with snow caps. If you think you can use it, you can download fractalzoom here. It should work on any machine with java 6.

UPDATE: After creating the map, I needed to divide it into tiles. The easiest way of doing this is using ImageMagick again:

# convert -crop 100x100 infile.gif outfile-%d.gif

The above results in tiles of 100 by 100 pixels each with the same name as the original file, appended with a number. The number of each individual tile designates where the tile is when ordered from left to right, top to bottom.


Categorised as: Uncategorized


3 Comments

  1. Christian says:

    Looks like Britain..

  2. Mike Bourke says:

    Can I get a ready-to-run version? If not, what format does the code have to be saved in – plain text? and what extension? I don’t know Java…

  3. thefoggiest says:

    Hi Mike, you download the tool from https://thefoggiest.dev/goodies/fractalzoom.tar.bz2 . There is a README in the archive which will tell you how to proceed.