Map Generator

Engine

-

Language

Java

Time-span

3 uur

Persoonlijke Gamejam

Downloads

Niet Afgemaakt

Samenvatting

De map generator is gemaakt gedurende een korte gamejam van drie uur. Mijn doel was om een map-generator te maken die 'natuurlijke' bergen en dalen kan creëren. Deze map-generator kan met enkele kleine aanpassing ook gebruikt worden om 'Height-maps' te creëren.
//Set Base Height  
int h = 128;

//Initializing World (normalizing)
for(int i = 0; i <= size; i++)
{
    for(int j = 0; j <= size; j++)
    {
        world[i][j] = 0;
    }
}
//Set Corners
world[0][0] = h;
world[0][size] = h;
world[size][0] = h;
world[size][size] = h;

//Creating Hightmap
for(int loop = 2; loop < size+1; loop*=2)//loop through the Algorythm
{
    for(int ax = 1; ax <= loop; ax++)//loop through x axis
    {
        for(int ay = 1; ay <= loop; ay++)//loop through y axis
        {
            //corner coordinates
            int x1 = (size/loop)*(ax-1);
            int y1 = (size/loop)*(ay-1);
            int x2 = (size/loop)*(ax);
            int y2 = (size/loop)*(ay);

            double avg; //temporal variable to store the average

            avg = (world[x1][y1] + world[x2][y1] + world[x1][y2] + world[x2][y2])/4;//centre
            world[(x1+x2)/2][(y1+y2)/2] = avg + ((Math.random()*(2*h))-h);

            avg = (world[x2][y1] + world[x2][y2] + world[(x1+x2)/2][(y1+y2)/2])/3;//right
            world[x2][(y1+y2)/2] = avg + ((Math.random()*(2*h))-h);

            avg = (world[x1][y1] + world[x1][y2] + world[(x1+x2)/2][(y1+y2)/2])/3;//left
            world[x1][(y1+y2)/2] = avg + ((Math.random()*(2*h))-h);

            avg = (world[x1][y1] + world[x2][y1] + world[(x1+x2)/2][(y1+y2)/2])/3;//top
            world[(x1+x2)/2][y1] = avg + ((Math.random()*(2*h))-h);

            avg = (world[x1][y2] + world[x2][y2] + world[(x1+x2)/2][(y1+y2)/2])/3;//bottom
            world[(x1+x2)/2][y2] = avg + ((Math.random()*(2*h))-h);
        }
    }
    h/=2;
}
for(int ix=0;ix<size;ix+=2)
{
    for(int iy=0;iy<size;iy+=2)
    {
        double cur = world[ix][iy];
        if(cur <= med)
            g.setColor(new Color(0,0,75+(int)Math.round(75*(cur/med))));
        else if(cur <= med+(top*0.025))
            g.setColor(new Color(125+(int)Math.round(25*(cur-med)/(top*0.025)),100+(int)Math.round(25*(cur-med)/(top*0.025)),0));
        else if(cur <= (top*0.75))
            g.setColor(new Color(0,110-(int)Math.round(60*(cur-(med+(top*0.05)))/((top*0.75)-med)),0));
        else if(cur <= (top*0.9))
            g.setColor(new Color(50+(int)(100*((cur-(top*0.75))/(top*0.15))),50+(int)(100*((cur-(top*0.75))/(top*0.15))),50+(int)(100*((cur-(top*0.75))/(top*0.15)))));
        else
            g.setColor(new Color(200+(int)(55*((cur-(top*0.9))/(top*0.1))),200+(int)(55*((cur-(top*0.9))/(top*0.1))),200+(int)(55*((cur-(top*0.9))/(top*0.1)))));
            //g.setColor(new Color(1,10,10));
        g.fillRect(ix, iy, 2, 2);
    }
}

Screenshots

1
2
Concept Schets
Map