Advertisement
Susceptance

turtlelib

Sep 15th, 2022 (edited)
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.98 KB | None | 0 0
  1. computers = {"computercraft:turtle_normal", "computercraft:turtle_advanced", "computercraft:computer_normal", "computercraft:computer_advanced", "computercraft:wireless_modem_normal", "computercraft:wireless_modem_advanced"}
  2. turtles = {"computercraft:turtle_normal", "computercraft:turtle_advanced"}
  3. dirTable = {{0, 1, 2, -1}, {-1, 0, 1, 2}, {2, -1, 0, 1}, {1, 2, -1, 0}}
  4. -- Rotations to get from a rotation to another rotation
  5. -- 0 is minecraft south (+z)
  6. -- dirtable[2][3] gives signed clockwise rotations to direction 2 to 3
  7.  
  8. function IsComputer(success, data)
  9.     if success then
  10.         for _, computer in ipairs(computers) do
  11.             if computer == data.name then
  12.                 return true
  13.             end
  14.         end
  15.     end
  16.     return false
  17. end
  18.  
  19. function RequestRescue(pos)
  20.     helpMessage = "Turtle rescue needed X:"..pos["x"].." Y:"..pos["y"].." Z:"..pos["z"]
  21.     print(helpMessage)
  22.     rednet.broadcast(helpMessage)
  23. end
  24.  
  25. function IsTurtle(success, data)
  26.     if success then
  27.         for _, turtle in ipairs(turtles) do
  28.             if turtle == data.name then
  29.                 return true
  30.             end
  31.         end
  32.     end
  33.     return false
  34. end
  35.  
  36. -- Returns true if theres at least one fuel available
  37. function CheckFuel()
  38.     if turtle.getFuelLevel() > 0 then
  39.         return true
  40.     end
  41.  
  42.     return false
  43. end
  44.  
  45. function AvoidUp(pos)
  46.     pos = Forward(pos)
  47.     pos = Up(pos)
  48.     pos = Up(pos)
  49.     pos = Turn(2, pos)
  50.     pos = Forward(pos)
  51.     pos = Turn(2, pos)
  52. end
  53.  
  54. function AvoidDown(pos)
  55.     pos = Turn(2, pos)
  56.     pos = Forward(pos)
  57.     pos = Down(pos)
  58.     pos = Down(pos)
  59.     pos = Turn(2, pos)
  60.     pos = Forward(pos)
  61. end
  62.  
  63. function AvoidForward(pos)
  64.     pos = Turn(1, pos)
  65.     pos = Forward(pos)
  66.     pos = Turn(-1, pos)
  67.     pos = Forward(pos)
  68.     pos = Forward(pos)
  69.     pos = Turn(-1, pos)
  70.     pos = Forward(pos)
  71.     pos = Turn(1, pos)
  72. end
  73.  
  74. function Forward(pos)
  75.     if CheckFuel() == false then
  76.         return pos, false
  77.     end
  78.  
  79.     if IsComputer(turtle.inspect()) then
  80.         AvoidForward(pos)
  81.     end
  82.  
  83.     while turtle.forward() == false do
  84.         turtle.dig()
  85.     end
  86.     --Set position
  87.     if pos["rot"] == 0 then
  88.         pos["z"] = pos["z"] + 1
  89.     elseif pos["rot"] == 1 then
  90.         pos["x"] = pos["x"] - 1
  91.     elseif pos["rot"] == 2 then
  92.         pos["z"] = pos["z"] - 1
  93.     elseif pos["rot"] == 3 then
  94.         pos["x"] = pos["x"] + 1
  95.     end
  96.  
  97.     return pos, true
  98. end
  99.  
  100. function Up(pos)
  101.     if CheckFuel() == false then
  102.         return pos, false
  103.     end
  104.  
  105.     if IsComputer(turtle.inspectUp()) then
  106.         AvoidDown(pos)
  107.     end
  108.  
  109.     while turtle.up() == false do
  110.         turtle.digUp()
  111.     end
  112.     --Set position
  113.     pos["y"] = pos["y"] + 1
  114.     return pos, true
  115. end
  116.  
  117. function Down(pos)
  118.     if CheckFuel() == false then
  119.         return pos, false
  120.     end
  121.  
  122.     if IsComputer(turtle.inspectDown()) then
  123.         AvoidDown(pos)
  124.     end
  125.  
  126.     while turtle.down() == false do
  127.         turtle.digDown()
  128.     end
  129.     --Set position
  130.     pos["y"] = pos["y"] - 1
  131.     return pos, true
  132. end
  133.  
  134. function Turn(amount, pos)
  135.     for i = 1, math.abs(amount), 1 do
  136.         print(i)
  137.         if amount < 0 then
  138.             turtle.turnLeft()
  139.             pos["rot"] = pos["rot"] - 1
  140.         else
  141.             turtle.turnRight()
  142.             pos["rot"] = pos["rot"] + 1
  143.         end
  144.  
  145.         if pos["rot"] > 3 then
  146.             pos["rot"] = 0
  147.         end
  148.  
  149.         if pos["rot"] < 0 then
  150.             pos["rot"] = 3
  151.         end
  152.     end
  153.  
  154.     return pos
  155. end
  156.  
  157. function FaceDir(dir, pos)
  158.     print("facing"..dir)
  159.     delta = dirTable[pos["rot"] + 1][dir + 1]
  160.     --delta = dir - pos["rot"]
  161.     print(delta)
  162.     pos = Turn(delta, pos)
  163.  
  164.     return pos
  165. end
  166.  
  167. function GetPos(pos)
  168.     xA,yA,zA = gps.locate(2)
  169.  
  170.     if x == nil then
  171.         if pos == nil then
  172.             pos = {}
  173.             pos["rot"] = 0
  174.             pos["x"] = 0
  175.             pos["y"] = 0
  176.             pos["z"] = 0
  177.         end
  178.         return pos
  179.     end
  180.  
  181.     xB,yB,zB = gps.locate(2) --Used to compare pos to get rotation
  182.     if xA ~= xB then
  183.         if xA > xB then
  184.             pos["rot"] = 3
  185.         else
  186.             pos["rot"] = 1
  187.         end
  188.     end
  189.  
  190.     if zA ~= zB then
  191.         if zA > zB then
  192.             pos["rot"] = 0
  193.         else
  194.             pos["rot"] = 2
  195.         end
  196.     end
  197.  
  198.     pos["x"] = xB
  199.     pos["y"] = yB
  200.     pos["z"] = zB
  201.     return pos
  202. end
  203.  
  204. function ToWorldHeight(pos)
  205.     while pos["y"] < 319 do
  206.         success, pos = Up(pos)
  207.     end
  208.  
  209.     return pos
  210. end
  211.  
  212. function Goto(targetPos, currentPos)
  213.     print("Going to "..targetPos["x"].." "..targetPos["y"].." "..targetPos["z"])
  214.     print("from "..currentPos["x"].." "..currentPos["y"].." "..currentPos["z"])
  215.  
  216.     --pos = ToWorldHeight(currentPos)
  217.     pos = currentPos --Links these variables
  218.  
  219.     deltaX = targetPos["x"] - currentPos["x"]
  220.     deltaY = targetPos["y"] - currentPos["y"]
  221.     deltaZ = targetPos["z"] - currentPos["z"]
  222.  
  223.     print("deltaX"..deltaX)
  224.     print("deltaY"..deltaY)
  225.     print("deltaZ"..deltaZ)
  226.  
  227.     if deltaY ~= 0 then
  228.         if deltaY > 0 then
  229.             while currentPos["y"] ~= targetPos["y"] do
  230.                 pos = Up(pos)
  231.  
  232.                 if targetPos["y"] > currentPos["y"] then
  233.                     RequestRescue(currentPos)
  234.                     error("Overshot")
  235.                 end
  236.             end
  237.         else
  238.             while currentPos["y"] ~= targetPos["y"] do
  239.                 pos = Down(pos)
  240.  
  241.                 if targetPos["y"] < currentPos["y"] then
  242.                     RequestRescue(currentPos)
  243.                     error("Overshot")
  244.                 end
  245.             end
  246.         end
  247.     end
  248.  
  249.     if deltaX ~= 0 then
  250.         if deltaX > 0 then
  251.             pos = FaceDir(3, pos)
  252.         else
  253.             pos = FaceDir(1, pos)
  254.         end
  255.  
  256.         while currentPos["x"] ~= targetPos["x"] do
  257.             pos = Forward(pos)
  258.            
  259.             if deltaX < 0 then
  260.                 if targetPos["x"] > currentPos["x"] then
  261.                     RequestRescue(currentPos)
  262.                     error("Overshot")
  263.                 end
  264.             else
  265.                 if targetPos["x"] < currentPos["x"] then
  266.                     RequestRescue(currentPos)
  267.                     error("Overshot")
  268.                 end
  269.             end
  270.         end
  271.     end
  272.  
  273.     if deltaZ ~= 0 then
  274.         if deltaZ > 0 then
  275.             pos = FaceDir(0, pos)
  276.         else
  277.             pos = FaceDir(2, pos)
  278.         end
  279.  
  280.         while currentPos["z"] ~= targetPos["z"] do
  281.             pos = Forward(pos)
  282.  
  283.             if deltaZ < 0 then
  284.                 if targetPos["z"] > currentPos["z"] then
  285.                     RequestRescue(currentPos)
  286.                     error("Overshot")
  287.                 end
  288.             else
  289.                 if targetPos["z"] < currentPos["z"] then
  290.                     RequestRescue(currentPos)
  291.                     error("Overshot")
  292.                 end
  293.             end
  294.         end
  295.     end
  296.    
  297.     return pos
  298. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement