From 7edf0fc75a218472b008e3d33d3ff3998b9dc8e8 Mon Sep 17 00:00:00 2001 From: Triangle345 Date: Sat, 6 May 2017 16:40:17 -0400 Subject: [PATCH] added cost member for node to use for weighted cost evalulation --- jumper/core/node.lua | 6 ++++-- jumper/core/utils.lua | 2 +- jumper/pathfinder.lua | 6 ++++++ jumper/search/astar.lua | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/jumper/core/node.lua b/jumper/core/node.lua index 4081853..914dd40 100644 --- a/jumper/core/node.lua +++ b/jumper/core/node.lua @@ -25,8 +25,8 @@ if (...) then -- @tparam int y the y-coordinate of the node on the collision map -- @treturn node a new `node` -- @usage local node = Node(3,4) - function Node:new(x,y) - return setmetatable({_x = x, _y = y, _clearance = {}}, Node) + function Node:new(x,y,c) + return setmetatable({_x = x, _y = y, _c=c,_clearance = {}}, Node) end -- Enables the use of operator '<' to compare nodes. @@ -45,6 +45,8 @@ if (...) then -- @usage local y = node:getY() function Node:getY() return self._y end + function Node:getCost() return self._c end + --- Returns x and y coordinates of a `node` -- @class function -- @treturn number the x-coordinate of the `node` diff --git a/jumper/core/utils.lua b/jumper/core/utils.lua index 50ab0da..8382d95 100644 --- a/jumper/core/utils.lua +++ b/jumper/core/utils.lua @@ -75,7 +75,7 @@ if (...) then for x in pairs(map[y]) do min_x = not min_x and x or (xmax_x and x or max_x) - nodes[y][x] = Node:new(x,y) + nodes[y][x] = Node:new(x,y,map[y][x]) end end return nodes, diff --git a/jumper/pathfinder.lua b/jumper/pathfinder.lua index 685d4a5..6c9b8f2 100644 --- a/jumper/pathfinder.lua +++ b/jumper/pathfinder.lua @@ -247,6 +247,12 @@ if (...) then return self end + function Pathfinder:setCostEval(costEval) + assert((type(costEval) == 'function'),'Not a valid heuristic!') + self._costEval = costEval + return self + end + --- Returns the `heuristic` used. Returns the function itself. -- @class function -- @treturn func the `heuristic` function being used by the `pathfinder` diff --git a/jumper/search/astar.lua b/jumper/search/astar.lua index 094a207..2a6df28 100644 --- a/jumper/search/astar.lua +++ b/jumper/search/astar.lua @@ -15,7 +15,7 @@ if (...) then -- Updates G-cost local function computeCost(node, neighbour, finder, clearance) - local mCost = Heuristics.EUCLIDIAN(neighbour, node) + local mCost = neighbour._c--Heuristics.EUCLIDIAN(neighbour, node) if node._g + mCost < neighbour._g then neighbour._parent = node neighbour._g = node._g + mCost