2019-10-31 11:02:23 +00:00
|
|
|
#include "TurnHandler.hpp"
|
2019-10-31 12:26:56 +00:00
|
|
|
#include "../../Core/Exceptions/NotImplementedException.hpp"
|
2019-11-28 11:55:22 +00:00
|
|
|
#include "../Models/Battle.hpp"
|
2019-11-09 11:15:45 +00:00
|
|
|
#include "../ScriptHandling/ScriptMacros.cpp"
|
2019-10-31 11:02:23 +00:00
|
|
|
|
2019-11-06 17:04:00 +00:00
|
|
|
using namespace CreatureLib::Battling;
|
|
|
|
|
|
|
|
void TurnHandler::RunTurn(Battle* battle, ChoiceQueue* queue) {
|
2019-11-28 11:55:22 +00:00
|
|
|
for (auto choice : queue->GetInnerQueue()) {
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(OnBeforeTurn, choice, choice);
|
|
|
|
}
|
2019-11-28 11:55:22 +00:00
|
|
|
while (queue->HasNext()) {
|
2019-11-06 17:04:00 +00:00
|
|
|
auto item = queue->Dequeue();
|
2019-11-05 07:06:12 +00:00
|
|
|
ExecuteChoice(item);
|
|
|
|
delete item;
|
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
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void TurnHandler::ExecuteChoice(BaseTurnChoice* choice) {
|
|
|
|
if (choice == nullptr) {
|
2019-11-02 12:57:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
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.
|
2019-11-28 11:55:22 +00:00
|
|
|
if (user->IsFainted()) {
|
2019-11-02 12:57:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto battle = user->GetBattle();
|
|
|
|
// If the user is not in the field, we don't want to execute its choice.
|
2019-11-28 11:55:22 +00:00
|
|
|
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.
|
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();
|
2019-11-28 11:55:22 +00:00
|
|
|
case TurnChoiceKind::Attack: return ExecuteAttackChoice(dynamic_cast<AttackTurnChoice*>(choice));
|
2019-12-14 11:15:30 +00:00
|
|
|
case TurnChoiceKind::Switch: return ExecuteSwitchChoice(dynamic_cast<SwitchTurnChoice*>(choice));
|
2019-12-15 10:52:10 +00:00
|
|
|
case TurnChoiceKind::Flee: return ExecuteFleeChoice(dynamic_cast<FleeTurnChoice*>(choice));
|
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
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
|
2019-11-23 10:53:00 +00:00
|
|
|
auto attackName = choice->GetAttack()->GetAttack()->GetName();
|
|
|
|
HOOK(ChangeAttack, choice, choice, 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
|
|
|
|
2019-11-24 10:06:51 +00:00
|
|
|
// FIXME: Resolve all targets
|
|
|
|
auto target = choice->GetUser()->GetBattle()->GetTarget(choice->GetTarget());
|
|
|
|
std::vector<Creature*> targets = {target};
|
|
|
|
|
|
|
|
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);
|
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;
|
|
|
|
HOOK(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
|
|
|
|
2019-11-24 09:34:42 +00:00
|
|
|
HOOK(StopBeforeAttack, attack, attack);
|
|
|
|
HOOK(OnBeforeAttack, attack, attack);
|
2019-11-03 12:47:50 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
for (auto& kv : attack->GetTargets()) {
|
2019-12-07 21:09:06 +00:00
|
|
|
HandleAttackForTarget(attack, kv.first, &kv.second);
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
2019-11-09 12:18:45 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
// TODO: We currently delete this, but we probably want to store this in a log, so scripts can look it up.
|
2019-11-09 12:18:45 +00:00
|
|
|
delete attack;
|
2019-10-31 12:13:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target,
|
2019-12-07 21:09:06 +00:00
|
|
|
ExecutingAttack::TargetData* targetData) {
|
2019-11-09 12:18:45 +00:00
|
|
|
auto user = attack->GetUser();
|
2019-11-09 11:55:48 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
ScriptSource* targetSource = target;
|
|
|
|
ScriptSource* userSource = attack;
|
2019-11-09 11:55:48 +00:00
|
|
|
|
|
|
|
bool fail = false;
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(FailIncomingAttack, targetSource, attack, target, 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
|
|
|
|
2019-11-09 11:55:48 +00:00
|
|
|
bool invulnerable = fail;
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(IsInvulnerable, targetSource, attack, target, 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
|
|
|
|
2019-12-07 21:09:06 +00:00
|
|
|
if (!targetData->IsHit()) {
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(OnAttackMiss, targetSource, attack, target);
|
2019-11-03 12:47:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:09:06 +00:00
|
|
|
auto numHits = targetData->GetNumberOfHits();
|
2019-11-03 12:47:50 +00:00
|
|
|
if (numHits == 0)
|
|
|
|
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();
|
|
|
|
auto dmgLibrary = library->GetDamageLibrary();
|
2019-11-28 11:55:22 +00:00
|
|
|
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++) {
|
|
|
|
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
|
|
|
// STOP, STOP! HE'S ALREADY DEAD ;_;
|
|
|
|
break;
|
|
|
|
}
|
2019-12-07 21:09:06 +00:00
|
|
|
auto hit = targetData->GetHit(hitIndex);
|
2019-11-05 07:06:12 +00:00
|
|
|
|
2019-12-07 21:09:06 +00:00
|
|
|
auto hitType = hit->GetType();
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, hitType);
|
2019-12-07 21:09:06 +00:00
|
|
|
hit->SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()));
|
2019-12-15 10:52:10 +00:00
|
|
|
hit->SetCritical(library->GetMiscLibrary()->IsCritical(attack, target, hitIndex));
|
2019-12-07 21:09:06 +00:00
|
|
|
hit->SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex));
|
|
|
|
hit->SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex));
|
2019-11-05 07:06:12 +00:00
|
|
|
|
2019-11-28 11:55:22 +00:00
|
|
|
if (attackData->GetCategory() == Library::AttackCategory::Status) {
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(OnStatusMove, userSource, attack, target, hitIndex);
|
2019-11-28 11:55:22 +00:00
|
|
|
} else {
|
2019-12-07 21:09:06 +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();
|
2019-12-07 21:09:06 +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);
|
|
|
|
|
2019-11-09 11:55:48 +00:00
|
|
|
bool preventSecondary = false;
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(PreventSecondaryEffects, targetSource, attack, target, hitIndex, preventSecondary);
|
2019-11-28 11:55:22 +00:00
|
|
|
if (!preventSecondary) {
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(OnSecondaryEffect, userSource, attack, target, hitIndex);
|
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()) {
|
2019-11-10 13:32:05 +00:00
|
|
|
HOOK(OnAfterHits, userSource, attack, target);
|
2019-11-03 12:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-14 11:15:30 +00:00
|
|
|
|
|
|
|
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);
|
2019-12-15 10:52:10 +00:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|