# Formulas

Formulas are most commonly used to calculate a change in a character or enemy's status. For example, determining the amount of damage an attack deals, or whether the attack hits or misses its target, or how much health a potion heals.

## Common Formulas

Refer to the Attributes page to understand the properties in the equations that follow.

The standard attributes such as strength, protection, and so on are molded together to compute the following properties for each character and enemy:

• Physical attack rating
• Ethereal attack rating
• Physical defense rating
• Ethereal defense rating

Ratings are generally computed by the following. First, we start with a base attribute of the actor, such as strength. Next, if the actor is wearing any equipment, the properties of that equipment are added. After that, the properties of any shards that are attached to the equipment are applied, which may be another addition or a percentage increase (e.g., +10 to rating or +15% to rating). Finally, any active status effects in battle are applied, such as a -25% reduction in physical attack power.

Defense and evade ratings are a little more complicated to compute, since an attack may target either the actor as a whole, or a specific point on the actor such as a leg or arm. Thus, the total number of defense and evade ratings is equal to the number of target points plus one, where the +1 is for the actor as a whole. Each point on the actor has a modifier for the actor's fortitude, protection, and evade attributes. This modifier is a percentage increase or decrease from the base stat, so for example an actor's torso may have a +25% boost to fortitude, but -50% to its evade attribue. Additionally, each piece of armor applies protection to a specific point (head, torso, arm, or leg). To compute the defense or evade rating of the actor as a whole, we simply use the average defense/evade ratings of all of the target points.

The formulas, therefore, for these ratings are as follows

• Physical attack = {strength} + {equipped weapon physical} +OR* {shard properties} * {active status effect}
• Ethereal attack = {vigor} + {equipped weapon ethereal} +OR* {shard properties} * {active status effect}
• Physical defense (per target point) = {fortitude} * {target point fortitude modifier} + {equipped armor physical} +OR* {shard properties} * {active status effect}
• Physical defense (actor) = average of physical defense for all target points
• Ethereal defense (per target point) = {protection} * {target point protection modifier} + {equipped armor physical} +OR* {shard properties} * {active status effect}
• Ethereal defense (actor) = average of ethereal defense for all target points
• Evade (per target point) = {evade} * {target point evade modifier} +OR* {shard properties of equipment} * {active status effect}

## Battle Formulas

Battle formulas are declared in the file src/modes/battle/battle_utils.h and are all available for use in Lua scripts. These formulas perform the most common calculations above that are needed during battles, but their use is entirely optional. For example, one could design a skill that always deals damage equal to 10% of an enemy's available hit points, in which case none of the damage calculation formulas would apply, though the evasion formulas still might.

NOTE: for now the comments and C++ declarations of the formulas are simply listed below.

