Continued re-integration of Entity::Initialize

This commit is contained in:
David Markowitz 2023-06-11 03:06:18 -07:00
parent 0b5df9f0b1
commit 77dc6ff312
8 changed files with 124 additions and 104 deletions

View File

@ -56,6 +56,7 @@ const uint32_t LWOCLONEID_INVALID = -1; //!< Invalid LWOCLONEID
const uint16_t LWOINSTANCEID_INVALID = -1; //!< Invalid LWOINSTANCEID const uint16_t LWOINSTANCEID_INVALID = -1; //!< Invalid LWOINSTANCEID
const uint16_t LWOMAPID_INVALID = -1; //!< Invalid LWOMAPID const uint16_t LWOMAPID_INVALID = -1; //!< Invalid LWOMAPID
const uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID const uint64_t LWOZONEID_INVALID = 0; //!< Invalid LWOZONEID
const uint32_t ZONE_CONTROL_LOT = 2365;
const float PI = 3.14159f; const float PI = 3.14159f;

View File

@ -220,7 +220,7 @@ void Entity::Initialize() {
// 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) { if (GetLOT() == 14) {
AddComponent<SimplePhysicsComponent>(0); AddComponent<SimplePhysicsComponent>(4246);
AddComponent<ModelBehaviorComponent>(); AddComponent<ModelBehaviorComponent>();
AddComponent<RenderComponent>(); AddComponent<RenderComponent>();
AddComponent<DestroyableComponent>(); AddComponent<DestroyableComponent>();
@ -231,7 +231,6 @@ void Entity::Initialize() {
TemplateComponents components = componentsRegistry->GetTemplateComponents(m_TemplateID); TemplateComponents components = componentsRegistry->GetTemplateComponents(m_TemplateID);
ApplyComponentWhitelist(components); ApplyComponentWhitelist(components);
ApplyComponentBlacklist(components); ApplyComponentBlacklist(components);
ApplyComponentConfig(components);
for (const auto& [componentTemplate, componentId] : components) { for (const auto& [componentTemplate, componentId] : components) {
switch (componentTemplate) { switch (componentTemplate) {
case eReplicaComponentType::CONTROLLABLE_PHYSICS: case eReplicaComponentType::CONTROLLABLE_PHYSICS:
@ -250,6 +249,15 @@ void Entity::Initialize() {
AddComponent<LevelProgressionComponent>(); AddComponent<LevelProgressionComponent>();
AddComponent<PlayerForcedMovementComponent>(); AddComponent<PlayerForcedMovementComponent>();
break; break;
case eReplicaComponentType::SCRIPT: {
auto script = ScriptComponent::GetScriptName(this, componentId);
if (!script.empty()) AddComponent<ScriptComponent>(script);
if (m_TemplateID == ZONE_CONTROL_LOT) {
const auto zoneScript = ScriptComponent::GetZoneScriptName(componentId);
if (!zoneScript.empty()) AddComponent<ScriptComponent>(zoneScript);
}
break;
}
case eReplicaComponentType::BOUNCER: case eReplicaComponentType::BOUNCER:
AddComponent<BouncerComponent>(); AddComponent<BouncerComponent>();
break; break;
@ -345,7 +353,6 @@ void Entity::Initialize() {
case eReplicaComponentType::MULTI_ZONE_ENTRANCE: case eReplicaComponentType::MULTI_ZONE_ENTRANCE:
AddComponent<MultiZoneEntranceComponent>(); AddComponent<MultiZoneEntranceComponent>();
break; break;
case eReplicaComponentType::BUFF: case eReplicaComponentType::BUFF:
AddComponent<BuffComponent>(); AddComponent<BuffComponent>();
break; break;
@ -358,7 +365,6 @@ void Entity::Initialize() {
case eReplicaComponentType::BUILD_BORDER: case eReplicaComponentType::BUILD_BORDER:
AddComponent<BuildBorderComponent>(); AddComponent<BuildBorderComponent>();
break; break;
case eReplicaComponentType::SCRIPT:
case eReplicaComponentType::GHOST: case eReplicaComponentType::GHOST:
case eReplicaComponentType::SPAWN: case eReplicaComponentType::SPAWN:
case eReplicaComponentType::MODULAR_BUILD: case eReplicaComponentType::MODULAR_BUILD:
@ -383,7 +389,6 @@ void Entity::Initialize() {
case eReplicaComponentType::FX: case eReplicaComponentType::FX:
case eReplicaComponentType::VEHICLE_PHYSICS: case eReplicaComponentType::VEHICLE_PHYSICS:
case eReplicaComponentType::PHYSICS_SYSTEM: case eReplicaComponentType::PHYSICS_SYSTEM:
case eReplicaComponentType::CHANGLING_BUILD: case eReplicaComponentType::CHANGLING_BUILD:
case eReplicaComponentType::CHOICE_BUILD: case eReplicaComponentType::CHOICE_BUILD:
case eReplicaComponentType::PACKAGE: case eReplicaComponentType::PACKAGE:
@ -443,22 +448,22 @@ void Entity::Initialize() {
} }
} }
for (const auto& [componentId, component] : m_Components) { std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) {
component->LoadTemplateData(); component.second->LoadTemplateData();
} });
for (const auto& [componentId, component] : m_Components) { std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) {
component->LoadConfigData(); component.second->LoadConfigData();
} });
for (const auto& [componentId, component] : m_Components) { std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) {
component->Startup(); component.second->Startup();
} });
if (!IsPlayer()) return; // No save data to load for non players if (!IsPlayer()) return; // No save data to load for non players
for (const auto& [componentId, component] : m_Components) { std::for_each(m_Components.begin(), m_Components.end(), [this](auto& component) {
component->LoadFromXml(m_Character->GetXMLDoc()); component.second->LoadFromXml(m_Character->GetXMLDoc());
} });
} }
bool Entity::operator==(const Entity& other) const { bool Entity::operator==(const Entity& other) const {

View File

@ -299,6 +299,12 @@ public:
template<typename Cmpt, typename...ConstructorValues> template<typename Cmpt, typename...ConstructorValues>
Cmpt* AddComponent(ConstructorValues... arguments); Cmpt* AddComponent(ConstructorValues... arguments);
/**
* @brief Removes a component from this Entity.
*/
template<typename Cmpt>
void RemoveComponent();
protected: protected:
LWOOBJID m_ObjectID; LWOOBJID m_ObjectID;

View File

@ -20,6 +20,11 @@ Cmpt* Entity::AddComponent(ConstructorValues...arguments) {
return dynamic_cast<Cmpt*>(insertedComponent.get()); return dynamic_cast<Cmpt*>(insertedComponent.get());
} }
template<typename Cmpt>
void Entity::RemoveComponent() {
m_Components.erase(Cmpt::ComponentType);
}
template<typename T> template<typename T>
const T& Entity::GetVar(const std::u16string& name) const { const T& Entity::GetVar(const std::u16string& name) const {
auto* data = GetVarData(name); auto* data = GetVarData(name);

View File

@ -1,4 +1,4 @@
// Entity::Initialize() { Entity::Initialize() {
/** /**
* Setup trigger * Setup trigger
*/ */
@ -309,67 +309,67 @@
* This is a bit of a mess * This is a bit of a mess
*/ */
CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable<CDScriptComponentTable>(); // CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable<CDScriptComponentTable>();
int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1); // int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1);
std::string scriptName = ""; // std::string scriptName = "";
bool client = false; // bool client = false;
if (scriptComponentID > 0 || m_Character) { // if (scriptComponentID > 0 || m_Character) {
std::string clientScriptName; // std::string clientScriptName;
if (!m_Character) { // if (!m_Character) {
CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID); // CDScriptComponent scriptCompData = scriptCompTable->GetByID(scriptComponentID);
scriptName = scriptCompData.script_name; // scriptName = scriptCompData.script_name;
clientScriptName = scriptCompData.client_script_name; // clientScriptName = scriptCompData.client_script_name;
} else { // } else {
scriptName = ""; // scriptName = "";
} // }
if (scriptName != "" || (scriptName == "" && m_Character)) { // if (!scriptName.empty() || (scriptName.empty() && m_Character)) {
} else if (clientScriptName != "") { // } else if (!clientScriptName.empty()) {
client = true; // client = true;
} else if (!m_Character) { // } else if (!m_Character) {
client = true; // client = true;
} // }
} // }
std::string customScriptServer; // std::string customScriptServer;
bool hasCustomServerScript = false; // bool hasCustomServerScript = false;
const auto customScriptServerName = GetVarAsString(u"custom_script_server"); // const auto customScriptServerName = GetVarAsString(u"custom_script_server");
const auto customScriptClientName = GetVarAsString(u"custom_script_client"); // const auto customScriptClientName = GetVarAsString(u"custom_script_client");
if (!customScriptServerName.empty()) { // if (!customScriptServerName.empty()) {
customScriptServer = customScriptServerName; // customScriptServer = customScriptServerName;
hasCustomServerScript = true; // hasCustomServerScript = true;
} // }
if (!customScriptClientName.empty()) { // if (!customScriptClientName.empty()) {
client = true; // client = true;
} // }
if (hasCustomServerScript && scriptName.empty()) { // if (hasCustomServerScript && scriptName.empty()) {
scriptName = customScriptServer; // scriptName = customScriptServer;
} // }
if (!scriptName.empty() || client || m_Character || scriptComponentID >= 0) { // if (!scriptName.empty() || client || m_Character || scriptComponentID >= 0) {
m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, new ScriptComponent(this, scriptName, true, client && scriptName.empty()))); // m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, new ScriptComponent(this, scriptName, true, client && scriptName.empty())));
} // }
// ZoneControl script // // ZoneControl script
if (m_TemplateID == 2365) { // if (m_TemplateID == 2365) {
CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable<CDZoneTableTable>(); // CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable<CDZoneTableTable>();
const auto zoneID = dZoneManager::Instance()->GetZoneID(); // const auto zoneID = dZoneManager::Instance()->GetZoneID();
const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID()); // const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID());
if (zoneData != nullptr) { // if (zoneData != nullptr) {
int zoneScriptID = zoneData->scriptID; // int zoneScriptID = zoneData->scriptID;
CDScriptComponent zoneScriptData = scriptCompTable->GetByID(zoneScriptID); // CDScriptComponent zoneScriptData = scriptCompTable->GetByID(zoneScriptID);
ScriptComponent* comp = new ScriptComponent(this, zoneScriptData.script_name, true); // ScriptComponent* comp = new ScriptComponent(this, zoneScriptData.script_name, true);
m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, comp)); // m_Components.insert(std::make_pair(eReplicaComponentType::SCRIPT, comp));
} // }
} // }
// if (compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SKILL, -1) != -1 || m_Character) { // if (compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SKILL, -1) != -1 || m_Character) {
// SkillComponent* comp = new SkillComponent(this); // SkillComponent* comp = new SkillComponent(this);
@ -383,8 +383,8 @@
// } // }
if (int componentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::QUICK_BUILD) > 0) { if (int componentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::QUICK_BUILD) > 0) {
QuickBuildComponent* comp = new QuickBuildComponent(this); // QuickBuildComponent* comp = new QuickBuildComponent(this);
m_Components.insert(std::make_pair(eReplicaComponentType::QUICK_BUILD, comp)); // m_Components.insert(std::make_pair(eReplicaComponentType::QUICK_BUILD, comp));
CDRebuildComponentTable* rebCompTable = CDClientManager::Instance().GetTable<CDRebuildComponentTable>(); CDRebuildComponentTable* rebCompTable = CDClientManager::Instance().GetTable<CDRebuildComponentTable>();
std::vector<CDRebuildComponent> rebCompData = rebCompTable->Query([=](CDRebuildComponent entry) { return (entry.id == quickBuildComponentID); }); std::vector<CDRebuildComponent> rebCompData = rebCompTable->Query([=](CDRebuildComponent entry) { return (entry.id == quickBuildComponentID); });
@ -638,4 +638,4 @@ no_ghosting:
controllablePhysicsComponent->SetSpeedMultiplier(levelComponent->GetSpeedBase() / 500.0f); controllablePhysicsComponent->SetSpeedMultiplier(levelComponent->GetSpeedBase() / 500.0f);
} }
} }
// } }

