Implementation of damage calculation.
continuous-integration/drone/push Build was killed Details

This commit is contained in:
Deukhoofd 2019-11-05 14:31:54 +01:00
parent db2a577a85
commit 1848d7b617
8 changed files with 121 additions and 6 deletions

View File

@ -89,6 +89,8 @@ void CreatureLib::Battling::TurnHandler::HandleAttackForTarget(CreatureLib::Batt
return; return;
auto user = attack.GetUser(); auto user = attack.GetUser();
auto attackData = attack.GetAttack()->GetAttack(); auto attackData = attack.GetAttack()->GetAttack();
auto library = user->GetBattle()->GetLibrary();
auto dmgLibrary = library->GetDamageLibrary();
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++){ for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++){
if (user->IsFainted()){ if (user->IsFainted()){
break; break;
@ -98,9 +100,13 @@ void CreatureLib::Battling::TurnHandler::HandleAttackForTarget(CreatureLib::Batt
break; break;
} }
auto hit = targetData.GetHit(hitIndex); auto hit = targetData.GetHit(hitIndex);
//TODO: calculate data for hit
hit.SetEffectiveness(user->GetBattle()->GetLibrary()->GetTypeLibrary()->GetEffectiveness(hit.GetType(), target->GetTypes())); //HOOK: Change move type
hit.SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hit.GetType(), target->GetTypes()));
//TODO: Critical calculation
hit.SetCritical(false);
hit.SetBasePower(dmgLibrary->GetBasePower(&attack, target, hitIndex));
hit.SetDamage(dmgLibrary->GetDamage(&attack, target, hitIndex));
if (attackData->GetCategory() == Library::AttackCategory::Status){ if (attackData->GetCategory() == Library::AttackCategory::Status){
//HOOK: Status attack //HOOK: Status attack
@ -109,7 +115,7 @@ void CreatureLib::Battling::TurnHandler::HandleAttackForTarget(CreatureLib::Batt
auto damage = hit.GetDamage(); auto damage = hit.GetDamage();
if (damage > target->GetCurrentHealth()){ if (damage > target->GetCurrentHealth()){
damage = target->GetCurrentHealth(); damage = target->GetCurrentHealth();
hit.OverrideDamage(damage); hit.SetDamage(damage);
} }
if (damage > 0){ if (damage > 0){
target->Damage(damage, DamageSource::AttackDamage); target->Damage(damage, DamageSource::AttackDamage);

View File

@ -29,3 +29,12 @@ const CreatureLib::Library::ItemLibrary* CreatureLib::Battling::BattleLibrary::G
const CreatureLib::Library::AttackLibrary *CreatureLib::Battling::BattleLibrary::GetAttackLibrary() const { const CreatureLib::Library::AttackLibrary *CreatureLib::Battling::BattleLibrary::GetAttackLibrary() const {
return _staticLib->GetAttackLibrary(); return _staticLib->GetAttackLibrary();
} }
const CreatureLib::Library::TypeLibrary *CreatureLib::Battling::BattleLibrary::GetTypeLibrary() const {
return _staticLib->GetTypeLibrary();
}
const CreatureLib::Battling::DamageLibrary *CreatureLib::Battling::BattleLibrary::GetDamageLibrary() const {
return _damageLibrary;
}

View File

@ -3,11 +3,13 @@
#include "BattleStatCalculator.hpp" #include "BattleStatCalculator.hpp"
#include "../../Library/DataLibrary.hpp" #include "../../Library/DataLibrary.hpp"
#include "DamageLibrary.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class BattleLibrary { class BattleLibrary {
const Library::DataLibrary* _staticLib; const Library::DataLibrary* _staticLib;
BattleStatCalculator* _statCalculator; BattleStatCalculator* _statCalculator;
DamageLibrary* _damageLibrary;
public: public:
BattleLibrary(Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator); BattleLibrary(Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator);
~BattleLibrary(); ~BattleLibrary();
@ -19,6 +21,7 @@ namespace CreatureLib::Battling {
const Library::TypeLibrary* GetTypeLibrary() const; const Library::TypeLibrary* GetTypeLibrary() const;
const BattleStatCalculator* GetStatCalculator() const; const BattleStatCalculator* GetStatCalculator() const;
const DamageLibrary* GetDamageLibrary() const;
}; };
} }

View File

@ -0,0 +1,66 @@
#include "DamageLibrary.hpp"
using namespace CreatureLib::Battling;
int DamageLibrary::GetDamage(ExecutingAttack *attack, Creature *target, uint8_t hitIndex) const{
auto levelMod = static_cast<float>(2 * attack->GetUser()->GetLevel());
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
auto bp = hit.GetBasePower();
auto statMod = GetStatModifier(attack, target, hitIndex);
//HOOK: Modify stat modifier
int damage = static_cast<int>((((levelMod * static_cast<float>(bp) * statMod) / 50) + 2)
* GetDamageModifier(attack, target, hitIndex));
//HOOK: Override damage
return damage;
}
int DamageLibrary::GetBasePower(ExecutingAttack *attack, Creature *target, uint8_t hitIndex) const{
auto bp = attack->GetAttack()->GetAttack()->GetBasePower();
//HOOK: modify base power.
return bp;
}
float DamageLibrary::GetStatModifier(ExecutingAttack *attack, Creature *target, uint8_t hitIndex) const{
auto user = attack->GetUser();
//HOOK: allow overriding for which users stat we use.
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
Core::Statistic offensiveStat;
Core::Statistic defensiveStat;
if (attack->GetAttack()->GetAttack()->GetCategory() == Library::AttackCategory::Physical){
offensiveStat = Core::Statistic::PhysicalAttack;
defensiveStat = Core::Statistic::PhysicalDefense;
}
else{
offensiveStat = Core::Statistic::MagicalAttack;
defensiveStat = Core::Statistic::MagicalDefense;
}
auto bypassDefensive = hit.IsCritical() && target->GetStatBoost(defensiveStat) > 0;
//HOOK: allow bypassing defensive stat modifiers.
auto bypassOffensive = hit.IsCritical() && user->GetStatBoost(offensiveStat) < 0;
//HOOK: Allow bypassing offensive stat modifiers.
float offensiveValue;
float defensiveValue;
if (bypassOffensive){
offensiveValue = user->GetFlatStat(offensiveStat);
} else{
offensiveValue = user->GetBoostedStat(offensiveStat);
}
if (bypassDefensive){
defensiveValue = target->GetFlatStat(defensiveStat);
} else{
defensiveValue = target->GetBoostedStat(defensiveStat);
}
return offensiveValue / defensiveValue;
}
float DamageLibrary::GetDamageModifier(ExecutingAttack *attack, Creature *target, uint8_t hitIndex) const{
float mod = 1;
auto hit = attack->GetAttackDataForTarget(target).GetHit(hitIndex);
mod *= hit.GetEffectiveness();
//HOOK: Modify damage modifier.
return mod;
}

View File

@ -0,0 +1,19 @@
#ifndef CREATURELIB_DAMAGELIBRARY_HPP
#define CREATURELIB_DAMAGELIBRARY_HPP
#include "../Models/Creature.hpp"
#include "../Models/ExecutingAttack.hpp"
namespace CreatureLib::Battling{
class DamageLibrary {
public:
virtual int GetDamage(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
virtual int GetBasePower(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
virtual float GetStatModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
virtual float GetDamageModifier(ExecutingAttack* attack, Creature* target, uint8_t hitIndex) const;
};
}
#endif //CREATURELIB_DAMAGELIBRARY_HPP

View File

@ -1,3 +1,4 @@
#include <algorithm>
#include "Creature.hpp" #include "Creature.hpp"
#include "../Models/Battle.hpp" #include "../Models/Battle.hpp"
@ -84,6 +85,10 @@ uint32_t Battling::Creature::GetStatExperience(Core::Statistic stat) const {
return __StatExperience.GetStat(stat); return __StatExperience.GetStat(stat);
} }
int8_t Battling::Creature::GetStatBoost(Core::Statistic stat) const {
return _statBoost.GetStat(stat);
}
void Battling::Creature::RecalculateFlatStats() { void Battling::Creature::RecalculateFlatStats() {
this->_flatStats = this->_library->GetStatCalculator()->CalculateFlatStats(this); this->_flatStats = this->_library->GetStatCalculator()->CalculateFlatStats(this);
RecalculateBoostedStats(); RecalculateBoostedStats();
@ -129,3 +134,8 @@ const std::vector<uint8_t>& Battling::Creature::GetTypes() const {
//HOOK: override types. //HOOK: override types.
return this->__Variant->GetTypes(); return this->__Variant->GetTypes();
} }
bool Battling::Creature::HasType(uint8_t type) const {
auto t = GetTypes();
return std::find(t.begin(), t.end(), type) != t.end();
}

View File

@ -53,8 +53,9 @@ namespace CreatureLib::Battling{
Battle* GetBattle() const; Battle* GetBattle() const;
BattleSide* GetBattleSide() const; BattleSide* GetBattleSide() const;
bool IsFainted() const; [[nodiscard]] bool IsFainted() const;
const std::vector<uint8_t>& GetTypes() const; [[nodiscard]] const std::vector<uint8_t>& GetTypes() const;
[[nodiscard]] bool HasType(uint8_t type) const;
//region Stat APIs //region Stat APIs
@ -67,6 +68,7 @@ namespace CreatureLib::Battling{
[[nodiscard]] uint32_t GetBaseStat(Core::Statistic stat) const; [[nodiscard]] uint32_t GetBaseStat(Core::Statistic stat) const;
[[nodiscard]] uint32_t GetStatPotential(Core::Statistic stat) const; [[nodiscard]] uint32_t GetStatPotential(Core::Statistic stat) const;
[[nodiscard]] uint32_t GetStatExperience(Core::Statistic stat) const; [[nodiscard]] uint32_t GetStatExperience(Core::Statistic stat) const;
[[nodiscard]] int8_t GetStatBoost(Core::Statistic stat) const;
void RecalculateFlatStats(); void RecalculateFlatStats();
void RecalculateBoostedStats(); void RecalculateBoostedStats();
void RecalculateFlatStat(Core::Statistic); void RecalculateFlatStat(Core::Statistic);

View File

@ -27,7 +27,7 @@ namespace CreatureLib::Battling {
inline void SetCritical(bool value) {_critical = value;} inline void SetCritical(bool value) {_critical = value;}
inline void SetBasePower(uint8_t value) { _basePower = value; } inline void SetBasePower(uint8_t value) { _basePower = value; }
inline void SetEffectiveness(float value) {_effectiveness = value;} inline void SetEffectiveness(float value) {_effectiveness = value;}
inline void OverrideDamage(uint32_t value) {_damage = value;} inline void SetDamage(uint32_t value) { _damage = value;}
inline void SetType(uint8_t value) {_type = value;} inline void SetType(uint8_t value) {_type = value;}
}; };