1
0
forked from 0ad/0ad

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.
This commit is contained in:
historic_bruno 2012-09-23 19:19:57 +00:00
parent db4b08d1ad
commit 7064e0673c
2 changed files with 54 additions and 26 deletions

View File

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

View File

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