77wisher77

wLibMine test ender

Jun 17th, 2021 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.86 KB | None | 0 0
  1. local PROGRAM_VERSION = "1.8"
  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. -- TODO: new refuel logic, consume whats needed, maybe based on distance, have checks at startup to ensure enough fuel is always present, otherwise only refuel as needed
  10. -- TODO: use `cc.inventory` module to check if inventories arent full, could also use this to pull fuel from enderchest if needed
  11. -- TODO: when mining check if any inventory blocks are infront (loot chests etc.) and then run a function to search them for loot table items that we want to extract
  12. -- TODO: add configuration to stripmine first run logic to prompt user for which chests are import and export OR none
  13. -- TODO: maneuver around unbreakable blocks like bedrock
  14.  
  15. -- searches inventory for a fuel item then consumes
  16. -- returns true/false if consumed, fuelRemaining
  17. function findAndRefuel()
  18.   local fuelStack, isFuel
  19.  
  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.  
  32.   if (isFuel) then
  33.     if (fuelStack["count"] >= 3) then
  34.       turtle.refuel(3)
  35.     else
  36.       turtle.refuel(1)
  37.     end
  38.   elseif (isFuel == nil) then
  39.     isFuel = false
  40.   end
  41.  
  42.   local currentFuel = turtle.getFuelLevel()
  43.   return isFuel, currentFuel
  44. end
  45.  
  46. function checkFuel(isImportChest, importChestSlot)
  47.   isImportChest = isImportChest or false
  48.   importChestSlot = importChestSlot or nil
  49.   local enderChestType = 0 -- import chest
  50.   local isFuel
  51.   if(turtle.getFuelLevel() == 0) then
  52.     sortInventory()
  53.     local currentFuel = turtle.getFuelLevel()
  54.     print("Fuel is low, Current fuel:"..currentFuel)
  55.     print("Refuelling...")
  56.  
  57.     isFuel, currentFuel = findAndRefuel()
  58.  
  59.     if (not isFuel) then
  60.       if isImportChest then
  61.         isImportChest, importChestSlot = importFuelEnder(isImportChest, importChestSlot)
  62.         currentFuel = turtle.getFuelLevel()
  63.       elseif (not isImportChest) then
  64.         print("Out of fuel!")
  65.         --TODO: pause this until turlte inventory updated, then run this function again to refuel. That way progress can be kept on mining strip, will work for persistent versions aswell as normal ones
  66.         -- this way a player or turtle can give the turtle fuel to keep it going
  67.         print("No import chest, waiting for user to fix fuel and restart")
  68.         os.pullEvent("key_up")
  69.         os.reboot()
  70.       end
  71.     end
  72.  
  73.     print("Refueled! Fuel is now:"..currentFuel)
  74.   else
  75.     -- print("Fuel levels are fine, resuming task")
  76.   end
  77.   return isImportChest, importChestSlot, enderChestType
  78. end
  79.  
  80. -- Inventory sorting program to keep stacks together to save space
  81. -- TODO: move into a more global robot lib rather than mining specific - for the old version that didnt do ender chest checks
  82. -- TODO: newer versions don't use "damage" need to update to handle that
  83. function sortInventory()
  84.   local stackOne, stackTwo
  85.   print("Organizing Inventory")
  86.   for slot=1,wLibVariables.SLOT_COUNT-1,1 do
  87.     stackOne = turtle.getItemDetail(slot)
  88.     if(stackOne~=nil) and (turtle.getItemSpace(slot) > 0) then
  89.       for slotTwo=slot+1,wLibVariables.SLOT_COUNT,1 do
  90.           turtle.select(slotTwo)
  91.           stackTwo = turtle.getItemDetail(slotTwo)
  92.           if(stackTwo~=nil) then
  93.             if(stackOne["name"] == stackTwo["name"]) and (stackOne["damage"] == stackTwo["damage"]) then
  94.               local reservedItemSlot = false -- ensure not breaking memory of items stored in reserved slots
  95.               if ( ((slot == 15)) or ((slotTwo == 15) or (slotTwo == 16)) ) then --reserved slots
  96.                 for enderInventoryIndex=1,#wLibVariables.ENDER_INVENTORIES,1 do
  97.                   if (stackOne["name"] == wLibVariables.ENDER_INVENTORIES[enderInventoryIndex]) then
  98.                     reservedItemSlot = true -- reserved items, currently only enderInventories, should expand too chests aswell?
  99.                   end
  100.                 end
  101.               end
  102.               if (not reservedItemSlot) then --if not reserved item, then stack together
  103.                 local stackSpace = turtle.getItemSpace(slot)
  104.                 if (stackSpace ~= 0) then
  105.                   if (stackSpace <= stackTwo["count"]) then
  106.                     turtle.transferTo(slot)
  107.                   else
  108.                     turtle.transferTo(slot, stackSpace)
  109.                   end
  110.                 end
  111.               end
  112.             end
  113.           end
  114.       end
  115.     end
  116.   end
  117.   print("Organizing Complete!")
  118. end
  119.  
  120. -- Cleans out inventory of items not in tables
  121. --   aimed at mining turtles
  122. function dropItems()
  123.   print("Dropping Waste Inventory")
  124.   for slot=1,wLibVariables.SLOT_COUNT,1 do
  125.     local item = turtle.getItemDetail(slot)
  126.     local keepItem = false
  127.     if(item~=nil) then
  128.       for keepItemIndex=1,#wLibVariables.MINING_KEEP_ITEMS,1 do
  129.         if(item["name"] == wLibVariables.MINING_KEEP_ITEMS[keepItemIndex]) then
  130.           keepItem = true
  131.           break
  132.         end
  133.       end
  134.       for fuelItemIndex=1,#wLibVariables.FUEL_ITEM,1 do
  135.         if(item["name"] == wLibVariables.FUEL_ITEM[fuelItemIndex]) then
  136.           keepItem = true
  137.           break
  138.         end
  139.       end
  140.       for exportInventoriesIndex=1,#wLibVariables.EXPORT_INVENTORIES,1 do
  141.         if(item["name"] == wLibVariables.EXPORT_INVENTORIES[exportInventoriesIndex]) then
  142.           keepItem = true
  143.           break
  144.         end
  145.       end
  146.       if(not keepItem) then
  147.         turtle.select(slot)
  148.         turtle.dropDown()
  149.       end
  150.     end
  151.   end
  152.   print("Waste Dropping Completed")
  153.   sortInventory()
  154. end
  155.  
  156. -- Custom detect that ignores pollution, achieves similar result as how turtle.detect() ignores water/lava
  157. -- returns true if any non-pollution block detected
  158. function detectIgnorePollution(direction)
  159.   direction = tonumber(direction) or 0 -- 0=forward, 1=up, 2=down
  160.   local isDetect
  161.   --TODO: validate direction is valid (0,1,2)
  162.  
  163.   -- Detect if block exists
  164.   if (direction == 0) then
  165.     isDetect = turtle.detect()
  166.   elseif (direction == 1) then
  167.     isDetect = turtle.detectUp()
  168.   elseif (direction == 2) then
  169.     isDetect = turtle.detectDown()
  170.   end
  171.  
  172.   -- If block exists then filter out pollution
  173.   if (isDetect) then
  174.     local var, block
  175.     if (direction == 0) then
  176.       var, block = turtle.inspect()
  177.     elseif (direction == 1) then
  178.       var, block = turtle.inspectUp()
  179.     elseif (direction == 2) then
  180.       var, block = turtle.inspectDown()
  181.     end
  182.  
  183.     for pollutionBlocksIndex=1, #wLibVariables.POLLUTION_BLOCKS, 1 do
  184.       if (block.name == wLibVariables.POLLUTION_BLOCKS[pollutionBlocksIndex]) then
  185.         isDetect = false -- if pollution then 'nothing' detected since it acts like air mostly
  186.       end
  187.     end
  188.   end
  189.  
  190.   return isDetect
  191. end
  192.  
  193. -- to dig, loop stops after 1 round as no block is detected infront!
  194. --  for strip mines
  195. function detectAndDigStrip()
  196.   --while turtle detects a block above OR below OR in front, that isn't pollution //logic
  197.     local isMine = true
  198.     local isMineUp = true
  199.     local isMineDown = true
  200.  
  201.     while (((isMine == true) or (isMineUp == true) or (isMineDown == true))) do
  202.  
  203.       local var1, block = turtle.inspect()
  204.       local var2, blockUp = turtle.inspectUp()
  205.       local var3, blockDown = turtle.inspectDown()
  206.  
  207.       for unbreakableIndex=1, #wLibVariables.UNBREAKABLE_BLOCKS, 1 do
  208.         if block.name == wLibVariables.UNBREAKABLE_BLOCKS[unbreakableIndex] then
  209.           isMine = false
  210.         end
  211.         if blockUp.name == wLibVariables.UNBREAKABLE_BLOCKS[unbreakableIndex] then
  212.           isMineUp = false
  213.         end
  214.         if blockDown.name == wLibVariables.UNBREAKABLE_BLOCKS[unbreakableIndex] then
  215.           isMineDown = false
  216.         end
  217.       end
  218.  
  219.       if ((isMine == true) and (turtle.detect())) then
  220.         turtle.dig()
  221.       elseif (not turtle.detect()) then
  222.         isMine = false
  223.       end
  224.       if ((isMineUp == true) and (turtle.detectUp())) then
  225.         turtle.digUp()
  226.       elseif (not turtle.detectUp()) then
  227.         isMineUp = false
  228.       end
  229.       if ((isMineDown == true) and (turtle.detectDown())) then
  230.         turtle.digDown()
  231.       elseif (not turtle.detectDown()) then
  232.         isMineDown = false
  233.       end
  234.     end
  235. end
  236.  
  237. function detectAndDigUp()
  238.   local isMine = true
  239.   while isMine do
  240.     local var, block = turtle.inspectUp()
  241.  
  242.     for unbreakableIndex=1, #wLibVariables.UNBREAKABLE_BLOCKS, 1 do
  243.       if block.name == wLibVariables.UNBREAKABLE_BLOCKS[unbreakableIndex] then
  244.         isMine = false
  245.       end
  246.     end
  247.  
  248.     if ((isMine == true) and (turtle.detectUp())) then
  249.       turtle.digUp()
  250.     elseif (not turtle.detectUp()) then
  251.       isMine = false
  252.     end
  253.  
  254.   end
  255. end
  256.  
  257.  
  258. -- Checks if an EXPORT_INVENTORY is infron then Deposit items into chest
  259. -- adjust so it returns booleans instead of changing the cycles so that start() handles setting the cycle instead
  260. -- TODO: move into a more global robot lib rather than mining specific
  261. -- TODO: make a check to see if there is space spaces and/or spare stack space to put items in
  262. function depositInventory()
  263.   local isBlock, block = turtle.inspect()
  264.   local isExportInventory
  265.   print("Attempting to deposit inventory")
  266.   if (isBlock) then
  267.     for i, v in ipairs(wLibVariables.EXPORT_INVENTORIES) do
  268.       if (block["name"] == v) then
  269.         print("Depositing into "..block["name"])
  270.         isExportInventory = true
  271.         -- exportInventoryMining()
  272.         return isExportInventory -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
  273.       else
  274.         if(i == #wLibVariables.EXPORT_INVENTORIES) and (block["name"] ~= v) then
  275.           print(block["name"].." is not a valid block!")
  276.           print("Stopping stripMine")
  277.           return false -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
  278.         end
  279.       end
  280.       if(isExportInventory == true) then break end
  281.     end
  282.   else
  283.     print("Error: No block detected")
  284.     print("STOPPING stripMine!!!")
  285.     return false -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
  286.   end
  287. end
  288.  
  289. -- 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
  290. -- TODO: move into a more global robot lib
  291. -- TODO: use reserved slots, saves time searching for inventories, allows removing excess inventories that are aquired
  292. function placeInventoryLocal()
  293.   local inventoryItem, isInventory
  294.   for slot=1,wLibVariables.SLOT_COUNT,1 do
  295.     inventoryItem = turtle.getItemDetail(slot)
  296.     for i, v in ipairs(wLibVariables.EXPORT_INVENTORIES) do
  297.       if (inventoryItem ~= nil) and (inventoryItem["name"] == v) then
  298.         turtle.select(slot)
  299.         isInventory = true
  300.         -- TODO: add logic to check if there is already an inventory block there
  301.         while(turtle.detect()) do
  302.           turtle.dig()
  303.         end
  304.         print("Placing Inventory: "..inventoryItem["name"])
  305.         turtle.place()
  306.       end
  307.       if(isInventory == true) then break end
  308.     end
  309.     if(isInventory == true) then break end
  310.   end
  311. end
  312.  
  313. -- Checks each slot of the turtle to see if the item should be kept or not
  314. --   to be used in the middle of mining, separate function should be used to export into an actual base/ME system
  315. --    such a function would be simply called exportInventory(), or exportInventoryAll()
  316. -- TODO: needs logic to only keep a certain amount of inventories on hand (inventories could be set by cycles) (similar to fuel)
  317. function exportInventoryMining(direction)
  318.   direction = tonumber(direction) or 0
  319.   local isFirstFuelstack = true
  320.   for slot=1,wLibVariables.SLOT_COUNT,1 do
  321.     local item = turtle.getItemDetail(slot)
  322.     local keepItem = false
  323.     if (item ~= nil) then
  324.       if((isFirstFuelstack)) then -- exports any excess fuel, keeps half a stack so inventory slot isn't wasted on stackable fuels
  325.         for fuelItemIndex=1,#wLibVariables.FUEL_ITEM,1 do
  326.           if(item["name"] == wLibVariables.FUEL_ITEM[fuelItemIndex]) then
  327.             local firstFuelStacksize = item["count"]
  328.             if (firstFuelStacksize > 32) then
  329.               local exportSize = firstFuelStacksize - 32
  330.               turtle.select(slot)
  331.               if (not dropDirection(direction, exportSize)) then
  332.                 print("Export inventory full or missing")
  333.                 return false
  334.               end
  335.             end
  336.             keepItem = true
  337.             isFirstFuelstack = false
  338.             break
  339.           end
  340.         end
  341.       end
  342.       for exportInventoriesIndex=1,#wLibVariables.EXPORT_INVENTORIES,1 do
  343.         if(item["name"] == wLibVariables.EXPORT_INVENTORIES[exportInventoriesIndex]) then
  344.           --TODO: update stripMine to only store any/all inventories in particular slots, then we can export them if not there (e.g. unneeded extras obtained somehow)
  345.           if (slot < 15) then -- if ender inventory is found in wrong slot export it
  346.             local isEnder = false
  347.             for enderInventoriesIndex=1,#wLibVariables.ENDER_INVENTORIES,1 do
  348.               if (item["name"] == wLibVariables.ENDER_INVENTORIES[enderInventoriesIndex]) then
  349.                 isEnder = true
  350.                 break
  351.               end
  352.             end
  353.             if not isEnder then --if inventory is ender then keepItem=false (default)
  354.               keepItem = true -- if NOT ender inventory, we already know it is an inventory so keep it
  355.             end
  356.             break -- need to stop the loop and move onto next slot, of it was an ender inventory in the wrong slot it drops the inventory
  357.           else -- if slot==15or16 and IS an inventory (any)
  358.             keepItem = true
  359.             break
  360.           end
  361.         end
  362.       end
  363.       if(not keepItem) then
  364.         turtle.select(slot)
  365.         if (not dropDirection(direction)) then
  366.           print("Export inventory full or missing")
  367.           return false
  368.         end
  369.       end
  370.     end
  371.   end
  372.   print("Finished exporting inventory")
  373.   return true
  374. end
  375.  
  376.  
  377. -- check for # of empty slots
  378. --  returns true if empty slots >= minEmpty
  379. function emptySlots(minEmpty)
  380.   minEmpty = tonumber(minEmpty)
  381.   local slots = 16
  382.   for i=1,wLibVariables.SLOT_COUNT,1 do
  383.       if (turtle.getItemCount(i) > 0) then
  384.         slots = slots - 1
  385.       end
  386.   end
  387.   if (slots < minEmpty) then
  388.     print("Inventory Full")
  389.     return false
  390.   end
  391.   if (slots >= minEmpty) then
  392.       return true
  393.   end
  394. end
  395.  
  396. -- Digs block infront of turtle to a specific slot in inventory
  397. -- returns whether successful and slot number
  398. function digToSlot(direction, slot)
  399.   -- 0/nil = forward, 1 = up, 2 = down
  400.   direction = tonumber(direction) or nil
  401.   slot = tonumber(slot) or nil
  402.   local isItem, itemSlot, tempSlot = false, nil, nil
  403.  
  404.   --TODO: better logic that can try stacking like items together, or run sortinventory()?
  405.   if (slot ~= nil) then
  406.     turtle.select(slot)
  407.     if (turtle.getItemCount(slot) > 0) then
  408.       for i=1,wLibVariables.SLOT_COUNT-2,1 do --don't attempt to transfer into slots 15/16 as they are reserved
  409.         if (i ~= slot) then
  410.           if (turtle.getItemCount(i) == 0) then
  411.             if (turtle.transferTo(i)) then
  412.               tempSlot = slot
  413.               break -- if successful in transferring items then stop looping
  414.             end
  415.           end
  416.         end
  417.       end
  418.     elseif (turtle.getItemCount(slot) == 0) then
  419.       tempSlot = slot
  420.     end
  421.   elseif (slot == nil) then
  422.     for i=1,wLibVariables.SLOT_COUNT-2,1 do
  423.       local item = turtle.getItemDetail(i)
  424.       if (item == nil) then
  425.         turtle.select(i)
  426.         tempSlot = i
  427.         break
  428.       end
  429.     end
  430.   end
  431.   if ((direction == 0) or (direction == nil)) then
  432.     if (turtle.dig()) then
  433.       itemSlot = tempSlot
  434.       isItem = true
  435.     end
  436.   elseif (direction == 1) then
  437.     if (turtle.digUp()) then
  438.       itemSlot = tempSlot
  439.       isItem = true
  440.     end
  441.   elseif (direction == 2) then
  442.     if (turtle.digDown()) then
  443.       itemSlot = tempSlot
  444.       isItem = true
  445.     end
  446.   end
  447.  
  448.   return isItem, itemSlot
  449. end
  450.  
  451. --suck from direction, inputs, direction, stacksize
  452. -- returns true if successful, false if failed
  453. function suckDirection(direction, size)
  454.   direction = tonumber(direction) or 0
  455.   size = tonumber(size) or nil
  456.   local isSuck
  457.  
  458.   if (direction == 0) then
  459.     isSuck = turtle.suck(size)
  460.   elseif (direction == 1) then
  461.     isSuck = turtle.suckUp(size)
  462.   elseif (direction == 2) then
  463.     isSuck = turtle.suckDown(size)
  464.   end
  465.   return isSuck
  466. end
  467.  
  468. --Drop to direction, inputs, direction, stacksize
  469. -- returns true if successful, false if failed
  470. function dropDirection(direction, size)
  471.   direction = tonumber(direction) or 0
  472.   size = tonumber(size) or nil
  473.   local isDrop
  474.  
  475.   if (direction == 0) then
  476.     isDrop = turtle.drop(size)
  477.   elseif (direction == 1) then
  478.     isDrop = turtle.dropUp(size)
  479.   elseif (direction == 2) then
  480.     isDrop = turtle.dropDown(size)
  481.   end
  482.   return isDrop
  483. end
  484.  
  485. -- tries to place a block from an inputed slot, attempts to do so from all 6 faces from the turtle
  486. -- returns is the block is placed, and number of turns the turtle done (for re-orienting to original direction)
  487. -- TODO: logic to check block being placed isn't already placed? (maybe this should be more of a logic flow control in stripMine)
  488. function selectAndPlaceOrient(slot)
  489.   slot = slot or nil
  490.   local isItem, itemSlot
  491.   local item = turtle.getItemDetail(slot)
  492.   if (item ~= nil) then
  493.     --TODO: logic to error safely if no item is provided, will also help ensure returns at end of funtion are correct
  494.     isItem, itemSlot = true, slot
  495.   end
  496.   local turns = 0
  497.   local direction = 0 -- 0=forward, 1=up, 2=down, direction block is placed
  498.   if ((slot ~= nil) and (item ~= nil)) then
  499.     local isValid = false
  500.  
  501.     --TODO: custom detect function that ignores pollution
  502.     -- find suitable position to place block
  503.     for i=1,4,1 do
  504.       if (not detectIgnorePollution(0)) then
  505.         isValid = true
  506.         break
  507.       end
  508.       if (i==1) then -- if not turned yet then check if block above/below is suitable, if so break loop
  509.         if (not detectIgnorePollution(1)) then
  510.           isValid = true
  511.           direction = 1
  512.           break
  513.         elseif (not detectIgnorePollution(2)) then
  514.           isValid = true
  515.           direction = 2
  516.           break
  517.         end
  518.       end
  519.       if (detectIgnorePollution(0)) then
  520.         turtle.turnRight()
  521.         turns = turns + 1
  522.       end
  523.     end
  524.  
  525.     if (not isValid) then
  526.       turns = 0
  527.       -- ensure space in inventory, incase we mine something good
  528.       if (not emptySlots(3)) then
  529.         dropItems() -- no way to tell is exportChest present, so have to drop items
  530.       end
  531.  
  532.       for i=1,4,1 do
  533.         if (detectIgnorePollution(0)) then
  534.           detectAndDigStrip()
  535.         end
  536.         if (i==1) then -- if not turned yet then check if block above/below is suitable, if so break loop
  537.           if (not detectIgnorePollution(1)) then
  538.             direction = 1
  539.             break
  540.           elseif (not detectIgnorePollution(2)) then
  541.             direction = 2
  542.             break
  543.           end
  544.         end
  545.         if (detectIgnorePollution(0)) then
  546.           turtle.turnRight()
  547.           turns = turns + 1
  548.         elseif (not detectIgnorePollution(0)) then --if suitable to place block then break loop
  549.           break
  550.         end
  551.       end
  552.  
  553.       if (not emptySlots(3)) then --Incase many items were obtained while finding a spot for chest (e.g. breaking an instanced chest could fill inventory in one go)
  554.         dropItems()
  555.       end
  556.     end
  557.  
  558.  
  559.     -- place block in first direction which was available
  560.     turtle.select(slot)
  561.     if (direction == 0) then
  562.       if (turtle.place()) then
  563.         isItem = false
  564.         itemSlot = nil
  565.       end
  566.     elseif (direction == 1) then
  567.       if (turtle.placeUp()) then
  568.         isItem = false
  569.         itemSlot = nil
  570.       end
  571.     elseif (direction == 2) then
  572.       if (turtle.placeDown()) then
  573.         isItem = false
  574.         itemSlot = nil
  575.       end
  576.     end
  577.  
  578.     -- if block hasnt been placed then error
  579.     if (isItem) then
  580.       print("error placing chest. No valid positions")
  581.       os.pullEvent("key_up")
  582.     end
  583.  
  584.   end
  585.   return isItem, itemSlot, turns, direction
  586. end
  587.  
  588. --input number of turns, function takes that and makes turtle face original direction
  589. -- assumes turtle turned Right to reach current position, which is how selectAndPlaceOrient() works
  590. function reOrientTurns(turns)
  591.   turns = turns or nil
  592.   if (turns ~= nil) then
  593.     if (turns > 0) then
  594.       if (turns == 3) then
  595.         turtle.turnRight()
  596.       elseif (turns < 3) then
  597.         for i=1,turns,1 do
  598.           turtle.turnLeft()
  599.         end
  600.       end
  601.     end
  602.   end
  603. end
  604.  
  605. --TODO: use depositInventory() function to ensure inventory was placed infront
  606. --  inputs, isImport - import chest exists in inventory, importChest - slot number for importChest
  607. --  returns, isImport - does import chest exist in inventory, importChest - slot number for importChest
  608. --TODO: check inventory is actually in the slot before trying anything, if not then return false, nil and print a msg informing user
  609. function importFuelEnder(isImport, importChest)
  610.   isImport = isImport or false
  611.   importChest = importChest or nil
  612.   local enderChestType = 0
  613.   --TODO: check item in slot is a valid ender inventory, should solve related problems with helper functions
  614.  
  615.   if (isImport) then
  616.     if (importChest ~= nil) then
  617.  
  618.       local isImported = false
  619.       local turns, direction
  620.       local digSlot = importChest
  621.  
  622.  
  623.       -- place inventory
  624.       isImport, importChest, turns, direction = selectAndPlaceOrient(importChest)
  625.  
  626.       --TODO: verify block attempting to import from actually is an inventory
  627.       -- TODO: for logic allowing chests from the same mod via reserving slots need to ensure fuel imported is imported to a slot that is NOT the importChest slot
  628.       -- import fuel
  629.       local importSlot
  630.       for i=1,wLibVariables.SLOT_COUNT,1 do
  631.         local item = turtle.getItemDetail(i)
  632.         if (item == nil) then
  633.           turtle.select(i)
  634.           importSlot = i
  635.           break
  636.         end
  637.       end
  638.       while (not isImported) do --tries to import fuel until successful, waiting 5s after each attempt (gives time for system to refill chest)
  639.         -- isImported = turtle.suck(9)
  640.         if (suckDirection(direction, 9)) then
  641.           local importedItem = turtle.getItemDetail(importSlot)
  642.           for i, v in ipairs(FUEL_ITEM) do
  643.             if ((importedItem ~= nil) and (importedItem["name"] == v)) then
  644.               isImported = true
  645.             end
  646.           end
  647.           if ((importedItem ~= nil) and (not isImported)) then --if not fuel return stack to inventory
  648.             local isReturned = false
  649.             while (not isReturned) do -- attempts to return stack until successful, sleeps between failures
  650.               isReturned = dropDirection(direction)
  651.               if (not isReturned) then
  652.                 sleep(2)
  653.               end
  654.             end
  655.           end
  656.         end
  657.         if (not isImported) then
  658.           sleep(5)
  659.         end
  660.       end
  661.  
  662.       -- retrieve inventory, update variables to return at end
  663.       isImport, importChest = digToSlot(direction, digSlot)
  664.  
  665.       -- re-orientate to original direction
  666.       reOrientTurns(turns)
  667.  
  668.       -- locate imported fuel and consume some
  669.       if isImported then
  670.         local isFuel = findAndRefuel()
  671.         if (not isFuel) then
  672.           print("Error importing fuel from ender inventory")
  673.         end
  674.       -- elseif (not isImported) then -- can this code ever even be reached? The while loop should prevent this...
  675.       --   print("failed to import fuel")
  676.       --   os.pullEvent("key_up")
  677.       end
  678.  
  679.     end
  680.   end
  681.   return isImport, importChest, enderChestType
  682. end
  683.  
  684. --TODO: use depositInventory() function to ensure inventory was placed infront
  685. --TODO: check inventory is actually in the slot before trying anything, if not then return false, nil and print a msg informing user
  686. function exportInventoryEnder(isExport, exportChest)
  687.   isExport = isExport or false
  688.   exportChest = exportChest or nil
  689.   local enderChestType = 1
  690.   --TODO: check item in slot is a valid ender inventory, should solve related problems with helper functions
  691.  
  692.   if isExport then
  693.     if (exportChest ~= nil) then
  694.  
  695.       local turns, direction
  696.       local digSlot = exportChest
  697.  
  698.       -- place inventory
  699.       isExport, exportChest, turns, direction = selectAndPlaceOrient(exportChest)
  700.  
  701.       --TODO: verify block attempting to drop to actual is an inventory
  702.       --TODO: need logic to check on each export attempt to check if the chest is still there so loop isnt infinite if chest is broken/stolen mid-execution
  703.       --    may need 2 returns, possibly a number code or string so logic can be applied to exit loop
  704.       --export into inventory, if not full, if full wait and try again
  705.       local isExportFin = false
  706.       while (not isExportFin)  do -- attempts to export until successfully exported every valid slot in inventory
  707.         isExportFin = exportInventoryMining(direction)
  708.         if (not isExportFin) then -- only wait if still exporting
  709.           sleep(5) -- wait inbetween failed exports for chest contents to be extracted remotely
  710.         end
  711.       end
  712.       print("Retrieving Export inventory")
  713.  
  714.       -- retrieve ender chest and note slot, if failed then isExport = false, exportChest = nil
  715.       isExport, exportChest = digToSlot(direction, digSlot)
  716.  
  717.       -- reOrient to original direction
  718.       reOrientTurns(turns)
  719.  
  720.     end
  721.   end
  722.   return isExport, exportChest, enderChestType
  723. end
  724.  
  725. return {
  726.   findAndRefuel = findAndRefuel,
  727.   checkFuel = checkFuel,
  728.   sortInventory = sortInventory,
  729.   dropItems = dropItems,
  730.   detectIgnorePollution = detectIgnorePollution,
  731.   detectAndDigStrip = detectAndDigStrip,
  732.   detectAndDigUp = detectAndDigUp,
  733.   depositInventory = depositInventory,
  734.   placeInventoryLocal = placeInventoryLocal,
  735.   exportInventoryMining = exportInventoryMining,
  736.   emptySlots = emptySlots,
  737.   digToSlot = digToSlot,
  738.   suckDirection = suckDirection,
  739.   dropDirection = dropDirection,
  740.   selectAndPlaceOrient = selectAndPlaceOrient,
  741.   reOrientTurns = reOrientTurns,
  742.   importFuelEnder = importFuelEnder,
  743.   exportInventoryEnder = exportInventoryEnder
  744. }
Add Comment
Please, Sign In to add comment