Implemented better script handling.

This commit is contained in:
2019-11-09 12:15:45 +01:00
parent c3bfbb569e
commit ee14efe22e
7 changed files with 121 additions and 15 deletions

View File

@@ -8,6 +8,9 @@
#include "Hooks.hpp"
namespace CreatureLib::Battling{
class ExecutingAttack;
class Creature;
class Script{
const std::string _name;
@@ -15,12 +18,15 @@ namespace CreatureLib::Battling{
explicit Script(std::string name) :_name(std::move(name)){}
virtual ~Script() = default;
virtual void Execute(Hook hook, const std::vector<std::any>& args){};
virtual void Stack(){};
const std::string& GetName(){
return _name;
}
virtual void OnIncomingAttackFails(ExecutingAttack* attack, Creature* target){};
virtual void IsInvulnerable(ExecutingAttack* attack, Creature* target){};
virtual void OnAttackMiss(ExecutingAttack* attack, Creature* target){};
};
}

View File

@@ -0,0 +1,66 @@
#ifndef CREATURELIB_SCRIPTAGGREGATOR_HPP
#define CREATURELIB_SCRIPTAGGREGATOR_HPP
#include <any>
#include <queue>
#include "Script.hpp"
#include "ScriptSet.hpp"
namespace CreatureLib::Battling{
class ScriptAggregator{
std::queue<std::any> _queue;
bool _isSetSet = false;
std::__detail::_Node_iterator<std::pair<const std::string, Script *>, false, true> _setIterator;
std::__detail::_Node_iterator<std::pair<const std::string, Script *>, false, true> _setEnd;
public:
ScriptAggregator() = default;
void Add(Script* script){
_queue.push(script);
}
void Add(ScriptSet* scriptSet){
_queue.push(scriptSet);
}
bool HasNext(){
return !_queue.empty() || _isSetSet;
}
Script* GetNext(){
// We can probably do this in a cleaner version once C++ 20 drops with Coroutine support.
if (_isSetSet){
if (_setIterator == _setEnd){
_isSetSet = false;
return GetNext();
}
auto s = _setIterator->second;
_setIterator.operator++();
if (_setIterator == _setEnd){
_isSetSet = false;
}
return s;
}
if (_queue.empty())
return nullptr;
auto next = _queue.front();
_queue.pop();
if (next.type() == typeid(Script*)){
return std::any_cast<Script*>(next);
}
else{
auto set = std::any_cast<ScriptSet*>(next);
if (set->Count() == 0)
return GetNext();
auto it = set->GetIterator();
_setIterator = it->begin();
_setEnd = it->end();
_isSetSet = true;
return GetNext();
}
throw NotReachableException();
}
};
}
#endif //CREATURELIB_SCRIPTAGGREGATOR_HPP

View File

@@ -0,0 +1,14 @@
#define HOOK(hookName, sources, ... ) \
{ \
auto aggregator = CreatureLib::Battling::ScriptAggregator(); \
for (auto source: sources){ \
source -> GetActiveScripts(aggregator); \
} \
while (aggregator.HasNext()){ \
auto next = aggregator.GetNext(); \
if (next == nullptr) continue; \
next->hookName(__VA_ARGS__); \
} \
}

View File

@@ -9,12 +9,6 @@ namespace CreatureLib::Battling{
class ScriptSet{
std::unordered_map<std::string, Script*> _scripts;
public:
void Execute(Hook hook, const std::vector<std::any>& args){
for (auto s: _scripts){
s.second->Execute(hook, args);
}
}
void Add(Script* script){
auto f = _scripts.find(script->GetName());
if (f != _scripts.end()){
@@ -28,6 +22,14 @@ namespace CreatureLib::Battling{
void Remove(const std::string& key){
_scripts.erase(key);
}
size_t Count() const{
return _scripts.size();
}
std::unordered_map<std::string, Script *> * GetIterator(){
return &_scripts;
}
};
}

View File

@@ -0,0 +1,15 @@
#ifndef CREATURELIB_SCRIPTSOURCE_HPP
#define CREATURELIB_SCRIPTSOURCE_HPP
#include "ScriptAggregator.hpp"
namespace CreatureLib::Battling{
class ScriptSource {
public:
virtual void GetActiveScripts(ScriptAggregator& aggr) = 0;
};
}
#endif //CREATURELIB_SCRIPTSOURCE_HPP