Index: binaries/data/mods/public/globalscripts/DamageTypes.js =================================================================== --- binaries/data/mods/public/globalscripts/DamageTypes.js +++ /dev/null @@ -1,22 +0,0 @@ -function DamageTypes() -{ - // TODO: load these from files - - this.names = { - "Hack": markForTranslationWithContext("damage type", "Hack"), - "Pierce": markForTranslationWithContext("damage type", "Pierce"), - "Crush": markForTranslationWithContext("damage type", "Crush"), - }; - - deepfreeze(this.names); -} - -DamageTypes.prototype.GetNames = function() -{ - return this.names; -}; - -DamageTypes.prototype.GetTypes = function() -{ - return Object.keys(this.names); -}; Index: binaries/data/mods/public/globalscripts/Templates.js =================================================================== --- binaries/data/mods/public/globalscripts/Templates.js +++ binaries/data/mods/public/globalscripts/Templates.js @@ -165,8 +165,12 @@ if (template.Armour) { ret.armour = {}; - for (let damageType of damageTypes.GetTypes()) + for (let damageType in template.Armour) + { + if (damageType == "Foundation") + continue; ret.armour[damageType] = getEntityValue("Armour/" + damageType); + } } if (template.Attack) @@ -190,7 +194,7 @@ "elevationBonus": getAttackStat("ElevationBonus"), "damage": {} }; - for (let damageType of damageTypes.GetTypes()) + for (let damageType in template.Attack[type].Damage) ret.attack[type].damage[damageType] = getAttackStat("Damage/" + damageType); ret.attack[type].elevationAdaptedRange = Math.sqrt(ret.attack[type].maxRange * @@ -206,7 +210,7 @@ "shape": template.Attack[type].Splash.Shape, "damage": {} }; - for (let damageType of damageTypes.GetTypes()) + for (let damageType in template.Attack[type].Splash.Damage) ret.attack[type].splash.damage[damageType] = getAttackStat("Splash/Damage/" + damageType); } } @@ -218,7 +222,7 @@ "friendlyFire": template.DeathDamage.FriendlyFire != "false", "damage": {} }; - for (let damageType of damageTypes.GetTypes()) + for (let damageType in template.DeathDamage.Damage) ret.deathDamage.damage[damageType] = getEntityValue("DeathDamage/Damage/" + damageType); } Index: binaries/data/mods/public/gui/common/tooltips.js =================================================================== --- binaries/data/mods/public/gui/common/tooltips.js +++ binaries/data/mods/public/gui/common/tooltips.js @@ -14,8 +14,6 @@ "Capture": translate("Capture Attack:") }; -var g_DamageTypes = new DamageTypes(); - var g_SplashDamageTypes = { "Circular": translate("Circular Splash Damage"), "Linear": translate("Linear Splash Damage") @@ -222,7 +220,7 @@ Object.keys(template.armour).map( dmgType => sprintf(translate("%(damage)s %(damageType)s %(armorPercentage)s"), { "damage": template.armour[dmgType].toFixed(1), - "damageType": unitFont(translateWithContext("damage type", g_DamageTypes.GetNames()[dmgType])), + "damageType": unitFont(translateWithContext("damage type", dmgType)), "armorPercentage": '[font="sans-10"]' + sprintf(translate("(%(armorPercentage)s)"), { @@ -238,11 +236,14 @@ if (!dmg) return '[font="sans-12"]' + translate("(None)") + '[/font]'; - return g_DamageTypes.GetTypes().filter( - dmgType => dmg[dmgType]).map( + let damageTypes = []; + for (let type in dmg) + damageTypes.push(type); + + return damageTypes.filter(dmgType => dmg[dmgType]).map( dmgType => sprintf(translate("%(damage)s %(damageType)s"), { "damage": dmg[dmgType].toFixed(1), - "damageType": unitFont(translateWithContext("damage type", g_DamageTypes.GetNames()[dmgType])) + "damageType": unitFont(translateWithContext("damage type", dmgType)) })).join(commaFont(translate(", "))); } Index: binaries/data/mods/public/gui/reference/common/helper.js =================================================================== --- binaries/data/mods/public/gui/reference/common/helper.js +++ binaries/data/mods/public/gui/reference/common/helper.js @@ -23,7 +23,7 @@ { upgrade.entity = upgrade.entity.replace(/\{(civ|native)\}/g, g_SelectedCiv); - let data = GetTemplateDataHelper(loadTemplate(upgrade.entity), null, g_AuraData, g_ResourceData, g_DamageTypes); + let data = GetTemplateDataHelper(loadTemplate(upgrade.entity), null, g_AuraData, g_ResourceData); data.name.internal = upgrade.entity; data.cost = upgrade.cost; data.icon = upgrade.icon || data.icon; @@ -125,7 +125,7 @@ function GetTemplateData(templateName) { var template = loadTemplate(templateName); - return GetTemplateDataHelper(template, null, g_AuraData, g_ResourceData, g_DamageTypes, g_CurrentModifiers); + return GetTemplateDataHelper(template, null, g_AuraData, g_ResourceData, g_CurrentModifiers); } function isPairTech(technologyCode) Index: binaries/data/mods/public/gui/reference/common/load.js =================================================================== --- binaries/data/mods/public/gui/reference/common/load.js +++ binaries/data/mods/public/gui/reference/common/load.js @@ -17,7 +17,6 @@ */ var g_ParsedData = {}; var g_ResourceData = new Resources(); -var g_DamageTypes = new DamageTypes(); // This must be defined after the g_TechnologyData cache object is declared. var g_AutoResearchTechList = findAllAutoResearchedTechs(); @@ -110,7 +109,7 @@ return null; let template = loadTemplate(templateName); - let parsed = GetTemplateDataHelper(template, null, g_AuraData, g_ResourceData, g_DamageTypes, g_CurrentModifiers); + let parsed = GetTemplateDataHelper(template, null, g_AuraData, g_ResourceData, g_CurrentModifiers); parsed.name.internal = templateName; parsed.history = template.Identity.History; Index: binaries/data/mods/public/maps/random/rmgen-common/wall_builder.js =================================================================== --- binaries/data/mods/public/maps/random/rmgen-common/wall_builder.js +++ binaries/data/mods/public/maps/random/rmgen-common/wall_builder.js @@ -262,7 +262,7 @@ function readyWallElement(path, civCode) { path = path.replace(/\{civ\}/g, civCode); - let template = GetTemplateDataHelper(Engine.GetTemplate(path), null, null, {}, g_DamageTypes, {}); + let template = GetTemplateDataHelper(Engine.GetTemplate(path), null, null, {}, {}); let length = template.wallPiece ? template.wallPiece.length : template.obstruction.shape.width; return deepfreeze({ Index: binaries/data/mods/public/maps/random/rmgen/library.js =================================================================== --- binaries/data/mods/public/maps/random/rmgen/library.js +++ binaries/data/mods/public/maps/random/rmgen/library.js @@ -26,8 +26,6 @@ */ const MAP_BORDER_WIDTH = 3; -const g_DamageTypes = new DamageTypes(); - /** * Constants needed for heightmap_manipulation.js */ Index: binaries/data/mods/public/simulation/ai/common-api/entity.js =================================================================== --- binaries/data/mods/public/simulation/ai/common-api/entity.js +++ binaries/data/mods/public/simulation/ai/common-api/entity.js @@ -201,11 +201,15 @@ if (!this.get("Armour")) return undefined; - return { - "Hack": +this.get("Armour/Hack"), - "Pierce": +this.get("Armour/Pierce"), - "Crush": +this.get("Armour/Crush") - }; + let armour = {}; + for (let damageType in this.get("Armour")) + { + if (damageType == "Foundation") + continue; + armour[damageType] = +this.get("Armour/" + damageType); + } + + return armour; }, "attackTypes": function() { @@ -229,14 +233,14 @@ }, "attackStrengths": function(type) { - if (!this.get("Attack/" + type +"")) + if (!this.get("Attack/" + type +"/Damage")) return undefined; - return { - "Hack": +(this.get("Attack/" + type + "/Damage/Hack") || 0), - "Pierce": +(this.get("Attack/" + type + "/Damage/Pierce") || 0), - "Crush": +(this.get("Attack/" + type + "/Damage/Crush") || 0) - }; + let damage = {}; + for (let damageType in this.get("Attack/" + type +"/Damage")) + damage[damageType] = +this.get("Attack/" + type + "/Damage/" + damageType); + + return damage; }, "captureStrength": function() { Index: binaries/data/mods/public/simulation/components/Armour.js =================================================================== --- binaries/data/mods/public/simulation/components/Armour.js +++ binaries/data/mods/public/simulation/components/Armour.js @@ -7,11 +7,11 @@ "0.0" + "5.0" + "" + - DamageTypes.BuildSchema("damage protection") + + BuildDamageTypesSchema("damage protection") + "" + "" + "" + - DamageTypes.BuildSchema("damage protection") + + BuildDamageTypesSchema("damage protection") + "" + "" + ""; @@ -44,24 +44,24 @@ return { "killed": false, "change": 0 }; // Adjust damage values based on armour; exponential armour: damage = attack * 0.9^armour - var armourStrengths = this.GetArmourStrengths(); + let armourStrengths = this.GetArmourStrengths(); // Total is sum of individual damages // Don't bother rounding, since HP is no longer integral. - var total = 0; + let total = 0; for (let type in strengths) total += strengths[type] * multiplier * Math.pow(0.9, armourStrengths[type] || 0); // Reduce health - var cmpHealth = Engine.QueryInterface(this.entity, IID_Health); + let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); return cmpHealth.Reduce(total); }; Armour.prototype.GetArmourStrengths = function() { // Work out the armour values with technology effects - var applyMods = (type, foundation) => { - var strength; + let applyMods = (type, foundation) => { + let strength; if (foundation) { strength = +this.template.Foundation[type]; @@ -73,11 +73,15 @@ return ApplyValueModificationsToEntity("Armour/" + type, strength, this.entity); }; - var foundation = Engine.QueryInterface(this.entity, IID_Foundation) && this.template.Foundation; + let foundation = Engine.QueryInterface(this.entity, IID_Foundation) && this.template.Foundation; let ret = {}; - for (let damageType of DamageTypes.GetTypes()) + for (let damageType in this.template) + { + if (damageType == "Foundation") + continue; ret[damageType] = applyMods(damageType, foundation); + } return ret; }; Index: binaries/data/mods/public/simulation/components/Attack.js =================================================================== --- binaries/data/mods/public/simulation/components/Attack.js +++ binaries/data/mods/public/simulation/components/Attack.js @@ -123,7 +123,7 @@ "" + "" + "" + - DamageTypes.BuildSchema("damage strength") + + BuildDamageTypesSchema("damage strength") + "" + "" + "" + @@ -142,7 +142,7 @@ "" + "" + "" + - DamageTypes.BuildSchema("damage strength") + + BuildDamageTypesSchema("damage strength") + "" + "" + "" + @@ -172,7 +172,7 @@ "" + "" + "" + - DamageTypes.BuildSchema("damage strength") + + BuildDamageTypesSchema("damage strength") + "" + Attack.prototype.bonusesSchema + "" + @@ -234,7 +234,7 @@ "" + "" + "" + - DamageTypes.BuildSchema("damage strength") + + BuildDamageTypesSchema("damage strength") + "" + "" + // TODO: how do these work? Attack.prototype.bonusesSchema + @@ -462,7 +462,7 @@ return { "value": ApplyValueModificationsToEntity("Attack/Capture/Value", +(template.Value || 0), this.entity) }; let ret = {}; - for (let damageType of DamageTypes.GetTypes()) + for (let damageType in template.Damage) ret[damageType] = applyMods(damageType); return ret; Index: binaries/data/mods/public/simulation/components/DeathDamage.js =================================================================== --- binaries/data/mods/public/simulation/components/DeathDamage.js +++ binaries/data/mods/public/simulation/components/DeathDamage.js @@ -32,7 +32,7 @@ "" + "" + "" + - DamageTypes.BuildSchema("damage strength") + + BuildDamageTypesSchema("damage strength") + "" + DeathDamage.prototype.bonusesSchema; @@ -49,7 +49,7 @@ ApplyValueModificationsToEntity("DeathDamage/Damage/" + damageType, +(this.template.Damage[damageType] || 0), this.entity); let ret = {}; - for (let damageType of DamageTypes.GetTypes()) + for (let damageType in this.template.Damage) ret[damageType] = applyMods(damageType); return ret; Index: binaries/data/mods/public/simulation/components/GuiInterface.js =================================================================== --- binaries/data/mods/public/simulation/components/GuiInterface.js +++ binaries/data/mods/public/simulation/components/GuiInterface.js @@ -547,14 +547,14 @@ let aurasTemplate = {}; if (!template.Auras) - return GetTemplateDataHelper(template, player, aurasTemplate, Resources, DamageTypes); + return GetTemplateDataHelper(template, player, aurasTemplate, Resources); let auraNames = template.Auras._string.split(/\s+/); for (let name of auraNames) aurasTemplate[name] = AuraTemplates.Get(name); - return GetTemplateDataHelper(template, player, aurasTemplate, Resources, DamageTypes); + return GetTemplateDataHelper(template, player, aurasTemplate, Resources); }; GuiInterface.prototype.IsTechnologyResearched = function(player, data) Index: binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js +++ binaries/data/mods/public/simulation/components/tests/test_UpgradeModification.js @@ -126,11 +126,11 @@ * To start with, no techs are researched... */ // T1: Check the cost of the upgrade without a player value being passed (as it would be in the structree). -let parsed_template = GetTemplateDataHelper(template, null, {}, Resources, DamageTypes); +let parsed_template = GetTemplateDataHelper(template, null, {}, Resources); TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, { "stone": 100, "wood": 50, "time": 100 }); // T2: Check the value, with a player ID (as it would be in-session). -parsed_template = GetTemplateDataHelper(template, playerID, {}, Resources, DamageTypes); +parsed_template = GetTemplateDataHelper(template, playerID, {}, Resources); TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, { "stone": 100, "wood": 50, "time": 100 }); // T3: Check that the value is correct within the Update Component. @@ -144,11 +144,11 @@ isResearched = true; // T4: Check that the player-less value hasn't increased... -parsed_template = GetTemplateDataHelper(template, null, {}, Resources, DamageTypes); +parsed_template = GetTemplateDataHelper(template, null, {}, Resources); TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, { "stone": 100, "wood": 50, "time": 100 }); // T5: ...but the player-backed value has. -parsed_template = GetTemplateDataHelper(template, playerID, {}, Resources, DamageTypes); +parsed_template = GetTemplateDataHelper(template, playerID, {}, Resources); TS_ASSERT_UNEVAL_EQUALS(parsed_template.upgrades[0].cost, { "stone": 160, "wood": 25, "time": 90 }); // T6: The upgrade component should still be using the old resource cost (but new time cost) for the upgrade in progress... Index: binaries/data/mods/public/simulation/helpers/DamageTypes.js =================================================================== --- binaries/data/mods/public/simulation/helpers/DamageTypes.js +++ binaries/data/mods/public/simulation/helpers/DamageTypes.js @@ -1,8 +1,22 @@ -DamageTypes.prototype.BuildSchema = function(helptext = "") +/** + * Builds a RelaxRNG schema based on currently valid elements. + * + * To prevent validation errors, disabled damage types are included in the schema. + * + * @param {string} helptext - Text displayed as help + * @return {string} - RelaxNG schema string + */ +function BuildDamageTypesSchema(helptext = "") { - return "" + this.GetTypes().reduce((schema, type) => - schema + "", - "") + ""; + return "" + + "" + + "" + + // Armour requires Foundation to not be a damage type. + "Foundation" + + "" + + "" + + "" + + ""; }; -DamageTypes = new DamageTypes(); +Engine.RegisterGlobal("BuildDamageTypesSchema", BuildDamageTypesSchema);