Index: binaries/data/mods/public/globalscripts/Technologies.js =================================================================== --- binaries/data/mods/public/globalscripts/Technologies.js +++ binaries/data/mods/public/globalscripts/Technologies.js @@ -19,6 +19,16 @@ if (!modifications.length) 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 add = 0; @@ -28,18 +38,33 @@ continue; if (modification.replace !== undefined) return modification.replace; - if (modification.tokens !== undefined) - return HandleTokens(originalValue, modification.tokens); - if (modification.multiply) + else if (modification.multiply) multiply *= modification.multiply; else if (modification.add) add += modification.add; else - warn("GetTechModifiedProperty: modification format not recognised : " + uneval(modification)); + warn("GetTechModifiedProperty: numeric modification format not recognised : " + uneval(modification)); } 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) + return HandleTokens(originalValue, modification.tokens); + 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 */ Index: binaries/data/mods/public/globalscripts/tests/test_Technologies_effects.js =================================================================== --- /dev/null +++ binaries/data/mods/public/globalscripts/tests/test_Technologies_effects.js @@ -0,0 +1,47 @@ +// This tests the GetTechModifiedProperty function. + +function test_numeric() +{ + let add = [{ "add": 10, "affects": "Unit" }]; + + let add_add = [{ "add": 10, "affects": "Unit" }, { "add": 5, "affects": "Unit" }]; + + let add_mul_add = [{ "add": 10, "affects": "Unit" }, { "multiply": 2, "affects": "Unit" }, { "add": 5, "affects": "Unit" }]; + + let add_replace = [{ "add": 10, "affects": "Unit" }, { "replace": 10, "affects": "Unit" }]; + + let replace_add = [{ "replace": 10, "affects": "Unit" }, { "add": 10, "affects": "Unit" }]; + + let replace_replace = [{ "replace": 10, "affects": "Unit" }, { "replace": 30, "affects": "Unit" }]; + + TS_ASSERT_EQUALS(GetTechModifiedProperty(add, "Unit", 5), 15); + TS_ASSERT_EQUALS(GetTechModifiedProperty(add_add, "Unit", 5), 20); + TS_ASSERT_EQUALS(GetTechModifiedProperty(add_add, "Other", 5), 5); + + // Technologies work by multiplying then adding all. + TS_ASSERT_EQUALS(GetTechModifiedProperty(add_mul_add, "Unit", 5), 25); + + TS_ASSERT_EQUALS(GetTechModifiedProperty(add_replace, "Unit", 5), 10); + + // Only the first replace is taken into account + TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_replace, "Unit", 5), 10); +} +test_numeric(); + +function test_non_numeric() +{ + let replace_nonnum = [{ "replace": "alpha", "affects": "Unit" }]; + + TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_nonnum, "Unit", "beta"), "alpha"); + TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_nonnum, "Structure", "beta"), "beta"); + + let replace_tokens = [{ "tokens": "-beta alpha gamma -delta", "affects": "Unit" }]; + TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_tokens, "Unit", "beta"), "alpha gamma"); + TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_tokens, "Structure", "beta"), "beta"); + + let replace_tokens_2 = [{ "tokens": "beta>gamma -delta", "affects": "Unit" }]; + TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_tokens_2, "Unit", "beta"), "gamma"); + TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_tokens_2, "Structure", "beta"), "beta"); +} + +test_non_numeric(); Index: binaries/data/mods/public/globalscripts/tests/test_Technologies_reqs.js =================================================================== --- binaries/data/mods/public/globalscripts/tests/test_Technologies_reqs.js +++ binaries/data/mods/public/globalscripts/tests/test_Technologies_reqs.js @@ -1,5 +1,3 @@ -// TODO: Move this to a folder of tests for GlobalScripts (once one is created) - // No requirements set in template let template = {}; TS_ASSERT_UNEVAL_EQUALS(DeriveTechnologyRequirements(template, "athen"), []); Index: binaries/data/mods/public/simulation/components/tests/test_Technologies_effects.js =================================================================== --- binaries/data/mods/public/simulation/components/tests/test_Technologies_effects.js +++ /dev/null @@ -1,30 +0,0 @@ -// TODO: Move this to a folder of tests for GlobalScripts (once one is created) - -// This tests the GetTechModifiedProperty function. -let add = [{ "add": 10, "affects": "Unit" }]; - -let add_add = [{ "add": 10, "affects": "Unit" }, { "add": 5, "affects": "Unit" }]; - -let add_mul_add = [{ "add": 10, "affects": "Unit" }, { "multiply": 2, "affects": "Unit" }, { "add": 5, "affects": "Unit" }]; - -let add_replace = [{ "add": 10, "affects": "Unit" }, { "replace": 10, "affects": "Unit" }]; - -let replace_add = [{ "replace": 10, "affects": "Unit" }, { "add": 10, "affects": "Unit" }]; - -let replace_replace = [{ "replace": 10, "affects": "Unit" }, { "replace": 30, "affects": "Unit" }]; - -let replace_nonnum = [{ "replace": "alpha", "affects": "Unit" }]; - -TS_ASSERT_EQUALS(GetTechModifiedProperty(add, "Unit", 5), 15); -TS_ASSERT_EQUALS(GetTechModifiedProperty(add_add, "Unit", 5), 20); -TS_ASSERT_EQUALS(GetTechModifiedProperty(add_add, "Other", 5), 5); - -// Technologies work by multiplying then adding all. -TS_ASSERT_EQUALS(GetTechModifiedProperty(add_mul_add, "Unit", 5), 25); - -TS_ASSERT_EQUALS(GetTechModifiedProperty(add_replace, "Unit", 5), 10); - -// Only the first replace is taken into account -TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_replace, "Unit", 5), 10); - -TS_ASSERT_EQUALS(GetTechModifiedProperty(replace_nonnum, "Unit", "beta"), "alpha");