From ce2fc320bdb21410a2cd4cfe09c6cf983b31ae0d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Mon, 9 Mar 2020 10:16:57 +0100 Subject: [PATCH] Tweaks for Battle and ScriptSet, added Battle C interface. --- CInterface/Battling/Battle.cpp | 60 +++++++++++++++++++++++ src/Battling/Flow/TurnHandler.cpp | 2 +- src/Battling/Models/Battle.cpp | 2 - src/Battling/Models/Battle.hpp | 12 +++-- src/Battling/ScriptHandling/ScriptSet.hpp | 23 +++++---- tests/Integration/BattleIntegrations.cpp | 4 +- 6 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 CInterface/Battling/Battle.cpp diff --git a/CInterface/Battling/Battle.cpp b/CInterface/Battling/Battle.cpp new file mode 100644 index 0000000..9c3d123 --- /dev/null +++ b/CInterface/Battling/Battle.cpp @@ -0,0 +1,60 @@ +#include "../../src/Battling/Models/Battle.hpp" +#define export extern "C" +using namespace CreatureLib::Battling; + +export Battle* CreatureLib_Battle_Construct(const BattleLibrary* library, BattleParty* partyArr[], + size_t numberOfParties, bool canFlee, uint8_t numberOfSides, + uint8_t creaturesPerSide) { + std::vector parties(partyArr, partyArr + numberOfParties); + return new Battle(library, parties, canFlee, numberOfSides, creaturesPerSide); +} + +export void CreatureLib_Battle_Destruct(const Battle* p) { delete p; } + +const BattleLibrary* CreatureLib_Battle_GetLibrary(const Battle* p) { return p->GetLibrary(); } +bool CreatureLib_Battle_CanUse(Battle* p, const BaseTurnChoice* turnChoice) { return p->CanUse(turnChoice); } +bool CreatureLib_Battle_TrySetChoice(Battle* p, BaseTurnChoice* turnChoice) { return p->TrySetChoice(turnChoice); } +bool CreatureLib_Battle_CanFlee(const Battle* p) { return p->CanFlee(); } +void CreatureLib_Battle_CheckChoicesSetAndRun(Battle* p) { p->CheckChoicesSetAndRun(); } + +ChoiceQueue* CreatureLib_Battle_GetCurrentTurnQueue(const Battle* p) { return p->GetCurrentTurnQueue(); } +BattleRandom* CreatureLib_Battle_GetRandom(Battle* p) { return p->GetRandom(); } +bool CreatureLib_Battle_CreatureInField(const Battle* p, Creature* c) { return p->CreatureInField(c); } +Creature* CreatureLib_Battle_GetCreature(const Battle* p, uint8_t side, uint8_t target) { + return p->GetCreature(side, target); +} + +void CreatureLib_Battle_ForceRecall(Battle* p, uint8_t side, uint8_t target) { p->ForceRecall(side, target); } +void CreatureLib_Battle_SwitchCreature(Battle* p, uint8_t side, uint8_t target, Creature* c) { + p->SwitchCreature(side, target, c); +} +bool CreatureLib_Battle_CanSlotBeFilled(const Battle* p, uint8_t side, uint8_t target) { + return p->CanSlotBeFilled(side, target); +} +void CreatureLib_Battle_ValidateBattleState(Battle* p) { p->ValidateBattleState(); } +bool CreatureLib_Battle_HasEnded(const Battle* p) { return p->HasEnded(); } + +bool CreatureLib_Battle_HasConclusiveResult(const Battle* p) { return p->GetResult().IsConclusiveResult(); } +bool CreatureLib_Battle_GetWinningSide(const Battle* p) { return p->GetResult().GetWinningSide(); } + +size_t CreatureLib_Battle_GetSidesCount(const Battle* p) { return p->GetSides().size(); } +BattleSide* const* CreatureLib_Battle_GetSides(const Battle* p) { return p->GetSides().data(); } + +Script* CreatureLib_Battle_GetVolatileScript(Battle* p, const char* key) { + return p->GetVolatileScript(ConstString::GetHash(key)); +} +void CreatureLib_Battle_AddVolatileScriptByName(Battle* p, const char* key) { p->AddVolatileScript(ConstString(key)); } +void CreatureLib_Battle_AddVolatileScript(Battle* p, Script* script) { p->AddVolatileScript(script); } + +void CreatureLib_Battle_RemoveVolatileScript(Battle* p, const char* key) { + p->RemoveVolatileScript(ConstString::GetHash(key)); +} +void CreatureLib_Battle_RemoveVolatileScriptWithScript(Battle* p, Script* script) { p->RemoveVolatileScript(script); } +bool CreatureLib_Battle_HasVolatileScript(Battle* p, const char* key) { + return p->HasVolatileScript(ConstString::GetHash(key)); +} + +void CreatureLib_Battle_RegisterEventListener(Battle* p, void (*func)(const EventData*)) { + p->RegisterEventListener(func); +} +void CreatureLib_Battle_TriggerEventListener(Battle* p, EventData* data) { p->TriggerEventListener(data); } diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index fafce78..3634205 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -58,7 +58,7 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) { } // FIXME: Resolve all targets - auto target = choice->GetUser()->GetBattle()->GetTarget(choice->GetTarget()); + auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget()); std::vector targets = {target}; auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); diff --git a/src/Battling/Models/Battle.cpp b/src/Battling/Models/Battle.cpp index 869de53..931c81a 100644 --- a/src/Battling/Models/Battle.cpp +++ b/src/Battling/Models/Battle.cpp @@ -129,6 +129,4 @@ void Battle::AddVolatileScript(const ConstString& key) { return _volatile.Add(script); } void Battle::AddVolatileScript(Script* script) { return _volatile.Add(script); } -void Battle::RemoveVolatileScript(const ConstString& name) { _volatile.Remove(name); } void Battle::RemoveVolatileScript(Script* script) { _volatile.Remove(script->GetName()); } -void Battle::HasVolatileScript(const ConstString& name) const { _volatile.Has(name); } diff --git a/src/Battling/Models/Battle.hpp b/src/Battling/Models/Battle.hpp index 09bedbb..27b476a 100644 --- a/src/Battling/Models/Battle.hpp +++ b/src/Battling/Models/Battle.hpp @@ -31,7 +31,7 @@ namespace CreatureLib::Battling { public: Battle(const BattleLibrary* library, std::vector parties, bool canFlee = true, uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1) - : _library(library), _parties(parties), _canFlee(canFlee), _numberOfSides(numberOfSides), + : _library(library), _parties(std::move(parties)), _canFlee(canFlee), _numberOfSides(numberOfSides), _creaturesPerSide(creaturesPerSide) { _sides = std::vector(numberOfSides); for (size_t i = 0; i < numberOfSides; i++) { @@ -63,9 +63,10 @@ namespace CreatureLib::Battling { bool CreatureInField(const Creature* creature) const; - Creature* GetTarget(const CreatureIndex& target) { + 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); } void ForceRecall(uint8_t side, uint8_t index); void SwitchCreature(uint8_t side, uint8_t index, Creature* c); @@ -79,11 +80,14 @@ namespace CreatureLib::Battling { const std::vector& GetSides() const { return _sides; } Script* GetVolatileScript(const ConstString& key) const { return _volatile.Get(key); } + Script* GetVolatileScript(uint32_t keyHash) const { return _volatile.Get(keyHash); } void AddVolatileScript(const ConstString& key); void AddVolatileScript(Script* script); - void RemoveVolatileScript(const ConstString& name); + void RemoveVolatileScript(const ConstString& name) { _volatile.Remove(name); } + void RemoveVolatileScript(uint32_t keyHash) { _volatile.Remove(keyHash); } void RemoveVolatileScript(Script* script); - void HasVolatileScript(const ConstString& name) const; + bool HasVolatileScript(const ConstString& name) const { return _volatile.Has(name); } + bool HasVolatileScript(uint32_t keyHash) const { return _volatile.Has(keyHash); } void RegisterEventListener(EVENT_HOOK_FUNC(listener)) { this->_eventHook.RegisterListener(listener); } void TriggerEventListener(EventData* data) { this->_eventHook.TriggerEvent(data); } diff --git a/src/Battling/ScriptHandling/ScriptSet.hpp b/src/Battling/ScriptHandling/ScriptSet.hpp index ec01f93..4038980 100644 --- a/src/Battling/ScriptHandling/ScriptSet.hpp +++ b/src/Battling/ScriptHandling/ScriptSet.hpp @@ -8,7 +8,7 @@ namespace CreatureLib::Battling { class ScriptSet { std::vector _scripts; - std::unordered_map _lookup; + std::unordered_map _lookup; public: ~ScriptSet() { @@ -28,22 +28,26 @@ namespace CreatureLib::Battling { _lookup.insert({script->GetName(), _scripts.size() - 1}); } - Script* Get(const ConstString& key) const { - auto f = _lookup.find(key); + Script* Get(const ConstString& key) const { return Get(key.GetHash()); } + + Script* Get(uint32_t keyHash) const { + auto f = _lookup.find(keyHash); if (f != _lookup.end()) { return _scripts[f->second]; } return nullptr; } - void Remove(const ConstString& key) { - auto find = _lookup.find(key); + void Remove(const ConstString& key) { Remove(key.GetHash()); } + + void Remove(uint32_t keyHash) { + auto find = _lookup.find(keyHash); if (find != _lookup.end()) { auto script = _scripts[find->second]; script->OnRemove(); delete script; _scripts.erase(_scripts.begin() + find.operator*().second); - _lookup.erase(key); + _lookup.erase(keyHash); } } @@ -55,10 +59,9 @@ namespace CreatureLib::Battling { _lookup.clear(); } - bool Has(const ConstString& key) const { - auto find = _lookup.find(key); - return find != _lookup.end(); - } + bool Has(const ConstString& key) const { return _lookup.find(key) != _lookup.end(); } + + bool Has(uint32_t keyHash) const { return _lookup.find(keyHash) != _lookup.end(); } size_t Count() const { return _scripts.size(); } diff --git a/tests/Integration/BattleIntegrations.cpp b/tests/Integration/BattleIntegrations.cpp index 6d5c2b9..2feadf3 100644 --- a/tests/Integration/BattleIntegrations.cpp +++ b/tests/Integration/BattleIntegrations.cpp @@ -176,12 +176,12 @@ TEST_CASE("Switch Creature in", "[Integrations]") { battle.SwitchCreature(0, 0, c1); battle.SwitchCreature(1, 0, c3); - REQUIRE(battle.GetTarget(CreatureIndex(0, 0)) == c1); + REQUIRE(battle.GetCreature(CreatureIndex(0, 0)) == c1); battle.TrySetChoice(new SwitchTurnChoice(c1, c2)); battle.TrySetChoice(new PassTurnChoice(c3)); - REQUIRE(battle.GetTarget(CreatureIndex(0, 0)) == c2); + REQUIRE(battle.GetCreature(CreatureIndex(0, 0)) == c2); } TEST_CASE("Switch Creature in, but have attack aimed at it. Attack should hit new creature", "[Integrations]") {