From 8fadb4bdb72a65852469ed1647372f72d8612e78 Mon Sep 17 00:00:00 2001 From: IronMonk88 Date: Mon, 29 Mar 2021 21:59:34 -0700 Subject: [PATCH] 1.0.17 changes --- README.md | 2 +- classes/terrainlayer.js | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 499e245..027c674 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ options {elevation: 0, reduce:[], tokenId: token.id, token:token} lets the terra - reduce: [{id:'arctic',value:1}] will result in any calculation essentially ignoring arctic terrain. [{id:'arctic',value:'-1',stop:1}] will result in any calculation reducing the difficulty by 1 and stopping at 1. You can also use '+1' to add to the difficulty. stop is an optional parameter. And you can use the id 'token' to have these settings applied when calculating cost through another token's space. - tokenId - pass in the token id to avoid having the result use the token's own space as difficult terrain. - token - pass in the token, will use both the id and elevation of that token. passing in elevation:false will result in the the function ignoring the token's elevation. -- calculate - this is how you'd like the cost to be calculated. default is 'maximum', which returns the highest value found while looking through all terrains. you can also passin 'additive' if you want all costs to be added together. +- calculate - this is how you'd like the cost to be calculated. default is 'maximum', which returns the highest value found while looking through all terrains. you can also pass in 'additive' if you want all costs to be added together. And if neither of those work, you can pass your own function in to make the final calculation `calculate(cost, total, object)` with cost being the current cost and total being the running total so far and object being either the terrain, measure, or token that's caused the difficult terrain. if you'd like all the details of how the cost was acheived, call costDetails(pts, options). It will return the cost, and an array of details that combined to make the cost. diff --git a/classes/terrainlayer.js b/classes/terrainlayer.js index 76bc65c..203adb0 100644 --- a/classes/terrainlayer.js +++ b/classes/terrainlayer.js @@ -107,11 +107,15 @@ export class TerrainLayer extends PlaceablesLayer { let calculate = options.calculate || 'maximum'; let calculateFn = function (cost, total) { return cost; }; - switch (calculate) { - case 'maximum': - calculateFn = function (cost, total) { return Math.max(cost, total); }; break; - case 'additive': - calculateFn = function (cost, total) { return cost + total; }; break; + if (typeof calculate == 'function') + calculateFn = calculate; + else { + switch (calculate) { + case 'maximum': + calculateFn = function (cost, total) { return Math.max(cost, total); }; break; + case 'additive': + calculateFn = function (cost, total) { return cost + total; }; break; + } } for (let pt of pts) { @@ -138,7 +142,7 @@ export class TerrainLayer extends PlaceablesLayer { detail.reduce = reduce; terraincost = reduceFn(terraincost, reduce); } - cost = calculateFn(terraincost, cost); + cost = calculateFn(terraincost, cost, terrain); detail.total = cost; details.push(detail); @@ -165,7 +169,7 @@ export class TerrainLayer extends PlaceablesLayer { terraincost = reduceFn(terraincost, reduce); } - cost = calculateFn(terraincost, cost); + cost = calculateFn(terraincost, cost, measure); detail.total = cost; details.push(detail); @@ -189,7 +193,7 @@ export class TerrainLayer extends PlaceablesLayer { terraincost = reduceFn(terraincost, reduce); } - cost = calculateFn(terraincost, cost); + cost = calculateFn(terraincost, cost, token); detail.total = cost; details.push(detail);