Archive library

Started by
7 comments, last by swiftcoder 15 years, 5 months ago
Hi, I'm making a roguelike game, where I randomly generate my levels. I want to only keep one map in memory at a time, so I want to be able to write my levels to disk. I want to write each map to a separate file, since I figure that would make it easier when I want to load up a specific map, but I don't want my game spreading all these map files on the player's computer. So, I have need of an archive library. I want to be able to create a zip file or something that can be my save file. I want to be able to create files inside this archive, write my maps to these files, and be able to load maps from any of these files inside the archive. I haven't been able to find any kind of archive library, so I was wondering if anyone here knew about one. All the archive-libraries I've seen are only able to read from and sometimes write to existing archives, but not create new archives. I also need this library to work on Windows and Mac, as well as having a fairly unrestrictive license.
Advertisement
Quote:Original post by Gerbert
Hi, I'm making a roguelike game, where I randomly generate my levels. I want to only keep one map in memory at a time, so I want to be able to write my levels to disk. I want to write each map to a separate file, since I figure that would make it easier when I want to load up a specific map, but I don't want my game spreading all these map files on the player's computer.
This isn't directly related to your question, but isn't one of the major selling points of randomly generated levels that you don't actually have to write them to disk? AFAIK, most rogue-likes with procedurally generated levels generate them on demand, and thus never have to store them. Furthermore, if you use a seeded algorithm, then you can generate the exact save levels at any time by providing the same seed value, thus removing another reason to write to disk.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I think I'd still need to store levels on disk if I want persistence (and allow the player to revisit past levels). I could store the seed, but then I'd have to keep track of stuff like the player leaving equipment around and the current locations of monsters. Plus, I'd have to run the level creation program for a level every time the player enters that level, which may be expensive for levels the player revisits frequently (overworld, home town, etc.). I think it'd be easier to store on disk.

As for archive libraries, the only one I've found so far is libzip. I'm unsure how mature it is though, and how good Windows support is.
Quote:Original post by Gerbert

All the archive-libraries I've seen are only able to read from and sometimes write to existing archives, but not create new archives. I also need this library to work on Windows and Mac, as well as having a fairly unrestrictive license.


A library capable of writing is known as File System. The reason most libraries don't provide this functionality is because it's a surprisingly hard problem, and has been studied extensively by OS makers.

The by far simplest solution to this is to define maximum size on disk a level can be (for example 7kb). Then, in your file, allocate n 7kb chunks, each of them having a flag whether it's used or not and the size of contents.

To write, find first unused chunk. They are located every (7kb + sizeof(flag) + sizeof(chunk_size)). If you find it, write your level data there. If not, append to file.

This will leave you with fair amount of unused space, and requires you to know maximum level size. But trying to support anything beyond that, and you have effectively entered the realm of file systems and all the problems related to that.
*sigh*

I wanted to avoid writing my own save format since I didn't want to deal with headers and indexes and things like that. I just wanted something like PhysicsFS, which seems to even be able to write inside of existing archives but is not able to create new archives.

Do you suppose it's worth using an XML file instead of a binary for my saves? I suppose each map would be a sub-tree, and most XML libraries know how to pull sub-trees out a document.
Have a look at archive-libraries at this page. It sounds to me like you are looking for something like zlib.
There are plenty of options like
PhysFS or wrap your own .zip file reader using ZLib
The problem with zlib is that it only compresses individual files. I want to create several files and have them reside in a single archive, which would look like a single folder to a user.

As I've said, the problem with PhysFS is that, while it is able to read and apparently write to existing archives, it provides no way to create new archives.
Quote:Original post by Gerbert
The problem with zlib is that it only compresses individual files. I want to create several files and have them reside in a single archive, which would look like a single folder to a user.
Why not just put all the files in a single directory? Unless your users are prone to mess about with random files in their user-data directories, I don't see it being that large of a problem.

Almost no archive software enables writing, so either you will have to make all your levels take a fixed amount of space, in which case you can do simple block allocation, or you will have to implement a full filesystem (there are a couple of virtual filesystems which support compression, that might be adapted).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement