• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows World Generator Doesn't Work

A

aGreenCrystal

Guest
Hey, i've coded a small world generator.
I've put the script into the rooms creation code,
but if i start the game it just does nothing.

Does anybody know why?

Code:

randomize();

scl = 128;
worldsize = 100;

grass_lowest = 2;
grass_highest = 7;

mountain_lowest = 8;
mountain_highest = 9;

water_lowest = 0;
water_highest = 1;

a = 0;
currentx = 0;
currenty = 0;

mapgrid = ds_grid_create(worldsize, worldsize);

repeat 5
{
currenty = -1;
repeat worldsize
{
currentx = -1;
currenty += 1;
repeat worldsize
{
currentx += 1;
if ds_grid_get(mapgrid, currentx, currenty) != ds_grid_get_max(mapgrid, currentx-1, currenty-1, currentx+1, currenty+1)
{
if ((ds_grid_get_max(mapgrid, currentx-1, currenty-1, currentx+1, currenty+1))-1)>0
{
ds_grid_set(mapgrid, currentx, currenty, (ds_grid_get_max(mapgrid, currentx-1, currenty-1, currentx+1, currenty+1))-1);
}
else
{
ds_grid_set(mapgrid, currentx, currenty, 0);
};
};
};
};
};

currenty = -1;
repeat worldsize
{
currenty += 1;
currentx = -1;
repeat worldsize
{
currentx += 1;

a = ds_grid_get(mapgrid, currentx, currenty);

if a > water_lowest-1 and a < water_highest+1
{
draw_sprite_tiled(sWater, 0, currentx*scl, currenty*scl);
};

if a > grass_lowest-1 and a < grass_highest+1
{
draw_sprite_tiled(sGrass, random_range(0, 3), currentx*scl, currenty*scl);
};

if a > mountain_lowest-1 and a < mountain_highest+1
{
draw_sprite_tiled(sMountain, 0, currentx*scl, currenty*scl);
};
};
};
 

Simon Gust

Member
The creation code for the room is run once when the room starts, any draw or step functions will not do much.
Also, if you're using data structures that aren't global, you can't destroy them.
Use objects and their events, it is what they were designed for.
Also note that each create event is run before the room creation code.
 
A

aGreenCrystal

Guest
Thank you for your quick help.
I've now moved the code over to an object, but it didn't change the result. :/
 
A

aGreenCrystal

Guest
Yes I did.
What seemed strange to you?
(I am, obviously, pretty new to GameMaker, so the code is not perfect, but it also doesn't need to. I'd be happy with it just working.)
 

Simon Gust

Member
Yes I did.
What seemed strange to you?
(I am, obviously, pretty new to GameMaker, so the code is not perfect, but it also doesn't need to. I'd be happy with it just working.)
Are you sure about how the code is supposed to work?
one major thing is using draw sprite tiled instead of draw sprite. Draw_sprite_tiled tiles the sprite over the whole room and since you are drawing per grid, it will do nothing but slow everything down.
I played around with it a bit.
Code:
/// CREATE
randomize();
worldsize = 64;
size = 16;

mapgrid = ds_grid_create(worldsize, worldsize);
ds_grid_clear(mapgrid, 1);
tempgrid = ds_grid_create(worldsize, worldsize);

stonelevel = 12;
waterlevel = 50;

// fill grid with random caves and air at the top
for (var i = 0; i < worldsize; i++)
{
    for (var j = 0; j < worldsize; j++)
    {
        if (j < 8)
        {
            ds_grid_set(mapgrid, i, j, 0);
        }
        else
        if (!random(10))
        {
            ds_grid_set_region(mapgrid, i-2, j-2, i+1, j+1, 0);
        }
    }
}
ds_grid_copy(tempgrid, mapgrid);

// cellular automata
repeat (6)
{   
    scr_automata();
}

// make types
for (var i = 0; i < worldsize; i++)
{
    for (var j = 0; j < worldsize; j++)
    {
        var grid = ds_grid_get(mapgrid, i, j);
        if (grid)
        {
            if (j > stonelevel) ds_grid_set(mapgrid, i, j, 2);
        }
        else
        {
            if (j > waterlevel) ds_grid_set(mapgrid, i, j, 3);
        }
    }
}
Code:
/// DRAW
for (var i = 0; i < worldsize; i++)
{
    var xx = i * 16;
    for (var j = 0; j < worldsize; j++)
    {
        var yy = j * 16;
        var grid = ds_grid_get(mapgrid, i, j);
        switch (grid) 
        {
            case 2: draw_sprite(sMountain, 0, xx, yy); break;
            case 1: draw_sprite(sGrass, 0, xx, yy); break;
            case 3: draw_sprite(sWater, 0, xx, yy); break;
        }
    }
}
Code:
/// scr_automata
for (var i = 0; i < worldsize; i++)
{
    for (var j = 0; j < worldsize; j++)
    {
        var flag = true;
        var grid = ds_grid_get(tempgrid, i, j);
        var count = getCount(i, j);
        if (grid)
        {
            if (count < 3) 
            {
                ds_grid_set(mapgrid, i, j, 0);
            }
            else 
            {
                ds_grid_set(mapgrid, i, j, 1);
            }
        }
        else
        {
            if (count > 4)
            {
                ds_grid_set(mapgrid, i, j, 1);
            }
            else
            {
                ds_grid_set(mapgrid, i, j, 0);
            }
        }           
    }
}
ds_grid_copy(tempgrid, mapgrid);
Code:
/// getCount(i, j)
var i = argument0;
var j = argument1;
var mid = ds_grid_get(tempgrid, i, j);
return (ds_grid_get_sum(tempgrid, i-1, j-1, i+1, j+1)-mid);
I made the 3 sprites with the size of 16x16

So this cellular automata thing is for making random caves, you can find the blog here
https://gamedevelopment.tutsplus.co...-levels-using-cellular-automata--gamedev-9664
 
A

aGreenCrystal

Guest
Thanks for the help.
I realized that what I had most problems with was,
that I didn't know draw_sprite would only draw it for one tick.
I now fixed that and created a new code, which works.
 
Top