Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help Needed] Creating levels of 4x4 rooms connected to each other (no corridors)

Discussion in 'Scripting' started by thoughtcrime, Dec 19, 2014.

  1. thoughtcrime

    thoughtcrime

    Joined:
    Dec 19, 2014
    Posts:
    6
    sampleMap.png I've read quite a bit on procedural generation of levels (dungeon types, caves/mines, over land, etc), but most of the techniques use rooms connected with hallways or corridors running between randomly generated rooms. However, what I'm looking to do is create a set of 4x4 prefab rooms, and then connect them directly to each other. The themes of each room are similar, so transitions aren't really an issue, as the prefabs and designs for each room are already defined. Also, the "door size" from one room to the next may vary (ie., ExitEdge = 1-4 squares), so many of the procedural or dungeon crawler tools don't really suit my needs.

    I will include an image of what a typical map would look like after a few rounds of play, but I'm curious if someone could point me in the right direction to accomplish my goal of randomly generating tilesets at run-time instead of preloading them, and where to look to learn how to write the code to place another 4x4 offset from the anchor of the current tile. I don't really want anyone to write the full code out for me, because I don't learn that way. I'm open to watching tutorials on YouTube, reading blogs, documentation, etc., but I am relatively new to Unity (and C#), so I don't even really know where to start beyond what I've already done.

    Thanks in advance. Here's the map with some icons and other info:
    View attachment 121809
     
  2. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    I'm pretty sure the typical way these kinds of dungeons are generated is as a tree. You start off picking directions to add new rooms, add them to the tree, generate the room (and running all the checks that would be involved), and then lather, rinse, and repeat until the tree is filled.

    Another options is using a BSP tree to subdivide an area, but this still requires you to manually connect the rooms how you want.
     
  3. thoughtcrime

    thoughtcrime

    Joined:
    Dec 19, 2014
    Posts:
    6
    I assume this still must be done before the players ever load into the level, meaning the entire level is built before the players spawn at the start location.

    I don't suppose it's possible to generate a map of 4x4 prefab "blanks" and then replace them with "new" prefab as they are revealed.

    For example, prefab_0 is a 4x4 set of flat tiles. When the level is loaded, the map generator places the max number of actual tiles (let's say there are 10 real tilesets) in a grid of 10 x 10,and sets their mesh to prefab_0. We have a true grid of 400x400, made from 10x10 prefab_0. Then, when a player moves to a square, if square.isTileExit = true, then newTile = TileLibrary.selectTile.id(Random.id(0,9));

    Once the id of newTile is known, mesh.replace(prefab_0.mesh,newTile.mesh);

    After the draw is done on the mesh replace, then setAlpha.fogofwar(0), and the player is able to see the new room.

    I know that is pseudo code gibberish, especially since I'm so new to unity and C#, but i think it conveys what I am visualizing better than just trying to describe it in an asynchronous conversation.

    Is it possible to do what I'm trying to do, or should I just resign myself to setting a start tile and an end tile, a general path, and then random path deviations that all load before the player unit spawns? Player spawn never changes, so that double tile is always known. It's what happens after that is player-driven.
     
  4. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    I theory it's possible to essentially have the dungeon generated "underneath" the player, but in practice I wouldn't imagine it would make decent dungeons. The way the first method works can be used this way since you are essentially searching through the tree. It's not that difficult to let the player become the entity that is traveling the tree and is responsible in a sense for how it generates.

    I'm against doing it this way though since the levels will be prone toward being very linear. You won't likely be able to retroactively loop rooms, so you will mostly end up with a root room that branches in a few directions that rarely interact.
     
  5. thoughtcrime

    thoughtcrime

    Joined:
    Dec 19, 2014
    Posts:
    6
    I don't disagree with you on the linear nature of the design. I'm actually working off of an existing board game series for the mechanics of how the world is created, and then I will be using some homebrewed assets, units, etc., to move away from the IP and only use the basic ruleset of the board game's systems to be familiar to players. The levels themselves can be boring in their layout, and sometimes loop back on themselves to a dead end wall, but the rules are strict in how things are constructed, which would allow for me to easily code all of the rules and mechanics (this is a personal development project more than anything).

    The real excitement of this system is in the random encounters and how they sometimes start falling like dominoes into each other and you end up with a single spawn triggering a chain of 5 or 6 more spawns within a turn. All of those are also strictly controlled, but very random, and the level is host to the encounters.
     
  6. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    It shouldn't be an issue then using a tree that is checking against the grid to see if the space is occupied. After that, all you are doing is instantiating from a pool of room prefabs.
     
  7. thoughtcrime

    thoughtcrime

    Joined:
    Dec 19, 2014
    Posts:
    6
    Ok, thanks. I'll research in those areas. I just kept coming up blank when searching for resources to generate the "room" (4x4) at the point of discovery, not preloading them all with the scene.
     
  8. thoughtcrime

    thoughtcrime

    Joined:
    Dec 19, 2014
    Posts:
    6
    I thought of another way to do this, maybe you could tell me which would be better... Create all of the tileset prefabs in a grid with -y values (under the visible world), cover 0y with a black plane, and when a player enters an EdgeTile, select a prefab and move it to (playerTile.center-16), 0, playerTile.z so it's now visible above the fog of war plane at 0y.

    All possible tilesets could be instantiated On Start(), but only need to be moved adjacent to a unit when selected. Basically, it would be similar to having a 3d spritesheet hidden under the game world to cherry pick from as each tileset is randomly chosen from the remaining prefabs.

    What do you think about that vs the object pool?
     
    Last edited: Dec 19, 2014
  9. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    It could work, but it's typically best to stay away from instantiating a ton of objects for tiles. They can work fine for small cases, but it's not the best practice. The best solution realistically is involving mesh modification, but at this point don't worry about it too much. Just keep it simple and learn from it.