281 lines
11 KiB
C++
281 lines
11 KiB
C++
#include "TurnHandler.hpp"
|
|
#include <Arbutils/Assert.hpp>
|
|
#include <unordered_set>
|
|
#include "../../Library/Exceptions/NotImplementedException.hpp"
|
|
#include "../EventHooks/EventDataClasses.hpp"
|
|
#include "../History/HistoryElements/AttackUseHistory.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();
|
|
AssertNotNull(item)
|
|
AssertNotNull(item->GetUser())
|
|
AssertNotNull(item->GetUser()->GetBattle())
|
|
AssertNotNull(item->GetUser()->GetBattleSide())
|
|
auto index = (uint32_t)item->GetUser()->GetBattleSide()->GetCreatureIndex(item->GetUser());
|
|
|
|
try_creature(ExecuteChoice(item.get()),
|
|
"Executing choice failed for choice by mon on side "
|
|
<< ((uint32_t)item->GetUser()->GetBattleSide()->GetSideIndex()) << " and index " << index
|
|
<< ". Choice had choice kind "
|
|
<< CreatureLib::Battling::TurnChoiceKindHelper::ToString(item->GetKind()) << " "
|
|
<< " with message");
|
|
}
|
|
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:
|
|
try_creature(return ExecuteAttackChoice(choice.ForceAs<AttackTurnChoice>()),
|
|
"Encountered exception during attack choice execution");
|
|
case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs<SwitchTurnChoice>());
|
|
case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs<FleeTurnChoice>());
|
|
|
|
case TurnChoiceKind::Item: throw NotImplementedException();
|
|
}
|
|
}
|
|
|
|
void TurnHandler::ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>& choice) {
|
|
AssertNotNull(choice)
|
|
auto battle = choice->GetUser()->GetBattle();
|
|
auto attackName = choice->GetAttack()->GetAttack()->GetName();
|
|
HOOK(ChangeAttack, choice, choice.GetRaw(), &attackName);
|
|
if (attackName != choice->GetAttack()->GetAttack()->GetName()) {
|
|
// TODO: Change attack
|
|
}
|
|
|
|
auto targetType = choice->GetAttack()->GetAttack()->GetTarget();
|
|
ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets;
|
|
try_creature(targets = TargetResolver::ResolveTargets(choice->GetTarget(), targetType, battle);
|
|
, "Exception during target determination");
|
|
|
|
auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
|
|
bool prevented = false;
|
|
HOOK(PreventAttack, attack, attack, &prevented);
|
|
if (prevented) {
|
|
delete attack;
|
|
return;
|
|
}
|
|
|
|
// HOOK: override targets
|
|
if (!choice->GetAttack()->TryUse(1)) {
|
|
delete attack;
|
|
return;
|
|
}
|
|
|
|
battle->TriggerEventListener<AttackUseEvent>(attack);
|
|
battle->RegisterHistoryElement<AttackUseHistory>(attack);
|
|
|
|
// HOOK: check if attack fails
|
|
bool fail = false;
|
|
HOOK(FailAttack, attack, attack, &fail);
|
|
if (fail) {
|
|
// TODO: Fail handling.
|
|
return;
|
|
}
|
|
|
|
bool stopBeforeAttack = false;
|
|
HOOK(StopBeforeAttack, attack, attack, &stopBeforeAttack);
|
|
if (stopBeforeAttack) {
|
|
return;
|
|
}
|
|
HOOK(OnBeforeAttack, attack, attack);
|
|
|
|
for (uint8_t i = 0; i < attack->GetTargetCount(); i++) {
|
|
auto target = attack->GetTargets()[i];
|
|
try_creature(HandleAttackForTarget(attack, target), "Exception occurred during handling attack for target");
|
|
}
|
|
}
|
|
|
|
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target) {
|
|
AssertNotNull(attack)
|
|
auto& user = attack->GetUser();
|
|
AssertNotNull(user)
|
|
AssertNotNull(target)
|
|
auto& battle = user->GetBattle();
|
|
AssertNotNull(battle)
|
|
if (battle->HasEnded())
|
|
return;
|
|
|
|
bool fail = false;
|
|
HOOK(FailIncomingAttack, target, attack, target.GetRaw(), &fail);
|
|
if (fail) {
|
|
// TODO: Fail handling.
|
|
return;
|
|
}
|
|
|
|
bool invulnerable = false;
|
|
HOOK(IsInvulnerable, target, 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, target, attack, target.GetRaw());
|
|
battle->TriggerEventListener<MissEvent>(user);
|
|
return;
|
|
}
|
|
|
|
auto& learnedAttack = attack->GetAttack();
|
|
AssertNotNull(learnedAttack);
|
|
auto& attackData = learnedAttack->GetAttack();
|
|
AssertNotNull(attackData);
|
|
|
|
auto& library = battle->GetLibrary();
|
|
AssertNotNull(library)
|
|
auto& dmgLibrary = library->GetDamageLibrary();
|
|
auto& typeLibrary = library->GetTypeLibrary();
|
|
auto& miscLibrary = library->GetMiscLibrary();
|
|
AssertNotNull(dmgLibrary)
|
|
AssertNotNull(typeLibrary)
|
|
AssertNotNull(miscLibrary)
|
|
|
|
auto hitIterator = attack->GetTargetIteratorBegin(target);
|
|
for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) {
|
|
if (battle->HasEnded())
|
|
return;
|
|
if (user->IsFainted()) {
|
|
break;
|
|
}
|
|
if (target->IsFainted()) {
|
|
break;
|
|
}
|
|
auto& hit = hitIterator[hitIndex];
|
|
uint8_t hitType = attackData->GetType();
|
|
HOOK(ChangeAttackType, target, attack, target.GetRaw(), hitIndex, &hitType);
|
|
hit.SetType(hitType);
|
|
auto effectiveness = typeLibrary->GetEffectiveness(hitType, target->GetTypes());
|
|
HOOK(ChangeEffectiveness, attack, attack, target.GetRaw(), hitIndex, &effectiveness)
|
|
hit.SetEffectiveness(effectiveness);
|
|
hit.SetCritical(miscLibrary->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()) {
|
|
try {
|
|
auto& effect = attackData->GetSecondaryEffect();
|
|
bool hasSecondaryEffect;
|
|
if (effect->GetChance() == -1) {
|
|
hasSecondaryEffect = true;
|
|
} else {
|
|
hasSecondaryEffect =
|
|
battle->GetRandom()->EffectChance(effect->GetChance(), attack, target.GetRaw());
|
|
}
|
|
if (hasSecondaryEffect) {
|
|
HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex);
|
|
}
|
|
} catch (const CreatureException& e) {
|
|
throw e;
|
|
} catch (const std::exception& e) {
|
|
THROW_CREATURE("Exception during status attack effect handling: " << e.what() << ".");
|
|
}
|
|
}
|
|
} 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, target, attack, target.GetRaw(), hitIndex, &preventSecondary);
|
|
if (!preventSecondary) {
|
|
try {
|
|
auto& effect = attackData->GetSecondaryEffect();
|
|
bool hasSecondaryEffect;
|
|
if (effect->GetChance() == -1) {
|
|
hasSecondaryEffect = true;
|
|
} else {
|
|
auto random = battle->GetRandom();
|
|
AssertNotNull(random);
|
|
hasSecondaryEffect = random->EffectChance(effect->GetChance(), attack, target.GetRaw());
|
|
}
|
|
if (hasSecondaryEffect) {
|
|
HOOK(OnSecondaryEffect, user, attack, target.GetRaw(), hitIndex);
|
|
}
|
|
} catch (const CreatureException& e) {
|
|
throw e;
|
|
} catch (const std::exception& e) {
|
|
THROW_CREATURE("Exception during offensive attack secondary effect handling: " << e.what()
|
|
<< ".");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!user->IsFainted()) {
|
|
HOOK(OnAfterHits, user, 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();
|
|
}
|
|
}
|