From 9d6c7be3db59404220258264d804671163a0e0e5 Mon Sep 17 00:00:00 2001 From: Steph Enders Date: Thu, 15 Dec 2022 22:38:19 -0500 Subject: Fix best effort to work properly Before it didn't do what I assumed it was doing. Still has bug where it can't figure out to move around an impasse (see lvl4) --- dnglib/algs.lua | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'dnglib') diff --git a/dnglib/algs.lua b/dnglib/algs.lua index 3bd3033..d2e6252 100644 --- a/dnglib/algs.lua +++ b/dnglib/algs.lua @@ -125,24 +125,21 @@ local function best_effort_move(start_pos, target_pos, map) local move = { dx = 0, dy = 0 } - if (math.abs(diff_x) > math.abs(diff_y)) then - -- do x moves first - if diff_x > 0 and can_move(start_pos.x + 1, start_pos.y, map) then - move.dx = 1 - return move - elseif diff_x < 0 and can_move(start_pos.x - 1, start_pos.y, map) then - move.dx = -1 - return move - end - else -- y moves - if diff_y > 0 and can_move(start_pos.x, start_pos.y + 1, map) then - move.dy = 1 - return move - elseif diff_y < 0 and can_move(start_pos.y, start_pos.y - 1, map) then - move.dy = -1 - return move - end + if diff_x > 0 and can_move(start_pos.x + 1, start_pos.y, map) then + move.dx = 1 + return move + elseif diff_x < 0 and can_move(start_pos.x - 1, start_pos.y, map) then + move.dx = -1 + return move end + if diff_y > 0 and can_move(start_pos.x, start_pos.y + 1, map) then + move.dy = 1 + return move + elseif diff_y < 0 and can_move(start_pos.x, start_pos.y - 1, map) then + move.dy = -1 + return move + end + -- return 0, 0 move return move end -- cgit v1.2.3-54-g00ecf