Tweaks for Battle and ScriptSet, added Battle C interface.
All checks were successful
continuous-integration/drone/push Build is passing

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

View File

@@ -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<Creature*> targets = {target};
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);
}
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); }

View File

@@ -31,7 +31,7 @@ namespace CreatureLib::Battling {
public:
Battle(const BattleLibrary* library, std::vector<BattleParty*> 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<BattleSide*>(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<BattleSide*>& 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); }

View File

@@ -8,7 +8,7 @@
namespace CreatureLib::Battling {
class ScriptSet {
std::vector<Script*> _scripts;
std::unordered_map<ConstString, size_t> _lookup;
std::unordered_map<uint32_t, size_t> _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(); }