Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: About creature types and xml.  (Read 1004 times)

Carlos Gustavos

  • Bay Watcher
    • View Profile
About creature types and xml.
« on: January 10, 2012, 04:33:52 pm »

Some time ago I had a look at moving creature types to xml again like I've done for vehicles and items. One problem with creature types is that there are a lot more special cases than for items and vehicles, but an idea had been fermenting in my head that I thought would take care of that. It turned out my idea didn't really solve it but I got something that partially worked and I've been thinking I should post it here for comments. So some description of it and thoughts I can remember having about it:

What I have now is an xml file for creature types that contains information on how much money they should get. Elements can look like these:
Code: [Select]
<creaturetype idname="CREATURE_SCIENTIST_EMINENT">
        <money>20-60</money>
</creaturetype>
<creaturetype idname="CREATURE_GUARDDOG">
    <money>
        <compare_law>
            <law>LAW_ANIMALRESEARCH</law>
            <comparison>EQUAL</comparison>
            <comparison_value>2</comparison_value>
            <value>20-40</value>
        </compare_law>
        <value>0</value>
    </money>
</creaturetype>
The amount of money a creature type can have can either be written as just one value or an interval like for the scientist above or conditions can be given allowing different values depending on (so far only) the laws. Dogs in the example above would get between $20 and $40 if animal research law is L+ and nothing otherwise. It's possible to have more than one condition as well as using 'and' and 'or' operators.

In the code this works through a family of class templates that can build conditional trees. The root of the tree is a ConditionalBranch that can hold other conditionals. When asked for its value it will check if the branches test true or false and return the value of the first branch to test true, or its own value if all test false or it has no branches. Other conditionals are ConditionalAnd, ConditionalOr, ConditionalLaw and ConditionalRandom. And and Or can also hold other conditionals as logical operators. Law will test a value against a law in some manner. Random is random.
Some cut down code:
Spoiler (click to show/hide)
Full changes: http://www.2shared.com/file/_8_6_WMp/lcs.html

The problem that I didn't see this approach solving is when a creature's properties depend on its other properties. To do that the conditionals must have access to the creature's data and when a creature is created the properties would have to have their values assigned in the right order.
  Although because some time has passed since I wrote this I do have a new idea now on how to continue. But it would involve adding a template parameter to the conditionals for the creature, some kind of dynamic data type and could perhaps suffer circular dependancy so it's not a sure thing. Also though because some time has passed, I might not be remembering the real problem.

One bad thing with the current approach is that if two properties depend on the same conditions, then you have to write the same conditional tree twice. One time for each property. It gets more complicated if two properties depend on one, and the same, random outcome. Then you have to first have one of the properties depend on the random outcome and the other depend on what value the first ended up with.

I also think the structure of the conditional trees look a little odd compared to equivalent if-else structures. Eg.
Code: [Select]
if (a && (b || c)) //a,b,c,d are for the example not specified conditions.
   v = 1;
else if (c || d)
   v = 2;
else
   v = 3;
will look something (pseudo-)like
Code: [Select]
<v>
   <and>
      <a>
         [...]
      </a>
      <or>
         <b>
            [...]
         </b>
         <c>
            [...]
         </c>
      </or>
      <value>1</value>
   </and>
   <or>
      <c>
         [...]
      </c>
      <d>
         [...]
      </d>
      <value>2</value>
   </or>
   <value>3</value>
</v>
Maybe it'd look better if and and or were called conjunction and disjunction instead.

Another thought I've had when working on this is that it might be skirting so close to scripting languages that it'd be better to just use one of those instead. I have looked a little at luabind and boost::python but I haven't really been able to tell how easy it would be to use them instead.

As I've been writing this, I have been considering to just have one comparison class template instead of one each for laws, sites and whatmore, and put the differences between laws, sites and other things into the operands of the comparison. It might also work better for creature properties.

If you look through the code you may also notice I added a little piece of logging. My thought there was that it would be nice if the user was alerted when the game couldn't make sense of something in an xml file. It was just supposed to be something small and simple but then I got stuck trying to make it the best thing ever so I abandoned it to focus on the conditionals that was the real thing I intended to work on.
Logged