CreatureLib/src/Battling/Flow/TurnHandler.cpp

154 lines
4.7 KiB
C++
Raw Normal View History

#include "TurnHandler.hpp"
2019-11-02 12:57:43 +00:00
#include "../Models/Battle.hpp"
2019-10-31 12:26:56 +00:00
#include "../../Core/Exceptions/NotImplementedException.hpp"
2019-11-09 11:15:45 +00:00
#include "../ScriptHandling/ScriptMacros.cpp"
using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(Battle* battle, ChoiceQueue* queue) {
2019-11-02 12:57:43 +00:00
//HOOK: On Before Turn hook for all choices
while (queue->HasNext()){
if (!battle->HasRecalledSlots()){
return;
}
auto item = queue->Dequeue();
2019-11-05 07:06:12 +00:00
ExecuteChoice(item);
delete item;
2019-10-31 12:13:36 +00:00
}
queue->HasCompletedQueue = true;
2019-10-31 12:13:36 +00:00
}
void TurnHandler::ExecuteChoice(const BaseTurnChoice *choice) {
2019-11-02 12:57:43 +00:00
if (choice == nullptr)
{
return;
}
auto choiceKind = choice->GetKind();
if (choiceKind == TurnChoiceKind::Pass) {
return;
}
auto user = choice->GetUser();
// If the user is fainted, we don't want to execute its choice.
if (user->IsFainted()){
return;
}
auto battle = user->GetBattle();
// If the user is not in the field, we don't want to execute its choice.
if (!battle->CreatureInField(user)){
return;
}
// If the choice is not valid, we don't want to execute it.
if (!battle->CanUse(choice)){
return;
}
switch (choiceKind){
case TurnChoiceKind::Pass: throw NotReachableException();
2019-10-31 12:13:36 +00:00
case TurnChoiceKind::Attack:
2019-11-05 07:06:12 +00:00
return ExecuteAttackChoice(dynamic_cast<const AttackTurnChoice*>(choice));
2019-10-31 12:13:36 +00:00
case TurnChoiceKind::Item:
case TurnChoiceKind::Switch:
case TurnChoiceKind::RunAway:
2019-10-31 12:26:56 +00:00
throw NotImplementedException();
2019-10-31 12:13:36 +00:00
}
}
2019-10-31 12:13:36 +00:00
void TurnHandler::ExecuteAttackChoice(const AttackTurnChoice *choice) {
2019-11-02 12:57:43 +00:00
//HOOK: Change attack
//HOOK: Prevent attack
2019-11-03 12:47:50 +00:00
auto attack = ExecutingAttack();
2019-10-31 12:13:36 +00:00
2019-11-03 12:47:50 +00:00
//HOOK: override targets
if (!choice->GetAttack()->TryUse(1)){
return;
}
//HOOK: check if attack fails
//HOOK: Check if attack stops after decreasing PP
//HOOK: On Before Attack
for (auto kv: attack.GetTargets()){
HandleAttackForTarget(attack, kv.first, kv.second);
}
2019-10-31 12:13:36 +00:00
}
void TurnHandler::HandleAttackForTarget(ExecutingAttack &attack, Creature *target, ExecutingAttack::TargetData &targetData) {
2019-11-09 11:55:48 +00:00
auto user = attack.GetUser();
std::array<ScriptSource*, 1> targetSources = {target};
std::array<ScriptSource*, 1> userSources = {user};
bool fail = false;
HOOK(FailIncomingAttack, targetSources, &attack, target, fail);
if (fail){
//TODO: Fail handling.
return;
}
2019-11-03 12:47:50 +00:00
2019-11-09 11:55:48 +00:00
bool invulnerable = fail;
HOOK(IsInvulnerable, targetSources, &attack, target, invulnerable);
if (invulnerable){
//TODO: We should probably do something when a target is invulnerable.
return;
}
2019-11-03 12:47:50 +00:00
if (!targetData.IsHit()){
2019-11-09 11:55:48 +00:00
HOOK(OnAttackMiss, targetSources, &attack, target);
2019-11-03 12:47:50 +00:00
return;
}
auto numHits = targetData.GetNumberOfHits();
if (numHits == 0)
return;
auto attackData = attack.GetAttack()->GetAttack();
2019-11-05 13:31:54 +00:00
auto library = user->GetBattle()->GetLibrary();
auto dmgLibrary = library->GetDamageLibrary();
2019-11-03 12:47:50 +00:00
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++){
if (user->IsFainted()){
break;
}
if (target->IsFainted()){
// STOP, STOP! HE'S ALREADY DEAD ;_;
break;
}
auto hit = targetData.GetHit(hitIndex);
2019-11-05 07:06:12 +00:00
2019-11-09 11:55:48 +00:00
auto hitType = hit.GetType();
HOOK(ChangeAttackType, targetSources, &attack, target, hitIndex, hitType);
hit.SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()));
hit.SetCritical(library->GetCriticalLibrary()->IsCritical(&attack, target, hitIndex));
2019-11-05 13:31:54 +00:00
hit.SetBasePower(dmgLibrary->GetBasePower(&attack, target, hitIndex));
hit.SetDamage(dmgLibrary->GetDamage(&attack, target, hitIndex));
2019-11-05 07:06:12 +00:00
2019-11-09 11:55:48 +00:00
std::array<ScriptSource*, 1> attackSource = {&attack};
2019-11-03 12:47:50 +00:00
if (attackData->GetCategory() == Library::AttackCategory::Status){
2019-11-09 11:55:48 +00:00
HOOK(OnStatusMove, attackSource, &attack, target, hitIndex);
2019-11-03 12:47:50 +00:00
}
else{
auto damage = hit.GetDamage();
if (damage > target->GetCurrentHealth()){
damage = target->GetCurrentHealth();
2019-11-05 13:31:54 +00:00
hit.SetDamage(damage);
2019-11-03 12:47:50 +00:00
}
if (damage > 0){
target->Damage(damage, DamageSource::AttackDamage);
2019-11-09 11:55:48 +00:00
bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSources, &attack, target, hitIndex, preventSecondary);
if (!preventSecondary){
//HOOK: On Move Hit
}
2019-11-03 12:47:50 +00:00
}
}
}
2019-11-09 11:55:48 +00:00
if (!user->IsFainted())
HOOK(OnAfterHits, userSources, &attack, target);
2019-11-03 12:47:50 +00:00
}
}