Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/globalscripts/Technologies.js
Show All 13 Lines | |||||
* @param originalValue Number storing the original value. Can also be | * @param originalValue Number storing the original value. Can also be | ||||
* non-numeric, but then only "replace" and "tokens" techs can be supported. | * non-numeric, but then only "replace" and "tokens" techs can be supported. | ||||
*/ | */ | ||||
function GetTechModifiedProperty(modifications, classes, originalValue) | function GetTechModifiedProperty(modifications, classes, originalValue) | ||||
{ | { | ||||
if (!modifications.length) | if (!modifications.length) | ||||
return originalValue; | return originalValue; | ||||
// From indicative profiling, splitting in two sub-functions or checking directly | |||||
// is about as efficient, but splitting makes it easier to report errors. | |||||
if (typeof originalValue === "string") | |||||
return GetTechModifiedProperty_string(modifications, classes, originalValue); | |||||
return GetTechModifiedProperty_numeric(modifications, classes, originalValue); | |||||
} | |||||
function GetTechModifiedProperty_numeric(modifications, classes, originalValue) | |||||
{ | |||||
let multiply = 1; | let multiply = 1; | ||||
let add = 0; | let add = 0; | ||||
for (let modification of modifications) | for (let modification of modifications) | ||||
{ | { | ||||
if (!DoesModificationApply(modification, classes)) | if (!DoesModificationApply(modification, classes)) | ||||
continue; | continue; | ||||
if (modification.replace !== undefined) | if (modification.replace !== undefined) | ||||
return modification.replace; | return modification.replace; | ||||
if (modification.tokens !== undefined) | else if (modification.multiply) | ||||
return HandleTokens(originalValue, modification.tokens); | |||||
if (modification.multiply) | |||||
multiply *= modification.multiply; | multiply *= modification.multiply; | ||||
else if (modification.add) | else if (modification.add) | ||||
add += modification.add; | add += modification.add; | ||||
else | else | ||||
warn("GetTechModifiedProperty: modification format not recognised : " + uneval(modification)); | warn("GetTechModifiedProperty: numeric modification format not recognised : " + uneval(modification)); | ||||
} | } | ||||
return originalValue * multiply + add; | return originalValue * multiply + add; | ||||
} | } | ||||
function GetTechModifiedProperty_string(modifications, classes, originalValue) | |||||
{ | |||||
for (let modification of modifications) | |||||
{ | |||||
if (!DoesModificationApply(modification, classes)) | |||||
continue; | |||||
if (modification.replace !== undefined) | |||||
return modification.replace; | |||||
else if (modification.tokens !== undefined) | |||||
Freagarach: `else` can be dropped. | |||||
return HandleTokens(originalValue, modification.tokens); | |||||
FreagarachUnsubmitted Done Inline ActionsMultiple techs adding some tokens is not supported? Freagarach: Multiple techs adding some tokens is not supported? | |||||
wraitiiAuthorUnsubmitted Done Inline ActionsHm, good point. It does seem like it should. wraitii: Hm, good point. It does seem like it should. | |||||
wraitiiAuthorUnsubmitted Done Inline ActionsThe trouble with that is that it runs into ordering issue -> the order in which you research tech matters. This is incidentally why the "multiply/add" stuff was added in the first place for numeric values, but I don't really see an easy way to fix it for tokens unless I were to implement a rather complex tree-system. As of now, it'll keep the order of research, I believe. This is sane enough I think, so I'll still allow it, but it's a thing to keep in mind. wraitii: The trouble with that is that it runs into ordering issue -> the order in which you research… | |||||
FreagarachUnsubmitted Done Inline ActionsYep, I figured that :) A word of caution -- in the code as well as in any documentation -- may be beneficial. Freagarach: Yep, I figured that :) A word of caution -- in the code as well as in any documentation -- may… | |||||
StanUnsubmitted Done Inline ActionsWill need a test too. Stan: Will need a test too. | |||||
else | |||||
warn("GetTechModifiedProperty: string modification format not recognised : " + uneval(modification)); | |||||
} | |||||
return originalValue; | |||||
} | |||||
/** | /** | ||||
* Returns whether the given modification applies to the entity containing the given class list | * Returns whether the given modification applies to the entity containing the given class list | ||||
*/ | */ | ||||
function DoesModificationApply(modification, classes) | function DoesModificationApply(modification, classes) | ||||
{ | { | ||||
return MatchesClassList(classes, modification.affects); | return MatchesClassList(classes, modification.affects); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 294 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
else can be dropped.