Advertisement
MavricMC

MIner client

May 30th, 2021 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.21 KB | None | 0 0
  1. local CLIENT_PORT = 10
  2. local SERVER_PORT = 0
  3.  
  4. local modem = peripheral.wrap("right")
  5. modem.open(CLIENT_PORT)
  6.  
  7. --name startup on the floppy disk--
  8. function dig()
  9.     --dig until there is not a block above it--
  10.     --for gravity blocks--
  11.     while turtle.detect() do
  12.         l, data = turtle.inspect()
  13.         l = nil
  14.         if data.name ~= "computercraft:turtle_advanced" and data.name ~= "computercraft:disk_drive" then
  15.             turtle.dig()
  16.         end
  17.     end
  18. end
  19.  
  20. function digUp()
  21.     while turtle.detectUp() do
  22.         l, data = turtle.inspectUp()
  23.         l = nil
  24.         if data.name ~= "computercraft:turtle_advanced" and data.name ~= "computercraft:disk_drive" then
  25.             turtle.digUp()
  26.         end
  27.     end
  28. end
  29.  
  30. function digDown()
  31.     while turtle.detectDown() do
  32.         l, data = turtle.inspectDown()
  33.         l = nil
  34.         if data.name ~= "computercraft:turtle_advanced" and data.name ~= "computercraft:disk_drive" then
  35.             turtle.digDown()
  36.         end
  37.     end
  38. end
  39.  
  40. function move(n)
  41.     for m = 1, n do
  42.         dig()
  43.         turtle.forward()
  44.         checkFuel()
  45.     end
  46. end
  47.  
  48. function moveUp(n)
  49.     for m = 1, n do
  50.         digUp()
  51.         turtle.up()
  52.         checkFuel()
  53.     end
  54. end
  55.  
  56. function moveDown(n)
  57.     for m = 1, n do
  58.         digDown()
  59.         turtle.down()
  60.         checkFuel()
  61.     end
  62. end
  63.  
  64. function getItemIndex(itemName)
  65.     --loops thru all slot and checks if there is the chosen item--
  66.     for slot = 1, 16 do
  67.         local item = turtle.getItemDetail(slot)
  68.         if item ~= nil then
  69.             if item.name == itemName then
  70.                 return slot
  71.             end
  72.         end
  73.     end
  74. end
  75.  
  76. function checkFuel()
  77.     if turtle.getFuelLevel() < 100 then
  78.         --make your own fuel up code as it depends on the mods you have--
  79.     end
  80.     return true
  81. end
  82.  
  83. --Ifound this code on the computercraft forums--
  84. function getOrientation()
  85.     checkFuel()
  86.     local loc1 = vector.new(gps.locate(2, false))
  87.     if not turtle.forward() then
  88.         for j = 1 ,6 do
  89.             if not turtle. forward() then
  90.                 turtle.dig()
  91.             else break end
  92.         end
  93.     end
  94.     local loc2 = vector.new(gps.locate(2, false))
  95.     turtle.back()
  96.     local heading = loc2 - loc1
  97.     return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
  98. end
  99.  
  100. function turnToFaceHeading(heading, destinationHeading)
  101.     if (heading > destinationHeading) then
  102.         dis = heading - destinationHeading
  103.         for t = 1, dis do
  104.             turtle.turnRight()
  105.         end
  106.     elseif (heading < destinationHeading) then
  107.         dis = destinationHeading - heading
  108.         for t = 1, dis do
  109.             turtle.turnLeft()
  110.         end
  111.     end
  112. end
  113.    
  114. function moveTo(x, y, z, heading, final)
  115.     curtX, curtY, curtZ = gps.locate()
  116.     cortX = math.floor(curtX)
  117.     cyrty = math.floor(curtY)
  118.     curtZ = math.floor(curtZ)
  119.    
  120.     xDif = x - curtX
  121.     yDif = y - curtY
  122.     zDif = z - curtZ
  123.    
  124.    
  125.     if (final) then
  126.         if yDif < 0 then
  127.             yDif = curtY - y
  128.             moveDown(yDif)
  129.         elseif yDif > 0 then
  130.             moveUp(yDif)
  131.         end
  132.     end
  133.    
  134.     --turn to x heading--
  135.     if xDif < 0 then
  136.         turnToFaceHeading(1, heading)
  137.         heading = 1
  138.         xDif = curtX - x
  139.     elseif xDif > 0 then
  140.         turnToFaceHeading(3, heading)
  141.         heading = 3
  142.     end
  143.    
  144.     --move to x cords
  145.     move(xDif)
  146.    
  147.     --turn to z heading--
  148.     if zDif < 0 then
  149.         turnToFaceHeading(2, heading)
  150.         heading = 2
  151.         zDif = curtZ - z
  152.     elseif zDif > 0 then
  153.         turnToFaceHeading(4, heading)
  154.         heading = 4
  155.     end
  156.    
  157.     --move to z cords--
  158.     if (final) then
  159.         zDif = zDif - 1
  160.     end
  161.     move(zDif)
  162.    
  163.     --move up of down to y cords--
  164.     if final == false then
  165.         if yDif < 0 then
  166.             yDif = curtY - y
  167.             moveDown(yDif)
  168.         elseif yDif > 0 then
  169.             moveUp(yDif)
  170.         end
  171.     end
  172.        
  173.     return heading
  174. end
  175.  
  176. --starting code--
  177. modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
  178. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  179. checkFuel()
  180. write("Deploying to ")
  181. write(msg[1])
  182. write(" ")
  183. write(msg[2])
  184. write(" ")
  185. print(msg[3])
  186. finalHeading = moveTo(msg[1], msg[2], msg[3], getOrientation(), false)
  187.  
  188. --turn west--
  189. turnToFaceHeading(1, finalHeading)
  190.  
  191. --main code--
  192.  
  193. keepItems = {
  194.     "minecraft:gold_ore",
  195.     "minecraft:iron_ore",
  196.     "minecraft:coal",
  197.     "minecraft:nether_gold_ore",
  198.     "minecraft:oak_log",
  199.     "minecraft:oak_sapling",
  200.     "minecraft:spruce_log",
  201.     "minecraft:spruce_sapling",
  202.     "minecraft:birch_log",
  203.     "minecraft:birch_sapling",
  204.     "minecraft:jungle_log",
  205.     "minecraft:jungle_sapling",
  206.     "minecraft:acacia_log",
  207.     "minecraft:acacia_sapling",
  208.     "minecraft:dark_oak_log",
  209.     "minecraft:dark_oak_sapling",
  210.     "minecraft:crimson_stem",
  211.     "minecraft:warped_stem",
  212.     "minecraft:diamond",
  213.     "minecraft:lapis_lazuli",
  214.     "minecraft:redstone",
  215.     "minecraft:emerald",
  216.     "minecraft:ancient_debris",
  217.     "minecraft:glowstone_dust",
  218.     "minecraft:apple",
  219.     "computercraft:turtle_advanced"
  220. }
  221.  
  222. function isDrop(item)
  223.     for k, v in pairs(keepItems) do
  224.         if item == keepItems[k] then
  225.             return false
  226.         end
  227.     end
  228.     return true
  229. end
  230.  
  231. function dropItems()
  232.     for d = 1, 16 do
  233.         data = turtle.getItemDetail(d)
  234.         if (data) then
  235.             if (isDrop(data.name)) then
  236.                 turtle.select(d)
  237.                 turtle.dropDown()
  238.             end
  239.         end
  240.     end
  241. end
  242.  
  243. function storeItems()
  244.     --make your own store code as it depends on the mods you have--
  245. end
  246.  
  247. function mine(xSize, ySize, zSize)
  248.     turn = true
  249.     down = false
  250.     for y = 1, ySize do
  251.         if (down) then
  252.             digDown()
  253.             checkFuel()
  254.             turtle.down()
  255.             turtle.turnLeft()
  256.             turtle.turnLeft()
  257.             storeItems()
  258.         else
  259.             down = true
  260.         end
  261.        
  262.         trg = false    
  263.         for z = 1, zSize do
  264.             if (trg) then
  265.                 checkFuel()
  266.                 if (turn) then
  267.                     turtle.turnRight()
  268.                     dig()
  269.                     turtle.forward()
  270.                     turtle.turnRight()
  271.                     turn = false
  272.                 else
  273.                     turtle.turnLeft()
  274.                     dig()
  275.                     turtle.forward()
  276.                     turtle.turnLeft()
  277.                     turn = true
  278.                 end
  279.                 dropItems()
  280.             else
  281.                 trg = true
  282.             end
  283.            
  284.             for x = 2, xSize do
  285.                 dig()
  286.                 checkFuel()
  287.                 turtle.forward()
  288.             end
  289.         end
  290.     end
  291. end
  292.  
  293. storeItems()
  294. mine(msg[4], msg[5], msg[6])
  295.  
  296. moveTo(msg[7], msg[8], msg[9], getOrientation(), true)
  297. sleep(10)
  298. turtle.dig()
  299. turtle.forward()
  300. storeItems()
  301.  
  302. local timeOut = 60
  303. for i = 1, timeOut do
  304.     sleep(1)
  305.     print("Waiting for brothers",i.."/"..timeOut)
  306. end
  307.  
  308. turtle.down()
  309. modem.transmit(SERVER_PORT, CLIENT_PORT, "done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement