1
0
forked from 0ad/0ad

Make technology code cleaner with helper functions. Refs #1692.

This was SVN commit r13056.
This commit is contained in:
Jonathan Waller 2013-01-08 00:00:21 +00:00
parent 670deaef2a
commit 5caddcdd20
15 changed files with 61 additions and 121 deletions

View File

@ -67,7 +67,6 @@ Armour.prototype.GetArmourStrengths = function()
// Work out the armour values with technology effects
var self = this;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
var applyTechs = function(type, foundation)
{
var strength;
@ -81,12 +80,9 @@ Armour.prototype.GetArmourStrengths = function()
strength = +self.template[type];
}
if (cmpTechMan)
{
// All causes caching problems so disable it for now.
// var allComponent = cmpTechMan.ApplyModifications("Armour/All", strength, self.entity) - self.template[type];
strength = cmpTechMan.ApplyModifications("Armour/" + type, strength, self.entity);
}
// All causes caching problems so disable it for now.
// var allComponent = ApplyTechModificationsToEntity("Armour/All", strength, self.entity) - self.template[type];
strength = ApplyTechModificationsToEntity("Armour/" + type, strength, self.entity);
return strength;
};

View File

@ -316,14 +316,10 @@ Attack.prototype.CompareEntitiesByPreference = function(a, b)
Attack.prototype.GetTimers = function(type)
{
var prepare = +(this.template[type].PrepareTime || 0);
prepare = ApplyTechModificationsToEntity("Attack/" + type + "/PrepareTime", prepare, this.entity);
var repeat = +(this.template[type].RepeatTime || 1000);
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
{
prepare = cmpTechMan.ApplyModifications("Attack/" + type + "/PrepareTime", prepare, this.entity);
repeat = cmpTechMan.ApplyModifications("Attack/" + type + "/RepeatTime", repeat, this.entity);
}
repeat = ApplyTechModificationsToEntity("Attack/" + type + "/RepeatTime", repeat, this.entity);
return { "prepare": prepare, "repeat": repeat, "recharge": repeat - prepare };
};
@ -341,17 +337,11 @@ Attack.prototype.GetAttackStrengths = function(type)
splash = "/Splash";
}
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
var applyTechs = function(damageType)
{
var strength = +(template[damageType] || 0);
if (cmpTechMan)
{
// All causes caching problems so disable it for now.
//var allComponent = cmpTechMan.ApplyModifications("Attack/" + type + splash + "/All", strength, self.entity) - self.template[type][damageType];
strength = cmpTechMan.ApplyModifications("Attack/" + type + splash + "/" + damageType, strength, self.entity);
}
return strength;
// All causes caching problems so disable it for now.
//var allComponent = ApplyTechModificationsToEntity("Attack/" + type + splash + "/All", +(template[damageType] || 0), self.entity) - self.template[type][damageType];
return ApplyTechModificationsToEntity("Attack/" + type + splash + "/" + damageType, +(template[damageType] || 0), self.entity);
};
return {
@ -364,14 +354,10 @@ Attack.prototype.GetAttackStrengths = function(type)
Attack.prototype.GetRange = function(type)
{
var max = +this.template[type].MaxRange;
max = ApplyTechModificationsToEntity("Attack/" + type + "/MaxRange", max, this.entity);
var min = +(this.template[type].MinRange || 0);
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
{
max = cmpTechMan.ApplyModifications("Attack/" + type + "/MaxRange", max, this.entity);
min = cmpTechMan.ApplyModifications("Attack/" + type + "/MinRange", min, this.entity);
}
min = ApplyTechModificationsToEntity("Attack/" + type + "/MinRange", min, this.entity);
return { "max": max, "min": min };
};

View File

@ -52,10 +52,7 @@ Builder.prototype.GetRange = function()
*/
Builder.prototype.PerformBuilding = function(target)
{
var rate = +this.template.Rate;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
rate = cmpTechMan.ApplyModifications("Builder/Rate", rate, this.entity);
var rate = ApplyTechModificationsToEntity("Builder/Rate", +this.template.Rate, this.entity);
// If it's a foundation, then build it
var cmpFoundation = Engine.QueryInterface(target, IID_Foundation);

View File

@ -180,19 +180,13 @@ BuildingAI.prototype.OnRangeUpdate = function(msg)
BuildingAI.prototype.GetDefaultArrowCount = function()
{
var arrowCount = +this.template.DefaultArrowCount;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
arrowCount = cmpTechMan.ApplyModifications("BuildingAI/DefaultArrowCount", arrowCount, this.entity);
return arrowCount;
return ApplyTechModificationsToEntity("BuildingAI/DefaultArrowCount", arrowCount, this.entity);
};
BuildingAI.prototype.GetGarrisonArrowMultiplier = function()
{
var arrowMult = +this.template.GarrisonArrowMultiplier;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
arrowMult = cmpTechMan.ApplyModifications("BuildingAI/GarrisonArrowMultiplier", arrowMult, this.entity);
return arrowMult;
return ApplyTechModificationsToEntity("BuildingAI/GarrisonArrowMultiplier", arrowMult, this.entity);
};
/**

View File

@ -51,21 +51,16 @@ Cost.prototype.GetBuildTime = function()
{
var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
var buildTime = (+this.template.BuildTime) * cmpPlayer.cheatTimeMultiplier;
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechnologyManager)
buildTime = cmpTechnologyManager.ApplyModifications("Cost/BuildTime", buildTime, this.entity);
return buildTime;
return ApplyTechModificationsToEntity("Cost/BuildTime", buildTime, this.entity);
}
Cost.prototype.GetResourceCosts = function()
{
var costs = {};
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
for (var r in this.template.Resources)
{
costs[r] = +this.template.Resources[r];
if (cmpTechnologyManager)
costs[r] = cmpTechnologyManager.ApplyModifications("Cost/Resources/"+r, costs[r], this.entity);
costs[r] = ApplyTechModificationsToEntity("Cost/Resources/"+r, costs[r], this.entity);
}
return costs;
};

View File

@ -63,11 +63,7 @@ GarrisonHolder.prototype.GetAllowedClassesList = function()
*/
GarrisonHolder.prototype.GetCapacity = function()
{
var max = +this.template.Max;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
max = cmpTechMan.ApplyModifications("GarrisonHolder/Max", max, this.entity);
return max;
return ApplyTechModificationsToEntity("GarrisonHolder/Max", +this.template.Max, this.entity);
};
/**
@ -75,11 +71,7 @@ GarrisonHolder.prototype.GetCapacity = function()
*/
GarrisonHolder.prototype.GetHealRate = function()
{
var rate = +this.template.BuffHeal;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
rate = cmpTechMan.ApplyModifications("GarrisonHolder/BuffHeal", rate, this.entity);
return rate;
return ApplyTechModificationsToEntity("GarrisonHolder/BuffHeal", +this.template.BuffHeal, this.entity);
};
/**

View File

@ -42,9 +42,8 @@ Heal.prototype.GetTimers = function()
var prepare = 1000;
var repeat = +this.template.Rate;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
repeat = cmpTechMan.ApplyModifications("Heal/Rate", repeat, this.entity);
repeat = ApplyTechModificationsToEntity("Heal/Rate", repeat, this.entity);
return { "prepare": prepare, "repeat": repeat };
};
@ -52,10 +51,8 @@ Heal.prototype.GetRange = function()
{
var min = 0;
var max = +this.template.Range;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
max = cmpTechMan.ApplyModifications("Heal/Range", max, this.entity);
max = ApplyTechModificationsToEntity("Heal/Range", max, this.entity);
return { "max": max, "min": min };
};
@ -85,9 +82,8 @@ Heal.prototype.PerformHeal = function(target)
return;
var hp = +this.template.HP;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
hp = cmpTechMan.ApplyModifications("Heal/HP", hp, this.entity);
hp = ApplyTechModificationsToEntity("Heal/HP", hp, this.entity);
var targetState = cmpHealth.Increase(hp);
// Add XP

View File

@ -276,7 +276,7 @@ Health.prototype.OnTechnologyModification = function(msg)
if (cmpTechnologyManager)
{
var oldMaxHitpoints = this.GetMaxHitpoints();
var newMaxHitpoints = Math.round(cmpTechnologyManager.ApplyModifications("Health/Max", +this.template.Max, this.entity));
var newMaxHitpoints = Math.round(ApplyTechModificationsToEntity("Health/Max", +this.template.Max, this.entity));
if (oldMaxHitpoints != newMaxHitpoints)
{
var newHitpoints = Math.round(this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints);

View File

@ -95,11 +95,7 @@ Pack.prototype.CancelPack = function()
Pack.prototype.GetPackTime = function()
{
var packTime = +this.template.Time;
var cmpTechManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechManager)
packTime = cmpTechManager.ApplyModifications("Pack/Time", packTime, this.entity);
return packTime;
return ApplyTechModificationsToEntity("Pack/Time", +this.template.Time, this.entity);
};
Pack.prototype.GetElapsedTime = function()

View File

@ -105,11 +105,7 @@ Player.prototype.SetMaxPopulation = function(max)
Player.prototype.GetMaxPopulation = function()
{
var cmpTechMan = Engine.QueryInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
return cmpTechMan.ApplyModifications("Player/MaxPopulation", this.maxPop, this.entity);
else
return this.maxPop;
return ApplyTechModificationsToPlayer("Player/MaxPopulation", this.maxPop, this.entity);
};
Player.prototype.IsTrainingBlocked = function()

View File

@ -194,18 +194,12 @@ ProductionQueue.prototype.AddBatch = function(templateName, type, count, metadat
// Obviously we don't have the entities yet, so we must use template data
var costs = {};
var totalCosts = {};
var buildTime = +template.Cost.BuildTime;
var cmpTechnologyManager = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechnologyManager)
buildTime = cmpTechnologyManager.ApplyModificationsTemplate("Cost/BuildTime", buildTime, template);
var buildTime = ApplyTechModificationsToEntity("Cost/BuildTime", +template.Cost.BuildTime, template);
var time = timeMult * buildTime;
for (var r in template.Cost.Resources)
{
costs[r] = +template.Cost.Resources[r];
if (cmpTechnologyManager)
costs[r] = cmpTechnologyManager.ApplyModificationsTemplate("Cost/Resources/"+r, costs[r], template);
costs[r] = ApplyTechModificationsToEntity("Cost/Resources/"+r, +template.Cost.Resources[r], template);
totalCosts[r] = Math.floor(count * costs[r]);
}
@ -404,10 +398,7 @@ ProductionQueue.prototype.GetBatchTime = function(batchSize)
{
var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player);
var batchTimeModifier = +this.template.BatchTimeModifier;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
batchTimeModifier = cmpTechMan.ApplyModifications("ProductionQueue/BatchTimeModifier", batchTimeModifier, this.entity);
var batchTimeModifier = ApplyTechModificationsToEntity("ProductionQueue/BatchTimeModifier", +this.template.BatchTimeModifier, this.entity);
// TODO: work out what equation we should use here.
return Math.pow(batchSize, batchTimeModifier) * cmpPlayer.cheatTimeMultiplier;

View File

@ -15,13 +15,7 @@ Promotion.prototype.Init = function()
Promotion.prototype.GetRequiredXp = function()
{
var requiredXp = +this.template.RequiredXp;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
requiredXp = cmpTechMan.ApplyModifications("Promotion/RequiredXp", requiredXp, this.entity);
return requiredXp;
return ApplyTechModificationsToEntity("Promotion/RequiredXp", +this.template.RequiredXp, this.entity);
};
Promotion.prototype.GetCurrentXp = function()
@ -96,8 +90,7 @@ Promotion.prototype.IncreaseXp = function(amount)
this.currentXp += +(amount);
if (this.currentXp >= this.GetRequiredXp())
{
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
{
var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager);
var promotionTemplate = this.template;
var promotedTemplateName;
@ -107,9 +100,7 @@ Promotion.prototype.IncreaseXp = function(amount)
// so find the highest level we can reach
do
{
requiredXp = +promotionTemplate.RequiredXp;
if (cmpTechMan)
requiredXp = cmpTechMan.ApplyModifications("Promotion/RequiredXp", requiredXp, this.entity);
requiredXp = this.GetRequiredXp();
this.currentXp -= requiredXp;
promotedTemplateName = promotionTemplate.Entity;
var template = cmpTemplateManager.GetTemplate(promotedTemplateName);

View File

@ -128,17 +128,11 @@ ResourceGatherer.prototype.GetLastCarriedType = function()
ResourceGatherer.prototype.GetGatherRates = function()
{
var ret = {};
var baseSpeed = +this.template.BaseSpeed;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
baseSpeed = cmpTechMan.ApplyModifications("ResourceGatherer/BaseSpeed", baseSpeed, this.entity);
var baseSpeed = ApplyTechModificationsToEntity("ResourceGatherer/BaseSpeed", +this.template.BaseSpeed, this.entity);
for (var r in this.template.Rates)
{
var rate = +this.template.Rates[r]
if (cmpTechMan)
rate = cmpTechMan.ApplyModifications("ResourceGatherer/Rates/" + r, rate, this.entity);
var rate = ApplyTechModificationsToEntity("ResourceGatherer/Rates/" + r, +this.template.Rates[r], this.entity);
ret[r] = rate * baseSpeed;
}
@ -148,14 +142,10 @@ ResourceGatherer.prototype.GetGatherRates = function()
ResourceGatherer.prototype.GetCapacities = function()
{
var ret = {};
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
for (var r in this.template.Capacities)
{
var capacity = +this.template.Capacities[r];
if (cmpTechMan)
capacity = cmpTechMan.ApplyModifications("ResourceGatherer/Capacities/" + r, capacity, this.entity);
ret[r] = capacity;
ret[r] = ApplyTechModificationsToEntity("ResourceGatherer/Capacities/" + r, +this.template.Capacities[r], this.entity);
}
return ret;

View File

@ -72,10 +72,7 @@ TerritoryDecay.prototype.Decay = function()
if (!cmpHealth)
return; // error
var decayRate = +this.template.HealthDecayRate;
var cmpTechMan = QueryOwnerInterface(this.entity, IID_TechnologyManager);
if (cmpTechMan)
decayRate = cmpTechMan.ApplyModifications("TerritoryDecay/HealthDecayRate", decayRate, this.entity);
var decayRate = ApplyTechModificationsToEntity("TerritoryDecay/HealthDecayRate", +this.template.HealthDecayRate, this.entity);
cmpHealth.Reduce(Math.round(decayRate));
};

View File

@ -0,0 +1,23 @@
// Little helper functions to make applying tehnology more covenient
function ApplyTechModificationsToEntity(tech_type, current_value, entity) {
var cmpTechMan = QueryOwnerInterface(IID_TechnologyManager, entity);
if (!cmpTechMan)
return current_value;
return cmpTechMan.ApplyModifications(entity, tech_type, current_value);
}
function ApplyTechModificationsToPlayer(tech_type, current_value, player_entity) {
var cmpTechMan = Engine.QueryInterface(IID_TechnologyManager, player_entity);
if (!cmpTechMan)
return current_value;
return cmpTechMan.ApplyModifications(player_entity, tech_type, current_value);
}
Engine.RegisterGlobal("ApplyTechModificationsToEntity", ApplyTechModificationsToEntity);
Engine.RegisterGlobal("ApplyTechModificationsToPlayer", ApplyTechModificationsToPlayer);