diff options
author | Steph Enders <smenders@gmail.com> | 2022-12-15 22:38:19 -0500 |
---|---|---|
committer | Steph Enders <smenders@gmail.com> | 2022-12-15 22:40:52 -0500 |
commit | 9d6c7be3db59404220258264d804671163a0e0e5 (patch) | |
tree | dfd7dffc8baf3d6f4f82b40e42102c61b2c63085 | |
parent | da3922f1aa532b628693f5093f464d263831b153 (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)
-rw-r--r-- | dnglib/algs.lua | 31 | ||||
-rw-r--r-- | maps/lvl4/dng.map | 10 |
2 files changed, 24 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 diff --git a/maps/lvl4/dng.map b/maps/lvl4/dng.map new file mode 100644 index 0000000..65617b0 --- /dev/null +++ b/maps/lvl4/dng.map @@ -0,0 +1,10 @@ +w w w w w w w w w w +w 0 0 0 0 0 0 0 0 w +w 0 t t t t t t 0 w +w 0 t 0 0 0 0 t 0 w +w p t 0 t e 0 t 0 w +w 0 t 0 t 0 0 t 0 w +w 0 t 0 0 0 0 t 0 w +w 0 t t t t t t 0 w +w 0 0 0 0 0 0 0 0 w +w w w w w w w w w w |