Advertisement
77wisher77

wLibMining

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