From 5e48cf6333aca7af6854d79194f138d57ce0b5e1 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 14 May 2018 18:40:54 -0400 Subject: Specialized treatment of behavior scripts The AutomatableComponent now links to the Runnable entity representing the behavior script. Also reordered the SystemManager and EntityManager members of the Game class such that the EntityManager is destroyed before the SystemManager is. This fixes a bug where the destruction of a component has some affect on the state of a system. Specifically, if the ScriptingSystem (and thus the Lua state) is destroyed before the EntityManager is and there are any Runnable entities, the game will crash when trying to destroy them. --- src/components/automatable.h | 34 +++++++++++++++++++++ src/components/prototypable.h | 11 +++---- src/components/runnable.h | 36 ++++++++++++++++++++++ src/game.h | 2 +- src/systems/realizing.cpp | 33 ++++++-------------- src/systems/scripting.cpp | 70 ++++++++++++++++++++++++++++++++++++------- src/systems/scripting.h | 10 +++++-- 7 files changed, 153 insertions(+), 43 deletions(-) create mode 100644 src/components/automatable.h diff --git a/src/components/automatable.h b/src/components/automatable.h new file mode 100644 index 0000000..22d9859 --- /dev/null +++ b/src/components/automatable.h @@ -0,0 +1,34 @@ +#ifndef AUTOMATABLE_H_FACB42A5 +#define AUTOMATABLE_H_FACB42A5 + +#include "component.h" +#include "entity_manager.h" + +class AutomatableComponent : public Component { +public: + + using id_type = EntityManager::id_type; + + /** + * Controls what script will be run as this entity's behavior. It should refer + * to a table in the global namespace of the script engine state, and that + * table should contain a function called "Behavior". + */ + std::string table; + + /** + * Whether or not the behavior script is running. + * + * @managed_by ScriptingSystem + */ + bool running = false; + + /** + * The entity ID of the running script, if there is one. + * + * @managed_by ScriptingSystem + */ + id_type script; +}; + +#endif /* end of include guard: AUTOMATABLE_H_FACB42A5 */ diff --git a/src/components/prototypable.h b/src/components/prototypable.h index 4659e7c..c0dd972 100644 --- a/src/components/prototypable.h +++ b/src/components/prototypable.h @@ -9,14 +9,15 @@ public: using id_type = EntityManager::id_type; + /** + * The index of the object in the map definition. + */ size_t mapObjectIndex; + /** + * The name of the prototype that the object was spawned from. + */ std::string prototypeId; - - bool hasBehavior = false; - bool runningBehavior = false; - - id_type behaviorScript; }; #endif /* end of include guard: PROTOTYPABLE_H_817F2205 */ diff --git a/src/components/runnable.h b/src/components/runnable.h index 1b994fb..956bfdc 100644 --- a/src/components/runnable.h +++ b/src/components/runnable.h @@ -4,12 +4,48 @@ #include "component.h" #include #include +#include "entity_manager.h" class RunnableComponent : public Component { public: + using id_type = EntityManager::id_type; + + /** + * A Lua stack where the entity's script is running. + * + * NOTE: This object is called a thread, but there is no multi-threading going + * on. + * + * @managed_by ScriptingSystem + */ std::unique_ptr runner; + + /** + * An entry point to the script running in the runner thread. + * + * @managed_by ScriptingSystem + */ std::unique_ptr callable; + + /** + * Whether or not this entity represents a behavior script. A behavior script + * usually does not terminate on its own, and can be terminated at will by + * another system, usually when the automatable entity leaves the active map. + * + * @managed_by ScriptingSystem + */ + bool behavior = false; + + /** + * If this is a behavior script, this is the ID of the automatable entity that + * the behavior belongs to. This is required so that the ScriptingSystem can + * notify the automatable entity if the behavior script terminates by itself, + * and that it shouldn't attempt to terminate it. + * + * @managed_by ScriptingSystem + */ + id_type actor; }; #endif /* end of include guard: AUTOMATABLE_H_3D519131 */ diff --git a/src/game.h b/src/game.h index dc256c6..d7fdcd7 100644 --- a/src/game.h +++ b/src/game.h @@ -44,8 +44,8 @@ private: std::mt19937 rng_; Renderer renderer_; - EntityManager entityManager_; SystemManager systemManager_; + EntityManager entityManager_; bool shouldQuit_ = false; }; diff --git a/src/systems/realizing.cpp b/src/systems/realizing.cpp index 7f5aefb..baacf5a 100644 --- a/src/systems/realizing.cpp +++ b/src/systems/realizing.cpp @@ -12,6 +12,7 @@ #include "components/ponderable.h" #include "components/transformable.h" #include "components/prototypable.h" +#include "components/automatable.h" #include "systems/mapping.h" #include "systems/animating.h" #include "systems/pondering.h" @@ -223,7 +224,10 @@ RealizingSystem::RealizingSystem( if (prototypeId == "movplat") { - prototypable.hasBehavior = true; + auto& automatable = game_.getEntityManager(). + emplaceComponent(mapObject); + + automatable.table = prototypeId; } else if (prototypeId == "checkpoint") { auto& ponderable = game_.getEntityManager(). @@ -403,19 +407,9 @@ void RealizingSystem::enterActiveMap(id_type entity) ponderable.active = true; } - if (game_.getEntityManager().hasComponent(entity)) + if (game_.getEntityManager().hasComponent(entity)) { - auto& prototypable = game_.getEntityManager(). - getComponent(entity); - - if (prototypable.hasBehavior) - { - auto& scripting = game_.getSystemManager().getSystem(); - - prototypable.hasBehavior = true; - prototypable.runningBehavior = true; - prototypable.behaviorScript = scripting.runBehaviorScript(entity); - } + game_.getSystemManager().getSystem().startBehavior(entity); } } @@ -437,17 +431,8 @@ void RealizingSystem::leaveActiveMap(id_type entity) ponderable.active = false; } - if (game_.getEntityManager().hasComponent(entity)) + if (game_.getEntityManager().hasComponent(entity)) { - auto& prototypable = game_.getEntityManager(). - getComponent(entity); - - if (prototypable.runningBehavior) - { - auto& scripting = game_.getSystemManager().getSystem(); - scripting.killScript(prototypable.behaviorScript); - - prototypable.runningBehavior = false; - } + game_.getSystemManager().getSystem().stopBehavior(entity); } } diff --git a/src/systems/scripting.cpp b/src/systems/scripting.cpp index 57c3fd5..c423558 100644 --- a/src/systems/scripting.cpp +++ b/src/systems/scripting.cpp @@ -6,6 +6,7 @@ #include "components/playable.h" #include "components/mappable.h" #include "components/prototypable.h" +#include "components/automatable.h" #include "systems/realizing.h" #include "vector.h" @@ -133,28 +134,34 @@ void ScriptingSystem::tick(double dt) if (!*runnable.callable) { - game_.getEntityManager().deleteEntity(entity); + killScript(entity); } } } void ScriptingSystem::killScript(id_type entity) { - if (game_.getEntityManager().hasComponent(entity)) + auto& runnable = game_.getEntityManager(). + getComponent(entity); + + if (runnable.behavior) { - game_.getEntityManager().deleteEntity(entity); + auto& automatable = game_.getEntityManager(). + getComponent(runnable.actor); + + automatable.running = false; } + + game_.getEntityManager().deleteEntity(entity); } template -EntityManager::id_type ScriptingSystem::runScript( +sol::optional ScriptingSystem::runScript( + std::string table, std::string event, id_type entity, Args&&... args) { - auto& prototypable = game_.getEntityManager(). - getComponent(entity); - id_type script = game_.getEntityManager().emplaceEntity(); auto& runnable = game_.getEntityManager(). @@ -171,7 +178,7 @@ EntityManager::id_type ScriptingSystem::runScript( new sol::coroutine( runnable.runner->state(). traverse_get( - prototypable.prototypeId, + table, event))); if (!*runnable.callable) @@ -189,17 +196,58 @@ EntityManager::id_type ScriptingSystem::runScript( throw std::runtime_error(e.what()); } - return script; + if (*runnable.callable) + { + return { script }; + } else { + killScript(script); + + return {}; + } +} + +void ScriptingSystem::startBehavior(id_type entity) +{ + auto& automatable = game_.getEntityManager(). + getComponent(entity); + + sol::optional script = + runScript( + automatable.table, + "Behavior", + entity); + + if (script) + { + automatable.script = *script; + automatable.running = true; + + auto& runnable = game_.getEntityManager(). + getComponent(automatable.script); + + runnable.behavior = true; + runnable.actor = entity; + } } -EntityManager::id_type ScriptingSystem::runBehaviorScript(id_type entity) +void ScriptingSystem::stopBehavior(id_type entity) { - return runScript("Behavior", entity); + auto& automatable = game_.getEntityManager(). + getComponent(entity); + + if (automatable.running) + { + killScript(automatable.script); + } } void ScriptingSystem::onTouch(id_type entity, id_type player) { + auto& prototypable = game_.getEntityManager(). + getComponent(entity); + runScript( + prototypable.prototypeId, "OnTouch", entity, script_entity(player)); diff --git a/src/systems/scripting.h b/src/systems/scripting.h index e330316..b119c3f 100644 --- a/src/systems/scripting.h +++ b/src/systems/scripting.h @@ -13,14 +13,20 @@ public: void killScript(id_type entity); - id_type runBehaviorScript(id_type entity); + void startBehavior(id_type entity); + + void stopBehavior(id_type entity); void onTouch(id_type entity, id_type player); private: template - id_type runScript(std::string event, id_type entity, Args&&... args); + sol::optional runScript( + std::string table, + std::string event, + id_type entity, + Args&&... args); sol::state engine_; }; -- cgit 1.4.1