Changeset View
Standalone View
binaries/data/mods/public/globalscripts/Templates.js
Show First 20 Lines • Show All 147 Lines • ▼ Show 20 Lines | |||||
* @param {object} resources - An instance of the Resources prototype. | * @param {object} resources - An instance of the Resources prototype. | ||||
* @param {object} damageTypes - An instance of the DamageTypes prototype. | * @param {object} damageTypes - An instance of the DamageTypes prototype. | ||||
* @param {object} modifiers - Modifications from auto-researched techs, unit upgrades | * @param {object} modifiers - Modifications from auto-researched techs, unit upgrades | ||||
* etc. Optional as only used if there's no player | * etc. Optional as only used if there's no player | ||||
* id provided. | * id provided. | ||||
*/ | */ | ||||
function GetTemplateDataHelper(template, player, auraTemplates, resources, damageTypes, modifiers={}) | function GetTemplateDataHelper(template, player, auraTemplates, resources, damageTypes, modifiers={}) | ||||
{ | { | ||||
// Cache the damageTypes for they will be called often. | |||||
let AllDamageTypes = DamageTypes.GetTypes(); | |||||
// Return data either from template (in tech tree) or sim state (ingame). | // Return data either from template (in tech tree) or sim state (ingame). | ||||
// @param {string} value_path - Route to the value within the template. | // @param {string} value_path - Route to the value within the template. | ||||
// @param {string} mod_key - Modification key, if not the same as the value_path. | // @param {string} mod_key - Modification key, if not the same as the value_path. | ||||
let getEntityValue = function(value_path, mod_key) { | let getEntityValue = function(value_path, mod_key) { | ||||
return GetModifiedTemplateDataValue(template, value_path, mod_key, player, modifiers); | return GetModifiedTemplateDataValue(template, value_path, mod_key, player, modifiers); | ||||
}; | }; | ||||
let ret = {}; | let ret = {}; | ||||
if (template.Armour) | if (template.Armour) | ||||
{ | { | ||||
ret.armour = {}; | ret.armour = {}; | ||||
for (let damageType of damageTypes.GetTypes()) | for (let damageType in template.Armour) | ||||
if (AllDamageTypes.some(x => x == damageType)) | |||||
Silier: you could switch this, go with for through AllDamageTypes and then ret.armour[damageType] =… | |||||
Done Inline ActionsThe ": 0" is not necessary, so I guess with the comment by @Stan below, this is not needed anymore? Freagarach: The ": 0" is not necessary, so I guess with the comment by @Stan below, this is not needed… | |||||
Done Inline ActionsMaybe you could write it as : for (let damageType in template.Armour) if (damageType != "Foundation") ret.armour[damageType] = getEntityValue("Armour/" + damageType); Stan: Maybe you could write it as :
```lang=js
for (let damageType in template.Armour)
if… | |||||
Done Inline ActionsI thought I had that already (was probably lost in one of many reverts ;) ). Freagarach: I thought I had that already (was probably lost in one of many reverts ;) ). | |||||
ret.armour[damageType] = getEntityValue("Armour/" + damageType); | ret.armour[damageType] = getEntityValue("Armour/" + damageType); | ||||
} | } | ||||
if (template.Attack) | if (template.Attack) | ||||
{ | { | ||||
ret.attack = {}; | ret.attack = {}; | ||||
for (let type in template.Attack) | for (let type in template.Attack) | ||||
{ | { | ||||
let getAttackStat = function(stat) { | let getAttackStat = function(stat) { | ||||
return getEntityValue("Attack/" + type + "/" + stat); | return getEntityValue("Attack/" + type + "/" + stat); | ||||
}; | }; | ||||
if (type == "Capture") | if (type == "Capture") | ||||
ret.attack.Capture = { | ret.attack.Capture = { | ||||
"value": getAttackStat("Value") | "value": getAttackStat("Value") | ||||
}; | }; | ||||
else | else | ||||
{ | { | ||||
ret.attack[type] = { | ret.attack[type] = { | ||||
"minRange": getAttackStat("MinRange"), | "minRange": getAttackStat("MinRange"), | ||||
"maxRange": getAttackStat("MaxRange"), | "maxRange": getAttackStat("MaxRange"), | ||||
"elevationBonus": getAttackStat("ElevationBonus") | "elevationBonus": getAttackStat("ElevationBonus") | ||||
}; | }; | ||||
for (let damageType of damageTypes.GetTypes()) | for (let damageType in template.Attack[type]) | ||||
Done Inline ActionsWill not that retake the other values as MinRange, MaxRange, etc.. ? Polakrity: Will not that retake the other values as MinRange, MaxRange, etc.. ? | |||||
Done Inline ActionsIt does, thanks, I re-incorporated the check. Freagarach: It does, thanks, I re-incorporated the check. | |||||
if (AllDamageTypes.some(x => x == damageType)) | |||||
Done Inline ActionsDoes writing if(!!AllDamageTypes[damageType]) work ? Instead of iterating through the whole collection ? Stan: Does writing if(!!AllDamageTypes[damageType]) work ? Instead of iterating through the whole… | |||||
ret.attack[type][damageType] = getAttackStat(damageType); | ret.attack[type][damageType] = getAttackStat(damageType); | ||||
Done Inline ActionsInstead use for (let damageType of template.Attack[type]) wraitii: Instead use `for (let damageType of template.Attack[type])` | |||||
ret.attack[type].elevationAdaptedRange = Math.sqrt(ret.attack[type].maxRange * | ret.attack[type].elevationAdaptedRange = Math.sqrt(ret.attack[type].maxRange * | ||||
(2 * ret.attack[type].elevationBonus + ret.attack[type].maxRange)); | (2 * ret.attack[type].elevationBonus + ret.attack[type].maxRange)); | ||||
} | } | ||||
ret.attack[type].repeatTime = getAttackStat("RepeatTime"); | ret.attack[type].repeatTime = getAttackStat("RepeatTime"); | ||||
if (template.Attack[type].Splash) | if (template.Attack[type].Splash) | ||||
{ | { | ||||
ret.attack[type].splash = { | ret.attack[type].splash = { | ||||
// true if undefined | // true if undefined | ||||
"friendlyFire": template.Attack[type].Splash.FriendlyFire != "false", | "friendlyFire": template.Attack[type].Splash.FriendlyFire != "false", | ||||
"shape": template.Attack[type].Splash.Shape | "shape": template.Attack[type].Splash.Shape | ||||
}; | }; | ||||
for (let damageType of damageTypes.GetTypes()) | for (let damageType in template.Attack[type].Splash) | ||||
if (AllDamageTypes.some(x => x == damageType)) | |||||
Done Inline ActionsSame here in all the occurences below. Stan: Same here in all the occurences below. | |||||
ret.attack[type].splash[damageType] = getAttackStat("Splash/" + damageType); | ret.attack[type].splash[damageType] = getAttackStat("Splash/" + damageType); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (template.DeathDamage) | if (template.DeathDamage) | ||||
{ | { | ||||
ret.deathDamage = { | ret.deathDamage = { | ||||
"friendlyFire": template.DeathDamage.FriendlyFire != "false" | "friendlyFire": template.DeathDamage.FriendlyFire != "false" | ||||
}; | }; | ||||
for (let damageType of damageTypes.GetTypes()) | for (let damageType in template.DeathDamage) | ||||
if (AllDamageTypes.some(x => x == damageType)) | |||||
Done Inline ActionsMight want to cache GetTypes() instead of calling it a lot of times. Also why do you need to check if the damagetype exists ? If it doesn't it will throw a warning and the modder will know something is up no ? Stan: Might want to cache GetTypes() instead of calling it a lot of times. Also why do you need to… | |||||
Done Inline ActionsYou're right, this is left over from my nigh endless fiddling to get this working. Freagarach: You're right, this is left over from my nigh endless fiddling to get this working. | |||||
Done Inline ActionsOops, you were not right, see comment above. Freagarach: Oops, you were not right, see comment above. | |||||
ret.deathDamage[damageType] = getEntityValue("DeathDamage/" + damageType); | ret.deathDamage[damageType] = getEntityValue("DeathDamage/" + damageType); | ||||
Done Inline ActionsWorked perfectly fine before. wraitii: Worked perfectly fine before. | |||||
} | } | ||||
if (template.Auras && auraTemplates) | if (template.Auras && auraTemplates) | ||||
{ | { | ||||
ret.auras = {}; | ret.auras = {}; | ||||
for (let auraID of template.Auras._string.split(/\s+/)) | for (let auraID of template.Auras._string.split(/\s+/)) | ||||
{ | { | ||||
let aura = auraTemplates[auraID]; | let aura = auraTemplates[auraID]; | ||||
▲ Show 20 Lines • Show All 314 Lines • Show Last 20 Lines |
you could switch this, go with for through AllDamageTypes and then ret.armour[damageType] = template.Armour[damageType] ? getEntityValue("Armour/" + damageType) : 0;