View File

@ -121,7 +121,6 @@ void VanityUtilities::SpawnVanity() {
if (scriptComponent != nullptr) { if (scriptComponent != nullptr) {
scriptComponent->SetScript(npc.m_Script); scriptComponent->SetScript(npc.m_Script);
scriptComponent->SetSerialized(false);
for (const auto& npc : npc.m_Flags) { for (const auto& npc : npc.m_Flags) {
npcEntity->SetVar<bool>(GeneralUtils::ASCIIToUTF16(npc.first), npc.second); npcEntity->SetVar<bool>(GeneralUtils::ASCIIToUTF16(npc.first), npc.second);

View File

@ -5,18 +5,15 @@
#include "Entity.h" #include "Entity.h"
#include "ScriptComponent.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) { ScriptComponent::ScriptComponent(Entity* parent, std::string scriptName) : Component(parent) {
m_Serialized = serialized;
m_Client = client;
SetScript(scriptName); SetScript(scriptName);
} }
ScriptComponent::~ScriptComponent() {
}
void ScriptComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) { void ScriptComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
if (bIsInitialUpdate) { if (bIsInitialUpdate) {
const auto& networkSettings = m_ParentEntity->GetNetworkSettings(); const auto& networkSettings = m_ParentEntity->GetNetworkSettings();
@ -46,11 +43,30 @@ CppScripts::Script* ScriptComponent::GetScript() {
} }
void ScriptComponent::SetScript(const std::string& scriptName) { 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); 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<CDScriptComponentTable>();
CDScriptComponent scriptCompData = scriptCompTable->GetByID(componentId);
return scriptCompData.script_name;
}
const std::string ScriptComponent::GetZoneScriptName(const uint32_t componentId) {
auto* zoneTable = CDClientManager::Instance().GetTable<CDZoneTableTable>();
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<CDScriptComponentTable>();
const auto& zoneScriptData = scriptCompTable->GetByID(zoneScriptID);
return zoneScriptData.script_name;
}

View File

@ -21,8 +21,7 @@ class ScriptComponent : public Component {
public: public:
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPT; inline static const eReplicaComponentType ComponentType = eReplicaComponentType::SCRIPT;
ScriptComponent(Entity* parent, std::string scriptName, bool serialized, bool client = false); ScriptComponent(Entity* parent, std::string scriptName);
~ScriptComponent() override;
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags); void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags);
@ -32,34 +31,23 @@ public:
*/ */
CppScripts::Script* GetScript(); 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 * Sets the script using a path by looking through dScripts for a script that matches
* @param scriptName the name of the script to find * @param scriptName the name of the script to find
*/ */
void SetScript(const std::string& scriptName); 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: private:
/** /**
* The script attached to this entity * The script attached to this entity
*/ */
CppScripts::Script* m_Script; 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 #endif // SCRIPTCOMPONENT_H