dadragon84

StoreKeeper

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