CreatureLib/src/Battling/Flow/TurnHandler.cpp

241 lines
9.0 KiB
C++

#include "TurnHandler.hpp"
#include <Arbutils/Assert.hpp>
#include <unordered_set>
#include "../../Library/Exceptions/NotImplementedException.hpp"
#include "../ScriptHandling/ScriptMacros.hpp"
#include "ResolveTarget.hpp"
using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue) {
AssertNotNull(queue)
for (auto choice : queue->GetInnerQueue()) {
HOOK(OnBeforeTurn, choice, choice.get());
}
while (queue->HasNext()) {
auto item = queue->Dequeue();
try {
ExecuteChoice(item.get());
} catch (const std::exception& e) {
THROW_CREATURE("Executing choice failed for choice by mon on side "
<< item.get()->GetUser()->GetBattleSide()->GetSideIndex() << " and index "
<< item.get()->GetUser()->GetBattleSide()->GetCreatureIndex(item->GetUser())
<< " with message '" << e.what() << "'.");
}
}
queue->HasCompletedQueue = true;
}
void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) {
AssertNotNull(choice)
auto choiceKind = choice->GetKind();
if (choiceKind == TurnChoiceKind::Pass) {
return;
}
auto user = choice->GetUser();
AssertNotNull(user)
if (user->GetBattle()->HasEnded())
return;
// If the user is fainted, we don't want to execute its choice.
if (user->IsFainted()) {
return;
}
auto battle = user->GetBattle();
AssertNotNull(battle)
// If the user is not in the field, we don't want to execute its choice.
if (!user->IsOnBattleField()) {
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();
case TurnChoiceKind::Attack: return ExecuteAttackChoice(choice.ForceAs<AttackTurnChoice>());
case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs<SwitchTurnChoice>());
case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs<FleeTurnChoice>());
case TurnChoiceKind::Item: throw NotImplementedException();
}
}
void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> choice) {
AssertNotNull(choice)
auto attackName = choice->GetAttack()->GetAttack()->GetName();
HOOK(ChangeAttack, choice, choice.GetRaw(), &attackName);
if (attackName != choice->GetAttack()->GetAttack()->GetName()) {
// TODO: Change attack
}
auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget());
ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets = TargetResolver::ResolveTargets(
choice->GetTarget(), choice->GetAttack()->GetAttack()->GetTarget(), choice->GetUser()->GetBattle());
auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
bool prevented = false;
HOOK_LOCAL(PreventAttack, attack, &attack, &prevented);
if (prevented) {
return;
}
// HOOK: override targets
if (!choice->GetAttack()->TryUse(1)) {
return;
}
// HOOK: check if attack fails
bool fail = false;
HOOK_LOCAL(FailAttack, attack, &attack, &fail);
if (fail) {
// TODO: Fail handling.
return;
}
bool stopBeforeAttack = false;
HOOK_LOCAL(StopBeforeAttack, attack, &attack, &stopBeforeAttack);
if (stopBeforeAttack) {
return;
}
HOOK_LOCAL(OnBeforeAttack, attack, &attack);
for (uint8_t i = 0; i < attack.GetTargetCount(); i++) {
HandleAttackForTarget(&attack, attack.GetTargets()[0]);
}
}
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target) {
auto user = attack->GetUser();
AssertNotNull(user)
AssertNotNull(target)
if (user->GetBattle()->HasEnded())
return;
auto targetSource = target;
auto userSource = attack;
bool fail = false;
HOOK(FailIncomingAttack, targetSource, attack, target.GetRaw(), &fail);
if (fail) {
// TODO: Fail handling.
return;
}
bool invulnerable = false;
HOOK(IsInvulnerable, targetSource, attack, target.GetRaw(), &invulnerable);
if (invulnerable) {
// TODO: We should probably do something when a target is invulnerable.
return;
}
auto numberOfHits = attack->GetNumberOfHits();
if (numberOfHits == 0) {
HOOK(OnAttackMiss, targetSource, attack, target.GetRaw());
return;
}
auto attackData = attack->GetAttack()->GetAttack();
auto library = user->GetBattle()->GetLibrary();
AssertNotNull(library)
auto& dmgLibrary = library->GetDamageLibrary();
auto hitIterator = attack->GetTargetIteratorBegin(target.GetRaw());
for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) {
if (user->GetBattle()->HasEnded())
return;
if (user->IsFainted()) {
break;
}
if (target->IsFainted()) {
break;
}
auto& hit = hitIterator[hitIndex];
uint8_t hitType = hit.GetType();
HOOK(ChangeAttackType, targetSource, attack, target.GetRaw(), hitIndex, &hitType);
hit.SetType(hitType);
auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes());
HOOK(ChangeEffectiveness, attack, attack, target.GetRaw(), hitIndex, &effectiveness)
hit.SetEffectiveness(effectiveness);
hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target.GetRaw(), hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target.GetRaw(), hitIndex, hit));
hit.SetDamage(dmgLibrary->GetDamage(attack, target.GetRaw(), hitIndex, hit));
if (attackData->GetCategory() == Library::AttackCategory::Status) {
if (attackData->HasSecondaryEffect()) {
auto& effect = attackData->GetSecondaryEffect();
bool hasSecondaryEffect;
if (effect->GetChance() == -1) {
hasSecondaryEffect = true;
} else {
hasSecondaryEffect =
user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target.GetRaw());
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex);
}
}
} else {
auto damage = hit.GetDamage();
if (damage > target->GetCurrentHealth()) {
damage = target->GetCurrentHealth();
hit.SetDamage(damage);
}
if (damage > 0) {
target->Damage(damage, DamageSource::AttackDamage);
if (attackData->HasSecondaryEffect() && !user->IsFainted()) {
bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSource, attack, target.GetRaw(), hitIndex, &preventSecondary);
if (!preventSecondary) {
auto& effect = attackData->GetSecondaryEffect();
bool hasSecondaryEffect;
if (effect->GetChance() == -1) {
hasSecondaryEffect = true;
} else {
hasSecondaryEffect = user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(),
attack, target.GetRaw());
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex);
}
}
}
}
}
}
if (!user->IsFainted()) {
HOOK(OnAfterHits, userSource, attack, target.GetRaw());
}
}
void TurnHandler::ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choice) {
bool preventSwitch = false;
HOOK(PreventSelfSwitch, choice, choice.GetRaw(), &preventSwitch);
if (preventSwitch) {
return;
}
// HOOK: PreventOpponentSwitch for each opponent.
auto user = choice->GetUser();
user->ClearVolatileScripts();
auto userSide = user->GetBattleSide();
auto userIndex = userSide->GetCreatureIndex(user);
userSide->SetCreature(choice->GetNewCreature(), userIndex);
}
void TurnHandler::ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice) {
auto user = choice->GetUser();
auto battle = user->GetBattle();
if (!battle->CanFlee()) {
return;
}
// TODO: If any of the creatures on the users side has a script that prevents it from running, block.
// TODO: If any of the creatures on any other side has a script that prevents this side from running, block.
if (battle->GetLibrary()->GetMiscLibrary()->CanFlee(choice.GetRaw())) {
user->GetBattleSide()->MarkAsFled();
battle->ValidateBattleState();
}
}