CreatureLib/src/Battling/Flow/TurnHandler.cpp

168 lines
5.4 KiB
C++
Raw Normal View History

#include "TurnHandler.hpp"
2019-10-31 12:26:56 +00:00
#include "../../Core/Exceptions/NotImplementedException.hpp"
#include "../Models/Battle.hpp"
2019-11-09 11:15:45 +00:00
#include "../ScriptHandling/ScriptMacros.cpp"
using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(Battle* battle, ChoiceQueue* 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);
delete item;
2019-10-31 12:13:36 +00:00
}
queue->HasCompletedQueue = true;
2019-10-31 12:13:36 +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.
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.
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();
case TurnChoiceKind::Attack: return ExecuteAttackChoice(dynamic_cast<AttackTurnChoice*>(choice));
2019-10-31 12:13:36 +00:00
case TurnChoiceKind::Item:
case TurnChoiceKind::Switch:
case TurnChoiceKind::RunAway: throw NotImplementedException();
2019-10-31 12:13:36 +00:00
}
}
2019-10-31 12:13:36 +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);
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);
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(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
2019-11-24 09:34:42 +00:00
HOOK(StopBeforeAttack, attack, attack);
HOOK(OnBeforeAttack, attack, attack);
2019-11-03 12:47:50 +00:00
for (auto& kv : attack->GetTargets()) {
HandleAttackForTarget(attack, kv.first, &kv.second);
2019-11-03 12:47:50 +00:00
}
// TODO: We currently delete this, but we probably want to store this in a log, so scripts can look it up.
delete attack;
2019-10-31 12:13:36 +00:00
}
void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target,
ExecutingAttack::TargetData* targetData) {
auto user = attack->GetUser();
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;
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
2019-11-09 11:55:48 +00:00
bool invulnerable = fail;
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
if (!targetData->IsHit()) {
HOOK(OnAttackMiss, targetSource, attack, target);
2019-11-03 12:47:50 +00:00
return;
}
auto numHits = targetData->GetNumberOfHits();
2019-11-03 12:47:50 +00:00
if (numHits == 0)
return;
auto attackData = attack->GetAttack()->GetAttack();
2019-11-05 13:31:54 +00:00
auto library = user->GetBattle()->GetLibrary();
auto dmgLibrary = library->GetDamageLibrary();
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++) {
if (user->IsFainted()) {
2019-11-03 12:47:50 +00:00
break;
}
if (target->IsFainted()) {
2019-11-03 12:47:50 +00:00
// STOP, STOP! HE'S ALREADY DEAD ;_;
break;
}
auto hit = targetData->GetHit(hitIndex);
2019-11-05 07:06:12 +00:00
auto hitType = hit->GetType();
HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, hitType);
hit->SetEffectiveness(library->GetTypeLibrary()->GetEffectiveness(hitType, target->GetTypes()));
hit->SetCritical(library->GetCriticalLibrary()->IsCritical(attack, target, hitIndex));
hit->SetBasePower(dmgLibrary->GetBasePower(attack, target, hitIndex));
hit->SetDamage(dmgLibrary->GetDamage(attack, target, hitIndex));
2019-11-05 07:06:12 +00:00
if (attackData->GetCategory() == Library::AttackCategory::Status) {
HOOK(OnStatusMove, 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);
2019-11-09 11:55:48 +00:00
bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSource, attack, target, hitIndex, preventSecondary);
if (!preventSecondary) {
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
}
}