Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TODO: extract functions into here to be used
- -- apparently args can be dynamically generated ie `function(...)` would take any number of args and store them in a table
- -- you can also specifiy args like `function(arg1, arg2, ...) now we have 2 predefined args and any number after
- -- checks if fuel is needed then consumes fuel if it is
- -- arg1 = #fuel to use, arg2 = minimum fuel before refuelling (upon checking)
- -- TODO: move into a more global robot lib rather than mining specific
- function checkFuel(fuelUsed, minFuel)
- fuelUsed = tonumber(fuelUsed)
- minFuel = tonumber(minFuel)
- if(not fuelUsed) then fuelUsed = 3 end
- if(not minFuel) then minFuel = 170 end
- local fuelStack, isFuel
- if(turtle.getFuelLevel() < minFuel) then
- sortInventory()
- local currentFuel = turtle.getFuelLevel()
- print("Fuel is low, Current fuel:"..currentFuel)
- print("Refuelling...")
- for slot=1,wLibVariables.SLOT_COUNT,1 do
- fuelStack = turtle.getItemDetail(slot)
- for i, v in ipairs(FUEL_ITEM) do
- if (fuelStack ~= nil) and (fuelStack["name"] == v) then
- turtle.select(slot)
- isFuel = true
- end
- if(isFuel == true) then break end
- end
- if(isFuel == true) then break end
- end
- turtle.refuel(fuelUsed)
- currentFuel = turtle.getFuelLevel()
- print("Refueled! Fuel is now:"..currentFuel)
- else
- print("Fuel levels are fine, resuming task")
- end
- end
- -- Inventory sorting program to keep stacks together to save space
- -- TODO: move into a more global robot lib rather than mining specific
- function sortInventory()
- local stackOne, stackTwo
- print("Organizing Inventory")
- for slot=1,wLibVariables.SLOT_COUNT-1,1 do
- stackOne = turtle.getItemDetail(slot)
- if(stackOne~=nil) and (turtle.getItemSpace(slot) > 0) then
- for slotTwo=slot+1,wLibVariables.SLOT_COUNT,1 do
- turtle.select(slotTwo)
- stackTwo = turtle.getItemDetail(slotTwo)
- if(stackTwo~=nil) then
- if(stackOne["name"] == stackTwo["name"]) and (stackOne["damage"] == stackTwo["damage"]) then
- local stackSpace = turtle.getItemSpace(slot)
- if (stackSpace ~= 0) then
- if (stackSpace <= stackTwo["count"]) then
- turtle.transferTo(slot)
- else
- turtle.transferTo(slot, stackSpace)
- end
- end
- end
- end
- end
- end
- end
- print("Organizing Complete!")
- end
- -- Cleans out inventory of items not in tables
- -- aimed at mining turtles
- function dropItems()
- print("Dropping Waste Inventory")
- for slot=1,wLibVariables.SLOT_COUNT,1 do
- local item = turtle.getItemDetail(slot)
- local keepItem = false
- if(item~=nil) then
- for keepItemIndex=1,#wLibVariables.MINING_KEEP_ITEMS,1 do
- if(item["name"] == wLibVariables.MINING_KEEP_ITEMS[keepItemIndex]) then
- keepItem = true
- break
- end
- end
- for fuelItemIndex=1,#wLibVariables.FUEL_ITEM,1 do
- if(item["name"] == wLibVariables.FUEL_ITEM[fuelItemIndex]) then
- keepItem = true
- break
- end
- end
- for exportInventoriesIndex=1,#wLibVariables.EXPORT_INVENTORIES,1 do
- if(item["name"] == wLibVariables.EXPORT_INVENTORIES[exportInventoriesIndex]) then
- keepItem = true
- break
- end
- end
- if(not keepItem) then
- turtle.select(slot)
- turtle.dropDown()
- end
- end
- end
- print("Waste Dropping Completed")
- sortInventory()
- end
- -- to dig, loop stops after 1 round as no block is detected infront!
- -- for strip mines
- function detectAndDigStrip()
- --while turtle detects a block above OR below OR in front //logic
- while(((turtle.detect()) or (turtle.detectDown())) or (turtle.detectUp())) do
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- end
- end
- -- Checks if an EXPORT_INVENTORY is infron then Deposit items into chest
- -- adjust so it returns booleans instead of changing the cycles so that start() handles setting the cycle instead
- -- TODO: move into a more global robot lib rather than mining specific
- -- TODO: make a check to see if there is space spaces and/or spare stack space to put items in
- function depositInventory()
- local isBlock, block = turtle.inspect()
- local isExportInventory
- print("Attempting to deposit inventory")
- if (isBlock) then
- for i, v in ipairs(wLibVariables.EXPORT_INVENTORIES) do
- if (block["name"] == v) then
- print("Depositing into "..block["name"])
- isExportInventory = true
- -- exportInventoryMining()
- return isExportInventory -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
- else
- if(i == #wLibVariables.EXPORT_INVENTORIES) and (block["name"] ~= v) then
- print(block["name"].." is not a valid block!")
- print("Stopping stripMine")
- return false -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
- end
- end
- if(isExportInventory == true) then break end
- end
- else
- print("Error: No block detected")
- print("STOPPING stripMine!!!")
- return false -- TODO: turn this into returning an int which is linked to a table of error codes, ie exit codes
- end
- end
- -- 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
- -- TODO: move into a more global robot lib
- -- 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
- -- will probably handle this by sending a return which should signal to use placeInventoryEnder()
- function placeInventoryLocal()
- local inventoryItem, isInventory
- for slot=1,wLibVariables.SLOT_COUNT,1 do
- inventoryItem = turtle.getItemDetail(slot)
- for i, v in ipairs(wLibVariables.EXPORT_INVENTORIES) do
- if (inventoryItem ~= nil) and (inventoryItem["name"] == v) then
- turtle.select(slot)
- isInventory = true
- while(turtle.detect()) do
- turtle.dig()
- end
- print("Placing Inventory: "..inventoryItem["name"])
- turtle.place()
- end
- if(isInventory == true) then break end
- end
- if(isInventory == true) then break end
- end
- end
- -- Checks each slot of the turtle to see if the item should be kept or not
- -- to be used in the middle of mining, separate function should be used to export into an actual base/ME system
- -- such a function would be simply called exportInventory(), or exportInventoryAll()
- -- needs logic to only keep a certain amount of fuel/inventories on hand (inventories could be set by cycles)
- function exportInventoryMining()
- for slot=1,wLibVariables.SLOT_COUNT,1 do
- local item = turtle.getItemDetail(slot)
- local keepItem = false
- if(item~=nil) then
- for fuelItemIndex=1,#wLibVariables.FUEL_ITEM,1 do
- if(item["name"] == wLibVariables.FUEL_ITEM[fuelItemIndex]) then
- keepItem = true
- break
- end
- end
- for exportInventoriesIndex=1,#wLibVariables.EXPORT_INVENTORIES,1 do
- if(item["name"] == wLibVariables.EXPORT_INVENTORIES[exportInventoriesIndex]) then
- keepItem = true
- break
- end
- end
- if(not keepItem) then
- turtle.select(slot)
- turtle.drop()
- end
- end
- end
- print("Finished exporting inventory")
- end
- -- check for # of empty slots
- -- returns true if empty slots >= minEmpty
- function emptySlots(minEmpty)
- minEmpty = tonumber(minEmpty)
- local slots = 16
- for i=1,wLibVariables.SLOT_COUNT,1 do
- if (turtle.getItemCount(i) > 0) then
- slots = slots - 1
- end
- end
- if (slots < minEmpty) then
- print("Inventory Full")
- return false
- end
- if (slots >= minEmpty) then
- return true
- end
- end
- return {
- checkFuel = checkFuel,
- sortInventory = sortInventory,
- dropItems = dropItems,
- detectAndDigStrip = detectAndDigStrip,
- depositInventory = depositInventory,
- placeInventoryLocal = placeInventoryLocal,
- exportInventoryMining = exportInventoryMining,
- emptySlots = emptySlots
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement