MV Implementing Pseudo-Roguelike Dungeon Levels Using Eventing Alone

Hi! This is a highly theoretical discussion (kind of a thought experiment) of how one might implement quasi-roguelike level design (random, different on each playthrough) using event code alone. (I wouldn't be shocked if someone had already tackled this topic and done a better job of it.)

I'm using RMMV but theoretically this should be possible using most versions of RPG Maker. NOT included: a way to enforce permadeath. For that I'm thinking you're going to need scripts.

Anyway, this is totally a case of me writing something out because if it turns out to be impossible, implausible, or impractical it will be much easier and less time consuming to figure that out "on paper" (or be told it by someone here smarter than me lol) than implementing it in the engine.

That said, some constraints are assumed here. One is that each map is the minimum map size allowable by your resolution (using the default resolution, this is 17x13). First, you create stock maps of set shapes and sizes. I think you could probably get away with just these fifteen templates:
  1. Straight Corridor Up/Down
  2. Straight Corridor Left/Right
  3. Four Way Intersection
  4. T-Intersection (Enter From Right)
  5. T-Intersection (Enter From Left)
  6. T-Intersection (Enter From Bottom)
  7. T-Intersection (Enter From Top)
  8. Corner, Down/Right
  9. Corner, Down/Left
  10. Corner, Up/Right
  11. Corner, Up/Left
  12. Dead-End, From Left.*
  13. Dead-End, From Right.*
  14. Dead-End, From Up.*
  15. Dead-End, From Down.*
The other thing you're going to need to do is create a lot of dummy maps (every map can, by my calculations, take the shape of any of 6 out of 15 of the above templates depending on the direction you're entering from, see below). The above templates incidentally can be reskinned almost infinitely, so you could create a random ice dungeon, a random lava dungeon, a random grass dungeon, a random high-tech dungeon, a random "flesh-cave" dungeon, and so on, or have one megadungeon that goes into and out of those zones (with some extra eventing not covered here).

If we wanted to get fancy and make things feel more roguelike-like we'd want to have a wide variety of different bespoke, fancy, individually non-random "entry vaults" (a la DCSS) but for the sake of argument, let's say we always start with a map that is Dead-End, From Down that always connects to a four-way intersection before any randomization happens.

Schrodinger's Maps
Now, each subsequent "level" map needs the following:
a) Its own variable, which I'll call MAP x SubID for now. Replace x with the Map ID in question.
b) To contain every possible map from the direction of entry (6, I think).

For instance, starting with our four way intersection example, we'd create Map IDs 003, 004, and 005, for going left, down, and right respectively and three variables, MAP 3 SubID, MAP 4 SubID, Map 5 SubID, that will be linked to those maps. Then inside those maps we would create every map could be arrived at from going in that direction. I.e.:
  • Going left can lead to 2, 3, 4, 8, 10, or 13.
  • Going down can lead to 1, 3, 7, 10, 11, or 14.
  • Going right can lead to 2, 3, 5, 9, 11, or 12.
So inside each map is six "sub maps" and which is ACTUALLY going to be the destination is determined the first time that you exit that side of the map.

