Suggestion: A centralized place for game rules

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Post Reply
Message
Author
jbarcz1
Creative Contributor
Posts: 226
Joined: Thu Jun 26, 2003 4:33 pm
Location: Baltimore, MD

Suggestion: A centralized place for game rules

#1 Post by jbarcz1 »

When we get to implementing the game logic for 0.1, and future revisions, there are a lot of constants that will need to be defined someplace.

For example: Initial population for new colonies, production numbers, ratios for starlane/non-starlane movement, etc etc

I've already got one of them, the initial colony population, as a constant declared in order.cpp, but I really dont want it to stay there.
These "rule-constants" would be cumbersome and difficult to change if we had their definitions scattered all over the code. Therefore, I propose we create a singleton class to centralize all of the rules in a single location.

The idea is that whenever you need one of these 'rule constants', you add it to the rules file, and update the rule manager class, adding accessors and data members for the constant. This way, all the rules are centralized in an external location, they're easy to change, and we're all getting at them the same way.

I've already specced out a class definition:

Code: Select all

/**
* GameRuleSet is a singleton class to manage all of the various
* game parameters which should be constant during a game, but which
* we may wish to change or tweak.  This includes things like
* default population levels in a colony, production numbers, 
* default ship speeds, and the like.  Any constant values pertaining
* to the rules of the game should be accessed through this class
*
*/
class GameRuleSet
{
public:
    /// Instance method -- this guy's a singleton
    static GameRuleSet& instance();
    
    /// Method to read game rules from a rule file. 
    /**
    * This method will read the specified rule file and obtain data
    * for all of the game rule constants.
    *  The method should throw an std::runtime_error if any problems occur
    */
    void LoadRuleFile(const std::string& filename);

    /// Returns the default initial population level for a new colony
    int InitialColonyPop() const;

private:

    /// Constructor - initializes all members
    /**
    * Initializes all member variables to some meaningless default value
    * (like 0 or NULL).  LoadRuleFile() should be called at game-start
    * to read actual values from a configuration file.
    */
    GameRuleSet();

    /// default initial population for a new colony (amount of ppl carried on colony ship)
    int m_initial_pop;
    
    /*
        Add any other rule related constants that we need.  Candidates are
        default planet population caps, default ship speeds, offroad vs starlane ratio
        etc etc etc
    */

};
[/code]
Empire Team Lead

Blade Runner
Space Squid
Posts: 53
Joined: Fri Sep 05, 2003 8:47 pm

#2 Post by Blade Runner »

I like to suggest a different place for the game constans.
You use Phyton, like a script language. Phyton is easier to understand for a beginner, like a game designer, so maybe better, if you put all this important game constans in a script. This way you have a big advantage: the designers can modify them and instantly try the new values without any programmer support (and valuable time) and one needn't compile again and again, when the designers try to find the right balance for the game.
-------------------------------------------
Te vagy a Blade Runner. :)

tzlaine
Programming Lead Emeritus
Posts: 1092
Joined: Thu Jun 26, 2003 1:33 pm

#3 Post by tzlaine »

Blade Runner wrote:I like to suggest a different place for the game constans.
You use Phyton, like a script language. Phyton is easier to understand for a beginner, like a game designer, so maybe better, if you put all this important game constans in a script. This way you have a big advantage: the designers can modify them and instantly try the new values without any programmer support (and valuable time) and one needn't compile again and again, when the designers try to find the right balance for the game.
Or, instead of adding support for an entire other language, we could just keep these constants in a config file. Text files are easier for designers to work with than any language. Fortunately, there's a function that indicates this approach in Josh's example code:

Code: Select all

void LoadRuleFile(const std::string& filename);

tzlaine
Programming Lead Emeritus
Posts: 1092
Joined: Thu Jun 26, 2003 1:33 pm

#4 Post by tzlaine »

As for the overall concept, I'm in favor of centralizing the rules and having a means of serializing them to and from config files. But I think a singleton might be a bit too burdensome to use. How about having a namespace called RuleConst, so we can get RuleConst::foo, RuleConst::Bar, etc? We can have a save and a load function in the namespace, since we only need to load at game startup anyway. This roughly mirrors the usage of the ClientUI constants for UI colors, etc., and that seems pretty usable so far.

jbarcz1
Creative Contributor
Posts: 226
Joined: Thu Jun 26, 2003 4:33 pm
Location: Baltimore, MD

#5 Post by jbarcz1 »

That would work too. The general idea is that we want something that's centrally located and globally accessible.
Empire Team Lead

Post Reply