Advertisement
Guest User

turnOn

a guest
Nov 26th, 2020
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. --Credit: Michael Reeves--
  2. --https://www.twitch.tv/michaelreeves--
  3. --CLIENT DIG--
  4.  
  5. local SLOT_COUNT = 16
  6.  
  7. local CLIENT_PORT = 0
  8. local SERVER_PORT = 420
  9.  
  10. local modem = peripheral.wrap("left")
  11. modem.open(CLIENT_PORT)
  12.  
  13. function split (inputstr, sep)
  14.     if sep == nil then
  15.             sep = "%s"
  16.     end
  17.     local t={}
  18.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  19.             table.insert(t, str)
  20.     end
  21.     return t
  22. end
  23.  
  24. function parseParams(data)
  25.     coords = {}
  26.     params = split(data, " ")
  27.    
  28.     coords[1] = vector.new(params[1], params[2], params[3])
  29.     coords[2] = vector.new(params[4], params[5], params[6])
  30.     coords[3] = vector.new(params[7], params[8], params[9])
  31.  
  32.     return (coords)
  33. end
  34.  
  35.  
  36. function checkFuel()
  37.     turtle.select(1)
  38.    
  39.     if(turtle.getFuelLevel() < 200) then
  40.         print("Attempting Refuel...")
  41.         for slot = 1, SLOT_COUNT, 1 do
  42.             turtle.select(slot)
  43.             if(turtle.refuel(1)) then
  44.                 return true
  45.             end
  46.         end
  47.  
  48.         return false
  49.     else
  50.         return true
  51.     end
  52. end
  53.  
  54.  
  55. function getOrientation()
  56.     loc1 = vector.new(gps.locate(2, false))
  57.     if not turtle.forward() then
  58.         for j=1,6 do
  59.             if not turtle.forward() then
  60.                 turtle.dig()
  61.             else
  62.                 break
  63.             end
  64.         end
  65.     end
  66.     loc2 = vector.new(gps.locate(2, false))
  67.     heading = loc2 - loc1
  68.     turtle.down()
  69.     turtle.down()
  70.     return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  71.     end
  72.  
  73.  
  74. function turnToFaceHeading(heading, destinationHeading)
  75.     if(heading > destinationHeading) then
  76.         for t = 1, math.abs(destinationHeading - heading), 1 do
  77.             turtle.turnLeft()
  78.         end
  79.     elseif(heading < destinationHeading) then
  80.         for t = 1, math.abs(destinationHeading - heading), 1 do
  81.             turtle.turnRight()
  82.         end
  83.     end
  84. end
  85.  
  86. function setHeadingZ(zDiff, heading)
  87.     local destinationHeading = heading
  88.     if(zDiff < 0) then
  89.         destinationHeading = 2
  90.     elseif(zDiff > 0) then
  91.         destinationHeading = 4
  92.     end
  93.     turnToFaceHeading(heading, destinationHeading)
  94.  
  95.     return destinationHeading
  96. end
  97.  
  98. function setHeadingX(xDiff, heading)
  99.     local destinationHeading = heading
  100.     if(xDiff < 0) then
  101.         destinationHeading = 1
  102.     elseif(xDiff > 0) then
  103.         destinationHeading = 3
  104.     end
  105.  
  106.     turnToFaceHeading(heading, destinationHeading)
  107.     return destinationHeading
  108. end
  109.  
  110. function digAndMove(n)
  111.     for x = 1, n, 1 do
  112.         while(turtle.detect()) do
  113.             turtle.dig()
  114.         end
  115.         turtle.forward()
  116.         checkFuel()
  117.     end
  118. end
  119.  
  120. function digAndMoveDown(n)
  121.     for y = 1, n, 1 do
  122.         print(y)
  123.         while(turtle.detectDown()) do
  124.             turtle.digDown()
  125.         end
  126.         turtle.down()
  127.         checkFuel()
  128.     end
  129. end
  130.  
  131. function digAndMoveUp(n)
  132.     for y = 1, n, 1 do
  133.         while(turtle.detectUp()) do
  134.             turtle.digUp()
  135.         end
  136.         turtle.up()
  137.         checkFuel()
  138.     end
  139. end
  140.  
  141.  
  142. function moveTo(coords, heading)
  143.     local currX, currY, currZ = gps.locate()
  144.     local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  145.     print(string.format("Distances from start: %d %d %d", xDiff, yDiff, zDiff))
  146.  
  147.     --    -x = 1
  148.     --    -z = 2
  149.     --    +x = 3
  150.     --    +z = 4
  151.    
  152.  
  153.     -- Move to X start
  154.     heading = setHeadingX(xDiff, heading)
  155.     digAndMove(math.abs(xDiff))
  156.  
  157.     -- Move to Z start
  158.     heading = setHeadingZ(zDiff, heading)
  159.     digAndMove(math.abs(zDiff))
  160.  
  161.     -- Move to Y start
  162.     if(yDiff < 0) then    
  163.         digAndMoveDown(math.abs(yDiff))
  164.     elseif(yDiff > 0) then
  165.         digAndMoveUp(math.abs(yDiff))
  166.     end
  167.  
  168.  
  169.     return heading
  170. end
  171.  
  172.  
  173. function calculateFuel(travels, digSize, fuelType)
  174.     local currX, currY, currZ = gps.locate()
  175.     local xDiff, yDiff, zDiff = travels.x - currX, travels.y - currY, travels.z - currZ
  176.  
  177.     local volume = digSize.x + digSize.y + digSize.z
  178.     local travelDistance = (math.abs(xDiff) + math.abs(yDiff) + math.abs(zDiff)) * 2
  179.    
  180.     local totalFuel = volume + travelDistance
  181.     print(string.format( "total steps: %d", totalFuel))
  182.  
  183.     if(fuelType == "minecraft:coals") then
  184.         totalFuel = totalFuel / 80
  185.     elseif(fuelType == "minecraft:coal_block") then
  186.         totalFuel = totalFuel / 800
  187.     else
  188.         print("INVALID FUEL SOURCE")
  189.         os.exit(1)
  190.     end
  191.  
  192.     return math.floor(totalFuel) + 15
  193. end
  194.  
  195.  
  196.  
  197. modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
  198. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  199. data = parseParams(msg)
  200.  
  201. -- Pick up coal and refuel
  202. local fuelNeeded = calculateFuel(data[1], data[2], "minecraft:coals")
  203. turtle.suckDown(fuelNeeded)
  204. checkFuel()
  205.  
  206. print(string.format( "Extracting %d fuel...", fuelNeeded))
  207.  
  208. -- Grab Ender Chest
  209. turtle.turnLeft(1)
  210. turtle.suck(1)
  211. turtle.turnRight(1)
  212.  
  213. local startCoords = data[1]
  214. local finalHeading = moveTo(startCoords, getOrientation())
  215.  
  216. local NORTH_HEADING = 2
  217. --Turn to face North
  218. turnToFaceHeading(finalHeading, NORTH_HEADING)
  219. finalHeading = NORTH_HEADING
  220. --Now in Starting Position--
  221.  
  222. --------------------------------START MINING CODE-----------------------------------------
  223.  
  224.  
  225.  
  226.  
  227.  
  228. ------------------------------------------------------------------------------------------
  229.  
  230. DROPPED_ITEMS = {
  231.     "minecraft:stone",
  232.     "minecraft:dirt",
  233.     "minecraft:granite",
  234.     "minecraft:cobblestone",
  235.     "minecraft:sand",
  236.     "minecraft:gravel",
  237.     "minecraft:redstone",
  238.     "minecraft:flint",
  239.     "railcraft:ore_metal",
  240.     "extrautils2:ingredients",
  241.     "minecraft:dye",
  242.     "thaumcraft:nugget",
  243.     "thaumcraft:crystal_essence",
  244.     "thermalfoundation:material",
  245.     "projectred-core:resource_item",
  246.     "thaumcraft:ore_cinnabar",
  247.     "deepresonance:resonating_ore",
  248.     "forestry:apatite",
  249.     "biomesoplenty:loamy_dirt",
  250.     "chisel:marble",
  251.     "chisel:limestone",
  252.     "chisel:basalt",
  253. }
  254. function dropItems()
  255.     print("Purging Inventory...")
  256.     for slot = 1, SLOT_COUNT, 1 do
  257.         local item = turtle.getItemDetail(slot)
  258.         if(item ~= nil) then
  259.             for filterIndex = 1, #DROPPED_ITEMS, 1 do
  260.                 if(item["name"] == DROPPED_ITEMS[filterIndex]) then
  261.                     print("Dropping - " .. item["name"])
  262.                     turtle.select(slot)
  263.                     turtle.dropDown()
  264.                 end
  265.             end
  266.         end
  267.     end
  268. end
  269.  
  270.  
  271. function getEnderIndex()
  272.     for slot = 1, SLOT_COUNT, 1 do
  273.         local item = turtle.getItemDetail(slot)
  274.         if(item ~= nil) then
  275.             if(item["name"] == "enderchests:ender_chest") then
  276.                 return slot
  277.             end
  278.         end
  279.     end
  280.     return nil
  281. end
  282.  
  283. function manageInventory()
  284.     dropItems()
  285.     index = getEnderIndex()
  286.     if(index ~= nil) then
  287.         turtle.select(index)
  288.         turtle.digUp()      
  289.         turtle.placeUp()  
  290.     end
  291.     -- Chest is now deployed
  292.     for slot = 1, SLOT_COUNT, 1 do
  293.         local item = turtle.getItemDetail(slot)
  294.         if(item ~= nil) then
  295.             if(item["name"] ~= "minecraft:coal_block" and item["name"] ~= "minecraft:coal" and item["name"] ~= "minecraft:charcoal") then
  296.                 turtle.select(slot)
  297.                 turtle.dropUp()
  298.             end
  299.         end
  300.     end
  301.     -- Items are now stored
  302.  
  303.     turtle.digUp()
  304. end
  305.  
  306.  
  307. function detectAndDig()
  308.     while(turtle.detect()) do
  309.         turtle.dig()
  310.     end
  311. end
  312.  
  313. function forward()
  314.     detectAndDig()
  315.     turtle.forward()
  316. end
  317.  
  318. function leftTurn()
  319.     turtle.turnLeft()
  320.     detectAndDig()
  321.     turtle.forward()
  322.     turtle.turnLeft()
  323. end
  324.  
  325.  
  326. function rightTurn()
  327.     turtle.turnRight()
  328.     detectAndDig()
  329.     turtle.forward()
  330.     turtle.turnRight()
  331. end
  332.  
  333.  
  334. function dropTier(heading)
  335.     turtle.turnRight()
  336.     turtle.turnRight()
  337.     turtle.digDown()
  338.     turtle.down()
  339.     return flipDirection(heading)
  340. end
  341.  
  342.  
  343. function flipDirection(heading)
  344.     return ((heading + 1) % 4) + 1
  345. end
  346.  
  347. function turnAround(tier, heading)
  348.     if(tier % 2 == 1) then
  349.         if(heading == 2 or heading == 3) then
  350.             rightTurn()
  351.         elseif(heading == 1 or heading == 4) then
  352.             leftTurn()
  353.         end
  354.     else
  355.         if(heading == 2 or heading == 3) then
  356.             leftTurn()
  357.         elseif(heading == 1 or heading == 4) then
  358.             rightTurn()
  359.         end
  360.     end
  361.    
  362.     return flipDirection(heading)
  363. end
  364.  
  365.  
  366.  
  367. function startQuary(width, height, depth, heading)
  368.  
  369.     for tier = 1, height, 1 do
  370.         for col = 1, width, 1 do
  371.             for row = 1, depth - 1, 1 do
  372.                 if(not checkFuel()) then
  373.                     print("Turtle is out of fuel, Powering Down...")
  374.                     return
  375.                 end
  376.                 forward()
  377.             end
  378.             if(col ~= width) then
  379.                 heading = turnAround(tier, heading)
  380.             end
  381.             manageInventory()
  382.         end
  383.         if(tier ~= height) then
  384.             heading = dropTier(heading)
  385.         end
  386.     end
  387.  
  388.     return heading
  389. end
  390.  
  391.  
  392. local quary = data[2]
  393. finishedHeading = startQuary(quary.x, quary.y, quary.z, finalHeading)
  394.  
  395.  
  396.  
  397. --------------------------------START RETURN TRIP CODE------------------------------------
  398.  
  399.  
  400.  
  401.  
  402.  
  403. ------------------------------------------------------------------------------------------
  404.  
  405.  
  406. function returnTo(coords, heading)
  407.     local currX, currY, currZ = gps.locate()
  408.     local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  409.     print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
  410.    
  411.     -- Move to Y start
  412.     if(yDiff < 0) then    
  413.         digAndMoveDown(math.abs(yDiff))
  414.     elseif(yDiff > 0) then
  415.         digAndMoveUp(math.abs(yDiff))
  416.     end
  417.  
  418.     -- Move to X start
  419.     heading = setHeadingX(xDiff, heading)
  420.     digAndMove(math.abs(xDiff))
  421.     manageInventory()
  422.  
  423.     -- Move to Z start
  424.     heading = setHeadingZ(zDiff, heading)
  425.     digAndMove(math.abs(zDiff))
  426.    
  427.    
  428.  
  429.     return heading
  430. end
  431.  
  432. endCoords = data[3]
  433. returnTo(endCoords ,finishedHeading)
  434.  
  435. local timoutWait = 200
  436. for i = 1, timoutWait, 1 do
  437.     os.sleep(1)
  438.     print(string.format( "Waiting for brothers %d/%d", i, timoutWait))
  439. end
  440.  
  441. modem.transmit(SERVER_PORT, CLIENT_PORT, "cum")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement