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 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;

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
if (GetLOT() == 14) {
AddComponent<SimplePhysicsComponent>(0);
AddComponent<SimplePhysicsComponent>(4246);
AddComponent<ModelBehaviorComponent>();
AddComponent<RenderComponent>();
AddComponent<DestroyableComponent>();
@ -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<LevelProgressionComponent>();
AddComponent<PlayerForcedMovementComponent>();
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:
AddComponent<BouncerComponent>();
break;
@ -345,7 +353,6 @@ void Entity::Initialize() {
case eReplicaComponentType::MULTI_ZONE_ENTRANCE:
AddComponent<MultiZoneEntranceComponent>();
break;
case eReplicaComponentType::BUFF:
AddComponent<BuffComponent>();
break;
@ -358,7 +365,6 @@ void Entity::Initialize() {
case eReplicaComponentType::BUILD_BORDER:
AddComponent<BuildBorderComponent>();
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 {

View File

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

View File

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

View File

@ -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<CDScriptComponentTable>();
int32_t scriptComponentID = compRegistryTable->GetByIDAndType(m_TemplateID, eReplicaComponentType::SCRIPT, -1);
// CDScriptComponentTable* scriptCompTable = CDClientManager::Instance().GetTable<CDScriptComponentTable>();
// 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<CDZoneTableTable>();
const auto zoneID = dZoneManager::Instance()->GetZoneID();
const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID());
// // ZoneControl script
// if (m_TemplateID == 2365) {
// CDZoneTableTable* zoneTable = CDClientManager::Instance().GetTable<CDZoneTableTable>();
// 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<CDRebuildComponentTable>();
std::vector<CDRebuildComponent> rebCompData = rebCompTable->Query([=](CDRebuildComponent entry) { return (entry.id == quickBuildComponentID); });
@ -638,4 +638,4 @@ no_ghosting:
controllablePhysicsComponent->SetSpeedMultiplier(levelComponent->GetSpeedBase() / 500.0f);
}
}
// }
}

View File

@ -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<bool>(GeneralUtils::ASCIIToUTF16(npc.first), npc.second);

View File

@ -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<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:
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