diff --git a/binaries/data/mods/public/simulation/ai/common-api/gamestate.js b/binaries/data/mods/public/simulation/ai/common-api/gamestate.js index 0cfd70ea84..d5279190a2 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/gamestate.js +++ b/binaries/data/mods/public/simulation/ai/common-api/gamestate.js @@ -153,10 +153,10 @@ m.GameState.prototype.getTemplate = function(type) if (this.techTemplates[type] !== undefined) return new m.Technology(this.techTemplates, type); - if (!this.templates[type]) - return null; + if (this.templates[type] === undefined) + this.sharedScript.GetTemplate(type); - return new m.Template(this.sharedScript, type, this.templates[type]); + return this.templates[type] ? new m.Template(this.sharedScript, type, this.templates[type]) : null; }; /** Return the template of the structure built from this foundation */ @@ -886,6 +886,8 @@ m.GameState.prototype.getEntityCounts = function() m.GameState.prototype.isTemplateAvailable = function(templateName) { + if (this.templates[templateName] === undefined) + this.sharedScript.GetTemplate(templateName); return this.templates[templateName] && !this.isTemplateDisabled(templateName); }; diff --git a/binaries/data/mods/public/simulation/ai/common-api/shared.js b/binaries/data/mods/public/simulation/ai/common-api/shared.js index d175f6dd40..f73ccdde45 100644 --- a/binaries/data/mods/public/simulation/ai/common-api/shared.js +++ b/binaries/data/mods/public/simulation/ai/common-api/shared.js @@ -9,7 +9,6 @@ m.SharedScript = function(settings) this._players = Object.keys(settings.players).map(key => settings.players[key]); // TODO SM55 Object.values(settings.players) this._templates = settings.templates; - this._derivedTemplates = {}; this._techTemplates = settings.techTemplates; this._entityMetadata = {}; @@ -49,28 +48,16 @@ m.SharedScript.prototype.Deserialize = function(data) this._templatesModifications = data.templatesModifications; this._entitiesModifications = data.entitiesModifications; this._entityMetadata = data.metadata; - this._derivedTemplates = {}; this.isDeserialized = true; }; m.SharedScript.prototype.GetTemplate = function(name) { - if (this._templates[name]) - return this._templates[name]; + if (this._templates[name] === undefined) + this._templates[name] = Engine.GetTemplate(name) || null - if (this._derivedTemplates[name]) - return this._derivedTemplates[name]; - - let template = Engine.GetTemplate(name); - if (template) - { - this._derivedTemplates[name] = template; - return template; - } - - error("Tried to retrieve invalid template '"+name+"'"); - return null; + return this._templates[name]; }; /** diff --git a/source/simulation2/components/CCmpAIManager.cpp b/source/simulation2/components/CCmpAIManager.cpp index eaf2fb6120..316bee2ab3 100644 --- a/source/simulation2/components/CCmpAIManager.cpp +++ b/source/simulation2/components/CCmpAIManager.cpp @@ -337,6 +337,8 @@ public: CParamNode GetTemplate(const std::string& name) { + if (!m_TemplateLoader.TemplateExists(name)) + return NULL; return m_TemplateLoader.GetTemplateFileData(name).GetChild("Entity"); } @@ -641,10 +643,6 @@ public: templates[i].second->ToJSVal(cx, false, &val); m_ScriptInterface->SetProperty(m_EntityTemplates, templates[i].first.c_str(), val, true); } - - // Since the template data is shared between AI players, freeze it - // to stop any of them changing it and confusing the other players - m_ScriptInterface->FreezeObject(m_EntityTemplates, true); } void Serialize(std::ostream& stream, bool isDebug)