diff --git a/dCommon/dEnums/dCommonVars.h b/dCommon/dEnums/dCommonVars.h index f67145da..7f80fc39 100644 --- a/dCommon/dEnums/dCommonVars.h +++ b/dCommon/dEnums/dCommonVars.h @@ -56,6 +56,7 @@ const uint32_t LWOCLONEID_INVALID = -1; //!< Invalid LWOCLONEID const uint16_t LWOINSTANCEID_INVALID = -1; //!< Invalid LWOINSTANCEID const uint16_t LWOMAPID_INVALID = -1; //!< Invalid LWOMAPID const uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID +const uint32_t ZONE_CONTROL_LOT = 2365; const float PI = 3.14159f; diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index c9b35155..5612e715 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -218,9 +218,9 @@ void Entity::Initialize() { if (m_ParentEntity) m_ParentEntity->AddChild(this); - // Brick-by-Brick models don't have all their components in the registry for some reason? Might have to be related to using ldf keys for physics + // Brick-by-Brick models don't have all their components in the registry for some reason? Might have to be related to using ldf keys for physics if (GetLOT() == 14) { - AddComponent(0); + AddComponent(4246); AddComponent(); AddComponent(); AddComponent(); @@ -231,7 +231,6 @@ void Entity::Initialize() { TemplateComponents components = componentsRegistry->GetTemplateComponents(m_TemplateID); ApplyComponentWhitelist(components); ApplyComponentBlacklist(components); - ApplyComponentConfig(components); for (const auto& [componentTemplate, componentId] : components) { switch (componentTemplate) { case eReplicaComponentType::CONTROLLABLE_PHYSICS: @@ -250,6 +249,15 @@ void Entity::Initialize() { AddComponent(); AddComponent(); break; + case eReplicaComponentType::SCRIPT: { + auto script = ScriptComponent::GetScriptName(this, componentId); + if (!script.empty()) AddComponent(script); + if (m_TemplateID == ZONE_CONTROL_LOT) { + const auto zoneScript = ScriptComponent::GetZoneScriptName(componentId); + if (!zoneScript.empty()) AddComponent(zoneScript); + } + break; + } case eReplicaComponentType::BOUNCER: AddComponent(); break; @@ -345,7 +353,6 @@ void Entity::Initialize() { case eReplicaComponentType::MULTI_ZONE_ENTRANCE: AddComponent(); break; - case eReplicaComponentType::BUFF: AddComponent(); break; @@ -358,7 +365,6 @@ void Entity::Initialize() { case eReplicaComponentType::BUILD_BORDER: AddComponent(); break; - case eReplicaComponentType::SCRIPT: case eReplicaComponentType::GHOST: case eReplicaComponentType::SPAWN: case eReplicaComponentType::MODULAR_BUILD: @@ -383,7 +389,6 @@ void Entity::Initialize() { case eReplicaComponentType::FX: case eReplicaComponentType::VEHICLE_PHYSICS: case eReplicaComponentType::PHYSICS_SYSTEM: - case eReplicaComponentType::CHANGLING_BUILD: case eReplicaComponentType::CHOICE_BUILD: case eReplicaComponentType::PACKAGE: @@ -443,22 +448,22 @@ void Entity::Initialize() { } } - for (const auto& [componentId, component] : m_Components) { - component->LoadTemplateData(); - } + std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) { + component.second->LoadTemplateData(); + }); - for (const auto& [componentId, component] : m_Components) { - component->LoadConfigData(); - } + std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) { + component.second->LoadConfigData(); + }); - for (const auto& [componentId, component] : m_Components) { - component->Startup(); - } + std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) { + component.second->Startup(); + }); if (!IsPlayer()) return; // No save data to load for non players - for (const auto& [componentId, component] : m_Components) { - component->LoadFromXml(m_Character->GetXMLDoc()); - } + std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) { + component.second->LoadFromXml(m_Character->GetXMLDoc()); + }); } bool Entity::operator==(const Entity& other) const { diff --git a/dGame/Entity.h b/dGame/Entity.h index 68f72966..b2c2d7c9 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -299,6 +299,12 @@ public: template Cmpt* AddComponent(ConstructorValues... arguments); + /** + * @brief Removes a component from this Entity. + */ + template + void RemoveComponent(); + protected: LWOOBJID m_ObjectID; diff --git a/dGame/Entity.tcc b/dGame/Entity.tcc index 27714608..58021c20 100644 --- a/dGame/Entity.tcc +++ b/dGame/Entity.tcc @@ -20,6 +20,11 @@ Cmpt* Entity::AddComponent(ConstructorValues...arguments) { return dynamic_cast(insertedComponent.get()); } +template +void Entity::RemoveComponent() { + m_Components.erase(Cmpt::ComponentType); +} + template const T& Entity::GetVar(const std::u16string& name) const { auto* data = GetVarData(name); diff --git a/dGame/EntityInitializeOld.cc b/dGame/EntityInitializeOld.cc index 5ba9dac7..786994c3 100644 --- a/dGame/EntityInitializeOld.cc +++ b/dGame/EntityInitializeOld.cc @@ -1,4 +1,4 @@ -// Entity::Initialize() { +Entity::Initialize() { /** * Setup trigger */ @@ -309,67 +309,67 @@ * This is a bit of a mess */ - CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable(); - int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1); + // CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable(); + // int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1); - std::string scriptName = ""; - bool client = false; - if (scriptComponentID > 0 || m_Character) { - std::string clientScriptName; - if (!m_Character) { - CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); - scriptName = scriptCompData.script_name; - clientScriptName = scriptCompData.client_script_name; - } else { - scriptName = ""; - } + // std::string scriptName = ""; + // bool client = false; + // if (scriptComponentID > 0 || m_Character) { + // std::string clientScriptName; + // if (!m_Character) { + // CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); + // scriptName = scriptCompData.script_name; + // clientScriptName = scriptCompData.client_script_name; + // } else { + // scriptName = ""; + // } - if (scriptName != "" || (scriptName == "" && m_Character)) { + // if (!scriptName.empty() || (scriptName.empty() && m_Character)) { - } else if (clientScriptName != "") { - client = true; - } else if (!m_Character) { - client = true; - } - } + // } else if (!clientScriptName.empty()) { + // client = true; + // } else if (!m_Character) { + // client = true; + // } + // } - std::string customScriptServer; - bool hasCustomServerScript = false; + // std::string customScriptServer; + // bool hasCustomServerScript = false; - const auto customScriptServerName = GetVarAsString(u"custom_script_server"); - const auto customScriptClientName = GetVarAsString(u"custom_script_client"); + // const auto customScriptServerName = GetVarAsString(u"custom_script_server"); + // const auto customScriptClientName = GetVarAsString(u"custom_script_client"); - if (!customScriptServerName.empty()) { - customScriptServer = customScriptServerName; - hasCustomServerScript = true; - } + // if (!customScriptServerName.empty()) { + // customScriptServer = customScriptServerName; + // hasCustomServerScript = true; + // } - if (!customScriptClientName.empty()) { - client = true; - } + // if (!customScriptClientName.empty()) { + // client = true; + // } - if (hasCustomServerScript && scriptName.empty()) { - scriptName = customScriptServer; - } + // if (hasCustomServerScript && scriptName.empty()) { + // scriptName = customScriptServer; + // } - if (!scriptName.empty() || client || m_Character || scriptComponentID >= 0) { - m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, new ScriptComponent(this, scriptName, true, client && scriptName.empty()))); - } + // if (!scriptName.empty() || client || m_Character || scriptComponentID >= 0) { + // m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, new ScriptComponent(this, scriptName, true, client && scriptName.empty()))); + // } - // ZoneControl script - if (m_TemplateID == 2365) { - CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); - const auto zoneID = dZoneManager::Instance()->GetZoneID(); - const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID()); + // // ZoneControl script + // if (m_TemplateID == 2365) { + // CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable(); + // const auto zoneID = dZoneManager::Instance()->GetZoneID(); + // const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID()); - if (zoneData != nullptr) { - int zoneScriptID = zoneData->scriptID; - CDScriptComponent zoneScriptData = scriptCompTable->GetByID(zoneScriptID); + // if (zoneData != nullptr) { + // int zoneScriptID = zoneData->scriptID; + // CDScriptComponent zoneScriptData = scriptCompTable->GetByID(zoneScriptID); - ScriptComponent* comp = new ScriptComponent(this, zoneScriptData.script_name, true); - m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, comp)); - } - } + // ScriptComponent* comp = new ScriptComponent(this, zoneScriptData.script_name, true); + // m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, comp)); + // } + // } // if (compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SKILL, -1) != -1 || m_Character) { // SkillComponent* comp = new SkillComponent(this); @@ -383,8 +383,8 @@ // } if (int componentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::QUICK_BUILD) > 0) { - QuickBuildComponent* comp = new QuickBuildComponent(this); - m_Components.insert(std::make_pair(eReplicaComponentType::QUICK_BUILD, comp)); + // QuickBuildComponent* comp = new QuickBuildComponent(this); + // m_Components.insert(std::make_pair(eReplicaComponentType::QUICK_BUILD, comp)); CDRebuildComponentTable* rebCompTable = CDClientManager::Instance().GetTable(); std::vector rebCompData = rebCompTable->Query([=](CDRebuildComponent entry) { return (entry.id == quickBuildComponentID); }); @@ -638,4 +638,4 @@ no_ghosting: controllablePhysicsComponent->SetSpeedMultiplier(levelComponent->GetSpeedBase() / 500.0f); } } -// } +} diff --git a/dGame/dUtilities/VanityUtilities.cpp b/dGame/dUtilities/VanityUtilities.cpp index 7fabfbce..1857018e 100644 --- a/dGame/dUtilities/VanityUtilities.cpp +++ b/dGame/dUtilities/VanityUtilities.cpp @@ -121,7 +121,6 @@ void VanityUtilities::SpawnVanity() { if (scriptComponent != nullptr) { scriptComponent->SetScript(npc.m_Script); - scriptComponent->SetSerialized(false); for (const auto& npc : npc.m_Flags) { npcEntity->SetVar(GeneralUtils::ASCIIToUTF16(npc.first), npc.second); diff --git a/dScripts/ScriptComponent.cpp b/dScripts/ScriptComponent.cpp index f5478083..c2066529 100644 --- a/dScripts/ScriptComponent.cpp +++ b/dScripts/ScriptComponent.cpp @@ -5,18 +5,15 @@ #include "Entity.h" #include "ScriptComponent.h" +#include "CDClientManager.h" +#include "CDScriptComponentTable.h" +#include "CDZoneTableTable.h" +#include "dZoneManager.h" -ScriptComponent::ScriptComponent(Entity* parent, std::string scriptName, bool serialized, bool client) : Component(parent) { - m_Serialized = serialized; - m_Client = client; - +ScriptComponent::ScriptComponent(Entity* parent, std::string scriptName) : Component(parent) { SetScript(scriptName); } -ScriptComponent::~ScriptComponent() { - -} - void ScriptComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) { if (bIsInitialUpdate) { const auto& networkSettings = m_ParentEntity->GetNetworkSettings(); @@ -46,11 +43,30 @@ CppScripts::Script* ScriptComponent::GetScript() { } void ScriptComponent::SetScript(const std::string& scriptName) { - //we don't need to delete the script because others may be using it :) - /*if (m_Client) { - m_Script = new InvalidScript(); - return; - }*/ - m_Script = CppScripts::GetScript(m_ParentEntity, scriptName); } + +const std::string ScriptComponent::GetScriptName(Entity* parentEntity, const uint32_t componentId) { + if (!parentEntity) return ""; + // LDF key script overrides script component Id + const auto customScriptServer = parentEntity->GetVarAsString(u"custom_script_server"); + if (!customScriptServer.empty() || componentId == 0) return customScriptServer; + + auto* scriptCompTable = CDClientManager::Instance().GetTable(); + CDScriptComponent scriptCompData = scriptCompTable->GetByID(componentId); + return scriptCompData.script_name; +} + +const std::string ScriptComponent::GetZoneScriptName(const uint32_t componentId) { + auto* zoneTable = CDClientManager::Instance().GetTable(); + const auto zoneID = dZoneManager::Instance()->GetZoneID(); + const auto* zoneData = zoneTable->Query(zoneID.GetMapID()); + + if (!zoneData) return ""; + + int zoneScriptID = zoneData->scriptID; + if (zoneScriptID == -1) return ""; + auto* scriptCompTable = CDClientManager::Instance().GetTable(); + const auto& zoneScriptData = scriptCompTable->GetByID(zoneScriptID); + return zoneScriptData.script_name; +} diff --git a/dScripts/ScriptComponent.h b/dScripts/ScriptComponent.h index 503f9de7..35c7796e 100644 --- a/dScripts/ScriptComponent.h +++ b/dScripts/ScriptComponent.h @@ -21,8 +21,7 @@ class ScriptComponent : public Component { public: inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPT; - ScriptComponent(Entity* parent, std::string scriptName, bool serialized, bool client = false); - ~ScriptComponent() override; + ScriptComponent(Entity* parent, std::string scriptName); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags); @@ -32,34 +31,23 @@ public: */ CppScripts::Script* GetScript(); - /** - * Sets whether the entity should be serialized, unused - * @param var whether the entity should be serialized - */ - void SetSerialized(const bool var) { m_Serialized = var; } - /** * Sets the script using a path by looking through dScripts for a script that matches * @param scriptName the name of the script to find */ void SetScript(const std::string& scriptName); + // Get the script attached to the provided component id. + // LDF key custom_script_server overrides component id script. + static const std::string GetScriptName(Entity* parentEntity, const uint32_t componentId = 0); + + static const std::string GetZoneScriptName(const uint32_t componentId); private: /** * The script attached to this entity */ CppScripts::Script* m_Script; - - /** - * Whether or not the comp should be serialized, unused - */ - bool m_Serialized; - - /** - * Whether or not this script is a client script - */ - bool m_Client; }; #endif // SCRIPTCOMPONENT_H