C interface for Battle Side.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-04 17:03:06 +02:00
parent 3429f1647c
commit 3dd5aeffd8
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 57 additions and 15 deletions

View File

@ -0,0 +1,42 @@
#include "../../src/Battling/Models/BattleSide.hpp"
#include "../Core.hpp"
using namespace CreatureLib::Battling;
export BattleSide* CreatureLib_BattleSide_Construct(uint8_t index, Battle* battle, uint8_t creaturesPerSide) {
return new BattleSide(index, battle, creaturesPerSide);
}
export void CreatureLib_BattleSide_Destruct(BattleSide* p) { delete p; }
export bool CreatureLib_BattleSide_AllChoicesSet(BattleSide* p) { return p->AllChoicesSet(); }
export BaseTurnChoice* const* CreatureLib_BattleSide_GetChoices(BattleSide* p) { return p->GetChoices().RawData(); }
export uint8_t CreatureLib_BattleSide_AllPossibleSlotsFilled(bool& out, BattleSide* p) {
Try(out = p->AllPossibleSlotsFilled());
}
export uint8_t CreatureLib_BattleSide_SetChoice(BattleSide* p, BaseTurnChoice* choice) { Try(p->SetChoice(choice);) }
export void CreatureLib_BattleSide_ResetChoices(BattleSide* p) { p->ResetChoices(); }
export uint8_t CreatureLib_BattleSide_SetCreature(BattleSide* p, Creature* creature, uint8_t index) {
Try(p->SetCreature(creature, index));
}
export uint8_t CreatureLib_BattleSide_GetCreature(Creature*& out, BattleSide* p, uint8_t index) {
Try(out = p->GetCreature(index));
}
export uint8_t CreatureLib_BattleSide_GetSideIndex(BattleSide* p) { return p->GetSideIndex(); }
export uint8_t CreatureLib_BattleSide_GetCreatureIndex(uint8_t& out, BattleSide* p, Creature* c) {
Try(out = p->GetCreatureIndex(c));
}
export uint8_t CreatureLib_BattleSide_MarkSlotAsUnfillable(BattleSide* p, Creature* c) {
Try(p->MarkSlotAsUnfillable(c));
}
export bool CreatureLib_BattleSide_IsDefeated(BattleSide* p) { return p->IsDefeated(); }
export bool CreatureLib_BattleSide_HasFled(BattleSide* p) { return p->HasFled(); }
export void CreatureLib_BattleSide_MarkAsFled(BattleSide* p) { p->MarkAsFled(); }

View File

