persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
BattleCalcs.cpp
Go to the documentation of this file.
1#include "BattleCalcs.hpp"
2#include "core/logger.hpp"
3#include <aegis/ndsTypes.hpp>
4#include <fpm/math.hpp>
5
7{
8 ae::q20_12_t base = attacker.calculateBaseDamage(defender, skill);
9 ae::q20_12_t range = ae::q20_12_t{95} + ae::q20_12_t{(rand() % 11)};
10 return std::clamp(
11 static_cast<u32>(fpm::trunc(MathManager::getInstance().div(base * ae::q20_12_t{range}, ae::q20_12_t{100}))),
12 (u32)1,
13 (u32)99999);
14}
15
17{
18 MathManager& math = MathManager::getInstance();
19
20 BattleStats* attackerStatsPtr = attacker.getBattleStats();
21 BattleStats* defenderStatsPtr = defender.getBattleStats();
22 if (!attackerStatsPtr || !defenderStatsPtr)
23 {
24 logMessage(LogSeverity::ERROR, "Called with null battleStats");
25 return false;
26 }
27
28 BattleStats& attackerStats = *attackerStatsPtr;
29 BattleStats& defenderStats = *defenderStatsPtr;
30
31 if (skill.hitRate == 100)
32 {
33 return true;
34 }
35
36 ae::q20_12_t baseAccuracy = math.div(ae::q20_12_t{attackerStats.ag + 200}, ae::q20_12_t{defenderStats.ag + 200});
37 ae::q20_12_t multipliedAccuracy;
39 {
40 ae::q20_12_t shoeMultiplier =
41 math.div(ae::q20_12_t{attackerStats.ag + 200},
42 math.div(ae::q20_12_t{defender.shoe.evasion}, ae::q20_12_t{2}) + ae::q20_12_t{200});
43 multipliedAccuracy = baseAccuracy * ae::q20_12_t{skill.hitRate} * shoeMultiplier;
44 }
45 else
46 {
47 multipliedAccuracy = baseAccuracy * ae::q20_12_t{skill.hitRate};
48 }
49
50 multipliedAccuracy = std::clamp(multipliedAccuracy, ae::q20_12_t{50}, ae::q20_12_t{99});
51 return multipliedAccuracy > ae::q20_12_t{rand() % 100};
52}
53
55{
56 BattleStats* battleStats = user.getBattleStats();
57 if (!battleStats)
58 {
59 logMessage(LogSeverity::ERROR, "Called with null battleStats");
60 return 0;
61 }
62
63 ae::q20_12_t teamMultiplier = user.getTeamMultiplier();
64
65 uint8_t magicBoost = BattleCalcs::getMagicBoostHeal(battleStats->ma);
66 ae::q20_12_t base = fpm::floor(ae::q20_12_t{skill.movePower + magicBoost} * teamMultiplier);
67 ae::q20_12_t range = ae::q20_12_t{95} + ae::q20_12_t{(rand() % 11)};
68
69 return static_cast<u32>(fpm::floor(MathManager::getInstance().div(base * range, ae::q20_12_t{100})));
70}
71
72u32 BattleCalcs::allOutAttack(Player& attacker, BattleParticipant& defender, u32 participantCount)
73{
74 MathManager& math = MathManager::getInstance();
75
76 ae::q20_12_t levelDifference = BattleCalcs::getLevelDifference(attacker.lv, defender.lv);
77 BattleStats& attackerStats = *attacker.getBattleStats();
78 BattleStats& defenderStats = *defender.getBattleStats();
79
80 ae::q20_12_t affinityMtp = BattleCalcs::getAffinityMtp(*defender.getBattleStats(), SkillDb::allOutAttack);
81
82 ae::q20_12_t base = fpm::trunc(
83 math.sqrt(math.div(ae::q20_12_t{(attacker.weapon.weaponPower / 2) * 15 * attackerStats.st},
84 ae::q20_12_t{defenderStats.en})) *
85 ae::q20_12_t{1.6} * (levelDifference * levelDifference) * affinityMtp * ae::q20_12_t{participantCount});
86
87 ae::q20_12_t range = ae::q20_12_t{95} + ae::q20_12_t{(rand() % 11)};
88 return static_cast<u32>(fpm::trunc(math.div(base * range, ae::q20_12_t{100})));
89}
90
91const ae::q20_12_t BattleCalcs::levelMultipliers[24] = {
92 ae::q20_12_t{0.5}, ae::q20_12_t{0.51}, ae::q20_12_t{0.53}, ae::q20_12_t{0.59}, ae::q20_12_t{0.66},
93 ae::q20_12_t{0.75}, ae::q20_12_t{0.84}, ae::q20_12_t{0.91}, ae::q20_12_t{0.97}, ae::q20_12_t{0.99},
94 ae::q20_12_t{1.0}, ae::q20_12_t{1.0}, ae::q20_12_t{1.0}, ae::q20_12_t{1.0}, ae::q20_12_t{1.01},
95 ae::q20_12_t{1.03}, ae::q20_12_t{1.09}, ae::q20_12_t{1.16}, ae::q20_12_t{1.25}, ae::q20_12_t{1.34},
96 ae::q20_12_t{1.41}, ae::q20_12_t{1.47}, ae::q20_12_t{1.49}, ae::q20_12_t{1.5}};
97
98const uint8_t BattleCalcs::magicBoostTableHeal[20] = {
99 0,
100 6,
101 12,
102 17,
103 22,
104 27,
105 34,
106 44,
107 54,
108 65,
109 75,
110 85,
111 93,
112 100,
113 105,
114 110,
115 115,
116 120,
117 125,
118 130
119};
120
121u32 BattleCalcs::getAtk(BattleStats& attackerStats, Skill& skill)
122{
123 return skill.skillRace == SkillRace::phys ? attackerStats.st : attackerStats.ma;
124}
125
126ae::q20_12_t BattleCalcs::getLevelDifference(u32 attackerLevel, u32 defenderLevel)
127{
128 s32 diff = attackerLevel - defenderLevel;
129 diff = std::clamp(diff, (s32)-13, (s32)10);
130
132 return levelMultipliers[diff + 13];
133}
134
135ae::q20_12_t BattleCalcs::getAffinityMtp(BattleStats& defenderStats, Skill& skill)
136{
137 u32 affinity = defenderStats.affinities[(u32)skill.element];
138
139 switch (affinity)
140 {
142 return ae::q20_12_t{1.25};
144 return ae::q20_12_t{0.5};
146 return ae::q20_12_t{0.0};
148 return ae::q20_12_t{-1.0};
150 return ae::q20_12_t{-2.0};
152 default:
153 return ae::q20_12_t{1.0};
154 }
155}
156
157uint8_t BattleCalcs::getMagicBoostHeal(uint8_t& magic)
158{
159 uint8_t index = (magic - 1) / 5;
160 index = std::clamp(index, (uint8_t)0, (uint8_t)19);
161 return magicBoostTableHeal[index];
162}
ae::q20_12_t sqrt(ae::q20_12_t v)
Computes the square root of a Q20.12 value.
ae::q20_12_t div(ae::q20_12_t num, ae::q20_12_t den)
Divides two Q20.12 values.
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.
static ae::q20_12_t getLevelDifference(u32 attackerLevel, u32 defenderLevel)
Gets a multiplier used for damage calcs based on an arbitrary hardcoded table.
static uint8_t getMagicBoostHeal(uint8_t &magic)
Calculate healing bonus based on an arbitrary hardcoded table.
static u32 healing(BattleParticipant &user, Skill &skill)
Calculates heal of a skill.
static u32 allOutAttack(Player &attacker, BattleParticipant &defender, u32 participantCount)
Calculates damage of an All out attack.
static const ae::q20_12_t levelMultipliers[24]
static ae::q20_12_t getAffinityMtp(BattleStats &battleStats, Skill &skill)
Calculate damage multiplier based on resistance & skill affinity.
static bool hit(BattleParticipant &attacker, BattleParticipant &defender, Skill &skill)
Calculates if a skill hit the target.
static const uint8_t magicBoostTableHeal[20]
static u32 attack(BattleParticipant &attacker, BattleParticipant &defender, Skill &skill)
Calculates damage of an attack.
static u32 getAtk(BattleStats &attackerStats, Skill &skill)
Gets correct stat for damage calculations based on skill type.
Participant of battle.
ParticipantType participantType
virtual ae::q20_12_t getTeamMultiplier()=0
virtual BattleStats * getBattleStats()=0
virtual ae::q20_12_t calculateBaseDamage(BattleParticipant &defender, Skill &skill)=0
Basic stats / affinities. Used by Personas or directly by Enemies.
Affinity affinities[11]
BattleStats * getBattleStats() override
u32 evasion
Definition Shoe.hpp:9
static Skill allOutAttack
Definition SkillDb.hpp:16
SkillRace skillRace
Definition Skill.hpp:17
u32 movePower
Definition Skill.hpp:15
Element element
Definition Skill.hpp:13
u32 hitRate
Definition Skill.hpp:14