From d90e53d0a55b6f211d95a1a40de51b82412bf130 Mon Sep 17 00:00:00 2001 From: Deiz Date: Tue, 14 Aug 2012 07:51:50 +0000 Subject: [PATCH] Show cost ranges in the wall construction tooltip, if possible. This was SVN commit r12421. --- .../public/gui/session/utility_functions.js | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/binaries/data/mods/public/gui/session/utility_functions.js b/binaries/data/mods/public/gui/session/utility_functions.js index aab841459b..52e62a59ce 100644 --- a/binaries/data/mods/public/gui/session/utility_functions.js +++ b/binaries/data/mods/public/gui/session/utility_functions.js @@ -269,6 +269,64 @@ function getEntityCostComponentsTooltipString(template) return costs; } +/** + * Returns an array of strings for a set of wall pieces. If the pieces share + * resource type requirements, output will be of the form '10 to 30 Stone', + * otherwise output will be, e.g. '10 Stone, 20 Stone, 30 Stone'. + */ +function getWallPieceTooltip(wallTypes) +{ + var out = []; + var resourceCount = {}; + + // Initialize the acceptable types for '$x to $y $resource' mode. + for (var resource in wallTypes[0].cost) + if (wallTypes[0].cost[resource]) + resourceCount[resource] = [wallTypes[0].cost[resource]]; + + var sameTypes = true; + for (var i = 1; i < wallTypes.length; ++i) + { + for (var resource in wallTypes[i].cost) + { + // Break out of the same-type mode if this wall requires + // resource types that the first didn't. + if (wallTypes[i].cost[resource] && !resourceCount[resource]) + { + sameTypes = false; + break; + } + } + + for (var resource in resourceCount) + { + if (wallTypes[i].cost[resource]) + resourceCount[resource].push(wallTypes[i].cost[resource]); + else + { + sameTypes = false; + break; + } + } + } + + if (sameTypes) + { + for (var resource in resourceCount) + { + var resourceMin = Math.min.apply(Math, resourceCount[resource]); + var resourceMax = Math.max.apply(Math, resourceCount[resource]); + + out.push(resourceMin + " to " + resourceMax + " [font=\"serif-12\"]" + getCostComponentDisplayName(resource) + "[/font]"); + } + } + else + for (var i = 0; i < wallTypes.length; ++i) + out.push(getEntityCostComponentsTooltipString(wallTypes[i]).join(", ")); + + return out; +} + /** * Returns the cost information to display in the specified entity's construction button tooltip. */ @@ -285,13 +343,11 @@ function getEntityCostTooltip(template) var templateShort = GetTemplateData(template.wallSet.templates.short); var templateTower = GetTemplateData(template.wallSet.templates.tower); - // TODO: the costs of the wall segments should be the same, and for now we will assume they are (ideally we - // should take the average here or something). - var wallCosts = getEntityCostComponentsTooltipString(templateLong); + var wallCosts = getWallPieceTooltip([templateShort, templateMedium, templateLong]); var towerCosts = getEntityCostComponentsTooltipString(templateTower); cost += "\n"; - cost += " Walls: " + wallCosts.join(", ") + "\n"; + cost += " Walls: " + wallCosts.join("; ") + "\n"; cost += " Towers: " + towerCosts.join(", "); } else if (template.cost)