Use smart pointers for BattleSide.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -39,13 +39,13 @@ void Battle::CheckChoicesSetAndRun() {
|
||||
}
|
||||
}
|
||||
|
||||
auto choices = std::vector<BaseTurnChoice*>(_numberOfSides * _creaturesPerSide);
|
||||
auto choices = std::vector<std::shared_ptr<BaseTurnChoice>>(_numberOfSides * _creaturesPerSide);
|
||||
auto i = 0;
|
||||
for (auto side : _sides) {
|
||||
for (BaseTurnChoice* choice : side->GetChoices()) {
|
||||
for (const auto& choice : side->GetChoices()) {
|
||||
AssertNotNull(choice)
|
||||
if (choice->GetKind() == TurnChoiceKind::Attack) {
|
||||
auto attack = (dynamic_cast<AttackTurnChoice*>((choice)))->GetAttack();
|
||||
auto attack = ((AttackTurnChoice*)choice.get())->GetAttack();
|
||||
uint8_t uses = 1;
|
||||
// HOOK: change number of uses needed.
|
||||
if (attack->GetRemainingUses() < uses) {
|
||||
@@ -73,7 +73,7 @@ ArbUt::BorrowedPtr<ChoiceQueue> Battle::GetCurrentTurnQueue() const noexcept { r
|
||||
|
||||
BattleRandom* Battle::GetRandom() noexcept { return &_random; }
|
||||
|
||||
bool Battle::CreatureInField(const Creature* creature) const {
|
||||
bool Battle::CreatureInField(ArbUt::BorrowedPtr<Creature> creature) const {
|
||||
AssertNotNull(creature)
|
||||
for (auto s : _sides) {
|
||||
if (s->CreatureOnSide(creature))
|
||||
|
||||
@@ -60,12 +60,14 @@ namespace CreatureLib::Battling {
|
||||
[[nodiscard]] ArbUt::BorrowedPtr<ChoiceQueue> GetCurrentTurnQueue() const noexcept;
|
||||
BattleRandom* GetRandom() noexcept;
|
||||
|
||||
bool CreatureInField(const Creature* creature) const;
|
||||
bool CreatureInField(ArbUt::BorrowedPtr<Creature> creature) const;
|
||||
|
||||
Creature* GetCreature(const CreatureIndex& target) const {
|
||||
const ArbUt::BorrowedPtr<Creature>& GetCreature(const CreatureIndex& target) const {
|
||||
return _sides[target.GetSideIndex()]->GetCreature(target.GetCreatureIndex());
|
||||
}
|
||||
Creature* GetCreature(uint8_t side, uint8_t target) const { return _sides[side]->GetCreature(target); }
|
||||
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);
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace CreatureLib::Battling {
|
||||
|
||||
public:
|
||||
BattleRandom() noexcept : _random() {}
|
||||
BattleRandom(uint_fast32_t seed) noexcept : _random(seed) {}
|
||||
explicit BattleRandom(uint_fast32_t seed) noexcept : _random(seed) {}
|
||||
|
||||
bool EffectChance(float chance, ExecutingAttack* attack, Creature* target);
|
||||
int32_t Get() noexcept { return _random.Get(); }
|
||||
|
||||
@@ -26,15 +26,13 @@ void BattleSide::ResetChoices() noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
const ArbUt::List<BaseTurnChoice*>& BattleSide::GetChoices() const noexcept { return _choices; }
|
||||
|
||||
void BattleSide::SetChoice(BaseTurnChoice* choice) {
|
||||
AssertNotNull(choice)
|
||||
auto find = std::find(_creatures.begin(), _creatures.end(), choice->GetUser());
|
||||
if (find == _creatures.end())
|
||||
throw CreatureException("User not found");
|
||||
uint8_t index = std::distance(_creatures.begin(), find);
|
||||
_choices[index] = choice;
|
||||
_choices[index] = std::shared_ptr<BaseTurnChoice>(choice);
|
||||
_choicesSet++;
|
||||
}
|
||||
|
||||
@@ -56,18 +54,18 @@ void BattleSide::SetCreature(Creature* creature, uint8_t index) {
|
||||
for (auto c : side->GetCreatures()) {
|
||||
if (c != nullptr) {
|
||||
c->MarkOpponentAsSeen(creature);
|
||||
creature->MarkOpponentAsSeen(c);
|
||||
creature->MarkOpponentAsSeen(c.GetRaw());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool BattleSide::CreatureOnSide(const Creature* creature) const {
|
||||
bool BattleSide::CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const {
|
||||
AssertNotNull(creature)
|
||||
return std::find(_creatures.begin(), _creatures.end(), creature) != _creatures.end();
|
||||
}
|
||||
|
||||
Creature* BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; }
|
||||
const ArbUt::BorrowedPtr<Creature>& BattleSide::GetCreature(uint8_t index) const { return _creatures[index]; }
|
||||
|
||||
void BattleSide::GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) {
|
||||
scripts.Append(ScriptWrapper::FromSet(&_volatile));
|
||||
|
||||
@@ -11,16 +11,16 @@ namespace CreatureLib::Battling {
|
||||
class BattleSide : public ScriptSource {
|
||||
uint8_t _index;
|
||||
uint8_t _creaturesPerSide;
|
||||
ArbUt::List<Creature*> _creatures;
|
||||
ArbUt::List<BaseTurnChoice*> _choices;
|
||||
ArbUt::List<ArbUt::BorrowedPtr<Creature>> _creatures;
|
||||
ArbUt::List<std::shared_ptr<BaseTurnChoice>> _choices;
|
||||
ArbUt::List<bool> _fillableSlots;
|
||||
uint8_t _choicesSet = 0;
|
||||
ScriptSet _volatile;
|
||||
Battle* _battle;
|
||||
ArbUt::BorrowedPtr<Battle> _battle;
|
||||
bool _hasFled = false;
|
||||
|
||||
public:
|
||||
BattleSide(uint8_t index, Battle* battle, uint8_t creaturesPerSide) noexcept
|
||||
BattleSide(uint8_t index, ArbUt::BorrowedPtr<Battle> battle, uint8_t creaturesPerSide) noexcept
|
||||
: _index(index), _creaturesPerSide(creaturesPerSide), _creatures(creaturesPerSide),
|
||||
_choices(creaturesPerSide), _fillableSlots(creaturesPerSide), _battle(battle) {
|
||||
for (size_t i = 0; i < creaturesPerSide; i++) {
|
||||
@@ -34,7 +34,9 @@ namespace CreatureLib::Battling {
|
||||
virtual ~BattleSide() = default;
|
||||
|
||||
[[nodiscard]] bool AllChoicesSet() const noexcept;
|
||||
[[nodiscard]] const ArbUt::List<BaseTurnChoice*>& GetChoices() const noexcept;
|
||||
[[nodiscard]] const ArbUt::List<std::shared_ptr<BaseTurnChoice>>& GetChoices() const noexcept {
|
||||
return _choices;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool AllPossibleSlotsFilled() const;
|
||||
|
||||
@@ -43,24 +45,24 @@ namespace CreatureLib::Battling {
|
||||
|
||||
void SetCreature(Creature* creature, uint8_t index);
|
||||
|
||||
Creature* GetCreature(uint8_t index) const;
|
||||
bool CreatureOnSide(const Creature* creature) const;
|
||||
const ArbUt::BorrowedPtr<Creature>& GetCreature(uint8_t index) const;
|
||||
bool CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const;
|
||||
|
||||
size_t ScriptCount() const override;
|
||||
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override;
|
||||
|
||||
const ArbUt::List<Creature*>& GetCreatures() { return _creatures; }
|
||||
const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetCreatures() { return _creatures; }
|
||||
|
||||
uint8_t GetSideIndex() noexcept { return _index; }
|
||||
uint8_t GetCreatureIndex(Creature* c) {
|
||||
uint8_t GetCreatureIndex(const ArbUt::BorrowedPtr<Creature>& c) {
|
||||
for (size_t i = 0; i < _creatures.Count(); i++) {
|
||||
if (c == _creatures[i])
|
||||
if (_creatures[i] == c)
|
||||
return i;
|
||||
}
|
||||
throw CreatureException("Unable to find creature on field.");
|
||||
}
|
||||
|
||||
void MarkSlotAsUnfillable(Creature* creature) noexcept {
|
||||
void MarkSlotAsUnfillable(const ArbUt::BorrowedPtr<Creature>& creature) noexcept {
|
||||
for (uint8_t i = 0; i < _creaturesPerSide; i++) {
|
||||
if (_creatures[i] == creature) {
|
||||
_fillableSlots.At(i) = false;
|
||||
|
||||
@@ -45,7 +45,7 @@ const ConstString& Battling::Creature::GetActiveTalent() const {
|
||||
return _variant->GetTalent(_talentIndex);
|
||||
}
|
||||
|
||||
void Battling::Creature::SetBattleData(Battling::Battle* battle, Battling::BattleSide* side) {
|
||||
void Battling::Creature::SetBattleData(ArbUt::BorrowedPtr<Battle> battle, BattleSide* side) {
|
||||
_battle = battle;
|
||||
_side = side;
|
||||
this->ResetActiveScripts();
|
||||
@@ -85,7 +85,7 @@ void Battling::Creature::RecalculateBoostedStat(Library::Statistic stat) {
|
||||
|
||||
// endregion
|
||||
|
||||
Battling::Battle* Battling::Creature::GetBattle() const { return _battle; }
|
||||
const ArbUt::BorrowedPtr<CreatureLib::Battling::Battle>& Battling::Creature::GetBattle() const { return _battle; }
|
||||
|
||||
Battling::BattleSide* Battling::Creature::GetBattleSide() const { return _side; }
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace CreatureLib::Battling {
|
||||
Library::StatisticSet<uint32_t> _flatStats;
|
||||
Library::StatisticSet<uint32_t> _boostedStats;
|
||||
|
||||
Battle* _battle = nullptr;
|
||||
ArbUt::BorrowedPtr<Battle> _battle = nullptr;
|
||||
BattleSide* _side = nullptr;
|
||||
bool _onBattleField = false;
|
||||
|
||||
@@ -101,8 +101,8 @@ namespace CreatureLib::Battling {
|
||||
|
||||
inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; }
|
||||
|
||||
void SetBattleData(Battle* battle, BattleSide* side);
|
||||
Battle* GetBattle() const;
|
||||
void SetBattleData(ArbUt::BorrowedPtr<Battle> battle, BattleSide* side);
|
||||
const ArbUt::BorrowedPtr<Battle>& GetBattle() const;
|
||||
BattleSide* GetBattleSide() const;
|
||||
void SetOnBattleField(bool value) { _onBattleField = value; }
|
||||
bool IsOnBattleField() const { return _onBattleField; }
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace CreatureLib::Battling {
|
||||
};
|
||||
|
||||
private:
|
||||
ArbUt::List<Creature*> _targets;
|
||||
ArbUt::List<ArbUt::BorrowedPtr<Creature>> _targets;
|
||||
uint8_t _numberHits;
|
||||
HitData* _hits;
|
||||
Creature* _user;
|
||||
@@ -44,7 +44,7 @@ namespace CreatureLib::Battling {
|
||||
Script* _script;
|
||||
|
||||
public:
|
||||
ExecutingAttack(const ArbUt::List<Creature*>& targets, uint8_t numberHits, Creature* user,
|
||||
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user,
|
||||
const ArbUt::BorrowedPtr<LearnedAttack>& attack, Script* script)
|
||||
: _targets(targets.Count()), _numberHits(numberHits), _hits(new HitData[targets.Count() * numberHits]),
|
||||
_user(user), _attack(attack), _script(script) {
|
||||
@@ -82,7 +82,7 @@ namespace CreatureLib::Battling {
|
||||
}
|
||||
|
||||
bool IsCreatureTarget(Creature* creature) noexcept { return _targets.IndexOf(creature) != (size_t)-1; }
|
||||
const ArbUt::List<Creature*>& GetTargets() noexcept { return _targets; }
|
||||
const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& GetTargets() noexcept { return _targets; }
|
||||
uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
|
||||
|
||||
Creature* GetUser() noexcept { return _user; }
|
||||
|
||||
Reference in New Issue
Block a user