Browse Source

make astar work like other pathfinding

master
Nicholas Hayashi 4 years ago
parent
commit
32566b3f61
  1. 4
      src/hexyz.lua
  2. 4
      src/mob.lua
  3. 2
      src/tower.lua

4
src/hexyz.lua

@ -517,7 +517,7 @@ end
-- function (from, to) -- from and to are vec2's -- function (from, to) -- from and to are vec2's
-- return some numeric value -- return some numeric value
-- --
function hex_Astar(map, start, goal, heuristic, cost_f)
function hex_Astar(map, start, goal, cost_f, neighbour_f, heuristic)
local path = {} local path = {}
hex_map_set(path, start, false) hex_map_set(path, start, false)
@ -536,7 +536,7 @@ function hex_Astar(map, start, goal, heuristic, cost_f)
break break
end end
for _,next_ in pairs(map.neighbours(current.hex)) do
for _,next_ in pairs(neighbour_f(map, current.hex)) do
local new_cost = hex_map_get(path_so_far, current.hex.x, current.hex.y) + cost_f(map, current.hex, next_) local new_cost = hex_map_get(path_so_far, current.hex.x, current.hex.y) + cost_f(map, current.hex, next_)
local next_cost = hex_map_get(path_so_far, next_.x, next_.y) local next_cost = hex_map_get(path_so_far, next_.x, next_.y)

4
src/mob.lua

@ -120,10 +120,6 @@ function make_mob_node(mob_type, mob)
end end
end end
function get_mob_path(mob, map, start, goal)
return Astar(map, goal, start, grid_heuristic, grid_cost)
end
local function get_spawn_hex() local function get_spawn_hex()
-- ensure we spawn on an random tile along the map's edges -- ensure we spawn on an random tile along the map's edges
local roll = math.random(HEX_GRID_WIDTH * 2 + HEX_GRID_HEIGHT * 2) - 1 local roll = math.random(HEX_GRID_WIDTH * 2 + HEX_GRID_HEIGHT * 2) - 1

2
src/tower.lua

@ -441,7 +441,7 @@ function update_tower_lighthouse(tower, tower_index)
-- is within some angle range...? if the mob is heading directly away from the tower, then -- is within some angle range...? if the mob is heading directly away from the tower, then
-- the lighthouse shouldn't do much -- the lighthouse shouldn't do much
local path, made_it = hex_Astar(state.map, tower.hex, m.hex, grid_heuristic, grid_cost)
local path, made_it = hex_Astar(state.map, tower.hex, m.hex, grid_neighbours, grid_cost, heuristic)
if made_it then if made_it then
m.path = path m.path = path

Loading…
Cancel
Save