views:

75

answers:

4

I am a novice C coder who would like to write a role playing game resolution library. What I mean by this is that this program would only deal with resolving those conflicts which is piped into it. For example, when informed that Captain Amazing uses his Blasto eye beams at medium distance with his d8 Shooting Skill costing 3 Power Points, and his Wild Die, against Commandant Nefarious who is behind Medium Cover, it determines the result. It will not deal with either character, per se, only their relevant statistics, who the attacker and defender are and any modifiers relevant to this particular action of which it is informed.

The thing is that it will not have a built-in set of rules. Instead, it will be fed the rule set from a configuration file. This way, the same core engine could be used for a Savage Worlds virtual tabletop game, a turn based rogue-like or a real time D&D 3rd Edition 3d game. Different sets of rules for different types of CRPGs all using the same RPG library. My question is with regards to how best to do this.

For the last couple of days, I've been looking at using XML as a possible way to store the game data which would be needed. At first appearance, it looks like a good way to go since does not appear to be terribly difficult. On the other hand, since it would load the rules at run time, I cannot do XML data binding as that would convert it to C code, which would of course need to be compiled. That is how I understand it, at least.

To build on the example in the first paragraph, this is what the library would and would not need after the rules have been read in:

Library knows through the read in rules: How to resolve a Ranged Attack. What Skill is needed for a Ranged Attack (Shooting). What the base Target Number is. What the penalty to hit at Medium distance is. What the penalty to hit for the Defender being behind Medium Cover. How to deal with the Wild Die (if it is higher than another rolled die, it is used instead.) Aceing. (On most, but not all rolls, if the die hits its maximum score, it is rolled again, adding to the total. This continues until the die does not hit the maximum score.) What the values for the dice are.

What the library will need to be informed of: What the Attacker's Shooting Skill is. Distance. What the relevant situational modifiers are (Medium Distance, behind Medium Cover) Any character modifiers relevant to resolving the Shooting attack.

What the library does not care about in performing its task: Character names. Character skill levels in subjects not pertaining to resolving the current question. Which character is the player and which the non-player character.

Would XML be fine or should I approach this in a different way?

+1  A: 

One advantage of using XML for the communications and storage is that if you decide it gets too hairy coding it in C, you drop back to a language with more nearly 'built-in' support for XML.

I would not particularly associate C with XML. There are (many) libraries written in C or C++ that can manipulate XML, but the interfaces used mean that C is largely coincidental - you are working with the library code and library calls more than with just C. If you go down this route, you should aim to use one of the many established XML libraries rather than inventing your own - the same would be true in any other language.

Note that XML is a fairly heavy-weight language to process. I suspect that many gaming systems would find it too slow to process the relevant data.

Jonathan Leffler
I was reading about the speed issue. I'm somewhat torn. This wouldn't be a MMORPG engine, though I suppose nothing would prevent it as a possible use.As you mentioned libraries, I did look around for one and I installed libxml2 and looked through some of their documentation, what I could find anyhow.I'm wondering (since I have little experience) if I could write a basic universal rpg data system interally and have the XML mirror that. Of course, I also have no idea how to do this. Part of the fun, I guess.
cptnapalm
Grabbed the lrdf and will check it out. Thanks :)
cptnapalm
Not sure why I mentioned LRDF other than trying to be helpful. You might try Mini XML: http://www.minixml.org/
James Morris
+2  A: 

The thing is that it will not have a built-in set of rules.

You might be better off using XML to store data and hosting a scripting engine in your game and have the rules programmed as scripts external to your program. Lua is a good option for C programs, Python is also another choice.

ShaderOp
Both did occur to me. As a novice C programmer, I wanted to stick to what I'm learning and add as little extra as possible.From the descriptions I've read, Lua might be better as it is smaller and faster. If I were doing the whole thing in a scripting language, then Python would probably be what I'd go for.
cptnapalm
I do understand your wish to keep things as simple as possibgle. That's a great strategy regardless of skill level. But as you've mentioned already, converting the XML rules to C code at runtime is not trivial and a bad idea altogether. Using XML to write the rules will be equivalent to defining a rules language in XML and writing an interpreter for it in C. It can be done and has been done (search for Apache Ant as an example). But unless your rules are very simple, it would be easier in the long term to integrate a scripting engine.
ShaderOp
Fair enough. I think I might bite the bullet and get a Lua book.
cptnapalm
+1 for Lua. Lua is surprisingly easy to integrate with C, even if used only for loading configuration files. Once you have a taste, you'll find more uses too. There's quite an active and friendly user community you can find starting from www.lua.org, and naturally a lua tag here at SO as well.
RBerteig
A: 

In essence, I envision this sort of flow:

                                rule file
                                   |
UI --| pipe with action data |-- Resolution Engine --| pipe back to UI |--

I would suggest that XML is an ok rule-file language. Personally I think it's too much big iron unless one needs schema validation, and that JSON is a better choice to start off with. Neither JSON or XML really are a good map into C's execution model, but them's the breaks. You won't be parsing the rule file much anyway, so you can afford to take the time to do that with a parsing library.

For the action data pipe, I'd suggest using operating system pipes and writing binary data suitable for directly reading into a C structure.

Edit - If you're going to be using the rule file as a full-on scripting language instead of a configuration language, you want to embed another language eventually. Lua is the language choice du jour for game scripting. I don't know any reason off-hand not to choose Lua for this purpose.

BTW you have chosen a good starting project area I think.

Paul Nathan
+1  A: 

Civilization IV uses XML and Python for everything except the graphics engine and the AI. All the rules are Python scripts, all the units characteristics are XML files.

So the answer is I guess yes, it can be a good format.

Alexandre C.