CreatureLib/src/Battling/Flow/TurnHandler.cpp

226 lines
8.0 KiB
C++
Raw Normal View History

#include "TurnHandler.hpp"
2020-03-22 12:42:26 +00:00
#include <Arbutils/Assert.hpp>
#include "../../Library/Exceptions/NotImplementedException.hpp"
#include "../ScriptHandling/ScriptMacros.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()) {
HOOK(OnBeforeTurn, choice, choice);
}
while (queue->HasNext()) {
auto item = queue->Dequeue();
2019-11-05 07:06:12 +00:00
ExecuteChoice(item);
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) {
2020-04-22 19:20:07 +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();
// 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 (!battle->CreatureInField(user)) {
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-05-31 16:00:39 +00:00
case TurnChoiceKind::Attack: return ExecuteAttackChoice(choice.ForceAs<AttackTurnChoice>());
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
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);
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
2019-11-24 10:06:51 +00:00
// FIXME: Resolve all targets
auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget());
2020-05-26 16:31:06 +00:00
ArbUt::List<Creature*> targets = {target};
2019-11-24 10:06:51 +00:00
auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
choice->MarkScriptAsTaken();
2019-11-23 10:53:00 +00:00
bool prevented = false;
HOOK_LOCAL(PreventAttack, attack, &attack, &prevented);
if (prevented) {
2019-11-23 10:53:00 +00:00
return;
}
2019-10-31 12:13:36 +00:00
// HOOK: override targets
2019-11-03 12:47:50 +00:00
if (!choice->GetAttack()->TryUse(1)) {
2019-11-03 12:47:50 +00:00
return;
}
// HOOK: check if attack fails
2019-11-24 09:34:42 +00:00
bool fail = false;
HOOK_LOCAL(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_LOCAL(StopBeforeAttack, attack, &attack, &stopBeforeAttack);
if (stopBeforeAttack) {
return;
}
HOOK_LOCAL(OnBeforeAttack, attack, &attack);
2019-11-03 12:47:50 +00:00
for (auto& t : attack.GetTargets()) {
HandleAttackForTarget(&attack, t);
2019-11-03 12:47:50 +00:00
}
2019-10-31 12:13:36 +00:00
}
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target) {
auto user = attack->GetUser();
AssertNotNull(user)
AssertNotNull(target)
2019-11-09 11:55:48 +00:00
ScriptSource* targetSource = target;
ScriptSource* userSource = attack;
2019-11-09 11:55:48 +00:00
bool fail = false;
2020-02-03 17:32:03 +00:00
HOOK(FailIncomingAttack, targetSource, attack, target, &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;
2020-02-03 17:32:03 +00:00
HOOK(IsInvulnerable, targetSource, attack, target, &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, targetSource, attack, target);
2019-11-03 12:47:50 +00:00
return;
}
auto attackData = attack->GetAttack()->GetAttack();
2019-11-05 13:31:54 +00:00
auto library = user->GetBattle()->GetLibrary();
AssertNotNull(library)
auto& dmgLibrary = library->GetDamageLibrary();
auto hitIterator = attack->GetTargetIteratorBegin(target);
for (uint8_t hitIndex = 0; hitIndex < numberOfHits; hitIndex++) {
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 = hit.GetType();
2020-02-03 17:32:03 +00:00
HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, &hitType);
hit.SetType(hitType);
2020-04-19 11:07:28 +00:00
auto effectiveness = library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes());
HOOK(ChangeEffectiveness, attack, attack, target, hitIndex, &effectiveness)
hit.SetEffectiveness(effectiveness);
hit.SetCritical(library->GetMiscLibrary()->IsCritical(attack, target, hitIndex));
hit.SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex, hit));
hit.SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex, hit));
2019-11-05 07:06:12 +00:00
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);
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, userSource, attack, target, hitIndex);
}
}
} 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()) {
bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSource, attack, target, 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);
}
if (hasSecondaryEffect) {
HOOK(OnSecondaryEffect, userSource, attack, target, hitIndex);
}
}
2019-11-09 11:55:48 +00:00
}
2019-11-03 12:47:50 +00:00
}
}
}
if (!user->IsFainted()) {
HOOK(OnAfterHits, userSource, attack, target);
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();
}
}