CreatureLib/src/Battling/Flow/TurnHandler.cpp

159 lines
4.8 KiB
C++
Raw Normal View History

#include "TurnHandler.hpp"
2019-11-02 12:57:43 +00:00
#include "../Models/Battle.hpp"
2019-10-31 12:26:56 +00:00
#include "../../Core/Exceptions/NotImplementedException.hpp"
2019-11-09 11:15:45 +00:00
#include "../ScriptHandling/ScriptMacros.cpp"
using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(Battle* battle, ChoiceQueue* queue) {
2019-11-02 12:57:43 +00:00
//HOOK: On Before Turn hook for all choices
for (auto choice: queue->GetInnerQueue()){
HOOK(OnBeforeTurn, choice, choice);
}
while (queue->HasNext()){
if (!battle->HasRecalledSlots()){
return;
}
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(const BaseTurnChoice *choice) {
2019-11-02 12:57:43 +00:00
if (choice == nullptr)
{
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()){
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)){
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();
2019-10-31 12:13:36 +00:00
case TurnChoiceKind::Attack:
2019-11-05 07:06:12 +00:00
return ExecuteAttackChoice(dynamic_cast<const AttackTurnChoice*>(choice));
2019-10-31 12:13:36 +00:00
case TurnChoiceKind::Item:
case TurnChoiceKind::Switch:
case TurnChoiceKind::RunAway:
2019-10-31 12:26:56 +00:00
throw NotImplementedException();
2019-10-31 12:13:36 +00:00
}
}
2019-10-31 12:13:36 +00:00
void TurnHandler::ExecuteAttackChoice(const AttackTurnChoice *choice) {
2019-11-02 12:57:43 +00:00
//HOOK: Change attack
//HOOK: Prevent attack
auto attack = new ExecutingAttack();
2019-10-31 12:13:36 +00:00
2019-11-03 12:47:50 +00:00
//HOOK: override targets
if (!choice->GetAttack()->TryUse(1)){
return;
}
//HOOK: check if attack fails
//HOOK: Check if attack stops after decreasing PP
//HOOK: On Before Attack
for (auto& kv: attack->GetTargets()){
2019-11-03 12:47:50 +00:00
HandleAttackForTarget(attack, kv.first, kv.second);
}
//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, const 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);
2019-11-09 11:55:48 +00:00
if (fail){
//TODO: Fail handling.
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);
2019-11-09 11:55:48 +00:00
if (invulnerable){
//TODO: We should probably do something when a target is invulnerable.
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();
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();
2019-11-03 12:47:50 +00:00
for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++){
if (user->IsFainted()){
break;
}
if (target->IsFainted()){
// STOP, STOP! HE'S ALREADY DEAD ;_;
break;
}
auto hit = targetData.GetHit(hitIndex);
2019-11-05 07:06:12 +00:00
2019-11-09 11:55:48 +00:00
auto hitType = hit.GetType();
HOOK(ChangeAttackType, targetSource, attack, target, hitIndex, hitType);
2019-11-09 11:55:48 +00:00
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
2019-11-03 12:47:50 +00:00
if (attackData->GetCategory() == Library::AttackCategory::Status){
HOOK(OnStatusMove, userSource, attack, target, hitIndex);
2019-11-03 12:47:50 +00:00
}
else{
auto damage = hit.GetDamage();
if (damage > target->GetCurrentHealth()){
damage = target->GetCurrentHealth();
2019-11-05 13:31:54 +00:00
hit.SetDamage(damage);
2019-11-03 12:47:50 +00:00
}
if (damage > 0){
target->Damage(damage, DamageSource::AttackDamage);
2019-11-09 11:55:48 +00:00
bool preventSecondary = false;
HOOK(PreventSecondaryEffects, targetSource, attack, target, hitIndex, preventSecondary);
2019-11-09 11:55:48 +00:00
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
}
}