Inksaver

SelfReplicate original 2014

Dec 9th, 2018
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 409.20 KB | None | 0 0
  1. version = "start.lua 1.1.1"
  2. pastebinCode = "JVD1RPfg"
  3. -- http://pastebin.com/JVD1RPfg
  4. -- origially pasted in 2014. The updated 2018 version is using the original pastebin code: http://pastebin.com/2Zf5an8H
  5. local function initialise()
  6.     -- create classes/objects
  7.     createCoordObject()
  8.     createFurnaceObject()
  9.     createChestObject()
  10.     createSlotObject()
  11.     createLogfileObject()
  12.     createTreeFarmObject()
  13.     createDigObject()
  14.     createPlaceStorageObject()
  15.  
  16.     -- set global variables. As each item is identified, set objectives["item"] = true
  17.     objectives = {}
  18.     local items = {}
  19.     items = {"wood","wood2","dirt","cobble","stone","saplings","saplings2","seeds","sand","gravel","clay",
  20.                 "apples","coal","charcoal","ironore","?ironore","goldore","?goldore","iron","?iron","gold","?gold",
  21.                 "redstone","redstone block","?diamonds","diamonds","emeralds","sugar cane","planks","planks2","chests","sticks",
  22.                 "torches","furnaces","signs","item1","item2","item3","item4","item5","item6","item7","item8",
  23.                 "fences","nuggets","buckets","paper","computer","crafting table","diamond pickaxe","glass","glass panes","floppy disk",
  24.                 "disk drive","turtle","crafty mining turtle","?moss stone","moss stone","walls","obsidian","sandstone",
  25.                 "?lapis","lapis","lapis block","flowers","roses","red mushrooms","brown mushrooms"}
  26.                
  27.     for key, value in ipairs(items) do
  28.         objectives[value] = false
  29.     end
  30.    
  31.     --special objectives as script progresses:
  32.     objectives["smelt iron"] = false                -- confirms ironore identified, iron and buckets crafted
  33.     objectives["smelt gold"] = false                -- confirms goldore identified, crafted to gold nuggets and stored in extended storage
  34.     objectives["place extended storage"] = false    -- 5 chests placed behind starting point to store goldore, lapis, obsidian, mossy cobblestone
  35.     objectives["choose wood"] = false               -- After harvestAllTrees() finished, treeFarm tree type identified, wood2 and saplings2 eliminated
  36.     objectives["goMining1"] = false                 -- During harvestAllTrees()
  37.     objectives["clear treefarm"] = false            -- Treefarm cleared and planted
  38.     objectives["complete sand"] = false             -- min 7 sand found, no more needed
  39.     objectives["complete sugar cane"] = false       -- min 3 sugar cane found, no more needed
  40.     objectives["diamond pickaxe"] = false           -- Diamond pickaxe created as part of identifying diamonds. Stored in storagePickaxes
  41.     objectives["pave treefarm"] = false             -- Decorates Treefarm, as not enough cobble when first planted
  42.     objectives["pave extendedStorage"] = false      -- Decorates extendedStorage area, if not enough cobble when created
  43.    
  44.     -- 10 mineshafts down to level 40 during harvestAllTrees() set to true if shaft sunk
  45.     -- shaft may not be mined if insufficient fuel. Check for mined shafts not yet implemented
  46.     mineshaft = {}
  47.     for i = 1, 10 do
  48.         mineshaft[i] = false
  49.     end
  50.    
  51.     startTime = os.time() -- used to time functions, and allow adjustments to return to base every 24 hours minimum
  52. end
  53.  
  54. function attack()
  55.     local mobAttacked = false
  56.     local itemCount = {}
  57.    
  58.     for i = 1, 16 do
  59.         itemCount[i] = turtle.getItemCount(i)
  60.     end
  61.    
  62.     turtle.select(1)
  63.     if turtle.attack() then
  64.         saveToLog("attack: Mob attacked in front!", true, false)
  65.         sleep(1.5)
  66.         while turtle.attack() do --in case mob in front
  67.             saveToLog("attack: Mob attacked in front again!", true, false)
  68.             sleep(1.5)
  69.         end
  70.         mobAttacked = true
  71.     end
  72.     if turtle.attackUp() then
  73.         saveToLog("attack: Mob attacked above!", true, false)
  74.         sleep(1.5)
  75.         while turtle.attackUp() do --in case mob in front
  76.             saveToLog("attack: Mob attacked above again!", true, false)
  77.             sleep(1.5)
  78.         end
  79.         mobAttacked = true
  80.     end
  81.     if turtle.attackDown() then
  82.         saveToLog("attack: Mob attacked below!", true, false)
  83.         sleep(1.5)
  84.         while turtle.attackDown() do --in case mob in front
  85.             saveToLog("attack: Mob attacked below again!", true, false)
  86.             sleep(1.5)
  87.         end
  88.         mobAttacked = true
  89.     end
  90.     if mobAttacked then --remove any mob drops
  91.         for i = 1, 16 do
  92.             if itemCount[i] == 0 then --check only previously empty slots
  93.                 if turtle.getItemCount(i) > 0 then
  94.                     turtle.select(i)
  95.                     while turtle.dropUp() do
  96.                         saveToLog ("attack: dumping mob drops", true, false)
  97.                     end
  98.                 end
  99.             end
  100.         end
  101.     end
  102.    
  103.     return mobAttacked
  104. end
  105.  
  106. function back(steps, useSlot, itemList)
  107.     useSlot = useSlot or 1
  108.     itemList = itemList or ""
  109.    
  110.     for i = 1, steps do
  111.         if not turtle.back() then --cant move back
  112.             turnRight(2)
  113.             forward(1, useSlot, itemList)
  114.             turnRight(2)
  115.         end
  116.         changeCoords("back")
  117.     end
  118. end
  119.  
  120. function changeCoords(direction)
  121.     --[[0 = go south (z increases)
  122.         1 = go west  (x decreases)
  123.         2 = go north (z decreases
  124.         3 = go east  (x increases)]]--
  125.        
  126.     if direction == "forward" then
  127.         if location:getFacing() == 0 then
  128.             --zCoord = zCoord + 1
  129.             location:setZ(location:getZ() + 1)
  130.         elseif location:getFacing() == 1 then
  131.             --xCoord = xCoord - 1
  132.             location:setX(location:getX() - 1)
  133.         elseif location:getFacing() == 2 then
  134.             --zCoord = zCoord - 1
  135.             location:setZ(location:getZ() - 1)
  136.         else
  137.             --xCoord = xCoord + 1
  138.             location:setX(location:getX() + 1)
  139.         end
  140.     elseif direction == "back" then
  141.         if location:getFacing() == 0 then
  142.             --zCoord = zCoord - 1
  143.             location:setZ(location:getZ() - 1)
  144.         elseif location:getFacing() == 1 then
  145.             --xCoord = xCoord + 1
  146.             location:setX(location:getX() + 1)
  147.         elseif location:getFacing() == 2 then
  148.             --zCoord = zCoord + 1
  149.             location:setZ(location:getZ() + 1)
  150.         else
  151.             --xCoord = xCoord - 1
  152.             location:setX(location:getX() - 1)
  153.         end
  154.     end
  155. end
  156.  
  157. function changeDirection(direction)
  158.     --changeDirection("faceMine")
  159.     --changeDirection("faceExtraStorage")
  160.     if direction == "faceMine" then
  161.         while location:getFacing() ~= coordHome:getFacing() do
  162.             turnRight(1)
  163.         end
  164.     else
  165.         if location:getFacing() == coordHome:getFacing() then
  166.             turnRight(2)
  167.         end
  168.     end
  169. end
  170.  
  171. function checkBedrock(itemList)
  172.     local success = false
  173.    
  174.     if turtle.detect() then --block in front so not air/water/lava
  175.         if not dig.digNew{self = dig, checkForItems = itemList, callFrom = "checkBedrock"} then --block not broken, so must be bedrock
  176.             success = true
  177.         end
  178.     end
  179.    
  180.     return success
  181. end
  182.  
  183. function checkDiamonds()
  184.     local numDiamonds = 0
  185.    
  186.     if objectives["diamond pickaxe"] then
  187.         numDiamonds = 3
  188.     end
  189.     if slot:getItemSlot("diamonds") > 0 then
  190.         numDiamonds = numDiamonds + turtle.getItemCount(slot:getItemSlot("diamonds"))
  191.     end
  192.    
  193.     return numDiamonds
  194. end
  195.  
  196. function checkDigDown()
  197.     local useSlot = 0
  198.     local useItem = ""
  199.     local useAmount = 0
  200.     local yCoordTemp = 0
  201.     local checkList = {}
  202.     local result = "onLand"
  203.    
  204.     checkList[1] = "wood"
  205.     checkList[2] = "wood2"
  206.     checkList[3] = "sand"
  207.     --checkList[4] = "item"
  208.    
  209.     turtle.select(1)
  210.     while not turtle.detectDown() do --could be water, lava or air
  211.         saveToLog("checkDigDown: checking for water", true)
  212.         if checkWater("down") then -- water found.
  213.             result = "onWater"
  214.             break
  215.         else --no water so go down
  216.             --check if in a cave or ravine
  217.             result = "onLand"
  218.             if location:getY() <= 55 then
  219.                 --place block and exit
  220.                 saveToLog("checkDigDown: Ravine or cave detected, building bridge")
  221.                 if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  222.                     turtle.select(slot:getItemSlot("cobble"))
  223.                 elseif turtle.getItemCount(slot:getItemSlot("dirt")) > 1 then
  224.                     turtle.select(slot:getItemSlot("dirt"))
  225.                 end
  226.                 turtle.placeDown()
  227.                 break
  228.             end
  229.             down(1, 1)
  230.             saveToLog("checkDigDown: Moved down y = "..location:getY(), false)
  231.         end
  232.     end --block underneath at bottom of air/water, ready to dig or check
  233.     --see if block below is known
  234.     saveToLog("checkDigDown: at base level "..result, false)
  235.     if result == "onLand" then --default
  236.         useSlot, useAmount, useItem = compareBlock("down", checkList, 3)  --does block below match wood,wood2,sand
  237.         turtle.select(1)
  238.         -- if wood, wood2, harvest
  239.         if useItem == "wood" or useItem == "wood2" then
  240.             saveToLog("checkDigDown: digDown: "..useItem, false)
  241.             --turtle.select(1)
  242.             turtle.select(useSlot)
  243.             while turtle.compareDown() do
  244.                 down(1, 1) --will automatically dig
  245.                 turtle.select(useSlot)
  246.             end
  247.         elseif useItem == "sand" then
  248.             yCoordTemp = location:getY()
  249.             if turtle.getItemCount(useSlot) < 8 then
  250.                 turtle.select(useSlot)
  251.                 while turtle.compareDown() do
  252.                     saveToLog("checkDigDown: compareDown = "..useItem, false)
  253.                     down(1, 1, "sand,gravel", "checkDigDown")
  254.                     turtle.select(useSlot)
  255.                 end
  256.                 turtle.select(slot:getItemSlot("dirt"))
  257.                 while location:getY() < yCoordTemp do
  258.                     up(1, 1)
  259.                     if turtle.getItemCount(slot:getItemSlot("dirt")) > 0 then
  260.                         turtle.placeDown()
  261.                     end
  262.                 end
  263.             else
  264.                 saveToLog("checkDigDown: digDown: sand found but not mined", false)
  265.             end
  266.         elseif string.find(useItem, "item") ~= nil then
  267.             dig.digNew{self = dig, direction = "down", checkForItems = "sand", callFrom = "checkDigDown"}
  268.         end
  269.     end
  270.    
  271.     return result --"onLand", "onWater"
  272. end
  273.  
  274. function checkDigUp()
  275.     while turtle.detect() do --if block in front move up
  276.         if turtle.detectUp() then
  277.             dig.digNew{self = dig, direction = "up", slotNo = 1, checkForItems = "wood2,saplings", callFrom = "checkDigUp"}
  278.             if dig:getSuccess() then --success digging. If dirt/grass itemcount ++
  279.                 --see if  1 item in slot 16 matches anything
  280.                 if dig:getDugItem() == "wood2" then
  281.                     saveToLog("harvesting new tree type, wood2", false)
  282.                     harvestTree("wood2")
  283.                     break
  284.                 end
  285.             end
  286.         end
  287.         up(1, 1)
  288.     end
  289. end
  290.  
  291. function checkForLooseSaplings()
  292.     --pick up loose ?saplings
  293.     dig.digNew{self = dig, suck = true, checkForItems = "saplings", callFrom = "checkForLooseSaplings"} --forward, slot1 by default
  294. end
  295.  
  296. function checkGravel()
  297.     --[[place gravel repeatedly. eventually will produce flint
  298.         which cannot be placed, need 2 blocks for comparison]]--
  299.     local tempyCoord = location:getY()
  300.     local emptySlot = 0
  301.     local slotCount = 0
  302.     local reRun = false
  303.     local doBreak = false
  304.     local gravelFound = false
  305.     local sTime = os.time()
  306.    
  307.     if slot:getItemSlot("gravel") == 0 or slot:getItemSlot("sand") == 0 then
  308.         if slot:getItemSlot("gravel") == 0 and slot:getItemSlot("sand") == 0 then
  309.             saveToLog("checkGravel: testing for gravel and sand")
  310.         elseif slot:getItemSlot("gravel") == 0 then
  311.             saveToLog("checkGravel: sand already found, testing for gravel")
  312.         else
  313.             saveToLog("checkGravel: gravel already found, testing for sand")
  314.         end
  315.         emptySlot = getFirstEmptySlot(true)
  316.         saveToLog("checkGravel: testing slot "..slot:getLeastUnknownItemSlot().." to "..slot:getMostUnknownItemSlot(), false)
  317.         --gravel, iron ore, sand, coal may have been found
  318.         --clear the area
  319.         saveToLog("checkGravel: clearing test area", true)
  320.         dig.digNew{self = dig, direction = "forward", callFrom = "checkGravel"}
  321.         up(1, 1) --move up 1 place
  322.         dig.digNew{self = dig, direction = "forward", callFrom = "checkGravel"}
  323.         repeat
  324.             reRun = false
  325.             if slot:getLeastUnknownItemSlot() > 0 then --updated when sortInventory(true) is run
  326.                 for i = slot:getLeastUnknownItemSlot(), slot:getMostUnknownItemSlot() do
  327.                     saveToLog("checkGravel:  testing slot "..i.." ("..slot:getSlotContains(i)..")", true)
  328.                     turtle.select(i)
  329.                     saveToLog("Placing ? sand/gravel..")
  330.                     if turtle.place() then --sand, gravel, iron ore,
  331.                         saveToLog("checkGravel: ? sand/gravel placed, waiting for drop", true)
  332.                         sleep(.5)
  333.                         if turtle.detect() then --cobble, ironore
  334.                             saveToLog("checkGravel: slot "..i.." is not gravel or sand", true)
  335.                             dig.digNew{self = dig, callFrom = "checkGravel"}
  336.                             if slot:getItemSlot("cobble") > 0 then --cobble known
  337.                                 slot.update{self = slot, slotNo = i, newItem = "?ironore"}
  338.                                 doBreak = true
  339.                                 reRun = true
  340.                                 break
  341.                             --else
  342.                             --continue with for loop (no break)
  343.                             --reRun already false
  344.                             end
  345.                         else
  346.                             saveToLog("checkGravel: gravel / sand fell")
  347.                             down(1)
  348.                             if slot:getItemSlot("gravel") > 0 then --gravel found
  349.                                 slot.update{self = slot, slotNo = i, newItem = "sand"}
  350.                                 dig.digNew{self = dig, callFrom = "checkGravel"} --will recover sand
  351.                             else
  352.                                 if turtle.getItemCount(i) > 0 then -- at least 1 ?gravel /?sand remaining
  353.                                     slotCount = turtle.getItemCount(i)
  354.                                     dig.digNew{self = dig, callFrom = "checkGravel"} --will recover either sand, gravel or flint, which would go to emptySlot
  355.                                     if turtle.getItemCount(i) == slotCount then -- flint dug
  356.                                         turtle.select(emptySlot)
  357.                                         while turtle.drop() do --drop flint
  358.                                             saveToLog("checkGravel: Dumping flint Found in slot "..emptySlot, false)
  359.                                         end
  360.                                         slot.update{self = slot, slotNo = emptySlot, delete = true}
  361.                                         slot.update{self = slot, slotNo = i, newItem = "gravel"}
  362.                                     else -- gravel or sand dug
  363.                                         if slot:getItemSlot("sand") == 0 then -- sand unknown
  364.                                             saveToLog("Checking if slot "..i.." is gravel")
  365.                                             turtle.select(i)
  366.                                             for j = 1, 100 do
  367.                                                 if turtle.place() then --gravel placed
  368.                                                     turtle.dig()
  369.                                                     if turtle.getItemCount(emptySlot) > 0 then --flint found   
  370.                                                         saveToLog("checkGravel: Gravel Found in slot "..i, true)
  371.                                                         slot.update{self = slot, slotNo = i, newItem = "gravel"}
  372.                                                         turtle.select(emptySlot)
  373.                                                         turnRight(2)
  374.                                                         while turtle.drop() do --drop flint
  375.                                                             saveToLog("checkGravel: Dumping flint Found in slot "..emptySlot, false)
  376.                                                         end
  377.                                                         turnRight(2)
  378.                                                         gravelFound = true
  379.                                                         break
  380.                                                     end
  381.                                                 end
  382.                                             end
  383.                                             if not gravelFound then
  384.                                                 saveToLog("checkGravel: Sand Found in slot "..i, true)
  385.                                                 slot.update{self = slot, slotNo = i, newItem = "sand"}
  386.                                             end
  387.                                         else --sand known therefore gravel just dug
  388.                                             saveToLog("checkGravel: Gravel Found in slot "..i, true)
  389.                                             slot.update{self = slot, slotNo = i, newItem = "gravel"}
  390.                                         end
  391.                                     end
  392.                                 else -- only 1 remaining so dump anyway
  393.                                     saveToLog("checkGravel: only one item in slot "..i, true)
  394.                                     turnRight(2)
  395.                                     while turtle.drop() do --drop flint
  396.                                         saveToLog("checkGravel: Dumping item in slot "..emptySlot, false)
  397.                                     end
  398.                                     turnRight(2)
  399.                                     slot.update{self = slot, slotNo = i, delete = true}
  400.                                 end
  401.                             end
  402.                             doBreak = true
  403.                             reRun = true
  404.                             up(1)-- go up again ready for testing next item (gravel/sand/flint only -NOT cobble)
  405.                             break                          
  406.                         end
  407.                     else -- flint, coal
  408.                         if turtle.refuel(0) then
  409.                             slot.update{self = slot, slotNo = i, newItem = "coal"}
  410.                             saveToLog("checkGravel: coal found in slot "..i, false)
  411.                         else
  412.                             turnRight(2)
  413.                             while turtle.drop() do --drop flint
  414.                                 saveToLog("checkGravel: Dumping flint Found in slot "..i, false)
  415.                             end
  416.                             turnRight(2)
  417.                             slot.update{self = slot, slotNo = i, delete = true}
  418.                         end
  419.                         doBreak = true
  420.                         reRun = true
  421.                         break
  422.                     end
  423.                 end
  424.                 if doBreak then
  425.                     sortInventory(true)
  426.                 end
  427.             end
  428.         until not reRun
  429.            
  430.         while tempyCoord < location:getY() do
  431.             down(1)
  432.         end
  433.        
  434.         saveToLog("checkGravel started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
  435.     else
  436.         saveToLog("checkGravel: sand and gravel already known", true)
  437.     end
  438. end
  439.  
  440. function checkItems(arg)
  441.     --checkItems{keepTorches = 2 + torchesNeeded, keepSigns = 1}
  442.     --checkItems{keepTorches = 2}
  443.     --checkItems{keepSticks = 0, keepTorches = 16, keepSigns = 0}
  444.     local numSticksOnboard = 0
  445.     local numTorchesOnboard = 0
  446.     local numSignsOnboard = 0
  447.     local numWoodStored = 0
  448.     local numSticksStored = 0
  449.     local numTorchesStored = 0
  450.     local numSignsStored = 0
  451.     local getTorches = false
  452.     local getSigns = false
  453.     local makeTorches = false
  454.     local makeSigns = false
  455.    
  456.     arg.keepSticks = arg.keepSticks or 0
  457.     arg.keepTorches = arg.keepTorches or 0
  458.     arg.keepSigns = arg.keepSigns or 0
  459.     arg.keepWood = arg.keepWood or 64
  460.  
  461.     changeDirection("faceMine")
  462.     saveToLog("checkItems: keepWood = "..arg.keepWood..
  463.                     " keepSticks = "..tostring(arg.keepSticks)..
  464.                     " keepTorches = "..tostring(arg.keepTorches)..
  465.                     " keepSigns = "..tostring(arg.keepSigns), false)
  466.  
  467.     numTorchesStored = storageTorches:getItemCount("torches")
  468.     numSignsStored = storageSigns:getItemCount("signs")
  469.     numSticksStored = storageSticks:getItemCount("sticks")
  470.     numWoodStored = storageWood:getItemCount("wood")
  471.     if slot:getItemSlot("torches") > 0 then
  472.         numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
  473.     end
  474.     if slot:getItemSlot("signs") > 0 then
  475.         numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
  476.     end
  477.     if slot:getItemSlot("sticks") > 0 then
  478.         numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
  479.     end
  480.  
  481.     --check if any signs or torches need to be crafted
  482.     --if so store torches and signs first, craft, then re-stock
  483.    
  484.  
  485.     if arg.keepTorches > 0 then
  486.         if numTorchesOnboard >= arg.keepTorches then
  487.             saveToLog("checkItems: "..numTorchesOnboard.." torches already in stock", false)
  488.             success = true
  489.         elseif numTorchesStored >= arg.keepTorches then
  490.             saveToLog("checkItems: "..numTorchesStored.." torches are in storage", false)
  491.             getTorches = true
  492.         elseif numTorchesOnboard + numTorchesStored >= arg.keepTorches then
  493.             saveToLog("checkItems: "..numTorchesOnboard + numTorchesStored.." torches are in stock / storage", false)
  494.             getTorches = true
  495.         else -- not enough onBoard or store
  496.             saveToLog("checkItems: insufficient torches in stock / store, crafting more", false)
  497.             makeTorches = true
  498.         end
  499.     end
  500.     if arg.keepSigns > 0 then
  501.         if numSignsOnboard >= arg.keepSigns then
  502.             saveToLog("checkItems: "..numSignsOnboard.." signs already in stock", false)
  503.             success = true
  504.         elseif numSignsStored >= arg.keepSigns then
  505.             saveToLog("checkItems: "..numSignsStored.." signs are in storage", false)
  506.             getSigns = true
  507.         elseif numSignsOnboard + numSignsStored >= arg.keepSigns then
  508.             saveToLog("checkItems: "..numSignsOnboard + numSignsStored.." signs are in stock / storage", false)
  509.             getSigns = true
  510.         else -- not enough onBoard or store
  511.             saveToLog("checkItems: insufficient signs in stock / store, crafting more", false)
  512.             makeSigns = true
  513.         end
  514.     end
  515.     if arg.keepSticks > 0 then
  516.         if numSticksOnboard >= arg.keepSticks then
  517.             saveToLog("checkItems: "..numSticksOnboard.." sticks already in stock", false)
  518.             success = true
  519.         elseif numSticksStored >= arg.keepSticks then
  520.             saveToLog("checkItems: "..numSticksStored.." sticks are in storage", false)
  521.             getSticks = true
  522.         elseif numSticksOnboard + numSticksStored >= arg.keepSticks then
  523.             saveToLog("checkItems: "..numSticksOnboard + numSticksStored.." sticks are in stock / storage", false)
  524.             getSticks = true
  525.         else -- not enough onBoard or store
  526.             saveToLog("checkItems: insufficient sticks in stock / store, crafting more", false)
  527.             makeSticks = true
  528.         end
  529.     end
  530.     if makeSticks then --store existing torches/signs first
  531.         if placeStorage:getStoragePlaced("torches") then
  532.             storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
  533.             if arg.keepTorches > 0 then
  534.                 getTorches = true
  535.             end
  536.         end
  537.         if placeStorage:getStoragePlaced("signs") then
  538.             forward(2, 1)
  539.             turnRight(2)
  540.             storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  541.             if arg.keepSigns > 0 then
  542.                 getSigns = true
  543.             end
  544.             forward(2, 1)
  545.             turnRight(2)
  546.         end
  547.         if numSticksStored > 0 then --get stored sticks
  548.             forward(4, 1)
  549.             turnRight(2)
  550.             getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
  551.             forward(4, 1)
  552.             turnRight(2)
  553.         end
  554.         craftSticks(arg.keepSticks - numSticksOnboard)
  555.     end
  556.     if makeSigns then --store existing torches first
  557.         if placeStorage:getStoragePlaced("torches") then
  558.             storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
  559.             if arg.keepTorches > 0 then
  560.                 getTorches = true
  561.             end
  562.         end
  563.         if numSignsStored > 0 then --get stored signs
  564.             forward(2, 1)
  565.             turnRight(2)
  566.             getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
  567.             forward(2, 1)
  568.             turnRight(2)
  569.         end
  570.         craftSigns(3, true)
  571.     end
  572.     if makeTorches then --store existing signs first
  573.         if placeStorage:getStoragePlaced("signs") then
  574.             forward(2, 1)
  575.             turnRight(2)
  576.             storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  577.             if arg.keepSigns > 0 then
  578.                 getSigns = true
  579.             end
  580.             forward(2, 1)
  581.             turnRight(2)
  582.         end
  583.         if numTorchesStored > 0 then --get stored torches
  584.             getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
  585.         end
  586.         craftTorches(arg.keepTorches - numTorchesOnboard)
  587.     end
  588.    
  589.     if getSticks then
  590.         forward(4, 1)
  591.         getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
  592.         back(4)
  593.     end
  594.     if slot:getItemSlot("sticks") > 0 then
  595.         numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
  596.     end
  597.     if numSticksOnboard - arg.keepSticks > 0 then --store sticks
  598.         if placeStorage:getStoragePlaced("sticks") then
  599.             forward(4, 1)
  600.             turnRight(2)
  601.             saveToLog("checkItems: toStore = "..storageSticks:getStoreName().." storing "..numSticksOnboard - arg.keepSticks.." sticks", true)
  602.             if arg.keepSticks > 0 then
  603.                 storeItem{toStore = storageSticks, item = "sticks", quantity = numSticksOnboard - arg.keepSticks, updateSlot = true, doSort = false}
  604.             else
  605.                 storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  606.             end
  607.             forward(4, 1)
  608.             turnRight(2)
  609.         end
  610.     end
  611.    
  612.     if getTorches then
  613.         getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
  614.     end
  615.     if slot:getItemSlot("torches") > 0 then
  616.         numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
  617.     end
  618.     if numTorchesOnboard - arg.keepTorches > 0 then
  619.         if placeStorage:getStoragePlaced("torches") then
  620.             saveToLog("checkItems: storing "..numTorchesOnboard - arg.keepTorches.." torches in storageTorches", true)
  621.             if arg.keepTorches > 0 then
  622.                 storeItem{toStore = storageTorches, item = "torches", quantity = numTorchesOnboard - arg.keepTorches, updateSlot = true, doSort = false}
  623.             else
  624.                 storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
  625.             end
  626.         end
  627.     end
  628.    
  629.     if getSigns then
  630.         forward(2, 1)
  631.         turnRight(2)
  632.         getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
  633.         forward(2, 1)
  634.         turnRight(2)
  635.     end
  636.     if slot:getItemSlot("signs") > 0 then
  637.         numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
  638.     end
  639.     if numSignsOnboard - arg.keepSigns > 0 then
  640.         if placeStorage:getStoragePlaced("signs") then
  641.             forward(2, 1)
  642.             turnRight(2)
  643.             saveToLog("checkItems: storing "..numSignsOnboard - arg.keepSigns.." signs in storageSigns", true)
  644.             if arg.keepSigns > 0 then
  645.                 storeItem{toStore = storageSigns, item = "signs", quantity = numSignsOnboard - arg.keepSigns, updateSlot = true, doSort = false}
  646.             else
  647.                 storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  648.             end
  649.             forward(2, 1)
  650.             turnRight(2)
  651.         end
  652.     end
  653.  
  654.  
  655.     if slot:getItemSlot("wood") > 0 then
  656.         numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  657.     end
  658.     if arg.keepWood == 0 and slot:getItemSlot("wood") > 0 then --mining run, no wood onboard
  659.         if placeStorage:getStoragePlaced("wood") then
  660.             forward(6, 1)
  661.             saveToLog("checkItems: storing "..numWoodOnboard.." wood in storageWood", true)
  662.             storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  663.             turnRight(2)
  664.             forward(6, 1)
  665.             turnRight(2)
  666.         end
  667.     end
  668. end
  669.  
  670. function checkLapis()
  671.     local success = false
  672.     --assume on ground
  673.     changeDirection("faceMine")
  674.     if not objectives["lapis"] then
  675.         sortInventory(true)
  676.         if slot:getMostUnknownItemSlot() > 0 then
  677.             saveToLog("checkLapis: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  678.             for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  679.                 turtle.select(i)
  680.                 saveToLog("checkLapis: checking "..slot:getSlotContains(i), true)
  681.                 if turtle.getItemCount(i) > 10 then
  682.                     changeDirection("faceExtraStorage")
  683.                     if turtle.place() then
  684.                         dig.digNew{self = dig, callFrom = "checkLapis"}
  685.                         changeDirection("faceMine")
  686.                     else --lapis, redstone,coal
  687.                         if objectives["redstone"] and slot:getItemSlot("coal") > 0 then
  688.                             saveToLog("checkLapis: lapis found", true)
  689.                             slot.update{self = slot, slotNo = i, newItem = "lapis"}
  690.                             objectives["lapis"] = true
  691.                             success = true
  692.                             break
  693.                         end
  694.                     end
  695.                 end
  696.             end
  697.         else
  698.             saveToLog("checkLapis: no unknown items to check", true)
  699.         end
  700.         if success then
  701.             sortInventory(true)
  702.         else
  703.             saveToLog("checkLapis: not found or amount onboard below 10", true)
  704.         end
  705.     else
  706.         saveToLog("checkLapis: lapis already found and in storage", false)
  707.         if slot:getItemSlot("diamonds") == 0 then
  708.             if slot:getMostUnknownItemSlot() > 0 then
  709.                 saveToLog("checkLapis: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  710.                 for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  711.                     turtle.select(i)
  712.                     saveToLog("checkLapis: checking "..slot:getSlotContains(i), true)
  713.                     if turtle.getItemCount(i) > 3 then
  714.                         changeDirection("faceExtraStorage")
  715.                         if turtle.place() then
  716.                             dig.digNew{self = dig, callFrom = "checkLapis"}
  717.                             changeDirection("faceMine")
  718.                         else --diamonds, redstone,coal
  719.                             if objectives["redstone"] and slot:getItemSlot("coal") > 0 then
  720.                                 saveToLog("checkLapis: ?diamonds found", true)
  721.                                 slot.update{self = slot, slotNo = i, newItem = "?diamonds"}
  722.                                 checkItems{keepTorches = 0, keepSigns = 0, keepSticks = 2}
  723.                                 if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "?diamonds", destSlot = 0} then
  724.                                     saveToLog("Pickaxe crafted", true)
  725.                                     slot.update{self = slot, item = "?diamonds", newItem = "diamonds"}
  726.                                     itemFound = "diamonds"
  727.                                     objectives["diamonds"] = true
  728.                                     success = true
  729.                                     break
  730.                                 end
  731.                             end
  732.                         end
  733.                     end
  734.                 end
  735.             else
  736.                 saveToLog("checkLapis: no unknown items to check", true)
  737.             end
  738.         end
  739.         success = true
  740.     end
  741.     changeDirection("faceMine")
  742.    
  743.     return success
  744. end
  745.  
  746. function checkMetal(useSlot)
  747.     local result = ""
  748.     local doContinue = true
  749.     local doSort = false
  750.     local tempItem = ""
  751.     useSlot = useSlot or 0
  752.    
  753.     --checking for ironore and goldore
  754.     --assume on ground
  755.     changeDirection("faceMine")
  756.     if objectives["smelt iron"] and objectives["smelt gold"] then  --iron and gold discovered so dont bother
  757.         doContinue = false
  758.     end
  759.     if slot:getItemSlot("?ironore") > 0 then --ironore provisionally identified, not enough to smelt / storageIronore not in place
  760.         if placeStorage:getStoragePlaced("ironore") then -- ok to smelt iron
  761.             if turtle.getItemCount(slot:getItemSlot("?ironore")) > 4 then --smelt to ?iron
  762.                 saveToLog("checkMetal: attempting to smelt iron", true)
  763.                 if smelt("?ironore", 4) then
  764.                     saveToLog("checkMetal: smelt succeeded, ?iron created", true)
  765.                     if craft{craftItem = "buckets", craftQuantity = 1, sourceItem1 = "?iron", destSlot = 0} then
  766.                         if slot:getItemCount("buckets") == 1 then
  767.                             slot.update{self = slot, item = "?iron", newItem = "iron"}
  768.                             slot.update{self = slot, item = "?ironore", newItem = "ironore"}
  769.                             forward(8) --over storageIronore
  770.                             storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
  771.                             storeItem{toStore = storageIronore, item = "buckets", quantity = 0, updateSlot = true, doSort = true}
  772.                             objectives["smelt iron"] = true
  773.                             turnRight(2)
  774.                             forward(8)
  775.                             turnRight(2)
  776.                             result = "ironore"
  777.                             saveToLog("checkMetal: iron and ironore confirmed", true)
  778.                             doSort = true
  779.                             doContinue = false
  780.                         else -- sand->glass->glass bottle x3
  781.                             turtle.select(slot:getItemSlot("buckets"))
  782.                             while turtle.drop() do
  783.                                 saveToLog("checkMetal: glass bottles dumped", true)
  784.                             end
  785.                             slot.update{self = slot, item = "buckets", delete = true}
  786.                             sortInventory(true)
  787.                             slot.update{self = slot, item = "?iron", newItem = "glass"}
  788.                             slot.update{self = slot, item = "?ironore", newItem = "sand"}
  789.                             saveToLog("checkMetal: ?iron and ?ironore renamed as glass and sand", true)
  790.                         end
  791.                     end
  792.                 else
  793.                     slot.update{self = slot, item = "?ironore", newItem = slot:getFreeUnknownItem()}
  794.                 end
  795.             end
  796.         end
  797.     end
  798.     if slot:getItemSlot("?goldore") > 0 then --goldore provisionally identified, not enough to smelt / storageIronore not in place
  799.         if turtle.getItemCount(slot:getItemSlot("?goldore")) > 1 then
  800.             if placeStorage:getStoragePlaced("ironore") then -- ok to smelt gold
  801.                 saveToLog("checkMetal: attempting to smelt gold", true)
  802.                 if smelt("?goldore", 1) then
  803.                     saveToLog("checkMetal: smelt succeeded, ?gold created", true)
  804.                     if craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "?gold", destSlot = 0} then
  805.                         slot.update{self = slot, item = "?goldore", newItem = "goldore"}
  806.                         --[[turtle.select(slot:getItemSlot("nuggets"))
  807.                         while turtle.drop() do
  808.                             saveToLog("checkMetal: gold nuggets dumped", true)
  809.                         end
  810.                         slot.update{self = slot, item = "nuggets", delete = true}]]--
  811.                         objectives["smelt gold"] = true
  812.                         result = "goldore"
  813.                         doSort = true
  814.                         doContinue = false
  815.                     end
  816.                 else
  817.                     slot.update{self = slot, item = "?ironore", newItem = slot:getFreeUnknownItem()}
  818.                 end
  819.             end
  820.         end
  821.     end
  822.     if doContinue then -- either gold or iron not known
  823.         if useSlot > 0 then --only check one item for gold as called from identifyItems
  824.             saveToLog("checkMetal: testing item in slot "..useSlot, true)
  825.             if turtle.getItemCount(useSlot) > 1 then --smelt to ?gold
  826.                 if not objectives["smelt gold"] then
  827.                     tempItem = slot:getSlotContains(useSlot)
  828.                     slot.update{self = slot, slotNo = useSlot, newItem = "?goldore"}
  829.                     saveToLog("checkMetal: gold ore provisionally identified from "..tempItem, true)
  830.                     if smelt("?goldore", 1) then
  831.                         saveToLog("checkMetal: smelt succeeded, ?gold created", true)
  832.                         if craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "?gold", destSlot = 0} then
  833.                             slot.update{self = slot, item = "?goldore", newItem = "goldore"}
  834.                             objectives["smelt gold"] = true
  835.                             result = "goldore"
  836.                         end
  837.                     else
  838.                         saveToLog("checkMetal: gold not smelted from "..tempItem, true)
  839.                         slot.update{self = slot, item = "?goldore", newItem = tempItem}
  840.                         result = ""
  841.                     end
  842.                 end
  843.             end
  844.         else
  845.             if slot:getMostUnknownItemSlot() > 0 then
  846.                 saveToLog("checkMetal: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  847.                 for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), -1 do
  848.                     if turtle.getItemCount(i) > 1 then
  849.                         turtle.select(i)
  850.                         if turtle.place() then --iron/gold/obsidian ? redstone
  851.                             if turtle.compare() then -- not redstone could be iron, gold, obsidian, moss stone
  852.                                 --iron most likely next item, then gold
  853.                                 dig.digNew{self = dig, slotNo = i, callFrom = "checkMetal"}
  854.                                 --if slot:getItemSlot("ironore") == 0 then --not already found
  855.                                 if not objectives["smelt iron"] then
  856.                                     slot.update{self = slot, slotNo = i, newItem = "?ironore"}
  857.                                     saveToLog("checkMetal: iron ore provisionally identified", true)
  858.                                     result = "?ironore"
  859.                                     if placeStorage:getStoragePlaced("ironore") then -- ok to smelt iron
  860.                                         if turtle.getItemCount(slot:getItemSlot("?ironore")) > 4 then --smelt to ?iron
  861.                                             saveToLog("checkMetal: attempting to smelt iron", true)
  862.                                             if smelt("?ironore", 4) then
  863.                                                 saveToLog("checkMetal: smelt succeeded, ?iron created", true)
  864.                                                 if craft{craftItem = "buckets", craftQuantity = 1, sourceItem1 = "?iron", destSlot = 0} then
  865.                                                     if slot:getItemCount("buckets") == 1 then
  866.                                                         slot.update{self = slot, item = "?iron", newItem = "iron"}
  867.                                                         slot.update{self = slot, item = "?ironore", newItem = "ironore"}
  868.                                                         forward(8) --over storageIronore
  869.                                                         storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
  870.                                                         storeItem{toStore = storageIronore, item = "buckets", quantity = 0, updateSlot = true, doSort = true}
  871.                                                         objectives["smelt iron"] = true
  872.                                                         turnRight(2)
  873.                                                         forward(8)
  874.                                                         turnRight(2)
  875.                                                         result = "ironore"
  876.                                                         saveToLog("checkMetal: iron and ironore confirmed", true)
  877.                                                         doSort = true
  878.                                                         break
  879.                                                     else -- sand->glass->glass bottle x3
  880.                                                         turtle.select(slot:getItemSlot("buckets"))
  881.                                                         while turtle.drop() do
  882.                                                             saveToLog("checkMetal: glass bottles dumped", true)
  883.                                                         end
  884.                                                         slot.update{self = slot, item = "buckets", delete = true}
  885.                                                         sortInventory(true)
  886.                                                         slot.update{self = slot, item = "?iron", newItem = "glass"}
  887.                                                         slot.update{self = slot, item = "?ironore", newItem = "sand"}
  888.                                                         saveToLog("checkMetal: ?iron and ?ironore renamed as glass and sand", true)
  889.                                                     end
  890.                                                 end
  891.                                             else
  892.                                                 slot.update{self = slot, item = "?ironore", newItem = slot:getFreeUnknownItem()}
  893.                                             end
  894.                                         end
  895.                                     end
  896.                                 --elseif slot:getItemSlot("goldore") == 0 then -- ironore found so check for gold
  897.                                 elseif not objectives["smelt gold"] then
  898.                                     tempItem = slot:getSlotContains(i)
  899.                                     slot.update{self = slot, slotNo = i, newItem = "?goldore"}
  900.                                     saveToLog("checkMetal: gold ore provisionally identified from "..tempItem, true)
  901.                                     result = "?goldore"
  902.                                     if placeStorage:getStoragePlaced("ironore") then -- ok to smelt iron
  903.                                         if turtle.getItemCount(slot:getItemSlot("?goldore")) > 1 then --smelt to ?gold
  904.                                             saveToLog("checkMetal: attempting to smelt gold", true)
  905.                                             if smelt("?goldore", 1) then
  906.                                                 saveToLog("checkMetal: smelt succeeded, ?gold created", true)
  907.                                                 if craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "?gold", destSlot = 0} then
  908.                                                     slot.update{self = slot, item = "?goldore", newItem = "goldore"}
  909.                                                     --[[turtle.select(slot:getItemSlot("nuggets"))
  910.                                                     while turtle.drop() do
  911.                                                         saveToLog("checkMetal: gold nuggets dumped", true)
  912.                                                     end
  913.                                                     slot.update{self = slot, item = "nuggets", delete = true}]]--
  914.                                                     objectives["smelt gold"] = true
  915.                                                     result = "goldore"
  916.                                                     doSort = true
  917.                                                     break
  918.                                                 end
  919.                                             else
  920.                                                 saveToLog("checkMetal: gold not smelted from "..tempItem.." trying next unknown item", true)
  921.                                                 slot.update{self = slot, item = "?goldore", newItem = tempItem}
  922.                                                 result = ""
  923.                                             end
  924.                                         end
  925.                                     end
  926.                                 end
  927.                             else -- did not compare to placed block
  928.                                 saveToLog("checkMetal: redstone confirmed - did not compare to placed block", true)
  929.                                 dig.digNew{self = dig, slotNo = i, callFrom = "checkMetal"}
  930.                                 slot.update{self = slot, slotNo = i, newItem = "redstone"}
  931.                                 doSort = true
  932.                                 break
  933.                             end
  934.                         else --not placed, could be coal
  935.                             if slot:getItemSlot("coal") == 0 then
  936.                                 if turtle.refuel(1) then
  937.                                     slot.update{self = slot, slotNo = i, newItem = "coal"}
  938.                                     doSort = true
  939.                                     break
  940.                                 end
  941.                             end
  942.                         end
  943.                     end
  944.                 end -- end for loop
  945.                 if doSort then
  946.                     sortInventory(true)
  947.                 end
  948.             else
  949.                 saveToLog("checkMetal: no unknown items to check", true)
  950.             end
  951.         end
  952.     else
  953.         saveToLog("checkMetal: gold/iron already discovered or ?ironore confirmed", true)
  954.     end
  955.    
  956.     return result -- "", ?ironore, ironore, ?goldore, goldore
  957. end
  958.  
  959. function checkMineEntranceForTree(direction)
  960.     local dugItem = ""
  961.    
  962.     if turtle.detect() then -- block in front
  963.         dugItem = checkSugarCane(true)
  964.         isTreeInFront(dugItem)
  965.     end
  966.     if direction == "right" then
  967.         turnLeft(1)
  968.     else
  969.         turnRight(1)
  970.     end
  971.     if turtle.detect() then -- block in front
  972.         dugItem = checkSugarCane(true)
  973.         isTreeInFront(dugItem)
  974.     end
  975.     if direction == "right" then
  976.         turnRight(1)
  977.     else
  978.         turnLeft(1)
  979.     end
  980. end
  981.  
  982. function checkMineStores()
  983.     --move over each buried block in turn to see if it is stored
  984.     --unknown items checked each time turtle returns to base
  985.     local success = false
  986.     local goBack = 0
  987.     local emptyStore = {}
  988.     local tempStore = {}
  989.     local firstEmptyBlock = 0
  990.     local doContinue = true
  991.    
  992.     -- check if ironore marker placed
  993.     if placeStorage:getStoragePlaced("ironore") then
  994.         if slot:getItemSlot("ironore") > 0 then
  995.             if not placeStorage:getMarkerPlaced("ironore") then
  996.                 placeStorage:place("ironore", true)
  997.             end
  998.         end
  999.     end
  1000.     --assume under furnace, facing forwards
  1001.     --first check any unknown items
  1002.     changeDirection("faceMine")
  1003.     if slot:getMostUnknownItemSlot() > 0 or
  1004.             slot:getItemSlot("lapis") > 0 or  
  1005.             slot:getItemSlot("moss stone") > 0 or
  1006.             slot:getItemSlot("goldore") > 0 or
  1007.             slot:getItemSlot("obsidian") > 0 then --see if anything can be stored
  1008.            
  1009.         turnRight(2) --face extended storage
  1010.         --find first empty storage area
  1011.         firstEmptyBlock, emptyStore = getEmptyStorage()
  1012.         saveToLog("checkMineStores: first empty storage is "..emptyStore:getStoreName(), false, true)
  1013.        
  1014.         -- first storage area now known, and no of blocks forward needed
  1015.         -- move turtle over blocks to check contents, only if not empty
  1016.         if firstEmptyBlock > 1 then
  1017.             forward(1)
  1018.             goBack = 1
  1019.             for k = 1, firstEmptyBlock - 2, 2 do
  1020.                 if k == 1 then
  1021.                     tempStore = extraStorage1
  1022.                 elseif k == 3 then
  1023.                     tempStore = extraStorage2
  1024.                 elseif k == 5 then
  1025.                     tempStore = extraStorage3
  1026.                 elseif k == 7 then
  1027.                     tempStore = extraStorage4
  1028.                 elseif k == 9 then
  1029.                     tempStore = extraStorage5
  1030.                 end
  1031.                 if tempStore:getStoreContains() == "lapis" then
  1032.                     forward(1, 1)
  1033.                     if slot:getItemSlot("lapis") > 0 then --already onboard and identified
  1034.                         storeItem{toStore = tempStore, item = "lapis", quantity = 0, updateSlot = true, doSort = true}
  1035.                     else
  1036.                         turtle.select(16)
  1037.                         if turtle.suckDown() then
  1038.                             for i = 1, 16 do
  1039.                                 if turtle.compareTo(i) then --lapis found
  1040.                                     turtle.select(16)
  1041.                                     while turtle.dropDown() do
  1042.                                         saveToLog("checkMineStores: returning lapis to storage")
  1043.                                     end
  1044.                                     slot.update{self = slot, slotNo = i, newItem = "lapis"}
  1045.                                     sortInventory(true)
  1046.                                     if not placeStorage:getMarkerPlaced("lapis") then
  1047.                                         if slot:getItemCount("lapis") >= 9 then
  1048.                                             if craft{craftItem = "lapis block", craftQuantity = 1, sourceItem1 = "lapis", destSlot = 0} then
  1049.                                                 dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
  1050.                                                 turtle.select(slot:getItemSlot("lapis block"))
  1051.                                                 turtle.placeDown()
  1052.                                                 slot.update{self = slot, item = "lapis block", delete = true}
  1053.                                                 placeStorage:setMarkerPlaced("lapis")
  1054.                                             end
  1055.                                         end
  1056.                                     end
  1057.                                     saveToLog("checkMineStores: adding additional lapis to storage", true)
  1058.                                     storeItem{toStore = tempStore, item = "lapis", quantity = 0, updateSlot = true, doSort = true}
  1059.                                     break
  1060.                                 end
  1061.                             end
  1062.                         end
  1063.                     end
  1064.                     back(1)
  1065.                 else
  1066.                     if slot:getItemSlot(tempStore:getStoreContains()) > 0 then --item already onboard and identified
  1067.                         forward(1, 1)
  1068.                         storeItem{toStore = tempStore, item = tempStore:getStoreContains(), quantity = 0, updateSlot = true, doSort = true}
  1069.                         back(1)
  1070.                     else
  1071.                         for i = 1, 16 do
  1072.                             if slot:getSlotStatus(i) == "unknown" then
  1073.                                 saveToLog("checkMineStores: comparing items in "..tempStore:getStoreName().." with "..tostring(slot:getSlotContains(i)), true)
  1074.                                 turtle.select(i)
  1075.                                 if turtle.compareDown() then --matches embedded block below
  1076.                                     forward(1, 1)
  1077.                                     while turtle.dropDown() do
  1078.                                         saveToLog("checkMineStores: adding "..tempStore:getStoreContains().." to "..tempStore:getStoreName(), true)
  1079.                                     end
  1080.                                     slot.update{self = slot, item = slot:getSlotContains(i), delete = true}
  1081.                                     sortInventory(true)
  1082.                                     back(1)
  1083.                                     break
  1084.                                 end
  1085.                             end
  1086.                         end
  1087.                     end
  1088.                 end
  1089.                 forward(2, 1)
  1090.                 goBack = goBack + 2
  1091.             end
  1092.             back(goBack)
  1093.         end
  1094.         changeDirection("faceMine")
  1095.         if slot:getItemSlot("moss stone") > 0 then --put moss stone and walls into storage
  1096.             changeDirection("faceExtendedStorage")
  1097.             forward(firstEmptyBlock)
  1098.             dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
  1099.             turtle.select(slot:getItemSlot("moss stone"))
  1100.             turtle.placeDown()
  1101.             forward(1, 1)
  1102.             storeItem{toStore = emptyStore, item = "moss stone", quantity = 0, updateSlot = true, doSort = true}
  1103.             storeItem{toStore = emptyStore, item = "walls", quantity = 0, updateSlot = true, doSort = true}
  1104.             --slot.update{self = slot, item = slot:getSlotContains(i), delete = true}
  1105.             back(firstEmptyBlock + 1)
  1106.             changeDirection("faceMine")
  1107.             firstEmptyBlock, emptyStore = getEmptyStorage()
  1108.         end
  1109.         if slot:getItemSlot("goldore") > 0 then
  1110.             changeDirection("faceExtendedStorage")
  1111.             forward(firstEmptyBlock)
  1112.             dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
  1113.             turtle.select(slot:getItemSlot("goldore"))
  1114.             turtle.placeDown()
  1115.             forward(1, 1)
  1116.             storeItem{toStore = emptyStore, item = "goldore", quantity = 0, updateSlot = true, doSort = true}
  1117.             storeItem{toStore = emptyStore, item = "nuggets", quantity = 0, updateSlot = true, doSort = true}
  1118.             back(firstEmptyBlock + 1)
  1119.             changeDirection("faceMine")
  1120.             firstEmptyBlock, emptyStore = getEmptyStorage()
  1121.         end
  1122.         if slot:getItemSlot("obsidian") > 0 then
  1123.             changeDirection("faceExtendedStorage")
  1124.             forward(firstEmptyBlock)
  1125.             dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
  1126.             turtle.select(slot:getItemSlot("obsidian"))
  1127.             turtle.placeDown()
  1128.             forward(1, 1)
  1129.             storeItem{toStore = emptyStore, item = "obsidian", quantity = 0, updateSlot = true, doSort = true}
  1130.             back(firstEmptyBlock + 1)
  1131.             changeDirection("faceMine")
  1132.             firstEmptyBlock, emptyStore = getEmptyStorage()
  1133.         end
  1134.         if slot:getItemSlot("lapis") > 0 then
  1135.             changeDirection("faceExtendedStorage")
  1136.             forward(firstEmptyBlock)
  1137.             if slot:getItemCount("lapis") > 9 then
  1138.                 if craft{craftItem = "lapis block", craftQuantity = 1, sourceItem1 = "lapis", destSlot = 0} then
  1139.                     dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
  1140.                     turtle.select(slot:getItemSlot("lapis block"))
  1141.                     turtle.placeDown()
  1142.                     slot.update{self = slot, item = "lapis block", delete = true}
  1143.                     placeStorage:setMarkerPlaced("lapis")
  1144.                 end
  1145.             end
  1146.             forward(1, 1)
  1147.             storeItem{toStore = emptyStore, item = "lapis", quantity = 0, updateSlot = true, doSort = true}
  1148.             back(firstEmptyBlock + 1)
  1149.             changeDirection("faceMine")
  1150.             firstEmptyBlock, emptyStore = getEmptyStorage()
  1151.         end
  1152.     else
  1153.         saveToLog("checkMineStores: no unknown items to store")
  1154.     end
  1155.    
  1156.     return success
  1157. end
  1158.  
  1159. function checkMossStone()
  1160.     --assume on ground
  1161.     local success = false
  1162.     local goBack = 0
  1163.     local tempItem = ""
  1164.     changeDirection("faceMine")
  1165.     if slot:getMostUnknownItemSlot() > 0 then
  1166.         saveToLog("checkMossStone: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  1167.         for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  1168.             turtle.select(i)
  1169.             if turtle.getItemCount(i) > 6 then
  1170.                 changeDirection("faceExtraStorage")
  1171.                 if turtle.place() then --iron/gold/redstone/moss stone
  1172.                     if turtle.compare() then --iron/gold/redstone/moss stone
  1173.                         tempItem = slot:getSlotContains(i)
  1174.                         saveToLog("checkMossStone: testing "..tempItem.." in slot "..i, true)
  1175.                         dig.digNew{self = dig, slotNo = i, callFrom = "checkMossStone"}
  1176.                         changeDirection("faceMine")
  1177.                         slot.update{self = slot, slotNo = i, newItem = "?moss stone"}
  1178.                         if craft{craftItem = "walls", craftQuantity = 6, sourceItem1 = "?moss stone", destSlot = 0} then
  1179.                             success = true
  1180.                             -- slot.update{self = slot, slotNo = i, newItem = "moss stone"}
  1181.                             slot.update{self = slot, item = "?moss stone", newItem = "moss stone"}
  1182.                             sortInventory(true)
  1183.                             --now store moss and walls in extended storage area
  1184.                             break
  1185.                         else
  1186.                             slot.update{self = slot, item = "?moss stone", newItem = tempItem}
  1187.                         end
  1188.                     else -- redstone
  1189.                         saveToLog("checkMossStone: redstone found", true)
  1190.                         dig.digNew{self = dig, slotNo = i, callFrom = "checkMossStone"}
  1191.                         changeDirection("faceMine")
  1192.                         slot.update{self = slot, slotNo = i, newItem = "redstone"}
  1193.                         sortInventory(true)
  1194.                         if not placeStorage:getStoragePlaced("redstone") then
  1195.                             placeStorage:place("redstone", false)
  1196.                         else
  1197.                             forward(12, 1)
  1198.                             turnRight(2)
  1199.                             storeRedstone()
  1200.                             --return to furnace
  1201.                             forward(12, 1)
  1202.                             turnRight(2)
  1203.                         end
  1204.                         break
  1205.                     end
  1206.                 end
  1207.             end
  1208.         end
  1209.         changeDirection("faceMine")
  1210.     else
  1211.         saveToLog("checkMossStone: no unknown items to check", true)
  1212.     end
  1213.        
  1214.     return success
  1215. end
  1216.  
  1217. function checkObjectives(colNo, side, torchesNeeded)
  1218.     local numTorches = 0
  1219.     local numSigns = 0
  1220.     local numSticks = 0
  1221.     local woodCount = 0
  1222.     local numSaplings = 0
  1223.     local success = false
  1224.     local woodType = 0
  1225.     local fuelStats = {}
  1226.     local doContinue = true
  1227.     local woodAvailable = 0
  1228.     local wood2Available = 0
  1229.    
  1230.     if torchesNeeded == nil then
  1231.         torchesNeeded = 0
  1232.     end
  1233.     refuel(0)
  1234.     --should be under furnace. Checks objectives before deep mining starts
  1235.     --Also loads turtle with torches/signs ready for next harvest run
  1236.     --[[
  1237.         before going deep mining:
  1238.         1) goMining1 completed at least once
  1239.         2) storageTorches placed
  1240.         3) storageSigns, storageSticks placed
  1241.         4) found the following items: dirt, cobble, stone, coal, gravel, sand(6), ironore (5), sugar cane (3)
  1242.         5) tree farm planted
  1243.         6) iron smelted (1 bucket crafted)
  1244.         7) storageSand, storageWood, storageRedstone, storageIronore, storagePickaxes, storageSaplings
  1245.         If all trees harvested and goMining1 repeated on all areas, can continue without iron and coal
  1246.         objectives:
  1247.         1) placeStorage:getStoragePlaced("torches")
  1248.         2) placeStorage:getStoragePlaced("signs")
  1249.         3) placeStorage:getStoragePlaced("sticks")
  1250.         4) placeStorage:getStoragePlaced("wood") -- only when treeFarm planted
  1251.         5) placeStorage:getStoragePlaced("ironore")
  1252.         6) placeStorage:getStoragePlaced("sand")
  1253.         7) placeStorage:getStoragePlaced("redstone") -- only when treeFarm planted
  1254.         8) placeStorage:getStoragePlaced("pickaxes)" -- only when treeFarm planted
  1255.         9) placeStorage:getStoragePlaced("saplings") -- only when treeFarm planted
  1256.        
  1257.     ]]--
  1258.     changeDirection("faceMine")
  1259.     if side == "rightside" and colNo == 1 then --not enough wood on first run unless branched tree harvested
  1260.         doContinue = false
  1261.         woodType = getWoodAvailable{woodNeeded = 4}
  1262.         if woodType ~= "none" then
  1263.             doContinue = true
  1264.         end
  1265.     end
  1266.     if doContinue then --excludes first tree harvest run if not enough wood
  1267.         --torches already checked by checkTorch()
  1268.         if placeStorage:getStoragePlaced("torches") then
  1269.             --check if at least 2 torches and 1 sign present first
  1270.             saveToLog("checkObjectives: checking for torches onboard or in storage", false, true)
  1271.             if getItemsAvailable("torches", true) < 2 + torchesNeeded then --check and remove torches from storage
  1272.                 -- make torches
  1273.                 woodType = getWoodAvailable{woodNeeded = 4}
  1274.                 saveToLog("checkObjectives: crafting torches if wood available", false, true)
  1275.                 if woodType ~= "none" then
  1276.                     craftTorches(4)
  1277.                 end
  1278.             end
  1279.             -- put them in storage
  1280.             saveToLog("checkObjectives: storing torches to free a slot", false, true)
  1281.             storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
  1282.             saveToLog("checkObjectives: checking for signs onboard or in storage", false, true)
  1283.             if getItemsAvailable("signs", true) < 1 then --check and remove signs from storage
  1284.                 woodType = getWoodAvailable{woodNeeded = 4}
  1285.                 saveToLog("checkObjectives: crafting signs if wood available", false, true)
  1286.                 if woodType ~= "none" then
  1287.                     craftSigns()
  1288.                 end
  1289.             end
  1290.             -- put them in storage
  1291.             if placeStorage:getStoragePlaced("signs") then
  1292.                 if not placeStorage:getMarkerPlaced("signs") then
  1293.                     placeStorage:place("signs", true)
  1294.                 end
  1295.                 forward(2, 1)
  1296.                 turnRight(2)
  1297.                 saveToLog("checkObjectives: storing signs to free a slot", false, true)
  1298.                 storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  1299.                 forward(2, 1)
  1300.                 turnRight(2)
  1301.             else
  1302.                 refuel(0)
  1303.                 woodType = getWoodAvailable{woodNeeded = 5}
  1304.                 if woodType ~= "none" then
  1305.                     saveToLog('checkObjectives: placeStorage("signs") function started', true)
  1306.                     getItemsAvailable("signs", true)
  1307.                     placeStorage:place("signs", false)
  1308.                     if placeStorage:getStoragePlaced("signs") then
  1309.                         saveToLog('checkObjectives: 2/9 - placeStorage:getStoragePlaced("signs") completed', true)
  1310.                         if slot:getItemSlot("signs") > 0 then
  1311.                             if turtle.getItemCount(slot:getItemSlot("signs")) > 0 then
  1312.                                 forward(2, 1)
  1313.                                 turnRight(2)
  1314.                                 saveToLog("checkObjectives: storing signs to free a slot", false, true)
  1315.                                 storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  1316.                                 forward(2, 1)
  1317.                                 turnRight(2)
  1318.                             end
  1319.                         end
  1320.                     end
  1321.                 else
  1322.                     saveToLog("checkObjectives: Not enough wood to place storageSigns", false, true)
  1323.                 end
  1324.             end
  1325.             --signs and torches made, see if any wood for storage, in order torches, signs, sticks, sand, ironore
  1326.             refuel(0)
  1327.             if placeStorage:getStoragePlaced("sticks") then
  1328.                 if not placeStorage:getMarkerPlaced("sticks") then
  1329.                     woodType = getWoodAvailable{woodNeeded = 4}
  1330.                     if woodType ~= "none" then
  1331.                         craftSticks(8)
  1332.                         placeStorage:place("sticks", true)
  1333.                     end
  1334.                 end
  1335.                 if slot:getItemSlot("sticks") > 0 then
  1336.                     if turtle.getItemCount(slot:getItemSlot("sticks")) > 0 then
  1337.                         -- put them in storage
  1338.                         forward(4, 1)
  1339.                         turnRight(2)
  1340.                         saveToLog("checkObjectives: storing signs to free a slot", false, true)
  1341.                         storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  1342.                         forward(4, 1)
  1343.                         turnRight(2)
  1344.                     end
  1345.                 end
  1346.             else
  1347.                 woodType = getWoodAvailable{woodNeeded = 6}
  1348.                 if woodType ~= "none" then
  1349.                     saveToLog('checkObjectives: placeStorage("sticks") function started', true)
  1350.                     placeStorage:place("sticks", false)
  1351.                     if placeStorage:getStoragePlaced("sticks") then
  1352.                         saveToLog('checkObjectives: 3/9 - placeStorage:getStoragePlaced("sticks") completed', true)
  1353.                         if slot:getItemSlot("sticks") > 0 then
  1354.                             if turtle.getItemCount(slot:getItemSlot("sticks")) > 0 then
  1355.                                 forward(4, 1)
  1356.                                 turnRight(2)
  1357.                                 saveToLog("checkObjectives: storing signs to free a slot", false, true)
  1358.                                 storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  1359.                                 forward(4, 1)
  1360.                                 turnRight(2)
  1361.                             end
  1362.                         end
  1363.                     end
  1364.                 else
  1365.                     saveToLog("checkObjectives: Not enough wood to place storageSticks", false, true)
  1366.                 end
  1367.             end
  1368.             refuel(0)
  1369.             if placeStorage:getStoragePlaced("sand") then --storageSand placed
  1370.                 if not placeStorage:getMarkerPlaced("sand") then
  1371.                     placeStorage:place("sand", true)
  1372.                 end
  1373.                 if slot:getItemSlot("sugar cane") > 0 then
  1374.                     forward(10)
  1375.                     storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
  1376.                     turnRight(2)
  1377.                     forward(10)
  1378.                     turnRight(2)
  1379.                     objectives["complete sugar cane"] = true
  1380.                     saveToLog('checkObjectives: 6/9 - objectives["complete sugar cane"] completed', true)          
  1381.                 end
  1382.             else --storageSand not yet placed
  1383.                 if slot:getItemSlot("sand") > 0 or
  1384.                     (placeStorage:getStoragePlaced("signs") and
  1385.                      placeStorage:getStoragePlaced("sticks")) then --see if sand onboard or signs/sticks already placed
  1386.                     woodType = getWoodAvailable{woodNeeded = 5}
  1387.                     if woodType ~= "none" then
  1388.                         if objectives["sand"] then --place storageSand if enough wood and sand discovered
  1389.                             placeStorage:place("sand", false)
  1390.                             saveToLog('checkObjectives: 1/9 - placeStorage:getStoragePlaced("sand") completed', true)
  1391.                         end
  1392.                         if slot:getItemSlot("sugar cane") > 0 then
  1393.                             forward(10)
  1394.                             storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
  1395.                             turnRight(2)
  1396.                             forward(10)
  1397.                             turnRight(2)
  1398.                             objectives["complete sugar cane"] = true
  1399.                             saveToLog('checkObjectives: 6/9 - objectives["complete sugar cane"] completed', true)          
  1400.                         end
  1401.                     end
  1402.                 end
  1403.             end
  1404.             refuel(0)
  1405.             if objectives["goMining1"] then --done on each run to see if ironore has been found
  1406.                 saveToLog('checkObjectives: checkMetal started', true)
  1407.                 checkMetal() --check if ironore discovered
  1408.                 if placeStorage:getStoragePlaced("ironore") then
  1409.                     if slot:getItemSlot("ironore") > 0 then
  1410.                         if not placeStorage:getMarkerPlaced("ironore") then
  1411.                             placeStorage:place("ironore", true)
  1412.                         end
  1413.                     end
  1414.                 else
  1415.                     if slot:getItemSlot("ironore") > 0 or
  1416.                         (placeStorage:getStoragePlaced("signs") and
  1417.                          placeStorage:getStoragePlaced("sticks")) then
  1418.                         woodType = getWoodAvailable{woodNeeded = 5}
  1419.                         if woodType ~= "none" then
  1420.                             saveToLog('checkObjectives: placeStorage("ironore") function started', true)
  1421.                             placeStorage:place("ironore", false)
  1422.                             if placeStorage:getStoragePlaced("ironore") then
  1423.                                 saveToLog('checkObjectives: 4/9 - placeStorage:getStoragePlaced("ironore") completed', true)
  1424.                             end
  1425.                         else
  1426.                             saveToLog("checkObjectives: Not enough wood to place storageIronore", false, true)
  1427.                         end
  1428.                     end
  1429.                 end
  1430.             end
  1431.            
  1432.             if colNo > 5 or side == "leftside" then --can plant tree farm anytime from now needs min 500 fuel
  1433.                 if objectives["clear treefarm"] then --treeFarm already present
  1434.                     if not objectives["pave treefarm"] then
  1435.                         objectives["pave treefarm"] = paveTreeFarm()
  1436.                     end
  1437.                     woodType = getWoodAvailable{woodNeeded = 1}
  1438.                     if woodType ~= "none" then
  1439.                         if not placeStorage:getMarkerPlaced("wood") then
  1440.                             placeStorage:place("wood", true)
  1441.                         end
  1442.                     end
  1443.                     if placeStorage:getStoragePlaced("ironore")  and  placeStorage:getStoragePlaced("sand") then
  1444.                         if not placeStorage:getStoragePlaced("redstone") then
  1445.                             woodType = getWoodAvailable{woodNeeded = 5}
  1446.                             if woodType ~= "none" then
  1447.                                 saveToLog('checkObjectives: placeStorage("redstone") function started', true)
  1448.                                 placeStorage:place("redstone", false)
  1449.                                 if placeStorage:getStoragePlaced("redstone") then
  1450.                                     saveToLog('checkObjectives: 7/9 - placeStorage:getStoragePlaced("redstone") completed', true)
  1451.                                 end
  1452.                             else
  1453.                                 saveToLog("checkObjectives: Not enough wood to place storageRedstone", false, true)
  1454.                             end
  1455.                         end
  1456.                         if not placeStorage:getStoragePlaced("pickaxes") then
  1457.                             woodType = getWoodAvailable{woodNeeded = 5}
  1458.                             if woodType ~= "none" then
  1459.                                 saveToLog('checkObjectives: placeStorage("pickaxes") function started', true)
  1460.                                 placeStorage:place("pickaxes", false)
  1461.                                 if placeStorage:getStoragePlaced("pickaxes") then
  1462.                                     saveToLog('checkObjectives: 8/9 - placeStorage:getStoragePlaced("pickaxes") completed', true)
  1463.                                 end
  1464.                             else
  1465.                                 saveToLog("checkObjectives: Not enough wood to place storagePickaxes", false, true)
  1466.                             end
  1467.                         end
  1468.                     end
  1469.                     saveToLog('checkObjectives: objectives["clear treefarm"] = true, checking sand and sugar cane', true)
  1470.                     if objectives["sand"] and objectives["sugar cane"] then
  1471.                         success = true
  1472.                     else
  1473.                         if not objectives["sand"] or not objectives["gravel"] then --check if sand onboard
  1474.                             back(1)
  1475.                             turnRight(1)
  1476.                             checkGravel()
  1477.                             turnLeft(1)
  1478.                             forward(1)
  1479.                         end
  1480.                     end
  1481.                     if not objectives["smelt iron"] then
  1482.                         success = false
  1483.                     end
  1484.                     -- need 14 wood for 7 chests, and 16 charcoal for 64 torches, fuel from coal during mining
  1485.                     woodType, woodAvailable, wood2Available = getWoodAvailable{woodNeeded = 24, checkWoodType = "any"}  -- 18 needed for deep mining + 6 spare
  1486.                     if woodType == "none" then
  1487.                         success = false
  1488.                         saveToLog("checkObjectives: not enough wood to start deep mining ("..woodAvailable.." wood, "..wood2Available.." wood2", false, true)
  1489.                     end
  1490.                 else --treefarm not yet planted
  1491.                     --check if enough saplings to plant tree farm - min 5 saplings/saplings2
  1492.                     if slot:getItemSlot("saplings") > 0 then
  1493.                         numSaplings = turtle.getItemCount(slot:getItemSlot("saplings"))
  1494.                     end
  1495.                     if slot:getItemSlot("saplings2") > 0 then
  1496.                         if turtle.getItemCount(slot:getItemSlot("saplings2")) > numSaplings then
  1497.                             numSaplings = turtle.getItemCount(slot:getItemSlot("saplings2"))
  1498.                         end
  1499.                     end
  1500.                     if numSaplings > 4 then --check if tree farm can be cleared
  1501.                         if slot:getItemCount("coal") > 2 then
  1502.                             woodType = getWoodAvailable{woodNeeded = 8}  -- 4 for chests, 3 for torches, 3 for fuel, 1 for marker
  1503.                         else
  1504.                             woodType = getWoodAvailable{woodNeeded = 11}  -- 4 for chests, 3 for torches, 3 for fuel, 1 for marker
  1505.                         end
  1506.                         if woodType ~= "none" then
  1507.                             saveToLog("CheckObjectives: checking fuel before clearing Tree Farm", true)
  1508.                             fuelStats = getFuelAvailable()
  1509.                             if fuelStats.totalFuelAvailable >  500 then
  1510.                                 saveToLog("CheckObjectives: placing storageSaplings")
  1511.                                 placeStorage:place("saplings", false)
  1512.                                 if placeStorage:getStoragePlaced("saplings") then
  1513.                                     saveToLog('checkObjectives: 9/9 - placeStorage:getStoragePlaced("saplings") completed')
  1514.                                 end
  1515.                                 saveToLog("CheckObjectives: placing storageWood")
  1516.                                 placeStorage:place("wood", false)
  1517.                                 if placeStorage:getStoragePlaced("wood") then
  1518.                                     saveToLog('checkObjectives: 8/9 - placeStorage:getStoragePlaced("wood") completed')
  1519.                                 end
  1520.                                 forward(2)
  1521.                                 storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  1522.                                 back(2)
  1523.                                 craftTorches(8)
  1524.                                 saveToLog("CheckObjectives: fuelAvailable = "..fuelStats.totalFuelAvailable.." starting Tree Farm", true)
  1525.                                 objectives["clear treefarm"] = clearTreeFarm()
  1526.                                 if objectives["clear treefarm"] then
  1527.                                     saveToLog('checkObjectives: 9/9 - objectives["clear treefarm"] completed')
  1528.                                 end
  1529.                             else
  1530.                                 saveToLog("CheckObjectives: fuelAvailable = "..fuelStats.totalFuelAvailable.." not enough for tree farm", true)
  1531.                             end
  1532.                         else
  1533.                             saveToLog("CheckObjectives: not enough wood for storageSaplings and storageWood")
  1534.                         end
  1535.                     else
  1536.                         saveToLog("CheckObjectives: not enough saplings to plant tree farm")
  1537.                     end
  1538.                 end
  1539.                 completeChests() -- use excess wood/wood2 to make chests or refuel
  1540.             end
  1541.             saveToLog('checkObjectives: checkItems{"keepTorches"} started', true)
  1542.             --If turtle loaded, may not be able to carry torches/signs to mark position of mineshafts
  1543.             if getNoOfEmptySlots(true) >= 3 then
  1544.                 saveToLog('checkObjectives: loading torches and signs', true)
  1545.                 checkItems{keepTorches = 1 + torchesNeeded, keepSigns = 1}
  1546.             elseif getNoOfEmptySlots(true) >= 2 then
  1547.                 saveToLog('checkObjectives: not enough empty slots to carry signs', true)
  1548.                 checkItems{keepTorches = 2 + torchesNeeded, keepSigns = 0}
  1549.             else
  1550.                 saveToLog('checkObjectives: not enough empty slots to carry signs or torches', true)
  1551.                 checkItems{keepTorches = 0 + torchesNeeded, keepSigns = 0}
  1552.             end
  1553.             if torchesNeeded > 0 then
  1554.                 restoreTorches(side, colNo)
  1555.             end
  1556.         else --did not get enough wood on first run
  1557.             woodType = getWoodAvailable{woodNeeded = 4}
  1558.             if woodType ~= "none" then
  1559.                 saveToLog('checkObjectives: place StorageTorches started', true)
  1560.                 placeStorage:place("torches", false)
  1561.                 if placeStorage:getStoragePlaced("torches") then
  1562.                     saveToLog("checkObjectives: 1/9 - place StorageTorches completed")
  1563.                 end
  1564.             else
  1565.                 saveToLog("checkObjectives: Not enough wood to place storageTorches", false, true)
  1566.             end
  1567.         end
  1568.         if slot:getItemSlot("planks") > 0 then
  1569.             turtle.select(slot:getItemSlot("planks"))
  1570.             turtle.refuel()
  1571.             slot.update{self = slot, slotNo = slot:getItemSlot("planks"), delete = true}
  1572.             sortInventory(true)
  1573.         end
  1574.         if slot:getItemSlot("planks2") > 0 then
  1575.             turtle.select(slot:getItemSlot("planks2"))
  1576.             turtle.refuel()
  1577.             slot.update{self = slot, slotNo = slot:getItemSlot("planks2"), delete = true}
  1578.             sortInventory(true)
  1579.         end
  1580.     end
  1581.    
  1582.     return success --if true ready for deep mining early, otherwise continue harvestAllTrees
  1583. end
  1584.  
  1585. function checkRedstone()
  1586.     local success = false
  1587.     --assume on ground
  1588.     sortInventory(true)
  1589.     changeDirection("faceMine")
  1590.     if slot:getMostUnknownItemSlot() > 0 then
  1591.         saveToLog("checkRedstone: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  1592.         for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  1593.             if turtle.getItemCount(i) > 1 then
  1594.                 turtle.select(i)
  1595.                 if turtle.getItemCount(i) > 15 then
  1596.                     changeDirection("faceExtraStorage")
  1597.                     if turtle.place() then --iron/gold/redstone
  1598.                         if turtle.compare() then
  1599.                             saveToLog("checkRedstone: redstone not found", true)
  1600.                             dig.digNew{self = dig, callFrom = "checkRedstone"}
  1601.                         else -- redstone
  1602.                             saveToLog("checkRedstone: redstone confirmed", true)
  1603.                             slot.update{self = slot, slotNo = i, newItem = "redstone"}
  1604.                             objectives["redstone"] = true
  1605.                             sortInventory(true)
  1606.                             dig.digNew{self = dig, callFrom = "checkRedstone"}
  1607.                             success = true
  1608.                             break
  1609.                         end
  1610.                         changeDirection("faceMine")
  1611.                     end
  1612.                 end
  1613.             end
  1614.         end
  1615.     else
  1616.         saveToLog("checkRedstone: no unknown items to check", true)
  1617.     end
  1618.    
  1619.     changeDirection("faceMine")
  1620.     if success then
  1621.         if not placeStorage:getStoragePlaced("redstone") then
  1622.             placeStorage:place("redstone", false)
  1623.         else
  1624.             forward(12, 1)
  1625.             storeRedstone()
  1626.             turnRight(2)
  1627.             --return to furnace
  1628.             forward(12, 1)
  1629.             turnRight(2)
  1630.         end
  1631.     end
  1632.    
  1633.     return success
  1634. end
  1635.  
  1636. function checkRemaining()
  1637.     local tempItem = ""
  1638.     local reRun = false
  1639.     local itemFound = ""
  1640.    
  1641.     --already checked for iron, gold, redstone, lapis
  1642.     --lapis, diamond, obsidian, emerald, planks or fence from abandoned mine
  1643.     --see if axe can be crafted to show diamond. need sticks in storage below
  1644.     changeDirection("faceMine")
  1645.     if objectives["ironore"] and objectives["redstone"] and objectives["lapis"] then -- iron found
  1646.         saveToLog("checkRemaining: starting")
  1647.         checkItems{keepTorches = 0, keepSigns = 0, keepSticks = 2}
  1648.         repeat
  1649.             reRun = false
  1650.             if slot:getMostUnknownItemSlot() > 0 then
  1651.                 for i = slot:getLeastUnknownItemSlot(), slot:getMostUnknownItemSlot(), 1 do
  1652.                     tempItem = slot:getSlotContains(i)
  1653.                     turtle.select(i)
  1654.                     changeDirection("faceExtraStorage")
  1655.                     if turtle.place() then --obsidian, moss stone < 6, planks or fence from old mineshaft
  1656.                         dig.digNew{self = dig, slotNo = i, callFrom = "checkRemaining"}
  1657.                         changeDirection("faceMine")
  1658.                         if turtle.refuel() then --planks / fence
  1659.                             saveToLog("checkRemaining: used wooden items as fuel", true)
  1660.                             slot.update{self = slot, slotNo = i, delete = true}
  1661.                             reRun = true
  1662.                             break
  1663.                         else
  1664.                             if objectives["ironore"] and objectives["goldore"] then --gold and iron found
  1665.                                 itemFound = "obsidian"
  1666.                                 saveToLog("checkRemaining: obsidian found", true)
  1667.                                 slot.update{self = slot, slotNo = i, newItem = "obsidian"}
  1668.                                 --reRun = true
  1669.                                 break
  1670.                             end
  1671.                         end
  1672.                     else --lapis, or diamond, possibly emerald
  1673.                         changeDirection("faceMine")
  1674.                         if slot:getItemSlot("diamonds") == 0 then
  1675.                             -- if all mining complete need min 3 diamonds, if still mining and 4 or more then continue
  1676.                             if turtle.getItemCount(i) > 3 then --test for diamond
  1677.                                 slot.update{self = slot, slotNo = i, newItem = "?diamonds"}
  1678.                                 if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "?diamonds", destSlot = 0} then
  1679.                                     saveToLog("Pickaxe crafted", true)
  1680.                                     slot.update{self = slot, item = "?diamonds", newItem = "diamonds"}
  1681.                                     itemFound = "diamonds"
  1682.                                     objectives["diamonds"] = true
  1683.                                     reRun = true
  1684.                                     break
  1685.                                 else --diamonds not found
  1686.                                     slot.update{self = slot, slotNo = i, newItem = tempItem}
  1687.                                     --[[turtle.select(i)
  1688.                                     while turtle.drop() do
  1689.                                         saveToLog("checkRemaining: dropping ? lapis after failed pickaxe craft", true)
  1690.                                     end
  1691.                                     slot.update{self = slot, slotNo = i, delete = true}]]--
  1692.                                 end
  1693.                             end
  1694.                         else
  1695.                             if turtle.getItemCount(i) >= 3 then --test for diamond already done
  1696.                                 while turtle.drop() do
  1697.                                     saveToLog("checkRemaining: dropping ? lapis", true)
  1698.                                 end
  1699.                                 slot.update{self = slot, slotNo = i, delete = true}
  1700.                                 reRun = true
  1701.                                 break
  1702.                             end
  1703.                         end
  1704.                     end
  1705.                 end
  1706.                 sortInventory(true)
  1707.             end
  1708.         until not reRun
  1709.         changeDirection("faceMine")
  1710.         if slot:getItemSlot("sticks") > 0 then
  1711.             forward(4, 1)
  1712.             storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  1713.             turnRight(2)
  1714.             forward(4, 1)
  1715.             turnRight(2)
  1716.         end
  1717.         if slot:getItemSlot("wood") > 0 then
  1718.             forward(6, 1)
  1719.             storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  1720.             turnRight(2)
  1721.             forward(6, 1)
  1722.             turnRight(2)
  1723.         end
  1724.         if slot:getItemSlot("diamond pickaxe") > 0 then
  1725.             saveToLog("checkRemaining: storing diamond pickaxe in storagePickaxes",true)
  1726.             forward(14, 1)
  1727.             storeItem{toStore = storagePickaxes, item = "diamond pickaxe", quantity = 0, updateSlot = true, doSort = true}
  1728.             turnRight(2)
  1729.             forward(14, 1)
  1730.             turnRight(2)
  1731.         end
  1732.         saveToLog("checkRemaining: all remaining items identified or dropped unless <4", true)
  1733.     else
  1734.         saveToLog("checkRemaining: either iron, gold, lapis, or redstone not yet identified", true)
  1735.     end
  1736.    
  1737.     return itemFound
  1738. end
  1739.  
  1740. function checkSugarCane(returnToGround)
  1741.     --function entered with block in front
  1742.     --return block type of top block
  1743.     local yCoordTemp = location:getY()
  1744.     local yCoordTemp2 = 0
  1745.     local useAmount = 0
  1746.     local useItem = ""
  1747.     local tempSlot = 0
  1748.     local tempDugAmount = 0
  1749.     local itemFound = ""
  1750.     local checkList = {}
  1751.     local blocksClimbed = 0
  1752.     local blockAbove = false
  1753.     local woodType = ""
  1754.  
  1755.    
  1756.     --used when forwardSafe is checking right and left sides only
  1757.     returnToGround = returnToGround or false
  1758.    
  1759.     checkList[1] = "all"
  1760.     --see if block in front is wood or wood2
  1761.     if slot:getItemSlot("wood") > 0 then
  1762.         turtle.select(slot:getItemSlot("wood"))
  1763.         if turtle.compare() then
  1764.             itemFound = "wood"
  1765.         end
  1766.     end
  1767.     if slot:getItemSlot("wood2") > 0 then
  1768.         turtle.select(slot:getItemSlot("wood2"))
  1769.         if turtle.compare() then
  1770.             itemFound = "wood2"
  1771.         end
  1772.     end
  1773.     --if wood/wood2 found exit function returning "wood"/"wood2"
  1774.     if itemFound == "" then --block in front is not wood or wood2 (if wood2 already known)
  1775.         saveToLog("checkSugarCane: moving up", true)
  1776.         while turtle.detect() do --climb up max 12 blocks
  1777.             if slot:getItemSlot("wood") > 0 then
  1778.                 turtle.select(slot:getItemSlot("wood"))
  1779.                 if turtle.compare() then
  1780.                     itemFound = "wood"
  1781.                     break
  1782.                 end
  1783.             end
  1784.             if slot:getItemSlot("wood2") > 0 then
  1785.                 turtle.select(slot:getItemSlot("wood2"))
  1786.                 if turtle.compare() then
  1787.                     itemFound = "wood2"
  1788.                     break
  1789.                 end
  1790.             end
  1791.             if slot:getItemSlot("cobble") > 0 then
  1792.                 turtle.select(slot:getItemSlot("cobble"))
  1793.                 if turtle.compare() then
  1794.                     itemFound = "cobble"
  1795.                     break
  1796.                 end
  1797.             end
  1798.             up(1, 1, "saplings", "checkSugarCane")
  1799.             blocksClimbed = blocksClimbed + 1
  1800.             if location:getY() - yCoordTemp == 12 then --climb up max 10 places
  1801.                 saveToLog("checkSugarCane: reached 12 blocks height", true)
  1802.             end
  1803.             sleep(0.5) --stops random "Too long without yielding" errors
  1804.         end
  1805.        
  1806.         --gone up until nothing in front, could be top of sugar cane, or top of leaves, or dirt. go forward and down
  1807.         if itemFound == "" then --wood/wood2/cobble not found
  1808.             saveToLog("checkSugarCane: moving over top of blocks", true)
  1809.             forward(1, 1)
  1810.             saveToLog("checkSugarCane: digging down", true)
  1811.             if blocksClimbed == 1 then --could be mushrooms/flowers/leaves/grass tuft or sand
  1812.                 --success, dugItem, dugSlot = dig{direction = "down", checkForItems = "flowers"}
  1813.                 dig.digNew{self = dig, direction = "down", checkForItems = "flowers", callFrom = "checkSugarCane"}
  1814.             else
  1815.                 --success, dugItem, dugSlot = dig{direction = "down", checkForItems = "sugar cane,sand,gravel,wood2,saplings"}
  1816.                 dig.digNew{self = dig, direction = "down", checkForItems = "sugar cane,sand,gravel,wood2,saplings", callFrom = "checkSugarCane"}
  1817.             end
  1818.             itemFound = dig:getDugItem()
  1819.             if  dig:getDugItem() == "sugar cane" then --only happens if sugar cane already onboard
  1820.                 saveToLog("checkSugarCane: more sugar cane found", true)
  1821.                 itemFound = "sugar cane"
  1822.                 down(1)--sit on top of sugar cane
  1823.             elseif dig:getDugItem() == "leaves" or dig:getDugItem() == "saplings" or dig:getDugItem() == "saplings2" then
  1824.                 saveToLog("checkSugarCane: leaves found, digging down", true)
  1825.                 --continue digging down until dirt/stone/cobble found
  1826.                 --leaves may disappear so return to ground level anyway
  1827.                 down(1, 1, "saplings") --move down to detect if more leaves
  1828.                 while turtle.detectDown() do
  1829.                     --success, dugItem, dugSlot = dig{self = slot, direction = "down", checkForItems = "saplings"}
  1830.                     dig.digNew{self = dig, direction = "down", checkForItems = "saplings", callFrom = "checkSugarCane"}
  1831.                     if dig:getDugItem() == "" or dig:getDugItem() == "leaves" or dig:getDugItem() == "saplings" or dig:getDugItem() == "saplings2" then
  1832.                         down(1, 1)
  1833.                     elseif dig:getDugItem() == "wood2" then
  1834.                         saveToLog("checkSugarCane: wood2 found", true)
  1835.                         itemFound = "wood2"
  1836.                         down(2)
  1837.                         break --leave detectDown() loop
  1838.                     else --dirt, cobble sand etc found so replace
  1839.                         saveToLog("checkSugarCane: "..dig:getDugItem().." found, replacing and moving on", true)
  1840.                         -- replace last block found
  1841.                         if dig:getDugSlot() > 0 then
  1842.                             turtle.select(dig:getDugSlot())
  1843.                             turtle.placeDown()
  1844.                         end
  1845.                         break -- leave detectDown() loop
  1846.                     end
  1847.                 end
  1848.             elseif string.find(dig:getDugItem(), "item") ~= nil then --new item or sugar cane returned
  1849.                 --sugar cane can be placeDown() but does not compareDown() on top of sugar cane or blocks next to water
  1850.                 --compareDown to check false = sugar cane, stone, grass but cobble already known and checked. Must be sugar cane
  1851.                 --true = dirt,sand,gravel,wood,
  1852.                 saveToLog("checkSugarCane: dug top of blocks checking for sugar cane", true)
  1853.                 down(1)
  1854.                 tempSlot, useAmount, useItem = compareBlock("down", checkList, 1)  --does block below match anything
  1855.                 if tempSlot == 0 then --sugar cane, stone, grass, saplings2 only. dirt and cobble known so must be cane
  1856.                     saveToLog("checkSugarCane: no compareDown(): sugar cane found in "..dig:getDugSlot(), true)
  1857.                     itemFound = "sugar cane"
  1858.                     slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = "sugar cane"}
  1859.                     --dig next block to make certain
  1860.                     tempDugAmount = turtle.getItemCount(slot:getItemSlot("sugar cane"))
  1861.                     dig.digNew{self = dig, direction = "down", checkForItems = "wood2", callFrom = "checkSugarCane"}
  1862.                     if turtle.getItemCount(slot:getItemSlot("sugar cane")) == tempDugAmount then --not sugar cane
  1863.                         itemFound = dig:getDugItem()
  1864.                         slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = dig:getDugItem()}
  1865.                     end
  1866.                 else
  1867.                     --check if wood2
  1868.                     if slot:getItemSlot("wood2") == 0 then
  1869.                         turtle.select(dig:getDugSlot())
  1870.                         if turtle.refuel(0) then
  1871.                             --itemFound = "wood2"
  1872.                             --dig:getDugItem() = "wood2"
  1873.                             itemFound = "wood2"
  1874.                             dig.digNew{self = dig, direction = "down", slotNo = dig:getDugSlot(), expectedItem = "wood2", callFrom = "checkSugarCane"}
  1875.                             slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = "wood2"}
  1876.                             sortInventory(true)
  1877.                             down(1)
  1878.                         else
  1879.                             saveToLog("checkSugarCane: "..dig:getDugItem().." found, keeping in stock", true)
  1880.                             turtle.select(slot:getItemSlot("dirt"))
  1881.                             turtle.placeDown()
  1882.                         end
  1883.                     else
  1884.                         saveToLog("checkSugarCane: "..dig:getDugItem().." found, keeping in stock", true)
  1885.                         turtle.select(slot:getItemSlot("dirt"))
  1886.                         turtle.placeDown()
  1887.                     end
  1888.                 end
  1889.             elseif dig:getDugItem() == "sand" then
  1890.                 if slot:getItemCount("sand") >= 7 then
  1891.                     turtle.select(slot:getItemSlot("sand"))
  1892.                     turtle.placeDown()
  1893.                 end
  1894.             elseif dig:getDugItem() == "dirt" or dig:getDugItem() == "cobble" then
  1895.                 turtle.select(dig:getDugSlot())
  1896.                 turtle.placeDown()
  1897.                 saveToLog("checkSugarCane: "..dig:getDugItem().." found and replaced", true)
  1898.             elseif dig:getDugItem() == "wood" or dig:getDugItem() == "wood2" then --on top of tree so harvest it
  1899.                 turtle.select(slot:getItemSlot(dig:getDugItem()))
  1900.                 local isBranched = false
  1901.                 while turtle.compareDown() do
  1902.                     isBranched = checkTree(dig:getDugItem(), isBranched, "checkSugarCane")
  1903.                     down(1, 1)
  1904.                     turtle.select(slot:getItemSlot(dig:getDugItem()))
  1905.                 end
  1906.                 turnRight(2)
  1907.                 while turtle.detect() do
  1908.                 --while location:getY() < yCoordTemp do
  1909.                     up(1, 1)
  1910.                 end
  1911.                 turnRight(2)
  1912.             end
  1913.             if itemFound == "sugar cane" then --recognised by not comparing down. Currently on top of sugar cane
  1914.                 --dig:getDugItem() = "sugar cane"
  1915.                 while dig:getDugItem() == "sugar cane" do
  1916.                     --success, dugItem, dugSlot = dig{self = slot, direction = "down"}
  1917.                     dig.digNew{self = dig, direction = "down", callFrom = "checkSugarCane"}
  1918.                     if dig:getDugItem() ~= "sugar cane" then --stop digging at base, should have just dug sand
  1919.                         if dig:getDugItem() ~= "dirt" then
  1920.                             if slot:getItemSlot("sand") == 0 then
  1921.                                 slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = "sand"}
  1922.                             end
  1923.                         end
  1924.                         break
  1925.                     end
  1926.                     down(1, 1)
  1927.                 end
  1928.                 --1 block below base of sugar cane - could be sand below
  1929.                 yCoordTemp = location:getY() + 1 --water level
  1930.                 if slot:getItemSlot("sand") > 0 then
  1931.                     tempSlot = slot:getItemSlot("sand")
  1932.                     if turtle.getItemCount(slot:getItemSlot("sand")) >= 8 then
  1933.                         tempSlot = 0
  1934.                     end
  1935.                 else --sand not found yet
  1936.                     dig.digNew{self = dig, direction = "down", expectedItem = "sand", callFrom = "checkSugarCane"}
  1937.                     tempSlot = slot:getItemSlot("sand")
  1938.                 end
  1939.                 if tempSlot > 0 then
  1940.                     objectives["sand"] = true
  1941.                     if turtle.getItemCount(tempSlot) < 8 then
  1942.                         turtle.select(tempSlot)
  1943.                         down(1, tempSlot)
  1944.                         while turtle.compareDown() do
  1945.                             down(1, tempSlot)
  1946.                         end
  1947.                     end
  1948.                 end
  1949.                 while location:getY() < yCoordTemp do
  1950.                     --up(1, 1)
  1951.                     if getDirtStock() > 0 then
  1952.                         turtle.select(slot:getItemSlot("dirt"))
  1953.                         turtle.placeDown()
  1954.                     end
  1955.                     up(1)
  1956.                 end
  1957.             elseif itemFound == "wood2" then
  1958.                 turtle.select(slot:getItemSlot("wood2"))
  1959.                 local isBranched = false
  1960.                 while turtle.compareDown() do
  1961.                     isBranched = checkTree(dig:getDugItem(), isBranched, "checkSugarCane")
  1962.                     down(1, 1)
  1963.                     turtle.select(slot:getItemSlot("wood2"))
  1964.                 end
  1965.                 turnRight(2)
  1966.                 while turtle.detect() do
  1967.                     up(1, 1)
  1968.                 end
  1969.                 turnRight(2)
  1970.                 itemFound = "dirt"
  1971.             end
  1972.             if returnToGround then
  1973.                 --if location:getY() < yCoordTemp then --lower than starting level, so back() will dig block
  1974.                     while location:getY() < yCoordTemp do
  1975.                         up(1, 1)
  1976.                     end
  1977.                     while location:getY() > yCoordTemp do
  1978.                         if turtle.detectDown() then --only go down if nothing below
  1979.                             break
  1980.                         else
  1981.                             down(1, 1)
  1982.                         end
  1983.                     end
  1984.                 --end
  1985.             end
  1986.             back(1)
  1987.         else --itemFound = tree and wood/wood2 selected
  1988.             if itemFound ~= "cobble" then
  1989.                 saveToLog("checkSugarCane: tree found above ground level", true)
  1990.                 --now harvest rest of tree
  1991.                 harvestTree(itemFound)
  1992.             end
  1993.         end
  1994.     end
  1995.  
  1996.     return itemFound --will return "wood", "wood2" if next to tree
  1997. end
  1998.  
  1999. function checkTorch(direction)
  2000.     -- dig torch on way back to base, turn and replace. If not present ignore
  2001.     local success = false
  2002.     local dugItem = ""
  2003.     local dugSlot = 0
  2004.    
  2005.     direction = direction or "home"
  2006.    
  2007.     if turtle.detect() then --? torch present
  2008.         dig.digNew{self = dig, expectedItem = "torches", callFrom = "checkTorch"}
  2009.         if dig:getSuccess() then
  2010.             if dig:getDugItem() == "torches" then
  2011.                 forward(2, 1)
  2012.                 turnRight(2)
  2013.                 turtle.select(dig:getDugSlot())
  2014.                 turtle.place()
  2015.                 slot.update{self = slot, item = "torches", delete = true}
  2016.                 if slot:getItemSlot("torches") == 0 then
  2017.                     sortInventory(true)
  2018.                 end
  2019.                 turnRight(2)
  2020.             end
  2021.         else
  2022.             forward(2, 1)
  2023.         end
  2024.     else
  2025.         forward(2, 1)
  2026.     end
  2027.     if direction == "home" then
  2028.         changeDirection("faceMine")
  2029.     end
  2030. end
  2031.  
  2032. function checkTree(woodType, isBranched, calledFrom)
  2033.     for i = 1, 4 do
  2034.         isBranched = checkTreeBranch(woodType, isBranched, calledFrom)
  2035.         turnRight(1)
  2036.     end
  2037.    
  2038.     return isBranched
  2039. end
  2040.  
  2041. function checkTreeBranch(woodType, isBranched, calledFrom)
  2042.     --success, dugItem, dugSlot = dig{checkForItems = "saplings"} -- dig space for refuelling
  2043.     dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom} -- dig space for refuelling
  2044.    
  2045.     --saveToLog("checkTreeBranch: fuel level = "..turtle.getFuelLevel())
  2046.     if dig:getDugItem() == woodType then --wood/wood2 found
  2047.         if turtle.getFuelLevel() < 10 then
  2048.             refuel(0)
  2049.         end
  2050.         isBranched = true
  2051.         forward(1, 1, "saplings", calledFrom)
  2052.         checkTreeBranch(woodType, isBranched, calledFrom)
  2053.         turnLeft(1)
  2054.         checkTreeBranch(woodType, isBranched, calledFrom)
  2055.         turnRight(2)
  2056.         checkTreeBranch(woodType, isBranched, calledFrom)
  2057.         turnLeft(1)
  2058.         back(1)
  2059.     else
  2060.         if isBranched then
  2061.             if turtle.getFuelLevel() < 10 then
  2062.                 refuel(0)
  2063.             end
  2064.             forward(1, 1, "saplings", calledFrom)
  2065.             turnLeft(1)
  2066.             dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
  2067.             turnRight(1)
  2068.             dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
  2069.             if dig:getDugItem() == woodType then --wood/wood2 found
  2070.                 forward(1, 1, "saplings", calledFrom)
  2071.                 turnLeft(1)
  2072.                 dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
  2073.                 turnRight(1)
  2074.                 dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
  2075.                 turnRight(1)
  2076.                 dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
  2077.                 turnLeft(1)
  2078.                 back(1)
  2079.             end
  2080.             turnRight(1)
  2081.             dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
  2082.             turnLeft(1)
  2083.             back(1)
  2084.         end
  2085.     end
  2086.    
  2087.     return isBranched
  2088. end
  2089.  
  2090. function checkWalls(itemList, calledFrom)
  2091.     for j = 1, 4 do
  2092.         mineItem(itemList, calledFrom) --call recursive function
  2093.         refuel(0)
  2094.         turnRight(1)
  2095.     end
  2096.    
  2097.     return success
  2098. end
  2099.  
  2100. function checkWater(direction)
  2101.     local result = false
  2102.     local slotNo = 0
  2103.    
  2104.     for i = 1, 16 do
  2105.         if turtle.getItemCount(i) == 0 then
  2106.             slotNo = i
  2107.             break
  2108.         end
  2109.     end
  2110.     if slotNo > 0 then
  2111.         turtle.select(slotNo)
  2112.         if direction == "down" then
  2113.             if not turtle.detectDown() then
  2114.                 if not turtle.compareDown() then --no block detected, but does not compare to air(empty slot)
  2115.                     result = true
  2116.                 end
  2117.             end
  2118.         elseif direction == "up" then
  2119.             if not turtle.detectUp() then
  2120.                 if not turtle.compareUp() then
  2121.                     result = true
  2122.                 end
  2123.             end
  2124.         elseif direction == "forward" then
  2125.             if not turtle.detect() then
  2126.                 if not turtle.compare() then
  2127.                     result = true
  2128.                 end
  2129.             end
  2130.         end
  2131.     end
  2132.    
  2133.     return result --true if water, (or lava)
  2134. end
  2135.  
  2136. function chooseWood()
  2137.     local woodType = ""
  2138.     local woodSpare = ""
  2139.     local success = false
  2140.     local numMoves = 0
  2141.     local emptySlot = 0
  2142.     local chestsNeeded = 7
  2143.     local numChests = 0
  2144.    
  2145.     -- return to tree farm, check if tree has grown.
  2146.     -- only do this when ready to mine for diamonds. While harvesting other trees leave alone
  2147.     forward(2, 1)
  2148.     turnRight(1)
  2149.     forward(2, 1)
  2150.     turnLeft(1)
  2151.     up(1, 1)
  2152.     -- compare wood/wood2 to block in front. If tree has grown
  2153.     for i = 1, 3 do -- check first 3 trees
  2154.         if turtle.detect() then
  2155.             turtle.select(slot:getItemSlot("wood"))
  2156.             if turtle.compare() then
  2157.                 woodType = "wood"
  2158.                 if slot:getItemSlot("wood2") > 0 then
  2159.                     woodSpare = "wood2"
  2160.                 end
  2161.                 saveToLog("chooseWood: wood grown!", true)
  2162.             else
  2163.                 if slot:getItemSlot("wood2") > 0 then
  2164.                     turtle.select(slot:getItemSlot("wood2"))
  2165.                     if turtle.compare() then
  2166.                         woodType = "wood2"
  2167.                         woodSpare = "wood"
  2168.                     end
  2169.                     saveToLog("chooseWood: wood2 grown!", true)
  2170.                 end
  2171.             end
  2172.             saveToLog('chooseWood: objectives["choose wood"] = true', true)
  2173.             objectives["choose wood"] = true
  2174.             break
  2175.         else
  2176.             saveToLog("chooseWood: Tree "..i.." still a sapling", true)
  2177.             forward(3, 1)
  2178.             numMoves = numMoves + 1
  2179.         end
  2180.     end
  2181.     if not objectives["choose wood"] then
  2182.         saveToLog("chooseWood: wood not grown yet", true)
  2183.     end
  2184.     down(1, 1)
  2185.     turnLeft(1)
  2186.     forward(2, 1)
  2187.     turnLeft(1) --facing furnace
  2188.     if numMoves > 0 then
  2189.         for i = 1, numMoves do
  2190.             forward(3, 1)
  2191.         end    
  2192.     end
  2193.     forward(2, 1) --under furnace
  2194.     turnLeft(2)
  2195.     if woodType == "" then
  2196.         saveToLog("chooseWood: wood not grown yet", true)
  2197.     else
  2198.         --convert spare wood/wood2 to chests/charcoal
  2199.         if woodSpare ~= "" then
  2200.             if slot:getItemSlot(woodSpare) > 0 then
  2201.                 if turtle.getItemCount(slot:getItemSlot(woodSpare)) < 3 then --make planks and burn
  2202.                     saveToLog("chooseWood: using "..turtle.getItemCount(slot:getItemSlot(woodSpare)).." excess "..woodSpare.." to refuel", true)
  2203.                     --check no planks around
  2204.                     if slot:getItemSlot("planks") > 0 then
  2205.                         turtle.select(slot:getItemSlot("planks"))
  2206.                         saveToLog("chooseWood: using existing planks to refuel", true)
  2207.                         turtle.refuel()
  2208.                     end
  2209.                     saveToLog("chooseWood: crafting planks", true)
  2210.                     craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = woodSpare, destSlot = 0, doSort = true}
  2211.                     turtle.select(slot:getItemSlot("planks"))
  2212.                     turtle.refuel()
  2213.                     slot.update{self = slot, item = "planks", delete = true}
  2214.                 else --craft chests if needed, min 4 wood = 2 chests
  2215.                     if placeStorage:getStoragePlaced("redstone") then
  2216.                         chestsNeeded = chestsNeeded - 2
  2217.                     end
  2218.                     if objectives["place extended storage"] then
  2219.                         chestsNeeded = chestsNeeded - 5 --min 7
  2220.                     end
  2221.                     chestsNeeded = chestsNeeded - turtle.getItemCount(slot:getItemSlot("chests")) + 1
  2222.                     if chestsNeeded > 0 then
  2223.                         saveToLog("chooseWood: converting "..woodSpare.." to chests", true)
  2224.                         numChests = math.floor(turtle.getItemCount(slot:getItemSlot(woodSpare)) / 2)
  2225.                         if numChests > chestsNeeded then
  2226.                             numChests = chestsNeeded
  2227.                         end
  2228.                         --need 14 wood for 7 chests
  2229.                         if slot:getItemSlot("planks") > 0 then
  2230.                             turtle.select(slot:getItemSlot("planks"))
  2231.                             turtle.refuel()
  2232.                             slot.update{self = slot, item = "planks", delete = true}
  2233.                             sortInventory(true)
  2234.                         end
  2235.                         craft{craftItem = "planks", craftQuantity = numChests * 8, sourceItem1 = woodSpare, destSlot = 0, doSort = true}
  2236.                         craft{craftItem = "chests", craftQuantity = numChests, sourceItem1 = "planks", destSlot = 0, doSort = true}
  2237.                     end
  2238.                     if slot:getItemSlot(woodSpare) > 0 then
  2239.                         if turtle.getItemCount(slot:getItemSlot(woodSpare)) > 0 then
  2240.                             saveToLog("chooseWood: converting remaining "..woodSpare.." to charcoal", true)
  2241.                             smelt("charcoal", 0, woodSpare)
  2242.                             if slot.getItemSlot(slot, woodSpare) > 0 then
  2243.                                 turtle.select(slot.getItemSlot(slot, woodSpare))
  2244.                                 turtle.refuel()
  2245.                                 slot.update{self = slot, item = woodSpare, delete = true}
  2246.                             end
  2247.                         end
  2248.                     end
  2249.                 end
  2250.                 if woodType == "wood2" then --change wood2 to wood
  2251.                     emptySlot = slot:getItemSlot(woodType)
  2252.                     slot.update{self = slot, slotNo = emptySlot, newItem = "wood"}
  2253.                 end
  2254.                 sortInventory(true)
  2255.             end
  2256.         end
  2257.         if slot:getItemSlot("saplings2") > 0 then --burn saplings2
  2258.             saveToLog("chooseWood: Saplings2 used for fuel", true)
  2259.             turtle.select(slot:getItemSlot("saplings2"))
  2260.             turtle.refuel()
  2261.             slot.update{self = slot, item = "saplings2", delete = true}
  2262.             sortInventory(true)
  2263.         end
  2264.         if not placeStorage:getMarkerPlaced("wood") then
  2265.             forward(7, 1)
  2266.             dig.digNew{self = dig, direction = "down", callFrom = "chooseWood"}
  2267.             turtle.select(slot:getItemSlot("wood"))
  2268.             turtle.placeDown()
  2269.             placeStorage:setMarkerPlaced("wood")
  2270.             turnRight(2)
  2271.             forward(7, 1)
  2272.             turnRight(2)
  2273.         end
  2274.         success = true
  2275.     end
  2276.    
  2277.     return success
  2278. end
  2279.  
  2280. function clearBase()
  2281.     --clear area around first tree 4 X 4 SQUARE
  2282.     reposition("FFRFFRFFFFRFFFFRFFFFRFRFFFLFFLFFLFRB", "wood2,saplings", true)
  2283.    
  2284.     --put furnace above
  2285.     turtle.select(slot:getItemSlot("furnaces"))
  2286.     turtle.placeUp()
  2287.     saveToLog("clearBase: furnace placed", true, true)
  2288.     slot.update{self = slot, item = "furnaces", delete = true}
  2289.  
  2290.     sortInventory(true)
  2291. end
  2292.  
  2293. function clearColumn(blockType, columnType)
  2294.     --move down to original y coord
  2295.     -- columnType = "storage" or "farm"
  2296.     local blockAbove = false
  2297.    
  2298.     if location:getY() > coordHome:getY() then
  2299.         saveToLog("Returning to original yCoord "..coordHome:getY(), true)
  2300.         while location:getY() > coordHome:getY() do
  2301.             down(1, 1)
  2302.         end
  2303.     elseif location:getY() < coordHome:getY() then
  2304.         while location:getY() < coordHome:getY() do
  2305.             up(1, 1)
  2306.         end
  2307.     end
  2308.     saveToLog("clearColumn checking fuel level", true)
  2309.     refuel(0)
  2310.     --at start of new col, could be space below, blocks above
  2311.     for i = 1, 15 do
  2312.         if i == 1 then
  2313.             placeBlock("cobble", columnType, i) -- placeBlock("cobble", "storage", 1)
  2314.         else
  2315.             placeBlock(blockType, columnType, i) -- placeBlock("cobble", "farm", 2)
  2316.         end
  2317.         --check above
  2318.         turtle.select(1)
  2319.         while turtle.detectUp() do
  2320.             up(1, 1)
  2321.             sleep(.5) -- allow for sand/gravel
  2322.             turnRight(1)
  2323.             dig.digNew{self = dig, waitForGravel = true, callFrom = "clearColumn"}
  2324.             turnRight(2)
  2325.             dig.digNew{self = dig, waitForGravel = true, callFrom = "clearColumn"}
  2326.             turnRight(1)
  2327.         end
  2328.         while location:getY() > coordHome:getY() do --gone upwards
  2329.             down(1, 1)
  2330.         end
  2331.         if i == 8 then --check fuel
  2332.             refuel(0)
  2333.         end
  2334.         if i < 15 then
  2335.             forward(1, 1)
  2336.         end
  2337.     end
  2338.     placeBlock("cobble", columnType, 16)
  2339.     saveToLog("Finished clearing column", true)
  2340. end
  2341.  
  2342. function clearTreeFarm()
  2343.     local emptySlot = 0
  2344.     local noOfSaplings = 0
  2345.     local noOfSaplings2 = 0
  2346.     local tempSlot = 0
  2347.     local useWood = "wood"
  2348.     local usePlanks = "planks"
  2349.     local success = false
  2350.  
  2351.     --first see how many saplings of each type and choose the most
  2352.     noOfSaplings = turtle.getItemCount(slot.getItemSlot(slot, "saplings"))
  2353.     if slot.getItemSlot(slot, "saplings2") > 0 then
  2354.         noOfSaplings2 = turtle.getItemCount(slot.getItemSlot(slot, "saplings2"))
  2355.     end
  2356.     if noOfSaplings < noOfSaplings2 then
  2357.         saveToLog(noOfSaplings2.." saplings2 collected, will use these for tree farming", true)
  2358.         --swap them over
  2359.         saveToLog("Swapping saplings with saplings2", false)
  2360.         emptySlot = getFirstEmptySlot()
  2361.         turtle.select(slot.getItemSlot(slot, "saplings"))
  2362.         turtle.transferTo(emptySlot) --put  saplings in spare slot
  2363.         turtle.select(slot.getItemSlot(slot, "saplings2"))
  2364.         turtle.transferTo(slot.getItemSlot(slot, "saplings")) --put saplings2 into original saplings slot
  2365.         turtle.select(emptySlot)
  2366.         turtle.transferTo(slot.getItemSlot(slot, "saplings2")) --put original saplings into saplings2 slot
  2367.         slot.update{self = slot, item = "saplings"}
  2368.         slot.update{self = slot, item = "saplings2"}
  2369.     else
  2370.         saveToLog(noOfSaplings.." saplings collected, will use these for tree farming", true)
  2371.     end
  2372.     --delete saplings2 when harvestAllTrees and clearTreeFarm  completed
  2373.     checkItems{keepSticks = 0, keepTorches = 9, keepSigns = 0} -- make 8 torches
  2374.     if slot:getItemSlot("planks") > 0 then
  2375.         turtle.select(slot:getItemSlot("planks"))
  2376.         turtle.refuel()
  2377.         slot.update{self = slot, item = "planks", delete = true}
  2378.     end
  2379.     if slot:getItemSlot("planks2") > 0 then
  2380.         turtle.select(slot:getItemSlot("planks2"))
  2381.         turtle.refuel()
  2382.         slot.update{self = slot, item = "planks2", delete = true}
  2383.     end
  2384.     --clear farm area
  2385.     forward(1, 1) --start tree farm 1 blocks in front of furnace
  2386.     -- may be storage chests on first column
  2387.     for i = 1, 4 do --clear 8 columns to ground level with grass
  2388.         saveToLog("Clearing column"..i, true)
  2389.         if i == 1 then
  2390.             clearColumn("cobble", "storage") --left side of square col 1 no saplings
  2391.         else
  2392.             clearColumn("dirt", "farm") --left side of square col 1 no saplings
  2393.         end
  2394.         turnRight(1)
  2395.         forward(1, 1) --start of next column
  2396.         turnRight(1) --ready to go
  2397.         saveToLog("Clearing column".. 1 + i, true)
  2398.         if i == 1 or i == 4 then
  2399.             clearColumn("cobble", "farm") --left side col 2 no saplings
  2400.         else
  2401.             clearColumn("dirt", "farm") --left side col 2 no saplings
  2402.         end
  2403.         if i < 4 then --do not move when last col is finished
  2404.             turnLeft(1)
  2405.             forward(1, 1)
  2406.             turnLeft(1)
  2407.         end
  2408.     end
  2409.    
  2410.     turnRight(1)
  2411.     refuel(0)
  2412.     forward(5, 1)
  2413.     turnRight(1)
  2414.     forward(1, 1) -- now on bottom of col 3 ready to plant saplings
  2415.     saveToLog(" clearTreeFarm: Farm cleared, now planting saplings", true)
  2416.     treeFarm:reset()
  2417.     up(1, 1)
  2418.     plantSaplings()
  2419.     turnRight(1)
  2420.     forward(3, 1)
  2421.     turnRight(1)
  2422.     plantSaplings()
  2423.     down(1, 1)
  2424.     forward(2, 1)
  2425.     turnRight(1)
  2426.     forward(3, 1)
  2427.     --now in front of torch
  2428.     checkTorch("home")
  2429.     saveToLog(treeFarm:getNumSaplings().." Saplings planted. Continuing tree harvest while waiting for treefarm to grow...", true)
  2430.     success = true
  2431.  
  2432.     return success
  2433. end
  2434.  
  2435. function compareBlock(direction, checkList, checkNum)
  2436.     --direction = "forward"
  2437.     --checkList = {} checkList[1] = "wood", checkList[2] = "wood2" etc
  2438.     --checkNum = 2 no of items in checklist
  2439.     --useSlot, amount, useItem = compareBlock("forward", checkList, 3)
  2440.     --useSlot, amount, useItem = compareBlock("forward", {"all"}, 1) checkList[1] = "all"
  2441.     local slotFound = 0
  2442.     local amount = 0
  2443.     local checkItem = ""
  2444.     local checkSlot = 0
  2445.     local item = ""
  2446.     local matchItem = false
  2447.    
  2448.     if checkList[1] == "all" then
  2449.         for i = 1, 16 do
  2450.             if turtle.getItemCount(i) > 0 then
  2451.                 turtle.select(i)
  2452.                 if direction == "forward" then
  2453.                     if turtle.compare() then --compare slot i with block in front
  2454.                         matchItem = true
  2455.                     end
  2456.                 elseif direction == "up" then
  2457.                     if turtle.compareUp() then --compare slot i with block above
  2458.                         matchItem = true
  2459.                     end
  2460.                 elseif direction == "down" then
  2461.                     if turtle.compareDown() then --compare slot i with block below
  2462.                         matchItem = true
  2463.                     end
  2464.                 end
  2465.                 if matchItem then
  2466.                     slotFound = i
  2467.                     amount = turtle.getItemCount(i)
  2468.                     item = slot:getSlotContains(i)
  2469.                     break
  2470.                 end
  2471.             end
  2472.         end
  2473.     else
  2474.         for i = 1, checkNum do
  2475.             if checkList[i] == "item" then
  2476.                 if slot:getFirstUnknownItem() ~= "" then
  2477.                     for j = 1, slot:getUnknownItemCount() do
  2478.                         checkItem = "item"..j
  2479.                         checkSlot = slot:getItemSlot(checkItem)
  2480.                         if checkSlot > 0 then
  2481.                             turtle.select(checkSlot)
  2482.                             if direction == "forward" then
  2483.                                 if turtle.compare() then --compare slot with block in front
  2484.                                     matchItem = true
  2485.                                 end
  2486.                             elseif direction == "up" then
  2487.                                 if turtle.compareUp() then --compare slot with block above
  2488.                                     matchItem = true
  2489.                                 end
  2490.                             elseif direction == "down" then
  2491.                                 if turtle.compareDown() then --compare slot with block below
  2492.                                     matchItem = true
  2493.                                 end
  2494.                             end
  2495.                             if matchItem then
  2496.                                 slotFound = checkSlot
  2497.                                 amount = turtle.getItemCount(checkSlot)
  2498.                                 item = checkList[i]
  2499.                                 break
  2500.                             end
  2501.                         end
  2502.                     end
  2503.                 end
  2504.             else
  2505.                 checkItem = checkList[i]
  2506.                 checkSlot = slot:getItemSlot(checkList[i])
  2507.                 if checkSlot > 0 then
  2508.                     turtle.select(checkSlot)
  2509.                     if direction == "forward" then
  2510.                         if turtle.compare() then --compare slot with block in front
  2511.                             matchItem = true
  2512.                         end
  2513.                     elseif direction == "up" then
  2514.                         if turtle.compareUp() then --compare slot with block above
  2515.                             matchItem = true
  2516.                         end
  2517.                     elseif direction == "down" then
  2518.                         if turtle.compareDown() then --compare slot with block below
  2519.                             matchItem = true
  2520.                         end
  2521.                     end
  2522.                     if matchItem then
  2523.                         slotFound = checkSlot
  2524.                         amount = turtle.getItemCount(checkSlot)
  2525.                         item = checkList[i]
  2526.                         break
  2527.                     end
  2528.                 end
  2529.             end
  2530.         end
  2531.     end
  2532.    
  2533.     return slotFound, amount, item
  2534. end
  2535.  
  2536. function completeChests()
  2537.     local woodAvailable = 0
  2538.     local woodNeeded = 0
  2539.     local wood4chests = 0
  2540.     local chestsFromWood = 0
  2541.     local chestsNeeded = 0
  2542.    
  2543.     -- Checks quantity of wood/wood2 and uses excess for chests/fuel as required
  2544.     -- will only operate if wood/wood2 > 20
  2545.     if not placeStorage:getStoragePlaced("redstone") then
  2546.         chestsNeeded = 2
  2547.     end
  2548.     if not objectives["place extended storage"] then
  2549.         chestsNeeded = chestsNeeded + 5
  2550.     end
  2551.     chestsNeeded = chestsNeeded - turtle.getItemCount(slot:getItemSlot("chests")) + 1
  2552.     if chestsNeeded < 0 then
  2553.         chestsNeeded = 0
  2554.     end
  2555.     woodNeeded = chestsNeeded * 2
  2556.     --max 7 chests needed = 56 planks = 14 wood
  2557.    
  2558.     woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 24
  2559.     if woodAvailable > 0 then
  2560.         if woodAvailable > 16 then
  2561.             woodAvailable = 16
  2562.         end
  2563.         if woodAvailable > 1 then
  2564.             wood4chests = math.floor(woodAvailable / 2)
  2565.             if chestsNeeded > 0 then
  2566.                 if chestsNeeded >= wood4chests then --more chests needed than available wood
  2567.                     chestsFromWood = wood4chests
  2568.                 else
  2569.                     chestsFromWood = chestsNeeded
  2570.                 end
  2571.                 saveToLog("completeChests: making chests", true)
  2572.                 craft{craftItem = "planks", craftQuantity = chestsFromWood * 8, sourceItem1 = "wood", destSlot = 0, doSort = true}
  2573.                 craft{craftItem = "chests", craftQuantity = chestsFromWood, sourceItem1 = "planks", destSlot = 0}
  2574.                 chestsNeeded = chestsNeeded - chestsFromWood
  2575.             else
  2576.                 saveToLog("completeChests: using excess wood to refuel", true)
  2577.                 craft{craftItem = "planks", craftQuantity = woodAvailable * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
  2578.                 turtle.select(slot:getItemSlot("planks"))
  2579.                 turtle.refuel()
  2580.                 slot.update{self = slot, item = "planks", delete = true}
  2581.             end
  2582.         else
  2583.             saveToLog("completeChests: only one spare wood - ignoring", true)
  2584.         end
  2585.     end
  2586.     if slot:getItemSlot("wood2") > 0 then
  2587.         woodAvailable = turtle.getItemCount(slot:getItemSlot("wood2")) - 24
  2588.         if woodAvailable > 0 then
  2589.             if woodAvailable > 16 then
  2590.                 woodAvailable = 16
  2591.             end
  2592.             if woodAvailable > 1 then
  2593.                 wood4chests = math.floor(woodAvailable / 2) --1 wood = 0, 2 wood = 1
  2594.                 if chestsNeeded > 0 then
  2595.                     if chestsNeeded >= wood4chests then --more chests needed than available wood
  2596.                         chestsFromWood = wood4chests
  2597.                     else
  2598.                         chestsFromWood = chestsNeeded
  2599.                     end
  2600.                     saveToLog("completeChests: making chests", true)
  2601.                     craft{craftItem = "planks2", craftQuantity = chestsFromWood * 8, sourceItem1 = "wood2", destSlot = 0, doSort = true}
  2602.                     craft{craftItem = "chests", craftQuantity = chestsFromWood, sourceItem1 = "planks2", destSlot = 0}
  2603.                 else
  2604.                     saveToLog("completeChests: using excess wood to refuel", true)
  2605.                     craft{craftItem = "planks2", craftQuantity = woodAvailable * 4, sourceItem1 = "wood2", destSlot = 0, doSort = true}
  2606.                     turtle.select(slot:getItemSlot("planks2"))
  2607.                     turtle.refuel()
  2608.                     slot.update{self = slot, item = "planks2", delete = true}
  2609.                 end
  2610.             else
  2611.                 saveToLog("completeChests: only one spare wood2 - ignoring", true)
  2612.             end
  2613.         end
  2614.     end
  2615.     if not placeStorage:getStoragePlaced("redstone") then
  2616.         placeStorage:place("redstone", false)
  2617.     end
  2618.    
  2619.     return true
  2620. end
  2621.  
  2622. function confirmCobble()
  2623.     --[[suspected cobble found, so empty out
  2624.         all slots into chest except cobble,
  2625.         move it to slot 16 and craft furnace.
  2626.         If craft ok then move furnace
  2627.         else try another unknown block]]--
  2628.     local sTime = os.time()
  2629.     local foundCobble = false
  2630.     local useItem = ""
  2631.     local useSlot = 0
  2632.  
  2633.    
  2634.     sortInventory(true)
  2635.     --cobble, gravel, flint, iron ore, ? sand, ? clay may have been found
  2636.     term.clear()
  2637.     term.setCursorPos(1,1)
  2638.     useSlot = slot:getItemSlot("cobble")
  2639.     if useSlot > 0 then
  2640.         if craft{craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 0} then
  2641.             foundCobble = true
  2642.         else
  2643.             slot.update{self = slot, slotNo = useSlot, newItem = slot:getFreeUnknownItem()}
  2644.         end
  2645.     end
  2646.     if not foundCobble then
  2647.         if slot:getUnknownItemCount() > 0 then
  2648.             for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  2649.                 saveToLog("confirmCobble: testing slot with unknown items = "..i.." with "..turtle.getItemCount(i).." items", false)
  2650.                 turtle.select(i)
  2651.                 if turtle.place() then --cobble, gravel, sand, iron ore, clay
  2652.                     turtle.dig()
  2653.                     if slot:getItemSlot("cobble") == 0 then
  2654.                         useItem = slot:getSlotContains(i)
  2655.                         slot.update{self = slot, slotNo = i, newItem = "cobble"}
  2656.                         saveToLog("confirmCobble: attempting to craft furnace", true)
  2657.                         if craft{craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 0} then
  2658.                             foundCobble = true
  2659.                             break
  2660.                         else
  2661.                             slot.update{self = slot, slotNo = i, newItem = useItem}
  2662.                         end
  2663.                     end
  2664.                 end
  2665.             end
  2666.         end
  2667.     end
  2668.  
  2669.    
  2670.     saveToLog("confirmCobble started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
  2671.  
  2672.     return foundCobble
  2673. end
  2674.  
  2675. function craft(arg)
  2676.     --[[ assume empty chest next to turtle except at very early game stages.
  2677.     chest options: "doNotUse"
  2678.     doSort = true/false
  2679.     Examples:
  2680.     firstTree craft - planks from 1 wood: craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 3, chest = "doNotUse"}
  2681.     firstTree make more planks: craft{craftItem = "planks", craftQuantity = 8, sourceItem1 = "wood", destSlot = 3, chest = "doNotUse"}
  2682.     firstTree make 1 chest: craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 4}
  2683.     checkRemaining craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = emptySlot}
  2684.     late stage: craft{craftItem = "computer", craftQuantity = 1, sourceItem1 = "glass panes", sourceItem2 = "redstone",sourceItem3 = "stone", destSlot = 16}
  2685.     ]]--
  2686.    
  2687.     --local sourceItem1 = arg.sourceItem1
  2688.     --local sourceItem2 = arg.sourceItem2
  2689.     --local sourceItem3 = arg.sourceItem3
  2690.     local sourceSlot1 = 0
  2691.     local sourceSlot2 = 0
  2692.     local sourceSlot3 = 0
  2693.     local sourceQuantity1 = 0
  2694.     local sourceQuantity2 = 0
  2695.     local sourceQuantity3 = 0
  2696.     local success = false
  2697.     local useChest = true
  2698.     local emptySlot = 0
  2699.     local gridContents = {}
  2700.     local gridSlot = {}
  2701.     for i = 1, 16 do
  2702.         gridContents[i] = ""
  2703.         gridSlot[i] = 0
  2704.     end
  2705.    
  2706.     if arg.doSort == nil then
  2707.         arg.doSort = false
  2708.     end
  2709.     if arg.doSort then
  2710.         sortInventory(true)
  2711.     end
  2712.    
  2713.     if arg.sourceItem1 == nil then
  2714.         sourceSlot1 = 0
  2715.         sourceQuantity1 = 0
  2716.     else
  2717.         sourceSlot1 = slot:getItemSlot(arg.sourceItem1)
  2718.         sourceQuantity1 = turtle.getItemCount(sourceSlot1)
  2719.     end
  2720.     if arg.sourceItem2 == nil then
  2721.         arg.sourceItem2 = ""
  2722.         sourceSlot2 = 0
  2723.     else
  2724.         sourceSlot2 = slot.getItemSlot(slot, arg.sourceItem2)
  2725.         sourceQuantity2 = turtle.getItemCount(sourceSlot2)
  2726.     end
  2727.     if arg.sourceItem3 == nil then
  2728.         arg.sourceItem3 = ""
  2729.         sourceSlot3 = 0
  2730.     else
  2731.         sourceSlot3 = slot.getItemSlot(slot, arg.sourceItem3)
  2732.         sourceQuantity3 = turtle.getItemCount(sourceSlot3)
  2733.     end
  2734.     if arg.destSlot == nil then
  2735.         arg.destSlot = 16
  2736.     end
  2737.     if arg.chest == "doNotUse" then
  2738.         useChest = false
  2739.     end
  2740.     saveToLog("craft: craftItem = "..tostring(arg.craftItem)..
  2741.                 ", craftQuantity = "..tostring(arg.craftQuantity)..
  2742.                 ", sourceItem1 = "..tostring(arg.sourceItem1)..
  2743.                 ", sourceItem2 = "..tostring(arg.sourceItem2)..
  2744.                 ", sourceItem3 = "..tostring(arg.sourceItem3)..
  2745.                 ", destSlot = "..tostring(arg.destSlot), false)
  2746.                
  2747.     if useChest then --place chest forwards
  2748.         --if arg.doSort then
  2749.             --sortInventory(true)
  2750.         --end
  2751.         turtle.select(1)
  2752.         while turtle.detect() do --clear space in front
  2753.             --turtle.dig()
  2754.             dig.digNew{self = dig, slotNo = 1, callFrom = "craft"}
  2755.         end
  2756.         --while turtle.attack() do --in case mob in front
  2757.         while attack() do --in case mobs in front
  2758.             saveToLog("Mob attacked again!", false)
  2759.         end
  2760.         turtle.select(slot:getItemSlot("chests"))
  2761.         if turtle.place() then
  2762.             --check if chest is actually in front until cc 1.6.3 bug is sorted
  2763.             local tempChestSlot = slot:getItemSlot("chests")
  2764.             slot.update{self = slot, item = "chests", delete = true}
  2765.             if not turtle.detect() then --chest not present
  2766.                 saveToLog("cc 1.6.3 place() bug, moving forward to rescue chest", true)
  2767.                 forward(1)
  2768.                 if turtle.detect() then
  2769.                     saveToLog("cc 1.6.3 place() bug, ? chest found in front, digging...", true)
  2770.                     dig.digNew{self = dig, slotNo = tempChestSlot, expectedItem = "chests", callFrom = "craft"}
  2771.                 elseif turtle.detectDown() then
  2772.                     saveToLog("cc 1.6.3 place() bug, ? chest found below, digging...", true)
  2773.                     dig.digNew{self = dig, direction = "down", slotNo = tempChestSlot, expectedItem = "chests", callFrom = "craft"}
  2774.                 end
  2775.                 turtle.select(tempChestSlot)
  2776.                 if turtle.refuel(0) then
  2777.                     saveToLog("cc 1.6.3 place() bug, chest found!", true)
  2778.                     slot.update{self = slot, slotNo = tempChestSlot, item = "chests"}
  2779.                 else
  2780.                     saveToLog("cc 1.6.3 place() bug, ? chest lost. exiting program...", true)
  2781.                     error()
  2782.                 end
  2783.                 back(1)
  2784.                 turtle.select(slot:getItemSlot("chests"))
  2785.                 turtle.place()
  2786.                 slot.update{self = slot, item = "chests", delete = true}
  2787.             end
  2788.             saveToLog("craft: About to fill chest", false)
  2789.             fillChest{direction = arg.direction, exceptSlot1 = sourceSlot1, exceptSlot2 = sourceSlot2, exceptSlot3 = sourceSlot3, doReset = false} -- fill except chosen item(s)
  2790.         else
  2791.             saveToLog("UNABLE TO PLACE CHEST", true)
  2792.             error()
  2793.         end
  2794.     end
  2795.     if destSlot == 0 then
  2796.         saveToLog("Craft: item = "..arg.craftItem.." Quantity = "..arg.craftQuantity.." from "..arg.sourceItem1..", "..arg.sourceItem2..", "..arg.sourceItem3.." put in first empty slot", true)
  2797.     else
  2798.         saveToLog("Craft: item = "..arg.craftItem.." Quantity = "..arg.craftQuantity.." from "..arg.sourceItem1..", "..arg.sourceItem2..", "..arg.sourceItem3.." put in slot "..arg.destSlot, true)
  2799.     end
  2800.     --turtle emptied out except for crafting items
  2801.     if sourceSlot1 ~= 16 then --not already in position
  2802.         if turtle.getItemCount(16) == 0 then
  2803.             saveToLog("Craft: moving "..arg.sourceItem1.." from "..sourceSlot1.." to slot 16", true, false)
  2804.             turtle.select(sourceSlot1)
  2805.             turtle.transferTo(16) --move selected first item to slot 16
  2806.             slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
  2807.         else -- already occupied, inventory full, so swap 15 and 16
  2808.             for  i = 1, 14 do
  2809.                 if turtle.getItemCount(i) == 0 then
  2810.                     emptySlot = i
  2811.                 end
  2812.                 break
  2813.             end
  2814.             if sourceSlot2 == 16 then
  2815.                 sourceSlot2 = emptySlot
  2816.             end
  2817.             saveToLog("Craft: moving "..slot:getSlotContains(16).." from slot 16 to slot "..emptySlot, false)
  2818.             turtle.select(16)
  2819.             turtle.transferTo(emptySlot)
  2820.             slot.transfer{self = slot, item = slot:getSlotContains(16), transferTo = emptySlot}
  2821.             saveToLog("Craft: moving "..arg.sourceItem1.." from "..sourceSlot1.." to slot 16", false)
  2822.             turtle.select(sourceSlot1)
  2823.             turtle.transferTo(16) --move selected first item to slot 16
  2824.             slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
  2825.         end
  2826.     end
  2827.     turtle.select(16)
  2828.     if arg.craftItem == "planks" or arg.craftItem == "planks2" then  --craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 3}
  2829.         turtle.transferTo(1, arg.craftQuantity / 4)
  2830.         gridContents[1] = arg.sourceItem1
  2831.     elseif arg.craftItem == "chests" then  --craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 4}
  2832.         --8 planks = 1 chest
  2833.         turtle.transferTo(1, arg.craftQuantity)
  2834.         turtle.transferTo(2, arg.craftQuantity)
  2835.         turtle.transferTo(3, arg.craftQuantity)
  2836.         turtle.transferTo(5, arg.craftQuantity)
  2837.         turtle.transferTo(7, arg.craftQuantity)
  2838.         turtle.transferTo(9, arg.craftQuantity)
  2839.         turtle.transferTo(10, arg.craftQuantity)
  2840.         turtle.transferTo(11, arg.craftQuantity)
  2841.         gridContents[1] = arg.sourceItem1
  2842.         gridContents[2] = arg.sourceItem1
  2843.         gridContents[3] = arg.sourceItem1
  2844.         gridContents[5] = arg.sourceItem1
  2845.         gridContents[7] = arg.sourceItem1
  2846.         gridContents[9] = arg.sourceItem1
  2847.         gridContents[10] = arg.sourceItem1
  2848.         gridContents[11] = arg.sourceItem1
  2849.         gridSlot[1] = sourceSlot1
  2850.         gridSlot[2] = sourceSlot1
  2851.         gridSlot[3] = sourceSlot1
  2852.         gridSlot[5] = sourceSlot1
  2853.         gridSlot[7] = sourceSlot1
  2854.         gridSlot[9] = sourceSlot1
  2855.         gridSlot[10] = sourceSlot1
  2856.         gridSlot[11] = sourceSlot1
  2857.     elseif arg.craftItem == "furnaces" then --craft{craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 16}
  2858.         -- 8 cobble = 1 furnace
  2859.         turtle.transferTo(1, arg.craftQuantity)
  2860.         turtle.transferTo(2, arg.craftQuantity)
  2861.         turtle.transferTo(3, arg.craftQuantity)
  2862.         turtle.transferTo(5, arg.craftQuantity)
  2863.         turtle.transferTo(7, arg.craftQuantity)
  2864.         turtle.transferTo(9, arg.craftQuantity)
  2865.         turtle.transferTo(10, arg.craftQuantity)
  2866.         turtle.transferTo(11, arg.craftQuantity)
  2867.         gridContents[1] = arg.sourceItem1
  2868.         gridContents[2] = arg.sourceItem1
  2869.         gridContents[3] = arg.sourceItem1
  2870.         gridContents[5] = arg.sourceItem1
  2871.         gridContents[7] = arg.sourceItem1
  2872.         gridContents[9] = arg.sourceItem1
  2873.         gridContents[10] = arg.sourceItem1
  2874.         gridContents[11] = arg.sourceItem1
  2875.         gridSlot[1] = sourceSlot1
  2876.         gridSlot[2] = sourceSlot1
  2877.         gridSlot[3] = sourceSlot1
  2878.         gridSlot[5] = sourceSlot1
  2879.         gridSlot[7] = sourceSlot1
  2880.         gridSlot[9] = sourceSlot1
  2881.         gridSlot[10] = sourceSlot1
  2882.         gridSlot[11] = sourceSlot1
  2883.     elseif arg.craftItem == "walls" then --craft{craftItem = "walls", craftQuantity = 6, sourceItem1 = "moss stone", destSlot = 0}
  2884.         -- 6 mossy cobble = 6 mossy cobblestone wall
  2885.         turtle.transferTo(1, arg.craftQuantity / 6)
  2886.         turtle.transferTo(2, arg.craftQuantity / 6)
  2887.         turtle.transferTo(3, arg.craftQuantity / 6)
  2888.         turtle.transferTo(5, arg.craftQuantity / 6)
  2889.         turtle.transferTo(6, arg.craftQuantity / 6)
  2890.         turtle.transferTo(7, arg.craftQuantity / 6)
  2891.         gridContents[1] = arg.sourceItem1
  2892.         gridContents[2] = arg.sourceItem1
  2893.         gridContents[3] = arg.sourceItem1
  2894.         gridContents[5] = arg.sourceItem1
  2895.         gridContents[6] = arg.sourceItem1
  2896.         gridContents[7] = arg.sourceItem1
  2897.         gridSlot[1] = sourceSlot1
  2898.         gridSlot[2] = sourceSlot1
  2899.         gridSlot[3] = sourceSlot1
  2900.         gridSlot[5] = sourceSlot1
  2901.         gridSlot[6] = sourceSlot1
  2902.         gridSlot[7] = sourceSlot1
  2903.     elseif arg.craftItem == "sticks" then --craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = 13}  
  2904.         -- 2 planks gives 4 sticks
  2905.         turtle.transferTo(1, arg.craftQuantity / 4)
  2906.         turtle.transferTo(5, arg.craftQuantity / 4)
  2907.         gridContents[1] = arg.sourceItem1
  2908.         gridContents[5] = arg.sourceItem1
  2909.         gridSlot[1] = sourceSlot1
  2910.         gridSlot[5] = sourceSlot1
  2911.     elseif arg.craftItem == "nuggets" then --craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "gold", destSlot = 0}
  2912.         --1 gold gives 9 nuggets                   
  2913.         turtle.transferTo(1, arg.craftQuantity / 9)
  2914.         gridContents[1] = arg.sourceItem1
  2915.         gridSlot[1] = sourceSlot1
  2916.     elseif arg.craftItem == "buckets" then --craft{craftItem = "buckets", craftQuantity = 1, sourceItem1 = "iron", destSlot = 16}
  2917.         turtle.transferTo(1, arg.craftQuantity)
  2918.         turtle.transferTo(3, arg.craftQuantity)
  2919.         turtle.transferTo(6, arg.craftQuantity)
  2920.         gridContents[1] = arg.sourceItem1
  2921.         gridContents[3] = arg.sourceItem1
  2922.         gridContents[6] = arg.sourceItem1
  2923.         gridSlot[1] = sourceSlot1
  2924.         gridSlot[3] = sourceSlot1
  2925.         gridSlot[6] = sourceSlot1
  2926.     elseif arg.craftItem == "crafting table" then --craft{craftItem = "crafting table", craftQuantity = 1, sourceItem1 = "planks", destSlot = 16}
  2927.         -- 4 planks = 1 table
  2928.         turtle.transferTo(1, arg.craftQuantity)
  2929.         turtle.transferTo(2, arg.craftQuantity)
  2930.         turtle.transferTo(5, arg.craftQuantity)
  2931.         turtle.transferTo(6, arg.craftQuantity)
  2932.         gridContents[1] = arg.sourceItem1
  2933.         gridContents[2] = arg.sourceItem1
  2934.         gridContents[5] = arg.sourceItem1
  2935.         gridContents[6] = arg.sourceItem1
  2936.         gridSlot[1] = sourceSlot1
  2937.         gridSlot[2] = sourceSlot1
  2938.         gridSlot[5] = sourceSlot1
  2939.         gridSlot[6] = sourceSlot1
  2940.     elseif arg.craftItem == "torches" then --craft{craftItem = "torches", craftQuantity = 4, sourceItem1 = "sticks", sourceItem2 = "charcoal" destSlot = 16}// OR sourceItem2 = "coal"
  2941.         -- 1 stick + 1 coal/charcoal = 4 torches
  2942.         if sourceSlot2 ~= 15 then
  2943.             turtle.select(sourceSlot2) -- move coal/charcoal to 15
  2944.             turtle.transferTo(15)
  2945.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  2946.         end
  2947.         turtle.select(16)
  2948.         turtle.transferTo(5, arg.craftQuantity / 4) --move sticks to 5
  2949.         turtle.select(15)
  2950.         turtle.transferTo(1, arg.craftQuantity / 4) --move coal/charcoal to 1
  2951.         gridContents[1] = arg.sourceItem2
  2952.         gridContents[5] = arg.sourceItem1
  2953.         gridSlot[1] = sourceSlot2
  2954.         gridSlot[5] = sourceSlot1
  2955.     elseif arg.craftItem == "signs" then --craft{craftItem = "signs", craftQuantity = 3, sourceItem1 = "sticks", sourceItem2 = "planks" destSlot = 16}
  2956.         -- 1 stick + 6 planks = 3 signs
  2957.         if sourceSlot2 ~= 15 then
  2958.             turtle.select(sourceSlot2) -- move planks to 15
  2959.             turtle.transferTo(15)
  2960.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  2961.         end
  2962.         turtle.select(16)
  2963.         turtle.transferTo(10, arg.craftQuantity / 3) --move sticks to 5
  2964.         turtle.select(15)
  2965.         turtle.transferTo(1, arg.craftQuantity / 3) --move planks to 1
  2966.         turtle.transferTo(2, arg.craftQuantity / 3) --move planks to 2
  2967.         turtle.transferTo(3, arg.craftQuantity / 3) --move planks to 3
  2968.         turtle.transferTo(5, arg.craftQuantity / 3) --move planks to 5
  2969.         turtle.transferTo(6, arg.craftQuantity / 3) --move planks to 6
  2970.         turtle.transferTo(7, arg.craftQuantity / 3) --move planks to 7
  2971.         gridContents[1] = arg.sourceItem2
  2972.         gridContents[2] = arg.sourceItem2
  2973.         gridContents[3] = arg.sourceItem2
  2974.         gridContents[5] = arg.sourceItem2
  2975.         gridContents[6] = arg.sourceItem2
  2976.         gridContents[7] = arg.sourceItem2
  2977.         gridContents[10] = arg.sourceItem1
  2978.         gridSlot[1] = sourceSlot2
  2979.         gridSlot[2] = sourceSlot2
  2980.         gridSlot[3] = sourceSlot2
  2981.         gridSlot[5] = sourceSlot2
  2982.         gridSlot[6] = sourceSlot2
  2983.         gridSlot[7] = sourceSlot2
  2984.         gridSlot[10] = sourceSlot1
  2985.     elseif arg.craftItem == "lapis block" or arg.craftItem == "redstone block" then --craft{craftItem = "lapis block", craftQuantity = 1, sourceItem1 = "lapis", destSlot = 0}
  2986.         --9 lapis = 1 lapis block
  2987.         turtle.transferTo(1, arg.craftQuantity)
  2988.         turtle.transferTo(2, arg.craftQuantity)
  2989.         turtle.transferTo(3, arg.craftQuantity)
  2990.         turtle.transferTo(5, arg.craftQuantity)
  2991.         turtle.transferTo(6, arg.craftQuantity)
  2992.         turtle.transferTo(7, arg.craftQuantity)
  2993.         turtle.transferTo(9, arg.craftQuantity)
  2994.         turtle.transferTo(10, arg.craftQuantity)
  2995.         turtle.transferTo(11, arg.craftQuantity)
  2996.         gridContents[1] = arg.sourceItem1
  2997.         gridContents[2] = arg.sourceItem1
  2998.         gridContents[3] = arg.sourceItem1
  2999.         gridContents[5] = arg.sourceItem1
  3000.         gridContents[6] = arg.sourceItem1
  3001.         gridContents[7] = arg.sourceItem1
  3002.         gridContents[9] = arg.sourceItem1
  3003.         gridContents[10] = arg.sourceItem1
  3004.         gridContents[11] = arg.sourceItem1
  3005.         gridSlot[1] = sourceSlot1
  3006.         gridSlot[2] = sourceSlot1
  3007.         gridSlot[3] = sourceSlot1
  3008.         gridSlot[5] = sourceSlot1
  3009.         gridSlot[6] = sourceSlot1
  3010.         gridSlot[7] = sourceSlot1
  3011.         gridSlot[9] = sourceSlot1
  3012.         gridSlot[10] = sourceSlot1
  3013.         gridSlot[11] = sourceSlot1
  3014.     elseif arg.craftItem == "diamond pickaxe" then --craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "diamonds" destSlot = 16}
  3015.         --2 sticks + 3 diamonds = 1 axe
  3016.         if sourceSlot2 ~= 15 then
  3017.             turtle.select(sourceSlot2) -- ?diamonds or diamonds
  3018.             turtle.transferTo(15)
  3019.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  3020.         end
  3021.         turtle.select(16)
  3022.         turtle.transferTo(6, arg.craftQuantity) --move sticks to 6
  3023.         turtle.transferTo(10, arg.craftQuantity) --move sticks to 10
  3024.         turtle.select(15)
  3025.         turtle.transferTo(1, arg.craftQuantity) --move diamond to 1
  3026.         turtle.transferTo(2, arg.craftQuantity) --move diamond to 2
  3027.         turtle.transferTo(3, arg.craftQuantity) --move diamond to 3
  3028.         gridContents[1] = arg.sourceItem2
  3029.         gridContents[2] = arg.sourceItem2
  3030.         gridContents[3] = arg.sourceItem2
  3031.         gridContents[6] = arg.sourceItem1
  3032.         gridContents[10] = arg.sourceItem1
  3033.         gridSlot[1] = sourceSlot2
  3034.         gridSlot[2] = sourceSlot2
  3035.         gridSlot[3] = sourceSlot2
  3036.         gridSlot[6] = sourceSlot1
  3037.         gridSlot[10] = sourceSlot1
  3038.     elseif arg.craftItem == "paper" then --craft{craftItem = "paper", craftQuantity = 3, sourceItem1 = "sugar cane", destSlot = 16}
  3039.         --3 sugar cane = 3 paper
  3040.         turtle.transferTo(1, arg.craftQuantity / 3)
  3041.         turtle.transferTo(2, arg.craftQuantity / 3)
  3042.         turtle.transferTo(3, arg.craftQuantity / 3)
  3043.         gridContents[1] = arg.sourceItem1
  3044.         gridContents[2] = arg.sourceItem1
  3045.         gridContents[3] = arg.sourceItem1
  3046.         gridSlot[1] = sourceSlot1
  3047.         gridSlot[2] = sourceSlot1
  3048.         gridSlot[3] = sourceSlot1
  3049.     elseif arg.craftItem == "fences" then --craft{craftItem = "fences", craftQuantity = 2, sourceItem1 = "sticks", destSlot = 0}
  3050.         -- 6 sticks = 2 fences
  3051.         turtle.transferTo(5, arg.craftQuantity / 2)
  3052.         turtle.transferTo(6, arg.craftQuantity / 2)
  3053.         turtle.transferTo(7, arg.craftQuantity / 2)
  3054.         turtle.transferTo(9, arg.craftQuantity / 2)
  3055.         turtle.transferTo(10, arg.craftQuantity / 2)
  3056.         turtle.transferTo(11, arg.craftQuantity / 2)
  3057.         gridContents[5] = arg.sourceItem1
  3058.         gridContents[6] = arg.sourceItem1
  3059.         gridContents[7] = arg.sourceItem1
  3060.         gridContents[9] = arg.sourceItem1
  3061.         gridContents[10] = arg.sourceItem1
  3062.         gridContents[11] = arg.sourceItem1
  3063.         gridSlot[5] = sourceSlot1
  3064.         gridSlot[6] = sourceSlot1
  3065.         gridSlot[7] = sourceSlot1
  3066.         gridSlot[9] = sourceSlot1
  3067.         gridSlot[10] = sourceSlot1
  3068.         gridSlot[11] = sourceSlot1
  3069.     elseif arg.craftItem == "glass panes" then --craft{craftItem = "glass panes", craftQuantity = 16, sourceItem1 = "glass", destSlot = 0}
  3070.         -- 6 glass = 16 panes
  3071.         turtle.transferTo(5, arg.craftQuantity / 16)
  3072.         turtle.transferTo(6, arg.craftQuantity / 16)
  3073.         turtle.transferTo(7, arg.craftQuantity / 16)
  3074.         turtle.transferTo(9, arg.craftQuantity / 16)
  3075.         turtle.transferTo(10, arg.craftQuantity / 16)
  3076.         turtle.transferTo(11, arg.craftQuantity / 16)
  3077.         gridContents[5] = arg.sourceItem1
  3078.         gridContents[6] = arg.sourceItem1
  3079.         gridContents[7] = arg.sourceItem1
  3080.         gridContents[9] = arg.sourceItem1
  3081.         gridContents[10] = arg.sourceItem1
  3082.         gridContents[11] = arg.sourceItem1
  3083.         gridSlot[5] = sourceSlot1
  3084.         gridSlot[6] = sourceSlot1
  3085.         gridSlot[7] = sourceSlot1
  3086.         gridSlot[9] = sourceSlot1
  3087.         gridSlot[10] = sourceSlot1
  3088.         gridSlot[11] = sourceSlot1
  3089.     elseif arg.craftItem == "floppy disk" then  --craft{craftItem = "floppy disk", craftQuantity = 1, sourceItem1 = "paper", sourceItem2 = "redstone" destSlot = 16}
  3090.         -- 1 paper, 1 redstone
  3091.         if sourceSlot2 ~= 15 then
  3092.             turtle.select(sourceSlot2) --redstone
  3093.             turtle.transferTo(15)
  3094.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  3095.         end
  3096.         turtle.select(16)
  3097.         turtle.transferTo(5, arg.craftQuantity) --move paper to 5
  3098.         turtle.select(15)
  3099.         turtle.transferTo(1, arg.craftQuantity) --move redstone to 1
  3100.         gridContents[1] = arg.sourceItem2
  3101.         gridContents[5] = arg.sourceItem1
  3102.         gridSlot[1] = sourceSlot2
  3103.         gridSlot[5] = sourceSlot1
  3104.     elseif arg.craftItem == "computer" then --craft{craftItem = "computer", craftQuantity = 1, sourceItem1 = "glass panes", sourceItem2 = "redstone",sourceItem3 = "stone", destSlot = 16}
  3105.         -- 1 glass panes, 1 redstone, 7 stone
  3106.         if sourceSlot2 ~= 15 then
  3107.             turtle.select(sourceSlot2) --redstone
  3108.             turtle.transferTo(15)
  3109.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  3110.         end
  3111.         if sourceSlot3 ~= 14 then
  3112.             turtle.select(sourceSlot3) --stone
  3113.             turtle.transferTo(14)
  3114.             slot.transfer{self = slot, item = arg.sourceItem3, transferTo = 14}
  3115.         end
  3116.         turtle.select(16)
  3117.         turtle.transferTo(10, arg.craftQuantity) --move glass panes to 10
  3118.         turtle.select(15)
  3119.         turtle.transferTo(6, arg.craftQuantity) --move redstone to 6
  3120.         turtle.select(14) --stone
  3121.         turtle.transferTo(1, arg.craftQuantity) --move stone to 6
  3122.         turtle.transferTo(2, arg.craftQuantity)
  3123.         turtle.transferTo(3, arg.craftQuantity)
  3124.         turtle.transferTo(5, arg.craftQuantity)
  3125.         turtle.transferTo(7, arg.craftQuantity)
  3126.         turtle.transferTo(9, arg.craftQuantity)
  3127.         turtle.transferTo(11, arg.craftQuantity)
  3128.         gridContents[1] = arg.sourceItem3
  3129.         gridContents[2] = arg.sourceItem3
  3130.         gridContents[3] = arg.sourceItem3
  3131.         gridContents[5] = arg.sourceItem3
  3132.         gridContents[6] = arg.sourceItem2
  3133.         gridContents[7] = arg.sourceItem3
  3134.         gridContents[9] = arg.sourceItem3
  3135.         gridContents[10] = arg.sourceItem1
  3136.         gridContents[11] = arg.sourceItem3
  3137.         gridSlot[1] = sourceSlot3
  3138.         gridSlot[2] = sourceSlot3
  3139.         gridSlot[3] = sourceSlot3
  3140.         gridSlot[5] = sourceSlot3
  3141.         gridSlot[6] = sourceSlot2
  3142.         gridSlot[7] = sourceSlot3
  3143.         gridSlot[9] = sourceSlot3
  3144.         gridSlot[10] = sourceSlot1
  3145.         gridSlot[11] = sourceSlot3
  3146.     elseif arg.craftItem == "disk drive" then --craft{craftItem = "disk drive", craftQuantity = 1, sourceItem1 = "redstone", sourceItem2 = "stone", destSlot = 16}
  3147.         -- 2 redstone, 7 stone
  3148.         if sourceSlot2 ~= 15 then
  3149.             turtle.select(sourceSlot2) --stone
  3150.             turtle.transferTo(15)
  3151.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  3152.         end
  3153.         turtle.select(16)
  3154.         turtle.transferTo(6, arg.craftQuantity) --move to 6
  3155.         turtle.transferTo(10, arg.craftQuantity) --move to 10
  3156.         turtle.select(15)
  3157.         turtle.transferTo(1, arg.craftQuantity) --move stone to 6
  3158.         turtle.transferTo(2, arg.craftQuantity)
  3159.         turtle.transferTo(3, arg.craftQuantity)
  3160.         turtle.transferTo(5, arg.craftQuantity)
  3161.         turtle.transferTo(7, arg.craftQuantity)
  3162.         turtle.transferTo(9, arg.craftQuantity)
  3163.         turtle.transferTo(11, arg.craftQuantity)
  3164.         gridContents[1] = arg.sourceItem2
  3165.         gridContents[2] = arg.sourceItem2
  3166.         gridContents[3] = arg.sourceItem2
  3167.         gridContents[5] = arg.sourceItem2
  3168.         gridContents[6] = arg.sourceItem1
  3169.         gridContents[7] = arg.sourceItem2
  3170.         gridContents[9] = arg.sourceItem2
  3171.         gridContents[10] = arg.sourceItem1
  3172.         gridContents[11] = arg.sourceItem2
  3173.         gridSlot[1] = sourceSlot2
  3174.         gridSlot[2] = sourceSlot2
  3175.         gridSlot[3] = sourceSlot2
  3176.         gridSlot[5] = sourceSlot2
  3177.         gridSlot[6] = sourceSlot1
  3178.         gridSlot[7] = sourceSlot2
  3179.         gridSlot[9] = sourceSlot2
  3180.         gridSlot[10] = sourceSlot1
  3181.         gridSlot[11] = sourceSlot2
  3182.     elseif arg.craftItem == "turtle" then --craft{craftItem = "turtle", craftQuantity = 1, sourceItem1 = "chests", sourceItem2 = "computer",sourceItem3 = "iron", destSlot = 16}
  3183.         -- 1 chest, 1 computer, 7 iron
  3184.         if sourceSlot2 ~= 15 then
  3185.             turtle.select(sourceSlot2) --computer
  3186.             turtle.transferTo(15, arg.craftQuantity)
  3187.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  3188.         end
  3189.         if sourceSlot3 ~= 14 then
  3190.             turtle.select(sourceSlot3) --iron
  3191.             turtle.transferTo(14)
  3192.             slot.transfer{self = slot, item = arg.sourceItem3, transferTo = 14}
  3193.         end
  3194.         turtle.select(16)
  3195.         turtle.transferTo(10, arg.craftQuantity)
  3196.         turtle.select(15) --computer
  3197.         turtle.transferTo(6, arg.craftQuantity)
  3198.         turtle.select(14) --iron
  3199.         turtle.transferTo(1, arg.craftQuantity) --move iron
  3200.         turtle.transferTo(2, arg.craftQuantity)
  3201.         turtle.transferTo(3, arg.craftQuantity)
  3202.         turtle.transferTo(5, arg.craftQuantity)
  3203.         turtle.transferTo(7, arg.craftQuantity)
  3204.         turtle.transferTo(9, arg.craftQuantity)
  3205.         turtle.transferTo(11, arg.craftQuantity)
  3206.         gridContents[1] = arg.sourceItem3
  3207.         gridContents[2] = arg.sourceItem3
  3208.         gridContents[3] = arg.sourceItem3
  3209.         gridContents[5] = arg.sourceItem3
  3210.         gridContents[6] = arg.sourceItem2
  3211.         gridContents[7] = arg.sourceItem3
  3212.         gridContents[9] = arg.sourceItem3
  3213.         gridContents[10] = arg.sourceItem1
  3214.         gridContents[11] = arg.sourceItem3
  3215.         gridSlot[1] = sourceSlot3
  3216.         gridSlot[2] = sourceSlot3
  3217.         gridSlot[3] = sourceSlot3
  3218.         gridSlot[5] = sourceSlot3
  3219.         gridSlot[6] = sourceSlot2
  3220.         gridSlot[7] = sourceSlot3
  3221.         gridSlot[9] = sourceSlot3
  3222.         gridSlot[10] = sourceSlot1
  3223.         gridSlot[11] = sourceSlot3
  3224.     elseif arg.craftItem == "crafty mining turtle" then --craft{craftItem = "crafty mining turtle", craftQuantity = 1, sourceItem1 = "crafting table", sourceItem2 = "diamond pickaxe",sourceItem3 = "turtle", destSlot = 16}
  3225.         -- 1 crafting table, 1 diamond pickaxe,  1 turtle
  3226.         if sourceSlot2 ~= 15 then
  3227.             turtle.select(sourceSlot2) --pickaxe
  3228.             turtle.transferTo(15, arg.craftQuantity)
  3229.             slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
  3230.         end
  3231.         if sourceSlot3 ~= 14 then
  3232.             turtle.select(sourceSlot3) --turtle
  3233.             turtle.transferTo(14)
  3234.             slot.transfer{self = slot, item = arg.sourceItem3, transferTo = 14}
  3235.         end
  3236.         turtle.select(16)
  3237.         turtle.transferTo(1, arg.craftQuantity) --move crafting table to 1
  3238.         turtle.select(15)
  3239.         turtle.transferTo(3, arg.craftQuantity) --move pickaxe to 3
  3240.         turtle.select(14)
  3241.         turtle.transferTo(2, arg.craftQuantity) --move turtle to 2
  3242.         gridContents[1] = arg.sourceItem1
  3243.         gridContents[2] = arg.sourceItem3
  3244.         gridContents[3] = arg.sourceItem2
  3245.         gridSlot[1] = sourceSlot1
  3246.         gridSlot[2] = sourceSlot3
  3247.         gridSlot[3] = sourceSlot2
  3248.     end
  3249.     if useChest then
  3250.         if turtle.getItemCount(16) > 0 then
  3251.             saveToLog("craft: adding excess "..arg.sourceItem1.." to chest from slot 16 (/"..sourceSlot1..")", false)
  3252.             fillChest{direction = arg.direction, addItem = arg.sourceItem1, addAmount = turtle.getItemCount(16), fromSlot = 16, originalSlot = sourceSlot1} -- add remaining source items to chest
  3253.         end
  3254.         if turtle.getItemCount(15) > 0 then
  3255.             saveToLog("craft: adding excess "..arg.sourceItem2.." to chest from slot 15 (/"..sourceSlot2..")", false)
  3256.             fillChest{direction = arg.direction, addItem = arg.sourceItem2, addAmount = turtle.getItemCount(15),  fromSlot = 15, originalSlot = sourceSlot2} -- add remaining source items to chest
  3257.         end
  3258.         if turtle.getItemCount(14) > 0 then
  3259.             saveToLog("craft: adding excess "..arg.sourceItem3.." to chest from slot 14 (/"..sourceSlot3..")", false)
  3260.             fillChest{direction = arg.direction, addItem = arg.sourceItem3, addAmount = turtle.getItemCount(14),  fromSlot = 14, originalSlot = sourceSlot3} -- add remaining source items to chest
  3261.         end
  3262.         --slot.update{self = slot, slotNo = 16, item = arg.sourceItem1, delete = true}
  3263.         slot.update{self = slot, item = arg.sourceItem1, delete = true}
  3264.         if arg.sourceItem2 ~= "" then
  3265.             slot.update{self = slot, item = arg.sourceItem2, delete = true}
  3266.         end
  3267.         if arg.sourceItem3 ~= "" then
  3268.             slot.update{self = slot, item = arg.sourceItem3, delete = true}
  3269.         end
  3270.     end
  3271.    
  3272.     -- Attempt to craft item
  3273.     turtle.select(16)
  3274.     saveToLog("Attempting to craft "..arg.craftItem.." in slot 16", false)
  3275.     if turtle.craft() then
  3276.         success = true
  3277.         saveToLog("craft: success "..arg.craftItem.." in slot 16", true)
  3278.         --now put crafted item in chest first, so will mix with any existing similar items
  3279.         if useChest then
  3280.             saveToLog("craft: adding  crafted item "..arg.craftItem.." to chest", false)
  3281.             fillChest{direction = arg.direction, addItem = arg.craftItem, addAmount = turtle.getItemCount(16), fromSlot = 16, originalSlot = 16} -- add crafted item
  3282.         end
  3283.     else --crafting not successful, so empty out all items from chest
  3284.         saveToLog("Attempting to craft "..arg.craftItem.." did not succeed", true)
  3285.         --error()
  3286.         if useChest then
  3287.             saveToLog("craft: failed, filling chest to empty turtle", false)
  3288.             for i = 1, 16 do
  3289.                 if turtle.getItemCount(i) > 0 then
  3290.                     fillChest{direction = arg.direction, addItem = gridContents[i], addAmount = turtle.getItemCount(i), fromSlot = i, originalSlot = gridSlot[i]} -- add crafted item
  3291.                 end
  3292.             end
  3293.         end
  3294.     end
  3295.    
  3296.     if useChest then -- always, except in firstTree()
  3297.         saveToLog("Crafting finished, emptying chest back into turtle", false)
  3298.         emptyChest(arg.direction)
  3299.         if slot.getItemSlot(slot, "chests") > 0 then
  3300.             emptySlot = slot:getItemSlot("chests")
  3301.         else
  3302.             emptySlot = getFirstEmptySlot(false)
  3303.         end
  3304.         dig.digNew{self = dig, slotNo = emptySlot, expectedItem = "chests", callFrom = "craft"}
  3305.         slot.update{self = slot, slotNo = emptySlot, item = "chests"}
  3306.         if success then --item crafted OK. if existing eg torches, will be in correct slot already, else should return to 16
  3307.             turtle.select(16)
  3308.             if turtle.getItemCount(16) > 0 then --new item, so returned to 16
  3309.                 if arg.destSlot == 0 then --use any slot
  3310.                     arg.destSlot = getFirstEmptySlot(false)
  3311.                     if arg.destSlot == 0 then --no empty slots
  3312.                         arg.destSlot = 16
  3313.                     end
  3314.                 else
  3315.                     if slot:getItemSlot(arg.craftItem) > 0 then
  3316.                         arg.destSlot = slot:getItemSlot(arg.craftItem)
  3317.                     end
  3318.                 end
  3319.                 if arg.destSlot == 16 then
  3320.                     slot.update{self = slot, slotNo = 16, item = arg.craftItem}
  3321.                 else --move to selected slot
  3322.                     turtle.select(16)
  3323.                     if turtle.transferTo(arg.destSlot) then
  3324.                         saveToLog("craft: Moved "..arg.craftItem.." to slot "..arg.destSlot, false)
  3325.                         --slot.update{self = slot, slotNo = arg.destSlot, item = arg.craftItem}
  3326.                         slot.transfer{self = slot, item = arg.craftItem, transferTo = arg.destSlot}
  3327.                     else --slot full/other item
  3328.                         saveToLog("craft: Moving "..arg.craftItem.." to slot "..arg.destSlot.." failed. Still in slot 16", false)
  3329.                         emptySlot = getFirstEmptySlot(false)
  3330.                         if turtle.transferTo(emptySlot) then
  3331.                             saveToLog("craft: Moving "..arg.craftItem.." to slot "..emptySlot.." instead", false)
  3332.                             slot.update{self = slot, slotNo = emptySlot, item = arg.craftItem}
  3333.                         end
  3334.                     end
  3335.                 end
  3336.             else
  3337.                 saveToLog("craft: crafted item "..arg.craftItem.." already in correct slot", false)
  3338.             end
  3339.             sortInventory(true)
  3340.         end
  3341.     else --chest not used, only for first log first planks and first chest
  3342.         saveToLog("craft: "..arg.craftItem.." created in slot 16, moving to "..arg.destSlot, false)
  3343.         turtle.transferTo(arg.destSlot)
  3344.         slot.update{self = slot, slotNo = arg.destSlot, item = arg.craftItem}
  3345.         slot.update{self = slot, slotNo = 16, delete = true}
  3346.     end
  3347.    
  3348.     saveToLog("craft: function exit, success = "..tostring(success), false)
  3349.     return success
  3350. end
  3351.  
  3352. function craftMiningTurtle()
  3353.     local numWood = 0
  3354.     local woodNeeded = 0
  3355.     local planksNeeded = 0
  3356.     local numSticks = 0
  3357.     local sticksNeeded  = 0
  3358.     local numTurtles = 1
  3359.     local startFile = ""
  3360.     local itemCount = {}
  3361.    
  3362.     --[[ storage contents:
  3363.     storageTorches: torches
  3364.     storageSigns: signs
  3365.     storageSticks: sticks
  3366.     storageWood: wood
  3367.     storageIronore: iron, (ironore), (buckets)
  3368.     storageSand: sugar cane, sand, glass, glass panes
  3369.     storageRedstone: redstone
  3370.     storagePickaxes: diamond pickaxe, paper
  3371.     ]]--
  3372.     --[[ Items needed:
  3373.         6 sand              2 wood      8 planks -> smelt to glass -> craft to 16 glass panes --> use 1 per turtle
  3374.         floppy disk:
  3375.         3 sugar cane
  3376.         1 redstone
  3377.         disk drive:
  3378.         7 cobble            2 wood      8 planks -> smelt to stone
  3379.         1 redstone
  3380.         per turtle:
  3381.         7 cobble            2 wood      7 planks -> smelt to stone
  3382.         7 ironore           2 wood      7 planks -> smelt to iron
  3383.         1 crafting table    1 wood      4 planks
  3384.         1 chests            2 wood      8 planks
  3385.         1 diamond pickaxe   2 sticks    2 planks
  3386.         1 glass pane
  3387.         1 redstone
  3388.         For Disk drive:
  3389.             4 wood, 16 planks (includes glass smelt)
  3390.         For 1 turtle:
  3391.             7 wood, 28 planks
  3392.         Min 11 wood, 44 planks
  3393.         Max 18 wood, 72 planks
  3394.     ]]--
  3395.     --[[
  3396.         start under furnace / over storage torches
  3397.         drop un-necessary items first to create crafting space
  3398.         storageTorches = 0
  3399.         storageSigns = 2
  3400.         storageSticks = 4
  3401.         storageWood = 6
  3402.         storageIronore = 8
  3403.         storageSand = 10
  3404.         storageRedstone = 12
  3405.         storagePickaxes = 14
  3406.     ]]--
  3407.     saveToLog("craftMiningTurtle: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!", true)
  3408.     fso:useFileName("logCraftMiningTurtle.txt")
  3409.     saveToLog("craftMiningTurtle: starting..", true)
  3410.     --get rid of unknown items
  3411.     if slot:getItemSlot("item1") > 0 then
  3412.         turnRight(2)
  3413.         forward(2, 1)
  3414.         for i = 1, 4 do
  3415.             storeItem{toStore = extraStorage1, item = "item"..i, quantity = 0, updateSlot = true, doSort = true}
  3416.         end
  3417.         turnRight(2)
  3418.         forward(2, 1)
  3419.     end
  3420.     if objectives["diamonds"] then
  3421.         if turtle.getItemCount(slot:getItemSlot("diamonds")) >= 6 then
  3422.             numTurtles = 2
  3423.             if storagePickaxes:getItemCount("diamond pickaxe") > 0 then --pickaxe already made
  3424.                 sticksNeeded = 2
  3425.             else
  3426.                 sticksNeeded = 4
  3427.             end
  3428.         elseif turtle.getItemCount(slot:getItemSlot("diamonds")) >= 3 then -- 3 - 5 diamonds
  3429.             if storagePickaxes:getItemCount("diamond pickaxe") > 0 then --pickaxe already made
  3430.                 numTurtles = 2
  3431.             else
  3432.                 numTurtles = 1
  3433.             end
  3434.             sticksNeeded = 2
  3435.         else -- 0 - 2 diamonds + pickaxe
  3436.             if storagePickaxes:getItemCount("diamond pickaxe") == 0 then
  3437.                 sticksNeeded = 2
  3438.             end
  3439.             numTurtles = 1
  3440.         end
  3441.         if numTurtles == 1 then
  3442.             woodNeeded = 11
  3443.             planksNeeded = 44
  3444.         else
  3445.             woodNeeded = 18
  3446.             planksNeeded = 72
  3447.         end
  3448.         turnRight(1)
  3449.         dig.digNew{self = dig, expectedItem = "torches", callFrom = "craftMiningTurtle"}
  3450.         turnLeft(2)
  3451.         dig.digNew{self = dig, expectedItem = "torches", callFrom = "craftMiningTurtle"}
  3452.         turnRight(1)
  3453.         saveToLog("craftMiningTurtle: storing torches in storageTorches", true)
  3454.         storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
  3455.         forward(2) --storageSigns 2
  3456.         if slot:getItemSlot("signs") > 0 then
  3457.             saveToLog("craftMiningTurtle: storing signs in storageSigns", true)
  3458.             storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  3459.         end
  3460.         forward(2, 1) --storageSticks 4
  3461.         if slot:getItemSlot("sticks") > 0 then
  3462.             saveToLog("craftMiningTurtle: storing sticks in storageSticks", true)
  3463.             storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  3464.         end
  3465.         forward(4) -- storageIronore 8
  3466.         saveToLog("craftMiningTurtle: storing iron and buckets in storageIronore", true)
  3467.         storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
  3468.         storeItem{toStore = storageIronore, item = "buckets", quantity = 0, updateSlot = true, doSort = true}
  3469.         forward(2) -- storageSand 10
  3470.         saveToLog("craftMiningTurtle: removing sand and sugar cane from storageSand", true)
  3471.         getItemsFromStorage{fromStore = storageSand, item1 = "sand", item2 = "sugar cane"}
  3472.         storeItem{toStore = storageSand, item = "sand", quantity = turtle.getItemCount(slot:getItemSlot("sand")) - 6, updateSlot = true, doSort = false}
  3473.         saveToLog("craftMiningTurtle: storing dirt in storageSand", true)
  3474.         storeItem{toStore = storageSand, item = "dirt", quantity = 0, updateSlot = true, doSort = true}
  3475.         saveToLog("craftMiningTurtle: storing gravel in storageSand", true)
  3476.         storeItem{toStore = storageSand, item = "gravel", quantity = 0, updateSlot = true, doSort = true}
  3477.         saveToLog("craftMiningTurtle: storing coal in storageSand", true)
  3478.         storeItem{toStore = storageSand, item = "coal", quantity = 0, updateSlot = true, doSort = true}
  3479.         saveToLog("craftMiningTurtle: storing stone in storageSand", true)
  3480.         storeItem{toStore = storageSand, item = "stone", quantity = 0, updateSlot = true, doSort = true}
  3481.         forward(2) --storageRedstone 12
  3482.         saveToLog("craftMiningTurtle: removing redstone from storageRedstone", true)
  3483.         -- remove redstone, may be 64 already in store, so prepare to dump the rest
  3484.         for i = 1, 16 do
  3485.             itemCount[i] = turtle.getItemCount(i)
  3486.         end
  3487.         --storeItem{toStore = storageRedstone, item = "redstone", quantity = 0, updateSlot = true, doSort = true}
  3488.         getItemsFromStorage{fromStore = storageRedstone, item1 = "redstone"}
  3489.         -- check if overspill of redstone
  3490.         for i = 1, 16 do
  3491.             if i ~= slot:getItemSlot("redstone") then
  3492.                 if turtle.getItemCount(i) > itemCount[i] then
  3493.                     turtle.select(i)
  3494.                     turtle.dropUp()
  3495.                     break
  3496.                 end
  3497.             end
  3498.         end
  3499.         -- check if enough redstone
  3500.         if turtle.getItemCount(slot:getItemSlot("redstone")) < 3 then --try removing more if it exists
  3501.             getItemsFromStorage{fromStore = storageRedstone, item1 = "redstone"}
  3502.             for i = 1, 16 do
  3503.                 if i ~= slot:getItemSlot("redstone") then
  3504.                     if turtle.getItemCount(i) > itemCount[i] then
  3505.                         turtle.select(i)
  3506.                         turtle.dropUp()
  3507.                         break
  3508.                     end
  3509.                 end
  3510.             end
  3511.         end
  3512.         if turtle.getItemCount(slot:getItemSlot("redstone")) > 2 then
  3513.             storeItem{toStore = storageRedstone, item = "redstone", quantity = turtle.getItemCount(slot:getItemSlot("redstone")) - 3 - numTurtles, updateSlot = true, doSort = false}
  3514.         end
  3515.         turnRight(2)
  3516.         forward(6) --storageWood 6
  3517.         turnRight(2)
  3518.         saveToLog("craftMiningTurtle: removing wood from storage", true)
  3519.         getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  3520.         turnRight(2)
  3521.         forward(2) --storageSticks 4
  3522.         turnRight(2)
  3523.         if sticksNeeded > 0 then --need 2/4 sticks
  3524.             saveToLog("craftMiningTurtle: removing sticks from storage", true)
  3525.             getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
  3526.             if slot:getItemSlot("sticks") > 0 then --sticks onboard
  3527.                 numSticks = turtle.getItemCount(slot:getItemSlot("sticks"))
  3528.             end
  3529.             if sticksNeeded <= numSticks then
  3530.                 sticksNeeded = 0
  3531.             end
  3532.         end
  3533.         turnRight(2)
  3534.         forward(4) --storageTorches 0
  3535.         turnRight(2)
  3536.        
  3537.         --wood: crafting table = 2, sticks  = 1, smelt
  3538.         --planks: crafting table = 8, sticks = 2, smelt cobble = 21, smelt ironore = 14, smelt sand = 6, total 64
  3539.         if slot:getItemSlot("wood") > 0 then --wood already on board
  3540.             numWood = turtle.getItemCount(slot:getItemSlot("wood"))
  3541.         end
  3542.         if numWood < woodNeeded then --not enough wood on board
  3543.             harvestTreeFarm(false)
  3544.             replantTreeFarm(false, true)
  3545.         end
  3546.         craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
  3547.         if planksNeeded <= 64 then
  3548.             planksNeeded = 0
  3549.         else
  3550.             planksNeeded = planksNeeded - 64
  3551.         end
  3552.         if sticksNeeded > 0 then
  3553.             craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = 0, doSort = true}
  3554.         end
  3555.         if numTurtles == 2 then --make diamond pickaxe, even if one is in storage
  3556.             craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks",  sourceItem2 = "diamonds", destSlot = 0, doSort = true}
  3557.             -- else retrieve or make later
  3558.             if storagePickaxes:getItemCount("diamond pickaxe") == 0 then --no pickaxe already made
  3559.                 saveToLog("craftMiningTurtle: storing diamond pickaxe in storagePickaxes", true)
  3560.                 forward(14, 1) --storagePickaxes 4
  3561.                 storeItem{toStore = storagePickaxes, item = "diamond pickaxe", quantity = 0, updateSlot = true, doSort = true}
  3562.                 turnRight(2)
  3563.                 forward(14, 1) --under furnace
  3564.                 turnRight(2)
  3565.                 --make second pickaxe
  3566.                 craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks",  sourceItem2 = "diamonds", destSlot = 0, doSort = true}
  3567.             end
  3568.         end
  3569.         if storagePickaxes:getItemCount("diamond pickaxe") > 0 then --pickaxe already made
  3570.             saveToLog("craftMiningTurtle: storing sticks in storageSticks", true)
  3571.             forward(4, 1) --storageSticks 4
  3572.             storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  3573.             turnRight(2)
  3574.             forward(4, 1) --under furnace
  3575.             turnRight(2)
  3576.         end
  3577.         smelt("cobble", 7  * numTurtles + 7)
  3578.         smelt("ironore", 7 * numTurtles)
  3579.         smelt("sand", 6)
  3580.         if planksNeeded > 0 then
  3581.             craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
  3582.             planksNeeded = 0
  3583.         end
  3584.         saveToLog("craftMiningTurtle: storing wood in storageWood", true)
  3585.         forward(6, 1) -- storageWood = 6
  3586.         storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  3587.         forward(2) --over storageIronore 8
  3588.         saveToLog("craftMiningTurtle: storing ironore in storageIronore", true)
  3589.         storeItem{toStore = storageIronore, item = "ironore", quantity = 0, updateSlot = true, doSort = true}
  3590.         forward(2) --over storageSand 10
  3591.         saveToLog("craftMiningTurtle: storing cobble in storageSand", true)
  3592.         --storeItem{toStore = storageSand, item = "sand", quantity = 0, updateSlot = true, doSort = true}
  3593.         storeItem{toStore = storageSand, item = "cobble", quantity = 0, updateSlot = true, doSort = true}
  3594.         turnRight(2)
  3595.         forward(10, 1) --under furnace
  3596.         turnRight(2)
  3597.         craft{craftItem = "chests", craftQuantity = numTurtles, sourceItem1 = "planks", destSlot = 0, doSort = true}
  3598.         craft{craftItem = "crafting table", craftQuantity = 1 * numTurtles, sourceItem1 = "planks", destSlot = 0, doSort = true}
  3599.         forward(6, 1) -- storageWood  6
  3600.         saveToLog("craftMiningTurtle: storing planks in storageWood", true)
  3601.         storeItem{toStore = storageWood, item = "planks", quantity = 0, updateSlot = true, doSort = true}
  3602.         forward(4, 1) --storageSand 10, facing mine
  3603.         craft{craftItem = "paper", craftQuantity = 3, sourceItem1 = "sugar cane", destSlot = 0, doSort = true}
  3604.         saveToLog("craftMiningTurtle: storing sugar cane in storageSand", true)
  3605.         storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
  3606.         craft{craftItem = "glass panes", craftQuantity = 16, sourceItem1 = "glass", destSlot = 0, doSort = true}
  3607.         saveToLog("craftMiningTurtle: storing glass in storageSand", true)
  3608.         storeItem{toStore = storageSand, item = "glass", quantity = 0, updateSlot = true, doSort = true}
  3609.         saveToLog("craftMiningTurtle: storing glass panes in storageSand", true)
  3610.         storeItem{toStore = storageSand, item = "glass panes", quantity = 16 - numTurtles, updateSlot = true, doSort = true}
  3611.         craft{craftItem = "floppy disk", craftQuantity = 1, sourceItem1 = "paper", sourceItem2 = "redstone", destSlot = 0, doSort = true}
  3612.         saveToLog("craftMiningTurtle: storing paper in storageSand", true)
  3613.         storeItem{toStore = storageSand, item = "paper", quantity = 0, updateSlot = true, doSort = true}
  3614.         if numTurtles == 1 then
  3615.             --get pickaxe from chest
  3616.             if storagePickaxes:getItemCount("diamond pickaxe") > 0 then
  3617.                 forward(4, 1) --storagePickaxes 14, facing mine
  3618.                 saveToLog("craftMiningTurtle: removing diamond pickaxe from storagePickaxes", true)
  3619.                 getItemsFromStorage{fromStore = storagePickaxes, item1 = "diamond pickaxe"}
  3620.                 turnRight(2)
  3621.                 forward(4, 1) --storageSand 10, facing furnace
  3622.             else
  3623.                 turnRight(2) -- facing furnace
  3624.             end
  3625.         else
  3626.             turnRight(2) -- facing furnace
  3627.         end
  3628.         forward(10, 1) --under furnace
  3629.         turnRight(2)
  3630.         craft{craftItem = "computer", craftQuantity = 1 * numTurtles, sourceItem1 = "glass panes", sourceItem2 = "redstone", sourceItem3 = "stone", destSlot = 0, doSort = true}
  3631.         craft{craftItem = "turtle", craftQuantity = 1 * numTurtles, sourceItem1 = "chests", sourceItem2 = "computer", sourceItem3 = "iron", destSlot = 0, doSort = true}
  3632.         craft{craftItem = "crafty mining turtle", craftQuantity = 1, sourceItem1 = "crafting table", sourceItem2 = "diamond pickaxe", sourceItem3 = "turtle", destSlot = 0, doSort = true}
  3633.         craft{craftItem = "disk drive", craftQuantity = 1, sourceItem1 = "redstone", sourceItem2 = "stone", destSlot = 0, doSort = true}
  3634.         forward(8) -- storageIronore 8
  3635.         if slot:getItemSlot("iron") > 0 then
  3636.             saveToLog("craftMiningTurtle: storing excess iron in storageIronore", true)
  3637.             storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
  3638.         end
  3639.         forward(6, 1) -- storagePickaxes 14
  3640.         if numTurtles == 2 then
  3641.             if storagePickaxes:getItemCount("diamond pickaxe") > 0 then
  3642.                 saveToLog("craftMiningTurtle: removing diamond pickaxe from storagePickaxes", true)
  3643.                 getItemsFromStorage{fromStore = storagePickaxes, item1 = "diamond pickaxe"}
  3644.             end
  3645.         end
  3646.         turnRight(2)
  3647.         forward(2, 1) -- storageRedstone 12
  3648.         saveToLog("craftMiningTurtle: storing redstone in storageRedstone", true)
  3649.         storeItem{toStore = storageRedstone, item = "redstone", quantity = 0, updateSlot = true, doSort = true}
  3650.         forward(10, 1) -- storageSigns 2
  3651.         getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
  3652.         storeItem{toStore = storageSigns, item = "signs", quantity = turtle.getItemCount(slot:getItemSlot("signs")) - 1, updateSlot = true, doSort = false}
  3653.         forward(3, 1)
  3654.         turnRight(2)
  3655.         saveToLog("craftMiningTurtle: removing fuel from furnace", true)
  3656.         up(1, 1)
  3657.         turtle.select(getFirstEmptySlot())
  3658.         if turtle.suck() then
  3659.             turtle.refuel()
  3660.         end
  3661.         down(1)
  3662.         forward(1, 1)
  3663.         sortInventory(true)
  3664.         --destroy furnace
  3665.         storeItem{toStore = storageTorches, item = "planks", quantity = 0, updateSlot = true, doSort = true}
  3666.         saveToLog("craftMiningTurtle: breaking furnace", true)
  3667.         dig.digNew{self = dig, direction = "up", expectedItem = "furnaces", callFrom = "craftMiningTurtle"}
  3668.         sortInventory(true)
  3669.         saveToLog("craftMiningTurtle: storing furnace in storageTorches", true)
  3670.         storeItem{toStore = storageTorches, item = "furnaces", quantity = 0, updateSlot = true, doSort = true}
  3671.         --program mining turtle
  3672.         saveToLog("craftMiningTurtle: placing disk drive", true)
  3673.         turtle.select(slot:getItemSlot("disk drive"))
  3674.         turtle.place()
  3675.         slot.update{self = slot, item = "disk drive", delete = true}
  3676.         sortInventory(true)
  3677.         saveToLog("craftMiningTurtle: inserting floppy disk", true)
  3678.         turtle.select(slot:getItemSlot("floppy disk"))
  3679.         turtle.drop()
  3680.         slot.update{self = slot, item = "floppy disk", delete = true}
  3681.         sortInventory(true)
  3682.         up(1)
  3683.         forward(1) --now on top of disk drive
  3684.  
  3685.    
  3686.         if disk.isPresent("bottom") then
  3687.             local filepath = shell.getRunningProgram()
  3688.             local filename = fs.getName(filepath)
  3689.             if fs.getFreeSpace("/") > 9000000 then --config modified. use filecopy
  3690.                 saveToLog("craftMiningTurtle: copying files to floppy disk", true)
  3691.                 fs.copy(filepath, "/disk/"..filename)
  3692.             end
  3693.             --[[
  3694.             This script is a file called startup stored on the floppy disk
  3695.             When a turtle is placed next to the disk drive, it reads this script
  3696.             which opens 'minerList.txt' and sets the label to Miner2, (as '2' is stored in this file)
  3697.             Either copies start.lua to the turtle then modifies 'minerList.txt' to '3'
  3698.             or if on a server, requests the start.lua file via http from pastebin
  3699.             so the name Miner3 given for the second turtle, (if constructed)
  3700.             ]]--
  3701.             -- create/overwrite 'minerList.txt' on floppy disk
  3702.             startFile = fs.open("/disk/minerList.txt", "w")
  3703.             startFile.writeLine("2")
  3704.             startFile.close()
  3705.             -- create/overwrite startup
  3706.             startFile = fs.open("/disk/startup", "w")
  3707.             startFile.writeLine('function get(name, code)')
  3708.             startFile.writeLine('   local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))')
  3709.             startFile.writeLine('   if response then')
  3710.             startFile.writeLine('       local sCode = response.readAll()')
  3711.             startFile.writeLine('       if sCode ~= nil and sCode ~= "" then')
  3712.             startFile.writeLine('           local file = fs.open( name, "w" )')
  3713.             startFile.writeLine('           response.close()')
  3714.             startFile.writeLine('           file.write(sCode)')
  3715.             startFile.writeLine('           file.close()')
  3716.             startFile.writeLine('           return true')
  3717.             startFile.writeLine('       end')
  3718.             startFile.writeLine('   end')
  3719.             startFile.writeLine('   return false')
  3720.             startFile.writeLine('end') 
  3721.             startFile.writeLine('if fs.exists("/disk/minerList.txt") then')
  3722.             startFile.writeLine('   textFile = fs.open("/disk/minerList.txt", "r")')
  3723.             startFile.writeLine('   textIn = textFile.readAll()')
  3724.             startFile.writeLine('   minerName = "Miner"..textIn')
  3725.             startFile.writeLine('   textFile.close()')
  3726.             startFile.writeLine('   textOut = tostring(tonumber(textIn) + 1)')
  3727.             startFile.writeLine('else')
  3728.             startFile.writeLine('   minerName = "Miner2"')
  3729.             startFile.writeLine('   textOut = "3"')
  3730.             startFile.writeLine('end')
  3731.             startFile.writeLine('textFile = fs.open("/disk/minerList.txt", "w")')
  3732.             startFile.writeLine('textFile.writeLine(textOut)')
  3733.             startFile.writeLine('textFile.close()')
  3734.             startFile.writeLine('if os.getComputerLabel() == nil or string.find(os.getComputerLabel(),"Miner") == nil then')
  3735.             startFile.writeLine('   os.setComputerLabel(minerName)')
  3736.             startFile.writeLine('end')
  3737.             startFile.writeLine('print("Hello, my name is "..os.getComputerLabel())')
  3738.             startFile.writeLine('if fs.exists("/disk/start.lua") then')
  3739.             startFile.writeLine('   fs.copy("/disk/start.lua", "start.lua")')
  3740.             startFile.writeLine('   print("Replication program copied")')
  3741.             startFile.writeLine('else')
  3742.             startFile.writeLine('   get("start.lua", "'..pastebinCode..'")')   
  3743.             startFile.writeLine('end') 
  3744.             startFile.writeLine('print("use start.lua to begin self replicating ")')
  3745.             startFile.close()
  3746.         end
  3747.         back(1)
  3748.         down (1)
  3749.         turnRight(1)
  3750.         forward(1)
  3751.         turnLeft(1)
  3752.         saveToLog("craftMiningTurtle: placing crafty mining turtle", true)
  3753.         turtle.select(slot:getItemSlot("crafty mining turtle"))
  3754.         turtle.place() --next to disk drive
  3755.         slot.update{self = slot, item = "crafty mining turtle", delete = true}
  3756.         sortInventory(true)
  3757.         turnLeft(1)
  3758.         if numTurtles == 2 then
  3759.             craft{craftItem = "crafty mining turtle", craftQuantity = 1, sourceItem1 = "crafting table", sourceItem2 = "diamond pickaxe", sourceItem3 = "turtle", destSlot = 0}
  3760.             forward(1)
  3761.             turnRight(1)
  3762.             up(1)
  3763.             saveToLog("craftMiningTurtle: placing second crafty mining turtle", true)
  3764.             turtle.select(slot:getItemSlot("crafty mining turtle"))
  3765.             turtle.place() --on top of disk drive
  3766.             down(1, 1)
  3767.             saveToLog("Two Crafty Mining Turtles placed. All objectives acheived!", true)
  3768.         else
  3769.             forward(1)
  3770.             turnRight(1)
  3771.             saveToLog("Crafty Mining Turtle placed. Insufficient diamonds found to create two", true)
  3772.         end
  3773.         up(1)
  3774.         if slot:getItemCount("signs") > 0 then 
  3775.             back(1)
  3776.             turtle.select(slot:getItemSlot("signs"))
  3777.             turtle.placeDown("Mission\nComplete!")
  3778.             forward(1)
  3779.         end
  3780.         if numTurtles == 2 then
  3781.             saveToLog("Mission successful. I have replicated myself twice", true)
  3782.             saveToLog("Right-Click on my offspring to check their status...", true)
  3783.         else
  3784.             saveToLog("Mission partially successful. I have replicated myself once", true)
  3785.             saveToLog("Right-Click on my offspring to check its status...", true)
  3786.         end
  3787.     else
  3788.         saveToLog("Insufficient diamonds found in this area. Please break turtle , reposition and restart", true)
  3789.     end
  3790. end
  3791.  
  3792. function craftSigns()
  3793.     local woodAvailable = 0
  3794.     local wood2Available = 0
  3795.     local woodType = "wood"
  3796.     local planksType = "planks"
  3797.     local success = false
  3798.     local numSignsOnboard = 0
  3799.     local numWoodOnboard = 0
  3800.     local numWoodStored = 0
  3801.     local numSignsStored = 0
  3802.     local overStore = true
  3803.    
  3804.     --always make 3 signs only , need 8 planks, leaves 3 sticks
  3805.     --only attempt when over storageTorches
  3806.    
  3807.     saveToLog("craftSigns: started, crafting 3 signs", true)
  3808.    
  3809.     -- will always be over storageTorches unless not yet placed
  3810.     if not placeStorage:getStoragePlaced("torches") then
  3811.         overStore = false
  3812.     end
  3813.    
  3814.     numSignsStored = storageSigns:getItemCount("signs")
  3815.     numWoodStored = storageWood:getItemCount("wood")
  3816.     if slot:getItemSlot("signs") > 0 then
  3817.         numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
  3818.     end
  3819.     if slot:getItemSlot("wood") > 0 then
  3820.         numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  3821.         if slot:getItemSlot("wood2") > 0 then
  3822.             if turtle.getItemCount(slot:getItemSlot("wood2")) > numWoodOnboard then
  3823.                 numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood2"))
  3824.                 woodType = "wood2"
  3825.                 planksType = "planks2"
  3826.             end
  3827.         end
  3828.     end
  3829.     if overStore then
  3830.         if numSignsStored > 0 then
  3831.             forward(2, 1)
  3832.             saveToLog("craftSigns: "..numSignsStored.." signs removed from storage", true)
  3833.             getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
  3834.             numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
  3835.             back(2)
  3836.         end
  3837.         if slot:getItemSlot("wood") == 0 then --wood in storage, needed for signs
  3838.             forward(6, 1)
  3839.             saveToLog("craftTorches: "..numWoodStored.." wood removed from storage", true)
  3840.             getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  3841.             numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  3842.             turnRight(2)
  3843.             forward(6, 1)
  3844.             turnRight(2)
  3845.         end
  3846.     end
  3847.  
  3848.     if numWoodOnboard >= 3 then --enough to make signs
  3849.         saveToLog("craftSigns: crafting planks from "..woodType, false)
  3850.         craft{craftItem = planksType, craftQuantity = 8, sourceItem1 = woodType, destSlot = 0, doSort = true}
  3851.         saveToLog("craftSigns: crafting sticks from "..planksType, false)
  3852.         craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = planksType, destSlot = 0, doSort = true}
  3853.         saveToLog("craftSigns: crafting signs from "..planksType.." and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
  3854.         if craft{craftItem = "signs", craftQuantity = 3, sourceItem1 = "sticks", sourceItem2  = planksType, destSlot = 0, doSort = true} then
  3855.             success = true
  3856.         end
  3857.     else
  3858.         saveToLog("craftSigns: insufficient wood available", true)
  3859.     end
  3860.    
  3861.     return success
  3862. end
  3863.  
  3864. function craftSticks(quantity)
  3865.     local success = false
  3866.     local makePlanks = false
  3867.     local woodType = "wood"
  3868.     local planksType = "planks"
  3869.     local woodNeeded = 0
  3870.     local planksNeeded = 0
  3871.     local success = false
  3872.     local numWoodOnboard = 0
  3873.     local numPlanksOnboard = 0
  3874.    
  3875.    
  3876.     if quantity <= 4 then
  3877.         quantity = 4
  3878.         planksNeeded = 4   
  3879.         woodNeeded = 1
  3880.     elseif quantity <= 8 then
  3881.         quantity = 8
  3882.         planksNeeded = 4
  3883.         woodNeeded = 1
  3884.     elseif quantity <= 12 then
  3885.         quantity = 12
  3886.         planksNeeded = 8
  3887.         woodNeeded = 2
  3888.     else
  3889.         quantity = 16
  3890.         planksNeeded = 8
  3891.         woodNeeded = 2
  3892.     end
  3893.    
  3894.     if slot:getItemSlot("planks") > 0 then
  3895.         numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks"))
  3896.     end
  3897.     if slot:getItemSlot("planks2") > 0 then
  3898.         numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks2"))
  3899.         planksType = "planks2"
  3900.     end
  3901.     if numPlanksOnboard < planksNeeded then
  3902.         makePlanks = true
  3903.         saveToLog("craftSticks: started, crafting planks", true)
  3904.         --woodNeeded already calculated
  3905.     else
  3906.         woodNeeded = 0 --reset
  3907.         saveToLog("craftSticks: started, using "..planksType.." already onboard", true)
  3908.     end
  3909.    
  3910.     if makePlanks then --need wood
  3911.         if slot:getItemSlot("wood") > 0 then
  3912.             numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  3913.             if slot:getItemSlot("wood2") > 0 then
  3914.                 if turtle.getItemCount(slot:getItemSlot("wood2")) > numWoodOnboard then
  3915.                     numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood2"))
  3916.                     woodType = "wood2"
  3917.                     planksType = "planks2"
  3918.                 end
  3919.             end
  3920.         end
  3921.         if slot:getItemSlot("wood") == 0 and storageWood:getItemCount("wood") > 0 then --wood in storage
  3922.             if makePlanks then --wood needed for planks so go get
  3923.                 forward(6, 1)
  3924.                 saveToLog("craftSticks: "..storageWood:getItemCount("wood").." wood removed from storage", true)
  3925.                 getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  3926.                 numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  3927.                 turnRight(2)
  3928.                 forward(6, 1)
  3929.                 turnRight(2)
  3930.             end
  3931.         end
  3932.         if numWoodOnboard >= woodNeeded then
  3933.             --planksType will be "planks" unless planks2 are in stock
  3934.             saveToLog("craftSticks: crafting "..planksType.." for sticks", true)
  3935.             craft{craftItem = planksType, craftQuantity = planksNeeded, sourceItem1 = woodType, destSlot = 0, doSort = true}
  3936.         end
  3937.     end
  3938.     saveToLog("craftSticks: crafting "..quantity.." sticks from "..planksType, true)               
  3939.     if craft{craftItem = "sticks", craftQuantity = quantity, sourceItem1 = planksType, destSlot = 0, doSort = true} then
  3940.         objectives["craft sticks"] = true
  3941.         success = true
  3942.     end
  3943.    
  3944.     return success
  3945. end
  3946.  
  3947. function craftTorches(quantity)
  3948.     local headType = "charcoal"
  3949.     local headQuantity = 0
  3950.     local makeCharcoal = false
  3951.     local makePlanks = false
  3952.     local makeSticks =false
  3953.     local woodType = "wood"
  3954.     local planksType = "planks"
  3955.     local numWoodNeeded = 0
  3956.     local planksNeeded = 0
  3957.     local sticksNeeded = 0
  3958.     local success = false
  3959.     local numSticksOnboard = 0
  3960.     local numTorchesOnboard = 0
  3961.     local numWoodOnboard = 0
  3962.     local numTorchesStored = 0
  3963.     local overStore = true
  3964.     local doContinue = true
  3965.    
  3966.     -- will always be over storageTorches unless not yet placed
  3967.     --[[if not placeStorage:getStoragePlaced("torches") then
  3968.         overStore = false
  3969.     end]]--
  3970.    
  3971.     if slot:getItemSlot("torches") > 0 then
  3972.         numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
  3973.     end
  3974.     if slot:getItemSlot("sticks") > 0 then
  3975.         numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
  3976.     end
  3977.     if slot:getItemSlot("wood") > 0 then
  3978.         numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  3979.         if slot:getItemSlot("wood2") > 0 then
  3980.             if turtle.getItemCount(slot:getItemSlot("wood2")) > numWoodOnboard then
  3981.                 numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood2"))
  3982.                 woodType = "wood2"
  3983.                 planksType = "planks2"
  3984.             end
  3985.         end
  3986.     end
  3987.     -- 4 torches min : 1 head + 1 stick = 4 torches. Min sticks  = 4, min planks = 4
  3988.     -- torches head planks sticks
  3989.     -- 4        1       4       4
  3990.     -- 8        2       4       4
  3991.     -- 12       3       4       4
  3992.     -- 16       4       4       4
  3993.     -- 20       5       4       8
  3994.     -- 24       6       4       8
  3995.     -- 28       7       4       8
  3996.     -- 32       8       4       8
  3997.     -- 36       9       8       12
  3998.     -- 40       10      8       12
  3999.     -- 44       11      8       12
  4000.     -- 48       12      8       12
  4001.     -- 52       13      8       16
  4002.     -- 56       14      8       16
  4003.     -- 60       15      8       16
  4004.     -- 64       16      8       16
  4005.     quantity = math.floor(quantity / 4) * 4
  4006.     if quantity == 0 then
  4007.         quantity = 4 -- torches
  4008.         headQuantity = 1 -- coal /charcoal
  4009.         planksNeeded = 4 -- 1 wood
  4010.         sticksNeeded = 4 -- 2 planks
  4011.         numWoodNeeded = 1
  4012.     elseif quantity <= 16 then-- 4, 8, 12, 16
  4013.         headQuantity = quantity / 4 -- coal, charcoal
  4014.         planksNeeded = 4  -- 8 planks = 16 sticks
  4015.         sticksNeeded = 4  -- 4 sticks
  4016.         numWoodNeededwoodNeeded = 1
  4017.     elseif quantity <= 32 then-- 4, 8, 12, 16
  4018.         headQuantity = quantity / 4 -- coal, charcoal
  4019.         planksNeeded = 4  -- 8 planks = 16 sticks
  4020.         sticksNeeded = 8  -- 8 sticks
  4021.         numWoodNeeded = 2
  4022.     elseif quantity <= 48 then-- 4, 8, 12, 16
  4023.         headQuantity = quantity / 4 -- coal, charcoal
  4024.         planksNeeded = 8  -- 8 planks = 16 sticks
  4025.         sticksNeeded = 12 -- 12 sticks
  4026.         numWoodNeeded = 2
  4027.     else
  4028.         headQuantity = quantity / 4 -- coal, charcoal
  4029.         planksNeeded = 8  -- 8 planks = 16 sticks
  4030.         sticksNeeded = 16 -- 16 sticks
  4031.         numWoodNeeded = 2
  4032.     end
  4033.     saveToLog("craftTorches: quantity = "..quantity, true)
  4034.     --need either coal or charcoal
  4035.     if slot:getItemSlot("coal") > 0 then
  4036.         if turtle.getItemCount(slot:getItemSlot("coal")) - 1 >= headQuantity  then
  4037.             headType = "coal"
  4038.             saveToLog("craftTorches: using coal", true)
  4039.         end
  4040.     end
  4041.     if headType == "charcoal" then --default value
  4042.         if slot:getItemSlot("charcoal") > 0 then
  4043.             if turtle.getItemCount(slot:getItemSlot("charcoal")) < headQuantity then
  4044.                 makeCharcoal = true
  4045.                 saveToLog("craftTorches: using charcoal", true)
  4046.             end
  4047.         else
  4048.             saveToLog("craftTorches: charcoal on crafting list", true)
  4049.             makeCharcoal = true
  4050.         end
  4051.     end
  4052.     if storageTorches:getItemCount("torches") > 0 then
  4053.         saveToLog("craftTorches: "..storageTorches:getItemCount("torches").." torches removed from storage", true)
  4054.         getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
  4055.         numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
  4056.     end
  4057.     if storageSticks:getItemCount("sticks") > 0 then
  4058.         forward(4, 1)
  4059.         saveToLog("craftTorches: "..storageSticks:getItemCount("sticks").." sticks removed from storage", true)
  4060.         getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
  4061.         numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
  4062.         turnRight(2)
  4063.         forward(4, 1)
  4064.         turnRight(2)
  4065.     end
  4066.     if slot:getItemSlot("wood") == 0 and makeCharcoal then --wood in storage, needed for charcoal
  4067.         forward(6, 1)
  4068.         saveToLog("craftTorches: "..storageWood:getItemCount("wood").." wood removed from storage", true)
  4069.         getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  4070.         numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
  4071.         turnRight(2)
  4072.         forward(6, 1)
  4073.         turnRight(2)
  4074.     end
  4075.  
  4076.     if numSticksOnboard == 0 or numSticksOnboard < headQuantity then
  4077.         makeSticks = true
  4078.         saveToLog("craftTorches: insufficient sticks in stock, on crafting list", true)
  4079.     else
  4080.         saveToLog("craftTorches: enough sticks in stock", true)
  4081.     end
  4082.    
  4083.     if makeSticks then
  4084.         woodType = getWoodAvailable{woodNeeded = 1}
  4085.         if woodType ~= "none" then
  4086.             saveToLog("craftTorches: crafting sticks")
  4087.             doContinue = false
  4088.             if craftSticks(sticksNeeded) then
  4089.                 doContinue = true
  4090.             end
  4091.         end
  4092.     end
  4093.     woodType = getWoodAvailable{woodNeeded = 1}
  4094.     if woodType == "none" then
  4095.         doContinue = false
  4096.         saveToLog("craftTorches: insufficient wood available", false, true)
  4097.     end
  4098.     if doContinue then
  4099.         if makeCharcoal then
  4100.             saveToLog("craftTorches: smelting charcoal")
  4101.             if smelt("charcoal", headQuantity) then
  4102.                 saveToLog("craftTorches: charcoal smelted", true)
  4103.             else
  4104.                 saveToLog("craftTorches: charcoal smelting failed", true)
  4105.                 doContinue = false
  4106.             end
  4107.         end
  4108.         if doContinue then
  4109.             saveToLog("craftTorches: crafting torches", true)
  4110.             --make quantity torches
  4111.             if craft{craftItem = "torches", craftQuantity = quantity, sourceItem1 = "sticks", sourceItem2 = headType, destSlot = 0, doSort = true} then
  4112.                 saveToLog("craftTorches: "..quantity.." torches made", true)
  4113.                 success = true
  4114.                 if placeStorage:getStoragePlaced("torches") then
  4115.                     if not placeStorage:getMarkerPlaced("torches") then
  4116.                         if slot:getItemSlot("torches") > 0 then
  4117.                             if turtle.getItemCount(slot:getItemSlot("torches")) > 0 then
  4118.                                 forward(1, 1)
  4119.                                 dig.digNew{self = dig, direction = "down", callFrom = "craftTorches"}
  4120.                                 turtle.select(slot:getItemSlot("torches"))
  4121.                                 turtle.placeDown()
  4122.                                 placeStorage:setMarkerPlaced("torches")
  4123.                                 back(1, 1)
  4124.                             end
  4125.                         end
  4126.                     end
  4127.                 end
  4128.             else
  4129.                 saveToLog("craftTorches: error: no torches made", true)
  4130.                 error()
  4131.             end
  4132.         end
  4133.     end
  4134.    
  4135.     return success
  4136. end
  4137.  
  4138. function createBedrockMine()
  4139.     local tempYcoord = location:getY()
  4140.     local torchesAt = {}
  4141.     local sTime = os.time()
  4142.    
  4143.     saveToLog("createBedrockMine: starting..", true)
  4144.     restoreTorches("bothsides", 1)
  4145.     --move to entrance
  4146.     forward(16, 1)
  4147.     --rewrite sign
  4148.     turtle.select(16)
  4149.     if turtle.dig() then
  4150.         turtle.place("Diamond Mine\nMining Bedrock\nSetting Up\n time: "..textutils.formatTime(os.time(), true))
  4151.     end
  4152.     turnRight(2)
  4153.     while location:getY() > 14 do
  4154.         down(1, 1)
  4155.     end
  4156.     down(7, 1) --now at level 7
  4157.     for i = 1, 33 do
  4158.         torchesAt[i] = false
  4159.     end
  4160.     -- now under N side of previous mining area dig 33 x 2 corridor
  4161.     torchesAt[1] = true
  4162.     torchesAt[9] = true
  4163.     torchesAt[17] = true
  4164.     torchesAt[25] = true
  4165.     torchesAt[33] = true
  4166.     for i = 1, 33 do
  4167.         mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
  4168.         if i < 33 then
  4169.             forward(1, 1)
  4170.         end
  4171.     end
  4172.     turnRight(2)
  4173.     forward(16, 1)
  4174.     --should be at centre now
  4175.     turnRight(1)
  4176.     for i = 1, 17 do
  4177.         mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
  4178.         if i < 17 then
  4179.             forward(1, 1)
  4180.         end
  4181.     end
  4182.     turnRight(2)
  4183.     forward(16, 1)
  4184.     for i = 1, 17 do
  4185.         mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
  4186.         if i < 17 then
  4187.             forward(1, 1)
  4188.         end
  4189.     end
  4190.     turnRight(2)
  4191.     forward(16, 1)
  4192.     turnLeft(1)
  4193.     up(7,1)
  4194.     forward(16, 1)
  4195.     --should be at start now
  4196.     turnRight(2)
  4197.     while location:getY() < tempYcoord do
  4198.         up(1, 1)
  4199.     end --at surface now
  4200.     forward(16, 1)
  4201.     turnRight(2)
  4202.     saveToLog("createBedrockMine: started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), true)
  4203. end
  4204.  
  4205. function createChestObject()   
  4206.     -- chest object, used to keep track of stored items
  4207.     clsChest = {} -- the table representing the class, which will double as the metatable for any instances
  4208.     clsChest.__index = clsChest -- failed table lookups on the instances should fallback to the class table, to get methods
  4209.  
  4210.     function clsChest.new(storeNo, setStoreName) --equivalent to VB class initialise
  4211.         local self = setmetatable({}, clsChest)
  4212.         self.items = {"wood", "wood2","dirt","cobble","stone","saplings","saplings2","seeds","sand","gravel","clay",
  4213.                 "apples","coal","charcoal","ironore","?ironore","goldore","?goldore","iron","?iron","gold","?gold",
  4214.                 "redstone","redstone block","?diamonds","diamonds","emeralds","sugar cane","planks","planks2","chests","sticks",
  4215.                 "torches","furnaces","signs","item1","item2","item3","item4","item5","item6","item7","item8",
  4216.                 "fences","nuggets","buckets","paper","computer","crafting table","diamond pickaxe","glass","glass panes","floppy disk",
  4217.                 "disk drive","turtle","crafty mining turtle","?moss stone","moss stone","walls","obsidian","sandstone",
  4218.                 "?lapis","lapis","lapis block","flowers","roses","red mushrooms","brown mushrooms"}
  4219.         self.value = storeNo
  4220.         self.slotContains = {}
  4221.         self.slotCount = {}
  4222.         self.itemSlot = {}
  4223.         self.turtleSlot = {}
  4224.         self.index = 1
  4225.         self.name = setStoreName
  4226.         -- initialise variables
  4227.         for i = 1, 16 do
  4228.             self.slotContains[i] = ""
  4229.             self.slotCount[i] = 0
  4230.             self.turtleSlot[i] = 0
  4231.         end
  4232.        
  4233.         for key, value in ipairs(self.items) do
  4234.             self.itemSlot[value] = 0
  4235.         end
  4236.  
  4237.         return self
  4238.     end
  4239.    
  4240.     function clsChest.resetVariables(self)
  4241.         for i = 1, 16 do
  4242.             self.slotContains[i] = ""
  4243.             self.slotCount[i] = 0
  4244.             self.turtleSlot[i] = 0
  4245.         end
  4246.        
  4247.         for key, value in ipairs(self.items) do
  4248.             self.itemSlot[value] = 0
  4249.         end
  4250.         self.index = 1
  4251.     end
  4252.     function clsChest.getValue(self) --property get in VB
  4253.         return self.value
  4254.     end
  4255.     --craftingChest.getIndex(craftingChest)
  4256.     function clsChest.getIndex(self)
  4257.         return self.index
  4258.     end
  4259.    
  4260.     function clsChest.setSlotCount(self, slotNo, newVal) --property let in VB
  4261.         self.slotCount[slotNo] = newVal
  4262.     end
  4263.    
  4264.     function clsChest.getStoreName(self)
  4265.         return self.name
  4266.     end
  4267.    
  4268.     function clsChest.getSlotCount(self, slotNo)
  4269.         return self.slotCount[slotNo]
  4270.     end
  4271.    
  4272.     function clsChest.getSlotContains(self, slotNo)
  4273.         return self.slotContains[slotNo]
  4274.     end
  4275.    
  4276.     function clsChest.getStoreContains(self)
  4277.         -- return contents of first slot
  4278.         return self.slotContains[1]
  4279.     end
  4280.    
  4281.     function clsChest.getItemSlot(self, item)
  4282.         local result = 0
  4283.         if item ~= nil and item ~= "" then
  4284.             result = self.itemSlot[item]
  4285.         end
  4286.         return result
  4287.     end
  4288.    
  4289.     function clsChest.getTurtleSlot(self, slotNo )
  4290.         return self.turtleSlot[slotNo]
  4291.     end
  4292.    
  4293.     function clsChest.getItemCount(self, item)
  4294.         local result = 0
  4295.         if item ~= nil and item ~= "" then
  4296.             if self.itemSlot[item] > 0 then
  4297.                 result = self.slotCount[self.itemSlot[item]]
  4298.             end
  4299.         end
  4300.         return result
  4301.     end
  4302.    
  4303.     function clsChest.getNoOfItems(self)       
  4304.         return self.index - 1
  4305.     end
  4306.    
  4307.     function clsChest.isItemInChest(self, item)
  4308.         local success = false
  4309.         if item ~= nil then
  4310.             if self.itemSlot[item] > 0 then
  4311.                 success = true
  4312.             end
  4313.         end
  4314.         return success
  4315.     end
  4316.  
  4317.     function clsChest.addItem(self, item, quantity, turtleSlot)
  4318.         --storageSticks:addItem("sticks", 2, 14)
  4319.         -- addItem(arg.toStore, arg.item, arg.quantity)
  4320.         local slotNo = 0
  4321.         local newItemFound = false
  4322.         local newIndex = 1
  4323.        
  4324.         if turtleSlot == nil then
  4325.             turtleSlot = 0
  4326.         end
  4327.         if item == nil or item == "" then
  4328.             item = "item8"
  4329.             saveToLog(" clsChest.addItem Error, unknown item found", true)
  4330.         end
  4331.         if quantity == nil then
  4332.             quantity = 0
  4333.             saveToLog(" clsChest.addItem Error, nil quantity passed", true)
  4334.         end
  4335.         --unknown item only dropped in full, not added to
  4336.  
  4337.         slotNo = self.itemSlot[item] -- 0 by default, unless items already in chest
  4338.        
  4339.         if slotNo == 0 then --none of this item in chest
  4340.             self.itemSlot[item] = self.index --default value 1
  4341.             slotNo = self.itemSlot[item]
  4342.             self.slotContains[self.index] = item
  4343.             self.slotCount[self.index] = quantity
  4344.             self.turtleSlot[self.index] = turtleSlot
  4345.             self.index = self.index + 1 --ready for next item
  4346.         else --already in chest, update quantity
  4347.             turtleSlot = self.turtleSlot[self.itemSlot[item]]
  4348.             self.slotCount[slotNo] = self.slotCount[slotNo] + quantity
  4349.             if self.slotCount[slotNo] > 64 then
  4350.                 self.slotCount[slotNo + 1] = self.slotCount[slotNo] - 64
  4351.                 self.slotContains[slotNo + 1] = item
  4352.                 self.turtleSlot[slotNo + 1] = turtleSlot
  4353.                 self.slotCount[slotNo] = 64
  4354.             end
  4355.         end
  4356.         saveToLog("addItem: "..self.name.." added "..quantity.." of "..item.." from turtle slot "..turtleSlot, false)
  4357.     end
  4358.     --create 5 chest objects, 1 used for storage while crafting, others in the ground
  4359.     craftingChest = clsChest.new(0, "craftingChest")   
  4360.     storageTorches = clsChest.new(1, "storageTorches") -- placed under furnace
  4361.     storageSigns = clsChest.new(2, "storageSigns") -- placed Relative North of furnace, on path to tree farm, block 2 - placeStorage()
  4362.     storageSticks = clsChest.new(3, "storageSticks") -- placed Relative North of furnace, on path to tree farm, block 4 - placeStorage()
  4363.     storageWood = clsChest.new(4, "storageWood") -- placed Relative North of furnace, on path to tree farm, block 6 - placeStorage()
  4364.     storageIronore = clsChest.new(5, "storageIronore")
  4365.     storageSand = clsChest.new(6, "storageSand")
  4366.     storageRedstone = clsChest.new(7, "storageRedstone")
  4367.     storagePickaxes = clsChest.new(8, "storagePickaxes")
  4368.     storageSaplings = clsChest.new(9, "storageSaplings")
  4369.     extraStorage1 = clsChest.new(10, "extraStorage1")
  4370.     extraStorage2 = clsChest.new(11, "extraStorage2")
  4371.     extraStorage3 = clsChest.new(12, "extraStorage3")
  4372.     extraStorage4 = clsChest.new(13, "extraStorage4")
  4373.     extraStorage5 = clsChest.new(14, "extraStorage5")
  4374. end
  4375.  
  4376. function createCoordObject()
  4377.     --[[
  4378.     0 = go south (z increases)
  4379.     1 = go west  (x decreases)
  4380.     2 = go north (z decreases
  4381.     3 = go east  (x increases)
  4382.  
  4383.     compass[0] = "south"
  4384.     compass[1] = "west"
  4385.     compass[2] = "north"
  4386.     compass[3] = "east"]]--
  4387.    
  4388.     clsCoord = {} -- the table representing the class, which will double as the metatable for any instances
  4389.     clsCoord.__index = clsCoord -- failed table lookups on the instances should fallback to the class table, to get methods
  4390.  
  4391.     function clsCoord.new(coordName) --equivalent to VB class initialise
  4392.         local self = setmetatable({}, clsCoord)
  4393.         self.name = coordName
  4394.         self.x = 0
  4395.         self.y = 0
  4396.         self.z = 0
  4397.         self.facing = 0
  4398.         self.compass = "south"
  4399.        
  4400.         return self
  4401.     end
  4402.     function clsCoord.getX(self)
  4403.         return self.x
  4404.     end
  4405.     function clsCoord.setX(self, newVal) -- property let in VB
  4406.         self.x = newVal
  4407.     end
  4408.     function clsCoord.getY(self) -- property get in VB
  4409.         return self.y
  4410.     end
  4411.     function clsCoord.setY(self, newVal)
  4412.         self.y = newVal
  4413.     end
  4414.     function clsCoord.getZ(self)
  4415.         return self.z
  4416.     end
  4417.     function clsCoord.setZ(self, newVal)
  4418.         self.z = newVal
  4419.     end
  4420.     function clsCoord.getFacing(self)
  4421.         return self.facing
  4422.     end
  4423.     function clsCoord.setFacing(self, newVal) --property let in VB
  4424.         self.facing = newVal
  4425.         if self.facing < 0 then
  4426.                 self.facing = 3
  4427.         elseif self.facing > 3 then
  4428.                 self.facing = 0
  4429.         end
  4430.         if self.facing == 0 then
  4431.             self.compass = "south"
  4432.         elseif self.facing == 1 then
  4433.             self.compass = "west"
  4434.         elseif self.facing == 2 then
  4435.             self.compass = "north"
  4436.         else
  4437.             self.compass = "east"
  4438.         end
  4439.     end
  4440.     function clsCoord.getCompass(self)
  4441.         return self.compass
  4442.     end
  4443.     function clsCoord.setCompass(self, newVal) --property let in VB
  4444.         self.compass = newVal
  4445.  
  4446.         if self.compass == "south" then
  4447.             self.facing = 0
  4448.         elseif self.compass == "west" then
  4449.             self.facing = 1
  4450.         elseif self.compass == "north" then
  4451.             self.facing = 2
  4452.         elseif self.compass == "east" then
  4453.             self.facing = 3
  4454.         end
  4455.     end
  4456.     function clsCoord.goUp(blocks)
  4457.         blocks = blocks or 1
  4458.         self.y = self.y + blocks
  4459.     end
  4460.     function clsCoord.goDown(blocks)
  4461.         blocks = blocks or 1
  4462.         self.y = self.y - blocks
  4463.     end
  4464.     --[[uses:
  4465.         location:getX()             get current turtle x coordinate
  4466.         location:getY()             get current turtle y coordinate
  4467.         location:getZ()             get current turtle z coordinate
  4468.         location:setX(xCoord)       set current turtle x coordinate eg location:setX(-235)
  4469.         location:setY(yCoord)       set current turtle y coordinate eg location:setY(66)
  4470.         location:setZ(zCoord)       set current turtle z coordinate eg location:setZ(125)
  4471.         location:getFacing()        returns a number 0 - 3 representing direction of player
  4472.         location:setFacing(facing)  sets direction eg location:setFacing(1) (West)
  4473.         location:getCompass()       returns direction as text eg "north"
  4474.         location:setCompass("X")    sets direction using text eg location:setCompass("south")
  4475.         location:goUp(X)            increases Y coord by X
  4476.         location:goDown(X)          decreases Y coord by X
  4477.     ]]--
  4478.     location = clsCoord.new("currentLocation")
  4479.     coordHome = clsCoord.new("homeLocation")
  4480.     mineEntrance = clsCoord.new("mineEntrance")
  4481. end
  4482.  
  4483. function createDigObject()
  4484.     -- dig{suck = true} use suck instead of dig
  4485.     -- success, item, slotNo = dig.digNew{self = dig, direction = "forward", slotNo = 1, expectedItem = "wood", waitForGravel = true, checkForItems = "saplings", callFrom = "firstTree"}
  4486.     -- dig item in specified direction
  4487.     -- if item not known allocate "item1-8"
  4488.     -- if checkForItems ~= "" then check eg for saplings, coal etc
  4489.     -- if item known, leave in selected slot
  4490.     -- if max quantity reached, dump excess
  4491.     -- return success, dugItem, dugSlot eg true, "wood", 1
  4492.     clsDig = {}
  4493.     clsDig.__index = clsDig
  4494.  
  4495.     function clsDig.create(self) --creates new instance of dig object
  4496.         local self = setmetatable({}, clsDig)
  4497.        
  4498.         -- setup variables
  4499.         self.callFrom = ""
  4500.         self.checkForItems = ""
  4501.         self.cobblePlaced = false
  4502.         self.detect = false
  4503.         self.direction = ""
  4504.         self.doSort = false
  4505.         self.dugAmount = 0
  4506.         self.dugItem = ""
  4507.         self.dugSlot = 0
  4508.         self.dugSlot2 = 0
  4509.         self.emptySlot = 0
  4510.         self.expectedItem = ""
  4511.         self.flintSlot = 0
  4512.         self.isGravel = false
  4513.         self.itemKnown = false
  4514.         self.itemCount = {}
  4515.         self.newItemFound = false
  4516.         self.oldItem = ""
  4517.         self.slotNo = 1
  4518.         self.success = false
  4519.         self.suck = false
  4520.         self.updateType =""
  4521.         self.useItem = ""
  4522.         self.useSlot = 0
  4523.         self.waitForGravel = false
  4524.        
  4525.         self.itemCount = {}
  4526.         -- set up array of current quantities in each turtle slot
  4527.         for i = 1, 16 do
  4528.             self.itemCount[i] = turtle.getItemCount(i)
  4529.         end
  4530.        
  4531.         return self    
  4532.     end
  4533.    
  4534.     function clsDig.digNew(arg)
  4535.         -- reset variables
  4536.         local self = arg.self
  4537.        
  4538.         self.callFrom = ""
  4539.         self.checkForItems = ""
  4540.         self.cobblePlaced = false
  4541.         self.detect = false
  4542.         self.direction = "forward"
  4543.         self.doSort = false
  4544.         self.dugAmount = 0
  4545.         self.dugItem = ""
  4546.         self.dugSlot = 0
  4547.         self.dugSlot2 = 0
  4548.         self.emptySlot = 0
  4549.         self.expectedItem = ""
  4550.         self.flintSlot = 0
  4551.         self.isGravel = false
  4552.         self.itemCount = {}
  4553.         self.itemKnown = false
  4554.         self.newItemFound = false
  4555.         self.oldItem = ""
  4556.         self.slotNo = 1
  4557.         self.success = false
  4558.         self.suck = false
  4559.         self.updateType = ""
  4560.         self.useItem = ""
  4561.         self.useSlot = 0
  4562.         self.waitForGravel = false
  4563.        
  4564.         local logText = "dig.newDig: "
  4565.         local result = false
  4566.         --[[examples
  4567.             dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
  4568.             dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
  4569.             dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "firstTree"}
  4570.         ]]--
  4571.        
  4572.         -- set up array of current quantities in each turtle slot
  4573.         for i = 1, 16 do
  4574.             self.itemCount[i] = turtle.getItemCount(i)
  4575.         end
  4576.        
  4577.         self.direction = arg.direction or "forward"
  4578.         self.slotNo = arg.slotNo or 1
  4579.         self.expectedItem = arg.expectedItem or ""
  4580.         self.checkForItems = arg.checkForItems or ""
  4581.         self.callFrom = arg.callFrom or ""
  4582.         self.waitForGravel = arg.waitForGravel or false
  4583.         self.suck = arg.suck or false
  4584.         if self.suck then --turtle is sucking
  4585.             clsDig.suck(self) -- will set self.success
  4586.         else -- turtle is digging, not sucking, so check for mobs
  4587.             --remove any mobs
  4588.             clsDig.attack(self)
  4589.             --mobs disposed of, now get on with digging
  4590.             clsDig.doDig(self) -- will set self.success
  4591.         end
  4592.         --dig/suck successful if suck large quantity then 2 slots will be updated, original + next available
  4593.         if self.success then --process dig/suck
  4594.             saveToLog("dig.newDig: direction = "..self.direction..
  4595.                         " slotNo = "..self.slotNo..
  4596.                         " expectedItem = "..self.expectedItem..
  4597.                         " checkForItems = "..self.checkForItems..
  4598.                         " callFrom = "..self.callFrom, false, true)
  4599.                        
  4600.             clsDig.setDugSlotAndItem(self) --sets self.dugSlot, self.dugSlot2, self.dugItem and self.dugAmount. itemX unless already identified
  4601.             if self.dugSlot > 0 then --item dug, could be into existing slot, empty slot, or specified slot
  4602.                 if clsDig.confirmGravel(self) == "flint" then -- item identified by confirmGravel only
  4603.                     turtle.select(self.dugSlot)
  4604.                     while turtle.drop() do
  4605.                         saveToLog("dig.digNew: flint in slot "..self.dugSlot.. " emptied out", false, true)
  4606.                     end
  4607.                     self.dugItem = ""
  4608.                     self.dugSlot = 0
  4609.                 end
  4610.                 if self.dugSlot > 0 then -- not flint. self.dugItem = itemX, or known item. expectedItem already set
  4611.                     -- self.updateType set in setDugSlotAndItem() and/or in checkForItem()
  4612.                     if string.find(self.dugItem, "item") ~= nil and self.checkForItems ~= "" then --check for item(s) when only 1 block available
  4613.                         clsDig.checkForItem(self) -- itemX changed to known item after checkForItem
  4614.                     end
  4615.                     -- item will be itemX or known item by now, slot.update needed
  4616.                     if self.updateType == "newItem" then
  4617.                         saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
  4618.                         slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  4619.                     elseif self.updateType == "updateItem" then
  4620.                         saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
  4621.                         slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  4622.                     elseif self.updateType == "changeItem" then
  4623.                         saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." old item = "..self.oldItem.." tested - dugItem = "..self.dugItem, false, true)
  4624.                         slot.update{self = slot, slotNo = self.dugSlot, newItem = self.dugItem}
  4625.                     elseif self.updateType == "deleteItem" then
  4626.                         saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." delete item = "..self.dugItem, false, true)
  4627.                         slot.update{self = slot, slotNo = self.dugSlot, delete = true}
  4628.                     end
  4629.                     if clsDig.checkSlotQuantity(self) then
  4630.                         slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  4631.                     end
  4632.                     if self.waitForGravel then
  4633.                         clsDig.waitForGravel(self)
  4634.                     end
  4635.                 end
  4636.                 if self.dugSlot > 0 then
  4637.                     result = true
  4638.                 end
  4639.             end
  4640.         else
  4641.             if self.detect then --dig not successful, if detected must be bedrock
  4642.                 saveToLog("dig.newDig: did not succeed after detect(), bedrock reached", true)
  4643.                 self.dugItem = "bedrock"
  4644.                 self.dugSlot = 0
  4645.             else
  4646.                 --saveToLog("dig.newDig: no block detected, nothing dug", false, true)
  4647.             end
  4648.             result = false
  4649.         end
  4650.         if self.doSort then
  4651.             saveToLog("dig.newDig: sorting inventory after identifying new item", true)
  4652.             sortInventory(true)
  4653.         end
  4654.        
  4655.         --if not fso:getUseVerbose() then --not using verbose logfile, so print summary only
  4656.             if self.dugItem ~= "" then
  4657.                 saveToLog("dig = "..self.dugAmount.." of "..self.dugItem.." into slot "..self.dugSlot.." at x="..location:getX()..",y="..location:getY()..",z="..location:getZ(), true, false)
  4658.             end
  4659.         --end
  4660.         return result, self.dugItem, self.dugSlot
  4661.     end
  4662.    
  4663.     function clsDig.attack(self) --attack any nearby mobs
  4664.         local mobAttacked = false
  4665.         --first get rid of any mobs
  4666.         --saveToLog("dig.attack: attack started", false, true)
  4667.         turtle.select(1)
  4668.         if turtle.attack() then
  4669.             saveToLog("dig.attack: Mob attacked in front!", true, false)
  4670.             sleep(1.5)
  4671.             while turtle.attack() do --in case mob in front
  4672.                 saveToLog("dig.attack: Mob attacked in front again!", true, false)
  4673.                 sleep(1.5)
  4674.             end
  4675.             mobAttacked = true
  4676.         end
  4677.         if turtle.attackUp() then
  4678.             saveToLog("dig.attack: Mob attacked above!", true, false)
  4679.             sleep(1.5)
  4680.             while turtle.attackUp() do --in case mob in front
  4681.                 saveToLog("dig.attack: Mob attacked above again!", true, false)
  4682.                 sleep(1.5)
  4683.             end
  4684.             mobAttacked = true
  4685.         end
  4686.         if turtle.attackDown() then
  4687.             saveToLog("dig.attack: Mob attacked below!", true, false)
  4688.             sleep(1.5)
  4689.             while turtle.attackDown() do --in case mob in front
  4690.                 saveToLog("dig.attack: Mob attacked below again!", true, false)
  4691.                 sleep(1.5)
  4692.             end
  4693.             mobAttacked = true
  4694.         end
  4695.         if mobAttacked then --remove any mob drops
  4696.             for i = 1, 16 do
  4697.                 if self.itemCount[i] == 0 then --check only previously empty slots
  4698.                     if turtle.getItemCount(i) > 0 then
  4699.                         turtle.select(i)
  4700.                         while turtle.dropUp() do
  4701.                             saveToLog ("dig.attack: dumping mob drops", true, false)
  4702.                         end
  4703.                     end
  4704.                 end
  4705.             end
  4706.         end
  4707.     end
  4708.    
  4709.     function clsDig.suck(self) -- carry out suck operation
  4710.         self.success = false
  4711.         turtle.select(self.slotNo)
  4712.        
  4713.         if self.direction == "up" then
  4714.             if turtle.suckUp() then
  4715.                 self.success = true
  4716.             end
  4717.         elseif self.direction == "down" then
  4718.             if turtle.suckDown() then
  4719.                 self.success = true
  4720.             end
  4721.         else
  4722.             if turtle.suck() then
  4723.                 self.success = true
  4724.             end
  4725.         end
  4726.         if self.success then
  4727.             saveToLog("dig.suck: suck items into slot "..self.slotNo, false, true)
  4728.         end
  4729.        
  4730.         return self.success
  4731.     end
  4732.    
  4733.     function clsDig.doDig(self) -- check if next block is gravel, then carry out dig operation
  4734.         local lookForGravel = false
  4735.         local testDig = false
  4736.        
  4737.         self.success = false
  4738.         --[[examples
  4739.             dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
  4740.             dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
  4741.         ]]--
  4742.         --[[
  4743.         saveToLog("dig.doDig: started, direction = "..self.direction..
  4744.                  " expectedItem = "..self.expectedItem..
  4745.                  " slotNo = "..self.slotNo, false, true)]]--
  4746.                  
  4747.         turtle.select(self.slotNo)
  4748.         -- grass:   turtle.detect() = false,    turtle.dig() = true, nothing returned, occasional seeds
  4749.         -- leaves:  turtle.detect() = true,     turtle.dig() = true, nothing returned
  4750.         -- water:   turtle.detect() = false,    turtle.dig() = false, nothing returned cc >= 1.6
  4751.         -- lava:    turtle.detect() = false,    turtle.dig() = false, nothing returned cc >= 1.6
  4752.    
  4753.         --Compare next block with gravel (if already found)
  4754.         if slot:getItemSlot("gravel") > 0 then
  4755.             turtle.select(slot:getItemSlot("gravel"))
  4756.             lookForGravel = true
  4757.         end
  4758.         if self.direction == "up" then
  4759.             if turtle.detectUp() then
  4760.                 self.detect = true
  4761.                 if turtle.compareUp() and lookForGravel then --compare gravel with block above
  4762.                     self.isGravel = true
  4763.                 end
  4764.             end
  4765.             turtle.select(self.slotNo)
  4766.             if turtle.digUp() then
  4767.                 self.success = true
  4768.             end
  4769.         elseif self.direction == "down" then
  4770.             if turtle.detectDown() then
  4771.                 self.detect = true
  4772.                 if turtle.compareDown() and lookForGravel then
  4773.                     self.isGravel = true
  4774.                 end
  4775.             end
  4776.             turtle.select(self.slotNo)
  4777.             if turtle.digDown() then
  4778.                 self.success = true
  4779.             end
  4780.         else
  4781.             if turtle.detect() then
  4782.                 self.detect = true
  4783.                 if turtle.compare() and lookForGravel then
  4784.                     self.isGravel = true
  4785.                 end
  4786.             end
  4787.             turtle.select(self.slotNo)
  4788.             if turtle.dig() then
  4789.                 self.success = true
  4790.             end
  4791.         end
  4792.         if not self.detect then
  4793.             --saveToLog("dig.doDig: no block detected "..self.direction, false, true)
  4794.         end
  4795.         if self.isGravel then
  4796.             saveToLog("dig.doDig: gravel found on current dig, will give flint or gravel", false, true)
  4797.         end
  4798.        
  4799.         return self.success     --turtle.digX() succeeded but may not have increased items onboard.
  4800.                         --if isGravel then new block is either gravel or flint. success = false: bedrock
  4801.     end
  4802.    
  4803.     function clsDig.setDugSlotAndItem(self) -- set self.dugSlot (0 = no dig) self.dugItem ("" = no dig)
  4804.         local success = false
  4805.        
  4806.         self.dugSlot = 0
  4807.         self.dugItem = ""
  4808.         self.dugAmount = 0
  4809.         self.dugSlot2 = 0
  4810.         self.updateType = ""
  4811.        
  4812.         for i = 1, 16 do --find slot item dug into (if any) by comparing array set up in digNew()
  4813.             if turtle.getItemCount(i) > self.itemCount[i] then --check if dugItem in slot(s)
  4814.                 if self.dugSlot == 0 then -- default value
  4815.                     self.dugSlot = i --first slot with additional items in it
  4816.                     self.dugAmount = turtle.getItemCount(i) - self.itemCount[i] -- amount dug
  4817.                 else
  4818.                     self.dugSlot2 = i --another slot with additional items
  4819.                 end
  4820.             end
  4821.         end
  4822.         --dugSlot set above
  4823.         if self.dugSlot > 0 then--check if additional items
  4824.             success = true
  4825.             if self.suck then -- suck, not dig
  4826.                 -- set dugSlot2 to slot number with added item in it
  4827.                 if self.dugSlot2 > 0 then --eg suck items has filled original slot, then moved into next available, or overfilled slot
  4828.                     saveToLog("dig.setDugSlotAndItem: second slot used "..self.dugSlot2, false, true)
  4829.                     turtle.select(self.dugSlot2)
  4830.                     for i = 1, 16 do
  4831.                         if i ~= self.dugSlot2 then
  4832.                             if turtle.compareTo(i) then --compare if excess material
  4833.                                 while turtle.dropDown() do
  4834.                                     saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " emptied out", true)
  4835.                                 end
  4836.                                 if turtle.getItemCount(i) >= 54 then
  4837.                                     saveToLog ("dig.setDugSlotAndItem: excess items dumped from "..i, true)
  4838.                                     turtle.select(i)
  4839.                                     turtle.dropDown(turtle.getItemCount(i) - 54)
  4840.                                 end
  4841.                                 self.dugSlot2 = 0
  4842.                                 break
  4843.                             end
  4844.                         end
  4845.                     end
  4846.                 end
  4847.                 if slot:getSlotContains(self.dugSlot) == "" then
  4848.                     turtle.select(self.dugSlot)
  4849.                     if turtle.refuel(0) then
  4850.                         if slot:getItemSlot("saplings") == 0 then
  4851.                             self.dugItem = "saplings"
  4852.                             self.updateType = "newItem"
  4853.                         else
  4854.                             if slot:getItemSlot("saplings2") == 0 then
  4855.                                 self.dugItem = "saplings2"
  4856.                                 self.updateType = "newItem"
  4857.                             else
  4858.                                 turtle.refuel()
  4859.                                 self.dugSlot = 0
  4860.                                 self.dugItem = ""
  4861.                                 success = false
  4862.                             end
  4863.                         end
  4864.                     else
  4865.                         while turtle.dropDown() do
  4866.                             saveToLog("dig.setDugSlotAndItem: suck unknown item into slot "..self.dugSlot.. " emptied out", true)
  4867.                         end
  4868.                         self.dugSlot = 0
  4869.                         success = false
  4870.                     end
  4871.                 else --item dug already identified or given itemX
  4872.                     self.dugItem = slot:getSlotContains(self.dugSlot)
  4873.                     self.itemKnown = true
  4874.                     self.updateType = "updateItem"
  4875.                 end
  4876.             else -- dig, not suck
  4877.                 if self.slotNo > 1 or self.expectedItem ~= "" then --dug into specific slot, so could match existing item, or placed and re-dug
  4878.                     -- eg dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
  4879.                     -- check if out of place
  4880.                     turtle.select(self.dugSlot)
  4881.                     for i = 1, 16 do
  4882.                         if i ~= self.dugSlot then
  4883.                             if turtle.compareTo(i) then --matches existing item
  4884.                                 self.dugItem = slot:getSlotContains(i)
  4885.                                 turtle.transferTo(i)
  4886.                                 self.dugSlot = i
  4887.                                 self.itemKnown = true
  4888.                                 self.updateType = "updateItem"
  4889.                                 break
  4890.                             end
  4891.                         end
  4892.                     end
  4893.                     if self.updateType ~= "updateItem" then -- not out of place
  4894.                         if self.expectedItem ~= "" then --allocate dugItem to expectedItem
  4895.                             self.dugItem = self.expectedItem
  4896.                             saveToLog("dig.checkExpectedItem: expected item '"..self.expectedItem.."' dug into slot "..self.dugSlot, false, true)
  4897.                             self.itemKnown = true
  4898.                             self.updateType = "updateItem"
  4899.                         else -- ? placed and re-dug
  4900.                             if slot:getSlotContains(self.slotNo) == "" then
  4901.                                 self.itemKnown = false
  4902.                                 self.updateType = "newItem"
  4903.                                 self.dugItem = slot:getFreeUnknownItem()
  4904.                             else
  4905.                                 self.itemKnown = true
  4906.                                 self.updateType = "updateItem"
  4907.                             end
  4908.                         end
  4909.                     end
  4910.                 else -- set to dig into slot 1 and self.expectedItem = ""
  4911.                     if self.dugAmount > 2 then
  4912.                         --can only be melon, clay, lapis, redstone as more than 1 mined from one block
  4913.                         if location:getY() > 35 then --must be clay or melon
  4914.                             turtle.select(self.dugSlot)
  4915.                             while turtle.dropUp() do
  4916.                                 saveToLog("dig.setDugSlotAndItem:: clay or melon dug: dumping..", false, true)
  4917.                             end
  4918.                             self.dugItem = ""
  4919.                             self.dugSlot = 0
  4920.                             success = false
  4921.                         else
  4922.                             if slot:getSlotContains(self.dugSlot) == "" then
  4923.                                 if slot:getItemSlot("redstone") > 0 then --redstone already found
  4924.                                     self.dugItem = "lapis"
  4925.                                     success = true
  4926.                                     self.itemKnown = true
  4927.                                     self.updateType = "newItem"
  4928.                                     saveToLog("dig.setDugSlotAndItem: lapis identified as redstone already known", false, true)
  4929.                                 else --no redstone found so far
  4930.                                     self.dugItem = slot:getFreeUnknownItem()
  4931.                                     self.itemKnown = false
  4932.                                     self.updateType = "newItem"
  4933.                                 end
  4934.                             else
  4935.                                 self.dugItem = slot:getSlotContains(self.dugSlot)
  4936.                                 newItemFound = false
  4937.                                 self.updateType = "updateItem"
  4938.                                 self.itemKnown = true
  4939.                             end
  4940.                         end
  4941.                     else -- dugAmount = 1
  4942.                         if self.dugSlot2 > 0 then -- dug items has filled original slot, then moved into next available, or redstone, lapis, coal overfilled slot
  4943.                             saveToLog("dig.setDugSlotAndItem: second slot used "..self.dugSlot2, false, true)
  4944.                             turtle.select(self.dugSlot2)
  4945.                             for i = 1, 16 do
  4946.                                 if i ~= self.dugSlot2 then
  4947.                                     if turtle.compareTo(i) then --compare if excess material
  4948.                                         while turtle.drop() do
  4949.                                             saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " emptied out", true)
  4950.                                         end
  4951.                                         if turtle.getItemCount(i) >= 54 then
  4952.                                             saveToLog ("dig.setDugSlotAndItem: excess items dumped from "..i, true)
  4953.                                             turtle.select(i)
  4954.                                             turtle.drop(turtle.getItemCount(i) - 54)
  4955.                                         end
  4956.                                         self.dugSlot2 = 0
  4957.                                         break
  4958.                                     end
  4959.                                 end
  4960.                             end
  4961.                         end
  4962.                         if slot:getSlotContains(self.dugSlot) == "" then
  4963.                             self.dugItem = slot:getFreeUnknownItem()
  4964.                             --saveToLog("dig.setDugSlotAndItem: new dugItem = "..self.dugItem, false)
  4965.                             self.itemKnown = false
  4966.                             self.updateType = "newItem"
  4967.                         else
  4968.                             self.dugItem = slot:getSlotContains(self.dugSlot)
  4969.                             --saveToLog("dig.setDugSlotAndItem: existing dugItem = "..self.dugItem, false)
  4970.                             newItemFound = false
  4971.                             self.updateType = "updateItem"
  4972.                             if string.find(self.dugItem, "item") ~= nil then -- itemX found
  4973.                                 self.itemKnown = false
  4974.                             else
  4975.                                 self.itemKnown = true
  4976.                             end
  4977.                         end
  4978.                         success = true -- item dug
  4979.                     end
  4980.                 end
  4981.             end
  4982.         else
  4983.             self.dugItem = "leaves"
  4984.         end
  4985.  
  4986.         saveToLog ("dig.setDugSlotAndItem: dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
  4987.        
  4988.         return success
  4989.         -- self.dugSlot, self.dugSlot2, self.dugAmount and self.dugItem have been set in this routine
  4990.         -- if self.dugSlot = 0, self.dugItem will only be set if = "water" or "leaves"
  4991.         -- self.updateType will be newItem or updateItem
  4992.     end
  4993.            
  4994.     function clsDig.confirmGravel(self) -- self.isGravel = true, either flint or gravel dug
  4995.         local item = ""
  4996.        
  4997.         if self.isGravel then --block just dug confirmed as gravel by comparison
  4998.             if self.dugSlot ~= slot:getItemSlot("gravel") then --must be flint
  4999.                 saveToLog("dig.confirmGravel: flint dug when gravel detected", false, true)
  5000.                 self.dugItem = "flint"
  5001.                 item = "flint"
  5002.             else
  5003.                 self.dugItem = "gravel"
  5004.                 self.itemKnown = true
  5005.                 item = "gravel"
  5006.             end
  5007.         end
  5008.        
  5009.         return item
  5010.     end
  5011.        
  5012.     function clsDig.checkSandGravel(self, useSlot, useItem)
  5013.         local result = ""
  5014.         local runTest = true
  5015.         local flintSlot = 0
  5016.         local dirtPlaced = false
  5017.         local numTries = 0
  5018.         local startLevel = location:getY()
  5019.         local itemCount = {}
  5020.        
  5021.         if turtle.detectDown() then --probably in mine going down. block found below
  5022.             turtle.select(1)
  5023.             for i = 1, 2 do --move up 2 places
  5024.                 while turtle.detectUp() do
  5025.                     turtle.digUp()
  5026.                     sleep(0.5)
  5027.                 end
  5028.                 up(1)
  5029.             end
  5030.         else --no block below. probably going up. plug with dirt
  5031.             saveToLog("clsDig.checkSandGravel: checking for gravel placing dirt plug down", false)
  5032.             turtle.select(slot:getItemSlot("dirt"))
  5033.             turtle.placeDown()
  5034.             dirtPlaced = true
  5035.             turtle.select(1)
  5036.             for i = 1, 2 do  --move up 2 places
  5037.                 while turtle.detectUp() do
  5038.                     saveToLog("clsDig.checkSandGravel: digging block above", false)
  5039.                     turtle.digUp()
  5040.                     sleep(0.5)
  5041.                 end
  5042.                 up(1)
  5043.             end
  5044.         end --now clear below 2 blocks, could have added more flint/gravel to turtle while clearing above
  5045.         turtle.select(useSlot)
  5046.         if turtle.placeDown() then --solid block or sand/ gravel
  5047.             sleep(0.5)
  5048.             if turtle.detectDown() then --not dropped into pit so not sand/gravel
  5049.                 turtle.digDown()
  5050.                 --down(2, 1)
  5051.                 if slot:getItemSlot("cobble") == 0 then
  5052.                     saveToLog("clsDig.checkSandGravel: first cobble found", false)
  5053.                     result = "cobble"
  5054.                 else
  5055.                     result = useItem
  5056.                 end
  5057.             else
  5058.                 saveToLog("clsDig.checkSandGravel: gravel or sand dropped, moving down", false)
  5059.                 while not turtle.detectDown() do
  5060.                     down(1)
  5061.                 end
  5062.                 if slot:getItemSlot("gravel") > 0 then --gravel found already
  5063.                     saveToLog("checkSandGravel: checking for gravel/sand - found sand in slot "..useSlot, false)
  5064.                     result = "sand"
  5065.                     runTest = false
  5066.                     turtle.digDown()
  5067.                 elseif slot:getItemSlot("sand") > 0 then --sand found already
  5068.                     saveToLog("checkSandGravel: checking for gravel/sand - found gravel in slot "..useSlot, false)
  5069.                     result = "gravel"
  5070.                 end
  5071.                 if runTest then -- only test to confirm gravel
  5072.                     if slot:getItemSlot("sand") > 0 then --sand found already
  5073.                         saveToLog("clsDig.checkSandGravel: sand already identified. Gravel dropped, digging up to 100x to check for flint", false)
  5074.                     else
  5075.                         saveToLog("clsDig.checkSandGravel: sand / gravel dropped, digging up to 100x to check for flint", false)
  5076.                     end
  5077.                     for i = 1, 16 do
  5078.                         itemCount[i] = turtle.getItemCount(i)
  5079.                     end
  5080.                     for i = 1, 100 do
  5081.                         turtle.select(1) --if gravel or flint dug, goes into correct slot
  5082.                         if turtle.digDown() then
  5083.                             numTries = numTries + 1
  5084.                             if turtle.getItemCount(useSlot) == itemCount[useSlot] then --flint found as ? gravel slot did not increase
  5085.                                 for k = 1, 16 do
  5086.                                     if turtle.getItemCount(k) > itemCount[k] then --check if dugItem in slot(s)
  5087.                                         flintSlot = k -- could be flint in an empty slot or add to existing item
  5088.                                         break
  5089.                                     end
  5090.                                 end
  5091.                                 saveToLog("clsDig.checkSandGravel: useSlot not used flint found in slot "..flintSlot, false)
  5092.                                 --delete flint after updating gravel slot, else itemX order will change
  5093.                                 if flintSlot > 0 then
  5094.                                     result = "gravel"
  5095.                                     break
  5096.                                 end
  5097.                             else
  5098.                                 turtle.select(useSlot)
  5099.                                 turtle.placeDown()
  5100.                             end
  5101.                         end
  5102.                     end
  5103.                    
  5104.                     if flintSlot == 0 then --sand found
  5105.                         saveToLog("clsDig.checkSandGravel: checking for gravel - sand found in slot "..useSlot, false)
  5106.                         result = "sand"
  5107.                     else
  5108.                         saveToLog("clsDig.checkSandGravel: gravel check took "..numTries.." attempts", false)
  5109.                     end
  5110.                 end
  5111.                 down(1, 1)
  5112.                 if flintSlot > 0 then --flint found, so dump it
  5113.                     turtle.select(flintSlot)
  5114.                     while turtle.dropUp() do
  5115.                         saveToLog("clsDig.checkSandGravel: dumping flint found in slot "..flintSlot, false)
  5116.                     end
  5117.                     slot.update{self = slot, slotNo = flintSlot, delete = true}
  5118.                 end
  5119.             end
  5120.         else --did not placeDown() = coal, flint
  5121.             turtle.select(useSlot)
  5122.             if turtle.refuel(0) then --coal found
  5123.                 saveToLog("clsDig.checkSandGravel: coal found in slot "..useSlot, false)
  5124.                 result = "coal"
  5125.             end
  5126.             down(2, 1)
  5127.         end
  5128.         if dirtPlaced then
  5129.             turtle.select(1)
  5130.             turtle.digDown()
  5131.         end
  5132.         while location:getY() < startLevel do
  5133.             up(1)
  5134.         end
  5135.        
  5136.         return result
  5137.     end
  5138.  
  5139.     function clsDig.checkForItem(self)
  5140.         local useItem = ""
  5141.         local slotCount = 0
  5142.         local itemCompare = false
  5143.         local useSlot = 0
  5144.         local itemStatus = ""
  5145.         local result = false
  5146.         local flintSlot = 0
  5147.  
  5148.         saveToLog("dig.checkForItem: checkForItems = "..self.checkForItems.." call from = "..self.callFrom, false)
  5149.         -- will only get here if item dug but not identified self.dugItem = "itemX"
  5150.         -- see if any instructions to test for new items, when first identified as itemX
  5151.         if self.dugAmount > 2 then --multiple items from dig
  5152.             --can only be melon, clay, lapis, redstone as more than 1 mined from one block
  5153.             if location:getY() > 35 then --must be clay or melon
  5154.                 turtle.select(self.dugSlot)
  5155.                 while turtle.dropUp() do
  5156.                     saveToLog("dig.checkForItem: clay or melon dug: dumping..", false, true)
  5157.                 end
  5158.                 self.dugItem = ""
  5159.                 self.dugSlot = 0
  5160.                 itemStatus = "deleteItem"
  5161.             else --below level 35 must be lapis or redstone
  5162.                 if slot:getItemSlot("redstone") > 0 then
  5163.                     self.dugItem = "lapis"
  5164.                     itemStatus = "newItem"
  5165.                     saveToLog("dig.checkForItem: lapis identified as redstone already known", false, true)
  5166.                 else
  5167.                     if turtle.detectDown() then
  5168.                         back(1)
  5169.                         turtle.select(self.dugSlot)
  5170.                         if turtle.place() then --redstone
  5171.                             if not turtle.compare() then
  5172.                                 saveToLog("dig.checkForItem: dugAmount = "..self.dugAmount.." item in slot "..self.dugSlot.." placed but not compared - redstone", false)
  5173.                                 self.dugItem = "redstone"
  5174.                                 turtle.dig()
  5175.                                 itemStatus = "newItem"
  5176.                             end
  5177.                         else --lapis (dug > 1, no place)
  5178.                             self.dugItem = "lapis"
  5179.                             itemStatus = "newItem"
  5180.                         end
  5181.                     end
  5182.                 end
  5183.             end
  5184.         else -- single item from dig
  5185.             -- check for coal, flint, saplings, saplings2, wood2, flowers, mushrooms if only one item in slot
  5186.             if turtle.getItemCount(self.dugSlot) == 1 then --first time itemX dug
  5187.                 turtle.select(self.dugSlot)
  5188.                 if turtle.refuel(0) then --saplings, saplings2, wood2, coal
  5189.                     itemStatus = "newItem"
  5190.                     saveToLog("dig.checkForItem: new item dug - can refuel", false, true)
  5191.                     if string.find("checkBedrock,findCobble,goMining1,goMining2,goMining3,mineCorridor,mineForward", self.callFrom) ~= nil then
  5192.                         if slot:getItemSlot("coal") == 0 then
  5193.                             self.dugItem = "coal"
  5194.                             saveToLog("dig.checkForItem: coal found", false, true)
  5195.                         else -- if coal already known must be wood product found in a disused mine
  5196.                             saveToLog("dig.checkForItem: wooden item found underground - using for fuel", false, true)
  5197.                             turtle.refuel() -- use for fuel
  5198.                             self.dugItem = ""
  5199.                             self.dugSlot = 0
  5200.                             itemStatus = "deleteItem"
  5201.                         end
  5202.                     else
  5203.                         if slot:getItemSlot("wood2") == 0 then -- wood2 not known
  5204.                             if turtle.detectUp() then
  5205.                                 turtle.select(1)
  5206.                                 turtle.digUp()
  5207.                                 turtle.select(self.dugSlot)
  5208.                             end
  5209.                             if turtle.placeUp() then --wood2
  5210.                                 turtle.digUp()
  5211.                                 self.dugItem = "wood2"
  5212.                                 saveToLog("dig.checkForItem: wood2 found", false)
  5213.                             else
  5214.                                 if slot:getItemSlot("saplings") > 0 then
  5215.                                     self.dugItem = "saplings2"
  5216.                                     saveToLog("dig.checkForItem: saplings2 found", false)
  5217.                                 else
  5218.                                     self.dugItem = "saplings"
  5219.                                     saveToLog("dig.checkForItem: saplings found", false)
  5220.                                 end
  5221.                             end
  5222.                         else --wood2 known, must be sapling
  5223.                             if slot:getItemSlot("saplings") > 0 then
  5224.                                 self.dugItem = "saplings2"
  5225.                                 saveToLog("dig.checkForItem: saplings2 found", false)
  5226.                             else
  5227.                                 self.dugItem = "saplings"
  5228.                                 saveToLog("dig.checkForItem: saplings found", false)
  5229.                             end
  5230.                         end
  5231.                     end
  5232.                 else --did not refuel, check for flint, flowers, seeds, mushrooms, sugar cane, diamonds
  5233.                     saveToLog("dig.checkForItem: new item - cannot refuel", false, true)
  5234.                     useItem = self.dugItem
  5235.                     if self.callFrom == "reposition" then
  5236.                         if turtle.placeUp() then -- not dirt, cobble, could be ironore, sand, gravel
  5237.                             turtle.digUp() --put back into dugSlot. If gravel dug, could turn to flint, leave for time being
  5238.                         else
  5239.                             while turtle.dropUp() do
  5240.                                 saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
  5241.                             end
  5242.                             self.dugSlot = 0
  5243.                             self.dugItem = ""
  5244.                             itemStatus = "deleteItem"
  5245.                         end
  5246.                     elseif self.callFrom == "checkSugarCane" then
  5247.                         --will only be if digDown() called from checkSugarCane()
  5248.                         --has to be sugar cane, sand, gravel, flowers, mushrooms
  5249.                         saveToLog("dig.checkForItem: checking for sugar cane", false)
  5250.                         if not turtle.detectDown() then --block below to check
  5251.                             down(1)
  5252.                         end
  5253.                         turtle.select(self.dugSlot)
  5254.                         if not turtle.compareDown() then -- does not compare, could be sugar cane, or flower, grass below
  5255.                             saveToLog ("dig.checkForItem: dugItem does not compare with block below", false)
  5256.                             for i = 1, 16 do --reset item checker
  5257.                                 self.itemCount[i] = turtle.getItemCount(i)
  5258.                             end
  5259.                             turtle.select(1)
  5260.                             turtle.digDown() --if sugar cane will add to slot,
  5261.                             if turtle.getItemCount(self.dugSlot) > self.itemCount[self.dugSlot] then --confirms sugar cane, as did not compare,dirt and cobble known
  5262.                                 saveToLog ("dig.checkForItem: sugar cane confirmed", false)
  5263.                                 self.dugItem = "sugar cane"
  5264.                                 itemStatus = "newItem"
  5265.                                 down(1)
  5266.                             else --find out whether block just dug was existing item, or new
  5267.                                 self.dugSlot2 = 0
  5268.                                 for i = 1, 16 do
  5269.                                     if turtle.getItemCount(i) > self.itemCount[i] then
  5270.                                         self.dugSlot2 = i
  5271.                                         break
  5272.                                     end
  5273.                                 end
  5274.                                 if self.dugSlot2 > 0 then
  5275.                                     if slot:getSlotContains(self.dugSlot2) == "" then -- second new item dug
  5276.                                         saveToLog ("clsDig.checkForItem: checking for sugar cane, new item found", false)
  5277.                                     else --dirt/cobble/sand/gravel
  5278.                                         --saveToLog ("clsDig.checkForItem: checking for sugar cane, "..slot:getSlotContains(self.dugSlot2).." found and replaced", false)
  5279.                                         --turtle.select(self.dugSlot2)
  5280.                                         --turtle.placeDown()
  5281.                                         saveToLog ("clsDig.checkForItem: checking for sugar cane, flowers, seeds or mushrooms found and dropped", false)
  5282.                                         turtle.select(self.dugSlot)
  5283.                                         turtle.dropUp()
  5284.                                         self.dugItem = slot:getSlotContains(self.dugSlot2)
  5285.                                         self.dugSlot = self.dugSlot2
  5286.                                         itemStatus = "updateItem"
  5287.                                     end
  5288.                                 end
  5289.                             end
  5290.                         end
  5291.                     elseif string.find("checkBedrock,findCobble,goMining1,goMining2,goMining3,mineCorridor,mineForward", self.callFrom) ~= nil then --look for diamonds
  5292.                         saveToLog("dig.checkForItem: new item cannot be identified in slot "..self.dugSlot, false)
  5293.                         itemStatus = "updateItem"
  5294.                     else --called from any other function
  5295.                          --test if can be placed and re-dug. saplings already tested
  5296.                         turtle.select(1)
  5297.                         while turtle.detectUp() do
  5298.                             turtle.digUp()
  5299.                             sleep(0.5)
  5300.                         end
  5301.                         turtle.select(self.dugSlot)
  5302.                         if turtle.placeUp() then -- not dirt, cobble, could be ironore, sand, gravel
  5303.                             turtle.digUp() --put back into dugSlot. If gravel dug, could turn to flint, leave for time being
  5304.                         else
  5305.                             while turtle.dropUp() do
  5306.                                 saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
  5307.                             end
  5308.                             self.dugSlot = 0
  5309.                             self.dugItem = ""
  5310.                             itemStatus = "deleteItem"
  5311.                         end
  5312.                     end
  5313.                 end
  5314.             elseif turtle.getItemCount(self.dugSlot) == 2 then -- 2 in slot
  5315.                 saveToLog("dig.checkDugItem: checking slot with 2 items of "..self.dugItem, false, true)
  5316.                 if string.find(self.dugItem, "item") ~= nil then --only checks item1, item2 etc. coal, gravel etc if already found do not get here
  5317.                     if self.dugAmount > 2 and slot:getItemSlot("redstone") > 0 and slot:getItemSlot("coal") > 0 then
  5318.                         self.oldItem = self.dugItem
  5319.                         self.dugItem = "lapis"
  5320.                         itemStatus = "newItem"
  5321.                     else
  5322.                         --check for sand, gravel, needs min 2 blocks
  5323.                         turtle.select(self.dugSlot)
  5324.                         if self.callFrom == "findCobble" then --cobble not yet found, coal and flint tested, could be cobble, sand gravel
  5325.                             result = clsDig.checkSandGravel(self, self.dugSlot, self.dugItem)
  5326.                             self.oldItem = self.dugItem
  5327.                             self.dugItem = result
  5328.                             itemStatus = "newItem"
  5329.                         else
  5330.                             if not slot:getItemTested(self.dugSlot, "gravel") then -- check if new item is gravel, sand, coal or flint
  5331.                                 if string.find(self.checkForItems, "gravel") ~= nil or string.find(self.checkForItems, "sand") ~= nil then
  5332.                                     result = clsDig.checkSandGravel(self, self.dugSlot, self.dugItem)
  5333.                                     if result == "sand" or result == "gravel" then
  5334.                                         self.oldItem = self.dugItem
  5335.                                         self.dugItem = result
  5336.                                         itemStatus = "newItem"
  5337.                                     elseif result == "flint" then
  5338.                                         while turtle.dropUp() do
  5339.                                             saveToLog("dig.checkDugItem: dropping ? flint from slot "..self.dugSlot, false, true)
  5340.                                         end
  5341.                                         slot.update{self = slot, slotNo = self.dugSlot, delete = true}
  5342.                                         self.dugItem = ""
  5343.                                         self.dugSlot = 0
  5344.                                     elseif result == self.dugItem then --not gravel or sand
  5345.                                         slot:setItemTested(self.dugSlot, "gravel")
  5346.                                         slot:setItemTested(self.dugSlot, "sand")
  5347.                                         slot:setItemTested(self.dugSlot, "flint")
  5348.                                     else --could be ironore
  5349.                                         if slot:getItemSlot("ironore") == 0 and string.find("goMining1,goMining2,goMining3", self.callFrom) ~= nil then
  5350.                                             saveToLog("dig.checkDugItem: assuming ?ironore found",true)
  5351.                                             self.oldItem = self.dugItem
  5352.                                             self.dugItem = "?ironore"
  5353.                                             self.doSort = true
  5354.                                             itemStatus = "newItem"
  5355.                                         end
  5356.                                     end
  5357.                                 end
  5358.                             end
  5359.                         end
  5360.                     end
  5361.                 end
  5362.                 if self.dugItem ~= "" then --tested items may have been dumped. will now be itemX or named item
  5363.                     if self.oldItem == "" then --new item found
  5364.                         saveToLog("dig.checkDugItem: final confirmation of new item found", false, true)
  5365.                         --slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
  5366.                     else --itemX tested - new item confirmed
  5367.                         saveToLog("dig.checkDugItem: changing item in "..self.dugSlot.." ("..self.oldItem..") after testing: dugItem = "..self.dugItem, false, true)
  5368.                         itemStatus = "changeItem"
  5369.                         self.doSort = true
  5370.                     end
  5371.                 end
  5372.             end
  5373.         end
  5374.        
  5375.         return itemStatus
  5376.     end
  5377.    
  5378.     function clsDig.checkSlotQuantity(self)
  5379.         local success = false
  5380.         --Check quantity and dump excess
  5381.         --redstone drops 4-5, lapis 4-8
  5382.         if self.dugSlot > 0 then
  5383.             if string.find(self.dugItem, "item") ~= nil or self.dugItem == "redstone" or self.dugItem == "lapis" then --unknown, lapis, redstone
  5384.                 if turtle.getItemCount(self.dugSlot) >= 54 then
  5385.                     saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." dumped from "..self.dugSlot, false, true)
  5386.                     turtle.select(self.dugSlot)
  5387.                     turtle.dropDown(turtle.getItemCount(self.dugSlot) - 54)
  5388.                     success = true
  5389.                 end
  5390.             elseif self.dugItem == "wood" or  self.dugItem == "wood2" then
  5391.                 if turtle.getItemCount(self.dugSlot) >= 60 then
  5392.                     saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." used for fuel", false, true)
  5393.                     turtle.select(self.dugSlot)
  5394.                     turtle.refuel(1)
  5395.                     success = true
  5396.                 end
  5397.             else --all other known items
  5398.                 if turtle.getItemCount(self.dugSlot) >= 62 then
  5399.                     saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." dumped from "..self.dugSlot, false, true)
  5400.                     turtle.select(self.dugSlot)
  5401.                     turtle.dropDown(1)
  5402.                     success = true
  5403.                 end
  5404.                 if self.dugItem == "sand" and turtle.getItemCount(self.dugSlot) >= 7 then
  5405.                     objectives["complete sand"] = true
  5406.                     saveToLog("dig.checkSlotQuantity: objectives['complete sand'] = true", false, true)
  5407.                     if turtle.getItemCount(self.dugSlot) > 62 then
  5408.                         turtle.select(self.dugSlot)
  5409.                         turtle.dropDown(turtle.getItemCount(self.dugSlot) - 54)
  5410.                         success = true
  5411.                     end
  5412.                 end
  5413.                 if self.dugItem == "sugar cane" and turtle.getItemCount(self.dugSlot) >= 4 then
  5414.                     objectives["complete sugar cane"] = true
  5415.                     saveToLog("dig.checkSlotQuantity: objectives['complete sugar cane'] = true", false, true)
  5416.                 end
  5417.             end
  5418.         end
  5419.        
  5420.         return success
  5421.     end
  5422.    
  5423.     function clsDig.waitForGravel(self)
  5424.         local useItem = ""
  5425.         local useSlot = 0
  5426.        
  5427.         for i = 1, 16 do
  5428.             self.itemCount[i] = turtle.getItemCount(i)
  5429.         end
  5430.        
  5431.         turtle.select(1)
  5432.         --sleep(0.5)
  5433.         if self.direction == "up" then
  5434.             while turtle.detectUp() do
  5435.                 turtle.digUp()
  5436.                 sleep(0.5)
  5437.             end
  5438.         elseif self.direction == "forward" then
  5439.             while turtle.detect() do
  5440.                 turtle.dig()
  5441.                 sleep(0.5)
  5442.             end
  5443.         end
  5444.         --now check if gravel/sand has fallen
  5445.         for i = 1, 16 do
  5446.             if  turtle.getItemCount(i) > self.itemCount[i] then --check if dugItem in slot
  5447.                 useSlot = i
  5448.                 break
  5449.             end
  5450.         end
  5451.         if useSlot > 0 then --sand/gravel fallen
  5452.             useItem = slot:getSlotContains(useSlot) --could be "", "itemX", "sand", "gravel"
  5453.             if string.find(useItem, "item") ~= nil or useItem == "" then --unknown must be sand or gravel
  5454.                 if self.itemCount[useSlot] > 1 then -- can be tested for sand/gravel
  5455.                     useItem = clsDig.checkSandGravel(self, useSlot, useItem)
  5456.                     if useItem ~= "" then
  5457.                         saveToLog("dig.waitForGravel: "..useItem.." found in slot "..useSlot, false, true)
  5458.                     end
  5459.                 else -- store as "itemx"
  5460.                     useItem = slot:getFreeUnknownItem()
  5461.                     saveToLog ("dig.waitForGravel: sand/gravel found. given id = "..useItem.." in slot no = "..self.useSlot, false, true)
  5462.                     slot.update{self = slot, slotNo = self.useSlot, item = useItem}
  5463.                 end
  5464.             end
  5465.         end
  5466.     end
  5467.    
  5468.     function clsDig.getDugSlot(self)
  5469.         return self.dugSlot
  5470.     end
  5471.    
  5472.     function clsDig.getDugItem(self)
  5473.         return self.dugItem
  5474.     end
  5475.    
  5476.     function clsDig.getDugAmount(self)
  5477.         return self.dugAmount
  5478.     end
  5479.    
  5480.     function clsDig.getSuccess(self)
  5481.         return self.success
  5482.     end
  5483.     --create initial dig object
  5484.     dig = clsDig.create()
  5485.     -- use in future:
  5486.     -- dig.digNew{self = dig, slotNo = 1, direction = "down", checkForItems = "sand", callFrom = "checkSugarCane"}
  5487. end
  5488.  
  5489. function createFurnaceObject()
  5490.     clsFurnace = {} -- the table representing the class, which will double as the metatable for any instances
  5491.     clsFurnace.__index = clsFurnace -- failed table lookups on the instances should fallback to the class table, to get methods
  5492.  
  5493.     function clsFurnace.new(furnaceName) --equivalent to VB class initialise
  5494.         local self = setmetatable({}, clsFurnace)
  5495.         self.name = furnaceName
  5496.         self.smeltIn = ""
  5497.         self.smeltItemCount = 0
  5498.         self.smeltOut = ""
  5499.         self.smeltOutItemCount = 0
  5500.         self.fuelType = ""
  5501.         self.fuelCount = 0
  5502.         self.smeltSuccess = false
  5503.        
  5504.         return self
  5505.     end
  5506.    
  5507.     function clsFurnace.reset(self)
  5508.         self.smeltIn = ""           -- item placed into top slot e.g. "wood", "?ironore" (turtle on top)
  5509.         self.smeltInItemCount = 0   -- amount placed into top slot 
  5510.         self.smeltOut = ""          -- expected item produced if successful eg "charcoal", "iron" (turtle underneath)  
  5511.         self.smeltOutItemCount = 0  -- amount of smelted product
  5512.         self.fuelType = ""          -- fuel used in bottom slot e.g "planks" (turtle at any side)
  5513.         self.fuelCount = 0          -- amount of fuel placed
  5514.         self.smeltSuccess = false   -- set to true if smelt succeeded
  5515.     end
  5516.     function clsFurnace.getFurnaceName(self)
  5517.         return self.name
  5518.     end
  5519.    
  5520.     function clsFurnace.getSmeltInItem(self)
  5521.         return self.smeltIn
  5522.     end
  5523.     function clsFurnace.setSmeltInItem(self, newVal)
  5524.         self.smeltIn = newVal
  5525.     end
  5526.     function clsFurnace.getSmeltInItemCount(self)
  5527.         return self.smeltInItemCount
  5528.     end
  5529.     function clsFurnace.setSmeltInItemCount(self, newVal)
  5530.         self.smeltInItemCount = newVal
  5531.     end
  5532.    
  5533.     function clsFurnace.getSmeltOutItem(self)
  5534.         return self.smeltOut
  5535.     end
  5536.     function clsFurnace.setSmeltOutItem(self, newVal)
  5537.         self.smeltOut = newVal
  5538.     end
  5539.     function clsFurnace.getSmeltOutItemCount(self)
  5540.         return self.smeltOutItemCount
  5541.     end
  5542.     function clsFurnace.setSmeltOutItemCount(self, newVal)
  5543.         self.smeltOutItemCount = newVal
  5544.     end
  5545.  
  5546.     function clsFurnace.getFuelType(self)
  5547.         return self.fuelType
  5548.     end
  5549.     function clsFurnace.setFuelType(self, newVal)
  5550.         self.fuelType = newVal
  5551.     end
  5552.    
  5553.     function clsFurnace.getFuelCount(self)
  5554.         return self.fuelCount
  5555.     end
  5556.     function clsFurnace.setFuelCount(self, newVal)
  5557.         self.fuelCount = newVal
  5558.     end
  5559.    
  5560.     function clsFurnace.getSmeltSuccess(self)
  5561.         return self.smeltSuccess
  5562.     end
  5563.     function clsFurnace.setSmeltSuccess(self, newVal)
  5564.         self.smeltSuccess = newVal
  5565.     end
  5566.    
  5567.     furnace1 = clsFurnace.new("Main Furnace")
  5568. end
  5569.  
  5570. function createIsland()
  5571.     if getCobbleStock() >= 5 then
  5572.         down(1)
  5573.         turtle.select(slot:getItemSlot("cobble"))
  5574.         for i = 1, 4 do
  5575.             if not turtle.detect() then
  5576.                 turtle.place()
  5577.             end
  5578.             turnRight(1)
  5579.         end
  5580.         up(1)
  5581.         turtle.placeDown()
  5582.     elseif getDirtStock() >= 5 then
  5583.         down(1)
  5584.         turtle.select(slot:getItemSlot("dirt"))
  5585.         for i = 1, 4 do
  5586.             if not turtle.detect() then
  5587.                 turtle.place()
  5588.             end
  5589.             turnRight(1)
  5590.         end
  5591.         up(1)
  5592.         turtle.placeDown()
  5593.     end
  5594. end
  5595.  
  5596. function createLogfileObject()
  5597.     clsLogfile = {} -- the table representing the class, which will double as the metatable for any instances
  5598.     clsLogfile.__index = clsLogfile -- failed table lookups on the instances should fallback to the class table, to get methods
  5599.  
  5600.     --[[
  5601.     Store details in "start.ini" on the local computer:
  5602.     onServer=true/false
  5603.     usePastebin=true/false
  5604.     configModified=true/false
  5605.     useLogfiles=true/false
  5606.     ]]--
  5607.     function clsLogfile.new() --equivalent to VB class initialise
  5608.         local self = setmetatable({}, clsLogfile)
  5609.         local iniText = ""
  5610.         local iniFile = ""
  5611.        
  5612.         self.size = 0
  5613.         self.useLog = false
  5614.         self.numLogFiles = 17 -- change if more logfiles needed
  5615.         self.logFiles = {}
  5616.         self.logFileExists = {}
  5617.         self.boolIniFileExists = false
  5618.         self.useVerbose = false
  5619.         self.onServer = false
  5620.         self.usePastebin = false
  5621.         self.configModified = false
  5622.        
  5623.         self.logFiles[1] = "logStart.txt"
  5624.         self.logFiles[2] = "logHarvestRight1.txt"
  5625.         self.logFiles[3] = "logHarvestRight2.txt"
  5626.         self.logFiles[4] = "logHarvestRight3.txt"
  5627.         self.logFiles[5] = "logHarvestRight4.txt"
  5628.         self.logFiles[6] = "logHarvestRight5.txt"
  5629.         self.logFiles[7] = "logHarvestRight6.txt"
  5630.         self.logFiles[8] = "logHarvestLeft1.txt"
  5631.         self.logFiles[9] = "logHarvestLeft2.txt"
  5632.         self.logFiles[10] = "logHarvestLeft3.txt"
  5633.         self.logFiles[11] = "logHarvestLeft4.txt"
  5634.         self.logFiles[12] = "logCreateMine.txt"
  5635.         self.logFiles[13] = "logMiningLayer1.txt"
  5636.         self.logFiles[14] = "logMiningLayer2.txt"
  5637.         self.logFiles[15] = "logMiningLayer3.txt"
  5638.         self.logFiles[16] = "logMiningBedrock.txt"
  5639.         self.logFiles[17] = "logCraftMiningTurtle.txt"
  5640.        
  5641.         for i = 1, self.numLogFiles do
  5642.             self.logFileExists[i] = false
  5643.         end
  5644.        
  5645.         self.currentFileName = ""
  5646.         self.currentFileNameIndex = 0
  5647.         self.startFileNameIndex = 1
  5648.        
  5649.         for i = 1, self.numLogFiles do
  5650.             if fs.exists(self.logFiles[i]) then
  5651.                 self.logFileExists[i] = true
  5652.             end
  5653.         end
  5654.         if fs.exists("start.ini") and fs.getSize("start.ini") > 0 then
  5655.             self.boolIniFileExists = true
  5656.             iniFile = fs.open("start.ini", "r")
  5657.             iniText = iniFile.readLine()
  5658.             self.onServer = string.sub(iniText, string.find(iniText, "onServer=") + 9)
  5659.             iniText = iniFile.readLine()
  5660.             if string.sub(iniText, string.find(iniText, "usePastebin=") + 12) == "true" then
  5661.                 self.usePastebin = true
  5662.             end
  5663.             iniText = iniFile.readLine()
  5664.             if string.sub(iniText, string.find(iniText, "configModified=") + 15) == "true" then
  5665.                 self.configModified = true
  5666.             end
  5667.             iniText = iniFile.readLine()
  5668.             if string.sub(iniText, string.find(iniText, "useLog=") + 7) == "true" then
  5669.                 self.useLog = true
  5670.             end
  5671.             iniText = iniFile.readLine()
  5672.             if string.sub(iniText, string.find(iniText, "useVerbose=") + 11) == "true" then
  5673.                 self.useVerbose = true
  5674.             end
  5675.             iniFile.close()    
  5676.         end
  5677.         return self
  5678.     end
  5679.    
  5680.     function clsLogfile.setUseVerbose(self, use)
  5681.         self.useVerbose = use
  5682.     end
  5683.    
  5684.     function clsLogfile.getUseVerbose(self)
  5685.         return self.useVerbose
  5686.     end
  5687.    
  5688.     function clsLogfile.getUseLog(self)
  5689.         return self.useLog
  5690.     end
  5691.    
  5692.     function clsLogfile.setUseLog(self, use)
  5693.         self.useLog = use
  5694.         self.writeIniFile(self)
  5695.     end
  5696.    
  5697.     function clsLogfile.checkLogExists(self)
  5698.         local result = false
  5699.        
  5700.         for i = 1, self.numLogFiles do
  5701.             if self.logFileExists[i] then
  5702.                 result = true
  5703.             end
  5704.         end
  5705.        
  5706.         return result
  5707.     end
  5708.    
  5709.     function clsLogfile.getCurrentFileName(self)
  5710.         return self.currentFileName
  5711.     end
  5712.    
  5713.     function clsLogfile.getCurrentFileSize(self)       
  5714.         return fs.getSize(self.currentFileName)
  5715.     end
  5716.    
  5717.     function clsLogfile.getCurrentFileNameIndex(self)
  5718.         return self.currentFileNameIndex
  5719.     end
  5720.    
  5721.     --use if one logfile not used, eg harvestallTrees only on right side
  5722.     function clsLogfile.setCurrentFileNameIndex(self, indexNo)
  5723.         self.currentFileNameIndex = indexNo
  5724.     end
  5725.    
  5726.     function clsLogfile.getStartFileNameIndex(self)
  5727.         return self.startFileNameIndex
  5728.     end
  5729.    
  5730.     --use during debugging to set logfile to appropriate stage
  5731.     function clsLogfile.setStartFileNameIndex(self, indexNo)
  5732.         self.startFileNameIndex = indexNo
  5733.     end
  5734.    
  5735.     function clsLogfile.deleteLog(self)
  5736.         local result = false
  5737.        
  5738.         for i = 1, self.numLogFiles do
  5739.             if fs.exists(self.logFiles[i]) then
  5740.                 fs.delete(self.logFiles[i])
  5741.             end
  5742.             self.logFileExists[i] = false
  5743.             result = true
  5744.         end
  5745.        
  5746.         return result
  5747.     end
  5748.    
  5749.     function clsLogfile.renameLog(self)
  5750.         local result = false
  5751.        
  5752.         for i = 1, self.numLogFiles do
  5753.             if fs.exists(self.logFiles[i]) then
  5754.                 if fs.exists(self.logFiles[i]..".bak") then
  5755.                     fs.delete(self.logFiles[i]..".bak")
  5756.                 end
  5757.                 fs.move(self.logFiles[i], self.logFiles[i]..".bak")
  5758.                 --fs.rename(self.logFiles[i], self.logFiles[i]..".bak")
  5759.             end
  5760.             self.logFileExists[i] = false
  5761.             result = true
  5762.         end
  5763.        
  5764.         return result
  5765.     end
  5766.    
  5767.     function clsLogfile.getNextFileName(self)
  5768.         local tempIndex = self.numLogFiles
  5769.        
  5770.         if self.currentFileNameIndex < self.numLogFiles then
  5771.             tempIndex = self.currentFileNameIndex + 1
  5772.         end
  5773.        
  5774.         return self.logFiles[tempIndex]
  5775.     end
  5776.    
  5777.     function clsLogfile.useFileName(self, fileName)
  5778.         local result = false
  5779.        
  5780.         for i = 1, self.numLogFiles do
  5781.             if self.logFiles[i] == fileName then
  5782.                 result = true
  5783.                 self.currentFileName = self.logFiles[i]
  5784.                 self.currentFileNameIndex = i
  5785.                 if not fs.exists(self.currentFileName) then
  5786.                     logFile = fs.open(self.currentFileName, "w")
  5787.                     logFile.writeLine("LogFile "..self.currentFileName.." started at "..textutils.formatTime(os.time(), true))
  5788.                     logFile.close()
  5789.                 end
  5790.                 break
  5791.             end
  5792.         end
  5793.        
  5794.         return result
  5795.     end
  5796.    
  5797.     function clsLogfile.useNextFileName(self)
  5798.         local result = false
  5799.         local logFile = ""
  5800.        
  5801.         self.currentFileNameIndex = self.currentFileNameIndex + 1
  5802.         if self.currentFileNameIndex > self.numLogFiles then
  5803.             self.currentFileNameIndex = self.numLogFiles
  5804.             result = true
  5805.         end
  5806.         self.currentFileName = self.logFiles[self.currentFileNameIndex]
  5807.         if not fs.exists(self.currentFileName) then
  5808.             logFile = fs.open(self.currentFileName, "w")
  5809.             logFile.writeLine("LogFile "..self.currentFileName.." started at "..textutils.formatTime(os.time(), true))
  5810.             logFile.close()
  5811.         end
  5812.         return result
  5813.     end
  5814.    
  5815.     function clsLogfile.appendLine(self, newText, verbose)
  5816.         local logFile = ""
  5817.         local doWrite = false
  5818.        
  5819.         -- use verbose flag to write line ONLY if useVerbose method has been chosen
  5820.         if verbose then
  5821.             if self.useVerbose then
  5822.                 doWrite = true
  5823.             end
  5824.         else
  5825.             doWrite = true
  5826.         end
  5827.        
  5828.         if doWrite then
  5829.             if self.currentFileName == "" then --no logfile written yet
  5830.                 self.currentFileNameIndex = self.startFileNameIndex --default 1 unless set first for debugging
  5831.                 self.currentFileName = self.logFiles[self.startFileNameIndex] -- set filename
  5832.             end
  5833.             if not fs.exists(self.currentFileName) then --logfile not created
  5834.                 logFile = fs.open(self.currentFileName, "w") --create file
  5835.                 logFile.writeLine(newText)
  5836.                 logFile.close()
  5837.             else
  5838.                 logFile = fs.open(self.currentFileName, "a")
  5839.                 logFile.writeLine(newText)
  5840.                 logFile.close()
  5841.             end
  5842.         end
  5843.     end
  5844.    
  5845.     function clsLogfile.iniFileExists(self)
  5846.         return self.boolIniFileExists
  5847.     end
  5848.    
  5849.     function clsLogfile.getOnServer(self)
  5850.         return self.onServer
  5851.     end
  5852.    
  5853.     function clsLogfile.setOnServer(self, setting)
  5854.         self.onServer = setting
  5855.         self.writeIniFile(self)
  5856.     end
  5857.    
  5858.     function clsLogfile.getUsePastebin(self)
  5859.         return self.usePastebin
  5860.     end
  5861.    
  5862.     function clsLogfile.setUsePastebin(self, setting)
  5863.         self.usePastebin = setting
  5864.         self.writeIniFile(self)
  5865.     end
  5866.    
  5867.     function clsLogfile.getConfigModified(self)
  5868.         return self.configModified
  5869.     end
  5870.    
  5871.     function clsLogfile.setConfigModified(self, setting)
  5872.         self.configModified = setting
  5873.         self.writeIniFile(self)
  5874.     end
  5875.    
  5876.     function clsLogfile.getUseVerbose(self)
  5877.         return self.useVerbose
  5878.     end
  5879.    
  5880.     function clsLogfile.setUseVerbose(self, setting)
  5881.         self.useVerbose = setting
  5882.         self.writeIniFile(self)
  5883.     end
  5884.    
  5885.     function clsLogfile.writeIniFile(self)
  5886.         local iniFile = ""
  5887.         iniFile = fs.open("start.ini", "w")
  5888.         iniFile.writeLine("onServer=".. tostring(self.onServer))
  5889.         iniFile.writeLine("usePastebin=".. tostring(self.usePastebin))
  5890.         iniFile.writeLine("configModified=".. tostring(self.configModified))
  5891.         iniFile.writeLine("useLog=".. tostring(self.useLog))
  5892.         iniFile.writeLine("useVerbose=".. tostring(self.useVerbose))
  5893.         iniFile.close()
  5894.     end
  5895.    
  5896.     function clsLogfile.deleteIniFile(self)
  5897.         if fs.exists("start.ini") then
  5898.             fs.delete("start.ini")
  5899.             self.boolIniFileExists = false
  5900.         end
  5901.     end
  5902.     --uses:
  5903.     --[[
  5904.         fso:setStartFileNameIndex(2) -- debugging using harvestTrees log
  5905.         local fileName = fso:getCurrentFileName()
  5906.         local fileNameIndex = fso:getCurrentFileNameIndex()
  5907.         fso:useNextFileName()
  5908.         fso:useFileName("logMiningLayer1.txt")
  5909.         if fso:checkLogExists() then
  5910.             fso:deleteLog()
  5911.         end
  5912.     ]]--
  5913.     --(fso is a reference to VB6 FileSystemObject :) old habits die hard)
  5914.     fso = clsLogfile.new()
  5915. end
  5916.  
  5917. function createMine()
  5918.     local tempYcoord = location:getY()
  5919.     local numNeeded = 0
  5920.     local itemsNeeded = {}
  5921.     local itemList = ""
  5922.     local torchesAt = {}
  5923.     local sTime = os.time()
  5924.    
  5925.     for i = 1, 33 do
  5926.         torchesAt[i] = false
  5927.     end
  5928.     saveToLog("createMine: starting..", true)
  5929.     --level out mine entrance and mark with a sign
  5930.     --go down to level 15 and create + shape with firstTree at centre, 16 blocks length each arm
  5931.     --torches at each end, and half way along
  5932.     forward(16, 1) --now over original mineshaft
  5933.     while turtle.detectUp() do
  5934.         up(1, 1)
  5935.     end
  5936.     up(1) --so can dig any existing torch/ sign
  5937.     while location:getY() >= tempYcoord - 1 do
  5938.         for i = 1,4 do
  5939.             dig.digNew{self = dig, callFrom = "createMine"}
  5940.             turnRight(1)
  5941.         end
  5942.         down(1, 1)
  5943.     end
  5944.     up(1)
  5945.     turtle.select(slot:getItemSlot("cobble"))
  5946.     for i = 1,4 do
  5947.         turtle.place()
  5948.         turnRight(1)
  5949.     end
  5950.     up(1)
  5951.     if slot.getItemSlot(slot, "signs") > 0 then
  5952.         turtle.select(slot.getItemSlot(slot, "signs"))
  5953.         saveToLog("createMine: placing sign Diamond Mine\nPreparing for\nextensive operations\n"..textutils.formatTime(os.time(), true), true)
  5954.         turtle.place("Diamond Mine\nPreparing for\nDeep Excavation\n"..textutils.formatTime(os.time(), true))
  5955.         slot.update{self = slot, item = "signs", delete = true}
  5956.         if slot:getItemSlot("signs") == 0 then
  5957.             sortInventory(true)
  5958.         end
  5959.     end
  5960.     turnRight(2)
  5961.     mineEntrance:setX(location:getX())
  5962.     mineEntrance:setY(location:getY())
  5963.     mineEntrance:setZ(location:getZ())
  5964.     mineEntrance:setFacing(location:getFacing())
  5965.     saveToLog("createMine: xCoordMine = "..mineEntrance:getX(), true)
  5966.     saveToLog("createMine: yCoordMine = "..mineEntrance:getY(), true)
  5967.     saveToLog("createMine: zCoordMine = "..mineEntrance:getZ(), true)
  5968.     saveToLog("createMine: facingMine = "..mineEntrance:getFacing(), true)
  5969.    
  5970.     -- need to find redstone, lapis, (obsidian), diamond, emerald
  5971.     -- go down to level 15, dig 16X2 corridoor, by digging down on 14, digging forward on 15, placing ceiling on 16
  5972.     itemsNeeded, numNeeded = getItemsNeeded(3) --eg {"diamond","redstone"}, 2
  5973.     saveToLog("createMine: no. items needed = "..numNeeded, true)
  5974.     if numNeeded > 0 then
  5975.         for i = 1, numNeeded do
  5976.             if i == 1 then
  5977.                 itemList = itemList.."'"..tostring(itemsNeeded[i]).."'"
  5978.             else
  5979.                 itemList = itemList..",'"..tostring(itemsNeeded[i]).."'"
  5980.             end
  5981.             saveToLog("createMine, item "..i.." needed = "..itemsNeeded[i], true)
  5982.         end
  5983.     end
  5984.     while location:getY() > 14 do
  5985.         down(1, 1, itemList)
  5986.     end
  5987.     refuel(0)
  5988.     -- eg itemList = 'redstone','ironore','diamonds'
  5989.     -- now dig 32 x 2 corridor
  5990.     -- mineCorridor(length, torchesAt, doNotPlug, doReturn)
  5991.     for i = 1, 3 do
  5992.         torchesAt[1] = false
  5993.         torchesAt[9] = true
  5994.         torchesAt[17] = false
  5995.         torchesAt[25] = true
  5996.         torchesAt[33] = true
  5997.         --do first corridor
  5998.         --mineCorridor(length = 33, torchesAt, doNotPlug entance, do not return)
  5999.         mineCorridor(33, torchesAt, true, false, itemList) --mineCorridor(length, torchesAt, doNotPlug, doReturn, itemList)
  6000.         turnRight(2) -- at end of first corridor, facing entrance (N)
  6001.         forward(16, 1)
  6002.         turnRight(1) --now in centre, facing E
  6003.         torchesAt[1] = false
  6004.         torchesAt[17] = true
  6005.         --mineCorridor(length = 17, torchesAt 9, 17, doNotPlug entrance, return 17 blocks and turn 180 deg)
  6006.         mineCorridor(17, torchesAt, false, false, itemList) --now in centre, facing E
  6007.         refuel(0)
  6008.         turnRight(2)
  6009.         forward(16, 1) --now in centre, facing W
  6010.         mineCorridor(17, torchesAt, false, false, itemList) --now in centre, facing W
  6011.         turnRight(2)
  6012.         forward(16, 1)
  6013.         turnLeft(1) --now in centre, facing N
  6014.         forward(16, 1)
  6015.         turnRight(2)
  6016.         if i < 3 then
  6017.             down(3, 1)
  6018.         end
  6019.     end
  6020.     refuel(0)
  6021.     while location:getY() < mineEntrance:getY() do
  6022.         up(1, 1)
  6023.     end --at surface now
  6024.     forward(5, 1)
  6025.     forward(1, 1) -- over markerSand
  6026.     storeUnknownItem("sand")
  6027.     forward(11, 1)
  6028.     turnRight(2)
  6029.     saveToLog("createMine: started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), true)
  6030. end
  6031.  
  6032. function createMinePlug()
  6033.     dig.digNew{self = dig, direction = "down"}
  6034.     down(1)
  6035.     for i = 1, 4 do
  6036.         if checkWater("forward") then
  6037.             if getCobbleStock() > 0 then
  6038.                 turtle.select(slot:getItemSlot("cobble"))
  6039.                 turtle.place()
  6040.             elseif getDirtStock() > 0 then
  6041.                 turtle.select(slot:getItemSlot("dirt"))
  6042.                 turtle.place()
  6043.             end
  6044.         end
  6045.         turnRight(1)
  6046.     end
  6047.     up(1)
  6048.     if getCobbleStock() > 0 then
  6049.         turtle.select(slot:getItemSlot("cobble"))
  6050.         turtle.placeDown()
  6051.     elseif getDirtStock() > 0 then
  6052.         turtle.select(slot:getItemSlot("dirt"))
  6053.         turtle.placeDown()
  6054.     end
  6055. end
  6056.  
  6057. function createPlaceStorageObject()
  6058.     clsPlaceStorage = {} -- the table representing the class, which will double as the metatable for any instances
  6059.     clsPlaceStorage.__index = clsPlaceStorage -- failed table lookups on the instances should fallback to the class table, to get methods
  6060.  
  6061.     function clsPlaceStorage.initialise() --equivalent to VB class initialise
  6062.         local self = setmetatable({}, clsPlaceStorage)
  6063.  
  6064.         self.storage = {}
  6065.         self.marker = {}
  6066.        
  6067.         self.storage["torches"] = false
  6068.         self.storage["sticks"] = false
  6069.         self.storage["signs"] = false
  6070.         self.storage["wood"] = false
  6071.         self.storage["ironore"] = false
  6072.         self.storage["sand"] = false
  6073.         self.storage["redstone"] = false
  6074.         self.storage["pickaxes"] = false
  6075.         self.storage["saplings"] = false
  6076.        
  6077.         self.marker["torches"] = false
  6078.         self.marker["signs"] = false
  6079.         self.marker["sticks"] = false
  6080.         self.marker["wood"] = false
  6081.         self.marker["ironore"] = false
  6082.         self.marker["sand"] = false
  6083.         self.marker["redstone"] = false
  6084.         self.marker["lapis"] = false
  6085.        
  6086.         return self
  6087.     end
  6088.  
  6089.     function clsPlaceStorage.place(self, storageName, markerOnly)
  6090.         --assume placed under furnace, facing tree farm
  6091.         local woodAvailable = 0
  6092.         local wood2Available = 0
  6093.         local woodType = ""
  6094.         local planksType = ""
  6095.         local numChestsNeeded = 0
  6096.         local doContinue = false
  6097.         local placeChest = false
  6098.         local placeMarker = true
  6099.         local success = false
  6100.         local planksType = {}
  6101.        
  6102.         changeDirection("faceMine")
  6103.         planksType["wood"] = "planks"
  6104.         planksType["wood2"] = "planks2"
  6105.         -- placed relative North of furnace, on path to tree farm, every other block, 8 chests in all
  6106.         -- storageTorches, storageSticks, storageSigns, storageWood, storageIronore, storageSand, storageRedstone, storagePickaxe
  6107.        
  6108.         if markerOnly then
  6109.             doContinue = true
  6110.             placeChest = false
  6111.         else
  6112.             placeChest = true
  6113.             saveToLog("placeStorage"..storageName..": checking if enough wood or chests")
  6114.             numChestsNeeded = 2 - turtle.getItemCount(slot:getItemSlot("chests"))
  6115.             if numChestsNeeded < 0 then
  6116.                 numChestsNeeded = 0
  6117.             else
  6118.                 saveToLog("placeStorage"..storageName..": need 1 chest crafted")
  6119.             end
  6120.            
  6121.             if storageName == "torches" then
  6122.                 --check if enough wood to make chest(s)
  6123.                 saveToLog("placeStorage: checking if enough wood / wood2...")
  6124.                 woodType, woodAvailable, wood2Available = getWoodAvailable{woodNeeded = 0} --need 2 for chest, 1 for sticks, 1 for charcoal
  6125.                 if woodType ~= "none" then --enough to continue
  6126.                     doContinue = true
  6127.                     placeChest = true
  6128.                 else
  6129.                     placeMarker = false
  6130.                 end
  6131.             else
  6132.                 if numChestsNeeded > 0 then
  6133.                     --make planks first
  6134.                     woodType = getWoodAvailable{woodNeeded = 2 * numChestsNeeded + 2} --2 per chest + 2 spare
  6135.                     if woodType ~= "none" then --enough to continue with 2 spare
  6136.                         if woodType == "wood" then
  6137.                             saveToLog("placeStorage: "..woodAvailable.." of "..woodType.." available.", false)
  6138.                         else
  6139.                             saveToLog("placeStorage: "..wood2Available.." of "..woodType.." available.", false)
  6140.                         end
  6141.                         saveToLog("placeStorage: crafting "..planksType[woodType].." from "..woodType)
  6142.                         craft{craftItem = planksType[woodType], craftQuantity = 8 * numChestsNeeded, sourceItem1 = woodType, destSlot = 0, doSort = true}
  6143.                         saveToLog("placeStorage: crafting chests from "..planksType[woodType])
  6144.                         craft{craftItem = "chests", craftQuantity = numChestsNeeded, sourceItem1 = planksType[woodType], destSlot = 0, doSort = true}
  6145.                         doContinue = true
  6146.                     end
  6147.                 else
  6148.                     saveToLog("placeStorage: enough chests already in stock")
  6149.                     doContinue = true
  6150.                 end
  6151.             end
  6152.         end
  6153.        
  6154.         if doContinue then -- chests available/ markerOnly
  6155.             if storageName == "torches" then
  6156.                 saveToLog("placeStorage: woodAvailable = "..woodAvailable.." of "..woodType, true)
  6157.                 saveToLog("placeStorage: crafting "..planksType[woodType].." from "..woodType, true)
  6158.                 craft{craftItem = planksType[woodType], craftQuantity = 12, sourceItem1 = woodType, destSlot = 0, doSort = true}
  6159.                 saveToLog("placeStorage: crafting chests from "..planksType[woodType], true)
  6160.                 craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = planksType[woodType], destSlot = 0, doSort = true}
  6161.                 craftSticks(4)
  6162.                 craftTorches(4)
  6163.                 smelt("cobble", 1) --use last planks, unless coal already found and used
  6164.                 turnRight(1)
  6165.                 turtle.select(slot:getItemSlot("torches"))
  6166.                 saveToLog("placeStorage: placing torch", true)
  6167.                 turtle.place() --next to chest/furnace
  6168.                 turnLeft(1)--back to start
  6169.                 success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 0)
  6170.             elseif storageName == "signs" then
  6171.                 success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 2)
  6172.             elseif storageName == "sticks" then
  6173.                 if slot:getItemSlot("sticks") > 0 then
  6174.                     if turtle.getItemCount(slot:getItemSlot("sticks")) >= 6 then
  6175.                         if not craft{craftItem = "fences", craftQuantity = 2, sourceItem1 = "sticks", destSlot = 0} then
  6176.                             placeMarker = false
  6177.                         end
  6178.                     else
  6179.                         if storageSticks:getItemCount("sticks") >= 6 then
  6180.                             forward(4, 1)
  6181.                             turnRight(2)
  6182.                             getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
  6183.                             forward(4, 1)
  6184.                             turnRight(2)
  6185.                             if not craft{craftItem = "fences", craftQuantity = 2, sourceItem1 = "sticks", destSlot = 0} then
  6186.                                 placeMarker = false
  6187.                             end
  6188.                         end
  6189.                     end
  6190.                 end
  6191.                 success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 4)
  6192.             elseif storageName == "wood" then
  6193.                 success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 6)
  6194.             elseif storageName == "ironore" then
  6195.                 if slot:getItemSlot("ironore") > 0 then
  6196.                     if turtle.getItemCount(slot:getItemSlot("ironore")) < 2 then --not enough ironore to mark
  6197.                         placeMarker = false
  6198.                     end
  6199.                 end
  6200.                 success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 8)
  6201.             elseif storageName == "sand" then
  6202.                 success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 10)
  6203.             elseif storageName == "redstone" then -- 1 chests to place
  6204.                 if slot:getItemSlot("redstone") > 0 then
  6205.                     if turtle.getItemCount(slot:getItemSlot("redstone")) > 9 then
  6206.                         if craft{craftItem = "redstone block", craftQuantity = 1, sourceItem1 = "redstone", destSlot = 0} then
  6207.                             placeMarker = true
  6208.                         else
  6209.                             placeMarker = false
  6210.                         end
  6211.                     else
  6212.                         placeMarker = false
  6213.                     end
  6214.                 else
  6215.                     placeMarker = false
  6216.                 end
  6217.                 success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 12)
  6218.             elseif storageName == "pickaxes" then
  6219.                 success = placeStorage.doTask(self, storageName, placeChest, false, 14)
  6220.             elseif storageName == "saplings" then
  6221.                 forward(2, 1)
  6222.                 turnRight(1)
  6223.                 forward(2)
  6224.                 turnLeft(1)
  6225.                 dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.place"}
  6226.                 turtle.select(slot:getItemSlot("chests"))
  6227.                 turtle.placeDown()
  6228.                 saveToLog("placeStorage: StorageSaplings placed")
  6229.                 self.storage["saplings"] = true
  6230.                 turnLeft(1)
  6231.                 forward(2, 1)
  6232.                 turnLeft(1)
  6233.                 forward(2, 1)
  6234.                 turnRight(2)
  6235.                 success = true
  6236.             end
  6237.         else
  6238.             saveToLog("placeStorage: not enough wood to place storage"..
  6239.                        string.upper(string.sub(storageName, 1, 1))..
  6240.                        string.sub(storageName, 2), false, true)
  6241.         end
  6242.        
  6243.         -- placeStorage:place("signs", false, 1")
  6244.         -- placeStorage:place(storageName, false, 1")
  6245.        
  6246.         return success
  6247.     end
  6248.    
  6249.     function clsPlaceStorage.doTask(self, storageName, placeChest, placeMarker, moveForward)
  6250.         local success = false
  6251.         local storagePlacedName = storageName
  6252.         local markerPlacedName = storageName
  6253.         local storageLogText = "placeStorage: storage"..
  6254.                                 string.upper(string.sub(storageName, 1, 1))..
  6255.                                 string.sub(storageName, 2).." placed"
  6256.                                
  6257.         local markerLogText = "placeStorage: placing marker "..storageName
  6258.        
  6259.         changeDirection("faceMine")
  6260.         forward(moveForward, 1)
  6261.        
  6262.         if placeChest then
  6263.             dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
  6264.             down(1)
  6265.             dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
  6266.             turtle.select(slot:getItemSlot("cobble"))
  6267.             turtle.placeDown()
  6268.             up(1)
  6269.             turtle.select(slot:getItemSlot("chests"))
  6270.             turtle.placeDown()
  6271.             saveToLog(storageLogText)
  6272.             self.storage[storagePlacedName] = true
  6273.             forward(1, 1)
  6274.             dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
  6275.             down(1)
  6276.             turtle.select(slot:getItemSlot("cobble"))
  6277.             turtle.placeDown()
  6278.             up(1)
  6279.             turtle.placeDown()
  6280.             success = true
  6281.         else
  6282.             forward(1, 1)
  6283.         end
  6284.         if placeMarker then
  6285.             if storageName == "sticks" then
  6286.                 storageName = "fences"
  6287.             elseif storageName == "redstone" then
  6288.                 storageName = "redstone block"
  6289.             end
  6290.             if slot:getItemSlot(storageName) > 0 then
  6291.                 if storageName == "wood" then
  6292.                     if turtle.getItemCount(slot:getItemSlot(storageName)) < 2 then
  6293.                         if slot:getItemSlot("wood2") > 0 then
  6294.                             if turtle.getItemCount(slot:getItemSlot(storageName)) < 2 then
  6295.                                 placeMarker = false
  6296.                             else
  6297.                                 storageName ="wood2"
  6298.                             end
  6299.                         end
  6300.                     end
  6301.                 end
  6302.                 if placeMarker then -- ok for wood/wood2
  6303.                     dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
  6304.                     down(1)
  6305.                     dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
  6306.                     if getCobbleStock() > 1 then
  6307.                         turtle.select(slot:getItemSlot("cobble"))
  6308.                     else
  6309.                         turtle.select(slot:getItemSlot("dirt"))
  6310.                     end
  6311.                     turtle.placeDown()
  6312.                     up(1)
  6313.                     saveToLog(markerLogText, true, false)
  6314.                     turtle.select(slot:getItemSlot(storageName))
  6315.                     if storageName == "signs" then
  6316.                         turtle.placeDown("You are \nStanding on \nstorageSigns \nGET OFF!")
  6317.                     else
  6318.                         turtle.placeDown()
  6319.                     end
  6320.                     self.marker[markerPlacedName] = true
  6321.                     success = true
  6322.                     if storageName == "fences" then
  6323.                         turtle.refuel() --destroy remaining fence
  6324.                     end
  6325.                     slot.update{self = slot, item = storageName, delete = true}
  6326.                 end
  6327.             end
  6328.         end
  6329.         --return to furnace
  6330.         turnRight(2)
  6331.         forward(moveForward + 1, 1)
  6332.         turnRight(2)
  6333.        
  6334.         return success
  6335.     end
  6336.    
  6337.     function clsPlaceStorage.getMarkerPlaced(self, markerName)
  6338.         return self.marker[markerName] or false
  6339.     end
  6340.    
  6341.     function clsPlaceStorage.getStoragePlaced(self, storageName)
  6342.         --placeStorage:getStoragePlaced("sand")
  6343.         return self.storage[storageName] or false
  6344.     end
  6345.    
  6346.     function clsPlaceStorage.setMarkerPlaced(self, markerName)
  6347.         --placeStorage:getMarkerPlaced("sand")
  6348.         self.marker[markerName] = true
  6349.     end
  6350.    
  6351.     function clsPlaceStorage.setStoragePlaced(self, storageName)
  6352.         self.storage[storageName] = true
  6353.     end
  6354.    
  6355.     placeStorage = clsPlaceStorage.initialise()
  6356. end
  6357.  
  6358. function createTreeFarmObject()
  6359.     clsTreeFarm = {} -- the table representing the class, which will double as the metatable for any instances
  6360.     clsTreeFarm.__index = clsTreeFarm -- failed table lookups on the instances should fallback to the class table, to get methods
  6361.  
  6362.     function clsTreeFarm.new() --equivalent to VB class initialise
  6363.         local self = setmetatable({}, clsTreeFarm)
  6364.        
  6365.         self.numSaplings = 0
  6366.         self.timePlanted = 0
  6367.         self.dayPlanted = 0
  6368.         self.timeHarvested = 0
  6369.         self.woodHarvested = 0
  6370.        
  6371.         self.saplingPattern = {}
  6372.    
  6373.         self.saplingPattern[1] = 0
  6374.         self.saplingPattern[2] = 1
  6375.         self.saplingPattern[3] = 2
  6376.         self.saplingPattern[4] = 0
  6377.         self.saplingPattern[5] = 1
  6378.         self.saplingPattern[6] = 2
  6379.         self.saplingPattern[7] = 0
  6380.         self.saplingPattern[8] = 1
  6381.         self.saplingPattern[9] = 2
  6382.         self.saplingPattern[10] = 0
  6383.         self.saplingPattern[11] = 1
  6384.         self.saplingPattern[12] = 2
  6385.         self.saplingPattern[13] = 0
  6386.         self.saplingPattern[14] = 0
  6387.         self.saplingPattern[15] = 0
  6388.        
  6389.         return self
  6390.     end
  6391.    
  6392.     function clsTreeFarm.reset(self)
  6393.         self.numSaplings = 0
  6394.         self.timePlanted = 0
  6395.         self.dayPlanted = 0
  6396.         self.timeHarvested = 0
  6397.         self.woodHarvested = 0
  6398.     end
  6399.    
  6400.     function clsTreeFarm.getNumSaplings(self)
  6401.         return self.numSaplings
  6402.     end
  6403.     function clsTreeFarm.setNumSaplings(self, num)
  6404.         self.numSaplings = num
  6405.     end
  6406.     function clsTreeFarm.addSapling(self)
  6407.         self.numSaplings = self.numSaplings + 1
  6408.     end
  6409.    
  6410.     function clsTreeFarm.getTimePlanted(self)
  6411.         return self.timePlanted
  6412.     end
  6413.     function clsTreeFarm.setTimePlanted(self, num)
  6414.         self.timePlanted = num
  6415.         self.dayPlanted = os.day()
  6416.     end
  6417.    
  6418.     function clsTreeFarm.getTimeHarvested(self)
  6419.         return self.timeHarvested
  6420.     end
  6421.     function clsTreeFarm.setTimeHarvested(self, num)
  6422.         self.timeHarvested = num
  6423.     end
  6424.    
  6425.     function clsTreeFarm.getWoodHarvested(self)
  6426.         return self.woodHarvested
  6427.     end
  6428.     function clsTreeFarm.setWoodHarvested(self, num)
  6429.         self.woodHarvested = num
  6430.     end
  6431.    
  6432.     function clsTreeFarm.getSaplingPattern(self, index)
  6433.         return self.saplingPattern[index]
  6434.     end
  6435.    
  6436.     function clsTreeFarm.getMaxHarvest(self)
  6437.         return self.numSaplings * 5
  6438.     end
  6439.    
  6440.     function clsTreeFarm.setDebug(self, saplings, trees)
  6441.         --use if debugging and treeFarm already planted
  6442.         self.numSaplings = saplings + trees
  6443.         self.timePlanted = os.time()
  6444.         if trees >= 6 then
  6445.             self.dayPlanted = os.day() - 3
  6446.         end
  6447.     end
  6448.     function clsTreeFarm.getPotentialHarvest(self)
  6449.         local currentDay = os.day()
  6450.         local currentTime = os.time()
  6451.         local numDays = 0
  6452.         local numHours = 0
  6453.         local harvest = 0
  6454.        
  6455.         --days measured from world creation
  6456.         numDays = currentDay - self.dayPlanted
  6457.        
  6458.         --time 0 - 24 then resets
  6459.         if currentTime > self.timePlanted then -- eg 13.5 > 7.5 (13.5 - 7.5 = 6 hours)
  6460.             numHours = currentTime - self.timePlanted
  6461.         else -- eg 6.465 < 21.454
  6462.             numHours = 24 - self.timePlanted + currentTime -- eg 24 - 21.500 + 6.500 = 9 hours
  6463.         end
  6464.         -- 8 trees planted, half done in 12-14 minecraft hours
  6465.         -- 1 tree  4.5 hrs
  6466.         -- 2 trees 7 hrs
  6467.         -- 3 trees 8 hrs
  6468.         -- 4 trees 13 hrs
  6469.         -- 5 trees 20 hrs
  6470.         -- 6 trees 30 hours
  6471.         -- estimate as half no of saplings per 12 hours
  6472.         if numDays == 0 then
  6473.             harvest = math.ceil((5 * self.numSaplings) / 2)
  6474.         elseif numDays == 1 then
  6475.             harvest = math.ceil(((5 * self.numSaplings) * 2) / 3)
  6476.         else -- > 2 days probably most trees * 5 wood
  6477.             harvest = 5 * self.numSaplings
  6478.         end
  6479.        
  6480.         return harvest
  6481.     end
  6482.    
  6483.     treeFarm = clsTreeFarm.new()
  6484. end
  6485.  
  6486. function createSlotObject()
  6487. -- slot variables
  6488.     clsSlot = {}
  6489.     clsSlot.__index = clsSlot
  6490.    
  6491.     function clsSlot.new(self) --creates new instance of slot object
  6492.         local self = setmetatable({}, clsSlot)
  6493.  
  6494.         self.slotContains = {}
  6495.         self.slotCount = {}
  6496.         self.slotStatus = {}
  6497.         self.index = 1
  6498.         self.itemTested = {}
  6499.         self.firstUnknownItem = ""
  6500.         self.lastUnknownItem = ""
  6501.         self.freeUnknownItem = "item1"
  6502.         self.numUnknownItems = 0
  6503.         -- initialise variables
  6504.         for i = 1, 16 do
  6505.             self.slotContains[i] = ""
  6506.             self.slotCount[i] = 0
  6507.             self.slotStatus[i] = "empty"
  6508.             self.itemTested[i] = ""
  6509.         end
  6510.        
  6511.         return self    
  6512.     end
  6513.    
  6514.     function clsSlot.resetVariables(self) --resets all variables (properties) of slot object
  6515.         for i = 1, 16 do
  6516.             self.slotContains[i] = ""
  6517.             self.slotCount[i] = 0
  6518.             self.slotStatus[i] = "empty"
  6519.             self.itemTested[i] = ""
  6520.         end
  6521.         self.firstUnknownItem = ""
  6522.         self.lastUnknownItem = ""
  6523.         self.freeUnknownItem = "item1"
  6524.         self.index = 1
  6525.         self.numUnknownItems = 0       
  6526.     end
  6527.    
  6528.     function clsSlot.getUnknownItemCount(self) --returns no of unknown items eg "item1", "item2" count = 2
  6529.         local amount = 0
  6530.         local tempSlot = 0
  6531.        
  6532.         for i = 1, 16 do
  6533.             if self.slotStatus[i] == "unknown" then
  6534.                 amount = amount + 1
  6535.             end
  6536.         end
  6537.  
  6538.         return amount--0 if no unknown item slots
  6539.     end
  6540.    
  6541.     function clsSlot.getSlotContains(self, slotNo) --return contents of selected slotNo eg slot 4 = "dirt"
  6542.         return self.slotContains[slotNo]
  6543.     end
  6544.    
  6545.     function clsSlot.getItemCount(self, item) --return number of items in turtle using item name eg "dirt" = 58
  6546.         local result = 0
  6547.        
  6548.         for i = 1, 16 do
  6549.             if self.slotContains[i] == item then
  6550.                 result = self.slotCount[i]
  6551.                 break
  6552.             end
  6553.         end
  6554.         return result
  6555.     end
  6556.    
  6557.     function clsSlot.getSlotCount(self, slotNo) --return number of items in turtle slot = turtle.getItemCount()
  6558.         return self.slotCount[slotNo]
  6559.     end
  6560.    
  6561.     function clsSlot.getSlotStatus(self, slotNo)    --return "empty", "known", "unknown"
  6562.         return self.slotStatus[slotNo]
  6563.     end
  6564.    
  6565.     function clsSlot.setSlotCount(self, slotNo, count)  --set number of items in turtle slot
  6566.         self.slotCount[slotNo] = count
  6567.     end
  6568.    
  6569.     function clsSlot.getItemSlot(self, item) --return slot number of selected item eg dirt in slot 4
  6570.         local result = 0
  6571.        
  6572.         for i = 1, 16 do
  6573.             if self.slotContains[i] == item then
  6574.                 result = i
  6575.                 break
  6576.             end
  6577.         end
  6578.        
  6579.         return result
  6580.     end
  6581.    
  6582.     function clsSlot.getItemTested(self, slotNo, item) --VB Property Get return if eg slot 11 has been tested for "gravel"
  6583.         local result = false
  6584.        
  6585.         if self.itemTested[slotNo] ~= "" then
  6586.             if string.find(self.itemTested[slotNo], item) ~= nil then
  6587.                 result = true
  6588.             end
  6589.         end
  6590.        
  6591.         return result
  6592.     end
  6593.    
  6594.     function clsSlot.setItemTested(self, slotNo, item) --VB Property Let equivalent used to see if an unknown item has been checked
  6595.         --self.itemTested[item1] = "coal,flint,gravel"
  6596.         local result = false
  6597.        
  6598.         if self.itemTested[slotNo] == "" then
  6599.             self.itemTested[slotNo] = item
  6600.         else
  6601.             if string.find(self.itemTested[slotNo], item) == nil then
  6602.                 self.itemTested[slotNo] = self.itemTested[slotNo]..","..item
  6603.                 result = true
  6604.             end
  6605.         end
  6606.        
  6607.         return result
  6608.     end
  6609.    
  6610.     function clsSlot.resetUnknownItems(self) --reset "item1", "item2" etc to default
  6611.         local index = 0
  6612.         local firstItem = ""
  6613.         local lastItem = ""
  6614.        
  6615.         self.numUnknownItems = 0
  6616.         for i = 1, 16 do
  6617.             if self.slotStatus[i] == "unknown" then
  6618.                 index = index + 1
  6619.                 self.numUnknownItems = self.numUnknownItems + 1
  6620.                 self.slotContains[i] = "item"..index
  6621.                 if firstItem == "" then
  6622.                     firstItem = "item"..index
  6623.                 end
  6624.                 lastItem = "item"..index
  6625.                 firstFreeItem = "item"..index + 1
  6626.             end
  6627.         end
  6628.         if index > 0 then --at least 1 unknown item
  6629.             self.firstUnknownItem = firstItem
  6630.             self.lastUnknownItem = lastItem
  6631.             self.freeUnknownItem = firstFreeItem
  6632.         else
  6633.             self.firstUnknownItem = ""
  6634.             self.lastUnknownItem = ""
  6635.             self.freeUnknownItem = "item1"
  6636.         end
  6637.     end
  6638.    
  6639.     function clsSlot.getFirstUnknownItem(self) --returns eg item1 or ""
  6640.         return self.firstUnknownItem
  6641.     end
  6642.    
  6643.     function clsSlot.getLastUnknownItem(self) --returns eg "item3"
  6644.         return self.lastUnknownItem
  6645.     end
  6646.    
  6647.     function clsSlot.getFreeUnknownItem(self) --returns first unused itemX eg "item4"
  6648.         return self.freeUnknownItem
  6649.     end
  6650.    
  6651.     function clsSlot.getMostUnknownItem(self) --returns itemX with largest itemCount eg "item3"
  6652.         local mostItem = ""
  6653.         local amount = 0
  6654.         local tempSlot = 0
  6655.        
  6656.         for i = 1, 16 do
  6657.             if self.slotStatus[i] == "unknown" then
  6658.                 if self.slotCount[i] > amount then
  6659.                     amount = self.slotCount[i]
  6660.                     mostItem = self.slotContains[i]
  6661.                 end
  6662.             end
  6663.         end
  6664.        
  6665.         return mostItem --"" if no unknown items
  6666.     end
  6667.    
  6668.    
  6669.     function clsSlot.getMostUnknownItemSlot(self) --returns itemX with largest itemCount eg "item3"
  6670.         local amount = 0
  6671.         local useSlot = 0
  6672.        
  6673.         for i = 1, 16 do
  6674.             if self.slotStatus[i] == "unknown" then
  6675.                 if self.slotCount[i] > amount then
  6676.                     amount = self.slotCount[i]
  6677.                     useSlot = i
  6678.                 end
  6679.             end
  6680.         end
  6681.        
  6682.         return useSlot --0 if no unknown items
  6683.     end
  6684.    
  6685.     function clsSlot.getLeastUnknownItemSlot(self) --returns slotNo with smallest itemCount eg "item1" - slot 7
  6686.         local amount = 65
  6687.         local useSlot = 0
  6688.        
  6689.         for i = 1, 16 do
  6690.             if self.slotStatus[i] == "unknown" then
  6691.                 if self.slotCount[i] < amount then
  6692.                     amount = self.slotCount[i]
  6693.                     useSlot = i
  6694.                 end
  6695.             end
  6696.         end
  6697.        
  6698.         return useSlot --0 if no unknown items
  6699.     end
  6700.    
  6701.     function clsSlot.transfer(arg)
  6702.         local self = arg.self
  6703.  
  6704.         --slot.transfer{self = slot, fromSlot, transferTo = 16}
  6705.         --slot.transfer{self = slot, item = "wood", transferTo = 16}
  6706.         --slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
  6707.         if arg.fromSlot == nil then --arg.item provided
  6708.             for i = 1, 16 do
  6709.                 if self.slotContains[i] == arg.item then
  6710.                     arg.fromSlot = i
  6711.                     break
  6712.                 end
  6713.             end
  6714.             --arg.fromSlot = self.itemSlot[arg.item] --itemSlot["wood"] (1)
  6715.         end
  6716.         if arg.item == nil then
  6717.             arg.item = self.slotContains[arg.fromSlot]
  6718.         end
  6719.         self.slotCount[arg.transferTo] = self.slotCount[arg.fromSlot]
  6720.         self.slotCount[arg.fromSlot] = 0
  6721.         self.slotStatus[arg.transferTo] = self.slotStatus[arg.fromSlot]
  6722.         self.slotStatus[arg.fromSlot] = "empty"
  6723.         self.itemTested[arg.transferTo] = self.itemTested[arg.fromSlot]
  6724.         self.itemTested[arg.fromSlot] = ""
  6725.         self.slotContains[arg.transferTo] = self.slotContains[arg.fromSlot]
  6726.         self.slotContains[arg.fromSlot] = ""
  6727.         --saveToLog("transfer: from slot "..arg.fromSlot.." contains "..self.slotCount[arg.fromSlot].." "..self.slotContains[arg.fromSlot])
  6728.         --saveToLog("transfer: to slot "..arg.transferTo.." contains "..self.slotCount[arg.transferTo].." "..self.slotContains[arg.transferTo])
  6729.     end
  6730.  
  6731.    
  6732.     --slot.update{self = slot, slotNo = dugSlot, item = "item1"}
  6733.     --slot.update{self = slot, item = arg.sourceItem1, delete = true}
  6734.     --slot.update{self = slot, item = "planks", delete = true}
  6735.     --slot.update{self = slot, item = "?iron", newItem = "iron"}
  6736.     --slot.update{self = slot, item = "furnaces", delete = true}
  6737.    
  6738.     function clsSlot.update(arg) -- update turtle slot with item contents
  6739.         local quantity = 0
  6740.         local self = arg.self
  6741.         local tempItem = ""
  6742.         local tempSlotNo = arg.slotNo
  6743.         local count = 0
  6744.        
  6745.         --arg.self
  6746.         --arg.item
  6747.         --arg.slotNo
  6748.         --arg.delete
  6749.         --arg.deleteSlot
  6750.         --arg.newItem
  6751.         -- if slotNo given use it, or use existing slot
  6752.         if arg.slotNo == nil then --slot not provided, default to 0
  6753.             arg.slotNo = 0
  6754.         else --slotNo provided
  6755.             if arg.item == nil then --get item from slotNo
  6756.                 arg.item = self.slotContains[arg.slotNo]
  6757.             end
  6758.         end
  6759.         if arg.item ~= nil then --item name provided
  6760.             if arg.slotNo == 0 then
  6761.                 for i = 1, 16 do
  6762.                     if self.slotContains[i] == arg.item then
  6763.                         arg.slotNo = i
  6764.                         break
  6765.                     end
  6766.                 end
  6767.             end
  6768.         end
  6769.         arg.delete = arg.delete or false
  6770.         arg.doReset = arg.doReset or false --used when crafting in fillChest
  6771.         -- item given. If not already in stock, use next available
  6772.        
  6773.         if arg.newItem ~= nil then -- naming unknown. delete is assumed, arg.slotNo, arg.item calculated above
  6774.             --slot.update{self = slot, slotNo = dugSlot, newItem = "coal"}
  6775.            
  6776.             self.itemTested[arg.slotNo] = ""
  6777.             self.slotContains[arg.slotNo] = arg.newItem
  6778.             self.slotCount[arg.slotNo] = turtle.getItemCount(arg.slotNo)
  6779.  
  6780.             if string.find(arg.newItem, "item") ~= nil then
  6781.                 self.slotStatus[arg.slotNo] = "unknown"
  6782.             else
  6783.                 self.slotStatus[arg.slotNo] = "known"
  6784.                 objectives[arg.newItem] = true
  6785.             end
  6786.            
  6787.             saveToLog(" slot.update: Slot "..tostring(tempSlotNo) .." contains "..tostring(self.slotCount[tempSlotNo]).." "..arg.newItem.." "..arg.item.." deleted", false, true)
  6788.         else --not changing an item, just update
  6789.             quantity = turtle.getItemCount(arg.slotNo)
  6790.             if quantity == 0 and arg.delete then --turtle slot empty - remove all references
  6791.                 self.slotContains[arg.slotNo] = ""
  6792.                 self.itemTested[arg.slotNo] = ""
  6793.                 self.slotCount[arg.slotNo] = 0
  6794.                 self.slotStatus[arg.slotNo] = "empty"
  6795.                 saveToLog(" slot.update: Deleting "..arg.item.." Slot "..arg.slotNo.." is empty", false, true)
  6796.             else --create or maintain selected slot, eg when wood2 detected and about to be harvested
  6797.                
  6798.                 self.slotCount[arg.slotNo] = quantity
  6799.                 --saveToLog("   slot.update: "..arg.item.." quantity updated to "..quantity.." in slot "..arg.slotNo, false)
  6800.                 self.slotContains[arg.slotNo] = arg.item
  6801.                 if string.find(arg.item, "item") ~= nil then --"item1", item2 etc
  6802.                     self.slotStatus[arg.slotNo] = "unknown"
  6803.                 else
  6804.                     self.slotStatus[arg.slotNo] = "known"
  6805.                     objectives[arg.item] = true
  6806.                 end
  6807.                 saveToLog(" slot.update: Slot "..arg.slotNo.." contains "..self.slotCount[arg.slotNo].." "..arg.item.." status = "..self.slotStatus[arg.slotNo], false)
  6808.             end
  6809.         end
  6810.        
  6811.         --update next free item, eg item1 just been allocated, make item2 the next free one
  6812.        
  6813.         for i = 1, 16 do
  6814.             if string.find(self.slotContains[i], "item") ~= nil then
  6815.                 count = count + 1
  6816.             end
  6817.         end
  6818.         self.freeUnknownItem = "item"..count + 1
  6819.         if count > 0 then
  6820.             self.firstUnknownItem = "item1"
  6821.             self.lastUnknownItem = "item"..count
  6822.             self.freeUnknownItem = "item"..count + 1
  6823.         end
  6824.        
  6825.         --[[for i = 1, 16 do
  6826.             saveToLog(" slot.update: slot "..i.." status = "..self.slotStatus[i])
  6827.         end]]--
  6828.         --saveToLog("   slot.update:unknownItemCount = "..self.numUnknownItems)
  6829.         if arg.doReset then
  6830.             if clsSlot:getUnknownItemCount() > 0 then
  6831.                 --saveToLog("   slot.update: resetting unknown items", false)
  6832.                 clsSlot:resetUnknownItems()
  6833.             end
  6834.         end
  6835.         --[[debug log
  6836.         local printSlot = 0
  6837.         for i = 1, 16 do
  6838.             if self.slotCount[i] > 0 then
  6839.                 saveToLog(" slot.update: debug - Slot "..i.." contains "..self.slotCount[i].." "..self.slotContains[i])
  6840.             end
  6841.         end
  6842.         for i = 1, 16 do
  6843.             if self.slotStatus[i] == "unknown" then
  6844.                 saveToLog(" slotUpdate: debug - item"..i.." slot is "..self.slotContains[i])
  6845.             end
  6846.         end]]--
  6847.     end
  6848.    
  6849.     slot = clsSlot.new()
  6850. end
  6851.  
  6852. function down(steps, useSlot, itemList, calledFrom)
  6853.     local maxTries = 10
  6854.     local success = false
  6855.    
  6856.     steps = steps or 1
  6857.     useSlot = useSlot or 1
  6858.     itemList = itemList or ""
  6859.     calledFrom = calledFrom or ""
  6860.    
  6861.     for i = 1, steps do
  6862.         if turtle.detectDown() then
  6863.             dig.digNew{self = dig, direction = "down", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
  6864.             if turtle.down() then
  6865.                 success = true
  6866.             end
  6867.         else --move down unless mob in the way
  6868.             while not turtle.down() do
  6869.                 dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = calledFrom}
  6870.                 --if turtle.attackDown() then
  6871.                 if attack() then
  6872.                     saveToLog("down: mob attacked below!", true)
  6873.                 end
  6874.                 maxTries = maxTries - 1
  6875.                 if maxTries <= 0 then
  6876.                     if turtle.getFuelLevel() == 0 then
  6877.                         if refuel(0) then
  6878.                             maxTries = 10
  6879.                         else
  6880.                             saveToLog("forward: Out of fuel. Game Over!", true)
  6881.                             error()
  6882.                         end
  6883.                     end
  6884.                 end
  6885.             end
  6886.             success = true
  6887.         end
  6888.         if success then
  6889.             location:setY(location:getY() - 1)
  6890.         end
  6891.     end
  6892.    
  6893.     return success
  6894. end
  6895.  
  6896. function emptyChest(direction)
  6897.     local useItem = ""
  6898.     local turtleSlot = 0
  6899.     local success = false
  6900.    
  6901.     for i = 1, 16 do
  6902.         useItem = craftingChest:getSlotContains(i) --eg "dirt", "item"
  6903.         if useItem ~= "" then --"item", "dirt" etc
  6904.             turtleSlot = craftingChest:getTurtleSlot(i)
  6905.             turtle.select(turtleSlot)
  6906.             if direction == "up" then
  6907.                 if turtle.suckUp() then
  6908.                     success = true
  6909.                 end
  6910.             elseif direction == "down" then
  6911.                 if turtle.suckDown() then
  6912.                     success = true
  6913.                 end
  6914.             else
  6915.                 if turtle.suck() then
  6916.                     success = true
  6917.                 end
  6918.             end
  6919.             if success then
  6920.                 --saveToLog("emptyChest: Removed "..craftingChest.getSlotCount(craftingChest, i).." "..useItem.." from chest into turtle slot "..turtleSlot, false)
  6921.                 slot.update{self = slot, slotNo = turtleSlot, item = useItem}
  6922.             else
  6923.                 saveToLog("emptyChest: failed to remove "..useItem.." from chest", false)
  6924.             end
  6925.         end
  6926.     end
  6927.     --clear arrays on emptychest routine only
  6928.     craftingChest.resetVariables(craftingChest)
  6929.     saveToLog("emptyChest completed.", false, true)
  6930.     --unable to test for empty chest so return default
  6931.     return true
  6932. end
  6933.  
  6934. function fillChest(arg)
  6935.     -- fillChest{direction = arg.direction, exceptSlot1 = sourceSlot1, exceptSlot2 = sourceSlot2, exceptSlot3 = sourceSlot3} -- fill except chosen item(s)
  6936.     -- fillChest{direction = "forward", addItem = storedItem, addAmount = storeCount, fromSlot = 16, originalSlot = 0}
  6937.     -- fillChest{direction = arg.direction, addItem = sourceItem3, addAmount = sourceQuantity3, fromSlot = 14} use this for single items
  6938.     -- turtle must be sorted first, so unknown items are after identified items
  6939.  
  6940.     local chestIndex = 1
  6941.     local useItem = ""
  6942.     local quantity = 0
  6943.     local itemAlreadyIn = false
  6944.     if arg.deleteFromTurtle == nil then
  6945.         arg.deleteFromTurtle = true
  6946.     end
  6947.     if arg.doReset == nil then
  6948.         arg.doReset = false
  6949.     end
  6950.    
  6951.     saveToLog("fillChest: arg.direction = "..tostring(arg.direction)..
  6952.                         " arg.exceptSlot1 = "..tostring(arg.exceptSlot1)..
  6953.                         " arg.exceptSlot2 = "..tostring(arg.exceptSlot2)..
  6954.                         " arg.exceptSlot3 = "..tostring(arg.exceptSlot3)..
  6955.                         " arg.addItem = "..tostring(arg.addItem)..
  6956.                         " arg.addAmount = "..tostring(arg.addAmount)..
  6957.                         " arg.fromSlot = "..tostring(arg.fromSlot)..
  6958.                         " arg.originalSlot = "..tostring(arg.originalSlot), false)
  6959.     if arg.addItem == nil then -- single item not defined, so empty all turtle except chosen items. could be failed craft attempt
  6960.         for  i = 1, 16 do
  6961.             if  arg.exceptSlot1 > 0 then --at least 1 craft item needed
  6962.                 if i == arg.exceptSlot1 or  i == arg.exceptSlot2 or  i == arg.exceptSlot3 then
  6963.                     saveToLog("Leaving crafting item "..slot.getSlotContains(slot, i).." in turtle slot "..i, false)
  6964.                 else
  6965.                     useItem = slot.getSlotContains(slot, i) --eg "", "item", "wood"
  6966.                     quantity = turtle.getItemCount(i)
  6967.                     if quantity > 0 then
  6968.                         --saveToLog("Adding ".. quantity.." "..useItem.." from turtle slot "..i.." to chest slot "..craftingChest.getIndex(craftingChest), false)
  6969.                         turtle.select(i)
  6970.                         craftingChest.addItem(craftingChest, useItem, quantity, i)
  6971.                         if arg.direction == "up" then
  6972.                             turtle.dropUp()
  6973.                         elseif arg.direction == "down" then
  6974.                             turtle.dropDown()
  6975.                         else
  6976.                             turtle.drop()
  6977.                         end
  6978.                         --update turtle slot
  6979.                         if arg.deleteFromTurtle then
  6980.                             slot.update{self = slot, slotNo = i, item = useItem, delete = true, doReset = arg.doReset}
  6981.                         end
  6982.                     end
  6983.                 end
  6984.             else --failed craft, empty turtle completely. cannot update quantities, items will go into existing slots, or form a new one
  6985.                 if  turtle.getItemCount(i) > 0 then
  6986.                     if arg.direction == "up" then
  6987.                         turtle.dropUp()
  6988.                     elseif arg.direction == "down" then
  6989.                         turtle.dropDown()
  6990.                     else
  6991.                         turtle.drop()
  6992.                     end
  6993.                 end
  6994.             end
  6995.         end
  6996.     else -- arg.addItem is "chests" or "stone"
  6997.         if arg.fromSlot ~= nil then
  6998.             turtle.select(arg.fromSlot)
  6999.         end
  7000.         if arg.originalSlot == nil then
  7001.             arg.originalSlot = arg.fromSlot
  7002.         end
  7003.         --clsChest.addItem(self, item, quantity, turtleSlot)
  7004.         --craftingChest.addItem(craftingChest, arg.addItem, arg.addAmount, arg.fromSlot)
  7005.         craftingChest:addItem(arg.addItem, arg.addAmount, arg.originalSlot)
  7006.         saveToLog("fillChest: Adding "..tostring(arg.addAmount).." "..tostring(arg.addItem).." to chest from slot "..tostring(arg.fromSlot).." (/"..tostring(arg.originalSlot)..")", false)
  7007.         if arg.direction == "up" then
  7008.             turtle.dropUp()
  7009.         elseif arg.direction == "down" then
  7010.             turtle.dropDown()
  7011.         else
  7012.             turtle.drop()
  7013.         end
  7014.     end
  7015.    
  7016.     local slotsEmptied = true
  7017.     for i = 1, 16 do
  7018.         if arg.exceptSlot1 ~= i and  arg.exceptSlot2 ~= i and  arg.exceptSlot3 ~= i then --miss out exceptSlots
  7019.             if turtle.getItemCount(i) > 0 then
  7020.                 slotsEmptied = false
  7021.                 break
  7022.             end
  7023.         end
  7024.     end
  7025.    
  7026.     return slotsEmptied
  7027. end
  7028.  
  7029. function findCobble()
  7030.     --dirt known, so dig down vertically for 10 blocks only to save time
  7031.     local sTime = os.time()
  7032.     local startyCoord = location:getY() --eg start at 68, go down to 58
  7033.     local endyCoord = location:getY() - 10
  7034.     local missionComplete = false
  7035.     local changeDirection = false
  7036.     local direction = "down"
  7037.     local testSlot = 0
  7038.  
  7039.     term.clear()
  7040.     term.setCursorPos(1, 1)
  7041.     saveToLog("findCobble: mining for cobble", true)
  7042.     down(1, 1, "coal,gravel", "findCobble")
  7043.    
  7044.     --cobble will be stored as item1, gravel as item2, coal as item3 or similar combo
  7045.     repeat
  7046.         --turtle.select(1)
  7047.         if direction == "down" then
  7048.             dig.digNew{self = dig, direction = "down", checkForItems = "coal,gravel", callFrom = "findCobble"}
  7049.             down(1, 1, "coal,gravel", "findCobble")
  7050.         else
  7051.             dig.digNew{self = dig, direction = "up", checkForItems = "coal,gravel", callFrom = "findCobble"}
  7052.             up(1, 1, "coal,gravel", "findCobble")
  7053.         end
  7054.         for i = 1, 4 do
  7055.             dig.digNew{self = dig, direction = "forward", checkForItems = "coal,gravel", callFrom = "findCobble"}
  7056.             turnRight(1)
  7057.         end
  7058.         if direction == "down" then --check amount of ? cobble collected
  7059.             testSlot = slot:getMostUnknownItemSlot()
  7060.             if testSlot > 0 then -- at least 1 ? cobble collected
  7061.                 if turtle.getItemCount(testSlot) > 30 then --need at least 25 cobble before returning
  7062.                     changeDirection = true
  7063.                 end
  7064.             end
  7065.         end
  7066.         if location:getY() <= endyCoord then
  7067.             if direction == "down" then
  7068.                 changeDirection = true
  7069.             end
  7070.         end
  7071.         if changeDirection  then --will not be true if already direction = "up"
  7072.             saveToLog("findCobble: Cobble, gravel and others may be at y = "..location:getY().." Moving forward and returning", true)
  7073.             for i = 1, 3 do
  7074.                 dig.digNew{self = dig, direction = "forward", checkForItems = "coal,gravel", callFrom = "findCobble"}
  7075.                 forward(1, 1, "coal,gravel", "findCobble")
  7076.             end
  7077.             saveToLog("Moved forward 3 blocks", false)
  7078.             turtle.select(1)
  7079.             direction = "up"
  7080.             changeDirection = false
  7081.         end
  7082.         if location:getY() == startyCoord - 5 and direction == "up" then
  7083.             missionComplete = true
  7084.         end
  7085.     until missionComplete
  7086.  
  7087.     turnRight(2)
  7088.     --if on hillside, will have left a hole behind, so fill it
  7089.     for i = 1, 3 do
  7090.         if not turtle.detectUp() then
  7091.             turtle.select(slot:getItemSlot("dirt"))
  7092.             turtle.placeUp()
  7093.         end
  7094.         forward(1, 1, "coal,gravel", "findCobble")
  7095.     end
  7096.     up(5, 1, "coal,gravel", "findCobble")
  7097.     turnRight(2) --now facing same direction as started
  7098.     --cant compare stone at this stage so return true
  7099.     --check area in front is clear
  7100.     if turtle.detect() then
  7101.         turtle.select(16)
  7102.         if turtle.dig() then
  7103.             turtle.dropUp()
  7104.         end
  7105.     end
  7106.     saveToLog("findCobble started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
  7107.     return true
  7108. end
  7109.  
  7110. function firstTree()
  7111.     local quantity = 0
  7112.     local tempyCoord = 0
  7113.     local emptySlot = 0
  7114.     local dugItem = ""
  7115.     local dugSlot = 0
  7116.     local success = false
  7117.     local isJungle = false --check if next to a jungle tree
  7118.     local isBranched = false
  7119.     local numLogs = 0
  7120.     local treeSide = ""
  7121.     local sTime = os.time()
  7122.    
  7123.     term.clear()
  7124.     term.setCursorPos(1,1)
  7125.     --break lower block of tree
  7126.     saveToLog("firstTree: Chopping tree base", true)
  7127.     dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
  7128.     --craft to 4 planks in slot 2
  7129.     saveToLog("firstTree: Crafting planks for fuel", true)
  7130.     craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 2, chest = "doNotUse", doSort = false}
  7131.     --use all planks as fuel
  7132.     turtle.select(slot:getItemSlot("planks"))
  7133.     saveToLog("firstTree: "..turtle.getItemCount(2).." planks used for fuel", true)
  7134.     turtle.refuel(turtle.getItemCount(2))
  7135.     slot.update{self = slot, item = "planks", delete = true}
  7136.     forward(1) --now under tree trunk - on level 0
  7137.     tempyCoord = location:getY() -- remember ground level
  7138.     saveToLog("firstTree: harvesting wood above", true)
  7139.     --original wood used to make planks, so turtle now empty, use expectedItem
  7140.     dig.digNew{self = dig, direction = "up", expectedItem = "wood", callFrom = "firstTree"}
  7141.     turtle.select(1)
  7142.     saveToLog("firstTree: checking if jungle tree", true, false)
  7143.     if turtle.compare() then
  7144.         saveToLog("firstTree: harvesting jungle tree", true, false)
  7145.         isJungle = true
  7146.         turnRight(1)
  7147.         if turtle.compare() then
  7148.             treeSide = "left" -- on left side of tree
  7149.         end
  7150.         turnLeft(2)
  7151.         if turtle.compare() then
  7152.             treeSide = "right" -- on right side of tree
  7153.         end
  7154.         turnRight(1)
  7155.     end
  7156.     if isJungle then
  7157.         saveToLog("firstTree: harvesting jungle wood")
  7158.     end
  7159.     up(1) -- 1 more wood. - on level 1
  7160.     dig.digNew{self = dig, direction = "up", callFrom = "firstTree"} -- dig wood from level 2
  7161.     --make 8 planks in slot 2 from wood in slot 1. Sorting will not be done as "doNotUse" chest
  7162.     saveToLog("firstTree: Crafting planks for crafting chest", true)
  7163.     craft{craftItem = "planks", craftQuantity = 8, sourceItem1 = "wood", destSlot = 2, chest = "doNotUse", doSort = false }
  7164.     saveToLog("firstTree: Crafting chest", true)
  7165.     craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 2, chest = "doNotUse", doSort = false}
  7166.     down(1) --go down and get dirt - on level 0
  7167.     saveToLog("firstTree: digging first dirt into slot 3", true)
  7168.     dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
  7169.     saveToLog("firstTree: Harvesting remaining first tree", true)
  7170.     --up(1) -- on level 1, wood above already dug
  7171.     if isJungle then
  7172.         dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
  7173.         for i = 1, 3 do
  7174.             dig.digNew{self = dig, callFrom = "firstTree"}
  7175.             if treeside == "left" then
  7176.                 turnRight(1)
  7177.             else
  7178.                 turnLeft(1)
  7179.             end
  7180.             dig.digNew{self = dig, callFrom = "firstTree"}
  7181.             if treeside == "left" then
  7182.                 turnLeft(1)
  7183.             else
  7184.                 turnRight(1)
  7185.             end
  7186.             forward(1)
  7187.             if treeside == "left" then
  7188.                 turnRight(1)
  7189.             else
  7190.                 turnLeft(1)
  7191.             end
  7192.             dig.digNew{self = dig, callFrom = "firstTree"}
  7193.             if treeside == "left" then
  7194.                 turnLeft(1)
  7195.             else
  7196.                 turnRight(1)
  7197.             end
  7198.             back(1)
  7199.             if i < 3 then
  7200.                 up(1)
  7201.             end
  7202.         end -- on level 1
  7203.     else
  7204.         up(2) -- on level 2
  7205.         dig.digNew{self = dig, direction = "up", expectedItem = "wood", callFrom = "firstTree"}
  7206.         up(1)
  7207.     end
  7208.    
  7209.     --slot1 = wood, slot2 = chests, slot3 = dirt
  7210.     while turtle.detectUp() do
  7211.         --leaves at tree top could give saplings
  7212.         dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "firstTree"}
  7213.         isBranched = checkTree("wood", isBranched, "firstTree") --look for more saplings from leaves or branches
  7214.         up(1, 1, "saplings", "firstTree")
  7215.     end
  7216.     --drop down to start
  7217.     while tempyCoord  < location:getY() do -- move to ground level
  7218.         down(1, 1)
  7219.     end
  7220.     if slot:getItemSlot("saplings") > 0 then
  7221.         for i = 1, 4 do --suck any saplings
  7222.             dig.digNew{self = dig, slotNo = 1, suck = true, checkForItems = "saplings", callFrom = "firstTree"} --forward, slot1 by default
  7223.             turnRight(1)
  7224.         end
  7225.     end
  7226.     saveToLog("firstTree: Crafting planks for fuel", true)
  7227.     turnRight(2) -- face starting point
  7228.     --if non branched tree, expect 4 logs, turn all into fuel, else preserve some logs
  7229.     numLogs = turtle.getItemCount(slot:getItemSlot("wood")) - 1
  7230.     if numLogs < 1 then
  7231.         saveToLog("firstTree: Only "..numLogs.." log available. Not enough to continue. Re-start at a taller tree", true)
  7232.         error()
  7233.     end
  7234.     if numLogs > 4 then
  7235.         numLogs = 4
  7236.     end
  7237.     if craft{craftItem = "planks", craftQuantity = numLogs * 4, sourceItem1 = "wood", destSlot = 0, doSort = false} then
  7238.         turtle.select(slot:getItemSlot("planks"))
  7239.         turtle.refuel()
  7240.         slot.update{self = slot, item = "planks", delete = true}
  7241.     else
  7242.         saveToLog("firstTree: Failed to craft planks. Check if more than 1 wood available", true)
  7243.         error()
  7244.     end
  7245.     saveToLog("firstTree: Fuel level: "..turtle.getFuelLevel(), true)
  7246.     saveToLog("firstTree: harvested all wood, now at ground level x="..location:getX().." y="..location:getY().." z="..location:getZ().." facing "..location:getCompass(), false)
  7247.     saveToLog("firstTree: Fuel level: "..turtle.getFuelLevel(), true)
  7248.     turnRight(2)
  7249.     saveToLog("firstTree started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
  7250. end
  7251.  
  7252. function forward(steps, useSlot, itemList, calledFrom)
  7253.     local maxTries = 10
  7254.    
  7255.     useSlot = useSlot or 1
  7256.     itemList = itemList or ""
  7257.     calledFrom = calledFrom or ""
  7258.    
  7259.     if steps > 0 then
  7260.         for i = 1, steps do
  7261.             if turtle.detect() then
  7262.                 while not turtle.forward() do
  7263.                     dig.digNew{self = dig, slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
  7264.                     maxTries = maxTries - 1
  7265.                     if maxTries <= 0 then
  7266.                         maxTries = 10
  7267.                         saveToLog("forward: maxTries reached. Unable to move forward check for bedrock", true)
  7268.                         break
  7269.                     end
  7270.                 end
  7271.             else --move unless mob in the way
  7272.                 while not turtle.forward() do
  7273.                     dig.digNew{self = dig, slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
  7274.                     --if turtle.attack() then
  7275.                     if attack() then
  7276.                         saveToLog("forward: mob attacked in front!", true)
  7277.                     end
  7278.                     maxTries = maxTries - 1
  7279.                     if maxTries <= 0 then
  7280.                         if turtle.getFuelLevel() == 0 then
  7281.                             if refuel(0) then
  7282.                                 maxTries = 10
  7283.                             else
  7284.                                 saveToLog("forward: Out of fuel. Game Over!", true)
  7285.                                 error()
  7286.                             end
  7287.                         end
  7288.                     end
  7289.                 end
  7290.             end
  7291.             changeCoords("forward")
  7292.         end
  7293.     end
  7294. end
  7295.  
  7296. function forwardSafe(steps)
  7297.     local amount = 0
  7298.     local useSlot = 0
  7299.     local useItem = ""
  7300.     local success = false
  7301.     local dugItem = ""
  7302.     local dugSlot = 0
  7303.     local checkList = {}
  7304.     local startLevel = location:getY()
  7305.    
  7306.     checkList[1] = "wood"
  7307.     checkList[2] = "wood2"
  7308.     checkList[3] = "sand"
  7309.     checkList[4] = "item"
  7310.    
  7311.     saveToLog("forwardSafe("..steps..") steps started, checking fuel level", true)
  7312.     refuel(0)
  7313.     turtle.select(1)
  7314.     for i = 1, steps do --each step should move 1 block forward only
  7315.         checkDigDown() -- either goes downwards, digs sand or any unknown matching item
  7316.         if location:getY() ~= startLevel then --moved down
  7317.             while checkWater("forward") do
  7318.                 up(1)
  7319.             end
  7320.             startLevel = location:getY()
  7321.         end
  7322.         checkForLooseSaplings() -- pick up loose saplings
  7323.         if turtle.detect() then
  7324.             --see if block in front is known
  7325.             useSlot, amount, useItem = compareBlock("forward", checkList, 4)  --does block in front match wood,wood2,sand
  7326.             if useSlot > 0 then --block recognised, must be wood,wood2,sand, or itemX
  7327.                 if useItem =="wood" or useItem == "wood2" then
  7328.                     saveToLog("forwardSafe: Harvesting tree")
  7329.                     harvestTree(useItem)
  7330.                 elseif useItem == "sand" then
  7331.                     if turtle.getItemCount(slot:getItemSlot("sand")) < 8 then
  7332.                         dig.digNew{self = dig, direction = "forward", callFrom = "forwardSafe"}
  7333.                     else
  7334.                         checkSugarCane()
  7335.                     end
  7336.                 else --itemX
  7337.                     dig.digNew{self = dig, direction = "forward", checkForItems = "sand,gravel", callFrom = "forwardSafe"}
  7338.                 end
  7339.             else --block not recognised, - grass/stone/coal/leaves or not yet found sand, gravel, cobble, flowers, sugar cane
  7340.                 --if sugar cane dug, it scatters everywhere, so check first
  7341.                 dugItem = checkSugarCane() -- if dugItem = wood/wood2 then tree found, else unknown
  7342.                 isTreeInFront(dugItem)
  7343.             end
  7344.             --block may be in front, so go up until space in front
  7345.             --checkDigUp() --will clear blocks above and go up or harvest tree if found
  7346.             forward(1, 1, "sand,gravel", "forwardSafe")
  7347.         else --not blocked so move forward 1 step
  7348.             forward(1, 1, "sand,gravel", "forwardSafe")
  7349.             checkForLooseSaplings()--pick up loose saplings
  7350.         end
  7351.         --1 step forward now taken
  7352.         --detect trees on both sides
  7353.         turnRight(1)
  7354.         checkForLooseSaplings()
  7355.         if turtle.detect() then
  7356.             dugItem = checkSugarCane(true)
  7357.             isTreeInFront(dugItem)
  7358.         end
  7359.         turnLeft(2)
  7360.         checkForLooseSaplings()
  7361.         if turtle.detect() then
  7362.             dugItem = checkSugarCane(true)
  7363.             isTreeInFront(dugItem)
  7364.         end
  7365.         turnRight(1)
  7366.         saveToLog("forwardSafe step "..i.." completed. Coords: "..location:getX()..", "..location:getY()..", "..location:getZ()..", "..location:getCompass())
  7367.         if turtle.getFuelLevel() < 100 then
  7368.             refuel(0) --refuel chest in already checked position
  7369.         end
  7370.         --now facing forward, moved 1 block
  7371.         sleep(0.5) --stops random "Too long without yielding" errors
  7372.     end
  7373.     saveToLog("forwardSafe finished at "..textutils.formatTime(os.time(), true))
  7374. end
  7375.  
  7376. function getCobbleStock()
  7377.     local result = 0
  7378.    
  7379.     if slot:getItemSlot("cobble") > 0 then
  7380.         result = turtle.getItemCount(slot:getItemSlot("cobble")) - 1
  7381.     end
  7382.    
  7383.     return result
  7384. end
  7385.  
  7386. function getCoords()
  7387.     --get world coordinates from player
  7388.     local coord = 0
  7389.     local response = ""
  7390.     local continue = true
  7391.     local event = ""
  7392.     local param1 = ""
  7393.     local useIniFile = false
  7394.    
  7395.     term.clear()
  7396.     term.setCursorPos(1,1)
  7397.     if fso:iniFileExists() then --read preferences, so only one question asked before starting
  7398.         print("Current settings:")
  7399.         print()
  7400.         print("  Playing on a server       = "..tostring(fso:getOnServer()))
  7401.         print("  Create logfiles           = "..tostring(fso:getUseLog()))
  7402.         print("  Use verbose logfiles      = "..tostring(fso:getUseVerbose()))
  7403.         print("  Use pastebin for logfiles = "..tostring(fso:getUsePastebin()))
  7404.         print("  CC config file edited     = "..tostring(fso:getConfigModified()))
  7405.        
  7406.         print()
  7407.         print("Press E to edit")
  7408.         print()
  7409.         print("Any other key to continue")
  7410.         event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
  7411.         term.clear()
  7412.         term.setCursorPos(1,1)
  7413.         if param1 == "e" or param1 == "E" then -- if the returned value was 'e'
  7414.             fso:deleteIniFile()
  7415.         else
  7416.             useIniFile = true
  7417.         end
  7418.     end
  7419.     if not useIniFile then -- no prefs loaded from file, or file deleted
  7420.         fso:setOnServer(false)
  7421.         fso:setUsePastebin(false)
  7422.         fso:setConfigModified(false)
  7423.         fso:setUseLog(false)
  7424.        
  7425.         term.clear()
  7426.         term.setCursorPos(1,1)
  7427.         print(version)
  7428.         print()
  7429.         print("Are you playing on a server")
  7430.         print("where you cannot alter the")
  7431.         print("config files? Y/N")
  7432.         event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
  7433.         if param1 == "y" or param1 == "Y" then -- if the returned value was 'y'
  7434.             fso:setOnServer(true)
  7435.             term.clear()
  7436.             term.setCursorPos(1,1)
  7437.             print("Do you want to send logfiles")
  7438.             print("to your pastebin account? Y/N")
  7439.             event, param1 = os.pullEvent ("char")
  7440.             term.clear()
  7441.             term.setCursorPos(1,1)
  7442.             if param1 == "y" or param1 == "Y" then
  7443.                 print("Sorry, pastebin is work in progress")
  7444.                 print("No logfiles will be produced")
  7445.             else
  7446.                 print("No logfiles will be produced")
  7447.             end
  7448.             print()
  7449.             print("Preparing to start....")
  7450.             sleep(4)
  7451.             --break
  7452.         else --not on server
  7453.             if fs.getFreeSpace("/") < 9000000 then --config not modified
  7454.                 while continue do
  7455.                     continue = false
  7456.                     print()
  7457.                     print("CompterCraft.cfg file needs modifying")
  7458.                     print("to allow logfiles up to 6MB in size")
  7459.                     print("    Press H for help")
  7460.                     print("    Press N if config can't be edited")
  7461.                     print()
  7462.                     print("    Press Q to quit and edit")
  7463.                     event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
  7464.                     if param1 == "q" or param1 == "Q" then -- if the returned value was 'q'
  7465.                         term.clear()
  7466.                         term.setCursorPos(1,1)
  7467.                         print("Restart when config file has been changed")
  7468.                         error()
  7469.                     elseif param1 == "h" or param1 == "H" then -- if the returned value was 'h'
  7470.                         term.clear()
  7471.                         term.setCursorPos(1,1)
  7472.                         print("Open this file in notepad:")
  7473.                         print(".minecraft/config/ComputerCraft.cfg")
  7474.                         print("In 'general' section find:")
  7475.                         print("# The disk space limit for comput..")
  7476.                         print("Change the following line to:")
  7477.                         print("I:computerSpaceLimit=10000000")
  7478.                         print("# The disk space limit for floppy..")
  7479.                         print("Change the following line to:")
  7480.                         print("I:floppySpaceLimit=500000")
  7481.                         print("Save and re-start Minecraft")
  7482.                         print()
  7483.                         print("Press any key to continue")
  7484.                         event, param1 = os.pullEvent ("char")
  7485.                         continue = true
  7486.                     --elseif param1 == "n" or param1 == "N" then
  7487.                         --fso:setUseLog(false)
  7488.                         --fso:setConfigModified(false)
  7489.                     end
  7490.                 end
  7491.             else --config modified ask if logfiles required
  7492.                 fso:setConfigModified(true)
  7493.                 term.clear()
  7494.                 term.setCursorPos(1,1)
  7495.                 print("Logfile will be created automatically")
  7496.                 print()
  7497.                 print("Press 'N' to prevent logfiles")
  7498.                 print()
  7499.                 print("Press 'M' for minimal logfiles")
  7500.                 print()
  7501.                 print("Any other key for default (verbose)")
  7502.                 print()
  7503.                 event, param1 = os.pullEvent ("char")
  7504.                 if param1 == "n" or param1 == "N" then
  7505.                     print("logfiles will not be created")
  7506.                 elseif param1 == "m" or param1 == "M" then
  7507.                     fso:setUseVerbose(false)
  7508.                     fso:setUseLog(true)
  7509.                     print("minimal logfiles will be created")
  7510.                 else
  7511.                     fso:setUseVerbose(true)
  7512.                     fso:setUseLog(true)
  7513.                     print("verbose logfiles will be created")
  7514.                 end
  7515.                 sleep(2)
  7516.                 if fso:checkLogExists() then
  7517.                     term.clear()
  7518.                     term.setCursorPos(1,1)
  7519.                     print("Logfile(s) already exist")
  7520.                     print()
  7521.                     print("Press 'Y' to keep existing logs")
  7522.                     print("Any other key to delete logs")
  7523.                     event, param1 = os.pullEvent ("char")
  7524.                     if param1 == "y" or param1 == "Y" then
  7525.                         fso:renameLog()
  7526.                     else
  7527.                         fso:deleteLog()
  7528.                     end
  7529.                 end
  7530.             end
  7531.         end
  7532.     end
  7533.     term.clear()
  7534.     term.setCursorPos(1,1)
  7535.     print("IMPORTANT! Stand directly behind turtle")
  7536.     print("Press F3 to read coordinates")
  7537.     print()
  7538.     continue = true
  7539.     while continue do
  7540.         print("Please enter your X coordinate")
  7541.         write("  x = ")
  7542.         --term.setCursorPos(term.getCursorPos())
  7543.         coord = nil
  7544.         while coord == nil do
  7545.             coord = tonumber(read())
  7546.             if coord == nil then
  7547.                 term.clear()
  7548.                 term.setCursorPos(1,1)
  7549.                 print("Incorrect input. Use numbers only!")
  7550.                 print()
  7551.                 print("Please enter your X coordinate")
  7552.                 write("  x = ")
  7553.                 --term.setCursorPos(term.getCursorPos())
  7554.             end
  7555.         end
  7556.         location:setX(coord)
  7557.        
  7558.         term.clear()
  7559.         term.setCursorPos(1,1)
  7560.         print("Please enter your Y coordinate")
  7561.         write("  y = ")
  7562.         coord = nil
  7563.         while coord == nil do
  7564.             coord = tonumber(read())
  7565.             if coord == nil then
  7566.                 term.clear()
  7567.                 term.setCursorPos(1,1)
  7568.                 print("Incorrect input. Use numbers only")
  7569.                 print()
  7570.                 print("Please enter your y coordinate")
  7571.                 write("  y = ")
  7572.             end
  7573.         end
  7574.         location:setY(coord)
  7575.        
  7576.         term.clear()
  7577.         term.setCursorPos(1,1)
  7578.         print("Please enter your Z coordinate")
  7579.         write("  z = ")
  7580.         coord = nil
  7581.         while coord == nil do
  7582.             coord = tonumber(read())
  7583.             if coord == nil then
  7584.                 term.clear()
  7585.                 term.setCursorPos(1,1)
  7586.                 print("Incorrect input. Use numbers only")
  7587.                 print()
  7588.                 print("Please enter your z coordinate")
  7589.                 write("  z = ")
  7590.             end
  7591.         end
  7592.         location:setZ(coord)
  7593.         response = true
  7594.         while response do
  7595.             response = false
  7596.             term.clear()
  7597.             term.setCursorPos(1,1)
  7598.             print("Enter Direction you are facing:")
  7599.             print("  0,1,2,3 (s,w,n,e)")
  7600.             print()
  7601.             print(  "  Direction = ")
  7602.             event, param1 = os.pullEvent ("char")
  7603.             --response = read()
  7604.             if param1 == "s" or param1 == "S" then
  7605.                 coord = 0
  7606.             elseif param1 == "w" or param1 == "W" then
  7607.                 coord = 1
  7608.             elseif param1 == "n" or param1 == "N" then
  7609.                 coord = 2
  7610.             elseif param1 == "e" or param1 == "E" then
  7611.                 coord = 3
  7612.             elseif param1 == "0" or param1 == "1" or param1 == "2" or param1 == "3" then
  7613.                 coord = tonumber(param1)
  7614.             else
  7615.                 print()
  7616.                 print("Incorrect input: "..param1)
  7617.                 print()
  7618.                 print("Use 0,1,2,3,n,s,w,e")
  7619.                 sleep(2)
  7620.                 response = true
  7621.             end
  7622.         end
  7623.         location:setFacing(coord)
  7624.         term.clear()
  7625.         term.setCursorPos(1,1)
  7626.         print("Your current location is:")
  7627.         print()
  7628.         print("  x = "..location:getX())
  7629.         print("  y = "..location:getY())
  7630.         print("  z = "..location:getZ())
  7631.         print("  facing "..location:getCompass().." ("..location:getFacing()..")")
  7632.         print()
  7633.         write("Is this correct? (y/n)")
  7634.         event, param1 = os.pullEvent ("char")
  7635.         if param1 == "y" or param1 == "Y" then
  7636.             continue = false
  7637.         end
  7638.         term.clear()
  7639.         term.setCursorPos(1,1)
  7640.     end
  7641.    
  7642.     --[[ correct coords to compensate for player standing position
  7643.     facing:     Change:
  7644.     0 (S)       z+1
  7645.     1 (W)       x-1
  7646.     2 (N)       z-1
  7647.     3 (E)       x+1
  7648.     ]]--
  7649.  
  7650.     if location:getFacing() == 0 then
  7651.         location:setZ(location:getZ() + 1)
  7652.     elseif location:getFacing() == 1 then
  7653.         location:setX(location:getX() - 1)
  7654.     elseif location:getFacing() == 2 then
  7655.         location:setZ(location:getZ() - 1)
  7656.     elseif location:getFacing() == 3 then
  7657.         location:setX(location:getX() + 1)
  7658.     end
  7659.     coordHome:setX(location:getX())
  7660.     coordHome:setY(location:getY())
  7661.     coordHome:setZ(location:getZ())
  7662.     coordHome:setFacing(location:getFacing())
  7663.     startTime = os.time()
  7664.     saveToLog("Starting replication at x="..location:getX().." y="..location:getY().." z="..location:getZ().." facing "..location:getCompass(), true)
  7665. end
  7666.  
  7667. function getDirtStock()
  7668.     local result = 0
  7669.    
  7670.     if slot:getItemSlot("dirt") > 0 then
  7671.         result = turtle.getItemCount(slot:getItemSlot("dirt")) - 1
  7672.     end
  7673.    
  7674.     return result
  7675. end
  7676.  
  7677. function getFirstEmptySlot(doSort)
  7678.     local checkSlot = 0
  7679.     local lastFullSlot = 0
  7680.    
  7681.     if doSort == nil then
  7682.         doSort = false
  7683.     end
  7684.    
  7685.     for i = 1, 16 do
  7686.         if turtle.getItemCount(i) == 0 then
  7687.             if checkSlot == 0 then
  7688.                 checkSlot = i
  7689.             end
  7690.         else
  7691.             lastFullSlot = i
  7692.         end
  7693.     end
  7694.  
  7695.     if lastFullSlot + 1 ~= checkSlot and doSort then
  7696.         saveToLog("getFirstEmptySlot: first empty = "..checkSlot.." lastFullSlot = "..lastFullSlot)
  7697.         sortInventory(true)
  7698.         checkSlot = 0
  7699.         for i = 1, 16 do
  7700.             if turtle.getItemCount(i) == 0 then
  7701.                 if checkSlot == 0 then
  7702.                     checkSlot = i
  7703.                     break
  7704.                 end
  7705.             end
  7706.         end
  7707.     end
  7708.    
  7709.     return checkSlot
  7710. end
  7711.  
  7712. function getEmptyStorage()
  7713.     local emptyStore = {}
  7714.     local firstEmptyBlock = 0
  7715.    
  7716.     for k = 1, 9, 2 do
  7717.         if k == 1 then
  7718.             emptyStore = extraStorage1
  7719.         elseif k == 3 then
  7720.             emptyStore = extraStorage2
  7721.         elseif k == 5 then
  7722.             emptyStore = extraStorage3
  7723.         elseif k == 7 then
  7724.             emptyStore = extraStorage4
  7725.         elseif k == 9 then
  7726.             emptyStore = extraStorage5
  7727.         end
  7728.         if emptyStore:getStoreContains() == "" then
  7729.             firstEmptyBlock = k
  7730.             break
  7731.         end
  7732.     end
  7733.    
  7734.     return firstEmptyBlock, emptyStore
  7735. end
  7736.  
  7737. function getFuelAvailable()
  7738.     local tFuelAvailable = 0
  7739.     local fAvailableCoal = 0
  7740.     local fAvailableCharcoal = 0
  7741.     local fAvailablePlanks = 0
  7742.     local fAvailablePlanks2 = 0
  7743.     local fAvailableWood = 0
  7744.     local fAvailableWood2 = 0
  7745.     local fFuelAvailableAllWood = 0
  7746.     local result = {}
  7747.    
  7748.     if slot:getItemSlot("coal") > 0 then
  7749.         fAvailableCoal = (turtle.getItemCount(slot:getItemSlot("coal")) - 1) * 80
  7750.     end
  7751.     if slot:getItemSlot("charcoal") > 0 then
  7752.         fAvailableCharcoal = (turtle.getItemCount(slot:getItemSlot("charcoal"))) * 80
  7753.     end
  7754.     if slot:getItemSlot("planks") > 0 then
  7755.         fAvailablePlanks = (turtle.getItemCount(slot:getItemSlot("planks"))) * 15
  7756.     end
  7757.     if slot:getItemSlot("planks2") > 0 then
  7758.         fAvailablePlanks2 = (turtle.getItemCount(slot:getItemSlot("planks2"))) * 15
  7759.     end
  7760.     if slot:getItemSlot("wood") > 0 then
  7761.         fAvailableWood = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 60
  7762.     end
  7763.     if slot:getItemSlot("wood2") > 0 then
  7764.         fAvailableWood2 = (turtle.getItemCount(slot:getItemSlot("wood2")) - 1) * 60
  7765.     end
  7766.     fFuelAvailableAllWood = fAvailableWood + fAvailableWood2
  7767.     tFuelAvailable = fAvailableCoal + fAvailableCharcoal + fAvailablePlanks + fAvailablePlanks2 + fAvailableWood + fAvailableWood2
  7768.     saveToLog("getFuelAvailable: coal = "..fAvailableCoal..
  7769.                 " charcoal = "..fAvailableCharcoal..
  7770.                 " planks = "..fAvailablePlanks..
  7771.                 " planks2 = "..fAvailablePlanks2..
  7772.                 " wood = "..fAvailableWood..
  7773.                 " wood2 = "..fAvailableWood2, false, true)
  7774.    
  7775.     result = {totalFuelAvailable = tFuelAvailable,
  7776.                     fuelAvailableCoal = fAvailableCoal,
  7777.                     fuelAvailableCharcoal = fAvailableCharcoal,
  7778.                     fuelAvailablePlanks = fAvailablePlanks,
  7779.                     fuelAvailablePlanks2 = fAvailablePlanks2,
  7780.                     fuelAvailableWood = fAvailableWood,
  7781.                     fuelAvailableWood2 = fAvailableWood2,
  7782.                     fuelAvailableAllWood = fFuelAvailableAllWood}
  7783.    
  7784.     return result
  7785. end
  7786.  
  7787. function getItemsFromStorage(arg)
  7788.     -- getItemsFromStorage{fromStore = storageIronore, item1 = "ironore", item2 = "buckets"}
  7789.     -- getItemsFromStorage{fromStore = storageTorches, itemList = getList} -- eg 'sticks,torches'
  7790.     -- getItemsFromStorage{fromStore = storagePickaxe, item1 = "diamond pickaxe"} -- eg 'sticks,torches'
  7791.     local itemInStorage = false
  7792.     local storeSlot = 0
  7793.     local storeCount = 0
  7794.     local storedItem = ""
  7795.     local emptySlot = 0
  7796.     local keepItems = {}
  7797.     local returnItems = {}
  7798.     local numItems = -1
  7799.     local logText = ""
  7800.     local tempChestItem = {}
  7801.     local tempIndex = 0
  7802.     local useCraftingChest = false
  7803.     local keepThis = false
  7804.     local useItem = ""
  7805.     local existingItem = false
  7806.     local start = 0
  7807.     local length = 0
  7808.     --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
  7809.     --when required items are retrieved, put excess back into storage chest
  7810.     if arg.itemList ~= nil then
  7811.         arg.item1 = arg.itemList[1]
  7812.         arg.item2 = arg.itemList[2]
  7813.         arg.item3 = arg.itemList[3]
  7814.         saveToLog("getItemsFromStorage: itemList[1] = "..tostring(arg.itemList[1]).." itemList[2] = "..tostring(arg.itemList[2]).." itemList[3] = "..tostring(arg.itemList[3]))
  7815.     else
  7816.         -- get no of items
  7817.         for key, value in pairs(arg) do
  7818.             if key == "fromStore" then
  7819.                 saveToLog("getItemsFromStorage: key = "..key.." value = "..value.getStoreName(value), false)
  7820.             else
  7821.                 saveToLog("getItemsFromStorage: key = "..key.." value = "..tostring(value), false)
  7822.             end
  7823.             --key = "fromStore", value = storageTorches
  7824.             --key = "item1", value = "sticks"
  7825.             --key = "itemList", value = ',sticks,torches'
  7826.             numItems = numItems + 1 --starts at -1 minimum 2 args
  7827.         end
  7828.     end
  7829.     keepItems[arg.item1] = 0 --default value of return
  7830.     if arg.item2 ~= nil then
  7831.         keepItems[arg.item2] = 0
  7832.     end
  7833.     if arg.item3 ~= nil then
  7834.         keepItems[arg.item3] = 0
  7835.     end
  7836.     saveToLog("getItemsFromStorage: checking for "..numItems.." items out of "..arg.fromStore.getNoOfItems(arg.fromStore))
  7837.     storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item1)
  7838.     --check if items needed are in storage
  7839.     if storeSlot > 0 then
  7840.         storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
  7841.         saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item1.." in storage slot "..storeSlot)
  7842.         itemInStorage = true
  7843.     end
  7844.     if arg.item2 ~= nil and arg.item2 ~= "" then
  7845.         storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item2)
  7846.         if storeSlot > 0 then
  7847.             storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
  7848.             saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item2.." in storage slot "..storeSlot)
  7849.             itemInStorage = true
  7850.         end
  7851.     end
  7852.     if arg.item3 ~= nil and arg.item3 ~= "" then
  7853.         storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item3)
  7854.         if storeSlot > 0 then
  7855.             storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
  7856.             saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item3.." in storage slot "..storeSlot)
  7857.             itemInStorage = true
  7858.         end
  7859.     end
  7860.     if itemInStorage then
  7861.         emptySlot = 16
  7862.         --place crafting chest if needed
  7863.         if arg.fromStore.getNoOfItems(arg.fromStore) > getNoOfEmptySlots() then
  7864.             turtle.select(slot.getItemSlot(slot, "chests"))
  7865.             turtle.place()
  7866.             saveToLog("getItemsFromStorage: crafting chest placed ready for "..arg.fromStore.getNoOfItems(arg.fromStore).." items")
  7867.             useCraftingChest = true
  7868.         else
  7869.             saveToLog("getItemsFromStorage: crafting chest not required ("..arg.fromStore.getNoOfItems(arg.fromStore)..") items in storage")
  7870.         end
  7871.         for i = 1, 16 do
  7872.             --use storeContains NOT slotContains
  7873.             storedItem = arg.fromStore:getSlotContains(i) --eg "sticks"
  7874.             if arg.item1 == storedItem or arg.item2 == storedItem  or arg.item3 == storedItem then
  7875.                 keepThis = true
  7876.             else
  7877.                 keepThis = false
  7878.             end
  7879.             storeCount = arg.fromStore:getSlotCount(i)  --eg 1
  7880.             if storeCount == nil then
  7881.                 storeCount = 0
  7882.             end
  7883.             if storeCount > 0 then
  7884.                 saveToLog("getItemsFromStorage: found "..storeCount.." of "..storedItem.." in storage")
  7885.                 existingItem = false
  7886.                 if slot:getItemSlot(storedItem) > 0 then --item already in turtle
  7887.                     emptySlot = slot:getItemSlot(storedItem)
  7888.                     keepThis = true
  7889.                     existingItem = true
  7890.                 end
  7891.                 turtle.select(emptySlot)
  7892.                 saveToLog("getItemsFromStorage: Removing "..storeCount.." "..storedItem.." from  storage chest into turtle slot "..emptySlot)
  7893.                 if turtle.suckDown() then --removed item from storage
  7894.                     saveToLog("getItemsFromStorage: removed "..turtle.getItemCount(emptySlot).." "..storedItem)
  7895.                     if keepThis then
  7896.                         if not existingItem then
  7897.                             keepItems[storedItem] = turtle.getItemCount(emptySlot)
  7898.                             saveToLog("getItemsFromStorage: "..keepItems[storedItem].." of "..storedItem.." retrieved. Updating slot")
  7899.                             emptySlot = getFirstEmptySlot(false)
  7900.                             if emptySlot > 0 then --turtle not full
  7901.                                 turtle.select(16)
  7902.                                 turtle.transferTo(emptySlot)
  7903.                             else
  7904.                                 emptySlot = 16
  7905.                             end
  7906.                         end
  7907.                         slot.update{self = slot, slotNo = emptySlot, item = storedItem}
  7908.                     else --not required, place in crafting chest if present
  7909.                         if useCraftingChest then
  7910.                             tempIndex = tempIndex + 1
  7911.                             fillChest{direction = "forward", addItem = storedItem, addAmount = storeCount, fromSlot = 16, originalSlot = tempIndex, deleteFromTurtle = false}
  7912.                         else
  7913.                             tempIndex = tempIndex + 1 --started at 0, so first =1
  7914.                             returnItems[tempIndex] = storedItem --returnItems[1] = "iron"
  7915.                             saveToLog("getItemsFromStorage: "..returnItems[tempIndex].." retrieved. Updating slot before returning to storage")
  7916.                             emptySlot = getFirstEmptySlot(false)
  7917.                             if emptySlot > 0 then --turtle not full
  7918.                                 turtle.select(16)
  7919.                                 turtle.transferTo(emptySlot)
  7920.                             else
  7921.                                 emptySlot = 16
  7922.                             end
  7923.                             slot.update{self = slot, slotNo = emptySlot, item = storedItem}
  7924.                         end
  7925.                     end
  7926.                     emptySlot = 16
  7927.                 end
  7928.             else
  7929.                 break
  7930.             end
  7931.         end
  7932.         --reset storage variables
  7933.         saveToLog("getItemsFromStorage: resetting storage object", false)
  7934.         arg.fromStore.resetVariables(arg.fromStore)
  7935.         -- now remove items from temp chest/turtle back into storage
  7936.         if tempIndex > 0 then
  7937.             if useCraftingChest then
  7938.                 for i = 1, tempIndex do
  7939.                     useItem = craftingChest:getSlotContains(i) --eg "dirt", "item"
  7940.                     turtle.select(16)
  7941.                     if turtle.suck() then --item removed from crafting chest into slot 16
  7942.                         saveToLog("getItemsFromStorage: returning "..useItem.." to ".. arg.fromStore:getStoreName())
  7943.                         storeItem{toStore = arg.fromStore, item = useItem, slotNo = 16, quantity = turtle.getItemCount(16)}
  7944.                     else
  7945.                         break
  7946.                     end
  7947.                 end
  7948.                 --reset chest variables
  7949.                 craftingChest:resetVariables()
  7950.                 --dig chest back. may not be a chest onboard
  7951.                 dig.digNew{self = dig, direction = "forward", expectedItem = "chests", callFrom = "getItemsFromStorage"}
  7952.             else
  7953.                 for i = 1, tempIndex do
  7954.                     saveToLog("getItemsFromStorage: returning "..returnItems[i].." to "..arg.fromStore:getStoreName())
  7955.                     storeItem{toStore = arg.fromStore, item = returnItems[i], quantity = 0, updateSlot = true, doSort = true}
  7956.                 end
  7957.             end
  7958.         end
  7959.         sortInventory(true)
  7960.     else
  7961.         saveToLog("getItemsFromStorage: itemInStorage = false")
  7962.     end
  7963.    
  7964.     return keepItems
  7965. end
  7966.  
  7967. function getItemsNeeded(stage)
  7968.     --itemsNeeded, numNeeded = getItemsNeeded(3)
  7969.     local needed = {}
  7970.     local index = 0
  7971.    
  7972.     if stage == 1 then
  7973.         if slot:getItemSlot("dirt") == 0 then
  7974.             index = index + 1
  7975.             needed[index] = "dirt"
  7976.         end
  7977.         if slot:getItemSlot("cobble") == 0 then
  7978.             index = index + 1
  7979.             needed[index] = "cobble"
  7980.         end
  7981.         if not objectives["ironore"] then
  7982.             index = index + 1
  7983.             needed[index] = "ironore"
  7984.         end
  7985.         if not objectives["gravel"] then
  7986.             index = index + 1
  7987.             needed[index] = "gravel"
  7988.         end
  7989.         if not objectives["sand"] then
  7990.             index = index + 1
  7991.             needed[index] = "sand"
  7992.         end
  7993.         if slot:getItemSlot("coal") == 0 then
  7994.             index = index + 1
  7995.             needed[index] = "coal"
  7996.         end
  7997.     end
  7998.     if stage == 2 then
  7999.         if slot:getItemSlot("coal") == 0 then
  8000.             index = index + 1
  8001.             needed[index] = "coal"
  8002.         end
  8003.         if not objectives["ironore"] then
  8004.             index = index + 1
  8005.             needed[index] = "ironore"
  8006.         end
  8007.     end
  8008.     if stage == 3 then
  8009.         --[[if not objectives["goldore"] then
  8010.             index = index + 1
  8011.             needed[index] = "goldore"
  8012.         end]]--
  8013.         if not objectives["redstone"] then
  8014.             index = index + 1
  8015.             needed[index] = "redstone"
  8016.         end
  8017.         if not objectives["lapis"] then
  8018.             index = index + 1
  8019.             needed[index] = "lapis"
  8020.         end
  8021.         if not objectives["diamonds"] then
  8022.             index = index + 1
  8023.             needed[index] = "diamonds"
  8024.         end
  8025.     end
  8026.    
  8027.     return needed, index  --eg {"coal", "ironore"}, 2
  8028. end
  8029.  
  8030. function getNoOfEmptySlots(excludeTorches)
  8031.     local result = 0
  8032.    
  8033.     for i = 1, 16 do
  8034.         if turtle.getItemCount(i) == 0 then
  8035.             result = result + 1
  8036.         end
  8037.     end
  8038.     if excludeTorches then
  8039.         if slot:getItemSlot("torches") > 0 then
  8040.             result = result - 1
  8041.         end
  8042.         if slot:getItemSlot("signs") > 0 then
  8043.             result = result - 1
  8044.         end
  8045.     end
  8046.    
  8047.     return result
  8048. end
  8049.  
  8050. function getPlanksAvailable(checkPlanksType)
  8051.     local planksAvailable = 0
  8052.     local planks2Available = 0
  8053.     local planksType = "planks"
  8054.    
  8055.     if checkPlanksType == nil then
  8056.         checkPlanksType = ""
  8057.     end
  8058.    
  8059.     if slot:getItemSlot("planks") > 0 then
  8060.         planksAvailable = turtle.getItemCount(slot:getItemSlot("planks"))
  8061.     end
  8062.     if slot:getItemSlot("planks2") > 0 then
  8063.         planks2Available = turtle.getItemCount(slot:getItemSlot("planks2"))
  8064.     end
  8065.     if checkPlanksType == "planks" then
  8066.         planksType = "planks"
  8067.     elseif checkPlanksType == "planks2" then
  8068.         planksType = "planks2"
  8069.         planksAvailable = planks2Available
  8070.     else
  8071.         if planks2Available > planksAvailable then
  8072.             planksType = "planks2"
  8073.             planksAvailable = planks2Available
  8074.         end
  8075.     end
  8076.     -- return amount available if type specified
  8077.     -- else return planks type largest amount
  8078.     return planksAvailable, planksType
  8079. end
  8080.  
  8081. function getItemsAvailable(itemName, recover)
  8082.     -- getItemsAvailable("signs", true)
  8083.     local onBoard = 0
  8084.     local inStore = 0
  8085.    
  8086.     recover = recover or false
  8087.    
  8088.     if itemName == "torches" then
  8089.         if slot:getItemSlot("torches") > 0 then
  8090.             onBoard = turtle.getItemCount(slot:getItemSlot("torches"))
  8091.         end
  8092.         inStore = storageTorches:getItemCount("torches")
  8093.         if recover and inStore > 0 then
  8094.             inStore = 0
  8095.             getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
  8096.             if slot:getItemSlot("torches") > 0 then
  8097.                 onBoard = turtle.getItemCount(slot:getItemSlot("torches"))
  8098.             end
  8099.         end
  8100.     elseif itemName == "signs" then
  8101.         if slot:getItemSlot("signs") > 0 then
  8102.             onBoard = turtle.getItemCount(slot:getItemSlot("signs"))
  8103.         end
  8104.         inStore = storageSigns:getItemCount("signs")
  8105.         if recover and inStore > 0 then
  8106.             forward(2, 1)
  8107.             turnRight(2)
  8108.             inStore = 0
  8109.             getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
  8110.             if slot:getItemSlot("signs") > 0 then
  8111.                 onBoard = turtle.getItemCount(slot:getItemSlot("signs"))
  8112.             end
  8113.             forward(2, 1)
  8114.             turnRight(2)
  8115.         end
  8116.     elseif itemName == "sticks" then
  8117.         if slot:getItemSlot("sticks") > 0 then
  8118.             onBoard = turtle.getItemCount(slot:getItemSlot("sticks"))
  8119.         end
  8120.         inStore = storageSticks:getItemCount("sticks")
  8121.         if recover and inStore > 0 then
  8122.             forward(4, 1)
  8123.             turnRight(2)
  8124.             inStore = 0
  8125.             getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
  8126.             if slot:getItemSlot("sticks") > 0 then
  8127.                 onBoard = turtle.getItemCount(slot:getItemSlot("sticks"))
  8128.             end
  8129.             forward(4, 1)
  8130.             turnRight(2)
  8131.         end
  8132.     end
  8133.  
  8134.     return onBoard + inStore
  8135. end
  8136.  
  8137. function getWoodAvailable(arg)
  8138.     local woodAvailable = 0
  8139.     local wood2Available = 0
  8140.     local mostWoodType = "none"
  8141.    
  8142.     -- getWoodAvailable{woodNeeded = 2} --checks if at least 2 wood of any type available
  8143.     arg.checkWoodType = arg.checkWoodType or "any"
  8144.     arg.woodNeeded = arg.woodNeeded or 0
  8145.    
  8146.     if slot:getItemSlot("wood") > 0 then --wood onboard
  8147.         woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 1
  8148.         if slot:getItemSlot("wood2") > 0 then
  8149.             wood2Available = turtle.getItemCount(slot:getItemSlot("wood2")) - 1
  8150.         end
  8151.         if arg.woodNeeded > 0 then --checking if enough wood for the purpose
  8152.             if arg.checkWoodType == "any" then
  8153.                 if woodAvailable >= arg.woodNeeded then
  8154.                     mostWoodType = "wood"
  8155.                 else
  8156.                     if wood2Available >= arg.woodNeeded then
  8157.                         mostWoodType = "wood2"
  8158.                     end
  8159.                 end
  8160.             elseif arg.checkWoodType == "wood" then --check specific wood
  8161.                 if woodAvailable >= arg.woodNeeded then
  8162.                     mostWoodType = "wood"
  8163.                 end
  8164.             elseif arg.checkWoodType == "wood2" then --check specific wood2
  8165.                 if wood2Available >= arg.woodNeeded then
  8166.                     mostWoodType = "wood2"
  8167.                 end
  8168.             end
  8169.         else --no specific amount required
  8170.             if arg.checkWoodType == "wood" then
  8171.                 if woodAvailable >= 1 then
  8172.                     mostWoodType = "wood"
  8173.                 end
  8174.             elseif arg.checkWoodType == "wood2" then
  8175.                 if wood2Available >= 1 then
  8176.                     mostWoodType = "wood2"
  8177.                 end
  8178.             else --woodType not specified
  8179.                 if woodAvailable >= 1 then
  8180.                     mostWoodType = "wood"
  8181.                 end
  8182.                 if wood2Available > woodAvailable then
  8183.                     mostWoodType = "wood2"
  8184.                 end
  8185.             end
  8186.         end
  8187.     else --wood may be in storageWood
  8188.         if  storageWood:getItemSlot("wood") > 0 then --wood in store
  8189.             mostWoodType = "store"
  8190.             woodAvailable = storageWood:getItemCount("wood")
  8191.         else
  8192.             mostWoodType = "none"
  8193.         end
  8194.     end
  8195.    
  8196.     return mostWoodType, woodAvailable, wood2Available -- ("none","wood","wood2", "store"), X, 0
  8197. end
  8198.  
  8199. function goMining1(direction, fuelLevel, mineExitY)
  8200.     local continue = true
  8201.     local numNeeded = 0
  8202.     local itemsNeeded = {}
  8203.     local success = false
  8204.     local inWater = false
  8205.     local useSlot = 0
  8206.     local itemList = ""
  8207.     local dugItem = ""
  8208.     local dugSlot = 0
  8209.    
  8210.     objectives["goMining1"] = true
  8211.     saveToLog("goMining1 total fuel available = "..fuelLevel, true, false)
  8212.     if fuelLevel > 100 then
  8213.         yCoordMine = location:getY()
  8214.         itemsNeeded, numNeeded = getItemsNeeded(1) --eg {"gravel","coal"}, 2
  8215.         if numNeeded > 0 then
  8216.             for i = 1, numNeeded do
  8217.                 if i == 1 then
  8218.                     itemList = itemList.."'"..itemsNeeded[i].."'"
  8219.                 else
  8220.                     itemList = itemList..",'"..itemsNeeded[i].."'"
  8221.                 end
  8222.                 saveToLog("First stage mining, item "..i.." needed = "..itemsNeeded[i])
  8223.             end
  8224.         else
  8225.             saveToLog("First stage mining, no items needed")
  8226.         end
  8227.         --eg itemList = 'gravel','coal','ironore'
  8228.         saveToLog("goMining1: going down...")
  8229.         --start at ground level
  8230.         for i = 1, 2 do
  8231.             dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "goMining1"}
  8232.             --could be on corner next to water, and water now pouring in
  8233.             down(1, 1)
  8234.             for i = 1, 4 do
  8235.                 turtle.select(slot:getItemSlot("dirt"))
  8236.                 if not turtle.detect() then --air or water next door
  8237.                     turtle.place()
  8238.                 else --check if dirt
  8239.                     if not turtle.compare() then --not dirt in front
  8240.                         turtle.select(slot:getItemSlot("cobble"))
  8241.                         if not turtle.compare() then --not cobble in front
  8242.                             dig.digNew{self = dig, checkForItems = itemList, callFrom = "goMining1"}
  8243.                             turtle.select(slot:getItemSlot("dirt"))
  8244.                             turtle.place()
  8245.                         end
  8246.                     end
  8247.                 end
  8248.                 turnRight(1)
  8249.             end
  8250.         end
  8251.         turtle.select(slot:getItemSlot("cobble"))
  8252.         turtle.placeUp()
  8253.         dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "goMining1"}
  8254.         down(1, 1, itemList, "goMining1")
  8255.         turtle.select(slot:getItemSlot("dirt"))
  8256.         turtle.placeUp()
  8257.         -- go down to level 40 only
  8258.         -- need to have or find: dirt, cobble, (stone), gravel, sand, coal, iron ore, gold ore
  8259.         refuel(0)
  8260.         --repair damage after refuel
  8261.         for i = 1, 4 do
  8262.             turtle.select(slot:getItemSlot("cobble"))
  8263.             if not turtle.detect() then --air or water next door
  8264.                 turtle.place()
  8265.             end
  8266.             turnRight(1)
  8267.         end
  8268.         saveToLog("Commencing mining run")
  8269.         --sortInventory(true)
  8270.        
  8271.         mineDown(40, itemList, "goMining1") --mine down to level 40
  8272.         sortInventory(true)
  8273.         saveToLog("goMining1: lastNewItemSlot = "..slot.getMostUnknownItemSlot(slot)..", firstNewItemSlot = "..slot.getLeastUnknownItemSlot(slot)..", numNeeded = "..numNeeded)
  8274.         repositionUnderground("FFF", itemList, "goMining1")
  8275.         saveToLog("goMining1: going up...", false, true)
  8276.         --mineUp(yCoordMine - 5, itemList, "goMining1")
  8277.         mineUp(mineExitY, itemList, "goMining1")
  8278.         success = true
  8279.     else
  8280.         saveToLog("goMining1: insufficient fuel. Abandoning mining run")
  8281.     end
  8282.    
  8283.     return success
  8284. end
  8285.  
  8286. function goMining2(level, sector)
  8287.     -- start top middle, go back towards first tree, do top right quarter
  8288.     -- start top middle, go back towards first tree, do top left quarter
  8289.     -- etc, return to base to replant tree farm/ process materials/ reset torch
  8290.     local continue = true
  8291.     local numNeeded = 0
  8292.     local itemsNeeded = {}
  8293.     local turnDirection = ""
  8294.     local itemList = ""
  8295.     local digSuccess = false
  8296.     local digItem = ""
  8297.     local digSlot = 0
  8298.     local sTime = os.time()
  8299.    
  8300.     --[[
  8301.     4|3|1|2
  8302.     -|-|-|-
  8303.     8|7|5|6
  8304.     ]]--
  8305.     --need to find redstone, lapis, (obsidian), diamond, emerald
  8306.     --go down to level 15, dig 16X8 rectangle, by digging down on 14, digging forward on 15, placing ceiling on 16
  8307.     itemsNeeded, numNeeded = getItemsNeeded(3) --eg {"diamond","redstone"}, 2
  8308.     saveToLog("goMining2: no. items needed = "..numNeeded)
  8309.     if numNeeded > 0 then
  8310.         for i = 1, numNeeded do
  8311.             if i == 1 then
  8312.                 itemList = itemList.."'"..tostring(itemsNeeded[i]).."'"
  8313.             else
  8314.                 itemList = itemList..",'"..tostring(itemsNeeded[i]).."'"
  8315.             end
  8316.             saveToLog("Third stage mining, item "..i.." needed = "..itemsNeeded[i])
  8317.         end
  8318.     end
  8319.     while location:getY() > 14 do
  8320.         down(1, 1, itemList)
  8321.     end
  8322.     forward(16, 1)
  8323.     if level == 2 then
  8324.         down(3, 1, itemList)
  8325.     elseif level == 3 then
  8326.         down(6, 1, itemList)
  8327.     end
  8328.    
  8329.     if sector == 1 then
  8330.         turnLeft(1)
  8331.         forward(1)
  8332.         turnLeft(1)
  8333.         turnDirection = "right"
  8334.     elseif sector == 2 then
  8335.         turnLeft(1)
  8336.         forward(9, 1)
  8337.         turnLeft(1)
  8338.         turnDirection = "right"
  8339.     elseif sector == 3 then
  8340.         turnRight(1)
  8341.         forward(1)
  8342.         turnRight(1)
  8343.         turnDirection = "left"
  8344.     elseif sector == 4 then
  8345.         turnRight(1)
  8346.         forward(9)
  8347.         turnRight(1)
  8348.         turnDirection = "left"
  8349.     elseif sector == 5 then
  8350.         turnLeft(1)
  8351.         forward(1)
  8352.         turnRight(1)
  8353.         turnDirection = "left"
  8354.     elseif sector == 6 then
  8355.         turnLeft(1)
  8356.         forward(9, 1)
  8357.         turnRight(1)
  8358.         turnDirection = "left"
  8359.     elseif sector == 7 then
  8360.         turnRight(1)
  8361.         forward(1)
  8362.         turnLeft(1)
  8363.         turnDirection = "right"
  8364.     elseif sector == 8 then
  8365.         turnRight(1)
  8366.         forward(9, 1)
  8367.         turnLeft(1)
  8368.         turnDirection = "right"
  8369.     end
  8370.     --eg itemList = 'redstone','ironore','diamonds'
  8371.     -- now dig 8 x 16 rectangle
  8372.     for sq = 1, 8 do
  8373.         refuel(0)
  8374.         for i = 1, 16 do
  8375.             if turtle.detectUp() then --block above
  8376.                 if isValuable("up") then --could be useful, so dig and replace with cobble
  8377.                     --digSuccess, digItem, digSlot = dig{direction = "up", checkForItems = itemList}
  8378.                     while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "goMining2"} do --allow for gravel
  8379.                         sleep(.5)
  8380.                         if not turtle.detectUp() then --water or lava falling down
  8381.                             break
  8382.                         end
  8383.                     end
  8384.                     turtle.select(slot:getItemSlot("cobble"))
  8385.                     if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  8386.                         turtle.placeUp()
  8387.                     end
  8388.                 end
  8389.             else --no block above or lava/water, so place cobble
  8390.                 turtle.select(slot:getItemSlot("cobble"))
  8391.                 if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  8392.                     turtle.placeUp()
  8393.                 end
  8394.             end
  8395.             if turtle.detectDown() then
  8396.                 dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "goMining2"}
  8397.             else --nothing below could be air, water, lava
  8398.                 if slot:getItemSlot("buckets") > 0 then
  8399.                     turtle.select(slot:getItemSlot("buckets"))
  8400.                     if turtle.placeDown() then --bucket filled water or lava
  8401.                         if not turtle.refuel() then --water
  8402.                             turtle.placeDown() --empty bucket out
  8403.                         end
  8404.                     end
  8405.                 end
  8406.             end
  8407.             if sq == 8 and (i == 9 or i == 1) then
  8408.                 if slot:getItemCount("torches") > 1 then
  8409.                     turtle.select(slot:getItemSlot("torches"))
  8410.                     if not turtle.placeDown() then
  8411.                         turtle.select(slot:getItemSlot("cobble"))
  8412.                         if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  8413.                             down(1)
  8414.                             turtle.placeDown()
  8415.                             up(1)
  8416.                             turtle.select(slot:getItemSlot("torches"))
  8417.                             turtle.placeDown()
  8418.                         end
  8419.                     end
  8420.                 end
  8421.             end
  8422.             if turtle.detect() then --dig and drop non-valuables
  8423.                 while dig.digNew{self = dig, checkForItems = itemList, callFrom = "goMining2"} do
  8424.                     sleep(.5)
  8425.                     if not turtle.detect() then --water or lava falling down
  8426.                         break
  8427.                     end
  8428.                 end
  8429.             end
  8430.             forward(1, 1, itemList)
  8431.         end
  8432.         if sq < 8 then
  8433.             if turnDirection == "right" then
  8434.                 turnRight(1)
  8435.                 mineForward(1, true, itemList)
  8436.                 turnRight(1)
  8437.                 turnDirection = "left"
  8438.             else
  8439.                 turnLeft(1)
  8440.                 mineForward(1, true, itemList)
  8441.                 turnLeft(1)
  8442.                 turnDirection = "right"
  8443.             end
  8444.         else
  8445.             if sector == 1 then
  8446.                 turnRight(1)
  8447.                 forward(8, 1)
  8448.                 turnRight(1)
  8449.             elseif sector == 2 then
  8450.                 turnRight(1)
  8451.                 forward(16, 1)
  8452.                 turnRight(1)
  8453.             elseif sector == 3 then
  8454.                 turnLeft(1)
  8455.                 forward(8, 1)
  8456.                 turnLeft(1)
  8457.             elseif sector == 4 then
  8458.                 turnLeft(1)
  8459.                 forward(16, 1)
  8460.                 turnLeft(1)
  8461.             elseif sector == 5 then
  8462.                 --forward(1)
  8463.                 turnLeft(1)
  8464.                 forward(8, 1)
  8465.                 turnRight(1)
  8466.             elseif sector == 6 then
  8467.                 --forward(1)
  8468.                 turnLeft(1)
  8469.                 forward(16, 1)
  8470.                 turnRight(1)
  8471.             elseif sector == 7 then
  8472.                 --forward(1)
  8473.                 turnRight(1)
  8474.                 forward(8, 1)
  8475.                 turnLeft(1)
  8476.             elseif sector == 8 then
  8477.                 --forward(1)
  8478.                 turnRight(1)
  8479.                 forward(16, 1)
  8480.                 turnLeft(1)
  8481.             end
  8482.         end
  8483.     end
  8484.     -- 8x16 rectangle mined out, now go home
  8485.     forward(16, 1)
  8486.     while location:getY() < mineEntrance:getY() do
  8487.         up(1, 1)
  8488.     end
  8489.     turnLeft(2) --over mine entrance, facing furnace
  8490.     forward(4, 1) --over storageRedstone
  8491.     storeRedstone()
  8492.     forward(1, 1) -- over markerSand
  8493.     storeUnknownItem("sand")
  8494.     forward(4, 1) -- over markerWood
  8495.     storeUnknownItem("wood")
  8496.     forward(4, 1) -- over markerSigns
  8497.     storeUnknownItem("signs")
  8498.     forward(2, 1) -- over markerTorches
  8499.     storeUnknownItem("torches")
  8500.     forward(1, 1)
  8501.     turnRight(2)
  8502.     saveToLog("goMining2 started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
  8503.     sortInventory(true)
  8504.     --should end with gold redstone, maybe lapis, diamond if lucky
  8505. end
  8506.  
  8507. function goMining3(sector)
  8508.     -- start top middle, go back towards first tree, do top right quarter
  8509.     -- start top middle, go back towards first tree, do top left quarter
  8510.     -- etc, return to base to replant tree farm/ process materials/ reset torch
  8511.     local numNeeded = 0
  8512.     local itemsNeeded = {}
  8513.     local turnDirection = ""
  8514.     local itemList = ""
  8515.     local digSuccess = false
  8516.     local digItem = ""
  8517.     local digSlot = 0
  8518.     local torchesAt = {}
  8519.     local sTime = os.time()
  8520.    
  8521.     --[[
  8522.     4|3|1|2
  8523.     -|-|-|-
  8524.     8|7|5|6
  8525.     ]]--
  8526.     --need to find redstone, lapis, (obsidian), diamond, emerald
  8527.     --go down to level 15, dig 16X8 rectangle, by digging down on 14, digging forward on 15, placing ceiling on 16
  8528.     itemsNeeded, numNeeded = getItemsNeeded(3) --eg {"diamond","redstone"}, 2
  8529.     saveToLog("goMining3: no. items needed = "..numNeeded)
  8530.     if numNeeded > 0 then
  8531.         for i = 1, numNeeded do
  8532.             if i == 1 then
  8533.                 itemList = itemList.."'"..tostring(itemsNeeded[i]).."'"
  8534.             else
  8535.                 itemList = itemList..",'"..tostring(itemsNeeded[i]).."'"
  8536.             end
  8537.             saveToLog("Third stage mining, item "..i.." needed = "..itemsNeeded[i])
  8538.         end
  8539.     end
  8540.     while location:getY() > 14 do
  8541.         down(1, 1, itemList)
  8542.     end
  8543.     forward(16, 1)
  8544.     down(7, 1)
  8545.     for i = 1, 33 do
  8546.         torchesAt[i] = false
  8547.     end
  8548.     -- now under far side of previous mining area dig 33 x 2 corridor
  8549.     torchesAt[1] = true
  8550.     torchesAt[9] = true
  8551.     torchesAt[17] = true
  8552.     torchesAt[25] = true
  8553.     torchesAt[33] = true
  8554.    
  8555.     if sector == 1 then
  8556.         turnLeft(1)
  8557.         forward(1)
  8558.         turnLeft(1)
  8559.         turnDirection = "right"
  8560.     elseif sector == 2 then
  8561.         turnLeft(1)
  8562.         forward(9, 1)
  8563.         turnLeft(1)
  8564.         turnDirection = "right"
  8565.     elseif sector == 3 then
  8566.         turnRight(1)
  8567.         forward(1)
  8568.         turnRight(1)
  8569.         turnDirection = "left"
  8570.     elseif sector == 4 then
  8571.         turnRight(1)
  8572.         forward(9)
  8573.         turnRight(1)
  8574.         turnDirection = "left"
  8575.     elseif sector == 5 then
  8576.         turnLeft(1)
  8577.         forward(1)
  8578.         turnRight(1)
  8579.         turnDirection = "left"
  8580.     elseif sector == 6 then
  8581.         turnLeft(1)
  8582.         forward(9, 1)
  8583.         turnRight(1)
  8584.         turnDirection = "left"
  8585.     elseif sector == 7 then
  8586.         turnRight(1)
  8587.         forward(1)
  8588.         turnLeft(1)
  8589.         turnDirection = "right"
  8590.     elseif sector == 8 then
  8591.         turnRight(1)
  8592.         forward(9, 1)
  8593.         turnLeft(1)
  8594.         turnDirection = "right"
  8595.     end
  8596.     --eg itemList = 'redstone','ironore','diamonds'
  8597.     -- now dig 8 x 16 rectangle
  8598.     for sq = 1, 8 do
  8599.         for i = 1, 17 do
  8600.             if sq == 8 then --torches needed here
  8601.                 torchesAt[1] = true
  8602.                 torchesAt[9] = true
  8603.                 torchesAt[17] = true
  8604.                 if i == 1 or i == 9 or i == 17 then
  8605.                     up(1)
  8606.                     dig.digNew{self = dig, callFrom = "goMining3"}
  8607.                     down(1)
  8608.                 end
  8609.             else
  8610.                 torchesAt[1] = false
  8611.                 torchesAt[9] = false
  8612.                 torchesAt[17] = false
  8613.             end
  8614.             mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
  8615.             if i < 17 then
  8616.                 forward(1, 1)
  8617.             end
  8618.         end
  8619.         if slot:getItemSlot("coal") > 0 then
  8620.             if turtle.getItemCount(slot:getItemSlot("coal")) > 1 then
  8621.                 saveToLog("goMining3: fuel level = "..turtle.getFuelLevel().." using coal")
  8622.                 turtle.select(slot:getItemSlot("coal"))
  8623.                 turtle.refuel(turtle.getItemCount(slot:getItemSlot("coal")) - 1)
  8624.                 slot.update{self = slot, item = "coal"}
  8625.                 saveToLog("goMining3: fuel level = "..turtle.getFuelLevel().." after using available coal")
  8626.             end
  8627.         end
  8628.         if sq < 8 then
  8629.             torchesAt[1] = false
  8630.             torchesAt[9] = false
  8631.             torchesAt[17] = false
  8632.             if turnDirection == "right" then
  8633.                 turnRight(1)
  8634.                 mineToBedrock(torchesAt[i])
  8635.                 forward(1,1)
  8636.                 turnRight(1)
  8637.                 turnDirection = "left"
  8638.             else
  8639.                 turnLeft(1)
  8640.                 up(1)
  8641.                 dig.digNew{self = dig, callFrom = "goMining3"}
  8642.                 down(1)
  8643.                 mineToBedrock(torchesAt[i])
  8644.                 forward(1,1)
  8645.                 turnLeft(1)
  8646.                 turnDirection = "right"
  8647.             end
  8648.         else
  8649.             torchesAt[1] = true
  8650.             torchesAt[9] = true
  8651.             torchesAt[17] = true
  8652.             mineToBedrock(true)
  8653.             if sector == 1 then
  8654.                 turnRight(1)
  8655.                 forward(8, 1)
  8656.                 turnRight(1)
  8657.             elseif sector == 2 then
  8658.                 turnRight(1)
  8659.                 forward(16, 1)
  8660.                 turnRight(1)
  8661.             elseif sector == 3 then
  8662.                 turnLeft(1)
  8663.                 forward(8, 1)
  8664.                 turnLeft(1)
  8665.             elseif sector == 4 then
  8666.                 turnLeft(1)
  8667.                 forward(16, 1)
  8668.                 turnLeft(1)
  8669.             elseif sector == 5 then
  8670.                 --forward(1)
  8671.                 turnLeft(1)
  8672.                 forward(8, 1)
  8673.                 turnRight(1)
  8674.             elseif sector == 6 then
  8675.                 --forward(1)
  8676.                 turnLeft(1)
  8677.                 forward(16, 1)
  8678.                 turnRight(1)
  8679.             elseif sector == 7 then
  8680.                 --forward(1)
  8681.                 turnRight(1)
  8682.                 forward(8, 1)
  8683.                 turnLeft(1)
  8684.             elseif sector == 8 then
  8685.                 --forward(1)
  8686.                 turnRight(1)
  8687.                 forward(16, 1)
  8688.                 turnLeft(1)
  8689.             end
  8690.         end
  8691.     end
  8692.     -- 8x16 rectangle mined out, now go home
  8693.     forward(16, 1)
  8694.     while location:getY() < mineEntrance:getY() do
  8695.         up(1, 1)
  8696.     end
  8697.     turnLeft(2) --over mine entrance, facing furnace
  8698.     forward(4, 1) --over storageRedstone
  8699.     storeRedstone()
  8700.     forward(1, 1) -- over markerSand
  8701.     storeUnknownItem("sand")
  8702.     forward(4, 1) -- over markerWood
  8703.     storeUnknownItem("wood")
  8704.     forward(4, 1) -- over markerSigns
  8705.     storeUnknownItem("signs")
  8706.     forward(2, 1) -- over markerTorches
  8707.     storeUnknownItem("torches")
  8708.     forward(1, 1)
  8709.     turnRight(2)
  8710.     saveToLog("goMining3 started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
  8711.     sortInventory(true)
  8712.     --should end with gold redstone, maybe lapis, diamond if lucky
  8713. end
  8714.  
  8715. function harvestAllTrees()
  8716.     local sTime = os.time()
  8717.     local continue = true
  8718.     local colNo = 0
  8719.     local torchesNeeded = 0
  8720.     local mineshaftIndex = 0
  8721.     local logText = ""
  8722.    
  8723.     term.clear()
  8724.     term.setCursorPos(1, 1)
  8725.    
  8726.    
  8727.     --long code version - easier to debug. For/next loop version at end of this function.
  8728.    
  8729.     logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8730.     saveToLog(logText, true, false)
  8731.     fso:useFileName("logHarvestRight1.txt")
  8732.     colNo = 1 --first harvest run. Unlikely enough resources to make torches etc. on this run
  8733.     mineshaftIndex = 1
  8734.     direction = "right"
  8735.     torchesNeeded = restoreTorches("rightside", 0) --check for torches and signs before starting
  8736.     if checkObjectives(colNo, "rightside", torchesNeeded) then
  8737.         continue = false
  8738.     end
  8739.     harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 1/2
  8740.    
  8741.     logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8742.     saveToLog(logText, true, false)
  8743.     fso:useFileName("logHarvestRight2.txt")
  8744.     colNo = 3
  8745.     mineshaftIndex = 2
  8746.     direction = "right"
  8747.     torchesNeeded = restoreTorches("rightside", 3) --check for torches and signs before starting
  8748.     if checkObjectives(colNo, "rightside", torchesNeeded) then
  8749.         continue = false
  8750.     end
  8751.     prepareHarvest(direction, 3, "rightside") -- move to correct starting position
  8752.     harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 3/4
  8753.    
  8754.     logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8755.     saveToLog(logText, true, false)
  8756.     fso:useFileName("logHarvestRight3.txt")
  8757.     colNo = 5
  8758.     mineshaftIndex = 3
  8759.     direction = "left"
  8760.     torchesNeeded = restoreTorches("rightside", 6) --check for torches and signs before starting
  8761.     if checkObjectives(colNo, "rightside", torchesNeeded) then
  8762.         continue = false
  8763.     end
  8764.     prepareHarvest(direction, 6, "rightside") -- move to correct starting position
  8765.     harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 5/6
  8766.  
  8767.     logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8768.     saveToLog(logText, true, false)
  8769.     fso:useFileName("logHarvestRight4.txt")
  8770.     colNo = 7
  8771.     mineshaftIndex = 4
  8772.     direction = "right"
  8773.     torchesNeeded = restoreTorches("rightside", 9) --check for torches and signs before starting
  8774.     if checkObjectives(colNo, "rightside", torchesNeeded) then
  8775.         continue = false
  8776.     end
  8777.     prepareHarvest(direction, 9, "rightside") -- move to correct starting position
  8778.     harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 7/8
  8779.  
  8780.     logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8781.     saveToLog(logText, true, false)
  8782.     fso:useFileName("logHarvestRight5.txt")
  8783.     colNo = 9
  8784.     mineshaftIndex = 5
  8785.     direction = "left"
  8786.     torchesNeeded = restoreTorches("rightside", 12) --check for torches and signs before starting
  8787.     if checkObjectives(colNo, "rightside", torchesNeeded) then
  8788.         continue = false
  8789.     end
  8790.     prepareHarvest(direction, 12, "rightside") -- move to correct starting position
  8791.     harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 9/10
  8792.    
  8793.     logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8794.     saveToLog(logText, true, false)
  8795.     fso:useFileName("logHarvestRight6.txt")
  8796.     colNo = 11
  8797.     mineshaftIndex = 6
  8798.     direction = "right"
  8799.     torchesNeeded = restoreTorches("rightside", 15) --check for torches and signs before starting
  8800.     if checkObjectives(colNo, "rightside", torchesNeeded) then
  8801.         continue = false
  8802.     end
  8803.     prepareHarvest(direction, 15, "rightside") -- move to correct starting position
  8804.     harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 11/12
  8805.    
  8806.     --Debug use only:
  8807.     --REMOVE
  8808.     --continue = true
  8809.     --/Debug use
  8810.    
  8811.     if continue then
  8812.         logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8813.         saveToLog(logText, true, false)
  8814.         fso:useFileName("logHarvestLeft1.txt")
  8815.         colNo = 3
  8816.         mineshaftIndex = 7
  8817.         direction = "right"
  8818.         torchesNeeded = restoreTorches("leftside", 3)  --check for torches and signs before starting
  8819.         if checkObjectives(colNo, "leftside", torchesNeeded) then
  8820.             continue = false
  8821.         end
  8822.         prepareHarvest(direction, 3, "leftside") -- move to correct starting position
  8823.         harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 1/2
  8824.        
  8825.         if continue then
  8826.             logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8827.             saveToLog(logText, true, false)
  8828.             fso:useFileName("logHarvestLeft2.txt")
  8829.             colNo = 5
  8830.             mineshaftIndex = 8
  8831.             direction = "left"
  8832.             torchesNeeded = restoreTorches("leftside", 6) --check for torches and signs before starting
  8833.             if checkObjectives(colNo, "leftside", torchesNeeded) then
  8834.                 continue = false
  8835.             end
  8836.             prepareHarvest(direction, 6, "leftside") -- move to correct starting position
  8837.             harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 3/4
  8838.         end
  8839.        
  8840.         if continue then
  8841.             logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8842.             saveToLog(logText, true, false)
  8843.             fso:useFileName("logHarvestLeft3.txt")
  8844.             colNo = 7
  8845.             mineshaftIndex = 9
  8846.             direction = "right"
  8847.             torchesNeeded = restoreTorches("leftside", 9) --check for torches and signs before starting
  8848.             if checkObjectives(colNo, "leftside", torchesNeeded) then
  8849.                 continue = false
  8850.             end
  8851.             prepareHarvest(direction, 9, "leftside") -- move to correct starting position
  8852.             harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 5/6
  8853.         end
  8854.        
  8855.         if continue then
  8856.             logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8857.             saveToLog(logText, true, false)
  8858.             fso:useFileName("logHarvestLeft4.txt")
  8859.             colNo = 9
  8860.             mineshaftIndex = 10
  8861.             direction = "left"
  8862.             torchesNeeded = restoreTorches("leftside", 12) --check for torches and signs before starting
  8863.             if checkObjectives(colNo, "leftside", torchesNeeded) then
  8864.                 continue = false
  8865.             end
  8866.             prepareHarvest(direction, 12, "leftside") -- move to correct starting position
  8867.             harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 7/8
  8868.         end
  8869.     end
  8870.    
  8871.     --Alternative to above using for/next loops
  8872.     --[[
  8873.     local fileIndex = 0
  8874.     colNo = 1
  8875.     mineshaftIndex = 0
  8876.     for i = 0, 15, 3 do --no of blocks after reset torch(6, 9, 12, 15)
  8877.         if i == 6 or i == 12 then
  8878.             direction = "left"
  8879.         else
  8880.             direction = "right"
  8881.         end
  8882.         logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8883.         saveToLog(logText, true, false)
  8884.         fileIndex = fileIndex + 1
  8885.         fso:useFileName("logHarvestRight"..fileIndex..".txt")
  8886.         mineshaftIndex = mineshaftIndex + 1
  8887.         torchesNeeded = restoreTorches("rightside", i) --check for torches and signs before starting
  8888.         if checkObjectives(colNo, "rightside", torchesNeeded) then
  8889.             continue = false
  8890.         end
  8891.         prepareHarvest(direction, i, "rightside") -- move to correct starting position
  8892.         harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area runs 5 / 7 /9 /11
  8893.         colNo = colNo + 2
  8894.     end
  8895.     --cleared first half, now do second if needed
  8896.     if continue then
  8897.         fileIndex = 0
  8898.         colNo = 3
  8899.         for i = 3, 12, 3 do
  8900.             if i == 3 or i == 9 then
  8901.                 direction = "right"
  8902.             else
  8903.                 direction = "left"
  8904.             end
  8905.             logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  8906.             saveToLog(logText, true, false)
  8907.             fileIndex = fileIndex + 1
  8908.             fso:useFileName("logHarvestLeft"..fileIndex..".txt")
  8909.             mineshaftIndex = mineshaftIndex + 1
  8910.             torchesNeeded = restoreTorches("leftside", i)
  8911.             if checkObjectives(colNo, "leftside", torchesNeeded) then
  8912.                 continue = false
  8913.             end
  8914.             prepareHarvest(direction, i, "leftside") -- move to correct starting position
  8915.             harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area runs 5 / 7 /9 /11
  8916.             colNo = colNo + 2
  8917.         end
  8918.     end]]--
  8919.    
  8920.     --[[
  8921.     end of this function will have found:
  8922.     wood, (wood2 if available),saplings, (saplings2 if available), dirt, cobble, gravel, sand
  8923.     coal, sugar cane, ironore (or suspected ironore: ?ironore, needs smelting and making into buckets to prove)
  8924.     found and discarded: seeds, flint, flowers
  8925.     will have crafted planks, chests, furnaces, sticks, torches, signs, (buckets if 4+ ironore found)
  8926.     will have smelted: charcoal, stone, (iron if 4+ ?ironore)
  8927.     cleared an area ready for a tree farm and planted it
  8928.     ]]--
  8929. end
  8930.  
  8931. function harvestArea(colNo, direction, side, mineshaftIndex)
  8932.     local sTime = os.time()
  8933.     local dugItem = ""
  8934.     local fuelLevel = 0
  8935.     local fuelStats = {}
  8936.     local mineEntranceY = 0
  8937.     local mineExitY = 0
  8938.  
  8939.     --move forward 16 blocks first side of square
  8940.     saveToLog("harvestArea: colNo = "..colNo.." direction = "..direction..", side = "..side, true)
  8941.     forwardSafe(16)
  8942.     saveToLog("harvestArea: checking if tree present around mineshaft area", false, true)
  8943.  
  8944.     checkMineEntranceForTree(direction)
  8945.     --place cobble at mine entrance
  8946.     if checkWater("down") then
  8947.         --create an island with hole in centre
  8948.         saveToLog("harvestArea: creating island around mineshaft entrance", false, true)
  8949.         createIsland()
  8950.     else
  8951.         saveToLog("harvestArea: placing plug on mineshaft entrance", false, true)
  8952.         createMinePlug()
  8953.     end
  8954.     if direction == "right" then
  8955.         turnRight(1)
  8956.     else
  8957.         turnLeft(1)
  8958.     end
  8959.     mineEntranceY = location:getY()
  8960.    
  8961.     --still at mine entrance point
  8962.     fuelStats = getFuelAvailable()
  8963.     fuelLevel = fuelStats.fuelAvailableAllWood
  8964.     saveToLog("harvestArea: fuel available from wood = "..fuelLevel, false, true)
  8965.     if not objectives["gravel"] or not objectives["ironore"] or getCobbleStock() < 25 then --gravel/ iron ore not found
  8966.         -- still at mine entrance. check out exit to see if over water, level ground, or stepped level
  8967.         forwardSafe(3)
  8968.         --now at mine exit point
  8969.         mineExitY = location:getY()
  8970.         checkMineEntranceForTree(direction)
  8971.         turnRight(2)
  8972.         forwardSafe(3)
  8973.         turnRight(2)
  8974.         if fuelLevel > 100 then
  8975.             placeMarker(direction, "Danger!\nMineshaft", "sign/torch placed at mineshaft entrance")
  8976.         else
  8977.             placeMarker(direction, "Safe Area\nNo Mineshaft\nNot enough\nfuel", "sign/torch placed at mineshaft entrance")
  8978.         end
  8979.         if goMining1(direction, fuelLevel, mineExitY) then --no mining if fuel level too low
  8980.             --mining complete, now back on surface of ground or water
  8981.             mineshaft[mineshaftIndex] = true
  8982.             --check if on water
  8983.             if checkWater("down") then
  8984.                 --create an island with hole in centre
  8985.                 saveToLog("harvestArea: creating island around mineshaft exit", false, true)
  8986.                 createIsland()
  8987.             else
  8988.                 saveToLog("harvestArea: placing plug on mineshaft exit", false, true)
  8989.                 createMinePlug()
  8990.             end
  8991.             for i = 1, 4 do
  8992.                 turnRight(1)
  8993.                 isTreePresent()
  8994.             end
  8995.             if not objectives["sand"] or not objectives["gravel"] then
  8996.                 checkGravel()
  8997.             end
  8998.             placeMarker(direction, "", "Torch placed at mineshaft exit")
  8999.         else --not enough fuel, so no mining
  9000.             forwardSafe(3)
  9001.             checkMineEntranceForTree(direction)
  9002.             --now at turning point ready to head home
  9003.             if checkWater("down") then
  9004.                 --create an island with hole in centre
  9005.                 saveToLog("harvestArea: creating island around mineshaft exit", false, true)
  9006.                 createIsland()
  9007.             else
  9008.                 saveToLog("harvestArea: placing plug on mineshaft exit", false, true)
  9009.                 createMinePlug()
  9010.             end
  9011.             placeMarker(direction, "", "Torch placed at mineshaft exit")
  9012.         end
  9013.     else -- gravel and iron ore found so no more mining
  9014.         placeMarker(direction, "Safe Area\nNo Mineshaft", "sign/torch placed at turning point")
  9015.         forwardSafe(3)
  9016.         --now at turning point ready to head home
  9017.         checkMineEntranceForTree(direction)
  9018.         if checkWater("down") then -- water found.
  9019.             --create an island with hole in centre
  9020.             saveToLog("harvestArea: creating island around mineshaft exit", false, true)
  9021.             createIsland()
  9022.         else
  9023.             saveToLog("harvestArea: placing plug on mineshaft exit", false, true)
  9024.             createMinePlug()
  9025.         end
  9026.         -- place torch if onboard
  9027.         placeMarker(direction, "", "Torch placed at mineshaft exit")
  9028.     end
  9029.     if direction == "right" then
  9030.         turnRight(1)
  9031.     else
  9032.         turnLeft(1)
  9033.     end
  9034.    
  9035.     sTime = os.time()
  9036.    
  9037.     if colNo == 3 and side == "rightside" then
  9038.         forwardSafe(13)
  9039.     else
  9040.         forwardSafe(16)
  9041.     end
  9042.     --end of 16 x 6 block, return to furnace
  9043.     if side == "rightside" then --harvesting right side of 33 x 33 square from centre
  9044.         --end up facing furnace on edge of 5x5 cobble square except col 3, which goes direct
  9045.         if colNo == 1 then
  9046.             turnRight(1)
  9047.             returnToGroundLevel()
  9048.             checkForLooseSaplings()
  9049.             forward(1, 1)
  9050.         elseif colNo == 3 then
  9051.             --no need to turn as end up under furnace
  9052.             returnToGroundLevel()
  9053.             forward(3, 1)
  9054.         else
  9055.             if colNo == 5 then
  9056.                 turnRight(1)
  9057.                 forwardSafe(6)
  9058.             elseif colNo == 7 then
  9059.                 turnLeft(1)
  9060.                 forwardSafe(3)
  9061.             elseif colNo == 9 then
  9062.                 turnRight(1)
  9063.                 forwardSafe(12)
  9064.             elseif colNo == 11 then
  9065.                 turnLeft(1)
  9066.                 forwardSafe(9)
  9067.             end
  9068.             returnToGroundLevel()
  9069.             forward(1, 1)
  9070.         end
  9071.     else --harvesting left side of 33 x 33 square from centre
  9072.         --return side of area
  9073.         if colNo == 3 then --3,5,7,9
  9074.             turnLeft(1)
  9075.             forwardSafe(3, 1)
  9076.         elseif colNo == 5 then
  9077.             turnRight(1)
  9078.             --back(1)
  9079.         elseif colNo == 7 then
  9080.             turnLeft(1)
  9081.             forwardSafe(9, 1)
  9082.         elseif colNo == 9 then
  9083.             turnRight(1)
  9084.             forwardSafe(6, 1)
  9085.         end
  9086.         returnToGroundLevel()
  9087.         forward(1, 1)
  9088.     end
  9089.     if colNo == 3 and side == "rightside" then
  9090.         saveToLog("harvestArea: second harvest run, checkTorch not called, started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
  9091.     else
  9092.         checkTorch("home")
  9093.         saveToLog("harvestArea: run "..colNo + 1 .." direction = "..direction.." side = "..side.." started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
  9094.     end
  9095. end
  9096.  
  9097. function harvestTree(woodType)
  9098.     --only called if block known to be wood
  9099.     --break lower block of tree
  9100.     local tempYstart = 0
  9101.     local emptySlot = 0
  9102.     local isIdentified = false
  9103.     local dirtPlaced = false
  9104.     local useSlot = slot:getItemSlot(woodType)
  9105.     local isBranched = false
  9106.    
  9107.     saveToLog("harvestTree: woodType = "..woodType)
  9108.     dig.digNew{self = dig, slotNo = 1, expectedItem = woodType, callFrom = "harvestTree"}
  9109.     forward(1, 1)
  9110.     tempYstart = location:getY()
  9111.     --check down first
  9112.     turtle.select(slot:getItemSlot(woodType))
  9113.     while turtle.compareDown() do
  9114.         dig.digNew{self = dig, direction = "down", callFrom = "harvestTree"}
  9115.         down(1, 1)
  9116.         turtle.select(slot:getItemSlot(woodType))
  9117.     end
  9118.     while location:getY() < tempYstart do
  9119.         up(1, 1)
  9120.     end
  9121.     --now back at starting position
  9122.     turtle.select(slot:getItemSlot(woodType))
  9123.    
  9124.     while turtle.detectUp() do
  9125.         --leaves at tree top could give saplings
  9126.         dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "harvestTree"}
  9127.         up(1, 1)
  9128.         isBranched = checkTree(woodType, isBranched, "harvestTree")
  9129.         turtle.select(slot:getItemSlot(woodType))
  9130.     end
  9131.     --drop down until yCoord = tempYstart
  9132.     while location:getY() > tempYstart do
  9133.         down(1, 1)
  9134.     end
  9135.     --move back to starting point
  9136.     back(1)
  9137.     refuel(0)
  9138. end
  9139.  
  9140. function harvestTreeFarm(checkTorches)
  9141.     local useSlot = 0
  9142.     if checkTorches == nil then
  9143.         checkTorches = true
  9144.     end
  9145.     --move forward and harvest all trees
  9146.     saveToLog("harvestTreeFarm: started")
  9147.     forward(2, 1)
  9148.     turnRight(1)
  9149.     forward(2, 1)
  9150.     turnLeft(1)
  9151.     refuel(0)
  9152.     getItemsFromStorage{fromStore = storageSaplings, item1 = "saplings"}
  9153.     --for i = 1, 3 do
  9154.     for i = 1, 2 do
  9155.         for j = 1, 4 do
  9156.             if turtle.detect() then
  9157.                 useSlot = slot:getItemSlot("saplings")
  9158.                 turtle.select(useSlot)
  9159.                 if turtle.compare() then
  9160.                     saveToLog("harvestTreeFarm: dormant sapling harvested")
  9161.                     dig.digNew{self = dig, slot = useSlot, callFrom = "harvestTreeFarm"}
  9162.                 else
  9163.                     turtle.select(slot:getItemSlot("wood"))
  9164.                     saveToLog("harvestTreeFarm: tree harvest started")
  9165.                     harvestTree("wood")
  9166.                 end
  9167.             end
  9168.             forward(3, 1)
  9169.         end
  9170.         if i == 1 then
  9171.             turnRight(1)
  9172.             forward(3, 1)
  9173.             turnRight(1)
  9174.         end
  9175.     end
  9176.     -- now return to furnace
  9177.     forward(2, 1)
  9178.     turnRight(1)
  9179.     forward(3, 1)
  9180.     if checkTorches  then
  9181.         checkTorch("home")
  9182.     else
  9183.         forward(2, 1)
  9184.         turnRight(1)
  9185.     end
  9186.     --back under furnace
  9187. end
  9188.  
  9189. function identifyItems()
  9190.     local doRepeat = false
  9191.     local testAmount = 0
  9192.     local checkForDiamonds = false
  9193.     local tempItem = ""
  9194.     -- called when under furnace and facing deep mineshaft. called after checkMineStores() to eliminate known items
  9195.     -- if new items identified then re-run checkMineStores()
  9196.    
  9197.     sortInventory(true)
  9198.     changeDirection("faceMine")
  9199.     -- check for wood, fencing, planks,
  9200.     if slot:getItemSlot("coal") > 0 then
  9201.         repeat
  9202.             doRepeat = false
  9203.             if slot:getUnknownItemCount() > 0 then
  9204.                 saveToLog("identifyItems: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  9205.                 -- check if any items burn
  9206.                 for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  9207.                     turtle.select(i)
  9208.                     saveToLog("identifyItems: checking wooden items"..slot:getSlotContains(i), true)
  9209.                     if turtle.refuel() then
  9210.                         slot.update{self = slot, slotNo = i, delete = true}
  9211.                         doRepeat = true
  9212.                         sortInventory(true)
  9213.                         break
  9214.                     end
  9215.                 end
  9216.             end
  9217.         until not doRepeat
  9218.     else --coal not identified yet
  9219.         if slot:getUnknownItemCount() > 0 then
  9220.             saveToLog("identifyItems: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  9221.             -- check if any items burn
  9222.             for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  9223.                 if turtle.getItemCount(i) > 1 then
  9224.                     turtle.select(i)
  9225.                     saveToLog("identifyItems: checking "..slot:getSlotContains(i).." for coal", true)
  9226.                     if turtle.refuel(0) then
  9227.                         slot.update{self = slot, slotNo = i, newItem = "coal"}
  9228.                         sortInventory(true)
  9229.                         break
  9230.                     end
  9231.                 end
  9232.             end
  9233.         end
  9234.     end
  9235.     --now see if items place()
  9236.     if slot:getUnknownItemCount() > 0 then
  9237.         --need sticks to test for diamonds
  9238.         checkItems{keepTorches = 0, keepSigns = 0, keepSticks = 2, keepWood = 0}
  9239.         repeat
  9240.             if slot:getUnknownItemCount() == 0 then
  9241.                 break
  9242.             end
  9243.             doRepeat = false
  9244.             saveToLog("identifyItems: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
  9245.             for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
  9246.                 if turtle.getItemCount(i) > 1 then
  9247.                     turtle.select(i)
  9248.                     tempItem = slot:getSlotContains(i)
  9249.                     testAmount = turtle.getItemCount(i)
  9250.                     saveToLog("identifyItems: checking "..slot:getSlotContains(i).." place()", true)
  9251.                     changeDirection("faceExtraStorage") -- face away from marker torches
  9252.                     if turtle.place() then -- redstone, goldore, ironore, obsidian, moss stone
  9253.                         if slot:getItemSlot("redstone") == 0 then --redstone not found
  9254.                             if turtle.compare() then
  9255.                                 saveToLog("identifyItems: redstone not found", true)
  9256.                                 turtle.dig()
  9257.                             else -- redstone
  9258.                                 saveToLog("identifyItems: redstone confirmed", true)
  9259.                                 slot.update{self = slot, slotNo = i, newItem = "redstone"}
  9260.                                 objectives["redstone"] = true
  9261.                                 doRepeat = true
  9262.                                 turtle.dig()
  9263.                                 sortInventory(true)
  9264.                                 break
  9265.                             end
  9266.                             changeDirection("faceMine") -- face mine
  9267.                         end
  9268.                         --only got here if not redstone
  9269.                         if slot:getItemSlot("ironore") == 0 or not objectives["goldore"] then --ironore/goldore not found
  9270.                             dig.digNew{self = dig, slotNo = i, callFrom = "identifyItems"}
  9271.                             saveToLog("identifyItems: checking for goldore and ironore", true)
  9272.                             turnRight(2) -- face mine
  9273.                             if checkMetal(i) ~= "" then --new item identified
  9274.                                 doRepeat = true
  9275.                                 sortInventory(true)
  9276.                                 break
  9277.                             else -- not goldore, must be obsidian or moss stone
  9278.                                 saveToLog("identifyItems: checking for goldore/ironore completed not identified", true)
  9279.                             end
  9280.                         end
  9281.                         -- only here if ironore and goldore known or eliminated
  9282.                         saveToLog("identifyItems: testing "..tempItem.." in slot "..i, true)
  9283.                         dig.digNew{self = dig, slotNo = i, callFrom = "identifyItems"}
  9284.                         changeDirection("faceMine") -- face mine
  9285.                         if slot:getItemSlot("moss stone") == 0 then
  9286.                             if testAmount > 6 then
  9287.                                 saveToLog("identifyItems: checking for moss stone", true)
  9288.                                 slot.update{self = slot, slotNo = i, newItem = "?moss stone"}
  9289.                                 if craft{craftItem = "walls", craftQuantity = 6, sourceItem1 = "?moss stone", destSlot = 0} then
  9290.                                     doRepeat = true
  9291.                                     saveToLog("identifyItems: moss stone found", true)
  9292.                                     slot.update{self = slot, item = "?moss stone", newItem = "moss stone"}
  9293.                                     sortInventory(true)
  9294.                                     break
  9295.                                 else
  9296.                                     slot.update{self = slot, item = "?moss stone", newItem = tempItem}
  9297.                                     saveToLog("identifyItems: obsidian found", true)
  9298.                                     slot.update{self = slot, slotNo = i, newItem = "obsidian"}
  9299.                                     doRepeat = true
  9300.                                     break
  9301.                                 end
  9302.                             end
  9303.                         end
  9304.                     else --lapis, redstone, coal, diamonds, emeralds
  9305.                         changeDirection("faceMine") -- face mine
  9306.                         if slot:getItemSlot("redstone") > 0  and slot:getItemSlot("coal") > 0 then --lapis, diamond, emerald
  9307.                             if objectives["lapis"] then -- lapis found and in storage, would have been found so not needing testing
  9308.                                 if testAmount > 1 then -- diamond, as emerald so rare, not likely to have 2
  9309.                                     saveToLog("identifyItems: diamonds found", true)
  9310.                                     slot.update{self = slot, slotNo = i, newItem = "diamonds"}
  9311.                                     objectives["diamonds"] = true
  9312.                                     doRepeat = true
  9313.                                 end
  9314.                             else
  9315.                                 if not objectives["diamonds"] then
  9316.                                     checkForDiamonds = true
  9317.                                 end
  9318.                             end
  9319.                         else
  9320.                             if not objectives["diamonds"] then
  9321.                                 checkForDiamonds = true
  9322.                             end
  9323.                         end
  9324.                     end
  9325.                     if checkForDiamonds then
  9326.                         if testAmount > 3 then --test for diamond
  9327.                             slot.update{self = slot, slotNo = i, newItem = "?diamonds"}
  9328.                             if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "?diamonds", destSlot = 0} then
  9329.                                 saveToLog("identifyItems: pickaxe crafted from ?diamonds", true)
  9330.                                 slot.update{self = slot, item = "?diamonds", newItem = "diamonds"}
  9331.                                 objectives["diamonds"] = true
  9332.                                 objectives["diamond pickaxe"] = true
  9333.                                 doRepeat = true
  9334.                                 sortInventory(true)
  9335.                                 break
  9336.                             else --diamonds not found
  9337.                                 slot.update{self = slot, slotNo = i, newItem = tempItem}
  9338.                             end
  9339.                         end
  9340.                     else
  9341.                         if objectives["diamonds"] then
  9342.                             if not objectives["diamond pickaxe"] then
  9343.                                 if turtle.getItemCount(slot:getItemSlot("diamonds")) > 3 then
  9344.                                     if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "diamonds", destSlot = 0} then
  9345.                                         saveToLog("identifyItems: pickaxe crafted from diamonds", true)
  9346.                                         objectives["diamond pickaxe"] = true
  9347.                                         doRepeat = true
  9348.                                         break
  9349.                                     end
  9350.                                 end
  9351.                             end
  9352.                         end
  9353.                     end
  9354.                 end
  9355.             end
  9356.         until not doRepeat
  9357.         --return sticks if still onboard
  9358.         changeDirection("faceMine")
  9359.         if slot:getItemSlot("sticks") > 0 then
  9360.             forward(4, 1)
  9361.             turnRight(2)
  9362.             saveToLog("identifyItems: storing sticks in storageSticks", true)
  9363.             storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  9364.             forward(4, 1)
  9365.             turnRight(2)
  9366.         end
  9367.         if slot:getItemSlot("diamond pickaxe") > 0 then
  9368.             saveToLog("identifyItems: storing diamond pickaxe in storagePickaxes",true)
  9369.             forward(14, 1)
  9370.             turnRight(2)
  9371.             storeItem{toStore = storagePickaxes, item = "diamond pickaxe", quantity = 0, updateSlot = true, doSort = true}
  9372.             forward(14, 1)
  9373.             turnRight(2)
  9374.         end
  9375.     else
  9376.         saveToLog("identifyItems: no unknown items to check", true)
  9377.     end
  9378. end
  9379.  
  9380. function isTreeAbove(woodType)
  9381.     local startyCoord = location:getY() + 1 --return to base of tree when finished
  9382.     local isTree = false
  9383.     local useSlot = 0
  9384.     local success = false
  9385.     local dugItem = ""
  9386.     local dugSlot = 0
  9387.    
  9388.     useSlot = slot:getItemSlot("wood")
  9389.     turtle.select(slot:getItemSlot("wood")) --wood
  9390.     if turtle.compareUp() then --wood above
  9391.         isTree = true
  9392.         saveToLog("isTreeAbove checking wood")
  9393.         while turtle.compareUp() do
  9394.             up(1, useSlot)
  9395.         end
  9396.     else --may be wood2
  9397.         if slot:getItemSlot("wood2") > 0 then --wood2 found
  9398.             useSlot = slot:getItemSlot("wood2")
  9399.             turtle.select(useSlot) --wood
  9400.             if turtle.compareUp() then --wood above
  9401.                 isTree = true
  9402.                 saveToLog("isTreeAbove checking wood2")
  9403.                 while turtle.compareUp() do
  9404.                     up(1, useSlot)
  9405.                 end
  9406.             end
  9407.         end
  9408.     end
  9409.     if isTree then
  9410.         while location:getY() > startyCoord do
  9411.             down(1, useSlot)
  9412.         end
  9413.     else --check for leaves
  9414.         dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "isTreeAbove"}
  9415.         if dig:getDugItem() == "leaves" or dig:getDugItem() == "saplings" then
  9416.             isTree = true
  9417.         end
  9418.     end
  9419.    
  9420.     return isTree
  9421. end
  9422.  
  9423. function isTreeBelow(woodType)
  9424.     local startyCoord = location:getY()
  9425.     local isTree = false
  9426.     local useSlot = 0
  9427.    
  9428.     useSlot = slot.getItemSlot(slot, woodType)
  9429.     turtle.select(useSlot) --wood
  9430.     if turtle.compareDown() then --wood/wood2  below
  9431.         isTree = true
  9432.         saveToLog("isTreeBelow checking wood")
  9433.         while turtle.compareDown() do
  9434.             down(1, 1)
  9435.         end
  9436.     end
  9437.     if isTree then
  9438.         while location:getY() < startyCoord do
  9439.             up(1, useSlot)
  9440.         end
  9441.     end
  9442.    
  9443.     return isTree
  9444. end
  9445.  
  9446. function isTreePresent()
  9447.     local isTree = false
  9448.     local compareSlot = 0
  9449.     local compareAmount = 0
  9450.     local compareItem = ""
  9451.    
  9452.     if turtle.detect() then  --block in front: check if tree
  9453.         for i = 1, 16 do
  9454.             turtle.select(i)
  9455.             if turtle.getItemCount(i) > 0 then
  9456.                 if turtle.compare() then
  9457.                     compareItem = slot:getSlotContains(i)
  9458.                     break
  9459.                 end
  9460.             else
  9461.                 break
  9462.             end
  9463.         end
  9464.         if compareItem == "wood" or compareItem == "wood2" then
  9465.             saveToLog("isTreePresent: match block in front to "..compareItem, false, true)
  9466.             isTree = true
  9467.             -- wood / wood2 so harvest
  9468.             saveToLog("isTreePresent: Harvesting tree type "..compareItem)
  9469.             harvestTree(compareItem)
  9470.         end
  9471.     end
  9472.    
  9473.     return isTree
  9474. end
  9475.  
  9476. function isTreeInFront(woodType)
  9477.     local success = false
  9478.     local dugSlot = 0
  9479.    
  9480.     if woodType == "tree" then --leaves/saplings dug above, may be next to a tree
  9481.         if slot:getItemSlot("wood2") == 0 then
  9482.             dig.digNew{self = dig, slotNo = 1, waitForGravel = true, checkForItems = "wood2", callFrom = "isTreeInFront"}
  9483.         else
  9484.             dig.digNew{self = dig, waitForGravel = true, callFrom = "isTreeInFront"}
  9485.         end
  9486.         if dig:getSuccess() then
  9487.             if dig:getDugItem() == "dirt" or dig:getDugItem() == "cobble" then
  9488.                 turtle.select(dig:getDugSlot())
  9489.                 turtle.place()
  9490.                 turtle.select(1)
  9491.             elseif dig:getDugItem() == "wood2" then  
  9492.                 isTreeBelow("wood2")
  9493.                 harvestTree("wood2")
  9494.             end
  9495.         end
  9496.     elseif woodType == "wood" or woodType == "wood2" then --found tree with base above current ground level
  9497.         harvestTree(woodType)
  9498.     end
  9499. end
  9500.  
  9501. function isValuable(direction)
  9502.     local itemlist = {}
  9503.     local test = true
  9504.     local blockType = ""
  9505.  
  9506.     itemlist[1] = "dirt"
  9507.     itemlist[2] = "stone"
  9508.     itemlist[3] = "gravel"
  9509.     itemlist[4] = "cobble"
  9510.     --itemlist[4] = "sand"
  9511.     if direction == "up" then
  9512.         if turtle.detectUp() then
  9513.             for i = 1, 4 do
  9514.                 if slot.getItemSlot(slot,itemlist[i]) > 0 then--eg gravel may not be known
  9515.                     turtle.select(slot.getItemSlot(slot,itemlist[i])) -- select compare blocks
  9516.                     if turtle.compareUp() then -- compare block above to dirt, stone, gravel in turn
  9517.                         test = false
  9518.                         blockType = itemlist[i]
  9519.                     end
  9520.                 end
  9521.             end
  9522.         else
  9523.             test = false
  9524.         end
  9525.     elseif direction == "down" then
  9526.         if turtle.detectDown() then
  9527.             for i = 1, 4 do
  9528.                 if slot.getItemSlot(slot, itemlist[i]) > 0 then--eg gravel may not be known
  9529.                     turtle.select(slot.getItemSlot(slot, itemlist[i])) -- select compare blocks
  9530.                     if turtle.compareDown() then -- compare block below to selected
  9531.                         test = false
  9532.                         blockType = itemlist[i]
  9533.                     end
  9534.                 end
  9535.             end
  9536.         else
  9537.             test = false
  9538.         end
  9539.     elseif direction == "side" then
  9540.         if turtle.detect() then
  9541.             for i = 1, 4 do
  9542.                 if slot:getItemSlot(itemlist[i]) > 0 then--eg gravel may not be known
  9543.                     turtle.select(slot:getItemSlot(itemlist[i])) -- select compare blocks
  9544.                     if turtle.compare() then -- compare front block to selected
  9545.                         test = false
  9546.                         blockType = itemlist[i]
  9547.                         break
  9548.                     end
  9549.                 end
  9550.             end
  9551.         else
  9552.             test = false
  9553.         end
  9554.     end
  9555.    
  9556.     return test, blockType --true only if block does not match those in slots 1,2,3, eg dirt, stone, gravel, cobble
  9557. end
  9558.  
  9559. function mineCorridor(length, torchesAt, doNotPlug, doReturn, itemList)
  9560.     local maxTries = 10
  9561.     -- torchesAt[1] = true, torchesAt[2] = false
  9562.     -- length = length of corridor, torchesAt = position of torches, doNotPlug  = place block above entrance to corridor
  9563.     -- doReturn = return same distance as length
  9564.     for i = 1, length do
  9565.         if turtle.detectUp() then --block above
  9566.             if isValuable("up") then --could be useful, so dig and replace with cobble
  9567.                 while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor"} do --allow for gravel
  9568.                     sleep(.5)
  9569.                     if not turtle.detectUp() then --water or lava falling down
  9570.                         break
  9571.                     end
  9572.                     maxTries = maxTries - 1
  9573.                     if maxTries <= 0 then
  9574.                         maxTries = 10
  9575.                         break
  9576.                     end
  9577.                 end
  9578.                 if getCobbleStock() > 1 then
  9579.                     turtle.select(slot:getItemSlot("cobble"))
  9580.                     turtle.placeUp()
  9581.                 elseif getDirtStock() > 1 then
  9582.                     turtle.select(slot:getItemSlot("dirt"))
  9583.                     turtle.placeUp()
  9584.                 end
  9585.             end
  9586.         else --no block above or lava/water, so place cobble
  9587.             if doNotPlug and i == 1 then --dont plug mine entrance
  9588.                 saveToLog("mineCorridor: Entrance left open")
  9589.             else
  9590.                 if getCobbleStock() > 1 then
  9591.                     turtle.select(slot:getItemSlot("cobble"))
  9592.                     turtle.placeUp()
  9593.                 elseif getDirtStock() > 1 then
  9594.                     turtle.select(slot:getItemSlot("dirt"))
  9595.                     turtle.placeUp()
  9596.                 end
  9597.             end
  9598.         end
  9599.         if turtle.detectDown() then
  9600.             dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
  9601.         else --nothing below could be air, water, lava
  9602.             if slot:getItemSlot("buckets") > 0 then
  9603.                 turtle.select(slot:getItemSlot("buckets"))
  9604.                 if turtle.placeDown() then --bucket filled water or lava
  9605.                     saveToLog("mineCorridor: water or lava detected")
  9606.                     if turtle.refuel() then --lava
  9607.                         saveToLog("mineCorridor: refuelled with lava to "..turtle.getFuelLevel())
  9608.                         sleep(2)
  9609.                         while turtle.placeDown() do
  9610.                             if turtle.refuel() then --lava
  9611.                                 saveToLog("mineCorridor: refuelling with lava again to "..turtle.getFuelLevel())
  9612.                             else
  9613.                                 break
  9614.                             end
  9615.                             sleep(2)
  9616.                             if turtle.getFuelLevel() > 10000 then
  9617.                                 break
  9618.                             end
  9619.                         end
  9620.                     else
  9621.                         turtle.placeDown() --empty bucket
  9622.                     end
  9623.                 end
  9624.             end
  9625.         end
  9626.         if torchesAt[i] then
  9627.             if slot:getItemCount("torches") > 1 then
  9628.                 turtle.select(slot:getItemSlot("torches"))
  9629.                 turtle.placeDown()
  9630.             end
  9631.         end
  9632.         if i < length then
  9633.             if turtle.detect() then --dig and drop non-valuables
  9634.                 while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineCorridor"} do
  9635.                     sleep(.5)
  9636.                     if not turtle.detect() then --water or lava falling down
  9637.                         break
  9638.                     end
  9639.                     maxTries = maxTries - 1
  9640.                     if maxTries <= 0 then
  9641.                         maxTries = 10
  9642.                         break
  9643.                     end
  9644.                 end
  9645.             end
  9646.             forward(1, 1, itemList, "mineCorridor")
  9647.         end
  9648.     end
  9649.     if doReturn then
  9650.         turnRight(2)
  9651.         forward(length, 1, itemList, "mineCorridor")
  9652.         turnRight(2)
  9653.     end
  9654. end
  9655.  
  9656. function mineDown(toLevel, itemList, calledFrom)
  9657.     while location:getY() >= toLevel do --mine down either to bedrock, or to selected level.
  9658.         checkWalls(itemList, calledFrom)
  9659.         if isValuable("down") then
  9660.             saveToLog("mineDown: valuable item detected below x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
  9661.         end
  9662.         down(1, 1, itemList, calledFrom)
  9663.     end
  9664.     checkWalls(itemList, calledFrom) --final check at base of chosen depth
  9665. end
  9666.  
  9667. function mineForDiamonds()
  9668.     local doContinue = true
  9669.     local numDiamonds = 0
  9670.     local logText = ""
  9671.     local fuelLevel = 0
  9672.     local debugLevel = 1 --1 is default value. Alter if needing to debug a particular level
  9673.     local debugSector = 1 -- 1 is default value. Alter if needing to debug a particular sector
  9674.     -- mine 8 sectors per layer. 3 layers, then down to bedrock
  9675.     saveToLog("mineForDiamonds: ready to mine. fuel level = "..turtle.getFuelLevel(), true, false)
  9676.     fuelLevel = turtle.getFuelLevel()
  9677.     checkMineStores() -- in case anything useful found during prepareMining()
  9678.     identifyItems()
  9679.     checkMineStores() --after checking for redstone, lapis, obsidian, diamonds
  9680.     refuelMining(300)
  9681.     checkItems{keepWood = 0, keepTorches = 16}
  9682.    
  9683.     --do 8 sectors per layer
  9684.     for i = debugLevel, 3 do
  9685.         logText = "prepareMining: changing logFile Name from "..tostring(fso:getCurrentFileName()).." to "..tostring(fso:getNextFileName()).." END OF LOGFILE!"
  9686.         saveToLog(logText, true, false)
  9687.         fso:useFileName("logMiningLayer"..i..".txt")
  9688.         for j = debugSector, 8 do
  9689.             fuelLevel = turtle.getFuelLevel()
  9690.             saveToLog("mineForDiamonds: fuel level sector "..j.." = "..turtle.getFuelLevel(), true, false)
  9691.             if debugSector > 1 then --reset debug: eg testing out on level 2 sector 4, but level 3 should start at sector 1
  9692.                 debugSector = 1
  9693.             end
  9694.             restoreTorches("bothsides", 1)
  9695.             --move to entrance
  9696.             forward(16, 1)
  9697.             --rewrite sign
  9698.             turtle.select(16)
  9699.             if turtle.dig() then
  9700.                 turtle.place("Diamond Mine\nMining Level "..i.."\nsector "..j.."\n time: "..textutils.formatTime(os.time(), true))
  9701.             end
  9702.             turnRight(2)
  9703.             goMining2(i, j) --level 1, sector 1, will return here, with new items onboard
  9704.             --check anything onboard already in extended storage
  9705.             checkMineStores()
  9706.             identifyItems()
  9707.             checkMineStores() --after checking for redstone, lapis, obsidian, diamonds
  9708.             numDiamonds = checkDiamonds()
  9709.             if turtle.getItemCount(slot:getItemSlot("ironore")) >= 14 then
  9710.                 if numDiamonds >= 6 then
  9711.                     doContinue = false -- at least 6 diamonds found
  9712.                     saveToLog("prepareMining: Mission accomplished "..numDiamonds.." diamonds found", true)
  9713.                     break
  9714.                 elseif numDiamonds >= 3 then
  9715.                     saveToLog("prepareMining: Mission partly finished "..numDiamonds.." diamonds found", true)
  9716.                 else
  9717.                     saveToLog("prepareMining: Insufficient diamonds found so far", true)
  9718.                 end
  9719.             else
  9720.                 saveToLog("prepareMining: Mission almost complete "..numDiamonds.." diamonds found, but more ironore needed", true)
  9721.             end
  9722.             if not objectives["pave extendedStorage"] then
  9723.                 objectives["pave extendedStorage"] = paveExtendedStorage()
  9724.             end
  9725.             refuelMining(250)
  9726.             --checkItems{keepWood = 0, keepTorches = 16}
  9727.             saveToLog("mineForDiamonds: sector "..j.." completed. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
  9728.         end
  9729.         if not doContinue then
  9730.             break
  9731.         end
  9732.         checkItems{keepWood = 0, keepTorches = 16}
  9733.     end
  9734.    
  9735.     if doContinue then
  9736.         logText = "prepareMining: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  9737.         saveToLog(logText, true, false)
  9738.         fso:useFileName("logMiningBedrock.txt")
  9739.         createBedrockMine()
  9740.         checkItems{keepWood = 0, keepTorches = 16}
  9741.         for i = 1, 8 do
  9742.             fuelLevel = turtle.getFuelLevel()
  9743.             saveToLog("mineForDiamonds: fuel level sector "..i.." = "..turtle.getFuelLevel(), true, false)
  9744.             restoreTorches("bothsides", 1)
  9745.             --move to entrance
  9746.             forward(16, 1)
  9747.             --rewrite sign
  9748.             turtle.select(16)
  9749.             if turtle.dig() then
  9750.                 turtle.place("Diamond Mine\nBedrock Mining\nSector "..i.."\n time: "..textutils.formatTime(os.time(), true))
  9751.             end
  9752.             turnRight(2)
  9753.             goMining3(i) --level 1, sector 1, will return here, with new items onboard
  9754.             checkMineStores()
  9755.             identifyItems()
  9756.             checkMineStores() --after checking for redstone, lapis, obsidian, diamonds
  9757.             numDiamonds = checkDiamonds()
  9758.             if numDiamonds >= 6 then
  9759.                 saveToLog("prepareMining: Mission accomplished "..numDiamonds.." diamonds found", true)
  9760.                 break
  9761.             elseif numDiamonds >= 3 then
  9762.                 saveToLog("prepareMining: Mission partly finished "..numDiamonds.." diamonds found", true)
  9763.             else
  9764.                 saveToLog("prepareMining: Insufficient diamonds found so far", true)
  9765.             end
  9766.             refuelMining(400)
  9767.             checkItems{keepWood = 0, keepTorches = 16}
  9768.             saveToLog("mineForDiamonds: sector "..i.." completed. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
  9769.         end
  9770.     end
  9771. end
  9772.  
  9773. function mineForward(steps, clear, itemList)   
  9774.     for i = 1, steps do
  9775.         turnLeft(1)
  9776.         if turtle.detect() then
  9777.             if isValuable("side") then
  9778.                 dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineForward"}
  9779.             end
  9780.         end
  9781.         turnRight(2)
  9782.         if turtle.detect() then
  9783.             if isValuable("side") then
  9784.                 dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineForward"}
  9785.             end
  9786.         end
  9787.         turnLeft(1)
  9788.         if turtle.detectUp() then
  9789.             if isValuable("up") then
  9790.                 while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineForward"} do
  9791.                     sleep(.5)
  9792.                     if not turtle.detectUp() then --water or lava falling down
  9793.                         break
  9794.                     end
  9795.                 end
  9796.                 turtle.select(slot:getItemSlot("cobble"))
  9797.                 if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  9798.                     turtle.placeUp()
  9799.                 end
  9800.             end
  9801.         else --fill above with cobble
  9802.             turtle.select(slot:getItemSlot("cobble"))
  9803.             if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  9804.                 turtle.placeUp()
  9805.             end
  9806.         end
  9807.         if turtle.detectDown() then
  9808.             dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineForward"}
  9809.         end
  9810.         if turtle.detect() then
  9811.             dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineForward"}
  9812.         end
  9813.         forward(1, 1, itemList)
  9814.     end
  9815. end
  9816.  
  9817. function mineToBedrock(placeTorch)
  9818.     local tempYCoord = 0
  9819.    
  9820.     tempYCoord = location:getY()
  9821.     --[[if placeTorch then --collect original torch
  9822.         up(1)
  9823.         dig{}
  9824.         down(1)
  9825.     end]]--
  9826.     dig.digNew{self = dig, callFrom = "mineToBedrock"}
  9827.     while down(1, 1) do
  9828.         dig.digNew{self = dig, callFrom = "mineToBedrock"}
  9829.         if location:getY() == 1 then
  9830.             break
  9831.         end
  9832.     end
  9833.     if placeTorch then
  9834.         up(1)
  9835.         if slot:getItemSlot("torches") > 0 then
  9836.             turtle.select(slot:getItemSlot("torches"))
  9837.             turtle.placeDown()
  9838.             slot.update{self = slot, item = "torches", delete = true}
  9839.         end
  9840.     end
  9841.     while location:getY() < tempYCoord do
  9842.         up(1, 1)
  9843.     end
  9844. end
  9845.  
  9846. function mineUp(toLevel, itemList, calledFrom)
  9847.     while location:getY() < toLevel - 5 do --mine up to selected level - 5
  9848.         checkWalls(itemList, calledFrom)
  9849.         up(1, 1, itemList, calledFrom)
  9850.     end
  9851.     checkWalls(itemList, calledFrom) --final check at top of chosen height
  9852.     while location:getY() < toLevel do --mine up to selected level - 5
  9853.         up(1, 1, itemList, calledFrom)
  9854.     end
  9855. end
  9856.  
  9857. function mineItem(itemList, calledFrom)
  9858.     --RECURSIVE FUNCTION - BEWARE!
  9859.     local doContinue = false
  9860.    
  9861.     --check if block in front is valuable. If so mine it
  9862.     if isValuable("side") then
  9863.         dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom}
  9864.         if dig:getDugSlot() > 0 then --did not get leaves, seeds
  9865.             if dig:getDugItem() == "dirt" or dig:getDugItem() == "coal" then --allows for grass in front, max 50 items
  9866.                 if turtle.getItemCount(dig:getDugSlot()) < 50 then
  9867.                     doContinue = true
  9868.                 end
  9869.             elseif dig:getDugItem() == "sand" or dig:getDugItem() == "gravel" then
  9870.                 if turtle.getItemCount(dig:getDugSlot()) < 8 then
  9871.                     doContinue = true
  9872.                 end
  9873.             elseif dig:getDugItem() == "cobble" then -- only on goMining1 before stone has been smelted
  9874.                 if turtle.getItemCount(dig:getDugSlot()) >= 60 then
  9875.                     turtle.select(slot:getItemSlot("cobble"))
  9876.                     turtle.place()
  9877.                 end
  9878.             else
  9879.                 if turtle.getItemCount(dig:getDugSlot()) < 20 then --need 18 iron ore
  9880.                     doContinue = true
  9881.                 end
  9882.             end
  9883.             if doContinue then
  9884.                 if turtle.getFuelLevel() < 10 then
  9885.                     saveToLog("mineItem: refuelling")
  9886.                     refuel(0)
  9887.                 end
  9888.                 forward(1, 1, itemList, calledFrom)
  9889.                 if isValuable("side") then
  9890.                     mineItem(itemList, calledFrom)
  9891.                 end
  9892.                 turnLeft(1)
  9893.                 if isValuable("side") then
  9894.                     mineItem(itemList, calledFrom)
  9895.                 end
  9896.                 turnRight(2)
  9897.                 if isValuable("side") then
  9898.                     mineItem(itemList, calledFrom)
  9899.                 end
  9900.                 turnLeft(1)
  9901.                 back(1)
  9902.             end
  9903.         end
  9904.     end
  9905. end
  9906.  
  9907. function paveExtendedStorage()
  9908.     local result = false
  9909.    
  9910.     if getCobbleStock() > 20 then
  9911.         forward(1,1)
  9912.         turnLeft(1)
  9913.         forward(1, 1)
  9914.         turnRight(1)
  9915.         forward(1, 1)
  9916.         for k = 1, 9 do
  9917.             forward(1, 1)
  9918.             dig.digNew{self = dig, direction = "up", callFrom = "paveExtendedStorage"}
  9919.             dig.digNew{self = dig, direction = "down", callFrom = "paveExtendedStorage"}
  9920.             turtle.select(slot:getItemSlot("cobble"))
  9921.             turtle.placeDown()
  9922.         end
  9923.         turnRight(1)
  9924.         forward(2, 1)
  9925.         turnRight(1)
  9926.         dig.digNew{self = dig, direction = "up", callFrom = "paveExtendedStorage"}
  9927.         dig.digNew{self = dig, direction = "down", callFrom = "paveExtendedStorage"}
  9928.         turtle.select(slot:getItemSlot("cobble"))
  9929.         turtle.placeDown()
  9930.         for k = 1, 8 do
  9931.             forward(1, 1)
  9932.             dig.digNew{self = dig, direction = "up", callFrom = "paveExtendedStorage"}
  9933.             dig.digNew{self = dig, direction = "down", callFrom = "paveExtendedStorage"}
  9934.             turtle.select(slot:getItemSlot("cobble"))
  9935.             turtle.placeDown()
  9936.         end
  9937.         forward(2, 1)
  9938.         turnRight(1)
  9939.         forward(1, 1)
  9940.         turnLeft(1)
  9941.         forward(1)
  9942.         result = true
  9943.     end
  9944.    
  9945.     return result
  9946. end
  9947.  
  9948. function paveTreeFarm()
  9949.     success = false
  9950.     --add decorative cobble to sides of storage chests
  9951.     if getCobbleStock() > 20 then
  9952.         saveToLog("paveTreeFarm: using cobble to pimp the treefarm")
  9953.         forward(3, 1)
  9954.         turnLeft(1)
  9955.         forward(1)
  9956.         turnRight(1)
  9957.         for i = 1, 13 do
  9958.             dig.digNew{self = dig, direction = "down", callFrom = "paveTreeFarm"}
  9959.             turtle.select(slot:getItemSlot("cobble"))
  9960.             turtle.placeDown()
  9961.             if i < 13 then
  9962.                 forward(1, 1)
  9963.             end
  9964.         end
  9965.         turnRight(2)
  9966.         forward(13, 1)
  9967.         turnLeft(1)
  9968.         forward(1)
  9969.         turnRight(1)
  9970.         forward(2, 1)
  9971.         turnRight(2)
  9972.         success = true
  9973.     else
  9974.         saveToLog("paveTreeFarm: insufficient cobble to pimp the treefarm")
  9975.     end
  9976.    
  9977.     return success
  9978. end
  9979.  
  9980. function placeBlock(blockType, columnType, blockNo)
  9981.     local success = false
  9982.     local chestBelow = false
  9983.     local dirtCount = 0
  9984.     local replaceBlock = false
  9985.     -- placeBlock("cobble", "storage", 1)
  9986.    
  9987.     if columnType == "storage" then
  9988.         if blockNo == 1 then -- storageTorches marker
  9989.             if not placeStorage:getMarkerPlaced("torches") then
  9990.                 replaceBlock = true
  9991.             end
  9992.         elseif blockNo == 2 then -- storageSigns
  9993.             if not placeStorage:getStoragePlaced("signs") then
  9994.                 replaceBlock = true
  9995.             end
  9996.         elseif blockNo == 3 then -- storageSigns marker
  9997.             if not placeStorage:getMarkerPlaced("signs") then
  9998.                 replaceBlock = true
  9999.             end
  10000.         elseif blockNo == 4 then -- storageSticks
  10001.             if not placeStorage:getStoragePlaced("sticks") then
  10002.                 replaceBlock = true
  10003.             end
  10004.         elseif blockNo == 5 then -- storageSticks marker
  10005.             if not placeStorage:getMarkerPlaced("sticks") then
  10006.                 replaceBlock = true
  10007.             end
  10008.         elseif blockNo == 6 then -- storageWood
  10009.             if not placeStorage:getStoragePlaced("wood") then
  10010.                 replaceBlock = true
  10011.             end
  10012.         elseif blockNo == 7 then -- storageWood marker
  10013.             if not placeStorage:getMarkerPlaced("wood") then
  10014.                 replaceBlock = true
  10015.             end
  10016.         elseif blockNo == 8 then -- storageIronore
  10017.             if not placeStorage:getStoragePlaced("ironore") then
  10018.                 replaceBlock = true
  10019.             end
  10020.         elseif blockNo == 9 then -- storageIronore marker
  10021.             if not placeStorage:getMarkerPlaced("ironore") then
  10022.                 replaceBlock = true
  10023.             end
  10024.         elseif blockNo == 10 then -- storageSand
  10025.             if not placeStorage:getStoragePlaced("sand") then
  10026.                 replaceBlock = true
  10027.             end
  10028.         elseif blockNo == 11 then -- storageSand marker
  10029.             if not placeStorage:getMarkerPlaced("sand") then
  10030.                 replaceBlock = true
  10031.             end
  10032.         elseif blockNo == 12 then -- storageRedstone
  10033.             if not placeStorage:getStoragePlaced("redstone") then
  10034.                 replaceBlock = true
  10035.             end
  10036.         elseif blockNo == 13 then -- storageRedstone marker
  10037.             if not placeStorage:getMarkerPlaced("redstone") then
  10038.                 replaceBlock = true
  10039.             end
  10040.         elseif blockNo == 14 then -- storagePickaxes
  10041.             if not placeStorage:getStoragePlaced("pickaxes") then
  10042.                 replaceBlock = true
  10043.             end
  10044.         elseif blockNo == 15 then
  10045.             replaceBlock = true
  10046.         end
  10047.         if replaceBlock then
  10048.             dig.digNew{self = dig, direction = "down", checkForItems = "flowers", callFrom = "placeBlock"}
  10049.             if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  10050.                 turtle.select(slot:getItemSlot("cobble"))
  10051.                 saveToLog("placeBlock: Replacing cobble", false)
  10052.                 turtle.placeDown()
  10053.                 success = true
  10054.             end
  10055.         end
  10056.     else
  10057.         turtle.select(slot.getItemSlot(slot, "chests"))
  10058.         if turtle.compareDown() then
  10059.             chestBelow = true
  10060.         end
  10061.         turtle.select(slot.getItemSlot(slot, "dirt"))
  10062.         if chestBelow then
  10063.             saveToLog("placeBlock: chest below, leaving alone....")
  10064.         else
  10065.             if dig.digNew{self = dig, direction = "down", checkForItems = "flowers", callFrom = "placeBlock"} then
  10066.                 if turtle.getItemCount(slot:getItemSlot(blockType)) > 1 then
  10067.                     turtle.select(slot:getItemSlot(blockType))
  10068.                     saveToLog("placeBlock: Replacing "..blockType, false)
  10069.                     turtle.placeDown()
  10070.                 else
  10071.                     saveToLog("Placing "..blockType.." failed - no "..blockType, false)
  10072.                 end
  10073.                 success = true
  10074.             else --dig not succeed - air/water/lava
  10075.                 if turtle.getItemCount(slot:getItemSlot(blockType)) > 1 then
  10076.                     turtle.select(slot:getItemSlot(blockType))
  10077.                     saveToLog("Placing "..blockType.." over air/water", false)
  10078.                     turtle.placeDown()
  10079.                 else
  10080.                     saveToLog("Placing "..blockType.." over air/water failed - no "..blockType, false)
  10081.                 end
  10082.                 success = true
  10083.             end
  10084.         end
  10085.     end
  10086.    
  10087.     return success
  10088. end
  10089.  
  10090. function placeExtendedStorage()
  10091.     local numChests = turtle.getItemCount(slot:getItemSlot("chests"))
  10092.    
  10093.     saveToLog("placeExtendedStorage: starting...")
  10094.     if numChests < 6 then
  10095.         numChests = 6 - numChests
  10096.         if slot:getItemSlot("wood") == 0 then --no wood in stock, so must be in storage
  10097.             saveToLog("placeExtendedStorage: getting wood from storage")
  10098.             forward(6,1)
  10099.             getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  10100.             turnRight(2)
  10101.             forward(6, 1)
  10102.             turnRight(2)
  10103.            
  10104.             if slot:getItemSlot("wood") > 0 then
  10105.                 if turtle.getItemCount(slot:getItemSlot("wood")) < 11 then --not enough wood in storage (10 needed)
  10106.                     saveToLog("placeExtendedStorage: harvesting tree farm")
  10107.                     harvestTreeFarm()
  10108.                     saveToLog("placeExtendedStorage: replanting tree farm")
  10109.                     replantTreeFarm(true, true)
  10110.                 end
  10111.             end
  10112.         end
  10113.         --should now have at least 11 wood
  10114.         saveToLog("placeExtendedStorage: making chests")
  10115.         craft{craftItem = "planks", craftQuantity = numChests * 8, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10116.         craft{craftItem = "chests", craftQuantity = numChests, sourceItem1 = "planks", destSlot = 0, doSort = true}
  10117.     end
  10118.     turnRight(2)
  10119.     forward(1, 1)
  10120.     dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10121.     turtle.select(slot:getItemSlot("cobble"))
  10122.     turtle.placeDown()
  10123.     forward(1, 1)
  10124.     dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10125.     turtle.select(slot:getItemSlot("chests"))
  10126.     turtle.placeDown()
  10127.     for k = 1, 4 do
  10128.         forward(1, 1)
  10129.         dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
  10130.         dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10131.         turtle.select(slot:getItemSlot("cobble"))
  10132.         turtle.placeDown()
  10133.         forward(1, 1)
  10134.         dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
  10135.         dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10136.         turtle.select(slot:getItemSlot("chests"))
  10137.         turtle.placeDown()
  10138.     end
  10139.     if turtle.getItemCount(slot:getItemSlot("cobble")) > 22 then
  10140.         forward(1,1)
  10141.         turnLeft(1)
  10142.         dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
  10143.         dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10144.         turtle.select(slot:getItemSlot("cobble"))
  10145.         turtle.placeDown()
  10146.         forward(1, 1)
  10147.         dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
  10148.         dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10149.         turtle.select(slot:getItemSlot("cobble"))
  10150.         turtle.placeDown()
  10151.         turnLeft(1)
  10152.         for k = 1, 8 do
  10153.             forward(1, 1)
  10154.             dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
  10155.             dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10156.             turtle.select(slot:getItemSlot("cobble"))
  10157.             turtle.placeDown()
  10158.         end
  10159.         turnLeft(1)
  10160.         forward(2, 1)
  10161.         turnLeft(1)
  10162.         dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
  10163.         dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10164.         turtle.select(slot:getItemSlot("cobble"))
  10165.         turtle.placeDown()
  10166.         for k = 1, 8 do
  10167.             forward(1, 1)
  10168.             dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
  10169.             dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
  10170.             turtle.select(slot:getItemSlot("cobble"))
  10171.             turtle.placeDown()
  10172.         end
  10173.         turnLeft(1)
  10174.         forward(1, 1)
  10175.         turnLeft(1)
  10176.         forward(17)
  10177.     else
  10178.         turnRight(2)
  10179.         forward(16)
  10180.     end
  10181.     objectives["place extended storage"] = true
  10182.     storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  10183.     turnRight(2)
  10184.     forward(6, 1)
  10185.     turnRight(2)
  10186.     objectives["pave extendedStorage"] = true
  10187. end
  10188.  
  10189. function placeMarker(direction, signText, logText)
  10190.     local doSort = false
  10191.    
  10192.     if direction == "right" then
  10193.         turnLeft(1)
  10194.     else
  10195.         turnRight(1)
  10196.     end
  10197.     forward(1, 1)
  10198.     --if not turtle.detectDown() then
  10199.     dig.digNew{self = dig, direction = "down"}
  10200.     turtle.select(slot:getItemSlot("cobble"))
  10201.     turtle.placeDown()
  10202.     --end
  10203.     up(1)
  10204.     if slot:getItemSlot("signs") > 0 and signText ~= "" then
  10205.         turtle.select(slot:getItemSlot("signs"))
  10206.         turtle.placeDown(signText)
  10207.         if turtle.getItemCount(slot:getItemSlot("signs")) == 0 then
  10208.             doSort = true
  10209.         end
  10210.         slot.update{self = slot, item = "signs", delete = true}
  10211.         saveToLog("placeMarker: "..logText, false, true)   
  10212.     elseif slot:getItemSlot("torches") > 0 then
  10213.         turtle.select(slot:getItemSlot("torches"))
  10214.         turtle.placeDown()
  10215.         if turtle.getItemCount(slot:getItemSlot("torches")) == 0 then
  10216.             doSort = true
  10217.         end
  10218.         slot.update{self = slot, item = "torches", delete = true}
  10219.         saveToLog("placeMarker: "..logText, false, true)
  10220.     else
  10221.         saveToLog("placeMarker: No signs or torches to place...", false, true) 
  10222.     end
  10223.     if doSort then
  10224.         saveToLog("placeMarker: Sorting Inventory", false, true)
  10225.         sortInventory(true)
  10226.     end
  10227.     turnRight(2)
  10228.     forward(1, 1)
  10229.     down(1)
  10230.     if direction == "right" then
  10231.         turnLeft(1)
  10232.     else
  10233.         turnRight(1)
  10234.     end
  10235. end
  10236.  
  10237. function plantSaplings()
  10238.     for i = 1, 13 do
  10239.         if treeFarm:getSaplingPattern(i) == 1 then --plant sapling on ground level
  10240.             if turtle.getItemCount(slot:getItemSlot("saplings")) > 1 then
  10241.                 turtle.select(slot:getItemSlot("saplings"))
  10242.                 if turtle.placeDown() then
  10243.                     treeFarm:addSapling()
  10244.                 end
  10245.             end
  10246.         elseif treeFarm:getSaplingPattern(i) == 2 then --dig pit, place torch
  10247.             down(1, 1)  --ground level
  10248.             dig.digNew{self = dig, direction = "down", callFrom = "plantSaplings"} --dig pit
  10249.             down(1)
  10250.             if not turtle.detectDown() then
  10251.                 turtle.select(slot:getItemSlot("dirt"))
  10252.                 turtle.placeDown()
  10253.             end
  10254.             up(1)
  10255.             turtle.select(slot:getItemSlot("torches"))
  10256.             turtle.placeDown() --place torch in pit
  10257.             up(1, 1) --ready for sapling
  10258.         end
  10259.         if i < 13 then
  10260.             forward(1, 1)
  10261.         end
  10262.     end
  10263.     treeFarm:setTimePlanted(os.time())
  10264. end
  10265.  
  10266. function prepareHarvest(direction, steps, side)
  10267.     local dugItem = ""
  10268.     local doContinue = true
  10269.    
  10270.     if side == "rightside" then
  10271.         if steps == 0 then --not first run on right side
  10272.             doContinue = false
  10273.         else
  10274.             turnRight(1)
  10275.         end
  10276.     elseif side == "leftside" then
  10277.         turnLeft(1)
  10278.     end
  10279.     --checking before harvestArea()
  10280.     if doContinue then --not first run on right side
  10281.         if placeStorage:getMarkerPlaced("torches") then --torches made
  10282.             saveToLog("prepareHarvest: checking if torch still present", true)
  10283.             if not turtle.detect() then --torch missing
  10284.                 if slot:getItemCount("torches") > 0 then
  10285.                     turtle.select(slot:getItemSlot("torches"))
  10286.                     turtle.place() --replace torch
  10287.                 end
  10288.             end
  10289.             dig.digNew{self = dig, expectedItem = "torches", callFrom = "prepareHarvest"}
  10290.             forward(2, 1)
  10291.             turnLeft(2)
  10292.             turtle.select(slot.getItemSlot(slot, "torches"))
  10293.             turtle.place() --replace torch
  10294.             turnRight(2)
  10295.             forward(1, 1, "flowers", "forwardSafe")
  10296.             slot.update{self = slot, item = "torches", delete = true}
  10297.             saveToLog("prepareHarvest: complete, moving forward "..steps - 3 .." before next harvesting run", true)
  10298.         else --no torches yet
  10299.             saveToLog("prepareHarvest: no torches made yet", true)
  10300.             forward(3, 1, "flowers", "forwardSafe")
  10301.         end
  10302.         if steps > 3 then
  10303.             forwardSafe(steps - 3)
  10304.         end
  10305.         if turtle.detect() then
  10306.             saveToLog("prepareHarvest: block in front at turning point, investigating", true)
  10307.             dugItem = checkSugarCane()
  10308.             isTreeInFront(dugItem)
  10309.         end
  10310.         if direction == "right" then
  10311.             turnRight(1)
  10312.         else
  10313.             turnLeft(1)
  10314.         end
  10315.     end
  10316. end
  10317.  
  10318. function prepareMining()
  10319.     local woodNeeded = 0
  10320.     local planksNeeded = 0
  10321.     local sticksNeeded = 0
  10322.     local chestsNeeded = 7 --should already have storageTorches, storageSigns, storageSticks, storageIronore, storageWood, storageSand
  10323.     local chestsAvailable = 0
  10324.     local woodAvailable = 0
  10325.     local woodGrowing = 0
  10326.     local maxWood = treeFarm:getMaxHarvest()
  10327.     local logText = ""
  10328.     local fuelLevel = 0
  10329.    
  10330.     -- start here after harvestAllTrees
  10331.     --[[object              wood        planks
  10332.         1 sign              2           8
  10333.         8 sticks            1           4       4 for pickaxes 4 spare
  10334.         16 sticks           2           8       for 48 torches
  10335.         16 charcoal         16          0       for 48 torches
  10336.         16 furnace fuel     4           16      for 16 charcoal
  10337.         7 chests            14          56
  10338.         500 turtle fuel     9           36
  10339.        
  10340.         total               47  wood    124 planks  24 sticks
  10341.     ]]--
  10342.     fuelLevel = turtle.getFuelLevel()
  10343.     if fso:getCurrentFileNameIndex() == 2 then --left side of tree harvest not done
  10344.         fso:setCurrentFileNameIndex(3)
  10345.     end
  10346.     logText = "prepareMining: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
  10347.     saveToLog(logText, true, false)
  10348.     fso:useFileName("logCreateMine.txt")
  10349.     saveToLog("prepareMining: fuel level = "..fuelLevel, true, false)
  10350.     -- check tree farm, continue until wood grown
  10351.     -- store signs, sticks and torches first
  10352.     saveToLog("prepareMining: storing torches in storageTorches")
  10353.     storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
  10354.     forward(2)
  10355.     saveToLog("prepareMining: storing signs in storageSigns")
  10356.     storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
  10357.     forward(2)
  10358.     saveToLog("prepareMining: storing sticks in storageSticks")
  10359.     storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  10360.     turnRight(2)
  10361.     forward(4)
  10362.     turnRight(2)
  10363.     while not objectives["choose wood"] do
  10364.         objectives["choose wood"] = chooseWood()  --identify type of trees farmed, and refuel, make chests and/or charcoal from remainder
  10365.     end
  10366.     woodNeeded = 48
  10367.     planksNeeded = 128
  10368.     sticksNeeded = 24
  10369.     if placeStorage:getStoragePlaced("redstone") then -- need 1 less chests
  10370.         chestsNeeded = chestsNeeded - 1
  10371.         woodNeeded = woodNeeded - 2
  10372.         planksNeeded = planksNeeded - 8
  10373.     end
  10374.     if placeStorage:getStoragePlaced("pickaxes") then -- need 1 less chests
  10375.         chestsNeeded = chestsNeeded - 1
  10376.         woodNeeded = woodNeeded - 2
  10377.         planksNeeded = planksNeeded - 8
  10378.     end
  10379.     chestsAvailable = turtle.getItemCount(slot:getItemSlot("chests")) - 1
  10380.     chestsNeeded = chestsNeeded - chestsAvailable
  10381.     if chestsNeeded < 0 then
  10382.         chestsNeeded = 0
  10383.     end
  10384.     woodNeeded = woodNeeded - chestsAvailable * 2
  10385.     planksNeeded = planksNeeded - chestsAvailable * 8
  10386.     --if 1 sign available 8 planks less required
  10387.     if storageSigns:getItemCount("signs") > 0 then
  10388.         woodNeeded = woodNeeded - 2
  10389.         planksNeeded = planksNeeded - 8
  10390.     end
  10391.  
  10392.     woodGrowing = treeFarm:getPotentialHarvest()
  10393.     woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 1
  10394.     woodNeeded = woodNeeded - woodAvailable
  10395.     if woodNeeded > 0 then
  10396.         saveToLog("prepareMining: wood growing approx "..woodGrowing, true)
  10397.         saveToLog("prepareMining: wood needed ("..woodNeeded..") harvesting tree farm", true)
  10398.         while woodGrowing < woodNeeded do
  10399.             sleep(10)
  10400.             -- 10 secs = 12 minecraft minutes
  10401.             -- 1  min  = 1 hour 12 minutes
  10402.             -- 20 mins = 1 minecraft day
  10403.             woodGrowing = treeFarm:getPotentialHarvest()
  10404.             --will be equal to maxHarvest after 2 days
  10405.             saveToLog("prepareMining: waiting for tree farm to grow. potential harvest = "..woodGrowing.." from "..maxWood)
  10406.             if woodGrowing == maxWood then
  10407.                 saveToLog("prepareMining: harvesting tree farm. Expecting "..maxWood.." wood")
  10408.                 break
  10409.             end
  10410.         end
  10411.         harvestTreeFarm()
  10412.         replantTreeFarm(true, true)
  10413.     else -- remove negative
  10414.         woodNeeded = 0
  10415.     end
  10416.     saveToLog("prepareMining: storing saplings in storageSaplings")
  10417.     forward(2)
  10418.     turnRight(1)
  10419.     forward(2)
  10420.     storeItem{toStore = storageSaplings, item = "saplings", quantity = 0, updateSlot = true, doSort = true}
  10421.     turnRight(2)
  10422.     forward(2)
  10423.     turnLeft(1)
  10424.     forward(2)
  10425.     turnRight(2)
  10426.     if chestsNeeded > 0 then --still need more chests made
  10427.         if planksNeeded > 64 then --craft chests first, then remaining planks
  10428.             craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10429.             craft{craftItem = "chests", craftQuantity = chestsNeeded, sourceItem1 = "planks", destSlot = 0, doSort = true}
  10430.             planksNeeded = planksNeeded - 64
  10431.             craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10432.         else
  10433.             craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10434.         end
  10435.     else --no chests, only charcoal and sticks needed
  10436.         if planksNeeded > 64 then
  10437.             craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10438.         else
  10439.             craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10440.         end
  10441.     end
  10442.     craft{craftItem = "sticks", craftQuantity = sticksNeeded, sourceItem1 = "planks", destSlot = 0, doSort = true}
  10443.     if not placeStorage:getStoragePlaced("redstone") then
  10444.         placeStorage:place("redstone", false)
  10445.     end
  10446.     if slot:getItemCount("charcoal") < 17 then -- not enough charcoal to make 48 torches
  10447.         smelt("charcoal", 16 - slot:getItemCount("charcoal"))
  10448.     end
  10449.     --make torches, wood has already been decided, wood2 turned to charcoal
  10450.     --make sticks for storage, pickaxe(s) and later torch making
  10451.     --1 wood = 4 planks = 8 sticks = 32 torches
  10452.     craft{craftItem = "torches", craftQuantity = 48, sourceItem1 = "sticks", sourceItem2 = "charcoal", destSlot = 0, doSort = true}
  10453.    
  10454.     -- if any sticks/planks left over make signs
  10455.     if slot:getItemCount("sticks") > 0 and slot:getItemCount("planks") >= 6 then
  10456.         craft{craftItem = "signs", craftQuantity = 3, sourceItem1 = "sticks", sourceItem2  = "planks", destSlot = 0, doSort = true}
  10457.     end
  10458.     checkItems{keepSticks = 0, keepTorches = 48, keepSigns = 1}
  10459.     --cleanup any remaining wood, planks, sticks by putting them in storage
  10460.     if slot:getItemSlot("sticks") > 0 then
  10461.         forward(4, 1)
  10462.         storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
  10463.         back(4)
  10464.     end
  10465.     if slot:getItemSlot("planks") > 0 then
  10466.         turtle.select(slot:getItemSlot("planks"))
  10467.         turtle.refuel()
  10468.         slot.update{self = slot, item = "planks", delete = true}
  10469.     end
  10470.     if slot:getItemSlot("planks2") > 0 then
  10471.         turtle.select(slot:getItemSlot("planks2"))
  10472.         turtle.refuel()
  10473.         slot.update{self = slot, item = "planks2", delete = true}
  10474.     end
  10475.     --use remaining charcoal for fuel
  10476.     if slot:getItemSlot("charcoal") > 0 then
  10477.         turtle.select(slot:getItemSlot("charcoal"))
  10478.         turtle.refuel()
  10479.         slot.update{self = slot, item = "charcoal", delete = true}
  10480.         sortInventory(true)
  10481.         saveToLog("prepareMining: refuelled with charcoal to "..turtle.getFuelLevel())
  10482.     end
  10483.     --use remaining planks for fuel
  10484.     if slot:getItemSlot("planks") > 0 then
  10485.         turtle.select(slot:getItemSlot("planks"))
  10486.         turtle.refuel()
  10487.         slot.update{self = slot, item = "planks", delete = true}
  10488.         sortInventory(true)
  10489.         saveToLog("prepareMining: refuelled with planks to "..turtle.getFuelLevel())
  10490.     end
  10491.     placeExtendedStorage()
  10492.     if turtle.getFuelLevel() < 500 then
  10493.         saveToLog("prepareMining: fuel level = "..turtle.getFuelLevel().." not enough for mining", true)
  10494.         -- more wood needed, harvest tree farm
  10495.         saveToLog("prepareMining: not enough fuel, harvesting tree farm", true)
  10496.         harvestTreeFarm()
  10497.         replantTreeFarm(true, true)
  10498.         forward(2)
  10499.         turnRight(1)
  10500.         forward(2)
  10501.         saveToLog("prepareMining: storing saplings in storageSaplings")
  10502.         storeItem{toStore = storageSaplings, item = "saplings", quantity = 0, updateSlot = true, doSort = true}
  10503.         turnRight(2)
  10504.         forward(2)
  10505.         turnLeft(1)
  10506.         forward(2)
  10507.         turnRight(2)
  10508.     end
  10509.     saveToLog("prepareMining: storing wood in storageWood")
  10510.     forward(6, 1)
  10511.     storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  10512.     forward(2)
  10513.     saveToLog("prepareMining: removing iron ore and buckets from storageIronore")
  10514.     getItemsFromStorage{fromStore = storageIronore, item1 = "ironore", item2 = "buckets"}
  10515.     sortInventory(true)
  10516.     saveToLog("prepareMining: storing sugar cane in storageSand")
  10517.     forward(2, 1)
  10518.     storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
  10519.     --forward(2)
  10520.     saveToLog("prepareMining: storing sand in storageSand")
  10521.     storeItem{toStore = storageSand, item = "sand", quantity = 0, updateSlot = true, doSort = true}
  10522.     turnRight(2)
  10523.     forward(10)
  10524.     turnRight(2)
  10525.     saveToLog("prepareMining: ready to create mine. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
  10526.     fuelLevel = turtle.getFuelLevel()
  10527.     createMine() --mine structure in place ready for mining sectors 12 x 16 block corridors takes 24 hrs
  10528.     saveToLog("prepareMining: mine created. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
  10529. end
  10530.  
  10531. function refuel(count)
  10532.     --[[1 wood block = 15 fuel
  10533.         1 planks = 15, 4 = 60
  10534.         1 stick = 5, 8 = 40
  10535.         planks in slot 3
  10536.         use coal> planks> wood2> wood]]--
  10537.        
  10538.     local refuel = false
  10539.     local startLevel = 0
  10540.     local emptySlot = 0
  10541.     local fuelStats = {}
  10542.     local turns = 0
  10543.    
  10544.     if count == nil then
  10545.         count = 0
  10546.     end
  10547.  
  10548.     --count is units of 15, 6 = 1 coal/charcoal 6 blocks of wood or 6 planks
  10549.     startLevel = turtle.getFuelLevel()
  10550.     if startLevel < 100 or count > 0 then -- refuel by 1 coal, 3 planks/wood2/wood
  10551.         saveToLog("refuel: current level = "..startLevel, true)
  10552.         --sortInventory(true)
  10553.         fuelStats = getFuelAvailable()
  10554.         saveToLog("refuel: fuelStats total fuel available = "..fuelStats.totalFuelAvailable)
  10555.         if fuelStats.fuelAvailableCoal > 0 then
  10556.             turtle.select(slot:getItemSlot("coal"))
  10557.             if turtle.refuel(1) then
  10558.                 refuel = true
  10559.                 slot.update{self = slot, item = "coal"}
  10560.                 saveToLog("Refuelled with coal to "..turtle.getFuelLevel().." (start = "..startLevel..")", true)
  10561.             else
  10562.                 saveToLog("Refuel: failed with coal")
  10563.             end
  10564.         end
  10565.         if not refuel and fuelStats.fuelAvailablePlanks2 > 0 then
  10566.             saveToLog("refuel: fuelStats fuelAvailablePlanks2 = "..fuelStats.fuelAvailablePlanks2)
  10567.             turtle.select(slot.getItemSlot(slot, "planks2"))
  10568.             if turtle.refuel() then
  10569.                 refuel = true
  10570.                 slot.update{self = slot, item = "planks2", delete = true}              
  10571.                 saveToLog("Refuelled with planks2 to "..turtle.getFuelLevel().." (start = "..startLevel..")")
  10572.                 sortInventory(true)
  10573.             else
  10574.                 saveToLog("Refuel: failed with planks2")
  10575.             end
  10576.         end
  10577.         if not refuel and fuelStats.fuelAvailablePlanks > 0 then
  10578.             saveToLog("refuel: fuelStats fuelAvailablePlanks = "..fuelStats.fuelAvailablePlanks)
  10579.             turtle.select(slot.getItemSlot(slot, "planks"))
  10580.             if turtle.refuel() then
  10581.                 refuel = true
  10582.                 slot.update{self = slot, item = "planks", delete = true}               
  10583.                 saveToLog("Refuelled with planks to "..turtle.getFuelLevel().." (start = "..startLevel..")")
  10584.                 sortInventory(true)
  10585.             else
  10586.                 saveToLog("Refuel: failed with planks")
  10587.             end
  10588.         end
  10589.         if not refuel and (fuelStats.fuelAvailableWood > 0 or fuelStats.fuelAvailableWood2 > 0 ) then
  10590.             if fuelStats.fuelAvailableWood > 0 then --use this to make planks          
  10591.                 saveToLog("refuel: No planks for fuel. Attempting to craft planks from wood")
  10592.                 if turtle.detect() then
  10593.                     for i = 1, 4 do
  10594.                         if turtle.detect() then
  10595.                             turnRight(1)
  10596.                             turns = turns + 1
  10597.                         else
  10598.                             break
  10599.                         end
  10600.                     end
  10601.                 end
  10602.                 -- bug in cc 1.6.3 place() can put chest 1 block away if grass is in the way
  10603.                 dig.digNew{self = dig}
  10604.                 if craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 0, doSort = true} then
  10605.                     fuelStats.fuelAvailablePlanks = turtle.getItemCount(slot.getItemSlot(slot, "planks")) * 15
  10606.                     turtle.select(slot.getItemSlot(slot, "planks"))
  10607.                     if turtle.refuel() then
  10608.                         refuel = true
  10609.                         slot.update{self = slot, item = "planks", delete = true}
  10610.                         saveToLog("Refuelled with planks to "..turtle.getFuelLevel().." (start = "..startLevel..")")
  10611.                     end
  10612.                 else
  10613.                     saveToLog("Refuel: error crafting planks from wood")
  10614.                     error()
  10615.                 end
  10616.                 if turns > 0 then
  10617.                     turnLeft(turns)
  10618.                 end
  10619.             else --check if wood2 can be harvested for fuel as last resort
  10620.                 if fuelStats.fuelAvailableWood2 > 0 then
  10621.                     saveToLog("No planks for fuel. attempting to craft from wood 2")
  10622.                     if turtle.detect() then
  10623.                         for i = 1, 4 do
  10624.                             if turtle.detect() then
  10625.                                 turnRight(1)
  10626.                                 turns = turns + 1
  10627.                             else
  10628.                                 break
  10629.                             end
  10630.                         end
  10631.                     end
  10632.                     craft{craftItem = "planks2", craftQuantity = 4, sourceItem1 = "wood2", destSlot = 0, doSort = true}
  10633.                     fuelStats.fuelAvailablePlanks2 = turtle.getItemCount(slot.getItemSlot(slot, "planks2")) * 15
  10634.                     turtle.select(slot.getItemSlot(slot, "planks2"))
  10635.                     if turtle.refuel() then
  10636.                         refuel = true
  10637.                         slot.update{self = slot, item = "planks2", delete = true}
  10638.                         saveToLog("Refuelled with planks2 (start = "..startLevel..")")
  10639.                     end
  10640.                     if turns > 0 then
  10641.                         turnLeft(turns)
  10642.                     end
  10643.                 else
  10644.                     saveToLog("No materials left for fuel")
  10645.                 end
  10646.             end
  10647.             if refuel then
  10648.                 sortInventory(true)
  10649.             end
  10650.         end
  10651.         if not refuel and fuelStats.fuelAvailableCharcoal > 0 then
  10652.             turtle.select(slot.getItemSlot(slot, "charcoal"))
  10653.             if turtle.refuel(fuelStats.fuelAvailableCharcoal/80) then
  10654.                 refuel = true
  10655.                 slot.update{self = slot, item = "charcoal", delete = true}
  10656.                 saveToLog("Refuelled with charcoal to "..turtle.getFuelLevel().." (start = "..startLevel..")")
  10657.                 sortInventory(true)
  10658.             else
  10659.                 saveToLog("Refuel: failed with charcoal")
  10660.             end
  10661.         end
  10662.     else
  10663.         --saveToLog("refuel: "..startLevel.." is sufficient, refuel not required",true)
  10664.     end --fuel > 100
  10665.    
  10666.     return refuel
  10667. end
  10668.  
  10669. function refuelMining(checkLevel)
  10670.     local fuelLevel = turtle.getFuelLevel()
  10671.    
  10672.     if fuelLevel < checkLevel then
  10673.         if slot:getItemSlot("coal") > 0 then
  10674.             if turtle.getItemCount(slot:getItemSlot("coal")) > 1 then
  10675.                 saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." using coal")
  10676.                 turtle.select(slot:getItemSlot("coal"))
  10677.                 turtle.refuel(turtle.getItemCount(slot:getItemSlot("coal")) - 1)
  10678.                 slot.update{self = slot, item = "coal"}
  10679.                 saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." after using available coal")
  10680.             end
  10681.         end
  10682.         fuelLevel = turtle.getFuelLevel()
  10683.         if fuelLevel < checkLevel then
  10684.             --check if enough wood in storage
  10685.             if storageWood:getItemCount("wood") > 16 then  --enough wood in store already
  10686.                 forward(6, 1)
  10687.                 getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  10688.                 if turtle.getItemCount(slot:getItemSlot("wood")) > 16 then
  10689.                     craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10690.                 else
  10691.                     craft{craftItem = "planks", craftQuantity = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10692.                 end
  10693.                 turtle.select(slot:getItemSlot("planks"))
  10694.                 turtle.refuel()
  10695.                 slot.update{self = slot, item = "planks", delete = true}
  10696.                 saveToLog("refuelMining: storing wood in storageWood after refuelling", false)
  10697.                 storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  10698.                 turnRight(2)
  10699.                 forward(6, 1)
  10700.                 turnRight(2)
  10701.             else
  10702.                 saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." harvesting tree farm", true)
  10703.                 if storageWood:getItemCount("wood") > 0 then
  10704.                     forward(6, 1)
  10705.                     getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  10706.                     turnRight(2)
  10707.                     forward(6, 1)
  10708.                     turnRight(2)
  10709.                 end
  10710.                 harvestTreeFarm()
  10711.                 replantTreeFarm(true, true)
  10712.                 if turtle.getItemCount(slot:getItemSlot("wood")) > 32 then
  10713.                     craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10714.                     turtle.select(slot:getItemSlot("planks"))
  10715.                     turtle.refuel()
  10716.                     slot.update{self = slot, item = "planks", delete = true}
  10717.                     craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10718.                 elseif turtle.getItemCount(slot:getItemSlot("wood")) > 16 then
  10719.                     craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10720.                 else
  10721.                     craft{craftItem = "planks", craftQuantity = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
  10722.                 end
  10723.                 turtle.select(slot:getItemSlot("planks"))
  10724.                 turtle.refuel()
  10725.                 slot.update{self = slot, item = "planks", delete = true}
  10726.                 saveToLog("refuelMining: storing wood in storageWood after refuelling", false)
  10727.                 forward(6, 1)
  10728.                 storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  10729.                 turnRight(2)
  10730.                 forward(6, 1)
  10731.                 turnRight(2)
  10732.             end
  10733.         else
  10734.             saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." refuel with coal sufficient", true)
  10735.         end
  10736.     else
  10737.         saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." refuel not required", true)
  10738.     end
  10739. end
  10740.  
  10741. function replantSaplings(treeRow)
  10742.     --[[local saplingPattern = {}
  10743.    
  10744.     saplingPattern[1] = 0
  10745.     saplingPattern[2] = 1
  10746.     saplingPattern[3] = 2
  10747.     saplingPattern[4] = 0
  10748.     saplingPattern[5] = 1
  10749.     saplingPattern[6] = 2
  10750.     saplingPattern[7] = 0
  10751.     saplingPattern[8] = 1
  10752.     saplingPattern[9] = 2
  10753.     saplingPattern[10] = 0
  10754.     saplingPattern[11] = 1
  10755.     saplingPattern[12] = 2
  10756.     saplingPattern[13] = 0]]--
  10757.    
  10758.     if treeRow == true then
  10759.         -- use sapling pattern to check position
  10760.         for i = 15, 1, -1 do
  10761.             turtle.suckDown()
  10762.             turnLeft(1)
  10763.             checkForLooseSaplings()
  10764.             turnRight(2)
  10765.             checkForLooseSaplings()
  10766.             turnLeft(1) --now facing forward, saplings checked  left and right
  10767.             --if saplingPattern[i - 1] == 1 and i < 15 then --sapling to be planted on current block
  10768.             if treeFarm:getSaplingPattern(i) - 1 == 1 and i < 15 then --sapling to be planted on current block
  10769.                 forward(1)
  10770.                 turnRight(1)
  10771.                 checkForLooseSaplings()
  10772.                 turnRight(1)
  10773.                 checkForLooseSaplings()
  10774.                 turtle.select(slot.getItemSlot(slot, "saplings"))
  10775.                 if turtle.getItemCount(slot.getItemSlot(slot, "saplings")) > 1 then
  10776.                     turtle.place()
  10777.                 end
  10778.                 turnRight(1)
  10779.                 checkForLooseSaplings()
  10780.                 turnRight(1)
  10781.                 checkForLooseSaplings()
  10782.             else
  10783.                 if i > 1 then
  10784.                     forward(1)
  10785.                 end
  10786.             end
  10787.         end
  10788.     else
  10789.         for i = 1, 15 do -- row 1 at ground level
  10790.             turnRight(1)
  10791.             checkForLooseSaplings()
  10792.             turnLeft(2)
  10793.             checkForLooseSaplings()
  10794.             turnRight(1)
  10795.             checkForLooseSaplings()
  10796.             if i < 15 then
  10797.                 forward(1, 1)
  10798.             end
  10799.         end
  10800.     end
  10801. end
  10802.  
  10803. function replantTreeFarm(checkTorches, storeSaplings)
  10804.     local plant = false
  10805.     local direction = "right"
  10806.    
  10807.     if checkTorches == nil then
  10808.         checkTorches = true
  10809.     end
  10810.     if storeSaplings == nil then
  10811.         storeSaplings = true
  10812.     end
  10813.  
  10814.     saveToLog("replantTreeFarm started y = "..location:getY())
  10815.     forward(1)
  10816.     turtle.select(slot:getItemSlot("saplings"))
  10817.     saveToLog("replantTreeFarm - now facing farm on left edge. x = "..location:getX().." y = "..location:getY().." z = "..location:getZ())
  10818.     for i = 1, 4 do
  10819.         if i == 2 or i == 3 then
  10820.             plant = true
  10821.         else
  10822.             plant = false
  10823.         end
  10824.         replantSaplings(plant)
  10825.         if i < 4 then
  10826.             if direction == "right" then
  10827.                 turnRight(1)
  10828.                 forward(2, 1)
  10829.                 turnRight(1)
  10830.                 direction = "left"
  10831.             else
  10832.                 turnLeft(1)
  10833.                 forward(3, 1)
  10834.                 turnLeft(1)
  10835.                 direction = "right"
  10836.             end
  10837.         end
  10838.     end
  10839.     if storeSaplings then
  10840.         turnRight(1)
  10841.         forward(5, 1)
  10842.         turnRight(1)
  10843.         forward(1, 1)
  10844.         storeItem{toStore = storageSaplings, item = "saplings", quantity = 0, updateSlot = true, doSort = true}
  10845.         turnRight(2)
  10846.         forward(2)
  10847.         turnRight(1)
  10848.     else
  10849.         turnRight(1)
  10850.         forward(5, 1)
  10851.         turnLeft(1)
  10852.         forward(1, 1)
  10853.         turnRight(1)
  10854.     end
  10855.     if checkTorches then
  10856.         checkTorch("home")
  10857.     else
  10858.         forward(2, 1)
  10859.         turnRight(1)
  10860.     end
  10861. end
  10862.  
  10863. function reposition(direction, itemList, fillCobble)
  10864.     --used on suface to reposition turtle while clearing blocks above as well to create space for player
  10865.     saveToLog("reposition "..direction.." at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass(), false)
  10866.     for i = 1, string.len(direction) do
  10867.         move = string.sub(direction, i, i)
  10868.         if move == "R" then
  10869.             turnRight(1)
  10870.             saveToLog("reposition turnRight. Facing "..location:getCompass(), false)
  10871.         elseif move == "L" then
  10872.             turnLeft(1)
  10873.             saveToLog("reposition turnLeft. Facing "..location:getCompass(), false)
  10874.         elseif move == "B" then
  10875.             back(1)
  10876.             saveToLog("reposition turn round. Facing "..location:getCompass(), false)
  10877.         else --F if none of above
  10878.             saveToLog("reposition at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass(), false)
  10879.             dig.digNew{self = dig, direction = "up", slotNo = 1, checkForItems = itemList, callFrom = "reposition"}
  10880.             checkForLooseSaplings()
  10881.             if dig:getSuccess() then --success digging. If dirt/grass itemcount ++
  10882.                 --see if  1 item in slot 16 matches anything
  10883.                 if dig:getDugItem() == "wood2" then
  10884.                     saveToLog("reposition: harvesting new tree type, wood2")
  10885.                     harvestTree("wood2")
  10886.                 end
  10887.             end
  10888.             dig.digNew{self = dig, checkForItems = "flowers,sand", callFrom = "reposition"}
  10889.             if fillCobble then
  10890.                 dig.digNew{self = dig, direction = "down", checkForItems = "flowers,sand", callFrom = "reposition"}
  10891.                 turtle.select(slot:getItemSlot("cobble"))
  10892.                 if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
  10893.                     turtle.placeDown()
  10894.                 end
  10895.             end
  10896.             forward(1, 1, itemList, "reposition")
  10897.         end
  10898.     end
  10899. end
  10900.  
  10901. function repositionUnderground(direction, itemList, calledFrom)
  10902.     saveToLog("repositionUnderground "..direction.." at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
  10903.     if location:getY() < 5 then
  10904.         up(6 - location:getY(), 1)
  10905.         saveToLog("repositionUnderground up ".. 6 - location:getY().." now at y = "..location:getY())
  10906.     end
  10907.     for i = 1, string.len(direction) do
  10908.         move = string.sub(direction, i, i)
  10909.         if move == "R" then
  10910.             turnRight(1)
  10911.             saveToLog("repositionUnderground turnRight. Facing "..location:getCompass())
  10912.         elseif move == "L" then
  10913.             turnLeft(1)
  10914.             saveToLog("repositionUnderground turnLeft. Facing "..location:getCompass())
  10915.         elseif move == "B" then
  10916.             turnLeft(2)
  10917.             saveToLog("repositionUnderground turn round. Facing "..location:getCompass())
  10918.         else --F if none of above
  10919.             if not checkBedrock(itemList) then
  10920.                 dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
  10921.                 forward(1, 1)
  10922.                 dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
  10923.             else --is bedrock
  10924.                 saveToLog("repositionUnderground bedrock in front, Facing "..location:getCompass())
  10925.                 up(1, 1, itemList, calledFrom)
  10926.                 forward(1, 1, itemList, calledFrom)
  10927.                 dig.digNew{self = dig, direction = "up", callFrom = calledFrom}
  10928.             end
  10929.             saveToLog("repositionUnderground at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
  10930.         end
  10931.     end
  10932. end
  10933.  
  10934. function restoreTorches(side, colNo)
  10935.     local torchesNeeded = 0
  10936.     local torchesOnboard = 0
  10937.     local torchPlaced = false
  10938.     local doContinue = true
  10939.    
  10940.     changeDirection("faceMine")
  10941.     -- checking before harvestArea()
  10942.     -- if torch(es) not present on correct side(s), need to add to onboard stock
  10943.     saveToLog("restoreTorches: started. side = "..side.." colNo = "..colNo, false, true)
  10944.     if side == "rightside" and colNo == 0 then --first run on right - no torches needed
  10945.         doContinue = false
  10946.     end
  10947.     if doContinue then
  10948.         if placeStorage:getMarkerPlaced("torches") then --torches made already
  10949.             getItemsFromStorage{fromStore = storageTorches, item1 = "torches"} --get any out of store
  10950.             if slot:getItemCount("torches") > 0 then
  10951.                 torchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
  10952.             end
  10953.             turnRight(1)
  10954.             if not turtle.detect() then --torch missing
  10955.                 torchesNeeded = 1
  10956.             end
  10957.             turnLeft(1)
  10958.             if side == "bothsides" or side == "leftside" then --left side harvest / diamond mining only
  10959.                 turnLeft(1)
  10960.                 if not turtle.detect() then --torch missing
  10961.                     torchesNeeded = torchesNeeded + 1
  10962.                 end
  10963.                 turnRight(1)
  10964.             end
  10965.             saveToLog("restoreTorches: torches needed = "..torchesNeeded.." Onboard = "..torchesOnboard)
  10966.             if torchesNeeded > 0 then
  10967.                 if torchesOnboard >= torchesNeeded then
  10968.                     torchesNeeded = 0
  10969.                     turtle.select(slot:getItemSlot("torches"))
  10970.                     turnRight(1)
  10971.                     if not turtle.detect() then --torch missing
  10972.                         turtle.place()
  10973.                         torchPlaced = true
  10974.                     end
  10975.                     turnLeft(1)
  10976.                     if side == "bothsides" or side == "leftside" then --left side harvest / diamond mining only
  10977.                         turnLeft(1)
  10978.                         if not turtle.detect() then --torch missing
  10979.                             turtle.place()
  10980.                             torchPlaced = true
  10981.                         end
  10982.                         turnRight(1)
  10983.                     end
  10984.                 end
  10985.             end    
  10986.             if torchPlaced then
  10987.                 slot.update{self = slot, item = "torches", delete = true}
  10988.                 if slot:getItemSlot("torches") == 0 then
  10989.                     sortInventory(true)
  10990.                 end
  10991.             end
  10992.         else --no torches yet
  10993.             saveToLog("restoreTorches: no torches made yet", true)
  10994.         end
  10995.     end
  10996.    
  10997.     return torchesNeeded
  10998. end
  10999.  
  11000. function returnToGroundLevel()
  11001.     while location:getY() > coordHome:getY() do
  11002.         down(1, 1)
  11003.     end
  11004.     while location:getY() < coordHome:getY() do
  11005.         up(1, 1)
  11006.     end
  11007. end
  11008.  
  11009. function saveToLog(text, toScreen, verbose)
  11010.     toScreen = toScreen or true
  11011.     verbose = verbose or false
  11012.     text = text or "Nil instead of text supplied, check your code!"
  11013.    
  11014.     if toScreen then
  11015.         print(text)
  11016.     end
  11017.     if fso:getUseLog() then
  11018.         fso:appendLine(text, verbose)
  11019.     end
  11020. end
  11021.  
  11022. function selectPlanks(planksType)
  11023.     local usePlanks = ""
  11024.     local woodAvailable = 0
  11025.     local wood2Available = 0
  11026.     local woodType = ""
  11027.    
  11028.     woodType, woodAvailable, wood2Available = getWoodAvailable{}
  11029.    
  11030.     if planksType == "planks" then
  11031.         if woodAvailable > 0 then
  11032.             usePlanks = "planks"
  11033.         else
  11034.             turtle.select(slot.getItemSlot(slot, "planks"))
  11035.             turtle.refuel()
  11036.             slot.update{self = slot, item = "planks", delete = true}
  11037.             --updateSlot(itemSlot["planks"], "planks", true)
  11038.             if woodType == "wood2" then
  11039.                 usePlanks = "planks2"
  11040.             end
  11041.         end
  11042.     elseif planksType == "planks2" then
  11043.         if wood2Available > 0 then
  11044.             usePlanks = "planks2"
  11045.         else
  11046.             turtle.select(slot.getItemSlot(slot, "planks2"))
  11047.             turtle.refuel()
  11048.             slot.update{self = slot, item = "planks2", delete = true}
  11049.             --updateSlot(itemSlot["planks2"], "planks2", true)
  11050.             if woodType == "wood" then
  11051.                 usePlanks = "planks"
  11052.             end
  11053.         end
  11054.     else --planksType = ""
  11055.         if woodType == "wood" then
  11056.             usePlanks = "planks"
  11057.         else
  11058.             usePlanks = "planks2"
  11059.         end
  11060.     end
  11061.    
  11062.     return usePlanks --empty string if not enough wood
  11063. end
  11064.  
  11065. function smelt(oreType, quantity, woodType)
  11066.     --charcoal, iron, stone, glass
  11067.     --assume function called with turtle under furnace, at ground level
  11068.     --move next to furnace to place planks
  11069.     --move to top of furnace to place wood
  11070.     --move under furnace to remove charcoal, stone, glass, iron
  11071.     local smeltType = ""
  11072.     local continue = true
  11073.     local waitTime = 0
  11074.     local planksNeeded = 0 --total needed for smelting
  11075.     local woodNeeded = 0
  11076.     local woodAvailable = 0
  11077.     local wood2Available = 0
  11078.     local planksAvailable = 0
  11079.     local planks2Available = 0
  11080.     local collectionSlot = 0
  11081.     local planksType = "planks"
  11082.     local success = false
  11083.     local checkPlanksAvailable = 0
  11084.     local checkPlanksType = ""
  11085.     local emptySlot = 0
  11086.     local returnToStore = false
  11087.     local getItem = {}
  11088.    
  11089.    
  11090.     if woodType == nil then --use any wood available
  11091.         woodType, woodAvailable, wood2Available = getWoodAvailable{}
  11092.     end
  11093.     if woodType == "store" then --no wood onboard, get from store
  11094.         saveToLog("smelt: fetching wood from storageWood")
  11095.         forward(6, 1)
  11096.         getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  11097.         turnRight(2)
  11098.         forward(6, 1)
  11099.         turnRight(2)
  11100.         woodType = "wood"
  11101.         planksType = "planks"
  11102.         returnToStore = true
  11103.     else
  11104.         if woodType == "wood" then--use specific wood
  11105.             woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 1
  11106.             planksType = "planks"
  11107.         elseif woodType == "wood2" then
  11108.             woodAvailable = turtle.getItemCount(slot:getItemSlot("wood2")) - 1
  11109.             planksType = "planks2"
  11110.         end
  11111.     end
  11112.  
  11113.     saveToLog("smelt: oreType = "..oreType.." quantity = "..quantity.." woodType = "..woodType.." planksType = "..planksType, false, true)
  11114.     if furnace1:getSmeltOutItemCount() > 0 then
  11115.         if dig.digNew{self = dig, suck = true, direction = "up"} then--could be charcoal, iron, stone, glass
  11116.             saveToLog("smelt: "..furnace1:getSmeltOutItem().." removed from furnace")
  11117.             furnace1:setSmeltOutItem("")
  11118.             furnace1:setSmeltOutItemCount(0)
  11119.             --sortInventory(true)
  11120.         end
  11121.     end
  11122.     back(1)
  11123.     up(2, 1)
  11124.     forward(1, 1) --now on top
  11125.     planksNeeded = quantity
  11126.     if oreType == "charcoal" then --eg quantity = 2, needs 2 wood/wood2 + 2 planks ASSUME only called if wood in stock
  11127.         smeltType = "charcoal"
  11128.         if quantity == 0 then -- smelt all available wood/wood2
  11129.             --calculate amount of wood needed to convert to planks and burn
  11130.             woodAvailable = woodAvailable + 1 -- need to use all supplies
  11131.             planksNeeded = math.ceil(woodAvailable / 3) * 2 --eg 43 wood needs 43 / 3 (15) * 2 = 30 planks
  11132.             quantity = woodAvailable - math.ceil(planksNeeded / 4) --eg 30/4 (8) 43 - 8 = 35 wood to be converted
  11133.             planksNeeded = math.ceil(planksNeeded / 4) * 4
  11134.             turtle.select(slot.getItemSlot(slot, woodType))
  11135.             turtle.dropDown(quantity)
  11136.             slot.update{self = slot, item = woodType, delete = true}
  11137.         elseif quantity == 64 then -- smelt all available wood, but leave 1 behind
  11138.             --calculate amount of wood needed to convert to planks and burn
  11139.             --woodAvailable = woodAvailable - 1 -- need to use all supplies
  11140.             planksNeeded = math.ceil(woodAvailable / 3) * 2 --eg 43 wood needs 43 / 3 (15) * 2 = 30 planks
  11141.             quantity = woodAvailable - math.ceil(planksNeeded / 4) --eg 30/4 (8) 43 - 8 = 35 wood to be converted
  11142.             planksNeeded = math.ceil(planksNeeded / 4) * 4
  11143.             turtle.select(slot:getItemSlot(woodType))
  11144.             turtle.dropDown(quantity)
  11145.             slot.update{self = slot, item = woodType, delete = true}
  11146.         else --smelt fixed amount
  11147.             saveToLog("smelt: dropping "..quantity.." "..woodType.." into furnace")
  11148.             turtle.select(slot.getItemSlot(slot, woodType))
  11149.             turtle.dropDown(quantity)
  11150.             slot.update{self = slot, item = woodType, delete = true}
  11151.         end
  11152.         furnace1:setSmeltOutItem("charcoal")
  11153.     elseif oreType == "cobble"then
  11154.         smeltType = "stone"
  11155.         turtle.select(slot.getItemSlot(slot, "cobble"))
  11156.         turtle.dropDown(quantity)
  11157.         slot.update{self = slot, item = "cobble"}
  11158.         saveToLog("smelt: "..quantity.." cobble dropped into furnace")
  11159.         furnace1:setSmeltOutItem("stone")
  11160.     elseif oreType == "?ironore" then
  11161.         smeltType = "?iron"
  11162.         turtle.select(slot.getItemSlot(slot, "?ironore"))
  11163.         turtle.dropDown(quantity)
  11164.         slot.update{self = slot, item = "?ironore"}
  11165.         saveToLog("smelt: "..quantity.." provisional iron ore dropped into furnace")
  11166.         furnace1:setSmeltOutItem("iron")
  11167.     elseif oreType == "ironore"  then
  11168.         smeltType = "iron"
  11169.         turtle.select(slot.getItemSlot(slot, "ironore"))
  11170.         turtle.dropDown(quantity)
  11171.         slot.update{self = slot, item = "ironore"}
  11172.         saveToLog("smelt: "..quantity.." iron ore dropped into furnace")
  11173.         --[[if not objectives["smelt iron"] then
  11174.             objectives["smelt iron"] = true
  11175.             saveToLog('OBJECTIVES["smelt iron"] = true')
  11176.         end]]--
  11177.     elseif oreType == "?goldore" then
  11178.         smeltType = "?gold"
  11179.         turtle.select(slot.getItemSlot(slot, "?goldore"))
  11180.         turtle.dropDown(quantity)
  11181.         slot.update{self = slot, item = "?goldore"}
  11182.         saveToLog("smelt: "..quantity.." ?gold ore dropped into furnace")
  11183.         furnace1:setSmeltOutItem("?gold")
  11184.     elseif oreType == "goldore" then
  11185.         smeltType = "gold"
  11186.         turtle.select(slot.getItemSlot(slot, "goldore"))
  11187.         turtle.dropDown(quantity)
  11188.         slot.update{self = slot, item = "goldore"}
  11189.         saveToLog("smelt: "..quantity.." gold ore dropped into furnace")
  11190.         furnace1:setSmeltOutItem("gold")
  11191.     elseif oreType == "sand" then
  11192.         smeltType = "glass"
  11193.         turtle.select(slot.getItemSlot(slot, "sand"))
  11194.         turtle.dropDown(quantity)
  11195.         slot.update{self = slot, item = "sand"}
  11196.         saveToLog("smelt: "..quantity.." sand dropped into furnace")
  11197.         furnace1:setSmeltOutItem("glass")
  11198.     end
  11199.     furnace1:setSmeltInItem(oreType)
  11200.     furnace1:setSmeltInItemCount(quantity)
  11201.     furnace1:setSmeltOutItemCount(quantity)
  11202.     back(1)
  11203.     down(1, 1)--in front of furnace
  11204.    
  11205.     saveToLog("smelt: furnace1:getFuelCount() = "..tostring(furnace1:getFuelCount()))
  11206.     if furnace1:getFuelCount() < planksNeeded then --not enough for smelting so refuel turtle
  11207.         turtle.select(16)
  11208.         turtle.suck()
  11209.         turtle.refuel()
  11210.         furnace1:setFuelCount(0)
  11211.         furnace1:setFuelType("")
  11212.     elseif furnace1:getFuelCount() >= planksNeeded then
  11213.         planksNeeded = 0
  11214.     end
  11215.     saveToLog("smelt: planksNeeded = "..planksNeeded)
  11216.     if planksNeeded > 0 then
  11217.         checkPlanksAvailable, checkPlanksType = getPlanksAvailable()
  11218.         if checkPlanksAvailable >=  planksNeeded then
  11219.             turtle.select(slot:getItemSlot(checkPlanksType))
  11220.             turtle.drop(planksNeeded)
  11221.             slot.update{self = slot, item = checkPlanksType, delete = true}
  11222.             saveToLog("smelt: "..planksNeeded.." planks added to fuel furnace")
  11223.             saveToLog("Smelting will take "..planksNeeded * 10 .." secs")
  11224.             planksNeeded = 0
  11225.             furnace1:setFuelType(checkPlanksType)
  11226.             furnace1:setFuelCount(planksNeeded)
  11227.         end
  11228.     end
  11229.     if planksNeeded > 0 then --reset to 0 only if planks already in furnace
  11230.         -- check if wood in stock
  11231.         if slot:getItemSlot("wood") == 0 then --get wood from storage
  11232.             saveToLog("smelt: getting wood from storage")
  11233.             down(1, 1)
  11234.             forward(1, 1) --back under furnace
  11235.             forward(6, 1)
  11236.             getItem = getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
  11237.             turnRight(2)
  11238.             forward(6)
  11239.             turnRight(2)
  11240.             woodType = "wood"
  11241.             back(1,1)
  11242.             up(1, 1)
  11243.         end
  11244.         planksNeeded = math.ceil(planksNeeded / 4) * 4 --eg 1/4 = 1 ,*4 = 4 planks
  11245.         saveToLog("smelt: planksNeeded = "..planksNeeded.." crafting more")
  11246.         turnRight(1) --side on to furnace
  11247.         craft{craftItem = planksType, craftQuantity = planksNeeded, sourceItem1 = woodType, destSlot = 0, doSort = true}
  11248.         turnLeft(1)
  11249.         turtle.select(slot:getItemSlot(planksType))
  11250.         turtle.drop() --drop existing planks
  11251.         slot.update{self = slot, item = planksType, delete = true}
  11252.         furnace1:setFuelType(planksType)
  11253.         furnace1:setFuelCount(planksNeeded)
  11254.     end
  11255.     down(1, 1)
  11256.     forward(1, 1) --back under furnace
  11257.     collectionSlot = getFirstEmptySlot()
  11258.     if slot.getItemSlot(slot, smeltType) > 0 then
  11259.         collectionSlot = slot.getItemSlot(slot, smeltType)
  11260.     end
  11261.     turtle.select(collectionSlot)
  11262.     repeat
  11263.         waitTime = waitTime + 1
  11264.         sleep(1)
  11265.         if waitTime == 10 then --every 10 secs check if any output
  11266.             if turtle.suckUp() then --continue to wait
  11267.                 continue = true
  11268.                 waitTime = 0
  11269.             else --either no product or all done
  11270.                 continue = false
  11271.             end
  11272.         end
  11273.     until not continue
  11274.     if turtle.getItemCount(collectionSlot) > 0 then
  11275.         success = true
  11276.         slot.update{self = slot, slotNo = collectionSlot, item = smeltType}
  11277.     else --item did not smelt
  11278.         back(1)
  11279.         up(2, 1)
  11280.         forward(1)
  11281.         turtle.select(1)
  11282.         turtle.suckDown()
  11283.         back(1)
  11284.         down(2)
  11285.         forward(1)
  11286.     end
  11287.     --check fuel status
  11288.     emptySlot = getFirstEmptySlot()
  11289.     if emptySlot > 0 then
  11290.         back(1)
  11291.         up(1)
  11292.         turtle.select(emptySlot)
  11293.         turtle.suck()
  11294.         if turtle.getItemCount(emptySlot) == 0 then
  11295.             furnace1:setFuelType("")
  11296.         end
  11297.         furnace1:setFuelCount(turtle.getItemCount(emptySlot))
  11298.         turtle.drop()
  11299.         down(1)
  11300.         forward(1)
  11301.     end
  11302.     furnace1:setSmeltInItem("")
  11303.     furnace1:setSmeltInItemCount(0)
  11304.     furnace1:setSmeltOutItem("")
  11305.     furnace1:setSmeltOutItemCount(0)
  11306.     if returnToStore then
  11307.         saveToLog("smelt: returning wood from storageWood")
  11308.         forward(6, 1)
  11309.         storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
  11310.         turnRight(2)
  11311.         forward(6, 1)
  11312.         turnRight(2)
  11313.     end
  11314.     saveToLog("smelt: success = "..tostring(success))
  11315.     return success
  11316. end
  11317.  
  11318. function sortInventory(moveItems)
  11319.     local useSlot = 0
  11320.     local usedSlots = 0
  11321.     local emptySlot = 0
  11322.     local checkItem = ""
  11323.     local compareItem = ""
  11324.     local moveAmount = 0
  11325.     local excessAmount = 0
  11326.     local amount = 65
  11327.     local firstSlot = 0
  11328.     local lastSlot = 16
  11329.     local toSlotNo = 0
  11330.     local fromSlotNo = 0
  11331.     local slotState = {}
  11332.     local unknownSlotCount = {}
  11333.     local count = 0
  11334.     local index = 0
  11335.     local fromAmount = 0
  11336.     local toAmount = 0
  11337.     local doAgain = false
  11338.    
  11339.     if moveItems == nil then
  11340.         moveItems = false
  11341.     end
  11342.     firstNewItemSlot = 0
  11343.     lastNewItemSlot = 0
  11344.     saveToLog("sortInventory: Sorting started, moveItems = "..tostring(moveItems), false)
  11345.     turnRight(2) --so if items are dropped do not get sucked up
  11346.  
  11347.     --gather like items together first
  11348.     for i = 1, 16 do
  11349.         checkItem = slot:getSlotContains(i)  -- eg "", "item1", "signs" contents of slot in current loop
  11350.         if turtle.getItemCount(i) > 0 then
  11351.             turtle.select(i) --now compare with slots above
  11352.             for j = i + 1, 15 do
  11353.                 compareItem = slot:getSlotContains(j)  -- eg "", "item1", "signs" contents of slot being compared
  11354.                 if turtle.compareTo(j) then --more of the same in slot j
  11355.                     if slot:getSlotStatus(i) == "known" then
  11356.                     --if checkItem ~= "" and string.find(checkItem, "item") ~= nil then --known item in i
  11357.                         moveAmount = 63 - turtle.getItemCount(i) --40 dirt in slot i, 40 dirt in slot j, moveAmount = 23
  11358.                         turtle.select(j)
  11359.                         if moveAmount > 0 then
  11360.                             turtle.transferTo(i, moveAmount) --move up to 63
  11361.                         end
  11362.                         excessAmount = turtle.getItemCount(j)
  11363.                         if excessAmount > 0 then
  11364.                             if checkItem == "wood" or checkItem =="wood2" then
  11365.                                 turtle.refuel()
  11366.                                 saveToLog("sortInventory: refuelling with excess "..checkItem)
  11367.                             else
  11368.                                 while turtle.drop() do
  11369.                                     saveToLog("sortInventory: dumping "..excessAmount.." of excess "..checkItem, true)
  11370.                                 end
  11371.                             end
  11372.                             slot.update{self = slot, slotNo = j, delete = true}
  11373.                         end
  11374.                         slot.update{self = slot, slotNo = i, item = checkItem}
  11375.                     else --"itemX" or ""
  11376.                         moveAmount = 63 - turtle.getItemCount(i)
  11377.                         turtle.select(j)
  11378.                         if moveAmount > 0 then
  11379.                             turtle.transferTo(i, moveAmount)
  11380.                         end
  11381.                         excessAmount = turtle.getItemCount(j)
  11382.                         if excessAmount > 0 then
  11383.                             while turtle.drop() do
  11384.                                 saveToLog("sortInventory: dumping "..excessAmount.." of excess "..checkItem)
  11385.                             end
  11386.                             --slot.update{self = slot, item = "item", slotNo = j, delete = true}
  11387.                         end
  11388.                         slot.update{self = slot, slotNo = i, item = checkItem}
  11389.                     end
  11390.                 end
  11391.             end
  11392.         end
  11393.         checkItem = slot:getSlotContains(i)  -- re-check, anything not identified dump
  11394.         if checkItem == "" then
  11395.             turtle.select(i)
  11396.             while turtle.drop() do
  11397.                 saveToLog(" sortInventory: dumping unknown item in slot "..i, true)
  11398.             end
  11399.         end
  11400.     end
  11401.     --loose items sorted into correct slots, excess dumped
  11402.     --use coal for fuel if above 24
  11403.     useSlot = slot.getItemSlot(slot, "coal")
  11404.     if useSlot > 0 then
  11405.         if turtle.getItemCount(useSlot) > 24 then
  11406.             turtle.select(useSlot)
  11407.             turtle.refuel(turtle.getItemCount(useSlot) - 24)
  11408.             saveToLog("sortInventory coal used for fuel. Level = "..turtle.getFuelLevel())
  11409.             slot.update{self = slot, item = "coal"}
  11410.         end
  11411.     end
  11412.     --[[saveToLog("sortInventory: start - debug log", false)
  11413.     for i = 1, 16 do
  11414.         saveToLog(" slotStatus["..i.."] = "..slot:getSlotStatus(i), false)
  11415.     end]]--
  11416.  
  11417.    
  11418.     if moveItems then
  11419.         --check for empty
  11420.         repeat
  11421.             doAgain = false
  11422.             for i = 1, 15 do
  11423.                 if slot:getSlotStatus(i) == "empty" then
  11424.                     if slot:getSlotStatus(i + 1) ~= "empty" then --move known item
  11425.                         turtle.select(i + 1)
  11426.                         turtle.transferTo(i)
  11427.                         slot.transfer{self = slot, fromSlot = i + 1, transferTo = i}
  11428.                         doAgain = true
  11429.                     end
  11430.                 end
  11431.             end
  11432.         until not doAgain
  11433.         --move known into correct place
  11434.         repeat
  11435.             doAgain = false
  11436.             for i = 16, 1, -1 do
  11437.                 if slot:getSlotStatus(i) == "known" then
  11438.                     if slot:getSlotStatus(i - 1) == "unknown" then --move known item
  11439.                         turtle.select(i)
  11440.                         turtle.transferTo(16)
  11441.                         slot.transfer{self = slot, fromSlot = i, transferTo = 16}
  11442.                         turtle.select(i - 1)
  11443.                         turtle.transferTo(i)
  11444.                         slot.transfer{self = slot, fromSlot = i - 1, transferTo = i}
  11445.                         turtle.select(16)
  11446.                         turtle.transferTo(i - 1)
  11447.                         slot.transfer{self = slot, fromSlot = 16, transferTo = i - 1}
  11448.                         doAgain = true
  11449.                     end
  11450.                 end
  11451.             end
  11452.         until not doAgain
  11453.         --move unknown depending on itemCount
  11454.         repeat
  11455.             doAgain = false
  11456.             for i = 16, 1, -1 do
  11457.                 if slot:getSlotStatus(i) == "unknown" then
  11458.                     fromAmount = turtle.getItemCount(i)
  11459.                     if slot:getSlotStatus(i - 1) == "unknown" then --move item
  11460.                         toAmount = turtle.getItemCount(i - 1)
  11461.                         if fromAmount < toAmount then
  11462.                             turtle.select(i)
  11463.                             turtle.transferTo(16)
  11464.                             slot.transfer{self = slot, fromSlot = i, transferTo = 16}
  11465.                             turtle.select(i - 1)
  11466.                             turtle.transferTo(i)
  11467.                             slot.transfer{self = slot, fromSlot = i - 1, transferTo = i}
  11468.                             turtle.select(16)
  11469.                             turtle.transferTo(i - 1)
  11470.                             slot.transfer{self = slot, fromSlot = 16, transferTo = i - 1}
  11471.                             doAgain = true
  11472.                         end
  11473.                     end
  11474.                 end
  11475.             end
  11476.         until not doAgain
  11477.         saveToLog("sortInventory: resetting unknown items", false)
  11478.         slot:resetUnknownItems()
  11479.     end
  11480.     saveToLog("sortInventory: debug log", false)
  11481.     for i = 1, 16 do
  11482.         saveToLog(" sortInventory: slot "..i.." slotCount = "..slot:getSlotCount(i).. " of "..slot:getSlotContains(i).."        slotState = "..slot:getSlotStatus(i), false)
  11483.     end
  11484.     turnRight(2)
  11485.     turtle.select(1)
  11486. end
  11487.  
  11488. function storeItem(arg)
  11489.     -- storeItem{toStore = useStore, item = "moss stone", quantity = 0, updateSlot = true, doSort = true}
  11490.     local storeIndex = 0
  11491.     local slotNum = 0
  11492.    
  11493.     if arg.doSort == nil then
  11494.         arg.doSort = false
  11495.     end
  11496.     if arg.updateSlot == nil then
  11497.         arg.updateSlot = false
  11498.     end
  11499.    
  11500.     if arg.slotNo ~= nil then
  11501.         if arg.quantity == nil or arg.quantity == 0 then
  11502.             arg.quantity = turtle.getItemCount(arg.slotNo)
  11503.         end
  11504.         saveToLog("Storing "..arg.quantity.." "..arg.item.." from turtle slot "..arg.slotNo.." to chest "..arg.toStore:getStoreName(), false)
  11505.         turtle.select(arg.slotNo)
  11506.         turtle.dropDown()
  11507.         arg.toStore.addItem(arg.toStore, arg.item, arg.quantity)
  11508.     else
  11509.         if slot:getItemSlot(arg.item) ~= nil then --itemSlot["moss stone"] exists
  11510.             slotNum = slot:getItemSlot(arg.item) --eg turtle slot 14 contains moss stone
  11511.             if slotNum > 0 then --eg sticks in turtle slot 8
  11512.                 if arg.quantity == 0 then --drop all
  11513.                     arg.quantity = turtle.getItemCount(slotNum)
  11514.                 end
  11515.                 if arg.quantity > 0 then
  11516.                     saveToLog("storeItem: arg.quantity = "..arg.quantity, false)
  11517.                     saveToLog("storeItem: arg.item = "..arg.item, false)
  11518.                     saveToLog("storeItem: slotNum = "..slotNum, false)
  11519.                     saveToLog("storeItem: arg.toStore:getStoreName() = "..tostring(arg.toStore.getStoreName(arg.toStore)), false)
  11520.                     saveToLog("Storing "..arg.quantity.." "..arg.item.." from turtle slot "..slotNum.." to chest "..arg.toStore:getStoreName(), false)
  11521.                     turtle.select(slotNum)
  11522.                     turtle.dropDown(arg.quantity)
  11523.                     arg.toStore.addItem(arg.toStore, arg.item, arg.quantity, slot:getItemSlot(arg.item))
  11524.                 end
  11525.                 if arg.updateSlot then
  11526.                     slot.update{self = slot, item = arg.item, delete = true}
  11527.                 end
  11528.                 if arg.doSort then
  11529.                     sortInventory(true)
  11530.                 end
  11531.             end
  11532.         end
  11533.     end
  11534. end
  11535.  
  11536. function storeRedstone()
  11537.     --called while returning from mineshaft to furnace
  11538.     if slot:getItemSlot("redstone") > 0 then
  11539.         if not placeStorage:getMarkerPlaced("redstone") then
  11540.             if turtle.getItemCount(slot:getItemSlot("redstone")) > 9 then
  11541.                 if craft{craftItem = "redstone block", craftQuantity = 1, sourceItem1 = "redstone", destSlot = 0} then
  11542.                     back(1)
  11543.                     dig.digNew{self = dig, direction = "down", callFrom = "storeRedstone"}
  11544.                     turtle.select(slot:getItemSlot("redstone block"))
  11545.                     turtle.placeDown()
  11546.                     slot.update{self = slot, item = "redstone block", delete = true}
  11547.                     placeStorage:setMarkerPlaced("redstone")
  11548.                     saveToLog("storeRedstone: marker redstone placed")
  11549.                     forward(1)
  11550.                 end
  11551.             end
  11552.         end
  11553.         if turtle.getItemCount(slot:getItemSlot("redstone")) > 1 then
  11554.             saveToLog("storeRedstone: storing redstone in storageRedstone")
  11555.             storeItem{toStore = storageRedstone, item = "redstone", quantity = turtle.getItemCount(slot:getItemSlot("redstone")) - 1, updateSlot = true, doSort = false}
  11556.         end
  11557.     end
  11558. end
  11559.  
  11560. function storeUnknownItem(checkItem)
  11561.     -- if sand, torches, wood or signs collected during mining but not identified are deposited here
  11562.     local tempStore = {}
  11563.    
  11564.     if checkItem == "sand" then
  11565.         tempStore = storageSand
  11566.     elseif checkItem == "wood" then
  11567.         tempStore = storageWood
  11568.     elseif checkItem == "signs" then
  11569.         tempStore = storageSigns
  11570.     elseif checkItem == "torches" then
  11571.         tempStore = storageTorches
  11572.     end
  11573.     if slot:getItemSlot(checkItem) > 0 then
  11574.         if checkItem ~= "torches" then
  11575.             saveToLog("storeUnknownItem('"..checkItem.."'): already onboard, storing in "..tempStore:getStoreName())
  11576.             forward(1, 1)
  11577.             storeItem{toStore = tempStore, item = checkItem, quantity = 0, updateSlot = true, doSort = true}
  11578.             back(1)-- over markerItem
  11579.         end
  11580.     else
  11581.         if placeStorage:getMarkerPlaced(checkItem) then
  11582.             for i = 1, 16 do
  11583.                 if slot:getSlotStatus() == "unknown" then
  11584.                     turtle.select(i)
  11585.                     if turtle.compareDown() then --sand, wood, signs, torches found
  11586.                         slot.update{self = slot, slotNo = i, newItem = checkItem}
  11587.                         sortInventory(true)
  11588.                         forward(1, 1)
  11589.                         storeItem{toStore = tempStore, item = checkItem, quantity = 0, updateSlot = true, doSort = true}
  11590.                         back(1)-- over markerItem
  11591.                     end
  11592.                 end
  11593.             end
  11594.         else --marker not placed
  11595.             saveToLog("storeUnknownItem('"..checkItem.."'): marker not placed")
  11596.         end
  11597.     end
  11598. end
  11599.  
  11600. function turnLeft(steps)
  11601.     for i = 1, steps do
  11602.         turtle.turnLeft()
  11603.         location:setFacing(location:getFacing() - 1)
  11604.     end
  11605. end
  11606.  
  11607. function turnRight(steps)
  11608.     for i = 1, steps do
  11609.         turtle.turnRight()
  11610.         location:setFacing(location:getFacing() + 1)
  11611.     end
  11612. end
  11613.  
  11614. function up(steps, useSlot, itemList, calledFrom)
  11615.     local success = false
  11616.     local maxTries = 10
  11617.    
  11618.     useSlot = useSlot or 1
  11619.    
  11620.     for i = 1,steps do
  11621.         if turtle.detectUp() then
  11622.             while turtle.detectUp() do
  11623.                 dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
  11624.                 --allow for sand and gravel
  11625.                 sleep(.7)
  11626.             end
  11627.             turtle.up()
  11628.         else       
  11629.             while not turtle.up() do
  11630.                 dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
  11631.                 --if turtle.attackUp() then
  11632.                 if attack() then
  11633.                     saveToLog("up: mob attacked above!", true)
  11634.                 end
  11635.                 maxTries = maxTries - 1
  11636.                 if maxTries <= 0 then
  11637.                     if turtle.getFuelLevel() == 0 then
  11638.                         if refuel(0) then
  11639.                             maxTries = 10
  11640.                         else
  11641.                             saveToLog("forward: Out of fuel. Game Over!", true)
  11642.                             error()
  11643.                         end
  11644.                     end
  11645.                 end
  11646.             end
  11647.             success = true
  11648.         end
  11649.         location:setY(location:getY() + 1)
  11650.         success = true
  11651.     end
  11652.    
  11653.     return success
  11654. end
  11655.  
  11656. function deBug(stage)
  11657.     --use this function to start at different stages while debugging
  11658.     if stage == "logFile" then
  11659.         --fso:setStartFileNameIndex(1) -- debugging using "logStart.txt" logFile
  11660.         --fso:setStartFileNameIndex(2) -- debugging using "logHarvestRight1.txt" logFile
  11661.         --fso:setStartFileNameIndex(3) -- debugging using "logHarvestRight2.txt" logFile
  11662.         --fso:setStartFileNameIndex(4) -- debugging using "logHarvestRight3.txt" logFile
  11663.         --fso:setStartFileNameIndex(5) -- debugging using "logHarvestRight4.txt" logFile
  11664.         --fso:setStartFileNameIndex(6) -- debugging using "logHarvestRight5.txt" logFile
  11665.         --fso:setStartFileNameIndex(7) -- debugging using "logHarvestRight6.txt" logFile
  11666.         --fso:setStartFileNameIndex(8) -- debugging using "logHarvestLeft1.txt" logFile
  11667.         --fso:setStartFileNameIndex(9) -- debugging using "logHarvestLeft2.txt" logFile
  11668.         --fso:setStartFileNameIndex(10) -- debugging using "logHarvestLeft3.txt" logFile
  11669.         --fso:setStartFileNameIndex(11) -- debugging using "logHarvestLeft4.txt" logFile
  11670.         fso:setStartFileNameIndex(12) -- debugging using "logCreateMine.txt" logFile
  11671.         --fso:setStartFileNameIndex(13) -- debugging using "logMiningLayer1.txt" logFile
  11672.         --fso:setStartFileNameIndex(14) -- debugging using "logMiningLayer2.txt" logFile
  11673.         --fso:setStartFileNameIndex(15) -- debugging using "logMiningLayer3.txt" logFile
  11674.         --fso:setStartFileNameIndex(16) -- debugging using "logMiningBedrock.txt" logFile
  11675.         --fso:setStartFileNameIndex(17) -- debugging using "logCraftMiningTurtle.txt" logFile
  11676.     else
  11677.         --set appropriate values if starting from different stages during debug process
  11678.         print("setting debug variables")
  11679.        
  11680.         slot.update{self = slot, slotNo = 1, item = "chests"}
  11681.         slot.update{self = slot, slotNo = 2, item = "dirt"}
  11682.         slot.update{self = slot, slotNo = 3, item = "cobble"}
  11683.         slot.update{self = slot, slotNo = 4, item = "gravel"}
  11684.         slot.update{self = slot, slotNo = 5, item = "stone"}
  11685.         slot.update{self = slot, slotNo = 6, item = "coal"}
  11686.         slot.update{self = slot, slotNo = 7, item = "ironore"}
  11687.         slot.update{self = slot, slotNo = 8, item = "buckets"}
  11688.         slot.update{self = slot, slotNo = 9, item = "item1"}
  11689.         slot.update{self = slot, slotNo = 10, item = "item2"}
  11690.         --slot.update{self = slot, slotNo = 11, item = "signs"}
  11691.         --slot.update{self = slot, slotNo = 12, item = "signs"}
  11692.         --slot.update{self = slot, slotNo = 13, item = "item3"}
  11693.         --slot.update{self = slot, slotNo = 14, item = "signs"}
  11694.        
  11695.         sortInventory(true)
  11696.        
  11697.         storageTorches:addItem("torches", 30, 1)
  11698.         storageSigns:addItem("signs", 3, 1)
  11699.         storageSticks:addItem("sticks", 20, 1)
  11700.         storageWood:addItem("wood", 42, 9)
  11701.         storageIronore:addItem("iron", 1, 1)
  11702.         --storageIronore:addItem("buckets", 1, 1)
  11703.         --storageSaplings:addItem("saplings", 23, 15)
  11704.         storageSand:addItem("sugar cane", 4, 1)
  11705.         storageSand:addItem("sand", 18, 14)
  11706.         --storageRedstone:addItem("redstone", 64, 15)
  11707.         --storageRedstone:addItem("redstone", 10, 15)
  11708.         --storagePickaxes:addItem("diamond pickaxe", 1, 14)
  11709.        
  11710.         --extraStorage1:addItem("goldore", 27, 14)
  11711.         --extraStorage1:addItem("nuggets", 9, 14)
  11712.         --extraStorage2:addItem("moss stone", 22, 14)
  11713.         --extraStorage2:addItem("walls", 6, 14)
  11714.         --extraStorage3:addItem("lapis", 41, 14)
  11715.        
  11716.         --mineshaft[1] = true
  11717.         --mineshaft[2] = true
  11718.         --mineshaft[3] = true
  11719.         --mineshaft[4] = true
  11720.         --mineshaft[5] = true
  11721.         --mineshaft[6] = true
  11722.         --mineshaft[7] = true
  11723.         --mineshaft[8] = true
  11724.         --mineshaft[9] = true
  11725.         --mineshaft[10] = true
  11726.        
  11727.         objectives["smelt iron"] = true
  11728.         --objectives["smelt gold"] = true
  11729.         placeStorage:setStoragePlaced("torches")
  11730.         placeStorage:setStoragePlaced("signs")
  11731.         placeStorage:setStoragePlaced("sticks")
  11732.         placeStorage:setStoragePlaced("saplings")
  11733.         placeStorage:setStoragePlaced("wood")
  11734.         placeStorage:setStoragePlaced("ironore")
  11735.         placeStorage:setStoragePlaced("sand")
  11736.         placeStorage:setStoragePlaced("redstone")
  11737.         placeStorage:setStoragePlaced("pickaxes")
  11738.        
  11739.         placeStorage:setMarkerPlaced("torches")
  11740.         placeStorage:setMarkerPlaced("signs")
  11741.         placeStorage:setMarkerPlaced("sticks")
  11742.         placeStorage:setMarkerPlaced("wood")
  11743.         placeStorage:setMarkerPlaced("ironore")
  11744.         placeStorage:setMarkerPlaced("sand")
  11745.         --placeStorage:setMarkerPlaced("redstone")
  11746.         --placeStorage:setMarkerPlaced("lapis")
  11747.        
  11748.         objectives["place extended storage"] = true
  11749.         objectives["choose wood"] = true
  11750.         objectives["goMining1"] = true
  11751.         objectives["clear treefarm"] = true
  11752.         objectives["pave treefarm"] = true
  11753.         --objectives["pave extendedStorage"] = true
  11754.         objectives["complete sand"] = true
  11755.         objectives["complete sugar cane"] = true
  11756.         --objectives["diamond pickaxe"] = true
  11757.        
  11758.         objectives["gravel"] = true
  11759.         objectives["sand"] = true
  11760.         objectives["sugar cane"] = true
  11761.         objectives["ironore"] = true
  11762.         --objectives["goldore"] = true
  11763.         --objectives["redstone"] = false
  11764.         --objectives["lapis"] = true
  11765.         --objectives["diamonds"] = true
  11766.         furnace1:setFuelType("planks")
  11767.         furnace1:setFuelCount(5)
  11768.        
  11769.         treeFarm:setDebug(0, 8) --x saplings y trees
  11770.         --if createMine() already done, put correct coords in here
  11771.         mineEntrance:setX(183)
  11772.         mineEntrance:setY(64)
  11773.         mineEntrance:setZ(275)
  11774.     end
  11775. end
  11776.  
  11777. function main()            
  11778.     initialise()            -- Setup global variables
  11779.     --deBug("logFile")      -- uncomment for debugging and starting at different stages
  11780.     getCoords()             -- Get input from player
  11781.     firstTree()             -- Harvests first tree, fuels, makes a chest
  11782.     findCobble()            -- Dig down far enough to find cobble
  11783.     checkGravel()           -- If gravel found, test to make sure
  11784.     confirmCobble()         -- Suspected cobble found so confirm, and place furnace
  11785.     clearBase()             -- Clear area around first tree 4X4
  11786.     --deBug("AllTrees")     -- uncomment for debugging and starting at different stages
  11787.     harvestAllTrees()       -- Harvest trees in 32 x 32 area until enough resources found for deep mining
  11788.     prepareMining()         -- Enough resources found to go diamond mining. Prepare mining area.
  11789.     mineForDiamonds()       -- Mining area prepared. Mine 3 layers, then to bedrock until min 6 diamonds found
  11790.     craftMiningTurtle()     -- All resources mined. Build new turtle(s) if minimum 3 diamonds found
  11791. end
  11792. --**************Functions and classes/objects above***************--
  11793. --****************************************************************--
  11794. --***********************PROGRAM START****************************--
  11795.  
  11796. main() -- Entire Program runs from here
Add Comment
Please, Sign In to add comment