Tweaks for Battle and ScriptSet, added Battle C interface.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-03-09 10:16:57 +01:00
parent 1afb13cfd1
commit ce2fc320bd
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 84 additions and 19 deletions

View File

@ -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<BattleParty*> 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); }

View File

@ -58,7 +58,7 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
} }
// FIXME: Resolve all targets // FIXME: Resolve all targets
auto target = choice->GetUser()->GetBattle()->GetTarget(choice->GetTarget()); auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget());
std::vector<Creature*> targets = {target}; std::vector<Creature*> targets = {target};
auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());

View File

@ -129,6 +129,4 @@ void Battle::AddVolatileScript(const ConstString& key) {
return _volatile.Add(script); return _volatile.Add(script);
} }
void Battle::AddVolatileScript(Script* script) { 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::RemoveVolatileScript(Script* script) { _volatile.Remove(script->GetName()); }
void Battle::HasVolatileScript(const ConstString& name) const { _volatile.Has(name); }

View File

@ -31,7 +31,7 @@ namespace CreatureLib::Battling {
public: public:
Battle(const BattleLibrary* library, std::vector<BattleParty*> parties, bool canFlee = true, Battle(const BattleLibrary* library, std::vector<BattleParty*> parties, bool canFlee = true,
uint8_t numberOfSides = 2, uint8_t creaturesPerSide = 1) 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) { _creaturesPerSide(creaturesPerSide) {
_sides = std::vector<BattleSide*>(numberOfSides); _sides = std::vector<BattleSide*>(numberOfSides);
for (size_t i = 0; i < numberOfSides; i++) { for (size_t i = 0; i < numberOfSides; i++) {
@ -63,9 +63,10 @@ namespace CreatureLib::Battling {
bool CreatureInField(const Creature* creature) const; bool CreatureInField(const Creature* creature) const;
Creature* GetTarget(const CreatureIndex& target) { Creature* GetCreature(const CreatureIndex& target) const {
return _sides[target.GetSideIndex()]->GetCreature(target.GetCreatureIndex()); 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 ForceRecall(uint8_t side, uint8_t index);
void SwitchCreature(uint8_t side, uint8_t index, Creature* c); void SwitchCreature(uint8_t side, uint8_t index, Creature* c);
@ -79,11 +80,14 @@ namespace CreatureLib::Battling {
const std::vector<BattleSide*>& GetSides() const { return _sides; } const std::vector<BattleSide*>& GetSides() const { return _sides; }
Script* GetVolatileScript(const ConstString& key) const { return _volatile.Get(key); } 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(const ConstString& key);
void AddVolatileScript(Script* script); 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 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 RegisterEventListener(EVENT_HOOK_FUNC(listener)) { this->_eventHook.RegisterListener(listener); }
void TriggerEventListener(EventData* data) { this->_eventHook.TriggerEvent(data); } void TriggerEventListener(EventData* data) { this->_eventHook.TriggerEvent(data); }

View File

@ -8,7 +8,7 @@
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class ScriptSet { class ScriptSet {
std::vector<Script*> _scripts; std::vector<Script*> _scripts;
std::unordered_map<ConstString, size_t> _lookup; std::unordered_map<uint32_t, size_t> _lookup;
public: public:
~ScriptSet() { ~ScriptSet() {
@ -28,22 +28,26 @@ namespace CreatureLib::Battling {
_lookup.insert({script->GetName(), _scripts.size() - 1}); _lookup.insert({script->GetName(), _scripts.size() - 1});
} }
Script* Get(const ConstString& key) const { Script* Get(const ConstString& key) const { return Get(key.GetHash()); }
auto f = _lookup.find(key);
Script* Get(uint32_t keyHash) const {
auto f = _lookup.find(keyHash);
if (f != _lookup.end()) { if (f != _lookup.end()) {
return _scripts[f->second]; return _scripts[f->second];
} }
return nullptr; return nullptr;
} }
void Remove(const ConstString& key) { void Remove(const ConstString& key) { Remove(key.GetHash()); }
auto find = _lookup.find(key);
void Remove(uint32_t keyHash) {
auto find = _lookup.find(keyHash);
if (find != _lookup.end()) { if (find != _lookup.end()) {
auto script = _scripts[find->second]; auto script = _scripts[find->second];
script->OnRemove(); script->OnRemove();
delete script; delete script;
_scripts.erase(_scripts.begin() + find.operator*().second); _scripts.erase(_scripts.begin() + find.operator*().second);
_lookup.erase(key); _lookup.erase(keyHash);
} }
} }
@ -55,10 +59,9 @@ namespace CreatureLib::Battling {
_lookup.clear(); _lookup.clear();
} }
bool Has(const ConstString& key) const { bool Has(const ConstString& key) const { return _lookup.find(key) != _lookup.end(); }
auto find = _lookup.find(key);
return find != _lookup.end(); bool Has(uint32_t keyHash) const { return _lookup.find(keyHash) != _lookup.end(); }
}
size_t Count() const { return _scripts.size(); } size_t Count() const { return _scripts.size(); }

View File

@ -176,12 +176,12 @@ TEST_CASE("Switch Creature in", "[Integrations]") {
battle.SwitchCreature(0, 0, c1); battle.SwitchCreature(0, 0, c1);
battle.SwitchCreature(1, 0, c3); 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 SwitchTurnChoice(c1, c2));
battle.TrySetChoice(new PassTurnChoice(c3)); 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]") { TEST_CASE("Switch Creature in, but have attack aimed at it. Attack should hit new creature", "[Integrations]") {