P
paulog
Guest
Hello! I'm thinking about adding a random terrain generator to my game, so I created an empty project to test some codes.
I managed to do something I like, but the problem is that it's somehow really slow.
I'll atach the code I used in the end of this post, but basically:
-There is a loop that fills the room with 10x10 tiles
-Each tile has a 50% chance of becoming grass or water
-If I press E, every grass tile that is surrounded by 4 water tiles or more, becomes a water tile
-If I press W, every water tile that is surrounded by 4 grass tiles or more, becomes a grass tile
If the room is 200x200 pixels or less the process is very fast, but as I increase the size, the process keeps getting slower. If the room is 1280x720 pixels, for example, it takes more than three seconds.
Is there a way to make this faster? Or that is just normal? Am I doing this the wrong way? If someone could help me I'd be very grateful. Thanks in advance!
Here is the code:
All the sprites have the origin point in the center.
I managed to do something I like, but the problem is that it's somehow really slow.
I'll atach the code I used in the end of this post, but basically:
-There is a loop that fills the room with 10x10 tiles
-Each tile has a 50% chance of becoming grass or water
-If I press E, every grass tile that is surrounded by 4 water tiles or more, becomes a water tile
-If I press W, every water tile that is surrounded by 4 grass tiles or more, becomes a grass tile
If the room is 200x200 pixels or less the process is very fast, but as I increase the size, the process keeps getting slower. If the room is 1280x720 pixels, for example, it takes more than three seconds.
Is there a way to make this faster? Or that is just normal? Am I doing this the wrong way? If someone could help me I'd be very grateful. Thanks in advance!
Here is the code:
GML:
randomize()
for(xx = 10; xx <room_width - 5; xx += 10)
{
for(yy = 10; yy <room_height - 5; yy += 10)
{
instance_create(xx,yy,obj_tile)
}
}
GML:
space = choose(1,2,3,4)
if space >= 1 and space <= 2
{
instance_create(x,y,obj_grass)
}
if space >= 3 and space <= 4
{
instance_create(x,y,obj_sea)
}
instance_destroy()
GML:
if position_meeting(x,y-6,obj_sea)
{
csea+= 1
}
if position_meeting(x,y+6,obj_sea)
{
csea+= 1
}
if position_meeting(x-6,y,obj_sea)
{
csea+= 1
}
if position_meeting(x+6,y,obj_sea)
{
csea+= 1
}
if csea>= 4
{
instance_create(x,y,obj_sea)
instance_destroy()
}
GML:
if position_meeting(x,y-6,obj_grass)
{
cgrass+= 1
}
if position_meeting(x,y+6,obj_grass)
{
cgrass+= 1
}
if position_meeting(x-6,y,obj_grass)
{
cgrass+= 1
}
if position_meeting(x+6,y,obj_grass)
{
cgrass+= 1
}
if cgrass>= 4
{
instance_create(x,y,obj_grass)
instance_destroy()
}
All the sprites have the origin point in the center.