Advertisement
Guest User

ComputerCraft Miner

a guest
Apr 14th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.01 KB | None | 0 0
  1. --[[
  2. This script is mining diamonds scheme. Place miner on height 5, in the edge of the chunk.
  3. On left side place chest for resources.
  4. On back side place chest with coal.
  5. First inventoy slot should be filled with coal.
  6. ]]--
  7.  
  8. -- Fuel operations
  9.  
  10. function refuel()
  11.   turtle.select(1)
  12.   local curFuel, maxFuel, count, result
  13.   curFuel = turtle.getFuelLevel()
  14.   maxFuel = turtle.getFuelLimit()
  15.   count = turtle.getItemCount()
  16.   count = (count > 0) and (count - 1) or 0
  17.   count = math.min(count, math.floor((maxFuel - curFuel) / 80)) -- 1 coal = 80 fuel
  18.   turtle.refuel(count)
  19.   return count > 0
  20. end
  21.  
  22. function loadFuel()
  23.   turtle.select(1)
  24.   local result
  25.   result = true
  26.   while (result) do
  27.     result = turtle.suck(64 - turtle.getItemCount())
  28.     result = result and refuel()
  29.   end
  30. end
  31.  
  32. -- Other inventory operations
  33.  
  34. function inventoryFilled()
  35.   local count
  36.   count = turtle.getItemCount(16)
  37.   return count > 0
  38. end
  39.  
  40. function inventoryUnload()
  41.   local i
  42.   for i = 2, 16 do
  43.     turtle.select(i)
  44.     if (turtle.compareTo(1)) then
  45.       turnLeft(1)
  46.       turtle.drop()
  47.       turnRight(1)
  48.     else
  49.       turtle.drop()
  50.     end
  51.   end
  52.   turtle.select(1)
  53. end
  54.  
  55. -- Basic rotation operations
  56.  
  57. function turnRight(times)
  58.   times = times % 4
  59.   while (times > 0) do
  60.     turtle.turnRight()
  61.     dx, dz = -dz, dx
  62.     times = times - 1
  63.   end
  64. end
  65.  
  66. function turnLeft(times)
  67.   times = times % 4
  68.   while (times > 0) do
  69.     turtle.turnLeft()
  70.     dx, dz = dz, -dx
  71.     times = times - 1
  72.   end
  73. end
  74.  
  75. function rotateToDirection(rx, rz)
  76.   if (rx == -dx and rz == -dz) then
  77.     turnRight(2)
  78.   elseif (rx == -dz and rz == dx) then
  79.     turnRight(1)
  80.   elseif (rx == dz and rz == -dx) then
  81.     turnLeft(1)
  82.   end
  83. end
  84.  
  85. -- Basic move operations
  86.  
  87. function digForward(length)
  88.   local result
  89.   while (length > 0) do
  90.     result = not turtle.detect() or (turtle.dig() == true)
  91.     result = turtle.forward() and result
  92.     if (result) then
  93.       length = length - 1
  94.       x = x + dx
  95.       z = z + dz
  96.       distance = distance + dx + dz
  97.     end
  98.   end
  99. end
  100.  
  101. function digUp(length)
  102.   local result
  103.   while (length > 0) do
  104.     result = not turtle.detectUp() or (turtle.digUp() == true)
  105.     result = turtle.up() and result
  106.     if (result) then
  107.       length = length - 1
  108.       y = y + 1
  109.       distance = distance + 1
  110.     end
  111.   end
  112. end
  113.  
  114. function digDown(length)
  115.   local result
  116.   while (length > 0) do
  117.     result = not turtle.detectDown() or (turtle.digDown() == true)
  118.     result = turtle.down() and result
  119.     if (result) then
  120.       length = length - 1
  121.       y = y - 1
  122.       distance = distance - 1
  123.     end
  124.   end
  125. end
  126.  
  127. function strafeRight(length)
  128.   turnRight(1)
  129.   digForward(length)
  130.   turnLeft(1)
  131. end
  132.  
  133. function strafeLeft(length)
  134.   turnLeft(1)
  135.   digForward(length)
  136.   turnRight(1)
  137. end
  138.  
  139. -- Intelligence functions
  140.  
  141. --  Home Functions
  142.  
  143. function goHomeHigh()
  144.   if ((x ~= 0 or z ~= 0) and y < 16) then
  145.     digUp(16 - y)
  146.   end
  147.   if (z < 0) then
  148.     rotateToDirection(0, 1)
  149.     digForward(-z)
  150.   elseif (z > 0) then
  151.     rotateToDirection(0, -1)
  152.     digForward(z)
  153.   elseif (x < 0) then
  154.     rotateToDirection(1, 0)
  155.     digForward(-x)
  156.   elseif (x > 0) then
  157.     rotateToDirection(-1, 0)
  158.     digForward(x)
  159.   elseif (y > 5) then
  160.     digDown(y - 5)
  161.   end
  162. end
  163.  
  164. function goHomeLow()
  165.   if (x % 16 ~= 0) then
  166.     if (y % 3 == 0) then
  167.       digDown(1)
  168.     else
  169.       rotateToDirection(-1, 0)
  170.       digForward(x)
  171.     end
  172.   elseif (y > 5) then
  173.     digDown(y - 5)
  174.   elseif (z > 0) then
  175.     rotateToDirection(0, -1)
  176.     digForward(z)
  177.   else
  178.     rotateToDirection(-1, 0)
  179.     digForward(x)
  180.   end
  181. end
  182.  
  183. function homeActions()
  184.   rotateToDirection(0, -1)
  185.   inventoryUnload()
  186.   turnLeft(1)
  187.   loadFuel()
  188.   turnLeft(2)
  189.  
  190.   if (newChunk) then
  191.     tx = cx
  192.     ty = 5
  193.     dz = 0
  194.   end
  195.  
  196.   if (tx - x + ty - y + tz - z > turtle.getFuelLevel() / 2 - 400) then
  197.     print("Not enough fuel to start working")
  198.     goHome = true
  199.     sleep(10)
  200.   else
  201.     print("Successfully resupplied, digging")
  202.     goHome = false
  203.   end
  204. end
  205.  
  206. function home()
  207.   if (y > 15 or x < 0 or z < 0 or z > 14) then
  208.     goHomeHigh()
  209.   elseif (x ~= 0 or y ~= 5 or z ~= 0) then
  210.     goHomeLow()
  211.   else
  212.     homeActions()
  213.   end
  214. end
  215.  
  216. --  Mine functions
  217.  
  218. function moveToTarget()
  219.   local maxHeight
  220.   maxHeight = ty
  221.   if (maxHeight % 3 == 0 and x ~= tx) then
  222.     maxHeight = maxHeight - 1
  223.   end
  224.   if (x < cx) then
  225.     rotateToDirection(1, 0)
  226.     digForward(cx - x)
  227.   elseif (z ~= tz) then
  228.     rotateToDirection(0, 1)
  229.     digForward(tz - z)
  230.   elseif (y ~= maxHeight) then
  231.     digUp(maxHeight - y)
  232.   else
  233.     rotateToDirection(1, 0)
  234.     digForward(tx - x)
  235.   end
  236. end
  237.  
  238. function makeNewTunnel()
  239.   if (z % 2 == 0) then
  240.     if (y > 6) then
  241.       digDown(y - 6)
  242.     else
  243.       rotateToDirection(0, 1)
  244.       digForward(1)
  245.     end
  246.   else
  247.     if (y == 6) then
  248.       digDown(1)
  249.     else
  250.       newTunnel = false
  251.       digForward(1)
  252.     end
  253. end
  254.  
  255. function startNewChunk()
  256.   if (y > 5) then
  257.     digDown(y - 5)
  258.   elseif (z > 0) then
  259.     rotateToDirection(0, -1)
  260.     digForward(14)
  261.   else
  262.     if (x < cx) then
  263.       rotateToDirection(1, 0)
  264.       digForward(1)
  265.     else
  266.       newChunk = false
  267.     end
  268.   end
  269. end
  270.  
  271. function usualMining()
  272.   if (y % 3 == 2) then
  273.     if (x % 16 ~= 15) then
  274.       rotateToDirection(1, 0)
  275.       digForward(1)
  276.     else
  277.       digUp(1)
  278.     end
  279.   elseif (y % 3 == 0) then
  280.     if (x % 16 ~= 0) then
  281.       rotateToDirection(-1, 0)
  282.       digForward(1)
  283.     else
  284.       if (y ~= 15) then
  285.         digUp(1)
  286.       elseif (z ~= 14) then
  287.         print("Starting new tunnel")
  288.         newTunnel = true
  289.       else
  290.         print("Starting new chunk")
  291.         newChunk = true
  292.         cx = cx + 16
  293.         saveImportantData()
  294.       end
  295.     end
  296.   else
  297.     digUp(1)
  298.   end
  299. end
  300.  
  301. function mine()
  302.   local maxHeight
  303.   if (tx ~= x or ty ~= y or tz ~= z) then
  304.     moveToTarget()
  305.   else
  306.     if (newTunnel) then
  307.       makeNewTunnel()
  308.     elseif (newChunk) then
  309.       startNewChunk()
  310.     else
  311.       usualMining()
  312.     end
  313.  
  314.     tx = x
  315.     ty = y
  316.     tz = z
  317.    
  318.     if (turtle.getFuelLevel() <= distance + 3 and (not refuel() or turtle.getFuelLevel() <= distance + 3)) then
  319.       print("Low fuel level, returning")
  320.       goHome = true
  321.     elseif (inventoryFilled()) then
  322.       print("Full inventory, returning")
  323.       goHome = true
  324.     end
  325.   end
  326. end
  327.  
  328. -- Initialization functions
  329.  
  330. function reset()
  331.   -- local chunk x start pos
  332.   cx = 0
  333.   -- local move direction
  334.   dx = 1
  335.   dz = 0
  336.   -- local target position
  337.   tx = 0
  338.   ty = 5
  339.   tz = 0
  340.   -- local current position
  341.   x = 0  -- curr x pos
  342.   y = 5  -- curr y pos
  343.   z = 0  -- curr z pos
  344.   -- real home position
  345.   hx = 0
  346.   hz = 0
  347.   -- real start move direction
  348.   mx = 1
  349.   mz = 0
  350.   -- other information for algorithms
  351.   distance = 0 -- blocks from internal point (0, 5, 0)
  352.   newTunnel = false -- flag to dig new tunnel in same chunk
  353.   newChunk = false -- flag to start dig new chunk
  354.   goHome = true -- flag to go to the home location
  355.   saveVersion = 1 -- version of save file
  356.   dataPath = "/data/miner.data"
  357.   tempDataPath = "/data/miner_temp.data"
  358. end
  359.  
  360. function saveImportantData()
  361.   local dataFile = fs.open(tempDataPath, "w")
  362.   dataFile.writeLine(tostring(saveVersion))
  363.   dataFile.writeLine(tostring(hx))
  364.   dataFile.writeLine(tostring(hz))
  365.   dataFile.writeLine(tostring(mx))
  366.   dataFile.writeLine(tostring(mz))
  367.   dataFile.writeLine(tostring(cx))
  368.   dataFile.close()
  369.   fs.delete(dataPath)
  370.   fs.move(tempDataPath, dataPath)
  371.   print("Important data saved")
  372. end
  373.  
  374. function adapt()
  375.   local data = {}
  376.   local dataFile
  377.   local str
  378.   if (not fs.exists(dataPath) and fs.exists(tempDataPath)) then
  379.     fs.move(tempDataPath, dataPath)
  380.   end
  381.  
  382.   if (fs.exists(dataPath)) then
  383.     fs.delete(tempDataPath)
  384.     dataFile = fs.open(dataPath, "r")
  385.    
  386.     -- Save File Version
  387.     str = dataFile.readLine()
  388.     data["version"] = str and tonumber(str) or nil
  389.     -- World coords of home
  390.     str = dataFile.readLine()
  391.     data["homeX"] = str and tonumber(str) or nil
  392.     str = dataFile.readLine()
  393.     data["homeZ"] = str and tonumber(str) or nil
  394.     -- World direction vector of work
  395.     str = dataFile.readLine()
  396.     data["dirX"] = str and tonumber(str) or nil
  397.     str = dataFile.readLine()
  398.     data["dirZ"] = str and tonumber(str) or nil
  399.     -- Local chunk start X
  400.     str = dataFile.readLine()
  401.     data["chunkX"] = str and tonumber(str) or nil
  402.     dataFile.close()
  403.   end
  404.    
  405.   if (fs.exists(dataPath) and data["version"] == saveVersion and data["chunkX"]) then
  406.     -- memorize info
  407.     hx = data["homeX"]
  408.     hz = data["homeZ"]
  409.     mx = data["dirX"]
  410.     mz = data["dirZ"]
  411.     cx = data["chunkX"]
  412.    
  413.     print("Home Coords: ", hx, hz)
  414.     print("Start Look Direction: ", mx, mz)
  415.     print("Local Chunk X Coord: ", cx)
  416.   else
  417.     io.write("Real Home X: ")
  418.     hx = tonumber(read())
  419.     io.write("Real Home Z: ")
  420.     hz = tonumber(read())
  421.     io.write("Real Start Direction X: ")
  422.     mx = tonumber(read())
  423.     io.write("Real Start Direction Z: ")
  424.     mz = tonumber(read())
  425.     io.write("Local Chunk Start X: ")
  426.     cx = tonumber(read())
  427.   end
  428.    
  429.   -- adapt coords
  430.   x = x - hx
  431.   z = z - hz
  432.  
  433.   -- rotate local coords
  434.   if (mx == -1) then
  435.     x, z = -x, -z
  436.     dx, dz = -dx, -dz
  437.   elseif (mz == 1) then
  438.     x, z = z, -x
  439.     dx, dz = dz, -dx
  440.   elseif (mz == -1) then
  441.     x, z = -z, x
  442.     dx, dz = -dz, dx
  443.   end
  444.  
  445.   -- update info
  446.   distance = x + z + y
  447.   tx = cx
  448.   ty = 5
  449.   tz = 0
  450. end
  451.  
  452. function locate()
  453.   if (peripheral.find("modem") and gps.locate()) then
  454.     x, y, z = gps.locate()
  455.    
  456.     local nx, ny, nz
  457.     local success, data, name
  458.    
  459.     -- avoiding the destruction of chest
  460.     success, data = turtle.inspect()
  461.     if (success) then
  462.       name = data.name
  463.       name = string.sub(name, string.find(name, ":") + 1)
  464.       if (string.find(name, "chest")) then
  465.         turtle.turnRight()
  466.         turtle.turnRight()
  467.       end
  468.     end
  469.    
  470.     --getting second location
  471.     turtle.dig()
  472.     turtle.forward()
  473.    
  474.     nx, ny, nz = gps.locate()
  475.     dx = nx - x
  476.     dz = nz - z
  477.    
  478.     turtle.turnRight()
  479.     turtle.turnRight()
  480.     turtle.dig()
  481.     turtle.forward()
  482.     turtle.turnRight()
  483.     turtle.turnRight()
  484.    
  485.     print("Real Coords: ", x, y, z)
  486.     print("Look Direction: ", dx, dz)
  487.   else
  488.     io.write("Real Current X: ")
  489.     x = tonumber(read())
  490.     io.write("Real Current Y: ")
  491.     y = tonumber(read())
  492.     io.write("Real Current Z: ")
  493.     z = tonumber(read())
  494.     io.write("Real Current Direction X: ")
  495.     dx = tonumber(read())
  496.     io.write("Real Current Direction Z: ")
  497.     dz = tonumber(read())
  498.   end
  499. end
  500.  
  501. function init()
  502.   print("Initialization starts")
  503.   reset()
  504.   sleep(0.05)
  505.   locate()
  506.   sleep(0.05)
  507.   adapt()
  508.   sleep(0.05)
  509.   saveImportantData()
  510.   sleep(0.05)
  511.   newChunk = true -- flag to start dig new chunk
  512.   goHome = true -- flag to go to the home location
  513.   print("Initialization ended, going home")
  514. end
  515.  
  516. init()
  517. while (true) do
  518.   if (goHome) then
  519.     home()
  520.   else
  521.     mine()
  522.   end
  523. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement