diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index 18a65be0..1f007746 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -504,10 +504,7 @@ void Entity::Initialize() { } AddCallbackTimer(0.0f, [this]() { - auto scripts = CppScripts::GetEntityScripts(this); - std::for_each(scripts.begin(), scripts.end(), [this](const auto& script) { - script->OnStartup(this); - }); + GetScript()->OnStartup(this); }); // Load data specific to this LOT first. These act as defaults for the components. @@ -523,7 +520,7 @@ void Entity::Initialize() { }); /** - * @brief Startup all the components. Some components need or want data from other components so + * Startup all the components. Some components need or want data from other components so * we want to ensure that * A) Most if not all components are newed and ready to be accessed. * B) All components have their personal data loaded and ready to be used. @@ -533,7 +530,7 @@ void Entity::Initialize() { }); /** - * @brief Load the player save data from XML. Ideally we do this after all initialization so the player + * Load the player save data from XML. Ideally we do this after all initialization so the player * save data overrides any defaults that may be applied. */ if (!IsPlayer()) std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) { @@ -647,7 +644,7 @@ void Entity::SetGMLevel(eGameMasterLevel value) { GameMessages::SendGMLevelBroadcast(m_ObjectID, value); } -void Entity::WriteBaseReplicaData(RakNet::BitStream* outBitStream, eReplicaPacketType packetType) { +void Entity::WriteBaseReplicaData(RakNet::BitStream* outBitStream, const eReplicaPacketType packetType) { if (packetType == eReplicaPacketType::CONSTRUCTION) { outBitStream->Write(m_ObjectID); outBitStream->Write(m_TemplateID); @@ -768,7 +765,7 @@ void Entity::WriteBaseReplicaData(RakNet::BitStream* outBitStream, eReplicaPacke } } -void Entity::WriteComponents(RakNet::BitStream* outBitStream, eReplicaPacketType packetType) { +void Entity::WriteComponents(RakNet::BitStream* outBitStream, const eReplicaPacketType packetType) { } @@ -787,34 +784,36 @@ void Entity::UpdateXMLDoc(tinyxml2::XMLDocument* doc) { } } +CppScripts::Script* Entity::GetScript() const { + auto* scriptComponent = GetComponent(); + auto* script = scriptComponent->GetScript(); + DluAssert(script != nullptr); + return script; +} + void Entity::Update(const float deltaTime) { - uint32_t timerPosition; - timerPosition = 0; - while (timerPosition < m_Timers.size()) { - m_Timers[timerPosition]->Update(deltaTime); - if (m_Timers[timerPosition]->GetTime() <= 0) { - const auto timerName = m_Timers[timerPosition]->GetName(); - - delete m_Timers[timerPosition]; - m_Timers.erase(m_Timers.begin() + timerPosition); - - for (CppScripts::Script* script : CppScripts::GetEntityScripts(this)) { - script->OnTimerDone(this, timerName); - } + auto namedTimerItr = std::remove_if(m_Timers.begin(), m_Timers.end(), [this, &deltaTime](EntityTimer* timer) { + timer->Update(deltaTime); + if (timer->GetTime() <= 0) { + GetScript()->OnTimerDone(this, timer->GetName()); TriggerEvent(eTriggerEventType::TIMER_DONE, this); - } else { - timerPosition++; + delete timer; + return true; } - } + return false; + }); + m_Timers.erase(namedTimerItr, m_Timers.end()); - for (int i = 0; i < m_CallbackTimers.size(); i++) { - m_CallbackTimers[i]->Update(deltaTime); - if (m_CallbackTimers[i]->GetTime() <= 0) { - m_CallbackTimers[i]->GetCallback()(); - delete m_CallbackTimers[i]; - m_CallbackTimers.erase(m_CallbackTimers.begin() + i); + auto callbackTimerItr = std::remove_if(m_CallbackTimers.begin(), m_CallbackTimers.end(), [this, &deltaTime](EntityCallbackTimer* timer) { + timer->Update(deltaTime); + if (timer->GetTime() <= 0) { + timer->GetCallback()(); + delete timer; + return true; } - } + return false; + }); + m_CallbackTimers.erase(callbackTimerItr, m_CallbackTimers.end()); // Add pending timers to the list of timers so they start next tick. if (m_PendingTimers.size() > 0) { @@ -1294,10 +1293,6 @@ void Entity::CancelTimer(const std::string& name) { } void Entity::CancelAllTimers() { - /*for (auto timer : m_Timers) { - if (timer) delete timer; - }*/ - for (auto* timer : m_Timers) { delete timer; } diff --git a/dGame/Entity.h b/dGame/Entity.h index ffbc38ee..2b745b51 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -223,6 +223,7 @@ public: virtual NiPoint3 GetRespawnPosition() const { return NiPoint3::ZERO; } virtual NiQuaternion GetRespawnRotation() const { return NiQuaternion::IDENTITY; } + CppScripts::Script* GetScript() const; void Sleep(); void Wake(); diff --git a/dGame/dComponents/CMakeLists.txt b/dGame/dComponents/CMakeLists.txt index fd5fb676..e0a0f39b 100644 --- a/dGame/dComponents/CMakeLists.txt +++ b/dGame/dComponents/CMakeLists.txt @@ -36,6 +36,7 @@ set(DGAME_DCOMPONENTS_SOURCES "BaseCombatAIComponent.cpp" "RigidbodyPhantomPhysicsComponent.cpp" "MultiZoneEntranceComponent.cpp" "RocketLaunchpadControlComponent.cpp" + "ScriptComponent.cpp" "ScriptedActivityComponent.cpp" "ShootingGalleryComponent.cpp" "SimplePhysicsComponent.cpp" diff --git a/dScripts/ScriptComponent.cpp b/dGame/dComponents/ScriptComponent.cpp similarity index 100% rename from dScripts/ScriptComponent.cpp rename to dGame/dComponents/ScriptComponent.cpp diff --git a/dScripts/ScriptComponent.h b/dGame/dComponents/ScriptComponent.h similarity index 100% rename from dScripts/ScriptComponent.h rename to dGame/dComponents/ScriptComponent.h diff --git a/dScripts/CMakeLists.txt b/dScripts/CMakeLists.txt index ac600dbc..5086a340 100644 --- a/dScripts/CMakeLists.txt +++ b/dScripts/CMakeLists.txt @@ -11,7 +11,6 @@ set(DSCRIPTS_SOURCES "Darkitect.cpp" "NPCAddRemoveItem.cpp" "NtFactionSpyServer.cpp" - "ScriptComponent.cpp" "ScriptedPowerupSpawner.cpp" "SpawnPetBaseServer.cpp") diff --git a/dScripts/CppScripts.cpp b/dScripts/CppScripts.cpp index 891a23f2..a74a953a 100644 --- a/dScripts/CppScripts.cpp +++ b/dScripts/CppScripts.cpp @@ -312,7 +312,7 @@ #include "WildNinjaSensei.h" #include "WildNinjaBricks.h" -InvalidScript* CppScripts::invalidToReturn = new InvalidScript(); +std::unique_ptr CppScripts::invalidScript = std::make_unique(); std::map CppScripts::m_Scripts; CppScripts::Script* CppScripts::GetScript(Entity* parent, const std::string& scriptName) { @@ -324,7 +324,7 @@ CppScripts::Script* CppScripts::GetScript(Entity* parent, const std::string& scr return script; } - script = invalidToReturn; + script = invalidScript.get(); //VE / AG: if (scriptName == "scripts\\ai\\AG\\L_AG_SHIP_PLAYER_DEATH_TRIGGER.lua") @@ -926,7 +926,7 @@ CppScripts::Script* CppScripts::GetScript(Entity* parent, const std::string& scr // handle invalid script reporting if the path is greater than zero and it's not an ignored script // information not really needed for sys admins but is for developers - else if (script == invalidToReturn) { + else if (script == invalidScript.get()) { if ((scriptName.length() > 0) && !((scriptName == "scripts\\02_server\\Enemy\\General\\L_SUSPEND_LUA_AI.lua") || (scriptName == "scripts\\02_server\\Enemy\\General\\L_BASE_ENEMY_SPIDERLING.lua") || (scriptName =="scripts\\ai\\FV\\L_ACT_NINJA_STUDENT.lua") || @@ -938,22 +938,3 @@ CppScripts::Script* CppScripts::GetScript(Entity* parent, const std::string& scr m_Scripts[scriptName] = script; return script; } - -std::vector CppScripts::GetEntityScripts(Entity* entity) { - std::vector scripts; - auto comps = entity->GetScriptComponents(); - for (auto& scriptComp : comps) { - if (scriptComp != nullptr) { - scripts.push_back(scriptComp->GetScript()); - } - } - return scripts; -} - -CppScripts::Script::Script() { - -} - -CppScripts::Script::~Script() { - -} diff --git a/dScripts/CppScripts.h b/dScripts/CppScripts.h index 7b4debfb..6bfff464 100644 --- a/dScripts/CppScripts.h +++ b/dScripts/CppScripts.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -14,7 +15,7 @@ enum class eRebuildState : uint32_t; namespace CppScripts { - extern InvalidScript* invalidToReturn; + extern std::unique_ptr invalidScript; extern std::map m_Scripts; /** * Base class for all scripts. Includes virtual methods to be overridden to handle LUA equivelent events. @@ -33,8 +34,7 @@ namespace CppScripts { */ class Script { public: - Script(); - ~Script(); + virtual ~Script() = default; /** * Invoked one frame after the script is loaded. @@ -365,5 +365,4 @@ namespace CppScripts { }; Script* GetScript(Entity* parent, const std::string& scriptName); - std::vector GetEntityScripts(Entity* entity); };