2019-10-31 11:02:23 +00:00
|
|
|
#include "TurnHandler.hpp"
|
2020-03-22 12:42:26 +00:00
|
|
|
#include <Arbutils/Assert.hpp>
|
2020-06-05 14:38:27 +00:00
|
|
|
#include <unordered_set>
|
2020-03-22 09:11:53 +00:00
|
|
|
#include "../../Library/Exceptions/NotImplementedException.hpp"
|
2020-02-19 14:32:43 +00:00
|
|
|
#include "../ScriptHandling/ScriptMacros.hpp"
|
2020-06-05 13:32:05 +00:00
|
|
|
#include "ResolveTarget.hpp"
|
2019-10-31 11:02:23 +00:00
|
|
|
|
2019-11-06 17:04:00 +00:00
|
|
|
using namespace CreatureLib::Battling;
|
|
|
|
|
2020-05-31 17:01:45 +00:00
|
|
|
void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue) {
|
2020-03-22 12:42:26 +00:00
|
|
|
AssertNotNull(queue)
|
2019-11-28 11:55:22 +00:00
|
|
|
for (auto choice : queue->GetInnerQueue()) {
|
2020-06-02 11:06:24 +00:00
|
|
|
HOOK(OnBeforeTurn, choice, choice.get());
|
2019-11-10 13:32:05 +00:00
|
|
|
}
|
2019-11-28 11:55:22 +00:00
|
|
|
while (queue->HasNext()) {
|
2019-11-06 17:04:00 +00:00
|
|
|
auto item = queue->Dequeue();
|
2020-07-30 18:15:27 +00:00
|
|
|
try_creature(ExecuteChoice(item.get()),
|
|
|
|
"Executing choice failed for choice by mon on side "
|
|
|
|
<< ((uint32_t)item.get()->GetUser()->GetBattleSide()->GetSideIndex()) << " and index "
|
|
|
|
<< ((uint32_t)item.get()->GetUser()->GetBattleSide()->GetCreatureIndex(item->GetUser()))
|
|
|
|
<< " with message");
|
2019-10-31 12:13:36 +00:00
|
|
|
}
|
2019-11-06 17:04:00 +00:00
|
|
|
queue->HasCompletedQueue = true;
|
2019-10-31 12:13:36 +00:00
|
|
|
}
|
2019-10-31 11:02:23 +00:00
|
|
|
|
2020-05-31 16:00:39 +00:00
|
|
|
void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) {
|
2020-06-12 14:48:49 +00:00
|
|
|
AssertNotNull(choice)
|
2019-11-02 12:57:43 +00:00
|
|
|
auto choiceKind = choice->GetKind();
|
|
|
|
if (choiceKind == TurnChoiceKind::Pass) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto user = choice->GetUser();
|
2020-06-12 14:48:49 +00:00
|
|
|
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.
|
2019-11-28 11:55:22 +00:00
|
|
|
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.
|
2020-06-05 12:35:23 +00:00
|
|
|
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.
|
2019-11-28 11:55:22 +00:00
|
|
|
if (!battle->CanUse(choice)) {
|
2019-11-02 12:57:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
switch (choiceKind) {
|
2019-11-06 17:06:48 +00:00
|
|
|
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-14 11:15:30 +00:00
|
|
|
|
2019-12-15 10:52:10 +00:00
|
|
|
case TurnChoiceKind::Item: throw NotImplementedException();
|
2019-10-31 12:13:36 +00:00
|
|
|
}
|
2019-10-31 11:02:23 +00:00
|
|
|
}
|
2019-10-31 12:13:36 +00:00
|
|
|
|
2020-05-31 16:00:39 +00:00
|
|
|
void TurnHandler::ExecuteAttackChoice(ArbUt::BorrowedPtr<AttackTurnChoice> choice) {
|
2020-03-22 12:42:26 +00:00
|
|
|
AssertNotNull(choice)
|
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);
|
2019-11-28 11:55:22 +00:00
|
|
|
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-03-09 09:16:57 +00:00
|
|
|
auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget());
|
2020-06-05 13:32:05 +00:00
|
|
|
ArbUt::List<ArbUt::BorrowedPtr<Creature>> targets = TargetResolver::ResolveTargets(
|
|
|
|
choice->GetTarget(), choice->GetAttack()->GetAttack()->GetTarget(), choice->GetUser()->GetBattle());
|
2019-11-24 10:06:51 +00:00
|
|
|
|
2020-05-29 16:49:48 +00:00
|
|
|
auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
|
2019-11-23 10:53:00 +00:00
|
|
|
bool prevented = false;
|
2020-05-29 16:49:48 +00:00
|
|
|
HOOK_LOCAL(PreventAttack, attack, &attack, &prevented);
|
2019-11-28 11:55:22 +00:00
|
|
|
if (prevented) {
|
2019-11-23 10:53:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-10-31 12:13:36 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
// HOOK: override targets
|
2019-11-03 12:47:50 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
if (!choice->GetAttack()->TryUse(1)) {
|
2019-11-03 12:47:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
// HOOK: check if attack fails
|
2019-11-24 09:34:42 +00:00
|
|
|
bool fail = false;
|
2020-05-29 16:49:48 +00:00
|
|
|
HOOK_LOCAL(FailAttack, attack, &attack, &fail);
|
2019-11-28 11:55:22 +00:00
|
|
|
if (fail) {
|
|
|
|
// TODO: Fail handling.
|
2019-11-24 09:34:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-03 12:47:50 +00:00
|
|
|
|
2020-02-03 18:11:44 +00:00
|
|
|
bool stopBeforeAttack = false;
|
2020-05-29 16:49:48 +00:00
|
|
|
HOOK_LOCAL(StopBeforeAttack, attack, &attack, &stopBeforeAttack);
|
2020-02-03 18:11:44 +00:00
|
|
|
if (stopBeforeAttack) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-29 16:49:48 +00:00
|
|
|
HOOK_LOCAL(OnBeforeAttack, attack, &attack);
|
2019-11-03 12:47:50 +00:00
|
|
|
|
2020-06-04 19:58:28 +00:00
|
|
|
for (uint8_t i = 0; i < attack.GetTargetCount(); i++) {
|
|
|
|
HandleAttackForTarget(&attack, attack.GetTargets()[0]);
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
2019-10-31 12:13:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 19:58:28 +00:00
|
|
|
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target) {
|
2019-11-09 12:18:45 +00:00
|
|
|
auto user = attack->GetUser();
|
2020-03-22 14:35:58 +00:00
|
|
|
AssertNotNull(user)
|
|
|
|
AssertNotNull(target)
|
2020-06-12 14:48:49 +00:00
|
|
|
if (user->GetBattle()->HasEnded())
|
|
|
|
return;
|
2019-11-09 11:55:48 +00:00
|
|
|
|
2020-06-04 19:58:28 +00:00
|
|
|
auto targetSource = target;
|
|
|
|
auto userSource = attack;
|
2019-11-09 11:55:48 +00:00
|
|
|
|
|
|
|
bool fail = false;
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(FailIncomingAttack, targetSource, attack, target.GetRaw(), &fail);
|
2019-11-28 11:55:22 +00:00
|
|
|
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;
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(IsInvulnerable, targetSource, attack, target.GetRaw(), &invulnerable);
|
2019-11-28 11:55:22 +00:00
|
|
|
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
|
|
|
|
2020-04-25 18:09:20 +00:00
|
|
|
auto numberOfHits = attack->GetNumberOfHits();
|
|
|
|
if (numberOfHits == 0) {
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(OnAttackMiss, targetSource, attack, target.GetRaw());
|
2019-11-03 12:47:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-09 12:18:45 +00:00
|
|
|
auto attackData = attack->GetAttack()->GetAttack();
|
2019-11-05 13:31:54 +00:00
|
|
|
auto library = user->GetBattle()->GetLibrary();
|
2020-03-22 14:35:58 +00:00
|
|
|
AssertNotNull(library)
|
2020-05-26 13:11:04 +00:00
|
|
|
auto& dmgLibrary = library->GetDamageLibrary();
|
2020-06-04 19:58:28 +00:00
|
|
|
auto hitIterator = attack->GetTargetIteratorBegin(target.GetRaw());
|
2020-04-25 18:09:20 +00:00
|
|
|
for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) {
|
2020-06-12 14:48:49 +00:00
|
|
|
if (user->GetBattle()->HasEnded())
|
|
|
|
return;
|
2019-11-28 11:55:22 +00:00
|
|
|
if (user->IsFainted()) {
|
2019-11-03 12:47:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-11-28 11:55:22 +00:00
|
|
|
if (target->IsFainted()) {
|
2019-11-03 12:47:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-27 16:26:09 +00:00
|
|
|
auto& hit = hitIterator[hitIndex];
|
|
|
|
uint8_t hitType = hit.GetType();
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(ChangeAttackType, targetSource, attack, target.GetRaw(), hitIndex, &hitType);
|
2020-05-27 16:26:09 +00:00
|
|
|
hit.SetType(hitType);
|
2020-04-19 11:07:28 +00:00
|
|
|
auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes());
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(ChangeEffectiveness, attack, attack, target.GetRaw(), hitIndex, &effectiveness)
|
2020-04-25 14:04:17 +00:00
|
|
|
hit.SetEffectiveness(effectiveness);
|
2020-06-04 19:58:28 +00:00
|
|
|
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));
|
2019-11-05 07:06:12 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
if (attackData->GetCategory() == Library::AttackCategory::Status) {
|
2020-04-06 10:02:29 +00:00
|
|
|
if (attackData->HasSecondaryEffect()) {
|
2020-05-26 12:41:43 +00:00
|
|
|
auto& effect = attackData->GetSecondaryEffect();
|
2020-04-10 21:06:19 +00:00
|
|
|
bool hasSecondaryEffect;
|
|
|
|
if (effect->GetChance() == -1) {
|
|
|
|
hasSecondaryEffect = true;
|
|
|
|
} else {
|
|
|
|
hasSecondaryEffect =
|
2020-06-04 19:58:28 +00:00
|
|
|
user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(), attack, target.GetRaw());
|
2020-04-10 21:06:19 +00:00
|
|
|
}
|
|
|
|
if (hasSecondaryEffect) {
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex);
|
2020-04-10 21:06:19 +00:00
|
|
|
}
|
2020-04-06 10:02:29 +00:00
|
|
|
}
|
2019-11-28 11:55:22 +00:00
|
|
|
} else {
|
2020-04-25 14:04:17 +00:00
|
|
|
auto damage = hit.GetDamage();
|
2019-11-28 11:55:22 +00:00
|
|
|
if (damage > target->GetCurrentHealth()) {
|
2019-11-03 12:47:50 +00:00
|
|
|
damage = target->GetCurrentHealth();
|
2020-04-25 14:04:17 +00:00
|
|
|
hit.SetDamage(damage);
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
2019-11-28 11:55:22 +00:00
|
|
|
if (damage > 0) {
|
2019-11-03 12:47:50 +00:00
|
|
|
target->Damage(damage, DamageSource::AttackDamage);
|
|
|
|
|
2020-06-12 14:48:49 +00:00
|
|
|
if (attackData->HasSecondaryEffect() && !user->IsFainted()) {
|
2020-04-06 10:02:29 +00:00
|
|
|
bool preventSecondary = false;
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(PreventSecondaryEffects, targetSource, attack, target.GetRaw(), hitIndex, &preventSecondary);
|
2020-04-06 10:02:29 +00:00
|
|
|
if (!preventSecondary) {
|
2020-05-26 12:41:43 +00:00
|
|
|
auto& effect = attackData->GetSecondaryEffect();
|
2020-04-06 10:02:29 +00:00
|
|
|
bool hasSecondaryEffect;
|
2020-04-10 20:17:48 +00:00
|
|
|
if (effect->GetChance() == -1) {
|
2020-04-06 10:02:29 +00:00
|
|
|
hasSecondaryEffect = true;
|
|
|
|
} else {
|
2020-06-04 19:58:28 +00:00
|
|
|
hasSecondaryEffect = user->GetBattle()->GetRandom()->EffectChance(effect->GetChance(),
|
|
|
|
attack, target.GetRaw());
|
2020-04-06 10:02:29 +00:00
|
|
|
}
|
|
|
|
if (hasSecondaryEffect) {
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(OnSecondaryEffect, userSource, attack, target.GetRaw(), hitIndex);
|
2020-04-06 10:02:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-09 11:55:48 +00:00
|
|
|
}
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
if (!user->IsFainted()) {
|
2020-06-04 19:58:28 +00:00
|
|
|
HOOK(OnAfterHits, userSource, attack, target.GetRaw());
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-14 11:15:30 +00:00
|
|
|
|
2020-05-31 16:00:39 +00:00
|
|
|
void TurnHandler::ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choice) {
|
2019-12-14 11:15:30 +00:00
|
|
|
bool preventSwitch = false;
|
2020-05-31 16:00:39 +00:00
|
|
|
HOOK(PreventSelfSwitch, choice, choice.GetRaw(), &preventSwitch);
|
2019-12-14 11:15:30 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|