When adding volatile script, return the script object.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2021-03-27 16:36:20 +01:00
parent d6858c2d44
commit bfe83ef271
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 14 additions and 13 deletions

View File

@ -153,11 +153,11 @@ void Battle::ValidateBattleState() {
this->_battleResult = BattleResult::Conclusive(winningSide);
EndBattle();
}
void Battle::AddVolatileScript(const ArbUt::StringView& key) {
BattleScript* Battle::AddVolatileScript(const ArbUt::StringView& key) {
auto script = _volatile.Get(key);
if (script.has_value()) {
script.value()->Stack();
return;
return script.value();
}
script = _library->LoadScript(ScriptCategory::Battle, key);
if (!script.has_value()) {
@ -165,6 +165,6 @@ void Battle::AddVolatileScript(const ArbUt::StringView& key) {
}
return _volatile.Add(script.value().GetRaw());
}
void Battle::AddVolatileScript(BattleScript* script) { return _volatile.Add(script); }
BattleScript* Battle::AddVolatileScript(BattleScript* script) { return _volatile.Add(script); }
void Battle::RemoveVolatileScript(BattleScript* script) { _volatile.Remove(script->GetName()); }
void Battle::DisplayText(const ArbUt::StringView& text) { TriggerEventListener<DisplayTextEvent>(text); }

View File

@ -92,8 +92,8 @@ namespace CreatureLib::Battling {
std::optional<ArbUt::BorrowedPtr<BattleScript>> GetVolatileScript(uint32_t keyHash) const noexcept {
return _volatile.Get(keyHash);
}
void AddVolatileScript(const ArbUt::StringView& key);
void AddVolatileScript(BattleScript* script);
BattleScript* AddVolatileScript(const ArbUt::StringView& key);
BattleScript* AddVolatileScript(BattleScript* script);
void RemoveVolatileScript(const ArbUt::BasicStringView& name) { _volatile.Remove(name); }
void RemoveVolatileScript(uint32_t keyHash) { _volatile.Remove(keyHash); }
void RemoveVolatileScript(BattleScript* script);

View File

@ -279,20 +279,20 @@ namespace CreatureLib::Battling {
_heldItem = v.value();
}
void Creature::AddVolatileScript(const ArbUt::StringView& name) {
BattleScript* Creature::AddVolatileScript(const ArbUt::StringView& name) {
auto script = _volatile.Get(name);
if (script.has_value()) {
script.value()->Stack();
return;
return script.value();
}
script = this->_library->LoadScript(ScriptCategory::Creature, name);
if (!script.has_value()) {
THROW("Invalid volatile script requested for creature: '" << name.c_str() << "'.");
}
_volatile.Add(script.value().GetRaw());
return _volatile.Add(script.value().GetRaw());
}
void Creature::AddVolatileScript(BattleScript* script) { _volatile.Add(script); }
BattleScript* Creature::AddVolatileScript(BattleScript* script) { return _volatile.Add(script); }
void Creature::RemoveVolatileScript(const ArbUt::BasicStringView& name) { _volatile.Remove(name); }
void Creature::RemoveVolatileScript(BattleScript* script) { _volatile.Remove(script->GetName()); }
bool Creature::HasVolatileScript(const ArbUt::BasicStringView& name) const { return _volatile.Has(name); }

View File

@ -134,8 +134,8 @@ namespace CreatureLib::Battling {
size_t ScriptCount() const override;
void GetActiveScripts(ArbUt::List<ScriptWrapper>& scripts) override;
void ClearVolatileScripts();
void AddVolatileScript(const ArbUt::StringView& name);
void AddVolatileScript(BattleScript* script);
BattleScript* AddVolatileScript(const ArbUt::StringView& name);
BattleScript* AddVolatileScript(BattleScript* script);
void RemoveVolatileScript(const ArbUt::BasicStringView& name);
void RemoveVolatileScript(BattleScript* script);
bool HasVolatileScript(const ArbUt::BasicStringView& name) const;

View File

@ -15,16 +15,17 @@ namespace CreatureLib::Battling {
static constexpr size_t defaultCapacity = 8;
ScriptSet() : _scripts(defaultCapacity), _lookup(defaultCapacity){};
void Add(BattleScript* script) {
BattleScript* Add(BattleScript* script) {
auto v = _lookup.TryGet(script->GetName());
if (v.has_value()) {
_scripts[v.value()]->Stack();
delete script;
return;
return _scripts[v.value()];
}
_scripts.Append(script);
_lookup.Insert(script->GetName(), _scripts.Count() - 1);
return script;
}
std::optional<ArbUt::BorrowedPtr<BattleScript>> Get(const ArbUt::BasicStringView& key) const {