The first time you went left at the first + intersection, you'd set MAP3 SubID to a random integer from 1 thru 6 (so essentialy, 1d6). This would determine if going left would lead you to a #2, a #3, a #4, #8, a #10, or a #13. That map's "SubID" would then be set, so that every time you used that "teleport" event, it would always lead to the same place, the same "version" of MAP3 and no longer be in a state of "quantum flux" (hence the reference to Schrodinger's Cat). Going right from that intersection would lead you either to a #2, #3, #5, #9, #11, or #12, and fix that as the destination of that path, and going down from that intersection would lead you either to a #1, #3, #7, #10, #11, or #14.

Problem I: Impossible Spaces
One immediate problem that arises--besides that your project is going to have at least six times the number of maps as it would if you weren't using this method, although in actuality your project only uses tons of clones of 15 different maps--is that these random destinations can result in impossible combinations. For instance, look at our first four way intersection. Say you go down from that into a #11. Whereas if you go left, you enter a #8. So far, so good. But the problem is, going down from that #8 (which is the only way to progress from an 8 having entered from the right), if you didn't "roll" a destination of 3, 7, or 10, you'd wind with in a room that doesn't connect to the #11 that should, on a grid, be immediately to the right of it. For instance if you "rolled" a 14 (crappy ascii map warning):


∩
Г +
U ⅃

See the problem? It's entirely possible for whatever map shape you enter going south to fail to "connect" to the map to the left and/or right of it in the way that it should.

I have a workaround for this, but it's 4% technical and 96% imaginative in nature. It is to keep the basic scheme above, but to make every one of the shapes above have you "exit" via a down staircase and "enter" via an up staircase. This results in equally impossible spaces, but their impossiblity isn't nearly as obvious when you add in abstracted verticality when each staircase moves you an indetermined vertical distance. For instance, maybe the room shaped like a backwards L leads to a #2 anyway, that #2 is just "BELOW"/"ABOVE" the #14 that "should" be occupying that space on an imaginary 2D grid. This may still lead to configurations that don't make sense with our understanding of Euclidean geometry, but if your setting is wild enough, you can have all kinds of fun with in-game justifications of why the dungeon works like this, from the proverbial shrug of "a wizard did it" that's available in any high fantasy setting, to elaborate quantum physics based technobabble in a scifi game to well, "Silent Hill making sense would defeat the purpose".

This method also supports having a DEPTH or "Z-Level" variable that consistently increments as you go downwards (and decrements as you go upwards) that would let you keep track of a player's total PROGRESS in descending (or you could flip descending/ascending if you were doing a "climb a tower" style roguelike as opposed to the standard "race to the bottom level" roguelike) through the dungeon and then you could use that variable as a benchmark/milestone to determine what level of monsters and treasure/gear are randomly generated (I assume if you want to do something roguelike-like you can figure out on your own how to randomize your treasure/encounters, it is much simpler and in any case not within the scope of this little tutorial/thought experiment). Even if you weren't to use this workaround, you're still probably going to want a "progress" variable equivalent to Depth that tracks how far the player is into the dungeon (basically, every time that a room gets randomly rolled and "set", this variable goes up by 1, and optionally goes back down when you retreat and back up when you advance again).

Problem II: Impossible Progress?
A problem I don't have a solution for is that it's very possible (if not quite likely) that this method will eventually result in a dead end in every direction and no way to progress. The only workaround for this I can think of is not ideal: remove dead ends/the possibility of dead ends entirely. But that a) requires your meta-map to expand infinitely in every direction and b) reduces the variety of what shapes a given map can lead to by 26%. Problem A can, I think, be worked around by having every direction start ending in dead ends once the DEPTH/Z-Level/Progress variable gets high enough, or even having four variables, Down, Left, Right, Up, tracking how many "screens" the player progresses in each direction. Problem B doesn't break the system, but it does exacerbate the "monotony" problem inherent to your game/dungeon consisting almost entirely of 15 endlessly cloned and occasionally reskinned map shapes/templates by reducing that number from 15 to just 11 and in general mazes without the occasional dead end are less interesting mazes. Esp. since dead ends are great places to put bosses/treasure.

Other Problems I Haven't Thought Of?
You tell me, gang! I'm definitely not a scripter, and my spatial awareness and understanding of mathematics and logic (in the mathematical/compsci sense) is I think a little below average. I have two decades of experience "coding" events in RM and have played many roguelikes (never beaten one, except for SotS: The Pit on Easy mode, which I don't count because it was on Easy) but that's where my qualifications begin and end. I could have entirely missed a glaring flaw in the above logic that makes even what I've written impossible to implement! I think this methodology/implementation is more practical for the occasional random dungeon in a game not entirely based on dungeon crawling as opposed to a traditional roguelike-like experience where the entire game takes place in a giant randomized megadungeon
I created a "grid randomizer" script as a proof-of-concept for randomizing dungeons on a project I was planning with @hadecynn years ago that fell apart, so I've actually spent a lot of time thinking through two of the issues you brought up, which are how to create dungeons that would always be "winnable" (without infinite breadth) and how to not create paths that could never be accessed by the player.

The solution I came up with, and scripted out in a few hundred lines of code, allows Ace to "instantly" come up with a winnable dungeon design and represent it as a visual grid of tiles. I've since expanded the same idea to create actual, walkable dungeons in GameMaker Studio 2 (didn't want to deal with object instantiation in RPGM), so I know that the idea works in practice and not just on paper. Here's a sample of randomly-generated layouts that my system created in Ace:
Random Path Generation.png

It will always create a successful path from the center-bottom tile to one of the top tiles (denoted with green dots). Blue squares and circles are walkable rooms (circles represent dead-end rooms), with lines (corridors or exits) connecting them. Red area is unwalkable.

I'm probably not going to release the code I used to make this, but I'm happy to offer the general approach that I used:

1) Create an array of arrays to represent the tiles (possible rooms) and their exits.
Each individual array within the larger array represents a single tile/potential room. Within each individual array, properties represent which exits the room has (e.g. the first value is "north exit available"), as well as whether it should be walkable (has at least one exit) and whether it's a special room (such as the start tile).

2) Create a single Critical Path from the Start Tile to any allowed Finish Tile.
Probably the hardest part to figure out, but actually not that hard to implement. Essentially, what the script does is pick a random direction to travel from the start tile, creating a new node (room) on the Critical Path. Then, it does a check to ensure that the path is still "winnable" given the presence of the new node (for example, if we were to go up, then all the way left, then down, it would no longer be winnable). To do this check, the script notes all tiles that can connect to the new node and that are not already on the Critical Path being created. Then, it notes all tiles that can connect to all of those nodes that aren't already on the Path, and so on.
  • If during any step an allowed Finish Tile (any tile in the top row) is found as a "possible connection", it means this node can create a winnable design, so it's OK to add the node to the Critical Path as a walkable room. The script sets the room's properties (using the array from Step 1) and then repeats Step 2, picking a random direction to travel from this new node (but will never pick a direction that would lead it back into an existing room).
  • If during any step there are no new possible connections found, it means that adding the node to the Critical Path would create something unwinnable - it would have to dead-end somewhere. Therefore, it's not OK to add the node to the Critical Path, and what the script does is it moves backward one node in the Path, then repeats Step 2 picking a different random direction to travel this time.
Once an allowed Finish Tile is actually added to the Critical Path, the Critical Path is complete (and winnable!). A playable, randomized design has been created, but it's completely linear. The next step allows us to create some loops, forks, and dead-ends.

3) Add additional complexity to the design beyond the Critical Path.
For each tile (room) in the Critical Path, the script checks which other exits could possibly be added to it (even if they would lead to dead ends or loops). Then, it does a random calculation for each possible exit to determine whether or not to add it (e.g. "If a random number 1-100 is less than 35, add a North Exit to this tile"). The only way an Exit would be impossible is if the tile is on an edge (for example, you can't add an East exit to the rightmost column of tiles).If an Exit is added that leads to a tile that's not already somewhere on the walkable path, that new tile is added to the walkable path (and the appropriate values are set for its array). Once the script does this for all tiles on the Critical Path, it will repeat this step for each new (non-Critical-Path) tile that was just added to the walkable path. Then, it will repeat it again for any new tiles added during this second run, and so on.

This third step will stop running once no new tiles are added to the walkable path during an entire round. At this point, a full, winnable, and hopefully-interesting random dungeon design has been made!

The only thing left to do at that point is to show it to the player (or actually create it). To show it to the player, what I did was create an event page for each possible look of the array which holds the data for that tile's "room" - so that if for example, the tile's room has a North Exit and an East Exit and is walkable (but not a special room), then the following page would have its conditions met and will automatically display the correct room, per standard RPG Maker event page logic (I recommend using Hime's Page Conditions script to avoid the need to event ridiculously long strings of If-Else branches to switch pages).
Event Page.png


Like I said, the logic and concept are a little bit mindbending, but as long as you properly organize the code into loops that can iterate through each "new" tile found or added for a path, it actually doesn't take a whole lot of work to code. It can be a gigantically useful process for designing certain types of games, and I've already used it in like three different contexts in my own games.

(This same process could also be evented from scratch with no code - the entire concept holds up and doesn't rely on any programming tricks - but it would take a gigantic amount more time and effort to do so with eventing and would probably require a lot of going back and changing things to accommodate different sizes/shapes of grids.)

Original thread: https://forums.rpgmakerweb.com/threads/implementing-pseudo-roguelike-dungeon-levels-using-eventing-alone.119157/