From 54e366fc1b3a44b200de41421707bfe4802aaecb Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 16 Feb 2020 15:08:22 +0100 Subject: [PATCH] Implements BattleRandom class with support for Effect Chance, and script hooks to modify this. --- src/Battling/Models/Battle.cpp | 4 ++-- src/Battling/Models/Battle.hpp | 5 +++-- src/Battling/Models/BattleRandom.cpp | 10 ++++++++++ src/Battling/Models/BattleRandom.hpp | 27 ++++++++++++++++++++++++++ src/Battling/ScriptHandling/Script.hpp | 3 +++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/Battling/Models/BattleRandom.cpp create mode 100644 src/Battling/Models/BattleRandom.hpp diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 0aa4c7e..d5d90a5 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -58,7 +58,7 @@ void Battle::CheckChoicesSetAndRun() { } side->ResetChoices(); } - TurnOrdering::OrderChoices(choices, _random); + TurnOrdering::OrderChoices(choices, _random.GetRNG()); this->_currentTurnQueue = new ChoiceQueue(choices); TurnHandler::RunTurn(this, this->_currentTurnQueue); if (this->_currentTurnQueue->HasCompletedQueue) { @@ -69,7 +69,7 @@ void Battle::CheckChoicesSetAndRun() { ChoiceQueue* Battle::GetCurrentTurnQueue() const { return _currentTurnQueue; } -Core::Random& Battle::GetRandom() { return _random; } +BattleRandom& Battle::GetRandom() { return _random; } bool Battle::CreatureInField(const Creature* creature) const { for (auto s : _sides) { diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 9ba2e48..5812c89 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -7,6 +7,7 @@ #include "../Library/BattleLibrary.hpp" #include "../TurnChoices/BaseTurnChoice.hpp" #include "BattleParty.hpp" +#include "BattleRandom.hpp" #include "BattleResult.hpp" #include "BattleSide.hpp" #include "CreatureIndex.hpp" @@ -19,7 +20,7 @@ namespace CreatureLib::Battling { uint8_t _numberOfSides; uint8_t _creaturesPerSide; std::vector _sides; - Core::Random _random; + BattleRandom _random; ChoiceQueue* _currentTurnQueue = nullptr; bool _hasEnded = false; BattleResult _battleResult = BattleResult::Empty(); @@ -55,7 +56,7 @@ namespace CreatureLib::Battling { void CheckChoicesSetAndRun(); [[nodiscard]] ChoiceQueue* GetCurrentTurnQueue() const; - Core::Random& GetRandom(); + BattleRandom& GetRandom(); bool CreatureInField(const Creature* creature) const; diff --git a/src/Battling/Models/BattleRandom.cpp b/src/Battling/Models/BattleRandom.cpp new file mode 100644 index 0000000..86ba62b --- /dev/null +++ b/src/Battling/Models/BattleRandom.cpp @@ -0,0 +1,10 @@ +#include "BattleRandom.hpp" +#include "Creature.hpp" +#include "ExecutingAttack.hpp" + +bool CreatureLib::Battling::BattleRandom::EffectChance(float chance, ExecutingAttack* attack, Creature* target) { + HOOK(ModifyEffectChance, attack, attack, target, &chance); + HOOK(ModifyIncomingEffectChance, target, attack, target, &chance); + chance /= 100; + return _random.GetFloat() < chance; +} diff --git a/src/Battling/Models/BattleRandom.hpp b/src/Battling/Models/BattleRandom.hpp new file mode 100644 index 0000000..c262d32 --- /dev/null +++ b/src/Battling/Models/BattleRandom.hpp @@ -0,0 +1,27 @@ +#ifndef CREATURELIB_BATTLERANDOM_HPP +#define CREATURELIB_BATTLERANDOM_HPP + +#include "../../Core/Random.hpp" +#include "../ScriptHandling/ScriptMacros.cpp" + +namespace CreatureLib::Battling { + class ExecutingAttack; + class Creature; + + class BattleRandom { + private: + Core::Random _random; + + public: + BattleRandom() : _random() {} + BattleRandom(int32_t seed) : _random(seed) {} + + bool EffectChance(float chance, ExecutingAttack* attack, Creature* target); + int32_t Get() { return _random.Get(); } + int32_t Get(int32_t max) { return _random.Get(max); } + int32_t Get(int32_t min, int32_t max) { return _random.Get(min, max); } + Core::Random& GetRNG() { return _random; } + }; +} + +#endif // CREATURELIB_BATTLERANDOM_HPP diff --git a/src/Battling/ScriptHandling/Script.hpp b/src/Battling/ScriptHandling/Script.hpp index bace9dc..6f919b5 100644 --- a/src/Battling/ScriptHandling/Script.hpp +++ b/src/Battling/ScriptHandling/Script.hpp @@ -46,6 +46,9 @@ namespace CreatureLib::Battling { virtual void OnAfterHits(const ExecutingAttack* attack, Creature* target){}; virtual void PreventSelfSwitch(const SwitchTurnChoice* choice, bool* outResult){}; + + virtual void ModifyEffectChance(const ExecutingAttack* attack, Creature* target, float* chance){}; + virtual void ModifyIncomingEffectChance(const ExecutingAttack* attack, Creature* target, float* chance){}; }; }