Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name for this sampling algorithm used in Minicraft?

Tags:

algorithm

For Ludum Dare 22, Notch programmed a game in 48 hours called Minicraft. It's like a 2D minecraft.

Anyway the source is available (here: http://www.ludumdare.com/compo/ludum-dare-22/?action=preview&uid=398 ), and I was taking a look since I am interested in random generation of terrain and levels. In the code is a block of code which runs the core generation, and the algorithm to me seems familiar, but I can't put a name to it. I'd like to know exactly what it is so I can read more about it and learn how it works.

Specifically, the code is from levelGen.java:

    do {
        int halfStep = stepSize / 2;
        for (int y = 0; y < w; y += stepSize) {
            for (int x = 0; x < w; x += stepSize) {
                double a = sample(x, y);
                double b = sample(x + stepSize, y);
                double c = sample(x, y + stepSize);
                double d = sample(x + stepSize, y + stepSize);

                double e = (a + b + c + d) / 4.0 + (random.nextFloat() * 2 - 1) * stepSize * scale;
                setSample(x + halfStep, y + halfStep, e);
            }
        }
        for (int y = 0; y < w; y += stepSize) {
            for (int x = 0; x < w; x += stepSize) {
                double a = sample(x, y);
                double b = sample(x + stepSize, y);
                double c = sample(x, y + stepSize);
                double d = sample(x + halfStep, y + halfStep);
                double e = sample(x + halfStep, y - halfStep);
                double f = sample(x - halfStep, y + halfStep);

                double H = (a + b + d + e) / 4.0 + (random.nextFloat() * 2 - 1) * stepSize * scale * 0.5;
                double g = (a + c + d + f) / 4.0 + (random.nextFloat() * 2 - 1) * stepSize * scale * 0.5;
                setSample(x + halfStep, y, H);
                setSample(x, y + halfStep, g);
            }
        }
        stepSize /= 2;
        scale *= (scaleMod + 0.8);
        scaleMod *= 0.3;
    } while (stepSize > 1);

Those two for loops are running some kind of sampling algorithm, and I would just like to know if this is known named algorithm, or if notch just rolled his own.

like image 379
Charles Randall Avatar asked Dec 21 '11 20:12

Charles Randall


1 Answers

This looks like the diamond-square algorithm.

like image 64
Oliver Charlesworth Avatar answered Nov 15 '22 06:11

Oliver Charlesworth