Stats ~ RPG stats implementation: Current stats struct design - uchicago-cs/chiventure GitHub Wiki

This wiki page is devoted to documenting how the player stats team designs and implements the stats and effects struct. *** Note that it's still a work in progress and will be updated once there are new changes.

Version 1.0

Idea for our implementation comes from

Player Stats Implementation

The implementation is as follows

typedef struct stats {
    char *key; 
    stats_global_t *global;
    double val;
    double max;
    double modifier; 
    UT_hash_handle hh; 
} stats_t;

typedef struct stats stats_hash_t;
global
stats_global_t *global;

Note that the stats_global_t is a struct covered in version 1.1, where the pointer points to the global list of stats that will be available in the game.

Value and Max Any stat will have a base value, i.e.,
double val;

and a maximal value, i.e.,

double max;

For example, we could have a player with a health stat that has 50/100 as the player's health stat. The 50 here denotes the base value, and 100 denotes the max value. Keep in mind that the max value inside the stats struct differs from the max value from global stat struct. The max in stats struct specifies the current maximal value of a given stat, so it could vary based on the level or other factors, whereas the max value from global stat struct denotes the maximal value that any such stat could ever have, regardless of levels and player class.

Modifier
double modifier;

Going back to the example we had under the Value and Max. Let's say the cumulative health modifier entry is 1.2, and the player has health 50/100. The final calculation will give 50 * 1.2 = 60 as the final stat value.

Player effects Implementation

The implementation is as follows

typedef struct effects{
    char *key; 
    effects_global_t *global;
    stat_mod_t *stat_list;
    UT_hash_handle hh; 
} stat_effect_t;

typedef struct effects effects_hash_t;
Status
bool status;

indicates if an effect is on or off.

Stat_list
stat_mod *stat_list;

Because one effect could be affecting multiple stats, we used the UT list struct to keep track of the connections between stats and effects. Further explanation could be found below.

Linking an effect to multiple stats

The implementation is as follows

typedef struct stat_mod {
    stats_t *stat;
    double modifier;
    int duration;
    struct stat_mod *next;
} stat_mod_t;
Modifier
double modifier;

The way that the final value of a stat is calculated is through multiplying it with the modifier. Note the difference between modifier in stat struct and modifier in effect struct: the modifier in stat struct is used to calculate the final value of a stat, whereas modifier in effect struct is used for a specific effect on the stat. In other words, the stat modifier is cumulative in that it takes all of the modifiers from the effect struct, gets multiplied by each of the effect modifiers, and computes a cumulative modifier.

For example, let's say we two effects "stunned" and "poisoned". Each of them affects the player's health by a modifier of 0.8. The modifier will be passed into the stats struct's modifier entry, which will be 0.8 * 0.8 = 0.64. The final value of the health stat will be the base value times 0.64.

Duration
int duration;

The duration is supposed to indicate the turns that are left for the effect to be on, since the battle system wants a pokemon style game. Hence we have the duration as an int.

Version 1.1

After meeting with Borja, we want to introduce the global stats and effects struct, which ensures the devs will be able to specify the possible stats and effects with the maximal value they could have in the game. The struct has not been finalized but it's partially implemented.

The Global stats

The implementation is as follows

typedef struct stats_global {
    char *name;
    double max;
    UT_hash_handle hh; 
} stats_global_t;

typedef struct stats_global stats_global_hash_t;
Max
double max;

To disambiguate, the max in the global struct differs from the max in stat struct. See Max under Stats struct for more details.

Global effect
// GLOBAL EFFECTS STRUCT DEFINITION ----------------------------------------------------
 /* This struct represents the effects table that keeps track of all available effects
  * It contains:
  *      the name of the effect,
  *      which is also the key to the hashtable
  * */
typedef struct effects_global {
    char *name;
    UT_hash_handle hh; 
} effects_global_t;
typedef struct effects_global effects_global_hash_t;
⚠️ **GitHub.com Fallback** ⚠️