imposiblaa

turtle client

Dec 17th, 2020 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.31 KB | None | 0 0
  1. local supplies = false
  2. local data = {}
  3. gps.CHANNEL_GPS = 5033
  4.  
  5. --modem things
  6. local modem = peripheral.wrap("left")
  7. modem.open(5038)
  8.  
  9. local currentDirection = 1
  10. --gets and stores the direction of the turtle
  11. function direction(change, strict)
  12.     if change == "left" then
  13.         if currentDirection == 1 then
  14.             currentDirection = 4
  15.         else
  16.             currentDirection = currentDirection - 1
  17.         end
  18.         return currentDirection
  19.     elseif change == "right" then
  20.         if currentDirection == 4 then
  21.             currentDirection = 1
  22.         else
  23.             currentDirection = currentDirection + 1
  24.         end
  25.         return currentDirection
  26.     else
  27.         if strict then
  28.             print("the function \"direction\" recieved and invalid direction while sctrict was set to true")
  29.         else
  30.             return currentDirection
  31.         end
  32.     end
  33. end
  34.  
  35. --turns the robot
  36. function turn(idirection)
  37.     if idirection == "left" then
  38.         turtle.turnLeft()
  39.     elseif idirection == "right" then
  40.         turtle.turnRight()
  41.     end
  42.     return direction(idirection,  true)
  43. end
  44. -- moves and mines at the same time to ensure there is no block where it is trying to move
  45. function moveMine(idirection)
  46.     local waitingCount = 0
  47.     if idirection == "up" then
  48.         location = {gps.locate()}
  49.         moving = true
  50.         while moving do
  51.             bool, block = turtle.inspectUp()
  52.             if block.name ~= "computercraft:turtle_expanded" then
  53.                 turtle.digUp()
  54.             else
  55.                 waitingCount = waitingCount + 1
  56.                 sleep(1)
  57.             end
  58.             turtle.up()
  59.             local newLocation = {gps.locate()}
  60.             if newLocation ~= location then
  61.                 moving = false
  62.             end
  63.             if waitingCount > 5 then
  64.                 turtle.dig()
  65.                 turtle.forward()
  66.                 return
  67.             end
  68.         end
  69.     elseif idirection == "forward" then
  70.         location = {gps.locate()}
  71.         moving = true
  72.         while moving do
  73.             bool, block = turtle.inspect()
  74.             if block.name ~= "computercraft:turtle_expanded" then
  75.                 turtle.dig()
  76.             else
  77.                 waitingCount = waitingCount + 1
  78.                 sleep(1)
  79.             end
  80.             turtle.forward()
  81.             local newLocation = {gps.locate()}
  82.             if newLocation ~= location then
  83.                 moving = false
  84.             end
  85.             if waitingCount > 5 then
  86.                 turtle.digDown()
  87.                 turtle.down()
  88.                 turtle.digDown()
  89.                 turtle.down()
  90.                 sleep(3)
  91.                 return
  92.             end
  93.         end
  94.     elseif idirection == "down" then
  95.         location = {gps.locate()}
  96.         moving = true
  97.         while moving do
  98.             bool, block = turtle.inspectDown()
  99.             if block.name ~= "computercraft:turtle_expanded" then
  100.                 turtle.digDown()
  101.             else
  102.                 waitingCount = waitingCount + 1
  103.             end
  104.             turtle.down()
  105.             local newLocation = {gps.locate()}
  106.             if newLocation ~= location then
  107.                 moving = false
  108.             end
  109.             if waitingCount > 5 then
  110.                 turtle.dig()
  111.                 turtle.forward()
  112.                 return
  113.             end
  114.         end
  115.     else
  116.         print("function \"moveMine\" recieved invalid direction")
  117.     end
  118. end
  119.  
  120. -- goes to a coordinate position
  121. function turtleGoto(x, y, z, reverse, gettingValues)
  122.     if gettingValues then
  123.         location = {gps.locate()}
  124.         for part = 1, #location do
  125.             location[part] = math.floor(location[part]+0.5)
  126.         end
  127.         distances = {x - location[1], y - location[2], z - location[3]}
  128.         return distances
  129.     end
  130.     pathing = true
  131.     if reverse then
  132.         while pathing do
  133.             location = {gps.locate()}
  134.             for part = 1, #location do
  135.                 location[part] = math.floor(location[part]+0.5)
  136.             end
  137.             distances = {x - location[1], y - location[2], z - location[3]}
  138.             print(textutils.serialize(distances))
  139.             if distances[3] > 0  then
  140.                 while direction() ~= 3 do
  141.                     turn("left")
  142.                 end
  143.                 moveMine("forward")
  144.             elseif distances[3] < 0 then
  145.                 while direction() ~= 1 do
  146.                     turn("left")
  147.                 end
  148.                 moveMine("forward")
  149.             elseif distances[1] > 0 then
  150.                 while direction() ~= 2 do
  151.                     turn("right")
  152.                 end
  153.                 moveMine("forward")
  154.             elseif distances[1] < 0 then
  155.                 while direction() ~= 4 do
  156.                     turn("left")
  157.                 end
  158.                 moveMine("forward")
  159.             elseif distances[2] > 0 then
  160.                 moveMine("up")
  161.             elseif distances[2] < 0 then
  162.                 moveMine("down")
  163.             else
  164.                 print("at location")
  165.                 pathing = false
  166.                 return
  167.             end
  168.         end
  169.     else
  170.         while pathing do
  171.             location = {gps.locate()}
  172.             for part = 1, #location do
  173.                 location[part] = math.floor(location[part]+0.5)
  174.             end
  175.             distances = {x - location[1], y - location[2], z - location[3]}
  176.             print(textutils.serialize(distances))
  177.             if distances[2] > 0 then
  178.                 moveMine("up")
  179.             elseif distances[2] < 0 then
  180.                 moveMine("down")
  181.             elseif distances[1] > 0 then
  182.                 while direction() ~= 2 do
  183.                     turn("right")
  184.                 end
  185.                 moveMine("forward")
  186.             elseif distances[1] < 0 then
  187.                 while direction() ~= 4 do
  188.                     turn("left")
  189.                 end
  190.                 moveMine("forward")
  191.             elseif distances[3] > 0  then
  192.                 while direction() ~= 3 do
  193.                     turn("left")
  194.                 end
  195.                 moveMine("forward")
  196.             elseif distances[3] < 0 then
  197.                 while direction() ~= 1 do
  198.                     turn("left")
  199.                 end
  200.                 moveMine("forward")
  201.             else
  202.                 print("at location")
  203.                 pathing = false
  204.                 return
  205.             end
  206.         end
  207.     end
  208. end
  209.  
  210.  
  211. -- mines the block in front if it and the blocks above and below that block
  212. function mineForward()
  213.     turtle.dig()
  214.     turtle.dig()
  215.     turtle.dig()
  216.     turtle.dig()
  217.     turtle.dig()
  218.     turtle.dig()
  219.     turtle.dig()
  220.     turtle.dig()
  221.     turtle.dig()
  222.     turtle.dig()
  223.     turtle.dig()
  224.     turtle.dig()
  225.     turtle.dig()
  226.     moveMine("forward")
  227.     turtle.digDown()
  228.     turtle.digUp()
  229.     return
  230. end
  231.  
  232. -- will drop all cobblestone in inventory
  233. function dropCobble()
  234.     for s = 1,16 do
  235.         turtle.select(s)
  236.         local block = turtle.getItemDetail()
  237.         if block then
  238.             if block.name == "minecraft:cobblestone" then
  239.                 turtle.drop(64)              
  240.             elseif block.name == "minecraft:stone" then
  241.                 turtle.drop(64)  
  242.             elseif block.name == "minecraft:gravel" then
  243.                 turtle.drop(64)  
  244.             elseif block.name == "minecraft:dirt" then
  245.                 turtle.drop(64)  
  246.             elseif block.name == "chisel:basalt2" then
  247.                 turtle.drop(64)  
  248.             end        
  249.         end
  250.     end
  251.     return
  252. end
  253.  
  254. -- moves using a sequece of commands in an array
  255. function path(givenPath, info)
  256.     print(textutils.serialize(givenPath))
  257.     for step = 1, #givenPath do
  258.         location = {gps.locate()}
  259.         if givenPath[step] == "forward" then
  260.             moveMine("forward")
  261.         elseif givenPath[step] == "backward" then
  262.             moveMinw("backward")
  263.         elseif givenPath[step] == "left" then
  264.             turtle.turnLeft()
  265.         elseif givenPath[step] == "right" then
  266.             turtle.turnRight()
  267.         elseif givenPath[step] == "up" then
  268.             moveMine("up")
  269.         elseif givenPath[step] == "down" then
  270.             moveMine("down")
  271.         elseif givenPath[step] == "fuel" then
  272.             journey = turtleGoto(info.destination.x, info.destination.y, info.destination.z, false, true)
  273.             turtle.suckUp(getCoalCount(journey[1], journey[2], journey[3]))
  274.             turtle.refuel()
  275.         elseif givenPath[step] == "supply" then
  276.             turtle.suckUp(1)
  277.         elseif givenPath[step] == "drop-off" then
  278.             for s = 1,16 do
  279.                 turtle.select(s)
  280.                 turtle.dropUp(64)
  281.             end
  282.         elseif givenPath[step] == "north" then
  283.             while direction() ~= 1 do
  284.                 turn("left")
  285.             end
  286.         elseif givenPath[step] == "east" then
  287.             while direction() ~= 2 do
  288.                 turn("left")
  289.             end
  290.         elseif givenPath[step] == "south" then
  291.             while direction() ~= 3 do
  292.                 turn("left")
  293.             end
  294.         elseif givenPath[step] == "west" then
  295.             while direction() ~= 4 do
  296.                 turn("left")
  297.             end
  298.         elseif givenPath[step] == "goto" then
  299.             step = step + 1
  300.             turtleGoto(givenPath[step] + info.parentLocation[1], givenPath[step+1] + info.parentLocation[2], givenPath[step+2] + info.parentLocation[3])
  301.             step = step + 2
  302.         end
  303.     end
  304. end
  305.  
  306.  
  307. -- will mine a 16x16x3 area
  308. function mineSubchunk(miningDestination, stationReturn)
  309.     if miningDestination.direction == 1 then
  310.         turtleGoto(miningDestination.x, miningDestination.y, miningDestination.z + 1)
  311.         while direction() ~= 1 do
  312.             turn("left")
  313.         end
  314.     elseif miningDestination.direction == 2 then
  315.         turtleGoto(miningDestination.x - 1, miningDestination.y, miningDestination.z - 16)
  316.         while direction() ~= 2  do
  317.             turn("left")
  318.         end
  319.     elseif miningDestination.direction == 3 then
  320.         turtleGoto(miningDestination.x + 16, miningDestination.y, miningDestination.z - 17)  
  321.         while direction() ~= 3 do
  322.             turn("left")
  323.         end
  324.     elseif miningDestination.direction == 4 then
  325.         turtleGoto(miningDestination.x + 17, miningDestination.y, miningDestination.z)
  326.         while direction() ~= 4 do
  327.             turn("left")
  328.         end
  329.     else
  330.         print("invalid direction given to function \"minSubChunk\"")
  331.     end
  332.     mineForward()
  333.     local direction = "right"
  334.     for y= 1,16 do
  335.         for x= 1,15 do
  336.             mineForward()
  337.         end
  338.  
  339.         dropCobble()
  340.  
  341.         if direction == "right" then
  342.             turtle.turnRight()
  343.             mineForward()
  344.             turtle.turnRight()
  345.             direction = "left"
  346.         elseif direction == "left" then
  347.             turtle.turnLeft()
  348.             mineForward()
  349.             turtle.turnLeft()
  350.             direction = "right"
  351.         end
  352.     end
  353.     turtleGoto(stationReturn.x, stationReturn.y, stationReturn.z, true)
  354.     return true
  355. end
  356.  
  357. -- calculates how much coal is needed for journey
  358. function getCoalCount(x, y, z)
  359.     local distance = ((math.abs(x) + math.abs(y) + math.abs(z)) * 2) + 256
  360.     print(distance)
  361.     return distance / 80 + 3
  362. end
  363.  
  364. function rollOut(startData)
  365.     distances = turtleGoto(startData.destination.x, startData.destination.y, startData.destination.z, false, true)
  366.     neededCoal = getCoalCount(distances[1], distances[2], distances[3])
  367.     path(startData.exitPath, startData)
  368.     print("done pathing")
  369.     if mineSubchunk(startData.destination, startData.returnPoint) then
  370.         rollIn(startData)
  371.     end
  372. end
  373.  
  374. --brings the bot back from outside the station
  375. function rollIn(startData)
  376.     path(startData.enterPath, startData)
  377.     modem.transmit(5037, 5038, "eatme")
  378. end
  379.  
  380. --waits for connection with parent bot
  381. print("debug: made it to loop")
  382. transmitting = true
  383. while transmitting do
  384.     print("debug: made it into loop")
  385.     modem.transmit(5037, 5038, "created")
  386.     response = {os.pullEvent("modem_message")}
  387.     modem.transmit(response[3], response[4], "leaving")
  388.     info = response[5]
  389.     print(textutils.serialize(info.exitPath))
  390.     rollOut(info)
  391.     transmitting = false
  392. end
  393.  
  394.  
Add Comment
Please, Sign In to add comment