Doodoofard

computercraft test script

Apr 26th, 2024 (edited)
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.54 KB | None | 0 0
  1. -- computercraft
  2.  
  3. -- init
  4. turtle.refuel()
  5. rednet.open("left")
  6.  
  7. -- utility functions
  8.  
  9. function determineDirection()
  10.     xc, yc, zc = gps.locate()
  11.     turtle.dig()
  12.     turtle.forward()
  13.     xc2, yc2, zc2 = gps.locate()
  14.     turtle.back()
  15.     -- based on which coordinate changed we can calculate which direction the turtle is facing
  16.     if xc - xc2 == -1 then
  17.         return "east"
  18.     end
  19.     if xc - xc2 == 1 then
  20.         return "west"
  21.     end
  22.     if zc - zc2 == -1 then
  23.         return "south"
  24.     end
  25.     if zc - zc2 == 1 then
  26.         return "north"
  27.     end
  28.     return "none"
  29. end
  30.  
  31. function turtle.turnAround()
  32.     turtle.turnLeft()
  33.     turtle.turnLeft()
  34. end
  35.  
  36. function sgn(num)
  37.     if num > 0 then
  38.         return 1
  39.     end
  40.     if num < 0 then
  41.         return -1
  42.     end
  43.     return 0
  44. end
  45.  
  46. function ensureFacingNorth()
  47.     i = 0
  48.     ::tryAgain::
  49.     direction = determineDirection()
  50.     -- make turtle always face north for movements
  51.     if direction == "east" then
  52.         turtle.turnLeft()
  53.     elseif direction == "west" then
  54.         turtle.turnRight()
  55.     elseif direction == "south" then
  56.         turtle.turnAround()
  57.     elseif direction ~= "north" then
  58.         if i ~= 4 then
  59.             turtle.turnRight()
  60.             i = i + 1
  61.             goto tryAgain
  62.         end
  63.     end
  64. end
  65.  
  66. function mineAllAroundEmergency()
  67.     for i=1,4 do
  68.         turtle.turnLeft()
  69.         turtle.dig()
  70.     end
  71.     turtle.digUp()
  72.     turtle.digDown()
  73. end
  74.  
  75. function gotoPos(x, y, z, try)
  76.     -- get turtle pos from gps
  77.     xpos, ypos, zpos = gps.locate()
  78.     ensureFacingNorth()
  79.     turtle.turnRight()
  80.     ::toPos::
  81.     for i=1, math.abs(x-xpos) do
  82.         if sgn(x-xpos) == 0 then
  83.             break
  84.         elseif sgn(x-xpos) == 1 then
  85.             turtle.dig()
  86.             turtle.forward()
  87.         elseif sgn(x-xpos) == -1 then
  88.             turtle.turnAround()
  89.             turtle.dig()
  90.             turtle.turnAround()
  91.             turtle.back()
  92.         end
  93.     end
  94.     turtle.turnLeft()
  95.     if try >= 2 then
  96.         mineAllAroundEmergency() --attempt to get unstuck
  97.     end
  98.     if try >= 2 and try <= 10 then
  99.         turtle.up()
  100.     end
  101.     for i=1, math.abs(z-zpos) do
  102.         if sgn(z-zpos) == 0 then
  103.             break
  104.         elseif sgn(z-zpos) == 1 then
  105.             turtle.dig()
  106.             turtle.forward()
  107.         elseif sgn(z-zpos) == -1 then
  108.             turtle.turnAround()
  109.             turtle.dig()
  110.             turtle.turnAround()
  111.             turtle.back()
  112.         end
  113.     end
  114.     for i=1, math.abs(y-ypos) do
  115.         if sgn(y-ypos) == 0 then
  116.             break
  117.         elseif sgn(y-ypos) == 1 then
  118.             turtle.digUp()
  119.             turtle.up()
  120.         elseif sgn(y-ypos) == -1 then
  121.             turtle.digDown()
  122.             turtle.down()
  123.         end
  124.     end
  125.     -- get turtle pos from gps again
  126.     xpos, ypos, zpos = gps.locate()
  127.     xdiff = x - xpos
  128.     ydiff = y - ypos
  129.     zdiff = z - zpos
  130.     -- if we aren't at target point then go back to trying to get there until we get there
  131.     if not (xdiff == 0 and ydiff == 0 and zdiff == 0) then
  132.         print("We are not at target point "..x.." "..y.." "..z..". Why?!?!")
  133.         ensureFacingNorth()
  134.         gotoPos(x, y, z, try + 1)
  135.     end
  136.     -- if we are at the target point then finish and end function
  137. end
  138.  
  139. function splitBySpace(string)
  140.     temp = {}
  141.     i2 = 1
  142.     for i in string.gmatch(string, "%S+") do
  143.         temp[i2] = i
  144.         i2 = i2 + 1
  145.     end
  146.     return temp
  147. end
  148.  
  149. function faceDirectionAssumingNorth(directiona)
  150.     if directiona == "east" then
  151.         turtle.turnLeft()
  152.     elseif directiona == "west" then
  153.         turtle.turnRight()
  154.     elseif directiona == "south" then
  155.         turtle.turnAround()
  156.     elseif directiona ~= "north" then
  157.         error("Invalid direction")
  158.     end
  159. end
  160.  
  161. function depositAndRefuel(homex, homey, homez, minestartx, minestarty, minestartz, facing)
  162.     curx, cury, curz = gps.locate()
  163.     gotoPos(minestartx, homey, minestartz, 1)
  164.     -- we are now on the surface (or should be)
  165.     gotoPos(homex, homey, homez, 1)
  166.     -- we are also guaranteed to be facing north after movement, so deposit in chest south of turtle
  167.     turtle.turnAround()
  168.     -- deposit everything in the chest
  169.     for i=1, 16 do
  170.         turtle.select(i)
  171.         turtle.drop()
  172.     end
  173.     turtle.turnAround()
  174.     -- we expect to have a fuel chest above the home point, so get a stack from the fuel chest
  175.     turtle.suckUp()
  176.     turtle.refuel()
  177.     -- return to what we were doing
  178.     gotoPos(minestartx, homey, minestartz, 1)
  179.     -- travel down the mineshaft
  180.     gotoPos(minestartx, minestarty, minestartz, 1)
  181.     -- we are now back in the layer, go to where we were before this function was called
  182.     gotoPos(curx, cury, curz, 1)
  183.     -- face where we were before this function was called
  184.     faceDirectionAssumingNorth(facing)
  185. end
  186. function goHome(homex, homey, homez, minestartx, minestarty, minestartz)
  187.     gotoPos(minestartx, homey, minestartz, 1)
  188.     gotoPos(homex, homey, homez, 1)
  189.     turtle.turnAround()
  190. end
  191.  
  192. function isInventoryFull()
  193.     full = 0
  194.     for i=1, 16 do
  195.         turtle.select(i)
  196.         if turtle.getItemCount() > 0 then
  197.             full = full + 1
  198.         end
  199.     end
  200.     turtle.select(1)
  201.     return full == 16
  202. end
  203.  
  204. -- main
  205.  
  206. homex, homey, homez = gps.locate()
  207. while true do
  208.     -- standby to recieve command
  209.     sender, message = rednet.receive()
  210.     if string.find(message, "GOTO") ~= nil then
  211.         message = splitBySpace(message)
  212.         rednet.send(sender, "ACK")
  213.         gotoPos(tonumber(message[2]), tonumber(message[3]), tonumber(message[4]), 1)
  214.     end
  215.     if string.find(message, "MINE") ~= nil then
  216.         message = splitBySpace(message)
  217.         rednet.send(sender, "ACK") -- since i don't get the ACK from the turtle, the bug must be occuring before here
  218.         -- message format: "MINE -4220 64 3433 -4110 50 3333"
  219.         x1 = message[2]
  220.         y1 = message[3]
  221.         z1 = message[4]
  222.         x2 = message[5]
  223.         y2 = message[6]
  224.         z2 = message[7]
  225.         startx, starty, startz = gps.locate()
  226.         -- depositAndRefuel(homex, homey, homez, startx, starty, startz, "north")
  227.         -- everything in y layers
  228.         for y=1, math.abs(y1-y2) do
  229.             gotoPos(math.min(x1, x2), math.min(y1, y2) + y - 1, math.max(z1, z2), 1)
  230.             switch = true
  231.             facing = "north"
  232.             -- everything in x layer
  233.             for x=1, math.abs(x1-x2) + 1 do
  234.                 -- everything in z line
  235.                 for z=1, math.abs(z1-z2) do
  236.                     turtle.dig()
  237.                     turtle.forward()
  238.                 end
  239.                 if switch then
  240.                     turtle.turnRight()
  241.                     turtle.dig()
  242.                     turtle.forward()
  243.                     turtle.turnRight()
  244.                     facing = "south"
  245.                 else
  246.                     turtle.turnLeft()
  247.                     turtle.dig()
  248.                     turtle.forward()
  249.                     turtle.turnLeft()
  250.                     facing = "north"
  251.                 end
  252.                 if isInventoryFull() or turtle.getFuelLevel() <= 200 then
  253.                     depositAndRefuel(homex, homey, homez, math.min(x1, x2), math.min(y1, y2) + math.abs(y1-y2) - 1, math.max(z1, z2), facing)
  254.                 end
  255.                 switch = not switch
  256.             end
  257.             turtle.dig()
  258.         end
  259.         goHome(homex, homey, homez, math.min(x1, x2), math.min(y1, y2) + math.abs(y1-y2) - 1, math.max(z1, z2))
  260.         turtle.turnAround()
  261.     end
  262. end
Add Comment
Please, Sign In to add comment