jump to navigation

Thoughts on maps May 3, 2008

Posted by blueapple in Hydro, Programming.
trackback

When coding something like this (Hydro), early in development I’m not really interested in developing tools. I want to get the “thing” done (or atleast close too), not waste time making leveleditors and such. Therefore, I looked around for alternatives this time.

I found a few (alternatives); pre-made mapeditors (Mappy, TileStudio etc.), hand-coding maps in notepad or using a RAD language (C#, Python) to create a mapeditor.

Using existing mapeditors has quite a few advantages, they work, they have a neat GUI and allows one to do some advanced stuff (animations for example). But they also requires you to learn how to use them and how to import/export the map. I guess there are ways to make them export maps “the way you want” but, again, I just didn’t feel like messing around with it.

And hand-coding is well..exhausting. You can’t really make anything more advanced than a few blocks, and depending on the map format, even basic stuff like that can be hard to create.

Speaking of map formats, I’ve used a few myself and had to choose one to use in this project. As this game is somewhat tilebased (each particle has an x and y value), the map file should contain their positions, types and perhaps some additional data (Temperature and pressure for example).

An easy way is to just print out a 2D array to a file, but when it needs to contain more data than just a type (tile id) things start to become big. Example:

# Tile types
00 00 01 00 01 00 00
00 00 00 00 00 00 00
00 02 00 00 00 02 00
00 00 02 02 02 00 00
00 00 03 03 03 00 00
03 02 03 03 03 02 03
02 00 03 00 02 03 00

# Pressure
00 00 00 00 01 00 00
00 00 00 00 03 00 00
00 13 00 00 05 13 00
00 00 13 13 13 00 00
00 00 20 20 17 00 00
20 13 20 20 21 13 20
13 00 20 00 30 20 00

The amount of matrices needed are dependent on how much data each tile will contain (one for type, one for pressure, one for temperature etc). As all the “empty” tiles are included too, this grows big very fast.

The format I’ve chosen to use (this far at least) is a similar one, but for maps with more “empty space” it requires less data. For example:

# X Y TYPE TEMP PRESSURE
52 75 3 37 0
52 76 3 38 1
53 75 5 29 5
...

The first two numbers tells the X and Y coordinate, the third the type and the other tell additional data. Of course these files will also require much space when containing a lot of tiles. (One thing that could keep the filesize down would be by defining “blocks” of tiles, (X Y W H). 20 20 10 15 would draw a 10 tiles high and 15 tiles wide block at (20, 20). This however requires a custom made mapeditor).

And as I said, I didn’t feel like writing one, so what I did was converting a image file into my map format (which of course is not a new idea). For this, I used (C++ and) SDL and MS Paint.

The idea is to read in a .bmp file, loop through each pixel and check what color it is. If a colored pixel (not white), it’ll save it in a temporary array (it could just output to the file directly) and then output it using my format. Additional data, such as temperature and pressure could be coded by using one of the three color values, for example, [3, 14, x] could be water where x is the temperature.

I’m attaching a simple converter which you can have a look at, it’s probably not optimized nor well-written, but it should give you the basic idea. (You can of course use any API that can read a image file, you’re not restricted to SDL).

Download it here.

To load the map, you’d use something like this:

bool loadLevel(std::string file)
{
    std::ifstream infile(file.c_str());
    if(infile.is_open())
    {
       // Load the map
       while(!infile.eof())
       {
           int x, y, type;

           infile >> x;
           infile >> y;
           infile >> type;

           map[x][y].type = (E_TILE)type;
       }

       infile.close();

       return true;
     }

     printf("Could not load %s!\n", file.c_str());
     return false;
}

I’m not sure why it refuses to use a monospaced font for that code.

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: