CreatureLib/src/Battling/Flow/TurnHandler.cpp

281 lines
11 KiB
C++
Raw Normal View History

#include "TurnHandler.hpp"
2020-03-22 12:42:26 +00:00
#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) {
2020-03-22 12:42:26 +00:00
AssertNotNull(queue)
for (auto choice : queue->GetInnerQueue()) {
2020-06-02 11:06:24 +00:00
HOOK(OnBeforeTurn, choice, choice.get());
}
while (queue->HasNext()) {
auto item = queue->Dequeue();
2020-08-03 15:44:29 +00:00
AssertNotNull(item)
AssertNotNull(item->GetUser())
AssertNotNull(item->GetUser()->GetBattle())
AssertNotNull(item->GetUser()->GetBattleSide())
auto index = (uint32_t)item->GetUser()->GetBattleSide()->GetCreatureIndex(item->GetUser());
2020-07-30 18:15:27 +00:00
try_creature(ExecuteChoice(item.get()),
"Executing choice failed for choice by mon on side "
2020-08-03 15:44:29 +00:00
<< ((uint32_t)item->GetUser()->GetBattleSide()->GetSideIndex()) << " and index " << index
2020-07-31 08:45:20 +00:00
<< ". Choice had choice kind "
<< CreatureLib::Battling::TurnChoiceKindHelper::ToString(item->GetKind()) << " "
2020-07-30 18:15:27 +00:00
<< " with message");
2019-10-31 12:13:36 +00:00
}
queue->HasCompletedQueue = true;
2019-10-31 12:13:36 +00:00
}
2020-05-31 16:00:39 +00:00
void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) {
AssertNotNull(choice)
2019-11-02 12:57:43 +00:00
auto choiceKind = choice->GetKind();
if (choiceKind == TurnChoiceKind::Pass) {
return;
}
auto user = choice->GetUser();
AssertNotNull(user)
if (user->GetBattle()->HasEnded())
return;
2019-11-02 12:57:43 +00:00
// If the user is fainted, we don't want to execute its choice.
if (user->IsFainted()) {
2019-11-02 12:57:43 +00:00
return;
}
auto battle = user->GetBattle();
2020-03-22 12:42:26 +00:00
AssertNotNull(battle)
2019-11-02 12:57:43 +00:00
// If the user is not in the field, we don't want to execute its choice.
if (!user->IsOnBattleField()) {
2019-11-02 12:57:43 +00:00
return;
}
// If the choice is not valid, we don't want to execute it.
if (!battle->CanUse(choice)) {
2019-11-02 12:57:43 +00:00
return;
}
switch (choiceKind) {
case TurnChoiceKind::Pass: throw NotReachableException();
2020-07-30 18:15:27 +00:00
case TurnChoiceKind::Attack:
try_creature(return ExecuteAttackChoice(choice.ForceAs<AttackTurnChoice>()),
"Encountered exception during attack choice execution");
2020-05-31 16:00:39 +00:00
case TurnChoiceKind::Switch: return ExecuteSwitchChoice(choice.ForceAs<SwitchTurnChoice>());
case TurnChoiceKind::Flee: return ExecuteFleeChoice(choice.ForceAs<FleeTurnChoice>());
2019-12-15 10:52:10 +00:00
case TurnChoiceKind::Item: throw NotImplementedException();
2019-10-31 12:13:36 +00:00
}
}
2019-10-31 12:13:36 +00:00
void TurnHandler::ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>& choice) {
2020-03-22 12:42:26 +00:00
AssertNotNull(choice)
auto battle = choice->GetUser()->GetBattle();
2019-11-23 10:53:00 +00:00
auto attackName = choice->GetAttack()->GetAttack()->GetName();
2020-05-31 16:00:39 +00:00
HOOK(ChangeAttack, choice, choice.GetRaw(), &attackName);
if (attackName != choice->GetAttack()->GetAttack()->GetName()) {
// TODO: Change attack
2019-11-23 10:53:00 +00:00
}
2019-11-02 12:57:43 +00:00
2020-07-31 08:45:20 +00:00
auto targetType = choice->GetAttack()->GetAttack()->GetTarget();
ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets;
try_creature(targets = TargetResolver::ResolveTargets(choice->GetTarget(), targetType, battle);
2020-07-31 09:23:23 +00:00
, "Exception during target determination");
2019-11-24 10:06:51 +00:00
auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
2019-11-23 10:53:00 +00:00
bool prevented = false;
HOOK(PreventAttack, attack, attack, &prevented);
if (prevented) {
delete attack;
2019-11-23 10:53:00 +00:00
return;
}
2019-10-31 12:13:36 +00:00
// HOOK: override targets
if (!choice->GetAttack()->TryUse(1)) {
delete attack;
2019-11-03 12:47:50 +00:00
return;
}
battle->TriggerEventListener<AttackUseEvent>(attack);
battle->RegisterHistoryElement<AttackUseHistory>(attack);
// HOOK: check if attack fails
2019-11-24 09:34:42 +00:00
bool fail = false;
HOOK(FailAttack, attack, attack, &fail);
if (fail) {
// TODO: Fail handling.
2019-11-24 09:34:42 +00:00
return;
}
2019-11-03 12:47:50 +00:00
bool stopBeforeAttack = false;
HOOK(StopBeforeAttack, attack, attack, &stopBeforeAttack);
if (stopBeforeAttack) {
return;
}
HOOK(OnBeforeAttack, attack, attack);
2019-11-03 12:47:50 +00:00
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");
2019-11-03 12:47:50 +00:00
}
2019-10-31 12:13:36 +00:00
}
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;
2019-11-09 11:55:48 +00:00
bool fail = false;
HOOK(FailIncomingAttack, target, attack, target.GetRaw(), &fail);
if (fail) {
// TODO: Fail handling.
2019-11-09 11:55:48 +00:00
return;
}
2019-11-03 12:47:50 +00:00
2020-04-22 19:20:07 +00:00
bool invulnerable = false;
HOOK(IsInvulnerable, target, attack, target.GetRaw(), &invulnerable);
if (invulnerable) {
// TODO: We should probably do something when a target is invulnerable.
2019-11-09 11:55:48 +00:00
return;
}
2019-11-03 12:47:50 +00:00
auto numberOfHits = attack->GetNumberOfHits();
if (numberOfHits == 0) {
HOOK(OnAttackMiss, target, attack, target.GetRaw());
battle->TriggerEventListener<MissEvent>(user);
2019-11-03 12:47:50 +00:00
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()) {
2019-11-03 12:47:50 +00:00
break;
}
if (target->IsFainted()) {
2019-11-03 12:47:50 +00:00
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));
2019-11-05 07:06:12 +00:00
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()) {
2019-11-03 12:47:50 +00:00
damage = target->GetCurrentHealth();
hit.SetDamage(damage);
2019-11-03 12:47:50 +00:00
}
if (damage > 0) {
2019-11-03 12:47:50 +00:00
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()
<< ".");
}
}
2019-11-09 11:55:48 +00:00
}
2019-11-03 12:47:50 +00:00
}
}
}
if (!user->IsFainted()) {
HOOK(OnAfterHits, user, attack, target.GetRaw());
2019-11-03 12:47:50 +00:00
}
}
2020-05-31 16:00:39 +00:00
void TurnHandler::ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choice) {
bool preventSwitch = false;
2020-05-31 16:00:39 +00:00
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);
2019-12-15 10:52:10 +00:00
}
2020-05-31 16:00:39 +00:00
void TurnHandler::ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice) {
2019-12-15 10:52:10 +00:00
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.
2020-05-31 16:00:39 +00:00
if (battle->GetLibrary()->GetMiscLibrary()->CanFlee(choice.GetRaw())) {
2019-12-15 10:52:10 +00:00
user->GetBattleSide()->MarkAsFled();
battle->ValidateBattleState();
}
}