From da3922f1aa532b628693f5093f464d263831b153 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Thu, 15 Dec 2022 22:37:34 -0500 Subject: Disable enemy movement through enemies or treasure --- dnglib/algs.lua | 21 ++++++++++++++++++--- dnglib/defaults.lua | 6 +++++- 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'dnglib') diff --git a/dnglib/algs.lua b/dnglib/algs.lua index 0b66604..3bd3033 100644 --- a/dnglib/algs.lua +++ b/dnglib/algs.lua @@ -151,20 +151,35 @@ end --- ---@param start_pos table [x, y] ---@param target_pos table [x, y] +-- @param enemies list of enemy positions (cannot pass thru enemy) +-- @param treasures list of treasure positions (cannot pass thru treasure) ---@param map table 2D map array ---@return table best move to target [x, y] --- -local function pathfind(start_pos, target_pos, map) +local function pathfind(start_pos, target_pos, enemies, treasures, map) local queue = Queue:new() + local visit_map = {} for k, v in ipairs(map) do row = {} for ik, iv in ipairs(v) do row[ik] = iv end - visit_map[k] = row + visit_map[k] = row end + for _, e in ipairs(enemies) do + if e ~= start_pos then + visit_map[e.y][e.x] = MAP_WALL -- use wall value for impass + end + end + for _, t in ipairs(treasures) do + visit_map[t.y][t.x] = MAP_WALL -- use wall value for impass + end + + -- since we mutate the visit_map let's calc this early if need be + local best_effort = best_effort_move(start_pos, target_pos, visit_map) + if (push_moves(start_pos.x, start_pos.y, nil, visit_map, queue, target_pos)) then return { dx = target_pos.x - start_pos.x, dy = target_pos.y - start_pos.y } end @@ -179,7 +194,7 @@ local function pathfind(start_pos, target_pos, map) end --- cannot find path - just move "towards" player - return best_effort_move(start_pos, target_pos, map) + return best_effort end return { diff --git a/dnglib/defaults.lua b/dnglib/defaults.lua index 77f688c..5b1c7c5 100644 --- a/dnglib/defaults.lua +++ b/dnglib/defaults.lua @@ -98,6 +98,9 @@ function onUpdate(dt) player = c_get_player_position() assert(type(player) == "table", "Player is not a table") + + treasures = c_get_treasures() + assert(type(treasures) == "table", "treasures is not a table") map = c_get_map(); assert(type(map) == "table", "map is not a table") @@ -105,10 +108,11 @@ function onUpdate(dt) for _, v in ipairs(enemies) do local next; if diff_time >= MOV_TIME then - next = algs.pathfind(v, player, map) + next = algs.pathfind(v, player, enemies, treasures, map) else next = { dx = 0, dy = 0 } end + new_pos = c_move_enemy(v.id, next.dx, next.dy) assert(type(new_pos) == "table", "new_pos is not a table") if new_pos.x == player.x and new_pos.y == player.y then -- cgit v1.2.3-54-g00ecf