77wisher77

wLibMining YamiDimStack

Jun 4th, 2021 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.51 KB | None | 0 0
  1. local PROGRAM_VERSION = "1.1"
  2. -- TODO: extract functions into here to be used
  3. -- apparently args can be dynamically generated ie `function(...)` would take any number of args and store them in a table
  4. --   you can also specifiy args like `function(arg1, arg2, ...) now we have 2 predefined args and any number after
  5.  
  6. -- checks if fuel is needed then consumes fuel if it is
  7. -- arg1 = #fuel to use, arg2 = minimum fuel before refuelling (upon checking)
  8. -- TODO: move into a more global robot lib rather than mining specific
  9. function checkFuel(fuelUsed, minFuel)
  10.   fuelUsed = tonumber(fuelUsed)
  11.   minFuel = tonumber(minFuel)
  12.   if(not fuelUsed) then fuelUsed = 3 end
  13.   if(not minFuel) then minFuel = 170 end
  14.   local fuelStack, isFuel
  15.   if(turtle.getFuelLevel() < minFuel) then
  16.     sortInventory()
  17.     local currentFuel = turtle.getFuelLevel()
  18.     print("Fuel is low, Current fuel:"..currentFuel)
  19.     print("Refuelling...")
  20.     for slot=1,wLibVariables.SLOT_COUNT,1 do
  21.       fuelStack = turtle.getItemDetail(slot)
  22.       for i, v in ipairs(FUEL_ITEM) do
  23.         if (fuelStack ~= nil) and (fuelStack["name"] == v) then
  24.           turtle.select(slot)
  25.           isFuel = true
  26.         end
  27.         if(isFuel == true) then break end
  28.       end
  29.       if(isFuel == true) then break end
  30.     end
  31.     turtle.refuel(fuelUsed)
  32.     currentFuel = turtle.getFuelLevel()
  33.     print("Refueled! Fuel is now:"..currentFuel)
  34.   else
  35.     print("Fuel levels are fine, resuming task")
  36.   end
  37. end
  38.  
  39. -- Inventory sorting program to keep stacks together to save space
  40. -- TODO: move into a more global robot lib rather than mining specific
  41. function sortInventory()
  42.   local stackOne, stackTwo
  43.   print("Organizing Inventory")
  44.   for slot=1,wLibVariables.SLOT_COUNT-1,1 do
  45.     stackOne = turtle.getItemDetail(slot)
  46.     if(stackOne~=nil) and (turtle.getItemSpace(slot) > 0) then
  47.       for slotTwo=slot+1,wLibVariables.SLOT_COUNT,1 do
  48.           turtle.select(slotTwo)
  49.           stackTwo = turtle.getItemDetail(slotTwo)
  50.           if(stackTwo~=nil) then
  51.             if(stackOne["name"] == stackTwo["name"]) and (stackOne["damage"] == stackTwo["damage"]) then
  52.               local stackSpace = turtle.getItemSpace(slot)
  53.               if (stackSpace ~= 0) then
  54.                 if (stackSpace <= stackTwo["count"]) then
  55.                   turtle.transferTo(slot)
  56.                 else
  57.                   turtle.transferTo(slot, stackSpace)
  58.                 end
  59.               end
  60.             end
  61.           end
  62.       end
  63.     end
  64.   end
  65.   print("Organizing Complete!")
  66. end
  67.  
  68. -- Cleans out inventory of items not in tables
  69. --   aimed at mining turtles
  70. function dropItems()
  71.   print("Dropping Waste Inventory")
  72.   for slot=1,wLibVariables.SLOT_COUNT,1 do
  73.     local item = turtle.getItemDetail(slot)
  74.     local keepItem = false
  75.     if(item~=nil) then
  76.       for keepItemIndex=1,#wLibVariables.MINING_KEEP_ITEMS,1 do
  77.         if(item["name"] == wLibVariables.MINING_KEEP_ITEMS[keepItemIndex]) then
  78.           keepItem = true
  79.           break
  80.         end
  81.       end
  82.       for fuelItemIndex=1,#wLibVariables.FUEL_ITEM,1 do
  83.         if(item["name"] == wLibVariables.FUEL_ITEM[fuelItemIndex]) then
  84.           keepItem = true
  85.           break
  86.         end
  87.       end
  88.       for exportInventoriesIndex=1,#wLibVariables.EXPORT_INVENTORIES,1 do
  89.         if(item["name"] == wLibVariables.EXPORT_INVENTORIES[exportInventoriesIndex]) then
  90.           keepItem = true
  91.           break
  92.         end
  93.       end
  94.       if(not keepItem) then
  95.         turtle.select(slot)
  96.         turtle.dropDown()
  97.       end
  98.     end
  99.   end
  100.   print("Waste Dropping Completed")
  101.   sortInventory()
  102. end
  103.  
  104. -- to dig, loop stops after 1 round as no block is detected infront!
  105. --  for strip mines
  106. function detectAndDigStrip()
  107.   --while turtle detects a block above OR below OR in front, that isn't pollution //logic
  108.     local isMine = true
  109.     local isMineUp = true
  110.     local isMineDown = true
  111.  
  112.     while (((isMine == true) or (isMineUp == true) or (isMineDown == true))) do
  113.  
  114.       local var1, block = turtle.inspect()
  115.       local var2, blockUp = turtle.inspectUp()
  116.       local var3, blockDown = turtle.inspectDown()
  117.  
  118.       for unbreakableIndex=1, #wLibVariables.UNBREAKABLE_BLOCKS, 1 do
  119.         if block.name == wLibVariables.UNBREAKABLE_BLOCKS[unbreakableIndex] then
  120.           isMine = false
  121.         end
  122.         if blockUp.name == wLibVariables.UNBREAKABLE_BLOCKS[unbreakableIndex] then
  123.           isMineUp = false
  124.         end
  125.         if blockDown.name == wLibVariables.UNBREAKABLE_BLOCKS[unbreakableIndex] then
  126.           isMineDown = false
  127.         end
  128.       end
  129.  
  130.       if ((isMine == true) and (turtle.detect())) then
  131.         turtle.dig()
  132.       elseif (not turtle.detect()) then
  133.         isMine = false
  134.       end
  135.       if ((isMineUp == true) and (turtle.detectUp())) then
  136.         turtle.digUp()
  137.       elseif (not turtle.detectUp()) then
  138.         isMineUp = false
  139.       end
  140.       if ((isMineDown == true) and (turtle.detectDown())) then
  141.         turtle.digDown()
  142.       elseif (not turtle.detectDown()) then
  143.         isMineDown = false
  144.       end
  145.     end
  146. end
  147.  
  148. -- Checks if an EXPORT_INVENTORY is infron then Deposit items into chest
  149. -- adjust so it returns booleans instead of changing the cycles so that start() handles setting the cycle instead
  150. -- TODO: move into a more global robot lib rather than mining specific
  151. -- TODO: make a check to see if there is space spaces and/or spare stack space to put items in
  152. function depositInventory()
  153.   local isBlock, block = turtle.inspect()
  154.   local isExportInventory
  155.   print("Attempting to deposit inventory")
  156.   if (isBlock) then
  157.     for i, v in ipairs(wLibVariables.EXPORT_INVENTORIES) do
  158.       if (block["name"] == v) then
  159.         print("Depositing into "..block["name"])
  160.         isExportInventory = true
  161.         -- exportInventoryMining()
  162.         return isExportInventory -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
  163.       else
  164.         if(i == #wLibVariables.EXPORT_INVENTORIES) and (block["name"] ~= v) then
  165.           print(block["name"].." is not a valid block!")
  166.           print("Stopping stripMine")
  167.           return false -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
  168.         end
  169.       end
  170.       if(isExportInventory == true) then break end
  171.     end
  172.   else
  173.     print("Error: No block detected")
  174.     print("STOPPING stripMine!!!")
  175.     return false -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
  176.   end
  177. end
  178.  
  179. -- Places down a valid inventory item from the EXPORT_INVENTORIES table, in future make a placeInventoryEnder() function which uses ender chests, that function can be used while mining rather than at end of cycles
  180. -- TODO: move into a more global robot lib
  181. -- TODO: improve logic so turtle can deploy an ender chest if it has one AND check to see if it is full, if so then recollect it without transferring, then empty into another inventory if available
  182. --  will probably handle this by sending a return which should signal to use placeInventoryEnder()
  183. function placeInventoryLocal()
  184.   local inventoryItem, isInventory
  185.   for slot=1,wLibVariables.SLOT_COUNT,1 do
  186.     inventoryItem = turtle.getItemDetail(slot)
  187.     for i, v in ipairs(wLibVariables.EXPORT_INVENTORIES) do
  188.       if (inventoryItem ~= nil) and (inventoryItem["name"] == v) then
  189.         turtle.select(slot)
  190.         isInventory = true
  191.         while(turtle.detect()) do
  192.           turtle.dig()
  193.         end
  194.         print("Placing Inventory: "..inventoryItem["name"])
  195.         turtle.place()
  196.       end
  197.       if(isInventory == true) then break end
  198.     end
  199.     if(isInventory == true) then break end
  200.   end
  201. end
  202.  
  203. -- Checks each slot of the turtle to see if the item should be kept or not
  204. --   to be used in the middle of mining, separate function should be used to export into an actual base/ME system
  205. --    such a function would be simply called exportInventory(), or exportInventoryAll()
  206. -- needs logic to only keep a certain amount of fuel/inventories on hand (inventories could be set by cycles)
  207. function exportInventoryMining()
  208.   for slot=1,wLibVariables.SLOT_COUNT,1 do
  209.     local item = turtle.getItemDetail(slot)
  210.     local keepItem = false
  211.     if(item~=nil) then
  212.       for fuelItemIndex=1,#wLibVariables.FUEL_ITEM,1 do
  213.         if(item["name"] == wLibVariables.FUEL_ITEM[fuelItemIndex]) then
  214.           keepItem = true
  215.           break
  216.         end
  217.       end
  218.       for exportInventoriesIndex=1,#wLibVariables.EXPORT_INVENTORIES,1 do
  219.         if(item["name"] == wLibVariables.EXPORT_INVENTORIES[exportInventoriesIndex]) then
  220.           keepItem = true
  221.           break
  222.         end
  223.       end
  224.       if(not keepItem) then
  225.         turtle.select(slot)
  226.         turtle.drop()
  227.       end
  228.     end
  229.   end
  230.   print("Finished exporting inventory")
  231. end
  232.  
  233. -- check for # of empty slots
  234. --  returns true if empty slots >= minEmpty
  235. function emptySlots(minEmpty)
  236.   minEmpty = tonumber(minEmpty)
  237.   local slots = 16
  238.   for i=1,wLibVariables.SLOT_COUNT,1 do
  239.       if (turtle.getItemCount(i) > 0) then
  240.         slots = slots - 1
  241.       end
  242.   end
  243.   if (slots < minEmpty) then
  244.     print("Inventory Full")
  245.     return false
  246.   end
  247.   if (slots >= minEmpty) then
  248.       return true
  249.   end
  250. end
  251.  
  252.  
  253. return {
  254.   checkFuel = checkFuel,
  255.   sortInventory = sortInventory,
  256.   dropItems = dropItems,
  257.   detectAndDigStrip = detectAndDigStrip,
  258.   depositInventory = depositInventory,
  259.   placeInventoryLocal = placeInventoryLocal,
  260.   exportInventoryMining = exportInventoryMining,
  261.   emptySlots = emptySlots
  262. }
Add Comment
Please, Sign In to add comment