From bb71823aee7a1b08b4c9b44401104d0fcb4313fa Mon Sep 17 00:00:00 2001 From: sanderd17 Date: Mon, 14 Apr 2014 15:00:49 +0000 Subject: [PATCH] Make the actor tech-modifiable. Fixes #2243 This was SVN commit r14928. --- .../components/TechnologyManager.js | 6 +-- .../components/CCmpVisualActor.cpp | 47 +++++++++++++++---- .../ICmpValueModificationManager.cpp | 5 ++ .../components/ICmpValueModificationManager.h | 1 + 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/TechnologyManager.js b/binaries/data/mods/public/simulation/components/TechnologyManager.js index 1b34077911..a66b705c7d 100644 --- a/binaries/data/mods/public/simulation/components/TechnologyManager.js +++ b/binaries/data/mods/public/simulation/components/TechnologyManager.js @@ -373,9 +373,9 @@ TechnologyManager.prototype.ApplyModifications = function(valueName, curValue, e this.modificationCache[valueName][ent] = {"origValue": curValue}; var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); var templateName = cmpTemplateManager.GetCurrentTemplateName(ent); - // Ensure that preview entites have the same properties as the final building - if (templateName.indexOf("preview|") != -1) - templateName = templateName.slice(8); + // Ensure that preview or construction entites have the same properties as the final building + if (templateName.indexOf("preview|") > -1 || templateName.indexOf("construction|") > -1 ) + templateName = templateName.slice(templateName.indexOf("|") + 1); this.modificationCache[valueName][ent].newValue = GetTechModifiedProperty(this.modifications, cmpTemplateManager.GetTemplate(templateName), valueName, curValue); } diff --git a/source/simulation2/components/CCmpVisualActor.cpp b/source/simulation2/components/CCmpVisualActor.cpp index 59f98015f6..7000a273f2 100644 --- a/source/simulation2/components/CCmpVisualActor.cpp +++ b/source/simulation2/components/CCmpVisualActor.cpp @@ -30,6 +30,7 @@ #include "ICmpTemplateManager.h" #include "ICmpTerrain.h" #include "ICmpUnitMotion.h" +#include "ICmpValueModificationManager.h" #include "ICmpVision.h" #include "graphics/Decal.h" @@ -57,13 +58,15 @@ public: componentManager.SubscribeToMessageType(MT_Interpolate); componentManager.SubscribeToMessageType(MT_RenderSubmit); componentManager.SubscribeToMessageType(MT_OwnershipChanged); + componentManager.SubscribeToMessageType(MT_ValueModification); componentManager.SubscribeGloballyToMessageType(MT_TerrainChanged); } DEFAULT_COMPONENT_ALLOCATOR(VisualActor) private: - std::wstring m_ActorName; + std::wstring m_BaseActorName, m_ActorName; + bool m_IsFoundationActor; CUnit* m_Unit; fixed m_R, m_G, m_B; // shading colour @@ -188,10 +191,11 @@ public: m_Seed = GetEntityId(); - if (paramNode.GetChild("Foundation").IsOk() && paramNode.GetChild("FoundationActor").IsOk()) - m_ActorName = paramNode.GetChild("FoundationActor").ToString(); + m_IsFoundationActor = paramNode.GetChild("Foundation").IsOk() && paramNode.GetChild("FoundationActor").IsOk(); + if (m_IsFoundationActor) + m_BaseActorName = m_ActorName = paramNode.GetChild("FoundationActor").ToString(); else - m_ActorName = paramNode.GetChild("Actor").ToString(); + m_BaseActorName = m_ActorName = paramNode.GetChild("Actor").ToString(); m_VisibleInAtlasOnly = paramNode.GetChild("VisibleInAtlasOnly").ToBool(); m_IsActorOnly = paramNode.GetChild("ActorOnly").IsOk(); @@ -228,6 +232,7 @@ public: serialize.NumberU32_Unbounded("seed", m_Seed); // TODO: variation/selection strings + serialize.String("actor", m_ActorName, 0, 256); serialize.NumberFixed_Unbounded("constructionprogress", m_ConstructionProgress); @@ -240,7 +245,7 @@ public: if (serialize.IsDebug()) { - serialize.String("actor", m_ActorName, 0, 256); + serialize.String("base actor", m_BaseActorName, 0, 256); } SerializeCommon(serialize); @@ -254,8 +259,8 @@ public: SerializeCommon(deserialize); - // If we serialized a different seed, reload actor - if (oldSeed != GetActorSeed()) + // If we serialized a different seed or different actor, reload actor + if (oldSeed != GetActorSeed() || m_BaseActorName != m_ActorName) ReloadActor(); fixed repeattime = m_AnimSyncRepeatTime; // save because SelectAnimation overwrites it @@ -313,6 +318,24 @@ public: m_Unit->GetModel().SetTerrainDirty(msgData.i0, msgData.j0, msgData.i1, msgData.j1); break; } + case MT_ValueModification: + { + const CMessageValueModification& msgData = static_cast (msg); + if (msgData.component != L"VisualActor") + break; + CmpPtr cmpValueModificationManager(GetSystemEntity()); + std::wstring newActorName; + if (m_IsFoundationActor) + newActorName = cmpValueModificationManager->ApplyModifications(L"VisualActor/FoundationActor", m_BaseActorName, GetEntityId()); + else + newActorName = cmpValueModificationManager->ApplyModifications(L"VisualActor/Actor", m_BaseActorName, GetEntityId()); + if (newActorName != m_ActorName) + { + m_ActorName = newActorName; + ReloadActor(); + } + break; + } } } @@ -519,7 +542,10 @@ void CCmpVisualActor::InitModel(const CParamNode& paramNode) if (GetSimContext().HasUnitManager()) { std::set selections; - m_Unit = GetSimContext().GetUnitManager().CreateUnit(m_ActorName, GetActorSeed(), selections); + std::wstring actorName = m_ActorName; + if (actorName.find(L".xml") == std::wstring::npos) + actorName += L".xml"; + m_Unit = GetSimContext().GetUnitManager().CreateUnit(actorName, GetActorSeed(), selections); if (m_Unit) { CModelAbstract& model = m_Unit->GetModel(); @@ -637,7 +663,10 @@ void CCmpVisualActor::ReloadActor() return; std::set selections; - CUnit* newUnit = GetSimContext().GetUnitManager().CreateUnit(m_ActorName, GetActorSeed(), selections); + std::wstring actorName = m_ActorName; + if (actorName.find(L".xml") == std::wstring::npos) + actorName += L".xml"; + CUnit* newUnit = GetSimContext().GetUnitManager().CreateUnit(actorName, GetActorSeed(), selections); if (!newUnit) return; diff --git a/source/simulation2/components/ICmpValueModificationManager.cpp b/source/simulation2/components/ICmpValueModificationManager.cpp index 0e1d3e4502..7588c9c292 100644 --- a/source/simulation2/components/ICmpValueModificationManager.cpp +++ b/source/simulation2/components/ICmpValueModificationManager.cpp @@ -39,6 +39,11 @@ public: { return m_Script.Call("ApplyModifications", valueName, currentValue, entity); } + + virtual std::wstring ApplyModifications(std::wstring valueName, std::wstring currentValue, entity_id_t entity) + { + return m_Script.Call("ApplyModifications", valueName, currentValue, entity); + } }; REGISTER_COMPONENT_SCRIPT_WRAPPER(ValueModificationManagerScripted) diff --git a/source/simulation2/components/ICmpValueModificationManager.h b/source/simulation2/components/ICmpValueModificationManager.h index d72db17917..8218ad12a2 100644 --- a/source/simulation2/components/ICmpValueModificationManager.h +++ b/source/simulation2/components/ICmpValueModificationManager.h @@ -32,6 +32,7 @@ class ICmpValueModificationManager : public IComponent public: virtual fixed ApplyModifications(std::wstring valueName, fixed currentValue, entity_id_t entity) = 0; virtual u32 ApplyModifications(std::wstring valueName, u32 currentValue, entity_id_t entity) = 0; + virtual std::wstring ApplyModifications(std::wstring valueName, std::wstring currentValue, entity_id_t entity) = 0; DECLARE_INTERFACE_TYPE(ValueModificationManager) };