Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Args = {...}
- os.loadAPI("apis/log.lua")
- os.loadAPI("apis/pos.lua")
- os.loadAPI("apis/storage.lua")
- local ST_IDLE = "idle"
- local ST_MAP_LIBRARY = "mapping_library"
- local ST_FIND_SPACE_FOR_ITEM = "finding_space_for_item"
- local ST_RETRIEVE_ITEMS = "retrieving_items"
- local ItemMap = {}
- local EmptyChests = {}
- local BoundsMap = {}
- local ItemAliases = {}
- --[[
- Log the current state to the resumefile
- ]]
- function SaveState()
- log.SaveData("resume", {"library"})
- log.SaveData("libraryItemMap", {textutils.serialize(ItemMap)})
- log.SaveData("libraryEmptyChests", {textutils.serialize(EmptyChests)})
- log.SaveData("libraryItemAliases", {textutils.serialize(ItemAliases)})
- end
- --[[
- Load state using args from the resumefile
- ]]
- function LoadState(args)
- local itemMapData = log.LoadData("libraryItemMap")
- local emptyChestData = log.LoadData("libraryEmptyChests")
- local itemAliasData = log.LoadData("libraryItemAliases")
- if (itemMapData ~= nil and emptyChestData ~= nil and itemAliasData ~= nil) then
- ItemMap = textutils.unserialize(itemMapData)
- EmptyChests = textutils.unserialize(emptyChestData)
- ItemAliases = textutils.unserialize(itemAliasData)
- else
- ItemMap = {}
- EmptyChests = {}
- ItemAliases = {}
- end
- end
- --[[
- Select an empty slot, if there is one.
- returns bool success
- ]]
- function SelectEmptySlot()
- local initialSlot = turtle.getSelectedSlot()
- turtle.select(1)
- while(turtle.getItemCount() > 0 and turtle.getSelectedSlot()+1 <= 16) do
- turtle.select(turtle.getSelectedSlot()+1)
- end
- if (turtle.getItemCount() == 0) then
- return true
- else
- turtle.select(initialSlot)
- return false
- end
- end
- function SelectFullSlot()
- local initialSlot = turtle.getSelectedSlot()
- turtle.select(1)
- while(turtle.getItemCount() == 0 and turtle.getSelectedSlot()+1 <= 16) do
- turtle.select(turtle.getSelectedSlot()+1)
- end
- if (turtle.getItemCount() > 0) then
- return true
- else
- turtle.select(initialSlot)
- return false
- end
- end
- --[[
- Test the chest ahead to see if it is empty.
- Requires at least one empty slot in inventory to function.
- returns bool success
- ]]
- function ChestIsEmpty()
- local initialSlot = turtle.getSelectedSlot()
- SelectEmptySlot()
- if(turtle.suck(1)) then
- turtle.drop()
- turtle.select(initialSlot)
- return false;
- else
- turtle.select(initialSlot)
- return true;
- end
- end
- --[[
- Load the turtle up with items, ensuring that the last slot is empty
- ]]
- function LoadEnsureEmpty()
- turtle.select(1)
- turtle.suck()
- while(turtle.select(turtle.getSelectedSlot()+1)) do
- turtle.suck()
- end
- turtle.drop()
- turtle.select(1)
- end
- function DepositToChest(itemStr)
- print("Depositing "..itemStr.." in chest.")
- if not (ItemAliases) then
- ItemAliases = {}
- end
- local aliases = ItemAliases[itemStr]
- local chestIsFull = true
- local currentAlias = 1
- -- Ensure there is a chest available
- if(ItemMap == nil) then ItemMap = {} end
- if (aliases == nil) then
- if not(AssignEmptyChest(itemStr)) then
- DepositToDefault()
- return
- end
- aliases = ItemAliases[itemStr]
- end
- while (chestIsFull) do
- -- Get the alias for the current chest
- local alias = aliases[currentAlias]
- -- Find the chest
- local chestPos = ItemMap[alias]
- if (chestPos.y > BoundsMap[pos.GetX()].y or chestPos.z > BoundsMap[pos.GetX()].z) then
- DepositToDefault()
- return
- end
- -- Navigate to the chest
- ReturnHome()
- if (chestPos.x > 0) then
- pos.TurnToFace(pos.X_POSITIVE)
- for i = 1, chestPos.x, 1 do
- pos.Forward()
- end
- pos.TurnToFace(pos.Z_POSITIVE)
- end
- for i = 1, chestPos.y, 1 do
- pos.Up()
- end
- for i = 1, chestPos.z, 1 do
- pos.Forward()
- end
- if (chestPos.positive_x) then
- pos.TurnToFace(pos.X_POSITIVE)
- else
- pos.TurnToFace(pos.X_NEGATIVE)
- end
- local detail = turtle.getItemDetail()
- if (turtle.drop()) then
- chestIsFull = false
- storage.AddToStore(detail.name, detail.count)
- else
- currentAlias = currentAlias + 1
- print("Chest is full. Finding new chest.")
- end
- end
- end
- function DepositToDefault()
- pos.TurnToFace(pos.X_NEGATIVE)
- local detail = turtle.getItemDetail()
- turtle.drop()
- if not (ItemCounts) then
- ItemCounts = {}
- end
- if (detail) then
- storage.AddToStore(detail.name, detail.count)
- end
- pos.TurnToFace(pos.Z_POSITIVE)
- end
- function AssignEmptyChest(itemStr)
- if not (ItemAliases[itemStr]) then
- ItemAliases[itemStr] = {}
- end
- local alias = itemStr..tostring(#ItemAliases[itemStr])
- table.insert(ItemAliases[itemStr], alias)
- if not(SetChest(alias, table.remove(EmptyChests, 1))) then
- return nil
- end
- SaveState()
- return ItemMap[alias]
- end
- function SetChest(itemStr, chestPos)
- local isFacingPositiveX = false
- if (pos.GetFace() == pos.X_POSITIVE) then
- isFacingPositiveX = true
- end
- if (chestPos ~= nil) then
- ItemMap[itemStr] = chestPos
- return true
- else
- return false
- end
- end
- function InitEmptyChests(y, z, xVal)
- print("x: "..tostring(xVal)..", y: "..tostring(y)..", z: "..tostring(z))
- EmptyChests = {}
- for i = 0, y do
- for j = 1, z do
- local posXChestIsFull = false
- local negXChestIsFull = false
- for key, value in pairs(ItemMap) do
- if (value.x == xVal and value.y == i and value.z == j) then
- if (value.positive_x) then
- posXChestIsFull = true
- else
- negXChestIsFull = true
- end
- end
- end
- if not (posXChestIsFull) then
- table.insert(EmptyChests, {
- x=xVal,
- y=i,
- z=j,
- positive_x=true
- })
- end
- if not (negXChestIsFull) then
- table.insert(EmptyChests, {
- x=xVal,
- y=i,
- z=j,
- positive_x=false
- })
- end
- end
- end
- end
- --[[
- Autonomously find the boundaries of the current library
- ]]
- function MapLibrary()
- while (pos.Forward()) do end
- while (pos.Up()) do end
- if not (BoundsMap) then BoundsMap = {} end
- InitBounds(pos.GetX(), pos.GetY(), pos.GetZ())
- ReturnHome()
- end
- function InitBounds(xLocation, y, z)
- InitEmptyChests(y, z, xLocation)
- BoundsMap[xLocation] = {y=y, z=z}
- end
- --[[
- Returns to the place the program was first run
- ]]
- function ReturnHome()
- pos.TurnToFace(pos.Z_POSITIVE)
- for i = 1, pos.GetY(), 1 do
- pos.Down()
- end
- for i = 1, pos.GetZ(), 1 do
- pos.Back()
- end
- if(pos.GetX() > 0) then
- pos.TurnToFace(pos.X_NEGATIVE)
- for i = 1, pos.GetX(), 1 do
- pos.Forward()
- end
- pos.TurnToFace(pos.Z_POSITIVE)
- end
- end
- LoadState()
- if ItemMap == nil then ItemMap = {} end
- if EmptyChests == nil then EmptyChests = {} end
- MapLibrary()
- SaveState()
- ReturnHome()
- while true do
- -- If items exist in inventory
- if (SelectFullSlot()) then
- local itemName = turtle.getItemDetail().name
- DepositToChest(itemName)
- local itemDetail = turtle.getItemDetail(turtle.getSelectedSlot()+1)
- while (itemDetail ~= nil and itemDetail.name == itemName) do
- turtle.select(turtle.getSelectedSlot()+1)
- turtle.drop()
- storage.AddToStore(itemDetail.name, itemDetail.count)
- itemDetail = turtle.getItemDetail(turtle.getSelectedSlot()+1)
- end
- ReturnHome()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement