From 7064e0673c8c5d0e17fcc00271a673c171473376 Mon Sep 17 00:00:00 2001 From: historic_bruno Date: Sun, 23 Sep 2012 19:19:57 +0000 Subject: [PATCH] Adds TechnologyModification message for newly created entities, so they are notified of previously researched techs (this will streamline implementation of certain techs). Fixes #1410 This was SVN commit r12698. --- .../mods/public/globalscripts/Technologies.js | 39 ++++++++++-------- .../components/TechnologyManager.js | 41 +++++++++++++++---- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/binaries/data/mods/public/globalscripts/Technologies.js b/binaries/data/mods/public/globalscripts/Technologies.js index 428ef8f74a..65304139e3 100644 --- a/binaries/data/mods/public/globalscripts/Technologies.js +++ b/binaries/data/mods/public/globalscripts/Technologies.js @@ -32,25 +32,10 @@ function GetTechModifiedProperty(currentTechModifications, entityTemplateData, p for (var i in modifications) { var modification = modifications[i]; - var applies = false; - // See if any of the lists of classes matches this entity - for (var j in modification.affects) + if (DoesModificationApply(modification, classes)) { - var hasAllClasses = true; - // Check each class in affects is present for the entity - for (var k in modification.affects[j]) - hasAllClasses = hasAllClasses && (classes.indexOf(modification.affects[j][k]) !== -1); + // We found a match, apply the modification - if (hasAllClasses) - { - applies = true; - break; - } - } - - // We found a match, apply the modification - if (applies) - { // Nothing is cumulative so that ordering doesn't matter as much as possible if (modification.multiplier) retValue += (modification.multiplier - 1) * propertyValue; @@ -65,3 +50,23 @@ function GetTechModifiedProperty(currentTechModifications, entityTemplateData, p return retValue; } + +/** + * Returns whether the given modification applies to the entity containing the given class list + */ +function DoesModificationApply(modification, classes) +{ + // See if any of the lists of classes matches this entity + for (var j in modification.affects) + { + var hasAllClasses = true; + // Check each class in affects is present for the entity + for (var k in modification.affects[j]) + hasAllClasses = hasAllClasses && (classes.indexOf(modification.affects[j][k]) !== -1); + + if (hasAllClasses) + return true; + } + + return false; +} diff --git a/binaries/data/mods/public/simulation/components/TechnologyManager.js b/binaries/data/mods/public/simulation/components/TechnologyManager.js index 4a5fbe22aa..2eb858abdb 100644 --- a/binaries/data/mods/public/simulation/components/TechnologyManager.js +++ b/binaries/data/mods/public/simulation/components/TechnologyManager.js @@ -180,18 +180,41 @@ TechnologyManager.prototype.OnGlobalOwnershipChanged = function (msg) return; var cmpIdentity = Engine.QueryInterface(msg.entity, IID_Identity); - if (cmpIdentity) + if (!cmpIdentity) + return; + + var classes = cmpIdentity.GetClassesList(); + for (var i in classes) { - var classes = cmpIdentity.GetClassesList(); - for (var i in classes) + this.classCounts[classes[i]] = this.classCounts[classes[i]] || 0; + this.classCounts[classes[i]] += 1; + + this.typeCountsByClass[classes[i]] = this.typeCountsByClass[classes[i]] || {}; + this.typeCountsByClass[classes[i]][template] = this.typeCountsByClass[classes[i]][template] || 0; + this.typeCountsByClass[classes[i]][template] += 1; + } + + // Newly created entity, check if any researched techs might apply + // (only do this for new entities because even if an entity is converted or captured, + // we want it to maintain whatever technologies previously applied) + if (msg.from == -1) + { + var cmpPlayer = Engine.QueryInterface(this.entity, IID_Player); + var playerID = cmpPlayer.GetPlayerID(); + var modifiedComponents = {}; + for (var name in this.modifications) { - this.classCounts[classes[i]] = this.classCounts[classes[i]] || 0; - this.classCounts[classes[i]] += 1; - - this.typeCountsByClass[classes[i]] = this.typeCountsByClass[classes[i]] || {}; - this.typeCountsByClass[classes[i]][template] = this.typeCountsByClass[classes[i]][template] || 0; - this.typeCountsByClass[classes[i]][template] += 1; + // We only need to find one one tech per component for a match + var modifications = this.modifications[name]; + var component = name.split("/")[0]; + for (var i in modifications) + if (!modifiedComponents[component] && DoesModificationApply(modifications[i], classes)) + modifiedComponents[component] = true; } + + // Send mesage(s) to the entity so it knows about researched techs + for (var component in modifiedComponents) + Engine.PostMessage(msg.entity, MT_TechnologyModification, { "component": component, "player": playerID }); } } if (msg.from == playerID)