|
|
View previous topic - View next topic |
Author |
Message |
Bjorn Demon Hunter
Joined: 29 May 2002 Posts: 1425 Location: Germany
|
Posted: Thu Apr 15, 2004 11:45 am Post subject: Using XML for saving map data |
[quote] |
|
Some of you hanging around the #indie-rpg channel already know I've been working on saving my maps in XML format. I really like the flexibility of XML and the fact that it is text based and easily understandable on looking at it. And this being the first time I ever used XML in an application, it was a good learning experience.
I'll give an example of what my map format currently looks like:
Code: | <?xml version="1.0"?>
<map version="4.0">
<layer width="85" height="33">
<tile name="buiten.052"/>
<tile name="buiten.052"/>
<tile name="buiten.052"/>
<tile name="buiten.052"/>
...
<tile name="buiten.052"/>
<tile name="buiten.052"/>
<tile name="buiten.052" obstacle="0100"/>
<tile name="tuin.072" obstacle="0001"/>
<tile name="tuin.073"/>
<tile name="tuin.074"/>
</layer>
<object x="1152" y="408" type="Schommel"/>
<object x="384" y="504" type="Tipi"/>
<object x="1080" y="192" type="Dakondersteuning"/>
<object x="1776" y="768" type="Vouwwagen"/>
<object x="984" y="504" type="Kippenhok"/>
<object x="1968" y="552" type="Boom4"/>
<object x="1896" y="384" type="Boom2"/>
<object x="2016" y="360" type="Boom2"/>
<object x="264" y="120" type="Boom"/>
</map> |
Each layer identifies its size, and contains width*height tiles, for each row all columns sequentially. The tile name consists of its tile bitmap name and index number. In addition to layers, maps can contain objects. A location and type is currently all I store, but given the ease with which XML can be extended, I'm thinking about optionally storing additional properties depending on the object type in the future.
However nice it might seem, I've come upon two down sides of storing your data this way. A small one is the dependency upon another library to easily handle your XML data (I used libxml2). The bigger downside is the size of the data. Our largest map (the sewers map from Blues Brothers RPG) grew from 56 kb binary data to 1 MB XML data. However while the binary version zipped to 25 kb, the XML version zipped to 8 kb. So if you're just interested in the extensibility of XML, but not in having it plain text, you'll have nothing to worry about in size. I think libxml2 will even allow you to save/load XML compressed automatically.
I was wondering if any other developers here were also using XML to store their data or parts of it in XML, and what their experiences with this are. Or maybe they are not, and for what reason?
|
|
Back to top |
|
|
LeoDraco Demon Hunter
Joined: 24 Jun 2003 Posts: 584 Location: Riverside, South Cali
|
Posted: Thu Apr 15, 2004 5:58 pm Post subject: |
[quote] |
|
You could always describe in the layer tag a "base" attribute, which is the normal base tile of map. (I.e. the predominant tile.) Then each tile definition would just store an offset from the corner of the map, and/or the index/name schema you're using now. Sure, for really detailed maps (ones in which there is not a definable "base"), you wouldn't save much space, but it might cut down some of your map sizes. _________________ "...LeoDraco is a pompus git..." -- Mandrake
|
|
Back to top |
|
|
Bjorn Demon Hunter
Joined: 29 May 2002 Posts: 1425 Location: Germany
|
Posted: Fri Apr 16, 2004 12:04 pm Post subject: |
[quote] |
|
Yeah, the offset idea certainly decreases the size of the big sewers map. Most of our other maps use the space rather well though, so it would make things worse in those cases.
I have been thinking about first storing the names of the tile bitmaps, giving them numbers, and then referring to those numbers instead of using their name all the time. That would decrease the size of the raw data a bit, though it would probably increase the size of the zipped version. :-)
I might introduce the base attribute on the layer element, and having it be the default tile for tiles without a name. Then I'd need to do <tile name=""/> for real empty tiles though. And I'd need to figure out what the dominant tile is.
At the moment, I think neither of these optimizations are really worth it. They save space at the cost of complexity, and just zipping up the data will be the best size reduction you can get already.
|
|
Back to top |
|
|
ThousandKnives Wandering Minstrel
Joined: 17 May 2003 Posts: 147 Location: Boston
|
Posted: Fri Apr 16, 2004 10:42 pm Post subject: |
[quote] |
|
I use XML files for map data. In fact, for almost all data. But I find the idea of using XML for storing map grids kind of goofy. I use a binary file for that. Using XML is a fairly needless repetition of a large amount of data. The more different tiles start appearing on a map the less the zip compression helps. Same with the "default tile" value - it is of little use once your maps get complicated and have a large number of different tiles.
The primary benefit for XML I find is for storing and editing "structured" data like that for items, spells, enemies, objects, and scripts (I don't like having any such game data hard-coded into my program). Every piece of data for these items has a default value for every tag so that a child only has to be included when the value is non-default. So maps consist of 2 parallel files, one for the grid data and one for pretty much all other information (map name, default background music & graphic effects, objects, scripts, etc).
As for needing an XML library, I don't know about that. I wrote my own DOM XML parser in a few hours. Its extremely easy (at least in C). I could have spend as much time fumbling around trying to find an XML library and learn how to use it as I did just writing my own and knowing inherently how to use it. Obviously it has no XSL support, etc. -but I don't see any need for that much functionality.
|
|
Back to top |
|
|
Bjorn Demon Hunter
Joined: 29 May 2002 Posts: 1425 Location: Germany
|
Posted: Sat Apr 17, 2004 2:06 am Post subject: |
[quote] |
|
Ok, thanks a lot for your input! I agree that the tile data is rather a big bunch of data and thinking about it, it would have been good to use a tight binary format for the tile block, while using XML for additional properties of the map and information about the objects present on the map. The only downside that I can think of is that you can't have two people edit the tiles of a map at the same time, but then the need for this hasn't ever come up yet. :-)
libxml2 was a very quick and easy way to start saving and loading XML, faster and more reliable than anything I could have done myself, and with more features for anything I might want to do in the future.
|
|
Back to top |
|
|
janus Mage
Joined: 29 Jun 2002 Posts: 464 Location: Issaquah, WA
|
Posted: Sat Apr 17, 2004 10:24 am Post subject: |
[quote] |
|
You should look at how Ika represents its maps. It's far more elegant than using plain XML like you're doing - still human-readable and editable in notepad, but much more compact and easier to load. It's all open source too, so it should be easy to learn from.
|
|
Back to top |
|
|
Bjorn Demon Hunter
Joined: 29 May 2002 Posts: 1425 Location: Germany
|
Posted: Sat Apr 17, 2004 6:10 pm Post subject: |
[quote] |
|
Their setup looks like Scheme or Lisp, and it's nice, but in my opinion there is no big difference with using XML, and I'd actually prefer XML because it's a standard, its widespread use and the many libs and tools available for it.
The one interesting thing they do, is storing the raw map tile data in a compressed text-based format (not editable by hand though) and embedded into the map file. I like this approach above storing it as a seperate file, and it could be done this way with XML too.
An example of what I looked at:
Code: | (ika-map
(version 1.0)
(information
(title )
(meta
(entityLayer B2)
(music winter.ogg)
)
)
(header
(dimensions
(width 320)
(height 240)
)
(tileset snowy.vsp)
)
...
(layers
(layer
(label B0)
(dimensions
(width 20)
(height 15)
)
...
(data
(format zlib)
eJy0kckKwkAMhqfvf+v00PbgAi7gAi5gK9gKVR+iL+Bz
GCGBn5+4HPTwkWmm8yWZ6UMI/Z/ZJCFshUpohFbXzE7Y
J995WvBVjrPVPc+JHuOuNOBE6jd9HqAXm8981QufOW3v
CL6T5muKuG4hh3Vr5Qa+TtZRSIVcI/Mpf6b+UnVm8E8J
56KTL+B/9HVULzo+zvM8Xn9WM9e6z++BMBRGFMfk5Puz
mUqqOxGmwoziXPcz7cHrz+bAu18IS2FFcU39oe/ivFep
tb13j07tK/nsnN0fzl7A2QzyEWqg75c8AAAA//8=
)
(obstructions
(format tile)
eJxiYBgFxAIAAAAA//8=
)
(entities
)
)
... |
So I like the embedding of compressed data, encoded potentially the same way email attachments are encoded, so that it'll work embedded in a text file.
|
|
Back to top |
|
|
ThousandKnives Wandering Minstrel
Joined: 17 May 2003 Posts: 147 Location: Boston
|
Posted: Sat Apr 17, 2004 7:25 pm Post subject: |
[quote] |
|
That is certainly do-able. The problem with embedding your data in your XML/etc file, is that is that your data isn't really in binary that way, and you lose certain values, like those for the ASCII characters < and >. That is, you may intend it as binary data but it is still considered ASCII by your XML parser. So certain numeric value have to be eliminated. Obviously with a custom file parser you could store the length of the data as a property and ignore such characters in that portion of the file, but now your files have lost their XML compliance. So you would have to sacrifice those values and figure out ways to handle them in order to keep them out of your files.
|
|
Back to top |
|
|
DrunkenCoder Demon Hunter
Joined: 29 May 2002 Posts: 559
|
Posted: Sun Apr 18, 2004 11:29 am Post subject: |
[quote] |
|
Or you could just use Base64 encoding like many other applications like e-mail does. _________________ If there's life after death there is no death, if there's no death we never live. | ENTP
|
|
Back to top |
|
|
white_door Icemonkey
Joined: 30 May 2002 Posts: 243 Location: New Zealand
|
Posted: Sun Apr 18, 2004 9:08 pm Post subject: |
[quote] |
|
Repeating massive amount of tile data is not ever going to be a problem if you are going to be compressing your xml files. Although I don't use xml for my map files, I do use a very similar text format. (I should really convert it to xml sometime) Like yourself, I use full text names for each tile. And in fact, I found that I ended up with smaller map sizes, than I did when I was using a binary format. (both compressed) The fact is my text format seemed to compress better. And since I store the files compressed.. and only decompress when the map loads.. the files stay tiny.
Okay you might lose some of the human readablity of it, due to the fact you have a ton of tiles to read past... but there are still other advantages to using a xml format or something like it, the primary one being flexibility, another being debugging.
Its much easier to expand or change a xml format, and 90% of the time you can still read your old maps with the same loader, this means you don't have to rush in to converting all your maps everytime there is a change, and if something goes wrong in the new format.. you don't have to then convert them back.
Okay you could do some of that with a good binary format, but you would end up with something like a binary form of xml anyway. And for sure using a binary format makes debuging a map file loaders or savers harder, you can't just go and read a map file to see what it is outputing or loading.
Anyway its just my opinion on the matter, but I think you should stick to your first format aboce.
|
|
Back to top |
|
|
Bjorn Demon Hunter
Joined: 29 May 2002 Posts: 1425 Location: Germany
|
Posted: Sun Apr 18, 2004 11:02 pm Post subject: |
[quote] |
|
white_door wrote: | Anyway its just my opinion on the matter, but I think you should stick to your first format above. |
Thanks for you opinion and indeed, for the moment I'll just be sticking with this because it works, it's small when compressed anyway, it does all I want, and because I should be spending my time on other stuff. :-)
About the embedding, I already made a reference to the way email attachments are encoded, didn't call is base64 yet though. I think these people above are storing a base64 encoded version of the zlibbed data. Escaping &, < and > isn't really a problem either, I'd even assume libxml2 will help me with that.
|
|
Back to top |
|
|
Mandrake elementry school minded asshole
Joined: 28 May 2002 Posts: 1341 Location: GNARR!
|
Posted: Tue Apr 20, 2004 10:20 pm Post subject: |
[quote] |
|
I've used this before....not a huge fna of how Ika does things. Mainly becuase it extends itself using Python rather than extending Python. Anyway- XML data is great. It compresses, it's human readable, and it's portable. I've got a short XML parser/generator I created awhile ago that works wonders.
Anyway- the nice thing about XML is you could also easily creat an XML compiler/compresser/encrypter for hiding game information....it's also very easy to patch an XML file or create a patch system (much like doom wad's) _________________ "Well, last time I flicked on a lighter, I'm pretty sure I didn't create a black hole."-
Xmark
http://pauljessup.com
|
|
Back to top |
|
|
omega_alpha Fluffy Bunny of Doom
Joined: 07 Jun 2003 Posts: 16 Location: California
|
Posted: Tue Apr 20, 2004 11:09 pm Post subject: |
[quote] |
|
XML is an excellent form for saving all kinds of program configuration data. I've used it for a plugin I wrote to save user configuration data as an alternative to the Windows directly. XML is highly portable and a basic parser is fairly simple to write. But I think its usability for game maps depends on the size and complexity of the map data to be saved. If the maps are large and complex than perhaps a simpler format could save room so that ultimately those who download your game can do so much faster.
|
|
Back to top |
|
|
|
Page 1 of 1 |
All times are GMT
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|