Advertisement
Jakey4543

turtletest

Jan 16th, 2022 (edited)
1,667
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local ports = {
  2.     ["phone"] = 8081,
  3.     ["server"] = 8080,
  4.     ["deployer"] = 5050,
  5.     ["turtle"] = math.random(1000,9000) -- generates a random port for this turtle
  6. }
  7.  
  8. local key = "" -- a key sent to us by the server which authenticates us
  9. local data = nil
  10. local modem = peripheral.wrap("left")
  11. modem.open(ports.turtle) -- opens a port for us
  12.  
  13.  
  14. local homeCoords = vector.new(gps.locate(5,false))
  15. -- modem.transmit(ports.server,ports.turtle,"MESSAGE") base transmit code
  16.  
  17. -- startup sub --
  18.  
  19. turtle.suckDown() -- gets lava sack
  20.  
  21. -- startup sub finished --
  22.  
  23. function split (inputstr, sep)
  24.     if sep == nil then
  25.             sep = "%s"
  26.     end
  27.     local t={}
  28.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  29.             table.insert(t, str)
  30.     end
  31.     return t
  32. end
  33.  
  34. function parseParams(data)
  35.     coords = {}
  36.     params = split(data, " ")
  37.  
  38.     coords[1] = vector.new(params[1], params[2], params[3])
  39.     coords[2] = vector.new(params[4], params[5], params[6])
  40.  
  41.     return (coords)
  42. end
  43.  
  44. function getItemIndex(itemName)
  45.     for slot = 1, 16, 1 do
  46.         local item = turtle.getItemDetail(slot)
  47.         if(item ~= nil) then
  48.             if(item["name"] == itemName) then
  49.                 return slot
  50.             end
  51.         end
  52.     end
  53. end
  54.  
  55. function checkFuel()
  56.     if(turtle.getFuelLevel() < 100) then
  57.         -- refuel code
  58.         local sackIndex = getItemIndex("supplementaries:sack")
  59.  
  60.         turtle.dig() -- digs one forward to be sure we can place the sack
  61.         turtle.place(sackIndex) -- places the sack
  62.         turtle.suck() -- sucks forward to get lava bucket
  63.         turtle.refuel(1) -- refuels
  64.  
  65.         local bucketIndex = getItemIndex("minecraft:bucket")
  66.         turtle.drop(bucketIndex)
  67.         turtle.dig() -- breaks the sack and puts it back in the turtles inventory
  68.  
  69.         print("Refueled.")
  70.     end
  71.     return true
  72. end
  73.  
  74. function getOrientation()
  75.     loc1 = vector.new(gps.locate(2, false))
  76.     if not turtle.forward() then
  77.         for j=1,6 do
  78.             if not turtle.forward() then
  79.                 turtle.dig()
  80.             else
  81.                 break
  82.             end
  83.         end
  84.     end
  85.     loc2 = vector.new(gps.locate(2, false))
  86.     heading = loc2 - loc1
  87.     turtle.back()
  88.     -- turtle.down()
  89.     return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  90. end
  91.  
  92. function turnToFaceHeading(heading, destinationHeading)
  93.     if(heading > destinationHeading) then
  94.         for t = 1, math.abs(destinationHeading - heading), 1 do
  95.             turtle.turnLeft()
  96.         end
  97.     elseif(heading < destinationHeading) then
  98.         for t = 1, math.abs(destinationHeading - heading), 1 do
  99.             turtle.turnRight()
  100.         end
  101.     end
  102. end
  103.  
  104. function setHeadingZ(zDiff, heading)
  105.     local destinationHeading = heading
  106.     if(zDiff < 0) then
  107.         destinationHeading = 2
  108.     elseif(zDiff > 0) then
  109.         destinationHeading = 4
  110.     end
  111.     turnToFaceHeading(heading, destinationHeading)
  112.  
  113.     return destinationHeading
  114. end
  115.  
  116. function setHeadingX(xDiff, heading)
  117.     local destinationHeading = heading
  118.     if(xDiff < 0) then
  119.         destinationHeading = 1
  120.     elseif(xDiff > 0) then
  121.         destinationHeading = 3
  122.     end
  123.  
  124.     turnToFaceHeading(heading, destinationHeading)
  125.     return destinationHeading
  126. end
  127.  
  128. function digAndMove(n)
  129.     for x = 1, n, 1 do
  130.         while(turtle.detect()) do
  131.             turtle.dig()
  132.         end
  133.         turtle.forward()
  134.         checkFuel()
  135.     end
  136. end
  137.  
  138. function digAndMoveDown(n)
  139.     for y = 1, n, 1 do
  140.         while(turtle.detectDown()) do
  141.             turtle.digDown()
  142.         end
  143.         turtle.down()
  144.         checkFuel()
  145.     end
  146. end
  147.  
  148. function digAndMoveUp(n)
  149.     for y = 1, n, 1 do
  150.         while(turtle.detectUp()) do
  151.             turtle.digUp()
  152.         end
  153.         turtle.up()
  154.         checkFuel()
  155.     end
  156. end
  157.  
  158. function moveTo(coords, heading)
  159.     local currX, currY, currZ = gps.locate()
  160.     print("CURR: "..currX..","..currY..","..currZ)
  161.     local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  162.     print(string.format("Distances from start: %d %d %d", xDiff, yDiff, zDiff))
  163.  
  164.  
  165.     --    -x = 1
  166.     --    -z = 2
  167.     --    +x = 3
  168.     --    +z = 4
  169.  
  170.  
  171.     -- Move to X start
  172.     heading = setHeadingX(xDiff, heading)
  173.     digAndMove(math.abs(xDiff))
  174.  
  175.     -- Move to Z start
  176.     heading = setHeadingZ(zDiff, heading)
  177.     digAndMove(math.abs(zDiff))
  178.  
  179.     -- Move to Y start
  180.     if(yDiff < 0) then    
  181.         digAndMoveDown(math.abs(yDiff))
  182.     elseif(yDiff > 0) then
  183.         digAndMoveUp(math.abs(yDiff))
  184.     end
  185.  
  186.     return heading
  187. end
  188.  
  189. modem.transmit(ports.server,ports.turtle,"CLIENT_READY")
  190. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message") -- server gives us key
  191. key = msg
  192. print("Recieved auth key "..key)
  193.  
  194. os.sleep(0.5)
  195.  
  196. -- parse info
  197. event, side, senderChannel, replyChannel, mineinfo, distance = os.pullEvent("modem_message") -- server gives us info
  198. local args = split(mineinfo," ")
  199. local withStorage = args[#args]
  200. withStorage = withStorage == "1" and true or false
  201. data = parseParams(mineinfo)
  202.  
  203. checkFuel()
  204.  
  205. local startCoords = data[1]
  206. local quarrySize = data[2]
  207. local finalHeading = moveTo(startCoords, getOrientation())
  208.  
  209. local EAST_HEADING = 3
  210. --Turn to face North
  211. turnToFaceHeading(finalHeading, EAST_HEADING)
  212. finalHeading = EAST_HEADING
  213. --Now in Starting Position--
  214.  
  215. --------------------------------START MINING CODE-----------------------------------------
  216.  
  217.  
  218.  
  219.  
  220.  
  221. ------------------------------------------------------------------------------------------
  222.  
  223. local DROPPED_ITEMS = {
  224.     "minecraft:stone",
  225.     "minecraft:dirt",
  226.     "minecraft:cobblestone",
  227.     "minecraft:sand",
  228.     "minecraft:gravel",
  229.     "minecraft:redstone",
  230.     "minecraft:flint",
  231.     "railcraft:ore_metal",
  232.     "extrautils2:ingredients",
  233.     "minecraft:dye",
  234.     "thaumcraft:nugget",
  235.     "thaumcraft:crystal_essence",
  236.     "thermalfoundation:material",
  237.     "projectred-core:resource_item",
  238.     "deepresonance:resonating_ore",
  239.     "forestry:apatite"
  240. }
  241.  
  242. function dropItems()
  243.     print("Purging Inventory...")
  244.     for slot = 1, 16, 1 do
  245.         local item = turtle.getItemDetail(slot)
  246.         if(item ~= nil) then
  247.             for filterIndex = 1, #DROPPED_ITEMS, 1 do
  248.                 if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal" and item["name"] ~= "supplementaries:sack") then
  249.                     print("Dropping - " .. item["name"])
  250.                     turtle.select(slot)
  251.                     turtle.dropDown()
  252.                 end
  253.             end
  254.         end
  255.     end
  256. end
  257.  
  258. function manageInventory()
  259.     dropItems()
  260. end
  261.  
  262. function detectAndDig()
  263.     while(turtle.detect()) do
  264.         turtle.dig()
  265.     end
  266. end
  267.  
  268. function forward()
  269.     detectAndDig()
  270.     turtle.forward()
  271. end
  272.  
  273. function leftTurn()
  274.     turtle.turnLeft()
  275.     detectAndDig()
  276.     turtle.forward()
  277.     turtle.turnLeft()
  278. end
  279.  
  280.  
  281. function rightTurn()
  282.     turtle.turnRight()
  283.     detectAndDig()
  284.     turtle.forward()
  285.     turtle.turnRight()
  286. end
  287.  
  288.  
  289. function dropTier(heading)
  290.     turtle.turnRight()
  291.     turtle.turnRight()
  292.     turtle.digDown()
  293.     turtle.down()
  294.     return flipDirection(heading)
  295. end
  296.  
  297.  
  298. function flipDirection(heading)
  299.     return ((heading + 1) % 4) + 1
  300. end
  301.  
  302. function turnAround(tier, heading)
  303.     if(tier % 2 == 1) then
  304.         if(heading == 2 or heading == 3) then
  305.             rightTurn()
  306.         elseif(heading == 1 or heading == 4) then
  307.             leftTurn()
  308.         end
  309.     else
  310.         if(heading == 2 or heading == 3) then
  311.             leftTurn()
  312.         elseif(heading == 1 or heading == 4) then
  313.             rightTurn()
  314.         end
  315.     end
  316.  
  317.     return flipDirection(heading)
  318. end
  319.  
  320. function startQuary(width, height, depth, heading)
  321.  
  322.     for tier = 1, height, 1 do
  323.         for col = 1, width, 1 do
  324.             for row = 1, depth - 1, 1 do
  325.                 -- if(not checkFuel()) then
  326.                 --     print("Turtle is out of fuel, Powering Down...")
  327.                 --     return
  328.                 -- end
  329.                 checkFuel()
  330.                 forward()
  331.             end
  332.             if(col ~= width) then
  333.                 heading = turnAround(tier, heading)
  334.             end
  335.             manageInventory()
  336.         end
  337.         if(tier ~= height) then
  338.             heading = dropTier(heading)
  339.         end
  340.     end
  341.  
  342.     return heading
  343. end
  344.  
  345. print(quarrySize)
  346. finishedHeading = startQuary(quarrySize.x, quarrySize.y, quarrySize.z, finalHeading)
  347.  
  348. --------------------------------START RETURN TRIP CODE------------------------------------
  349.  
  350.  
  351.  
  352.  
  353.  
  354. ------------------------------------------------------------------------------------------
  355.  
  356.  
  357. function returnTo(coords, heading)
  358.     local currX, currY, currZ = gps.locate()
  359.     local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  360.     print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
  361.  
  362.     -- Move to Y start
  363.     if(yDiff < 0) then    
  364.         digAndMoveDown(math.abs(yDiff))
  365.     elseif(yDiff > 0) then
  366.         digAndMoveUp(math.abs(yDiff))
  367.     end
  368.  
  369.     -- Move to X start
  370.     heading = setHeadingX(xDiff, heading)
  371.     digAndMove(math.abs(xDiff))
  372.  
  373.     -- Move to Z start
  374.     heading = setHeadingZ(zDiff, heading)
  375.     digAndMove(math.abs(zDiff))
  376.  
  377.  
  378.  
  379.     return heading
  380. end
  381.  
  382. returnTo(homeCoords,finishedHeading)
  383.  
  384. turtle.dropDown(getItemIndex("supplementaries:sack"))
  385. os.sleep(1.5)
  386. modem.transmit(ports.deployer,ports.turtle, "BREAK_TURTLE")
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement