t8rt0t

sps.lua

Jun 29th, 2024 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.61 KB | None | 0 0
  1. --Safe Positioning System
  2. --Inspired by https://github.com/zyxkad/cc/blob/master/turtle/lps.lua
  3.  
  4. --zyxkad's system uses files to store the turtle position
  5. --Which on world crashes, breaks because cc files are stored at different times than the world nbt
  6. --This system uses computer labels to store the turtle position
  7. --Computer labels modify the nbt of the block, meaning that the position on the label is saved at the same time as the turtles actual position
  8. --This is also extremely useful for glitched duplicate id turtles, as they share files, but not computer ids as computer ids are specific to the block
  9. --The main downside is that you cant use computer labels at the same time as this
  10.  
  11. --Use turtle.native instead of turtle to bypass the positioning
  12.  
  13.  
  14.  
  15. --First 2 bytes are magic numbers (to check if label valid)
  16. --Next 4 bytes x coordinate (signed integer)
  17. --Next 2 bytes y coordinate (signed short)
  18. --Next 4 bytes z coordinate (signed integer)
  19. --Next byte the rotatation (0-3)
  20. --Next byte flags
  21. --  bit 0[1]: is fuel level odd (true) or even (false)? [These two bits are used to determine turtles position if server crashed while the turtle was moving]
  22. --  bit 1-2[2-3]: last move (0 for forward,1 for backwards,2 for up, 3 for down
  23. --  bit 3-7[4-8]: unused
  24.  
  25. --First 2 chars are the magic numbers
  26. --Next 8 chars are the x coordinate as signed integer (represented in hex)
  27. --Next 4 chars are the y coordinate as signed short (represented in hex)
  28. --Next 8 chars are the z coordinate as signed integer (represented in hex)
  29. --Next char is the rotation (0-3)
  30. --Next char is the flags as hex
  31. --  bit 0: is fuel level odd (true) or even (false)? [Used to determine if the turtle moved during a crash]
  32. --  bit 1-2: last move (0 for forward,1 for backwards,2 for up, 3 for down
  33. --  bit 3: unused
  34.  
  35. --Override setComputerLabel to prevent people from modifying the position
  36. local setComputerLabel = os.setComputerLabel
  37. os.setComputerLabel = function(label,override)
  38.     if override == true then
  39.         return setComputerLabel(label,true)
  40.     else
  41.         error("It is dangerous to modify the computer label! Use `os.setComputerLabel("..tostring(label)..",true)` to override this error")
  42.     end
  43. end
  44.  
  45. --constants
  46. local magicNumbers = string.char(167) .. "k"
  47. local hex = "%x"
  48. local regex = "^"..magicNumbers.."("..hex:rep(8)..")("..hex:rep(4)..")("..hex:rep(8)..")(%d)(%x)"
  49. local completion = require("cc.completion")
  50. local expect = require("cc.expect").expect
  51. local facingTab = {
  52.     [0] = "-z",
  53.     [1] = "+x",
  54.     [2] = "+z",
  55.     [3] = "-x",
  56.     ["-z"] = 0,
  57.     ["+x"] = 1,
  58.     ["+z"] = 2,
  59.     ["-x"] = 3,
  60.     north = 0,
  61.     east = 1,
  62.     south = 2,
  63.     west = 3
  64. } --For compat with zyxkad's system
  65.  
  66.  
  67. local function numToStr(num,len)
  68.     num = num + 2^(len*4 - 1)
  69.     local str = hex:format(num)
  70.     str = string.rep("0",len-#str) .. str
  71.     return str
  72. end
  73. local function strToNum(str)
  74.     return tonumber(str,16) - 2^(#str*4 - 1)
  75. end
  76.  
  77. local function hasbit(x,p)
  78.     return x%(p+p)>=p
  79. end
  80. local function setbit(x,p)
  81.     return hasbit(x,p) and x or x+p
  82. end
  83.  
  84. local function getLabel(pos)
  85.     return magicNumbers ..numToStr(pos.x,8)..numToStr(pos.y,4)..numToStr(pos.z,8)..pos.rot .. hex:format(pos.flags)
  86. end
  87.  
  88. local function promptRot()
  89.     local rot = {north=0,east=1,south=2,west=3}
  90.     term.clear()
  91.     term.setCursorPos(1,1)
  92.     print("Please input the starting rotation of the turtle (north,south,east,west)")
  93.     local input = read(nil,nil,function(txt) return completion.choice(txt,{"north","south","east","west"}) end)
  94.     if not rot[input] then error("Invalid starting rotation") end
  95.     return rot[input]
  96. end
  97. local function movePre(pos,dir)
  98.     pos.flags = bit.bor(bit.band(pos.flags,8),dir*2+turtle.getFuelLevel()%2) --update flags to show which direction you're going + fuel level
  99.     setComputerLabel(getLabel(pos),true)
  100. end
  101. local function movePost(pos,dir)
  102.     pos.flags = setbit(pos.flags,turtle.getFuelLevel()%2) --update flags to show new fuel level
  103.     if dir <= 1 then --update the turtle position
  104.         local rot = (pos.rot + dir*2)%4
  105.         if rot == 0 then
  106.             pos.z = pos.z - 1
  107.         elseif rot == 1 then
  108.             pos.x = pos.x + 1
  109.         elseif rot == 2 then
  110.             pos.z = pos.z + 1
  111.         else
  112.             pos.x = pos.x - 1
  113.         end
  114.     elseif dir == 2 then
  115.         pos.y = pos.y + 1
  116.     else
  117.         pos.y = pos.y - 1
  118.     end
  119.     setComputerLabel(getLabel(pos),true)
  120. end
  121.  
  122.  
  123. local SPS = {}
  124. pos = nil
  125.  
  126. function SPS.init(facing,x,y,z)
  127.     if facing and not facingTab[facing] then error("Invalid facing, must be +/-x, +/-z, or north, south, east, or west") end
  128.     pos = {
  129.         x = tonumber(x) or 0,
  130.         y = tonumber(y) or 0,
  131.         z = tonumber(z) or 0,
  132.         rot = facing and facingTab[facing],
  133.         flags = turtle.getFuelLevel()%2
  134.     }
  135.     local label = os.getComputerLabel()
  136.     if label then
  137.         local x,y,z,rot,flags = label:match(regex)
  138.         if not x then
  139.             local file = fs.open("label.txt","w+")
  140.             file.write(label)
  141.             file.close()
  142.             -- pos.rot = promptRot()
  143.             if not pos.rot then return false end
  144.             setComputerLabel(getLabel(pos),true)
  145.         else
  146.             pos.x,pos.y,pos.z,pos.rot = strToNum(x),strToNum(y),strToNum(z),tonumber(rot)
  147.             flags = tonumber(flags,16)
  148.             if flags%2 ~= pos.flags%2 then --turtle fuel level is not consistent, means the turtle moved, but movePost was never called before the server went down.
  149.                 pos.flags = flags
  150.                 movePost(pos,math.floor(flags/2)%4)
  151.             end
  152.         end
  153.     else
  154.         -- pos.rot = promptRot()
  155.         if not pos.rot then return false end --Changing from my prompt function to returning false if there is no facing provided, for compatibility with the old lps version
  156.         setComputerLabel(getLabel(pos),true)
  157.     end
  158.    
  159.     turtle.forward = function()
  160.         movePre(pos,0)
  161.         if turtle.native.forward() then
  162.             movePost(pos,0)
  163.             return true
  164.         end
  165.         return false
  166.     end
  167.     turtle.back = function()
  168.         movePre(pos,1)
  169.         if turtle.native.back() then
  170.             movePost(pos,1)
  171.             return true
  172.         end
  173.         return false
  174.     end
  175.     turtle.up = function()
  176.         movePre(pos,2)
  177.         if turtle.native.up() then
  178.             movePost(pos,2)
  179.             return true
  180.         end
  181.         return false
  182.     end
  183.     turtle.down = function()
  184.         movePre(pos,3)
  185.         if turtle.native.down() then
  186.             movePost(pos,3)
  187.             return true
  188.         end
  189.         return false
  190.     end
  191.     turtle.turnLeft = function()
  192.         pos.rot = (pos.rot - 1)%4
  193.         setComputerLabel(getLabel(pos),true)
  194.         return turtle.native.turnLeft()
  195.     end
  196.     turtle.turnRight = function()
  197.         pos.rot = (pos.rot + 1)%4
  198.         setComputerLabel(getLabel(pos),true)
  199.         return turtle.native.turnRight()
  200.     end
  201.     return true
  202. end
  203. function SPS.locate()
  204.     return pos.x,pos.y,pos.z
  205. end
  206. function SPS.facing()
  207.     return facingTab[pos.rot],pos.rot --first one only exists for compat, second one is more useful imo
  208. end
  209. function SPS.version() --Exists only for compat, i dont plan on using this ever
  210.     return 1,"This is deprecated for sps and only exists for compat!"
  211. end
  212. function SPS.updatePosition(x,y,z,rot)
  213.     expect(1,x,"number")
  214.     expect(2,y,"number")
  215.     expect(3,z,"number")
  216.     expect(4,rot,"number","nil")
  217.     pos.x,pos.y,pos.z = x,y,z
  218.     if rot then
  219.         pos.rot = math.floor(rot)%4
  220.     end
  221.     setComputerLabel(getLabel(pos),true)
  222. end
  223. return SPS
Advertisement
Add Comment
Please, Sign In to add comment