CreatureLib/src/Battling/Models/Battle.hpp

109 lines
4.9 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>
2020-05-26 16:31:06 +00:00
#include <memory>
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
namespace CreatureLib::Battling {
class Battle : public ScriptSource {
protected:
2020-05-26 16:31:06 +00:00
ArbUt::BorrowedPtr<const BattleLibrary> _library;
ArbUt::UniquePtrList<BattleParty> _parties;
2019-12-15 10:52:10 +00:00
bool _canFlee;
uint8_t _numberOfSides;
uint8_t _creaturesPerSide;
ArbUt::UniquePtrList<BattleSide> _sides;
BattleRandom _random;
std::unique_ptr<ChoiceQueue> _currentTurnQueue = nullptr;
bool _hasEnded = false;
2019-12-15 10:52:10 +00:00
BattleResult _battleResult = BattleResult::Empty();
2020-07-31 17:52:12 +00:00
EventHook _eventHook;
uint32_t _currentTurn = 0;
ScriptSet _volatile;
2019-10-17 12:33:25 +00:00
public:
2020-05-26 16:31:06 +00:00
Battle(const BattleLibrary* library, ArbUt::List<BattleParty*> parties, bool canFlee = true,
2020-07-26 08:16:25 +00:00
uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1,
uint_fast32_t randomSeed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count())
: _library(library), _parties(parties.GetStdList()), _canFlee(canFlee), _numberOfSides(numberOfSides),
2020-07-26 08:16:25 +00:00
_creaturesPerSide(creaturesPerSide), _sides(numberOfSides), _random(randomSeed) {
2020-04-25 08:41:15 +00:00
AssertNotNull(_library);
AssertAllNotNull(parties);
2020-03-22 12:42:26 +00:00
for (size_t i = 0; i < numberOfSides; i++) {
2020-04-25 08:41:15 +00:00
_sides.Append(new BattleSide(i, this, creaturesPerSide));
}
}
virtual ~Battle() = default;
2020-05-26 16:31:06 +00:00
[[nodiscard]] const ArbUt::BorrowedPtr<const BattleLibrary>& GetLibrary() const noexcept;
[[nodiscard]] uint32_t GetCurrentTurn() const noexcept { return _currentTurn; }
inline uint8_t GetCreaturesPerSide() const noexcept { return _creaturesPerSide; }
2020-05-31 16:00:39 +00:00
virtual bool CanUse(const ArbUt::BorrowedPtr<BaseTurnChoice>& choice);
virtual bool TrySetChoice(BaseTurnChoice* choice);
bool CanFlee() const noexcept { return _canFlee; }
2019-12-15 10:52:10 +00:00
void CheckChoicesSetAndRun();
[[nodiscard]] ArbUt::BorrowedPtr<ChoiceQueue> GetCurrentTurnQueue() const noexcept;
BattleRandom* GetRandom() noexcept;
2019-11-02 12:57:43 +00:00
2020-06-02 11:06:24 +00:00
bool CreatureInField(ArbUt::BorrowedPtr<Creature> creature) const;
2019-11-02 12:57:43 +00:00
2020-06-02 11:06:24 +00:00
const ArbUt::BorrowedPtr<Creature>& GetCreature(const CreatureIndex& target) const {
2019-11-02 12:57:43 +00:00
return _sides[target.GetSideIndex()]->GetCreature(target.GetCreatureIndex());
}
2020-06-02 11:06:24 +00:00
const ArbUt::BorrowedPtr<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;
size_t ScriptCount() const override;
2020-05-26 16:31:06 +00:00
void GetActiveScripts(ArbUt::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 ArbUt::UniquePtrList<BattleParty>& GetParties() const noexcept { return _parties; }
const ArbUt::UniquePtrList<BattleSide>& GetSides() const noexcept { return _sides; }
2020-06-26 15:08:23 +00:00
ArbUt::BorrowedPtr<Script> GetVolatileScript(const ArbUt::StringView& key) const { return _volatile.Get(key); }
2020-06-02 13:03:31 +00:00
ArbUt::BorrowedPtr<Script> GetVolatileScript(uint32_t keyHash) const noexcept { return _volatile.Get(keyHash); }
2020-06-26 16:23:40 +00:00
void AddVolatileScript(const ArbUt::StringView& key);
void AddVolatileScript(Script* script);
2020-06-26 15:08:23 +00:00
void RemoveVolatileScript(const ArbUt::BasicStringView& name) { _volatile.Remove(name); }
void RemoveVolatileScript(uint32_t keyHash) { _volatile.Remove(keyHash); }
void RemoveVolatileScript(Script* script);
2020-06-26 15:08:23 +00:00
bool HasVolatileScript(const ArbUt::BasicStringView& name) const { return _volatile.Has(name); }
bool HasVolatileScript(uint32_t keyHash) const { return _volatile.Has(keyHash); }
const EventHook& GetEventHook() const noexcept { return _eventHook; }
2019-12-15 11:24:08 +00:00
void DisplayText(const ArbUt::StringView& text);
void RegisterEventListener(EventHook::EventHookFunc listener) { this->_eventHook.RegisterListener(listener); }
template <class T, class... parameters> void TriggerEventListener(parameters... args) {
this->_eventHook.Trigger<T>(args...);
}
2019-10-17 12:33:25 +00:00
};
}
#endif // CREATURELIB_BATTLE_HPP