Map Storage Suggestions

So I’ve lurked around here for a while, but the time has come for me to reveal myself and ask a question.

I’m currently developing a game with a 2D tiled map, similart to the 2D Final Fantasy games and their ilk. I plan on handling tiled maps through instances of a Map class.

Here’s a very simple overview of how I plan to handle the Map class:

    private Image tileSet = null;
    private Image[][] tiles = null;
    private int[][] solidTiles;
    private int[][] eventTiles;
    private int[][] mapGroundLayer;
    private int[][] mapWallLayer;
    private int[][] mapOverheadLayer; 

So each Map class has its own tileSet PNG and a 2D matrix of Images that serve as tiles for the map.

I plan to use five 2D integer matrices to handle collision, event activation, and draw order:

  • The solidTiles matrix is filled with 1s and 0s, where a 1 is a solid tile
  • The eventTiles matrix is filled with 0s, 1s, 2s, and 3s, where 0 is a tile with no attached event, 1 is a tile that activates an event upon entering the tile, 2 is a tile that activates an event when the player “interacts” with the tile, and 3 is a tile that has both “enter” and “interact” events.
  • The map____Layer matrices will be filled with integers that each correspond to an entry in the tiles[][] Image matrix. The draw hierarchy is mapGroundLayer<mapWallLayer<entities on screen such as NPCs and the player<mapOverheadLayer

Now we get to my actual question: how should I store these matrices? I’d rather not define a subclass of Map for every single map in my game; the only reasonable alternative I see is a text file, but that could get very cumbersome very fast, especially if I have maps with hundreds of tiles.

So! If you have suggestions, let me hear them! And if you think my way of handling maps is ass-backwards for some reason, then tell me that, too! I’ve never tried to make a game before, and I’m quite sure that my inexperience shows.

Thanks!

[Edited to explain the map____Layer matrices a bit better.]
[Edit 2: Obviously my Map class will have more variables/methods than what I put here; I just showed these because they’re relevant to my question.]

Consider either Mappy or Tiled.

They’re both pretty ok. I usually end up writing something custom or using my own ( Analog ) but they’re a good starting point.

I use tiled, and set it to save as a csv. Then I take all the csv layers and put it in my own map file format, because I don’t need all the extra features tiled provides.

Solid tiles are only going to be 0s and 1s? I pretty sure that a boolean type would be more efficient than integers.

I created a tiled map game a while ago, and I used a rather interesting method maybe you can make it work for you.

What I did is saved the map as an image. Mind you my maps only had to deal with height, and terrain type, so this was fairly simple. The general idea is that each tile represents one pixel on the image. The color range based on the tile’s properties. For example, with my map height was represented in the pixel color as the alpha value. Then the terrain type (grass, water, sand, etc…) was represented as the actual color of the pixel. Then just make a loader that reads each pixel and builds the map accordingly. You can actually store quite a bit of information this way. RGB can easily represent at least 3 different attributes, then add in alpha, and you could even make each digit of each RGBA value represent different things, so without getting complicated you could save up to 12 attributes just in pixel color.

I hope this helps, and maybe sparks some ideas for you.

zngga that’s very interesting indeed, what about save and load times? How does it scale on bigger maps? What about the final weight of the save file compared to say, other ways of resolving this issue? :slight_smile:

Images based maps are ok for basic stuff (I’ve used them for 48-hour games before) but even though you can technically pack in a lot of information, I find that after about 7/8 different colours for different things it’s really hard to remember what does what. And after that it gets awkward to tell the different pixel types apart.

Might be worth a try just to get something up and running, but IMHO you’ll out grow it pretty quickly.

For me, with the largest of maps being a max of 999 x 999 tiles (each represented as a pixel) save and load times were about 2 - 3 seconds, but as it used the bufferedImage class you will get longer times as the size gets bigger. As I said before my max map size was 999 x 999 tiles, which translates to 999 x 999 pixel images (pretty large) not an ideal solution. One of the cool things about it though is you can open up paint and literally draw a map and your game will load it! Which was one of the reasons I did it this way. Compared to other methods, serialization obviously kicks-ass compared to this, txt files would be better as far as size is concerned. But I used this for the ability to draw maps, as I said earlier, it a very cool advantage over other methods.

I use Tiled for visual part of the map. Object layers I find too cumbersome to handle.
I have a second game internal tile class layer made of booleans. Thus, each tile can get several attributes, like WALL, FLOOR, and for events something like DOOR, for example. Integers allow 32 attributes, Long for up to 64.
The initial class setup can easily be derived from the Tiled map (tiles 1 to x are floor tiles, 9-12 walls, etc.), later on bits for OPEN_DOOR or CLOSED_DOOR are set dynamically.
So, I don’t need additional storage beside Tiled.