summaryrefslogtreecommitdiff
path: root/dnglib/algs.lua
diff options
context:
space:
mode:
authorSteph Enders <smenders@gmail.com>2022-12-15 22:38:19 -0500
committerSteph Enders <smenders@gmail.com>2022-12-15 22:40:52 -0500
commit9d6c7be3db59404220258264d804671163a0e0e5 (patch)
treedfd7dffc8baf3d6f4f82b40e42102c61b2c63085 /dnglib/algs.lua
parentda3922f1aa532b628693f5093f464d263831b153 (diff)
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)
Diffstat (limited to 'dnglib/algs.lua')
-rw-r--r--dnglib/algs.lua31
1 files changed, 14 insertions, 17 deletions
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