Procedural Generation v3.

S

SagyDemn

Guest
GM Version: GM7
Target Platform: All
Download: https://www.dropbox.com/sh/ahcvg0ludyarh28/AAA_k-qUVXRsSToo3nMAjpu0a?dl=0

Summary: This is a Tutorial about Procedural Generation of a room with Walls and Floors.

RandomGen3.PNG

Step1: Create a (Background) for the Floor tile. (Default size 16x16)
Step2: Create a Wall (Sprite). (Same size with the Floor Tile)
Step3: Create an (Object) called "oWall".
Step4: Create another (Object) called "oGenController".
Step5: Inside oGenController (Create Event) paste this code.

Code:
///Create a random map
randomize();
c_x = room_width/2;
c_y = room_height/2;
wall = oWall;//The Wall object
t_size = 16;//Size of the floor tile
steps = 50;//This is how many times it will create a block.Bigger  value = better shapes
t_depth = 1000;//Tile Depth
//Place floor
for(var s = 0;s <= steps;s ++){
    var dir = choose(0,1);
    if (dir = 0){ c_x += choose(-t_size,t_size); }//chooses to move at x
    if (dir = 1){ c_y += choose(-t_size,t_size); }//chooses to move at y
    tile_add(tile_floor,0,0,t_size,t_size,c_x,c_y,t_depth);
}
//Reset the position of the checked area.
c_x = 0;
c_y = 0;
////////
//Check The whole Room and place walls
for(i = 0;i <= room_width/t_size;i ++){
    for(j = 0;j <= room_height/t_size;j ++){
        if c_x <= room_width{
            c_x += t_size;
        }else{
            c_x = 0;
            c_y += t_size;
        }
        var cur_tile = tile_layer_find(t_depth,c_x,c_y);//Check the current tile that we are colliding.
        var tmp_tile0 = tile_layer_find(t_depth,c_x+t_size,c_y);//Check right of the current tile
        var tmp_tile1 = tile_layer_find(t_depth,c_x-t_size,c_y);//Check left of the current tile
        var tmp_tile2 = tile_layer_find(t_depth,c_x,c_y+t_size);//Check bottom of the current tile
        var tmp_tile3 = tile_layer_find(t_depth,c_x,c_y-t_size);//Check top of the current tile
        if cur_tile != -1{//Check we are colliding with a tile
            if(tmp_tile0 == -1){instance_create(c_x+t_size,c_y,oWall);}//Place wall on the right
            if(tmp_tile1 == -1){instance_create(c_x-t_size,c_y,oWall);}//Place wall on the left
            if(tmp_tile2 == -1){instance_create(c_x,c_y+t_size,oWall);}//Place wall on the bottom
            if(tmp_tile3 == -1){instance_create(c_x,c_y-t_size,oWall);}//Place wall on the yop
        }
    }
}


Step6
: Create a room.
Step7: Default Size of the Room = 640x320
Step8: Place oGenController inside the room.
Step9: Run the Game.

RandomGen v3.gif
 
Last edited by a moderator:
J

Jan2

Guest
I am looking for a random generated starfield! For me this is NOT the solution! Creating hundreds of instances on a map is a very bad solution, the problem is that such a game is very cpu intensive. How can I DRAW hundreds of stars on a map which are randomly placed?
 
S

SagyDemn

Guest
I am looking for a random generated starfield! For me this is NOT the solution! Creating hundreds of instances on a map is a very bad solution, the problem is that such a game is very cpu intensive. How can I DRAW hundreds of stars on a map which are randomly placed?
I'm trying the exact same thing for my space game. I'm working on it.
 
J

Jan2

Guest
Let's make some things clear!
If you want to draw something you must use the draw event, the problem is that this event is called every step!
How can you draw something only once?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I am looking for a random generated starfield! For me this is NOT the solution! Creating hundreds of instances on a map is a very bad solution, the problem is that such a game is very cpu intensive. How can I DRAW hundreds of stars on a map which are randomly placed?
Let's make some things clear!
If you want to draw something you must use the draw event, the problem is that this event is called every step!
How can you draw something only once?
This has nothing to do with this topic. Please show some respect for the work that the OP has done and keep replies specific to the topic. Anything else should be posted in the Programming forum. ;)
 
S

Shariku Onikage

Guest
I get the feeling Jan2 thought they were in a different thread. They seemed very offended that someone dared to write a tutorial that didn't match up with exactly what they asked for (they do have another post which is about asking for random starfield generation, so maybe they just got confused...).

Anyway this was great. It's simple and very efficient. Does the steps variable control the size of the room (i.e. the higher the number the bigger the rooms will be)? I'm thinking you could have a choose(10,50,150) value in there for different size rooms, or have it relate to global variables that work outside the room to make large dungeons that guarantee a mix of different room types.
 
S

SagyDemn

Guest
I get the feeling Jan2 thought they were in a different thread. They seemed very offended that someone dared to write a tutorial that didn't match up with exactly what they asked for (they do have another post which is about asking for random starfield generation, so maybe they just got confused...).

Anyway this was great. It's simple and very efficient. Does the steps variable control the size of the room (i.e. the higher the number the bigger the rooms will be)? I'm thinking you could have a choose(10,50,150) value in there for different size rooms, or have it relate to global variables that work outside the room to make large dungeons that guarantee a mix of different room types.
Bigger umber = more steps so it will try to add more blocks. and the room will be bigger
 
S

SagyDemn

Guest
I get the feeling Jan2 thought they were in a different thread. They seemed very offended that someone dared to write a tutorial that didn't match up with exactly what they asked for (they do have another post which is about asking for random starfield generation, so maybe they just got confused...).

Anyway this was great. It's simple and very efficient. Does the steps variable control the size of the room (i.e. the higher the number the bigger the rooms will be)? I'm thinking you could have a choose(10,50,150) value in there for different size rooms, or have it relate to global variables that work outside the room to make large dungeons that guarantee a mix of different room types.
I'm vorking on the version 4 that you can easily edit the room size and stuff.
 
A

Ambition13z

Guest
Let's make some things clear!
If you want to draw something you must use the draw event, the problem is that this event is called every step!
How can you draw something only once?
Its in the CREATE EVENT which will only create this generation ONCE!
 
A

Ambition13z

Guest
A small issue, I did everything as follows and received the following error:Capture.PNG
 

3dgeminis

Member
I was able to download the example, change of browser. Even so you should upload a zip for more comfort-
By the way good example, I learned some things.
 
B

Bigheadface

Guest
This is a wonderful tutorial. Thanks for posting it.

I'm learning more and more each day thanks to the generous postings of folks like you. So THANKS!
 
  • Like
Reactions: Yal
R

Ramaraunt

Guest
I am looking for a random generated starfield! For me this is NOT the solution! Creating hundreds of instances on a map is a very bad solution, the problem is that such a game is very cpu intensive. How can I DRAW hundreds of stars on a map which are randomly placed?

I've made a galaxy with 10,000 stars before no lag. You do it by making a bunch of arrays and a single object to draw them all. You need one array for x cords, one for y cords, and one for star names.

As for the spiral shape of a galaxy, its actually pretty easy to make. Follow these steps:

Make a "Galactic Core" object that will be the center. Then, over one frame, send out "Galaxy Guides", which go out in a spiral shape with golden spirals (https://en.wikipedia.org/wiki/Golden_spiral). You do this by:

  • For each arm of the galaxy
    • Send out a Guide 30 pixels, and stop it with an alarm that lasts one frame.
    • add 16 degrees to the angle, and send out another guide 30 more pixels than the last.
    • keep repeating until you have 20 guides, then go to the next arm. Evenly space the starting angles of each arm. For instance, if you have four arms, it would be good to have them at 0, 90, 180, and 270.
  • Then, randomly place stars around each guide within a certain distance. The result is a spiral galaxy.
So you get a shape like this, for each arm, created by 20 spaced out guide points

 
Last edited by a moderator:
Top