From 0eb5a7f20099660cba13fae0095249dc9269310a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Tue, 18 Aug 2020 19:32:52 +0200 Subject: [PATCH] Mine: write movement functions --- turtles/mine.lua | 61 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/turtles/mine.lua b/turtles/mine.lua index 6c30af9..f02518f 100644 --- a/turtles/mine.lua +++ b/turtles/mine.lua @@ -51,15 +51,44 @@ function turn_rel(dir) turn_abs((facing + dir) % 4) end +-- Updates the turtle position for a move in the given direction +function _count_move_dir(dir) + dir = dir % 4 + if dir == 0 then depth = depth + 1 + elseif dir == 1 then side = side + 1 + elseif dir == 2 then depth = depth - 1 + elseif dir == 3 then side = side - 1 + end +end + +function move_forward() + rc, desc = turtle.forward() + if not rc then abort("Move: "..desc) end + _count_move_dir(facing) +end +function move_back() + rc, desc = turtle.back() + if not rc then abort("Move: "..desc) end + _count_move_dir(facing + 2) +end +function move_up() + rc, desc = turtle.up() + if not rc then abort("Move: "..desc) end + height = height + 1 +end +function move_down() + rc, desc = turtle.down() + if not rc then abort("Move: "..desc) end + height = height - 1 +end + function go_to_depth(dd) turn_abs(0) while depth > dd do - turtle.back() - depth = depth - 1 + move_back() end while depth < dd do - turtle.forward() - depth = depth + 1 + move_forward() end end @@ -70,12 +99,10 @@ function go_to(dh, dd, ds) if height ~= dh then while height > dh do - turtle.down() - height = height - 1 + move_down() end while height < dh do - turtle.up() - height = height + 1 + move_up() end end @@ -83,12 +110,10 @@ function go_to(dh, dd, ds) turn_abs(1) while side > ds do - turtle.back() - side = side - 1 + move_back() end while side < ds do - turtle.forward() - side = side + 1 + move_forward() end end @@ -149,8 +174,7 @@ function find_next_shaft() if turtle.detectUp() then turtle.digUp() end - if not turtle.up() then abort("Misshaped lobby") end - height = height + 1 + move_up() end turn_abs(1) @@ -158,8 +182,7 @@ function find_next_shaft() if turtle.detect() then turtle.dig() end - if not turtle.forward() then abort("Misshaped lobby") end - side = side + 1 + move_forward() end turn_abs(0) @@ -174,8 +197,7 @@ function find_next_shaft() if turtle.detectDown() then turtle.digDown() end - if not turtle.down() then abort("Misshaped lobby") end - height = height - 1 + move_down() end turn_abs(1) @@ -183,8 +205,7 @@ function find_next_shaft() if turtle.detect() then turtle.dig() end - if not turtle.forward() then abort("Misshaped lobby") end - side = side + 1 + move_forward() end turn_abs(0) end