Advertisement
Guest User

mine.lua

a guest
Dec 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.26 KB | None | 0 0
  1. local x, y = ...
  2. local horiDir = false
  3. local vertDir = false
  4. local utils = dofile("utils.lua")
  5. local move = dofile("smartMove.lua")
  6. local front, back, left, right =
  7.     move.orientation.front,
  8.     move.orientation.back,
  9.     move.orientation.left,
  10.     move.orientation.right
  11. local blacklist = {
  12.     "minecraft:dirt",
  13.     "minecraft:stone",
  14.     "minecraft:cobblestone",
  15.     "minecraft:grass"
  16. }
  17. local chests = {
  18.     "minecraft:chest",
  19.     "bibliocraft:framedchest",
  20.     "ironchest:iron_chest"
  21. }
  22. local fuel = {
  23.     "minecraft:coal",
  24.     "minecraft:coal_block"
  25. }
  26.  
  27. local function unload()
  28.     log("Unloading content\n")
  29.     move.turn(back)
  30.  
  31.     local success, data = turtle.inspect()
  32.     local successUp, dataUp = turtle.inspectUp()
  33.    
  34.     if not success or not utils.isInList(chests, data.name) then
  35.         error("Cannot unload when not in front of a chest")
  36.     end
  37.     if not successUp or not utils.isInList(chests, dataUp.name) then
  38.         error("Cannot unload when not under a chest")
  39.     end
  40.     for i=1, 16 do
  41.         local item = turtle.getItemDetail(i)
  42.  
  43.         turtle.select(i)
  44.         if item and utils.isInList(fuel, item.name) then
  45.             turtle.dropUp(64)
  46.         else
  47.             turtle.drop(64)
  48.         end
  49.     end
  50. end
  51.  
  52. local function refuel()
  53.     log("Refueling\n")
  54.     unload()
  55.     if turtle.getFuelLevel() == turtle.getFuelLevel() then
  56.         error("Fuel limit already reached")
  57.     end
  58.     turtle.suckUp(64)
  59.     turtle.refuel(64)
  60. end
  61.  
  62. local function calculateMinimumFuelNeeded(x, y, z)
  63.     return (
  64.         math.abs(x) +
  65.         math.abs(y) +
  66.         math.abs(z)
  67.     ) * 2
  68. end
  69.  
  70. local function fuelToGoBack()
  71.     return calculateMinimumFuelNeeded(move.getLocation()) / 2
  72. end
  73.  
  74. local function saveState(i, j)
  75.     local file = io.open("state", "w")
  76.    
  77.     file:write(tostring(i).."\n")
  78.     file:write(tostring(j).."\n")
  79.     file:write(tostring(x).."\n")
  80.     file:write(tostring(y).."\n")
  81.     file:write(tostring(horiDir).."\n")
  82.     file:write(tostring(vertDir).."\n")
  83.     file:close()
  84. end
  85.  
  86. local function inventoryFull()
  87.     for i = 1, 16 do
  88.         if turtle.getItemCount(i) == 0 then
  89.             return false
  90.         end
  91.     end
  92.     log("Inventory full !\n")
  93.     return true
  94. end
  95.  
  96. local function unloadAndRefuel()
  97.     local x, y, z, o = move.getLocation()
  98.    
  99.     log("Unloading and refueling\n")
  100.     move.getBack()
  101.     move.turn(back)
  102.     unload()
  103.     while calculateMinimumFuelNeeded(x, y, z) > turtle.getFuelLevel() do
  104.         refuel()
  105.     end
  106.     move.goTo(x, y, z, o)
  107. end
  108.  
  109. function log(str)
  110.     io.write(str)
  111.    
  112.     local file = io.open("log.log", "a")
  113.    
  114.     if not file then return end
  115.     file:write(str)
  116.     file:close()
  117. end
  118.  
  119. local firsti, firstj = 1, 1
  120.  
  121. if fd.exists("state") then
  122.    
  123. else
  124.     x, y = tonumber(x), tonumber(y)
  125. end    
  126.  
  127. if not x or not y then
  128.     print("Usage: mine <sizex> <sizey>")
  129.     return
  130. end
  131.  
  132. if x % 4 ~= 0 then
  133.     print("Xsize must be a multiple of 4")
  134.     return
  135. end
  136.  
  137. if not fs.exists("state") then
  138.     move.loadLocation()
  139.     move.getBack()
  140.     unload()
  141. end
  142.  
  143. for j=1, y do
  144.     for i=1, x, 4 do
  145.         while not move.go(horiDir and back or front) do
  146.             move.vertGo(vertDir)
  147.         end
  148.         move.go(horiDir and back or front)
  149.         move.go(horiDir and back or front)
  150.         move.go(horiDir and back or front)
  151.  
  152.         local zz
  153.         while move.vertGo(vertDir) and (zz ~= 0 or not vertDir) do
  154.             for k=0, 3 do
  155.                 move.turn(k)
  156.                 local success, data = turtle.inspect()
  157.                 if success and not utils.isInList(
  158.                     blacklist,
  159.                     data.name
  160.                 ) then
  161.                     turtle.dig()
  162.                 end
  163.             end
  164.  
  165.             zz = move.getLocation()
  166.             if
  167.                 inventoryFull() or
  168.                 fuelToGoBack() > turtle.getFuelLevel()
  169.             then
  170.                 unloadAndRefuel()
  171.             end
  172.         end
  173.         vertDir = not vertDir
  174.     end
  175.     while not move.go(right) do
  176.         move.vertGo(vertDir)
  177.     end
  178.     move.go(horiDir and back or front)
  179.     move.go(horiDir and back or front)
  180.     horiDir = not horiDir
  181. end
  182.  
  183. log("end\n")
  184. fs.delete(state)
  185. move.getBack()
  186. unload()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement