1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Procedural Generation v3.

Discussion in 'Tutorials' started by SagyDemn, Jul 20, 2016.

  1. SagyDemn

    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: Jul 20, 2016
  2. Jan2

    Jan2 Member

    Joined:
    Jul 20, 2016
    Posts:
    3
    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?
     
  3. SagyDemn

    SagyDemn Guest

    I'm trying the exact same thing for my space game. I'm working on it.
     
  4. Jan2

    Jan2 Member

    Joined:
    Jul 20, 2016
    Posts:
    3
    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?
     
  5. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    7,042
    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. ;)
     
    Yal, Jaqueta, Nathan Laing and 6 others like this.
  6. 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.
     
    elementbound likes this.
  7. SagyDemn

    SagyDemn Guest

    Bigger umber = more steps so it will try to add more blocks. and the room will be bigger
     
    Shariku Onikage likes this.
  8. SagyDemn

    SagyDemn Guest

    I'm vorking on the version 4 that you can easily edit the room size and stuff.
     
    Shariku Onikage likes this.
  9. 3dgeminis

    3dgeminis Member

    Joined:
    Jun 21, 2016
    Posts:
    133
    I cant download the example, can you upload a zip ?
     
    SagyDemn likes this.
  10. Ambition13z

    Ambition13z Guest

    ..
     
    Last edited by a moderator: Mar 6, 2017
  11. Ambition13z

    Ambition13z Guest

    Its in the CREATE EVENT which will only create this generation ONCE!
     
  12. Ambition13z

    Ambition13z Guest

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

    SagyDemn Guest

    You should set the 'wall' variable
     
  14. SagyDemn

    SagyDemn Guest

    I ll send it as soon as possible
     
  15. 3dgeminis

    3dgeminis Member

    Joined:
    Jun 21, 2016
    Posts:
    133
    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.
     
  16. Bigheadface

    Bigheadface Member

    Joined:
    Mar 14, 2017
    Posts:
    5
    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!
     
    Yal likes this.
  17. CKlidify

    CKlidify Member

    Joined:
    Nov 22, 2016
    Posts:
    9
    Extremely cool and helpful, thanks for the tutorial!
     
  18. OverLogic

    OverLogic Member

    Joined:
    Apr 6, 2017
    Posts:
    84
    is it possible for the island to be spawned in the middle
     
  19. Ramaraunt

    Ramaraunt Guest


    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

    [​IMG]
     
    Last edited by a moderator: Apr 24, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice