In modern C++, how would you represent the exit directions in code?


#1

To remind you how it was done in ROM:

Interestingly tables.c does not hold any information regarding exit directions. I would have expected there to be a table that matches DIR_NORTH with a string north and so on.

I tried the enum class but didn’t like the resulting code because I was unable to use the enum class values as keys to std::map and other containers. It would make some sense to have a possibility to iterate over all exit directions and quickly fetch their respective string representation. It could also make sense to represent exit directions as single bits, so they could meaningfully be added together in order to indicate rooms with multiple exits.

DIR_NORTH 1
DIR_SOUTH 2
DIR_EAST 4
DIR_WEST 8

Here a room that has exits to the north, east and west would have its exits bitmap set to 13. It turns useful in procedural labyrinth generation as explained here:
http://weblog.jamisbuck.org/2010/12/27/maze-generation-recursive-backtracking

My current approach as an architect to a whole new MUD engine is to work around this problem by discarding the concept of hardcoded exit directions, making it theoretically possible to have any number of exits from a room in a way that each exit is represented by its own data structure. To get the list of neighboring rooms I would not iterate over a fixed set of exit directions but the container of references to exit data structures. Each of them could have a name such as north but is not limited to 6 directions even though I intend to only use the common 6 directions all the time everywhere.


#2

That ROM code dates back to a time when memory, disk, and cpu were orders of magnitude slower, less plentiful, and more expensive. Every byte used to count, and it was worth it to jump through hoops to save as many as possible, especially in a huge program like a MUD.

Now? Not so much. Write your code so it makes sense and is easy to maintain, and leave those enums alone.