Inksaver

superminer.lua

Jul 24th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 298.02 KB | None | 0 0
  1. version = "1.0"
  2. pastebinCode = "4BmfBfp3"
  3.  
  4. --[[
  5.     http://pastebin.com/4BmfBfp3
  6.     Start with crafty mining turtle and the following in players inventory:
  7.     1 chest, 1 dirt, 1 stone, 1 cobble, 1 gravel, 1 bucket, 128 wood
  8.     Place the turtle anywhere you like, even above water or lava.
  9.     Right-click, type in:
  10.    
  11.         pastebin get 4BmfBfp3 superminer
  12.        
  13.     (or pastebin get 4BmfBfp3 superminer.lua to make editing easier in Notepad++)
  14.     Type in:
  15.    
  16.         superminer
  17.        
  18.     (or superminer.lua if that is how you saved it)    
  19.     The script will download a file called 'startup' and reboot
  20.     Check out the startup file here: pastebin.com/DjYnc3QN
  21.     This file allows for restarting the script after reboot.
  22.     Instructions will guide you to place the following into the turtle:
  23.    
  24.     1 chest in slot 1
  25.     1 dirt in slot 2
  26.     1 cobble in slot 3
  27.     1 stone in slot 4
  28.     1 gravel in  slot 5
  29.     1 bucket in slot 6
  30.     64 wood in slot 7
  31.     64 wood in slot 8
  32.    
  33.     Choose whether you want diamonds mined first, and it will start at level 15,
  34.     or just let it run, and it will start at level 51, and finish at bedrock.
  35.    
  36.     No fuel needed. Allow for 5 x 5 x 2 cobble area to be created as base camp,
  37.     either on surface or below ground. Storage chests will be placed on each layer
  38.     for mining waste. A ladder will be crafted to bedrock.
  39.     2 cobble staircases will be crafted on opposite sides of the mine.
  40.    
  41.     Keep an eye out for the turtle returning to the chest marked with a wood block
  42.     If it is there it needs more wood, so put 64 wood in the chest. (NOT the turtle!)
  43.     Check the contents of each waste chest,
  44.     as the contents of any dungeon chests will be stored there.
  45.    
  46.     Enjoy
  47. ]]--
  48.  
  49. local function initialise()
  50.     local countdown = 10
  51.     local doContinue = true
  52.  
  53.     if not fs.exists("startup") then
  54.         if getPastebin("startup", "DjYnc3QN") then
  55.             print("startup file obtained. Restarting...")
  56.             sleep(2)
  57.             os.reboot()
  58.         else
  59.             print("Error getting startup file from pastebin")
  60.             print("Get it from http://pastebin.com/DjYnc3QN")
  61.             sleep(5)
  62.         end
  63.     end
  64.     -- set global variables.
  65.     diamondsFirst = true
  66.     diamondsOnly = true
  67.     currentFunction = "initialise"
  68.     callingFunction = "main"
  69.    
  70.     stage = {}
  71.    
  72.     --special stage as script progresses:
  73.     stage["startUnderground"] = false
  74.     stage["getPreferences"] = false
  75.     stage["getSupplies"] = false                -- confirms player has added required items
  76.     stage["getCoords"] = false              -- confirms player has input starting coordinates
  77.     stage["craftChests"] = false                -- 8 chests created
  78.     stage["craftSticks"] = false                -- sticks crafted and stored in chest
  79.     stage["craftLadders"] = false               -- ladders crafted
  80.     stage["craftSigns"] = false
  81.     stage["getReady"] = false                   -- getReady function completed
  82.     stage["findCobble"] = false             -- cobble mined ready for base construction
  83.     stage["craftFurnaces"] = false
  84.     stage["cobblePlaced"] = false
  85.     stage["clearBase"] = false                  -- clearBase function completed
  86.     stage["completeMineshaft"] = false      -- completeMineshaft function completed - dug to bedrock
  87.    
  88.     for i = 51, 6, -3 do -- mine from level 51 to bedrock in 16 levels
  89.         stage["completeMine"..tostring(i)] = false
  90.     end
  91.     for i = 51, 6, -3 do -- mine from level 51 to bedrock in 16 levels
  92.         stage["createBase"..tostring(i)] = false
  93.     end
  94.     for i = 1, 4 do -- clear mine in 4 sectors
  95.         stage["mineSector"..tostring(i)] = false
  96.     end
  97.     for i = 1, 3 do -- clear each sector in 2 or 3 parts (non-diamond/diamond)
  98.         stage["mineSectorPart"..tostring(i)] = false
  99.     end
  100.     --[[
  101.         createMine51
  102.         createMine48
  103.         createMine45
  104.         ...
  105.         createMine15
  106.         createMine12
  107.         createMine9
  108.         createMine6
  109.     ]]--
  110.     -- create classes/objects
  111.     createCoordObject()
  112.     createChestObject()
  113.     createSlotObject()
  114.     createDigObject()
  115.     createPlaceStorageObject()
  116.     createLogfileObject() --checks if recovery file used, loads if found
  117.    
  118.     term.clear()
  119.     term.setCursorPos(1,1)
  120.     print("Superminer.lua version: "..version)
  121.     print()
  122.     if os.getComputerLabel() == nil or os.getComputerLabel() ~= "Superminer" then
  123.         os.setComputerLabel("Superminer")
  124.         print("Computer label set to 'Superminer'")
  125.     end
  126.     print()
  127.     print("Checking for recovery file...")
  128.     print()
  129.     if fso:recoveryFileExists() then --could have crashed/chunk unloaded etc
  130.         --[[
  131.         startup file runs this script automatically either when rebooting,
  132.         or after chunk reloading.The recovery file stores current location, current function
  133.         and contents of the turtle.
  134.         ]]--
  135.         print("Recovery File exists")
  136.         repeat
  137.             os.startTimer(2)
  138.             local event, param1 = os.pullEvent()
  139.             if event == "timer" then
  140.                 term.setCursorPos(1,6)
  141.                 print("loading recovery file in "..countdown.." secs...")
  142.                 print()
  143.                 print("Press 'x' to abort")
  144.                 countdown = countdown - 1
  145.             elseif event == "char" and param1 == "x" then
  146.                 print("Loading recovery file aborted")
  147.                 doContinue = false
  148.                 break
  149.             end
  150.         until countdown == 0
  151.         if doContinue then
  152.             fromRecoveryFile = true
  153.             saveToLog("initialise: using recovery file", false, true)
  154.             --[[resets contents of turtle, and completed functions
  155.                 1)check if chest in front that may be half loaded/emptied during crafting
  156.                 2)move turtle back to starting position
  157.             ]]--
  158.             loadStatus()
  159.             sleep(2)
  160.         else
  161.             saveToLog("initialise: Player cancelled recovery File import", true, true)
  162.             sleep(2)
  163.         end
  164.     else
  165.         print("No Recovery File exists")
  166.         sleep(2)
  167.         saveToLog("initialise: No Recovery File exists", false, true)
  168.     end
  169.     --[[mining levels
  170.         ores levels:
  171.         ceiling = 52    49      46      43      40      37      34      31      28      25      22      19
  172.         turtle  = 51    48      45      42      39      36      35      30      27      24      21      18
  173.         floor   = 50    47      44      41      38      35      32      29      26      23      20      17
  174.         levels    53+   50-52   47-49   44-46   41-43   38-40   35-37   32-34   29-31   26-28   23-25   20-22  
  175.         Diamond levels:
  176.         ceiling = 16        13      10      7
  177.         turtle  = 15        12      9       6
  178.         floor   = 14        11      8       5
  179.         levels    17-19     14-16   11-13   8-10
  180.     ]]--
  181. end            
  182.  
  183. function attack()
  184.     local numAttacks = 0
  185.     local mobAttacked = false
  186.     local itemCount = {}
  187.    
  188.     for i = 1, 16 do
  189.         itemCount[i] = turtle.getItemCount(i)
  190.     end
  191.    
  192.     -- minecart with chest is not detected, but responds to attack without breaking!
  193.     if turtle.attack() then
  194.         saveToLog("attack: Mob attacked in front!", true, false)
  195.         sleep(1.5)
  196.         while turtle.attack() do --in case mob in front
  197.             saveToLog("attack: Mob attacked in front again!", true, false)
  198.             sleep(1.5)
  199.             numAttacks =  numAttacks + 1
  200.             if numAttacks > 10 then
  201.                 numAttacks = 0
  202.                 saveToLog("attack: digging forward in case of minecart", true, false)
  203.                 dig.digNew{self = dig, direction = "forward", callFrom = "attack"}
  204.             end
  205.         end
  206.         mobAttacked = true
  207.     end
  208.     if turtle.attackUp() then
  209.         saveToLog("attack: Mob attacked above!", true, false)
  210.         sleep(1.5)
  211.         while turtle.attackUp() do --in case mob in front
  212.             saveToLog("attack: Mob attacked above again!", true, false)
  213.             sleep(1.5)
  214.             numAttacks =  numAttacks + 1
  215.             if numAttacks > 10 then
  216.                 numAttacks = 0
  217.                 saveToLog("attack: digging up in case of minecart", true, false)
  218.                 dig.digNew{self = dig, direction = "up", callFrom = "attack"}
  219.             end
  220.         end
  221.         mobAttacked = true
  222.     end
  223.     if turtle.attackDown() then
  224.         saveToLog("attack: Mob attacked below!", true, false)
  225.         sleep(1.5)
  226.         while turtle.attackDown() do --in case mob in front
  227.             saveToLog("attack: Mob attacked below again!", true, false)
  228.             sleep(1.5)
  229.             numAttacks =  numAttacks + 1
  230.             if numAttacks > 10 then
  231.                 numAttacks = 0
  232.                 saveToLog("attack: digging down in case of minecart", true, false)
  233.                 dig.digNew{self = dig, direction = "down", callFrom = "attack"}
  234.             end
  235.         end
  236.         mobAttacked = true
  237.     end
  238.     if mobAttacked then --remove any mob drops
  239.         for i = 1, 16 do
  240.             if itemCount[i] == 0 then --check only previously empty slots
  241.                 if turtle.getItemCount(i) > 0 then
  242.                     turtle.select(i)
  243.                     while turtle.dropUp() do
  244.                         saveToLog ("attack: dumping mob drops", true, false)
  245.                     end
  246.                 end
  247.             end
  248.         end
  249.     end
  250.    
  251.     return mobAttacked
  252. end
  253.  
  254. function back(steps, useSlot, itemList)
  255.     local previousFunction = currentFunction
  256.     local success = false
  257.    
  258.     useSlot = useSlot or 1
  259.     itemList = itemList or ""
  260.    
  261.     if turtle.getFuelLevel() < steps then
  262.         saveToLog(" ***** back: Fuel < "..steps.." *****", true, false)
  263.         if not refuel(0) then
  264.             saveToLog(" ***** back: refuel failed*****", true, false)
  265.         end
  266.     end
  267.     for i = 1, steps do
  268.         if not turtle.back() then --cant move back
  269.             turnRight(2)
  270.             if forward(1, useSlot, itemList) then
  271.                 success = true
  272.             end
  273.             turnRight(2)
  274.         end
  275.         changeCoords("back")
  276.     end
  277.    
  278.     currentFunction = previousFunction
  279.     return success
  280. end
  281.  
  282. function buildStaircase(level, position)
  283.     currentFunction = "buildStaircase"
  284.     callingFunction = "createMine"
  285.     local side = "left"
  286.    
  287.     -- level decides R or L version of staircase
  288.     -- move to correct position
  289.     if (level / 3) % 2 ~= 0 then -- levels 51, 45, 39, 33, 27, 21, 15, 9
  290.         side = "right"
  291.     end
  292.     saveToLog("buildStaircase: started on side = "..side.." on level "..level.." checking cobble", true, false)
  293.     -- craft stairs
  294.     sortCobble()
  295.     if getStock("cobble-1") > 0 and getStock("cobble-1") < 7 then -- dump it so it does not get stored in crafting chest as "cobble"
  296.         saveToLog("buildStaircase: dumping excess cobble", true, false)
  297.         turtle.select(slot:getItemSlot("cobble-1"))
  298.         turtle.drop()
  299.         slot.update{self = slot, item = "cobble-1", delete = true}
  300.         sortInventory(true)
  301.     end
  302.     if getStock("cobble") > 7 then
  303.         saveToLog("buildStaircase: crafting stairs", true, false)
  304.         craft{craftItem = "stairs", calledFrom = "buildStaircase", craftQuantity = 4, sourceItem1 = "cobble", destSlot = 0, doSort = false}
  305.         if getStock("cobble-1") > 0 and getStock("cobble") < 64 then
  306.             turtle.select(slot:getItemSlot("cobble-1"))
  307.             turtle.transferTo(slot:getItemSlot("cobble"))
  308.             slot.update{self = slot, item = "cobble", delete = true}
  309.             slot.update{self = slot, item = "cobble-1", delete = true}
  310.             sortInventory(true)
  311.         end
  312.         saveToLog("buildStaircase: started on side = "..side.." on level "..level)
  313.         -- move to correct starting position depending on Front/Back staircase and R/L side
  314.         if side == "left" then
  315.             go("LFuC DxC FxC UuC FuCDxCR") -- ready to start to left of door at foot level
  316.             go("FuCxC FuCxC FuCxC FuCxC BdC BdC BdC BdCR FL") -- left side of stairs created, back to door level
  317.             go("FuxC FuxC FuxC FuxCR") -- on back wall, turned right
  318.             go("FuCxC FuCxC FuCxC FuCxC FuCxCR FuCxC FuCxC FuCxC BdC BdC BdC LBdC BdC BdC BdC BdCL BdCR") -- back and right walls of stairs created, back to door level
  319.             go("FuRR")
  320.             if down(1) then
  321.                 go("xCUSRR Fux FuCx FuCx BdCRR F F FL FL") -- back row finished.
  322.             else --bedrock
  323.                 go("RRFux FuCx FuCx BdCRR F F FL FL") -- back row finished.
  324.             end
  325.             go("FuxC FuxC FuxC FuxCuC BuCdC BdC UxC UxSRR F DRRSRR FL FL D") -- middle row finished
  326.             go("FuxC FuCxC FuCxC FuCxC BdC BdC BdC BdCL") --front row finished
  327.             go("FFdCUdC")
  328.             back(1)
  329.             turtle.select(slot:getItemSlot("signs"))
  330.             turtle.place("Staircase\nLevel  "..level.."\n"..location:getCompass())
  331.             slot.update{self = slot, item = "signs", delete = true}
  332.             go("RFRdTRFLFFLFFL")
  333.             sortInventory(true)
  334.         else
  335.             go("RFuCDxC FxCUuC FuCDxCL") -- ready to start to left of door at foot level
  336.             go("FuCxC FuCxC FuCxC FuCxC BdC BdC BdC BdCL FR") -- left side of stairs created, back to door level
  337.             go("FuxC FuxC FuxC FuxCL") -- on back wall, turned left
  338.             go("FuCxC FuCxC FuCxC FuCxC FuCxCL FuCxC FuCxC FuCxC BdC BdC BdC RBdC BdC BdC BdC BdCR BdCL") -- back and right walls of stairs created, back to door level
  339.             go("FuxC FuxC FuxC FuxCuC BdCuC BdCUxC UxSRR F DRRSRR FR FR D") -- back row finished
  340.             go("FuRR")
  341.             if down(1) then
  342.                 go("xC USRR Fux FuCx FuCx BdCRR F F FR FR") -- middle row finished
  343.             else --bedrock
  344.                 go("RRFux FuCx FuCx BdCRR F F FR FR") -- middle row finished
  345.             end
  346.             go("FuxC FuCxC FuCxC FuCxC BdC BdC BdC BdCR") --front row finished
  347.             go("FFdCUdC")
  348.             back(1)
  349.             if getStock("signs") > 0 then
  350.                 turtle.select(slot:getItemSlot("signs"))
  351.                 turtle.place("Staircase\nLevel  "..level.."\n"..location:getCompass())
  352.                 slot.update{self = slot, item = "signs", delete = true}
  353.             end
  354.             go("LFLdTLFRFFRFFR")
  355.             sortInventory(true)
  356.         end
  357.         if getStock("stairs") > 0 then
  358.             turtle.select(slot:getItemSlot("stairs"))
  359.             turtle.drop()
  360.             slot.update{self = slot, item = "stairs", delete = true}
  361.             sortInventory(true)
  362.         end
  363.         saveToLog("buildStaircase: finished on side = "..side.." on level "..level, true, false)
  364.     else
  365.         saveToLog("buildStaircase: insufficient cobble to build staircase", true, false)
  366.     end
  367.     -- return to starting position, ready to continue with mineCorridor
  368.     if position == "forward" then --front staircase
  369.         turnRight(1)
  370.     else -- rear staircase,
  371.         turnLeft(1)
  372.     end
  373. end
  374.  
  375. function changeCoords(direction)
  376.     --[[0 = go south (z increases)
  377.         1 = go west  (x decreases)
  378.         2 = go north (z decreases
  379.         3 = go east  (x increases)]]--
  380.        
  381.     if direction == "forward" then
  382.         if location:getFacing() == 0 then
  383.             -- zCoord = zCoord + 1
  384.             location:setZ(location:getZ() + 1)
  385.         elseif location:getFacing() == 1 then
  386.             -- xCoord = xCoord - 1
  387.             location:setX(location:getX() - 1)
  388.         elseif location:getFacing() == 2 then
  389.             -- zCoord = zCoord - 1
  390.             location:setZ(location:getZ() - 1)
  391.         else
  392.             -- xCoord = xCoord + 1
  393.             location:setX(location:getX() + 1)
  394.         end
  395.     elseif direction == "back" then
  396.         if location:getFacing() == 0 then
  397.             -- zCoord = zCoord - 1
  398.             location:setZ(location:getZ() - 1)
  399.         elseif location:getFacing() == 1 then
  400.             -- xCoord = xCoord + 1
  401.             location:setX(location:getX() + 1)
  402.         elseif location:getFacing() == 2 then
  403.             -- zCoord = zCoord + 1
  404.             location:setZ(location:getZ() + 1)
  405.         else
  406.             -- xCoord = xCoord - 1
  407.             location:setX(location:getX() - 1)
  408.         end
  409.     end
  410.     saveStatus()
  411. end
  412.  
  413. function changeDirection(direction)
  414.     -- changeDirection("faceForward")
  415.     -- changeDirection("faceBackward")
  416.     while location:getFacing() ~= coordHome:getFacing() do
  417.         turnRight(1)
  418.     end
  419.     if direction ~= "faceForward" then
  420.         turnRight(2)
  421.     end
  422. end
  423.  
  424. function checkChest(checkDirection)
  425.     local success = false
  426.    
  427.     currentFunction = "checkChest"
  428.    
  429.     turtle.select(slot:getItemSlot("chests"))
  430.     if checkDirection == "forward" then
  431.         if turtle.compare() then -- chest in front
  432.             saveToLog("checkChest: chest found ahead, dumping mining waste")
  433.             success = true
  434.             --dump any excess materials
  435.             storeMiningWaste(false)
  436.             refuel(8) --refuel with 8 planks instead of 4
  437.             -- suck out contents and break
  438.             while turtle.suck() do
  439.                 saveToLog("checkChest: chest found, removing contents")
  440.             end
  441.         end
  442.     elseif checkDirection == "down" then
  443.         if turtle.compareDown() then -- chest below
  444.             saveToLog("checkChest: chest found below, dumping mining waste")
  445.             success = true
  446.             storeMiningWaste(false)
  447.             refuel(8) --refuel with 8 planks instead of 4
  448.             -- suck out contents and break
  449.             while turtle.suckDown() do
  450.                 saveToLog("checkChest: chest found, removing contents")
  451.             end
  452.         end
  453.     elseif checkDirection == "up" then
  454.         if turtle.compareUp() then -- chest above
  455.             saveToLog("checkChest: chest found above, dumping mining waste")
  456.             success = true
  457.             storeMiningWaste(false)
  458.             refuel(8) --refuel with 8 planks instead of 4
  459.             -- suck out contents and break
  460.             while turtle.suckUp() do
  461.                 saveToLog("checkChest: chest found, removing contents")
  462.             end
  463.         end
  464.     end
  465.     if success then
  466.         dig.digNew{self = dig, direction = checkDirection, callFrom = "checkChest"}
  467.     end
  468.    
  469.     return success
  470. end
  471.  
  472. function checkReturnToBase(level, sector)
  473.     local fuelNeeded = 0
  474.     local fuelStats = {}
  475.     local numTorchesNeeded = 0
  476.     local numTorchesToCraft = 0
  477.    
  478.     currentFunction = "checkReturnToBase"
  479.     if level > 15 then
  480.         if sector == 1 then
  481.             numTorchesNeeded = 22
  482.             numTorchesToCraft = 24
  483.         elseif sector == 2 then
  484.             numTorchesNeeded = 15
  485.             numTorchesToCraft = 16
  486.         elseif sector == 3 then
  487.             numTorchesNeeded = 7
  488.             numTorchesToCraft = 8
  489.         else
  490.             numTorchesNeeded = 0
  491.         end
  492.     else -- diamond levels
  493.         numTorchesNeeded = 22
  494.         if sector == 1 then -- how many torches needed for sector 2 and above
  495.             numTorchesNeeded = 14
  496.             numTorchesToCraft = 16
  497.         elseif sector == 2 then
  498.             numTorchesNeeded = 9
  499.             numTorchesToCraft = 12
  500.         elseif sector == 3 then
  501.             numTorchesNeeded = 4
  502.             numTorchesToCraft = 4
  503.         else
  504.             numTorchesNeeded = 0
  505.         end
  506.     end
  507.    
  508.     if getNoOfValuableSlots() >= 4 or getNoOfEmptySlots() <= 4 or getStock("torches") < numTorchesNeeded then
  509.         refuel(0)
  510.         saveToLog("createMine: returning to base to empty loot", true, false)
  511.         while location:getY() < coordHome:getY() do
  512.             up(1)
  513.         end
  514.         storeWoodItems()
  515.         storeSand()
  516.         storeOres()
  517.         storeMinerals()
  518.         if getStock("torches") < numTorchesNeeded then
  519.             craftTorches(numTorchesToCraft)
  520.         end
  521.         if not stage["createBase"..tostring(level)] then -- whole mine needs to be done
  522.             fuelNeeded = 1020 -- 17 logs (17 * 60)
  523.         else -- find out how many stages completed
  524.             if not stage["mineSector1"] then
  525.                 fuelNeeded = 1020
  526.             elseif not stage["mineSector2"] then
  527.                 fuelNeeded = 760
  528.             elseif not stage["mineSector3"] then
  529.                 fuelNeeded = 510
  530.             elseif not stage["mineSector4"] then
  531.                 fuelNeeded = 260
  532.             end
  533.         end
  534.         fuelStats = getFuelAvailable()
  535.         if fuelStats.totalFuelAvailable + fuelStats.fuelLevel < fuelNeeded then
  536.             goToWoodStore(false)
  537.         end
  538.         while location:getY() > level do
  539.             down(1)
  540.         end
  541.     end
  542. end
  543.  
  544. function checkWaterLava(direction)
  545.     local result = ""
  546.     local slotNo = 0
  547.    
  548.     currentFunction = "checkWaterLava"
  549.     for i = 16, 1, -1 do
  550.         if turtle.getItemCount(i) == 0 then
  551.             slotNo = i
  552.             break
  553.         end
  554.     end
  555.     if slotNo > 0 then
  556.         turtle.select(slotNo)
  557.         if direction == "down" then
  558.             if not turtle.detectDown() then
  559.                 if turtle.compareDown() then --no block detected, compares to air(empty slot)
  560.                     result = "air"
  561.                 else
  562.                     result = "liquid"
  563.                 end
  564.             end
  565.         elseif direction == "up" then
  566.             if not turtle.detectUp() then
  567.                 if turtle.compareUp() then
  568.                     result = "air"
  569.                 else
  570.                     result = "liquid"
  571.                 end
  572.             end
  573.         elseif direction == "forward" then
  574.             if not turtle.detect() then
  575.                 if turtle.compare() then
  576.                     result = "air"
  577.                 else
  578.                     result = "liquid"
  579.                 end
  580.             end
  581.         end
  582.     end
  583.    
  584.     if result == "liquid" then -- water or lava found
  585.         turtle.select(slot:getItemSlot("buckets"))
  586.         turtle.refuel() --empty bucket if lava left in
  587.         if direction == "down" then
  588.             if turtle.placeDown() then --bucket filled water or lava
  589.                 result = "lava"
  590.                 if not turtle.refuel() then --water
  591.                     turtle.placeDown() --empty bucket out
  592.                     result = "water"
  593.                 end
  594.             end
  595.         else
  596.             if turtle.place() then --bucket filled water or lava
  597.                 result = "lava"
  598.                 if not turtle.refuel() then --water
  599.                     result = "water"
  600.                     turtle.place()--empty bucket out
  601.                 end
  602.             end
  603.         end
  604.     end
  605.    
  606.     return result -- "", "air", "liquid", "water", "lava"
  607. end
  608.  
  609. function checkWalls(itemList, calledFrom, do3D, getCobble)
  610.     currentFunction = "checkWalls"
  611.     for j = 1, 4 do
  612.         mineItem(itemList, calledFrom, do3D, "") --call recursive function
  613.         if turtle.detect() then --nothing found on mineItem
  614.             dig.digNew{self = dig, direction = "forward", checkForItems = "coal,sand", callFrom = "checkWalls"}
  615.         end
  616.         refuel(0)
  617.         turnRight(1)
  618.     end
  619.    
  620.     return success
  621. end
  622.  
  623. function clearBase()
  624.     currentFunction = "clearBase"
  625.     callingFunction = "main"
  626.     local numLadders = 0
  627.     --[[
  628.     5   C . C . C
  629.     4   . . . . .
  630.     3   C . F . C
  631.     2   . . X . .
  632.     1   C . . . C
  633.    
  634.         A B C D E
  635.     ]]--
  636.    
  637.     if not stage["clearBase"] then
  638.         -- 9 signs and some ladders left
  639.         saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
  640.                     " to logClearBase.txt END OF LOGFILE!", false, false)
  641.         fso:useFileName("logClearBase.txt")
  642.         saveToLog("clearBase: checking if furnace needs crafting", false, false)
  643.        
  644.         if not stage["craftFurnaces"] then
  645.             if craft{calledFrom = "clearBase", craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 0, doSort = true} then
  646.                 saveToLog("Crafted 1 furnace.")
  647.             else
  648.                 saveToLog("Crafting furnace did not succeed")
  649.                 error()
  650.             end
  651.             stage["craftFurnaces"] = true
  652.         end
  653.         --clear area around starting point 5 X 5 SQUARE
  654.         if not stage["cobblePlaced"] then
  655.             if stage["startUnderground"] then -- could be underground
  656.                 go("FuxC FuxCR FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxCR FuxC FuxC FuxCL FuxC FuxCL FuxC FuxCL FuxCR B", "coal,sand")
  657.             else
  658.                 go("FuxC FuxCR FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxCR FuxC FuxC FuxCL FuxC FuxCL FuxC FuxCL FuxCR B", "wood,saplings")
  659.             end
  660.             stage["cobblePlaced"] = true
  661.         end
  662.         --place ladder at top of pile
  663.         go("RRDd") 
  664.         turtle.select(slot:getItemSlot("ladders"))
  665.         if not turtle.compare() then
  666.             turtle.place()
  667.         end
  668.         go("URR")
  669.         sortInventory(true) --C3
  670.         forward(2) -- 2 blocks in front of furnace site (C5)
  671.        
  672.         placeChest("down","wood") --C5
  673.         turtle.select(slot:getItemSlot("wood"))
  674.         if not turtle.compareUp() then
  675.             turtle.placeUp()
  676.         end
  677.        
  678.         go("RFFL") --E5
  679.         turtle.select(slot:getItemSlot("cobble"))
  680.         if not turtle.compareUp() then
  681.             turtle.placeUp()
  682.         end
  683.         placeChest("down", "sticks") --E5
  684.        
  685.         go("RRFF") --E3
  686.         turtle.select(slot:getItemSlot("cobble"))
  687.         if not turtle.compareUp() then
  688.             go("URR") --E3
  689.             turtle.select(slot:getItemSlot("signs"))
  690.             turtle.place("Sticks\nOnly")
  691.             go("DRRuC") --still on E3
  692.         end
  693.         placeChest("down", "building")
  694.        
  695.         go("FF") -- 2 blocks behind furnace, 2 to right (E1)
  696.         turtle.select(slot:getItemSlot("cobble"))
  697.         if not turtle.compareUp() then
  698.             go("URR") --E1
  699.             turtle.select(slot:getItemSlot("signs"))
  700.             turtle.place("Dirt\nCobble\nGravel\nSand")
  701.             go("DRRuC") --still on E1
  702.         end
  703.         placeChest("down", "ores")
  704.        
  705.         go("RFFURR") -- C1
  706.         if getStock("signs") > 0 then
  707.             turtle.select(slot:getItemSlot("signs"))
  708.             if not turtle.detect() then
  709.                 turtle.place("Iron & Gold ores\nObsidian\nMossy Cobble")
  710.             end
  711.         end
  712.        
  713.         go("RRDFFRFFFF") --A5
  714.         turtle.select(slot:getItemSlot("cobble"))
  715.         if not turtle.compareUp() then
  716.             turtle.placeUp()
  717.         end
  718.         placeChest("down", "torches")
  719.        
  720.         go("BB") --A3
  721.         turtle.select(slot:getItemSlot("cobble"))
  722.         if not turtle.compareUp() then
  723.             go("U") --A3
  724.             turtle.select(slot:getItemSlot("signs"))
  725.             turtle.place("Ladders, planks\nfences and\nother wooden\nitems only")
  726.             go("DRRuC") --still on A3
  727.         end
  728.         placeChest("down", "minerals")
  729.        
  730.         go("FFRR") -- A1
  731.         turtle.select(slot:getItemSlot("cobble"))
  732.         if not turtle.compareUp() then
  733.             go("U") --A1
  734.             turtle.select(slot:getItemSlot("signs"))
  735.             turtle.place("Odds\nand\nSods")
  736.             go("DuC") --still on A1
  737.         end
  738.         placeChest("down", "misc")
  739.        
  740.         go("RFFURR") --C1
  741.         turtle.select(slot:getItemSlot("signs"))
  742.         if not turtle.compare() then
  743.             turtle.place("Redstone\nLapis\nDiamonds\nCoal")
  744.         end
  745.        
  746.         go("DRFF") -- C3
  747.         if not turtle.detectUp() then
  748.             up(1)
  749.             turtle.select(slot:getItemSlot("signs"))
  750.             turtle.place("If turtle here\nAdd 64 wood\nto chest.\nNo other items!")
  751.             slot.update{self = slot, item = "signs", delete = true}
  752.             down(1)
  753.             sortInventory(true)
  754.         end
  755.         --put furnace above
  756.         if getStock("furnaces") > 0 then
  757.             turtle.select(slot:getItemSlot("furnaces"))
  758.             turtle.placeUp()
  759.             saveToLog("clearBase: furnace placed", true, true)
  760.             slot.update{self = slot, item = "furnaces", delete = true}
  761.         end
  762.         storeSigns()
  763.         -- should be some ladders in stock. If surface is average level 64 then need say 75 altogether, and torches
  764.         if coordHome:getY() > 54 then --more ladders needed
  765.             numLadders = coordHome:getY() - 54
  766.         end
  767.         if numLadders > 0 then
  768.             craftLadders(math.ceil(numLadders / 3) * 3) -- eg 20 needed, 20 / 3 * 3 = 6 * 3 = 18
  769.         end
  770.         currentFunction = "clearBase"
  771.         craftTorches(8)
  772.         currentFunction = "clearBase"
  773.         go("FR FRUxT FFRxT FFRxT FFRRxT FDLFL") -- place 4 torches
  774.         changeDirection("faceBackward")
  775.         stage["clearBase"] = true
  776.         saveStatus()
  777.         displaySaveBackup()
  778.     end
  779. end
  780.  
  781. function completeMineshaft()
  782.     local dirtAmount = 0
  783.     local waterlava = ""
  784.     local itemList = "coal,redstone,lapis"
  785.    
  786.     currentFunction = "completeMineshaft"
  787.     callingFunction = "main"
  788.     if not stage["completeMineshaft"] then
  789.         saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
  790.                     " to logCompleteMineshaft.txt END OF LOGFILE!", false, false)
  791.         fso:useFileName("logCompleteMineshaft.txt")
  792.         saveToLog("completeMineshaft: starting", false, false)
  793.         --store excess sticks in sticks chest
  794.         storeSticks()
  795.         -- deposit excess dirt, cobble, gravel, sand in chest
  796.         dirtAmount = getStock("dirt")
  797.         if dirtAmount > 40 then
  798.             dirtAmount = dirtAmount - 40
  799.         else
  800.             dirtAmount = 0
  801.         end
  802.         storeBuildingBlocks{dirt = dirtAmount, cobble = 0, gravel = -1, sand = 64}
  803.         -- deposit any unknown items in ore chest
  804.         storeWoodItems()
  805.         storeSand()
  806.         storeOres()
  807.         -- if started below level 19, cobble mined from above
  808.         changeDirection("faceBackward")
  809.         turtle.select(slot:getItemSlot("ladders"))
  810.         down(1)
  811.         while turtle.compare() do
  812.             saveToLog("completeMineshaft: ladder in front")
  813.             for i = 1, 3 do
  814.                 turnRight(1)
  815.                 if not turtle.detect() then
  816.                     waterlava = checkWaterLava("forward")
  817.                     if waterlava ~= "air" then --water or lava present
  818.                         saveToLog("completeMineshaft: "..waterlava.." in front")
  819.                         if getStock("cobble") > 1 then
  820.                             turtle.select(slot:getItemSlot("cobble"))
  821.                             turtle.place()
  822.                         elseif getStock("dirt") > 1 then
  823.                             turtle.select(slot:getItemSlot("dirt"))
  824.                             turtle.place()
  825.                         end
  826.                     end
  827.                 end
  828.             end
  829.             turnRight(1)
  830.             down(1)
  831.             turtle.select(slot:getItemSlot("ladders"))
  832.         end
  833.         up(1) -- no more ladders in front, move up ready to continue down
  834.         repeat
  835.             waterlava = checkWaterLava("down")
  836.             if waterlava ~= "" and waterlava ~= "air" then -- water, liquid or lava found below
  837.                 if down(1) then --not at bedrock
  838.                     if waterlava == "lava" then
  839.                          --build cobble shield and add ladder
  840.                         go("RR") -- face area opposite ladder
  841.                         if not turtle.detect() then
  842.                             if checkWaterLava("forward") == "lava" then -- lava present
  843.                                 if getStock("cobble") > 1 then
  844.                                     turtle.select(slot:getItemSlot("cobble"))
  845.                                 elseif getStock("dirt") > 1 then
  846.                                     turtle.select(slot:getItemSlot("dirt"))
  847.                                 end
  848.                                 turtle.place()
  849.                             end
  850.                         end
  851.                         turnRight(1)
  852.                         if not turtle.detect() then
  853.                             if checkWaterLava("forward") == "lava" then -- lava present
  854.                                 if getStock("cobble") > 1 then
  855.                                     turtle.select(slot:getItemSlot("cobble"))
  856.                                 elseif getStock("dirt") > 1 then
  857.                                     turtle.select(slot:getItemSlot("dirt"))
  858.                                 end
  859.                                 turtle.place()
  860.                             end
  861.                         end
  862.                         turnLeft(2)
  863.                         if not turtle.detect() then
  864.                             if checkWaterLava("forward") == "lava" then -- lava present
  865.                                 if getStock("cobble") > 1 then
  866.                                     turtle.select(slot:getItemSlot("cobble"))
  867.                                 elseif getStock("dirt") > 1 then
  868.                                     turtle.select(slot:getItemSlot("dirt"))
  869.                                 end
  870.                                 turtle.place()
  871.                             end
  872.                         end
  873.                         turnLeft(1)
  874.                         if forward(1) then -- in ladder section, below existing ladder
  875.                             turnLeft(1)
  876.                             if not turtle.detect() then
  877.                                 if checkWaterLava("forward") == "lava" then -- lava present
  878.                                     if getStock("cobble") > 1 then
  879.                                         turtle.select(slot:getItemSlot("cobble"))
  880.                                     elseif getStock("dirt") > 1 then
  881.                                         turtle.select(slot:getItemSlot("dirt"))
  882.                                     end
  883.                                     turtle.place()
  884.                                 end
  885.                             end
  886.                             turnRight(2)
  887.                             if not turtle.detect() then
  888.                                 if checkWaterLava("forward") == "lava" then -- lava present
  889.                                     if getStock("cobble") > 1 then
  890.                                         turtle.select(slot:getItemSlot("cobble"))
  891.                                     elseif getStock("dirt") > 1 then
  892.                                         turtle.select(slot:getItemSlot("dirt"))
  893.                                     end
  894.                                     turtle.place()
  895.                                 end
  896.                             end
  897.                             turnLeft(1)
  898.                             back(1)
  899.                             if getStock("ladders") > 1 then
  900.                                 turtle.select(slot:getItemSlot("ladders"))
  901.                                 while not turtle.place() do
  902.                                     while attack() do --in case mobs in front
  903.                                         saveToLog("Mob attacked again!", false)
  904.                                     end
  905.                                 end
  906.                                 saveToLog("completeMineshaft: ladder placed at "..location:getY())
  907.                             end
  908.                         end
  909.                     end
  910.                 else -- hit bedrock
  911.                     break
  912.                 end
  913.             else -- air or block below
  914.                 dig.digNew{self = dig, direction = "down", callFrom = "mineToBedrock", checkForItems = itemList}
  915.                 if down(1) then
  916.                     saveToLog("completeMineshaft: gone down. placing ladder in front")
  917.                     --place ladder
  918.                     if turtle.detect() then -- block in front
  919.                         if dig.digNew{self = dig, callFrom = "mineToBedrock", checkForItems = itemList} then
  920.                             saveToLog("completeMineshaft: block cleared for cobble replacement")
  921.                         end
  922.                         -- dig = false can be gravel-> flint
  923.                     end
  924.                     if not turtle.detect() then --block dug successfully
  925.                         if forward(1) then --not bedrock
  926.                             saveToLog("completeMineshaft: moved forward for placing cobble")
  927.                             dig.digNew{self = dig, waitForGravel = true, checkForItems = itemList}
  928.                             if not turtle.detect() then
  929.                                 if getStock("cobble") > 1 then
  930.                                     turtle.select(slot:getItemSlot("cobble"))
  931.                                 elseif getStock("dirt") > 1 then
  932.                                     turtle.select(slot:getItemSlot("dirt"))
  933.                                 end
  934.                                 while not turtle.place() do
  935.                                     while attack() do --in case mobs in front
  936.                                         saveToLog("Mob attacked again!", false)
  937.                                     end
  938.                                 end
  939.                                 saveToLog("completeMineshaft: cobble placed for ladder")
  940.                             else
  941.                                 saveToLog("completeMineshaft: bedrock in front for ladder")
  942.                             end
  943.                             back(1)
  944.                             if getStock("ladders") > 1 then
  945.                                 turtle.select(slot:getItemSlot("ladders"))
  946.                                 while not turtle.place() do
  947.                                     while attack() do --in case mobs in front
  948.                                         saveToLog("Mob attacked again!", false)
  949.                                     end
  950.                                 end
  951.                                 saveToLog("completeMineshaft: ladder placed at "..location:getY())
  952.                             end
  953.                             -- now check for water/lava on sides
  954.                             for i = 1, 3 do
  955.                                 turnRight(1)
  956.                                 if not turtle.detect() then
  957.                                     waterlava = checkWaterLava("forward")
  958.                                     if waterlava ~= "air" then --water, liquid or lava present
  959.                                         saveToLog("completeMineshaft: "..waterlava.." in front")
  960.                                         if getStock("cobble") > 1 then
  961.                                             turtle.select(slot:getItemSlot("cobble"))
  962.                                         elseif getStock("dirt") > 1 then
  963.                                             turtle.select(slot:getItemSlot("dirt"))
  964.                                         end
  965.                                         turtle.place()
  966.                                     end
  967.                                 end
  968.                             end
  969.                             turnRight(1) --return to correct position
  970.                         else
  971.                             break
  972.                         end
  973.                     else --bedrock in front
  974.                         break
  975.                     end
  976.                 else -- hit bedrock
  977.                     break
  978.                 end
  979.             end
  980.         until location:getY() == 1
  981.         if getStock("torches") > 0 then
  982.             saveToLog("completeMineshaft: at bedrock. placing torch")
  983.             up(1)
  984.             turtle.select(slot:getItemSlot("torches"))
  985.             turtle.placeDown()
  986.             slot.update{self = slot, item = "torches", delete = true}
  987.         end
  988.         while location:getY() < coordHome:getY() do
  989.             up(1, 1)
  990.         end
  991.         dirtAmount = getStock("dirt")
  992.         if dirtAmount > 40 then
  993.             dirtAmount = dirtAmount - 40
  994.         else
  995.             dirtAmount = 0
  996.         end
  997.         storeBuildingBlocks{dirt = dirtAmount, cobble = 0, gravel = -1, sand = 64}
  998.         storeLadders()
  999.         stage["completeMineshaft"] = true
  1000.         saveStatus()
  1001.         displaySaveBackup()
  1002.     end
  1003. end
  1004.  
  1005. function craft(arg)
  1006.     local tempCurrentFunction = currentFunction
  1007.    
  1008.     callingFunction = arg.calledFrom
  1009.     --[[
  1010.     Examples:
  1011.     make planks: craft{craftItem = "planks", craftQuantity = 8, sourceItem1 = "wood", destSlot = 3, chest = "doNotUse"}
  1012.     make 1 chest: craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 4}
  1013.     checkRemaining craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = emptySlot}
  1014.     late stage: craft{craftItem = "computer", craftQuantity = 1, sourceItem1 = "glass panes", sourceItem2 = "redstone",sourceItem3 = "stone", destSlot = 16}
  1015.     ]]--
  1016.    
  1017.     --local sourceItem1 = arg.sourceItem1
  1018.     --local sourceItem2 = arg.sourceItem2
  1019.     --local sourceItem3 = arg.sourceItem3
  1020.     local sourceSlot1 = 0
  1021.     local sourceSlot2 = 0
  1022.     local sourceSlot3 = 0
  1023.     local sourceQuantity1 = 0
  1024.     local sourceQuantity2 = 0
  1025.     local sourceQuantity3 = 0
  1026.     local success = false
  1027.     local useChest = true
  1028.     local emptySlot = 0
  1029.     local gridContents = {}
  1030.     local gridSlot = {}
  1031.     local turns = 0
  1032.    
  1033.     for i = 1, 16 do
  1034.         gridContents[i] = ""
  1035.         gridSlot[i] = 0
  1036.     end
  1037.    
  1038.     if arg.doSort == nil then
  1039.         arg.doSort = false
  1040.     end
  1041.     if arg.doSort then
  1042.         sortInventory(true)
  1043.     end
  1044.    
  1045.     if arg.sourceItem1 == nil then
  1046.         sourceSlot1 = 0
  1047.         sourceQuantity1 = 0
  1048.     else
  1049.         sourceSlot1 = slot:getItemSlot(arg.sourceItem1)
  1050.         sourceQuantity1 = turtle.getItemCount(sourceSlot1)
  1051.     end
  1052.     if arg.sourceItem2 == nil then
  1053.         arg.sourceItem2 = ""
  1054.         sourceSlot2 = 0
  1055.     else
  1056.         sourceSlot2 = slot.getItemSlot(slot, arg.sourceItem2)
  1057.         sourceQuantity2 = turtle.getItemCount(sourceSlot2)
  1058.     end
  1059.     if arg.sourceItem3 == nil then
  1060.         arg.sourceItem3 = ""
  1061.         sourceSlot3 = 0
  1062.     else
  1063.         sourceSlot3 = slot.getItemSlot(slot, arg.sourceItem3)
  1064.         sourceQuantity3 = turtle.getItemCount(sourceSlot3)
  1065.     end
  1066.     if arg.destSlot == nil then
  1067.         arg.destSlot = 16
  1068.     end
  1069.     if arg.chest == "doNotUse" then
  1070.         useChest = false
  1071.     end
  1072.     saveToLog("craft: craftItem = "..tostring(arg.craftItem)..
  1073.                 ", craftQuantity = "..tostring(arg.craftQuantity)..
  1074.                 ", sourceItem1 = "..tostring(arg.sourceItem1)..
  1075.                 ", sourceItem2 = "..tostring(arg.sourceItem2)..
  1076.                 ", sourceItem3 = "..tostring(arg.sourceItem3)..
  1077.                 ", destSlot = "..tostring(arg.destSlot), false)
  1078.                
  1079.     if useChest then --place chest forwards
  1080.         while turtle.detect() do --check for clear space
  1081.             turnRight(1)
  1082.             turns = turns + 1
  1083.             if turns == 4 then
  1084.                 turns = 0
  1085.                 break
  1086.             end
  1087.         end
  1088.         turtle.select(1)
  1089.         while turtle.detect() do --clear space in front
  1090.             dig.digNew{self = dig, slotNo = 1, callFrom = "craft"}
  1091.         end
  1092.         while attack() do --in case mobs in front
  1093.             saveToLog("Mob attacked again!", false)
  1094.         end
  1095.         turtle.select(slot:getItemSlot("chests"))
  1096.         while not turtle.place() do
  1097.             while attack() do --in case mobs in front
  1098.                 saveToLog("Mob attacked again!", false)
  1099.             end
  1100.         end
  1101.         currentFunction = "craft"
  1102.         --check if chest is actually in front until cc 1.6.3 bug is sorted
  1103.         local tempChestSlot = slot:getItemSlot("chests")
  1104.         slot.update{self = slot, item = "chests", delete = true}
  1105.         if not turtle.detect() then --chest not present
  1106.             saveToLog("cc 1.6.3 place() bug, moving forward to rescue chest", true)
  1107.             forward(1)
  1108.             if turtle.detect() then
  1109.                 saveToLog("cc 1.6.3 place() bug, ? chest found in front, digging...", true)
  1110.                 dig.digNew{self = dig, slotNo = tempChestSlot, expectedItem = "chests", callFrom = "craft"}
  1111.             elseif turtle.detectDown() then
  1112.                 saveToLog("cc 1.6.3 place() bug, ? chest found below, digging...", true)
  1113.                 dig.digNew{self = dig, direction = "down", slotNo = tempChestSlot, expectedItem = "chests", callFrom = "craft"}
  1114.             end
  1115.             turtle.select(tempChestSlot)
  1116.             if turtle.refuel(0) then
  1117.                 saveToLog("cc 1.6.3 place() bug, chest found!", true)
  1118.                 slot.update{self = slot, slotNo = tempChestSlot, item = "chests"}
  1119.             else
  1120.                 saveToLog("cc 1.6.3 place() bug, ? chest lost. exiting program...", true)
  1121.                 error()
  1122.             end
  1123.             back(1)
  1124.             turtle.select(slot:getItemSlot("chests"))
  1125.             turtle.place()
  1126.             slot.update{self = slot, item = "chests", delete = true}
  1127.         end
  1128.         -- saveToLog("craft: Filling crafting chest..", false)
  1129.         saveStatus()
  1130.         fillChest{direction = arg.direction, exceptSlot1 = sourceSlot1, exceptSlot2 = sourceSlot2, exceptSlot3 = sourceSlot3, doReset = false} -- fill except chosen item(s)
  1131.         saveStatus()
  1132.     end
  1133.     if destSlot == 0 then
  1134.         saveToLog("Craft: item = "..arg.craftItem.." Quantity = "..arg.craftQuantity.." from "..arg.sourceItem1..", "..arg.sourceItem2..", "..arg.sourceItem3.." put in first empty slot", true)
  1135.     else
  1136.         saveToLog("Craft: item = "..arg.craftItem.." Quantity = "..arg.craftQuantity.." from "..arg.sourceItem1..", "..arg.sourceItem2..", "..arg.sourceItem3.." put in slot "..arg.destSlot, true)
  1137.     end
  1138.     --turtle emptied out except for crafting items
  1139.     if sourceSlot1 ~= 16 then --not already in position
  1140.         if turtle.getItemCount(16) == 0 then
  1141.             -- saveToLog("Craft: moving "..arg.sourceItem1.." from "..sourceSlot1.." to slot 16", true, false)
  1142.             turtle.select(sourceSlot1)
  1143.             turtle.transferTo(16) --move selected first item to slot 16
  1144.             slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
  1145.         else -- already occupied, inventory full, so swap 15 and 16
  1146.             for  i = 1, 14 do
  1147.                 if turtle.getItemCount(i) == 0 then
  1148.                     emptySlot = i
  1149.                 end
  1150.                 break
  1151.             end
  1152.             if sourceSlot2 == 16 then
  1153.                 sourceSlot2 = emptySlot
  1154.             end
  1155.             -- saveToLog("Craft: moving "..slot:getSlotContains(16).." from slot 16 to slot "..emptySlot, false)
  1156.             turtle.select(16)
  1157.             turtle.transferTo(emptySlot)
  1158.             slot.transfer{self = slot, item = slot:getSlotContains(16), transferTo = emptySlot}
  1159.             -- saveToLog("Craft: moving "..arg.sourceItem1.." from "..sourceSlot1.." to slot 16", false)
  1160.             turtle.select(sourceSlot1)
  1161.             turtle.transferTo(16) --move selected first item to slot 16
  1162.             slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
  1163.         end
  1164.     end
  1165.     turtle.select(16)
  1166.     if arg.craftItem == "planks" or arg.craftItem == "planks2" then  --craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 3}
  1167.         turtle.transferTo(1, arg.craftQuantity / 4)
  1168.         gridContents[1] = arg.sourceItem1
  1169.     elseif arg.craftItem == "chests" then  --craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 4}
  1170.         --8 planks = 1 chest
  1171.         turtle.transferTo(1, arg.craftQuantity)
  1172.         turtle.transferTo(2, arg.craftQuantity)
  1173.         turtle.transferTo(3, arg.craftQuantity)
  1174.         turtle.transferTo(5, arg.craftQuantity)
  1175.         turtle.transferTo(7, arg.craftQuantity)
  1176.         turtle.transferTo(9, arg.craftQuantity)
  1177.         turtle.transferTo(10, arg.craftQuantity)
  1178.         turtle.transferTo(11, arg.craftQuantity)
  1179.         gridContents[1] = arg.sourceItem1
  1180.         gridContents[2] = arg.sourceItem1
  1181.         gridContents[3] = arg.sourceItem1
  1182.         gridContents[5] = arg.sourceItem1
  1183.         gridContents[7] = arg.sourceItem1
  1184.         gridContents[9] = arg.sourceItem1
  1185.         gridContents[10] = arg.sourceItem1
  1186.         gridContents[11] = arg.sourceItem1
  1187.         gridSlot[1] = sourceSlot1
  1188.         gridSlot[2] = sourceSlot1
  1189.         gridSlot[3] = sourceSlot1
  1190.         gridSlot[5] = sourceSlot1
  1191.         gridSlot[7] = sourceSlot1
  1192.         gridSlot[9] = sourceSlot1
  1193.         gridSlot[10] = sourceSlot1
  1194.         gridSlot[11] = sourceSlot1
  1195.     elseif arg.craftItem == "furnaces" then --craft{craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 16}
  1196.         -- 8 cobble = 1 furnace
  1197.         turtle.transferTo(1, arg.craftQuantity)
  1198.         turtle.transferTo(2, arg.craftQuantity)
  1199.         turtle.transferTo(3, arg.craftQuantity)
  1200.         turtle.transferTo(5, arg.craftQuantity)
  1201.         turtle.transferTo(7, arg.craftQuantity)
  1202.         turtle.transferTo(9, arg.craftQuantity)
  1203.         turtle.transferTo(10, arg.craftQuantity)
  1204.         turtle.transferTo(11, arg.craftQuantity)
  1205.         gridContents[1] = arg.sourceItem1
  1206.         gridContents[2] = arg.sourceItem1
  1207.         gridContents[3] = arg.sourceItem1
  1208.         gridContents[5] = arg.sourceItem1
  1209.         gridContents[7] = arg.sourceItem1
  1210.         gridContents[9] = arg.sourceItem1
  1211.         gridContents[10] = arg.sourceItem1
  1212.         gridContents[11] = arg.sourceItem1
  1213.         gridSlot[1] = sourceSlot1
  1214.         gridSlot[2] = sourceSlot1
  1215.         gridSlot[3] = sourceSlot1
  1216.         gridSlot[5] = sourceSlot1
  1217.         gridSlot[7] = sourceSlot1
  1218.         gridSlot[9] = sourceSlot1
  1219.         gridSlot[10] = sourceSlot1
  1220.         gridSlot[11] = sourceSlot1
  1221.     elseif arg.craftItem == "sticks" then --craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = 13}  
  1222.         -- 2 planks gives 4 sticks
  1223.         turtle.transferTo(1, arg.craftQuantity / 4)
  1224.         turtle.transferTo(5, arg.craftQuantity / 4)
  1225.         gridContents[1] = arg.sourceItem1
  1226.         gridContents[5] = arg.sourceItem1
  1227.         gridSlot[1] = sourceSlot1
  1228.         gridSlot[5] = sourceSlot1
  1229.     elseif arg.craftItem == "torches" then --craft{craftItem = "torches", craftQuantity = 4, sourceItem1 = "sticks", sourceItem2 = "charcoal" destSlot = 16}// OR sourceItem2 = "coal"
  1230.         -- 1 stick + 1 coal/charcoal = 4 torches
  1231.         if sourceSlot2 ~= 15 then
  1232.             turtle.select(sourceSlot2) -- move coal/charcoal to 15
  1233.             turtle.transferTo(15)
  1234.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  1235.         end
  1236.         turtle.select(16)
  1237.         turtle.transferTo(5, arg.craftQuantity / 4) --move sticks to 5
  1238.         turtle.select(15)
  1239.         turtle.transferTo(1, arg.craftQuantity / 4) --move coal/charcoal to 1
  1240.         gridContents[1] = arg.sourceItem2
  1241.         gridContents[5] = arg.sourceItem1
  1242.         gridSlot[1] = sourceSlot2
  1243.         gridSlot[5] = sourceSlot1
  1244.     elseif arg.craftItem == "signs" then --craft{craftItem = "signs", craftQuantity = 3, sourceItem1 = "sticks", sourceItem2 = "planks" destSlot = 16}
  1245.         -- 1 stick + 6 planks = 3 signs
  1246.         if sourceSlot2 ~= 15 then
  1247.             turtle.select(sourceSlot2) -- move planks to 15
  1248.             turtle.transferTo(15)
  1249.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  1250.         end
  1251.         turtle.select(16)
  1252.         turtle.transferTo(10, arg.craftQuantity / 3) --move sticks to 5
  1253.         turtle.select(15)
  1254.         turtle.transferTo(1, arg.craftQuantity / 3) --move planks to 1
  1255.         turtle.transferTo(2, arg.craftQuantity / 3) --move planks to 2
  1256.         turtle.transferTo(3, arg.craftQuantity / 3) --move planks to 3
  1257.         turtle.transferTo(5, arg.craftQuantity / 3) --move planks to 5
  1258.         turtle.transferTo(6, arg.craftQuantity / 3) --move planks to 6
  1259.         turtle.transferTo(7, arg.craftQuantity / 3) --move planks to 7
  1260.         gridContents[1] = arg.sourceItem2
  1261.         gridContents[2] = arg.sourceItem2
  1262.         gridContents[3] = arg.sourceItem2
  1263.         gridContents[5] = arg.sourceItem2
  1264.         gridContents[6] = arg.sourceItem2
  1265.         gridContents[7] = arg.sourceItem2
  1266.         gridContents[10] = arg.sourceItem1
  1267.         gridSlot[1] = sourceSlot2
  1268.         gridSlot[2] = sourceSlot2
  1269.         gridSlot[3] = sourceSlot2
  1270.         gridSlot[5] = sourceSlot2
  1271.         gridSlot[6] = sourceSlot2
  1272.         gridSlot[7] = sourceSlot2
  1273.         gridSlot[10] = sourceSlot1
  1274.     elseif arg.craftItem == "ladders" then --craft{craftItem = "ladders", craftQuantity = 3, sourceItem1 = "sticks", destSlot = 0}
  1275.         -- 7 sticks = 3 ladders
  1276.         turtle.transferTo(1, arg.craftQuantity / 3)
  1277.         turtle.transferTo(3, arg.craftQuantity / 3)
  1278.         turtle.transferTo(5, arg.craftQuantity / 3)
  1279.         turtle.transferTo(6, arg.craftQuantity / 3)
  1280.         turtle.transferTo(7, arg.craftQuantity / 3)
  1281.         turtle.transferTo(9, arg.craftQuantity / 3)
  1282.         turtle.transferTo(11, arg.craftQuantity / 3)
  1283.         gridContents[1] = arg.sourceItem1
  1284.         gridContents[3] = arg.sourceItem1
  1285.         gridContents[5] = arg.sourceItem1
  1286.         gridContents[6] = arg.sourceItem1
  1287.         gridContents[7] = arg.sourceItem1
  1288.         gridContents[9] = arg.sourceItem1
  1289.         gridContents[11] = arg.sourceItem1
  1290.         gridSlot[1] = sourceSlot1
  1291.         gridSlot[3] = sourceSlot1
  1292.         gridSlot[5] = sourceSlot1
  1293.         gridSlot[6] = sourceSlot1
  1294.         gridSlot[7] = sourceSlot1
  1295.         gridSlot[9] = sourceSlot1
  1296.         gridSlot[11] = sourceSlot1
  1297.     elseif arg.craftItem == "stairs" then  --craft{craftItem = "stairs", craftQuantity = 4, sourceItem1 = "cobble", destSlot = 0}
  1298.         --6 cobble = 4 stairs
  1299.         turtle.transferTo(1, arg.craftQuantity / 4)
  1300.         turtle.transferTo(5, arg.craftQuantity / 4)
  1301.         turtle.transferTo(6, arg.craftQuantity / 4)
  1302.         turtle.transferTo(9, arg.craftQuantity / 4)
  1303.         turtle.transferTo(10, arg.craftQuantity / 4)
  1304.         turtle.transferTo(11, arg.craftQuantity / 4)
  1305.  
  1306.         gridContents[1] = arg.sourceItem1
  1307.         gridContents[5] = arg.sourceItem1
  1308.         gridContents[6] = arg.sourceItem1
  1309.         gridContents[9] = arg.sourceItem1
  1310.         gridContents[10] = arg.sourceItem1
  1311.         gridContents[11] = arg.sourceItem1
  1312.  
  1313.         gridSlot[1] = sourceSlot1
  1314.         gridSlot[5] = sourceSlot1
  1315.         gridSlot[6] = sourceSlot1
  1316.         gridSlot[9] = sourceSlot1
  1317.         gridSlot[10] = sourceSlot1
  1318.         gridSlot[11] = sourceSlot1
  1319.     end
  1320.     if useChest then
  1321.         if turtle.getItemCount(16) > 0 then
  1322.             -- saveToLog("craft: adding excess "..arg.sourceItem1.." to chest from slot 16 (/"..sourceSlot1..")", false)
  1323.             fillChest{direction = arg.direction, addItem = arg.sourceItem1, addAmount = turtle.getItemCount(16), fromSlot = 16, originalSlot = sourceSlot1} -- add remaining source items to chest
  1324.         end
  1325.         if turtle.getItemCount(15) > 0 then
  1326.             -- saveToLog("craft: adding excess "..arg.sourceItem2.." to chest from slot 15 (/"..sourceSlot2..")", false)
  1327.             fillChest{direction = arg.direction, addItem = arg.sourceItem2, addAmount = turtle.getItemCount(15),  fromSlot = 15, originalSlot = sourceSlot2} -- add remaining source items to chest
  1328.         end
  1329.         if turtle.getItemCount(14) > 0 then
  1330.             -- saveToLog("craft: adding excess "..arg.sourceItem3.." to chest from slot 14 (/"..sourceSlot3..")", false)
  1331.             fillChest{direction = arg.direction, addItem = arg.sourceItem3, addAmount = turtle.getItemCount(14),  fromSlot = 14, originalSlot = sourceSlot3} -- add remaining source items to chest
  1332.         end
  1333.         --slot.update{self = slot, slotNo = 16, item = arg.sourceItem1, delete = true}
  1334.         slot.update{self = slot, item = arg.sourceItem1, delete = true}
  1335.         if arg.sourceItem2 ~= "" then
  1336.             slot.update{self = slot, item = arg.sourceItem2, delete = true}
  1337.         end
  1338.         if arg.sourceItem3 ~= "" then
  1339.             slot.update{self = slot, item = arg.sourceItem3, delete = true}
  1340.         end
  1341.         saveStatus()
  1342.     end
  1343.    
  1344.     -- Attempt to craft item
  1345.     turtle.select(16)
  1346.     -- saveToLog("Attempting to craft "..arg.craftItem.." in slot 16", false)
  1347.     if turtle.craft() then
  1348.         success = true
  1349.         saveToLog("craft: success "..arg.craftItem.." in slot 16", true)
  1350.         --now put crafted item in chest first, so will mix with any existing similar items
  1351.         if useChest then
  1352.             -- saveToLog("craft: adding  crafted item "..arg.craftItem.." to chest", false)
  1353.             fillChest{direction = arg.direction, addItem = arg.craftItem, addAmount = turtle.getItemCount(16), fromSlot = 16, originalSlot = 16} -- add crafted item
  1354.         end
  1355.     else --crafting not successful, so empty out all items from chest
  1356.         saveToLog("Attempting to craft "..arg.craftItem.." did not succeed", true)
  1357.         --error()
  1358.         if useChest then
  1359.             saveToLog("craft: failed, filling chest to empty turtle", false)
  1360.             for i = 1, 16 do
  1361.                 if turtle.getItemCount(i) > 0 then
  1362.                     fillChest{direction = arg.direction, addItem = gridContents[i], addAmount = turtle.getItemCount(i), fromSlot = i, originalSlot = gridSlot[i]} -- add crafted item
  1363.                 end
  1364.             end
  1365.         end
  1366.     end
  1367.     saveStatus()
  1368.     if useChest then -- always, except in firstTree()
  1369.         saveToLog("Crafting finished, emptying chest back into turtle", false)
  1370.         emptyChest(arg.direction)
  1371.         if slot.getItemSlot(slot, "chests") > 0 then
  1372.             emptySlot = slot:getItemSlot("chests")
  1373.         else
  1374.             emptySlot = getFirstEmptySlot(false)
  1375.         end
  1376.         dig.digNew{self = dig, slotNo = emptySlot, expectedItem = "chests", callFrom = "craft"}
  1377.         slot.update{self = slot, slotNo = emptySlot, item = "chests"}
  1378.         currentFunction = tempCurrentFunction
  1379.         if success then --item crafted OK. if existing eg torches, will be in correct slot already, else should return to 16
  1380.             turtle.select(16)
  1381.             if turtle.getItemCount(16) > 0 then --new item, so returned to 16
  1382.                 if arg.destSlot == 0 then --use any slot
  1383.                     arg.destSlot = getFirstEmptySlot(false)
  1384.                     if arg.destSlot == 0 then --no empty slots
  1385.                         arg.destSlot = 16
  1386.                     end
  1387.                 else
  1388.                     if slot:getItemSlot(arg.craftItem) > 0 then
  1389.                         arg.destSlot = slot:getItemSlot(arg.craftItem)
  1390.                     end
  1391.                 end
  1392.                 if arg.destSlot == 16 then
  1393.                     slot.update{self = slot, slotNo = 16, item = arg.craftItem}
  1394.                 else --move to selected slot
  1395.                     turtle.select(16)
  1396.                     if turtle.transferTo(arg.destSlot) then
  1397.                         saveToLog("craft: Moved "..arg.craftItem.." to slot "..arg.destSlot, false)
  1398.                         slot.transfer{self = slot, item = arg.craftItem, transferTo = arg.destSlot}
  1399.                     else --slot full/other item
  1400.                         saveToLog("craft: Moving "..arg.craftItem.." to slot "..arg.destSlot.." failed. Still in slot 16", false)
  1401.                         emptySlot = getFirstEmptySlot(false)
  1402.                         if turtle.transferTo(emptySlot) then
  1403.                             saveToLog("craft: Moving "..arg.craftItem.." to slot "..emptySlot.." instead", false)
  1404.                             slot.update{self = slot, slotNo = emptySlot, item = arg.craftItem}
  1405.                         end
  1406.                     end
  1407.                 end
  1408.             else
  1409.                 saveToLog("craft: crafted item "..arg.craftItem.." already in correct slot", false)
  1410.             end
  1411.             sortInventory(true)
  1412.         end
  1413.     else --chest not used, only for first log first planks and first chest
  1414.         saveToLog("craft: "..arg.craftItem.." created in slot 16, moving to "..arg.destSlot, false)
  1415.         turtle.transferTo(arg.destSlot)
  1416.         slot.update{self = slot, slotNo = arg.destSlot, item = arg.craftItem}
  1417.         slot.update{self = slot, slotNo = 16, delete = true}
  1418.     end
  1419.     saveStatus()
  1420.     if turns > 0 then
  1421.         turnLeft(turns)
  1422.     end
  1423.     saveToLog("craft: function exit, success = "..tostring(success), false)
  1424.     return success
  1425. end
  1426.  
  1427. function craftChests(quantity)
  1428.     local previousCallingFunction = callingFunction
  1429.     local previousFunction = currentFunction
  1430.     local success = false
  1431.     local makePlanks = false
  1432.     local woodNeeded = 0
  1433.     local planksNeeded = 0
  1434.     local numWoodOnboard = 0
  1435.     local numPlanksOnboard = 0
  1436.    
  1437.     currentFunction = "craftChests"
  1438.    
  1439.     planksNeeded = 8 * quantity
  1440.     woodNeeded = 2 * quantity
  1441.    
  1442.     changeDirection("faceForward")
  1443.     if slot:getItemSlot("planks") > 0 then
  1444.         numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks"))
  1445.     end
  1446.     if numPlanksOnboard < planksNeeded then
  1447.         makePlanks = true
  1448.         saveToLog("craftChests: started, crafting planks", true)
  1449.         --woodNeeded already calculated
  1450.     else
  1451.         woodNeeded = 0 --reset
  1452.         saveToLog("craftChests: started, using planks already onboard", true)
  1453.     end
  1454.    
  1455.     if makePlanks then --need wood
  1456.         if slot:getItemSlot("wood") > 0 then
  1457.             numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1458.         end
  1459.         if numWoodOnboard - 1 >= woodNeeded then
  1460.             saveToLog("craftChests: crafting planks for chests", true)
  1461.             craft{calledFrom = "craftChests",craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
  1462.         else
  1463.             goToWoodStore(false)
  1464.             numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1465.         end
  1466.     end
  1467.     saveToLog("craftChests: crafting "..quantity.." chests from planks", true)             
  1468.     if craft{calledFrom = "craftChests", craftItem = "chests", craftQuantity = quantity, sourceItem1 = "planks", destSlot = 0, doSort = true} then
  1469.         success = true
  1470.     end
  1471.    
  1472.     callingFunction = previousCallingFunction
  1473.     currentFunction = previousFunction
  1474.     return success
  1475. end
  1476.  
  1477. function craftLadders(quantity)
  1478.     local woodAvailable = 0
  1479.     local success = false
  1480.     local numSticksOnboard = 0
  1481.     local numLaddersOnboard = 0
  1482.     local numWoodOnboard = 0
  1483.     local numWood2Onboard = 0
  1484.     local numWoodNeeded = 0
  1485.     local numRequested = 0
  1486.     local previousCallingFunction = callingFunction
  1487.     local previousFunction = currentFunction
  1488.    
  1489.     currentFunction = "craftLadders"
  1490.     if quantity > 27 then
  1491.         quantity = 27
  1492.     end
  1493.     numRequested = quantity
  1494.     if quantity <= 21 then
  1495.         numWoodNeeded = math.floor(quantity / 3)
  1496.     elseif quantity == 24 then
  1497.         numWoodNeeded = 7
  1498.     else
  1499.         numWoodNeeded = 8
  1500.     end
  1501.    
  1502.     -- 7 sticks = 3 ladders
  1503.     -- 1 wood = 4 planks
  1504.     -- 4 planks = 8 sticks
  1505.     -- 7 sticks = 3 ladders
  1506.     -- ladders  sticks  planks  wood
  1507.     -- 3        7       4       1
  1508.     -- 6        14      8       2
  1509.     -- 9        21      12      3
  1510.     -- 12       28      14      4
  1511.     -- 15       35      18      5
  1512.     -- 18       42      22      6
  1513.     -- 21       49      26      7
  1514.     -- 24       54      28      7
  1515.     -- 27       63      32      8
  1516.     saveToLog("craftLadders: started, crafting "..quantity.."ladders", true)
  1517.     changeDirection("faceForward")
  1518.  
  1519.     numSticksOnboard = getStock("sticks")
  1520.     numLaddersOnboard = getStock("ladders")
  1521.     numWoodOnboard = getStock("wood")
  1522.     numWood2Onboard = getStock("wood-1")
  1523.     if numWoodOnboard == 0 then --wood in storage, needed for ladders
  1524.         goToWoodStore(false)
  1525.         numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1526.     end
  1527.     if numWoodOnboard >= 1 or numWood2Onboard >= 1 then --enough to make ladders
  1528.         if numWoodNeeded > numWoodOnboard + numWood2Onboard then
  1529.             numWoodNeeded = numWoodOnboard + numWood2Onboard
  1530.             quantity = numWoodOnboard * 3
  1531.         end
  1532.         if numWood2Onboard > 0 then
  1533.             if numWoodNeeded <= numWood2Onboard then --enough wood-1 for all ladders
  1534.                 saveToLog("craftLadders: crafting planks from wood-1", false)
  1535.                 craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = numWoodNeeded * 4, sourceItem1 = "wood-1", destSlot = 0, doSort = true}
  1536.                 saveToLog("craftLadders: crafting sticks from planks", false)
  1537.                 craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = numWoodNeeded * 8, sourceItem1 = "planks", destSlot = 0, doSort = true}
  1538.                 saveToLog("craftLadders: crafting ladders from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
  1539.                 if craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = quantity, sourceItem1 = "sticks", destSlot = 0, doSort = true} then
  1540.                     success = true
  1541.                 end
  1542.             else --only enough for some ladders
  1543.                 --make all wood-1 into sticks
  1544.                 saveToLog("craftLadders: crafting planks from wood-1", false)
  1545.                 craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = getStock("wood-1") * 4, sourceItem1 = "wood-1", destSlot = 0, doSort = true}
  1546.                 saveToLog("craftLadders: crafting sticks from planks", false)
  1547.                 craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = getStock("planks") * 2, sourceItem1 = "planks", destSlot = 0, doSort = true}
  1548.                 craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = math.floor(getStock("sticks") / 7) * 3, sourceItem1 = "sticks", destSlot = 0, doSort = true}
  1549.                 quantity = quantity - getStock("ladders")
  1550.                 if quantity > 0 then
  1551.                     numWoodNeeded = math.floor(quantity / 3)
  1552.                     saveToLog("craftLadders: crafting planks from wood", false)
  1553.                     craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = numWoodNeeded * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
  1554.                     saveToLog("craftLadders: crafting sticks from planks", false)
  1555.                     craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = numWoodNeeded * 8, sourceItem1 = "planks", destSlot = 0, doSort = true}
  1556.                     saveToLog("craftLadders: crafting ladders from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
  1557.                     craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = quantity, sourceItem1 = "sticks", destSlot = 0, doSort = true}
  1558.                 end
  1559.             end
  1560.         else
  1561.             saveToLog("craftLadders: crafting planks from wood", false)
  1562.             craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = numWoodNeeded * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
  1563.             saveToLog("craftLadders: crafting sticks from planks", false)
  1564.             craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = numWoodNeeded * 8, sourceItem1 = "planks", destSlot = 0, doSort = true}
  1565.             saveToLog("craftLadders: crafting ladders from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
  1566.             craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = quantity, sourceItem1 = "sticks", destSlot = 0, doSort = true}
  1567.         end
  1568.         if getStock("ladders") >= numRequested then
  1569.             success = true
  1570.         end
  1571.     else
  1572.         saveToLog("craftLadders: insufficient wood available", true)
  1573.     end
  1574.    
  1575.     callingFunction = previousCallingFunction
  1576.     currentFunction = previousFunction
  1577.     return success
  1578. end
  1579.  
  1580. function craftSigns(quantity)
  1581.     local success = false
  1582.     local numWoodOnboard = 0
  1583.     local numWoodNeeded = 0
  1584.     local numPlanksNeeded = 0
  1585.     local previousCallingFunction = callingFunction
  1586.     local previousFunction = currentFunction
  1587.    
  1588.     currentFunction = "craftSigns"
  1589.  
  1590.     quantity = quantity or 3
  1591.     if quantity > 12 then
  1592.         quantity = 12
  1593.     end
  1594.     --make 3 signs by default , need 8 planks, leaves 3 sticks
  1595.     --[[
  1596.         signs   planks  sticks  wood
  1597.         3       6 + 2   1       2
  1598.         6       12 + 2  2       4
  1599.         9       18 + 2  3       5
  1600.         12      24 + 2  4       7
  1601.     ]]--
  1602.    
  1603.     saveToLog("craftSigns: started, crafting "..quantity.." signs", true)
  1604.     if quantity == 3 then
  1605.         numWoodNeeded = 2
  1606.         numPlanksNeeded = 8
  1607.     elseif quantity == 6 then
  1608.         numWoodNeeded = 4
  1609.         numPlanksNeeded = 16
  1610.     elseif quantity == 9 then
  1611.         numWoodNeeded = 5
  1612.         numPlanksNeeded = 20
  1613.     else
  1614.         numWoodNeeded = 7
  1615.         numPlanksNeeded = 28
  1616.     end
  1617.     changeDirection("faceForward")
  1618.     numWoodOnboard = getStock("wood")
  1619.     if numWoodOnboard < numWoodNeeded then
  1620.         saveToLog("craftSigns: insufficient wood available", true)
  1621.         goToWoodStore(false)
  1622.     end
  1623.     numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1624.     saveToLog("craftSigns: crafting planks from wood", false)
  1625.     craft{calledFrom = "craftSigns", craftItem = "planks", craftQuantity = numPlanksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = false}
  1626.     saveToLog("craftSigns: crafting sticks from planks", false)
  1627.     craft{calledFrom = "craftSigns", craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = 0, doSort = false}
  1628.     saveToLog("craftSigns: crafting signs from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
  1629.     if craft{calledFrom = "craftSigns", craftItem = "signs", craftQuantity = quantity, sourceItem1 = "sticks", sourceItem2  = "planks", destSlot = 0, doSort = true} then
  1630.         success = true
  1631.     end
  1632.     if getStock("sticks") > 0 then
  1633.         storeSticks()
  1634.     end
  1635.     if getStock("planks") > 0 then
  1636.         turtle.select(slot:getItemSlot("planks"))
  1637.         turtle.refuel()
  1638.         slot.update{self = slot, item = "planks", delete = true}
  1639.         sortInventory(true)
  1640.     end
  1641.    
  1642.     callingFunction = previousCallingFunction
  1643.     currentFunction = previousFunction
  1644.     return success
  1645. end
  1646.  
  1647. function craftSticks(quantity)
  1648.     local success = false
  1649.     local makePlanks = false
  1650.     local woodNeeded = 0
  1651.     local planksNeeded = 0
  1652.     local numWoodOnboard = 0
  1653.     local numPlanksOnboard = 0
  1654.     local previousCallingFunction = callingFunction
  1655.     local previousFunction = currentFunction
  1656.    
  1657.     currentFunction = "craftSticks"
  1658.     if quantity <= 4 then
  1659.         quantity = 4
  1660.         planksNeeded = 4   
  1661.         woodNeeded = 1
  1662.     elseif quantity <= 8 then
  1663.         quantity = 8
  1664.         planksNeeded = 4
  1665.         woodNeeded = 1
  1666.     elseif quantity <= 12 then
  1667.         quantity = 12
  1668.         planksNeeded = 8
  1669.         woodNeeded = 2
  1670.     else
  1671.         quantity = 16
  1672.         planksNeeded = 8
  1673.         woodNeeded = 2
  1674.     end
  1675.    
  1676.     changeDirection("faceForward")
  1677.     if slot:getItemSlot("planks") > 0 then
  1678.         numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks"))
  1679.     end
  1680.     if numPlanksOnboard < planksNeeded then
  1681.         makePlanks = true
  1682.         saveToLog("craftSticks: started, crafting planks", true)
  1683.         --woodNeeded already calculated
  1684.     else
  1685.         woodNeeded = 0 --reset
  1686.         saveToLog("craftSticks: started, using planks already onboard", true)
  1687.     end
  1688.    
  1689.     if makePlanks then --need wood
  1690.         if slot:getItemSlot("wood") > 0 then
  1691.             numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1692.         end
  1693.         if numWoodOnboard >= woodNeeded then
  1694.             saveToLog("craftSticks: crafting planks for sticks", true)
  1695.             craft{calledFrom = "craftSticks", craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
  1696.         else
  1697.             goToWoodStore(false)
  1698.             numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1699.         end
  1700.     end
  1701.     saveToLog("craftSticks: crafting "..quantity.." sticks from planks", true)             
  1702.     if craft{calledFrom = "craftSticks", craftItem = "sticks", craftQuantity = quantity, sourceItem1 = "planks", destSlot = 0, doSort = true} then
  1703.         stage["craftSticks"] = true
  1704.         saveStatus()
  1705.         success = true
  1706.     end
  1707.    
  1708.     callingFunction = previousCallingFunction
  1709.     currentFunction = previousFunction
  1710.     return success
  1711. end
  1712.  
  1713. function craftTorches(quantity)
  1714.     local headType = "charcoal"
  1715.     local headQuantity = 0
  1716.     local makeCharcoal = false
  1717.     local makePlanks = false
  1718.     local makeSticks =false
  1719.     local numWoodNeeded = 0
  1720.     local planksNeeded = 0
  1721.     local sticksNeeded = 0
  1722.     local success = false
  1723.     local numSticksOnboard = 0
  1724.     local numTorchesOnboard = 0
  1725.     local numWoodOnboard = 0
  1726.     local numTorchesStored = 0
  1727.     local doContinue = true
  1728.     local previousCallingFunction = callingFunction
  1729.     local previousFunction = currentFunction
  1730.    
  1731.     currentFunction = "craftTorches"
  1732.     --changeDirection("faceForward")
  1733.     getSticks()
  1734.     numTorchesOnboard = getStock("torches")
  1735.     numSticksOnboard = getStock("sticks")
  1736.  
  1737.     -- 4 torches min : 1 head + 1 stick = 4 torches. Min sticks  = 4, min planks = 4
  1738.     -- torches head planks sticks
  1739.     -- 4        1       4       4
  1740.     -- 8        2       4       4
  1741.     -- 12       3       4       4
  1742.     -- 16       4       4       4
  1743.     -- 20       5       4       8
  1744.     -- 24       6       4       8
  1745.     -- 28       7       4       8
  1746.     -- 32       8       4       8
  1747.     -- 36       9       8       12
  1748.     -- 40       10      8       12
  1749.     -- 44       11      8       12
  1750.     -- 48       12      8       12
  1751.     -- 52       13      8       16
  1752.     -- 56       14      8       16
  1753.     -- 60       15      8       16
  1754.     -- 64       16      8       16
  1755.     quantity = math.floor(quantity / 4) * 4
  1756.     if quantity <= 0 then
  1757.         quantity = 4 -- torches
  1758.         headQuantity = 1 -- coal /charcoal
  1759.         if numSticksOnboard < 4 then
  1760.             planksNeeded = 4 -- 1 wood
  1761.             sticksNeeded = 4 -- 2 planks
  1762.             numWoodNeeded = 1
  1763.         end
  1764.     elseif quantity <= 16 then-- 4, 8, 12, 16
  1765.         headQuantity = quantity / 4 -- coal, charcoal
  1766.         if numSticksOnboard < 4 then
  1767.             planksNeeded = 4  -- 8 planks = 16 sticks
  1768.             sticksNeeded = 4  -- 4 sticks
  1769.             numWoodNeededwoodNeeded = 1
  1770.         end
  1771.     elseif quantity <= 32 then-- 4, 8, 12, 16
  1772.         headQuantity = quantity / 4 -- coal, charcoal
  1773.         if numSticksOnboard < 8 then
  1774.             planksNeeded = 4  -- 8 planks = 16 sticks
  1775.             sticksNeeded = 8  -- 8 sticks
  1776.             numWoodNeeded = 2
  1777.         end
  1778.     elseif quantity <= 48 then-- 4, 8, 12, 16
  1779.         headQuantity = quantity / 4 -- coal, charcoal
  1780.         if numSticksOnboard < 12 then
  1781.             planksNeeded = 8  -- 8 planks = 16 sticks
  1782.             sticksNeeded = 12 -- 12 sticks
  1783.             numWoodNeeded = 2
  1784.         end
  1785.     else
  1786.         headQuantity = quantity / 4 -- coal, charcoal
  1787.         if numSticksOnboard < 16 then
  1788.             planksNeeded = 8  -- 8 planks = 16 sticks
  1789.             sticksNeeded = 16 -- 16 sticks
  1790.             numWoodNeeded = 2
  1791.         end
  1792.     end
  1793.     --need either coal or charcoal
  1794.     if slot:getItemSlot("coal") > 0 then
  1795.         if turtle.getItemCount(slot:getItemSlot("coal")) - 1 >= headQuantity  then
  1796.             headType = "coal"
  1797.             saveToLog("craftTorches: using coal", true)
  1798.         end
  1799.     end
  1800.  
  1801.     if headType == "charcoal" then --default value
  1802.         if slot:getItemSlot("charcoal") > 0 then
  1803.             if turtle.getItemCount(slot:getItemSlot("charcoal")) < headQuantity then
  1804.                 makeCharcoal = true
  1805.                 saveToLog("craftTorches: using charcoal", true)
  1806.             end
  1807.         else
  1808.             saveToLog("craftTorches: charcoal on crafting list", true)
  1809.             makeCharcoal = true
  1810.         end
  1811.         numWoodNeeded = numWoodNeeded + headQuantity
  1812.     end
  1813.     numWoodOnboard = getStock("wood")
  1814.     if getStock("wood") < 1 + numWoodNeeded then
  1815.         goToWoodStore(false)
  1816.     end
  1817.     saveToLog("craftTorches: quantity = "..quantity, true)
  1818.     getSticks()
  1819.     if slot:getItemSlot("wood") == 0 and makeCharcoal then --wood in storage, needed for charcoal
  1820.         goToWoodStore(false)
  1821.         numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1822.     end
  1823.  
  1824.     if numSticksOnboard == 0 or numSticksOnboard < headQuantity then
  1825.         makeSticks = true
  1826.         saveToLog("craftTorches: insufficient sticks in stock, on crafting list", true)
  1827.     else
  1828.         saveToLog("craftTorches: enough sticks in stock", true)
  1829.     end
  1830.    
  1831.     if makeSticks then
  1832.         if numWoodOnboard > 0 then
  1833.             saveToLog("craftTorches: crafting sticks")
  1834.             doContinue = false
  1835.             if craftSticks(sticksNeeded) then
  1836.                 doContinue = true
  1837.             end
  1838.             if slot:getItemSlot("wood") > 0 then
  1839.                 numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  1840.             end
  1841.         end
  1842.     end
  1843.     if numWoodOnboard == 0 then
  1844.         doContinue = false
  1845.         saveToLog("craftTorches: insufficient wood available", false, true)
  1846.     end
  1847.     if doContinue then
  1848.         if makeCharcoal then
  1849.             saveToLog("craftTorches: smelting charcoal")
  1850.             if smelt("charcoal", headQuantity) then
  1851.                 saveToLog("craftTorches: charcoal smelted", true)
  1852.             else
  1853.                 saveToLog("craftTorches: charcoal smelting failed", true)
  1854.                 doContinue = false
  1855.             end
  1856.         end
  1857.         if doContinue then
  1858.             saveToLog("craftTorches: crafting torches", true)
  1859.             --make quantity torches
  1860.             if craft{calledFrom = "craftTorches", craftItem = "torches", craftQuantity = quantity, sourceItem1 = "sticks", sourceItem2 = headType, destSlot = 0, doSort = true} then
  1861.                 saveToLog("craftTorches: "..quantity.." torches made", true)
  1862.                 success = true
  1863.             else
  1864.                 saveToLog("craftTorches: error: no torches made", true)
  1865.                 error()
  1866.             end
  1867.         end
  1868.     end
  1869.     storeSticks()
  1870.     if getStock("planks") > 0 then
  1871.         turtle.select(slot:getItemSlot("planks"))
  1872.         turtle.refuel()
  1873.         slot.update{self = slot, item = "planks", delete = true}
  1874.         sortInventory(true)
  1875.     end
  1876.    
  1877.     callingFunction = previousCallingFunction
  1878.     currentFunction = previousFunction
  1879.     return success
  1880. end
  1881.  
  1882. function createChestObject()   
  1883.     -- chest object, used to keep track of stored items
  1884.     clsChest = {} -- the table representing the class, which will double as the metatable for any instances
  1885.     clsChest.__index = clsChest -- failed table lookups on the instances should fallback to the class table, to get methods
  1886.  
  1887.     function clsChest.new(storeNo, setStoreName) --equivalent to VB class initialise
  1888.         local self = setmetatable({}, clsChest)
  1889.         self.items = {"wood", "wood2","dirt","cobble","stone","saplings","saplings2","seeds","sand","gravel","clay",
  1890.                 "apples","coal","charcoal","ironore","?ironore","goldore","?goldore","iron","?iron","gold","?gold",
  1891.                 "redstone","redstone block","?diamonds","diamonds","emeralds","sugar cane","planks","planks2","chests","sticks",
  1892.                 "torches","furnaces","signs","item1","item2","item3","item4","item5","item6","item7","item8",
  1893.                 "fences","ladders","gates","nuggets","buckets","paper","computer","crafting table","diamond pickaxe","glass","glass panes","floppy disk",
  1894.                 "disk drive","turtle","crafty mining turtle","?moss stone","moss stone","walls","obsidian","sandstone",
  1895.                 "?lapis","lapis","lapis block","flowers","roses","red mushrooms","brown mushrooms","cobble-1","cobble-2","cobble-3","cobble-4"
  1896.                 ,"cobble-5","cobble-6","dirt-1","dirt-2","dirt-3","dirt-4","gravel-1","gravel-2","redstone-1","redstone-2","redstone-3","wood-1", "stairs","coal-1","coal-2"}
  1897.         self.value = storeNo
  1898.         self.slotContains = {}
  1899.         self.slotCount = {}
  1900.         self.itemSlot = {}
  1901.         self.turtleSlot = {}
  1902.         self.index = 1
  1903.         self.name = setStoreName
  1904.         -- initialise variables
  1905.         for i = 1, 16 do
  1906.             self.slotContains[i] = ""
  1907.             self.slotCount[i] = 0
  1908.             self.turtleSlot[i] = 0
  1909.         end
  1910.        
  1911.         for key, value in ipairs(self.items) do
  1912.             self.itemSlot[value] = 0
  1913.         end
  1914.  
  1915.         return self
  1916.     end
  1917.    
  1918.     function clsChest.getStatus(self)
  1919.         local foundItemSlots = {}
  1920.        
  1921.         for key, value in ipairs(self.items) do
  1922.             if self.itemSlot[value] > 0 then
  1923.                 foundItemSlots[value] = self.itemSlot[value]
  1924.             end
  1925.         end
  1926.         return {self.slotContains, self.slotCount, foundItemSlots, self.turtleSlot}
  1927.     end
  1928.    
  1929.     function clsChest.setStatus(self, status)
  1930.         self.slotContains = status[1]
  1931.         self.slotCount = status[2]
  1932.         for key, value in ipairs(status[3]) do
  1933.             self.itemSlot[value] = status[3][value]
  1934.         end
  1935.         self.turtleSlot = status[4]
  1936.     end
  1937.    
  1938.     function clsChest.printContents(self)
  1939.         --debug log
  1940.         local printSlot = 0
  1941.         for i = 1, 16 do
  1942.             if self.slotCount[i] > 0 then
  1943.                 saveToLog(" clsChest: Slot "..i.." contains "..self.slotCount[i].." "..self.slotContains[i])
  1944.             end
  1945.         end
  1946.     end
  1947.    
  1948.     function clsChest.resetVariables(self)
  1949.         for i = 1, 16 do
  1950.             self.slotContains[i] = ""
  1951.             self.slotCount[i] = 0
  1952.             self.turtleSlot[i] = 0
  1953.         end
  1954.        
  1955.         for key, value in ipairs(self.items) do
  1956.             self.itemSlot[value] = 0
  1957.         end
  1958.         self.index = 1
  1959.     end
  1960.     function clsChest.getValue(self) --property get in VB
  1961.         return self.value
  1962.     end
  1963.     --craftingChest.getIndex(craftingChest)
  1964.     function clsChest.getIndex(self)
  1965.         return self.index
  1966.     end
  1967.    
  1968.     function clsChest.setSlotCount(self, slotNo, newVal) --property let in VB
  1969.         self.slotCount[slotNo] = newVal
  1970.     end
  1971.    
  1972.     function clsChest.getStoreName(self)
  1973.         return self.name
  1974.     end
  1975.    
  1976.     function clsChest.getSlotCount(self, slotNo)
  1977.         return self.slotCount[slotNo]
  1978.     end
  1979.    
  1980.     function clsChest.getSlotContains(self, slotNo)
  1981.         return self.slotContains[slotNo]
  1982.     end
  1983.    
  1984.     function clsChest.getStoreContains(self)
  1985.         -- return contents of first slot
  1986.         return self.slotContains[1]
  1987.     end
  1988.    
  1989.     function clsChest.getItemSlot(self, item)
  1990.         local result = 0
  1991.         if item ~= nil and item ~= "" then
  1992.             result = self.itemSlot[item]
  1993.         end
  1994.         return result
  1995.     end
  1996.    
  1997.     function clsChest.getTurtleSlot(self, slotNo )
  1998.         return self.turtleSlot[slotNo]
  1999.     end
  2000.    
  2001.     function clsChest.getItemCount(self, item)
  2002.         local result = 0
  2003.         if item ~= nil and item ~= "" then
  2004.             if self.itemSlot[item] > 0 then
  2005.                 result = self.slotCount[self.itemSlot[item]]
  2006.             end
  2007.         end
  2008.         return result
  2009.     end
  2010.    
  2011.     function clsChest.getNoOfItems(self)       
  2012.         return self.index - 1
  2013.     end
  2014.    
  2015.     function clsChest.isItemInChest(self, item)
  2016.         local success = false
  2017.         if item ~= nil then
  2018.             if self.itemSlot[item] > 0 then
  2019.                 success = true
  2020.             end
  2021.         end
  2022.         return success
  2023.     end
  2024.  
  2025.     function clsChest.addItem(self, item, quantity, turtleSlot)
  2026.         --storageSticks:addItem("sticks", 2, 14)
  2027.         -- addItem(arg.toStore, arg.item, arg.quantity)
  2028.         local slotNo = 0
  2029.         local newItemFound = false
  2030.         local newIndex = 1
  2031.        
  2032.         if turtleSlot == nil then
  2033.             turtleSlot = 0
  2034.         end
  2035.         if item == nil or item == "" then
  2036.             item = "item8"
  2037.             saveToLog(" clsChest.addItem Error, unknown item found", true)
  2038.         end
  2039.         if quantity == nil then
  2040.             quantity = 0
  2041.             saveToLog(" clsChest.addItem Error, nil quantity passed", true)
  2042.         end
  2043.         --unknown item only dropped in full, not added to
  2044.  
  2045.         slotNo = self.itemSlot[item] -- 0 by default, unless items already in chest
  2046.        
  2047.         if slotNo == 0 then --none of this item in chest
  2048.             self.itemSlot[item] = self.index --default value 1
  2049.             slotNo = self.itemSlot[item]
  2050.             self.slotContains[self.index] = item
  2051.             self.slotCount[self.index] = quantity
  2052.             self.turtleSlot[self.index] = turtleSlot
  2053.             self.index = self.index + 1 --ready for next item
  2054.         else --already in chest, update quantity
  2055.             turtleSlot = self.turtleSlot[self.itemSlot[item]]
  2056.             self.slotCount[slotNo] = self.slotCount[slotNo] + quantity
  2057.             if self.slotCount[slotNo] > 64 then
  2058.                 self.slotCount[slotNo + 1] = self.slotCount[slotNo] - 64
  2059.                 self.slotContains[slotNo + 1] = item
  2060.                 self.turtleSlot[slotNo + 1] = turtleSlot
  2061.                 self.slotCount[slotNo] = 64
  2062.             end
  2063.         end
  2064.         -- saveToLog("addItem: "..self.name.." added "..quantity.." of "..item.." from turtle slot "..turtleSlot, false)
  2065.     end
  2066.     --create 5 chest objects, 1 used for storage while crafting, others in the ground
  2067.     craftingChest = clsChest.new(0, "craftingChest")   
  2068.     storageWoodItems = clsChest.new(1, "storageWoodItems")      -- placed 2 blocks in front and 2 blocks to left of furnace
  2069.     storageWood = clsChest.new(2, "storageWood")            -- placed 2 blocks in front of furnace
  2070.     storageSticks = clsChest.new(3, "storageSticks")        -- placed 2 blocks in front and 2 blocks to right of furnace
  2071.     storageBuilding = clsChest.new(4, "storageBuilding")    -- placed 2 blocks to left of furnace
  2072.     storageOres = clsChest.new(5, "storageOres")            -- placed 2 blocks to right of furnace
  2073.     storageMinerals = clsChest.new(6, "storageMinerals")    -- placed 2 blocks behind and 2 blocks to left of furnace
  2074.     storageMisc = clsChest.new(7, "storageMisc")            -- placed 2 blocks behind and 2 blocks to right of furnace
  2075. end
  2076.  
  2077. function createCoordObject()
  2078.     currentFunction = "createCoordObject"
  2079.     --[[
  2080.     0 = go south (z increases)
  2081.     1 = go west  (x decreases)
  2082.     2 = go north (z decreases
  2083.     3 = go east  (x increases)
  2084.  
  2085.     compass[0] = "south"
  2086.     compass[1] = "west"
  2087.     compass[2] = "north"
  2088.     compass[3] = "east"]]--
  2089.    
  2090.     clsCoord = {} -- the table representing the class, which will double as the metatable for any instances
  2091.     clsCoord.__index = clsCoord -- failed table lookups on the instances should fallback to the class table, to get methods
  2092.  
  2093.     function clsCoord.new(coordName) --equivalent to VB class initialise
  2094.         local self = setmetatable({}, clsCoord)
  2095.         self.name = coordName
  2096.         self.x = 0
  2097.         self.y = 0
  2098.         self.z = 0
  2099.         self.facing = 0
  2100.         self.compass = "South"
  2101.        
  2102.         return self
  2103.     end
  2104.     function clsCoord.getX(self)
  2105.         return self.x
  2106.     end
  2107.     function clsCoord.setX(self, newVal) -- property let in VB
  2108.         self.x = newVal
  2109.     end
  2110.     function clsCoord.getY(self) -- property get in VB
  2111.         return self.y
  2112.     end
  2113.     function clsCoord.setY(self, newVal)
  2114.         self.y = newVal
  2115.     end
  2116.     function clsCoord.getZ(self)
  2117.         return self.z
  2118.     end
  2119.     function clsCoord.setZ(self, newVal)
  2120.         self.z = newVal
  2121.     end
  2122.     function clsCoord.getFacing(self)
  2123.         return self.facing
  2124.     end
  2125.     function clsCoord.setFacing(self, newVal) --property let in VB
  2126.         self.facing = newVal
  2127.         if self.facing < 0 then
  2128.                 self.facing = 3
  2129.         elseif self.facing > 3 then
  2130.                 self.facing = 0
  2131.         end
  2132.         if self.facing == 0 then
  2133.             self.compass = "South"
  2134.         elseif self.facing == 1 then
  2135.             self.compass = "West"
  2136.         elseif self.facing == 2 then
  2137.             self.compass = "North"
  2138.         else
  2139.             self.compass = "East"
  2140.         end
  2141.     end
  2142.     function clsCoord.getCompass(self)
  2143.         return self.compass
  2144.     end
  2145.     function clsCoord.setCompass(self, newVal) --property let in VB
  2146.         self.compass = newVal
  2147.  
  2148.         if self.compass == "South" then
  2149.             self.facing = 0
  2150.         elseif self.compass == "West" then
  2151.             self.facing = 1
  2152.         elseif self.compass == "North" then
  2153.             self.facing = 2
  2154.         elseif self.compass == "East" then
  2155.             self.facing = 3
  2156.         end
  2157.     end
  2158.     function clsCoord.goUp(blocks)
  2159.         blocks = blocks or 1
  2160.         self.y = self.y + blocks
  2161.     end
  2162.     function clsCoord.goDown(blocks)
  2163.         blocks = blocks or 1
  2164.         self.y = self.y - blocks
  2165.     end
  2166.     --[[uses:
  2167.         location:getX()             get current turtle x coordinate
  2168.         location:getY()             get current turtle y coordinate
  2169.         location:getZ()             get current turtle z coordinate
  2170.         location:setX(xCoord)       set current turtle x coordinate eg location:setX(-235)
  2171.         location:setY(yCoord)       set current turtle y coordinate eg location:setY(66)
  2172.         location:setZ(zCoord)       set current turtle z coordinate eg location:setZ(125)
  2173.         location:getFacing()        returns a number 0 - 3 representing direction of player
  2174.         location:setFacing(facing)  sets direction eg location:setFacing(1) (West)
  2175.         location:getCompass()       returns direction as text eg "north"
  2176.         location:setCompass("X")    sets direction using text eg location:setCompass("south")
  2177.         location:goUp(X)            increases Y coord by X
  2178.         location:goDown(X)          decreases Y coord by X
  2179.     ]]--
  2180.     location = clsCoord.new("currentLocation")
  2181.     coordHome = clsCoord.new("homeLocation")
  2182.     mineTopLevel = clsCoord.new("mineTopLevel")
  2183. end
  2184.  
  2185. function createDigObject()
  2186.     -- success, item, slotNo = dig.digNew{self = dig, direction = "forward", slotNo = 1, expectedItem = "wood", waitForGravel = true, checkForItems = "saplings", callFrom = "firstTree"}
  2187.     -- dig item in specified direction
  2188.     -- if item not known allocate "item1-8"
  2189.     -- if checkForItems ~= "" then check eg for saplings, coal etc
  2190.     -- if item known, leave in selected slot
  2191.     -- if max quantity reached, dump excess
  2192.     -- return success, dugItem, dugSlot eg true, "wood", 1
  2193.     clsDig = {}
  2194.     clsDig.__index = clsDig
  2195.  
  2196.     function clsDig.create(self) --creates new instance of dig object
  2197.         local self = setmetatable({}, clsDig)
  2198.        
  2199.         -- setup variables
  2200.         self.callFrom = ""
  2201.         self.checkForItems = ""
  2202.         self.cobblePlaced = false
  2203.         self.detect = false
  2204.         self.direction = ""
  2205.         self.doSort = false
  2206.         self.dugAmount = 0
  2207.         self.dugItem = ""
  2208.         self.dugSlot = 0
  2209.         self.dugSlot2 = 0
  2210.         self.emptySlot = 0
  2211.         self.expectedItem = ""
  2212.         self.flintSlot = 0
  2213.         self.isGravel = false
  2214.         self.itemKnown = false
  2215.         self.itemCount = {}
  2216.         self.newItemFound = false
  2217.         self.oldItem = ""
  2218.         self.slotNo = 1
  2219.         self.success = false
  2220.         self.updateType =""
  2221.         self.useItem = ""
  2222.         self.useSlot = 0
  2223.         self.waitForGravel = false
  2224.        
  2225.         self.itemCount = {}
  2226.         -- set up array of current quantities in each turtle slot
  2227.         for i = 1, 16 do
  2228.             self.itemCount[i] = turtle.getItemCount(i)
  2229.         end
  2230.        
  2231.         return self    
  2232.     end
  2233.    
  2234.     function clsDig.digNew(arg)
  2235.         -- reset variables
  2236.         local self = arg.self
  2237.        
  2238.         self.callFrom = ""
  2239.         self.checkForItems = ""
  2240.         self.cobblePlaced = false
  2241.         self.detect = false
  2242.         self.direction = "forward"
  2243.         self.doSort = false
  2244.         self.dugAmount = 0
  2245.         self.dugItem = ""
  2246.         self.dugSlot = 0
  2247.         self.dugSlot2 = 0
  2248.         self.emptySlot = 0
  2249.         self.expectedItem = ""
  2250.         self.flintSlot = 0
  2251.         self.isGravel = false
  2252.         self.itemCount = {}
  2253.         self.itemKnown = false
  2254.         self.newItemFound = false
  2255.         self.oldItem = ""
  2256.         self.slotNo = 1
  2257.         self.success = false
  2258.         self.updateType = ""
  2259.         self.useItem = ""
  2260.         self.useSlot = 0
  2261.         self.waitForGravel = false
  2262.        
  2263.         local logText = "dig.newDig: "
  2264.         local result = false
  2265.         --[[examples
  2266.             dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
  2267.             dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
  2268.             dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "firstTree"}
  2269.         ]]--
  2270.        
  2271.         -- set up array of current quantities in each turtle slot
  2272.         for i = 1, 16 do
  2273.             self.itemCount[i] = turtle.getItemCount(i)
  2274.         end
  2275.        
  2276.         self.direction = arg.direction or "forward"
  2277.         self.slotNo = arg.slotNo or 1
  2278.         self.expectedItem = arg.expectedItem or ""
  2279.         self.checkForItems = arg.checkForItems or ""
  2280.         self.callFrom = arg.callFrom or ""
  2281.         self.waitForGravel = arg.waitForGravel or false
  2282.         --remove any mobs
  2283.         clsDig.attack(self)
  2284.         --mobs disposed of, now get on with digging
  2285.         clsDig.doDig(self) -- will set self.success
  2286.         --dig successful if dug large quantity then 2 slots will be updated, original + next available
  2287.         if self.success then --process dig
  2288.             --[[saveToLog("dig.newDig: direction = "..self.direction..
  2289.                         " slotNo = "..self.slotNo..
  2290.                         " expectedItem = "..self.expectedItem..
  2291.                         " checkForItems = "..self.checkForItems..
  2292.                         " callFrom = "..self.callFrom, false, true)]]--
  2293.                        
  2294.             clsDig.setDugSlotAndItem(self) --sets self.dugSlot, self.dugSlot2, self.dugItem and self.dugAmount. itemX unless already identified
  2295.             if self.dugSlot > 0 then --item dug, could be into existing slot, empty slot, or specified slot
  2296.                 if clsDig.confirmGravel(self) == "flint" then -- item identified by confirmGravel only
  2297.                     turtle.select(self.dugSlot)
  2298.                     while turtle.drop() do
  2299.                         saveToLog("dig.digNew: flint in slot "..self.dugSlot.. " emptied out", false, true)
  2300.                     end
  2301.                     self.dugItem = ""
  2302.                     self.dugSlot = 0
  2303.                 end
  2304.                 if self.dugSlot > 0 then -- not flint. self.dugItem = itemX, or known item. expectedItem already set
  2305.                     -- self.updateType set in setDugSlotAndItem() and/or in checkForItem()
  2306.                     if string.find(self.dugItem, "item") ~= nil and self.checkForItems ~= "" then --check for item(s) when only 1 block available
  2307.                         clsDig.checkForItem(self) -- itemX changed to known item after checkForItem
  2308.                     end
  2309.                     -- item will be itemX or known item by now, slot.update needed
  2310.                     if self.updateType == "newItem" then
  2311.                         --saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
  2312.                         slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  2313.                     elseif self.updateType == "updateItem" then
  2314.                         --saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
  2315.                         slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  2316.                     elseif self.updateType == "changeItem" then
  2317.                         --saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." old item = "..self.oldItem.." tested - dugItem = "..self.dugItem, false, true)
  2318.                         slot.update{self = slot, slotNo = self.dugSlot, newItem = self.dugItem}
  2319.                     elseif self.updateType == "deleteItem" then
  2320.                         --saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." delete item = "..self.dugItem, false, true)
  2321.                         slot.update{self = slot, slotNo = self.dugSlot, delete = true}
  2322.                     end
  2323.                     --[[if clsDig.checkSlotQuantity(self) then
  2324.                         slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  2325.                     end]]--
  2326.                     if self.waitForGravel then
  2327.                         clsDig.waitForGravel(self)
  2328.                     end
  2329.                 end
  2330.                 if self.dugSlot > 0 then
  2331.                     result = true
  2332.                 end
  2333.             end
  2334.         else
  2335.             if self.detect then --dig not successful, if detected must be bedrock
  2336.                 saveToLog("dig.newDig: did not succeed after detect "..self.direction.." , bedrock reached", true)
  2337.                 self.dugItem = "bedrock"
  2338.                 self.dugSlot = 0
  2339.             else
  2340.                 --saveToLog("dig.newDig: no block detected, nothing dug", false, true)
  2341.             end
  2342.             result = false
  2343.         end
  2344.         if self.doSort then
  2345.             saveToLog("dig.newDig: sorting inventory after identifying new item", true)
  2346.             sortInventory(true)
  2347.         end
  2348.        
  2349.         --if not fso:getUseVerbose() then --not using verbose logfile, so print summary only
  2350.             if self.dugItem ~= "" then
  2351.                 -- saveToLog("dig = "..self.dugAmount.." of "..self.dugItem.." into slot "..self.dugSlot.." at x="..location:getX()..",y="..location:getY()..",z="..location:getZ(), true, false)
  2352.             end
  2353.         --end
  2354.         return result, self.dugItem, self.dugSlot
  2355.     end
  2356.    
  2357.     function clsDig.attack(self) --attack any nearby mobs
  2358.         local mobAttacked = false
  2359.         --first get rid of any mobs
  2360.         --saveToLog("dig.attack: attack started", false, true)
  2361.         turtle.select(1)
  2362.         if turtle.attack() then
  2363.             saveToLog("dig.attack: Mob attacked in front!", true, false)
  2364.             sleep(1.5)
  2365.             while turtle.attack() do --in case mob in front
  2366.                 saveToLog("dig.attack: Mob attacked in front again!", true, false)
  2367.                 sleep(1.5)
  2368.             end
  2369.             mobAttacked = true
  2370.         end
  2371.         if turtle.attackUp() then
  2372.             saveToLog("dig.attack: Mob attacked above!", true, false)
  2373.             sleep(1.5)
  2374.             while turtle.attackUp() do --in case mob in front
  2375.                 saveToLog("dig.attack: Mob attacked above again!", true, false)
  2376.                 sleep(1.5)
  2377.             end
  2378.             mobAttacked = true
  2379.         end
  2380.         if turtle.attackDown() then
  2381.             saveToLog("dig.attack: Mob attacked below!", true, false)
  2382.             sleep(1.5)
  2383.             while turtle.attackDown() do --in case mob in front
  2384.                 saveToLog("dig.attack: Mob attacked below again!", true, false)
  2385.                 sleep(1.5)
  2386.             end
  2387.             mobAttacked = true
  2388.         end
  2389.         if mobAttacked then --remove any mob drops
  2390.             for i = 1, 16 do
  2391.                 if self.itemCount[i] == 0 then --check only previously empty slots
  2392.                     if turtle.getItemCount(i) > 0 then
  2393.                         turtle.select(i)
  2394.                         while turtle.dropUp() do
  2395.                             saveToLog ("dig.attack: dumping mob drops", true, false)
  2396.                         end
  2397.                     end
  2398.                 end
  2399.             end
  2400.         end
  2401.     end
  2402.    
  2403.     function clsDig.doDig(self) -- check if next block is gravel, then carry out dig operation
  2404.         local lookForGravel = false
  2405.         local testDig = false
  2406.        
  2407.         self.success = false
  2408.         --[[examples
  2409.             dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
  2410.             dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
  2411.         ]]--
  2412.         --[[
  2413.         saveToLog("dig.doDig: started, direction = "..self.direction..
  2414.                  " expectedItem = "..self.expectedItem..
  2415.                  " slotNo = "..self.slotNo, false, true)]]--
  2416.                  
  2417.         turtle.select(self.slotNo)
  2418.         -- grass:   turtle.detect() = false,    turtle.dig() = true, nothing returned, occasional seeds
  2419.         -- leaves:  turtle.detect() = true,     turtle.dig() = true, nothing returned
  2420.         -- water:   turtle.detect() = false,    turtle.dig() = false, nothing returned cc >= 1.6
  2421.         -- lava:    turtle.detect() = false,    turtle.dig() = false, nothing returned cc >= 1.6
  2422.    
  2423.         --Compare next block with gravel (if already found)
  2424.         if slot:getItemSlot("gravel") > 0 then
  2425.             turtle.select(slot:getItemSlot("gravel"))
  2426.             lookForGravel = true
  2427.         end
  2428.         if self.direction == "up" then
  2429.             if turtle.detectUp() then
  2430.                 self.detect = true
  2431.                 if turtle.compareUp() and lookForGravel then --compare gravel with block above
  2432.                     self.isGravel = true
  2433.                 end
  2434.             end
  2435.             turtle.select(self.slotNo)
  2436.             if turtle.digUp() then
  2437.                 self.success = true
  2438.             end
  2439.         elseif self.direction == "down" then
  2440.             if turtle.detectDown() then
  2441.                 self.detect = true
  2442.                 if turtle.compareDown() and lookForGravel then
  2443.                     self.isGravel = true
  2444.                 end
  2445.             end
  2446.             turtle.select(self.slotNo)
  2447.             if turtle.digDown() then
  2448.                 self.success = true
  2449.             end
  2450.         else
  2451.             if turtle.detect() then
  2452.                 self.detect = true
  2453.                 if turtle.compare() and lookForGravel then
  2454.                     self.isGravel = true
  2455.                 end
  2456.             end
  2457.             turtle.select(self.slotNo)
  2458.             if turtle.dig() then
  2459.                 self.success = true
  2460.             end
  2461.         end
  2462.         if not self.detect then
  2463.             --saveToLog("dig.doDig: no block detected "..self.direction, false, true)
  2464.         end
  2465.         --[[if self.isGravel then
  2466.             saveToLog("dig.doDig: gravel found on current dig, will give flint or gravel", false, true)
  2467.         end]]--
  2468.        
  2469.         return self.success     --turtle.digX() succeeded but may not have increased items onboard.
  2470.                         --if isGravel then new block is either gravel or flint. success = false: bedrock
  2471.     end
  2472.    
  2473.     function clsDig.setDugSlotAndItem(self) -- set self.dugSlot (0 = no dig) self.dugItem ("" = no dig)
  2474.         local success = false
  2475.         local addQualifier = ""
  2476.         local itemTitle = ""
  2477.        
  2478.         self.dugSlot = 0
  2479.         self.dugItem = ""
  2480.         self.dugAmount = 0
  2481.         self.dugSlot2 = 0
  2482.         self.updateType = ""
  2483.        
  2484.         for i = 1, 16 do --find slot item dug into (if any) by comparing array set up in digNew()
  2485.             if turtle.getItemCount(i) > self.itemCount[i] then --check if dugItem in slot(s)
  2486.                 if self.dugSlot == 0 then -- default value
  2487.                     self.dugSlot = i --first slot with additional items in it
  2488.                     self.dugAmount = turtle.getItemCount(i) - self.itemCount[i] -- amount dug
  2489.                 else
  2490.                     self.dugSlot2 = i --another slot with additional items. eg 58 redstone in slot, 4 more dug, fills original slot
  2491.                 end
  2492.             end
  2493.         end
  2494.         --dugSlot set above
  2495.         if self.dugSlot > 0 then --item dug.
  2496.             success = true
  2497.             if self.slotNo > 1 or self.expectedItem ~= "" then --dug into specific slot, so could match existing item, or placed and re-dug
  2498.                 -- check if out of place
  2499.                 turtle.select(self.dugSlot)
  2500.                 for i = 1, 16 do
  2501.                     if i ~= self.dugSlot then
  2502.                         if turtle.compareTo(i) then --matches existing item
  2503.                             self.dugItem = slot:getSlotContains(i)
  2504.                             turtle.transferTo(i)
  2505.                             self.dugSlot = i
  2506.                             self.itemKnown = true
  2507.                             self.updateType = "updateItem"
  2508.                             break
  2509.                         end
  2510.                     end
  2511.                 end
  2512.                 if self.updateType ~= "updateItem" then -- not out of place
  2513.                     if self.expectedItem ~= "" then --allocate dugItem to expectedItem
  2514.                         self.dugItem = self.expectedItem
  2515.                         saveToLog("dig.checkExpectedItem: expected item '"..self.expectedItem.."' dug into slot "..self.dugSlot, false, true)
  2516.                         self.itemKnown = true
  2517.                         self.updateType = "updateItem"
  2518.                     else -- ? placed and re-dug
  2519.                         if slot:getSlotContains(self.slotNo) == "" then
  2520.                             self.itemKnown = false
  2521.                             self.updateType = "newItem"
  2522.                             self.dugItem = slot:getFreeUnknownItem()
  2523.                         else
  2524.                             self.itemKnown = true
  2525.                             self.updateType = "updateItem"
  2526.                         end
  2527.                     end
  2528.                 end
  2529.             else -- set to dig into slot 1 and self.expectedItem = ""
  2530.                 if self.dugAmount > 2 then
  2531.                     --can only be melon, clay, lapis, redstone as more than 1 mined from one block
  2532.                     if location:getY() > 35 then --must be clay or melon
  2533.                         turtle.select(self.dugSlot)
  2534.                         while turtle.dropUp() do
  2535.                             saveToLog("dig.setDugSlotAndItem:: clay or melon dug: dumping..", false, true)
  2536.                         end
  2537.                         self.dugItem = ""
  2538.                         self.dugSlot = 0
  2539.                         success = false
  2540.                     else
  2541.                         if slot:getSlotContains(self.dugSlot) == "" then
  2542.                             if slot:getItemSlot("redstone") > 0 then --redstone already found
  2543.                                 self.dugItem = "lapis"
  2544.                                 success = true
  2545.                                 self.itemKnown = true
  2546.                                 self.updateType = "newItem"
  2547.                                 saveToLog("dig.setDugSlotAndItem: lapis identified as redstone already known", false, true)
  2548.                             else --no redstone found so far
  2549.                                 self.dugItem = slot:getFreeUnknownItem()
  2550.                                 self.itemKnown = false
  2551.                                 self.updateType = "newItem"
  2552.                             end
  2553.                         else
  2554.                             self.dugItem = slot:getSlotContains(self.dugSlot)
  2555.                             newItemFound = false
  2556.                             self.updateType = "updateItem"
  2557.                             self.itemKnown = true
  2558.                         end
  2559.                     end
  2560.                 else
  2561.                     if self.dugSlot2 > 0 then -- dug items has filled original slot, then moved into next available, or redstone, lapis, coal overfilled slot
  2562.                         -- dugAmount = 1 or 63 in slot >- 64 + excess into another slot
  2563.                         saveToLog("dig.setDugSlotAndItem: second slot used "..self.dugSlot2, false, true)
  2564.                         turtle.select(self.dugSlot2)
  2565.                         for i = 1, 16 do
  2566.                             if i ~= self.dugSlot2 then
  2567.                                 if turtle.compareTo(i) then --compares to existing item
  2568.                                     saveToLog("dig.setDugSlotAndItem: second slot compares to slot "..i, false, true)
  2569.                                     if string.find(slot:getSlotContains(i), "-") == nil then -- eg "cobble"
  2570.                                         itemTitle = slot:getSlotContains(i)
  2571.                                     else
  2572.                                         itemTitle = string.sub(slot:getSlotContains(i), 1, string.find(slot:getSlotContains(i), "-") - 1)
  2573.                                     end
  2574.                                     for j = 1, 6 do
  2575.                                         if slot:getItemSlot(itemTitle.."-"..j) == 0 then
  2576.                                             addQualifier = tostring("-"..j)
  2577.                                             break
  2578.                                         end
  2579.                                     end
  2580.                                     self.dugItem = itemTitle..addQualifier
  2581.                                     self.itemKnown = true
  2582.                                     self.updateType = "newItem"
  2583.                                     self.dugSlot = self.dugSlot2
  2584.                                     self.dugSlot2 = 0
  2585.                                     saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " contains "..self.dugItem, true)
  2586.                                     break
  2587.                                 end
  2588.                             end
  2589.                         end
  2590.                     else -- not dug into more than 1 slot, but could be 64 in one slot, new item discovered
  2591.                         if slot:getSlotContains(self.dugSlot) == "" then -- not known. compare to other slots before allocating new item
  2592.                             turtle.select(self.dugSlot)
  2593.                             for i = 1, 16 do
  2594.                                 if i ~= self.dugSlot then
  2595.                                     if turtle.compareTo(i) then --compare if excess material just dug
  2596.                                         saveToLog("dig.setDugSlotAndItem: self.dugSlot compares to slot "..i, false, true)
  2597.                                         if string.find(slot:getSlotContains(i), "-") == nil then -- eg "cobble"
  2598.                                             itemTitle = slot:getSlotContains(i)
  2599.                                         else
  2600.                                             itemTitle = string.sub(slot:getSlotContains(i), 1, string.find(slot:getSlotContains(i), "-") - 1)
  2601.                                         end
  2602.                                         for j = 1, 6 do
  2603.                                             if slot:getItemSlot(itemTitle.."-"..j) == 0 then
  2604.                                                 addQualifier = tostring("-"..j)
  2605.                                                 break
  2606.                                             end
  2607.                                         end
  2608.                                         self.dugItem = itemTitle..addQualifier
  2609.                                         self.itemKnown = true
  2610.                                         self.updateType = "newItem"
  2611.                                         saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " contains "..self.dugItem, true)
  2612.                                         break
  2613.                                     end
  2614.                                 end
  2615.                             end
  2616.                             if self.dugItem == "" then
  2617.                                 self.dugItem = slot:getFreeUnknownItem()
  2618.                                 self.itemKnown = false
  2619.                                 self.updateType = "newItem"
  2620.                             end
  2621.                         else
  2622.                             self.dugItem = slot:getSlotContains(self.dugSlot)
  2623.                             --saveToLog("dig.setDugSlotAndItem: existing dugItem = "..self.dugItem, false)
  2624.                             newItemFound = false
  2625.                             self.updateType = "updateItem"
  2626.                             if string.find(self.dugItem, "item") ~= nil then -- itemX found
  2627.                                 self.itemKnown = false
  2628.                             else
  2629.                                 self.itemKnown = true
  2630.                             end
  2631.                         end
  2632.                     end
  2633.                     success = true -- item dug
  2634.                 end
  2635.             end
  2636.         else
  2637.             self.dugItem = "leaves"
  2638.         end
  2639.  
  2640.         -- saveToLog ("dig.setDugSlotAndItem: dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
  2641.        
  2642.         return success
  2643.         -- self.dugSlot, self.dugSlot2, self.dugAmount and self.dugItem have been set in this routine
  2644.         -- if self.dugSlot = 0, self.dugItem will only be set if = "water" or "leaves"
  2645.         -- self.updateType will be newItem or updateItem
  2646.     end
  2647.            
  2648.     function clsDig.confirmGravel(self) -- self.isGravel = true, either flint or gravel dug
  2649.         local item = ""
  2650.        
  2651.         if self.isGravel then --block just dug confirmed as gravel by comparison
  2652.             if self.dugSlot ~= slot:getItemSlot("gravel") then --must be flint or gravel is full
  2653.                 turtle.select(self.dugSlot)
  2654.                 if turtle.compareTo(slot:getItemSlot("gravel")) then
  2655.                     if slot:getItemSlot("gravel-1") > 0 then
  2656.                         self.dugItem = "gravel-2"
  2657.                         self.itemKnown = true
  2658.                         item = "gravel-2"
  2659.                     else
  2660.                         self.dugItem = "gravel-1"
  2661.                         self.itemKnown = true
  2662.                         item = "gravel-1"
  2663.                     end
  2664.                     saveToLog("dig.confirmGravel: "..item.." found", false, true)
  2665.                 else
  2666.                     saveToLog("dig.confirmGravel: flint dug when gravel detected", false, true)
  2667.                     self.dugItem = "flint"
  2668.                     item = "flint"
  2669.                 end
  2670.             else
  2671.                 self.dugItem = "gravel"
  2672.                 self.itemKnown = true
  2673.                 item = "gravel"
  2674.             end
  2675.         end
  2676.        
  2677.         return item
  2678.     end
  2679.        
  2680.     function clsDig.checkSandGravel(self, useSlot, useItem)
  2681.         local result = ""
  2682.         local runTest = true
  2683.         local flintSlot = 0
  2684.         local dirtPlaced = false
  2685.         local numTries = 0
  2686.         local startLevel = location:getY()
  2687.         local itemCount = {}
  2688.        
  2689.         if turtle.detectDown() then --probably in mine going down. block found below
  2690.             turtle.select(1)
  2691.             for i = 1, 2 do --move up 2 places
  2692.                 while turtle.detectUp() do
  2693.                     turtle.digUp()
  2694.                     sleep(0.5)
  2695.                 end
  2696.                 up(1)
  2697.             end
  2698.         else --no block below. probably going up. plug with dirt
  2699.             saveToLog("clsDig.checkSandGravel: checking for gravel placing dirt plug down", false)
  2700.             turtle.select(slot:getItemSlot("dirt"))
  2701.             turtle.placeDown()
  2702.             dirtPlaced = true
  2703.             turtle.select(1)
  2704.             for i = 1, 2 do  --move up 2 places
  2705.                 while turtle.detectUp() do
  2706.                     saveToLog("clsDig.checkSandGravel: digging block above", false)
  2707.                     turtle.digUp()
  2708.                     sleep(0.5)
  2709.                 end
  2710.                 up(1)
  2711.             end
  2712.         end --now clear below 2 blocks, could have added more flint/gravel to turtle while clearing above
  2713.         turtle.select(useSlot)
  2714.         if turtle.placeDown() then --solid block or sand/ gravel
  2715.             sleep(0.5)
  2716.             if turtle.detectDown() then --not dropped into pit so not sand/gravel
  2717.                 turtle.digDown()
  2718.                 --down(2, 1)
  2719.                 if slot:getItemSlot("cobble") == 0 then
  2720.                     saveToLog("clsDig.checkSandGravel: first cobble found", false)
  2721.                     result = "cobble"
  2722.                 else
  2723.                     result = useItem
  2724.                 end
  2725.             else
  2726.                 saveToLog("clsDig.checkSandGravel: gravel or sand dropped, moving down", false)
  2727.                 while not turtle.detectDown() do
  2728.                     down(1)
  2729.                 end
  2730.                 if slot:getItemSlot("gravel") > 0 then --gravel found already
  2731.                     saveToLog("checkSandGravel: checking for gravel/sand - found sand in slot "..useSlot, false)
  2732.                     result = "sand"
  2733.                     runTest = false
  2734.                     turtle.digDown()
  2735.                 elseif slot:getItemSlot("sand") > 0 then --sand found already
  2736.                     saveToLog("checkSandGravel: checking for gravel/sand - found gravel in slot "..useSlot, false)
  2737.                     result = "gravel"
  2738.                 end
  2739.                 if runTest then -- only test to confirm gravel
  2740.                     if slot:getItemSlot("sand") > 0 then --sand found already
  2741.                         saveToLog("clsDig.checkSandGravel: sand already identified. Gravel dropped, digging up to 100x to check for flint", false)
  2742.                     else
  2743.                         saveToLog("clsDig.checkSandGravel: sand / gravel dropped, digging up to 100x to check for flint", false)
  2744.                     end
  2745.                     for i = 1, 16 do
  2746.                         itemCount[i] = turtle.getItemCount(i)
  2747.                     end
  2748.                     for i = 1, 100 do
  2749.                         turtle.select(1) --if gravel or flint dug, goes into correct slot
  2750.                         if turtle.digDown() then
  2751.                             numTries = numTries + 1
  2752.                             if turtle.getItemCount(useSlot) == itemCount[useSlot] then --flint found as ? gravel slot did not increase
  2753.                                 for k = 1, 16 do
  2754.                                     if turtle.getItemCount(k) > itemCount[k] then --check if dugItem in slot(s)
  2755.                                         flintSlot = k -- could be flint in an empty slot or add to existing item
  2756.                                         break
  2757.                                     end
  2758.                                 end
  2759.                                 saveToLog("clsDig.checkSandGravel: useSlot not used flint found in slot "..flintSlot, false)
  2760.                                 --delete flint after updating gravel slot, else itemX order will change
  2761.                                 if flintSlot > 0 then
  2762.                                     result = "gravel"
  2763.                                     break
  2764.                                 end
  2765.                             else
  2766.                                 turtle.select(useSlot)
  2767.                                 turtle.placeDown()
  2768.                             end
  2769.                         end
  2770.                     end
  2771.                    
  2772.                     if flintSlot == 0 then --sand found
  2773.                         saveToLog("clsDig.checkSandGravel: checking for gravel - sand found in slot "..useSlot, false)
  2774.                         result = "sand"
  2775.                     else
  2776.                         saveToLog("clsDig.checkSandGravel: gravel check took "..numTries.." attempts", false)
  2777.                     end
  2778.                 end
  2779.                 down(1, 1)
  2780.                 if flintSlot > 0 then --flint found, so dump it
  2781.                     turtle.select(flintSlot)
  2782.                     while turtle.dropUp() do
  2783.                         saveToLog("clsDig.checkSandGravel: dumping flint found in slot "..flintSlot, false)
  2784.                     end
  2785.                     slot.update{self = slot, slotNo = flintSlot, delete = true}
  2786.                 end
  2787.             end
  2788.         else --did not placeDown() = coal, flint
  2789.             turtle.select(useSlot)
  2790.             if turtle.refuel(0) then --coal found
  2791.                 saveToLog("clsDig.checkSandGravel: coal found in slot "..useSlot, false)
  2792.                 result = "coal"
  2793.             end
  2794.             down(2, 1)
  2795.         end
  2796.         if dirtPlaced then
  2797.             turtle.select(1)
  2798.             turtle.digDown()
  2799.         end
  2800.         while location:getY() < startLevel do
  2801.             up(1)
  2802.         end
  2803.        
  2804.         return result
  2805.     end
  2806.  
  2807.     function clsDig.checkForItem(self)
  2808.         local useItem = ""
  2809.         local slotCount = 0
  2810.         local itemCompare = false
  2811.         local useSlot = 0
  2812.         local itemStatus = ""
  2813.         local result = false
  2814.         local flintSlot = 0
  2815.         local usedCobble = false
  2816.  
  2817.         -- saveToLog("dig.checkForItem: checkForItems = "..self.checkForItems.." call from = "..self.callFrom, false)
  2818.         -- will only get here if item dug but not identified self.dugItem = "itemX"
  2819.         -- see if any instructions to test for new items, when first identified as itemX
  2820.         if self.dugAmount > 2 then --multiple items from dig
  2821.             --can only be melon, clay, lapis, redstone as more than 1 mined from one block
  2822.             if location:getY() > 35 then --must be clay or melon
  2823.                 turtle.select(self.dugSlot)
  2824.                 while turtle.dropUp() do
  2825.                     saveToLog("dig.checkForItem: clay or melon dug: dumping..", false, true)
  2826.                 end
  2827.                 self.dugItem = ""
  2828.                 self.dugSlot = 0
  2829.                 itemStatus = "deleteItem"
  2830.             else --below level 35 must be lapis or redstone
  2831.                 saveToLog("dig.checkForItem: checking for redstone or lapis", true, false)
  2832.                 if slot:getItemSlot("redstone") > 0 then
  2833.                     self.dugItem = "lapis"
  2834.                     itemStatus = "newItem"
  2835.                     saveToLog("dig.checkForItem: lapis identified as redstone already known", false, true)
  2836.                 else
  2837.                     if not turtle.detect() then -- space in front
  2838.                         while not turtle.forward() do
  2839.                             attack()
  2840.                         end
  2841.                         --must have moved forward now
  2842.                         if not turtle.detectDown() then
  2843.                             turtle.select(slot:getItemSlot("cobble"))
  2844.                             while not turtle.placeDown() do
  2845.                                 attack()
  2846.                             end
  2847.                             usedCobble = true
  2848.                             saveToLog("dig.checkForItem: placing cobble down", true, false)
  2849.                         end
  2850.                         turtle.turnRight()
  2851.                         turtle.turnRight()
  2852.                         while not turtle.forward() do
  2853.                             attack()
  2854.                         end
  2855.                         turtle.turnRight()
  2856.                         turtle.turnRight()
  2857.                         turtle.select(self.dugSlot)
  2858.                         if turtle.place() then --redstone
  2859.                             if not turtle.compare() then
  2860.                                 saveToLog("dig.checkForItem: dugAmount = "..self.dugAmount.." item in slot "..self.dugSlot.." placed but not compared - redstone", false)
  2861.                                 self.dugItem = "redstone"
  2862.                                 turtle.dig()
  2863.                                 itemStatus = "newItem"
  2864.                             end
  2865.                         else --lapis (dug > 1, no place)
  2866.                             self.dugItem = "lapis"
  2867.                             itemStatus = "newItem"
  2868.                         end
  2869.                         --[[if usedCobble then
  2870.                             if turtle.forward() then
  2871.                                 turtle.select(slot:getItemSlot("cobble"))
  2872.                                 turtle.digDown()
  2873.                                 turtle.back()
  2874.                                 saveToLog("dig.checkForItem: retrieving cobble ", true, false)
  2875.                             end
  2876.                         end]]--
  2877.                     end
  2878.                 end
  2879.             end
  2880.         else -- single item from dig
  2881.             -- check for coal, flint, saplings, flowers, mushrooms if only one item in slot
  2882.             if turtle.getItemCount(self.dugSlot) == 1 then --first time itemX dug
  2883.                 turtle.select(self.dugSlot)
  2884.                 if turtle.refuel(0) then --saplings, saplings2, wood2, coal
  2885.                     itemStatus = "newItem"
  2886.                     saveToLog("dig.checkForItem: new item dug - can refuel", false, true)
  2887.                     if string.find("findCobble, buildStaircase, completeMineshaft", self.callFrom) ~= nil or string.find(self.callFrom, "mine") ~= nil then
  2888.                         if slot:getItemSlot("coal") == 0 then
  2889.                             self.dugItem = "coal"
  2890.                             saveToLog("dig.checkForItem: coal found", false, true)
  2891.                         else -- if coal already known must be wood product found in a disused mine
  2892.                             saveToLog("dig.checkForItem: wooden item found underground - using for fuel", false, true)
  2893.                             turtle.refuel() -- use for fuel
  2894.                             self.dugItem = ""
  2895.                             self.dugSlot = 0
  2896.                             itemStatus = "deleteItem"
  2897.                         end
  2898.                     else -- not coal so destroy
  2899.                         turtle.refuel(0)
  2900.                         saveToLog("dig.checkForItem: saplings or wood2 found and used for fuel", false)
  2901.                         self.dugSlot = 0
  2902.                         self.dugItem = ""
  2903.                         itemStatus = "deleteItem"
  2904.                     end
  2905.                 else --did not refuel, check for flint, flowers, seeds, mushrooms, sugar cane, diamonds
  2906.                     saveToLog("dig.checkForItem: new item - cannot refuel", false, true)
  2907.                     useItem = self.dugItem
  2908.                     if self.callFrom == "go" then
  2909.                         if turtle.placeUp() then -- not dirt, cobble, could be ironore, sand, gravel
  2910.                             turtle.digUp() --put back into dugSlot. If gravel dug, could turn to flint, leave for time being
  2911.                         else
  2912.                             if location:getY() > 51 then
  2913.                                 while turtle.dropUp() do
  2914.                                     saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
  2915.                                 end
  2916.                                 self.dugSlot = 0
  2917.                                 self.dugItem = ""
  2918.                                 itemStatus = "deleteItem"
  2919.                             else
  2920.                                 itemStatus = "newItem"
  2921.                             end
  2922.                         end
  2923.                     elseif string.find("findCobble, buildStaircase, completeMineshaft", self.callFrom) ~= nil or string.find(self.callFrom, "mine") ~= nil then --look for diamonds
  2924.                         saveToLog("dig.checkForItem: new item cannot be identified in slot "..self.dugSlot, false)
  2925.                         itemStatus = "newItem"
  2926.                     else --called from any other function
  2927.                          --test if can be placed and re-dug. saplings already tested
  2928.                         turtle.select(1)
  2929.                         while turtle.detectUp() do
  2930.                             turtle.digUp()
  2931.                             sleep(0.5)
  2932.                         end
  2933.                         turtle.select(self.dugSlot)
  2934.                         if turtle.placeUp() then -- not dirt, cobble, could be ironore, sand, gravel
  2935.                             turtle.digUp() --put back into dugSlot. If gravel dug, could turn to flint, leave for time being
  2936.                         else
  2937.                             if location:getY() > 51 then
  2938.                                 while turtle.dropUp() do
  2939.                                     saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
  2940.                                 end
  2941.                                 self.dugSlot = 0
  2942.                                 self.dugItem = ""
  2943.                                 itemStatus = "deleteItem"
  2944.                             else
  2945.                                 itemStatus = "newItem"
  2946.                             end
  2947.                         end
  2948.                     end
  2949.                 end
  2950.             elseif turtle.getItemCount(self.dugSlot) == 2 then -- 2 in slot
  2951.                 saveToLog("dig.checkDugItem: checking slot with 2 items of "..self.dugItem, false, true)
  2952.                 if string.find(self.dugItem, "item") ~= nil then --only checks item1, item2 etc. coal, gravel etc if already found do not get here
  2953.                     if self.dugAmount > 2 and slot:getItemSlot("redstone") > 0 and slot:getItemSlot("coal") > 0 then
  2954.                         self.oldItem = self.dugItem
  2955.                         self.dugItem = "lapis"
  2956.                         itemStatus = "newItem"
  2957.                     else
  2958.                         --check for sand, gravel, needs min 2 blocks
  2959.                         turtle.select(self.dugSlot)
  2960.                         if self.callFrom == "findCobble" then --cobble not yet found, coal and flint tested, could be cobble, sand gravel
  2961.                             result = clsDig.checkSandGravel(self, self.dugSlot, self.dugItem)
  2962.                             self.oldItem = self.dugItem
  2963.                             self.dugItem = result
  2964.                             itemStatus = "newItem"
  2965.                         else
  2966.                             if not slot:getItemTested(self.dugSlot, "gravel") then -- check if new item is gravel, sand, coal or flint
  2967.                                 if string.find(self.checkForItems, "gravel") ~= nil or string.find(self.checkForItems, "sand") ~= nil then
  2968.                                     result = clsDig.checkSandGravel(self, self.dugSlot, self.dugItem)
  2969.                                     if result == "sand" or result == "gravel" then
  2970.                                         self.oldItem = self.dugItem
  2971.                                         self.dugItem = result
  2972.                                         itemStatus = "newItem"
  2973.                                     elseif result == "flint" then
  2974.                                         while turtle.dropUp() do
  2975.                                             saveToLog("dig.checkDugItem: dropping ? flint from slot "..self.dugSlot, false, true)
  2976.                                         end
  2977.                                         slot.update{self = slot, slotNo = self.dugSlot, delete = true}
  2978.                                         self.dugItem = ""
  2979.                                         self.dugSlot = 0
  2980.                                     elseif result == self.dugItem then --not gravel or sand
  2981.                                         slot:setItemTested(self.dugSlot, "gravel")
  2982.                                         slot:setItemTested(self.dugSlot, "sand")
  2983.                                         slot:setItemTested(self.dugSlot, "flint")
  2984.                                     else --could be ironore
  2985.                                         if slot:getItemSlot("ironore") == 0 and string.find("goMining1,goMining2,goMining3", self.callFrom) ~= nil then
  2986.                                             saveToLog("dig.checkDugItem: assuming ?ironore found",true)
  2987.                                             self.oldItem = self.dugItem
  2988.                                             self.dugItem = "?ironore"
  2989.                                             self.doSort = true
  2990.                                             itemStatus = "newItem"
  2991.                                         end
  2992.                                     end
  2993.                                 end
  2994.                             end
  2995.                         end
  2996.                     end
  2997.                 end
  2998.                 if self.dugItem ~= "" then --tested items may have been dumped. will now be itemX or named item
  2999.                     if self.oldItem == "" then --new item found
  3000.                         saveToLog("dig.checkDugItem: final confirmation of new item found", false, true)
  3001.                         --slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  3002.                     else --itemX tested - new item confirmed
  3003.                         saveToLog("dig.checkDugItem: changing item in "..self.dugSlot.." ("..self.oldItem..") after testing: dugItem = "..self.dugItem, false, true)
  3004.                         itemStatus = "changeItem"
  3005.                         self.doSort = true
  3006.                     end
  3007.                 end
  3008.             end
  3009.         end
  3010.        
  3011.         return itemStatus
  3012.     end
  3013.    
  3014.     function clsDig.checkSlotQuantity(self)
  3015.         local success = false
  3016.         --Check quantity and dump excess
  3017.         --redstone drops 4-5, lapis 4-8
  3018.         if self.dugSlot > 0 then
  3019.             if string.find(self.dugItem, "item") ~= nil or self.dugItem == "redstone" or self.dugItem == "lapis" then --unknown, lapis, redstone
  3020.                 if turtle.getItemCount(self.dugSlot) >= 54 then
  3021.                     saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." dumped from "..self.dugSlot, false, true)
  3022.                     turtle.select(self.dugSlot)
  3023.                     turtle.dropDown(turtle.getItemCount(self.dugSlot) - 54)
  3024.                     success = true
  3025.                 end
  3026.             elseif self.dugItem == "wood" or  self.dugItem == "wood2" then
  3027.                 if turtle.getItemCount(self.dugSlot) >= 60 then
  3028.                     saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." used for fuel", false, true)
  3029.                     turtle.select(self.dugSlot)
  3030.                     turtle.refuel(1)
  3031.                     success = true
  3032.                 end
  3033.             else --all other known items
  3034.                 if turtle.getItemCount(self.dugSlot) >= 62 then
  3035.                     saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." dumped from "..self.dugSlot, false, true)
  3036.                     turtle.select(self.dugSlot)
  3037.                     turtle.dropDown(1)
  3038.                     success = true
  3039.                 end
  3040.                 if self.dugItem == "sand" and turtle.getItemCount(self.dugSlot) >= 7 then
  3041.                     if turtle.getItemCount(self.dugSlot) > 62 then
  3042.                         turtle.select(self.dugSlot)
  3043.                         turtle.dropDown(turtle.getItemCount(self.dugSlot) - 54)
  3044.                         success = true
  3045.                     end
  3046.                 end
  3047.                 if self.dugItem == "sugar cane" and turtle.getItemCount(self.dugSlot) >= 4 then
  3048.  
  3049.                 end
  3050.             end
  3051.         end
  3052.        
  3053.         return success
  3054.     end
  3055.    
  3056.     function clsDig.waitForGravel(self)
  3057.         local useItem = ""
  3058.         local useSlot = 0
  3059.        
  3060.         for i = 1, 16 do
  3061.             self.itemCount[i] = turtle.getItemCount(i)
  3062.         end
  3063.        
  3064.         turtle.select(1)
  3065.         --sleep(0.5)
  3066.         if self.direction == "up" then
  3067.             while turtle.detectUp() do
  3068.                 turtle.digUp()
  3069.                 sleep(0.5)
  3070.             end
  3071.         elseif self.direction == "forward" then
  3072.             while turtle.detect() do
  3073.                 turtle.dig()
  3074.                 sleep(0.5)
  3075.             end
  3076.         end
  3077.         --now check if gravel/sand has fallen
  3078.         for i = 1, 16 do
  3079.             if  turtle.getItemCount(i) > self.itemCount[i] then --check if dugItem in slot
  3080.                 useSlot = i
  3081.                 break
  3082.             end
  3083.         end
  3084.         if useSlot > 0 then --sand/gravel fallen
  3085.             useItem = slot:getSlotContains(useSlot) --could be "", "itemX", "sand", "gravel"
  3086.             if string.find(useItem, "item") ~= nil or useItem == "" then --unknown must be sand or gravel
  3087.                 if self.itemCount[useSlot] > 1 then -- can be tested for sand/gravel
  3088.                     useItem = clsDig.checkSandGravel(self, useSlot, useItem)
  3089.                     if useItem ~= "" then
  3090.                         saveToLog("dig.waitForGravel: "..useItem.." found in slot "..useSlot, false, true)
  3091.                     end
  3092.                 else -- store as "itemx"
  3093.                     useItem = slot:getFreeUnknownItem()
  3094.                     saveToLog ("dig.waitForGravel: sand/gravel found. given id = "..useItem.." in slot no = "..self.useSlot, false, true)
  3095.                     slot.update{self = slot, slotNo = self.useSlot, item = useItem}
  3096.                 end
  3097.             end
  3098.         end
  3099.     end
  3100.    
  3101.     function clsDig.getDugSlot(self)
  3102.         return self.dugSlot
  3103.     end
  3104.    
  3105.     function clsDig.getDugItem(self)
  3106.         return self.dugItem
  3107.     end
  3108.    
  3109.     function clsDig.getDugAmount(self)
  3110.         return self.dugAmount
  3111.     end
  3112.    
  3113.     function clsDig.getSuccess(self)
  3114.         return self.success
  3115.     end
  3116.     --create initial dig object
  3117.     dig = clsDig.create()
  3118.     -- use in future:
  3119.     -- dig.digNew{self = dig, slotNo = 1, direction = "down", checkForItems = "sand", callFrom = "checkSugarCane"}
  3120. end
  3121.  
  3122. function createLogfileObject()
  3123.     clsLogfile = {} -- the table representing the class, which will double as the metatable for any instances
  3124.     clsLogfile.__index = clsLogfile -- failed table lookups on the instances should fallback to the class table, to get methods
  3125.  
  3126.     --[[
  3127.     Store details in "superminer.ini" on the local computer:
  3128.     onServer=true/false
  3129.     usePastebin=true/false
  3130.     useLogfiles=true/false
  3131.     ]]--
  3132.     function clsLogfile.new() --equivalent to VB class initialise
  3133.         local self = setmetatable({}, clsLogfile)
  3134.         local iniText = ""
  3135.         local iniFile = ""
  3136.        
  3137.         self.size = 0
  3138.         self.useLog = false
  3139.         self.numLogFiles = 22 -- change if more logfiles needed
  3140.         self.logFiles = {}
  3141.         self.logFileExists = {}
  3142.         self.boolIniFileExists = false
  3143.         self.boolRecoveryFileExists = false
  3144.         self.useVerbose = false
  3145.         self.onServer = false
  3146.         self.usePastebin = false
  3147.         self.useRecoveryFile = false
  3148.        
  3149.         self.logFiles[1] = "logGetSupplies.txt"
  3150.         self.logFiles[2] = "logGetCoords.txt"
  3151.         self.logFiles[3] = "logGetReady.txt"
  3152.         self.logFiles[4] = "logFindCobble.txt"
  3153.         self.logFiles[5] = "logClearBase.txt"
  3154.         self.logFiles[6] = "logCompleteMineshaft.txt"
  3155.         self.logFiles[7] = "logCreateMine51.txt"
  3156.         self.logFiles[8] = "logCreateMine48.txt"
  3157.         self.logFiles[9] = "logCreateMine45.txt"
  3158.         self.logFiles[10] = "logCreateMine42.txt"
  3159.         self.logFiles[11] = "logCreateMine39.txt"
  3160.         self.logFiles[12] = "logCreateMine36.txt"
  3161.         self.logFiles[13] = "logCreateMine33.txt"
  3162.         self.logFiles[14] = "logCreateMine30.txt"
  3163.         self.logFiles[15] = "logCreateMine27.txt"
  3164.         self.logFiles[16] = "logCreateMine24.txt"
  3165.         self.logFiles[17] = "logCreateMine21.txt"
  3166.         self.logFiles[18] = "logCreateMine18.txt"
  3167.         self.logFiles[19] = "logCreateMine15.txt"
  3168.         self.logFiles[20] = "logCreateMine12.txt"
  3169.         self.logFiles[21] = "logCreateMine9.txt"
  3170.         self.logFiles[22] = "logCreateMine6.txt"
  3171.        
  3172.         for i = 1, self.numLogFiles do
  3173.             self.logFileExists[i] = false
  3174.         end
  3175.        
  3176.         self.currentFileName = ""
  3177.         self.currentFileNameIndex = 0
  3178.         self.startFileNameIndex = 1
  3179.        
  3180.         for i = 1, self.numLogFiles do
  3181.             if fs.exists(self.logFiles[i]) then
  3182.                 self.logFileExists[i] = true
  3183.             end
  3184.         end
  3185.         if fs.exists("superminer.ini") and fs.getSize("superminer.ini") > 0 then
  3186.             self.boolIniFileExists = true
  3187.             iniFile = fs.open("superminer.ini", "r")
  3188.             iniText = iniFile.readLine()
  3189.             self.onServer = string.sub(iniText, string.find(iniText, "onServer=") + 9)
  3190.             iniText = iniFile.readLine()
  3191.             if string.sub(iniText, string.find(iniText, "usePastebin=") + 12) == "true" then
  3192.                 self.usePastebin = true
  3193.             end
  3194.             iniText = iniFile.readLine()
  3195.             if string.sub(iniText, string.find(iniText, "useLog=") + 7) == "true" then
  3196.                 self.useLog = true
  3197.             end
  3198.             iniText = iniFile.readLine()
  3199.             if string.sub(iniText, string.find(iniText, "useVerbose=") + 11) == "true" then
  3200.                 self.useVerbose = true
  3201.             end
  3202.             iniFile.close()    
  3203.         end
  3204.         if fs.exists("superminer.recover") and fs.getSize("superminer.recover") > 0 then
  3205.             self.useRecoveryFile = true
  3206.             self.boolRecoveryFileExists = true
  3207.         end
  3208.        
  3209.         return self
  3210.     end
  3211.    
  3212.     function clsLogfile.setUseVerbose(self, use)
  3213.         self.useVerbose = use
  3214.     end
  3215.    
  3216.     function clsLogfile.getUseVerbose(self)
  3217.         return self.useVerbose
  3218.     end
  3219.    
  3220.     function clsLogfile.getUseLog(self)
  3221.         return self.useLog
  3222.     end
  3223.    
  3224.     function clsLogfile.setUseLog(self, use)
  3225.         self.useLog = use
  3226.         self.writeIniFile(self)
  3227.     end
  3228.    
  3229.     function clsLogfile.checkLogExists(self)
  3230.         local result = false
  3231.        
  3232.         for i = 1, self.numLogFiles do
  3233.             if self.logFileExists[i] then
  3234.                 result = true
  3235.             end
  3236.         end
  3237.        
  3238.         return result
  3239.     end
  3240.    
  3241.     function clsLogfile.getCurrentFileName(self)
  3242.         return self.currentFileName
  3243.     end
  3244.    
  3245.     function clsLogfile.getCurrentFileSize(self)       
  3246.         return fs.getSize(self.currentFileName)
  3247.     end
  3248.    
  3249.     function clsLogfile.getCurrentFileNameIndex(self)
  3250.         return self.currentFileNameIndex
  3251.     end
  3252.    
  3253.     --use if one logfile not used, eg harvestallTrees only on right side
  3254.     function clsLogfile.setCurrentFileNameIndex(self, indexNo)
  3255.         self.currentFileNameIndex = indexNo
  3256.     end
  3257.    
  3258.     function clsLogfile.getStartFileNameIndex(self)
  3259.         return self.startFileNameIndex
  3260.     end
  3261.    
  3262.     --use during debugging to set logfile to appropriate stage
  3263.     function clsLogfile.setStartFileNameIndex(self, indexNo)
  3264.         self.startFileNameIndex = indexNo
  3265.     end
  3266.    
  3267.     function clsLogfile.deleteLog(self)
  3268.         local result = false
  3269.        
  3270.         for i = 1, self.numLogFiles do
  3271.             if fs.exists(self.logFiles[i]) then
  3272.                 fs.delete(self.logFiles[i])
  3273.             end
  3274.             self.logFileExists[i] = false
  3275.             result = true
  3276.         end
  3277.        
  3278.         return result
  3279.     end
  3280.    
  3281.     function clsLogfile.renameLastLogFile(self, fileIndex)
  3282.         if fs.exists("recover."..self.logFiles[fileIndex]) then
  3283.             fs.delete("recover."..self.logFiles[fileIndex])
  3284.         end
  3285.         if fs.exists(self.logFiles[fileIndex]) then
  3286.             fs.move(self.logFiles[fileIndex], "recover."..self.logFiles[fileIndex])
  3287.         end
  3288.     end
  3289.    
  3290.     function clsLogfile.renameLog(self)
  3291.         local result = false
  3292.        
  3293.         for i = 1, self.numLogFiles do
  3294.             if fs.exists(self.logFiles[i]) then
  3295.                 if fs.exists(self.logFiles[i]..".bak") then
  3296.                     fs.delete(self.logFiles[i]..".bak")
  3297.                 end
  3298.                 fs.move(self.logFiles[i], self.logFiles[i]..".bak")
  3299.                 --fs.rename(self.logFiles[i], self.logFiles[i]..".bak")
  3300.             end
  3301.             self.logFileExists[i] = false
  3302.             result = true
  3303.         end
  3304.        
  3305.         return result
  3306.     end
  3307.    
  3308.     function clsLogfile.getNextFileName(self)
  3309.         local tempIndex = self.numLogFiles
  3310.        
  3311.         if self.currentFileNameIndex < self.numLogFiles then
  3312.             tempIndex = self.currentFileNameIndex + 1
  3313.         end
  3314.        
  3315.         return self.logFiles[tempIndex]
  3316.     end
  3317.    
  3318.     function clsLogfile.useFileName(self, fileName)
  3319.         local result = false
  3320.        
  3321.         for i = 1, self.numLogFiles do
  3322.             if self.logFiles[i] == fileName then
  3323.                 result = true
  3324.                 self.currentFileName = self.logFiles[i]
  3325.                 self.currentFileNameIndex = i
  3326.                 if not fs.exists(self.currentFileName) then
  3327.                     logFile = fs.open(self.currentFileName, "w")
  3328.                     logFile.writeLine("LogFile "..self.currentFileName.." started at "..textutils.formatTime(os.time(), true))
  3329.                     logFile.close()
  3330.                 end
  3331.                 break
  3332.             end
  3333.         end
  3334.        
  3335.         return result
  3336.     end
  3337.    
  3338.     function clsLogfile.useNextFileName(self)
  3339.         local result = false
  3340.         local logFile = ""
  3341.        
  3342.         self.currentFileNameIndex = self.currentFileNameIndex + 1
  3343.         if self.currentFileNameIndex > self.numLogFiles then
  3344.             self.currentFileNameIndex = self.numLogFiles
  3345.             result = true
  3346.         end
  3347.         self.currentFileName = self.logFiles[self.currentFileNameIndex]
  3348.         if not fs.exists(self.currentFileName) then
  3349.             logFile = fs.open(self.currentFileName, "w")
  3350.             logFile.writeLine("LogFile "..self.currentFileName.." started at "..textutils.formatTime(os.time(), true))
  3351.             logFile.close()
  3352.         end
  3353.         return result
  3354.     end
  3355.    
  3356.     function clsLogfile.appendLine(self, newText, verbose)
  3357.         local logFile = ""
  3358.         local doWrite = false
  3359.        
  3360.         -- use verbose flag to write line ONLY if useVerbose method has been chosen
  3361.         if verbose then
  3362.             if self.useVerbose then
  3363.                 doWrite = true
  3364.             end
  3365.         else
  3366.             doWrite = true
  3367.         end
  3368.        
  3369.         if doWrite then
  3370.             if self.currentFileName == "" then --no logfile written yet
  3371.                 self.currentFileNameIndex = self.startFileNameIndex --default 1 unless set first for debugging
  3372.                 self.currentFileName = self.logFiles[self.startFileNameIndex] -- set filename
  3373.             end
  3374.             if not fs.exists(self.currentFileName) then --logfile not created
  3375.                 logFile = fs.open(self.currentFileName, "w") --create file
  3376.                 logFile.writeLine(newText)
  3377.                 logFile.close()
  3378.             else
  3379.                 logFile = fs.open(self.currentFileName, "a")
  3380.                 logFile.writeLine(newText)
  3381.                 logFile.close()
  3382.             end
  3383.         end
  3384.     end
  3385.    
  3386.     function clsLogfile.iniFileExists(self)
  3387.         return self.boolIniFileExists
  3388.     end
  3389.    
  3390.     function clsLogfile.recoveryFileExists(self)
  3391.         return self.boolRecoveryFileExists
  3392.     end
  3393.    
  3394.     function clsLogfile.getOnServer(self)
  3395.         return self.onServer
  3396.     end
  3397.    
  3398.     function clsLogfile.setOnServer(self, setting)
  3399.         self.onServer = setting
  3400.         self.writeIniFile(self)
  3401.     end
  3402.    
  3403.     function clsLogfile.getUsePastebin(self)
  3404.         return self.usePastebin
  3405.     end
  3406.    
  3407.     function clsLogfile.setUsePastebin(self, setting)
  3408.         self.usePastebin = setting
  3409.         self.writeIniFile(self)
  3410.     end
  3411.    
  3412.     function clsLogfile.getUseVerbose(self)
  3413.         return self.useVerbose
  3414.     end
  3415.    
  3416.     function clsLogfile.setUseVerbose(self, setting)
  3417.         self.useVerbose = setting
  3418.         self.writeIniFile(self)
  3419.     end
  3420.    
  3421.     function clsLogfile.getUseRecovery(self)
  3422.         return self.useRecoveryFile
  3423.     end
  3424.    
  3425.     function clsLogfile.setUseRecovery(self, setting)
  3426.         self.useRecoveryFile = setting
  3427.     end
  3428.    
  3429.     function clsLogfile.writeIniFile(self)
  3430.         local iniFile = ""
  3431.         iniFile = fs.open("superminer.ini", "w")
  3432.         iniFile.writeLine("onServer=".. tostring(self.onServer))
  3433.         iniFile.writeLine("usePastebin=".. tostring(self.usePastebin))
  3434.         iniFile.writeLine("useLog=".. tostring(self.useLog))
  3435.         iniFile.writeLine("useVerbose=".. tostring(self.useVerbose))
  3436.         iniFile.close()
  3437.     end
  3438.    
  3439.     function clsLogfile.deleteIniFile(self)
  3440.         if fs.exists("superminer.ini") then
  3441.             fs.delete("superminer.ini")
  3442.             self.boolIniFileExists = false
  3443.         end
  3444.     end
  3445.     --uses:
  3446.     --[[
  3447.         fso:setStartFileNameIndex(2) -- debugging using harvestTrees log
  3448.         local fileName = fso:getCurrentFileName()
  3449.         local fileNameIndex = fso:getCurrentFileNameIndex()
  3450.         fso:useNextFileName()
  3451.         fso:useFileName("logBranch01.txt")
  3452.         if fso:checkLogExists() then
  3453.             fso:deleteLog()
  3454.         end
  3455.     ]]--
  3456.     --(fso is a reference to VB6 FileSystemObject :) old habits die hard)
  3457.     fso = clsLogfile.new()
  3458. end
  3459.  
  3460. function createMine(level)
  3461.     currentFunction = "createMine"
  3462.     callingFunction = "mineAll"
  3463.  
  3464.     local itemList = "coal,redstone,lapis"
  3465.     local torchesAt = {}
  3466.     local plugAt = {}
  3467.     local do3D = false
  3468.    
  3469.     -- if mining levels above 15 use vertical and horizontal recursive mining
  3470.     if level > 15 then
  3471.         do3D = true
  3472.     end
  3473.    
  3474.     for i = 1, 16 do
  3475.         torchesAt[i] = false
  3476.         plugAt[i] = true
  3477.     end
  3478.     saveToLog("createMine: starting level "..level, true)
  3479.  
  3480.     --[[
  3481.         NSEW used in relative terms for comments, NOT real compass directions
  3482.         go down to chosen level and create + shape,
  3483.         16x2 front/rear arm (N/S)
  3484.         16x1 side arms
  3485.         chest in floor to NW of mineshaft
  3486.         ladder to S of mineshaft on cobble column
  3487.         torches at each end, and half way along
  3488.     ]]--
  3489.     if not stage["createBase"..level] then --base not created yet
  3490.         --clear round mineshaft for player
  3491.         refuel(0)
  3492.         down(1) --now on floor level
  3493.         mineBase(1, itemList, true)
  3494.         turnRight(1)
  3495.         mineBase(1, itemList, true)
  3496.         turnRight(1)
  3497.         mineBase(4, itemList, true)
  3498.         turnRight(1)
  3499.         mineBase(2, itemList, true)
  3500.         turnRight(1)
  3501.         mineBase(3, itemList, true)
  3502.         mineBase(1, itemList, false) -- do not empty chest above
  3503.         if level == 6 then -- add another chest in ceiling on level 7
  3504.             if location:getY() == 5 then
  3505.                 up(1)
  3506.             end
  3507.             go("RFFuRR")
  3508.             turtle.select(slot:getItemSlot("chests"))
  3509.             turtle.placeUp()
  3510.             go("DFFR")
  3511.         else
  3512.             dig.digNew{self = dig, direction = "down", checkForItems = itemList}
  3513.             turtle.select(slot:getItemSlot("chests"))
  3514.             turtle.placeDown()
  3515.         end
  3516.         storeMiningWaste(true)
  3517.         go("RFRFLFdCDdCU")
  3518.         if getStock("torches") > 1 then
  3519.             turtle.select(slot:getItemSlot("torches"))
  3520.             turtle.placeDown()
  3521.             slot.update{self = slot, item = "torches", delete = true}
  3522.         end
  3523.         back(1)
  3524.         if getStock("signs") > 0 then
  3525.             turtle.select(slot:getItemSlot("signs"))
  3526.             turtle.place("Level "..level.."\n"..location:getCompass())
  3527.         end
  3528.         go("LFR FL FdCRdCuCDdCxCLdCU B")
  3529.         if getStock("signs") > 0 then
  3530.             turtle.select(slot:getItemSlot("signs"))
  3531.             turtle.place("Temporary Sign\nStored here for\nnext staircase\nDO NOT REMOVE!")
  3532.         end
  3533.         go("LFLFLL")
  3534.         stage["createBase"..level] = true
  3535.         saveStatus()
  3536.         displaySaveBackup()
  3537.     end
  3538.    
  3539.     if not stage["mineSector1"] then --sector1 of 4 not finished yet
  3540.         --NE Sector
  3541.         if not stage["mineSectorPart1"] then --sector Part1 not finished
  3542.             refuel(0)
  3543.             torchesAt[8] = true
  3544.             mineCorridor(level, 16, torchesAt, plugAt, itemList, "none", do3D) -- torches 8, mining north (1)
  3545.             -- build staircase
  3546.             -- check if sign in stock. no sign = stairs already done and this is a resume
  3547.             if getStock("signs") > 0 then
  3548.                 buildStaircase(level, "forward")
  3549.             else
  3550.                 turnRight(1) -- at end of first corridor, facing E
  3551.             end
  3552.             -- above level 15 mine corridors, below mine flat areas
  3553.             if level <= 15 then
  3554.                 go("FRuCxDxCU")
  3555.                 mineStrip(level, 12, itemList) -- 1 S
  3556.                 go("LFLuCxC")
  3557.                 mineStrip(level, 11, itemList) -- 2 N
  3558.                 go("FRuCxDxCU FRuCxDxCU")
  3559.                 mineStrip(level, 14, itemList) -- 3 S
  3560.                 go("FRuC FuCxDxCU BxDxCdUL FuCxDxCUL FLuCxDxCU")
  3561.                 mineStrip(level, 15, itemList) -- 4 N
  3562.                 go("FRuCxDxCU FRuCxDxCU")
  3563.                 mineStrip(level, 15, itemList) -- 5 S
  3564.                 go("FLuCxDxCU FLuCxDxCU")
  3565.                 mineStrip(level, 15, itemList) -- 6 N
  3566.                 go("FRuCxDxCU FRuCxDxCU")
  3567.                 mineStrip(level, 15, itemList) -- 7 S
  3568.                 go("FRRuCxDxCU FL FFFF FFFFL") -- return to chest, face S
  3569.             else
  3570.                 torchesAt[8] = true
  3571.                 torchesAt[16] = true
  3572.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16  moving E on top edge (2)
  3573.                 turnRight(1)
  3574.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8, 15  moving S on right side(3)
  3575.                 back(1)
  3576.                 turnRight(1)
  3577.                 mineCorridor(level, 14, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving W on lower edge (4)
  3578.                 go("FFFL") -- over chest facing S
  3579.             end
  3580.             storeMiningWaste(true)
  3581.             go("LFL")
  3582.             emptyBucket()
  3583.             back(1) -- over mineshaft
  3584.             refuel(8)
  3585.             stage["mineSectorPart1"] = true
  3586.             checkReturnToBase(level, 1)
  3587.             saveStatus()
  3588.             displaySaveBackup()
  3589.         end
  3590.         if not stage["mineSectorPart2"] then
  3591.             if level <= 15 then
  3592.                 forward(16)
  3593.                 turnRight(1)
  3594.                 forward(8)
  3595.                 go("RxDxCUxT") -- place torch
  3596.                 mineStrip(level, 15, itemList) -- 8 S
  3597.                 go("FLxDxCUxT FLuCxDxCU")
  3598.                 mineStrip(level, 15, itemList) -- 9 N
  3599.                 go("FRuCxDxCU FRuCxDxCU")
  3600.                 mineStrip(level, 15, itemList) -- 10 S
  3601.                 go("FLuCxDxCU FLuCxDxCU")
  3602.                 mineStrip(level, 15, itemList) -- 11 N
  3603.                 go("FRuCxDxCU FRuCxDxCU")
  3604.                 mineStrip(level, 15, itemList) -- 12 S
  3605.                 go("FLuCxDxCU FLuCxDxCU")
  3606.                 mineStrip(level, 15, itemList) -- 13 N
  3607.                 go("FRuCxDxCU FRuCxDxCU")
  3608.                 mineStrip(level, 15, itemList) -- 14 S
  3609.                 go("FLuCxDxCU FLuCxDxCU")
  3610.                 mineStrip(level, 15, itemList) -- 15 N
  3611.                 go("FRuCxFRxDxCUxT")
  3612.                 torchesAt[8] = true
  3613.                 torchesAt[16] = true
  3614.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8, 15  moving S on right side(3)
  3615.                 go("RRFL")
  3616.                 forward(17)
  3617.                 turnLeft(1)
  3618.             else
  3619.                 forward(11)
  3620.                 turnRight(1)
  3621.                 torchesAt[16] = false
  3622.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8  moving E (5a)
  3623.                 go("RFFFFFR")
  3624.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving W (6a)
  3625.                 go("LFFFFFRFL") -- face S
  3626.                 storeMiningWaste(true)
  3627.             end
  3628.             storeMiningWaste(true)
  3629.             go("LFL")
  3630.             emptyBucket()
  3631.             back(1) -- over mineshaft
  3632.             stage["mineSectorPart2"] = true
  3633.         end
  3634.         for i = 1, 3 do
  3635.             stage["mineSectorPart"..tostring(i)] = false
  3636.         end
  3637.         stage["mineSector1"] = true
  3638.         --? return home first
  3639.         checkReturnToBase(level, 1)
  3640.         saveStatus()
  3641.         displaySaveBackup()
  3642.     end
  3643.    
  3644.     if not stage["mineSector2"] then
  3645.         --NW Sector
  3646.         if not stage["mineSectorPart1"] then
  3647.             refuel(0)
  3648.             go("LFRF") -- over chest
  3649.             if level <= 15 then
  3650.                 mineStrip(level, 14, itemList) -- 1 N
  3651.                 go("FLuCxDxCU FLuCxDxCU")
  3652.                 mineStrip(level, 15, itemList) -- 2 S
  3653.                 go("FRuCxDxCU FRuCxDxCU")
  3654.                 mineStrip(level, 15, itemList) -- 3 N
  3655.                 go("FLuCxDxCU FLuCxDxCU")
  3656.                 mineStrip(level, 15, itemList) -- 4 S
  3657.                 go("FRuCxDxCU FRuCxDxCU")
  3658.                 mineStrip(level, 15, itemList) -- 5 N
  3659.                 go("FLuCxDxCU FLuCxDxCU")
  3660.                 mineStrip(level, 15, itemList) -- 6 S
  3661.                 go("FRuCxDxCU FRuCxDxCU")
  3662.                 mineStrip(level, 15, itemList) -- 7 N
  3663.                 go("FLuCxDxCU FLuCxDxCU")
  3664.                 mineStrip(level, 15, itemList) -- 8 S
  3665.                 go("FRRuCxDxCU FR FFFF FFFR")
  3666.             else
  3667.                 torchesAt[8] = false
  3668.                 torchesAt[16] = false
  3669.                 mineCorridor(level, 15, torchesAt, plugAt, itemList, "none", do3D) -- torches none, mining north (9)
  3670.                 turnLeft(1) -- at end of first corridor, facing W
  3671.                 torchesAt[8] = true
  3672.                 torchesAt[16] = true
  3673.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16  moving W on top edge (10)
  3674.                 turnLeft(1)
  3675.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8, 16  moving S on left side(11)
  3676.                 back(1)
  3677.                 turnLeft(1)
  3678.                 mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving E on lower edge (12)
  3679.                 go("FR") -- face S
  3680.             end
  3681.             storeMiningWaste(true)
  3682.             go("LFL")
  3683.             emptyBucket()
  3684.             back(1) -- over mineshaft
  3685.             stage["mineSectorPart1"] = true
  3686.             checkReturnToBase(level, 2)
  3687.             saveStatus()
  3688.             displaySaveBackup()
  3689.         end
  3690.         if not stage["mineSectorPart2"] then
  3691.             refuel(0)
  3692.             if level <= 15 then
  3693.                 go("LFR FFFF FFFF FFFF FFFFL FFFF FFFFLxDxCUxT")
  3694.                 mineStrip(level, 15, itemList) -- 9 S
  3695.                 go("FRxDxCUxTFRuCxDxCU")
  3696.                 mineStrip(level, 15, itemList) -- 10 N
  3697.                 go("FLuCxDxCU FLuCxDxCU")
  3698.                 mineStrip(level, 15, itemList) -- 11 S
  3699.                 go("FRuCxDxCU FRuCxDxCU")
  3700.                 mineStrip(level, 15, itemList) -- 12 N
  3701.                 go("FLuCxDxCU FLuCxDxCU")
  3702.                 mineStrip(level, 15, itemList) -- 13 S
  3703.                 go("FRuCxDxCU FRuCxDxCU")
  3704.                 mineStrip(level, 15, itemList) -- 14 N
  3705.                 go("FLuCxDxCU FLuCxDxCU")
  3706.                 mineStrip(level, 15, itemList) -- 15 S
  3707.                 go("FRuCxDxCU FRuCxDxCU")
  3708.                 mineStrip(level, 15, itemList) -- 16 N
  3709.                 go("FLuCxDxCU FLxDxCUxT")
  3710.                 torchesAt[8] = true
  3711.                 torchesAt[16] = true
  3712.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8, 16  moving S on left side(11)
  3713.                 go("BLFFFFFFFFFFFFFFFFR")
  3714.                 storeMiningWaste(true)
  3715.                 go("LFL")
  3716.                 emptyBucket()
  3717.                 back(1) -- over mineshaft
  3718.                 stage["mineSectorPart1"] = true
  3719.             else
  3720.                 go("LFRF") -- over chest
  3721.                 forward(10)
  3722.                 turnLeft(1)
  3723.                 torchesAt[16] = false
  3724.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8  moving W (13)
  3725.                 go("LFFFFFL")
  3726.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving E (14)
  3727.                 while location:getY() < level do
  3728.                     up(1)
  3729.                 end
  3730.                 go("RFFFFF")
  3731.                 storeMiningWaste(true)
  3732.                 go("LFL")
  3733.                 emptyBucket()
  3734.                 back(1) -- over mineshaft
  3735.                 stage["mineSectorPart2"] = true
  3736.             end
  3737.         end
  3738.         for i = 1, 2 do
  3739.             stage["mineSectorPart"..tostring(i)] = false
  3740.         end
  3741.         stage["mineSector2"] = true
  3742.         --? return home first
  3743.         checkReturnToBase(level, 2)
  3744.         saveStatus()
  3745.         displaySaveBackup()
  3746.     end
  3747.    
  3748.     if not stage["mineSector3"] then -- SE sector
  3749.         if not stage["mineSectorPart1"] then
  3750.             refuel(0)
  3751.             go("FRFL") --collect sign from wall
  3752.             dig.digNew{self = dig, expectedItem = "signs"}
  3753.             go("LFFLFFFFLFR") -- move to start
  3754.             torchesAt[8] = false
  3755.             torchesAt[6] = true
  3756.             mineCorridor(level, 14, torchesAt, plugAt, itemList, "none", do3D) -- torches 6, 14 mining S (17)
  3757.             torchesAt[6] = false
  3758.             torchesAt[8] = true
  3759.             torchesAt[16] = false
  3760.             -- build staircase
  3761.             --check if sign in stock. no sign = stairs already done and this is a resume
  3762.            
  3763.             if getStock("signs") > 0 then
  3764.                 buildStaircase(level, "backward")
  3765.             else
  3766.                 turnLeft(1) -- at end of first corridor, facing E
  3767.             end
  3768.             if level <= 15 then
  3769.                 go("FLuCxDxCU")
  3770.                 mineStrip(level, 13, itemList) -- 1 N
  3771.                 go("FFFR FRuCxDxCU")
  3772.                 mineStrip(level, 15, itemList) -- 2 S
  3773.                 go("FLuCxDxCU FLuCxDxCU")
  3774.                 mineStrip(level, 15, itemList) -- 3 N
  3775.                 go("FRuCxDxCU FRuCxDxCU")
  3776.                 mineStrip(level, 15, itemList) -- 4 S
  3777.                 go("FLuCxDxCU FLuCxDxCU")
  3778.                 mineStrip(level, 15, itemList) -- 5 N
  3779.                 go("FRuCxDxCU FRuCxDxCU")
  3780.                 mineStrip(level, 15, itemList) -- 6 S
  3781.                 go("FLuCxDxCU FLuCxDxCU")
  3782.                 mineStrip(level, 15, itemList) -- 7 N
  3783.                 go("FRRuCxDxCU FR FFF FFF LFRFFRFFFFRR") -- over chest
  3784.             else
  3785.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16  moving E on lower edge (18)
  3786.                 turnLeft(1)
  3787.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8  moving N on right side(19)
  3788.                 back(1)
  3789.                 turnLeft(1)
  3790.                 mineCorridor(level, 14, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving W on mid section (20)
  3791.                 go("FLFRFFRFFFFRR")
  3792.             end
  3793.             storeMiningWaste(true)
  3794.             go("LFL")
  3795.             emptyBucket()
  3796.             back(1) -- over mineshaft
  3797.             refuel(8)
  3798.             stage["mineSectorPart1"] = true
  3799.             checkReturnToBase(level, 3)
  3800.             saveStatus()
  3801.             displaySaveBackup()
  3802.         end        
  3803.         if not stage["mineSectorPart2"] then
  3804.             refuel(0)
  3805.             if level <= 15 then
  3806.                 go("LFLFFFLFR")
  3807.                 forward(14)
  3808.                 turnLeft(1)
  3809.                 forward(8)
  3810.                 go("LxDxCUxT")
  3811.                 mineStrip(level, 15, itemList) -- 8 N
  3812.                 go("FRuCxDxCU FRuCxDxCU")
  3813.                 mineStrip(level, 15, itemList) -- 9 S
  3814.                 go("FLuCxDxCU FLuCxDxCU")
  3815.                 mineStrip(level, 15, itemList) -- 10 N
  3816.                 go("FRuCxDxCU FRuCxDxCU")
  3817.                 mineStrip(level, 15, itemList) -- 11 S
  3818.                 go("FLuCxDxCU FLuCxDxCU")
  3819.                 mineStrip(level, 15, itemList) -- 12 N
  3820.                 go("FRuCxDxCU FRuCxDxCU")
  3821.                 mineStrip(level, 15, itemList) -- 13 S
  3822.                 go("FLuCxDxCU FLuCxDxCU")
  3823.                 mineStrip(level, 15, itemList) -- 14 N
  3824.                 go("FRuCxDxCU FRuCxDxCU")
  3825.                 mineStrip(level, 15, itemList) -- 15 S
  3826.                 go("FLuCxDxCU FLxDxCUxT")
  3827.                 torchesAt[8] = true
  3828.                 torchesAt[16] = false
  3829.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8  moving N on right side(19)
  3830.                 back(1)
  3831.                 turnLeft(1)
  3832.                 forward(15)
  3833.                 go("LFRFFRFFFFRR")         
  3834.             else
  3835.                 go("LFLFFFLFR")
  3836.                 forward(9)
  3837.                 turnLeft(1)
  3838.                 torchesAt[16] = false
  3839.                 mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8  moving E (21a)
  3840.                 go("FLFFFFFL")
  3841.                 mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving W (22a)
  3842.                 while location:getY() < level do
  3843.                     up(1)
  3844.                 end
  3845.                 go("FRFFFFLFRFFFFLL")
  3846.             end
  3847.             storeMiningWaste(true)
  3848.             go("LFL")
  3849.             emptyBucket()
  3850.             back(1) -- over mineshaft
  3851.             stage["mineSectorPart2"] = true
  3852.         end
  3853.         for i = 1, 2 do
  3854.             stage["mineSectorPart"..tostring(i)] = false
  3855.         end
  3856.         stage["mineSector3"] = true
  3857.         checkReturnToBase(level, 3)
  3858.         saveStatus()
  3859.         displaySaveBackup()
  3860.     end
  3861.    
  3862.     if not stage["mineSector4"] then
  3863.         -- SW sector
  3864.         if not stage["mineSectorPart1"] then
  3865.             refuel(0)
  3866.             go("LFLFFF")
  3867.             if level <= 15 then
  3868.                 mineStrip(level, 13, itemList) -- 1 S
  3869.                 go("FRuCxDxCU FRuCxDxCU")
  3870.                 mineStrip(level, 15, itemList) -- 2 N
  3871.                 go("FLuCxDxCU FLuCxDxCU")
  3872.                 mineStrip(level, 15, itemList) -- 3 S
  3873.                 go("FRuCxDxCU FRuCxDxCU")
  3874.                 mineStrip(level, 15, itemList) -- 4 N
  3875.                 go("FLuCxDxCU FLuCxDxCU")
  3876.                 mineStrip(level, 15, itemList) -- 5 S
  3877.                 go("FRuCxDxCU FRuCxDxCU")
  3878.                 mineStrip(level, 15, itemList) -- 6 N
  3879.                 go("FLuCxDxCU FLuCxDxCU")
  3880.                 mineStrip(level, 15, itemList) -- 7 S
  3881.                 go("FRuCxDxCU FRuCxDxCU")
  3882.                 mineStrip(level, 15, itemList) -- 8 N
  3883.                 go("FRuCxDxCU FFFF FFFL FFLL")
  3884.             else
  3885.                 torchesAt[8] = false
  3886.                 torchesAt[16] = false
  3887.                 mineCorridor(level, 14, torchesAt, plugAt, itemList, "none", do3D) -- torches none, mining S (25)
  3888.                 turnRight(1)
  3889.                 torchesAt[8] = true
  3890.                 torchesAt[16] = true
  3891.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16  moving W on lower edge (26)
  3892.                 turnRight(1)
  3893.                 torchesAt[16] = false
  3894.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8  moving N on left side(27)
  3895.                 back(1)
  3896.                 turnRight(1)
  3897.                 mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving E on mid section(28)
  3898.                 go("FLFFFRR")  
  3899.             end
  3900.             storeMiningWaste(true)
  3901.             go("LFL")
  3902.             emptyBucket()
  3903.             back(1) -- over mineshaft
  3904.             stage["mineSectorPart1"] = true
  3905.             checkReturnToBase(level, 4)
  3906.             saveStatus()
  3907.             displaySaveBackup()
  3908.         end
  3909.         if not stage["mineSectorPart2"] then
  3910.             if level <= 15 then --diamond level
  3911.                 go("LFL")
  3912.                 forward(17)
  3913.                 turnRight(1)
  3914.                 forward(7)
  3915.                 go("FRxDxCUxT")
  3916.                 mineStrip(level, 15, itemList) -- 9 N
  3917.                 go("FLuCxDxCU FLuCxDxCU")
  3918.                 mineStrip(level, 15, itemList) -- 10 S
  3919.                 go("FRuCxDxCU FRuCxDxCU")
  3920.                 mineStrip(level, 15, itemList) -- 11 N
  3921.                 go("FLuCxDxCU FLuCxDxCU")
  3922.                 mineStrip(level, 15, itemList) -- 12 S
  3923.                 go("FRuCxDxCU FRuCxDxCU")
  3924.                 mineStrip(level, 15, itemList) -- 13 N
  3925.                 go("FLuCxDxCU FLuCxDxCU")
  3926.                 mineStrip(level, 15, itemList) -- 14 S
  3927.                 go("FRuCxDxCU FRuCxDxCU")
  3928.                 mineStrip(level, 15, itemList) -- 15 N
  3929.                 go("FLuCxDxCU FLuCxDxCU")
  3930.                 mineStrip(level, 15, itemList) -- 16 S
  3931.                 go("FRuCxFRxDxCUxT")
  3932.                 torchesAt[8] = true
  3933.                 torchesAt[16] = false
  3934.                 mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8  moving N on left side(27)
  3935.                 back(1)
  3936.                 turnRight(1)
  3937.                 forward(16)
  3938.                 go("LFFFRR")
  3939.             else
  3940.                 refuel(0)
  3941.                 go("LFLFFF")
  3942.                 forward(9)
  3943.                 turnRight(1)
  3944.                 torchesAt[16] = false
  3945.                 mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8  moving W (29a)
  3946.                 go("FRFFFFFR")
  3947.                 mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving E (30a)
  3948.                 while location:getY() < level do
  3949.                     up(1)
  3950.                 end
  3951.                 go("FLFFFFFFFFRR")
  3952.             end
  3953.             storeMiningWaste(true)
  3954.             go("LFL")
  3955.             emptyBucket()
  3956.             back(1) -- over mineshaft
  3957.             stage["mineSectorPart1"] = true
  3958.         end
  3959.         stage["mineSector4"] = true
  3960.     end
  3961.     -- update mine status
  3962.     stage["completeMine"..tostring(level)] = true
  3963.     -- reset sectors ready for next level
  3964.     for i = 1, 4 do
  3965.         stage["mineSector"..tostring(i)] = false
  3966.     end
  3967.     for i = 1, 2 do
  3968.         stage["mineSectorPart"..tostring(i)] = false
  3969.     end
  3970.     saveToLog("createMine: mine at level "..level.." completed", true, true)
  3971.     saveStatus()
  3972.     displaySaveBackup()
  3973. end
  3974.  
  3975. function createPlaceStorageObject()
  3976.     clsPlaceStorage = {} -- the table representing the class, which will double as the metatable for any instances
  3977.     clsPlaceStorage.__index = clsPlaceStorage -- failed table lookups on the instances should fallback to the class table, to get methods
  3978.  
  3979.     function clsPlaceStorage.initialise() --equivalent to VB class initialise
  3980.         local self = setmetatable({}, clsPlaceStorage)
  3981.  
  3982.         self.storage = {}
  3983.        
  3984.         self.storage["wood"] = false
  3985.         self.storage["torches"] = false
  3986.         self.storage["sticks"] = false
  3987.         self.storage["building"] = false
  3988.         self.storage["ores"] = false
  3989.         self.storage["minerals"] = false
  3990.         self.storage["misc"] = false
  3991.        
  3992.         return self
  3993.     end
  3994.  
  3995.     function clsPlaceStorage.getStoragePlaced(self, storageName)
  3996.         --placeStorage:getStoragePlaced("wood")
  3997.         return self.storage[storageName] or false
  3998.     end
  3999.    
  4000.     function clsPlaceStorage.setStoragePlaced(self, storageName)
  4001.         --placeStorage:setStoragePlaced("wood")
  4002.         self.storage[storageName] = true
  4003.     end
  4004.    
  4005.     placeStorage = clsPlaceStorage.initialise()
  4006. end
  4007.  
  4008. function createSlotObject()
  4009. -- slot variables
  4010.     clsSlot = {}
  4011.     clsSlot.__index = clsSlot
  4012.    
  4013.     function clsSlot.new(self) --creates new instance of slot object
  4014.         local self = setmetatable({}, clsSlot)
  4015.        
  4016.         self.index = 1
  4017.         self.slotContains = {}
  4018.         self.slotCount = {}
  4019.         self.slotStatus = {}
  4020.         self.itemTested = {}
  4021.         self.firstUnknownItem = ""
  4022.         self.lastUnknownItem = ""
  4023.         self.freeUnknownItem = "item1"
  4024.         self.numUnknownItems = 0
  4025.         -- initialise variables
  4026.         for i = 1, 16 do
  4027.             self.slotContains[i] = ""
  4028.             self.slotCount[i] = 0
  4029.             self.slotStatus[i] = "empty"
  4030.             self.itemTested[i] = ""
  4031.         end
  4032.        
  4033.         return self    
  4034.     end
  4035.    
  4036.     function clsSlot.getStatus(self) -- used to save status
  4037.         return {self.slotContains, self.slotCount, self.slotStatus, self.itemTested,
  4038.         self.firstUnknownItem, self.lastUnknownItem, self.freeUnknownItem, self.numUnknownItems}
  4039.     end
  4040.    
  4041.     function clsSlot.setStatus(self, status) -- used to load status
  4042.         self.slotContains = status[1]
  4043.         self.slotCount = status[2]
  4044.         self.slotStatus = status[3]
  4045.         self.itemTested = status[4]
  4046.         self.firstUnknownItem = status[5]
  4047.         self.lastUnknownItem = status[6]
  4048.         self.freeUnknownItem = status[7]
  4049.         self.numUnknownItems = status[8]
  4050.     end
  4051.    
  4052.     function clsSlot.printSlotContents(self)
  4053.         --debug log
  4054.         local printSlot = 0
  4055.         for i = 1, 16 do
  4056.             if self.slotCount[i] > 0 then
  4057.                 saveToLog(" slot.update: debug - Slot "..i.." contains "..self.slotCount[i].." "..self.slotContains[i])
  4058.             end
  4059.         end
  4060.         for i = 1, 16 do
  4061.             if self.slotStatus[i] == "unknown" then
  4062.                 saveToLog(" slotUpdate: debug - item"..i.." slot is "..self.slotContains[i])
  4063.             end
  4064.         end
  4065.     end
  4066.    
  4067.     function clsSlot.resetVariables(self) --resets all variables (properties) of slot object
  4068.         for i = 1, 16 do
  4069.             self.slotContains[i] = ""
  4070.             self.slotCount[i] = 0
  4071.             self.slotStatus[i] = "empty"
  4072.             self.itemTested[i] = ""
  4073.         end
  4074.         self.firstUnknownItem = ""
  4075.         self.lastUnknownItem = ""
  4076.         self.freeUnknownItem = "item1"
  4077.         self.index = 1
  4078.         self.numUnknownItems = 0       
  4079.     end
  4080.    
  4081.     function clsSlot.getUnknownItemCount(self) --returns no of unknown items eg "item1", "item2" count = 2
  4082.         local amount = 0
  4083.         local tempSlot = 0
  4084.        
  4085.         for i = 1, 16 do
  4086.             if self.slotStatus[i] == "unknown" then
  4087.                 amount = amount + 1
  4088.             end
  4089.         end
  4090.  
  4091.         return amount--0 if no unknown item slots
  4092.     end
  4093.    
  4094.     function clsSlot.getSlotContains(self, slotNo) --return contents of selected slotNo eg slot 4 = "dirt"
  4095.         return self.slotContains[slotNo]
  4096.     end
  4097.    
  4098.     function clsSlot.getItemCount(self, item) --return number of items in turtle using item name eg "dirt" = 58
  4099.         local result = 0
  4100.        
  4101.         for i = 1, 16 do
  4102.             if self.slotContains[i] == item then
  4103.                 result = self.slotCount[i]
  4104.                 break
  4105.             end
  4106.         end
  4107.         return result
  4108.     end
  4109.    
  4110.     function clsSlot.getSlotCount(self, slotNo) --return number of items in turtle slot = turtle.getItemCount()
  4111.         return self.slotCount[slotNo]
  4112.     end
  4113.    
  4114.     function clsSlot.getSlotStatus(self, slotNo)    --return "empty", "known", "unknown"
  4115.         return self.slotStatus[slotNo]
  4116.     end
  4117.    
  4118.     function clsSlot.setSlotCount(self, slotNo, count)  --set number of items in turtle slot
  4119.         self.slotCount[slotNo] = count
  4120.     end
  4121.    
  4122.     function clsSlot.getItemSlot(self, item) --return slot number of selected item eg dirt in slot 4
  4123.         local result = 0
  4124.        
  4125.         for i = 1, 16 do
  4126.             if self.slotContains[i] == item then
  4127.                 result = i
  4128.                 break
  4129.             end
  4130.         end
  4131.        
  4132.         return result
  4133.     end
  4134.    
  4135.     function clsSlot.getItemTested(self, slotNo, item) --VB Property Get return if eg slot 11 has been tested for "gravel"
  4136.         local result = false
  4137.        
  4138.         if self.itemTested[slotNo] ~= "" then
  4139.             if string.find(self.itemTested[slotNo], item) ~= nil then
  4140.                 result = true
  4141.             end
  4142.         end
  4143.        
  4144.         return result
  4145.     end
  4146.    
  4147.     function clsSlot.setItemTested(self, slotNo, item) --VB Property Let equivalent used to see if an unknown item has been checked
  4148.         --self.itemTested[item1] = "coal,flint,gravel"
  4149.         local result = false
  4150.        
  4151.         if self.itemTested[slotNo] == "" then
  4152.             self.itemTested[slotNo] = item
  4153.         else
  4154.             if string.find(self.itemTested[slotNo], item) == nil then
  4155.                 self.itemTested[slotNo] = self.itemTested[slotNo]..","..item
  4156.                 result = true
  4157.             end
  4158.         end
  4159.        
  4160.         return result
  4161.     end
  4162.    
  4163.     function clsSlot.resetUnknownItems(self) --reset "item1", "item2" etc to default
  4164.         local index = 0
  4165.         local firstItem = ""
  4166.         local lastItem = ""
  4167.        
  4168.         self.numUnknownItems = 0
  4169.         for i = 1, 16 do
  4170.             if self.slotStatus[i] == "unknown" then
  4171.                 index = index + 1
  4172.                 self.numUnknownItems = self.numUnknownItems + 1
  4173.                 self.slotContains[i] = "item"..index
  4174.                 if firstItem == "" then
  4175.                     firstItem = "item"..index
  4176.                 end
  4177.                 lastItem = "item"..index
  4178.                 firstFreeItem = "item"..index + 1
  4179.             end
  4180.         end
  4181.         if index > 0 then --at least 1 unknown item
  4182.             self.firstUnknownItem = firstItem
  4183.             self.lastUnknownItem = lastItem
  4184.             self.freeUnknownItem = firstFreeItem
  4185.         else
  4186.             self.firstUnknownItem = ""
  4187.             self.lastUnknownItem = ""
  4188.             self.freeUnknownItem = "item1"
  4189.         end
  4190.     end
  4191.    
  4192.     function clsSlot.getFirstUnknownItem(self) --returns eg item1 or ""
  4193.         return self.firstUnknownItem
  4194.     end
  4195.    
  4196.     function clsSlot.getLastUnknownItem(self) --returns eg "item3"
  4197.         return self.lastUnknownItem
  4198.     end
  4199.    
  4200.     function clsSlot.getFreeUnknownItem(self) --returns first unused itemX eg "item4"
  4201.         return self.freeUnknownItem
  4202.     end
  4203.    
  4204.     function clsSlot.getMostUnknownItem(self) --returns itemX with largest itemCount eg "item3"
  4205.         local mostItem = ""
  4206.         local amount = 0
  4207.         local tempSlot = 0
  4208.        
  4209.         for i = 1, 16 do
  4210.             if self.slotStatus[i] == "unknown" then
  4211.                 if self.slotCount[i] > amount then
  4212.                     amount = self.slotCount[i]
  4213.                     mostItem = self.slotContains[i]
  4214.                 end
  4215.             end
  4216.         end
  4217.        
  4218.         return mostItem --"" if no unknown items
  4219.     end
  4220.    
  4221.    
  4222.     function clsSlot.getMostUnknownItemSlot(self) --returns itemX with largest itemCount eg "item3"
  4223.         local amount = 0
  4224.         local useSlot = 0
  4225.        
  4226.         for i = 1, 16 do
  4227.             if self.slotStatus[i] == "unknown" then
  4228.                 if self.slotCount[i] > amount then
  4229.                     amount = self.slotCount[i]
  4230.                     useSlot = i
  4231.                 end
  4232.             end
  4233.         end
  4234.        
  4235.         return useSlot --0 if no unknown items
  4236.     end
  4237.    
  4238.     function clsSlot.getLeastUnknownItemSlot(self) --returns slotNo with smallest itemCount eg "item1" - slot 7
  4239.         local amount = 65
  4240.         local useSlot = 0
  4241.        
  4242.         for i = 1, 16 do
  4243.             if self.slotStatus[i] == "unknown" then
  4244.                 if self.slotCount[i] < amount then
  4245.                     amount = self.slotCount[i]
  4246.                     useSlot = i
  4247.                 end
  4248.             end
  4249.         end
  4250.        
  4251.         return useSlot --0 if no unknown items
  4252.     end
  4253.    
  4254.     function clsSlot.transfer(arg)
  4255.         local self = arg.self
  4256.  
  4257.         --slot.transfer{self = slot, fromSlot, transferTo = 16}
  4258.         --slot.transfer{self = slot, item = "wood", transferTo = 16}
  4259.         --slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
  4260.         if arg.fromSlot == nil then --arg.item provided
  4261.             for i = 1, 16 do
  4262.                 if self.slotContains[i] == arg.item then
  4263.                     arg.fromSlot = i
  4264.                     break
  4265.                 end
  4266.             end
  4267.             --arg.fromSlot = self.itemSlot[arg.item] --itemSlot["wood"] (1)
  4268.         end
  4269.         if arg.item == nil then
  4270.             arg.item = self.slotContains[arg.fromSlot]
  4271.         end
  4272.         self.slotCount[arg.transferTo] = self.slotCount[arg.fromSlot]
  4273.         self.slotCount[arg.fromSlot] = 0
  4274.         self.slotStatus[arg.transferTo] = self.slotStatus[arg.fromSlot]
  4275.         self.slotStatus[arg.fromSlot] = "empty"
  4276.         self.itemTested[arg.transferTo] = self.itemTested[arg.fromSlot]
  4277.         self.itemTested[arg.fromSlot] = ""
  4278.         self.slotContains[arg.transferTo] = self.slotContains[arg.fromSlot]
  4279.         self.slotContains[arg.fromSlot] = ""
  4280.         --saveToLog("transfer: from slot "..arg.fromSlot.." contains "..self.slotCount[arg.fromSlot].." "..self.slotContains[arg.fromSlot])
  4281.         --saveToLog("transfer: to slot "..arg.transferTo.." contains "..self.slotCount[arg.transferTo].." "..self.slotContains[arg.transferTo])
  4282.     end
  4283.  
  4284.    
  4285.     --slot.update{self = slot, slotNo = dugSlot, item = "item1"}
  4286.     --slot.update{self = slot, item = arg.sourceItem1, delete = true}
  4287.     --slot.update{self = slot, item = "planks", delete = true}
  4288.     --slot.update{self = slot, item = "?iron", newItem = "iron"}
  4289.     --slot.update{self = slot, item = "furnaces", delete = true}
  4290.    
  4291.     function clsSlot.update(arg) -- update turtle slot with item contents
  4292.         local quantity = 0
  4293.         local self = arg.self
  4294.         local tempItem = ""
  4295.         local tempSlotNo = arg.slotNo
  4296.         local count = 0
  4297.        
  4298.         --arg.self
  4299.         --arg.item
  4300.         --arg.slotNo
  4301.         --arg.delete
  4302.         --arg.deleteSlot
  4303.         --arg.newItem
  4304.         -- if slotNo given use it, or use existing slot
  4305.         if arg.slotNo == nil then --slot not provided, default to 0
  4306.             arg.slotNo = 0
  4307.         else --slotNo provided
  4308.             if arg.item == nil then --get item from slotNo
  4309.                 arg.item = self.slotContains[arg.slotNo]
  4310.             end
  4311.         end
  4312.         if arg.item ~= nil then --item name provided
  4313.             if arg.slotNo == 0 then
  4314.                 for i = 1, 16 do
  4315.                     if self.slotContains[i] == arg.item then
  4316.                         arg.slotNo = i
  4317.                         break
  4318.                     end
  4319.                 end
  4320.             end
  4321.         end
  4322.         arg.delete = arg.delete or false
  4323.         arg.doReset = arg.doReset or false --used when crafting in fillChest
  4324.         -- item given. If not already in stock, use next available
  4325.        
  4326.         if arg.newItem ~= nil then -- naming unknown. delete is assumed, arg.slotNo, arg.item calculated above
  4327.             --slot.update{self = slot, slotNo = dugSlot, newItem = "coal"}
  4328.            
  4329.             self.itemTested[arg.slotNo] = ""
  4330.             self.slotContains[arg.slotNo] = arg.newItem
  4331.             self.slotCount[arg.slotNo] = turtle.getItemCount(arg.slotNo)
  4332.  
  4333.             if string.find(arg.newItem, "item") ~= nil then
  4334.                 self.slotStatus[arg.slotNo] = "unknown"
  4335.             else
  4336.                 self.slotStatus[arg.slotNo] = "known"
  4337.                 --objectives[arg.newItem] = true
  4338.             end
  4339.            
  4340.             saveToLog(" slot.update: Slot "..tostring(tempSlotNo) .." contains "..tostring(self.slotCount[tempSlotNo]).." "..arg.newItem.." "..arg.item.." deleted", false, true)
  4341.         else --not changing an item, just update
  4342.             quantity = turtle.getItemCount(arg.slotNo)
  4343.             if quantity == 0 and arg.delete then --turtle slot empty - remove all references
  4344.                 self.slotContains[arg.slotNo] = ""
  4345.                 self.itemTested[arg.slotNo] = ""
  4346.                 self.slotCount[arg.slotNo] = 0
  4347.                 self.slotStatus[arg.slotNo] = "empty"
  4348.                 saveToLog(" slot.update: Deleting "..arg.item.." Slot "..arg.slotNo.." is empty", false, true)
  4349.             else --create or maintain selected slot, eg when wood2 detected and about to be harvested
  4350.                
  4351.                 self.slotCount[arg.slotNo] = quantity
  4352.                 --saveToLog("   slot.update: "..arg.item.." quantity updated to "..quantity.." in slot "..arg.slotNo, false)
  4353.                 self.slotContains[arg.slotNo] = arg.item
  4354.                 if string.find(arg.item, "item") ~= nil then --"item1", item2 etc
  4355.                     self.slotStatus[arg.slotNo] = "unknown"
  4356.                 else
  4357.                     self.slotStatus[arg.slotNo] = "known"
  4358.                     --objectives[arg.item] = true
  4359.                 end
  4360.                 saveToLog(" slot.update: Slot "..arg.slotNo.." contains "..self.slotCount[arg.slotNo].." of "..self.slotContains[arg.slotNo], true, false)
  4361.             end
  4362.         end
  4363.        
  4364.         --update next free item, eg item1 just been allocated, make item2 the next free one
  4365.        
  4366.         for i = 1, 16 do
  4367.             if string.find(self.slotContains[i], "item") ~= nil then
  4368.                 count = count + 1
  4369.             end
  4370.         end
  4371.         self.freeUnknownItem = "item"..count + 1
  4372.         if count > 0 then
  4373.             self.firstUnknownItem = "item1"
  4374.             self.lastUnknownItem = "item"..count
  4375.             self.freeUnknownItem = "item"..count + 1
  4376.         end
  4377.        
  4378.         --[[for i = 1, 16 do
  4379.             saveToLog(" slot.update: slot "..i.." status = "..self.slotStatus[i])
  4380.         end]]--
  4381.         --saveToLog("   slot.update:unknownItemCount = "..self.numUnknownItems)
  4382.         if arg.doReset then
  4383.             if clsSlot:getUnknownItemCount() > 0 then
  4384.                 --saveToLog("   slot.update: resetting unknown items", false)
  4385.                 clsSlot:resetUnknownItems()
  4386.             end
  4387.         end
  4388.     end
  4389.    
  4390.     slot = clsSlot.new()
  4391. end
  4392.  
  4393. function displaySaveBackup()
  4394.     local numSeconds = 20
  4395.    
  4396.     -- during debug, pause game and use Windows explorer to copy current game save
  4397.     term.clear()
  4398.     for i = 1, 20 do
  4399.         term.setCursorPos(1,6)
  4400.         print(" Safe to pause and make save backup")
  4401.         print()
  4402.         if numSeconds > 9 then
  4403.             print(" Continuing in "..numSeconds.." seconds")
  4404.         else
  4405.             print(" Continuing in 0"..numSeconds.." seconds")
  4406.         end
  4407.         numSeconds = numSeconds - 1
  4408.         sleep(1)
  4409.     end
  4410.     term.clear()
  4411.     term.setCursorPos(1, 1)
  4412. end
  4413.  
  4414. function down(steps, useSlot, itemList, calledFrom)
  4415.     local maxTries = 2
  4416.     local success = false
  4417.     local previousFunction = currentFunction
  4418.    
  4419.     currentFunction = "down"
  4420.    
  4421.     steps = steps or 1
  4422.     useSlot = useSlot or 1
  4423.     itemList = itemList or ""
  4424.     calledFrom = calledFrom or ""
  4425.     if turtle.getFuelLevel() < steps then
  4426.         saveToLog(" ***** down: Fuel < "..steps.." *****", true, false)
  4427.         if not refuel(0) then
  4428.             saveToLog(" ***** down: refuel failed*****", true, false)
  4429.         end
  4430.     end
  4431.     for i = 1, steps do
  4432.         if turtle.detectDown() then
  4433.             dig.digNew{self = dig, direction = "down", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
  4434.             if turtle.down() then
  4435.                 success = true
  4436.             end
  4437.         else --move down unless mob in the way
  4438.             while not turtle.down() do
  4439.                 dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = calledFrom}
  4440.                 --if turtle.attackDown() then
  4441.                 if attack() then
  4442.                     saveToLog("down: mob attacked below!", true)
  4443.                 end
  4444.                 maxTries = maxTries - 1
  4445.                 if maxTries <= 0 then
  4446.                     if turtle.getFuelLevel() == 0 then
  4447.                         if refuel(0) then
  4448.                             maxTries = 2
  4449.                         else
  4450.                             saveToLog("forward: Out of fuel. Game Over!", true)
  4451.                             error()
  4452.                         end
  4453.                     end
  4454.                 end
  4455.             end
  4456.             success = true
  4457.         end
  4458.         if success then
  4459.             location:setY(location:getY() - 1)
  4460.             saveStatus()
  4461.         end
  4462.     end
  4463.    
  4464.     currentFunction = previousFunction
  4465.    
  4466.     return success
  4467. end
  4468.  
  4469. function emptyBucket()
  4470.     turtle.select(slot:getItemSlot("buckets"))
  4471.     if turtle.place() then --bucket emptied
  4472.         saveToLog("emptyBucket: water emptied from bucket", true, false)
  4473.         turtle.select(slot:getItemSlot("cobble"))
  4474.         turtle.place()
  4475.         sleep(0.2)
  4476.         turtle.dig()
  4477.     end
  4478. end
  4479.  
  4480. function emptyChest(direction)
  4481.     local useItem = ""
  4482.     local turtleSlot = 0
  4483.     local success = false
  4484.    
  4485.     for i = 1, 16 do
  4486.         useItem = craftingChest:getSlotContains(i) --eg "dirt", "item"
  4487.         if useItem ~= "" then --"item", "dirt" etc
  4488.             turtleSlot = craftingChest:getTurtleSlot(i)
  4489.             turtle.select(turtleSlot)
  4490.             if direction == "up" then
  4491.                 if turtle.suckUp() then
  4492.                     success = true
  4493.                 end
  4494.             elseif direction == "down" then
  4495.                 if turtle.suckDown() then
  4496.                     success = true
  4497.                 end
  4498.             else
  4499.                 if turtle.suck() then
  4500.                     success = true
  4501.                 end
  4502.             end
  4503.             if success then
  4504.                 --saveToLog("emptyChest: Removed "..craftingChest.getSlotCount(craftingChest, i).." "..useItem.." from chest into turtle slot "..turtleSlot, false)
  4505.                 slot.update{self = slot, slotNo = turtleSlot, item = useItem}
  4506.             else
  4507.                 saveToLog("emptyChest: failed to remove "..useItem.." from chest", false)
  4508.             end
  4509.         end
  4510.     end
  4511.     --clear arrays on emptychest routine only
  4512.     craftingChest.resetVariables(craftingChest)
  4513.     saveToLog("emptyChest completed.", false, true)
  4514.     --unable to test for empty chest so return default
  4515.     return true
  4516. end
  4517.  
  4518. function fillChest(arg)
  4519.     -- fillChest{direction = arg.direction, exceptSlot1 = sourceSlot1, exceptSlot2 = sourceSlot2, exceptSlot3 = sourceSlot3} -- fill except chosen item(s)
  4520.     -- fillChest{direction = "forward", addItem = storedItem, addAmount = storeCount, fromSlot = 16, originalSlot = 0}
  4521.     -- fillChest{direction = arg.direction, addItem = sourceItem3, addAmount = sourceQuantity3, fromSlot = 14} use this for single items
  4522.     -- turtle must be sorted first, so unknown items are after identified items
  4523.  
  4524.     local chestIndex = 1
  4525.     local useItem = ""
  4526.     local quantity = 0
  4527.     local itemAlreadyIn = false
  4528.     if arg.deleteFromTurtle == nil then
  4529.         arg.deleteFromTurtle = true
  4530.     end
  4531.     if arg.doReset == nil then
  4532.         arg.doReset = false
  4533.     end
  4534.    
  4535.     saveToLog("fillChest: arg.direction = "..tostring(arg.direction)..
  4536.                         " arg.exceptSlot1 = "..tostring(arg.exceptSlot1)..
  4537.                         " arg.exceptSlot2 = "..tostring(arg.exceptSlot2)..
  4538.                         " arg.exceptSlot3 = "..tostring(arg.exceptSlot3)..
  4539.                         " arg.addItem = "..tostring(arg.addItem)..
  4540.                         " arg.addAmount = "..tostring(arg.addAmount)..
  4541.                         " arg.fromSlot = "..tostring(arg.fromSlot)..
  4542.                         " arg.originalSlot = "..tostring(arg.originalSlot), false)
  4543.     if arg.addItem == nil then -- single item not defined, so empty all turtle except chosen items. could be failed craft attempt
  4544.         for  i = 1, 16 do
  4545.             if  arg.exceptSlot1 > 0 then --at least 1 craft item needed
  4546.                 if i == arg.exceptSlot1 or  i == arg.exceptSlot2 or  i == arg.exceptSlot3 then
  4547.                     saveToLog("Leaving crafting item "..slot.getSlotContains(slot, i).." in turtle slot "..i, false)
  4548.                 else
  4549.                     useItem = slot.getSlotContains(slot, i) --eg "", "item", "wood"
  4550.                     quantity = turtle.getItemCount(i)
  4551.                     if quantity > 0 then
  4552.                         --saveToLog("Adding ".. quantity.." "..useItem.." from turtle slot "..i.." to chest slot "..craftingChest.getIndex(craftingChest), false)
  4553.                         turtle.select(i)
  4554.                         craftingChest.addItem(craftingChest, useItem, quantity, i)
  4555.                         if arg.direction == "up" then
  4556.                             turtle.dropUp()
  4557.                         elseif arg.direction == "down" then
  4558.                             turtle.dropDown()
  4559.                         else
  4560.                             turtle.drop()
  4561.                         end
  4562.                         --update turtle slot
  4563.                         if arg.deleteFromTurtle then
  4564.                             slot.update{self = slot, slotNo = i, item = useItem, delete = true, doReset = arg.doReset}
  4565.                         end
  4566.                     end
  4567.                 end
  4568.             else --failed craft, empty turtle completely. cannot update quantities, items will go into existing slots, or form a new one
  4569.                 if  turtle.getItemCount(i) > 0 then
  4570.                     if arg.direction == "up" then
  4571.                         turtle.dropUp()
  4572.                     elseif arg.direction == "down" then
  4573.                         turtle.dropDown()
  4574.                     else
  4575.                         turtle.drop()
  4576.                     end
  4577.                 end
  4578.             end
  4579.         end
  4580.     else -- arg.addItem is "chests" or "stone"
  4581.         if arg.fromSlot ~= nil then
  4582.             turtle.select(arg.fromSlot)
  4583.         end
  4584.         if arg.originalSlot == nil then
  4585.             arg.originalSlot = arg.fromSlot
  4586.         end
  4587.         --clsChest.addItem(self, item, quantity, turtleSlot)
  4588.         --craftingChest.addItem(craftingChest, arg.addItem, arg.addAmount, arg.fromSlot)
  4589.         craftingChest:addItem(arg.addItem, arg.addAmount, arg.originalSlot)
  4590.         -- saveToLog("fillChest: Adding "..tostring(arg.addAmount).." "..tostring(arg.addItem).." to chest from slot "..tostring(arg.fromSlot).." (/"..tostring(arg.originalSlot)..")", false)
  4591.         if arg.direction == "up" then
  4592.             turtle.dropUp()
  4593.         elseif arg.direction == "down" then
  4594.             turtle.dropDown()
  4595.         else
  4596.             turtle.drop()
  4597.         end
  4598.     end
  4599.    
  4600.     local slotsEmptied = true
  4601.     for i = 1, 16 do
  4602.         if arg.exceptSlot1 ~= i and  arg.exceptSlot2 ~= i and  arg.exceptSlot3 ~= i then --miss out exceptSlots
  4603.             if turtle.getItemCount(i) > 0 then
  4604.                 slotsEmptied = false
  4605.                 break
  4606.             end
  4607.         end
  4608.     end
  4609.    
  4610.     return slotsEmptied
  4611. end
  4612.  
  4613. function findCobble()
  4614.     currentFunction = "findCobble"
  4615.    
  4616.     if not stage["findCobble"] then
  4617.         saveToLog("findCobble: changing logFile Name from "..fso:getCurrentFileName()..
  4618.                     " to logFindCobble.txt END OF LOGFILE!", false, false)
  4619.         fso:useFileName("logFindCobble.txt")
  4620.         saveToLog("findCobble: starting", false, false)
  4621.         turtle.select(slot:getItemSlot("stone"))
  4622.         if turtle.compareDown() then --on stone so underground
  4623.             stage["startUnderground"] = true
  4624.         end
  4625.         --dig down vertically until 60 cobble found or bedrock reached then return
  4626.         saveToLog("findCobble: mining for cobble", true)
  4627.         --dirt, cobble, gravel, known. coal can be identified and used, others
  4628.         for i = 1, 2 do -- go down 2 levels, in case on surface and grass causes problems as id = valuable
  4629.             dig.digNew{self = dig, direction = "down", checkForItems = "coal,sand", callFrom = "findCobble"}
  4630.             down(1, 1, "coal,sand", "findCobble")
  4631.             for i = 1, 4 do
  4632.                 dig.digNew{self = dig, direction = "forward", checkForItems = "coal,sand", callFrom = "findCobble"}
  4633.                 turnRight(1)
  4634.             end
  4635.         end
  4636.         mineDown(1, "coal,ironore,goldore,redstone,diamonds", "findCobble") --mine down to level 1 or until 60 cobble found
  4637.         changeDirection("faceBackward")
  4638.         repeat
  4639.             forward(1)
  4640.             dig.digNew{self = dig, waitForGravel = true}
  4641.             if getStock("cobble") > 1 then
  4642.                 turtle.select(slot:getItemSlot("cobble"))
  4643.             elseif getStock("dirt") > 1 then
  4644.                 turtle.select(slot:getItemSlot("dirt"))
  4645.             end
  4646.             turtle.place()
  4647.             back(1)
  4648.             turtle.select(slot:getItemSlot("ladders"))
  4649.             turtle.place()
  4650.             up(1, 1, "coal,sand", "findCobble")
  4651.         until location:getY() == coordHome:getY()
  4652.         -- remove last ladder
  4653.         down(1, 1)
  4654.         dig.digNew{self = dig}
  4655.         up(1, 1)
  4656.        
  4657.         if getStock("cobble") < 40 then -- go up to get cobble
  4658.             -- dig up until surface reached(64 common) or cobble reaches 60
  4659.             mineUp(80, "coal,ironore,goldore,redstone,diamonds", "findCobble", true, true)
  4660.             while location:getY() > homeCoord:getY() do
  4661.                 down(1)
  4662.             end
  4663.         end
  4664.         changeDirection("faceForward")
  4665.         saveToLog("findCobble: cobble supplies replenished", true)
  4666.         stage["findCobble"] = true
  4667.         saveStatus()
  4668.         displaySaveBackup()
  4669.     end
  4670.    
  4671.     return true
  4672. end
  4673.  
  4674. function forward(steps, useSlot, itemList, calledFrom)
  4675.     local maxTries = 2
  4676.     local success = true
  4677.     local previousFunction = currentFunction
  4678.    
  4679.     currentFunction = "forward"
  4680.    
  4681.     useSlot = useSlot or 1
  4682.     itemList = itemList or ""
  4683.     calledFrom = calledFrom or ""
  4684.    
  4685.     if steps > 0 then
  4686.         if turtle.getFuelLevel() < steps then
  4687.             saveToLog(" ***** forward: Fuel < "..steps.." *****", true, false)
  4688.             if not refuel(0) then
  4689.                 saveToLog(" ***** forward: refuel failed*****", true, false)
  4690.             end
  4691.         end
  4692.         for i = 1, steps do
  4693.             if turtle.detect() then
  4694.                 while turtle.detect() do
  4695.                     if dig.digNew{self = dig, direction = "forward", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom} then
  4696.                         sleep(0.5)
  4697.                     else
  4698.                         maxTries = maxTries - 1
  4699.                         if maxTries <= 0 then --bedrock
  4700.                             maxTries = 2
  4701.                             break
  4702.                         end
  4703.                     end
  4704.                 end
  4705.             end
  4706.  
  4707.             while not turtle.forward() do
  4708.                 if turtle.detect() then
  4709.                     if dig.digNew{self = dig, direction = "forward", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom} then
  4710.                         sleep(0.5)
  4711.                     else
  4712.                         success = false
  4713.                         break
  4714.                     end
  4715.                 end
  4716.                 attack()
  4717.                 maxTries = maxTries - 1
  4718.                 if maxTries <= 0 then
  4719.                     if turtle.getFuelLevel() == 0 then
  4720.                         if refuel(0) then
  4721.                             maxTries = 2
  4722.                         else
  4723.                             saveToLog("forward: Out of fuel. Game Over!", true)
  4724.                             error()
  4725.                         end
  4726.                     end
  4727.                 end
  4728.             end
  4729.  
  4730.             if success then
  4731.                 changeCoords("forward")
  4732.             end
  4733.         end
  4734.     end
  4735.    
  4736.     currentFunction = previousFunction
  4737.     return success
  4738. end
  4739.  
  4740. function getCoords()
  4741.     currentFunction = "getCoords"
  4742.     callingFunction = "main"
  4743.     --get world coordinates from player
  4744.     local coord = 0
  4745.     local response = ""
  4746.     local continue = true
  4747.     local event = ""
  4748.     local param1 = ""
  4749.    
  4750.     if not stage["getCoords"] then -- not resuming script
  4751.         saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
  4752.                     " to logGetCoords.txt END OF LOGFILE!", false, false)
  4753.         fso:useFileName("logGetCoords.txt")
  4754.         saveToLog("getCoords: Starting", false, false)
  4755.         term.clear()
  4756.         term.setCursorPos(1,1)
  4757.         print("IMPORTANT! Stand directly behind turtle")
  4758.         print("Press F3 to read coordinates")
  4759.         print()
  4760.         continue = true
  4761.         while continue do
  4762.             print("Please enter your X coordinate")
  4763.             write("  x = ")
  4764.             --term.setCursorPos(term.getCursorPos())
  4765.             coord = nil
  4766.             while coord == nil do
  4767.                 coord = tonumber(read())
  4768.                 if coord == nil then
  4769.                     term.clear()
  4770.                     term.setCursorPos(1,1)
  4771.                     print("Incorrect input. Use numbers only!")
  4772.                     print()
  4773.                     print("Please enter your X coordinate")
  4774.                     write("  x = ")
  4775.                     --term.setCursorPos(term.getCursorPos())
  4776.                 end
  4777.             end
  4778.             location:setX(coord)
  4779.             saveToLog("getCoords: x coord set to "..location:getX(), false, false)
  4780.             term.clear()
  4781.             term.setCursorPos(1,1)
  4782.             print("Please enter your Y coordinate")
  4783.             write("  y = ")
  4784.             coord = nil
  4785.             while coord == nil do
  4786.                 coord = tonumber(read())
  4787.                 if coord == nil then
  4788.                     term.clear()
  4789.                     term.setCursorPos(1,1)
  4790.                     print("Incorrect input. Use numbers only")
  4791.                     print()
  4792.                     print("Please enter your y coordinate")
  4793.                     write("  y = ")
  4794.                 end
  4795.             end
  4796.             location:setY(coord)
  4797.             saveToLog("getCoords: y coord set to "..location:getY(), false, false)
  4798.             term.clear()
  4799.             term.setCursorPos(1,1)
  4800.             print("Please enter your Z coordinate")
  4801.             write("  z = ")
  4802.             coord = nil
  4803.             while coord == nil do
  4804.                 coord = tonumber(read())
  4805.                 if coord == nil then
  4806.                     term.clear()
  4807.                     term.setCursorPos(1,1)
  4808.                     print("Incorrect input. Use numbers only")
  4809.                     print()
  4810.                     print("Please enter your z coordinate")
  4811.                     write("  z = ")
  4812.                 end
  4813.             end
  4814.             location:setZ(coord)
  4815.             saveToLog("getCoords: z coord set to "..location:getZ(), false, false)
  4816.             response = true
  4817.             while response do
  4818.                 response = false
  4819.                 term.clear()
  4820.                 term.setCursorPos(1,1)
  4821.                 print("Enter Direction you are facing:")
  4822.                 print("  0,1,2,3 (s,w,n,e)")
  4823.                 print()
  4824.                 print(  "  Direction = ")
  4825.                 event, param1 = os.pullEvent ("char")
  4826.                 --response = read()
  4827.                 if param1 == "s" or param1 == "S" then
  4828.                     coord = 0
  4829.                 elseif param1 == "w" or param1 == "W" then
  4830.                     coord = 1
  4831.                 elseif param1 == "n" or param1 == "N" then
  4832.                     coord = 2
  4833.                 elseif param1 == "e" or param1 == "E" then
  4834.                     coord = 3
  4835.                 elseif param1 == "0" or param1 == "1" or param1 == "2" or param1 == "3" then
  4836.                     coord = tonumber(param1)
  4837.                 else
  4838.                     print()
  4839.                     print("Incorrect input: "..param1)
  4840.                     print()
  4841.                     print("Use 0,1,2,3,n,s,w,e")
  4842.                     sleep(2)
  4843.                     response = true
  4844.                 end
  4845.             end
  4846.             location:setFacing(coord)
  4847.             saveToLog("getCoords: facing set to "..location:getFacing(), false, false)
  4848.             term.clear()
  4849.             term.setCursorPos(1,1)
  4850.             print("Your current location is:")
  4851.             print()
  4852.             print("  x = "..location:getX())
  4853.             print("  y = "..location:getY())
  4854.             print("  z = "..location:getZ())
  4855.             print("  facing "..location:getCompass().." ("..location:getFacing()..")")
  4856.             --[[if floorToCeiling then
  4857.                 print("  Mineshaft surface to bedrock")
  4858.             else]]--
  4859.                 print("  Mineshaft from level "..location:getY().." to bedrock")
  4860.             --end
  4861.             print()
  4862.             write("Is this correct? (y/n)")
  4863.             event, param1 = os.pullEvent ("char")
  4864.             if param1 == "y" or param1 == "Y" then
  4865.                 continue = false
  4866.             end
  4867.         end
  4868.         term.clear()
  4869.         term.setCursorPos(1,1)
  4870.         if location:getY() > 20 then
  4871.             response = true
  4872.             while response do
  4873.                 response = false
  4874.                 term.clear()
  4875.                 term.setCursorPos(1,1)
  4876.                 print("Which mining mode do you want?")
  4877.                 print()
  4878.                 print("1) Diamonds Only")
  4879.                 print("   Starts @ 15->6 takes 4-5 hours")
  4880.                 print()
  4881.                 print("2) Diamonds first, then other ores")
  4882.                 print("   Starts @ 15, then 51->18. >24 hours")
  4883.                 print()
  4884.                 print("3) Ores first")
  4885.                 print("   Starts @ 51, then 15->6. >24 hours")
  4886.                 print()
  4887.                 print(" Choose option: 1, 2, 3")
  4888.                 event, param1 = os.pullEvent ("char")
  4889.                 if param1 == "1" then
  4890.                     diamondsFirst = true
  4891.                     diamondsOnly = true
  4892.                 elseif param1 == "2" then
  4893.                     diamondsFirst = true
  4894.                     diamondsOnly = false
  4895.                 elseif param1 == "3" then
  4896.                     diamondsFirst = false
  4897.                     diamondsOnly = false
  4898.                 else
  4899.                     print()
  4900.                     print("Incorrect input: "..param1)
  4901.                     print()
  4902.                     print("Use 1, 2 or 3")
  4903.                     sleep(2)
  4904.                     response = true
  4905.                 end
  4906.             end
  4907.             saveToLog("getCoords: mine diamonds only set to "..tostring(diamondsOnly), false, false)
  4908.             saveToLog("getCoords: mine diamonds first set to "..tostring(diamondsFirst), false, false)
  4909.         end
  4910.        
  4911.         --[[ correct coords to compensate for player standing position
  4912.         facing:     Change:
  4913.         0 (S)       z+1
  4914.         1 (W)       x-1
  4915.         2 (N)       z-1
  4916.         3 (E)       x+1
  4917.         ]]--
  4918.  
  4919.         if location:getFacing() == 0 then
  4920.             location:setZ(location:getZ() + 1)
  4921.         elseif location:getFacing() == 1 then
  4922.             location:setX(location:getX() - 1)
  4923.         elseif location:getFacing() == 2 then
  4924.             location:setZ(location:getZ() - 1)
  4925.         elseif location:getFacing() == 3 then
  4926.             location:setX(location:getX() + 1)
  4927.         end
  4928.         coordHome:setX(location:getX())
  4929.         coordHome:setY(location:getY())
  4930.         coordHome:setZ(location:getZ())
  4931.         coordHome:setFacing(location:getFacing())
  4932.        
  4933.         mineTopLevel:setX(coordHome:getX())
  4934.         if coordHome:getY() >= 54 then
  4935.             mineTopLevel:setY(51)
  4936.         elseif coordHome:getY() < 8 then
  4937.             term.clear()
  4938.             print("Y coordinate below 8")
  4939.             print("Move up above level 7")
  4940.             error()
  4941.         else -- between level 18 and 53
  4942.             mineTopLevel:setY((math.ceil((coordHome:getY() - 2) / 3) * 3)) -- eg 52/51/50 - 2 /3 = 16 * 3 = 48, 47/48/49 = 45
  4943.         end
  4944.         mineTopLevel:setZ(coordHome:getZ())
  4945.         mineTopLevel:setFacing(coordHome:getFacing())
  4946.        
  4947.         stage["getCoords"] = true
  4948.         saveStatus()
  4949.         saveToLog("Completed coordinates registration at x="..location:getX().." y="..location:getY().." z="..location:getZ().." facing "..location:getCompass(), true)
  4950.         displaySaveBackup()
  4951.     end
  4952. end
  4953.  
  4954. function getFirstEmptySlot(doSort)
  4955.     local checkSlot = 0
  4956.     local lastFullSlot = 0
  4957.    
  4958.     if doSort == nil then
  4959.         doSort = false
  4960.     end
  4961.    
  4962.     for i = 1, 16 do
  4963.         if turtle.getItemCount(i) == 0 then
  4964.             if checkSlot == 0 then
  4965.                 checkSlot = i
  4966.             end
  4967.         else
  4968.             lastFullSlot = i
  4969.         end
  4970.     end
  4971.  
  4972.     if lastFullSlot + 1 ~= checkSlot and doSort then
  4973.         saveToLog("getFirstEmptySlot: first empty = "..checkSlot.." lastFullSlot = "..lastFullSlot)
  4974.         sortInventory(true)
  4975.         checkSlot = 0
  4976.         for i = 1, 16 do
  4977.             if turtle.getItemCount(i) == 0 then
  4978.                 if checkSlot == 0 then
  4979.                     checkSlot = i
  4980.                     break
  4981.                 end
  4982.             end
  4983.         end
  4984.     end
  4985.    
  4986.     return checkSlot
  4987. end
  4988.  
  4989. function getFuelAvailable()
  4990.     local tFuelAvailable = 0
  4991.     local fAvailableCoal = 0
  4992.     local fAvailableCharcoal = 0
  4993.     local fAvailablePlanks = 0
  4994.     local fAvailableWood = 0
  4995.     local fCurrentFuelLevel = turtle.getFuelLevel()
  4996.     local result = {}
  4997.    
  4998.     if slot:getItemSlot("coal") > 0 then
  4999.         fAvailableCoal = (turtle.getItemCount(slot:getItemSlot("coal")) - 1) * 80
  5000.     end
  5001.     if slot:getItemSlot("charcoal") > 0 then
  5002.         fAvailableCharcoal = (turtle.getItemCount(slot:getItemSlot("charcoal"))) * 80
  5003.     end
  5004.     if slot:getItemSlot("planks") > 0 then
  5005.         fAvailablePlanks = (turtle.getItemCount(slot:getItemSlot("planks"))) * 15
  5006.     end
  5007.     if slot:getItemSlot("wood") > 0 then
  5008.         fAvailableWood = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 60
  5009.     end
  5010.     tFuelAvailable = fAvailableCoal + fAvailableCharcoal + fAvailablePlanks + fAvailableWood
  5011.     saveToLog("getFuelAvailable: coal = "..fAvailableCoal..
  5012.                 " charcoal = "..fAvailableCharcoal..
  5013.                 " planks = "..fAvailablePlanks..
  5014.                 " wood = "..fAvailableWood, false, true)
  5015.    
  5016.     result = {totalFuelAvailable = tFuelAvailable,
  5017.                     fuelAvailableCoal = fAvailableCoal,
  5018.                     fuelAvailableCharcoal = fAvailableCharcoal,
  5019.                     fuelAvailablePlanks = fAvailablePlanks,
  5020.                     fuelAvailableWood = fAvailableWood,
  5021.                     fuelLevel = fCurrentFuelLevel}
  5022.    
  5023.     return result
  5024. end
  5025.  
  5026. function getItemsFromStorage(arg)
  5027.     -- getItemsFromStorage{fromStore = storageIronore, item1 = "ironore", item2 = "buckets"}
  5028.     -- getItemsFromStorage{fromStore = storageTorches, itemList = getList} -- eg 'sticks,torches'
  5029.     -- getItemsFromStorage{fromStore = storagePickaxe, item1 = "diamond pickaxe"} -- eg 'sticks,torches'
  5030.     local itemInStorage = false
  5031.     local storeSlot = 0
  5032.     local storeCount = 0
  5033.     local storedItem = ""
  5034.     local emptySlot = 0
  5035.     local keepItems = {}
  5036.     local returnItems = {}
  5037.     local numItems = -1
  5038.     local logText = ""
  5039.     local tempChestItem = {}
  5040.     local tempIndex = 0
  5041.     local useCraftingChest = false
  5042.     local keepThis = false
  5043.     local useItem = ""
  5044.     local existingItem = false
  5045.     local start = 0
  5046.     local length = 0
  5047.     local itemRemoved = false
  5048.    
  5049.     arg.direction = arg.direction or "down"
  5050.     --put crafting chest in front if not enough slots for stored items, remove items from storage one by one, either to keep, or transfer to crafting chest
  5051.     --when required items are retrieved, put excess back into storage chest
  5052.     if arg.itemList ~= nil then
  5053.         arg.item1 = arg.itemList[1]
  5054.         arg.item2 = arg.itemList[2]
  5055.         arg.item3 = arg.itemList[3]
  5056.         saveToLog("getItemsFromStorage: itemList[1] = "..tostring(arg.itemList[1]).." itemList[2] = "..tostring(arg.itemList[2]).." itemList[3] = "..tostring(arg.itemList[3]))
  5057.     else
  5058.         -- get no of items
  5059.         for key, value in pairs(arg) do
  5060.             if key == "fromStore" then
  5061.                 saveToLog("getItemsFromStorage: from "..value.getStoreName(value), false)
  5062.             else
  5063.                 saveToLog("getItemsFromStorage: getting "..tostring(value), false)
  5064.             end
  5065.             --key = "fromStore", value = storageTorches
  5066.             --key = "item1", value = "sticks"
  5067.             --key = "itemList", value = ',sticks,torches'
  5068.             numItems = numItems + 1 --starts at -1 minimum 2 args
  5069.         end
  5070.     end
  5071.     keepItems[arg.item1] = 0 --default value of return
  5072.     if arg.item2 ~= nil then
  5073.         keepItems[arg.item2] = 0
  5074.     end
  5075.     if arg.item3 ~= nil then
  5076.         keepItems[arg.item3] = 0
  5077.     end
  5078.     --saveToLog("getItemsFromStorage: checking for "..numItems.." items out of "..arg.fromStore.getNoOfItems(arg.fromStore))
  5079.     storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item1)
  5080.     --check if items needed are in storage
  5081.     if storeSlot > 0 then
  5082.         storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
  5083.         saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item1.." in storage slot "..storeSlot)
  5084.         itemInStorage = true
  5085.     end
  5086.     if arg.item2 ~= nil and arg.item2 ~= "" then
  5087.         storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item2)
  5088.         if storeSlot > 0 then
  5089.             storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
  5090.             saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item2.." in storage slot "..storeSlot)
  5091.             itemInStorage = true
  5092.         end
  5093.     end
  5094.     if arg.item3 ~= nil and arg.item3 ~= "" then
  5095.         storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item3)
  5096.         if storeSlot > 0 then
  5097.             storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
  5098.             saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item3.." in storage slot "..storeSlot)
  5099.             itemInStorage = true
  5100.         end
  5101.     end
  5102.     if itemInStorage then
  5103.         emptySlot = 16
  5104.         --place crafting chest if needed
  5105.         if arg.fromStore.getNoOfItems(arg.fromStore) > getNoOfEmptySlots() then
  5106.             turtle.select(slot.getItemSlot(slot, "chests"))
  5107.             turtle.place()
  5108.             saveToLog("getItemsFromStorage: crafting chest placed ready for "..arg.fromStore.getNoOfItems(arg.fromStore).." items")
  5109.             useCraftingChest = true
  5110.         else
  5111.             saveToLog("getItemsFromStorage: crafting chest not required ("..arg.fromStore.getNoOfItems(arg.fromStore)..") items in storage")
  5112.         end
  5113.         for i = 1, 16 do
  5114.             --use storeContains NOT slotContains
  5115.             storedItem = arg.fromStore:getSlotContains(i) --eg "sticks"
  5116.             if arg.item1 == storedItem or arg.item2 == storedItem  or arg.item3 == storedItem then
  5117.                 keepThis = true
  5118.             else
  5119.                 keepThis = false
  5120.             end
  5121.             storeCount = arg.fromStore:getSlotCount(i)  --eg 1
  5122.             if storeCount == nil then
  5123.                 storeCount = 0
  5124.             end
  5125.             if storeCount > 0 then
  5126.                 saveToLog("getItemsFromStorage: found "..storeCount.." of "..storedItem.." in storage")
  5127.                 existingItem = false
  5128.                 if slot:getItemSlot(storedItem) > 0 then --item already in turtle
  5129.                     emptySlot = slot:getItemSlot(storedItem)
  5130.                     keepThis = true
  5131.                     existingItem = true
  5132.                 end
  5133.                 turtle.select(emptySlot)
  5134.                 saveToLog("getItemsFromStorage: Removing "..storeCount.." "..storedItem.." from  storage chest into turtle slot "..emptySlot)
  5135.                 itemRemoved = false
  5136.                 if arg.direction == "down" then
  5137.                     if turtle.suckDown() then --removed item from storage
  5138.                         itemRemoved = true
  5139.                     end
  5140.                 elseif arg.direction == "forward" then
  5141.                     if turtle.suck() then --removed item from storage
  5142.                         itemRemoved = true
  5143.                     end
  5144.                 else
  5145.                     if turtle.suckUp() then --removed item from storage
  5146.                         itemRemoved = true
  5147.                     end
  5148.                 end
  5149.                
  5150.                 if itemRemoved then
  5151.                     saveToLog("getItemsFromStorage: removed "..turtle.getItemCount(emptySlot).." "..storedItem)
  5152.                     if keepThis then
  5153.                         if not existingItem then
  5154.                             keepItems[storedItem] = turtle.getItemCount(emptySlot)
  5155.                             saveToLog("getItemsFromStorage: "..keepItems[storedItem].." of "..storedItem.." retrieved. Updating slot")
  5156.                             emptySlot = getFirstEmptySlot(false)
  5157.                             if emptySlot > 0 then --turtle not full
  5158.                                 turtle.select(16)
  5159.                                 turtle.transferTo(emptySlot)
  5160.                             else
  5161.                                 emptySlot = 16
  5162.                             end
  5163.                         end
  5164.                         slot.update{self = slot, slotNo = emptySlot, item = storedItem}
  5165.                     else --not required, place in crafting chest if present
  5166.                         if useCraftingChest then
  5167.                             tempIndex = tempIndex + 1
  5168.                             fillChest{direction = "forward", addItem = storedItem, addAmount = storeCount, fromSlot = 16, originalSlot = tempIndex, deleteFromTurtle = false}
  5169.                         else
  5170.                             tempIndex = tempIndex + 1 --started at 0, so first =1
  5171.                             returnItems[tempIndex] = storedItem --returnItems[1] = "iron"
  5172.                             saveToLog("getItemsFromStorage: "..returnItems[tempIndex].." retrieved. Updating slot before returning to storage")
  5173.                             emptySlot = getFirstEmptySlot(false)
  5174.                             if emptySlot > 0 then --turtle not full
  5175.                                 turtle.select(16)
  5176.                                 turtle.transferTo(emptySlot)
  5177.                             else
  5178.                                 emptySlot = 16
  5179.                             end
  5180.                             slot.update{self = slot, slotNo = emptySlot, item = storedItem}
  5181.                         end
  5182.                     end
  5183.                     emptySlot = 16
  5184.                 end
  5185.             else
  5186.                 break
  5187.             end
  5188.         end
  5189.         --reset storage variables
  5190.         -- saveToLog("getItemsFromStorage: resetting storage object", false)
  5191.         arg.fromStore.resetVariables(arg.fromStore)
  5192.         -- now remove items from temp chest/turtle back into storage
  5193.         if tempIndex > 0 then
  5194.             if useCraftingChest then
  5195.                 for i = 1, tempIndex do
  5196.                     useItem = craftingChest:getSlotContains(i) --eg "dirt", "item"
  5197.                     turtle.select(16)
  5198.                     if turtle.suck() then --item removed from crafting chest into slot 16
  5199.                         saveToLog("getItemsFromStorage: returning "..useItem.." to ".. arg.fromStore:getStoreName())
  5200.                         storeItem{toStore = arg.fromStore, item = useItem, slotNo = 16, quantity = turtle.getItemCount(16)}
  5201.                     else
  5202.                         break
  5203.                     end
  5204.                 end
  5205.                 --reset chest variables
  5206.                 craftingChest:resetVariables()
  5207.                 --dig chest back. may not be a chest onboard
  5208.                 dig.digNew{self = dig, direction = "forward", expectedItem = "chests", callFrom = "getItemsFromStorage"}
  5209.             else
  5210.                 for i = 1, tempIndex do
  5211.                     saveToLog("getItemsFromStorage: returning "..returnItems[i].." to "..arg.fromStore:getStoreName())
  5212.                     storeItem{toStore = arg.fromStore, item = returnItems[i], quantity = 0, updateSlot = true, doSort = true}
  5213.                 end
  5214.             end
  5215.         end
  5216.         sortInventory(true)
  5217.     else
  5218.         saveToLog("getItemsFromStorage: itemInStorage = false")
  5219.     end
  5220.    
  5221.     return keepItems
  5222. end
  5223.  
  5224. function getNoOfEmptySlots()
  5225.     local result = 0
  5226.    
  5227.     for i = 1, 16 do
  5228.         if turtle.getItemCount(i) == 0 then
  5229.             result = result + 1
  5230.         end
  5231.     end
  5232.    
  5233.     return result
  5234. end
  5235.  
  5236. function getNoOfValuableSlots()
  5237.     local result = 0
  5238.    
  5239.     for i = 1, 16 do
  5240.         if string.find(slot:getSlotContains(i),"item") ~= nil then
  5241.             result = result + 1
  5242.         end
  5243.     end
  5244.     if slot:getItemSlot("redstone") > 0 then
  5245.         result = result + 1
  5246.     end
  5247.     if slot:getItemSlot("lapis") > 0 then
  5248.         result = result + 1
  5249.     end
  5250.     if slot:getItemSlot("coal") > 0 then
  5251.         result = result + 1
  5252.     end
  5253.     if slot:getItemSlot("coal-1") > 0 then
  5254.         result = result + 1
  5255.     end
  5256.     if slot:getItemSlot("coal-2") > 0 then
  5257.         result = result + 1
  5258.     end
  5259.    
  5260.     return result
  5261. end
  5262.  
  5263. function getPastebin(name, code)
  5264.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))
  5265.    
  5266.     if response then
  5267.         local sCode = response.readAll()
  5268.         if sCode ~= nil and sCode ~= "" then
  5269.             local file = fs.open(name, "w")
  5270.             response.close()
  5271.             file.write(sCode)
  5272.             file.close()
  5273.             return true
  5274.         end
  5275.     end
  5276.    
  5277.     return false
  5278. end    
  5279.  
  5280. function getPreferences()
  5281.     currentFunction = "getPreferences"
  5282.     callingFunction = "main"
  5283.     if not stage["getPreferences"] then
  5284.         local response = ""
  5285.         local event = ""
  5286.         local param1 = ""
  5287.         local useIniFile = false
  5288.        
  5289.         term.clear()
  5290.         term.setCursorPos(1,1)
  5291.         if fso:iniFileExists() then --read preferences, so only one question asked before starting
  5292.             print("Current settings:")
  5293.             print()
  5294.             print("  Playing on a server       = "..tostring(fso:getOnServer()))
  5295.             print("  Create logfiles           = "..tostring(fso:getUseLog()))
  5296.             print("  Use verbose logfiles      = "..tostring(fso:getUseVerbose()))
  5297.             print("  Use pastebin for logfiles = "..tostring(fso:getUsePastebin()))
  5298.            
  5299.             print()
  5300.             print("Press E to edit")
  5301.             print()
  5302.             print("Any other key to continue")
  5303.             event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
  5304.             term.clear()
  5305.             term.setCursorPos(1,1)
  5306.             if param1 == "e" or param1 == "E" then -- if the returned value was 'e'
  5307.                 fso:deleteIniFile()
  5308.             else
  5309.                 useIniFile = true
  5310.             end
  5311.         end
  5312.         if not useIniFile then -- no prefs loaded from file, or file deleted
  5313.             fso:setOnServer(false)
  5314.             fso:setUsePastebin(false)
  5315.             fso:setUseLog(false)
  5316.            
  5317.             term.clear()
  5318.             term.setCursorPos(1,1)
  5319.             print("superminer.lua version: "..version)
  5320.             print()
  5321.             print("Are you playing on a server? Y/N")
  5322.             event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
  5323.             if param1 == "y" or param1 == "Y" then -- if the returned value was 'y'
  5324.                 fso:setOnServer(true)
  5325.                 term.clear()
  5326.                 term.setCursorPos(1,1)
  5327.                 print("Do you want to send logfiles")
  5328.                 print("to your pastebin account? Y/N")
  5329.                 event, param1 = os.pullEvent ("char")
  5330.                 term.clear()
  5331.                 term.setCursorPos(1,1)
  5332.                 if param1 == "y" or param1 == "Y" then
  5333.                     print("Sorry, pastebin is work in progress")
  5334.                     print("No logfiles will be produced")
  5335.                 else
  5336.                     print("No logfiles will be produced")
  5337.                 end
  5338.                 print()
  5339.                 print("Preparing to start....")
  5340.                 sleep(4)
  5341.                 --break
  5342.             else --not on server
  5343.                 term.clear()
  5344.                 term.setCursorPos(1,1)
  5345.                 print("Logfiles can be created for debugging")
  5346.                 print("Use only if CC config file is modified")
  5347.                 print("to I:computerSpaceLimit=10000000")
  5348.                 print()
  5349.                 print("Press 'V' for verbose logfiles")
  5350.                 print()
  5351.                 print("Press 'M' for minimal logfiles")
  5352.                 print()
  5353.                 print("Any other key for default (None)")
  5354.                 print()
  5355.                 event, param1 = os.pullEvent ("char")
  5356.                 if param1 == "v" or param1 == "V" then
  5357.                     fso:setUseVerbose(true)
  5358.                     fso:setUseLog(true)
  5359.                     print("verbose logfiles will be created")
  5360.                 elseif param1 == "m" or param1 == "M" then
  5361.                     fso:setUseVerbose(false)
  5362.                     fso:setUseLog(true)
  5363.                     print("minimal logfiles will be created")
  5364.                 else
  5365.                     print("logfiles will not be created")
  5366.                 end
  5367.                 sleep(2)
  5368.                 if fso:checkLogExists() then
  5369.                     term.clear()
  5370.                     term.setCursorPos(1,1)
  5371.                     print("Logfile(s) already exist")
  5372.                     print()
  5373.                     print("Press 'Y' to keep existing logs")
  5374.                     print("Any other key to delete logs")
  5375.                     event, param1 = os.pullEvent ("char")
  5376.                     if param1 == "y" or param1 == "Y" then
  5377.                         fso:renameLog()
  5378.                     else
  5379.                         fso:deleteLog()
  5380.                     end
  5381.                 end
  5382.             end
  5383.         end
  5384.         stage["getPreferences"] = true
  5385.         saveStatus()
  5386.         saveToLog("Completed preferences registration. LogFiles started", true)
  5387.         displaySaveBackup()
  5388.     end
  5389. end
  5390.  
  5391. function getReady()
  5392.     currentFunction = "getReady"
  5393.     callingFunction = "main"
  5394.     local numChests = 0
  5395.     local findCobbleLevel = 0
  5396.     --[[
  5397.     1 chest slot 1
  5398.     1 dirt slot 2
  5399.     1 cobble slot 3
  5400.     1 stone slot 4
  5401.     1 gravel slot 5
  5402.     1 bucket slot 6
  5403.     64 wood slot 7
  5404.     ]]--
  5405.     if not stage["getReady"] then
  5406.         saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
  5407.                     " to logGetReady.txt END OF LOGFILE!", false, false)
  5408.         fso:useFileName("logGetReady.txt")
  5409.         saveToLog("getReady: starting", false, false)
  5410.         if fso:recoveryFileExists() then --use file to update slot object
  5411.             saveToLog("using recovery file")
  5412.         else
  5413.             saveToLog("Initialising slot object")
  5414.             slot.update{self = slot, slotNo = 1, item = "chests"}
  5415.             slot.update{self = slot, slotNo = 2, item = "dirt"}
  5416.             slot.update{self = slot, slotNo = 3, item = "cobble"}
  5417.             slot.update{self = slot, slotNo = 4, item = "stone"}
  5418.             slot.update{self = slot, slotNo = 5, item = "gravel"}
  5419.             slot.update{self = slot, slotNo = 6, item = "buckets"}
  5420.             slot.update{self = slot, slotNo = 7, item = "wood"}
  5421.             slot.update{self = slot, slotNo = 8, item = "wood-1"}
  5422.         end
  5423.         sortInventory(true)
  5424.  
  5425.         -- wood = 2 x 64. craft will check area in front is clear, else move around
  5426.         -- make chests
  5427.         numChests = getStock("chests")
  5428.         if not stage["craftChests"] then
  5429.             if numChests < 25 then
  5430.                 repeat
  5431.                     if craft{calledFrom = "getReady", craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood-1", destSlot = 0, doSort = false} then
  5432.                         saveToLog("Crafted 64 planks.")
  5433.                     else
  5434.                         saveToLog("Crafting planks did not succeed")
  5435.                         error()
  5436.                     end
  5437.                     if craft{calledFrom = "getReady", craftItem = "chests", craftQuantity = 8, sourceItem1 = "planks", destSlot = 0, doSort = false} then
  5438.                         saveToLog("Crafted 8 chests.")
  5439.                     else
  5440.                         saveToLog("Crafting chests did not succeed")
  5441.                         error()
  5442.                     end
  5443.                 until  getStock("chests") >= 25
  5444.             end
  5445.             -- 3 x 8 chests crafted, used 48 wood-1 = 16 left
  5446.             stage["craftChests"] = true
  5447.         end
  5448.         saveStatus()
  5449.         if not stage["craftLadders"] then
  5450.             for i =1, 2 do
  5451.                 craftLadders(27)
  5452.                 turtle.select(slot:getItemSlot("sticks"))
  5453.                 turtle.refuel()
  5454.                 slot.update{self = slot, item = "sticks", delete = true}
  5455.             end
  5456.             stage["craftSticks"] = true
  5457.             stage["craftLadders"] = true
  5458.         end
  5459.         saveStatus()
  5460.         -- 0 wood-1, 64 wood, 54 ladders, 0 sticks
  5461.         if not stage["craftSigns"] then
  5462.             if getStock("signs") == 0 then
  5463.                 craftSigns(9)
  5464.             end
  5465.             stage["craftSigns"] = true
  5466.         end
  5467.         -- 62 wood, 54 ladders, 3 signs, 3 sticks
  5468.         if getStock("wood-1") > 0 then
  5469.             if craft{calledFrom = "getReady", craftItem = "planks", craftQuantity = getStock("wood-1") * 4, sourceItem1 = "wood-1", destSlot = 0, doSort = true} then
  5470.                 saveToLog("Crafted 32 planks.")
  5471.             else
  5472.                 saveToLog("Crafting planks did not succeed")
  5473.                 error()
  5474.             end
  5475.             turtle.select(slot:getItemSlot("planks"))
  5476.             turtle.refuel()
  5477.             slot.update{self = slot, item = "planks", delete = true}
  5478.             sortInventory(true)
  5479.         end
  5480.         -- craft planks for fuel
  5481.         if craft{calledFrom = "getReady", craftItem = "planks", craftQuantity = 32, sourceItem1 = "wood", destSlot = 0, doSort = true} then
  5482.             saveToLog("Crafted 32 planks.")
  5483.         else
  5484.             saveToLog("Crafting planks did not succeed")
  5485.             error()
  5486.         end
  5487.         -- 56 wood left, 54 ladders, 32 planks, 3 signs, 3 sticks
  5488.    
  5489.         if slot:getItemSlot("planks") > 0 then
  5490.             turtle.select(slot:getItemSlot("planks"))
  5491.             turtle.refuel()
  5492.             slot.update{self = slot, item = "planks", delete = true}
  5493.             sortInventory(true)
  5494.         end
  5495.         stage["getReady"] = true
  5496.         saveStatus()
  5497.         displaySaveBackup()
  5498.     end
  5499. end
  5500.  
  5501. function getSticks()
  5502.     local useSlot = 0
  5503.    
  5504.     currentFunction = "getSticks"
  5505.     changeDirection("faceForward")
  5506.     go("FFRFF")
  5507.     saveToLog("getSticks: removing sticks from storage", true, false)
  5508.     if getStock("sticks") > 0 then
  5509.         useSlot = slot:getItemSlot("sticks")
  5510.     else
  5511.        useSlot = getFirstEmptySlot(false)
  5512.     end
  5513.     if turtle.suckDown() then --sticks found
  5514.         slot.update{self = slot, slotNo = useSlot, item = "sticks"}
  5515.     end
  5516.  
  5517.     go("RRFFLFFRR")
  5518. end
  5519.  
  5520. function getStock(ofItem)
  5521.     local result = 0
  5522.    
  5523.     if slot:getItemSlot(ofItem) > 0 then
  5524.         result = turtle.getItemCount(slot:getItemSlot(ofItem))
  5525.     end
  5526.    
  5527.     return result
  5528. end
  5529.  
  5530. function getSupplies()
  5531.     currentFunction = "getSupplies"
  5532.     callingFunction = "main"
  5533.     local gotChest = false
  5534.     local gotDirt = false
  5535.     local gotCobble = false
  5536.     local gotStone = false
  5537.     local gotGravel = false
  5538.     local gotBucket = false
  5539.     local gotWood = false
  5540.     local gotWood2 = false
  5541.     local extraSlot = 0
  5542.     local fromRecoveryFile = false
  5543.    
  5544.     if not stage["getSupplies"] then
  5545.         saveToLog("getSupplies: started", false, true)
  5546.         repeat
  5547.             extraSlot = 0
  5548.             for i = 9, 16 do
  5549.                 if turtle.getItemCount(i) > 0 then
  5550.                     extraSlot = i
  5551.                     break
  5552.                 end
  5553.             end
  5554.             term.clear()
  5555.             term.setCursorPos(1,1)
  5556.             if extraSlot > 0 then
  5557.                 print("Remove items from slot "..extraSlot.."!")
  5558.                 print()
  5559.             else
  5560.                 print("Add the following supplies:")
  5561.                 print()
  5562.                 if not gotChest then
  5563.                     if turtle.getItemCount(1) == 0 then
  5564.                         print(" 1   chest  in slot 1")
  5565.                     else --may be reboot, and chest already present
  5566.                         turtle.select(1)
  5567.                         if turtle.refuel(0) then
  5568.                             gotChest = true
  5569.                             saveToLog("getSupplies: chest added to slot 1", false, true)
  5570.                         else
  5571.                             print(" NOT a chest in slot 1! Try again")
  5572.                             saveToLog("getSupplies: player put non-combustible item in slot 1", false, true)
  5573.                         end
  5574.                     end
  5575.                 else -- player has removed item
  5576.                     if turtle.getItemCount(1) ~= 1 then
  5577.                         gotChest = false
  5578.                         print(" 1   chest  in slot 1")
  5579.                         -- print("Add the following supplies:")
  5580.                         -- print()
  5581.                     end
  5582.                 end
  5583.                
  5584.                 if not gotDirt then
  5585.                     if turtle.getItemCount(2) == 0 then
  5586.                         print(" 1   dirt   in slot 2")
  5587.                     else
  5588.                         gotDirt = true
  5589.                         saveToLog("getSupplies: dirt added to slot 2", false, true)
  5590.                     end
  5591.                 else
  5592.                     if turtle.getItemCount(2) ~= 1 then
  5593.                         gotDirt = false
  5594.                         print(" 1   dirt   in slot 2")
  5595.                         --print("Add the following supplies:")
  5596.                         --print()
  5597.                     end
  5598.                 end
  5599.                
  5600.                 if not gotCobble then
  5601.                     if turtle.getItemCount(3) == 0 then
  5602.                         print(" 1   cobble in slot 3")
  5603.                     else
  5604.                         gotCobble = true
  5605.                         saveToLog("getSupplies: cobble added to slot 3", false, true)
  5606.                     end
  5607.                 else
  5608.                     if turtle.getItemCount(3) ~= 1 then
  5609.                         gotCobble = false
  5610.                         print(" 1   cobble in slot 3")
  5611.                         --print("Add the following supplies:")
  5612.                         --print()
  5613.                     end
  5614.                 end
  5615.                
  5616.                 if not gotStone then
  5617.                     if turtle.getItemCount(4) == 0 then
  5618.                         print(" 1   stone  in slot 4")
  5619.                     else
  5620.                         gotStone = true
  5621.                         saveToLog("getSupplies: stone added to slot 4", false, true)
  5622.                     end
  5623.                 else
  5624.                     if turtle.getItemCount(4) ~= 1 then
  5625.                         gotStone = false
  5626.                         print(" 1   stone  in slot 4")
  5627.                         --print("Add the following supplies:")
  5628.                         --print()
  5629.                     end
  5630.                 end
  5631.                
  5632.                 if not gotGravel then
  5633.                     if turtle.getItemCount(5) == 0 then
  5634.                         print(" 1   gravel in slot 5")
  5635.                     else
  5636.                         gotGravel = true
  5637.                         saveToLog("getSupplies: gravel added to slot 5", false, true)
  5638.                     end
  5639.                 else
  5640.                     if turtle.getItemCount(5) ~= 1 then
  5641.                         gotGravel = false
  5642.                         print(" 1   gravel in slot 5")
  5643.                         --print("Add the following supplies:")
  5644.                         --print()
  5645.                     end
  5646.                 end
  5647.                
  5648.                 if not gotBucket then
  5649.                     if turtle.getItemCount(6) == 0 then
  5650.                         print(" 1   bucket in slot 6")
  5651.                     else
  5652.                         gotBucket = true
  5653.                         saveToLog("getSupplies: bucket added to slot 6", false, true)
  5654.                     end
  5655.                 else
  5656.                     if turtle.getItemCount(1) ~= 1 then
  5657.                         gotBucket = false
  5658.                         print(" 1   bucket in slot 6")
  5659.                         --print("Add the following supplies:")
  5660.                         --print()
  5661.                     end
  5662.                 end
  5663.                
  5664.                 if turtle.getItemCount(7) == 0 then
  5665.                     print(" 64  wood   in slot 7")
  5666.                 else
  5667.                     turtle.select(7)
  5668.                     if turtle.refuel(0) then
  5669.                         if turtle.getItemCount(7) == 64 then
  5670.                             if not gotWood then
  5671.                                 saveToLog("getSupplies: 64 wood added to slot 7", false, true)
  5672.                             end
  5673.                             gotWood = true
  5674.                         else
  5675.                             print(" MORE wood needed in slot 7!")
  5676.                         end
  5677.                     else
  5678.                         print(" NOT wood in slot 7! Try again")
  5679.                     end
  5680.                 end
  5681.                
  5682.                 if turtle.getItemCount(8) == 0 then
  5683.                     print(" 64  wood   in slot 8")
  5684.                 else
  5685.                     turtle.select(8)
  5686.                     if turtle.refuel(0) then
  5687.                         if turtle.getItemCount(8) == 64 then
  5688.                             if not gotWood2 then
  5689.                                 saveToLog("getSupplies: 64 wood added to slot 8", false, true)
  5690.                             end
  5691.                             gotWood2 = true
  5692.                         else
  5693.                             print(" MORE wood needed in slot 8!")
  5694.                         end
  5695.                     else
  5696.                         print(" NOT wood in slot 8! Try again")
  5697.                     end
  5698.                 end
  5699.             end
  5700.             sleep(1)
  5701.         until gotChest and gotDirt and gotCobble and gotStone and gotGravel and gotWood and gotWood2
  5702.         stage["getSupplies"] = true
  5703.         saveStatus()
  5704.         saveToLog("getSupplies: completed", false, true)
  5705.         displaySaveBackup()
  5706.     end
  5707. end
  5708.  
  5709. function go(path, itemList)
  5710.     local previousCallingFunction = callingFunction
  5711.     local previousFunction = currentFunction
  5712.     local digDirection = "forward"
  5713.     local itemSelected = false
  5714.     itemList = itemList or "coal,redstone,lapis"
  5715.    
  5716.     currentFunction = "go"
  5717.     -- R L F B U D = Right, Left, Forward, Back, Up, Down
  5718.     -- d u x = dig, digUp, digDown (x-cavate ?!)
  5719.     -- C S T = place Cobble, Stairs, Torch
  5720.     saveToLog("go: (called from "..callingFunction..") "..path.." at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
  5721.     for i = 1, string.len(path) do
  5722.         move = string.sub(path, i, i)
  5723.         if move == "R" then
  5724.             turnRight(1)
  5725.             -- saveToLog("go: turnRight. Facing "..location:getCompass())
  5726.         elseif move == "L" then
  5727.             turnLeft(1)
  5728.             -- saveToLog("go: turnLeft. Facing "..location:getCompass())
  5729.         elseif move == "B" then
  5730.             back(1, 1)
  5731.             -- saveToLog("go: back 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
  5732.         elseif move == "F" then
  5733.             forward(1, 1, itemList, "go")
  5734.             -- saveToLog("go: forward 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
  5735.         elseif move == "u" then
  5736.             digDirection = "up"
  5737.             while turtle.detectUp() do
  5738.                 if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
  5739.                     --allow for sand and gravel
  5740.                     sleep(.7)
  5741.                 end
  5742.             end
  5743.         elseif move == "U" then
  5744.             up(1, 1, itemList, "go")
  5745.             -- saveToLog("go: up 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
  5746.         elseif move == "d" then
  5747.             digDirection = "forward"
  5748.             while turtle.detect() do
  5749.                 if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
  5750.                     --allow for sand and gravel
  5751.                     sleep(.7)
  5752.                 end
  5753.             end
  5754.         elseif move == "D" then
  5755.             down(1, 1, itemList, "go")
  5756.             -- saveToLog("go: down 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
  5757.         elseif move == "x" then
  5758.             digDirection = "down"
  5759.             dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5760.         elseif move == "C" then
  5761.             if digDirection == "forward" then
  5762.                 if turtle.detect() then
  5763.                     while turtle.detect() do
  5764.                         if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
  5765.                             --allow for sand and gravel
  5766.                             sleep(.7)
  5767.                         end
  5768.                     end
  5769.                 end
  5770.             elseif digDirection == "up" then
  5771.                 if turtle.detectUp() then
  5772.                     while turtle.detectUp() do
  5773.                         if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
  5774.                             --allow for sand and gravel
  5775.                             sleep(.7)
  5776.                         end
  5777.                     end
  5778.                 end
  5779.             elseif digDirection == "down" then
  5780.                 if turtle.detectDown() then
  5781.                     dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5782.                 end
  5783.             end
  5784.             itemSelected = false
  5785.             if getStock("cobble") > 1 then
  5786.                 turtle.select(slot:getItemSlot("cobble"))
  5787.                 itemSelected = true
  5788.             elseif getStock("dirt") > 1 then
  5789.                 turtle.select(slot:getItemSlot("dirt"))
  5790.                 itemSelected = true
  5791.             end
  5792.             if itemSelected then
  5793.                 if digDirection == "forward" then
  5794.                     turtle.place()
  5795.                 elseif digDirection == "up" then
  5796.                     turtle.placeUp()
  5797.                 elseif digDirection == "down" then
  5798.                     turtle.placeDown()
  5799.                 end
  5800.             end
  5801.         elseif move == "S" then
  5802.             if digDirection == "forward" then
  5803.                 if turtle.detect() then
  5804.                     dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5805.                 end
  5806.             elseif digDirection == "up" then
  5807.                 if turtle.detectUp() then
  5808.                     dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5809.                 end
  5810.             elseif digDirection == "down" then
  5811.                 if turtle.detectDown() then
  5812.                     dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5813.                 end
  5814.             end
  5815.             if getStock("stairs") > 0 then
  5816.                 turtle.select(slot:getItemSlot("stairs"))
  5817.                 if digDirection == "forward" then
  5818.                     turtle.place()
  5819.                 elseif digDirection == "up" then
  5820.                     turtle.placeUp()
  5821.                 elseif digDirection == "down" then
  5822.                     turtle.placeDown()
  5823.                 end
  5824.             end
  5825.         elseif move == "T" then
  5826.             if digDirection == "forward" then
  5827.                 if turtle.detect() then
  5828.                     dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5829.                 end
  5830.             elseif digDirection == "up" then
  5831.                 if turtle.detectUp() then
  5832.                     dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5833.                 end
  5834.             elseif digDirection == "down" then
  5835.                 if turtle.detectDown() then
  5836.                     dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
  5837.                 end
  5838.             end
  5839.             if getStock("torches") > 1 then
  5840.                 turtle.select(slot:getItemSlot("torches"))
  5841.                 if digDirection == "forward" then
  5842.                     turtle.place()
  5843.                 elseif digDirection == "up" then
  5844.                     turtle.placeUp()
  5845.                 elseif digDirection == "down" then
  5846.                     turtle.placeDown()
  5847.                 end
  5848.             end
  5849.         end
  5850.     end
  5851.     saveToLog("go: "..path.." completed at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
  5852.     callingFunction = previousCallingFunction
  5853.     currentFunction = previousFunction
  5854. end
  5855.  
  5856. function goToWoodStore(fromResume)
  5857.     currentFunction = "goToWoodStore"
  5858.     callingFunction = "goToWoodStore"
  5859.     local emptySlot = 0
  5860.     local woodSlot = 0
  5861.     local waitTime = 0
  5862.    
  5863.     if not fromResume then
  5864.         changeDirection("faceForward")
  5865.         if getStock("wood") >= 2 then
  5866.             saveToLog("goToWoodStore: converting excess wood to planks/chests", true, false)
  5867.             craft{calledFrom = "goToWoodStore", craftItem = "planks", craftQuantity = getStock("wood") * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
  5868.             if getStock("planks") > 8 then
  5869.                 craft{calledFrom = "goToWoodStore", craftItem = "chests", craftQuantity = math.floor(getStock("planks") / 8), sourceItem1 = "planks", destSlot = 0, doSort = true}
  5870.             end
  5871.             if slot:getItemSlot("planks") > 0 then
  5872.                 saveToLog("goToWoodStore: converting excess planks to fuel", true, false)
  5873.                 turtle.select(slot:getItemSlot("planks"))
  5874.                 turtle.refuel()
  5875.                 slot.update{self = slot, item = "planks", delete = true}
  5876.                 sortInventory(true)
  5877.             end
  5878.         end
  5879.        
  5880.         forward(2)
  5881.     end
  5882.     emptySlot = getFirstEmptySlot()
  5883.     turtle.select(emptySlot)
  5884.  
  5885.     if turtle.suckDown() then -- wood removed from chest
  5886.         saveToLog("goToWoodStore: wood removed from chest", true, false)
  5887.         slot.update{self = slot, slotNo = emptySlot, item = "wood"}
  5888.     else
  5889.         while not turtle.suckDown() do
  5890.             term.clear()
  5891.             term.setCursorPos(1,1)
  5892.             print("Please add wood to the chest:")
  5893.             print()
  5894.             print("I have been waiting for "..waitTime.." seconds")
  5895.             print()
  5896.             print("Checking for wood in 10 seconds")
  5897.             sleep(10)
  5898.             waitTime = waitTime + 10
  5899.         end
  5900.     end
  5901.     saveToLog("goToWoodStore: wood removed from chest", true, false)
  5902.     slot.update{self = slot, slotNo = emptySlot, item = "wood"}
  5903.     turnRight(2)
  5904.     forward(2)
  5905.     changeDirection("faceForward")
  5906. end
  5907.  
  5908. function isCeilingOk()
  5909.     local itemlist = {}
  5910.     local success = false
  5911.     local previousFunction = currentFunction
  5912.    
  5913.     currentFunction = "isCeilingOk"
  5914.  
  5915.     itemlist[1] = "dirt"
  5916.     itemlist[2] = "stone"
  5917.     itemlist[3] = "cobble"
  5918.    
  5919.     if turtle.detectUp() then
  5920.         for i = 1, 3 do
  5921.             turtle.select(slot.getItemSlot(slot,itemlist[i])) -- select compare blocks
  5922.             if turtle.compareUp() then -- compare block above to dirt, stone, gravel in turn
  5923.                 success = true
  5924.             end
  5925.         end
  5926.     else
  5927.         success = true -- air
  5928.     end
  5929.    
  5930.     currentFunction = previousFunction
  5931.    
  5932.     return success --true only if block matches those in selected slots eg dirt, stone, cobble
  5933. end
  5934.  
  5935. function isFloorOk()
  5936.     local itemlist = {}
  5937.     local success = false
  5938.     local previousFunction = currentFunction
  5939.    
  5940.     currentFunction = "isFloorOk"
  5941.  
  5942.     itemlist[1] = "dirt"
  5943.     itemlist[2] = "stone"
  5944.     itemlist[3] = "cobble"
  5945.    
  5946.     if turtle.detectDown() then
  5947.         for i = 1, 3 do
  5948.             turtle.select(slot.getItemSlot(slot,itemlist[i])) -- select compare blocks
  5949.             if turtle.compareDown() then -- compare block below to dirt, stone, gravel in turn
  5950.                 success = true
  5951.             end
  5952.         end
  5953.     else
  5954.         success = true -- air
  5955.     end
  5956.    
  5957.     currentFunction = previousFunction
  5958.    
  5959.     return success --true only if block matches those in selected slots eg dirt, stone, cobble, or no block present
  5960. end
  5961.  
  5962. function isValuable(direction)
  5963.     local itemlist = {}
  5964.     local success = true
  5965.     local blockType = ""
  5966.     local previousFunction = currentFunction
  5967.    
  5968.     currentFunction = "isValuable"
  5969.  
  5970.     itemlist[1] = "dirt"
  5971.     itemlist[2] = "stone"
  5972.     itemlist[3] = "gravel"
  5973.     itemlist[4] = "cobble"
  5974.     itemlist[5] = "sand"
  5975.     itemlist[6] = "torches"
  5976.     if direction == "up" then
  5977.         if turtle.detectUp() then
  5978.             for i = 1, 6 do
  5979.                 if slot.getItemSlot(slot,itemlist[i]) > 0 then--eg gravel may not be known
  5980.                     turtle.select(slot.getItemSlot(slot,itemlist[i])) -- select compare blocks
  5981.                     if turtle.compareUp() then -- compare block above to dirt, stone, gravel in turn
  5982.                         success = false
  5983.                         blockType = itemlist[i]
  5984.                     end
  5985.                 end
  5986.             end
  5987.         else
  5988.             success = false
  5989.         end
  5990.     elseif direction == "down" then
  5991.         if turtle.detectDown() then
  5992.             for i = 1, 6 do
  5993.                 if slot.getItemSlot(slot, itemlist[i]) > 0 then--eg gravel may not be known
  5994.                     turtle.select(slot.getItemSlot(slot, itemlist[i])) -- select compare blocks
  5995.                     if turtle.compareDown() then -- compare block below to selected
  5996.                         success = false
  5997.                         blockType = itemlist[i]
  5998.                     end
  5999.                 end
  6000.             end
  6001.         else
  6002.             success = false
  6003.         end
  6004.     elseif direction == "side" then
  6005.         if turtle.detect() then
  6006.             for i = 1, 6 do
  6007.                 if slot:getItemSlot(itemlist[i]) > 0 then--eg gravel may not be known
  6008.                     turtle.select(slot:getItemSlot(itemlist[i])) -- select compare blocks
  6009.                     if turtle.compare() then -- compare front block to selected
  6010.                         success = false
  6011.                         blockType = itemlist[i]
  6012.                         break
  6013.                     end
  6014.                 end
  6015.             end
  6016.         else
  6017.             success = false
  6018.         end
  6019.     end
  6020.    
  6021.     currentFunction = previousFunction
  6022.     return success, blockType --true only if block does not match those in selected slots eg dirt, stone, gravel, cobble
  6023. end
  6024.  
  6025. function loadStatus()
  6026.     local numSlots = 0
  6027.     local wholeTable = {}
  6028.     local sContains = {}
  6029.     local sCount = {}
  6030.     local sSlot = {}
  6031.     local sTurtleSlot = {}
  6032.     local wholeFile = ""
  6033.     local curLine = ""
  6034.     local numTurns = 0
  6035.     local emptySlot = 0
  6036.     local startLevel = 0
  6037.     local diamondStartLevel = 15
  6038.     local levelSequence = {}
  6039.     local numLevels = 0
  6040.     local miningStarted = false
  6041.     local functionList = {}
  6042.     local onGroundLevel = false
  6043.    
  6044.     functionList = {"initialise", "getPreferences", "getSupplies", "getReady", "clearBase"}
  6045.  
  6046.     -- read saved status variables from file
  6047.     local handle = fs.open("superminer.recover", "r")
  6048.     wholeFile = handle.readAll()
  6049.     handle.close()
  6050.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6051.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6052.     --save current logfile with new name, then continue with new version
  6053.     fso:renameLastLogFile(tonumber(curLine))
  6054.     fso:setStartFileNameIndex(tonumber(curLine))
  6055.     saveToLog("recover: fso:setStartFileNameIndex = "..fso:getStartFileNameIndex(),false, false)
  6056.    
  6057.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6058.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6059.     location:setX(tonumber(curLine))
  6060.     saveToLog("recover: Current location x = "..location:getX(),false, false)
  6061.    
  6062.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6063.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6064.     location:setY(tonumber(curLine))
  6065.     saveToLog("recover: Current location y = "..location:getY(),false, false)
  6066.    
  6067.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6068.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6069.     location:setZ(tonumber(curLine))
  6070.     saveToLog("recover: Current location z = "..location:getZ(),false, false)
  6071.    
  6072.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6073.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6074.     location:setFacing(tonumber(curLine))
  6075.     saveToLog("recover: Current location facing = "..location:getFacing(),false, false)
  6076.    
  6077.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6078.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6079.     coordHome:setX(tonumber(curLine))
  6080.     saveToLog("recover: Home location x = "..coordHome:getX(),false, false)
  6081.    
  6082.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6083.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6084.     coordHome:setY(tonumber(curLine))
  6085.     saveToLog("recover: Home location y = "..coordHome:getY(),false, false)
  6086.    
  6087.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6088.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6089.     coordHome:setZ(tonumber(curLine))
  6090.     saveToLog("recover: Home location z = "..coordHome:getZ(),false, false)
  6091.    
  6092.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6093.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6094.     coordHome:setFacing(tonumber(curLine))
  6095.     saveToLog("recover: Home location facing = "..coordHome:getFacing(),false, false)
  6096.    
  6097.    
  6098.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6099.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6100.     mineTopLevel:setX(tonumber(curLine))
  6101.     saveToLog("recover: mineTopLevel x = "..mineTopLevel:getX(),false, false)
  6102.    
  6103.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6104.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6105.     mineTopLevel:setY(tonumber(curLine))
  6106.     saveToLog("recover: mineTopLevel y = "..mineTopLevel:getY(),false, false)
  6107.    
  6108.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6109.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6110.     mineTopLevel:setZ(tonumber(curLine))
  6111.     saveToLog("recover: mineTopLevel z = "..mineTopLevel:getZ(),false, false)
  6112.    
  6113.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6114.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6115.     mineTopLevel:setFacing(tonumber(curLine))
  6116.     saveToLog("recover: mineTopLevel facing = "..mineTopLevel:getFacing(),false, false)
  6117.    
  6118.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6119.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6120.     currentFunction = curLine
  6121.     saveToLog("recover: current function = "..currentFunction, false, false)
  6122.    
  6123.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6124.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6125.     callingFunction = curLine
  6126.     saveToLog("recover: calling function = "..callingFunction, false, false)
  6127.    
  6128.     --curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6129.     --wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6130.     --floorToCeiling = (curLine == "true")
  6131.     --saveToLog("recover: floorToCeiling = "..tostring(floorToCeiling), false, false)
  6132.    
  6133.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6134.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6135.     diamondsOnly = (curLine == "true")
  6136.     saveToLog("recover: diamondsOnly = "..tostring(diamondsOnly), false, false)
  6137.    
  6138.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
  6139.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
  6140.     diamondsFirst = (curLine == "true")
  6141.     saveToLog("recover: diamondsFirst = "..tostring(diamondsFirst), false, false)
  6142.     --get slot object
  6143.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"}\n"))
  6144.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"}\n") + 2)
  6145.     wholeTable = textutils.unserialize(curLine)
  6146.     slot:setStatus(wholeTable)
  6147.     slot:printSlotContents()
  6148.     --get stage table
  6149.     curLine = string.sub(wholeFile, 1, string.find(wholeFile,"}\n"))
  6150.     wholeFile = string.sub(wholeFile, string.find(wholeFile,"}\n") + 2)
  6151.     stage = textutils.unserialize(curLine)
  6152.     --get chest object
  6153.     curLine = wholeFile
  6154.     craftingChest:setStatus(textutils.unserialize(curLine))
  6155.     craftingChest:printContents()
  6156.    
  6157.     --[[
  6158.         0 = go south (z increases)
  6159.         1 = go west  (x decreases)
  6160.         2 = go north (z decreases
  6161.         3 = go east  (x increases)
  6162.     ]]--
  6163.     startLevel = mineTopLevel:getY()
  6164.     if diamondsFirst or diamondsOnly then --start at level 15 or lower
  6165.         if startLevel > 15 then
  6166.             levelSequence[1] = 15
  6167.             levelSequence[2] = 12
  6168.             levelSequence[3] = 9
  6169.             levelSequence[4] = 6
  6170.             levelIndex = 4
  6171.             for i = startLevel, 18, -3 do
  6172.                 levelIndex = levelIndex + 1
  6173.                 levelSequence[levelIndex] = i
  6174.             end
  6175.             -- eg 15,12,9,6,51,48,45,42...21,18
  6176.         else
  6177.             for i = startLevel, 6, -3 do
  6178.                 levelIndex = levelIndex + 1
  6179.                 levelSequence[levelIndex] = i
  6180.             end
  6181.             -- eg 12,9,6
  6182.         end
  6183.     else
  6184.         for i = startLevel, 6, -3 do
  6185.             levelIndex = levelIndex + 1
  6186.             levelSequence[levelIndex] = i
  6187.         end
  6188.         -- eg 51,48,45,42...15,12,9,6
  6189.     end
  6190.     numLevels = levelIndex
  6191.     levelIndex = 1
  6192.     if stage["createBase"..tostring(levelSequence[1])] then --mining started
  6193.         repeat
  6194.             level = levelSequence[levelIndex]
  6195.             if not stage["createBase"..tostring(level)] then --mineXX not completed
  6196.                 break
  6197.             end
  6198.             levelIndex = levelIndex + 1
  6199.         until levelIndex > numLevels
  6200.         level = levelSequence[levelIndex - 1]
  6201.         saveToLog("recover: currently mining level "..level, true, false)
  6202.         miningStarted = true
  6203.     end
  6204.     for i = 1, #(functionList) do
  6205.         if callingFunction == functionList[i] or currentFunction == functionList[i] then
  6206.             onGroundLevel = true
  6207.             saveToLog("recover: on ground level", true, false)
  6208.         end
  6209.     end
  6210.     if location:getY() == coordHome:getY() then
  6211.         onGroundLevel = true
  6212.         saveToLog("recover: on ground level", true, false)
  6213.     end
  6214.    
  6215.     if currentFunction == "goToWoodStore" or callingFunction == "goToWoodStore" then
  6216.         goToWoodStore(true)
  6217.     elseif onGroundLevel then
  6218.         returnHome("y")
  6219.         returnHome("x")
  6220.         returnHome("z")
  6221.         returnHome("facing")
  6222.     elseif currentFunction == "findCobble" then -- somewhere in mineshaft, so return
  6223.         returnHome("x")
  6224.         returnHome("z")
  6225.         returnHome("facing")
  6226.         turnRight(2) --face backwards
  6227.         returnHome("y")
  6228.         returnHome("facing")
  6229.     else -- all other functions
  6230.         if currentFunction == "craft" or craftingChest:getSlotContains(1) ~= "" then -- check if chest in front
  6231.             saveToLog("loadStatus: currentFunction = "..currentFunction.." chest slot 1 contains "..craftingChest:getSlotContains(1), false, false)
  6232.             if turtle.getItemCount(slot:getItemSlot("chests")) == 0 then -- chest should be in front
  6233.                 while not turtle.detect() do
  6234.                     numTurns = numTurns  + 1
  6235.                     if numTurns == 4 then
  6236.                         break
  6237.                     else
  6238.                         turnRight(1)
  6239.                     end
  6240.                 end
  6241.             else
  6242.                 turtle.select(slot:getItemSlot("chests"))
  6243.                 while not turtle.compare() do
  6244.                     numTurns = numTurns  + 1
  6245.                     if numTurns == 4 then
  6246.                         break
  6247.                     else
  6248.                         turnRight(1)
  6249.                     end
  6250.                 end
  6251.             end
  6252.             -- assume now facing chest
  6253.             -- slot:restoreAllContents(sContains, sCount, sSlot, sTurtleSlot)
  6254.             saveToLog("loadStatus: emptying chest back into turtle", false)
  6255.             emptyChest("forward")
  6256.             if slot:getItemSlot("chests") > 0 then
  6257.                 emptySlot = slot:getItemSlot("chests")
  6258.             else
  6259.                 emptySlot = getFirstEmptySlot(false)
  6260.             end
  6261.             dig.digNew{self = dig, slotNo = emptySlot, expectedItem = "chests", callFrom = "loadStatus"}
  6262.             slot.update{self = slot, slotNo = emptySlot, item = "chests"}
  6263.         end
  6264.         -- check if at home location in front of or above furnace
  6265.         saveToLog("recover: refuelling", true, false)
  6266.         refuel(0)
  6267.         if miningStarted then
  6268.             while location:getY() > level do
  6269.                 down(1, 1)
  6270.             end
  6271.             while location:getY() < level do -- inside mine on ground level or below
  6272.                 up(1) -- back to eye level
  6273.             end
  6274.             returnHome("facing") -- now facing home direction
  6275.             if location:getX() == coordHome:getX() and location:getZ() == coordHome:getZ() then -- already in shaft
  6276.                 returnHome("y")
  6277.             else
  6278.                 if coordHome:getFacing() == 0 or coordHome:getFacing() == 2 then -- home Facing S/N            
  6279.                     if location:getX() == coordHome:getX() then -- already in centre
  6280.                         --move to Right if s, Left if N
  6281.                         if location:getFacing() == 0 then --south for home and current position
  6282.                             go("RFR")
  6283.                         else
  6284.                             go("LFL")
  6285.                         end
  6286.                         -- now in corridor to L of centre
  6287.                     end
  6288.                     returnHome("z")
  6289.                     returnHome("facing")
  6290.                     -- now lined up with mineshaft in NS direction
  6291.                     if location:getFacing() == 0 then --move into line 1 ahead of mineshaft
  6292.                        go("F")
  6293.                     else
  6294.                        go("RRFRR")
  6295.                     end
  6296.                     returnHome("x")
  6297.                 else -- home facing 1/3 W/E
  6298.                     if location:getZ() == coordHome:getZ() then -- already in centre
  6299.                         --move to Right if s, l if N
  6300.                         if location:getFacing() == 1 then --east for home and current position
  6301.                             go("RFR")
  6302.                         else
  6303.                             go("LFL")
  6304.                         end
  6305.                         -- now in corridor to L of centre
  6306.                     end
  6307.                     returnHome("x")
  6308.                     returnHome("facing")
  6309.                     -- now lined up with mineshaft in EW direction
  6310.                     if location:getFacing() == 1 then --move into line 1 ahead of mineshaft
  6311.                        go("F")
  6312.                     else
  6313.                        go("RRFRR")
  6314.                     end
  6315.                     returnHome("z")
  6316.                 end
  6317.                 --now 1 block in front of mineshaft
  6318.                 returnHome("facing")
  6319.                 back(1) --over mineshaft
  6320.                 returnHome("y")
  6321.             end
  6322.         else --mining not started. still on surface, or in mineshaft
  6323.             returnHome("y")
  6324.         end
  6325.         -- now continue script. If stage completed, will skip through the functions in main()
  6326.     end
  6327. end
  6328.  
  6329. function mineAll()
  6330.     currentFunction = "mineAll"
  6331.     callingFunction = "main"
  6332.     local woodAvailable = 0
  6333.     local logText = ""
  6334.     local fuelLevel = 0
  6335.     local fuelStats = {}
  6336.     local startLevel = 0
  6337.     local numTorchesNeeded = 0
  6338.     local numChestsNeeded = 0
  6339.     local fuelNeeded = 0
  6340.     local diamondStartLevel = 15
  6341.     local fuelStats = {}
  6342.     local levelSequence = {}
  6343.     local levelIndex = 0
  6344.     local numLevels = 0
  6345.     local dirtAmount = 0
  6346.  
  6347.     startLevel = mineTopLevel:getY()
  6348.     if diamondsOnly then --start at level 15 or lower
  6349.         levelSequence[1] = 15
  6350.         levelSequence[2] = 12
  6351.         levelSequence[3] = 9
  6352.         levelSequence[4] = 6
  6353.         levelIndex = 4
  6354.     elseif diamondsFirst then --start at level 15 or lower
  6355.         if startLevel > 15 then
  6356.             levelSequence[1] = 15
  6357.             levelSequence[2] = 12
  6358.             levelSequence[3] = 9
  6359.             levelSequence[4] = 6
  6360.             levelIndex = 4
  6361.             for i = startLevel, 18, -3 do
  6362.                 levelIndex = levelIndex + 1
  6363.                 levelSequence[levelIndex] = i
  6364.             end
  6365.             -- eg 15,12,9,6,51,48,45,42...21,18
  6366.         else
  6367.             for i = startLevel, 6, -3 do
  6368.                 levelIndex = levelIndex + 1
  6369.                 levelSequence[levelIndex] = i
  6370.             end
  6371.             -- eg 12,9,6
  6372.         end
  6373.     else
  6374.         for i = startLevel, 6, -3 do
  6375.             levelIndex = levelIndex + 1
  6376.             levelSequence[levelIndex] = i
  6377.         end
  6378.         -- eg 51,48,45,42...15,12,9,6
  6379.     end
  6380.     numLevels = levelIndex
  6381.     -- central control function. From here move turtle to correct level and
  6382.     -- mine out basic shape, followed by detailed mining
  6383.     -- ensure enough fuel onboard else go to chest on base camp and wait.
  6384.     -- make 38 torches per diamond mining level
  6385.     -- create 1 level in cross shape with empty chest in between
  6386.    
  6387.     -- check if enough fuel onboard, else goToStorageWood
  6388.     fuelStats = getFuelAvailable()
  6389.     saveToLog("mineAll: fuel level = "..turtle.getFuelLevel())
  6390.     saveToLog("mineAll: fuelStats total fuel available = "..fuelStats.totalFuelAvailable)
  6391.     if fuelStats.totalFuelAvailable < 600 then
  6392.         goToWoodStore(false)
  6393.     end
  6394.     --in case of re-start
  6395.     dirtAmount = getStock("dirt")
  6396.     if dirtAmount > 40 then
  6397.         dirtAmount = dirtAmount - 40
  6398.     else
  6399.         dirtAmount = 0
  6400.     end
  6401.     storeWoodItems()
  6402.     storeSand()
  6403.     storeOres()
  6404.     storeMinerals()
  6405.     storeBuildingBlocks{dirt = dirtAmount, cobble = 0, gravel = -1, sand = 64}
  6406.     saveToLog("mineAll: changing logFile Name from "..fso:getCurrentFileName().." to logCreateMineXX.txt. END OF LOGFILE!", true, false)
  6407.     levelIndex = 1
  6408.     repeat
  6409.         level = levelSequence[levelIndex]
  6410.         levelIndex = levelIndex + 1
  6411.         saveToLog("mineAll: checking level "..level.." (index "..levelIndex..") of "..numLevels.." levels", true, false)
  6412.         if not stage["completeMine"..tostring(level)] then --mineXX not completed
  6413.             saveToLog("mineAll: changing logFile Name from "..fso:getCurrentFileName().." to logCreateMine"..level..".txt. END OF LOGFILE!", true, false)
  6414.             fso:useFileName("logCreateMine"..level..".txt")
  6415.             -- base and one or more sectors may have been completed
  6416.             if level <= 15 then
  6417.                 numTorchesNeeded = 24
  6418.             else
  6419.                 numTorchesNeeded = 44
  6420.             end
  6421.             if not stage["createBase"..tostring(level)] then -- whole mine needs to be done
  6422.                 numChestsNeeded = 1
  6423.                 fuelNeeded = 1020 -- 17 logs (17 * 60)
  6424.             else -- find out how many stages completed
  6425.                 if not stage["mineSector1"] then
  6426.                     fuelNeeded = 1020
  6427.                 elseif not stage["mineSector2"] then
  6428.                     numTorchesNeeded = numTorchesNeeded - (numTorchesNeeded / 4) -- 18, 33
  6429.                     fuelNeeded = 760
  6430.                 elseif not stage["mineSector3"] then
  6431.                     numTorchesNeeded =  numTorchesNeeded - (numTorchesNeeded / 2) -- 12, 22
  6432.                     fuelNeeded = 510
  6433.                 elseif not stage["mineSector4"] then
  6434.                     numTorchesNeeded =  numTorchesNeeded - (numTorchesNeeded / 2) - (numTorchesNeeded / 4) -- 6, 11
  6435.                     fuelNeeded = 260
  6436.                 end
  6437.             end
  6438.             if getStock("torches") < numTorchesNeeded then
  6439.                 craftTorches(numTorchesNeeded - getStock("torches"))
  6440.                 currentFunction = "mineAll"
  6441.                 callingFunction = "main"
  6442.             end
  6443.             if getStock("chests") <= numChestsNeeded then --make more chests
  6444.                 craftChests(1)
  6445.                 currentFunction = "mineAll"
  6446.                 callingFunction = "main"
  6447.             end
  6448.             if not stage["createBase"..level] then
  6449.                 if getStock("signs") == 0 then --make signs
  6450.                     craftSigns(3)
  6451.                     currentFunction = "mineAll"
  6452.                     callingFunction = "main"
  6453.                 end
  6454.             end
  6455.             fuelStats = getFuelAvailable()
  6456.             if fuelStats.totalFuelAvailable + fuelStats.fuelLevel < fuelNeeded then
  6457.                 goToWoodStore(false)
  6458.             end
  6459.             if getStock("sticks") > 0 then
  6460.                 storeSticks()
  6461.             end
  6462.             if getStock("planks") > 0 then
  6463.                 turtle.select(slot:getItemSlot("planks"))
  6464.                 turtle.refuel()
  6465.                 slot.update{self = slot, item = "planks", delete = true}
  6466.             end
  6467.             --go down to correct level
  6468.             while location:getY() > level do
  6469.                 down(1)
  6470.             end
  6471.             createMine(level) --mine structure in place ready for mining sectors 12 x 16 block corridors takes 24 hrs
  6472.             while location:getY() < coordHome:getY() do
  6473.                 up(1)
  6474.             end
  6475.             saveToLog("prepareMining: mine created. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
  6476.             storeWoodItems()
  6477.             storeSand()
  6478.             storeOres()
  6479.             storeMinerals()
  6480.             saveStatus()
  6481.             displaySaveBackup()
  6482.         else
  6483.             saveToLog("mineAll: level "..level.." (index "..levelIndex..") of "..numLevels.." levels already completed", true, false)
  6484.         end
  6485.     until levelIndex > numLevels   
  6486. end
  6487.  
  6488. function mineBase(length, itemList, checkForChest)
  6489.     local doPlaceDown = true
  6490.     local doPlaceUp = true
  6491.    
  6492.     currentFunction = "mineBase"
  6493.     callingFunction = "createMine"
  6494.     -- starts on ground level to create platform for player
  6495.     for i = 1, length do
  6496.         if turtle.detect() then
  6497.             if checkForChest then
  6498.                 checkChest("forward")
  6499.             end
  6500.             while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineBase"} do
  6501.                 sleep(.5)
  6502.             end
  6503.         end
  6504.         forward(1, 1, itemList, "mineBase")
  6505.         up(1)
  6506.         if turtle.detectUp() then -- block above
  6507.             if checkForChest then
  6508.                 if checkChest("up") then
  6509.                     doPlaceUp = true
  6510.                 end
  6511.             end
  6512.         else -- no block above
  6513.             checkWaterLava("up")
  6514.             doPlaceUp = true
  6515.         end
  6516.         if doPlaceUp then
  6517.             if getStock("cobble") > 1 then
  6518.                 turtle.select(slot:getItemSlot("cobble"))
  6519.                 turtle.placeUp()
  6520.             elseif getStock("dirt") > 1 then
  6521.                 turtle.select(slot:getItemSlot("dirt"))
  6522.                 turtle.placeUp()
  6523.             end
  6524.         end
  6525.         down(1)
  6526.         if turtle.detectDown() then
  6527.             if checkForChest then
  6528.                 if checkChest("down") then
  6529.                     doPlaceDown = true
  6530.                 end
  6531.             end
  6532.             if not dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineBase"} then
  6533.                 doPlaceDown = false --bedrock found
  6534.             else
  6535.                 doPlaceDown = true
  6536.             end
  6537.         else --nothing below could be air, water, lava
  6538.             if checkWaterLava("down") == "lava" then
  6539.                 turtle.select(slot:getItemSlot("buckets"))
  6540.                 turtle.refuel()
  6541.                 saveToLog("mineBase: refuelled with lava to "..turtle.getFuelLevel())
  6542.                 sleep(2)
  6543.                 while turtle.placeDown() do
  6544.                     if turtle.refuel() then --lava
  6545.                         saveToLog("mineBase: refuelling with lava again to "..turtle.getFuelLevel())
  6546.                     else
  6547.                         break
  6548.                     end
  6549.                     sleep(2)
  6550.                     if turtle.getFuelLevel() > 18000 then
  6551.                         break
  6552.                     end
  6553.                 end
  6554.             end
  6555.         end
  6556.         if doPlaceDown then
  6557.             if getStock("cobble") > 1 then
  6558.                 turtle.select(slot:getItemSlot("cobble"))
  6559.                 turtle.placeDown()
  6560.             elseif getStock("dirt") > 1 then
  6561.                 turtle.select(slot:getItemSlot("dirt"))
  6562.                 turtle.placeDown()
  6563.             end
  6564.         end
  6565.     end
  6566. end
  6567.  
  6568. function mineCorridor(topLevel, length, torchesAt, plugAt, itemList, checkSides, do3D)
  6569.     local numMoves = 0
  6570.  
  6571.     checkSides = checkSides or "none" -- "right", "left", "both", "none"
  6572.     -- length = length of corridor, torchesAt = position of torches
  6573.     -- moves forward FIRST then mines up/down, checks walls, places torch if required
  6574.     -- level 15 and below moves up and down to search both layers
  6575.     -- level 17 and above moves on same level and searches only one layer, but calls mineItem with do3D arg
  6576.     for i = 1, length do
  6577.         -- saveToLog("mineCorridor: i = "..i, false, false)
  6578.         -- saveToLog("mineCorridor: "..location:getX()..","..location:getY()..","..location:getZ()..","..location:getFacing(), false, false)
  6579.         if topLevel > 15 then -- non diamond levels. move to ceiling level
  6580.             while location:getY() < topLevel do
  6581.                 up(1)
  6582.             end
  6583.             while location:getY() > topLevel do
  6584.                 down(1)
  6585.             end
  6586.         end
  6587.         if turtle.detect() then --dig
  6588.             -- saveToLog("mineCorridor: detected block in front, digging "..i, false, false)
  6589.             checkChest("forward")
  6590.             while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineCorridor"} do
  6591.                 -- if at bedrock will exit
  6592.                 sleep(.5)
  6593.             end
  6594.         end
  6595.         --check if on level 5, different mining technique used here
  6596.         if location:getY() == 5 then -- level 5. may be valuable item below
  6597.             go("xC")
  6598.         end
  6599.         -- and move forward
  6600.         while not forward(1, 1, itemList, "mineCorridor") do -- at bedrock
  6601.             while not up(1) do
  6602.                 back(1)
  6603.                 numMoves = numMoves + 1
  6604.             end
  6605.             -- go forward again if gone back
  6606.         end
  6607.         while numMoves > 0 do
  6608.             forward(1, 1, itemList, "mineCorridor")
  6609.             numMoves = numMoves - 1
  6610.         end
  6611.         -- moved forward. If at ceiling level move down 1. If at floor level move up 1
  6612.         if location:getY() == topLevel then -- at top of mining level, or do3D
  6613.             -- check if ceiling in place. If so check if valuable and mine/replace it
  6614.             if turtle.detectUp() then --block above
  6615.                 if checkChest("up") then
  6616.                     if getStock("cobble") > 1 then
  6617.                         turtle.select(slot:getItemSlot("cobble"))
  6618.                         turtle.placeUp()
  6619.                     elseif getStock("dirt") > 1 then
  6620.                         turtle.select(slot:getItemSlot("dirt"))
  6621.                         turtle.placeUp()
  6622.                     end
  6623.                 else
  6624.                     if isValuable("up") then --could be useful, so dig and replace with cobble
  6625.                         saveToLog("mineCorridor: block above valuable, digging "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
  6626.                         while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor", waitForGravel = true} do --allow for gravel
  6627.                             sleep(.5)
  6628.                         end
  6629.                         if plugAt[i] then
  6630.                             saveToLog("mineCorridor: placing cobble plug above "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
  6631.                             if getStock("cobble") > 1 then
  6632.                                 turtle.select(slot:getItemSlot("cobble"))
  6633.                                 turtle.placeUp()
  6634.                             elseif getStock("dirt") > 1 then
  6635.                                 turtle.select(slot:getItemSlot("dirt"))
  6636.                                 turtle.placeUp()
  6637.                             end
  6638.                         end
  6639.                     end
  6640.                 end
  6641.             else --no block above or lava/water, so place cobble
  6642.                 -- saveToLog("mineCorridor: no block detected above "..i, false, false)
  6643.                 if plugAt[i] then
  6644.                     saveToLog("mineCorridor: placing cobble plug above "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
  6645.                     if getStock("cobble") > 1 then
  6646.                         turtle.select(slot:getItemSlot("cobble"))
  6647.                         turtle.placeUp()
  6648.                     elseif getStock("dirt") > 1 then
  6649.                         turtle.select(slot:getItemSlot("dirt"))
  6650.                         turtle.placeUp()
  6651.                     end
  6652.                 end
  6653.             end
  6654.             -- still at ceiling level. Could be lava below
  6655.             if not turtle.detectDown() then --nothing below could be air, water, lava
  6656.                 if checkWaterLava("down") == "lava" then
  6657.                     turtle.select(slot:getItemSlot("buckets"))
  6658.                     turtle.refuel()
  6659.                     saveToLog("mineCorridor: refuelled with lava to "..turtle.getFuelLevel())
  6660.                     sleep(2)
  6661.                     while turtle.placeDown() do
  6662.                         if turtle.refuel() then --lava
  6663.                             saveToLog("mineCorridor: refuelling with lava again to "..turtle.getFuelLevel())
  6664.                         else
  6665.                             break
  6666.                         end
  6667.                         sleep(2)
  6668.                         if turtle.getFuelLevel() > 18000 then
  6669.                             break
  6670.                         end
  6671.                     end
  6672.                 end
  6673.             end
  6674.             -- check if any valuable items before moving down
  6675.             if checkSides == "both" or checkSides == "right" then
  6676.                 turnRight(1)
  6677.                 if turtle.detect() then
  6678.                     if not checkChest("forward") then
  6679.                         -- saveToLog("mineCorridor: check right side "..i, false, false)
  6680.                         mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6681.                     end
  6682.                 end
  6683.                 turnLeft(1)
  6684.             end
  6685.             if checkSides == "both" or checkSides == "left" then
  6686.                 turnLeft(1)
  6687.                 if turtle.detect() then
  6688.                     if not checkChest("forward") then
  6689.                         -- saveToLog("mineCorridor: check left side"..i, false, false)
  6690.                         mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6691.                     end
  6692.                 end
  6693.                 turnRight(1)
  6694.             end
  6695.             if topLevel <= 6 then -- <= level 6 - mining from toplevel to bedrock
  6696.                 while down(1) do
  6697.                     if location:getY() < 4 then
  6698.                         if turtle.detect() then --check forward
  6699.                             mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6700.                         end
  6701.                         turnRight(1)
  6702.                         if turtle.detect() then
  6703.                             mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6704.                         end
  6705.                         turnLeft(2)
  6706.                         if turtle.detect() then
  6707.                             mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6708.                         end
  6709.                         turnRight(1)
  6710.                     else -- level 5 and 6 only
  6711.                         if checkSides == "both" or checkSides == "right" then
  6712.                             turnRight(1)
  6713.                             if turtle.detect() then
  6714.                                 -- saveToLog("mineCorridor: check right side "..i, false, false)
  6715.                                 mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6716.                             end
  6717.                             turnLeft(1)
  6718.                         end
  6719.                         if checkSides == "both" or checkSides == "left" then
  6720.                             turnLeft(1)
  6721.                             if turtle.detect() then
  6722.                                 -- saveToLog("mineCorridor: check left side"..i, false, false)
  6723.                                 mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6724.                             end
  6725.                             turnRight(1)
  6726.                         end
  6727.                     end    
  6728.                 end
  6729.                 -- moved down to bedrock, so go up to level 5
  6730.                 if location:getY() < 5 then
  6731.                     repeat
  6732.                         if up(1) then
  6733.                             if numMoves > 0 then
  6734.                                 if forward(numMoves, 1, itemList, "mineCorridor") then
  6735.                                     numMoves = 0
  6736.                                 end
  6737.                             end
  6738.                         else
  6739.                             back(1)
  6740.                             numMoves = numMoves + 1
  6741.                         end
  6742.                     until location:getY() == 5
  6743.                 end
  6744.                 while numMoves > 0 do
  6745.                     while not forward(1, 1, itemList, "mineCorridor") do -- at bedrock
  6746.                         while not up(1) do
  6747.                             back(1)
  6748.                             numMoves = numMoves + 1
  6749.                         end
  6750.                     end
  6751.                     numMoves = numMoves - 1
  6752.                 end
  6753.             else -- topLevel > 6
  6754.                 -- move to floor level
  6755.                 down(1)
  6756.                 -- at floor level
  6757.                 if checkSides == "both" or checkSides == "right" then
  6758.                     turnRight(1)
  6759.                     if turtle.detect() then
  6760.                         if not checkChest("forward") then
  6761.                             -- saveToLog("mineCorridor: check right side "..i, false, false)
  6762.                             mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6763.                         end
  6764.                     else
  6765.                         if checkWaterLava("forward") == "lava" then -- block it off
  6766.                             saveToLog("mineCorridor: blocking lava with plug at "..location:getX()..", "..location:getY() ..", "..location:getZ(), false, false)
  6767.                             if getStock("cobble") > 1 then
  6768.                                 turtle.select(slot:getItemSlot("cobble"))
  6769.                                 turtle.place()
  6770.                             elseif getStock("dirt") > 1 then
  6771.                                 turtle.select(slot:getItemSlot("dirt"))
  6772.                                 turtle.place()
  6773.                             end
  6774.                         end
  6775.                     end
  6776.                     turnLeft(1)
  6777.                 end
  6778.                 if checkSides == "both" or checkSides == "left" then
  6779.                     turnLeft(1)
  6780.                     if turtle.detect() then
  6781.                         if not checkChest("forward") then
  6782.                             -- saveToLog("mineCorridor: check left side"..i, false, false)
  6783.                             mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6784.                         end
  6785.                     else
  6786.                         if checkWaterLava("forward") == "lava" then -- block it off
  6787.                             saveToLog("mineCorridor: blocking lava with plug at "..location:getX()..", "..location:getY() ..", "..location:getZ(), false, false)
  6788.                             if getStock("cobble") > 1 then
  6789.                                 turtle.select(slot:getItemSlot("cobble"))
  6790.                                 turtle.place()
  6791.                             elseif getStock("dirt") > 1 then
  6792.                                 turtle.select(slot:getItemSlot("dirt"))
  6793.                                 turtle.place()
  6794.                             end
  6795.                         end
  6796.                     end
  6797.                     turnRight(1)
  6798.                 end
  6799.                 if turtle.detectDown() then
  6800.                     if not checkChest("down") then
  6801.                         if isValuable("down") then
  6802.                             saveToLog("mineCorridor: block below is valuable, digging "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  6803.                             dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"} -- not bedrock
  6804.                         end
  6805.                         turtle.select(slot:getItemSlot("gravel"))
  6806.                         if turtle.compareDown() then
  6807.                             saveToLog("mineCorridor: block below is gravel, digging "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  6808.                             dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
  6809.                         end
  6810.                     end
  6811.                 else --nothing below could be air, water, lava
  6812.                     if checkWaterLava("down") == "lava" then
  6813.                         turtle.select(slot:getItemSlot("buckets"))
  6814.                         turtle.refuel()
  6815.                         saveToLog("mineCorridor: refuelled with lava to "..turtle.getFuelLevel())
  6816.                         sleep(2)
  6817.                         while turtle.placeDown() do
  6818.                             if turtle.refuel() then --lava
  6819.                                 saveToLog("mineCorridor: refuelling with lava again to "..turtle.getFuelLevel())
  6820.                             else
  6821.                                 break
  6822.                             end
  6823.                             sleep(2)
  6824.                             if turtle.getFuelLevel() > 18000 then
  6825.                                 break
  6826.                             end
  6827.                         end
  6828.                     end
  6829.                 end
  6830.                 if not turtle.detectDown() then
  6831.                     saveToLog("mineCorridor: placing floor plug "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  6832.                     if getStock("cobble") > 1 then
  6833.                         turtle.select(slot:getItemSlot("cobble"))
  6834.                         turtle.placeDown()
  6835.                     elseif getStock("dirt") > 1 then
  6836.                         turtle.select(slot:getItemSlot("dirt"))
  6837.                         turtle.placeDown()
  6838.                     end
  6839.                 end
  6840.                 -- moved to floor level
  6841.             end
  6842.             if torchesAt[i] then
  6843.                 if slot:getItemCount("torches") > 1 then
  6844.                     if up(1) then
  6845.                         turtle.select(slot:getItemSlot("torches"))
  6846.                         turtle.placeDown()
  6847.                         slot.update{self = slot, item = "torches", delete = true}
  6848.                         saveToLog("mineCorridor: placing torch "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  6849.                     end
  6850.                 end
  6851.             end
  6852.             -- at floor level
  6853.         else -- started at floor level        
  6854.             if turtle.detectDown() then
  6855.                 -- saveToLog("mineCorridor: block detected below "..i, false, false)
  6856.                 if not checkChest("down") then
  6857.                     if isValuable("down") then
  6858.                         dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
  6859.                     end
  6860.                     turtle.select(slot:getItemSlot("gravel"))
  6861.                     if turtle.compareDown() then
  6862.                         saveToLog("mineCorridor: block below is gravel, digging "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  6863.                         dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
  6864.                     end
  6865.                 end
  6866.             else
  6867.                 if checkWaterLava("down") == "lava" then
  6868.                     turtle.select(slot:getItemSlot("buckets"))
  6869.                     turtle.refuel()
  6870.                     saveToLog("mineCorridor: refuelled with lava to "..turtle.getFuelLevel())
  6871.                     sleep(2)
  6872.                     while turtle.placeDown() do
  6873.                         if turtle.refuel() then --lava
  6874.                             saveToLog("mineCorridor: refuelling with lava again to "..turtle.getFuelLevel())
  6875.                         else
  6876.                             break
  6877.                         end
  6878.                         sleep(2)
  6879.                         if turtle.getFuelLevel() > 18000 then
  6880.                             break
  6881.                         end
  6882.                     end
  6883.                 end
  6884.             end
  6885.             if not turtle.detectDown() then
  6886.                 saveToLog("mineCorridor: placing floor plug "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  6887.                 if getStock("cobble") > 1 then
  6888.                     turtle.select(slot:getItemSlot("cobble"))
  6889.                     turtle.placeDown()
  6890.                 elseif getStock("dirt") > 1 then
  6891.                     turtle.select(slot:getItemSlot("dirt"))
  6892.                     turtle.placeDown()
  6893.                 end
  6894.             end
  6895.             if checkSides == "both" or checkSides == "right" then
  6896.                 turnRight(1)
  6897.                 if turtle.detect() then
  6898.                     if not checkChest("forward") then
  6899.                         -- saveToLog("mineCorridor: check right side "..i, false, false)
  6900.                         mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6901.                     end
  6902.                 end
  6903.                 turnLeft(1)
  6904.             end
  6905.             if checkSides == "both" or checkSides == "left" then
  6906.                 turnLeft(1)
  6907.                 if turtle.detect() then
  6908.                     if not checkChest("forward") then
  6909.                         -- saveToLog("mineCorridor: check left side"..i, false, false)
  6910.                         mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6911.                     end
  6912.                 end
  6913.                 turnRight(1)
  6914.             end
  6915.             up(1)
  6916.             if checkSides == "both" or checkSides == "right" then
  6917.                 turnRight(1)
  6918.                 if turtle.detect() then
  6919.                     if not checkChest("forward") then
  6920.                         -- saveToLog("mineCorridor: check right side "..i, false, false)
  6921.                         mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6922.                     end
  6923.                 end
  6924.                 turnLeft(1)
  6925.             end
  6926.             if checkSides == "both" or checkSides == "left" then
  6927.                 turnLeft(1)
  6928.                 if turtle.detect() then
  6929.                     if not checkChest("forward") then
  6930.                         -- saveToLog("mineCorridor: check left side "..i, false, false)
  6931.                         mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
  6932.                     end
  6933.                 end
  6934.                 turnRight(1)
  6935.             end
  6936.             -- check ceiling
  6937.             if location:getY() == topLevel then -- at top of mining level
  6938.                 if turtle.detectUp() then
  6939.                     if not checkChest("up") then
  6940.                         if isValuable("up") then --could be useful, so dig and replace with cobble
  6941.                             while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor", waitForGravel = true} do --allow for gravel
  6942.                                 sleep(.5)
  6943.                             end
  6944.                         end
  6945.                     end
  6946.                     if (not turtle.detectUp()) and plugAt[i] then
  6947.                         saveToLog("mineCorridor: placing ceiling plug "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
  6948.                         if getStock("cobble") > 1 then
  6949.                             turtle.select(slot:getItemSlot("cobble"))
  6950.                             turtle.placeUp()
  6951.                         elseif getStock("dirt") > 1 then
  6952.                             turtle.select(slot:getItemSlot("dirt"))
  6953.                             turtle.placeUp()
  6954.                         end
  6955.                     end
  6956.                 else
  6957.                     if plugAt[i] then
  6958.                         saveToLog("mineCorridor: placing ceiling plug "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
  6959.                         if getStock("cobble") > 1 then
  6960.                             turtle.select(slot:getItemSlot("cobble"))
  6961.                             turtle.placeUp()
  6962.                         elseif getStock("dirt") > 1 then
  6963.                             turtle.select(slot:getItemSlot("dirt"))
  6964.                             turtle.placeUp()
  6965.                         end
  6966.                     end
  6967.                 end
  6968.                 if torchesAt[i] then
  6969.                     if slot:getItemCount("torches") > 1 then
  6970.                         turtle.select(slot:getItemSlot("torches"))
  6971.                         turtle.placeDown()
  6972.                         slot.update{self = slot, item = "torches", delete = true}
  6973.                         saveToLog("mineCorridor: placing torch "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  6974.                     end
  6975.                 end
  6976.             end
  6977.         end
  6978.     end
  6979.     while location:getY() < topLevel do
  6980.         up(1)
  6981.     end
  6982. end
  6983.  
  6984. function mineStrip(topLevel, length, itemList)
  6985.     local numMoves = 0
  6986.     local placeCobble = false
  6987.     local lavawater = ""
  6988.     local skipColumn = true
  6989.     -- length = length of corridor
  6990.     -- moves forward FIRST then mines up/down
  6991.     -- level 15 and below moves up and down to search both layers
  6992.     -- level 17 and above moves on same level and searches only one layer
  6993.     for i = 1, length do
  6994.         placeCobble = false
  6995.         while location:getY() < topLevel do
  6996.             up(1)
  6997.         end
  6998.         while location:getY() > topLevel do
  6999.             down(1)
  7000.         end
  7001.         if turtle.detect() then --dig
  7002.             checkChest("forward")
  7003.             while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineStrip"} do
  7004.                 -- if at bedrock will exit
  7005.                 sleep(.5)
  7006.             end
  7007.         end
  7008.         -- and move forward
  7009.         while not forward(1, 1, itemList, "mineStrip") do -- at bedrock
  7010.             while not up(1) do
  7011.                 back(1)
  7012.                 numMoves = numMoves + 1
  7013.             end
  7014.             -- go forward again if gone back
  7015.         end
  7016.         while numMoves > 0 do
  7017.             forward(1, 1, itemList, "mineStrip")
  7018.             numMoves = numMoves - 1
  7019.         end
  7020.         -- moved forward
  7021.         skipColumn = not skipColumn
  7022.         if turtle.detectUp() then --block above
  7023.             if not isCeilingOk() then
  7024.                 saveToLog("mineStrip: block above valuable, digging "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
  7025.                 checkChest("up") --empty and collect chest if found
  7026.                 while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor", waitForGravel = true} do --allow for gravel
  7027.                     sleep(.5)
  7028.                 end
  7029.                 placeCobble = true
  7030.             end  
  7031.         else
  7032.             lavawater = checkWaterLava("up")
  7033.             if lavawater == "lava" or lavawater == "water" or lavawater == "liquid" then
  7034.                 turtle.select(slot:getItemSlot("buckets"))
  7035.                 turtle.refuel()
  7036.                 placeCobble = true
  7037.             end
  7038.         end --no block above or lava/water, so place cobble
  7039.         if placeCobble then
  7040.             saveToLog("mineStrip: placing cobble plug above "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
  7041.             if getStock("cobble") > 1 then
  7042.                 turtle.select(slot:getItemSlot("cobble"))
  7043.             else
  7044.                 sortCobble()
  7045.             end
  7046.             if getStock("cobble") > 1 then
  7047.                 turtle.select(slot:getItemSlot("cobble"))
  7048.             elseif getStock("dirt") > 1 then
  7049.                 turtle.select(slot:getItemSlot("dirt"))
  7050.             end
  7051.             turtle.placeUp()
  7052.         end
  7053.         -- Could be lava below
  7054.         if not turtle.detectDown() then --nothing below could be air, water, lava
  7055.             if checkWaterLava("down") == "lava" then
  7056.                 turtle.select(slot:getItemSlot("buckets"))
  7057.                 turtle.refuel()
  7058.                 saveToLog("mineStrip: refuelled with lava to "..turtle.getFuelLevel())
  7059.                 sleep(2)
  7060.                 while turtle.placeDown() do
  7061.                     if turtle.refuel() then --lava
  7062.                         saveToLog("mineStrip: refuelling with lava again to "..turtle.getFuelLevel())
  7063.                     else
  7064.                         break
  7065.                     end
  7066.                     sleep(2)
  7067.                     if turtle.getFuelLevel() > 18000 then
  7068.                         break
  7069.                     end
  7070.                 end
  7071.             end
  7072.         end
  7073.         if topLevel <= 6 then -- <= level 6 - mining from toplevel to bedrock
  7074.             if not skipColumn then
  7075.                 while down(1) do
  7076.                     if location:getY() < 6 then
  7077.                         placeCobble = false
  7078.                         if turtle.detect() then --check forward
  7079.                             mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineStrip", do3D, "")
  7080.                             if not turtle.detect() then --check forward
  7081.                                 placeCobble = true
  7082.                             end
  7083.                         end
  7084.                     end
  7085.                     if placeCobble then
  7086.                         if getStock("cobble") > 1 then
  7087.                             turtle.select(slot:getItemSlot("cobble"))
  7088.                         else
  7089.                             sortCobble()
  7090.                         end
  7091.                         if getStock("cobble") > 1 then
  7092.                             turtle.select(slot:getItemSlot("cobble"))
  7093.                             turtle.place()
  7094.                         elseif getStock("dirt") > 1 then
  7095.                             turtle.select(slot:getItemSlot("dirt"))
  7096.                             turtle.place()
  7097.                         end
  7098.                     end
  7099.                 end
  7100.                 -- moved down to bedrock, so go up to level 5
  7101.                 if location:getY() < 6 then
  7102.                     repeat
  7103.                         if up(1) then
  7104.                             if numMoves > 0 then
  7105.                                 if forward(numMoves, 1, itemList, "mineStrip") then
  7106.                                     numMoves = 0
  7107.                                 end
  7108.                             end
  7109.                         else
  7110.                             back(1)
  7111.                             numMoves = numMoves + 1
  7112.                         end
  7113.                         if getStock("cobble") == 1 then
  7114.                             sortCobble()
  7115.                         end
  7116.                         if getStock("cobble") > 1 then
  7117.                             turtle.select(slot:getItemSlot("cobble"))
  7118.                             turtle.placeDown()
  7119.                         elseif getStock("dirt") > 1 then
  7120.                             turtle.select(slot:getItemSlot("dirt"))
  7121.                             turtle.placeDown()
  7122.                         end  
  7123.                     until location:getY() == 6
  7124.                 end
  7125.                 while numMoves > 0 do
  7126.                     while not forward(1, 1, itemList, "mineStrip") do -- at bedrock
  7127.                         while not up(1) do
  7128.                             back(1)
  7129.                             numMoves = numMoves + 1
  7130.                         end
  7131.                     end
  7132.                     numMoves = numMoves - 1
  7133.                 end
  7134.             end
  7135.         else -- topLevel > 6
  7136.             placeCobble = false
  7137.             if turtle.detectDown() then
  7138.                 if not isFloorOk() then --valuable item below
  7139.                     placeCobble = true
  7140.                     checkChest("down")
  7141.                     if turtle.detectDown() then
  7142.                         dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineStrip"} -- not bedrock
  7143.                     end
  7144.                 end
  7145.             else --nothing below could be air, water, lava
  7146.                 if checkWaterLava("down") == "lava" then
  7147.                     turtle.refuel()
  7148.                     saveToLog("mineStrip: refuelled with lava to "..turtle.getFuelLevel())
  7149.                     sleep(2)
  7150.                     while turtle.placeDown() do
  7151.                         if turtle.refuel() then --lava
  7152.                             saveToLog("mineStrip: refuelling with lava again to "..turtle.getFuelLevel())
  7153.                         else
  7154.                             break
  7155.                         end
  7156.                         sleep(2)
  7157.                         if turtle.getFuelLevel() > 18000 then
  7158.                             break
  7159.                         end
  7160.                     end
  7161.                 end
  7162.             end
  7163.             if placeCobble then
  7164.                 saveToLog("mineStrip: placing ground level plug "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
  7165.                 if getStock("cobble") == 1 then
  7166.                     sortCobble()
  7167.                 end
  7168.                 if getStock("cobble") > 1 then
  7169.                     turtle.select(slot:getItemSlot("cobble"))
  7170.                     turtle.placeDown()
  7171.                 elseif getStock("dirt") > 1 then
  7172.                     turtle.select(slot:getItemSlot("dirt"))
  7173.                     turtle.placeDown()
  7174.                 end
  7175.             end
  7176.         end
  7177.     end
  7178. end
  7179.  
  7180. function mineDown(toLevel, itemList, calledFrom)
  7181.     currentFunction = "mineDown"
  7182.     callingFunction = calledFrom
  7183.  
  7184.     saveToLog("mineDown: mining down to level "..toLevel, true)
  7185.    
  7186.     while location:getY() >= toLevel do --mine down either to bedrock, or to selected level.
  7187.         checkWalls(itemList, calledFrom, true) --check surrounding walls for valuable items, use recursive function to mine them
  7188.         if getStock("cobble") > 60 then --enough cobble found
  7189.             break
  7190.         else
  7191.             if not down(1, 1, itemList, calledFrom) then -- bedrock
  7192.                 break
  7193.             end
  7194.         end
  7195.     end
  7196.     checkWalls(itemList, calledFrom, true) --final check at base of chosen depth
  7197. end
  7198.  
  7199. function mineUp(toLevel, itemList, calledFrom, getCobble, stopAtSurface)
  7200.     currentFunction = "mineUp"
  7201.     callingFunction = calledFrom
  7202.     getCobble = getCobble or false
  7203.     stopAtSurface = stopAtSurface or false
  7204.     saveToLog("mineUp: mining to level "..toLevel, true)
  7205.     while location:getY() < toLevel do --mine up to selected level
  7206.         checkWalls(itemList, calledFrom, true)
  7207.         if getCobble then
  7208.             for i = 1, 4 do
  7209.                 dig.digNew{self = dig, direction = "forward", checkForItems = itemList, callFrom = calledFrom}
  7210.                 turnRight(1)
  7211.             end
  7212.         end
  7213.         if getStock("cobble") > 60 then --enough cobble found
  7214.             break
  7215.         end
  7216.         if stopAtSurface and location:getY() > 50 and not turtle.detectUp() then
  7217.             up(1, 1, itemList, calledFrom)
  7218.             saveToLog("mineUp: surface reached at level "..location:getY(), true)
  7219.             break
  7220.         end
  7221.         up(1, 1, itemList, calledFrom)
  7222.     end
  7223.     checkWalls(itemList, calledFrom, true) --final check at top of chosen height
  7224. end
  7225.  
  7226. function mineItem(itemList, calledFrom, do3D, direction)   
  7227.     currentFunction = "mineItem"
  7228.     callingFunction = calledFrom
  7229.     local doContinue = true
  7230.     --RECURSIVE FUNCTION - BEWARE!
  7231.    
  7232.     -- check if block in front is valuable. If so mine it
  7233.     -- saveToLog("mineItem: checking block in front ", false, false)
  7234.     if direction == "up" then
  7235.         if not checkChest("up") then
  7236.             if isValuable("up") then
  7237.                 if dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom} then --not bedrock
  7238.                     saveToLog("mineItem: valuable item dug above", false, false)
  7239.                     if dig:getDugSlot() > 0 then
  7240.                         if turtle.getItemCount(dig:getDugSlot()) > 58 then
  7241.                             doContinue = false
  7242.                         end
  7243.                     end
  7244.                     while turtle.detectUp() do
  7245.                         dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
  7246.                         sleep(0.5)
  7247.                     end
  7248.                 else
  7249.                     doContinue = false
  7250.                 end
  7251.             end
  7252.         else
  7253.             doContinue = false
  7254.         end
  7255.         if turtle.getFuelLevel() < 10 then
  7256.             saveToLog("mineItem: refuelling", true, false)
  7257.             refuel(0)
  7258.         end
  7259.         if doContinue then
  7260.             -- move up into space dug out
  7261.             up(1)
  7262.             if isValuable("up") then --experimental
  7263.                 mineItem(itemList, calledFrom, do3D, "")
  7264.             end
  7265.             -- check if item in front is valuable
  7266.             if isValuable("side") then
  7267.                 mineItem(itemList, calledFrom, do3D, "")
  7268.             end
  7269.             --check right side
  7270.             turnRight(1)
  7271.             if isValuable("side") then
  7272.                 mineItem(itemList, calledFrom, do3D, "")
  7273.             end
  7274.             --check behind
  7275.             turnRight(1)
  7276.             if isValuable("side") then
  7277.                 mineItem(itemList, calledFrom, do3D, "")
  7278.             end
  7279.             --check left side
  7280.             turnRight(1)
  7281.             if isValuable("side") then
  7282.                 mineItem(itemList, calledFrom, do3D, "")
  7283.             end
  7284.             --return to front
  7285.             turnRight(1)
  7286.             down(1)
  7287.         end
  7288.     end
  7289.     if direction == "down" then
  7290.         if not checkChest("down") then
  7291.             if isValuable("down") then
  7292.                 if dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = calledFrom} then --not bedrock
  7293.                     saveToLog("mineItem: valuable item dug below", false, false)
  7294.                     if dig:getDugSlot() > 0 then
  7295.                         if turtle.getItemCount(dig:getDugSlot()) > 58 then
  7296.                             doContinue = false
  7297.                         end
  7298.                     end
  7299.                 end
  7300.             else
  7301.                 doContinue = false
  7302.             end
  7303.         else
  7304.             doContinue = false
  7305.         end
  7306.         if turtle.getFuelLevel() < 10 then
  7307.             saveToLog("mineItem: refuelling", true, false)
  7308.             refuel(0)
  7309.         end
  7310.         -- move down into space dug out
  7311.         if doContinue then
  7312.             down(1)
  7313.             if isValuable("down") then --experimental
  7314.                 mineItem(itemList, calledFrom, do3D, "")
  7315.             end
  7316.             -- check if item in front is valuable
  7317.             if isValuable("side") then
  7318.                 mineItem(itemList, calledFrom, do3D, "")
  7319.             end
  7320.             --check right side
  7321.             turnRight(1)
  7322.             if isValuable("side") then
  7323.                 mineItem(itemList, calledFrom, do3D, "")
  7324.             end
  7325.             --check behind
  7326.             turnRight(1)
  7327.             if isValuable("side") then
  7328.                 mineItem(itemList, calledFrom, do3D, "")
  7329.             end
  7330.             --check left side
  7331.             turnRight(1)
  7332.             if isValuable("side") then
  7333.                 mineItem(itemList, calledFrom, do3D, "")
  7334.             end
  7335.             --return to front
  7336.             turnRight(1)
  7337.             up(1)
  7338.         end
  7339.     end
  7340.     if isValuable("side") then
  7341.         if not checkChest("forward") then
  7342.             dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom}
  7343.             if dig:getDugSlot() > 0 then
  7344.                 saveToLog("mineItem: valuable item dug", false, false)
  7345.                 if turtle.getItemCount(dig:getDugSlot()) > 58 then
  7346.                     doContinue = false
  7347.                     saveToLog("mineItem: slot filled- quitting", false, false)
  7348.                 end
  7349.             end
  7350.         else
  7351.             doContinue = false
  7352.         end
  7353.        
  7354.         while turtle.detect() do --clear sand and gravel
  7355.             if dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom} then
  7356.                 sleep(0.5)
  7357.                 saveToLog("mineItem: clearing sand or gravel", false, false)
  7358.             else -- bedrock
  7359.                 doContinue = false
  7360.                 break
  7361.             end
  7362.         end
  7363.         if turtle.getFuelLevel() < 10 then
  7364.             saveToLog("mineItem: refuelling", true, false)
  7365.             refuel(0)
  7366.         end
  7367.         -- move forward into space dug out
  7368.         -- saveToLog("mineItem: debug: about to move forward "..location:getX()..","..location:getY()..","..location:getZ()..","..location:getFacing(), false, false)
  7369.         -- forward(1, 1, itemList, calledFrom)
  7370.         if doContinue then --not bedrock and < 59 in stock
  7371.             while not forward(1) do
  7372.                 dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom}
  7373.                 saveToLog("mineItem: clearing sand or gravel")
  7374.                 sleep(0.5)
  7375.             end
  7376.             -- changeCoords("forward")
  7377.             -- saveToLog("mineItem: debug: moved forwards "..location:getX()..","..location:getY()..","..location:getZ()..","..location:getFacing(), false, false)
  7378.             if isValuable("up") then --experimental
  7379.                 checkChest("up")
  7380.                 if do3D then
  7381.                     mineItem(itemList, calledFrom, do3D, "up")
  7382.                 else
  7383.                     dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
  7384.                 end
  7385.             end
  7386.             if isValuable("down") then --experimental
  7387.                 checkChest("down")
  7388.                 if do3D then
  7389.                     mineItem(itemList, calledFrom, do3D, "down")
  7390.                 else
  7391.                     dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = calledFrom}
  7392.                 end
  7393.             end
  7394.             -- check if item in front is valuable
  7395.             if isValuable("side") then
  7396.                 mineItem(itemList, calledFrom, do3D, "")
  7397.             end
  7398.             --check left side
  7399.             turnLeft(1)
  7400.             if isValuable("side") then
  7401.                 mineItem(itemList, calledFrom, do3D, "")
  7402.             end
  7403.             -- check right side
  7404.             turnRight(2)
  7405.             if isValuable("side") then
  7406.                 mineItem(itemList, calledFrom, do3D, "")
  7407.             end
  7408.             turnLeft(1)
  7409.             back(1)
  7410.         end
  7411.     end
  7412. end
  7413.  
  7414. function placeChest(direction, storageName)
  7415.     currentFunction = "placeChest"
  7416.     turtle.select(slot:getItemSlot("chests"))
  7417.     if direction == "down" then
  7418.         if not turtle.compareDown() then
  7419.             down(1, 1)
  7420.             turtle.select(slot:getItemSlot("cobble"))
  7421.             turtle.placeDown()
  7422.             up(1)
  7423.             turtle.select(slot:getItemSlot("chests"))
  7424.             turtle.placeDown()
  7425.         end
  7426.     else
  7427.         if not turtle.compareUp() then
  7428.             up(1)
  7429.             turtle.select(slot:getItemSlot("chests"))
  7430.             turtle.placeDown()
  7431.             up(1)
  7432.             turtle.select(slot:getItemSlot("cobble"))
  7433.             turtle.placeDown()
  7434.         end
  7435.     end
  7436.     placeStorage:setStoragePlaced(storageName)
  7437.     slot.update{self = slot, item = "chests", delete = true}
  7438.     slot.update{self = slot, item = "cobble", delete = true}
  7439. end
  7440.  
  7441. function refuel(count)
  7442.     local previousFunction = currentFunction
  7443.     currentFunction = "refuel"
  7444.     --[[1 wood block = 15 fuel
  7445.         1 planks = 15, 4 = 60
  7446.         1 stick = 5, 8 = 40
  7447.         planks in slot 3
  7448.         use coal> planks> wood2> wood]]--
  7449.        
  7450.     local success = false
  7451.     local startLevel = 0
  7452.     local emptySlot = 0
  7453.     local fuelStats = {}
  7454.     local turns = 0
  7455.     local goUp = 0
  7456.     local waste = {"cobble-6","cobble-5","cobble-4","cobble-3","cobble-2","cobble-1","gravel-2","gravel-1","dirt-4","dirt-3","dirt-2","dirt-1",}
  7457.     local numEmptySlots = getNoOfEmptySlots()
  7458.    
  7459.     if count == nil then
  7460.         count = 0
  7461.     end
  7462.  
  7463.     --count is units of 15, 6 = 1 coal/charcoal 6 blocks of wood or 6 planks
  7464.     startLevel = turtle.getFuelLevel()
  7465.     if startLevel < 100 or count > 0 then -- refuel by 1 coal, 3 planks/wood
  7466.         saveToLog("refuel: current level = "..startLevel, true)
  7467.         --sortInventory(true)
  7468.         fuelStats = getFuelAvailable()
  7469.         saveToLog("refuel: fuelStats total fuel available = "..fuelStats.totalFuelAvailable)
  7470.         sortCoal()
  7471.         if fuelStats.fuelAvailableCoal > 0 then
  7472.             turtle.select(slot:getItemSlot("coal"))
  7473.             if turtle.refuel(1) then
  7474.                 success = true
  7475.                 slot.update{self = slot, item = "coal"}
  7476.                 saveToLog("Refuelled with coal to "..turtle.getFuelLevel().." (start = "..startLevel..")", true)
  7477.             else
  7478.                 saveToLog("Refuel: failed with coal")
  7479.             end
  7480.         end
  7481.         if not success and fuelStats.fuelAvailablePlanks > 0 then
  7482.             saveToLog("refuel: fuelStats fuelAvailablePlanks = "..fuelStats.fuelAvailablePlanks)
  7483.             turtle.select(slot.getItemSlot(slot, "planks"))
  7484.             if turtle.refuel() then
  7485.                 success = true
  7486.                 slot.update{self = slot, item = "planks", delete = true}               
  7487.                 saveToLog("Refuelled with planks to "..turtle.getFuelLevel().." (start = "..startLevel..")")
  7488.                 sortInventory(true)
  7489.             else
  7490.                 saveToLog("Refuel: failed with planks")
  7491.             end
  7492.         end
  7493.         if not success and (fuelStats.fuelAvailableWood > 0) then      
  7494.             saveToLog("refuel: No planks for fuel. Attempting to craft planks from wood")
  7495.             -- need 2 slots free for crafting
  7496.            
  7497.             if numEmptySlots < 2 then
  7498.                 repeat
  7499.                     for i = 1, 12 do
  7500.                         if slot:getItemSlot(waste[i]) > 0 then
  7501.                             numEmptySlots = numEmptySlots + 1
  7502.                             saveToLog("refuel: dumping mining waste: "..waste[i], false, true)
  7503.                             turtle.select(slot:getItemSlot(waste[i]))
  7504.                             turtle.dropDown()
  7505.                             slot.update{self = slot, item = waste[i], delete = true}
  7506.                             sortInventory(true)
  7507.                         end
  7508.                     end
  7509.                 until numEmptySlots >= 2
  7510.             end
  7511.             if turtle.detect() then
  7512.                 for i = 1, 4 do
  7513.                     if turtle.detect() then
  7514.                         turnRight(1)
  7515.                         turns = turns + 1
  7516.                     else
  7517.                         break
  7518.                     end
  7519.                 end
  7520.             end
  7521.             if turns == 4 and location:getY() < 6 then -- could be at bedrock
  7522.                 turns = 0
  7523.                 for i = 1, 4 do
  7524.                     if not dig.digNew{self = dig} then
  7525.                         turnRight(1)
  7526.                         turns = turns + 1
  7527.                     else
  7528.                         break
  7529.                     end
  7530.                 end
  7531.                 if turns == 4 then --surrounded by bedrock
  7532.                     if turtle..getFuelLevel() == 0 then
  7533.                         if getStock("wood") > 1 then
  7534.                             turtle.select(slot:getItemSlot("wood"))
  7535.                             turtle.refuel(1)
  7536.                         else
  7537.                             saveToLog("refuel: No wood for fuel. Out of fuel. GAME OVER", true, false)
  7538.                             error()
  7539.                         end
  7540.                     end
  7541.                     up(1)
  7542.                     goUp = 1
  7543.                     turns = 0
  7544.                     for i = 1, 4 do
  7545.                         if not dig.digNew{self = dig} then
  7546.                             turnRight(1)
  7547.                             turns = turns + 1
  7548.                         else
  7549.                             break
  7550.                         end
  7551.                     end
  7552.                 end
  7553.             end
  7554.             -- bug in cc 1.6.3 place() can put chest 1 block away if grass is in the way
  7555.             dig.digNew{self = dig}
  7556.             if count == 0 then
  7557.                 count = 4
  7558.             end
  7559.             if craft{calledFrom = "refuel", craftItem = "planks", craftQuantity = count, sourceItem1 = "wood", destSlot = 0, doSort = true} then
  7560.                 fuelStats.fuelAvailablePlanks = turtle.getItemCount(slot.getItemSlot(slot, "planks")) * 15
  7561.                 turtle.select(slot.getItemSlot(slot, "planks"))
  7562.                 if turtle.refuel() then
  7563.                     success = true
  7564.                     slot.update{self = slot, item = "planks", delete = true}
  7565.                     saveToLog("Refuelled with planks to "..turtle.getFuelLevel().." (start = "..startLevel..")")
  7566.                 end
  7567.             else
  7568.                 saveToLog("Refuel: error crafting planks from wood")
  7569.                 error()
  7570.             end
  7571.             if turns > 0 then
  7572.                 turnLeft(turns)
  7573.             end
  7574.             if goUp > 0 then
  7575.                 down(goUp)
  7576.             end
  7577.         end
  7578.         if not success and fuelStats.fuelAvailableCharcoal > 0 then
  7579.             turtle.select(slot.getItemSlot(slot, "charcoal"))
  7580.             if turtle.refuel(fuelStats.fuelAvailableCharcoal/80) then
  7581.                 success = true
  7582.                 slot.update{self = slot, item = "charcoal", delete = true}
  7583.                 saveToLog("Refuelled with charcoal to "..turtle.getFuelLevel().." (start = "..startLevel..")")
  7584.                 sortInventory(true)
  7585.             else
  7586.                 saveToLog("Refuel: failed with charcoal")
  7587.             end
  7588.         end
  7589.         if success then
  7590.             sortInventory(true)
  7591.         else
  7592.             saveToLog("No materials left for fuel")
  7593.         end
  7594.     end --fuel > 100
  7595.     currentFunction = previousFunction
  7596.    
  7597.     return success
  7598. end
  7599.  
  7600. function returnHome(goDirection)
  7601.     if goDirection == "x" then
  7602.         if location:getX() < coordHome:getX() then
  7603.             while location:getFacing() ~= 3 do
  7604.                 turnRight(1)
  7605.             end
  7606.             while location:getX() < coordHome:getX() do
  7607.                 forward(1, 1)
  7608.             end
  7609.         elseif location:getX() > coordHome:getX() then
  7610.             while location:getFacing() ~= 1 do
  7611.                 turnRight(1)
  7612.             end
  7613.             while location:getX() > coordHome:getX() do
  7614.                 forward(1, 1)
  7615.             end
  7616.         end
  7617.     elseif goDirection == "y" then
  7618.         while location:getY() > coordHome:getY() do
  7619.             down(1, 1)
  7620.         end
  7621.         while location:getY() < coordHome:getY() do --
  7622.             up(1, 1) -- back to ground level at home
  7623.         end
  7624.     elseif goDirection == "z" then
  7625.         if location:getZ() > coordHome:getZ() then -- go north
  7626.             while location:getFacing() ~= 2 do
  7627.                 turnRight(1)
  7628.             end
  7629.             while location:getZ() > coordHome:getZ() do
  7630.                 forward(1, 1)
  7631.             end
  7632.         elseif location:getZ() < coordHome:getZ() then
  7633.             while location:getFacing() ~= 0 do
  7634.                 turnRight(1)
  7635.             end
  7636.             while location:getZ() < coordHome:getZ() do
  7637.                 forward(1, 1)
  7638.             end
  7639.         end
  7640.     elseif goDirection == "facing" then
  7641.         while location:getFacing() ~= coordHome:getFacing() do
  7642.             turnRight(1)
  7643.         end
  7644.     end
  7645. end
  7646.  
  7647. function saveStatus()
  7648.     --[[
  7649.     current filename index = #
  7650.     x = #
  7651.     y = #
  7652.     z = #
  7653.     facing = #
  7654.     HomeX = #
  7655.     HomeY = #
  7656.     HomeZ = #
  7657.     HomeFacing = #
  7658.     slot-01 = "item"
  7659.     ...
  7660.     slot-16 = "item"
  7661.     diamondsFirst = true/false
  7662.     stage["startUnderground"] = false
  7663.     stage["getPreferences"] = true/false
  7664.     stage["getSupplies"] = true/false
  7665.     stage["getCoords"] = true/false
  7666.     stage["craftChests"] = true/false
  7667.     stage["craftSticks"] = true/false
  7668.     stage["craftLadders"] = true/false
  7669.     stage["craftSigns"] = true/false
  7670.     stage["cobblePlaced"] = false
  7671.     stage["getReady"] = true/false
  7672.     stage["findCobble"] = true/false
  7673.     stage["craftFurnaces"] = true/false
  7674.     stage["clearBase"] = true/false
  7675.     stage["completeMineshaft"] = true/false
  7676.     ]]--
  7677.  
  7678.     local handle = fs.open("superminer.recover", "w")
  7679.    
  7680.     handle.write(tostring(fso:getCurrentFileNameIndex()).."\n")
  7681.     handle.write(tostring(location:getX()).."\n")
  7682.     handle.write(tostring(location:getY()).."\n")
  7683.     handle.write(tostring(location:getZ()).."\n")
  7684.     handle.write(tostring(location:getFacing()).."\n")
  7685.     handle.write(tostring(coordHome:getX()).."\n")
  7686.     handle.write(tostring(coordHome:getY()).."\n")
  7687.     handle.write(tostring(coordHome:getZ()).."\n")
  7688.     handle.write(tostring(coordHome:getFacing()).."\n")
  7689.     handle.write(tostring(mineTopLevel:getX()).."\n")
  7690.     handle.write(tostring(mineTopLevel:getY()).."\n")
  7691.     handle.write(tostring(mineTopLevel:getZ()).."\n")
  7692.     handle.write(tostring(mineTopLevel:getFacing()).."\n")
  7693.     handle.write(currentFunction.."\n")
  7694.     if callingFunction == nil then callingFunction = "" end
  7695.     handle.write(callingFunction.."\n")
  7696.     handle.write(tostring(diamondsOnly).."\n")
  7697.     handle.write(tostring(diamondsFirst).."\n")
  7698.     handle.write(textutils.serialize(slot:getStatus()).."\n") -- get all slot object variables
  7699.     handle.write(textutils.serialize(stage).."\n")
  7700.     handle.write(textutils.serialize(craftingChest:getStatus()).."\n")
  7701.  
  7702.     handle.close()
  7703. end
  7704.  
  7705. function saveToLog(text, toScreen, verbose)
  7706.     toScreen = toScreen or true
  7707.     verbose = verbose or false
  7708.     text = text or "Nil instead of text supplied, check your code!"
  7709.    
  7710.     if toScreen then
  7711.         print(text)
  7712.     end
  7713.     if fso:getUseLog() then
  7714.         fso:appendLine(text, verbose)
  7715.     end
  7716. end
  7717.  
  7718. function smelt(oreType, quantity)
  7719.     currentFunction = "smelt"
  7720.     --charcoal, iron, stone, glass
  7721.     --assume function called with turtle under furnace, at ground level
  7722.     --move next to furnace to place planks
  7723.     --move to top of furnace to place wood
  7724.     --move under furnace to remove charcoal, stone, glass, iron
  7725.     local smeltType = ""
  7726.     local continue = true
  7727.     local waitTime = 0
  7728.     local planksNeeded = 0 --total needed for smelting
  7729.     local planksAvailable = 0
  7730.     local planksInFurnace = 0
  7731.     local woodNeeded = 0
  7732.     local woodAvailable = 0
  7733.     local collectionSlot = 0
  7734.     local success = false
  7735.     local emptySlot = 0
  7736.    
  7737.     woodAvailable = getStock("wood")
  7738.     planksAvailable = getStock("planks")
  7739.     planksNeeded = quantity
  7740.     if oreType == "charcoal" then
  7741.         woodNeeded = quantity + math.ceil(quantity / 4)
  7742.     end
  7743.     if woodNeeded > woodAvailable then
  7744.         goToWoodStore(false)
  7745.         woodAvailable = getStock("wood")
  7746.     end
  7747.     saveToLog("smelt: oreType = "..oreType.." quantity = "..quantity, false, true)
  7748.     go("BUUF") --now on top
  7749.    
  7750.     if oreType == "charcoal" then --eg quantity = 2, needs 2 wood + 2 planks ASSUME only called if wood in stock
  7751.         smeltType = "charcoal"
  7752.         saveToLog("smelt: dropping "..quantity.." wood into furnace")
  7753.         turtle.select(slot:getItemSlot("wood"))
  7754.         turtle.dropDown(quantity)
  7755.         slot.update{self = slot, item = "wood", delete = true}
  7756.     elseif oreType == "cobble"then
  7757.         smeltType = "stone"
  7758.         turtle.select(slot.getItemSlot(slot, "cobble"))
  7759.         turtle.dropDown(quantity)
  7760.         slot.update{self = slot, item = "cobble"}
  7761.         saveToLog("smelt: "..quantity.." cobble dropped into furnace")
  7762.     elseif oreType == "?ironore" then
  7763.         smeltType = "?iron"
  7764.         turtle.select(slot.getItemSlot(slot, "?ironore"))
  7765.         turtle.dropDown(quantity)
  7766.         slot.update{self = slot, item = "?ironore"}
  7767.         saveToLog("smelt: "..quantity.." provisional iron ore dropped into furnace")
  7768.     elseif oreType == "ironore"  then
  7769.         smeltType = "iron"
  7770.         turtle.select(slot.getItemSlot(slot, "ironore"))
  7771.         turtle.dropDown(quantity)
  7772.         slot.update{self = slot, item = "ironore"}
  7773.         saveToLog("smelt: "..quantity.." iron ore dropped into furnace")
  7774.     elseif oreType == "?goldore" then
  7775.         smeltType = "?gold"
  7776.         turtle.select(slot.getItemSlot(slot, "?goldore"))
  7777.         turtle.dropDown(quantity)
  7778.         slot.update{self = slot, item = "?goldore"}
  7779.         saveToLog("smelt: "..quantity.." ?gold ore dropped into furnace")
  7780.     elseif oreType == "goldore" then
  7781.         smeltType = "gold"
  7782.         turtle.select(slot.getItemSlot(slot, "goldore"))
  7783.         turtle.dropDown(quantity)
  7784.         slot.update{self = slot, item = "goldore"}
  7785.         saveToLog("smelt: "..quantity.." gold ore dropped into furnace")
  7786.     elseif oreType == "sand" then
  7787.         smeltType = "glass"
  7788.         turtle.select(slot.getItemSlot(slot, "sand"))
  7789.         turtle.dropDown(quantity)
  7790.         slot.update{self = slot, item = "sand"}
  7791.         saveToLog("smelt: "..quantity.." sand dropped into furnace")
  7792.     end
  7793.     go("BD") --in front of furnace
  7794.     turtle.select(16)
  7795.     if turtle.suck() then
  7796.         planksInFurnace = turtle.getItemCount(16)
  7797.         planksAvailable = planksAvailable + planksInFurnace
  7798.         if planksNeeded < 0 then
  7799.             planksNeeded = 0
  7800.         end
  7801.         turtle.drop()
  7802.     end
  7803.  
  7804.     saveToLog("smelt: planksNeeded = "..planksNeeded)
  7805.     if planksInFurnace > planksNeeded then
  7806.         planksNeeded = 0
  7807.     else
  7808.         if getStock("planks") >=  planksNeeded then
  7809.             turtle.select(slot:getItemSlot("planks"))
  7810.             turtle.drop(planksNeeded)
  7811.             slot.update{self = slot, item = "planks", delete = true}
  7812.             saveToLog("smelt: "..planksNeeded.." planks added to fuel furnace")
  7813.             saveToLog("Smelting will take "..planksNeeded * 10 .." secs")
  7814.             planksNeeded = 0
  7815.         end
  7816.     end
  7817.     if planksNeeded > 0 then --reset to 0 only if planks already in furnace
  7818.         planksNeeded = math.ceil(planksNeeded / 4) * 4 --eg 1/4 = 1 ,*4 = 4 planks
  7819.         saveToLog("smelt: planksNeeded = "..planksNeeded.." crafting more")
  7820.         turnRight(1) --side on to furnace
  7821.         craft{calledFrom = "smelt", craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
  7822.         currentFunction = "smelt"
  7823.         turnLeft(1)
  7824.         turtle.select(slot:getItemSlot("planks"))
  7825.         turtle.drop() --drop existing planks
  7826.         slot.update{self = slot, item = "planks", delete = true}
  7827.     end
  7828.     go("DF") --back under furnace
  7829.     collectionSlot = getFirstEmptySlot()
  7830.     if slot.getItemSlot(slot, smeltType) > 0 then
  7831.         collectionSlot = slot.getItemSlot(slot, smeltType)
  7832.     end
  7833.     turtle.select(collectionSlot)
  7834.     repeat
  7835.         waitTime = waitTime + 1
  7836.         sleep(1)
  7837.         if waitTime == 10 then --every 10 secs check if any output
  7838.             if turtle.suckUp() then --continue to wait
  7839.                 continue = true
  7840.                 waitTime = 0
  7841.             else --either no product or all done
  7842.                 continue = false
  7843.             end
  7844.         end
  7845.     until not continue
  7846.     if turtle.getItemCount(collectionSlot) > 0 then
  7847.         success = true
  7848.         slot.update{self = slot, slotNo = collectionSlot, item = smeltType}
  7849.     else --item did not smelt
  7850.         go("BUUF")
  7851.         turtle.select(1)
  7852.         turtle.suckDown()
  7853.         go("BDDF")
  7854.     end
  7855.     saveToLog("smelt: success = "..tostring(success))
  7856.     return success
  7857. end
  7858.  
  7859. function sortCoal()
  7860.     if getStock("coal-2") > 0 then
  7861.         if getStock("coal") < 64 then
  7862.             saveToLog("sortCoal: moving coal-2 from "..slot:getItemSlot("coal-2").." to coal in slot"..slot:getItemSlot("coal"), true, false)
  7863.             turtle.select(slot:getItemSlot("coal-2"))
  7864.             turtle.transferTo(slot:getItemSlot("coal"))
  7865.             slot.update{self = slot, item = "coal-2", delete = true}
  7866.             sortInventory(true)
  7867.         end
  7868.     end
  7869.     if getStock("coal-1") > 0 then
  7870.         if getStock("coal") < 64 then
  7871.             saveToLog("sortCoal: moving coal-1 from "..slot:getItemSlot("coal-1").." to coal in slot"..slot:getItemSlot("coal"), true, false)
  7872.             turtle.select(slot:getItemSlot("coal-1"))
  7873.             turtle.transferTo(slot:getItemSlot("coal"))
  7874.             slot.update{self = slot, item = "coal-1", delete = true}
  7875.             sortInventory(true)
  7876.             slot.update{self = slot, item = "coal", delete = true}
  7877.         end
  7878.     end
  7879. end
  7880.  
  7881. function sortCobble()
  7882.     local cobbleIndex = 1
  7883.    
  7884.     if getStock("cobble-4") > 0 then
  7885.         if getStock("cobble") < 64 then
  7886.             saveToLog("sortCobble: moving cobble-4 from "..slot:getItemSlot("cobble-4").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
  7887.             turtle.select(slot:getItemSlot("cobble-4"))
  7888.             turtle.transferTo(slot:getItemSlot("cobble"))
  7889.             slot.update{self = slot, item = "cobble-4", delete = true}
  7890.             sortInventory(true)
  7891.         end
  7892.     end
  7893.     if getStock("cobble-3") > 0 then
  7894.         if getStock("cobble") < 64 then
  7895.             saveToLog("sortCobble: moving cobble-3 from "..slot:getItemSlot("cobble-3").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
  7896.             turtle.select(slot:getItemSlot("cobble-3"))
  7897.             turtle.transferTo(slot:getItemSlot("cobble"))
  7898.             slot.update{self = slot, item = "cobble-3", delete = true}
  7899.             sortInventory(true)
  7900.         end
  7901.     end
  7902.     if getStock("cobble-2") > 0 then
  7903.         if getStock("cobble") < 64 then
  7904.             saveToLog("sortCobble: moving cobble-2 from "..slot:getItemSlot("cobble-2").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
  7905.             turtle.select(slot:getItemSlot("cobble-2"))
  7906.             turtle.transferTo(slot:getItemSlot("cobble"))
  7907.             slot.update{self = slot, item = "cobble-2", delete = true}
  7908.             sortInventory(true)
  7909.         end
  7910.     end
  7911.     if getStock("cobble-1") > 0 then
  7912.         if getStock("cobble") < 64 then
  7913.             saveToLog("sortCobble: moving cobble-1 from "..slot:getItemSlot("cobble-1").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
  7914.             turtle.select(slot:getItemSlot("cobble-1"))
  7915.             turtle.transferTo(slot:getItemSlot("cobble"))
  7916.             slot.update{self = slot, item = "cobble-1", delete = true}
  7917.             sortInventory(true)
  7918.         end
  7919.     end
  7920.     for i = 6, 1, -1 do
  7921.         if getStock("cobble-"..tostring(i)) > 0 then
  7922.             cobbleIndex = i
  7923.             break
  7924.         end
  7925.     end
  7926.     -- now check for itemX incorrectly identified
  7927.     turtle.select(slot:getItemSlot("cobble"))
  7928.     for i = 6, 1, -1 do
  7929.         if getStock("item"..tostring(i)) > 0 then
  7930.             if turtle.compareTo(slot:getItemSlot("item"..tostring(i))) then -- cobble found with wrong id
  7931.                 turtle.select(slot:getItemSlot("item"..tostring(i)))
  7932.                 turtle.transferTo(slot:getItemSlot("cobble-"..tostring(cobbleIndex)))
  7933.                 if turtle.getItemCount(slot:getItemSlot("item"..tostring(i))) > 0 then --still some remaining
  7934.                     slot.update{self = slot, slotNo = getItemSlot("item"..tostring(i)), newItem = "cobble-"..tostring(cobbleIndex + 1)}
  7935.                 else
  7936.                     slot.update{self = slot, slotNo = getItemSlot("item"..tostring(i)), delete = true}
  7937.                 end
  7938.                 sortInventory(true)
  7939.                 break
  7940.             end
  7941.         end
  7942.     end
  7943. end
  7944.  
  7945. function sortInventory(moveItems)
  7946.     local useSlot = 0
  7947.     local usedSlots = 0
  7948.     local emptySlot = 0
  7949.     local checkItem = ""
  7950.     local compareItem = ""
  7951.     local moveAmount = 0
  7952.     local excessAmount = 0
  7953.     local amount = 65
  7954.     local firstSlot = 0
  7955.     local lastSlot = 16
  7956.     local toSlotNo = 0
  7957.     local fromSlotNo = 0
  7958.     local slotState = {}
  7959.     local unknownSlotCount = {}
  7960.     local count = 0
  7961.     local index = 0
  7962.     local fromAmount = 0
  7963.     local toAmount = 0
  7964.     local doAgain = false
  7965.    
  7966.     if moveItems == nil then
  7967.         moveItems = false
  7968.     end
  7969.     firstNewItemSlot = 0
  7970.     lastNewItemSlot = 0
  7971.     saveToLog("sortInventory: Sorting started, moveItems = "..tostring(moveItems), false)
  7972.  
  7973.     --gather like items together first
  7974.     for i = 1, 16 do
  7975.         checkItem = slot:getSlotContains(i)  -- eg "", "item1", "signs" contents of slot in current loop
  7976.         if turtle.getItemCount(i) > 0 then
  7977.             turtle.select(i) --now compare with slots above
  7978.             for j = i + 1, 15 do
  7979.                 compareItem = slot:getSlotContains(j)  -- eg "", "item1", "signs" contents of slot being compared
  7980.                 if turtle.compareTo(j) then --more of the same in slot j
  7981.                     if slot:getSlotStatus(i) == "known" then
  7982.                         moveAmount = 64 - turtle.getItemCount(i) --40 dirt in slot i, 40 dirt in slot j, moveAmount = 24
  7983.                         turtle.select(j)
  7984.                         if moveAmount > 0 then
  7985.                             turtle.transferTo(i, moveAmount) --move up to 64
  7986.                         end
  7987.                         slot.update{self = slot, slotNo = i, item = checkItem}
  7988.                     else --"itemX" or ""
  7989.                         moveAmount = 63 - turtle.getItemCount(i)
  7990.                         turtle.select(j)
  7991.                         if moveAmount > 0 then
  7992.                             turtle.transferTo(i, moveAmount)
  7993.                         end
  7994.                         excessAmount = turtle.getItemCount(j)
  7995.                         if excessAmount > 0 then
  7996.                             while turtle.drop() do
  7997.                                 turnRight(2) --so if items are dropped do not get sucked up
  7998.                                 saveToLog("sortInventory: dumping "..excessAmount.." of excess "..checkItem)
  7999.                                 turnRight(2)
  8000.                             end
  8001.                             --slot.update{self = slot, item = "item", slotNo = j, delete = true}
  8002.                         end
  8003.                         slot.update{self = slot, slotNo = i, item = checkItem}
  8004.                     end
  8005.                 end
  8006.             end
  8007.         end
  8008.         checkItem = slot:getSlotContains(i)  -- re-check, anything not identified dump
  8009.         if checkItem == "" then
  8010.             turtle.select(i)
  8011.             while turtle.drop() do
  8012.                 turnRight(2) --so if items are dropped do not get sucked up
  8013.                 saveToLog(" sortInventory: dumping unknown item in slot "..i, true)
  8014.                 turnRight(2)
  8015.             end
  8016.         end
  8017.     end
  8018.     --loose items sorted into correct slots, excess dumped
  8019.     if moveItems then
  8020.         --check for empty
  8021.         repeat
  8022.             doAgain = false
  8023.             for i = 1, 15 do
  8024.                 if slot:getSlotStatus(i) == "empty" then
  8025.                     if slot:getSlotStatus(i + 1) ~= "empty" then --move known item
  8026.                         turtle.select(i + 1)
  8027.                         turtle.transferTo(i)
  8028.                         slot.transfer{self = slot, fromSlot = i + 1, transferTo = i}
  8029.                         doAgain = true
  8030.                     end
  8031.                 end
  8032.             end
  8033.         until not doAgain
  8034.         --move known into correct place
  8035.         repeat
  8036.             doAgain = false
  8037.             for i = 16, 1, -1 do
  8038.                 if slot:getSlotStatus(i) == "known" then
  8039.                     if slot:getSlotStatus(i - 1) == "unknown" then --move known item
  8040.                         turtle.select(i)
  8041.                         turtle.transferTo(16)
  8042.                         slot.transfer{self = slot, fromSlot = i, transferTo = 16}
  8043.                         turtle.select(i - 1)
  8044.                         turtle.transferTo(i)
  8045.                         slot.transfer{self = slot, fromSlot = i - 1, transferTo = i}
  8046.                         turtle.select(16)
  8047.                         turtle.transferTo(i - 1)
  8048.                         slot.transfer{self = slot, fromSlot = 16, transferTo = i - 1}
  8049.                         doAgain = true
  8050.                     end
  8051.                 end
  8052.             end
  8053.         until not doAgain
  8054.         --move unknown depending on itemCount
  8055.         repeat
  8056.             doAgain = false
  8057.             for i = 16, 1, -1 do
  8058.                 if slot:getSlotStatus(i) == "unknown" then
  8059.                     fromAmount = turtle.getItemCount(i)
  8060.                     if slot:getSlotStatus(i - 1) == "unknown" then --move item
  8061.                         toAmount = turtle.getItemCount(i - 1)
  8062.                         if fromAmount < toAmount then
  8063.                             turtle.select(i)
  8064.                             turtle.transferTo(16)
  8065.                             slot.transfer{self = slot, fromSlot = i, transferTo = 16}
  8066.                             turtle.select(i - 1)
  8067.                             turtle.transferTo(i)
  8068.                             slot.transfer{self = slot, fromSlot = i - 1, transferTo = i}
  8069.                             turtle.select(16)
  8070.                             turtle.transferTo(i - 1)
  8071.                             slot.transfer{self = slot, fromSlot = 16, transferTo = i - 1}
  8072.                             doAgain = true
  8073.                         end
  8074.                     end
  8075.                 end
  8076.             end
  8077.         until not doAgain
  8078.         -- saveToLog("sortInventory: resetting unknown items", false)
  8079.         slot:resetUnknownItems()
  8080.     end
  8081.     saveToLog("sortInventory: debug log", false)
  8082.     for i = 1, 16 do
  8083.         if slot:getSlotCount(i) > 0 then
  8084.             saveToLog(" sortInventory: slot "..i.." slotCount = "..slot:getSlotCount(i).. " of "..slot:getSlotContains(i), false, false)
  8085.         end
  8086.     end
  8087.     --turnRight(2)
  8088.     turtle.select(1)
  8089. end
  8090.  
  8091. function storeBuildingBlocks(arg)
  8092.     currentFunction = "storeBuildingBlocks"
  8093.     -- storeBuildingBlocks{dirt = -1, cobble = -1, gravel = -1, sand = -1}
  8094.     -- 0 = store nothing
  8095.     -- 1 to 63 store that number
  8096.     -- -1 leave 1 behind
  8097.     -- 64 store everything
  8098.     arg.dirt = arg.dirt or -1
  8099.     arg.cobble = arg.cobble or -1
  8100.     arg.gravel = arg.gravel or -1
  8101.     arg.sand = arg.sand or -1
  8102.    
  8103.     changeDirection("faceForward")
  8104.     turnRight(1)
  8105.     forward(2)
  8106.     saveToLog("storeBuildingBlocks: storing excess blocks", true, false)
  8107.     if arg.dirt ~= 0 then
  8108.         storeItem{toStore = storageBuilding, item = "dirt", quantity = arg.dirt, updateSlot = true, doSort = false}
  8109.     end
  8110.     storeExcess("dirt-1")
  8111.     storeExcess("dirt-2")
  8112.     storeExcess("dirt-3")
  8113.     storeExcess("dirt-4")
  8114.     if arg.cobble ~= 0 then
  8115.         storeItem{toStore = storageBuilding, item = "cobble", quantity = arg.cobble, updateSlot = true, doSort = false}
  8116.     end
  8117.     storeExcess("cobble-1")
  8118.     storeExcess("cobble-2")
  8119.     storeExcess("cobble-3")
  8120.     storeExcess("cobble-4")
  8121.     storeExcess("cobble-5")
  8122.     storeExcess("cobble-6")
  8123.     if arg.gravel ~= 0 then
  8124.         storeItem{toStore = storageBuilding, item = "gravel", quantity = arg.gravel, updateSlot = true, doSort = false}
  8125.     end
  8126.     storeExcess("gravel-1")
  8127.     storeExcess("gravel-2")
  8128.     if arg.sand ~= 0 then
  8129.         storeItem{toStore = storageBuilding, item = "sand", quantity = arg.sand, updateSlot = true, doSort = false}
  8130.     end
  8131.     turnRight(2)
  8132.     forward(2)
  8133.     changeDirection("faceForward")
  8134. end
  8135.  
  8136. function storeExcess(itemTypeQualified)
  8137.     if slot:getItemSlot(itemTypeQualified) > 0 then
  8138.         saveToLog("Storing excess '"..itemTypeQualified, false, true)
  8139.         turtle.select(slot:getItemSlot(itemTypeQualified))
  8140.         if not turtle.dropDown() then
  8141.             turtle.dropUp()
  8142.         end
  8143.         slot.update{self = slot, item = itemTypeQualified, delete = true}
  8144.         sortInventory(true)
  8145.     end
  8146. end
  8147.  
  8148. function storeMinerals()
  8149.     currentFunction = "storeMinerals"
  8150.    
  8151.     changeDirection("faceForward")
  8152.     go("LFFLFFRR")
  8153.     saveToLog("storeMinerals: storing mined identified items", true, false)
  8154.     storeItem{toStore = storageMinerals, item = "redstone", quantity = 0, updateSlot = true, doSort = false}
  8155.     storeItem{toStore = storageMinerals, item = "redstone-1", quantity = 0, updateSlot = true, doSort = false}
  8156.     storeItem{toStore = storageMinerals, item = "redstone-2", quantity = 0, updateSlot = true, doSort = false}
  8157.     storeItem{toStore = storageMinerals, item = "redstone-3", quantity = 0, updateSlot = true, doSort = false}
  8158.     storeItem{toStore = storageMinerals, item = "lapis", quantity = 0, updateSlot = true, doSort = false}
  8159.     storeItem{toStore = storageMinerals, item = "coal-1", quantity = 0, updateSlot = true, doSort = false}
  8160.     storeItem{toStore = storageMinerals, item = "coal-2", quantity = 0, updateSlot = true, doSort = false}
  8161.     if getStock("coal") > 35 then
  8162.         turtle.select(slot:getItemSlot("coal"))
  8163.         if not turtle.refuel(turtle.getItemCount(slot:getItemSlot("coal")) - 35) then
  8164.             turtle.dropDown(turtle.getItemCount(slot:getItemSlot("coal")) - 35)
  8165.         end
  8166.     end
  8167.     for i = 6, 1, -1 do
  8168.         if getStock(tostring("item"..i)) > 0 then
  8169.             storeItem{toStore = storageOres, item = tostring("item"..i), quantity = 0, updateSlot = true, doSort = false}
  8170.         end
  8171.     end
  8172.     sortInventory(true)
  8173.     go("FFRFFL")
  8174. end
  8175.  
  8176. function storeOres()
  8177.     local redstoneFound = false
  8178.    
  8179.     currentFunction = "storeOres"
  8180.     changeDirection("faceForward")
  8181.     go("RFFRFFRR")
  8182.     saveToLog("storeOres: storing mined ores", true, false)
  8183.     for i = 6, 1, -1 do
  8184.         if getStock(tostring("item"..i)) > 0 then
  8185.             turtle.select(slot:getItemSlot(tostring("item"..i)))
  8186.             if turtle.place() then --ores, obsidian, mossy cobble, redstone if not identified already
  8187.                 saveToLog("storeOres: testing item"..i, true, false)
  8188.                 if turtle.getItemCount(slot:getItemSlot(tostring("item"..i))) > 0 then
  8189.                     if not turtle.compare() then
  8190.                         saveToLog("storeOres: redstone identified", true, false)
  8191.                         redstoneFound = true
  8192.                         slot.update{self = slot, slotNo = slot:getItemSlot(tostring("item"..i)), newItem = "redstone"}
  8193.                     end
  8194.                 end
  8195.                 turtle.dig()
  8196.                 if not redstoneFound then
  8197.                     storeItem{toStore = storageOres, item = tostring("item"..i), quantity = 0, updateSlot = true, doSort = false}
  8198.                 end
  8199.             end
  8200.         end
  8201.     end
  8202.     sortInventory(true)
  8203.     go("FFLFFR")
  8204. end
  8205.  
  8206. function storeMiningWaste(overChest)
  8207.     local waste = {"dirt-1","dirt-2","dirt-3","dirt-4","cobble-1","cobble-2","cobble-3","cobble-4","cobble-5","cobble-6","gravel-1","gravel-2"}
  8208.     local wasteFound = true
  8209.     local direction = "down"
  8210.     local useOtherChest = false
  8211.     local doSort = false
  8212.    
  8213.     saveToLog("storeMiningWaste: started")
  8214.     if not overChest then --chest found in dungeon, clear some space for items inside
  8215.         repeat
  8216.             wasteFound = false
  8217.             for i = 1, 12 do
  8218.                 if slot:getItemSlot(waste[i]) > 0 then
  8219.                     wasteFound = true
  8220.                     saveToLog("storeMiningWaste: Dumping mining waste: "..waste[i], false, true)
  8221.                     turtle.select(slot:getItemSlot(waste[i]))
  8222.                     turtle.dropDown()
  8223.                     slot.update{self = slot, item = waste[i], delete = true}
  8224.                     sortInventory(true)
  8225.                 end
  8226.             end
  8227.         until not wasteFound
  8228.         if getStock("cobble") > 50 then
  8229.             turtle.select(slot:getItemSlot("cobble"))
  8230.             turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 50)
  8231.             slot.update{self = slot, item = "cobble", delete = true}
  8232.         end
  8233.     else -- over or under storage chest
  8234.         if not turtle.detectDown() then --move down to floor level. could now be on level 5
  8235.             down(1)
  8236.         end
  8237.         if location:getY() == 5 then -- use chest above first. If full move to second chest
  8238.             direction = "up"
  8239.             up(1)
  8240.             for i = 1, 16 do --store unknown items
  8241.                 if slot:getSlotContains(i) == "" and turtle.getItemCount(i) > 0 then
  8242.                     doSort = true
  8243.                     turtle.select(i)
  8244.                     if not turtle.dropUp() then-- chest full
  8245.                         if not useOtherChest then -- move to other chest
  8246.                             useOtherChest = true
  8247.                             changeDirection("faceBackward")
  8248.                             go("LFFRR")
  8249.                             turtle.select(i)
  8250.                             if not turtle.dropUp() then-- chest full
  8251.                                 turtle.dropDown()
  8252.                             end
  8253.                         else -- already under other chest
  8254.                             turtle.select(i)
  8255.                             turtle.dropDown()
  8256.                         end
  8257.                     end
  8258.                 end
  8259.             end
  8260.             if doSort then
  8261.                 sortInventory(true)
  8262.             end
  8263.             if getStock("dirt") > 40 then
  8264.                 turtle.select(slot:getItemSlot("dirt"))
  8265.                 if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("dirt")) - 40) then-- chest full
  8266.                     if not useOtherChest then
  8267.                         useOtherChest = true
  8268.                         changeDirection("faceBackward")
  8269.                         go("LFFRR")
  8270.                         turtle.select(slot:getItemSlot("dirt"))
  8271.                         if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("dirt")) - 40) then-- chest full
  8272.                             turtle.dropDown(turtle.getItemCount(slot:getItemSlot("dirt")) - 40)
  8273.                         end
  8274.                     else
  8275.                         turtle.select(slot:getItemSlot("dirt"))
  8276.                         turtle.dropDown(turtle.getItemCount(slot:getItemSlot("dirt")) - 40)
  8277.                     end
  8278.                 end
  8279.                 slot.update{self = slot, item = "dirt", delete = true}
  8280.             end
  8281.             --[[if getStock("cobble") > 35 then
  8282.                 turtle.select(slot:getItemSlot("cobble"))
  8283.                 if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("cobble")) - 35) then --chest full
  8284.                     if not useOtherChest then
  8285.                         changeDirection("faceBackward")
  8286.                         go("LFFRR")
  8287.                         useOtherChest = true
  8288.                         turtle.select(slot:getItemSlot("cobble"))
  8289.                         if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("cobble")) - 35) then --chest full
  8290.                             turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 35)
  8291.                         end
  8292.                     else
  8293.                         turtle.select(slot:getItemSlot("cobble"))
  8294.                         turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 35)
  8295.                     end
  8296.                 end
  8297.                 slot.update{self = slot, item = "cobble", delete = true}
  8298.             end]]--
  8299.             if getStock("gravel") > 1 then
  8300.                 turtle.select(slot:getItemSlot("gravel")) -- could be on second chest already
  8301.                 if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("gravel")) - 1) then --chest full
  8302.                     if not useOtherChest then
  8303.                         changeDirection("faceBackward")
  8304.                         go("LFFRR")
  8305.                         useOtherChest = true
  8306.                         turtle.select(slot:getItemSlot("gravel"))
  8307.                         if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("gravel")) - 1) then --chest full
  8308.                             turtle.dropDown(turtle.getItemCount(slot:getItemSlot("gravel")) - 1)
  8309.                         end
  8310.                     else
  8311.                         turtle.select(slot:getItemSlot("gravel"))
  8312.                         turtle.dropDown(turtle.getItemCount(slot:getItemSlot("gravel")) - 1)
  8313.                     end
  8314.                 end
  8315.                 slot.update{self = slot, item = "gravel", delete = true}
  8316.             end
  8317.             if getStock("buckets") > 1 then
  8318.                 turtle.select(slot:getItemSlot("buckets"))
  8319.                 if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("buckets")) - 1) then-- chest full
  8320.                     if not useOtherChest then
  8321.                         useOtherChest = true
  8322.                         changeDirection("faceBackward")
  8323.                         go("LFFRR")
  8324.                         turtle.select(slot:getItemSlot("buckets"))
  8325.                         if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("buckets")) - 1) then-- chest full
  8326.                             turtle.dropDown(turtle.getItemCount(slot:getItemSlot("buckets")) - 1)
  8327.                         end
  8328.                     else
  8329.                         turtle.select(slot:getItemSlot("buckets"))
  8330.                         turtle.dropDown(turtle.getItemCount(slot:getItemSlot("buckets")) - 1)
  8331.                     end
  8332.                 end
  8333.                 slot.update{self = slot, item = "buckets", delete = true}
  8334.             end
  8335.             repeat
  8336.                 wasteFound = false
  8337.                 for i = 1, 12 do
  8338.                     if slot:getItemSlot(waste[i]) > 0 then
  8339.                         wasteFound = true
  8340.                         saveToLog("Storing mining waste: "..waste[i], false, true)
  8341.                         turtle.select(slot:getItemSlot(waste[i]))
  8342.                         if not turtle.dropUp() then --chest is full
  8343.                             if not useOtherChest then
  8344.                                 changeDirection("faceBackward")
  8345.                                 go("LFFRR")
  8346.                                 useOtherChest = true
  8347.                                 turtle.select(slot:getItemSlot(waste[i]))
  8348.                                 if not turtle.dropUp() then --chest full
  8349.                                     turtle.dropDown()
  8350.                                 end
  8351.                             else
  8352.                                 turtle.select(slot:getItemSlot(waste[i]))
  8353.                                 turtle.dropDown()
  8354.                             end
  8355.                         end
  8356.                         slot.update{self = slot, item = waste[i], delete = true}
  8357.                         sortInventory(true)
  8358.                     end
  8359.                 end
  8360.             until not wasteFound
  8361.             if useOtherChest then
  8362.                 go("FFL")
  8363.             end
  8364.         else
  8365.             for i = 1, 16 do
  8366.                 if slot:getSlotContains(i) == "" and turtle.getItemCount(i) > 0 then
  8367.                     doSort = true
  8368.                     turtle.select(i)
  8369.                     if not turtle.dropDown() then-- chest full
  8370.                         turtle.dropUp()
  8371.                     end
  8372.                 end
  8373.             end
  8374.             if doSort then
  8375.                 sortInventory(true)
  8376.             end
  8377.             if getStock("dirt") > 40 then
  8378.                 turtle.select(slot:getItemSlot("dirt"))
  8379.                 if not turtle.dropDown(turtle.getItemCount(slot:getItemSlot("dirt")) - 40) then-- chest full
  8380.                     turtle.dropUp(turtle.getItemCount(slot:getItemSlot("dirt")) - 40)
  8381.                 end
  8382.                 slot.update{self = slot, item = "dirt", delete = true}
  8383.             end
  8384.             if getStock("gravel") > 1 then
  8385.                 turtle.select(slot:getItemSlot("gravel"))
  8386.                 if not turtle.dropDown(turtle.getItemCount(slot:getItemSlot("gravel")) - 1) then-- chest full
  8387.                     turtle.dropUp(turtle.getItemCount(slot:getItemSlot("gravel")) - 1)
  8388.                 end
  8389.                 slot.update{self = slot, item = "gravel", delete = true}
  8390.             end
  8391.             --[[if getStock("cobble") > 35 then
  8392.                 turtle.select(slot:getItemSlot("cobble"))
  8393.                 if not turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 35) then --chest full
  8394.                     turtle.dropUp(turtle.getItemCount(slot:getItemSlot("cobble")) - 35)
  8395.                 end
  8396.                 slot.update{self = slot, item = "cobble", delete = true}
  8397.             end]]--
  8398.             repeat
  8399.                 wasteFound = false
  8400.                 for i = 1, 12 do
  8401.                     if slot:getItemSlot(waste[i]) > 0 then
  8402.                         wasteFound = true
  8403.                         saveToLog("Storing mining waste: "..waste[i], false, true)
  8404.                         turtle.select(slot:getItemSlot(waste[i]))
  8405.                         if not turtle.dropDown() then --chest is full
  8406.                             turtle.dropUp()
  8407.                         end
  8408.                         slot.update{self = slot, item = waste[i], delete = true}
  8409.                         sortInventory(true)
  8410.                     end
  8411.                 end
  8412.             until not wasteFound
  8413.         end
  8414.         if direction == "down" then
  8415.             up(1)
  8416.         end
  8417.     end
  8418. end
  8419.  
  8420. function storeItem(arg)
  8421.     -- storeItem{toStore = useStore, item = "moss stone", quantity = 0, updateSlot = true, doSort = true}
  8422.     local storeIndex = 0
  8423.     local slotNum = 0
  8424.    
  8425.     if arg.doSort == nil then
  8426.         arg.doSort = false
  8427.     end
  8428.     if arg.updateSlot == nil then
  8429.         arg.updateSlot = false
  8430.     end
  8431.     arg.direction = arg.direction or "down"
  8432.     if arg.slotNo ~= nil then
  8433.         if arg.quantity == nil or arg.quantity == 0 then
  8434.             arg.quantity = turtle.getItemCount(arg.slotNo)
  8435.         elseif arg.quantity == -1 then --store all but 1 item
  8436.             arg.quantity = turtle.getItemCount(arg.slotNo) - 1
  8437.         end
  8438.         saveToLog("Storing "..arg.quantity.." "..arg.item.." from turtle slot "..arg.slotNo.." to chest "..arg.toStore:getStoreName(), false)
  8439.         turtle.select(arg.slotNo)
  8440.         if arg.direction == "down" then
  8441.             turtle.dropDown()
  8442.         elseif arg.direction == "forward" then
  8443.             turtle.drop()
  8444.         else
  8445.             turtle.dropUp()
  8446.         end
  8447.         arg.toStore.addItem(arg.toStore, arg.item, arg.quantity)
  8448.     else
  8449.         if slot:getItemSlot(arg.item) ~= nil then --itemSlot["moss stone"] exists
  8450.             slotNum = slot:getItemSlot(arg.item) --eg turtle slot 14 contains moss stone
  8451.             if slotNum > 0 then --eg sticks in turtle slot 8
  8452.                 if arg.quantity == 0 then --drop all
  8453.                     arg.quantity = turtle.getItemCount(slotNum)
  8454.                 elseif arg.quantity == -1 then --store all but 1 item
  8455.                     arg.quantity = turtle.getItemCount(slotNum) - 1
  8456.                 end
  8457.                 if arg.quantity > 0 then
  8458.                     -- saveToLog("storeItem: arg.quantity = "..arg.quantity, false)
  8459.                     -- saveToLog("storeItem: arg.item = "..arg.item, false)
  8460.                     -- saveToLog("storeItem: slotNum = "..slotNum, false)
  8461.                     -- saveToLog("storeItem: arg.toStore:getStoreName() = "..tostring(arg.toStore.getStoreName(arg.toStore)), false)
  8462.                     saveToLog("Storing "..arg.quantity.." "..arg.item.." from turtle slot "..slotNum.." to chest "..arg.toStore:getStoreName(), false)
  8463.                     turtle.select(slotNum)
  8464.                     if arg.direction == "down" then
  8465.                         turtle.dropDown(arg.quantity)
  8466.                     elseif arg.direction == "forward" then
  8467.                         turtle.drop(arg.quantity)
  8468.                     else
  8469.                         turtle.dropUp(arg.quantity)
  8470.                     end
  8471.                     arg.toStore.addItem(arg.toStore, arg.item, arg.quantity, slot:getItemSlot(arg.item))
  8472.                 end
  8473.                 if arg.updateSlot then
  8474.                     slot.update{self = slot, item = arg.item, delete = true}
  8475.                 end
  8476.                 if arg.doSort then
  8477.                     sortInventory(true)
  8478.                 end
  8479.             end
  8480.         end
  8481.     end
  8482. end
  8483.  
  8484. function storeLadders()
  8485.     if getStock("ladders") > 0 then
  8486.         currentFunction = "storeLadders"
  8487.         changeDirection("faceForward")
  8488.         go("FFLFFRR")
  8489.         saveToLog("storeLadders: storing excess ladders", true, false)
  8490.         storeItem{toStore = storageWoodItems, direction = "down", item = "ladders", quantity = 0, updateSlot = true, doSort = true}
  8491.         go("FFRFFRR")
  8492.     end
  8493. end
  8494.  
  8495. function storeSand()
  8496.     local sandFound = false
  8497.     local cobbleAmount = 0
  8498.    
  8499.     currentFunction = "storeSand"
  8500.     if getStock("sand") > 0 then -- already identified
  8501.         sandFound = true
  8502.     else
  8503.         changeDirection("faceBackward")
  8504.         go("FFUU")
  8505.         saveToLog("storeSand: checking for sand", true, false)
  8506.         for i = 1, 16 do
  8507.             if string.find(slot:getSlotContains(i), "item") ~= nil then --item1, item 2 etc
  8508.                 turtle.select(i)
  8509.                 if turtle.placeDown() then -- only sand, ores, obsidian, mossy cobble
  8510.                     saveToLog("storeSand: "..slot:getSlotContains(i).." placed down", true, false)
  8511.                     sleep(0.5)
  8512.                     if turtle.detectDown() then
  8513.                         turtle.digDown()
  8514.                     else --dropped must be sand
  8515.                         saveToLog("storeSand: "..slot:getSlotContains(i).." not detected below", true, false)
  8516.                         down(1)
  8517.                         if turtle.compareDown() then
  8518.                             saveToLog("storeSand: "..slot:getSlotContains(i).." compares, sand found", true, false)
  8519.                             turtle.digDown()
  8520.                             slot.update{self = slot, slotNo = i, newItem = "sand"}
  8521.                             sandFound = true
  8522.                         else
  8523.                             saveToLog("storeSand: "..slot:getSlotContains(i).." does not compare, redstone found", true, false)
  8524.                             turtle.digDown()
  8525.                             slot.update{self = slot, slotNo = i, newItem = "redstone"}
  8526.                         end
  8527.                         sortInventory(true)
  8528.                         up(1)
  8529.                         break
  8530.                     end
  8531.                 end
  8532.             end
  8533.         end
  8534.         go("DDRRFF")
  8535.     end  
  8536. end
  8537.  
  8538. function storeSigns()
  8539.     if getStock("signs") > 0 then
  8540.         currentFunction = "storeSigns"
  8541.         changeDirection("faceForward")
  8542.         go("FFLFFRR")
  8543.         saveToLog("storeSigns: storing excess signs", true, false)
  8544.         storeItem{toStore = storageWoodItems, direction = "down", item = "signs", quantity = 0, updateSlot = true, doSort = true}
  8545.         go("FFRFFRR")
  8546.     end
  8547. end
  8548.  
  8549. function storeSticks()
  8550.     if getStock("sticks") > 0 then
  8551.         currentFunction = "storeSticks"
  8552.         changeDirection("faceForward")
  8553.         go("FFRFF")
  8554.         saveToLog("storeSticks: storing excess sticks", true, false)
  8555.         storeItem{toStore = storageSticks, direction = "down", item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  8556.         go("RRFFLFFRR")
  8557.     end
  8558. end
  8559.  
  8560. function storeWoodItems()
  8561.     currentFunction = "storeWoodItems"
  8562.     changeDirection("faceForward")
  8563.     go("FFLFFRR")
  8564.     saveToLog("storeWoodItems: storing planks, fences and other wooden items", true, false)
  8565.     for i = 1, 16 do
  8566.         if string.find(slot:getSlotContains(i), "item") ~= nil then --item1, item 2 etc
  8567.             turtle.select(i)
  8568.             if turtle.refuel(0) then
  8569.                 saveToLog("storeWoodItems: storing "..slot:getSlotContains(i), true, false)
  8570.                 turtle.dropDown()
  8571.                 slot.update{self = slot, item = slot:getSlotContains(i), delete = true}
  8572.                 sortInventory(true)
  8573.             end
  8574.         end
  8575.     end
  8576.     go("FFRFFRR")
  8577. end
  8578.  
  8579. function turnLeft(steps)
  8580.     for i = 1, steps do
  8581.         turtle.turnLeft()
  8582.         location:setFacing(location:getFacing() - 1)
  8583.         saveStatus()
  8584.     end
  8585. end
  8586.  
  8587. function turnRight(steps)
  8588.     for i = 1, steps do
  8589.         turtle.turnRight()
  8590.         location:setFacing(location:getFacing() + 1)
  8591.         saveStatus()
  8592.     end
  8593. end
  8594.  
  8595. function up(steps, useSlot, itemList, calledFrom)
  8596.     local success = true
  8597.     local maxTries = 2
  8598.     local previousFunction = currentFunction
  8599.     currentFunction = "up"
  8600.    
  8601.     useSlot = useSlot or 1
  8602.     if turtle.getFuelLevel() < steps then
  8603.         saveToLog(" ***** up: Fuel < "..steps.." *****", true, false)
  8604.         if not refuel(0) then
  8605.             saveToLog(" ***** up: refuel failed*****", true, false)
  8606.         end
  8607.     end
  8608.     for i = 1, steps do
  8609.         if turtle.detectUp() then
  8610.             while turtle.detectUp() do
  8611.                 if dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom} then --not bedrock
  8612.                 --allow for sand and gravel
  8613.                     sleep(.7)
  8614.                 else -- at bedrock
  8615.                     success = false
  8616.                     break
  8617.                 end
  8618.             end
  8619.         end
  8620.         if success then
  8621.             while not turtle.up() do
  8622.                 dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
  8623.                 if attack() then
  8624.                     saveToLog("up: mob attacked above!", true)
  8625.                 end
  8626.                 maxTries = maxTries - 1
  8627.                 if maxTries <= 0 then
  8628.                     if turtle.getFuelLevel() == 0 then
  8629.                         if refuel(0) then
  8630.                             maxTries = 2
  8631.                         else
  8632.                             saveToLog("forward: Out of fuel. Game Over!", true)
  8633.                             error()
  8634.                         end
  8635.                     end
  8636.                 end
  8637.             end
  8638.             success = true
  8639.             location:setY(location:getY() + 1)
  8640.             saveStatus()
  8641.         end
  8642.     end
  8643.    
  8644.     currentFunction = previousFunction
  8645.     return success
  8646. end
  8647.  
  8648. function main()            
  8649.     initialise()            -- Setup global variables
  8650.     getPreferences()        -- setup or read logfile preferences
  8651.     getSupplies()           -- ask player for dirt, cobble, stone, gravel, bucket, wood
  8652.     getCoords()             -- Get input from player
  8653.     getReady()              -- Create chests, ladders, signs and refuel
  8654.     findCobble()            -- Get enough cobble to craft furnace and build base camp
  8655.     clearBase()             -- Clear area around base, place chests, make torches
  8656.     completeMineshaft()     -- dig down to bedrock / up to surface
  8657.     mineAll()               -- go to level 8 below current and begin
  8658. end
  8659. --**************Functions and classes/objects above***************--
  8660. --****************************************************************--
  8661. --***********************PROGRAM START****************************--
  8662.  
  8663. main() -- Entire Program runs from here
Add Comment
Please, Sign In to add comment