Advertisement
Renjestoo

StoreKeeper 2.0

Aug 30th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 49.35 KB | None | 0 0
  1. --[[
  2. Name: storeKeeperV
  3. Version: 2.0
  4. Description: Put items away in an inventory system made up of a network of stacked chests.  Be able
  5. to store, identify and count the contents of each chest and be able to retrieve items when requested
  6. by the InventoryControl program.
  7.  
  8. Note:  This program has been extensively rewritten from the original program written by nitrogetfingers used in his Turtle Butler example.
  9. The use of the parallel option was removed because of problems using it on a server.
  10.  
  11. Date: 01/14/13
  12. Author: Velander
  13.  
  14. Future enhancements planned:
  15. - Unloading mine carts
  16.  
  17. ]]--
  18.  
  19. -- Current inventory location
  20. location = 0
  21. locLevel = 0
  22. locDir = 0
  23. -- Current Inventory Location
  24. currentLocation = locLevel..","..locDir
  25. -- Current orientation within the room (1 = forward, 2=right, 3=back, 4=left)
  26. orientation = 1
  27. -- Flag to abort mission.
  28. abortMission = false
  29. -- Maximum slot to use to transfer inventory
  30. maxSlot = 15
  31. -- Slot to use to store fuel.
  32. fuelSlot = 16
  33. -- Setup Directions.
  34. invDir = 1
  35. fuelDir = 2
  36. unknownDir = 4
  37. -- Inventory Names
  38. inventoryName = {}
  39. -- Home Terminal
  40. orderComputer = nil
  41. storeKeeperID = nil
  42.  
  43. -- Has Pick Axe
  44. hasPickAxe = false
  45. --The list of all tasks to be done
  46. queue = {}
  47.  
  48. -- List of empty chests encountered.
  49. emptyChests = {}
  50.  
  51. function saveTable(tableData, tableName)
  52.     save = textutils.serialize(tableData)
  53.     file = fs.open(tableName, "w")
  54.     file.write(save)
  55.     file.close()
  56. end
  57. function loadTable(tableName)
  58.     if fs.exists(tableName) then
  59.       file = fs.open(tableName, "r")
  60.       vars = file.readAll()
  61.       file.close()
  62.       tableData = textutils.unserialize(vars)
  63.     else
  64.       file = fs.open(tableName, "w")
  65.       tableData = {}
  66.       vars = textutils.serialize(tableData)
  67.       file.write(vars)
  68.       file.close()
  69.     end
  70.     return tableData
  71. end
  72. function reportInventory(computerId)
  73.   invData = textutils.serialize(inventoryName)
  74.   if computerId == nil then
  75.     rednet.broadcast("inventory"..invData)
  76.   else
  77.     rednet.send(computerId, "inventory"..invData)
  78.   end
  79. end
  80. function identifyInventory()
  81.   -- If there is inventory left and there are empty chests, put it in the empty chests
  82.   if not isTurtleEmpty() then
  83.     if not goToStart() then return end
  84.     emptyInventory()
  85.   end
  86.  
  87.   -- This function locates inventory locations that have not been identified and requests descriptions.
  88.   local level = 1
  89.   local dir = 0
  90.   local slot = 0
  91.   local locationsLevel = {}
  92.   local locationsDir = {}
  93.   local locationsName = {}
  94.   local locationNotEmpty = true
  95.   setStatus("Retrieving Inventory")
  96.   while slot < maxSlot and locationNotEmpty do
  97.     for dir = 1,4 do
  98.       local invLoc = level..","..dir
  99.       local name, qty = getInventoryNameQty(invLoc)
  100.       if name == "_" then
  101.         gotoLocation(level,dir)
  102.         if turtle.detect() then
  103.           slot = slot + 1
  104.           turtle.select(slot)
  105.           if turtle.suck() then
  106.             turtle.drop(turtle.getItemCount(slot)-1)
  107.             locationsLevel[slot] = level
  108.             locationsDir[slot] = dir
  109.             print("at "..invLoc)
  110.             setStatus("at "..invLoc)
  111.             if qty ~= nil then
  112.               if tonumber(qty) > 0 then
  113.                 qty = qty - 1
  114.                 inventoryName[invLoc] = "_#"..qty
  115.               end
  116.             end
  117.           else
  118.             slot = slot - 1
  119.           end
  120.         else
  121.           locationNotEmpty = false
  122.           break
  123.         end
  124.       end
  125.       if slot == maxSlot then break end
  126.     end
  127.     level = level + 1
  128.   end
  129.   if goToStart() then
  130.     if not isTurtleEmpty() then
  131.       -- Something was loaded.  Get names for each item in
  132.       local i = 1
  133.       while locationsLevel[i] ~= nil do
  134.         if turtle.getItemCount(i) > 0 then
  135.           level = locationsLevel[i]
  136.           dir = locationsDir[i]
  137.           local name, qty = getInventoryNameQty(invLoc)
  138.           name = nil
  139.           while name == nil do
  140.             setStatus("Identify slot "..i)
  141.             term.write("Identify slot "..i.." (b=back):")
  142.             name = io.read()
  143.           end
  144.           if string.lower(name) == "b" then
  145.             if i > 1 then i = i - 2 else i = i - 1 end
  146.           else
  147.             inventoryName[level..","..dir] = name.."#"..qty
  148.             setStatus("identify"..level..","..dir.."-"..name)
  149.           end
  150.         end
  151.         i = i + 1
  152.       end
  153.       saveTable(inventoryName, "inventory")
  154.       -- Now go and put everything away.
  155.       setStatus("Putting inventory back.")
  156.       local i = 1
  157.       while locationsLevel[i] ~= nil do
  158.         if turtle.getItemCount(i) > 0 then
  159.           level = locationsLevel[i]
  160.           dir = locationsDir[i]
  161.           returnItemToInventory(i, level, dir)          
  162.         end
  163.         i = i + 1
  164.       end
  165.       goToStart()
  166.     else
  167.       setStatus("done identifying.")
  168.     end
  169.   end
  170. end
  171. -- Is there anything in the turtle.
  172. function isTurtleEmpty()
  173.   for s=1,maxSlot,1 do
  174.     if turtle.getItemCount(s) > 0 then return false end
  175.   end
  176.   return true
  177. end
  178. -- Change the orientation of the turtle
  179. function changeOrientation(dir)
  180.   if dir < 1 then dir = dir + 4 end
  181.   if dir > 4 then dir = dir - 4 end
  182.   while orientation ~= dir do
  183.     if (orientation - dir) == -3 or (orientation - dir) == 1 then
  184.       turtle.turnLeft()
  185.       orientation = orientation - 1
  186.     else
  187.       turtle.turnRight()
  188.       orientation = orientation + 1
  189.     end
  190.         if orientation > 4 then
  191.       orientation = 1
  192.     elseif orientation == 0 then
  193.       orientation = 4
  194.     end
  195.     end
  196. end
  197. --Go to starting position
  198. function goToStart()
  199.   print("Going to start.")
  200.   setStatus("going to start")
  201.   refuel()
  202.   -- Move out of chest shaft.
  203.   local ceilingFound = false
  204.   for a=1,70 do
  205.     refuel()
  206.     if not turtle.detectUp() then turtle.down() else
  207.       ceilingFound = true
  208.       break
  209.     end
  210.   end
  211.   if not ceilingFound then
  212.     for b=1,70 do
  213.       refuel()
  214.       turtle.up()
  215.     end
  216.     print("Ceiling not found.")
  217.     return false
  218.   end
  219.   changeOrientation(invDir)  -- Now facing material chest
  220.  
  221.   -- Reset location to indicate at Start position.
  222.   location = 0
  223.   locLevel = 0
  224.   locDir = 0
  225.   currentLocation = locLevel..","..locDir
  226.   changeOrientation(invDir)
  227.   return true
  228. end
  229. function refuel()
  230.   -- Refuel
  231.   if turtle.getFuelLevel() < 100 then
  232.     print("Refueling.")
  233.     setStatus("refueling")
  234.     turtle.select(fuelSlot)
  235.         changeOrientation(fuelDir)
  236.     while turtle.getFuelLevel() < 100 do
  237.       if turtle.getItemCount(fuelSlot) <= 1 then
  238.         if not turtle.suck() then
  239.           print("Place more fuel in slot "..fuelSlot)
  240.           setStatus("waiting for fuel")
  241.           sleep(20)
  242.         end
  243.       end
  244.       turtle.refuel(1)
  245.     end
  246.     -- Put the extra fuel back.
  247.     print("Refuelled to level "..turtle.getFuelLevel())
  248.     turtle.select(1)
  249.     end
  250.     changeOrientation(invDir)
  251.   return true
  252. end
  253. -- Go to a specific inventory location.
  254. function gotoLocation(level, dir)
  255.   if locDir == 0 then
  256.     -- Should be 1 above floor
  257.     if not turtle.up() then return false end  -- Now even with floor
  258.     if not turtle.up() then return false end  -- Now 1 below fllor
  259.     if not turtle.up() then return false end  -- Now even with the first level of chest.
  260.     locLevel = 1
  261.     changeOrientation(1)
  262.     locDir = 1
  263.     currentLocation = locLevel..","..locDir
  264.   end
  265.   while level ~= locLevel do
  266.     refuel()
  267.     if locLevel < level  then
  268.       if turtle.up() then locLevel = locLevel + 1 else return false end
  269.     else
  270.       if turtle.down() then locLevel = locLevel - 1 else return false end
  271.     end
  272.     currentLocation = locLevel..","..locDir
  273.   end
  274.   -- Now must be at the correct level.
  275.   changeOrientation(dir)
  276.   locDir = dir
  277.   return turtle.detect()
  278. end
  279. -- Go to next inventory location.
  280. function gotoNextInventoryLocation()
  281.     -- Check to see if the turtle has started a level.
  282.   if locDir == 0 then
  283.     -- Should be 1 above floor
  284.     turtle.down()  -- Now even with floor
  285.     turtle.down()  -- Now 1 below fllor
  286.     turtle.down()  -- Now even with the first level of chest.
  287.     locLevel = 1
  288.     changeOrientation(1)
  289.     locDir = 1
  290.     currentLocation = locLevel..","..locDir
  291.   elseif locDir == 4 then
  292.     if turtle.detectUp() then
  293.       -- We have reached the bottom.
  294.       return false
  295.     else
  296.       -- Move down to the next chest.
  297.       while not turtle.down() do
  298.         setStatus("blocked at level "..locLevel)
  299.         sleep(10)
  300.       end
  301.       locLevel = locLevel + 1
  302.       locDir = 1
  303.       changeOrientation(locDir)
  304.       currentLocation = locLevel..","..locDir
  305.       -- Check to see if there isn't a chest there.
  306.       if not turtle.detect() then return false end
  307.     end
  308.   else
  309.     locDir = locDir + 1
  310.     changeOrientation(locDir)
  311.     currentLocation = locLevel..","..locDir
  312.     -- Check to see if there isn't a chest there.
  313.     if not turtle.detect() then return false end
  314.   end
  315.   return true
  316. end
  317.  
  318. -- Load Inventory To Put Away
  319. function loadInventory()
  320.     -- Pick up all of the items in the chest in front of the turtle.
  321.     -- Set the flag that the inventory is empty.
  322.     inventoryLoaded = false
  323.     -- Start filling at slot 2.  Keep slot 1 clear for comparing to contents of chest.
  324.     s=2
  325.     while s <= maxSlot do    
  326.         turtle.select(s)
  327.         if turtle.suck() then
  328.             -- Something was picked up so reset the flag.
  329.             inventoryLoaded = true
  330.         else
  331.             -- The inventory chest is empty.
  332.             break
  333.         end
  334.         -- Move to the next slot in the turtle.
  335.         s = s + 1
  336.     end
  337.     return inventoryLoaded
  338. end
  339.  
  340. -- Empty non-identified items
  341. function emptyInventory()
  342.     print("Emptying inv.")
  343.   setStatus("emptying inventory")
  344.     changeOrientation(unknownDir)
  345.     -- Dump everything into chest of unknown material.
  346.     for s=1,maxSlot,1 do
  347.         turtle.select(s)
  348.         turtle.drop()
  349.     end
  350.     -- Return to Start position
  351.     changeOrientation(invDir)
  352. end
  353. function digFoward()
  354.   turtle.select(6)
  355.   while turtle.detect() do
  356.     turtle.dig()
  357.     sleep(.5)
  358.   end
  359. end
  360. function selectFill()
  361.   for s=7,15 do
  362.     turtle.select(s)
  363.     if turtle.compareTo(6) then return end
  364.   end
  365.   turtle.select(6)
  366. end
  367. function placeSeal()
  368.   selectFill()
  369.   while not turtle.detect() do
  370.     turtle.place()
  371.     sleep(.5)
  372.   end
  373. end
  374. function placeSealDown()
  375.   selectFill()
  376.   while not turtle.detectDown() do
  377.     turtle.placeDown()
  378.     sleep(.5)
  379.   end
  380. end
  381. function digUp()
  382.   turtle.select(6)
  383.   while turtle.detectUp() do
  384.     turtle.digUp()
  385.     sleep(.5)
  386.   end
  387. end
  388. function goForward()
  389.   while not turtle.forward() do
  390.     print("Forward blocked")
  391.     sleep(4)
  392.   end
  393. end
  394. function goBack()
  395.   while not turtle.back() do
  396.     print("Back blocked")
  397.     sleep(4)
  398.   end
  399. end
  400. function selectChest()
  401.   for s=5,15 do
  402.     turtle.select(s)
  403.     if turtle.compareTo(1) then return true end
  404.   end
  405.   if turtle.getItemCount(1) > 0 then
  406.     turtle.select(1)
  407.     return true
  408.   end
  409.   return false
  410. end
  411. function placeChest()
  412.   selectChest()
  413.   turtle.place()
  414. end
  415. function placeChestUp()
  416.   selectChest()
  417.   turtle.placeUp()
  418. end  
  419. function selectGlass()
  420.   for s=5,15 do
  421.     turtle.select(s)
  422.     if turtle.compareTo(2) then return true end
  423.   end
  424.   turtle.select(2)
  425.   if turtle.getItemCount(2) > 0 then
  426.     turtle.select(2)
  427.     return true
  428.   end
  429.   return false
  430. end
  431. function placeGlass()
  432.   if selectGlass() then
  433.     turtle.place()
  434.   else
  435.     placeSeal()
  436.   end
  437. end
  438. function selectTorch()
  439.   for s=5,15 do
  440.     turtle.select(s)
  441.     if turtle.compareTo(3) then return true end
  442.   end
  443.   if turtle.getItemCount(3) > 0 then
  444.     turtle.select(3)
  445.     return true
  446.   end
  447.   return false
  448. end
  449. function placeTorchDown()
  450.   if selectTorch() then
  451.     turtle.placeDown()
  452.     return true
  453.   end
  454.   return false
  455. end
  456. function selectLadder()
  457.   for s=5,15 do
  458.     turtle.select(s)
  459.     if turtle.compareTo(4) then return true end
  460.   end
  461.   if turtle.getItemCount(4) > 0 then
  462.     turtle.select(4)
  463.     return true
  464.   end
  465.   return false
  466. end
  467. function placeLadderDown()
  468.   if selectLadder() then
  469.     turtle.placeDown()
  470.     return true
  471.   end
  472.   return false
  473. end
  474. function placeSign(message)
  475.   if hasSigns then
  476.     if not turtle.detect() then
  477.       goForward()
  478.       if not turtle.detect() then placeSeal() end
  479.       goBack()
  480.     end
  481.     turtle.select(5)
  482.     turtle.place(message)
  483.   end
  484. end
  485. -- Create Initial Chest Setup
  486. function Initialize()
  487.   if not hasPickAxe then
  488.     print("Turtle must have a pick axe to")
  489.     print("be able to create warehouse shaft.")
  490.     return false
  491.   end
  492.   -- First check that you are facing a wall.
  493.   local wallDetected = false
  494.   for a=1,4 do
  495.     if turtle.detect() then
  496.       wallDetected = true
  497.       break
  498.     end
  499.     turtle.turnRight()
  500.   end
  501.   if not wallDetected then
  502.     print("Turtle must be next to a wall to create initial setup.")
  503.     return false
  504.   end
  505.   -- Now give instructions about where to put chests.
  506.   while turtle.getItemCount(fuelSlot) < 5 do
  507.     print("Place 5+ fuel in slot "..fuelSlot..".")
  508.     term.write("Ready? (Y/N):")
  509.     continue = string.upper(io.read())
  510.     if continue ~= "Y" then return false end
  511.   end
  512.   while turtle.getItemCount(1) < 10 do
  513.     print("Place the following material in the slots.")
  514.     print("Slot 1: 30+ chests")
  515.     print("Slot 2: 30+ glass blocks")
  516.     print("Slot 3: 20+ torches")
  517.     print("Slot 4: 30+ ladders")
  518.     print("Slot 5: 3 signs")
  519.     print("Slot 6: Material to seal walls")
  520.     print("Place additional chests, glass and material in other slots.")
  521.     term.write("Ready? (Y/N):")
  522.     continue = string.upper(io.read())
  523.     if continue ~= "Y" then return false end
  524.   end
  525.   -- Check for clearance to place chests
  526.   refuel()
  527.   rightOk = false
  528.   backOk = false
  529.   leftOk = false
  530.   upOk = false
  531.   hasGlass = turtle.getItemCount(2) > 30
  532.   hasTorches = turtle.getItemCount(3) > 20
  533.   hasLadders = turtle.getItemCount(4) > 30
  534.   hasSigns = turtle.getItemCount(5) > 3
  535.   while not (rightOk and leftOk and backOk and upOk) do
  536.     -- Check to the right
  537.     turtle.turnRight()
  538.     -- Facing right
  539.     if not turtle.detect() then
  540.       if turtle.forward() then
  541.         if not turtle.detect() then rightOk = true end
  542.         turtle.back()
  543.       end
  544.     end
  545.     -- Check to the back.
  546.     turtle.turnRight()
  547.     -- Facing front
  548.     if not turtle.detect() then backOk = true end
  549.     -- Check to the left
  550.     turtle.turnRight()
  551.     -- Facing left
  552.     if not turtle.detect() then
  553.       if turtle.forward() then
  554.         if not turtle.detect() then leftOk = true end
  555.         turtle.back()
  556.       end
  557.     end
  558.     -- Check Up
  559.     if not turtle.detectUp() then upOk = true end
  560.     turtle.turnRight()
  561.     -- Facing back
  562.     if not upOk then print("Space above turtle must be clear.") end
  563.     if not rightOk then print("2 spaces to right must be clear.") end
  564.     if not backOk then print("Space behind turtle must be clear.") end
  565.     if not leftOk then print("2 spaces to left must be clear.") end
  566.     if not (rightOk and leftOk and backOk and upOk) then
  567.       print("Continue?")
  568.       cont = io.read()
  569.     end
  570.   end
  571.   ---------- BLOCK PATTERN ---------
  572.   --             Back
  573.   --               X
  574.   --            P  U  W
  575.   --         R  O  T  V  Y
  576.   -- Left 11 N  M  S  A  B 12 Right
  577.   --      10 L  I  G  E  C 13
  578.   --       9 K  J  H  F  D 14
  579.   --         8  1  2  3  4
  580.   --            7  6  5
  581.   --             Front
  582.   -- Everthing ready, start setting things up.
  583.   refuel()
  584.   -- Place double unidentified chest
  585.   turtle.turnRight()
  586.   -- Facing right
  587.   placeChest()
  588.   -- Now put a sign on it.
  589.   if hasSigns then
  590.     if turtle.down() then
  591.       turtle.turnRight()
  592.       if turtle.forward() then
  593.         turtle.turnLeft()
  594.         if turtle.forward() then
  595.           turtle.turnLeft()
  596.           placeSign("\nOverflow\nInventory")
  597.           turtle.turnRight()
  598.           turtle.back()
  599.         end
  600.         turtle.turnRight()
  601.         turtle.back()
  602.       end
  603.       turtle.turnLeft()
  604.       turtle.up()
  605.     end
  606.   end
  607.   -- Place double fuel chest
  608.   turtle.turnRight()
  609.   turtle.turnRight()
  610.   -- Facing left
  611.   placeChest()
  612.   -- Now put some fuel in it.
  613.   turtle.select(fuelSlot)
  614.   turtle.drop(1)
  615.   -- Now put a sign on it.
  616.   if hasSigns then
  617.     if turtle.down() then
  618.       turtle.turnLeft()
  619.       if turtle.forward() then
  620.         turtle.turnRight()
  621.         if turtle.forward() then
  622.           turtle.turnRight()
  623.           placeSign("\nStoreKeeper\nFuel")
  624.           turtle.turnLeft()
  625.           turtle.back()
  626.         end
  627.         turtle.turnLeft()
  628.         turtle.back()
  629.       end
  630.       turtle.turnRight()
  631.       turtle.up()
  632.     end
  633.   end
  634.   -- Place retrieval chest
  635.   turtle.turnRight()
  636.   -- Facing back
  637.   placeChestUp()
  638.   -- Now put a sign on it.
  639.   if turtle.back() then
  640.     if turtle.down() then
  641.       if turtle.down() then
  642.         placeSign("\nRequested\nItems")
  643.         turtle.up()
  644.       end
  645.       turtle.up()
  646.       turtle.forward()
  647.     end
  648.   end
  649.   -- Place unknown item chest
  650.   turtle.turnRight()
  651.   turtle.turnRight()
  652.   -- Facing front
  653.   placeChest()
  654.   turtle.turnRight()
  655.   turtle.turnRight()
  656.   -- Facing back
  657.  
  658.   -- Now replace the floor with glass blocks and a crawl hole  
  659.   refuel()
  660.   local level = 0
  661.   turtle.turnRight()
  662.   -- Facing right
  663.   turtle.digDown()
  664.   turtle.up()
  665.   level = level + 1
  666.   -- If we have glass blocks then replace floor with glass blocks
  667.   -- Replace floor to right
  668.   digFoward()
  669.   goForward() -- A
  670.   turtle.turnLeft() -- Facing back
  671.   placeGlass() -- V
  672.   turtle.turnRight() -- Facing left
  673.   digFoward()
  674.   goForward() -- B
  675.   turtle.turnLeft() -- Facing back
  676.   placeGlass() -- Y
  677.   turtle.turnRight() -- Facing left
  678.   digFoward()
  679.   goForward() -- 12
  680.   turtle.turnRight() -- Facing front
  681.   digFoward()
  682.   goForward() -- 13
  683.   digFoward()
  684.   goForward() -- 14
  685.   digFoward()
  686.   placeSeal() -- 15
  687.   goBack() -- 13
  688.   placeSeal() -- 14
  689.   goBack() -- 12
  690.   placeSeal() -- 13
  691.   turtle.turnLeft() -- Facing right
  692.   goBack() -- B
  693.   placeSeal() -- 12
  694.   turtle.turnRight() -- Facing front
  695.   digFoward()
  696.   goForward() -- C
  697.   digForward()
  698.   goForward() -- D
  699.   goBack() -- C
  700.   goBack() -- B
  701.   placeGlass() -- C
  702.   turtle.turnLeft() -- Facing right
  703.   goBack() -- A
  704.   placeGlass() -- B
  705.   turtle.turnRight() -- Facing front
  706.   digFoward()
  707.   placeGlass() -- E
  708.   turtle.turnLeft() -- Facing right
  709.   goBack() -- Start
  710.   placeGlass() -- A
  711.   turtle.turnRight() -- Facing front
  712.   -- Replace floor to front with glass
  713.   digFoward()
  714.   goForward() -- G
  715.   digFoward()
  716.   goForward() -- H
  717.   digFoward()
  718.   goForward() -- 2
  719.   turtle.turnLeft() -- Facing right
  720.   digFoward()
  721.   goForward() -- 3
  722.   placeGlass() -- 4
  723.   goBack() -- 2
  724.   placeGlass() -- 3
  725.   turtle.turnRight() -- Facing front
  726.   turtle.turnRight() -- Facing left
  727.   digFoward()
  728.   goForward() -- 1
  729.   digForward()
  730.   goForward() -- 8
  731.   placeGlass() -- 16
  732.   goBack() -- 1
  733.   placeGlass() -- 8
  734.   goBack() -- 2
  735.   placeGlass() -- 1
  736.   turtle.turnLeft() -- Facing front
  737.   goBack() -- H
  738.   placeGlass() -- 2
  739.   turtle.turnLeft() -- Facing right
  740.   digFoward()
  741.   goForward() -- F
  742.   digFoward()
  743.   goBack() -- H
  744.   placeGlass() -- F
  745.   turtle.turnRight() -- Facing front
  746.   turtle.turnRight() -- Facing left
  747.   digFoward()
  748.   goForward() -- J
  749.   digFoward() -- K
  750.   goForward() -- K
  751.   placeGlass() -- 9
  752.   goBack() -- J
  753.   goBack() -- H
  754.   placeGlass() -- J
  755.   turtle.turnLeft() -- Facing front
  756.   goBack() -- G
  757.   placeGlass() -- H
  758.   goBack() -- S
  759.   placeGlass() -- G
  760.   -- Replace floor on left
  761.   turtle.turnRight() -- Facing left
  762.   digFoward()
  763.   goForward() -- M
  764.   turtle.turnLeft() -- Facing front
  765.   digFoward()
  766.   placeGlass() -- I
  767.   turtle.turnRight() -- Facing left
  768.   digFoward()
  769.   goForward() -- N
  770.   digForward()
  771.   goForward() -- 11
  772.   turtle.turnLeft() -- Facing front
  773.   placeGlass() -- 10
  774.   turtle.turnRight() -- Facing left
  775.   goBack() -- N
  776.   turtle.turnLeft() -- Facing front
  777.   digFoward()
  778.   placeGlass() -- L
  779.   turtle.turnRight() -- Facing left
  780.   placeSeal() -- 11
  781.   goBack() -- M
  782.   placeGlass() -- N
  783.   goBack() -- Start
  784.   placeGlass() -- M
  785.   turtle.turnRight() -- Facing back
  786.   -- Put fill in for the back
  787.   digForward()
  788.   goForward() -- T
  789.   turtle.turnLeft() -- Facing left
  790.   digForward() -- O
  791.   goForward() -- O
  792.   placeGlass() -- R
  793.   goBack() -- T
  794.   placeGlass() -- O
  795.   turtle.turnRight() -- Facing back
  796.   digForward() -- U
  797.   goForward() -- U
  798.   turtle.turnLeft() -- Facing left
  799.   placeSeal() -- P
  800.   turtle.turnRight() -- Facing back
  801.   placeGlass() -- X
  802.   turtle.turnRight() -- Facing right
  803.   placeGlass() -- W
  804.   turtle.turnLeft() -- Facing back
  805.   goBack() -- T
  806.   placeGlass() -- U
  807.   goBack() -- S
  808.   placeGlass() -- T w
  809.   turtle.turnRight() -- Facing right
  810.   turtle.digUp()
  811.   turtle.down()
  812.   level = level + 1
  813.   -- Now start placing chests for warehouse until we run out of chests.
  814.   -- Clear out space above chests.
  815.   refuel()
  816.   for d=1,4 do
  817.     digFoward()
  818.     goForward() -- 1=A, 2=G, 3=M, 4=T
  819.     if d == 1 or d == 4 then
  820.       turtle.turnLeft() -- Facing 1=back, 3=left
  821.       placeSeal() -- 1=V, 4=O
  822.       turtle.turnRight() -- Facing 1=right, 3=back
  823.     elseif d == 2 or d == 4 then
  824.       turtle.turnLeft() -- Facing right
  825.       if d == 2 then digFoward() else placeSeal() end -- O
  826.       turtle.turnRight() -- Facing front
  827.       turtle.turnRight() -- Facing left
  828.       if d == 2 then digFoward() else placeSeal() end -- V
  829.       turtle.turnLeft() -- Facing front
  830.     end
  831.     digFoward()
  832.     goForward() -- 1=B, 2=H, 3=N, 4=U
  833.     if d == 1 or d == 4 then
  834.       placeSeal() -- 1=12, 4=X
  835.       turtle.turnLeft() -- Facing 1=back, 4=left
  836.       placeSeal() -- 1=Y, 4=P
  837.       turtle.turnRight() -- Facing 1=right, 4=back
  838.       turtle.turnRight() -- Facing 1=front, 4=right
  839.       if d == 1 then
  840.         digFoward()
  841.         placeGlass() -- C
  842.       elseif d == 4 then placeSeal() end -- W
  843.       turtle.turnLeft() -- Facing 1=right, 4=back
  844.     elseif d == 2 then
  845.       turtle.turnLeft() -- Facing 2=right
  846.       digForward() -- F
  847.       goForward() -- F
  848.       digForward() -- D
  849.       goForward() -- D
  850.       placeSeal() -- 14
  851.       goBack() -- F
  852.       goBack() -- H
  853.       placeGlass() -- F
  854.       turtle.turnRight() -- Facing front
  855.       turtle.turnRight() -- Facing left
  856.       digForward()
  857.       goForward() -- J
  858.       digForward() -- K
  859.       goForward() -- K
  860.       placeSeal() -- 9
  861.       turtle.turnLeft() -- Facing front
  862.       placeSeal() -- 8
  863.       turtle.turnRight() -- Facing left
  864.       goBack() -- J
  865.       goBack() -- H
  866.       placeGlass() -- J
  867.       turtle.turnLeft() -- Facing front
  868.       digForward()
  869.       goForward() -- 2
  870.       placeSeal() -- 6
  871.       turtle.turnLeft() -- Facing right
  872.       digForward() -- 3
  873.       goForward() -- 3
  874.       placeSeal() -- 4
  875.       goBack() -- 2
  876.       placeGlass() -- 3
  877.       turtle.turnRight() -- Facing front
  878.       turtle.turnRight() -- Facing left
  879.       digForward() -- 1
  880.       placeGlass() -- 1
  881.       turtle.turnLeft() -- Facing front
  882.       goBack() -- H
  883.     elseif d == 3 then
  884.       placeSeal() -- 11
  885.       turtle.turnLeft() -- Facing front
  886.       digForward()
  887.       placeGlass() -- L
  888.       turtle.turnRight() -- Facing left
  889.       turtle.turnRight() -- Facing back
  890.       placeSeal() -- R
  891.       turtle.turnLeft() -- Facing left      
  892.     end
  893.     goBack() -- 1=A, 2=G, 3=M, 4=T
  894.     goBack() -- Start
  895.     turtle.turnRight() -- Facing 1=front, 2=left, 3=back, 4=right
  896.   end
  897.   -- Facing right
  898.   -- S
  899.   -- Now start adding layers of chests until they are all gone.
  900.   while turtle.getItemCount(1) > 1 do
  901.     refuel()
  902.     turtle.digUp()
  903.     turtle.down()
  904.     level = level + 1
  905.     for d=1,4 do
  906.       -- Facing 1=right, 2=front, 3=left, 4=back
  907.       turtle.select(5)
  908.       digForward() -- 1=A, 2=G, 3=M, 4=T
  909.       goForward() -- 1=A, 2=G, 3=M, 4=T      
  910.       if d == 1 or d == 4 then
  911.         turtle.turnLeft() -- Facing 1=back, 3=left
  912.         placeSeal() -- 1=V, 4=O
  913.         turtle.turnRight() -- Facing 1=right, 3=back
  914.       elseif d == 2 or d == 4 then
  915.         turtle.turnLeft() -- Facing right
  916.         if d == 2 then
  917.           digFoward()
  918.           if (level-2)%6 == 0 then placeGlass() else -- E
  919.             goForward()
  920.             goBack()
  921.           end
  922.         else placeSeal() end -- O
  923.         turtle.turnRight() -- Facing front
  924.         turtle.turnRight() -- Facing left
  925.         if d == 2 then
  926.           digFoward()
  927.           if (level-2)%6 == 0 then placeGlass() else -- I
  928.             goForward()
  929.             goBack()
  930.           end
  931.         else placeSeal() end -- V
  932.         turtle.turnLeft() -- Facing front
  933.       end
  934.       digForward() -- 1=B, 2=H, 3=N, 4=U
  935.       goForward() -- 1=B, 2=H, 3=N, 4=U
  936.       if (d == 1) then
  937.         placeSeal() -- 12
  938.         turtle.turnLeft() -- Facing back
  939.         placeSeal() -- Y
  940.         turtle.turnRight() -- Facing right
  941.         turtle.turnRight() -- Facing front
  942.         digForward() -- C
  943.         goForward()
  944.         goBack()
  945.         if not(level%6 == 0 or (level-1)%6 == 0) then
  946.           placeGlass() -- F
  947.         else
  948.           goForward() -- C
  949.           turtle.turnLeft() -- Facing right
  950.           placeSeal() -- 13
  951.           turtle.turnRight() -- Facing front
  952.           goBack() -- B
  953.         end
  954.         turtle.turnLeft() -- Facing 2=right        
  955.       elseif (d == 2) then
  956.         turtle.turnLeft() -- Facing 2=right
  957.         digForward() -- F
  958.         goForward() -- F
  959.         digForward() -- D
  960.         goForward() -- D
  961.         placeSeal() -- 14
  962.         placeLadderUp() -- D
  963.         turtle.turnRight() -- Facing front
  964.         placeSeal() -- 4
  965.         turtle.turnLeft() -- Facing right
  966.         goBack() -- F
  967.         turtle.turnRight() -- Facing 2=front
  968.         digForward() -- 3
  969.         if level%8 then
  970.           placeGlass() -- 3
  971.         else
  972.           goForward() -- 3
  973.           placeSeal() -- 5
  974.           goBack() -- F
  975.         end
  976.         turtle.turnLeft() -- Facing 2=right
  977.         goBack() -- H
  978.         if not(level%6 == 0 or (level-1)%6 == 0) then placeGlass() end -- F
  979.         turtle.turnRight() -- Facing 2=front
  980.         digForward() -- 2
  981.         if level%8 then
  982.           placeGlass() -- 2
  983.         else
  984.           goForward() -- 2
  985.           placeSeal() -- 6
  986.           goBack() -- H
  987.         end
  988.         turtle.turnRight() -- Facing 2=left
  989.         digForward() -- J
  990.         goForward() -- J
  991.         digForward() -- K
  992.         goForward() -- K
  993.         placeSeal() -- 9
  994.         placeLadderUp() -- K
  995.         turtle.turnLeft() -- Facing front
  996.         placeSeal() -- 8
  997.         turtle.turnRight() -- Facing left
  998.         goBack() -- J
  999.         turtle.turnLeft() -- Facing 2=front
  1000.         digForward() -- 1
  1001.         if level%8 then
  1002.           placeGlass() -- 1
  1003.         else
  1004.           goForward() -- 1
  1005.           placeSeal() -- 7
  1006.           goBack() -- J
  1007.         end
  1008.         turtle.turnRight() -- Facing 2=left
  1009.         goBack() -- H
  1010.         if not(level%6 == 0 or (level-1)%6 == 0) then placeGlass() end -- J
  1011.         turtle.turnLeft() -- Facing 2=front
  1012.       elseif (d == 3) then
  1013.         placeSeal() -- 11
  1014.         turtle.turnRight() -- Facing back
  1015.         placeSeal() -- R
  1016.         turtle.turnLeft() -- Facing left
  1017.         turtle.turnLeft() -- Facing 3=front
  1018.         digForward() -- L
  1019.         if not(level%6 == 0 or (level-1)%6 == 0) then
  1020.           placeGlass() -- L
  1021.         else
  1022.           goForward() -- L
  1023.           turtle.turnRight() -- Facing left
  1024.           placeSeal() -- 10
  1025.           placeTorchUp()
  1026.           turtle.turnLeft() -- Facing front
  1027.           goBack() -- N
  1028.         end
  1029.         turtle.turnRight() -- Facing 3=left          
  1030.       elseif (d == 4) then
  1031.         placeSeal() -- X
  1032.         turtle.turnLeft() -- Facing left
  1033.         placeSeal() -- P
  1034.         turtle.turnRight() -- Facing back
  1035.         turtle.turnRight() -- Facing right
  1036.         placeSeal() -- W
  1037.         turtle.turnLeft() -- Facing back
  1038.       end
  1039.       -- Facing 1=right, 2=front, 3=left, 4=back
  1040.       goBack() -- 1=A, 2=G, 3=M, 4=T
  1041.       goBack() -- Start
  1042.       turtle.turnRight() -- Facing 1=front, 2=left, 3=back, 3=right
  1043.     end
  1044.     for d=1,4 do
  1045.       placeChest() -- 1=A, 2=G, 3=M, 4=T
  1046.       turtle.turnRight() -- Facing 1=front, 2=left, 3=back, 3=right
  1047.     end
  1048.     print("Finished layer "..level)
  1049.   end
  1050.   -- Now put in an empty space below the last chest.
  1051.   digDown()
  1052.   turtle.up()
  1053.   for d=1,4 do
  1054.     digForward()
  1055.     goForward() -- 1=A, 2=G, 3=M, 4=T
  1056.     turtle.turnLeft()
  1057.     digForward()
  1058.     goForward()
  1059.     placeSeal()
  1060.     goBack()
  1061.     turtle.turnRight()    
  1062.     digForward()
  1063.     goForward() -- 1=B, 2=H, 3=N, 4=U
  1064.     turtle.turnLeft()
  1065.     digForward()
  1066.     goForward()
  1067.     placeSeal()
  1068.     goBack()
  1069.     turtle.turnRight()    
  1070.     turtle.turnRight()
  1071.     digForward()
  1072.     goForward() -- 1=C, 2=H, 3=N, 4=U
  1073.     digForward()
  1074.     goForward() -- 1=D, 2=H, 3=N, 4=U
  1075.     placeSeal()
  1076.     placeLadderUp()
  1077.     goBack() -- 1=C
  1078.     goBack() -- 1=B
  1079.     turtle.turnLeft()
  1080.     goBack() -- 1=A
  1081.     goBack() -- 1=S
  1082.     turtle.turnRight()
  1083.   end
  1084.   -- Now seal the bottom.
  1085.   digDown()
  1086.   turtle.up()
  1087.   placeSealDown()
  1088.   for d=1,4 do
  1089.     digForward()
  1090.     goForward() -- 1=A, 2=G, 3=M, 4=T
  1091.     placeSealDown()
  1092.     turtle.turnLeft()
  1093.     digForward()
  1094.     goForward()
  1095.     placeSeal()
  1096.     placeSealDown()
  1097.     goBack()
  1098.     turtle.turnRight()    
  1099.     digForward()
  1100.     goForward() -- 1=B, 2=H, 3=N, 4=U
  1101.     placeSealDown()
  1102.     turtle.turnLeft()
  1103.     digForward()
  1104.     goForward()
  1105.     placeSeal()
  1106.     placeSealDown()
  1107.     goBack()
  1108.     turtle.turnRight()    
  1109.     turtle.turnRight()
  1110.     digForward()
  1111.     goForward() -- 1=C, 2=H, 3=N, 4=U
  1112.     placeSealDown()
  1113.     digForward()
  1114.     goForward() -- 1=D, 2=H, 3=N, 4=U
  1115.     placeSeal()
  1116.     placeSealDown()
  1117.     placeLadderUp()
  1118.     goBack() -- 1=C
  1119.     goBack() -- 1=B
  1120.     turtle.turnLeft()
  1121.     goBack() -- 1=A
  1122.     goBack() -- 1=S
  1123.     turtle.turnRight()
  1124.   end
  1125.   -- All down now go home.
  1126.   if goToStart() then return true else return false end
  1127. end
  1128. function moveDownDigging()
  1129.   if turtle.detectDown() then
  1130.     if not hasPickAxe then
  1131.       print("Turtle must have a pick axe to")
  1132.       print("be able to dig.")
  1133.       setStatus("unable to dig without pickaxe")
  1134.       return false
  1135.     end
  1136.     if not turtle.digDown() then return false end
  1137.   end
  1138.   if not turtle.up() then return false end
  1139.   return true
  1140. end
  1141. function digForward()
  1142.   if turtle.detect() then
  1143.     if not hasPickAxe then
  1144.       print("Turtle must have a pick axe to")
  1145.       print("be able to dig.")
  1146.       setStatus("unable to dig without pickaxe")
  1147.       return false
  1148.     end
  1149.     if not turtle.dig() then return false end
  1150.   end
  1151.   return true
  1152. end
  1153. function addInventoryLocation()
  1154.   -- If a chest is found in inventory to be put away then create a new inventory location.
  1155.   -- Go find the first inventory location without a chest.
  1156.   if turtle.getItemCount(1) > 1 then
  1157.     print("Adding inventory location.")
  1158.     --print("Place glass blocks in slot 2.")
  1159.     --print("Place torches in slot 3.")
  1160.     --print("Place ladders in slot 4.")
  1161.     --print("Place fill material in slot 6.")
  1162.     --print("Press enter to continue:")
  1163.     --io.read()
  1164.     local level = 0
  1165.     local location = 0
  1166.     locDir = locDir + 1
  1167.     changeOrientation(locDir)
  1168.     if not moveDownDigging() then return false end
  1169.     -- Now at floor level
  1170.     level = level + 1
  1171.     if not moveDownDigging() then return false end
  1172.     -- Now at level above chests
  1173.     level = level + 1
  1174.     if not moveDownDigging() then return false end
  1175.     -- Now at level of first chests
  1176.     level = level + 1
  1177.     locLevel = 1
  1178.     while turtle.getItemCount(1) > 1 do
  1179.       -- Find next Inventory location with no chest.
  1180.       refuel()
  1181.       print("Checking level "..locLevel)
  1182.       for d=0,3 do
  1183.         changeOrientation(invDir + d)
  1184.         -- Check for an existing chest
  1185.         if turtle.detect() then
  1186.           turtle.select(1)
  1187.           if not turtle.compare(1) then
  1188.             while turtle.detect() do
  1189.               turtle.select(2)
  1190.               if not digForward() then return false end
  1191.               sleep(.5)
  1192.             end
  1193.             turtle.forward()
  1194.             while turtle.detect() do
  1195.               turtle.select(2)
  1196.               if not digForward() then return false end
  1197.               sleep(.5)
  1198.             end
  1199.             turtle.back()
  1200.           end
  1201.         end
  1202.         if not turtle.detect() then
  1203.           turtle.forward()
  1204.           while turtle.detect() do
  1205.             turtle.select(2)
  1206.             if not digForward() then return false end
  1207.             sleep(.5)
  1208.           end
  1209.           turtle.select(1)
  1210.           turtle.place()
  1211.           turtle.back()
  1212.           turtle.place()
  1213.         end
  1214.       end
  1215.       if turtle.detectDown() then
  1216.         turtle.select(2)
  1217.         if not moveDownDigging() then return false end
  1218.       else
  1219.         if not turtle.up() then return false end
  1220.       end
  1221.       level = level + 1
  1222.       locLevel = locLevel + 1
  1223.     end
  1224.     for l=1,level do turtle.down() end
  1225.     level = 0
  1226.     changeOrientation(unknownDir)
  1227.     for s=1,maxSlot do
  1228.       turtle.select(s)
  1229.       if turtle.getItemCount(s) > 0 then turtle.drop() end
  1230.     end
  1231.     locDir = 0
  1232.   end
  1233. end
  1234. -- Compare Inventory to contents of chest.
  1235. function compareLocation(fillEmptyChests)
  1236.   local emptyChestsFound = false
  1237.     -- Set flag to indicate turtle is empty
  1238.   turtle.select(1)
  1239.   turtle.suck()
  1240.   -- Only compare the slots if the chest wasn't empty.
  1241.   local quantity = 0
  1242.   if turtle.getItemCount(1) == 0 then
  1243.     if fillEmptyChests then    
  1244.       -- Put the next unidentified item in the slot to compare and place in the chest.
  1245.       for s=2,maxSlot do
  1246.         if turtle.getItemCount(s) > 0 then
  1247.           turtle.select(s)
  1248.           turtle.transferTo(1)
  1249.           quantity = quantity + turtle.getItemCount(1)
  1250.           break
  1251.         end
  1252.       end
  1253.     else
  1254.       emptyChestsFound = true
  1255.       table.insert(emptyChests, currentLocation)
  1256.     end
  1257.   end
  1258.   for s=2,maxSlot do
  1259.     -- Are there any items in this slot?
  1260.     if turtle.getItemCount(s) > 0 then
  1261.       turtle.select(s)
  1262.       if turtle.compareTo(1) then
  1263.         -- The current item matches the contents of the chest.
  1264.         quantity = quantity + turtle.getItemCount(s)
  1265.         turtle.drop()
  1266.         -- Subtract any that failed to be dropped.
  1267.         quantity = quantity - turtle.getItemCount(s)
  1268.         -- How do we detect that the chest is full?  You won't be able to drop it.
  1269.       end
  1270.     end
  1271.   end
  1272.   -- Now put the items back that were in slot 1.
  1273.   -- If the chest is full the material will remain in slot 1 and needs to be moved out.
  1274.   turtle.select(1)
  1275.   turtle.drop()
  1276.   -- Update inventory of location
  1277.   local name, qty = getInventoryNameQty(currentLocation)
  1278.   qty = qty + quantity
  1279.   inventoryName[currentLocation] = name.."#"..qty
  1280.   return emptyChestsFound
  1281. end
  1282. -- Put Inventory Away
  1283. function putInventoryAway()
  1284.   setStatus("putting inventory away")
  1285.   -- Make sure there is nothing is slot 1.
  1286.   if turtle.getItemCount(1) > 0 then
  1287.     changeOrientation(unknownDir)
  1288.     turtle.select(1)
  1289.     turtle.drop()
  1290.   end
  1291.   -- First put any fuel away.
  1292.   changeOrientation(fuelDir)
  1293.   for s=2,maxSlot do
  1294.     turtle.select(s)
  1295.     if turtle.compareTo(fuelSlot) then turtle.drop() end
  1296.   end
  1297.  
  1298. -- Check to see if any chests are in inventory
  1299.   local chestFound = false
  1300.   for s=2,maxSlot do
  1301.     turtle.select(s)
  1302.     if turtle.compare() then
  1303.       turtle.transferTo(1)
  1304.       chestFound = true
  1305.     end
  1306.   end
  1307.  
  1308.   -- If there are chests then create new inventory locations with them.
  1309.   changeOrientation(invDir)
  1310.   if chestFound then addInventoryLocation() end
  1311.   changeOrientation(invDir)
  1312.   -- If there is still something in slot 1 then move it back to inventory chest.
  1313.   if turtle.getItemCount(1) > 0 then turtle.drop() end
  1314.   -- If there is still something in the turtle then put it away.
  1315.   local emptyChestsFound = false, emptyChestLevel, emptyChestDir
  1316.   emptyChests = {}
  1317.     while not isTurtleEmpty() and gotoNextInventoryLocation() and not abortMission do
  1318.     emptyChestsFound = compareLocation(false)
  1319.     if emptyChestsFound and emptyChestLevel == nil then
  1320.       emptyChestLevel = locLevel
  1321.       emptyChestDir = locDir
  1322.     end
  1323.   end
  1324.   while not isTurtleEmpty() and #emptyChests > 0 do  
  1325.     local chestLocation = emptyChests[1]
  1326.     table.remove(emptyChests, 1)
  1327.     print("Filling location "..chestLocation)
  1328.     dirIdx = string.find(chestLocation, ",")
  1329.     if dirIdx ~= nil then
  1330.       emptyChestLevel = tonumber(string.sub(chestLocation, 1, dirIdx-1))
  1331.       emptyChestDir = tonumber(string.sub(chestLocation, dirIdx+1))
  1332.       gotoLocation(emptyChestLevel, emptyChestDir)
  1333.       compareLocation(true)
  1334.     end
  1335.   end
  1336.     if not goToStart() then return end
  1337.   -- If there is inventory left then put it in the Unknown inventory chest
  1338.     if not isTurtleEmpty() then emptyInventory() end
  1339. end
  1340. function putAwayInventory()
  1341.     -- Is there inventory to put away
  1342.     if not isTurtleEmpty() or loadInventory() then
  1343.         putInventoryAway()
  1344.     saveTable(inventoryName, "inventory")
  1345.     return true
  1346.   else
  1347.     return false
  1348.   end
  1349. end
  1350.  function findFuelChest()
  1351.   -- Find first empty slot.
  1352.   for s=1,maxSlot do
  1353.     if turtle.getItemCount(s)==0 then
  1354.       turtle.select(s)
  1355.       break
  1356.     end
  1357.   end
  1358.   testOK = false
  1359.   for a=1,4 do
  1360.     if turtle.suck() then
  1361.       -- Was the fuel chest was found so we are in the correct location.
  1362.       if turtle.compareTo(fuelSlot) then testOK = true end
  1363.       turtle.drop()
  1364.     end
  1365.     if testOK then break else turtle.turnRight() end
  1366.   end
  1367.   return testOK
  1368. end
  1369. function powerUpChecks()
  1370.   if turtle.getItemCount(fuelSlot) == 0 then
  1371.     print("Place fuel in slot "..fuelSlot.." for fuel.")
  1372.     while turtle.getItemCount(fuelSlot) == 0 do
  1373.       sleep(10)
  1374.     end
  1375.   end
  1376.   if turtle.getFuelLevel() < 60 then
  1377.     print("Refueling.")
  1378.     refuel()
  1379.   end
  1380.   getPickAxeStatus()
  1381.   --Move to the top
  1382.   local x = 0
  1383.   firstEmpty = false
  1384.   while not turtle.detectUp() do
  1385.     if turtle.down() then x = x + 1 end
  1386.     refuel()
  1387.    
  1388.     if x > 10 and not turtle.detect() and firstEmpty then
  1389.       -- We have travelled far enough
  1390.       for z=1,x do
  1391.         refuel()
  1392.         turtle.up()
  1393.       end
  1394.       print("Can't locate ceiling.")
  1395.       break
  1396.     end
  1397.     if not turtle.detect() then
  1398.       firstEmpty = true
  1399.     else
  1400.       firstEmpty = false
  1401.     end
  1402.   end
  1403.   print("Testing orientation.")
  1404.   local testOK = "N"
  1405.   if findFuelChest() then testOK = "Y" end
  1406.   if string.upper(testOK) ~= "Y" then
  1407.     local setupNeeded = ""
  1408.     term.write("Create inventory chests? (Y/N):")
  1409.     setupNeeded = string.upper(io.read())
  1410.     if setupNeeded == "Y" then
  1411.       hasPickAxe = true
  1412.       local onFloor = "N"
  1413.       while onFloor ~= "Y" do
  1414.         term.write("At floor level? ('Y'/'U'p/'D'own):")
  1415.         onFloor = string.upper(io.read())
  1416.         if onFloor == "U" then turtle.down() end
  1417.         if onFloor == "D" then turtle.up() end
  1418.       end
  1419.       if Initialize() then
  1420.         if findFuelChest() then
  1421.           testOK = "Y"
  1422.           orientation = fuelDir
  1423.           emptyInventory()
  1424.           changeOrientation(fuelDir)
  1425.         end
  1426.       else
  1427.         abortMission = true
  1428.       end
  1429.     else
  1430.       return false
  1431.     end
  1432.   end
  1433.   while abortMission == false and string.upper(testOK) ~= "Y" do
  1434.     turtle.turnLeft()
  1435.     print("Facing fuel chest? (Y/N):")
  1436.     testOK = string.upper(io.read())
  1437.     if testOK == "Y" then
  1438.       turtle.select(fuelSlot)
  1439.       turtle.drop(1)
  1440.     end
  1441.   end
  1442.   orientation = fuelDir
  1443.   print("Found fuel chest")
  1444.   -- Make sure nothing is in slot 1.  Bad things happen if that is so.
  1445.   if turtle.getItemCount(1) > 0 then
  1446.     changeOrientation(unknownDir)
  1447.     turtle.select(1)
  1448.     turtle.drop()
  1449.   end  
  1450.   changeOrientation(invDir)
  1451.   inventoryName = loadTable("inventory")  
  1452.   return true
  1453. end
  1454. function listenForMessages()
  1455.   rednet.open("right")
  1456.   while true do
  1457.     setStatus("Listening")
  1458.     local id, msg = rednet.receive(5)
  1459.     if type(id) == "number" then
  1460.       if id ~= storeKeeperID then table.insert(queue, msg) end
  1461.     else
  1462.       return false
  1463.     end
  1464.   end
  1465.   rednet.close("right")
  1466. end
  1467. function setStatus(msg)
  1468.   if rednet.isOpen("right") then
  1469.     if orderComputer ~= nil then
  1470.       rednet.send(orderComputer, msg)
  1471.     else
  1472.       rednet.broadcast(msg)
  1473.     end
  1474.   end
  1475. end
  1476. function returnItemToInventory(slot, level, dir)
  1477.   local invLoc = level..","..dir
  1478.   if turtle.getItemCount(slot) > 0 then
  1479.     if gotoLocation(level,dir) then
  1480.       turtle.select(slot)
  1481.       local name, qty = getInventoryNameQty(invLoc)
  1482.       qty = qty + turtle.getItemCount(slot)
  1483.       inventoryName[invLoc] = name.."#"..qty
  1484.       turtle.drop()
  1485.     end
  1486.   end
  1487. end
  1488. function countNew()
  1489.   -- Count all of the inventory locations that don't have a quantity
  1490.   local level = 1
  1491.   local dir = 1  
  1492.   while true do
  1493.     local invLoc = level..","..dir
  1494.     local name = inventoryName[invLoc]
  1495.     if name ~= nil then
  1496.       local qidx = string.find(name,"#")
  1497.       if qidx == nil then countLocation(invLoc) end
  1498.       if dir == 4 then
  1499.         dir = 1
  1500.         level = level + 1
  1501.       else
  1502.         dir = dir + 1
  1503.       end
  1504.     end
  1505.   end
  1506.   goToStart()
  1507. end
  1508. function countAll()
  1509.   -- Count all of the inventory locations that don't have a quantity
  1510.   local level = 1
  1511.   local dir = 1  
  1512.   while true do
  1513.     local invLoc = level..","..dir
  1514.     if countLocation(invLoc) then
  1515.       if dir == 4 then
  1516.         dir = 1
  1517.         level = level + 1
  1518.       else
  1519.         dir = dir + 1
  1520.       end
  1521.     else
  1522.       break
  1523.     end
  1524.   end
  1525.   goToStart()
  1526. end
  1527. function countLocation(invLoc)
  1528.   -- Count item in invLoc.
  1529.   print("Counting "..invLoc)
  1530.   local totalQty = 0
  1531.   local name, quantity = getInventoryNameQty(invLoc)
  1532.   local quantity = 0  
  1533.   local displayname = name
  1534.   if name == "_" then displayname = "Unidentified" end
  1535.   setStatus("counting "..displayname.." in location "..invLoc)
  1536.   local idx = string.find(invLoc.."",",")
  1537.   local level = tonumber(string.sub(invLoc,1,idx-1))
  1538.   local dir = tonumber(string.sub(invLoc,idx+1))
  1539.   if gotoLocation(level,dir) then
  1540.     turtle.select(1)
  1541.     while turtle.suck() and turtle.getItemCount(maxSlot)==0 do found = true end
  1542.     for slot=1,maxSlot do
  1543.       quantity = quantity + turtle.getItemCount(slot)
  1544.     end
  1545.     if quantity==0 then
  1546.       inventoryName = tableRemoveKey(inventoryName,invLoc)
  1547.     else
  1548.       inventoryName[invLoc] = name.."#"..quantity
  1549.     end
  1550.    
  1551.     setStatus(quantity.." "..displayname.." in location "..invLoc)
  1552.     -- Now put it all back
  1553.     for slot=1,maxSlot do
  1554.       if turtle.getItemCount(slot) > 0 then
  1555.         turtle.select(slot)
  1556.         turtle.drop()
  1557.       else
  1558.         break
  1559.       end
  1560.     end
  1561.   else
  1562.     return false
  1563.   end
  1564.   return true
  1565. end
  1566. function fetchItem(command)
  1567.   local invLoc = string.sub(command,6)
  1568.   local quantity = 1
  1569.   local quantityIdx = string.find(invLoc.."","-")
  1570.   if quantityIdx ~= nil then
  1571.     quantity = tonumber(string.sub(invLoc, quantityIdx+1))
  1572.     invLoc = string.sub(invLoc,1,quantityIdx-1)
  1573.   end
  1574.   local item,qty = getInventoryNameQty(invLoc)
  1575.   setStatus("Fetching "..item.." from "..invLoc)
  1576.   print("Fetching "..quantity.." of "..item)
  1577.   setStatus("fetching "..quantity.." of "..item)
  1578.   depositInMasterChest(item, invLoc, quantity)
  1579. end
  1580. function tableRemoveKey(sourceTable, keyToRemove)
  1581.   local newTable = {}
  1582.   for key,value in pairs(sourceTable) do
  1583.     if key ~= keyToRemove then newTable[key]=value end
  1584.   end
  1585.   return newTable
  1586. end
  1587. function emptyInMasterChest(invLoc)
  1588.   -- Locate item in inventoryName.
  1589.   local found = false
  1590.   local totalQty = 0
  1591.   print("emptying location "..invLoc)
  1592.   setStatus("emptying location "..invLoc)
  1593.   found = true
  1594.   local idx = string.find(invLoc.."",",")
  1595.   local level = tonumber(string.sub(invLoc,1,idx-1))
  1596.   local dir = tonumber(string.sub(invLoc,idx+1))
  1597.   if gotoLocation(level,dir) then
  1598.     turtle.select(1)
  1599.     print("sucking from "..invLoc)
  1600.     while turtle.suck() and turtle.getItemCount(maxSlot)==0 do found = true end
  1601.     if turtle.getItemCount(maxSlot) == 0 then
  1602.       print("removing "..invLoc)
  1603.       inventoryName = tableRemoveKey(inventoryName,invLoc)
  1604.     end
  1605.   else
  1606.     setStatus("Unable to access location")
  1607.     return false
  1608.   end
  1609.   if goToStart() then
  1610.     if found then
  1611.       if turtleInventory() == 0 then
  1612.         setStatus("Location "..invLoc.." already empty.")
  1613.       else
  1614.         for s=1,maxSlot do
  1615.           turtle.select(s)
  1616.           turtle.dropUp()
  1617.         end
  1618.         setStatus("Location "..invLoc.." emptied.")
  1619.       end
  1620.       return true
  1621.     end
  1622.   end
  1623.   setStatus("Unable to find location "..invLoc)
  1624.   return false
  1625. end
  1626. function turtleInventory()
  1627.   local qty = 0
  1628.   for s=1,maxSlot do
  1629.     qty = qty + turtle.getItemCount(s)
  1630.   end
  1631.   return qty
  1632. end
  1633. function getInventoryNameQty(invLocation)        
  1634.   local name = inventoryName[invLocation]
  1635.   local qty = 0
  1636.   if name ~= nil then
  1637.     local idx = string.find(name,"#")
  1638.     if idx ~= nil then
  1639.       qty = string.sub(name,idx+1)
  1640.       if qty == nil then qty = 0 end
  1641.       name = string.sub(name,1,idx-1)
  1642.     end
  1643.   else
  1644.     name = "_"
  1645.   end
  1646.   return name,qty
  1647. end
  1648. function depositInMasterChest(item, invLoc, quantity)
  1649.   -- Locate item in inventoryName.
  1650.   local found = false
  1651.   local totalQty = 0
  1652.   for invLocation,value in pairs(inventoryName) do
  1653.     local name, qty = getInventoryNameQty(invLocation)    
  1654.     if name == item then
  1655.       print("checking location "..invLocation)
  1656.       setStatus("checking location "..invLocation)
  1657.       found = true
  1658.       local idx = string.find(invLocation.."", ",")
  1659.       local level = tonumber(string.sub(invLocation,1,idx-1))
  1660.       local dir = tonumber(string.sub(invLocation,idx+1))
  1661.       if gotoLocation(level,dir) then
  1662.         local prevQty = 0
  1663.         turtle.select(1)
  1664.         local firstSuck = true
  1665.         totalQty = turtleInventory()
  1666.         while totalQty < quantity do
  1667.           if not turtle.suck() then
  1668.             -- No more to suck
  1669.             if firstSuck then tableRemoveKey(inventoryName,invLocation) end
  1670.             break
  1671.           else
  1672.             totalQty = turtleInventory()
  1673.             if totalQty > quantity then
  1674.               -- We sucked more than we need
  1675.               turtle.drop(totalQty - quantity)
  1676.               break
  1677.             elseif firstSuck then
  1678.               -- We need to leave at least 1 per location.
  1679.               turtle.drop(1)
  1680.               firstSuck = false
  1681.             end
  1682.             qty = totalQty - prevQty
  1683.             prevQty = totalQty
  1684.             found = true
  1685.           end
  1686.         end
  1687.         inventoryName[invLocation] = name.."#"..qty  
  1688.         if totalQty == quantity then break end
  1689.       else
  1690.         setStatus("Unable to access location")
  1691.         inventoryName = tableRemoveKey(inventoryName,invLocation)
  1692.         return false
  1693.       end
  1694.       break
  1695.     end
  1696.   end
  1697.   if goToStart() then
  1698.     if found then
  1699.       if turtleInventory() == 0 then
  1700.         setStatus("only 1 "..item.." found.")
  1701.       else
  1702.         for s=1,maxSlot do
  1703.           turtle.select(s)
  1704.           turtle.dropUp()
  1705.         end
  1706.         setStatus("finished retrieving item "..item)
  1707.       end
  1708.       return true
  1709.     end
  1710.   end
  1711.   setStatus("Unable to find item "..item)
  1712.   return false
  1713. end
  1714. function getPickAxeStatus()
  1715.   local fname = "hasPickAxe"
  1716.   if fs.exists(fname) then
  1717.     file = fs.open(fname, "r")
  1718.     local frcd = file.readAll()
  1719.     if frcd == "Y" then hasPickAxe = true else hasPickAxe = false end
  1720.     file.close()
  1721.   else
  1722.     file = fs.open(fname, "w")
  1723.     file.write("N")
  1724.     file.close()
  1725.     hasPickAxe = false
  1726.   end
  1727. end
  1728. function getOrderComputer()
  1729.   local fname = "InventoryControlID"
  1730.   if fs.exists(fname) then
  1731.     file = fs.open(fname, "r")
  1732.     local frcd = file.readAll()
  1733.     orderComputer = tonumber(frcd)
  1734.     file.close()
  1735.   else
  1736.     print("Inventory Manager ID:")
  1737.     orderComputer = tonumber(io.read())
  1738.     file = fs.open(fname, "w")
  1739.     file.write(orderComputer)
  1740.     file.close()
  1741.   end
  1742. end
  1743. function runQueue()
  1744.   while true do
  1745.     if #queue == 0 then
  1746.       if not putAwayInventory() then
  1747.         refuel()
  1748.         setStatus("waiting")
  1749.       end
  1750.       return true
  1751.     else
  1752.       local command = queue[1]
  1753.       print("Queue contains "..command)
  1754.       table.remove(queue, 1)
  1755.       if command == "identify" then
  1756.         setStatus("Identify Acknowledged")
  1757.         -- Identify items in unidentified locations
  1758.         identifyInventory()
  1759.         setStatus("finished")
  1760.       elseif (command == "reportinventory") then
  1761.         setStatus("Report Acknowledged")
  1762.         reportInventory(orderComputer)
  1763.       elseif (string.sub(command,1,5) == "fetch") then
  1764.         setStatus("Fetch Acknowledged")
  1765.         fetchItem(command)
  1766.         saveTable(inventoryName, "inventory")
  1767.         setStatus("finished")
  1768.       elseif (string.sub(command,1,5) == "empty") then
  1769.         setStatus("Empty Acknowledged")
  1770.         local invLoc = string.sub(command,6)
  1771.         setStatus("Emptying "..invLoc)
  1772.         while inventoryName[invLoc] ~= nil do emptyInMasterChest(invLoc) end
  1773.         saveTable(inventoryName, "inventory")
  1774.         setStatus("finished")
  1775.       elseif (string.sub(command,1,5) == "count") then
  1776.         setStatus("Count Acknowledged")
  1777.         local invLoc = string.sub(command,7)
  1778.         if invLoc == "all" then
  1779.           countAll()
  1780.         elseif invLoc == "new" then
  1781.           countNew()
  1782.         else
  1783.           countLocation(invLoc)
  1784.           goToStart()
  1785.         end
  1786.         saveTable(inventoryName, "inventory")
  1787.         setStatus("finished")
  1788.       end
  1789.     end
  1790.   end
  1791. end
  1792. --------------------- Main Program ---------------------
  1793. -- Check orientation. and move to start position.
  1794. storeKeeperID = os.getComputerID()
  1795. print("Computer ID: "..storeKeeperID)
  1796. if not powerUpChecks() then return end
  1797. print("Startup completed")
  1798. getOrderComputer()
  1799. while true do
  1800.   if not listenForMessages() then
  1801.     runQueue()
  1802.   end
  1803. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement