Make battle class fields all smart pointers.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-31 19:01:45 +02:00
parent 29cb4eac37
commit 01f6e59cb6
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 16 additions and 24 deletions

View File

@ -21,7 +21,9 @@ export uint8_t CreatureLib_Battle_TrySetChoice(bool& out, Battle* p, BaseTurnCho
export bool CreatureLib_Battle_CanFlee(const Battle* p) { return p->CanFlee(); } export bool CreatureLib_Battle_CanFlee(const Battle* p) { return p->CanFlee(); }
export uint8_t CreatureLib_Battle_CheckChoicesSetAndRun(Battle* p) { Try(p->CheckChoicesSetAndRun();) } export uint8_t CreatureLib_Battle_CheckChoicesSetAndRun(Battle* p) { Try(p->CheckChoicesSetAndRun();) }
export ChoiceQueue* CreatureLib_Battle_GetCurrentTurnQueue(const Battle* p) { return p->GetCurrentTurnQueue(); } export ChoiceQueue* CreatureLib_Battle_GetCurrentTurnQueue(const Battle* p) {
return p->GetCurrentTurnQueue().operator->();
}
export BattleRandom* CreatureLib_Battle_GetRandom(Battle* p) { return p->GetRandom(); } export BattleRandom* CreatureLib_Battle_GetRandom(Battle* p) { return p->GetRandom(); }
export uint8_t CreatureLib_Battle_CreatureInField(bool& out, const Battle* p, Creature* c) { export uint8_t CreatureLib_Battle_CreatureInField(bool& out, const Battle* p, Creature* c) {
Try(out = p->CreatureInField(c);) Try(out = p->CreatureInField(c);)

View File

@ -5,7 +5,7 @@
using namespace CreatureLib::Battling; using namespace CreatureLib::Battling;
void TurnHandler::RunTurn(ChoiceQueue* queue) { void TurnHandler::RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue) {
AssertNotNull(queue) AssertNotNull(queue)
for (auto choice : queue->GetInnerQueue()) { for (auto choice : queue->GetInnerQueue()) {
HOOK(OnBeforeTurn, choice, choice); HOOK(OnBeforeTurn, choice, choice);

View File

@ -20,7 +20,7 @@ namespace CreatureLib::Battling {
static void ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice); static void ExecuteFleeChoice(ArbUt::BorrowedPtr<FleeTurnChoice> choice);
public: public:
static void RunTurn(ChoiceQueue* queue); static void RunTurn(ArbUt::BorrowedPtr<ChoiceQueue> queue);
}; };
} }

View File

@ -62,15 +62,14 @@ void Battle::CheckChoicesSetAndRun() {
} }
_currentTurn++; _currentTurn++;
TurnOrdering::OrderChoices(choices, _random.GetRNG()); TurnOrdering::OrderChoices(choices, _random.GetRNG());
this->_currentTurnQueue = new ChoiceQueue(choices); this->_currentTurnQueue.reset(new ChoiceQueue(choices));
TurnHandler::RunTurn(this->_currentTurnQueue); TurnHandler::RunTurn(this->_currentTurnQueue);
if (this->_currentTurnQueue->HasCompletedQueue) { if (this->_currentTurnQueue->HasCompletedQueue) {
delete this->_currentTurnQueue;
this->_currentTurnQueue = nullptr; this->_currentTurnQueue = nullptr;
} }
} }
ChoiceQueue* Battle::GetCurrentTurnQueue() const noexcept { return _currentTurnQueue; } ArbUt::BorrowedPtr<ChoiceQueue> Battle::GetCurrentTurnQueue() const noexcept { return _currentTurnQueue; }
BattleRandom* Battle::GetRandom() noexcept { return &_random; } BattleRandom* Battle::GetRandom() noexcept { return &_random; }

View File

