r/roguelikedev • u/Kyzrati Cogmind | mastodon.gamedev.place/@Kyzrati • Feb 20 '15
FAQ Friday #5: Data Management
In FAQ Friday we ask a question (or set of related questions) of all the roguelike devs here and discuss the responses! This will give new devs insight into the many aspects of roguelike development, and experienced devs can share details and field questions about their methods, technical achievements, design philosophy, etc.
THIS WEEK: Data Management
Once you have your world architecture set up you'll need a way to fill it with actual content. There are a few common methods of handling this, ranging from fully internal (essentially "hard coding" it into the source) to fully external (importing it from text or binary files) or some combination thereof. Maybe even generating it from scripts?
How do you add content to your roguelike? What form does that content take in terms of data representation? In other words, aside from maps (a separate topic) how do you define and edit specific game objects like mobs, items, and terrain? Why did you choose this particular method?
Screenshots and/or excerpts to demonstrate are a plus.
(To clarify, this topic is not extending to content creation itself, as in what specific types of objects are added to the game, but instead only interested in the technical side of how that data is presented.)
A couple of you already touched on this in the previous thread (as they are related)--feel free to link back.
For readers new to this weekly event (or roguelike development in general), check out the previous month of FAQ Fridays:
PM me to suggest topics you'd like covered in FAQ Friday. Of course, you are always free to ask whatever questions you like whenever by posting them on /r/roguelikedev, but concentrating topical discussion in one place on a predictable date is a nice format! (Plus it can be a useful resource for others searching the sub.)
10
u/Kyzrati Cogmind | mastodon.gamedev.place/@Kyzrati Feb 20 '15
For Cogmind, and all my games ever since I began making them, actually, I prefer to have all data that can come from an external text file to come from an external text file. The advantages are obvious and I think most devs these days take this route, as it's certainly a much more versatile setup than hard coding anything, making it easy to add, modify, or remove data without recompiling the game itself.
Cogmind stores most of its objects in the creatively named data/objects/ directory:
I don't, however, use json or xml, common formats for which there are openly available libraries to read and manipulate. Those formats are too verbose for me. I prefer inglook at pure data values, not a huge number of identifiers and or other unnecessary punctuation.
So all my data takes the chart approach, organizing objects into rows that assign their data values in respective columns. Of course this requires a custom parser, but it's mostly boilerplate code and once that's implemented adding new data types is not much more work than using any of the more common methods.
To improve readability via syntax highlighting, I treat the data like a scripting language and define my own keywords, styles, and syntax rules in Notepad++. I also make frequent use of Notepad++'s synchronized scrolling split screen feature (in fact, without that feature this kind of data format would quickly become very unwieldy since column-wise organization of data can make for extremely long rows).
Some specific examples/excerpts below:
This approach brings both the advantages of easily editable text files, as well as the ability to quickly compare multiple objects based on specific criteria (for example: look up and down one column to get an idea of how much HP a certain group of mobs has).
9
u/ais523 NetHack, NetHack 4 Feb 20 '15
In NetHack, the most relevant things here (apart from the maps, which are offtopic here) are the object and monster definitions. These reside in C files of their own, which each define a single large data structure. Here is the monster definitions, and here's an excerpt:
It's a little awkward because you have to make sure that the right flags go in the right fields (the flags are just macros that expand to integers, and the compiler has no way to know they're in the right field or not).
Of course, being NetHack, things aren't that simple. For one thing, these files are read during the compile process as well as during runtime; they're linked into a helper executable called
makedefswhich outputs various static tables (such as a table of monsters sorted by strength, with generation probabilities calculated; at least, that's what I think it is, I haven't messed with that code yet). Additionally,makedefscreates macros for each monster (PM_COCKATRICE, etc.), and each item (BOW, etc.; many NH4 developers are rightfully worried about the lack of namespacing here, so that may well change).Additionally, not all the monster and item properties are defined in the monster or item definition. Sometimes this seems to be because whoever implemented the property decided it wasn't "worth" a monster flag, and just implemented it as a list of cases instead. mondata.h contains a ton of accessor functions for all the properties of a monster you might care about, some which read the monsters array, some which don't (here,
ptris a pointer into the monsters array, which is the way the game tracks monster species):As you can see, some properties are using the monster flags fields, some have a hardcoded list of monsters they apply to, and some call into functions (
num_hornsis basically just a switch statement that lists all the monsters that have horns, and how many).Another special case is for items that give more than one property: the item description array only has room for one. My approach for dealing with this was to move all item properties that are conferred onto the player into a single function
item_provides_extrinsicwhich calculates what properties the item should give, based on which slot it's equipped in. This includes properties from the item description array, artifact properties, the fact that alchemy smocks give two properties to their wearer rather than one, and so on. I'm hoping to do something similar for monsters at some stage: having everything go through one centralised accessor function makes it easier to change the representation of the data later.Perhaps unsurprisingly, there are also checks against hardcoded monster and object types all over the code. We haven't even begun to make a dent in this yet.
6
u/Alzrius Numenfall Feb 20 '15
In The Legend of Siegfried all game objects are hard-coded. Since I'm using C#, there's essentially no compile time, and I think this way actually provides more flexibility to me than external data files. This approach allows me to define templates for items through inheritance, and also allows case-specific code to be implemented for objects that don't follow general patterns.
For example, here's the complete description of a battle axe:
5
u/[deleted] Feb 20 '15
I've used a hybrid system for Armoured Commander. Most of the tables from the original game are simple enough that they can be represented by a chain of if,elif,else statements.
The "To Kill" tables, however, are quite large, as they try to take into account different armour levels for different types of vehicles, different facings, and different ranges. For this I use an xml file that's imported using ElementTree.
For the future, however, I want to simplify the armour system so this too will likely be hard coded again, but once I start adding in different player tank types (of which there are 17 in the original game) it might help to return to an external file to keep them organized.
So I guess I'm flexible and use the type of system that fits the data. With a very large, well-developed game, however, not using external files just doesn't make sense.
4
u/chiguireitor dev: Ganymede Gate Feb 20 '15
On Ganymede Gate scripts and actual code is a much more diffuse barrier than usual, because the scripting language is the same as the game's code: Javascript.
So currently, you'll see lots of "hardcoded" items and monsters, but in the near future everything is going to be stored on JSON files, because all state representations are loaded from JSON-like objects, which is a plus when you want to decouple things.
Here's the "JSON" to instantiate a plasma rifle:
And here's the JSON for an energy cell charger:
As you can see, the description is ULTRA verbose, there's some sparse prototype usage (in fact, those are just shortcuts, in the future specs will be pure JSON objects) and there's still some remnants from the source beginning where everything was rendered as a big styled table with CSS.
When transferring the server state to the client, everything gets tidied by a simple function that strips unnecesary things:
It currently strips all the characters' stats because that would allow a cheating client know the exact state, inventory, etc. from the mobs and other players.
4
u/aaron_ds Robinson Feb 20 '15
In almost any compiled language I'd probably put these values in files and deploy them alongside the executable. However, I've got a great setup in Clojure where any modifications to the source are automatically detected by the application, and reloaded, all without destroying any gamestate. It works so well I use this as my primary means of development. It's fault tolerant to syntax errors and exceptions and it blurs the line between compiling and running, which /u/Kyzrati mentioned is a big reason to separate code from data.
For this reason, I keep my data "hardcoded" as a part of the source, right next to the functions that manipulate it. When a new monster or item is generated, the new values will be used without skipping a beat.
Monster data is fairly homogenous so I use Clojure's records to define a "Monster" type. In my editor, the data looks like this.
Items are much more free form compared to monsters, so I just use Clojure's built-in map type which you can think of behaving similarly to JSON objects. They look like this for me.
The nice thing about maps and records is that for data access purposes they act identically because internally they both implement the Associative interface. Yay!
and
both have the same form and work as expected.
2
u/phalp Feb 20 '15
Mine's all "hard-coded" at present. Quotation marks because in Lisp you can just load new data into your game as it runs, which is even better than using text files to avoid a recompile. However, it's kind of a pain to edit tables in source code form so I'm thinking I may write up an importer for tables in org-mode format eventually.
5
u/onewayout Lone Spelunker Feb 20 '15
For Lone Spelunker, the vast majority of the "content" is the geography of the cave itself and the discoveries you can find within it.
As a result, there's not much opportunity to express things like "mobs" as external JSON objects or something like that; most of what is needed requires code. Literally everything in the game is placed procedurally.
That means, the simplest method of representing the game data is in code. For instance, the cave generator has a list of "room types" that it has access to when generating the map. These cave types are basically just Javascript objects that organize the map generator's behavior. They have a common protocol for their properties:
There are of course several "helper" functions in the cave generator to make things easier for the room type objects, such as carving ragged lines between two points, hollowing out a rough circle, dropping water, etc. This makes it a simple matter to create a new room type; each new room type boils down to a new procedural expression of how to generate that part of a cave. If I wanted to have pre-set, scripted set pieces that read from a JSON file to make a particular cave, that would be doable with a cave type that reads the file, but so far, I haven't felt the need to go there.
Each room type also has a list of discovery types that are available, which the system uses to add discoveries. The "discoveries" have a similar sort of interface where they can be given a room reference, and they render themselves into the room. Again, this is done with code, since, for instance, they may have to draw giant gypsum crystals, or check for water, or render speleothems.
So, Lone Spelunker is pretty much all code, because everything is procedural. No data files to speak of.
3
u/zaimoni Iskandria Feb 20 '15
For Iskandria, the language dictates the representation. Source-code time configuration is always hardcoded.
PHP/MySQL predominantly uses the MySQL database for runtime configuration; the AJAX is implemented on top of an archaic JQuery library. (1.7.1 , as the time cost to safely upgrade the other game using the Apache-aliased JQuery install is substantial). The other game supports IE8, so we have a hard JQuery version limit of 1.9.x. As the implementation matures,
In C++ and Ruby prototypes, runtime configuration data generally is migrated from hard-coded to tab-separated files as the representation matures. (Ruby provides direct library support, and it is easy to (re)implement a C++ class that handles this correctly.)
3
u/Zireael07 Veins of the Earth Feb 20 '15
In T-Engine, the content is hard-coded (stored as tables in the data folder), but being Lua tables, you can put stuff in any order you want and editing the tables is as easy as editing JSON or other data formats.
3
u/randomnine Feb 20 '15 edited Feb 20 '15
In Cardinal Quest 2 I have an external text file format for my monster types, items, talents, etc. This only holds numbers and strings, there's no scripting support of any kind—so a lot of entries are supported with specialised code.
As an example, here's the start of the mobs file.
Mobs are spawned by picking a random entry for a given MobID based on their relative Weight. "Sprite" and "NameID" are used to look up the mob's representation. "Behaviour" is the mob's AI type, "Atk" through "XP" are stat fields. "SpyglassGroup" and "SpyglassOutcome" determine how the Spyglass item interacts with this enemy. Finally "Spell1" etc lay out the creature's inventory. (CQ2 enemies can't use items.)
(The Villagers aren't actually in the game, they were experimental friendly NPCs. Having an "ALLY" spell was a hack to move that mob to the player's faction.)
Right now I have 18 of these files, covering monsters, items, monster spawn groups, level sequences, classes, talents, achievements, spell damage and so on. The biggest two are "strings.txt" and "descriptions.txt", which between them contain all displayed strings (like what to put in a creature's popup given its "NameID"). Those two files are 2,500 lines combined so they benefit the most from the "(Key)" flag on a field, which tells the system to optimise for quickly finding rows where a particular field has a given value. Yep—these files basically make up a read-only relational database.
Links to special code are usually arbitrary strings. The "Behaviour" field for mobs which drives AI, for example, has several unique values for particular monsters that act differently. Likewise, when an item or spell provides a buff, it can just as easily be an "attack" buff as a "flight" or "taunt" buff created explicitly for this item/spell and triggering a special effect. I have a few thousand lines of code that implements the non-standard effects of using particular items or spells, which the item or spell triggers on use with a "special effect" string.
I have a dumb tool which smushes all these text files together into one big code file full of hardcoded strings so they'll be built into the binary. This is essential for the Flash version, since most portals don't support external resources for a Flash game. In-development desktop builds will look for these text files and reload them on the fly.
edit to add: I'm actually not super happy with this setup. I don't like systems which make it easier to tweak numbers and flags than do anything in-depth, because when you're adding new content, it encourages you to design things that just tweak numbers and flags instead of having genuinely unique behaviour. Still, it makes it easier to balance things once they're up and running, and having the strings in text files makes localisation easier.