Guest User

pos.lua

a guest
Aug 6th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.31 KB | None | 0 0
  1. local V = vector.new
  2. local R = function(v, r)
  3.     local f = r > 1 and -1 or 1
  4.     if r % 2 == 1 then
  5.         return V(-v.z * f, v.y, v.x * f)
  6.     end
  7.     return V(v.x * f, v.y, v.z * f)
  8. end
  9.  
  10.  
  11. moveUpdates = {
  12.     forward = {1, 0, 0, 0},
  13.     back = {-1, 0, 0, 0},
  14.     turnLeft = {0, 0, 0, -1},
  15.     turnRight = {0, 0, 0, 1},
  16.     up = {0, 1, 0, 0},
  17.     down = {0, -1, 0, 0}
  18. }
  19.  
  20. function savePos()
  21.     local h = fs.open("/.pos", "w")
  22.     h.write(textutils.serialize(turtle.pos))
  23.     h.close()
  24.     --print(unpack(turtle.pos))
  25. end
  26.  
  27. function loadPos()
  28.     local h = fs.open("/.pos", "r")
  29.     if h ~= nil then
  30.         turtle.pos = textutils.unserialize(h.readAll())
  31.         h.close()
  32.         term.setTextColor(0x100)
  33.         term.write("current position is: ")
  34.         term.setTextColor(0x2000)
  35.         print(turtle.pos[1], turtle.pos[2], turtle.pos[3], string.sub("ESWN", turtle.pos[4] + 1, turtle.pos[4] + 1))
  36.         term.setTextColor(0x100)
  37.         print("if incorrect, 'rm /.pos' and reboot")
  38.         term.setTextColor(1)
  39.     else
  40.         local p = {}
  41.         term.setTextColor(0x2000)
  42.         print("Please enter turtle coordinates")
  43.         term.setTextColor(1)
  44.         term.write("x: ")
  45.         p[1] = tonumber(read())
  46.         assert(p[1] ~= nil)
  47.         term.write("y: ")
  48.         p[2] = tonumber(read())
  49.         assert(p[2] ~= nil)
  50.         term.write("z: ")
  51.         p[3] = tonumber(read())
  52.         assert(p[3] ~= nil)
  53.         term.write("orientation (N/E/S/W): ")
  54.         local ori = string.upper(read())
  55.         if ori == "E" then
  56.             p[4] = 0
  57.         elseif ori == "S" then
  58.             p[4] = 1
  59.         elseif ori == "W" then
  60.             p[4] = 2
  61.         elseif ori == "N" then
  62.             p[4] = 3
  63.         end
  64.         assert(p[4] ~= nil)
  65.         turtle.pos  = p
  66.         savePos()
  67.     end
  68. end
  69.  
  70.  
  71. term.clear()
  72. term.setCursorPos(1, 1)
  73. loadPos()
  74.  
  75. for k, move in pairs(moveUpdates) do
  76.     local fn = turtle[k]
  77.     turtle[k] = function()
  78.         local oldPos = turtle.pos
  79.         local posV = V(unpack(oldPos)) + R(V(unpack(move)), oldPos[4])
  80.         turtle.pos = { posV.x, posV.y, posV.z, (oldPos[4] + 4 + move[4]) % 4 }
  81.         savePos()
  82.         local r = fn()
  83.         if not r then
  84.             turtle.pos = oldPos
  85.             savePos()
  86.         end
  87.         return r
  88.     end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment