placing keys for doors

Discussion in 'Programming' started by 1213brett, Mar 14, 2019 at 8:29 PM.

  1. 1213brett

    1213brett Member

    Joined:
    May 30, 2018
    Posts:
    14
    Hi im have a randomly generated maze with some doors. I have keys for the doors. They are made on both sides of the door and one is destroyed when the other is picked up. Making shore that the player is always on the side of a key. I wont the keys to be randomly placed in the maze on both sides so the player has to look for them. I think the best way to do this is use a mp path. But I not only do I not know how to make the path destination random and still on the write sides of the door. you see the way the maze is the a key can be on the right and left of door and still on the same side of door. Is there a way to check to see if the path destination is accesoble meaning not blocked by the maze walls? hear is what I have so far. I don't think im vary close but hope I am. Thank you!

    //in the create event makes path goal on both sides of door but not off room
    east_keyX=random_range[room_width-32,door.x+63]
    east_keyY=random_range[room_height-32.32]
    west_keyX=random_range[room_width-door.x,door.x-63]
    west_keyY=random_range[room_height-door.y,door.y-32.]

    //in the create event makes path and grid
    key_grid = mp_grid_create(0,0,room_width/64,room_height/64,64,64)
    key_east_path = path_add()
    key_west_path = path_add()
    mp_grid_path(key_grid,key_east_path,door.x,door.y,east_keyX,east_KeyY,true)
    mp_grid_path(key_grid,key_west_path,door.x,door.y,west_keyX,west_keyY,true)
    mp_grid_add_instance(key_grid,obj_wall,true)
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,618
    There are methods to create mazes with keys and doors, but those can be heavy on code and logic for beginners, and I don't know what your familiarity level with coding is. However, you can use mp grid pathfinding to place keys on the right side of doors. First, generate a random position in the maze where the key might be placed. Then, try constructing a path from player to that key. If no path can be created, the key is inaccessible. Repeat with new random key position until a valid path can be found. For second key and onwards, remove the door from mp grid that the key opens, and start randomizing positions for the next key. Repeat until keys for all the doors have been generated. Note that randomizing positions like this means you can find keys "out of order" or for doors that are not yet accessible. However the key that will open the door that lets player progress will always be reachable.

    Doing lots of pathfinding like that may take a little while, but you can always obfuscate the action by displaying a loading screen.
     
  3. 1213brett

    1213brett Member

    Joined:
    May 30, 2018
    Posts:
    14
  4. 1213brett

    1213brett Member

    Joined:
    May 30, 2018
    Posts:
    14
    ok im almost there I have this in the doors create event but it creates all the keys on top of one another dose any one know how to fix this?



    var placed = false,
    mpg = mp_grid_create(0, 0, room_width div 32, room_height div 32, 32, 32),
    mpp = path_add();
    mp_grid_add_instances(mpg,obj_Wall_Sprite, false);
    mp_grid_add_instances(mpg,h_door, false);
    randomize(){
    with (instance_create(irandom(room_width), irandom(room_height), key))
    while (!mp_grid_path(mpg, mpp, x, y, player.x, player.y, false)) or place_meeting(x,y,key) {
    key.x = irandom(room_width);
    key.y = irandom(room_height);
    move_snap(32,32);
    }
    }
    mp_grid_destroy(mpg);
    path_delete(mpp);
     

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