Adds script hooks for preventing runaway.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-03-27 14:49:17 +01:00
parent 46307fe71f
commit 4dd3700352
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 37 additions and 14 deletions

View File

@ -6,8 +6,8 @@
using namespace CreatureLib::Battling; using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue, ArbUt::BorrowedPtr<Battle> battle) { void TurnHandler::RunTurn(const ArbUt::BorrowedPtr<ChoiceQueue>& queue, const ArbUt::BorrowedPtr<Battle>& battle) {
for (auto choice : queue->GetInnerQueue()) { for (const auto& choice : queue->GetInnerQueue()) {
HOOK(OnBeforeTurn, choice, choice.get()); HOOK(OnBeforeTurn, choice, choice.get());
} }
while (queue->HasNext() && !battle->HasEnded()) { while (queue->HasNext() && !battle->HasEnded()) {
@ -27,17 +27,19 @@ void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue, ArbUt::Borrowed
queue->HasCompletedQueue = true; queue->HasCompletedQueue = true;
} }
void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice) { void TurnHandler::ExecuteChoice(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice) {
auto choiceKind = choice->GetKind(); auto choiceKind = choice->GetKind();
if (choiceKind == TurnChoiceKind::Pass) { if (choiceKind == TurnChoiceKind::Pass) {
return; return;
} }
auto user = choice->GetUser(); auto user = choice->GetUser();
if (!user->GetBattle().HasValue()) if (!user->GetBattle().HasValue()) {
return; return;
auto battle = user->GetBattle().GetValue(); }
if (battle->HasEnded()) auto* battle = user->GetBattle().GetValue();
if (battle->HasEnded()) {
return; return;
}
// If the user is fainted, we don't want to execute its choice. // If the user is fainted, we don't want to execute its choice.
if (user->IsFainted()) { if (user->IsFainted()) {
return; return;
@ -248,7 +250,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo
} }
} }
void TurnHandler::ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choice) { void TurnHandler::ExecuteSwitchChoice(const ArbUt::BorrowedPtr<SwitchTurnChoice>& choice) {
bool preventSwitch = false; bool preventSwitch = false;
HOOK(PreventSelfSwitch, choice, choice.GetRaw(), &preventSwitch); HOOK(PreventSelfSwitch, choice, choice.GetRaw(), &preventSwitch);
if (preventSwitch) { if (preventSwitch) {
@ -264,14 +266,32 @@ void TurnHandler::ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choic
userSide.GetValue()->SetCreature(choice->GetNewCreature(), userIndex); userSide.GetValue()->SetCreature(choice->GetNewCreature(), userIndex);
} }
} }
void TurnHandler::ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice) { void TurnHandler::ExecuteFleeChoice(const ArbUt::BorrowedPtr<FleeTurnChoice>& choice) {
auto user = choice->GetUser(); auto user = choice->GetUser();
auto battle = user->GetBattle(); auto battle = user->GetBattle();
if (!battle.HasValue() || !battle.GetValue()->CanFlee()) { if (!battle.HasValue() || !battle.GetValue()->CanFlee()) {
return; 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. bool preventRun = false;
HOOK(PreventRunAway, choice, &preventRun);
if (preventRun) {
return;
}
for (auto* side : battle.GetValue()->GetSides()) {
if (side == user->GetBattleSide()) {
continue;
}
for (const auto& creature : side->GetCreatures()) {
if (!creature.HasValue()) {
continue;
}
HOOK(PreventOpponentRunAway, creature.GetValue(), &preventRun);
if (preventRun) {
return;
}
}
}
if (battle.GetValue()->GetLibrary()->GetMiscLibrary()->CanFlee(choice.GetRaw())) { if (battle.GetValue()->GetLibrary()->GetMiscLibrary()->CanFlee(choice.GetRaw())) {
if (user->GetBattleSide().HasValue()) { if (user->GetBattleSide().HasValue()) {

View File

@ -11,16 +11,16 @@ namespace CreatureLib::Battling {
class Battle; class Battle;
class TurnHandler { class TurnHandler {
static void ExecuteChoice(ArbUt::BorrowedPtr<BaseTurnChoice> choice); static void ExecuteChoice(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice);
static void ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>& choice); static void ExecuteAttackChoice(const ArbUt::BorrowedPtr<AttackTurnChoice>& choice);
static void HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target); static void HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr<Creature>& target);
static void ExecuteSwitchChoice(ArbUt::BorrowedPtr<SwitchTurnChoice> choice); static void ExecuteSwitchChoice(const ArbUt::BorrowedPtr<SwitchTurnChoice>& choice);
static void ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice); static void ExecuteFleeChoice(const ArbUt::BorrowedPtr<FleeTurnChoice>& choice);
public: public:
static void RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue, ArbUt::BorrowedPtr<Battle> battle); static void RunTurn(const ArbUt::BorrowedPtr<ChoiceQueue>& queue, const ArbUt::BorrowedPtr<Battle>& battle);
}; };
} }

View File

@ -78,6 +78,9 @@ namespace CreatureLib::Battling {
virtual void OnFail([[maybe_unused]] Creature* target){}; virtual void OnFail([[maybe_unused]] Creature* target){};
virtual void OnOpponentFail([[maybe_unused]] Creature* target){}; virtual void OnOpponentFail([[maybe_unused]] Creature* target){};
virtual void PreventRunAway([[maybe_unused]] bool* result){};
virtual void PreventOpponentRunAway([[maybe_unused]] bool* result){};
}; };
} }