CreatureLib/src/Battling/Flow/TurnHandler.cpp

228 lines
8.0 KiB
C++

#include "TurnHandler.hpp"
#include <Arbutils/Assert.hpp>
#include "../../Library/Exceptions/NotImplementedException.hpp"
#include "../ScriptHandling/ScriptMacros.hpp"
using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(ChoiceQueue* queue) {
AssertNotNull(queue)
for (auto choice : queue->GetInnerQueue()) {
HOOK(OnBeforeTurn, choice, choice);
}
while (queue->HasNext()) {
auto item = queue->Dequeue();
ExecuteChoice(item);
delete item;
}
queue->HasCompletedQueue = true;
}
void TurnHandler::ExecuteChoice(BaseTurnChoice* choice) {
AssertNotNull(choice);
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()) {
return;
}
auto battle = user->GetBattle();
AssertNotNull(battle)
// If the user is not in the field, we don't want to execute its choice.
if (!battle->CreatureInField(user)) {
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(dynamic_cast<AttackTurnChoice*>(choice));
case TurnChoiceKind::Switch: return ExecuteSwitchChoice(dynamic_cast<SwitchTurnChoice*>(choice));
case TurnChoiceKind::Flee: return ExecuteFleeChoice(dynamic_cast<FleeTurnChoice*>(choice));
case TurnChoiceKind::Item: throw NotImplementedException();
}
}
void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
AssertNotNull(choice)
auto attackName = choice->GetAttack()->GetAttack()->GetName();
HOOK(ChangeAttack, choice, choice, &attackName);
if (attackName != choice->GetAttack()->GetAttack()->GetName()) {
// TODO: Change attack
}
// FIXME: Resolve all targets
auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget());
Arbutils::Collections::List<Creature*> targets = {target};
auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
bool prevented = false;
HOOK(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(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 (auto& t : attack->GetTargets()) {
HandleAttackForTarget(attack, t);
}
// TODO: We currently delete this, but we probably want to store this in a log, so scripts can look it up.
delete attack;
}
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target) {
auto user = attack->GetUser();
AssertNotNull(user)
AssertNotNull(target)
ScriptSource* targetSource = target;
ScriptSource* userSource = attack;
bool fail = false;
HOOK(FailIncomingAttack, targetSource, attack, target, &fail);
if (fail) {
// TODO: Fail handling.
return;
}
bool invulnerable = false;
HOOK(IsInvulnerable, targetSource, attack, target, &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);
return;
}
auto attackData = attack->GetAttack()->GetAttack();
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()) {
break;
}
if (target->IsFainted()) {
break;
}
auto& hit = *(hitIterator++);
auto hitType = hit.GetType();
HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, &hitType);
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));
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()) {
damage = target->GetCurrentHealth();
hit.SetDamage(damage);
}
if (damage > 0) {
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);
}
}
}
}
}
}
if (!user->IsFainted()) {
HOOK(OnAfterHits, userSource, attack, target);
}
}
void TurnHandler::ExecuteSwitchChoice(CreatureLib::Battling::SwitchTurnChoice* choice) {
bool preventSwitch = false;
HOOK(PreventSelfSwitch, choice, choice, &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(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)) {
user->GetBattleSide()->MarkAsFled();
battle->ValidateBattleState();
}
}