Changeset View
Standalone View
binaries/data/mods/public/globalscripts/Templates.js
Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | |||||
* | * | ||||
* @param template A valid template as returned by the template loader. | * @param template A valid template as returned by the template loader. | ||||
* @param player An optional player id to get the technology modifications | * @param player An optional player id to get the technology modifications | ||||
* of properties. | * of properties. | ||||
* @param auraTemplates An object in the form of {key: {auraName: "", auraDescription: ""}} | * @param auraTemplates An object in the form of {key: {auraName: "", auraDescription: ""}} | ||||
* @param resources An instance of the Resources prototype | * @param resources An instance of the Resources prototype | ||||
*/ | */ | ||||
function GetTemplateDataHelper(template, player, auraTemplates, resources) | function GetTemplateDataHelper(template, player, auraTemplates, resources) | ||||
{ | { | ||||
bb: One could add spaces in the commented object for consistency, meh.
Committer can add them too | |||||
// Return data either from template (in tech tree) or sim state (ingame) | // Return data either from template (in tech tree) or sim state (ingame) | ||||
let getEntityValue = function(tech_type) { | // @param value_path - Route to the value within the template | ||||
// @param mod_key - Modification key, if not the same as the value_path | |||||
bbUnsubmitted Done Inline Actionsperiods bb: periods | |||||
s0600204AuthorUnsubmitted Not Done Inline Actions...are what lady-folk get roughly once a month. I'll add in some full-stops instead. *ahem* ok, levity over. Back to work. s0600204: ...are what lady-folk get roughly once a month. I'll add in some full-stops instead.
*ahem* ok… | |||||
let getEntityValue = function(value_path, mod_key = null) { | |||||
Not Done Inline ActionsThis function should be at the top scope instead. It will be useful for D302 and there's no reason not to put it there in general. This function should not used g_CurrentModifiers but get a list of modifiers as a parameter, so that it becomes pure. See comments below about deriveAutoresearchedModifications. Also, I'm not entirely sure why you add mod_key here, the description is unclear and it doesn't actually seem used? wraitii: This function should be at the top scope instead. It will be useful for D302 and there's no… | |||||
Not Done Inline Actions
See D92#5200. Unless you meant why added in this commit. Hypothetically to reduce possibility of merge conflict with D92, which also changes code in this area, and looked to be closer to being committed at the time. s0600204: > Also, I'm not entirely sure why you add mod_key here, the description is unclear and it… | |||||
let current_value = template; | let current_value = template; | ||||
for (let property of tech_type.split("/")) | for (let property of value_path.split("/")) | ||||
current_value = current_value[property] || 0; | current_value = current_value[property] || 0; | ||||
current_value = +current_value; | current_value = +current_value; | ||||
Not Done Inline ActionsThis will crash when the template and tech paths does not correspond, but I guess thats ok. bb: This will crash when the template and tech paths does not correspond, but I guess thats ok. | |||||
Not Done Inline ActionsIt should not crash. It may give the wrong value under certain specific circumstances, but it should not crash. If you can prove evidence or a test case where it does crash, I'll look into it. s0600204: It should not crash. It may give the wrong value under certain specific circumstances, but it… | |||||
if (!player) | if (player) | ||||
return current_value; | return ApplyValueModificationsToTemplate(mod_key || value_path, current_value, player, template); | ||||
Not Done Inline ActionsWondering what this call is (trying to) do... bb: Wondering what this call is (trying to) do... | |||||
Not Done Inline ActionsThe same thing that the call does in the pre-revision state of the code. If called from within a running game session, it returns the appropriately modified value by running it through the simulation state, which is more complete and includes player-specific modifications unlike the call below which only applies global or game-start modifications. s0600204: The same thing that the call does in the pre-revision state of the code.
If called from within… | |||||
Not Done Inline ActionsThen I think it's broken, tested with the corral ups for cav speed... probably out of scope of this diff bb: Then I think it's broken, tested with the corral ups for cav speed...
probably out of scope of… | |||||
Not Done Inline ActionsI don't suspect any stat needs to be displayed with more than 8 decimals, so seems fine bb: I don't suspect any stat needs to be displayed with more than 8 decimals, so seems fine | |||||
if (typeof g_CurrentModifiers !== "undefined") | |||||
return GetTechModifiedProperty(g_CurrentModifiers, GetIdentityClasses(template.Identity), mod_key || value_path, current_value); | |||||
return ApplyValueModificationsToTemplate(tech_type, current_value, player, template); | return current_value; | ||||
Done Inline ActionsI like adding "else" for incompatible ifs anyhow. wraitii: I like adding "else" for incompatible ifs anyhow. | |||||
}; | }; | ||||
let ret = {}; | let ret = {}; | ||||
if (template.Armour) | if (template.Armour) | ||||
ret.armour = { | ret.armour = { | ||||
"hack": getEntityValue("Armour/Hack"), | "hack": getEntityValue("Armour/Hack"), | ||||
"pierce": getEntityValue("Armour/Pierce"), | "pierce": getEntityValue("Armour/Pierce"), | ||||
▲ Show 20 Lines • Show All 192 Lines • ▼ Show 20 Lines | function GetTemplateDataHelper(template, player, auraTemplates, resources) | ||||
if (template.Identity) | if (template.Identity) | ||||
{ | { | ||||
ret.selectionGroupName = template.Identity.SelectionGroupName; | ret.selectionGroupName = template.Identity.SelectionGroupName; | ||||
ret.name = { | ret.name = { | ||||
"specific": (template.Identity.SpecificName || template.Identity.GenericName), | "specific": (template.Identity.SpecificName || template.Identity.GenericName), | ||||
"generic": template.Identity.GenericName | "generic": template.Identity.GenericName | ||||
}; | }; | ||||
ret.icon = template.Identity.Icon; | ret.icon = template.Identity.Icon; | ||||
ret.tooltip = template.Identity.Tooltip; | ret.tooltip = template.Identity.Tooltip; | ||||
ret.requiredTechnology = template.Identity.RequiredTechnology; | ret.requiredTechnology = template.Identity.RequiredTechnology; | ||||
ret.visibleIdentityClasses = GetVisibleIdentityClasses(template.Identity); | ret.visibleIdentityClasses = GetVisibleIdentityClasses(template.Identity); | ||||
} | } | ||||
if (template.UnitMotion) | if (template.UnitMotion) | ||||
{ | { | ||||
ret.speed = { | ret.speed = { | ||||
"walk": getEntityValue("UnitMotion/WalkSpeed"), | "walk": getEntityValue("UnitMotion/WalkSpeed"), | ||||
Show All 29 Lines | function GetTemplateDataHelper(template, player, auraTemplates, resources) | ||||
if (template.WallPiece) | if (template.WallPiece) | ||||
ret.wallPiece = { "length": +template.WallPiece.Length }; | ret.wallPiece = { "length": +template.WallPiece.Length }; | ||||
return ret; | return ret; | ||||
} | } | ||||
/** | /** | ||||
* Get basic information about a technology template. | |||||
* @param {object} template - A valid template as obtained by loading the tech JSON file. | |||||
bbUnsubmitted Not Done Inline ActionsNever seen that hyphen in comments, nuke bb: Never seen that hyphen in comments, nuke | |||||
s0600204AuthorUnsubmitted Not Done Inline ActionsThen with due respect, you're not looking very hard. Hint: run ack --type=js "\* \@param \{" in binaries/data/mods/public and you'll find that more frequently than not, a hyphen is included. s0600204: Then with due respect, you're not looking very hard.
Hint: run `ack --type=js "\* \@param \{"`… | |||||
* @param {string} civ - Civilization for which the specific name should be returned. | |||||
bbUnsubmitted Done Inline Actionsrequirements seem to also be based on civ bb: requirements seem to also be based on civ | |||||
*/ | |||||
function GetTechnologyBasicDataHelper(template, civ) | |||||
{ | |||||
return { | |||||
"name": { | |||||
"generic": template.genericName | |||||
}, | |||||
"icon": template.icon ? "technologies/" + template.icon : undefined, | |||||
"description": template.description, | |||||
"reqs": DeriveTechnologyRequirements(template, civ) | |||||
}; | |||||
} | |||||
/** | |||||
* Get information about a technology template. | * Get information about a technology template. | ||||
* @param template A valid template as obtained by loading the tech JSON file. | * @param {object} template - A valid template as obtained by loading the tech JSON file. | ||||
* @param civ Civilization for which the specific name should be returned. | * @param {string} civ - Civilization for which the specific name should be returned. | ||||
bbUnsubmitted Done Inline ActionsSee above bb: See above | |||||
* @param resources An instance of the Resources prototype. | |||||
*/ | */ | ||||
function GetTechnologyDataHelper(template, civ, resources) | function GetTechnologyDataHelper(template, civ, resources) | ||||
{ | { | ||||
let ret = {}; | let ret = GetTechnologyBasicDataHelper(template, civ); | ||||
// Get specific name for this civ or else the generic specific name | |||||
let specific; | |||||
if (template.specificName) | if (template.specificName) | ||||
specific = template.specificName[civ] || template.specificName.generic; | ret.name.specific = template.specificName[civ] || template.specificName.generic; | ||||
Done Inline ActionsTrailling comma. bb: Trailling comma. | |||||
Done Inline ActionsMerge this with ret declaration above. bb: Merge this with `ret` declaration above. | |||||
ret.name = { | |||||
"specific": specific, | |||||
"generic": template.genericName, | |||||
}; | |||||
ret.icon = template.icon ? "technologies/" + template.icon : null; | |||||
ret.cost = { "time": template.researchTime ? +template.researchTime : 0 }; | ret.cost = { "time": template.researchTime ? +template.researchTime : 0 }; | ||||
for (let type of resources.GetCodes()) | for (let type of resources.GetCodes()) | ||||
Done Inline ActionsShouldn't that be undefined (instead on null)? bb: Shouldn't that be `undefined` (instead on `null`)?
Imo ternary was better. | |||||
ret.cost[type] = +(template.cost && template.cost[type] || 0); | ret.cost[type] = +(template.cost && template.cost[type] || 0); | ||||
ret.tooltip = template.tooltip; | ret.tooltip = template.tooltip; | ||||
ret.requirementsTooltip = template.requirementsTooltip || ""; | ret.requirementsTooltip = template.requirementsTooltip || ""; | ||||
ret.reqs = DeriveTechnologyRequirements(template, civ); | |||||
ret.description = template.description; | |||||
return ret; | return ret; | ||||
} | } | ||||
function calculateCarriedResources(carriedResources, tradingGoods) | function calculateCarriedResources(carriedResources, tradingGoods) | ||||
{ | { | ||||
var resources = {}; | var resources = {}; | ||||
Done Inline ActionsImproper JsDocs (same as above). bb: Improper JsDocs (same as above). | |||||
if (carriedResources) | if (carriedResources) | ||||
for (let resource of carriedResources) | for (let resource of carriedResources) | ||||
resources[resource.type] = (resources[resource.type] || 0) + resource.amount; | resources[resource.type] = (resources[resource.type] || 0) + resource.amount; | ||||
if (tradingGoods && tradingGoods.amount) | if (tradingGoods && tradingGoods.amount) | ||||
resources[tradingGoods.type] = | resources[tradingGoods.type] = | ||||
(resources[tradingGoods.type] || 0) + | (resources[tradingGoods.type] || 0) + | ||||
(tradingGoods.amount.traderGain || 0) + | (tradingGoods.amount.traderGain || 0) + | ||||
(tradingGoods.amount.market1Gain || 0) + | (tradingGoods.amount.market1Gain || 0) + | ||||
(tradingGoods.amount.market2Gain || 0); | (tradingGoods.amount.market2Gain || 0); | ||||
return resources; | return resources; | ||||
} | } | ||||
One could add spaces in the commented object for consistency, meh.
Committer can add them too