Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Name: storeKeeperV
- Version: 1.4
- Description: Put items away in an inventory system made up of a network of stacked chests. Be able
- to store, identify and count the contents of each chest and be able to retrieve items when requested
- by the InventoryControl program.
- Note: This program has been extensively rewritten from the original program written by nitrogetfingers used in his Turtle Butler example.
- The use of the parallel option was removed because of problems using it on a server.
- Date: 01/14/13
- Author: Severino
- Future enhancements planned:
- - Unloading mine carts
- ]]--
- -- Current inventory location
- location = 0
- locLevel = 0
- locDir = 0
- -- Current Inventory Location
- currentLocation = locLevel..","..locDir
- -- Current orientation within the room (1 = forward, 2=right, 3=back, 4=left)
- orientation = 1
- -- Flag to abort mission.
- abortMission = false
- -- Maximum slot to use to transfer inventory
- maxSlot = 15
- -- Slot to use to store fuel.
- fuelSlot = 16
- -- Setup Directions.
- invDir = 1
- fuelDir = 2
- unknownDir = 4
- -- Inventory Names
- inventoryName = {}
- -- Home Terminal
- orderComputer = nil
- storeKeeperID = nil
- -- Has Pick Axe
- hasPickAxe = true
- --The list of all tasks to be done
- queue = {}
- -- List of empty chests encountered.
- emptyChests = {}
- function saveTable(tableData, tableName)
- save = textutils.serialize(tableData)
- file = fs.open(tableName, "w")
- file.write(save)
- file.close()
- end
- function loadTable(tableName)
- if fs.exists(tableName) then
- file = fs.open(tableName, "r")
- vars = file.readAll()
- file.close()
- tableData = textutils.unserialize(vars)
- else
- file = fs.open(tableName, "w")
- tableData = {}
- vars = textutils.serialize(tableData)
- file.write(vars)
- file.close()
- end
- return tableData
- end
- function reportInventory(computerId)
- invData = textutils.serialize(inventoryName)
- if computerId == nil then
- rednet.broadcast("inventory"..invData)
- else
- rednet.send(computerId, "inventory"..invData)
- end
- end
- function identifyInventory()
- -- If there is inventory left and there are empty chests, put it in the empty chests
- if not isTurtleEmpty() then
- if not goToStart() then return end
- emptyInventory()
- end
- -- This function locates inventory locations that have not been identified and requests descriptions.
- local level = 1
- local dir = 0
- local slot = 0
- local locationsLevel = {}
- local locationsDir = {}
- local locationsName = {}
- local locationNotEmpty = true
- setStatus("Retrieving Inventory")
- while slot < maxSlot and locationNotEmpty do
- for dir = 1,4 do
- local invLoc = level..","..dir
- local name, qty = getInventoryNameQty(invLoc)
- if name == "_" then
- gotoLocation(level,dir)
- if turtle.detect() then
- slot = slot + 1
- turtle.select(slot)
- if turtle.suck() then
- turtle.drop(turtle.getItemCount(slot)-1)
- locationsLevel[slot] = level
- locationsDir[slot] = dir
- print("at "..invLoc)
- setStatus("at "..invLoc)
- if qty ~= nil then
- if tonumber(qty) > 0 then
- qty = qty - 1
- inventoryName[invLoc] = "_#"..qty
- end
- end
- else
- slot = slot - 1
- end
- else
- locationNotEmpty = false
- break
- end
- end
- if slot == maxSlot then break end
- end
- level = level + 1
- end
- if goToStart() then
- if not isTurtleEmpty() then
- -- Something was loaded. Get names for each item in
- local i = 1
- while locationsLevel[i] ~= nil do
- if turtle.getItemCount(i) > 0 then
- level = locationsLevel[i]
- dir = locationsDir[i]
- local name, qty = getInventoryNameQty(invLoc)
- name = nil
- while name == nil do
- setStatus("Identify slot "..i)
- term.write("Identify slot "..i.." (b=back):")
- name = io.read()
- end
- if string.lower(name) == "b" then
- if i > 1 then i = i - 2 else i = i - 1 end
- else
- inventoryName[level..","..dir] = name.."#"..qty
- setStatus("identify"..level..","..dir.."-"..name)
- end
- end
- i = i + 1
- end
- saveTable(inventoryName, "inventory")
- -- Now go and put everything away.
- setStatus("Putting inventory back.")
- local i = 1
- while locationsLevel[i] ~= nil do
- if turtle.getItemCount(i) > 0 then
- level = locationsLevel[i]
- dir = locationsDir[i]
- returnItemToInventory(i, level, dir)
- end
- i = i + 1
- end
- goToStart()
- else
- setStatus("done identifying.")
- end
- end
- end
- -- Is there anything in the turtle.
- function isTurtleEmpty()
- for s=1,maxSlot,1 do
- if turtle.getItemCount(s) > 0 then return false end
- end
- return true
- end
- -- Change the orientation of the turtle
- function changeOrientation(dir)
- if dir < 1 then dir = dir + 4 end
- if dir > 4 then dir = dir - 4 end
- while orientation ~= dir do
- if (orientation - dir) == -3 or (orientation - dir) == 1 then
- turtle.turnLeft()
- orientation = orientation - 1
- else
- turtle.turnRight()
- orientation = orientation + 1
- end
- if orientation > 4 then
- orientation = 1
- elseif orientation == 0 then
- orientation = 4
- end
- end
- end
- --Go to starting position
- function goToStart()
- print("Going to start.")
- setStatus("going to start")
- -- Move out of chest shaft.
- local ceilingFound = false
- for a=1,70 do
- if not turtle.detectUp() then turtle.up() else
- ceilingFound = true
- break
- end
- end
- if not ceilingFound then
- for b=1,70 do
- turtle.down()
- end
- print("Ceiling not found.")
- return false
- end
- changeOrientation(invDir) -- Now facing material chest
- -- Reset location to indicate at Start position.
- location = 0
- locLevel = 0
- locDir = 0
- currentLocation = locLevel..","..locDir
- changeOrientation(invDir)
- return true
- end
- -- Go to a specific inventory location.
- function gotoLocation(level, dir)
- if locDir == 0 then
- -- Should be 1 above floor
- if not turtle.down() then return false end -- Now even with floor
- if not turtle.down() then return false end -- Now 1 below fllor
- if not turtle.down() then return false end -- Now even with the first level of chest.
- locLevel = 1
- changeOrientation(1)
- locDir = 1
- currentLocation = locLevel..","..locDir
- end
- while level ~= locLevel do
- if locLevel < level then
- if turtle.down() then locLevel = locLevel + 1 else return false end
- else
- if turtle.up() then locLevel = locLevel - 1 else return false end
- end
- currentLocation = locLevel..","..locDir
- end
- -- Now must be at the correct level.
- changeOrientation(dir)
- locDir = dir
- return turtle.detect()
- end
- -- Go to next inventory location.
- function gotoNextInventoryLocation()
- -- Check to see if the turtle has started a level.
- if locDir == 0 then
- -- Should be 1 above floor
- turtle.down() -- Now even with floor
- turtle.down() -- Now 1 below fllor
- turtle.down() -- Now even with the first level of chest.
- locLevel = 1
- changeOrientation(1)
- locDir = 1
- currentLocation = locLevel..","..locDir
- elseif locDir == 4 then
- if turtle.detectDown() then
- -- We have reached the bottom.
- return false
- else
- -- Move down to the next chest.
- while not turtle.down() do
- setStatus("blocked at level "..locLevel)
- sleep(10)
- end
- locLevel = locLevel + 1
- locDir = 1
- changeOrientation(locDir)
- currentLocation = locLevel..","..locDir
- -- Check to see if there isn't a chest there.
- if not turtle.detect() then return false end
- end
- else
- locDir = locDir + 1
- changeOrientation(locDir)
- currentLocation = locLevel..","..locDir
- -- Check to see if there isn't a chest there.
- if not turtle.detect() then return false end
- end
- return true
- end
- -- Load Inventory To Put Away
- function loadInventory()
- -- Pick up all of the items in the chest in front of the turtle.
- -- Set the flag that the inventory is empty.
- inventoryLoaded = false
- -- Start filling at slot 2. Keep slot 1 clear for comparing to contents of chest.
- s=2
- while s <= maxSlot do
- turtle.select(s)
- if turtle.suck() then
- -- Something was picked up so reset the flag.
- inventoryLoaded = true
- else
- -- The inventory chest is empty.
- break
- end
- -- Move to the next slot in the turtle.
- s = s + 1
- end
- return inventoryLoaded
- end
- -- Empty non-identified items
- function emptyInventory()
- print("Emptying inv.")
- setStatus("emptying inventory")
- changeOrientation(unknownDir)
- -- Dump everything into chest of unknown material.
- for s=1,maxSlot,1 do
- turtle.select(s)
- turtle.drop()
- end
- -- Return to Start position
- changeOrientation(invDir)
- end
- function digFoward()
- turtle.select(6)
- while turtle.detect() do
- turtle.dig()
- sleep(.5)
- end
- end
- function selectFill()
- for s=7,15 do
- turtle.select(s)
- if turtle.compareTo(6) then return end
- end
- turtle.select(6)
- end
- function placeSeal()
- selectFill()
- while not turtle.detect() do
- turtle.place()
- sleep(.5)
- end
- end
- function placeSealDown()
- selectFill()
- while not turtle.detectDown() do
- turtle.placeDown()
- sleep(.5)
- end
- end
- function digDown()
- turtle.select(6)
- while turtle.detectDown() do
- turtle.digDown()
- sleep(.5)
- end
- end
- function goForward()
- while not turtle.forward() do
- print("Forward blocked")
- sleep(4)
- end
- end
- function goBack()
- while not turtle.back() do
- print("Back blocked")
- sleep(4)
- end
- end
- function selectChest()
- for s=5,15 do
- turtle.select(s)
- if turtle.compareTo(1) then return true end
- end
- if turtle.getItemCount(1) > 0 then
- turtle.select(1)
- return true
- end
- return false
- end
- function placeChest()
- selectChest()
- turtle.place()
- end
- function placeChestUp()
- selectChest()
- turtle.placeUp()
- end
- function selectGlass()
- for s=5,15 do
- turtle.select(s)
- if turtle.compareTo(2) then return true end
- end
- turtle.select(2)
- if turtle.getItemCount(2) > 0 then
- turtle.select(2)
- return true
- end
- return false
- end
- function placeGlass()
- if selectGlass() then
- turtle.place()
- else
- placeSeal()
- end
- end
- function selectTorch()
- for s=5,15 do
- turtle.select(s)
- if turtle.compareTo(3) then return true end
- end
- if turtle.getItemCount(3) > 0 then
- turtle.select(3)
- return true
- end
- return false
- end
- function placeTorchUp()
- if selectTorch() then
- turtle.placeUp()
- return true
- end
- return false
- end
- function selectLadder()
- for s=5,15 do
- turtle.select(s)
- if turtle.compareTo(4) then return true end
- end
- if turtle.getItemCount(4) > 0 then
- turtle.select(4)
- return true
- end
- return false
- end
- function placeLadderUp()
- if selectLadder() then
- turtle.placeUp()
- return true
- end
- return false
- end
- function placeSign(message)
- if hasSigns then
- if not turtle.detect() then
- goForward()
- if not turtle.detect() then placeSeal() end
- goBack()
- end
- turtle.select(5)
- turtle.place(message)
- end
- end
- -- Create Initial Chest Setup
- function Initialize()
- if not hasPickAxe then
- print("Turtle must have a pick axe to")
- print("be able to create warehouse shaft.")
- return false
- end
- -- First check that you are facing a wall.
- local wallDetected = false
- for a=1,4 do
- if turtle.detect() then
- wallDetected = true
- break
- end
- turtle.turnRight()
- end
- if not wallDetected then
- print("Turtle must be next to a wall to create initial setup.")
- return false
- end
- -- Now give instructions about where to put chests.
- while turtle.getItemCount(fuelSlot) < 5 do
- print("Place 5+ fuel in slot "..fuelSlot..".")
- term.write("Ready? (Y/N):")
- continue = string.upper(io.read())
- if continue ~= "Y" then return false end
- end
- while turtle.getItemCount(1) < 10 do
- print("Place the following material in the slots.")
- print("Slot 1: 30+ chests")
- print("Slot 2: 30+ glass blocks")
- print("Slot 3: 20+ torches")
- print("Slot 4: 30+ ladders")
- print("Slot 5: 3 signs")
- print("Slot 6: Material to seal walls")
- print("Place additional chests, glass and material in other slots.")
- term.write("Ready? (Y/N):")
- continue = string.upper(io.read())
- if continue ~= "Y" then return false end
- end
- -- Check for clearance to place chests
- rightOk = false
- backOk = false
- leftOk = false
- upOk = false
- hasGlass = turtle.getItemCount(2) > 0
- hasTorches = turtle.getItemCount(3) > 0
- hasLadders = turtle.getItemCount(4) > 0
- hasSigns = turtle.getItemCount(5) > 0
- while not (rightOk and leftOk and backOk and upOk) do
- -- Check to the right
- turtle.turnRight()
- -- Facing right
- if not turtle.detect() then
- if turtle.forward() then
- if not turtle.detect() then rightOk = true end
- turtle.back()
- end
- end
- -- Check to the back.
- turtle.turnRight()
- -- Facing front
- if not turtle.detect() then backOk = true end
- -- Check to the left
- turtle.turnRight()
- -- Facing left
- if not turtle.detect() then
- if turtle.forward() then
- if not turtle.detect() then leftOk = true end
- turtle.back()
- end
- end
- -- Check Up
- if not turtle.detectUp() then upOk = true end
- turtle.turnRight()
- -- Facing back
- if not upOk then print("Space above turtle must be clear.") end
- if not rightOk then print("2 spaces to right must be clear.") end
- if not backOk then print("Space behind turtle must be clear.") end
- if not leftOk then print("2 spaces to left must be clear.") end
- if not (rightOk and leftOk and backOk and upOk) then
- print("Continue?")
- cont = io.read()
- end
- end
- ---------- BLOCK PATTERN ---------
- -- Back
- -- X
- -- P U W
- -- R O T V Y
- -- Left 11 N M S A B 12 Right
- -- 10 L I G E C 13
- -- 9 K J H F D 14
- -- 8 1 2 3 4
- -- 7 6 5
- -- Front
- -- Everthing ready, start setting things up.
- -- Place double unidentified chest
- turtle.turnRight()
- -- Facing right
- goForward()
- placeChest()
- goBack()
- placeChest()
- -- Now put a sign on it.
- if hasSigns then
- if turtle.up() then
- turtle.turnRight()
- if turtle.forward() then
- turtle.turnLeft()
- if turtle.forward() then
- if turtle.forward() then
- turtle.turnLeft()
- placeSign("\nOverflow\nInventory")
- turtle.turnRight()
- turtle.back()
- end
- turtle.back()
- end
- turtle.turnRight()
- turtle.back()
- end
- turtle.turnLeft()
- turtle.down()
- end
- end
- -- Place double fuel chest
- turtle.turnRight()
- turtle.turnRight()
- -- Facing left
- goForward()
- placeChest()
- goBack()
- placeChest()
- -- Now put some fuel in it.
- turtle.select(fuelSlot)
- turtle.drop(1)
- -- Now put a sign on it.
- if hasSigns then
- if turtle.up() then
- turtle.turnLeft()
- if turtle.forward() then
- turtle.turnRight()
- if turtle.forward() then
- if turtle.forward() then
- turtle.turnRight()
- placeSign("\nStoreKeeper\nFuel")
- turtle.turnLeft()
- turtle.back()
- end
- turtle.back()
- end
- turtle.turnLeft()
- turtle.back()
- end
- turtle.turnRight()
- turtle.down()
- end
- end
- -- Place retrieval chest
- turtle.turnRight()
- -- Facing back
- placeChestUp()
- -- Now put a sign on it.
- if turtle.back() then
- if turtle.up() then
- if turtle.up() then
- placeSign("\nRequested\nItems")
- turtle.down()
- end
- turtle.down()
- turtle.forward()
- end
- end
- -- Place unknown item chest
- turtle.turnRight()
- turtle.turnRight()
- -- Facing front
- goForward()
- placeChest()
- goBack()
- placeChest()
- turtle.turnRight()
- turtle.turnRight()
- -- Facing back
- -- Now replace the floor with glass blocks and a crawl hole
- local level = 0
- turtle.turnRight()
- -- Facing right
- turtle.digDown()
- turtle.down()
- level = level + 1
- -- If we have glass blocks then replace floor with glass blocks
- -- Replace floor to right
- digFoward()
- goForward() -- A
- turtle.turnLeft() -- Facing back
- placeGlass() -- V
- turtle.turnRight() -- Facing left
- digFoward()
- goForward() -- B
- turtle.turnLeft() -- Facing back
- placeGlass() -- Y
- turtle.turnRight() -- Facing left
- digFoward()
- goForward() -- 12
- turtle.turnRight() -- Facing front
- digFoward()
- goForward() -- 13
- digFoward()
- goForward() -- 14
- digFoward()
- placeSeal() -- 15
- goBack() -- 13
- placeSeal() -- 14
- goBack() -- 12
- placeSeal() -- 13
- turtle.turnLeft() -- Facing right
- goBack() -- B
- placeSeal() -- 12
- turtle.turnRight() -- Facing front
- digFoward()
- goForward() -- C
- digForward()
- goForward() -- D
- goBack() -- C
- goBack() -- B
- placeGlass() -- C
- turtle.turnLeft() -- Facing right
- goBack() -- A
- placeGlass() -- B
- turtle.turnRight() -- Facing front
- digFoward()
- placeGlass() -- E
- turtle.turnLeft() -- Facing right
- goBack() -- Start
- placeGlass() -- A
- turtle.turnRight() -- Facing front
- -- Replace floor to front with glass
- digFoward()
- goForward() -- G
- digFoward()
- goForward() -- H
- digFoward()
- goForward() -- 2
- turtle.turnLeft() -- Facing right
- digFoward()
- goForward() -- 3
- placeGlass() -- 4
- goBack() -- 2
- placeGlass() -- 3
- turtle.turnRight() -- Facing front
- turtle.turnRight() -- Facing left
- digFoward()
- goForward() -- 1
- digForward()
- goForward() -- 8
- placeGlass() -- 16
- goBack() -- 1
- placeGlass() -- 8
- goBack() -- 2
- placeGlass() -- 1
- turtle.turnLeft() -- Facing front
- goBack() -- H
- placeGlass() -- 2
- turtle.turnLeft() -- Facing right
- digFoward()
- goForward() -- F
- digFoward()
- goBack() -- H
- placeGlass() -- F
- turtle.turnRight() -- Facing front
- turtle.turnRight() -- Facing left
- digFoward()
- goForward() -- J
- digFoward() -- K
- goForward() -- K
- placeGlass() -- 9
- goBack() -- J
- goBack() -- H
- placeGlass() -- J
- turtle.turnLeft() -- Facing front
- goBack() -- G
- placeGlass() -- H
- goBack() -- S
- placeGlass() -- G
- -- Replace floor on left
- turtle.turnRight() -- Facing left
- digFoward()
- goForward() -- M
- turtle.turnLeft() -- Facing front
- digFoward()
- placeGlass() -- I
- turtle.turnRight() -- Facing left
- digFoward()
- goForward() -- N
- digForward()
- goForward() -- 11
- turtle.turnLeft() -- Facing front
- placeGlass() -- 10
- turtle.turnRight() -- Facing left
- goBack() -- N
- turtle.turnLeft() -- Facing front
- digFoward()
- placeGlass() -- L
- turtle.turnRight() -- Facing left
- placeSeal() -- 11
- goBack() -- M
- placeGlass() -- N
- goBack() -- Start
- placeGlass() -- M
- turtle.turnRight() -- Facing back
- -- Put fill in for the back
- digForward()
- goForward() -- T
- turtle.turnLeft() -- Facing left
- digForward() -- O
- goForward() -- O
- placeGlass() -- R
- goBack() -- T
- placeGlass() -- O
- turtle.turnRight() -- Facing back
- digForward() -- U
- goForward() -- U
- turtle.turnLeft() -- Facing left
- placeSeal() -- P
- turtle.turnRight() -- Facing back
- placeGlass() -- X
- turtle.turnRight() -- Facing right
- placeGlass() -- W
- turtle.turnLeft() -- Facing back
- goBack() -- T
- placeGlass() -- U
- goBack() -- S
- placeGlass() -- T w
- turtle.turnRight() -- Facing right
- turtle.digDown()
- turtle.down()
- level = level + 1
- -- Now start placing chests for warehouse until we run out of chests.
- -- Clear out space above chests.
- for d=1,4 do
- digFoward()
- goForward() -- 1=A, 2=G, 3=M, 4=T
- if d == 1 or d == 4 then
- turtle.turnLeft() -- Facing 1=back, 3=left
- placeSeal() -- 1=V, 4=O
- turtle.turnRight() -- Facing 1=right, 3=back
- elseif d == 2 or d == 4 then
- turtle.turnLeft() -- Facing right
- if d == 2 then digFoward() else placeSeal() end -- O
- turtle.turnRight() -- Facing front
- turtle.turnRight() -- Facing left
- if d == 2 then digFoward() else placeSeal() end -- V
- turtle.turnLeft() -- Facing front
- end
- digFoward()
- goForward() -- 1=B, 2=H, 3=N, 4=U
- if d == 1 or d == 4 then
- placeSeal() -- 1=12, 4=X
- turtle.turnLeft() -- Facing 1=back, 4=left
- placeSeal() -- 1=Y, 4=P
- turtle.turnRight() -- Facing 1=right, 4=back
- turtle.turnRight() -- Facing 1=front, 4=right
- if d == 1 then
- digFoward()
- placeGlass() -- C
- elseif d == 4 then placeSeal() end -- W
- turtle.turnLeft() -- Facing 1=right, 4=back
- elseif d == 2 then
- turtle.turnLeft() -- Facing 2=right
- digForward() -- F
- goForward() -- F
- digForward() -- D
- goForward() -- D
- placeSeal() -- 14
- goBack() -- F
- goBack() -- H
- placeGlass() -- F
- turtle.turnRight() -- Facing front
- turtle.turnRight() -- Facing left
- digForward()
- goForward() -- J
- digForward() -- K
- goForward() -- K
- placeSeal() -- 9
- turtle.turnLeft() -- Facing front
- placeSeal() -- 8
- turtle.turnRight() -- Facing left
- goBack() -- J
- goBack() -- H
- placeGlass() -- J
- turtle.turnLeft() -- Facing front
- digForward()
- goForward() -- 2
- placeSeal() -- 6
- turtle.turnLeft() -- Facing right
- digForward() -- 3
- goForward() -- 3
- placeSeal() -- 4
- goBack() -- 2
- placeGlass() -- 3
- turtle.turnRight() -- Facing front
- turtle.turnRight() -- Facing left
- digForward() -- 1
- placeGlass() -- 1
- turtle.turnLeft() -- Facing front
- goBack() -- H
- elseif d == 3 then
- placeSeal() -- 11
- turtle.turnLeft() -- Facing front
- digForward()
- placeGlass() -- L
- turtle.turnRight() -- Facing left
- turtle.turnRight() -- Facing back
- placeSeal() -- R
- turtle.turnLeft() -- Facing left
- end
- goBack() -- 1=A, 2=G, 3=M, 4=T
- goBack() -- Start
- turtle.turnRight() -- Facing 1=front, 2=left, 3=back, 4=right
- end
- -- Facing right
- -- S
- -- Now start adding layers of chests until they are all gone.
- while turtle.getItemCount(1) > 1 do
- turtle.digDown()
- turtle.down()
- level = level + 1
- for d=1,4 do
- -- Facing 1=right, 2=front, 3=left, 4=back
- turtle.select(5)
- digForward() -- 1=A, 2=G, 3=M, 4=T
- goForward() -- 1=A, 2=G, 3=M, 4=T
- if d == 1 or d == 4 then
- turtle.turnLeft() -- Facing 1=back, 3=left
- placeSeal() -- 1=V, 4=O
- turtle.turnRight() -- Facing 1=right, 3=back
- elseif d == 2 or d == 4 then
- turtle.turnLeft() -- Facing right
- if d == 2 then
- digFoward()
- if (level-2)%6 == 0 then placeGlass() else -- E
- goForward()
- goBack()
- end
- else placeSeal() end -- O
- turtle.turnRight() -- Facing front
- turtle.turnRight() -- Facing left
- if d == 2 then
- digFoward()
- if (level-2)%6 == 0 then placeGlass() else -- I
- goForward()
- goBack()
- end
- else placeSeal() end -- V
- turtle.turnLeft() -- Facing front
- end
- digForward() -- 1=B, 2=H, 3=N, 4=U
- goForward() -- 1=B, 2=H, 3=N, 4=U
- if (d == 1) then
- placeSeal() -- 12
- turtle.turnLeft() -- Facing back
- placeSeal() -- Y
- turtle.turnRight() -- Facing right
- turtle.turnRight() -- Facing front
- digForward() -- C
- goForward()
- goBack()
- if not(level%6 == 0 or (level-1)%6 == 0) then
- placeGlass() -- F
- else
- goForward() -- C
- turtle.turnLeft() -- Facing right
- placeSeal() -- 13
- turtle.turnRight() -- Facing front
- goBack() -- B
- end
- turtle.turnLeft() -- Facing 2=right
- elseif (d == 2) then
- turtle.turnLeft() -- Facing 2=right
- digForward() -- F
- goForward() -- F
- digForward() -- D
- goForward() -- D
- placeSeal() -- 14
- placeLadderUp() -- D
- turtle.turnRight() -- Facing front
- placeSeal() -- 4
- turtle.turnLeft() -- Facing right
- goBack() -- F
- turtle.turnRight() -- Facing 2=front
- digForward() -- 3
- if level%8 then
- placeGlass() -- 3
- else
- goForward() -- 3
- placeSeal() -- 5
- goBack() -- F
- end
- turtle.turnLeft() -- Facing 2=right
- goBack() -- H
- if not(level%6 == 0 or (level-1)%6 == 0) then placeGlass() end -- F
- turtle.turnRight() -- Facing 2=front
- digForward() -- 2
- if level%8 then
- placeGlass() -- 2
- else
- goForward() -- 2
- placeSeal() -- 6
- goBack() -- H
- end
- turtle.turnRight() -- Facing 2=left
- digForward() -- J
- goForward() -- J
- digForward() -- K
- goForward() -- K
- placeSeal() -- 9
- placeLadderUp() -- K
- turtle.turnLeft() -- Facing front
- placeSeal() -- 8
- turtle.turnRight() -- Facing left
- goBack() -- J
- turtle.turnLeft() -- Facing 2=front
- digForward() -- 1
- if level%8 then
- placeGlass() -- 1
- else
- goForward() -- 1
- placeSeal() -- 7
- goBack() -- J
- end
- turtle.turnRight() -- Facing 2=left
- goBack() -- H
- if not(level%6 == 0 or (level-1)%6 == 0) then placeGlass() end -- J
- turtle.turnLeft() -- Facing 2=front
- elseif (d == 3) then
- placeSeal() -- 11
- turtle.turnRight() -- Facing back
- placeSeal() -- R
- turtle.turnLeft() -- Facing left
- turtle.turnLeft() -- Facing 3=front
- digForward() -- L
- if not(level%6 == 0 or (level-1)%6 == 0) then
- placeGlass() -- L
- else
- goForward() -- L
- turtle.turnRight() -- Facing left
- placeSeal() -- 10
- placeTorchUp()
- turtle.turnLeft() -- Facing front
- goBack() -- N
- end
- turtle.turnRight() -- Facing 3=left
- elseif (d == 4) then
- placeSeal() -- X
- turtle.turnLeft() -- Facing left
- placeSeal() -- P
- turtle.turnRight() -- Facing back
- turtle.turnRight() -- Facing right
- placeSeal() -- W
- turtle.turnLeft() -- Facing back
- end
- -- Facing 1=right, 2=front, 3=left, 4=back
- goBack() -- 1=A, 2=G, 3=M, 4=T
- goBack() -- Start
- turtle.turnRight() -- Facing 1=front, 2=left, 3=back, 3=right
- end
- for d=1,4 do
- goForward()
- placeChest() -- 1=B, 2=H, 3=N, 4=U
- goBack() -- Start
- placeChest() -- 1=A, 2=G, 3=M, 4=T
- turtle.turnRight() -- Facing 1=front, 2=left, 3=back, 3=right
- end
- print("Finished layer "..level)
- end
- -- Now put in an empty space below the last chest.
- digDown()
- turtle.down()
- for d=1,4 do
- digForward()
- goForward() -- 1=A, 2=G, 3=M, 4=T
- turtle.turnLeft()
- digForward()
- goForward()
- placeSeal()
- goBack()
- turtle.turnRight()
- digForward()
- goForward() -- 1=B, 2=H, 3=N, 4=U
- turtle.turnLeft()
- digForward()
- goForward()
- placeSeal()
- goBack()
- turtle.turnRight()
- turtle.turnRight()
- digForward()
- goForward() -- 1=C, 2=H, 3=N, 4=U
- digForward()
- goForward() -- 1=D, 2=H, 3=N, 4=U
- placeSeal()
- placeLadderUp()
- goBack() -- 1=C
- goBack() -- 1=B
- turtle.turnLeft()
- goBack() -- 1=A
- goBack() -- 1=S
- turtle.turnRight()
- end
- -- Now seal the bottom.
- digDown()
- turtle.down()
- placeSealDown()
- for d=1,4 do
- digForward()
- goForward() -- 1=A, 2=G, 3=M, 4=T
- placeSealDown()
- turtle.turnLeft()
- digForward()
- goForward()
- placeSeal()
- placeSealDown()
- goBack()
- turtle.turnRight()
- digForward()
- goForward() -- 1=B, 2=H, 3=N, 4=U
- placeSealDown()
- turtle.turnLeft()
- digForward()
- goForward()
- placeSeal()
- placeSealDown()
- goBack()
- turtle.turnRight()
- turtle.turnRight()
- digForward()
- goForward() -- 1=C, 2=H, 3=N, 4=U
- placeSealDown()
- digForward()
- goForward() -- 1=D, 2=H, 3=N, 4=U
- placeSeal()
- placeSealDown()
- placeLadderUp()
- goBack() -- 1=C
- goBack() -- 1=B
- turtle.turnLeft()
- goBack() -- 1=A
- goBack() -- 1=S
- turtle.turnRight()
- end
- -- All down now go home.
- if goToStart() then return true else return false end
- end
- function moveDownDigging()
- if turtle.detectDown() then
- if not hasPickAxe then
- print("Turtle must have a pick axe to")
- print("be able to dig.")
- setStatus("unable to dig without pickaxe")
- return false
- end
- if not turtle.digDown() then return false end
- end
- if not turtle.down() then return false end
- return true
- end
- function digForward()
- if turtle.detect() then
- if not hasPickAxe then
- print("Turtle must have a pick axe to")
- print("be able to dig.")
- setStatus("unable to dig without pickaxe")
- return false
- end
- if not turtle.dig() then return false end
- end
- return true
- end
- function addInventoryLocation()
- -- If a chest is found in inventory to be put away then create a new inventory location.
- -- Go find the first inventory location without a chest.
- if turtle.getItemCount(1) > 1 then
- print("Adding inventory location.")
- --print("Place glass blocks in slot 2.")
- --print("Place torches in slot 3.")
- --print("Place ladders in slot 4.")
- --print("Place fill material in slot 6.")
- --print("Press enter to continue:")
- --io.read()
- local level = 0
- local location = 0
- locDir = locDir + 1
- changeOrientation(locDir)
- if not moveDownDigging() then return false end
- -- Now at floor level
- level = level + 1
- if not moveDownDigging() then return false end
- -- Now at level above chests
- level = level + 1
- if not moveDownDigging() then return false end
- -- Now at level of first chests
- level = level + 1
- locLevel = 1
- while turtle.getItemCount(1) > 1 do
- -- Find next Inventory location with no chest.
- print("Checking level "..locLevel)
- for d=0,3 do
- changeOrientation(invDir + d)
- -- Check for an existing chest
- if turtle.detect() then
- turtle.select(1)
- if not turtle.compare(1) then
- while turtle.detect() do
- turtle.select(2)
- if not digForward() then return false end
- sleep(.5)
- end
- turtle.forward()
- while turtle.detect() do
- turtle.select(2)
- if not digForward() then return false end
- sleep(.5)
- end
- turtle.back()
- end
- end
- if not turtle.detect() then
- turtle.forward()
- while turtle.detect() do
- turtle.select(2)
- if not digForward() then return false end
- sleep(.5)
- end
- turtle.select(1)
- turtle.place()
- turtle.back()
- turtle.place()
- end
- end
- if turtle.detectDown() then
- turtle.select(2)
- if not moveDownDigging() then return false end
- else
- if not turtle.down() then return false end
- end
- level = level + 1
- locLevel = locLevel + 1
- end
- for l=1,level do turtle.up() end
- level = 0
- changeOrientation(unknownDir)
- for s=1,maxSlot do
- turtle.select(s)
- if turtle.getItemCount(s) > 0 then turtle.drop() end
- end
- locDir = 0
- end
- end
- -- Compare Inventory to contents of chest.
- function compareLocation(fillEmptyChests)
- local emptyChestsFound = false
- -- Set flag to indicate turtle is empty
- turtle.select(1)
- turtle.suck()
- -- Only compare the slots if the chest wasn't empty.
- local quantity = 0
- if turtle.getItemCount(1) == 0 then
- if fillEmptyChests then
- -- Put the next unidentified item in the slot to compare and place in the chest.
- for s=2,maxSlot do
- if turtle.getItemCount(s) > 0 then
- turtle.select(s)
- turtle.transferTo(1)
- quantity = quantity + turtle.getItemCount(1)
- break
- end
- end
- else
- emptyChestsFound = true
- table.insert(emptyChests, currentLocation)
- end
- end
- for s=2,maxSlot do
- -- Are there any items in this slot?
- if turtle.getItemCount(s) > 0 then
- turtle.select(s)
- if turtle.compareTo(1) then
- -- The current item matches the contents of the chest.
- quantity = quantity + turtle.getItemCount(s)
- turtle.drop()
- -- Subtract any that failed to be dropped.
- quantity = quantity - turtle.getItemCount(s)
- -- How do we detect that the chest is full? You won't be able to drop it.
- end
- end
- end
- -- Now put the items back that were in slot 1.
- -- If the chest is full the material will remain in slot 1 and needs to be moved out.
- turtle.select(1)
- turtle.drop()
- -- Update inventory of location
- local name, qty = getInventoryNameQty(currentLocation)
- qty = qty + quantity
- inventoryName[currentLocation] = name.."#"..qty
- return emptyChestsFound
- end
- -- Put Inventory Away
- function putInventoryAway()
- setStatus("putting inventory away")
- -- Make sure there is nothing is slot 1.
- if turtle.getItemCount(1) > 0 then
- changeOrientation(unknownDir)
- turtle.select(1)
- turtle.drop()
- end
- -- First put any fuel away.
- changeOrientation(fuelDir)
- for s=2,maxSlot do
- turtle.select(s)
- if turtle.compareTo(fuelSlot) then turtle.drop() end
- end
- -- Check to see if any chests are in inventory
- local chestFound = false
- for s=2,maxSlot do
- turtle.select(s)
- if turtle.compare() then
- turtle.transferTo(1)
- chestFound = true
- end
- end
- -- If there are chests then create new inventory locations with them.
- changeOrientation(invDir)
- if chestFound then addInventoryLocation() end
- changeOrientation(invDir)
- -- If there is still something in slot 1 then move it back to inventory chest.
- if turtle.getItemCount(1) > 0 then turtle.drop() end
- -- If there is still something in the turtle then put it away.
- local emptyChestsFound = false, emptyChestLevel, emptyChestDir
- emptyChests = {}
- while not isTurtleEmpty() and gotoNextInventoryLocation() and not abortMission do
- emptyChestsFound = compareLocation(false)
- if emptyChestsFound and emptyChestLevel == nil then
- emptyChestLevel = locLevel
- emptyChestDir = locDir
- end
- end
- while not isTurtleEmpty() and #emptyChests > 0 do
- local chestLocation = emptyChests[1]
- table.remove(emptyChests, 1)
- print("Filling location "..chestLocation)
- dirIdx = string.find(chestLocation, ",")
- if dirIdx ~= nil then
- emptyChestLevel = tonumber(string.sub(chestLocation, 1, dirIdx-1))
- emptyChestDir = tonumber(string.sub(chestLocation, dirIdx+1))
- gotoLocation(emptyChestLevel, emptyChestDir)
- compareLocation(true)
- end
- end
- if not goToStart() then return end
- -- If there is inventory left then put it in the Unknown inventory chest
- if not isTurtleEmpty() then emptyInventory() end
- end
- function putAwayInventory()
- -- Is there inventory to put away
- if not isTurtleEmpty() or loadInventory() then
- putInventoryAway()
- saveTable(inventoryName, "inventory")
- return true
- else
- return false
- end
- end
- function findFuelChest()
- -- Find first empty slot.
- for s=1,maxSlot do
- if turtle.getItemCount(s)==0 then
- turtle.select(s)
- break
- end
- end
- testOK = false
- for a=1,4 do
- if turtle.suck() then
- -- Was the fuel chest was found so we are in the correct location.
- if turtle.compareTo(fuelSlot) then testOK = true end
- turtle.drop()
- end
- if testOK then break else turtle.turnRight() end
- end
- return testOK
- end
- function powerUpChecks()
- if turtle.getItemCount(fuelSlot) == 0 then
- print("Place fuel in slot "..fuelSlot.." for fuel.")
- while turtle.getItemCount(fuelSlot) == 0 do
- sleep(10)
- end
- end
- getPickAxeStatus()
- --Move to the top
- local x = 0
- firstEmpty = false
- while not turtle.detectUp() do
- if turtle.up() then x = x + 1 end
- if x > 10 and not turtle.detect() and firstEmpty then
- -- We have travelled far enough
- for z=1,x do
- turtle.down()
- end
- print("Can't locate ceiling.")
- break
- end
- if not turtle.detect() then
- firstEmpty = true
- else
- firstEmpty = false
- end
- end
- print("Testing orientation.")
- local testOK = "N"
- if findFuelChest() then testOK = "Y" end
- if string.upper(testOK) ~= "Y" then
- local setupNeeded = ""
- term.write("Create inventory chests? (Y/N):")
- setupNeeded = string.upper(io.read())
- if setupNeeded == "Y" then
- hasPickAxe = true
- local onFloor = "N"
- while onFloor ~= "Y" do
- term.write("At floor level? ('Y'/'U'p/'D'own):")
- onFloor = string.upper(io.read())
- if onFloor == "U" then turtle.up() end
- if onFloor == "D" then turtle.down() end
- end
- if Initialize() then
- if findFuelChest() then
- testOK = "Y"
- orientation = fuelDir
- emptyInventory()
- changeOrientation(fuelDir)
- end
- else
- abortMission = true
- end
- else
- return false
- end
- end
- while abortMission == false and string.upper(testOK) ~= "Y" do
- turtle.turnLeft()
- print("Facing fuel chest? (Y/N):")
- testOK = string.upper(io.read())
- if testOK == "Y" then
- turtle.select(fuelSlot)
- turtle.drop(1)
- end
- end
- orientation = fuelDir
- print("Found fuel chest")
- -- Make sure nothing is in slot 1. Bad things happen if that is so.
- if turtle.getItemCount(1) > 0 then
- changeOrientation(unknownDir)
- turtle.select(1)
- turtle.drop()
- end
- changeOrientation(invDir)
- inventoryName = loadTable("inventory")
- return true
- end
- function listenForMessages()
- rednet.open("right")
- while true do
- setStatus("Listening")
- local id, msg = rednet.receive(5)
- if type(id) == "number" then
- if id ~= storeKeeperID then table.insert(queue, msg) end
- else
- return false
- end
- end
- rednet.close("right")
- end
- function setStatus(msg)
- if rednet.isOpen("right") then
- if orderComputer ~= nil then
- rednet.send(orderComputer, msg)
- else
- rednet.broadcast(msg)
- end
- end
- end
- function returnItemToInventory(slot, level, dir)
- local invLoc = level..","..dir
- if turtle.getItemCount(slot) > 0 then
- if gotoLocation(level,dir) then
- turtle.select(slot)
- local name, qty = getInventoryNameQty(invLoc)
- qty = qty + turtle.getItemCount(slot)
- inventoryName[invLoc] = name.."#"..qty
- turtle.drop()
- end
- end
- end
- function countNew()
- -- Count all of the inventory locations that don't have a quantity
- local level = 1
- local dir = 1
- while true do
- local invLoc = level..","..dir
- local name = inventoryName[invLoc]
- if name ~= nil then
- local qidx = string.find(name,"#")
- if qidx == nil then countLocation(invLoc) end
- if dir == 4 then
- dir = 1
- level = level + 1
- else
- dir = dir + 1
- end
- end
- end
- goToStart()
- end
- function countAll()
- -- Count all of the inventory locations that don't have a quantity
- local level = 1
- local dir = 1
- while true do
- local invLoc = level..","..dir
- if countLocation(invLoc) then
- if dir == 4 then
- dir = 1
- level = level + 1
- else
- dir = dir + 1
- end
- else
- break
- end
- end
- goToStart()
- end
- function countLocation(invLoc)
- -- Count item in invLoc.
- print("Counting "..invLoc)
- local totalQty = 0
- local name, quantity = getInventoryNameQty(invLoc)
- local quantity = 0
- local displayname = name
- if name == "_" then displayname = "Unidentified" end
- setStatus("counting "..displayname.." in location "..invLoc)
- local idx = string.find(invLoc.."",",")
- local level = tonumber(string.sub(invLoc,1,idx-1))
- local dir = tonumber(string.sub(invLoc,idx+1))
- if gotoLocation(level,dir) then
- turtle.select(1)
- while turtle.suck() and turtle.getItemCount(maxSlot)==0 do found = true end
- for slot=1,maxSlot do
- quantity = quantity + turtle.getItemCount(slot)
- end
- if quantity==0 then
- inventoryName = tableRemoveKey(inventoryName,invLoc)
- else
- inventoryName[invLoc] = name.."#"..quantity
- end
- setStatus(quantity.." "..displayname.." in location "..invLoc)
- -- Now put it all back
- for slot=1,maxSlot do
- if turtle.getItemCount(slot) > 0 then
- turtle.select(slot)
- turtle.drop()
- else
- break
- end
- end
- else
- return false
- end
- return true
- end
- function fetchItem(command)
- local invLoc = string.sub(command,6)
- local quantity = 1
- local quantityIdx = string.find(invLoc.."","-")
- if quantityIdx ~= nil then
- quantity = tonumber(string.sub(invLoc, quantityIdx+1))
- invLoc = string.sub(invLoc,1,quantityIdx-1)
- end
- local item,qty = getInventoryNameQty(invLoc)
- setStatus("Fetching "..item.." from "..invLoc)
- print("Fetching "..quantity.." of "..item)
- setStatus("fetching "..quantity.." of "..item)
- depositInMasterChest(item, invLoc, quantity)
- end
- function tableRemoveKey(sourceTable, keyToRemove)
- local newTable = {}
- for key,value in pairs(sourceTable) do
- if key ~= keyToRemove then newTable[key]=value end
- end
- return newTable
- end
- function emptyInMasterChest(invLoc)
- -- Locate item in inventoryName.
- local found = false
- local totalQty = 0
- print("emptying location "..invLoc)
- setStatus("emptying location "..invLoc)
- found = true
- local idx = string.find(invLoc.."",",")
- local level = tonumber(string.sub(invLoc,1,idx-1))
- local dir = tonumber(string.sub(invLoc,idx+1))
- if gotoLocation(level,dir) then
- turtle.select(1)
- print("sucking from "..invLoc)
- while turtle.suck() and turtle.getItemCount(maxSlot)==0 do found = true end
- if turtle.getItemCount(maxSlot) == 0 then
- print("removing "..invLoc)
- inventoryName = tableRemoveKey(inventoryName,invLoc)
- end
- else
- setStatus("Unable to access location")
- return false
- end
- if goToStart() then
- if found then
- if turtleInventory() == 0 then
- setStatus("Location "..invLoc.." already empty.")
- else
- for s=1,maxSlot do
- turtle.select(s)
- turtle.dropUp()
- end
- setStatus("Location "..invLoc.." emptied.")
- end
- return true
- end
- end
- setStatus("Unable to find location "..invLoc)
- return false
- end
- function turtleInventory()
- local qty = 0
- for s=1,maxSlot do
- qty = qty + turtle.getItemCount(s)
- end
- return qty
- end
- function getInventoryNameQty(invLocation)
- local name = inventoryName[invLocation]
- local qty = 0
- if name ~= nil then
- local idx = string.find(name,"#")
- if idx ~= nil then
- qty = string.sub(name,idx+1)
- if qty == nil then qty = 0 end
- name = string.sub(name,1,idx-1)
- end
- else
- name = "_"
- end
- return name,qty
- end
- function depositInMasterChest(item, invLoc, quantity)
- -- Locate item in inventoryName.
- local found = false
- local totalQty = 0
- for invLocation,value in pairs(inventoryName) do
- local name, qty = getInventoryNameQty(invLocation)
- if name == item then
- print("checking location "..invLocation)
- setStatus("checking location "..invLocation)
- found = true
- local idx = string.find(invLocation.."", ",")
- local level = tonumber(string.sub(invLocation,1,idx-1))
- local dir = tonumber(string.sub(invLocation,idx+1))
- if gotoLocation(level,dir) then
- local prevQty = 0
- turtle.select(1)
- local firstSuck = true
- totalQty = turtleInventory()
- while totalQty < quantity do
- if not turtle.suck() then
- -- No more to suck
- if firstSuck then tableRemoveKey(inventoryName,invLocation) end
- break
- else
- totalQty = turtleInventory()
- if totalQty > quantity then
- -- We sucked more than we need
- turtle.drop(totalQty - quantity)
- break
- elseif firstSuck then
- -- We need to leave at least 1 per location.
- turtle.drop(1)
- firstSuck = false
- end
- qty = totalQty - prevQty
- prevQty = totalQty
- found = true
- end
- end
- inventoryName[invLocation] = name.."#"..qty
- if totalQty == quantity then break end
- else
- setStatus("Unable to access location")
- inventoryName = tableRemoveKey(inventoryName,invLocation)
- return false
- end
- break
- end
- end
- if goToStart() then
- if found then
- if turtleInventory() == 0 then
- setStatus("only 1 "..item.." found.")
- else
- for s=1,maxSlot do
- turtle.select(s)
- turtle.dropUp()
- end
- setStatus("finished retrieving item "..item)
- end
- return true
- end
- end
- setStatus("Unable to find item "..item)
- return false
- end
- function getPickAxeStatus()
- local fname = "hasPickAxe"
- if fs.exists(fname) then
- file = fs.open(fname, "r")
- local frcd = file.readAll()
- if frcd == "Y" then hasPickAxe = true else hasPickAxe = false end
- file.close()
- else
- file = fs.open(fname, "w")
- file.write("N")
- file.close()
- hasPickAxe = false
- end
- end
- function getOrderComputer()
- local fname = "InventoryControlID"
- if fs.exists(fname) then
- file = fs.open(fname, "r")
- local frcd = file.readAll()
- orderComputer = tonumber(frcd)
- file.close()
- else
- print("Inventory Manager ID:")
- orderComputer = tonumber(io.read())
- file = fs.open(fname, "w")
- file.write(orderComputer)
- file.close()
- end
- end
- function runQueue()
- while true do
- if #queue == 0 then
- if not putAwayInventory() then
- setStatus("waiting")
- end
- return true
- else
- local command = queue[1]
- print("Queue contains "..command)
- table.remove(queue, 1)
- if command == "identify" then
- setStatus("Identify Acknowledged")
- -- Identify items in unidentified locations
- identifyInventory()
- setStatus("finished")
- elseif (command == "reportinventory") then
- setStatus("Report Acknowledged")
- reportInventory(orderComputer)
- elseif (string.sub(command,1,5) == "fetch") then
- setStatus("Fetch Acknowledged")
- fetchItem(command)
- saveTable(inventoryName, "inventory")
- setStatus("finished")
- elseif (string.sub(command,1,5) == "empty") then
- setStatus("Empty Acknowledged")
- local invLoc = string.sub(command,6)
- setStatus("Emptying "..invLoc)
- while inventoryName[invLoc] ~= nil do emptyInMasterChest(invLoc) end
- saveTable(inventoryName, "inventory")
- setStatus("finished")
- elseif (string.sub(command,1,5) == "count") then
- setStatus("Count Acknowledged")
- local invLoc = string.sub(command,7)
- if invLoc == "all" then
- countAll()
- elseif invLoc == "new" then
- countNew()
- else
- countLocation(invLoc)
- goToStart()
- end
- saveTable(inventoryName, "inventory")
- setStatus("finished")
- end
- end
- end
- end
- --------------------- Main Program ---------------------
- -- Check orientation. and move to start position.
- storeKeeperID = os.getComputerID()
- print("Computer ID: "..storeKeeperID)
- if not powerUpChecks() then return end
- print("Startup completed")
- getOrderComputer()
- while true do
- if not listenForMessages() then
- runQueue()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement