Thread: Map System for Text Based RPG

  1. #1
    Registered User
    Join Date
    Aug 2020
    Posts
    5

    Arrow Map System for Text Based RPG

    Hello Guys,

    I am attempting to develop a map system for text based rpg I am working. I am wondering if anyone here might have some thoughts on how to implement it.

    I was thinking of having a class called rooms that was similar to the below.

    Code:
    enum EXIT_DIRECTION { NORTH, SOUTH...}
    
    Rooms
    ----------------------------------------------------------
    string _roomID (uniqueIdentifier or hash of desc)
    string _roomDesc ( a description of the room)
    List<Enemies> _enemies (a list of the enemies in the room)
    Inventory _roomInventory (a list of items in the room )
    Map<exitDirection, roomID> _exits ( a map exit directions of connecting rooms )
    Then I would have a DungeonMap class that would have a list of rooms and functions to allow the movement between rooms based on the presence of an exit from the current room. It would basically work like a tree structure that the player can traverse.

    Am I on the right path here or does this problem need a different type of solution.

    Lucas cage

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What you have looks workable, although List<Enemies> probably should be List<Enemy>, and might even be List<unique_ptr<Enemy>> if you're trying to do some fancy OO design for a hierarchy of enemies (but I'm not saying you should: if you don't need it, don't do it).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2020
    Posts
    5
    Hello LaserLight,

    Thank you for responding. Good catch on the List<Enemy>. I haven't worked that part out yet because that class hasn't been developed yet. I think I am going to code this out tonight and see how it goes.

    Lucas Cage

  4. #4
    Registered User
    Join Date
    Aug 2020
    Posts
    5
    Yeah I think this could work but, I am wondering if the exits map is going to be a source of frustration. When I attempt to initialize a DungeonMap and fill it with rooms I am not sure how that part will work. I suppose I could create a text file that gets processed defining the rooms and their relationships. Would I be better of simply creating a 2D array of room objects that the player object traverses.

    |X|R|R|X|X|
    |R|R|X|X|X|
    |R|X|R|R|R|
    |R|R|R|X|X|
    |R|X|X|X|X|

    This grid would be my 2D array or vector of vectors filled with R for roomObject or X for impassible. This seems a bit simpler to me.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to represent the rooms such that adjacent rooms are always accessible from each other, then certainly creating a grid would be a good idea. On the other hand, it could be that just because rooms are conceptually adjacent doesn't mean that they can be accessed from each other, or maybe it is possible to enter a room and find that you cannot return to the room from which you came. In that case, a list of exits per room would be better.

    Quote Originally Posted by lucas_cage
    When I attempt to initialize a DungeonMap and fill it with rooms I am not sure how that part will work.
    If you were storing pointers to rooms as the exits, then yes, that might be a little tricky because you might need a two-pass solution: first create the room objects, then create the exits with pointers to the room objects. But here you have assigned unique string identifiers to each room, so all you need to do is to map the identifiers to the room objects, and then store the identifiers as the exit values. You might even create a file (e.g., using JSON, YAML, TOML, etc) to represent the room data in human-readable form, then parse that file to generate the dungeon map.
    Last edited by laserlight; 08-13-2020 at 08:41 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2020
    Posts
    5
    That is something to consider. I guess it depends on the features I want to implement. I will think it over and update when I am complete. Thank you for the information.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by lucas_cage View Post
    Hello Guys,

    I am attempting to develop a map system for text based rpg I am working. I am wondering if anyone here might have some thoughts on how to implement it.

    I was thinking of having a class called rooms that was similar to the below.

    Code:
    enum EXIT_DIRECTION { NORTH, SOUTH...}
    
    Rooms
    ----------------------------------------------------------
    string _roomID (uniqueIdentifier or hash of desc)
    string _roomDesc ( a description of the room)
    List<Enemies> _enemies (a list of the enemies in the room)
    Inventory _roomInventory (a list of items in the room )
    Map<exitDirection, roomID> _exits ( a map exit directions of connecting rooms )
    Then I would have a DungeonMap class that would have a list of rooms and functions to allow the movement between rooms based on the presence of an exit from the current room. It would basically work like a tree structure that the player can traverse.

    Am I on the right path here or does this problem need a different type of solution.

    Lucas cage
    You seem to have taken the decision that your rooms are constrained to have exits to the north, south, east and west, and it's not possible to have a room with two north-facing exits, for example.

    So you need to take a further decision, are your rooms constrained to lie on a grid? Are they all of the same dimensions in layout space, and do they exclude each other?

    There's no right answer to this. If you don't make this requirement, then you are logically a bit more consistent. For example a cupboard within a bed chamber could be a "room". Wth the constraint on, the cupboard has to occupy a grid sqaure adjacent to the bedchamber. However, applying the constrain will simplify your design. You can hold the world in a 2d array, you can plot it out with pencil and paper. The layout space doesn't have to match story space, you can still have "a tiny cupboard" as one of your rooms.

    If you go for the constraint, then your natural data structure is a 2D array, with connections between the adjacent blacks stored. You might say that all connections are bidirectional but it's probably better to allow for unidirectional connections (e.g. a chute). So just 4 bits per square indicating whether there is a connection to the adjacent squares.

    If you don't go for the constraint, you have a graph, with up to four edges between nodes. The graph doesn't have to make any topological sense, so you could have things like car journeys coded in there between very distant nodes in story space. So you need a text file as input giving identifiers for all the rooms and connectiosn between them - you'll have to load it in with two passes to get the rooms, and then match up the identifiers.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  8. #8
    Registered User
    Join Date
    Aug 2020
    Posts
    5
    Yes, originally I laid this out on a piece of graph paper so the natural instinct was to represent it simply as a grid. However, I finally decided on a more flexible approach.

    Room:
    -------------------------------------------
    static int roomID (increment when a room object is created)
    int _roomID
    inventory *_roomInventory //inventory object that holds item objects that are in the room
    std::string _roomDescription // the details the player can read when entering the rooms
    std::list<exitIds> _exits // a list of possible rooms that can be reached from this room.

    I think this adds flexibility. If I decide the make rooms of different dimensions I still can. If I decide to have rooms with one-way connections, I can do that.

    My plan now is to ingest a document json or xml that contains the world data defining the rooms and the dungeon. I am at the point now where I am contemplating the value of the dungeon/map class. It appears that its only real function is to hold a list of room objects and reference them based on ID. I could alternatively add a list of room pointers to each room object and then create a tree of room pointers after all the rooms have instantiated. At this point I don't know that a class to define the whole dungeon is even needed.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lucas_cage
    static int roomID (increment when a room object is created)
    Maybe nextID would be a better name?

    Quote Originally Posted by lucas_cage
    int _roomID
    inventory *_roomInventory //inventory object that holds item objects that are in the room
    std::string _roomDescription // the details the player can read when entering the rooms
    I feel that given that these are member variables of a room class, prefixing them with "room" would just be "stutter", i.e., unnecessary repetition, e.g., you'll end up writing room._roomDescription instead of just room._description. (Having said that, I think a member variable named _ID will violate reserved name rules restricting names that begin with an underscore followed by an uppercase letter, but there's the argument that such name decoration is unnecessary, or it could be a suffix instead.)

    Quote Originally Posted by lucas_cage
    At this point I don't know that a class to define the whole dungeon is even needed.
    It might not be needed, but it might still be useful as an abstraction, e.g., if you want to pass around the entire dungeon to a function by reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by Malcolm McLean View Post
    You seem to have taken the decision that your rooms are constrained to have exits to the north, south, east and west, and it's not possible to have a room with two north-facing exits, for example.

    So you need to take a further decision, are your rooms constrained to lie on a grid? Are they all of the same dimensions in layout space, and do they exclude each other?

    There's no right answer to this. If you don't make this requirement, then you are logically a bit more consistent. For example a cupboard within a bed chamber could be a "room". Wth the constraint on, the cupboard has to occupy a grid sqaure adjacent to the bedchamber. However, applying the constrain will simplify your design. You can hold the world in a 2d array, you can plot it out with pencil and paper. The layout space doesn't have to match story space, you can still have "a tiny cupboard" as one of your rooms.

    If you go for the constraint, then your natural data structure is a 2D array, with connections between the adjacent blacks stored. You might say that all connections are bidirectional but it's probably better to allow for unidirectional connections (e.g. a chute). So just 4 bits per square indicating whether there is a connection to the adjacent squares.

    If you don't go for the constraint, you have a graph, with up to four edges between nodes. The graph doesn't have to make any topological sense, so you could have things like car journeys coded in there between very distant nodes in story space. So you need a text file as input giving identifiers for all the rooms and connectiosn between them - you'll have to load it in with two passes to get the rooms, and then match up the identifiers.
    thanks,I find this interesting,trying to make a dungeon,tiles based on 2d text arrays
    I also want functionality I seen in games,for example a town above ground should be possible to have a nodes to the different buildings you can go in and out and inside buildings,secret passages between buildings,so read in lots of 2D data and store in 3d array(x,y,level)?
    you tell me you can C,why dont you C your own bugs?

  11. #11
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Most Commonly Maps are stored arrays...

    Code:
    map = [
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ];
    I have used JSON and XML for levels in HTML5.
    It can be more efficient and much more capable.
    Last edited by Structure; 08-24-2020 at 09:16 AM.
    "without goto we would be wtf'd"

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Structure View Post
    Most Commonly Maps are stored arrays...

    Code:
    map = [
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    ];
    I have used JSON and XML for levels in HTML5.
    It can be more efficient and much more capable.
    How would a map like that have, for example, a room at (1,2) with an exit to the south that leads to the room at (7,8)? How would it name each room or list the inventory in each room?

  13. #13
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    Quote Originally Posted by christop View Post
    How would a map like that have, for example, a room at (1,2) with an exit to the south that leads to the room at (7,8)? How would it name each room or list the inventory in each room?
    create room class and other classes and simple fill in the map with pointers to array of room structs,readin data from a database or textfiles
    room(1,2).south=room(7,8);
    you tell me you can C,why dont you C your own bugs?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by I C everything
    create room class and other classes and simple fill in the map with pointers to array of room structs,readin data from a database or textfiles
    room(1,2).south=room(7,8);
    If you're doing that, then you're no longer working with an array with the given representation. Refer to the earlier posts for more information.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System Type : x64 based PC - Unbuntu 12.10 32bit || 64 bit?
    By Lesshardtofind in forum Tech Board
    Replies: 15
    Last Post: 12-14-2012, 02:58 AM
  2. Event based system design
    By EVOEx in forum Tech Board
    Replies: 4
    Last Post: 11-01-2012, 04:14 AM
  3. Smooth walking on tile based map system
    By abraham2119 in forum C Programming
    Replies: 8
    Last Post: 07-10-2009, 10:33 AM
  4. Turn Based Stradegy System Methods
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 07-30-2007, 10:45 PM
  5. Setting Up a sector based grid system
    By Auron in forum C Programming
    Replies: 6
    Last Post: 05-20-2005, 08:19 AM

Tags for this Thread