Implements BattleRandom class with support for Effect Chance, and script hooks to modify this.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-16 15:08:22 +01:00
parent 579ee82f02
commit 54e366fc1b
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 45 additions and 4 deletions

View File

@ -58,7 +58,7 @@ void Battle::CheckChoicesSetAndRun() {
} }
side->ResetChoices(); side->ResetChoices();
} }
TurnOrdering::OrderChoices(choices, _random); TurnOrdering::OrderChoices(choices, _random.GetRNG());
this->_currentTurnQueue = new ChoiceQueue(choices); this->_currentTurnQueue = new ChoiceQueue(choices);
TurnHandler::RunTurn(this, this->_currentTurnQueue); TurnHandler::RunTurn(this, this->_currentTurnQueue);
if (this->_currentTurnQueue->HasCompletedQueue) { if (this->_currentTurnQueue->HasCompletedQueue) {
@ -69,7 +69,7 @@ void Battle::CheckChoicesSetAndRun() {
ChoiceQueue* Battle::GetCurrentTurnQueue() const { return _currentTurnQueue; } ChoiceQueue* Battle::GetCurrentTurnQueue() const { return _currentTurnQueue; }
Core::Random& Battle::GetRandom() { return _random; } BattleRandom& Battle::GetRandom() { return _random; }
bool Battle::CreatureInField(const Creature* creature) const { bool Battle::CreatureInField(const Creature* creature) const {
for (auto s : _sides) { for (auto s : _sides) {

View File

@ -7,6 +7,7 @@
#include "../Library/BattleLibrary.hpp" #include "../Library/BattleLibrary.hpp"
#include "../TurnChoices/BaseTurnChoice.hpp" #include "../TurnChoices/BaseTurnChoice.hpp"
#include "BattleParty.hpp" #include "BattleParty.hpp"
#include "BattleRandom.hpp"
#include "BattleResult.hpp" #include "BattleResult.hpp"
#include "BattleSide.hpp" #include "BattleSide.hpp"
#include "CreatureIndex.hpp" #include "CreatureIndex.hpp"
@ -19,7 +20,7 @@ namespace CreatureLib::Battling {
uint8_t _numberOfSides; uint8_t _numberOfSides;
uint8_t _creaturesPerSide; uint8_t _creaturesPerSide;
std::vector<BattleSide*> _sides; std::vector<BattleSide*> _sides;
Core::Random _random; BattleRandom _random;
ChoiceQueue* _currentTurnQueue = nullptr; ChoiceQueue* _currentTurnQueue = nullptr;
bool _hasEnded = false; bool _hasEnded = false;
BattleResult _battleResult = BattleResult::Empty(); BattleResult _battleResult = BattleResult::Empty();
@ -55,7 +56,7 @@ namespace CreatureLib::Battling {
void CheckChoicesSetAndRun(); void CheckChoicesSetAndRun();
[[nodiscard]] ChoiceQueue* GetCurrentTurnQueue() const; [[nodiscard]] ChoiceQueue* GetCurrentTurnQueue() const;
Core::Random& GetRandom(); BattleRandom& GetRandom();
bool CreatureInField(const Creature* creature) const; bool CreatureInField(const Creature* creature) const;

View File

@ -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;
}

View File

@ -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

View File

@ -46,6 +46,9 @@ namespace CreatureLib::Battling {
virtual void OnAfterHits(const ExecutingAttack* attack, Creature* target){}; virtual void OnAfterHits(const ExecutingAttack* attack, Creature* target){};
virtual void PreventSelfSwitch(const SwitchTurnChoice* choice, bool* outResult){}; 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){};
}; };
} }