From c8e5f1e67788976ff65611fbf77b3be13bffd79a Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 4 Apr 2024 00:33:07 +0100 Subject: launch cactus --- cactus.dat | 1 + cactus.lua | 50 +++++++++++++++++++++++++++++++++++++++----------- numutil.lua | 11 +++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 cactus.dat create mode 100644 numutil.lua diff --git a/cactus.dat b/cactus.dat new file mode 100644 index 0000000..c00c3d5 --- /dev/null +++ b/cactus.dat @@ -0,0 +1 @@ +dep numutil diff --git a/cactus.lua b/cactus.lua index 6613df5..982d987 100644 --- a/cactus.lua +++ b/cactus.lua @@ -1,3 +1,5 @@ +local numutil = require 'numutil' + local function ensure_equipped(ptype, item) local cur = peripheral.getType"left" print(cur) @@ -15,17 +17,21 @@ end local function ensure_modem() return ensure_equipped("modem","computercraft:wireless_modem_normal") end local function ensure_scanner() return error("aaa") end -assert(ensure_modem()) +local function turte_pos_init() + assert(ensure_modem()) + + local POS = vector.new(gps.locate()) -local POS = vector.new(gps.locate()) -local function get_facing() while turtle.detect() do turtle.turnRight() end turtle.forward() local newpos = vector.new(gps.locate()) turtle.back() - return newpos - POS + local FACING = newpos - POS + + return POS, FACING end -local FACING = get_facing() + +local POS, FACING = turte_pos_init() local function moveby_y(delta) local fn = turtle.up @@ -34,7 +40,7 @@ local function moveby_y(delta) end local function rotatev(v,nq) - -- rotates v by n quarters + -- rotates v by n quarters, clockwise (rightwards) nq = nq%4 if nq == 0 then return vector.new( v.x, v.y, v.z) elseif nq == 1 then return vector.new(-v.z, v.y, v.x) @@ -43,15 +49,37 @@ local function rotatev(v,nq) end end -local v = vector.new(1,0,2) -for i=0,4 do print(i,rotatev(v,i)) end - -local function moveby_xz(dv) - -- y component of dv is ignored +local function turn_right() + FACING = rotatev(FACING,1) + turtle.turnRight() +end +local function turn_left() + FACING = rotatev(FACING,-1) + turtle.turnLeft() +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() + else error("invalid facing "..tostring(newfacing)) + end end +local function moveby_xz(dv) + turn_to_face(vector.new(numutil.sign(dv.x), 0, 0)) + for i=1,math.abs(dv.x) do turtle.forward() end + turn_to_face(vector.new(0, 0, numutil.sign(dv.z))) + for i=1,math.abs(dv.z) do turtle.forward() end +end +local function moveby(delta) + moveby_y(delta.y) + moveby_xz(delta) +end local datum = vector.new(-18, 128, -1837) +local delta = datum - POS +moveby(delta) diff --git a/numutil.lua b/numutil.lua new file mode 100644 index 0000000..ce212c4 --- /dev/null +++ b/numutil.lua @@ -0,0 +1,11 @@ +local function sign(x) + if x > 0 then return 1 + elseif x < 0 then return -1 + else return 0 + end +end + + +return { + sign = sign, +} -- cgit v1.2.3