CreatureLib/src/Battling/Models/Battle.hpp

108 lines
4.2 KiB
C++
Raw Normal View History

2019-10-17 12:33:25 +00:00
#ifndef CREATURELIB_BATTLE_HPP
#define CREATURELIB_BATTLE_HPP
2020-03-22 12:42:26 +00:00
#include <Arbutils/Assert.hpp>
#include <Arbutils/Collections/List.hpp>
2019-12-15 11:24:08 +00:00
#include "../EventHooks/EventHook.hpp"
#include "../Flow/ChoiceQueue.hpp"
2019-10-17 12:33:25 +00:00
#include "../Library/BattleLibrary.hpp"
#include "../TurnChoices/BaseTurnChoice.hpp"
#include "BattleParty.hpp"
#include "BattleRandom.hpp"
2019-12-15 10:52:10 +00:00
#include "BattleResult.hpp"
#include "BattleSide.hpp"
2019-12-05 08:53:48 +00:00
#include "CreatureIndex.hpp"
2019-10-17 12:33:25 +00:00
using namespace Arbutils::Collections;
2019-10-17 12:33:25 +00:00
namespace CreatureLib::Battling {
class Battle : public ScriptSource {
2019-10-17 12:33:25 +00:00
const BattleLibrary* _library;
List<BattleParty*> _parties;
2019-12-15 10:52:10 +00:00
bool _canFlee;
uint8_t _numberOfSides;
uint8_t _creaturesPerSide;
List<BattleSide*> _sides;
BattleRandom _random;
ChoiceQueue* _currentTurnQueue = nullptr;
bool _hasEnded = false;
2019-12-15 10:52:10 +00:00
BattleResult _battleResult = BattleResult::Empty();
2019-12-15 11:27:56 +00:00
EventHook _eventHook = EventHook();
uint32_t _currentTurn = 0;
ScriptSet _volatile;
2019-10-17 12:33:25 +00:00
public:
Battle(const BattleLibrary* library, List<BattleParty*> parties, bool canFlee = true, uint8_t numberOfSides = 2,
uint8_t creaturesPerSide = 1)
: _library(library), _parties(std::move(parties)), _canFlee(canFlee), _numberOfSides(numberOfSides),
2019-12-15 10:52:10 +00:00
_creaturesPerSide(creaturesPerSide) {
2020-03-22 12:42:26 +00:00
AssertNotNull(_library)
for (auto p : parties)
AssertNotNull(p)
_sides = List<BattleSide*>(numberOfSides);
for (size_t i = 0; i < numberOfSides; i++) {
_sides[i] = new BattleSide(i, this, creaturesPerSide);
}
}
2020-02-17 14:52:02 +00:00
virtual ~Battle() {
for (auto s : _sides) {
delete s;
}
for (auto party : _parties) {
delete party;
}
delete _currentTurnQueue;
}
[[nodiscard]] const BattleLibrary* GetLibrary() const noexcept;
[[nodiscard]] uint32_t GetCurrentTurn() const noexcept { return _currentTurn; }
2019-11-02 12:57:43 +00:00
virtual bool CanUse(const BaseTurnChoice* choice);
virtual bool TrySetChoice(BaseTurnChoice* choice);
bool CanFlee() const noexcept { return _canFlee; }
2019-12-15 10:52:10 +00:00
void CheckChoicesSetAndRun();
[[nodiscard]] ChoiceQueue* GetCurrentTurnQueue() const noexcept;
BattleRandom* GetRandom() noexcept;
2019-11-02 12:57:43 +00:00
bool CreatureInField(const Creature* creature) const;
Creature* GetCreature(const CreatureIndex& target) const {
2019-11-02 12:57:43 +00:00
return _sides[target.GetSideIndex()]->GetCreature(target.GetCreatureIndex());
}
Creature* GetCreature(uint8_t side, uint8_t target) const { return _sides[side]->GetCreature(target); }
void ForceRecall(uint8_t side, uint8_t index);
void SwitchCreature(uint8_t side, uint8_t index, Creature* c);
bool CanSlotBeFilled(uint8_t side, uint8_t index) const;
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override;
void ValidateBattleState();
inline bool HasEnded() const noexcept { return _hasEnded; }
inline const BattleResult& GetResult() const noexcept { return _battleResult; }
2019-12-14 11:40:50 +00:00
const List<BattleSide*>& GetSides() const noexcept { return _sides; }
Script* GetVolatileScript(const ConstString& key) const { return _volatile.Get(key); }
Script* GetVolatileScript(uint32_t keyHash) const noexcept { return _volatile.Get(keyHash); }
void AddVolatileScript(const ConstString& key);
void AddVolatileScript(Script* script);
void RemoveVolatileScript(const ConstString& name) { _volatile.Remove(name); }
void RemoveVolatileScript(uint32_t keyHash) { _volatile.Remove(keyHash); }
void RemoveVolatileScript(Script* script);
bool HasVolatileScript(const ConstString& name) const { return _volatile.Has(name); }
bool HasVolatileScript(uint32_t keyHash) const { return _volatile.Has(keyHash); }
2019-12-15 11:24:08 +00:00
2020-03-09 16:43:36 +00:00
void DisplayText(const std::string& text) const;
2019-12-15 11:27:56 +00:00
void RegisterEventListener(EVENT_HOOK_FUNC(listener)) { this->_eventHook.RegisterListener(listener); }
2020-03-09 16:43:36 +00:00
void TriggerEventListener(EventData* data) const { this->_eventHook.TriggerEvent(data); }
2019-10-17 12:33:25 +00:00
};
}
#endif // CREATURELIB_BATTLE_HPP