Inksaver

SelfReplicate original 2014

Dec 9th, 2018
706
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 +</