@ -5,7 +5,7 @@
using namespace CreatureLib::Battling; using namespace CreatureLib::Battling;
bool BattleSide::AllChoicesSet() const { return _choicesSet == _creaturesPerSide; } bool BattleSide::AllChoicesSet() const noexcept { return _choicesSet == _creaturesPerSide; }
bool BattleSide::AllPossibleSlotsFilled() const { bool BattleSide::AllPossibleSlotsFilled() const {
AssertNotNull(_battle) AssertNotNull(_battle)
@ -19,14 +19,14 @@ bool BattleSide::AllPossibleSlotsFilled() const {
return true; return true;
} }
void BattleSide::ResetChoices() { void BattleSide::ResetChoices() noexcept {
_choicesSet = 0; _choicesSet = 0;
for (uint8_t i = 0; i < _creaturesPerSide; i++) { for (uint8_t i = 0; i < _creaturesPerSide; i++) {
_choices[i] = nullptr; _choices[i] = nullptr;
} }
} }
const List<BaseTurnChoice*>& BattleSide::GetChoices() const { return _choices; } const List<BaseTurnChoice*>& BattleSide::GetChoices() const noexcept { return _choices; }
void BattleSide::SetChoice(BaseTurnChoice* choice) { void BattleSide::SetChoice(BaseTurnChoice* choice) {
AssertNotNull(choice) AssertNotNull(choice)

View File

@ -22,7 +22,7 @@ namespace CreatureLib::Battling {
bool _hasFled = false; bool _hasFled = false;
public: public:
explicit BattleSide(uint8_t index, Battle* battle, uint8_t creaturesPerSide) BattleSide(uint8_t index, Battle* battle, uint8_t creaturesPerSide) noexcept
: _index(index), _creaturesPerSide(creaturesPerSide), _creatures(creaturesPerSide), : _index(index), _creaturesPerSide(creaturesPerSide), _creatures(creaturesPerSide),
_choices(creaturesPerSide), _fillableSlots(creaturesPerSide), _battle(battle) { _choices(creaturesPerSide), _fillableSlots(creaturesPerSide), _battle(battle) {
for (size_t i = 0; i < creaturesPerSide; i++) { for (size_t i = 0; i < creaturesPerSide; i++) {
@ -35,24 +35,24 @@ namespace CreatureLib::Battling {
virtual ~BattleSide() = default; virtual ~BattleSide() = default;
[[nodiscard]] bool AllChoicesSet() const; [[nodiscard]] bool AllChoicesSet() const noexcept;
[[nodiscard]] const List<BaseTurnChoice*>& GetChoices() const; [[nodiscard]] const List<BaseTurnChoice*>& GetChoices() const noexcept;
[[nodiscard]] bool AllPossibleSlotsFilled() const; [[nodiscard]] bool AllPossibleSlotsFilled() const;
void SetChoice(BaseTurnChoice* choice); void SetChoice(BaseTurnChoice* choice);
void ResetChoices(); void ResetChoices() noexcept;
void SetCreature(Creature* creature, uint8_t index); void SetCreature(Creature* creature, uint8_t index);
Creature* GetCreature(uint8_t index) const; Creature* GetCreature(uint8_t index) const;
bool CreatureOnSide(const Creature* creature) const; bool CreatureOnSide(const Creature* creature) const;
void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) final; void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) override;
const List<Creature*>& GetCreatures() { return _creatures; } const List<Creature*>& GetCreatures() { return _creatures; }
uint8_t GetSideIndex() { return _index; } uint8_t GetSideIndex() noexcept { return _index; }
uint8_t GetCreatureIndex(Creature* c) { uint8_t GetCreatureIndex(Creature* c) {
for (size_t i = 0; i < _creatures.Count(); i++) { for (size_t i = 0; i < _creatures.Count(); i++) {
if (c == _creatures[i]) if (c == _creatures[i])
@ -61,7 +61,7 @@ namespace CreatureLib::Battling {
throw CreatureException("Unable to find creature on field."); throw CreatureException("Unable to find creature on field.");
} }
void MarkSlotAsUnfillable(Creature* creature) { void MarkSlotAsUnfillable(Creature* creature) noexcept {
for (uint8_t i = 0; i < _creaturesPerSide; i++) { for (uint8_t i = 0; i < _creaturesPerSide; i++) {
if (_creatures[i] == creature) { if (_creatures[i] == creature) {
_fillableSlots.At(i) = false; _fillableSlots.At(i) = false;
@ -70,7 +70,7 @@ namespace CreatureLib::Battling {
} }
} }
bool IsDefeated() { bool IsDefeated() noexcept {
for (auto b : _fillableSlots) { for (auto b : _fillableSlots) {
if (b) if (b)
return false; return false;
@ -78,9 +78,9 @@ namespace CreatureLib::Battling {
return true; return true;
} }
bool HasFled() { return _hasFled; } bool HasFled() noexcept { return _hasFled; }
void MarkAsFled() { _hasFled = true; } void MarkAsFled() noexcept { _hasFled = true; }
uint8_t GetRandomCreatureIndex(); uint8_t GetRandomCreatureIndex();
}; };

View File

@ -103,8 +103,8 @@ void Battling::Creature::OnFaint() {
_battle->TriggerEventListener(new FaintEvent(this)); _battle->TriggerEventListener(new FaintEvent(this));
} }
_library->GetExperienceLibrary()->HandleExperienceGain(this, _seenOpponents); _library->GetExperienceLibrary()->HandleExperienceGain(this, _seenOpponents);
auto sideIndex = _side->GetCreatureIndex(this);
if (!_battle->CanSlotBeFilled(_side->GetSideIndex(), _side->GetCreatureIndex(this))) { if (!_battle->CanSlotBeFilled(_side->GetSideIndex(), sideIndex)) {
_side->MarkSlotAsUnfillable(this); _side->MarkSlotAsUnfillable(this);
} }
_battle->ValidateBattleState(); _battle->ValidateBattleState();