Browse Source

Freeze results of operations on costs. Fix prod upgrades not getting the food cost of buildings

tags/v0.0.6
Fen Dweller 5 years ago
parent
commit
c1693e8804
No known key found for this signature in database GPG Key ID: E80B35A6F11C3656
3 changed files with 9 additions and 10 deletions
  1. +1
    -1
      constants.js
  2. +7
    -8
      gorge.js
  3. +1
    -1
      util.js

+ 1
- 1
constants.js View File

@@ -317,7 +317,7 @@ function createProdUpgrades() {
"desc": contents.desc, "desc": contents.desc,
"icon": buildings[key].icon, "icon": buildings[key].icon,
"cost": { "cost": {
"food": buildings[key].cost * 5 * Math.pow(10, counter - 1)
"food": buildings[key].cost.food * 5 * Math.pow(10, counter - 1)
}, },
"effects": [ "effects": [
{ {


+ 7
- 8
gorge.js View File

@@ -116,27 +116,26 @@ function productivityMultiplierOf(type) {
function productivityOf(type) { function productivityOf(type) {
let baseProd = makeCost(buildings[type].prod); let baseProd = makeCost(buildings[type].prod);


baseProd.food *= productivityMultiplierOf(type);
baseProd = scaleCost(baseProd, productivityMultiplierOf(type));


baseProd.food *= getGlobalProdBonus();
baseProd = scaleCost(baseProd, productivityMultiplierOf(type));


baseProd.food *= belongings[type].count;
baseProd = scaleCost(baseProd, belongings[type].count);


return baseProd; return baseProd;
} }


function makeCost(source) { function makeCost(source) {
const empty = mapObject(resourceTypes, () => 0); const empty = mapObject(resourceTypes, () => 0);
Object.preventExtensions(empty);
return {...empty, ...source};
return deepFreeze({...empty, ...source});
} }


function addCost(cost1, cost2) { function addCost(cost1, cost2) {
return Object.keys(resourceTypes).reduce((o, k) => ({ ...o, [k]: cost1[k] + cost2[k]}), {});
return deepFreeze(Object.keys(resourceTypes).reduce((o, k) => ({ ...o, [k]: cost1[k] + cost2[k]}), {}));
} }


function scaleCost(cost, scale) { function scaleCost(cost, scale) {
return Object.keys(resourceTypes).reduce((o, k) => ({ ...o, [k]: cost[k] * scale}), {});
return deepFreeze(Object.keys(resourceTypes).reduce((o, k) => ({ ...o, [k]: cost[k] * scale}), {}));
} }


function costOfBuilding(type, count = 1) { function costOfBuilding(type, count = 1) {
@@ -144,7 +143,7 @@ function costOfBuilding(type, count = 1) {


while (count > 0) { while (count > 0) {
let baseCost = makeCost(buildings[type].cost); let baseCost = makeCost(buildings[type].cost);
baseCost.food *= Math.pow(1.15, belongings[type].count + count - 1);
baseCost = scaleCost(baseCost, Math.pow(1.15, belongings[type].count + count - 1));
total = addCost(total, baseCost); total = addCost(total, baseCost);
count--; count--;
} }


+ 1
- 1
util.js View File

@@ -20,7 +20,7 @@ function round(val, places = 0) {
} }


function mapObject(obj, func) { function mapObject(obj, func) {
return Object.keys(obj).reduce((o, k) => ({ ...o, [k]: func(obj[k])}), {});
return deepFreeze(Object.keys(obj).reduce((o, k) => ({ ...o, [k]: func(obj[k])}), {}));
} }


function deepFreeze(object) { function deepFreeze(object) {


Loading…
Cancel
Save