From fa7f48bac1c8e87695598e66aadb7b198805b919 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 4 Apr 2024 00:40:21 +0100 Subject: launch cactus --- cactus.lua | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/cactus.lua b/cactus.lua index 0934d55..30603e3 100644 --- a/cactus.lua +++ b/cactus.lua @@ -37,6 +37,7 @@ local function moveby_y(delta) local fn = turtle.up if delta < 0 then fn = turtle.down delta = -delta end for i=1,delta do fn() end + POS.y = POS.y + delta end local function rotatev(v,nq) @@ -60,21 +61,33 @@ end local function turn_to_face(newfacing) if newfacing == FACING then return - elseif newfacing == -FACING then turtle.turnRight() turtle.turnRight() - elseif newfacing == rotatev(FACING,1) then turtle.turnRight() - elseif newfacing == rotatev(FACING,-1) then turtle.turnLeft() + elseif newfacing == -FACING then turn_right() turn_right() + elseif newfacing == rotatev(FACING,1) then turn_right() + elseif newfacing == rotatev(FACING,-1) then turn_left() else error("invalid facing "..tostring(newfacing),2) end end +local function move_linear(n) + local dir = turtle.forward + if n < 0 then dir = turtle.back end + for i = 1,math.abs(n) do dir() end + POS = POS + n*FACING +end + local function moveby_xz(dv) - if dv.x ~= 0 then - turn_to_face(vector.new(numutil.sign(dv.x), 0, 0)) - for i=1,math.abs(dv.x) do turtle.forward() end - end - if dv.z ~= 0 then - turn_to_face(vector.new(0, 0, numutil.sign(dv.z))) - for i=1,math.abs(dv.z) do turtle.forward() end + if FACING.x ~= 0 then + move_linear(dv.x) + if dv.z ~= 0 then + turn_to_face(0,0,numutil.sign(dv.z)) + move_linear(math.abs(dv.z)) + end + elseif FACING.z ~= 0 then + move_linear(dv.z) + if dv.x ~= 0 then + turn_to_face(numutil.sign(dv.x),0,0) + move_linear(math.abs(dv.x)) + end end end -- cgit v1.2.3