From 9fe465884d37ffd20cf09dc131442dbf3eaeb627 Mon Sep 17 00:00:00 2001 From: Spahbod Date: Fri, 31 Jan 2014 12:01:49 +0000 Subject: [PATCH] Implements resource trickle. Allows decimal player resources. Fixes #2413. This was SVN commit r14710. --- .../data/mods/public/gui/session/session.js | 8 +-- .../public/simulation/components/Player.js | 2 +- .../simulation/components/ResourceTrickle.js | 63 +++++++++++++++++++ .../components/interfaces/ResourceTrickle.js | 1 + .../templates/structures/pers_apadana.xml | 9 ++- 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 binaries/data/mods/public/simulation/components/ResourceTrickle.js create mode 100644 binaries/data/mods/public/simulation/components/interfaces/ResourceTrickle.js diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js index 199ed4444b..242890be95 100644 --- a/binaries/data/mods/public/gui/session/session.js +++ b/binaries/data/mods/public/gui/session/session.js @@ -669,10 +669,10 @@ function updatePlayerDisplay() if (!playerState) return; - Engine.GetGUIObjectByName("resourceFood").caption = playerState.resourceCounts.food; - Engine.GetGUIObjectByName("resourceWood").caption = playerState.resourceCounts.wood; - Engine.GetGUIObjectByName("resourceStone").caption = playerState.resourceCounts.stone; - Engine.GetGUIObjectByName("resourceMetal").caption = playerState.resourceCounts.metal; + Engine.GetGUIObjectByName("resourceFood").caption = Math.floor(playerState.resourceCounts.food); + Engine.GetGUIObjectByName("resourceWood").caption = Math.floor(playerState.resourceCounts.wood); + Engine.GetGUIObjectByName("resourceStone").caption = Math.floor(playerState.resourceCounts.stone); + Engine.GetGUIObjectByName("resourceMetal").caption = Math.floor(playerState.resourceCounts.metal); Engine.GetGUIObjectByName("resourcePop").caption = playerState.popCount + "/" + playerState.popLimit; g_IsTrainingBlocked = playerState.trainingBlocked; diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js index 94b25a6770..6de6532660 100644 --- a/binaries/data/mods/public/simulation/components/Player.js +++ b/binaries/data/mods/public/simulation/components/Player.js @@ -199,7 +199,7 @@ Player.prototype.GetNeededResources = function(amounts) var amountsNeeded = {}; for (var type in amounts) if (this.resourceCount[type] != undefined && amounts[type] > this.resourceCount[type]) - amountsNeeded[type] = amounts[type] - this.resourceCount[type]; + amountsNeeded[type] = amounts[type] - Math.floor(this.resourceCount[type]); if (Object.keys(amountsNeeded).length == 0) return undefined; diff --git a/binaries/data/mods/public/simulation/components/ResourceTrickle.js b/binaries/data/mods/public/simulation/components/ResourceTrickle.js new file mode 100644 index 0000000000..f2f09e1972 --- /dev/null +++ b/binaries/data/mods/public/simulation/components/ResourceTrickle.js @@ -0,0 +1,63 @@ +function ResourceTrickle() {} + +ResourceTrickle.prototype.Schema = + "Controls the resource trickle ability of the unit." + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + +ResourceTrickle.prototype.Init = function() +{ + // Call the timer + var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); + cmpTimer.SetInterval(this.entity, IID_ResourceTrickle, "Trickle", this.GetTimer(), this.GetTimer(), undefined) +}; + +ResourceTrickle.prototype.GetTimer = function() +{ + var interval = +this.template.Interval; + return interval; +}; + +ResourceTrickle.prototype.GetRates = function() +{ + var foodrate = +this.template.FoodRate; + var woodrate = +this.template.WoodRate; + var stonerate = +this.template.StoneRate; + var metalrate = +this.template.MetalRate; + + foodrate = ApplyValueModificationsToEntity("ResourceTrickle/FoodRate", foodrate, this.entity); + woodrate = ApplyValueModificationsToEntity("ResourceTrickle/WoodRate", woodrate, this.entity); + stonerate = ApplyValueModificationsToEntity("ResourceTrickle/StoneRate", stonerate, this.entity); + metalrate = ApplyValueModificationsToEntity("ResourceTrickle/MetalRate", metalrate, this.entity); + + return {"food": foodrate, "wood": woodrate, "stone": stonerate, "metal": metalrate }; +}; + +// Do the actual work here +ResourceTrickle.prototype.Trickle = function(data, lateness) +{ + // Get the rates + var rates = this.GetRates(); + + // Get the player + var cmpPlayer = QueryOwnerInterface(this.entity, IID_Player); + + for (var resource in rates) + cmpPlayer.AddResource(resource, rates[resource]); + +}; + +Engine.RegisterComponentType(IID_ResourceTrickle, "ResourceTrickle", ResourceTrickle); diff --git a/binaries/data/mods/public/simulation/components/interfaces/ResourceTrickle.js b/binaries/data/mods/public/simulation/components/interfaces/ResourceTrickle.js new file mode 100644 index 0000000000..f577a0bad8 --- /dev/null +++ b/binaries/data/mods/public/simulation/components/interfaces/ResourceTrickle.js @@ -0,0 +1 @@ +Engine.RegisterInterface("ResourceTrickle"); diff --git a/binaries/data/mods/public/simulation/templates/structures/pers_apadana.xml b/binaries/data/mods/public/simulation/templates/structures/pers_apadana.xml index f1a34b5133..1e9cda88d8 100644 --- a/binaries/data/mods/public/simulation/templates/structures/pers_apadana.xml +++ b/binaries/data/mods/public/simulation/templates/structures/pers_apadana.xml @@ -31,7 +31,7 @@ The term Apadana designates a large hypostyle palace found in Persia. The best known example, and by far the largest, was the great Apadana at Persepolis. Functioning as the empire's central audience hall, the palace is famous for the reliefs of the tribute-bearers and of the army, including the Immortals. The annual tribute that the Persians received from their satrapies and vassal states, as regularised by Darius the Great, accounted for incredible annual revenue. structures/palace.png - "Satrapy Tribute": Gain a trickle of food, wood, stone, and metal resources (not implemented yet). Train Persian heroes and their "Immortals" bodyguards. Build Limit: 1. + "Satrapy Tribute": Gain a trickle of food, wood, stone, and metal resources. Train Persian heroes and their "Immortals" bodyguards. Build Limit: 1. @@ -52,6 +52,13 @@ persians/immortals + + 1.0 + 1.0 + 0.75 + 0.75 + 1000 + structures/persians/sb1_new.xml structures/fndn_6x6.xml