```/** \name Command battle calculation functions
*** These functions perform many of the common calculations that are needed in battle such as determining
*** evasion and the amount of damage dealt. Lua functions that implement the effect of skills and items
*** make the most use of these functions. Thus, these functions are specifically designed for that use
*** and do not utilize C++ features that Lua can not take advantage of, such as references or default
*** values for function arguments.
***
*** There are also many functions that share the same name but have a different function signature. These
*** functions perform the same task but some take extra arguments to make the calculation more flexible. For
*** example, many functions have a version that allows adjustment of the variation by accepting a standard deviation
*** argument.
***
*** \note These calculations only work for valid non-party type targets. If it is desired to use these methods
*** on a party target, a set of targets for each actor in the target party must be extracted and those actor
*** targets used individually for these various methods.
**/
//@{
/** \brief Determines if a target has evaded an attack or other action
*** \param target A pointer to the target to calculate evasion for
*** \return True if the target evasion was successful
**/
bool CalculateStandardEvasion(BattleTarget* target);

/** \brief Determines if a target has evaded an attack or other action, utilizing an addition modifier
*** \param target A pointer to the target to calculate evasion for
*** \param add_eva A modifier value to be added/subtracted to the standard evasion rating
*** \return True if the target evasion was successful
***
*** The additional_evasion may be positive or negative. If the total evasion value falls below 0.0f
*** the function will return false and if that value exceeds 100.0f it will return true. Otherwise the total
*** evade value will serve as a standard probability distribution to determine whether the evasion was
*** successful or not.
**/

/** \brief Determines if a target has evaded an attack or other action, utilizing a multiplication modifier
*** \param target A pointer to the target to calculate evasion for
*** \param mul_eva A modifier value to be multiplied to the standard evasion rating
*** \return True if the target evasion was successful
***
*** This function operates the same as the CalculateStandardEvasion(...) functions with the exception that
*** its float argument is used as a multiple in the evasion calculation instead of an addition. So for instance
*** if the user wants the evasion chance to increase by 20%, 1.2f would be passed in for the multiple_evasion
*** argument. A decrease by 35% would need a value of 0.65f. Negative multiplier values will cause a warning to
*** be printed and the absolute value of the multiplier will be used.
**/
bool CalculateStandardEvasionMultiplier(BattleTarget* target, float mul_eva);

/** \brief Determines the amount of damage caused with a physical attack
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** This function uses the physical attack/defense ratings to calculate the total damage caused. This function
*** uses a gaussian random distribution with a standard deviation of ten percent to perform variation in the
*** damage caused. Therefore this function may return different values each time it is called with the same arguments.
*** If the amount of damage calculates out to zero, a small random non-zero value will be returned instead.
**/
uint32 CalculatePhysicalDamage(BattleActor* attacker, BattleTarget* target);

/** \brief Determines the amount of damage caused with a physical attack
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param std_dev The standard deviation to use in the gaussian distribution, where "0.075f" would represent 7.5% standard deviation
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** The std_dev value is always relative to the amount of absolute damage calculated prior to the gaussian randomization.
*** This means that you can -not- use this function to declare an absolute standard deviation, such as a value of 20 damage
*** points.
**/
uint32 CalculatePhysicalDamage(BattleActor* attacker, BattleTarget* target, float std_dev);

/** \brief Determines the amount of damage caused with a physical attack, utilizing an addition modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** The add_atk argument may be positive or negative. Large negative values can skew the damage calculation
*** and cause the damage dealt to drop to zero, so be cautious when setting this argument to a negative value.
**/

/** \brief Determines the amount of damage caused with a physical attack, utilizing an addition modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param std_dev The standard deviation to use in the gaussian distribution, where "0.075f" would represent 7.5% standard deviation
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
**/

/** \brief Determines the amount of damage caused with a physical attack, utilizing a mulitplication modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param mul_atk An additional amount to be multiplied to the physical damage dealt
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** This function operates the same as the CalculatePhysicalDamageAdder(...) functions with the exception that
*** its float argument is used as a multipler in the damage calculation instead of an integer addition modifier.
*** So for instance if the user wants the physical damage to decrease by 20% the value of mul_atk would
*** be 0.8f. If a negative multiplier value is passed to this function, its absoute value will be used and
*** a warning will be printed.
**/
uint32 CalculatePhysicalDamageMultiplier(BattleActor* attacker, BattleTarget* target, float mul_phys);

/** \brief Determines the amount of damage caused with a physical attack, utilizing a multiplication modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param mul_atk A modifier to be multiplied to the physical damage dealt
*** \param std_dev The standard deviation to use in the gaussian distribution, where "0.075f" would represent 7.5% standard deviation
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** This function signature allows the additional option of setting the standard deviation in the gaussian random value calculation.
**/
uint32 CalculatePhysicalDamageMultiplier(BattleActor* attacker, BattleTarget* target, float mul_atk, float std_dev);

/** \brief Determines the amount of damage caused with an ethereal attack
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** This function uses the ethereal attack/defense ratings to calculate the total damage caused. This function
*** uses a gaussian random distribution with a standard deviation of ten percent to perform variation in the
*** damage caused. Therefore this function may return different values each time it is called with the same arguments.
*** If the amount of damage calculates out to zero, a small random non-zero value will be returned instead.
**/
uint32 CalculateEtherealDamage(BattleActor* attacker, BattleTarget* target);

/** \brief Determines the amount of damage caused with an ethereal attack
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param std_dev The standard deviation to use in the gaussian distribution, where "0.075f" would represent 7.5% standard deviation
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** The std_dev value is always relative to the amount of absolute damage calculated prior to the gaussian randomization.
*** This means that you can -not- use this function to declare an absolute standard deviation, such as a value of 20 damage
*** points.
**/
uint32 CalculateEtherealDamage(BattleActor* attacker, BattleTarget* target, float std_dev);

/** \brief Determines the amount of damage caused with an ethereal attack, utilizing an addition modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** The add_atk argument may be positive or negative. Large negative values can skew the damage calculation
*** and cause the damage dealt to drop to zero, so be cautious when setting this argument to a negative value.
**/

/** \brief Determines the amount of damage caused with a physical attack, utilizing an addition modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param std_dev The standard deviation to use in the gaussian distribution, where "0.075f" would represent 7.5% standard deviation
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
**/

/** \brief Determines the amount of damage caused with a physical attack, utilizing a mulitplication modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param mul_phys An additional amount to be multiplied to the ethereal damage dealt
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** This function operates the same as the CalculateEtherealDamageAdder(...) functions with the exception that
*** its float argument is used as a multipler in the damage calculation instead of an integer addition modifier.
*** So for instance if the user wants the ethereal damage to decrease by 20% the value of mul_atk would
*** be 0.8f. If a negative multiplier value is passed to this function, its absoute value will be used and
*** a warning will be printed.
**/
uint32 CalculateEtherealDamageMultiplier(BattleActor* attacker, BattleTarget* target, float mul_atk);

/** \brief Determines the amount of damage caused with an ethereal attack, utilizing a multiplication modifier
*** \param attacker A pointer to the attacker who is causing the damage
*** \param target A pointer to the target that will be receiving the damage
*** \param mul_atk A modifier to be multiplied to the ethereal damage dealt
*** \param std_dev The standard deviation to use in the gaussian distribution, where "0.075f" would represent 7.5% standard deviation
*** \return The amount of damage dealt, which will always be a non-zero value unless there was an error
***
*** This function signature allows the additional option of setting the standard deviation in the gaussian random value calculation.
**/
uint32 CalculateEtherealDamageMultiplier(BattleActor* attacker, BattleTarget* target, float mul_atk, float std_dev);
//@}
```