@ -18,13 +18,13 @@ namespace CreatureLib::Battling {
class Battle : public ScriptSource { class Battle : public ScriptSource {
protected: protected:
ArbUt::BorrowedPtr<const BattleLibrary> _library; ArbUt::BorrowedPtr<const BattleLibrary> _library;
ArbUt::List<std::unique_ptr<BattleParty>> _parties; ArbUt::UniquePtrList<BattleParty> _parties;
bool _canFlee; bool _canFlee;
uint8_t _numberOfSides; uint8_t _numberOfSides;
uint8_t _creaturesPerSide; uint8_t _creaturesPerSide;
ArbUt::List<BattleSide*> _sides; ArbUt::UniquePtrList<BattleSide> _sides;
BattleRandom _random; BattleRandom _random;
ChoiceQueue* _currentTurnQueue = nullptr; std::unique_ptr<ChoiceQueue> _currentTurnQueue = nullptr;
bool _hasEnded = false; bool _hasEnded = false;
BattleResult _battleResult = BattleResult::Empty(); BattleResult _battleResult = BattleResult::Empty();
EventHook _eventHook = EventHook(); EventHook _eventHook = EventHook();
@ -35,26 +35,17 @@ namespace CreatureLib::Battling {
public: public:
Battle(const BattleLibrary* library, ArbUt::List<BattleParty*> parties, bool canFlee = true, Battle(const BattleLibrary* library, ArbUt::List<BattleParty*> parties, bool canFlee = true,
uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1) uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1)
: _library(library), _parties(parties.Count()), _canFlee(canFlee), _numberOfSides(numberOfSides), : _library(library), _parties(parties.GetStdList()), _canFlee(canFlee), _numberOfSides(numberOfSides),
_creaturesPerSide(creaturesPerSide) { _creaturesPerSide(creaturesPerSide), _sides(numberOfSides) {
AssertNotNull(_library); AssertNotNull(_library);
AssertAllNotNull(parties); AssertAllNotNull(parties);
for (size_t i = 0; i < parties.Count(); i++) {
_parties.GetStdList().push_back(std::unique_ptr<BattleParty>(parties[i]));
}
_sides = ArbUt::List<BattleSide*>(numberOfSides);
for (size_t i = 0; i < numberOfSides; i++) { for (size_t i = 0; i < numberOfSides; i++) {
_sides.Append(new BattleSide(i, this, creaturesPerSide)); _sides.Append(new BattleSide(i, this, creaturesPerSide));
} }
} }
virtual ~Battle() { virtual ~Battle() = default;
for (auto s : _sides) {
delete s;
}
delete _currentTurnQueue;
}
[[nodiscard]] const ArbUt::BorrowedPtr<const BattleLibrary>& GetLibrary() const noexcept; [[nodiscard]] const ArbUt::BorrowedPtr<const BattleLibrary>& GetLibrary() const noexcept;
[[nodiscard]] uint32_t GetCurrentTurn() const noexcept { return _currentTurn; } [[nodiscard]] uint32_t GetCurrentTurn() const noexcept { return _currentTurn; }
@ -66,7 +57,7 @@ namespace CreatureLib::Battling {
void CheckChoicesSetAndRun(); void CheckChoicesSetAndRun();
[[nodiscard]] ChoiceQueue* GetCurrentTurnQueue() const noexcept; [[nodiscard]] ArbUt::BorrowedPtr<ChoiceQueue> GetCurrentTurnQueue() const noexcept;
BattleRandom* GetRandom() noexcept; BattleRandom* GetRandom() noexcept;
bool CreatureInField(const Creature* creature) const; bool CreatureInField(const Creature* creature) const;
@ -87,8 +78,8 @@ namespace CreatureLib::Battling {
inline bool HasEnded() const noexcept { return _hasEnded; } inline bool HasEnded() const noexcept { return _hasEnded; }
inline const BattleResult& GetResult() const noexcept { return _battleResult; } inline const BattleResult& GetResult() const noexcept { return _battleResult; }
const ArbUt::List<std::unique_ptr<BattleParty>>& GetParties() const noexcept { return _parties; } const ArbUt::UniquePtrList<BattleParty>& GetParties() const noexcept { return _parties; }
const ArbUt::List<BattleSide*>& GetSides() const noexcept { return _sides; } const ArbUt::UniquePtrList<BattleSide>& GetSides() const noexcept { return _sides; }
Script* GetVolatileScript(const ConstString& key) const { return _volatile.Get(key); } Script* GetVolatileScript(const ConstString& key) const { return _volatile.Get(key); }
Script* GetVolatileScript(uint32_t keyHash) const noexcept { return _volatile.Get(keyHash); } Script* GetVolatileScript(uint32_t keyHash) const noexcept { return _volatile.Get(keyHash); }
void AddVolatileScript(const ConstString& key); void AddVolatileScript(const ConstString& key);