Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = "start.lua 1.1.1"
- pastebinCode = "JVD1RPfg"
- -- http://pastebin.com/JVD1RPfg
- -- origially pasted in 2014. The updated 2018 version is using the original pastebin code: http://pastebin.com/2Zf5an8H
- local function initialise()
- -- create classes/objects
- createCoordObject()
- createFurnaceObject()
- createChestObject()
- createSlotObject()
- createLogfileObject()
- createTreeFarmObject()
- createDigObject()
- createPlaceStorageObject()
- -- set global variables. As each item is identified, set objectives["item"] = true
- objectives = {}
- local items = {}
- items = {"wood","wood2","dirt","cobble","stone","saplings","saplings2","seeds","sand","gravel","clay",
- "apples","coal","charcoal","ironore","?ironore","goldore","?goldore","iron","?iron","gold","?gold",
- "redstone","redstone block","?diamonds","diamonds","emeralds","sugar cane","planks","planks2","chests","sticks",
- "torches","furnaces","signs","item1","item2","item3","item4","item5","item6","item7","item8",
- "fences","nuggets","buckets","paper","computer","crafting table","diamond pickaxe","glass","glass panes","floppy disk",
- "disk drive","turtle","crafty mining turtle","?moss stone","moss stone","walls","obsidian","sandstone",
- "?lapis","lapis","lapis block","flowers","roses","red mushrooms","brown mushrooms"}
- for key, value in ipairs(items) do
- objectives[value] = false
- end
- --special objectives as script progresses:
- objectives["smelt iron"] = false -- confirms ironore identified, iron and buckets crafted
- objectives["smelt gold"] = false -- confirms goldore identified, crafted to gold nuggets and stored in extended storage
- objectives["place extended storage"] = false -- 5 chests placed behind starting point to store goldore, lapis, obsidian, mossy cobblestone
- objectives["choose wood"] = false -- After harvestAllTrees() finished, treeFarm tree type identified, wood2 and saplings2 eliminated
- objectives["goMining1"] = false -- During harvestAllTrees()
- objectives["clear treefarm"] = false -- Treefarm cleared and planted
- objectives["complete sand"] = false -- min 7 sand found, no more needed
- objectives["complete sugar cane"] = false -- min 3 sugar cane found, no more needed
- objectives["diamond pickaxe"] = false -- Diamond pickaxe created as part of identifying diamonds. Stored in storagePickaxes
- objectives["pave treefarm"] = false -- Decorates Treefarm, as not enough cobble when first planted
- objectives["pave extendedStorage"] = false -- Decorates extendedStorage area, if not enough cobble when created
- -- 10 mineshafts down to level 40 during harvestAllTrees() set to true if shaft sunk
- -- shaft may not be mined if insufficient fuel. Check for mined shafts not yet implemented
- mineshaft = {}
- for i = 1, 10 do
- mineshaft[i] = false
- end
- startTime = os.time() -- used to time functions, and allow adjustments to return to base every 24 hours minimum
- end
- function attack()
- local mobAttacked = false
- local itemCount = {}
- for i = 1, 16 do
- itemCount[i] = turtle.getItemCount(i)
- end
- turtle.select(1)
- if turtle.attack() then
- saveToLog("attack: Mob attacked in front!", true, false)
- sleep(1.5)
- while turtle.attack() do --in case mob in front
- saveToLog("attack: Mob attacked in front again!", true, false)
- sleep(1.5)
- end
- mobAttacked = true
- end
- if turtle.attackUp() then
- saveToLog("attack: Mob attacked above!", true, false)
- sleep(1.5)
- while turtle.attackUp() do --in case mob in front
- saveToLog("attack: Mob attacked above again!", true, false)
- sleep(1.5)
- end
- mobAttacked = true
- end
- if turtle.attackDown() then
- saveToLog("attack: Mob attacked below!", true, false)
- sleep(1.5)
- while turtle.attackDown() do --in case mob in front
- saveToLog("attack: Mob attacked below again!", true, false)
- sleep(1.5)
- end
- mobAttacked = true
- end
- if mobAttacked then --remove any mob drops
- for i = 1, 16 do
- if itemCount[i] == 0 then --check only previously empty slots
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- while turtle.dropUp() do
- saveToLog ("attack: dumping mob drops", true, false)
- end
- end
- end
- end
- end
- return mobAttacked
- end
- function back(steps, useSlot, itemList)
- useSlot = useSlot or 1
- itemList = itemList or ""
- for i = 1, steps do
- if not turtle.back() then --cant move back
- turnRight(2)
- forward(1, useSlot, itemList)
- turnRight(2)
- end
- changeCoords("back")
- end
- end
- function changeCoords(direction)
- --[[0 = go south (z increases)
- 1 = go west (x decreases)
- 2 = go north (z decreases
- 3 = go east (x increases)]]--
- if direction == "forward" then
- if location:getFacing() == 0 then
- --zCoord = zCoord + 1
- location:setZ(location:getZ() + 1)
- elseif location:getFacing() == 1 then
- --xCoord = xCoord - 1
- location:setX(location:getX() - 1)
- elseif location:getFacing() == 2 then
- --zCoord = zCoord - 1
- location:setZ(location:getZ() - 1)
- else
- --xCoord = xCoord + 1
- location:setX(location:getX() + 1)
- end
- elseif direction == "back" then
- if location:getFacing() == 0 then
- --zCoord = zCoord - 1
- location:setZ(location:getZ() - 1)
- elseif location:getFacing() == 1 then
- --xCoord = xCoord + 1
- location:setX(location:getX() + 1)
- elseif location:getFacing() == 2 then
- --zCoord = zCoord + 1
- location:setZ(location:getZ() + 1)
- else
- --xCoord = xCoord - 1
- location:setX(location:getX() - 1)
- end
- end
- end
- function changeDirection(direction)
- --changeDirection("faceMine")
- --changeDirection("faceExtraStorage")
- if direction == "faceMine" then
- while location:getFacing() ~= coordHome:getFacing() do
- turnRight(1)
- end
- else
- if location:getFacing() == coordHome:getFacing() then
- turnRight(2)
- end
- end
- end
- function checkBedrock(itemList)
- local success = false
- if turtle.detect() then --block in front so not air/water/lava
- if not dig.digNew{self = dig, checkForItems = itemList, callFrom = "checkBedrock"} then --block not broken, so must be bedrock
- success = true
- end
- end
- return success
- end
- function checkDiamonds()
- local numDiamonds = 0
- if objectives["diamond pickaxe"] then
- numDiamonds = 3
- end
- if slot:getItemSlot("diamonds") > 0 then
- numDiamonds = numDiamonds + turtle.getItemCount(slot:getItemSlot("diamonds"))
- end
- return numDiamonds
- end
- function checkDigDown()
- local useSlot = 0
- local useItem = ""
- local useAmount = 0
- local yCoordTemp = 0
- local checkList = {}
- local result = "onLand"
- checkList[1] = "wood"
- checkList[2] = "wood2"
- checkList[3] = "sand"
- --checkList[4] = "item"
- turtle.select(1)
- while not turtle.detectDown() do --could be water, lava or air
- saveToLog("checkDigDown: checking for water", true)
- if checkWater("down") then -- water found.
- result = "onWater"
- break
- else --no water so go down
- --check if in a cave or ravine
- result = "onLand"
- if location:getY() <= 55 then
- --place block and exit
- saveToLog("checkDigDown: Ravine or cave detected, building bridge")
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif turtle.getItemCount(slot:getItemSlot("dirt")) > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.placeDown()
- break
- end
- down(1, 1)
- saveToLog("checkDigDown: Moved down y = "..location:getY(), false)
- end
- end --block underneath at bottom of air/water, ready to dig or check
- --see if block below is known
- saveToLog("checkDigDown: at base level "..result, false)
- if result == "onLand" then --default
- useSlot, useAmount, useItem = compareBlock("down", checkList, 3) --does block below match wood,wood2,sand
- turtle.select(1)
- -- if wood, wood2, harvest
- if useItem == "wood" or useItem == "wood2" then
- saveToLog("checkDigDown: digDown: "..useItem, false)
- --turtle.select(1)
- turtle.select(useSlot)
- while turtle.compareDown() do
- down(1, 1) --will automatically dig
- turtle.select(useSlot)
- end
- elseif useItem == "sand" then
- yCoordTemp = location:getY()
- if turtle.getItemCount(useSlot) < 8 then
- turtle.select(useSlot)
- while turtle.compareDown() do
- saveToLog("checkDigDown: compareDown = "..useItem, false)
- down(1, 1, "sand,gravel", "checkDigDown")
- turtle.select(useSlot)
- end
- turtle.select(slot:getItemSlot("dirt"))
- while location:getY() < yCoordTemp do
- up(1, 1)
- if turtle.getItemCount(slot:getItemSlot("dirt")) > 0 then
- turtle.placeDown()
- end
- end
- else
- saveToLog("checkDigDown: digDown: sand found but not mined", false)
- end
- elseif string.find(useItem, "item") ~= nil then
- dig.digNew{self = dig, direction = "down", checkForItems = "sand", callFrom = "checkDigDown"}
- end
- end
- return result --"onLand", "onWater"
- end
- function checkDigUp()
- while turtle.detect() do --if block in front move up
- if turtle.detectUp() then
- dig.digNew{self = dig, direction = "up", slotNo = 1, checkForItems = "wood2,saplings", callFrom = "checkDigUp"}
- if dig:getSuccess() then --success digging. If dirt/grass itemcount ++
- --see if 1 item in slot 16 matches anything
- if dig:getDugItem() == "wood2" then
- saveToLog("harvesting new tree type, wood2", false)
- harvestTree("wood2")
- break
- end
- end
- end
- up(1, 1)
- end
- end
- function checkForLooseSaplings()
- --pick up loose ?saplings
- dig.digNew{self = dig, suck = true, checkForItems = "saplings", callFrom = "checkForLooseSaplings"} --forward, slot1 by default
- end
- function checkGravel()
- --[[place gravel repeatedly. eventually will produce flint
- which cannot be placed, need 2 blocks for comparison]]--
- local tempyCoord = location:getY()
- local emptySlot = 0
- local slotCount = 0
- local reRun = false
- local doBreak = false
- local gravelFound = false
- local sTime = os.time()
- if slot:getItemSlot("gravel") == 0 or slot:getItemSlot("sand") == 0 then
- if slot:getItemSlot("gravel") == 0 and slot:getItemSlot("sand") == 0 then
- saveToLog("checkGravel: testing for gravel and sand")
- elseif slot:getItemSlot("gravel") == 0 then
- saveToLog("checkGravel: sand already found, testing for gravel")
- else
- saveToLog("checkGravel: gravel already found, testing for sand")
- end
- emptySlot = getFirstEmptySlot(true)
- saveToLog("checkGravel: testing slot "..slot:getLeastUnknownItemSlot().." to "..slot:getMostUnknownItemSlot(), false)
- --gravel, iron ore, sand, coal may have been found
- --clear the area
- saveToLog("checkGravel: clearing test area", true)
- dig.digNew{self = dig, direction = "forward", callFrom = "checkGravel"}
- up(1, 1) --move up 1 place
- dig.digNew{self = dig, direction = "forward", callFrom = "checkGravel"}
- repeat
- reRun = false
- if slot:getLeastUnknownItemSlot() > 0 then --updated when sortInventory(true) is run
- for i = slot:getLeastUnknownItemSlot(), slot:getMostUnknownItemSlot() do
- saveToLog("checkGravel: testing slot "..i.." ("..slot:getSlotContains(i)..")", true)
- turtle.select(i)
- saveToLog("Placing ? sand/gravel..")
- if turtle.place() then --sand, gravel, iron ore,
- saveToLog("checkGravel: ? sand/gravel placed, waiting for drop", true)
- sleep(.5)
- if turtle.detect() then --cobble, ironore
- saveToLog("checkGravel: slot "..i.." is not gravel or sand", true)
- dig.digNew{self = dig, callFrom = "checkGravel"}
- if slot:getItemSlot("cobble") > 0 then --cobble known
- slot.update{self = slot, slotNo = i, newItem = "?ironore"}
- doBreak = true
- reRun = true
- break
- --else
- --continue with for loop (no break)
- --reRun already false
- end
- else
- saveToLog("checkGravel: gravel / sand fell")
- down(1)
- if slot:getItemSlot("gravel") > 0 then --gravel found
- slot.update{self = slot, slotNo = i, newItem = "sand"}
- dig.digNew{self = dig, callFrom = "checkGravel"} --will recover sand
- else
- if turtle.getItemCount(i) > 0 then -- at least 1 ?gravel /?sand remaining
- slotCount = turtle.getItemCount(i)
- dig.digNew{self = dig, callFrom = "checkGravel"} --will recover either sand, gravel or flint, which would go to emptySlot
- if turtle.getItemCount(i) == slotCount then -- flint dug
- turtle.select(emptySlot)
- while turtle.drop() do --drop flint
- saveToLog("checkGravel: Dumping flint Found in slot "..emptySlot, false)
- end
- slot.update{self = slot, slotNo = emptySlot, delete = true}
- slot.update{self = slot, slotNo = i, newItem = "gravel"}
- else -- gravel or sand dug
- if slot:getItemSlot("sand") == 0 then -- sand unknown
- saveToLog("Checking if slot "..i.." is gravel")
- turtle.select(i)
- for j = 1, 100 do
- if turtle.place() then --gravel placed
- turtle.dig()
- if turtle.getItemCount(emptySlot) > 0 then --flint found
- saveToLog("checkGravel: Gravel Found in slot "..i, true)
- slot.update{self = slot, slotNo = i, newItem = "gravel"}
- turtle.select(emptySlot)
- turnRight(2)
- while turtle.drop() do --drop flint
- saveToLog("checkGravel: Dumping flint Found in slot "..emptySlot, false)
- end
- turnRight(2)
- gravelFound = true
- break
- end
- end
- end
- if not gravelFound then
- saveToLog("checkGravel: Sand Found in slot "..i, true)
- slot.update{self = slot, slotNo = i, newItem = "sand"}
- end
- else --sand known therefore gravel just dug
- saveToLog("checkGravel: Gravel Found in slot "..i, true)
- slot.update{self = slot, slotNo = i, newItem = "gravel"}
- end
- end
- else -- only 1 remaining so dump anyway
- saveToLog("checkGravel: only one item in slot "..i, true)
- turnRight(2)
- while turtle.drop() do --drop flint
- saveToLog("checkGravel: Dumping item in slot "..emptySlot, false)
- end
- turnRight(2)
- slot.update{self = slot, slotNo = i, delete = true}
- end
- end
- doBreak = true
- reRun = true
- up(1)-- go up again ready for testing next item (gravel/sand/flint only -NOT cobble)
- break
- end
- else -- flint, coal
- if turtle.refuel(0) then
- slot.update{self = slot, slotNo = i, newItem = "coal"}
- saveToLog("checkGravel: coal found in slot "..i, false)
- else
- turnRight(2)
- while turtle.drop() do --drop flint
- saveToLog("checkGravel: Dumping flint Found in slot "..i, false)
- end
- turnRight(2)
- slot.update{self = slot, slotNo = i, delete = true}
- end
- doBreak = true
- reRun = true
- break
- end
- end
- if doBreak then
- sortInventory(true)
- end
- end
- until not reRun
- while tempyCoord < location:getY() do
- down(1)
- end
- saveToLog("checkGravel started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
- else
- saveToLog("checkGravel: sand and gravel already known", true)
- end
- end
- function checkItems(arg)
- --checkItems{keepTorches = 2 + torchesNeeded, keepSigns = 1}
- --checkItems{keepTorches = 2}
- --checkItems{keepSticks = 0, keepTorches = 16, keepSigns = 0}
- local numSticksOnboard = 0
- local numTorchesOnboard = 0
- local numSignsOnboard = 0
- local numWoodStored = 0
- local numSticksStored = 0
- local numTorchesStored = 0
- local numSignsStored = 0
- local getTorches = false
- local getSigns = false
- local makeTorches = false
- local makeSigns = false
- arg.keepSticks = arg.keepSticks or 0
- arg.keepTorches = arg.keepTorches or 0
- arg.keepSigns = arg.keepSigns or 0
- arg.keepWood = arg.keepWood or 64
- changeDirection("faceMine")
- saveToLog("checkItems: keepWood = "..arg.keepWood..
- " keepSticks = "..tostring(arg.keepSticks)..
- " keepTorches = "..tostring(arg.keepTorches)..
- " keepSigns = "..tostring(arg.keepSigns), false)
- numTorchesStored = storageTorches:getItemCount("torches")
- numSignsStored = storageSigns:getItemCount("signs")
- numSticksStored = storageSticks:getItemCount("sticks")
- numWoodStored = storageWood:getItemCount("wood")
- if slot:getItemSlot("torches") > 0 then
- numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
- end
- if slot:getItemSlot("signs") > 0 then
- numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
- end
- if slot:getItemSlot("sticks") > 0 then
- numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
- end
- --check if any signs or torches need to be crafted
- --if so store torches and signs first, craft, then re-stock
- if arg.keepTorches > 0 then
- if numTorchesOnboard >= arg.keepTorches then
- saveToLog("checkItems: "..numTorchesOnboard.." torches already in stock", false)
- success = true
- elseif numTorchesStored >= arg.keepTorches then
- saveToLog("checkItems: "..numTorchesStored.." torches are in storage", false)
- getTorches = true
- elseif numTorchesOnboard + numTorchesStored >= arg.keepTorches then
- saveToLog("checkItems: "..numTorchesOnboard + numTorchesStored.." torches are in stock / storage", false)
- getTorches = true
- else -- not enough onBoard or store
- saveToLog("checkItems: insufficient torches in stock / store, crafting more", false)
- makeTorches = true
- end
- end
- if arg.keepSigns > 0 then
- if numSignsOnboard >= arg.keepSigns then
- saveToLog("checkItems: "..numSignsOnboard.." signs already in stock", false)
- success = true
- elseif numSignsStored >= arg.keepSigns then
- saveToLog("checkItems: "..numSignsStored.." signs are in storage", false)
- getSigns = true
- elseif numSignsOnboard + numSignsStored >= arg.keepSigns then
- saveToLog("checkItems: "..numSignsOnboard + numSignsStored.." signs are in stock / storage", false)
- getSigns = true
- else -- not enough onBoard or store
- saveToLog("checkItems: insufficient signs in stock / store, crafting more", false)
- makeSigns = true
- end
- end
- if arg.keepSticks > 0 then
- if numSticksOnboard >= arg.keepSticks then
- saveToLog("checkItems: "..numSticksOnboard.." sticks already in stock", false)
- success = true
- elseif numSticksStored >= arg.keepSticks then
- saveToLog("checkItems: "..numSticksStored.." sticks are in storage", false)
- getSticks = true
- elseif numSticksOnboard + numSticksStored >= arg.keepSticks then
- saveToLog("checkItems: "..numSticksOnboard + numSticksStored.." sticks are in stock / storage", false)
- getSticks = true
- else -- not enough onBoard or store
- saveToLog("checkItems: insufficient sticks in stock / store, crafting more", false)
- makeSticks = true
- end
- end
- if makeSticks then --store existing torches/signs first
- if placeStorage:getStoragePlaced("torches") then
- storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
- if arg.keepTorches > 0 then
- getTorches = true
- end
- end
- if placeStorage:getStoragePlaced("signs") then
- forward(2, 1)
- turnRight(2)
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- if arg.keepSigns > 0 then
- getSigns = true
- end
- forward(2, 1)
- turnRight(2)
- end
- if numSticksStored > 0 then --get stored sticks
- forward(4, 1)
- turnRight(2)
- getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
- forward(4, 1)
- turnRight(2)
- end
- craftSticks(arg.keepSticks - numSticksOnboard)
- end
- if makeSigns then --store existing torches first
- if placeStorage:getStoragePlaced("torches") then
- storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
- if arg.keepTorches > 0 then
- getTorches = true
- end
- end
- if numSignsStored > 0 then --get stored signs
- forward(2, 1)
- turnRight(2)
- getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
- forward(2, 1)
- turnRight(2)
- end
- craftSigns(3, true)
- end
- if makeTorches then --store existing signs first
- if placeStorage:getStoragePlaced("signs") then
- forward(2, 1)
- turnRight(2)
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- if arg.keepSigns > 0 then
- getSigns = true
- end
- forward(2, 1)
- turnRight(2)
- end
- if numTorchesStored > 0 then --get stored torches
- getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
- end
- craftTorches(arg.keepTorches - numTorchesOnboard)
- end
- if getSticks then
- forward(4, 1)
- getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
- back(4)
- end
- if slot:getItemSlot("sticks") > 0 then
- numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
- end
- if numSticksOnboard - arg.keepSticks > 0 then --store sticks
- if placeStorage:getStoragePlaced("sticks") then
- forward(4, 1)
- turnRight(2)
- saveToLog("checkItems: toStore = "..storageSticks:getStoreName().." storing "..numSticksOnboard - arg.keepSticks.." sticks", true)
- if arg.keepSticks > 0 then
- storeItem{toStore = storageSticks, item = "sticks", quantity = numSticksOnboard - arg.keepSticks, updateSlot = true, doSort = false}
- else
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- end
- forward(4, 1)
- turnRight(2)
- end
- end
- if getTorches then
- getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
- end
- if slot:getItemSlot("torches") > 0 then
- numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
- end
- if numTorchesOnboard - arg.keepTorches > 0 then
- if placeStorage:getStoragePlaced("torches") then
- saveToLog("checkItems: storing "..numTorchesOnboard - arg.keepTorches.." torches in storageTorches", true)
- if arg.keepTorches > 0 then
- storeItem{toStore = storageTorches, item = "torches", quantity = numTorchesOnboard - arg.keepTorches, updateSlot = true, doSort = false}
- else
- storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
- end
- end
- end
- if getSigns then
- forward(2, 1)
- turnRight(2)
- getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
- forward(2, 1)
- turnRight(2)
- end
- if slot:getItemSlot("signs") > 0 then
- numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
- end
- if numSignsOnboard - arg.keepSigns > 0 then
- if placeStorage:getStoragePlaced("signs") then
- forward(2, 1)
- turnRight(2)
- saveToLog("checkItems: storing "..numSignsOnboard - arg.keepSigns.." signs in storageSigns", true)
- if arg.keepSigns > 0 then
- storeItem{toStore = storageSigns, item = "signs", quantity = numSignsOnboard - arg.keepSigns, updateSlot = true, doSort = false}
- else
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- end
- forward(2, 1)
- turnRight(2)
- end
- end
- if slot:getItemSlot("wood") > 0 then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- if arg.keepWood == 0 and slot:getItemSlot("wood") > 0 then --mining run, no wood onboard
- if placeStorage:getStoragePlaced("wood") then
- forward(6, 1)
- saveToLog("checkItems: storing "..numWoodOnboard.." wood in storageWood", true)
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- end
- end
- function checkLapis()
- local success = false
- --assume on ground
- changeDirection("faceMine")
- if not objectives["lapis"] then
- sortInventory(true)
- if slot:getMostUnknownItemSlot() > 0 then
- saveToLog("checkLapis: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- turtle.select(i)
- saveToLog("checkLapis: checking "..slot:getSlotContains(i), true)
- if turtle.getItemCount(i) > 10 then
- changeDirection("faceExtraStorage")
- if turtle.place() then
- dig.digNew{self = dig, callFrom = "checkLapis"}
- changeDirection("faceMine")
- else --lapis, redstone,coal
- if objectives["redstone"] and slot:getItemSlot("coal") > 0 then
- saveToLog("checkLapis: lapis found", true)
- slot.update{self = slot, slotNo = i, newItem = "lapis"}
- objectives["lapis"] = true
- success = true
- break
- end
- end
- end
- end
- else
- saveToLog("checkLapis: no unknown items to check", true)
- end
- if success then
- sortInventory(true)
- else
- saveToLog("checkLapis: not found or amount onboard below 10", true)
- end
- else
- saveToLog("checkLapis: lapis already found and in storage", false)
- if slot:getItemSlot("diamonds") == 0 then
- if slot:getMostUnknownItemSlot() > 0 then
- saveToLog("checkLapis: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- turtle.select(i)
- saveToLog("checkLapis: checking "..slot:getSlotContains(i), true)
- if turtle.getItemCount(i) > 3 then
- changeDirection("faceExtraStorage")
- if turtle.place() then
- dig.digNew{self = dig, callFrom = "checkLapis"}
- changeDirection("faceMine")
- else --diamonds, redstone,coal
- if objectives["redstone"] and slot:getItemSlot("coal") > 0 then
- saveToLog("checkLapis: ?diamonds found", true)
- slot.update{self = slot, slotNo = i, newItem = "?diamonds"}
- checkItems{keepTorches = 0, keepSigns = 0, keepSticks = 2}
- if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "?diamonds", destSlot = 0} then
- saveToLog("Pickaxe crafted", true)
- slot.update{self = slot, item = "?diamonds", newItem = "diamonds"}
- itemFound = "diamonds"
- objectives["diamonds"] = true
- success = true
- break
- end
- end
- end
- end
- end
- else
- saveToLog("checkLapis: no unknown items to check", true)
- end
- end
- success = true
- end
- changeDirection("faceMine")
- return success
- end
- function checkMetal(useSlot)
- local result = ""
- local doContinue = true
- local doSort = false
- local tempItem = ""
- useSlot = useSlot or 0
- --checking for ironore and goldore
- --assume on ground
- changeDirection("faceMine")
- if objectives["smelt iron"] and objectives["smelt gold"] then --iron and gold discovered so dont bother
- doContinue = false
- end
- if slot:getItemSlot("?ironore") > 0 then --ironore provisionally identified, not enough to smelt / storageIronore not in place
- if placeStorage:getStoragePlaced("ironore") then -- ok to smelt iron
- if turtle.getItemCount(slot:getItemSlot("?ironore")) > 4 then --smelt to ?iron
- saveToLog("checkMetal: attempting to smelt iron", true)
- if smelt("?ironore", 4) then
- saveToLog("checkMetal: smelt succeeded, ?iron created", true)
- if craft{craftItem = "buckets", craftQuantity = 1, sourceItem1 = "?iron", destSlot = 0} then
- if slot:getItemCount("buckets") == 1 then
- slot.update{self = slot, item = "?iron", newItem = "iron"}
- slot.update{self = slot, item = "?ironore", newItem = "ironore"}
- forward(8) --over storageIronore
- storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
- storeItem{toStore = storageIronore, item = "buckets", quantity = 0, updateSlot = true, doSort = true}
- objectives["smelt iron"] = true
- turnRight(2)
- forward(8)
- turnRight(2)
- result = "ironore"
- saveToLog("checkMetal: iron and ironore confirmed", true)
- doSort = true
- doContinue = false
- else -- sand->glass->glass bottle x3
- turtle.select(slot:getItemSlot("buckets"))
- while turtle.drop() do
- saveToLog("checkMetal: glass bottles dumped", true)
- end
- slot.update{self = slot, item = "buckets", delete = true}
- sortInventory(true)
- slot.update{self = slot, item = "?iron", newItem = "glass"}
- slot.update{self = slot, item = "?ironore", newItem = "sand"}
- saveToLog("checkMetal: ?iron and ?ironore renamed as glass and sand", true)
- end
- end
- else
- slot.update{self = slot, item = "?ironore", newItem = slot:getFreeUnknownItem()}
- end
- end
- end
- end
- if slot:getItemSlot("?goldore") > 0 then --goldore provisionally identified, not enough to smelt / storageIronore not in place
- if turtle.getItemCount(slot:getItemSlot("?goldore")) > 1 then
- if placeStorage:getStoragePlaced("ironore") then -- ok to smelt gold
- saveToLog("checkMetal: attempting to smelt gold", true)
- if smelt("?goldore", 1) then
- saveToLog("checkMetal: smelt succeeded, ?gold created", true)
- if craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "?gold", destSlot = 0} then
- slot.update{self = slot, item = "?goldore", newItem = "goldore"}
- --[[turtle.select(slot:getItemSlot("nuggets"))
- while turtle.drop() do
- saveToLog("checkMetal: gold nuggets dumped", true)
- end
- slot.update{self = slot, item = "nuggets", delete = true}]]--
- objectives["smelt gold"] = true
- result = "goldore"
- doSort = true
- doContinue = false
- end
- else
- slot.update{self = slot, item = "?ironore", newItem = slot:getFreeUnknownItem()}
- end
- end
- end
- end
- if doContinue then -- either gold or iron not known
- if useSlot > 0 then --only check one item for gold as called from identifyItems
- saveToLog("checkMetal: testing item in slot "..useSlot, true)
- if turtle.getItemCount(useSlot) > 1 then --smelt to ?gold
- if not objectives["smelt gold"] then
- tempItem = slot:getSlotContains(useSlot)
- slot.update{self = slot, slotNo = useSlot, newItem = "?goldore"}
- saveToLog("checkMetal: gold ore provisionally identified from "..tempItem, true)
- if smelt("?goldore", 1) then
- saveToLog("checkMetal: smelt succeeded, ?gold created", true)
- if craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "?gold", destSlot = 0} then
- slot.update{self = slot, item = "?goldore", newItem = "goldore"}
- objectives["smelt gold"] = true
- result = "goldore"
- end
- else
- saveToLog("checkMetal: gold not smelted from "..tempItem, true)
- slot.update{self = slot, item = "?goldore", newItem = tempItem}
- result = ""
- end
- end
- end
- else
- if slot:getMostUnknownItemSlot() > 0 then
- saveToLog("checkMetal: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), -1 do
- if turtle.getItemCount(i) > 1 then
- turtle.select(i)
- if turtle.place() then --iron/gold/obsidian ? redstone
- if turtle.compare() then -- not redstone could be iron, gold, obsidian, moss stone
- --iron most likely next item, then gold
- dig.digNew{self = dig, slotNo = i, callFrom = "checkMetal"}
- --if slot:getItemSlot("ironore") == 0 then --not already found
- if not objectives["smelt iron"] then
- slot.update{self = slot, slotNo = i, newItem = "?ironore"}
- saveToLog("checkMetal: iron ore provisionally identified", true)
- result = "?ironore"
- if placeStorage:getStoragePlaced("ironore") then -- ok to smelt iron
- if turtle.getItemCount(slot:getItemSlot("?ironore")) > 4 then --smelt to ?iron
- saveToLog("checkMetal: attempting to smelt iron", true)
- if smelt("?ironore", 4) then
- saveToLog("checkMetal: smelt succeeded, ?iron created", true)
- if craft{craftItem = "buckets", craftQuantity = 1, sourceItem1 = "?iron", destSlot = 0} then
- if slot:getItemCount("buckets") == 1 then
- slot.update{self = slot, item = "?iron", newItem = "iron"}
- slot.update{self = slot, item = "?ironore", newItem = "ironore"}
- forward(8) --over storageIronore
- storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
- storeItem{toStore = storageIronore, item = "buckets", quantity = 0, updateSlot = true, doSort = true}
- objectives["smelt iron"] = true
- turnRight(2)
- forward(8)
- turnRight(2)
- result = "ironore"
- saveToLog("checkMetal: iron and ironore confirmed", true)
- doSort = true
- break
- else -- sand->glass->glass bottle x3
- turtle.select(slot:getItemSlot("buckets"))
- while turtle.drop() do
- saveToLog("checkMetal: glass bottles dumped", true)
- end
- slot.update{self = slot, item = "buckets", delete = true}
- sortInventory(true)
- slot.update{self = slot, item = "?iron", newItem = "glass"}
- slot.update{self = slot, item = "?ironore", newItem = "sand"}
- saveToLog("checkMetal: ?iron and ?ironore renamed as glass and sand", true)
- end
- end
- else
- slot.update{self = slot, item = "?ironore", newItem = slot:getFreeUnknownItem()}
- end
- end
- end
- --elseif slot:getItemSlot("goldore") == 0 then -- ironore found so check for gold
- elseif not objectives["smelt gold"] then
- tempItem = slot:getSlotContains(i)
- slot.update{self = slot, slotNo = i, newItem = "?goldore"}
- saveToLog("checkMetal: gold ore provisionally identified from "..tempItem, true)
- result = "?goldore"
- if placeStorage:getStoragePlaced("ironore") then -- ok to smelt iron
- if turtle.getItemCount(slot:getItemSlot("?goldore")) > 1 then --smelt to ?gold
- saveToLog("checkMetal: attempting to smelt gold", true)
- if smelt("?goldore", 1) then
- saveToLog("checkMetal: smelt succeeded, ?gold created", true)
- if craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "?gold", destSlot = 0} then
- slot.update{self = slot, item = "?goldore", newItem = "goldore"}
- --[[turtle.select(slot:getItemSlot("nuggets"))
- while turtle.drop() do
- saveToLog("checkMetal: gold nuggets dumped", true)
- end
- slot.update{self = slot, item = "nuggets", delete = true}]]--
- objectives["smelt gold"] = true
- result = "goldore"
- doSort = true
- break
- end
- else
- saveToLog("checkMetal: gold not smelted from "..tempItem.." trying next unknown item", true)
- slot.update{self = slot, item = "?goldore", newItem = tempItem}
- result = ""
- end
- end
- end
- end
- else -- did not compare to placed block
- saveToLog("checkMetal: redstone confirmed - did not compare to placed block", true)
- dig.digNew{self = dig, slotNo = i, callFrom = "checkMetal"}
- slot.update{self = slot, slotNo = i, newItem = "redstone"}
- doSort = true
- break
- end
- else --not placed, could be coal
- if slot:getItemSlot("coal") == 0 then
- if turtle.refuel(1) then
- slot.update{self = slot, slotNo = i, newItem = "coal"}
- doSort = true
- break
- end
- end
- end
- end
- end -- end for loop
- if doSort then
- sortInventory(true)
- end
- else
- saveToLog("checkMetal: no unknown items to check", true)
- end
- end
- else
- saveToLog("checkMetal: gold/iron already discovered or ?ironore confirmed", true)
- end
- return result -- "", ?ironore, ironore, ?goldore, goldore
- end
- function checkMineEntranceForTree(direction)
- local dugItem = ""
- if turtle.detect() then -- block in front
- dugItem = checkSugarCane(true)
- isTreeInFront(dugItem)
- end
- if direction == "right" then
- turnLeft(1)
- else
- turnRight(1)
- end
- if turtle.detect() then -- block in front
- dugItem = checkSugarCane(true)
- isTreeInFront(dugItem)
- end
- if direction == "right" then
- turnRight(1)
- else
- turnLeft(1)
- end
- end
- function checkMineStores()
- --move over each buried block in turn to see if it is stored
- --unknown items checked each time turtle returns to base
- local success = false
- local goBack = 0
- local emptyStore = {}
- local tempStore = {}
- local firstEmptyBlock = 0
- local doContinue = true
- -- check if ironore marker placed
- if placeStorage:getStoragePlaced("ironore") then
- if slot:getItemSlot("ironore") > 0 then
- if not placeStorage:getMarkerPlaced("ironore") then
- placeStorage:place("ironore", true)
- end
- end
- end
- --assume under furnace, facing forwards
- --first check any unknown items
- changeDirection("faceMine")
- if slot:getMostUnknownItemSlot() > 0 or
- slot:getItemSlot("lapis") > 0 or
- slot:getItemSlot("moss stone") > 0 or
- slot:getItemSlot("goldore") > 0 or
- slot:getItemSlot("obsidian") > 0 then --see if anything can be stored
- turnRight(2) --face extended storage
- --find first empty storage area
- firstEmptyBlock, emptyStore = getEmptyStorage()
- saveToLog("checkMineStores: first empty storage is "..emptyStore:getStoreName(), false, true)
- -- first storage area now known, and no of blocks forward needed
- -- move turtle over blocks to check contents, only if not empty
- if firstEmptyBlock > 1 then
- forward(1)
- goBack = 1
- for k = 1, firstEmptyBlock - 2, 2 do
- if k == 1 then
- tempStore = extraStorage1
- elseif k == 3 then
- tempStore = extraStorage2
- elseif k == 5 then
- tempStore = extraStorage3
- elseif k == 7 then
- tempStore = extraStorage4
- elseif k == 9 then
- tempStore = extraStorage5
- end
- if tempStore:getStoreContains() == "lapis" then
- forward(1, 1)
- if slot:getItemSlot("lapis") > 0 then --already onboard and identified
- storeItem{toStore = tempStore, item = "lapis", quantity = 0, updateSlot = true, doSort = true}
- else
- turtle.select(16)
- if turtle.suckDown() then
- for i = 1, 16 do
- if turtle.compareTo(i) then --lapis found
- turtle.select(16)
- while turtle.dropDown() do
- saveToLog("checkMineStores: returning lapis to storage")
- end
- slot.update{self = slot, slotNo = i, newItem = "lapis"}
- sortInventory(true)
- if not placeStorage:getMarkerPlaced("lapis") then
- if slot:getItemCount("lapis") >= 9 then
- if craft{craftItem = "lapis block", craftQuantity = 1, sourceItem1 = "lapis", destSlot = 0} then
- dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
- turtle.select(slot:getItemSlot("lapis block"))
- turtle.placeDown()
- slot.update{self = slot, item = "lapis block", delete = true}
- placeStorage:setMarkerPlaced("lapis")
- end
- end
- end
- saveToLog("checkMineStores: adding additional lapis to storage", true)
- storeItem{toStore = tempStore, item = "lapis", quantity = 0, updateSlot = true, doSort = true}
- break
- end
- end
- end
- end
- back(1)
- else
- if slot:getItemSlot(tempStore:getStoreContains()) > 0 then --item already onboard and identified
- forward(1, 1)
- storeItem{toStore = tempStore, item = tempStore:getStoreContains(), quantity = 0, updateSlot = true, doSort = true}
- back(1)
- else
- for i = 1, 16 do
- if slot:getSlotStatus(i) == "unknown" then
- saveToLog("checkMineStores: comparing items in "..tempStore:getStoreName().." with "..tostring(slot:getSlotContains(i)), true)
- turtle.select(i)
- if turtle.compareDown() then --matches embedded block below
- forward(1, 1)
- while turtle.dropDown() do
- saveToLog("checkMineStores: adding "..tempStore:getStoreContains().." to "..tempStore:getStoreName(), true)
- end
- slot.update{self = slot, item = slot:getSlotContains(i), delete = true}
- sortInventory(true)
- back(1)
- break
- end
- end
- end
- end
- end
- forward(2, 1)
- goBack = goBack + 2
- end
- back(goBack)
- end
- changeDirection("faceMine")
- if slot:getItemSlot("moss stone") > 0 then --put moss stone and walls into storage
- changeDirection("faceExtendedStorage")
- forward(firstEmptyBlock)
- dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
- turtle.select(slot:getItemSlot("moss stone"))
- turtle.placeDown()
- forward(1, 1)
- storeItem{toStore = emptyStore, item = "moss stone", quantity = 0, updateSlot = true, doSort = true}
- storeItem{toStore = emptyStore, item = "walls", quantity = 0, updateSlot = true, doSort = true}
- --slot.update{self = slot, item = slot:getSlotContains(i), delete = true}
- back(firstEmptyBlock + 1)
- changeDirection("faceMine")
- firstEmptyBlock, emptyStore = getEmptyStorage()
- end
- if slot:getItemSlot("goldore") > 0 then
- changeDirection("faceExtendedStorage")
- forward(firstEmptyBlock)
- dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
- turtle.select(slot:getItemSlot("goldore"))
- turtle.placeDown()
- forward(1, 1)
- storeItem{toStore = emptyStore, item = "goldore", quantity = 0, updateSlot = true, doSort = true}
- storeItem{toStore = emptyStore, item = "nuggets", quantity = 0, updateSlot = true, doSort = true}
- back(firstEmptyBlock + 1)
- changeDirection("faceMine")
- firstEmptyBlock, emptyStore = getEmptyStorage()
- end
- if slot:getItemSlot("obsidian") > 0 then
- changeDirection("faceExtendedStorage")
- forward(firstEmptyBlock)
- dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
- turtle.select(slot:getItemSlot("obsidian"))
- turtle.placeDown()
- forward(1, 1)
- storeItem{toStore = emptyStore, item = "obsidian", quantity = 0, updateSlot = true, doSort = true}
- back(firstEmptyBlock + 1)
- changeDirection("faceMine")
- firstEmptyBlock, emptyStore = getEmptyStorage()
- end
- if slot:getItemSlot("lapis") > 0 then
- changeDirection("faceExtendedStorage")
- forward(firstEmptyBlock)
- if slot:getItemCount("lapis") > 9 then
- if craft{craftItem = "lapis block", craftQuantity = 1, sourceItem1 = "lapis", destSlot = 0} then
- dig.digNew{self = dig, direction = "down", callFrom = "checkMineStores"}
- turtle.select(slot:getItemSlot("lapis block"))
- turtle.placeDown()
- slot.update{self = slot, item = "lapis block", delete = true}
- placeStorage:setMarkerPlaced("lapis")
- end
- end
- forward(1, 1)
- storeItem{toStore = emptyStore, item = "lapis", quantity = 0, updateSlot = true, doSort = true}
- back(firstEmptyBlock + 1)
- changeDirection("faceMine")
- firstEmptyBlock, emptyStore = getEmptyStorage()
- end
- else
- saveToLog("checkMineStores: no unknown items to store")
- end
- return success
- end
- function checkMossStone()
- --assume on ground
- local success = false
- local goBack = 0
- local tempItem = ""
- changeDirection("faceMine")
- if slot:getMostUnknownItemSlot() > 0 then
- saveToLog("checkMossStone: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- turtle.select(i)
- if turtle.getItemCount(i) > 6 then
- changeDirection("faceExtraStorage")
- if turtle.place() then --iron/gold/redstone/moss stone
- if turtle.compare() then --iron/gold/redstone/moss stone
- tempItem = slot:getSlotContains(i)
- saveToLog("checkMossStone: testing "..tempItem.." in slot "..i, true)
- dig.digNew{self = dig, slotNo = i, callFrom = "checkMossStone"}
- changeDirection("faceMine")
- slot.update{self = slot, slotNo = i, newItem = "?moss stone"}
- if craft{craftItem = "walls", craftQuantity = 6, sourceItem1 = "?moss stone", destSlot = 0} then
- success = true
- -- slot.update{self = slot, slotNo = i, newItem = "moss stone"}
- slot.update{self = slot, item = "?moss stone", newItem = "moss stone"}
- sortInventory(true)
- --now store moss and walls in extended storage area
- break
- else
- slot.update{self = slot, item = "?moss stone", newItem = tempItem}
- end
- else -- redstone
- saveToLog("checkMossStone: redstone found", true)
- dig.digNew{self = dig, slotNo = i, callFrom = "checkMossStone"}
- changeDirection("faceMine")
- slot.update{self = slot, slotNo = i, newItem = "redstone"}
- sortInventory(true)
- if not placeStorage:getStoragePlaced("redstone") then
- placeStorage:place("redstone", false)
- else
- forward(12, 1)
- turnRight(2)
- storeRedstone()
- --return to furnace
- forward(12, 1)
- turnRight(2)
- end
- break
- end
- end
- end
- end
- changeDirection("faceMine")
- else
- saveToLog("checkMossStone: no unknown items to check", true)
- end
- return success
- end
- function checkObjectives(colNo, side, torchesNeeded)
- local numTorches = 0
- local numSigns = 0
- local numSticks = 0
- local woodCount = 0
- local numSaplings = 0
- local success = false
- local woodType = 0
- local fuelStats = {}
- local doContinue = true
- local woodAvailable = 0
- local wood2Available = 0
- if torchesNeeded == nil then
- torchesNeeded = 0
- end
- refuel(0)
- --should be under furnace. Checks objectives before deep mining starts
- --Also loads turtle with torches/signs ready for next harvest run
- --[[
- before going deep mining:
- 1) goMining1 completed at least once
- 2) storageTorches placed
- 3) storageSigns, storageSticks placed
- 4) found the following items: dirt, cobble, stone, coal, gravel, sand(6), ironore (5), sugar cane (3)
- 5) tree farm planted
- 6) iron smelted (1 bucket crafted)
- 7) storageSand, storageWood, storageRedstone, storageIronore, storagePickaxes, storageSaplings
- If all trees harvested and goMining1 repeated on all areas, can continue without iron and coal
- objectives:
- 1) placeStorage:getStoragePlaced("torches")
- 2) placeStorage:getStoragePlaced("signs")
- 3) placeStorage:getStoragePlaced("sticks")
- 4) placeStorage:getStoragePlaced("wood") -- only when treeFarm planted
- 5) placeStorage:getStoragePlaced("ironore")
- 6) placeStorage:getStoragePlaced("sand")
- 7) placeStorage:getStoragePlaced("redstone") -- only when treeFarm planted
- 8) placeStorage:getStoragePlaced("pickaxes)" -- only when treeFarm planted
- 9) placeStorage:getStoragePlaced("saplings") -- only when treeFarm planted
- ]]--
- changeDirection("faceMine")
- if side == "rightside" and colNo == 1 then --not enough wood on first run unless branched tree harvested
- doContinue = false
- woodType = getWoodAvailable{woodNeeded = 4}
- if woodType ~= "none" then
- doContinue = true
- end
- end
- if doContinue then --excludes first tree harvest run if not enough wood
- --torches already checked by checkTorch()
- if placeStorage:getStoragePlaced("torches") then
- --check if at least 2 torches and 1 sign present first
- saveToLog("checkObjectives: checking for torches onboard or in storage", false, true)
- if getItemsAvailable("torches", true) < 2 + torchesNeeded then --check and remove torches from storage
- -- make torches
- woodType = getWoodAvailable{woodNeeded = 4}
- saveToLog("checkObjectives: crafting torches if wood available", false, true)
- if woodType ~= "none" then
- craftTorches(4)
- end
- end
- -- put them in storage
- saveToLog("checkObjectives: storing torches to free a slot", false, true)
- storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
- saveToLog("checkObjectives: checking for signs onboard or in storage", false, true)
- if getItemsAvailable("signs", true) < 1 then --check and remove signs from storage
- woodType = getWoodAvailable{woodNeeded = 4}
- saveToLog("checkObjectives: crafting signs if wood available", false, true)
- if woodType ~= "none" then
- craftSigns()
- end
- end
- -- put them in storage
- if placeStorage:getStoragePlaced("signs") then
- if not placeStorage:getMarkerPlaced("signs") then
- placeStorage:place("signs", true)
- end
- forward(2, 1)
- turnRight(2)
- saveToLog("checkObjectives: storing signs to free a slot", false, true)
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- forward(2, 1)
- turnRight(2)
- else
- refuel(0)
- woodType = getWoodAvailable{woodNeeded = 5}
- if woodType ~= "none" then
- saveToLog('checkObjectives: placeStorage("signs") function started', true)
- getItemsAvailable("signs", true)
- placeStorage:place("signs", false)
- if placeStorage:getStoragePlaced("signs") then
- saveToLog('checkObjectives: 2/9 - placeStorage:getStoragePlaced("signs") completed', true)
- if slot:getItemSlot("signs") > 0 then
- if turtle.getItemCount(slot:getItemSlot("signs")) > 0 then
- forward(2, 1)
- turnRight(2)
- saveToLog("checkObjectives: storing signs to free a slot", false, true)
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- forward(2, 1)
- turnRight(2)
- end
- end
- end
- else
- saveToLog("checkObjectives: Not enough wood to place storageSigns", false, true)
- end
- end
- --signs and torches made, see if any wood for storage, in order torches, signs, sticks, sand, ironore
- refuel(0)
- if placeStorage:getStoragePlaced("sticks") then
- if not placeStorage:getMarkerPlaced("sticks") then
- woodType = getWoodAvailable{woodNeeded = 4}
- if woodType ~= "none" then
- craftSticks(8)
- placeStorage:place("sticks", true)
- end
- end
- if slot:getItemSlot("sticks") > 0 then
- if turtle.getItemCount(slot:getItemSlot("sticks")) > 0 then
- -- put them in storage
- forward(4, 1)
- turnRight(2)
- saveToLog("checkObjectives: storing signs to free a slot", false, true)
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- forward(4, 1)
- turnRight(2)
- end
- end
- else
- woodType = getWoodAvailable{woodNeeded = 6}
- if woodType ~= "none" then
- saveToLog('checkObjectives: placeStorage("sticks") function started', true)
- placeStorage:place("sticks", false)
- if placeStorage:getStoragePlaced("sticks") then
- saveToLog('checkObjectives: 3/9 - placeStorage:getStoragePlaced("sticks") completed', true)
- if slot:getItemSlot("sticks") > 0 then
- if turtle.getItemCount(slot:getItemSlot("sticks")) > 0 then
- forward(4, 1)
- turnRight(2)
- saveToLog("checkObjectives: storing signs to free a slot", false, true)
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- forward(4, 1)
- turnRight(2)
- end
- end
- end
- else
- saveToLog("checkObjectives: Not enough wood to place storageSticks", false, true)
- end
- end
- refuel(0)
- if placeStorage:getStoragePlaced("sand") then --storageSand placed
- if not placeStorage:getMarkerPlaced("sand") then
- placeStorage:place("sand", true)
- end
- if slot:getItemSlot("sugar cane") > 0 then
- forward(10)
- storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(10)
- turnRight(2)
- objectives["complete sugar cane"] = true
- saveToLog('checkObjectives: 6/9 - objectives["complete sugar cane"] completed', true)
- end
- else --storageSand not yet placed
- if slot:getItemSlot("sand") > 0 or
- (placeStorage:getStoragePlaced("signs") and
- placeStorage:getStoragePlaced("sticks")) then --see if sand onboard or signs/sticks already placed
- woodType = getWoodAvailable{woodNeeded = 5}
- if woodType ~= "none" then
- if objectives["sand"] then --place storageSand if enough wood and sand discovered
- placeStorage:place("sand", false)
- saveToLog('checkObjectives: 1/9 - placeStorage:getStoragePlaced("sand") completed', true)
- end
- if slot:getItemSlot("sugar cane") > 0 then
- forward(10)
- storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(10)
- turnRight(2)
- objectives["complete sugar cane"] = true
- saveToLog('checkObjectives: 6/9 - objectives["complete sugar cane"] completed', true)
- end
- end
- end
- end
- refuel(0)
- if objectives["goMining1"] then --done on each run to see if ironore has been found
- saveToLog('checkObjectives: checkMetal started', true)
- checkMetal() --check if ironore discovered
- if placeStorage:getStoragePlaced("ironore") then
- if slot:getItemSlot("ironore") > 0 then
- if not placeStorage:getMarkerPlaced("ironore") then
- placeStorage:place("ironore", true)
- end
- end
- else
- if slot:getItemSlot("ironore") > 0 or
- (placeStorage:getStoragePlaced("signs") and
- placeStorage:getStoragePlaced("sticks")) then
- woodType = getWoodAvailable{woodNeeded = 5}
- if woodType ~= "none" then
- saveToLog('checkObjectives: placeStorage("ironore") function started', true)
- placeStorage:place("ironore", false)
- if placeStorage:getStoragePlaced("ironore") then
- saveToLog('checkObjectives: 4/9 - placeStorage:getStoragePlaced("ironore") completed', true)
- end
- else
- saveToLog("checkObjectives: Not enough wood to place storageIronore", false, true)
- end
- end
- end
- end
- if colNo > 5 or side == "leftside" then --can plant tree farm anytime from now needs min 500 fuel
- if objectives["clear treefarm"] then --treeFarm already present
- if not objectives["pave treefarm"] then
- objectives["pave treefarm"] = paveTreeFarm()
- end
- woodType = getWoodAvailable{woodNeeded = 1}
- if woodType ~= "none" then
- if not placeStorage:getMarkerPlaced("wood") then
- placeStorage:place("wood", true)
- end
- end
- if placeStorage:getStoragePlaced("ironore") and placeStorage:getStoragePlaced("sand") then
- if not placeStorage:getStoragePlaced("redstone") then
- woodType = getWoodAvailable{woodNeeded = 5}
- if woodType ~= "none" then
- saveToLog('checkObjectives: placeStorage("redstone") function started', true)
- placeStorage:place("redstone", false)
- if placeStorage:getStoragePlaced("redstone") then
- saveToLog('checkObjectives: 7/9 - placeStorage:getStoragePlaced("redstone") completed', true)
- end
- else
- saveToLog("checkObjectives: Not enough wood to place storageRedstone", false, true)
- end
- end
- if not placeStorage:getStoragePlaced("pickaxes") then
- woodType = getWoodAvailable{woodNeeded = 5}
- if woodType ~= "none" then
- saveToLog('checkObjectives: placeStorage("pickaxes") function started', true)
- placeStorage:place("pickaxes", false)
- if placeStorage:getStoragePlaced("pickaxes") then
- saveToLog('checkObjectives: 8/9 - placeStorage:getStoragePlaced("pickaxes") completed', true)
- end
- else
- saveToLog("checkObjectives: Not enough wood to place storagePickaxes", false, true)
- end
- end
- end
- saveToLog('checkObjectives: objectives["clear treefarm"] = true, checking sand and sugar cane', true)
- if objectives["sand"] and objectives["sugar cane"] then
- success = true
- else
- if not objectives["sand"] or not objectives["gravel"] then --check if sand onboard
- back(1)
- turnRight(1)
- checkGravel()
- turnLeft(1)
- forward(1)
- end
- end
- if not objectives["smelt iron"] then
- success = false
- end
- -- need 14 wood for 7 chests, and 16 charcoal for 64 torches, fuel from coal during mining
- woodType, woodAvailable, wood2Available = getWoodAvailable{woodNeeded = 24, checkWoodType = "any"} -- 18 needed for deep mining + 6 spare
- if woodType == "none" then
- success = false
- saveToLog("checkObjectives: not enough wood to start deep mining ("..woodAvailable.." wood, "..wood2Available.." wood2", false, true)
- end
- else --treefarm not yet planted
- --check if enough saplings to plant tree farm - min 5 saplings/saplings2
- if slot:getItemSlot("saplings") > 0 then
- numSaplings = turtle.getItemCount(slot:getItemSlot("saplings"))
- end
- if slot:getItemSlot("saplings2") > 0 then
- if turtle.getItemCount(slot:getItemSlot("saplings2")) > numSaplings then
- numSaplings = turtle.getItemCount(slot:getItemSlot("saplings2"))
- end
- end
- if numSaplings > 4 then --check if tree farm can be cleared
- if slot:getItemCount("coal") > 2 then
- woodType = getWoodAvailable{woodNeeded = 8} -- 4 for chests, 3 for torches, 3 for fuel, 1 for marker
- else
- woodType = getWoodAvailable{woodNeeded = 11} -- 4 for chests, 3 for torches, 3 for fuel, 1 for marker
- end
- if woodType ~= "none" then
- saveToLog("CheckObjectives: checking fuel before clearing Tree Farm", true)
- fuelStats = getFuelAvailable()
- if fuelStats.totalFuelAvailable > 500 then
- saveToLog("CheckObjectives: placing storageSaplings")
- placeStorage:place("saplings", false)
- if placeStorage:getStoragePlaced("saplings") then
- saveToLog('checkObjectives: 9/9 - placeStorage:getStoragePlaced("saplings") completed')
- end
- saveToLog("CheckObjectives: placing storageWood")
- placeStorage:place("wood", false)
- if placeStorage:getStoragePlaced("wood") then
- saveToLog('checkObjectives: 8/9 - placeStorage:getStoragePlaced("wood") completed')
- end
- forward(2)
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- back(2)
- craftTorches(8)
- saveToLog("CheckObjectives: fuelAvailable = "..fuelStats.totalFuelAvailable.." starting Tree Farm", true)
- objectives["clear treefarm"] = clearTreeFarm()
- if objectives["clear treefarm"] then
- saveToLog('checkObjectives: 9/9 - objectives["clear treefarm"] completed')
- end
- else
- saveToLog("CheckObjectives: fuelAvailable = "..fuelStats.totalFuelAvailable.." not enough for tree farm", true)
- end
- else
- saveToLog("CheckObjectives: not enough wood for storageSaplings and storageWood")
- end
- else
- saveToLog("CheckObjectives: not enough saplings to plant tree farm")
- end
- end
- completeChests() -- use excess wood/wood2 to make chests or refuel
- end
- saveToLog('checkObjectives: checkItems{"keepTorches"} started', true)
- --If turtle loaded, may not be able to carry torches/signs to mark position of mineshafts
- if getNoOfEmptySlots(true) >= 3 then
- saveToLog('checkObjectives: loading torches and signs', true)
- checkItems{keepTorches = 1 + torchesNeeded, keepSigns = 1}
- elseif getNoOfEmptySlots(true) >= 2 then
- saveToLog('checkObjectives: not enough empty slots to carry signs', true)
- checkItems{keepTorches = 2 + torchesNeeded, keepSigns = 0}
- else
- saveToLog('checkObjectives: not enough empty slots to carry signs or torches', true)
- checkItems{keepTorches = 0 + torchesNeeded, keepSigns = 0}
- end
- if torchesNeeded > 0 then
- restoreTorches(side, colNo)
- end
- else --did not get enough wood on first run
- woodType = getWoodAvailable{woodNeeded = 4}
- if woodType ~= "none" then
- saveToLog('checkObjectives: place StorageTorches started', true)
- placeStorage:place("torches", false)
- if placeStorage:getStoragePlaced("torches") then
- saveToLog("checkObjectives: 1/9 - place StorageTorches completed")
- end
- else
- saveToLog("checkObjectives: Not enough wood to place storageTorches", false, true)
- end
- end
- if slot:getItemSlot("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, slotNo = slot:getItemSlot("planks"), delete = true}
- sortInventory(true)
- end
- if slot:getItemSlot("planks2") > 0 then
- turtle.select(slot:getItemSlot("planks2"))
- turtle.refuel()
- slot.update{self = slot, slotNo = slot:getItemSlot("planks2"), delete = true}
- sortInventory(true)
- end
- end
- return success --if true ready for deep mining early, otherwise continue harvestAllTrees
- end
- function checkRedstone()
- local success = false
- --assume on ground
- sortInventory(true)
- changeDirection("faceMine")
- if slot:getMostUnknownItemSlot() > 0 then
- saveToLog("checkRedstone: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- if turtle.getItemCount(i) > 1 then
- turtle.select(i)
- if turtle.getItemCount(i) > 15 then
- changeDirection("faceExtraStorage")
- if turtle.place() then --iron/gold/redstone
- if turtle.compare() then
- saveToLog("checkRedstone: redstone not found", true)
- dig.digNew{self = dig, callFrom = "checkRedstone"}
- else -- redstone
- saveToLog("checkRedstone: redstone confirmed", true)
- slot.update{self = slot, slotNo = i, newItem = "redstone"}
- objectives["redstone"] = true
- sortInventory(true)
- dig.digNew{self = dig, callFrom = "checkRedstone"}
- success = true
- break
- end
- changeDirection("faceMine")
- end
- end
- end
- end
- else
- saveToLog("checkRedstone: no unknown items to check", true)
- end
- changeDirection("faceMine")
- if success then
- if not placeStorage:getStoragePlaced("redstone") then
- placeStorage:place("redstone", false)
- else
- forward(12, 1)
- storeRedstone()
- turnRight(2)
- --return to furnace
- forward(12, 1)
- turnRight(2)
- end
- end
- return success
- end
- function checkRemaining()
- local tempItem = ""
- local reRun = false
- local itemFound = ""
- --already checked for iron, gold, redstone, lapis
- --lapis, diamond, obsidian, emerald, planks or fence from abandoned mine
- --see if axe can be crafted to show diamond. need sticks in storage below
- changeDirection("faceMine")
- if objectives["ironore"] and objectives["redstone"] and objectives["lapis"] then -- iron found
- saveToLog("checkRemaining: starting")
- checkItems{keepTorches = 0, keepSigns = 0, keepSticks = 2}
- repeat
- reRun = false
- if slot:getMostUnknownItemSlot() > 0 then
- for i = slot:getLeastUnknownItemSlot(), slot:getMostUnknownItemSlot(), 1 do
- tempItem = slot:getSlotContains(i)
- turtle.select(i)
- changeDirection("faceExtraStorage")
- if turtle.place() then --obsidian, moss stone < 6, planks or fence from old mineshaft
- dig.digNew{self = dig, slotNo = i, callFrom = "checkRemaining"}
- changeDirection("faceMine")
- if turtle.refuel() then --planks / fence
- saveToLog("checkRemaining: used wooden items as fuel", true)
- slot.update{self = slot, slotNo = i, delete = true}
- reRun = true
- break
- else
- if objectives["ironore"] and objectives["goldore"] then --gold and iron found
- itemFound = "obsidian"
- saveToLog("checkRemaining: obsidian found", true)
- slot.update{self = slot, slotNo = i, newItem = "obsidian"}
- --reRun = true
- break
- end
- end
- else --lapis, or diamond, possibly emerald
- changeDirection("faceMine")
- if slot:getItemSlot("diamonds") == 0 then
- -- if all mining complete need min 3 diamonds, if still mining and 4 or more then continue
- if turtle.getItemCount(i) > 3 then --test for diamond
- slot.update{self = slot, slotNo = i, newItem = "?diamonds"}
- if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "?diamonds", destSlot = 0} then
- saveToLog("Pickaxe crafted", true)
- slot.update{self = slot, item = "?diamonds", newItem = "diamonds"}
- itemFound = "diamonds"
- objectives["diamonds"] = true
- reRun = true
- break
- else --diamonds not found
- slot.update{self = slot, slotNo = i, newItem = tempItem}
- --[[turtle.select(i)
- while turtle.drop() do
- saveToLog("checkRemaining: dropping ? lapis after failed pickaxe craft", true)
- end
- slot.update{self = slot, slotNo = i, delete = true}]]--
- end
- end
- else
- if turtle.getItemCount(i) >= 3 then --test for diamond already done
- while turtle.drop() do
- saveToLog("checkRemaining: dropping ? lapis", true)
- end
- slot.update{self = slot, slotNo = i, delete = true}
- reRun = true
- break
- end
- end
- end
- end
- sortInventory(true)
- end
- until not reRun
- changeDirection("faceMine")
- if slot:getItemSlot("sticks") > 0 then
- forward(4, 1)
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(4, 1)
- turnRight(2)
- end
- if slot:getItemSlot("wood") > 0 then
- forward(6, 1)
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- if slot:getItemSlot("diamond pickaxe") > 0 then
- saveToLog("checkRemaining: storing diamond pickaxe in storagePickaxes",true)
- forward(14, 1)
- storeItem{toStore = storagePickaxes, item = "diamond pickaxe", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(14, 1)
- turnRight(2)
- end
- saveToLog("checkRemaining: all remaining items identified or dropped unless <4", true)
- else
- saveToLog("checkRemaining: either iron, gold, lapis, or redstone not yet identified", true)
- end
- return itemFound
- end
- function checkSugarCane(returnToGround)
- --function entered with block in front
- --return block type of top block
- local yCoordTemp = location:getY()
- local yCoordTemp2 = 0
- local useAmount = 0
- local useItem = ""
- local tempSlot = 0
- local tempDugAmount = 0
- local itemFound = ""
- local checkList = {}
- local blocksClimbed = 0
- local blockAbove = false
- local woodType = ""
- --used when forwardSafe is checking right and left sides only
- returnToGround = returnToGround or false
- checkList[1] = "all"
- --see if block in front is wood or wood2
- if slot:getItemSlot("wood") > 0 then
- turtle.select(slot:getItemSlot("wood"))
- if turtle.compare() then
- itemFound = "wood"
- end
- end
- if slot:getItemSlot("wood2") > 0 then
- turtle.select(slot:getItemSlot("wood2"))
- if turtle.compare() then
- itemFound = "wood2"
- end
- end
- --if wood/wood2 found exit function returning "wood"/"wood2"
- if itemFound == "" then --block in front is not wood or wood2 (if wood2 already known)
- saveToLog("checkSugarCane: moving up", true)
- while turtle.detect() do --climb up max 12 blocks
- if slot:getItemSlot("wood") > 0 then
- turtle.select(slot:getItemSlot("wood"))
- if turtle.compare() then
- itemFound = "wood"
- break
- end
- end
- if slot:getItemSlot("wood2") > 0 then
- turtle.select(slot:getItemSlot("wood2"))
- if turtle.compare() then
- itemFound = "wood2"
- break
- end
- end
- if slot:getItemSlot("cobble") > 0 then
- turtle.select(slot:getItemSlot("cobble"))
- if turtle.compare() then
- itemFound = "cobble"
- break
- end
- end
- up(1, 1, "saplings", "checkSugarCane")
- blocksClimbed = blocksClimbed + 1
- if location:getY() - yCoordTemp == 12 then --climb up max 10 places
- saveToLog("checkSugarCane: reached 12 blocks height", true)
- end
- sleep(0.5) --stops random "Too long without yielding" errors
- end
- --gone up until nothing in front, could be top of sugar cane, or top of leaves, or dirt. go forward and down
- if itemFound == "" then --wood/wood2/cobble not found
- saveToLog("checkSugarCane: moving over top of blocks", true)
- forward(1, 1)
- saveToLog("checkSugarCane: digging down", true)
- if blocksClimbed == 1 then --could be mushrooms/flowers/leaves/grass tuft or sand
- --success, dugItem, dugSlot = dig{direction = "down", checkForItems = "flowers"}
- dig.digNew{self = dig, direction = "down", checkForItems = "flowers", callFrom = "checkSugarCane"}
- else
- --success, dugItem, dugSlot = dig{direction = "down", checkForItems = "sugar cane,sand,gravel,wood2,saplings"}
- dig.digNew{self = dig, direction = "down", checkForItems = "sugar cane,sand,gravel,wood2,saplings", callFrom = "checkSugarCane"}
- end
- itemFound = dig:getDugItem()
- if dig:getDugItem() == "sugar cane" then --only happens if sugar cane already onboard
- saveToLog("checkSugarCane: more sugar cane found", true)
- itemFound = "sugar cane"
- down(1)--sit on top of sugar cane
- elseif dig:getDugItem() == "leaves" or dig:getDugItem() == "saplings" or dig:getDugItem() == "saplings2" then
- saveToLog("checkSugarCane: leaves found, digging down", true)
- --continue digging down until dirt/stone/cobble found
- --leaves may disappear so return to ground level anyway
- down(1, 1, "saplings") --move down to detect if more leaves
- while turtle.detectDown() do
- --success, dugItem, dugSlot = dig{self = slot, direction = "down", checkForItems = "saplings"}
- dig.digNew{self = dig, direction = "down", checkForItems = "saplings", callFrom = "checkSugarCane"}
- if dig:getDugItem() == "" or dig:getDugItem() == "leaves" or dig:getDugItem() == "saplings" or dig:getDugItem() == "saplings2" then
- down(1, 1)
- elseif dig:getDugItem() == "wood2" then
- saveToLog("checkSugarCane: wood2 found", true)
- itemFound = "wood2"
- down(2)
- break --leave detectDown() loop
- else --dirt, cobble sand etc found so replace
- saveToLog("checkSugarCane: "..dig:getDugItem().." found, replacing and moving on", true)
- -- replace last block found
- if dig:getDugSlot() > 0 then
- turtle.select(dig:getDugSlot())
- turtle.placeDown()
- end
- break -- leave detectDown() loop
- end
- end
- elseif string.find(dig:getDugItem(), "item") ~= nil then --new item or sugar cane returned
- --sugar cane can be placeDown() but does not compareDown() on top of sugar cane or blocks next to water
- --compareDown to check false = sugar cane, stone, grass but cobble already known and checked. Must be sugar cane
- --true = dirt,sand,gravel,wood,
- saveToLog("checkSugarCane: dug top of blocks checking for sugar cane", true)
- down(1)
- tempSlot, useAmount, useItem = compareBlock("down", checkList, 1) --does block below match anything
- if tempSlot == 0 then --sugar cane, stone, grass, saplings2 only. dirt and cobble known so must be cane
- saveToLog("checkSugarCane: no compareDown(): sugar cane found in "..dig:getDugSlot(), true)
- itemFound = "sugar cane"
- slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = "sugar cane"}
- --dig next block to make certain
- tempDugAmount = turtle.getItemCount(slot:getItemSlot("sugar cane"))
- dig.digNew{self = dig, direction = "down", checkForItems = "wood2", callFrom = "checkSugarCane"}
- if turtle.getItemCount(slot:getItemSlot("sugar cane")) == tempDugAmount then --not sugar cane
- itemFound = dig:getDugItem()
- slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = dig:getDugItem()}
- end
- else
- --check if wood2
- if slot:getItemSlot("wood2") == 0 then
- turtle.select(dig:getDugSlot())
- if turtle.refuel(0) then
- --itemFound = "wood2"
- --dig:getDugItem() = "wood2"
- itemFound = "wood2"
- dig.digNew{self = dig, direction = "down", slotNo = dig:getDugSlot(), expectedItem = "wood2", callFrom = "checkSugarCane"}
- slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = "wood2"}
- sortInventory(true)
- down(1)
- else
- saveToLog("checkSugarCane: "..dig:getDugItem().." found, keeping in stock", true)
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- else
- saveToLog("checkSugarCane: "..dig:getDugItem().." found, keeping in stock", true)
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- end
- elseif dig:getDugItem() == "sand" then
- if slot:getItemCount("sand") >= 7 then
- turtle.select(slot:getItemSlot("sand"))
- turtle.placeDown()
- end
- elseif dig:getDugItem() == "dirt" or dig:getDugItem() == "cobble" then
- turtle.select(dig:getDugSlot())
- turtle.placeDown()
- saveToLog("checkSugarCane: "..dig:getDugItem().." found and replaced", true)
- elseif dig:getDugItem() == "wood" or dig:getDugItem() == "wood2" then --on top of tree so harvest it
- turtle.select(slot:getItemSlot(dig:getDugItem()))
- local isBranched = false
- while turtle.compareDown() do
- isBranched = checkTree(dig:getDugItem(), isBranched, "checkSugarCane")
- down(1, 1)
- turtle.select(slot:getItemSlot(dig:getDugItem()))
- end
- turnRight(2)
- while turtle.detect() do
- --while location:getY() < yCoordTemp do
- up(1, 1)
- end
- turnRight(2)
- end
- if itemFound == "sugar cane" then --recognised by not comparing down. Currently on top of sugar cane
- --dig:getDugItem() = "sugar cane"
- while dig:getDugItem() == "sugar cane" do
- --success, dugItem, dugSlot = dig{self = slot, direction = "down"}
- dig.digNew{self = dig, direction = "down", callFrom = "checkSugarCane"}
- if dig:getDugItem() ~= "sugar cane" then --stop digging at base, should have just dug sand
- if dig:getDugItem() ~= "dirt" then
- if slot:getItemSlot("sand") == 0 then
- slot.update{self = slot, slotNo = dig:getDugSlot(), newItem = "sand"}
- end
- end
- break
- end
- down(1, 1)
- end
- --1 block below base of sugar cane - could be sand below
- yCoordTemp = location:getY() + 1 --water level
- if slot:getItemSlot("sand") > 0 then
- tempSlot = slot:getItemSlot("sand")
- if turtle.getItemCount(slot:getItemSlot("sand")) >= 8 then
- tempSlot = 0
- end
- else --sand not found yet
- dig.digNew{self = dig, direction = "down", expectedItem = "sand", callFrom = "checkSugarCane"}
- tempSlot = slot:getItemSlot("sand")
- end
- if tempSlot > 0 then
- objectives["sand"] = true
- if turtle.getItemCount(tempSlot) < 8 then
- turtle.select(tempSlot)
- down(1, tempSlot)
- while turtle.compareDown() do
- down(1, tempSlot)
- end
- end
- end
- while location:getY() < yCoordTemp do
- --up(1, 1)
- if getDirtStock() > 0 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- up(1)
- end
- elseif itemFound == "wood2" then
- turtle.select(slot:getItemSlot("wood2"))
- local isBranched = false
- while turtle.compareDown() do
- isBranched = checkTree(dig:getDugItem(), isBranched, "checkSugarCane")
- down(1, 1)
- turtle.select(slot:getItemSlot("wood2"))
- end
- turnRight(2)
- while turtle.detect() do
- up(1, 1)
- end
- turnRight(2)
- itemFound = "dirt"
- end
- if returnToGround then
- --if location:getY() < yCoordTemp then --lower than starting level, so back() will dig block
- while location:getY() < yCoordTemp do
- up(1, 1)
- end
- while location:getY() > yCoordTemp do
- if turtle.detectDown() then --only go down if nothing below
- break
- else
- down(1, 1)
- end
- end
- --end
- end
- back(1)
- else --itemFound = tree and wood/wood2 selected
- if itemFound ~= "cobble" then
- saveToLog("checkSugarCane: tree found above ground level", true)
- --now harvest rest of tree
- harvestTree(itemFound)
- end
- end
- end
- return itemFound --will return "wood", "wood2" if next to tree
- end
- function checkTorch(direction)
- -- dig torch on way back to base, turn and replace. If not present ignore
- local success = false
- local dugItem = ""
- local dugSlot = 0
- direction = direction or "home"
- if turtle.detect() then --? torch present
- dig.digNew{self = dig, expectedItem = "torches", callFrom = "checkTorch"}
- if dig:getSuccess() then
- if dig:getDugItem() == "torches" then
- forward(2, 1)
- turnRight(2)
- turtle.select(dig:getDugSlot())
- turtle.place()
- slot.update{self = slot, item = "torches", delete = true}
- if slot:getItemSlot("torches") == 0 then
- sortInventory(true)
- end
- turnRight(2)
- end
- else
- forward(2, 1)
- end
- else
- forward(2, 1)
- end
- if direction == "home" then
- changeDirection("faceMine")
- end
- end
- function checkTree(woodType, isBranched, calledFrom)
- for i = 1, 4 do
- isBranched = checkTreeBranch(woodType, isBranched, calledFrom)
- turnRight(1)
- end
- return isBranched
- end
- function checkTreeBranch(woodType, isBranched, calledFrom)
- --success, dugItem, dugSlot = dig{checkForItems = "saplings"} -- dig space for refuelling
- dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom} -- dig space for refuelling
- --saveToLog("checkTreeBranch: fuel level = "..turtle.getFuelLevel())
- if dig:getDugItem() == woodType then --wood/wood2 found
- if turtle.getFuelLevel() < 10 then
- refuel(0)
- end
- isBranched = true
- forward(1, 1, "saplings", calledFrom)
- checkTreeBranch(woodType, isBranched, calledFrom)
- turnLeft(1)
- checkTreeBranch(woodType, isBranched, calledFrom)
- turnRight(2)
- checkTreeBranch(woodType, isBranched, calledFrom)
- turnLeft(1)
- back(1)
- else
- if isBranched then
- if turtle.getFuelLevel() < 10 then
- refuel(0)
- end
- forward(1, 1, "saplings", calledFrom)
- turnLeft(1)
- dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
- turnRight(1)
- dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
- if dig:getDugItem() == woodType then --wood/wood2 found
- forward(1, 1, "saplings", calledFrom)
- turnLeft(1)
- dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
- turnRight(1)
- dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
- turnRight(1)
- dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
- turnLeft(1)
- back(1)
- end
- turnRight(1)
- dig.digNew{self = dig, checkForItems = "saplings", callFrom = calledFrom}
- turnLeft(1)
- back(1)
- end
- end
- return isBranched
- end
- function checkWalls(itemList, calledFrom)
- for j = 1, 4 do
- mineItem(itemList, calledFrom) --call recursive function
- refuel(0)
- turnRight(1)
- end
- return success
- end
- function checkWater(direction)
- local result = false
- local slotNo = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- slotNo = i
- break
- end
- end
- if slotNo > 0 then
- turtle.select(slotNo)
- if direction == "down" then
- if not turtle.detectDown() then
- if not turtle.compareDown() then --no block detected, but does not compare to air(empty slot)
- result = true
- end
- end
- elseif direction == "up" then
- if not turtle.detectUp() then
- if not turtle.compareUp() then
- result = true
- end
- end
- elseif direction == "forward" then
- if not turtle.detect() then
- if not turtle.compare() then
- result = true
- end
- end
- end
- end
- return result --true if water, (or lava)
- end
- function chooseWood()
- local woodType = ""
- local woodSpare = ""
- local success = false
- local numMoves = 0
- local emptySlot = 0
- local chestsNeeded = 7
- local numChests = 0
- -- return to tree farm, check if tree has grown.
- -- only do this when ready to mine for diamonds. While harvesting other trees leave alone
- forward(2, 1)
- turnRight(1)
- forward(2, 1)
- turnLeft(1)
- up(1, 1)
- -- compare wood/wood2 to block in front. If tree has grown
- for i = 1, 3 do -- check first 3 trees
- if turtle.detect() then
- turtle.select(slot:getItemSlot("wood"))
- if turtle.compare() then
- woodType = "wood"
- if slot:getItemSlot("wood2") > 0 then
- woodSpare = "wood2"
- end
- saveToLog("chooseWood: wood grown!", true)
- else
- if slot:getItemSlot("wood2") > 0 then
- turtle.select(slot:getItemSlot("wood2"))
- if turtle.compare() then
- woodType = "wood2"
- woodSpare = "wood"
- end
- saveToLog("chooseWood: wood2 grown!", true)
- end
- end
- saveToLog('chooseWood: objectives["choose wood"] = true', true)
- objectives["choose wood"] = true
- break
- else
- saveToLog("chooseWood: Tree "..i.." still a sapling", true)
- forward(3, 1)
- numMoves = numMoves + 1
- end
- end
- if not objectives["choose wood"] then
- saveToLog("chooseWood: wood not grown yet", true)
- end
- down(1, 1)
- turnLeft(1)
- forward(2, 1)
- turnLeft(1) --facing furnace
- if numMoves > 0 then
- for i = 1, numMoves do
- forward(3, 1)
- end
- end
- forward(2, 1) --under furnace
- turnLeft(2)
- if woodType == "" then
- saveToLog("chooseWood: wood not grown yet", true)
- else
- --convert spare wood/wood2 to chests/charcoal
- if woodSpare ~= "" then
- if slot:getItemSlot(woodSpare) > 0 then
- if turtle.getItemCount(slot:getItemSlot(woodSpare)) < 3 then --make planks and burn
- saveToLog("chooseWood: using "..turtle.getItemCount(slot:getItemSlot(woodSpare)).." excess "..woodSpare.." to refuel", true)
- --check no planks around
- if slot:getItemSlot("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- saveToLog("chooseWood: using existing planks to refuel", true)
- turtle.refuel()
- end
- saveToLog("chooseWood: crafting planks", true)
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = woodSpare, destSlot = 0, doSort = true}
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- else --craft chests if needed, min 4 wood = 2 chests
- if placeStorage:getStoragePlaced("redstone") then
- chestsNeeded = chestsNeeded - 2
- end
- if objectives["place extended storage"] then
- chestsNeeded = chestsNeeded - 5 --min 7
- end
- chestsNeeded = chestsNeeded - turtle.getItemCount(slot:getItemSlot("chests")) + 1
- if chestsNeeded > 0 then
- saveToLog("chooseWood: converting "..woodSpare.." to chests", true)
- numChests = math.floor(turtle.getItemCount(slot:getItemSlot(woodSpare)) / 2)
- if numChests > chestsNeeded then
- numChests = chestsNeeded
- end
- --need 14 wood for 7 chests
- if slot:getItemSlot("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- sortInventory(true)
- end
- craft{craftItem = "planks", craftQuantity = numChests * 8, sourceItem1 = woodSpare, destSlot = 0, doSort = true}
- craft{craftItem = "chests", craftQuantity = numChests, sourceItem1 = "planks", destSlot = 0, doSort = true}
- end
- if slot:getItemSlot(woodSpare) > 0 then
- if turtle.getItemCount(slot:getItemSlot(woodSpare)) > 0 then
- saveToLog("chooseWood: converting remaining "..woodSpare.." to charcoal", true)
- smelt("charcoal", 0, woodSpare)
- if slot.getItemSlot(slot, woodSpare) > 0 then
- turtle.select(slot.getItemSlot(slot, woodSpare))
- turtle.refuel()
- slot.update{self = slot, item = woodSpare, delete = true}
- end
- end
- end
- end
- if woodType == "wood2" then --change wood2 to wood
- emptySlot = slot:getItemSlot(woodType)
- slot.update{self = slot, slotNo = emptySlot, newItem = "wood"}
- end
- sortInventory(true)
- end
- end
- if slot:getItemSlot("saplings2") > 0 then --burn saplings2
- saveToLog("chooseWood: Saplings2 used for fuel", true)
- turtle.select(slot:getItemSlot("saplings2"))
- turtle.refuel()
- slot.update{self = slot, item = "saplings2", delete = true}
- sortInventory(true)
- end
- if not placeStorage:getMarkerPlaced("wood") then
- forward(7, 1)
- dig.digNew{self = dig, direction = "down", callFrom = "chooseWood"}
- turtle.select(slot:getItemSlot("wood"))
- turtle.placeDown()
- placeStorage:setMarkerPlaced("wood")
- turnRight(2)
- forward(7, 1)
- turnRight(2)
- end
- success = true
- end
- return success
- end
- function clearBase()
- --clear area around first tree 4 X 4 SQUARE
- reposition("FFRFFRFFFFRFFFFRFFFFRFRFFFLFFLFFLFRB", "wood2,saplings", true)
- --put furnace above
- turtle.select(slot:getItemSlot("furnaces"))
- turtle.placeUp()
- saveToLog("clearBase: furnace placed", true, true)
- slot.update{self = slot, item = "furnaces", delete = true}
- sortInventory(true)
- end
- function clearColumn(blockType, columnType)
- --move down to original y coord
- -- columnType = "storage" or "farm"
- local blockAbove = false
- if location:getY() > coordHome:getY() then
- saveToLog("Returning to original yCoord "..coordHome:getY(), true)
- while location:getY() > coordHome:getY() do
- down(1, 1)
- end
- elseif location:getY() < coordHome:getY() then
- while location:getY() < coordHome:getY() do
- up(1, 1)
- end
- end
- saveToLog("clearColumn checking fuel level", true)
- refuel(0)
- --at start of new col, could be space below, blocks above
- for i = 1, 15 do
- if i == 1 then
- placeBlock("cobble", columnType, i) -- placeBlock("cobble", "storage", 1)
- else
- placeBlock(blockType, columnType, i) -- placeBlock("cobble", "farm", 2)
- end
- --check above
- turtle.select(1)
- while turtle.detectUp() do
- up(1, 1)
- sleep(.5) -- allow for sand/gravel
- turnRight(1)
- dig.digNew{self = dig, waitForGravel = true, callFrom = "clearColumn"}
- turnRight(2)
- dig.digNew{self = dig, waitForGravel = true, callFrom = "clearColumn"}
- turnRight(1)
- end
- while location:getY() > coordHome:getY() do --gone upwards
- down(1, 1)
- end
- if i == 8 then --check fuel
- refuel(0)
- end
- if i < 15 then
- forward(1, 1)
- end
- end
- placeBlock("cobble", columnType, 16)
- saveToLog("Finished clearing column", true)
- end
- function clearTreeFarm()
- local emptySlot = 0
- local noOfSaplings = 0
- local noOfSaplings2 = 0
- local tempSlot = 0
- local useWood = "wood"
- local usePlanks = "planks"
- local success = false
- --first see how many saplings of each type and choose the most
- noOfSaplings = turtle.getItemCount(slot.getItemSlot(slot, "saplings"))
- if slot.getItemSlot(slot, "saplings2") > 0 then
- noOfSaplings2 = turtle.getItemCount(slot.getItemSlot(slot, "saplings2"))
- end
- if noOfSaplings < noOfSaplings2 then
- saveToLog(noOfSaplings2.." saplings2 collected, will use these for tree farming", true)
- --swap them over
- saveToLog("Swapping saplings with saplings2", false)
- emptySlot = getFirstEmptySlot()
- turtle.select(slot.getItemSlot(slot, "saplings"))
- turtle.transferTo(emptySlot) --put saplings in spare slot
- turtle.select(slot.getItemSlot(slot, "saplings2"))
- turtle.transferTo(slot.getItemSlot(slot, "saplings")) --put saplings2 into original saplings slot
- turtle.select(emptySlot)
- turtle.transferTo(slot.getItemSlot(slot, "saplings2")) --put original saplings into saplings2 slot
- slot.update{self = slot, item = "saplings"}
- slot.update{self = slot, item = "saplings2"}
- else
- saveToLog(noOfSaplings.." saplings collected, will use these for tree farming", true)
- end
- --delete saplings2 when harvestAllTrees and clearTreeFarm completed
- checkItems{keepSticks = 0, keepTorches = 9, keepSigns = 0} -- make 8 torches
- if slot:getItemSlot("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- end
- if slot:getItemSlot("planks2") > 0 then
- turtle.select(slot:getItemSlot("planks2"))
- turtle.refuel()
- slot.update{self = slot, item = "planks2", delete = true}
- end
- --clear farm area
- forward(1, 1) --start tree farm 1 blocks in front of furnace
- -- may be storage chests on first column
- for i = 1, 4 do --clear 8 columns to ground level with grass
- saveToLog("Clearing column"..i, true)
- if i == 1 then
- clearColumn("cobble", "storage") --left side of square col 1 no saplings
- else
- clearColumn("dirt", "farm") --left side of square col 1 no saplings
- end
- turnRight(1)
- forward(1, 1) --start of next column
- turnRight(1) --ready to go
- saveToLog("Clearing column".. 1 + i, true)
- if i == 1 or i == 4 then
- clearColumn("cobble", "farm") --left side col 2 no saplings
- else
- clearColumn("dirt", "farm") --left side col 2 no saplings
- end
- if i < 4 then --do not move when last col is finished
- turnLeft(1)
- forward(1, 1)
- turnLeft(1)
- end
- end
- turnRight(1)
- refuel(0)
- forward(5, 1)
- turnRight(1)
- forward(1, 1) -- now on bottom of col 3 ready to plant saplings
- saveToLog(" clearTreeFarm: Farm cleared, now planting saplings", true)
- treeFarm:reset()
- up(1, 1)
- plantSaplings()
- turnRight(1)
- forward(3, 1)
- turnRight(1)
- plantSaplings()
- down(1, 1)
- forward(2, 1)
- turnRight(1)
- forward(3, 1)
- --now in front of torch
- checkTorch("home")
- saveToLog(treeFarm:getNumSaplings().." Saplings planted. Continuing tree harvest while waiting for treefarm to grow...", true)
- success = true
- return success
- end
- function compareBlock(direction, checkList, checkNum)
- --direction = "forward"
- --checkList = {} checkList[1] = "wood", checkList[2] = "wood2" etc
- --checkNum = 2 no of items in checklist
- --useSlot, amount, useItem = compareBlock("forward", checkList, 3)
- --useSlot, amount, useItem = compareBlock("forward", {"all"}, 1) checkList[1] = "all"
- local slotFound = 0
- local amount = 0
- local checkItem = ""
- local checkSlot = 0
- local item = ""
- local matchItem = false
- if checkList[1] == "all" then
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if direction == "forward" then
- if turtle.compare() then --compare slot i with block in front
- matchItem = true
- end
- elseif direction == "up" then
- if turtle.compareUp() then --compare slot i with block above
- matchItem = true
- end
- elseif direction == "down" then
- if turtle.compareDown() then --compare slot i with block below
- matchItem = true
- end
- end
- if matchItem then
- slotFound = i
- amount = turtle.getItemCount(i)
- item = slot:getSlotContains(i)
- break
- end
- end
- end
- else
- for i = 1, checkNum do
- if checkList[i] == "item" then
- if slot:getFirstUnknownItem() ~= "" then
- for j = 1, slot:getUnknownItemCount() do
- checkItem = "item"..j
- checkSlot = slot:getItemSlot(checkItem)
- if checkSlot > 0 then
- turtle.select(checkSlot)
- if direction == "forward" then
- if turtle.compare() then --compare slot with block in front
- matchItem = true
- end
- elseif direction == "up" then
- if turtle.compareUp() then --compare slot with block above
- matchItem = true
- end
- elseif direction == "down" then
- if turtle.compareDown() then --compare slot with block below
- matchItem = true
- end
- end
- if matchItem then
- slotFound = checkSlot
- amount = turtle.getItemCount(checkSlot)
- item = checkList[i]
- break
- end
- end
- end
- end
- else
- checkItem = checkList[i]
- checkSlot = slot:getItemSlot(checkList[i])
- if checkSlot > 0 then
- turtle.select(checkSlot)
- if direction == "forward" then
- if turtle.compare() then --compare slot with block in front
- matchItem = true
- end
- elseif direction == "up" then
- if turtle.compareUp() then --compare slot with block above
- matchItem = true
- end
- elseif direction == "down" then
- if turtle.compareDown() then --compare slot with block below
- matchItem = true
- end
- end
- if matchItem then
- slotFound = checkSlot
- amount = turtle.getItemCount(checkSlot)
- item = checkList[i]
- break
- end
- end
- end
- end
- end
- return slotFound, amount, item
- end
- function completeChests()
- local woodAvailable = 0
- local woodNeeded = 0
- local wood4chests = 0
- local chestsFromWood = 0
- local chestsNeeded = 0
- -- Checks quantity of wood/wood2 and uses excess for chests/fuel as required
- -- will only operate if wood/wood2 > 20
- if not placeStorage:getStoragePlaced("redstone") then
- chestsNeeded = 2
- end
- if not objectives["place extended storage"] then
- chestsNeeded = chestsNeeded + 5
- end
- chestsNeeded = chestsNeeded - turtle.getItemCount(slot:getItemSlot("chests")) + 1
- if chestsNeeded < 0 then
- chestsNeeded = 0
- end
- woodNeeded = chestsNeeded * 2
- --max 7 chests needed = 56 planks = 14 wood
- woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 24
- if woodAvailable > 0 then
- if woodAvailable > 16 then
- woodAvailable = 16
- end
- if woodAvailable > 1 then
- wood4chests = math.floor(woodAvailable / 2)
- if chestsNeeded > 0 then
- if chestsNeeded >= wood4chests then --more chests needed than available wood
- chestsFromWood = wood4chests
- else
- chestsFromWood = chestsNeeded
- end
- saveToLog("completeChests: making chests", true)
- craft{craftItem = "planks", craftQuantity = chestsFromWood * 8, sourceItem1 = "wood", destSlot = 0, doSort = true}
- craft{craftItem = "chests", craftQuantity = chestsFromWood, sourceItem1 = "planks", destSlot = 0}
- chestsNeeded = chestsNeeded - chestsFromWood
- else
- saveToLog("completeChests: using excess wood to refuel", true)
- craft{craftItem = "planks", craftQuantity = woodAvailable * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- end
- else
- saveToLog("completeChests: only one spare wood - ignoring", true)
- end
- end
- if slot:getItemSlot("wood2") > 0 then
- woodAvailable = turtle.getItemCount(slot:getItemSlot("wood2")) - 24
- if woodAvailable > 0 then
- if woodAvailable > 16 then
- woodAvailable = 16
- end
- if woodAvailable > 1 then
- wood4chests = math.floor(woodAvailable / 2) --1 wood = 0, 2 wood = 1
- if chestsNeeded > 0 then
- if chestsNeeded >= wood4chests then --more chests needed than available wood
- chestsFromWood = wood4chests
- else
- chestsFromWood = chestsNeeded
- end
- saveToLog("completeChests: making chests", true)
- craft{craftItem = "planks2", craftQuantity = chestsFromWood * 8, sourceItem1 = "wood2", destSlot = 0, doSort = true}
- craft{craftItem = "chests", craftQuantity = chestsFromWood, sourceItem1 = "planks2", destSlot = 0}
- else
- saveToLog("completeChests: using excess wood to refuel", true)
- craft{craftItem = "planks2", craftQuantity = woodAvailable * 4, sourceItem1 = "wood2", destSlot = 0, doSort = true}
- turtle.select(slot:getItemSlot("planks2"))
- turtle.refuel()
- slot.update{self = slot, item = "planks2", delete = true}
- end
- else
- saveToLog("completeChests: only one spare wood2 - ignoring", true)
- end
- end
- end
- if not placeStorage:getStoragePlaced("redstone") then
- placeStorage:place("redstone", false)
- end
- return true
- end
- function confirmCobble()
- --[[suspected cobble found, so empty out
- all slots into chest except cobble,
- move it to slot 16 and craft furnace.
- If craft ok then move furnace
- else try another unknown block]]--
- local sTime = os.time()
- local foundCobble = false
- local useItem = ""
- local useSlot = 0
- sortInventory(true)
- --cobble, gravel, flint, iron ore, ? sand, ? clay may have been found
- term.clear()
- term.setCursorPos(1,1)
- useSlot = slot:getItemSlot("cobble")
- if useSlot > 0 then
- if craft{craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 0} then
- foundCobble = true
- else
- slot.update{self = slot, slotNo = useSlot, newItem = slot:getFreeUnknownItem()}
- end
- end
- if not foundCobble then
- if slot:getUnknownItemCount() > 0 then
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- saveToLog("confirmCobble: testing slot with unknown items = "..i.." with "..turtle.getItemCount(i).." items", false)
- turtle.select(i)
- if turtle.place() then --cobble, gravel, sand, iron ore, clay
- turtle.dig()
- if slot:getItemSlot("cobble") == 0 then
- useItem = slot:getSlotContains(i)
- slot.update{self = slot, slotNo = i, newItem = "cobble"}
- saveToLog("confirmCobble: attempting to craft furnace", true)
- if craft{craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 0} then
- foundCobble = true
- break
- else
- slot.update{self = slot, slotNo = i, newItem = useItem}
- end
- end
- end
- end
- end
- end
- saveToLog("confirmCobble started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
- return foundCobble
- end
- function craft(arg)
- --[[ assume empty chest next to turtle except at very early game stages.
- chest options: "doNotUse"
- doSort = true/false
- Examples:
- firstTree craft - planks from 1 wood: craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 3, chest = "doNotUse"}
- firstTree make more planks: craft{craftItem = "planks", craftQuantity = 8, sourceItem1 = "wood", destSlot = 3, chest = "doNotUse"}
- firstTree make 1 chest: craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 4}
- checkRemaining craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = emptySlot}
- late stage: craft{craftItem = "computer", craftQuantity = 1, sourceItem1 = "glass panes", sourceItem2 = "redstone",sourceItem3 = "stone", destSlot = 16}
- ]]--
- --local sourceItem1 = arg.sourceItem1
- --local sourceItem2 = arg.sourceItem2
- --local sourceItem3 = arg.sourceItem3
- local sourceSlot1 = 0
- local sourceSlot2 = 0
- local sourceSlot3 = 0
- local sourceQuantity1 = 0
- local sourceQuantity2 = 0
- local sourceQuantity3 = 0
- local success = false
- local useChest = true
- local emptySlot = 0
- local gridContents = {}
- local gridSlot = {}
- for i = 1, 16 do
- gridContents[i] = ""
- gridSlot[i] = 0
- end
- if arg.doSort == nil then
- arg.doSort = false
- end
- if arg.doSort then
- sortInventory(true)
- end
- if arg.sourceItem1 == nil then
- sourceSlot1 = 0
- sourceQuantity1 = 0
- else
- sourceSlot1 = slot:getItemSlot(arg.sourceItem1)
- sourceQuantity1 = turtle.getItemCount(sourceSlot1)
- end
- if arg.sourceItem2 == nil then
- arg.sourceItem2 = ""
- sourceSlot2 = 0
- else
- sourceSlot2 = slot.getItemSlot(slot, arg.sourceItem2)
- sourceQuantity2 = turtle.getItemCount(sourceSlot2)
- end
- if arg.sourceItem3 == nil then
- arg.sourceItem3 = ""
- sourceSlot3 = 0
- else
- sourceSlot3 = slot.getItemSlot(slot, arg.sourceItem3)
- sourceQuantity3 = turtle.getItemCount(sourceSlot3)
- end
- if arg.destSlot == nil then
- arg.destSlot = 16
- end
- if arg.chest == "doNotUse" then
- useChest = false
- end
- saveToLog("craft: craftItem = "..tostring(arg.craftItem)..
- ", craftQuantity = "..tostring(arg.craftQuantity)..
- ", sourceItem1 = "..tostring(arg.sourceItem1)..
- ", sourceItem2 = "..tostring(arg.sourceItem2)..
- ", sourceItem3 = "..tostring(arg.sourceItem3)..
- ", destSlot = "..tostring(arg.destSlot), false)
- if useChest then --place chest forwards
- --if arg.doSort then
- --sortInventory(true)
- --end
- turtle.select(1)
- while turtle.detect() do --clear space in front
- --turtle.dig()
- dig.digNew{self = dig, slotNo = 1, callFrom = "craft"}
- end
- --while turtle.attack() do --in case mob in front
- while attack() do --in case mobs in front
- saveToLog("Mob attacked again!", false)
- end
- turtle.select(slot:getItemSlot("chests"))
- if turtle.place() then
- --check if chest is actually in front until cc 1.6.3 bug is sorted
- local tempChestSlot = slot:getItemSlot("chests")
- slot.update{self = slot, item = "chests", delete = true}
- if not turtle.detect() then --chest not present
- saveToLog("cc 1.6.3 place() bug, moving forward to rescue chest", true)
- forward(1)
- if turtle.detect() then
- saveToLog("cc 1.6.3 place() bug, ? chest found in front, digging...", true)
- dig.digNew{self = dig, slotNo = tempChestSlot, expectedItem = "chests", callFrom = "craft"}
- elseif turtle.detectDown() then
- saveToLog("cc 1.6.3 place() bug, ? chest found below, digging...", true)
- dig.digNew{self = dig, direction = "down", slotNo = tempChestSlot, expectedItem = "chests", callFrom = "craft"}
- end
- turtle.select(tempChestSlot)
- if turtle.refuel(0) then
- saveToLog("cc 1.6.3 place() bug, chest found!", true)
- slot.update{self = slot, slotNo = tempChestSlot, item = "chests"}
- else
- saveToLog("cc 1.6.3 place() bug, ? chest lost. exiting program...", true)
- error()
- end
- back(1)
- turtle.select(slot:getItemSlot("chests"))
- turtle.place()
- slot.update{self = slot, item = "chests", delete = true}
- end
- saveToLog("craft: About to fill chest", false)
- fillChest{direction = arg.direction, exceptSlot1 = sourceSlot1, exceptSlot2 = sourceSlot2, exceptSlot3 = sourceSlot3, doReset = false} -- fill except chosen item(s)
- else
- saveToLog("UNABLE TO PLACE CHEST", true)
- error()
- end
- end
- if destSlot == 0 then
- saveToLog("Craft: item = "..arg.craftItem.." Quantity = "..arg.craftQuantity.." from "..arg.sourceItem1..", "..arg.sourceItem2..", "..arg.sourceItem3.." put in first empty slot", true)
- else
- saveToLog("Craft: item = "..arg.craftItem.." Quantity = "..arg.craftQuantity.." from "..arg.sourceItem1..", "..arg.sourceItem2..", "..arg.sourceItem3.." put in slot "..arg.destSlot, true)
- end
- --turtle emptied out except for crafting items
- if sourceSlot1 ~= 16 then --not already in position
- if turtle.getItemCount(16) == 0 then
- saveToLog("Craft: moving "..arg.sourceItem1.." from "..sourceSlot1.." to slot 16", true, false)
- turtle.select(sourceSlot1)
- turtle.transferTo(16) --move selected first item to slot 16
- slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
- else -- already occupied, inventory full, so swap 15 and 16
- for i = 1, 14 do
- if turtle.getItemCount(i) == 0 then
- emptySlot = i
- end
- break
- end
- if sourceSlot2 == 16 then
- sourceSlot2 = emptySlot
- end
- saveToLog("Craft: moving "..slot:getSlotContains(16).." from slot 16 to slot "..emptySlot, false)
- turtle.select(16)
- turtle.transferTo(emptySlot)
- slot.transfer{self = slot, item = slot:getSlotContains(16), transferTo = emptySlot}
- saveToLog("Craft: moving "..arg.sourceItem1.." from "..sourceSlot1.." to slot 16", false)
- turtle.select(sourceSlot1)
- turtle.transferTo(16) --move selected first item to slot 16
- slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
- end
- end
- turtle.select(16)
- if arg.craftItem == "planks" or arg.craftItem == "planks2" then --craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 3}
- turtle.transferTo(1, arg.craftQuantity / 4)
- gridContents[1] = arg.sourceItem1
- elseif arg.craftItem == "chests" then --craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 4}
- --8 planks = 1 chest
- turtle.transferTo(1, arg.craftQuantity)
- turtle.transferTo(2, arg.craftQuantity)
- turtle.transferTo(3, arg.craftQuantity)
- turtle.transferTo(5, arg.craftQuantity)
- turtle.transferTo(7, arg.craftQuantity)
- turtle.transferTo(9, arg.craftQuantity)
- turtle.transferTo(10, arg.craftQuantity)
- turtle.transferTo(11, arg.craftQuantity)
- gridContents[1] = arg.sourceItem1
- gridContents[2] = arg.sourceItem1
- gridContents[3] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridContents[7] = arg.sourceItem1
- gridContents[9] = arg.sourceItem1
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[2] = sourceSlot1
- gridSlot[3] = sourceSlot1
- gridSlot[5] = sourceSlot1
- gridSlot[7] = sourceSlot1
- gridSlot[9] = sourceSlot1
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot1
- elseif arg.craftItem == "furnaces" then --craft{craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 16}
- -- 8 cobble = 1 furnace
- turtle.transferTo(1, arg.craftQuantity)
- turtle.transferTo(2, arg.craftQuantity)
- turtle.transferTo(3, arg.craftQuantity)
- turtle.transferTo(5, arg.craftQuantity)
- turtle.transferTo(7, arg.craftQuantity)
- turtle.transferTo(9, arg.craftQuantity)
- turtle.transferTo(10, arg.craftQuantity)
- turtle.transferTo(11, arg.craftQuantity)
- gridContents[1] = arg.sourceItem1
- gridContents[2] = arg.sourceItem1
- gridContents[3] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridContents[7] = arg.sourceItem1
- gridContents[9] = arg.sourceItem1
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[2] = sourceSlot1
- gridSlot[3] = sourceSlot1
- gridSlot[5] = sourceSlot1
- gridSlot[7] = sourceSlot1
- gridSlot[9] = sourceSlot1
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot1
- elseif arg.craftItem == "walls" then --craft{craftItem = "walls", craftQuantity = 6, sourceItem1 = "moss stone", destSlot = 0}
- -- 6 mossy cobble = 6 mossy cobblestone wall
- turtle.transferTo(1, arg.craftQuantity / 6)
- turtle.transferTo(2, arg.craftQuantity / 6)
- turtle.transferTo(3, arg.craftQuantity / 6)
- turtle.transferTo(5, arg.craftQuantity / 6)
- turtle.transferTo(6, arg.craftQuantity / 6)
- turtle.transferTo(7, arg.craftQuantity / 6)
- gridContents[1] = arg.sourceItem1
- gridContents[2] = arg.sourceItem1
- gridContents[3] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridContents[7] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[2] = sourceSlot1
- gridSlot[3] = sourceSlot1
- gridSlot[5] = sourceSlot1
- gridSlot[6] = sourceSlot1
- gridSlot[7] = sourceSlot1
- elseif arg.craftItem == "sticks" then --craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = 13}
- -- 2 planks gives 4 sticks
- turtle.transferTo(1, arg.craftQuantity / 4)
- turtle.transferTo(5, arg.craftQuantity / 4)
- gridContents[1] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[5] = sourceSlot1
- elseif arg.craftItem == "nuggets" then --craft{craftItem = "nuggets", craftQuantity = 9, sourceItem1 = "gold", destSlot = 0}
- --1 gold gives 9 nuggets
- turtle.transferTo(1, arg.craftQuantity / 9)
- gridContents[1] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- elseif arg.craftItem == "buckets" then --craft{craftItem = "buckets", craftQuantity = 1, sourceItem1 = "iron", destSlot = 16}
- turtle.transferTo(1, arg.craftQuantity)
- turtle.transferTo(3, arg.craftQuantity)
- turtle.transferTo(6, arg.craftQuantity)
- gridContents[1] = arg.sourceItem1
- gridContents[3] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[3] = sourceSlot1
- gridSlot[6] = sourceSlot1
- elseif arg.craftItem == "crafting table" then --craft{craftItem = "crafting table", craftQuantity = 1, sourceItem1 = "planks", destSlot = 16}
- -- 4 planks = 1 table
- turtle.transferTo(1, arg.craftQuantity)
- turtle.transferTo(2, arg.craftQuantity)
- turtle.transferTo(5, arg.craftQuantity)
- turtle.transferTo(6, arg.craftQuantity)
- gridContents[1] = arg.sourceItem1
- gridContents[2] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[2] = sourceSlot1
- gridSlot[5] = sourceSlot1
- gridSlot[6] = sourceSlot1
- elseif arg.craftItem == "torches" then --craft{craftItem = "torches", craftQuantity = 4, sourceItem1 = "sticks", sourceItem2 = "charcoal" destSlot = 16}// OR sourceItem2 = "coal"
- -- 1 stick + 1 coal/charcoal = 4 torches
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) -- move coal/charcoal to 15
- turtle.transferTo(15)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- turtle.select(16)
- turtle.transferTo(5, arg.craftQuantity / 4) --move sticks to 5
- turtle.select(15)
- turtle.transferTo(1, arg.craftQuantity / 4) --move coal/charcoal to 1
- gridContents[1] = arg.sourceItem2
- gridContents[5] = arg.sourceItem1
- gridSlot[1] = sourceSlot2
- gridSlot[5] = sourceSlot1
- elseif arg.craftItem == "signs" then --craft{craftItem = "signs", craftQuantity = 3, sourceItem1 = "sticks", sourceItem2 = "planks" destSlot = 16}
- -- 1 stick + 6 planks = 3 signs
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) -- move planks to 15
- turtle.transferTo(15)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- turtle.select(16)
- turtle.transferTo(10, arg.craftQuantity / 3) --move sticks to 5
- turtle.select(15)
- turtle.transferTo(1, arg.craftQuantity / 3) --move planks to 1
- turtle.transferTo(2, arg.craftQuantity / 3) --move planks to 2
- turtle.transferTo(3, arg.craftQuantity / 3) --move planks to 3
- turtle.transferTo(5, arg.craftQuantity / 3) --move planks to 5
- turtle.transferTo(6, arg.craftQuantity / 3) --move planks to 6
- turtle.transferTo(7, arg.craftQuantity / 3) --move planks to 7
- gridContents[1] = arg.sourceItem2
- gridContents[2] = arg.sourceItem2
- gridContents[3] = arg.sourceItem2
- gridContents[5] = arg.sourceItem2
- gridContents[6] = arg.sourceItem2
- gridContents[7] = arg.sourceItem2
- gridContents[10] = arg.sourceItem1
- gridSlot[1] = sourceSlot2
- gridSlot[2] = sourceSlot2
- gridSlot[3] = sourceSlot2
- gridSlot[5] = sourceSlot2
- gridSlot[6] = sourceSlot2
- gridSlot[7] = sourceSlot2
- gridSlot[10] = sourceSlot1
- elseif arg.craftItem == "lapis block" or arg.craftItem == "redstone block" then --craft{craftItem = "lapis block", craftQuantity = 1, sourceItem1 = "lapis", destSlot = 0}
- --9 lapis = 1 lapis block
- turtle.transferTo(1, arg.craftQuantity)
- turtle.transferTo(2, arg.craftQuantity)
- turtle.transferTo(3, arg.craftQuantity)
- turtle.transferTo(5, arg.craftQuantity)
- turtle.transferTo(6, arg.craftQuantity)
- turtle.transferTo(7, arg.craftQuantity)
- turtle.transferTo(9, arg.craftQuantity)
- turtle.transferTo(10, arg.craftQuantity)
- turtle.transferTo(11, arg.craftQuantity)
- gridContents[1] = arg.sourceItem1
- gridContents[2] = arg.sourceItem1
- gridContents[3] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridContents[7] = arg.sourceItem1
- gridContents[9] = arg.sourceItem1
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[2] = sourceSlot1
- gridSlot[3] = sourceSlot1
- gridSlot[5] = sourceSlot1
- gridSlot[6] = sourceSlot1
- gridSlot[7] = sourceSlot1
- gridSlot[9] = sourceSlot1
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot1
- elseif arg.craftItem == "diamond pickaxe" then --craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "diamonds" destSlot = 16}
- --2 sticks + 3 diamonds = 1 axe
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) -- ?diamonds or diamonds
- turtle.transferTo(15)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- turtle.select(16)
- turtle.transferTo(6, arg.craftQuantity) --move sticks to 6
- turtle.transferTo(10, arg.craftQuantity) --move sticks to 10
- turtle.select(15)
- turtle.transferTo(1, arg.craftQuantity) --move diamond to 1
- turtle.transferTo(2, arg.craftQuantity) --move diamond to 2
- turtle.transferTo(3, arg.craftQuantity) --move diamond to 3
- gridContents[1] = arg.sourceItem2
- gridContents[2] = arg.sourceItem2
- gridContents[3] = arg.sourceItem2
- gridContents[6] = arg.sourceItem1
- gridContents[10] = arg.sourceItem1
- gridSlot[1] = sourceSlot2
- gridSlot[2] = sourceSlot2
- gridSlot[3] = sourceSlot2
- gridSlot[6] = sourceSlot1
- gridSlot[10] = sourceSlot1
- elseif arg.craftItem == "paper" then --craft{craftItem = "paper", craftQuantity = 3, sourceItem1 = "sugar cane", destSlot = 16}
- --3 sugar cane = 3 paper
- turtle.transferTo(1, arg.craftQuantity / 3)
- turtle.transferTo(2, arg.craftQuantity / 3)
- turtle.transferTo(3, arg.craftQuantity / 3)
- gridContents[1] = arg.sourceItem1
- gridContents[2] = arg.sourceItem1
- gridContents[3] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[2] = sourceSlot1
- gridSlot[3] = sourceSlot1
- elseif arg.craftItem == "fences" then --craft{craftItem = "fences", craftQuantity = 2, sourceItem1 = "sticks", destSlot = 0}
- -- 6 sticks = 2 fences
- turtle.transferTo(5, arg.craftQuantity / 2)
- turtle.transferTo(6, arg.craftQuantity / 2)
- turtle.transferTo(7, arg.craftQuantity / 2)
- turtle.transferTo(9, arg.craftQuantity / 2)
- turtle.transferTo(10, arg.craftQuantity / 2)
- turtle.transferTo(11, arg.craftQuantity / 2)
- gridContents[5] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridContents[7] = arg.sourceItem1
- gridContents[9] = arg.sourceItem1
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem1
- gridSlot[5] = sourceSlot1
- gridSlot[6] = sourceSlot1
- gridSlot[7] = sourceSlot1
- gridSlot[9] = sourceSlot1
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot1
- elseif arg.craftItem == "glass panes" then --craft{craftItem = "glass panes", craftQuantity = 16, sourceItem1 = "glass", destSlot = 0}
- -- 6 glass = 16 panes
- turtle.transferTo(5, arg.craftQuantity / 16)
- turtle.transferTo(6, arg.craftQuantity / 16)
- turtle.transferTo(7, arg.craftQuantity / 16)
- turtle.transferTo(9, arg.craftQuantity / 16)
- turtle.transferTo(10, arg.craftQuantity / 16)
- turtle.transferTo(11, arg.craftQuantity / 16)
- gridContents[5] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridContents[7] = arg.sourceItem1
- gridContents[9] = arg.sourceItem1
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem1
- gridSlot[5] = sourceSlot1
- gridSlot[6] = sourceSlot1
- gridSlot[7] = sourceSlot1
- gridSlot[9] = sourceSlot1
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot1
- elseif arg.craftItem == "floppy disk" then --craft{craftItem = "floppy disk", craftQuantity = 1, sourceItem1 = "paper", sourceItem2 = "redstone" destSlot = 16}
- -- 1 paper, 1 redstone
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) --redstone
- turtle.transferTo(15)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- turtle.select(16)
- turtle.transferTo(5, arg.craftQuantity) --move paper to 5
- turtle.select(15)
- turtle.transferTo(1, arg.craftQuantity) --move redstone to 1
- gridContents[1] = arg.sourceItem2
- gridContents[5] = arg.sourceItem1
- gridSlot[1] = sourceSlot2
- gridSlot[5] = sourceSlot1
- elseif arg.craftItem == "computer" then --craft{craftItem = "computer", craftQuantity = 1, sourceItem1 = "glass panes", sourceItem2 = "redstone",sourceItem3 = "stone", destSlot = 16}
- -- 1 glass panes, 1 redstone, 7 stone
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) --redstone
- turtle.transferTo(15)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- if sourceSlot3 ~= 14 then
- turtle.select(sourceSlot3) --stone
- turtle.transferTo(14)
- slot.transfer{self = slot, item = arg.sourceItem3, transferTo = 14}
- end
- turtle.select(16)
- turtle.transferTo(10, arg.craftQuantity) --move glass panes to 10
- turtle.select(15)
- turtle.transferTo(6, arg.craftQuantity) --move redstone to 6
- turtle.select(14) --stone
- turtle.transferTo(1, arg.craftQuantity) --move stone to 6
- turtle.transferTo(2, arg.craftQuantity)
- turtle.transferTo(3, arg.craftQuantity)
- turtle.transferTo(5, arg.craftQuantity)
- turtle.transferTo(7, arg.craftQuantity)
- turtle.transferTo(9, arg.craftQuantity)
- turtle.transferTo(11, arg.craftQuantity)
- gridContents[1] = arg.sourceItem3
- gridContents[2] = arg.sourceItem3
- gridContents[3] = arg.sourceItem3
- gridContents[5] = arg.sourceItem3
- gridContents[6] = arg.sourceItem2
- gridContents[7] = arg.sourceItem3
- gridContents[9] = arg.sourceItem3
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem3
- gridSlot[1] = sourceSlot3
- gridSlot[2] = sourceSlot3
- gridSlot[3] = sourceSlot3
- gridSlot[5] = sourceSlot3
- gridSlot[6] = sourceSlot2
- gridSlot[7] = sourceSlot3
- gridSlot[9] = sourceSlot3
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot3
- elseif arg.craftItem == "disk drive" then --craft{craftItem = "disk drive", craftQuantity = 1, sourceItem1 = "redstone", sourceItem2 = "stone", destSlot = 16}
- -- 2 redstone, 7 stone
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) --stone
- turtle.transferTo(15)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- turtle.select(16)
- turtle.transferTo(6, arg.craftQuantity) --move to 6
- turtle.transferTo(10, arg.craftQuantity) --move to 10
- turtle.select(15)
- turtle.transferTo(1, arg.craftQuantity) --move stone to 6
- turtle.transferTo(2, arg.craftQuantity)
- turtle.transferTo(3, arg.craftQuantity)
- turtle.transferTo(5, arg.craftQuantity)
- turtle.transferTo(7, arg.craftQuantity)
- turtle.transferTo(9, arg.craftQuantity)
- turtle.transferTo(11, arg.craftQuantity)
- gridContents[1] = arg.sourceItem2
- gridContents[2] = arg.sourceItem2
- gridContents[3] = arg.sourceItem2
- gridContents[5] = arg.sourceItem2
- gridContents[6] = arg.sourceItem1
- gridContents[7] = arg.sourceItem2
- gridContents[9] = arg.sourceItem2
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem2
- gridSlot[1] = sourceSlot2
- gridSlot[2] = sourceSlot2
- gridSlot[3] = sourceSlot2
- gridSlot[5] = sourceSlot2
- gridSlot[6] = sourceSlot1
- gridSlot[7] = sourceSlot2
- gridSlot[9] = sourceSlot2
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot2
- elseif arg.craftItem == "turtle" then --craft{craftItem = "turtle", craftQuantity = 1, sourceItem1 = "chests", sourceItem2 = "computer",sourceItem3 = "iron", destSlot = 16}
- -- 1 chest, 1 computer, 7 iron
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) --computer
- turtle.transferTo(15, arg.craftQuantity)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- if sourceSlot3 ~= 14 then
- turtle.select(sourceSlot3) --iron
- turtle.transferTo(14)
- slot.transfer{self = slot, item = arg.sourceItem3, transferTo = 14}
- end
- turtle.select(16)
- turtle.transferTo(10, arg.craftQuantity)
- turtle.select(15) --computer
- turtle.transferTo(6, arg.craftQuantity)
- turtle.select(14) --iron
- turtle.transferTo(1, arg.craftQuantity) --move iron
- turtle.transferTo(2, arg.craftQuantity)
- turtle.transferTo(3, arg.craftQuantity)
- turtle.transferTo(5, arg.craftQuantity)
- turtle.transferTo(7, arg.craftQuantity)
- turtle.transferTo(9, arg.craftQuantity)
- turtle.transferTo(11, arg.craftQuantity)
- gridContents[1] = arg.sourceItem3
- gridContents[2] = arg.sourceItem3
- gridContents[3] = arg.sourceItem3
- gridContents[5] = arg.sourceItem3
- gridContents[6] = arg.sourceItem2
- gridContents[7] = arg.sourceItem3
- gridContents[9] = arg.sourceItem3
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem3
- gridSlot[1] = sourceSlot3
- gridSlot[2] = sourceSlot3
- gridSlot[3] = sourceSlot3
- gridSlot[5] = sourceSlot3
- gridSlot[6] = sourceSlot2
- gridSlot[7] = sourceSlot3
- gridSlot[9] = sourceSlot3
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot3
- elseif arg.craftItem == "crafty mining turtle" then --craft{craftItem = "crafty mining turtle", craftQuantity = 1, sourceItem1 = "crafting table", sourceItem2 = "diamond pickaxe",sourceItem3 = "turtle", destSlot = 16}
- -- 1 crafting table, 1 diamond pickaxe, 1 turtle
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) --pickaxe
- turtle.transferTo(15, arg.craftQuantity)
- slot.transfer{self = slot, item = arg.sourceItem2, transferTo = 15}
- end
- if sourceSlot3 ~= 14 then
- turtle.select(sourceSlot3) --turtle
- turtle.transferTo(14)
- slot.transfer{self = slot, item = arg.sourceItem3, transferTo = 14}
- end
- turtle.select(16)
- turtle.transferTo(1, arg.craftQuantity) --move crafting table to 1
- turtle.select(15)
- turtle.transferTo(3, arg.craftQuantity) --move pickaxe to 3
- turtle.select(14)
- turtle.transferTo(2, arg.craftQuantity) --move turtle to 2
- gridContents[1] = arg.sourceItem1
- gridContents[2] = arg.sourceItem3
- gridContents[3] = arg.sourceItem2
- gridSlot[1] = sourceSlot1
- gridSlot[2] = sourceSlot3
- gridSlot[3] = sourceSlot2
- end
- if useChest then
- if turtle.getItemCount(16) > 0 then
- saveToLog("craft: adding excess "..arg.sourceItem1.." to chest from slot 16 (/"..sourceSlot1..")", false)
- fillChest{direction = arg.direction, addItem = arg.sourceItem1, addAmount = turtle.getItemCount(16), fromSlot = 16, originalSlot = sourceSlot1} -- add remaining source items to chest
- end
- if turtle.getItemCount(15) > 0 then
- saveToLog("craft: adding excess "..arg.sourceItem2.." to chest from slot 15 (/"..sourceSlot2..")", false)
- fillChest{direction = arg.direction, addItem = arg.sourceItem2, addAmount = turtle.getItemCount(15), fromSlot = 15, originalSlot = sourceSlot2} -- add remaining source items to chest
- end
- if turtle.getItemCount(14) > 0 then
- saveToLog("craft: adding excess "..arg.sourceItem3.." to chest from slot 14 (/"..sourceSlot3..")", false)
- fillChest{direction = arg.direction, addItem = arg.sourceItem3, addAmount = turtle.getItemCount(14), fromSlot = 14, originalSlot = sourceSlot3} -- add remaining source items to chest
- end
- --slot.update{self = slot, slotNo = 16, item = arg.sourceItem1, delete = true}
- slot.update{self = slot, item = arg.sourceItem1, delete = true}
- if arg.sourceItem2 ~= "" then
- slot.update{self = slot, item = arg.sourceItem2, delete = true}
- end
- if arg.sourceItem3 ~= "" then
- slot.update{self = slot, item = arg.sourceItem3, delete = true}
- end
- end
- -- Attempt to craft item
- turtle.select(16)
- saveToLog("Attempting to craft "..arg.craftItem.." in slot 16", false)
- if turtle.craft() then
- success = true
- saveToLog("craft: success "..arg.craftItem.." in slot 16", true)
- --now put crafted item in chest first, so will mix with any existing similar items
- if useChest then
- saveToLog("craft: adding crafted item "..arg.craftItem.." to chest", false)
- fillChest{direction = arg.direction, addItem = arg.craftItem, addAmount = turtle.getItemCount(16), fromSlot = 16, originalSlot = 16} -- add crafted item
- end
- else --crafting not successful, so empty out all items from chest
- saveToLog("Attempting to craft "..arg.craftItem.." did not succeed", true)
- --error()
- if useChest then
- saveToLog("craft: failed, filling chest to empty turtle", false)
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- fillChest{direction = arg.direction, addItem = gridContents[i], addAmount = turtle.getItemCount(i), fromSlot = i, originalSlot = gridSlot[i]} -- add crafted item
- end
- end
- end
- end
- if useChest then -- always, except in firstTree()
- saveToLog("Crafting finished, emptying chest back into turtle", false)
- emptyChest(arg.direction)
- if slot.getItemSlot(slot, "chests") > 0 then
- emptySlot = slot:getItemSlot("chests")
- else
- emptySlot = getFirstEmptySlot(false)
- end
- dig.digNew{self = dig, slotNo = emptySlot, expectedItem = "chests", callFrom = "craft"}
- slot.update{self = slot, slotNo = emptySlot, item = "chests"}
- if success then --item crafted OK. if existing eg torches, will be in correct slot already, else should return to 16
- turtle.select(16)
- if turtle.getItemCount(16) > 0 then --new item, so returned to 16
- if arg.destSlot == 0 then --use any slot
- arg.destSlot = getFirstEmptySlot(false)
- if arg.destSlot == 0 then --no empty slots
- arg.destSlot = 16
- end
- else
- if slot:getItemSlot(arg.craftItem) > 0 then
- arg.destSlot = slot:getItemSlot(arg.craftItem)
- end
- end
- if arg.destSlot == 16 then
- slot.update{self = slot, slotNo = 16, item = arg.craftItem}
- else --move to selected slot
- turtle.select(16)
- if turtle.transferTo(arg.destSlot) then
- saveToLog("craft: Moved "..arg.craftItem.." to slot "..arg.destSlot, false)
- --slot.update{self = slot, slotNo = arg.destSlot, item = arg.craftItem}
- slot.transfer{self = slot, item = arg.craftItem, transferTo = arg.destSlot}
- else --slot full/other item
- saveToLog("craft: Moving "..arg.craftItem.." to slot "..arg.destSlot.." failed. Still in slot 16", false)
- emptySlot = getFirstEmptySlot(false)
- if turtle.transferTo(emptySlot) then
- saveToLog("craft: Moving "..arg.craftItem.." to slot "..emptySlot.." instead", false)
- slot.update{self = slot, slotNo = emptySlot, item = arg.craftItem}
- end
- end
- end
- else
- saveToLog("craft: crafted item "..arg.craftItem.." already in correct slot", false)
- end
- sortInventory(true)
- end
- else --chest not used, only for first log first planks and first chest
- saveToLog("craft: "..arg.craftItem.." created in slot 16, moving to "..arg.destSlot, false)
- turtle.transferTo(arg.destSlot)
- slot.update{self = slot, slotNo = arg.destSlot, item = arg.craftItem}
- slot.update{self = slot, slotNo = 16, delete = true}
- end
- saveToLog("craft: function exit, success = "..tostring(success), false)
- return success
- end
- function craftMiningTurtle()
- local numWood = 0
- local woodNeeded = 0
- local planksNeeded = 0
- local numSticks = 0
- local sticksNeeded = 0
- local numTurtles = 1
- local startFile = ""
- local itemCount = {}
- --[[ storage contents:
- storageTorches: torches
- storageSigns: signs
- storageSticks: sticks
- storageWood: wood
- storageIronore: iron, (ironore), (buckets)
- storageSand: sugar cane, sand, glass, glass panes
- storageRedstone: redstone
- storagePickaxes: diamond pickaxe, paper
- ]]--
- --[[ Items needed:
- 6 sand 2 wood 8 planks -> smelt to glass -> craft to 16 glass panes --> use 1 per turtle
- floppy disk:
- 3 sugar cane
- 1 redstone
- disk drive:
- 7 cobble 2 wood 8 planks -> smelt to stone
- 1 redstone
- per turtle:
- 7 cobble 2 wood 7 planks -> smelt to stone
- 7 ironore 2 wood 7 planks -> smelt to iron
- 1 crafting table 1 wood 4 planks
- 1 chests 2 wood 8 planks
- 1 diamond pickaxe 2 sticks 2 planks
- 1 glass pane
- 1 redstone
- For Disk drive:
- 4 wood, 16 planks (includes glass smelt)
- For 1 turtle:
- 7 wood, 28 planks
- Min 11 wood, 44 planks
- Max 18 wood, 72 planks
- ]]--
- --[[
- start under furnace / over storage torches
- drop un-necessary items first to create crafting space
- storageTorches = 0
- storageSigns = 2
- storageSticks = 4
- storageWood = 6
- storageIronore = 8
- storageSand = 10
- storageRedstone = 12
- storagePickaxes = 14
- ]]--
- saveToLog("craftMiningTurtle: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!", true)
- fso:useFileName("logCraftMiningTurtle.txt")
- saveToLog("craftMiningTurtle: starting..", true)
- --get rid of unknown items
- if slot:getItemSlot("item1") > 0 then
- turnRight(2)
- forward(2, 1)
- for i = 1, 4 do
- storeItem{toStore = extraStorage1, item = "item"..i, quantity = 0, updateSlot = true, doSort = true}
- end
- turnRight(2)
- forward(2, 1)
- end
- if objectives["diamonds"] then
- if turtle.getItemCount(slot:getItemSlot("diamonds")) >= 6 then
- numTurtles = 2
- if storagePickaxes:getItemCount("diamond pickaxe") > 0 then --pickaxe already made
- sticksNeeded = 2
- else
- sticksNeeded = 4
- end
- elseif turtle.getItemCount(slot:getItemSlot("diamonds")) >= 3 then -- 3 - 5 diamonds
- if storagePickaxes:getItemCount("diamond pickaxe") > 0 then --pickaxe already made
- numTurtles = 2
- else
- numTurtles = 1
- end
- sticksNeeded = 2
- else -- 0 - 2 diamonds + pickaxe
- if storagePickaxes:getItemCount("diamond pickaxe") == 0 then
- sticksNeeded = 2
- end
- numTurtles = 1
- end
- if numTurtles == 1 then
- woodNeeded = 11
- planksNeeded = 44
- else
- woodNeeded = 18
- planksNeeded = 72
- end
- turnRight(1)
- dig.digNew{self = dig, expectedItem = "torches", callFrom = "craftMiningTurtle"}
- turnLeft(2)
- dig.digNew{self = dig, expectedItem = "torches", callFrom = "craftMiningTurtle"}
- turnRight(1)
- saveToLog("craftMiningTurtle: storing torches in storageTorches", true)
- storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
- forward(2) --storageSigns 2
- if slot:getItemSlot("signs") > 0 then
- saveToLog("craftMiningTurtle: storing signs in storageSigns", true)
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- end
- forward(2, 1) --storageSticks 4
- if slot:getItemSlot("sticks") > 0 then
- saveToLog("craftMiningTurtle: storing sticks in storageSticks", true)
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- end
- forward(4) -- storageIronore 8
- saveToLog("craftMiningTurtle: storing iron and buckets in storageIronore", true)
- storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
- storeItem{toStore = storageIronore, item = "buckets", quantity = 0, updateSlot = true, doSort = true}
- forward(2) -- storageSand 10
- saveToLog("craftMiningTurtle: removing sand and sugar cane from storageSand", true)
- getItemsFromStorage{fromStore = storageSand, item1 = "sand", item2 = "sugar cane"}
- storeItem{toStore = storageSand, item = "sand", quantity = turtle.getItemCount(slot:getItemSlot("sand")) - 6, updateSlot = true, doSort = false}
- saveToLog("craftMiningTurtle: storing dirt in storageSand", true)
- storeItem{toStore = storageSand, item = "dirt", quantity = 0, updateSlot = true, doSort = true}
- saveToLog("craftMiningTurtle: storing gravel in storageSand", true)
- storeItem{toStore = storageSand, item = "gravel", quantity = 0, updateSlot = true, doSort = true}
- saveToLog("craftMiningTurtle: storing coal in storageSand", true)
- storeItem{toStore = storageSand, item = "coal", quantity = 0, updateSlot = true, doSort = true}
- saveToLog("craftMiningTurtle: storing stone in storageSand", true)
- storeItem{toStore = storageSand, item = "stone", quantity = 0, updateSlot = true, doSort = true}
- forward(2) --storageRedstone 12
- saveToLog("craftMiningTurtle: removing redstone from storageRedstone", true)
- -- remove redstone, may be 64 already in store, so prepare to dump the rest
- for i = 1, 16 do
- itemCount[i] = turtle.getItemCount(i)
- end
- --storeItem{toStore = storageRedstone, item = "redstone", quantity = 0, updateSlot = true, doSort = true}
- getItemsFromStorage{fromStore = storageRedstone, item1 = "redstone"}
- -- check if overspill of redstone
- for i = 1, 16 do
- if i ~= slot:getItemSlot("redstone") then
- if turtle.getItemCount(i) > itemCount[i] then
- turtle.select(i)
- turtle.dropUp()
- break
- end
- end
- end
- -- check if enough redstone
- if turtle.getItemCount(slot:getItemSlot("redstone")) < 3 then --try removing more if it exists
- getItemsFromStorage{fromStore = storageRedstone, item1 = "redstone"}
- for i = 1, 16 do
- if i ~= slot:getItemSlot("redstone") then
- if turtle.getItemCount(i) > itemCount[i] then
- turtle.select(i)
- turtle.dropUp()
- break
- end
- end
- end
- end
- if turtle.getItemCount(slot:getItemSlot("redstone")) > 2 then
- storeItem{toStore = storageRedstone, item = "redstone", quantity = turtle.getItemCount(slot:getItemSlot("redstone")) - 3 - numTurtles, updateSlot = true, doSort = false}
- end
- turnRight(2)
- forward(6) --storageWood 6
- turnRight(2)
- saveToLog("craftMiningTurtle: removing wood from storage", true)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- turnRight(2)
- forward(2) --storageSticks 4
- turnRight(2)
- if sticksNeeded > 0 then --need 2/4 sticks
- saveToLog("craftMiningTurtle: removing sticks from storage", true)
- getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
- if slot:getItemSlot("sticks") > 0 then --sticks onboard
- numSticks = turtle.getItemCount(slot:getItemSlot("sticks"))
- end
- if sticksNeeded <= numSticks then
- sticksNeeded = 0
- end
- end
- turnRight(2)
- forward(4) --storageTorches 0
- turnRight(2)
- --wood: crafting table = 2, sticks = 1, smelt
- --planks: crafting table = 8, sticks = 2, smelt cobble = 21, smelt ironore = 14, smelt sand = 6, total 64
- if slot:getItemSlot("wood") > 0 then --wood already on board
- numWood = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- if numWood < woodNeeded then --not enough wood on board
- harvestTreeFarm(false)
- replantTreeFarm(false, true)
- end
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
- if planksNeeded <= 64 then
- planksNeeded = 0
- else
- planksNeeded = planksNeeded - 64
- end
- if sticksNeeded > 0 then
- craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = 0, doSort = true}
- end
- if numTurtles == 2 then --make diamond pickaxe, even if one is in storage
- craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "diamonds", destSlot = 0, doSort = true}
- -- else retrieve or make later
- if storagePickaxes:getItemCount("diamond pickaxe") == 0 then --no pickaxe already made
- saveToLog("craftMiningTurtle: storing diamond pickaxe in storagePickaxes", true)
- forward(14, 1) --storagePickaxes 4
- storeItem{toStore = storagePickaxes, item = "diamond pickaxe", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(14, 1) --under furnace
- turnRight(2)
- --make second pickaxe
- craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "diamonds", destSlot = 0, doSort = true}
- end
- end
- if storagePickaxes:getItemCount("diamond pickaxe") > 0 then --pickaxe already made
- saveToLog("craftMiningTurtle: storing sticks in storageSticks", true)
- forward(4, 1) --storageSticks 4
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(4, 1) --under furnace
- turnRight(2)
- end
- smelt("cobble", 7 * numTurtles + 7)
- smelt("ironore", 7 * numTurtles)
- smelt("sand", 6)
- if planksNeeded > 0 then
- craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
- planksNeeded = 0
- end
- saveToLog("craftMiningTurtle: storing wood in storageWood", true)
- forward(6, 1) -- storageWood = 6
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- forward(2) --over storageIronore 8
- saveToLog("craftMiningTurtle: storing ironore in storageIronore", true)
- storeItem{toStore = storageIronore, item = "ironore", quantity = 0, updateSlot = true, doSort = true}
- forward(2) --over storageSand 10
- saveToLog("craftMiningTurtle: storing cobble in storageSand", true)
- --storeItem{toStore = storageSand, item = "sand", quantity = 0, updateSlot = true, doSort = true}
- storeItem{toStore = storageSand, item = "cobble", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(10, 1) --under furnace
- turnRight(2)
- craft{craftItem = "chests", craftQuantity = numTurtles, sourceItem1 = "planks", destSlot = 0, doSort = true}
- craft{craftItem = "crafting table", craftQuantity = 1 * numTurtles, sourceItem1 = "planks", destSlot = 0, doSort = true}
- forward(6, 1) -- storageWood 6
- saveToLog("craftMiningTurtle: storing planks in storageWood", true)
- storeItem{toStore = storageWood, item = "planks", quantity = 0, updateSlot = true, doSort = true}
- forward(4, 1) --storageSand 10, facing mine
- craft{craftItem = "paper", craftQuantity = 3, sourceItem1 = "sugar cane", destSlot = 0, doSort = true}
- saveToLog("craftMiningTurtle: storing sugar cane in storageSand", true)
- storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
- craft{craftItem = "glass panes", craftQuantity = 16, sourceItem1 = "glass", destSlot = 0, doSort = true}
- saveToLog("craftMiningTurtle: storing glass in storageSand", true)
- storeItem{toStore = storageSand, item = "glass", quantity = 0, updateSlot = true, doSort = true}
- saveToLog("craftMiningTurtle: storing glass panes in storageSand", true)
- storeItem{toStore = storageSand, item = "glass panes", quantity = 16 - numTurtles, updateSlot = true, doSort = true}
- craft{craftItem = "floppy disk", craftQuantity = 1, sourceItem1 = "paper", sourceItem2 = "redstone", destSlot = 0, doSort = true}
- saveToLog("craftMiningTurtle: storing paper in storageSand", true)
- storeItem{toStore = storageSand, item = "paper", quantity = 0, updateSlot = true, doSort = true}
- if numTurtles == 1 then
- --get pickaxe from chest
- if storagePickaxes:getItemCount("diamond pickaxe") > 0 then
- forward(4, 1) --storagePickaxes 14, facing mine
- saveToLog("craftMiningTurtle: removing diamond pickaxe from storagePickaxes", true)
- getItemsFromStorage{fromStore = storagePickaxes, item1 = "diamond pickaxe"}
- turnRight(2)
- forward(4, 1) --storageSand 10, facing furnace
- else
- turnRight(2) -- facing furnace
- end
- else
- turnRight(2) -- facing furnace
- end
- forward(10, 1) --under furnace
- turnRight(2)
- craft{craftItem = "computer", craftQuantity = 1 * numTurtles, sourceItem1 = "glass panes", sourceItem2 = "redstone", sourceItem3 = "stone", destSlot = 0, doSort = true}
- craft{craftItem = "turtle", craftQuantity = 1 * numTurtles, sourceItem1 = "chests", sourceItem2 = "computer", sourceItem3 = "iron", destSlot = 0, doSort = true}
- craft{craftItem = "crafty mining turtle", craftQuantity = 1, sourceItem1 = "crafting table", sourceItem2 = "diamond pickaxe", sourceItem3 = "turtle", destSlot = 0, doSort = true}
- craft{craftItem = "disk drive", craftQuantity = 1, sourceItem1 = "redstone", sourceItem2 = "stone", destSlot = 0, doSort = true}
- forward(8) -- storageIronore 8
- if slot:getItemSlot("iron") > 0 then
- saveToLog("craftMiningTurtle: storing excess iron in storageIronore", true)
- storeItem{toStore = storageIronore, item = "iron", quantity = 0, updateSlot = true, doSort = true}
- end
- forward(6, 1) -- storagePickaxes 14
- if numTurtles == 2 then
- if storagePickaxes:getItemCount("diamond pickaxe") > 0 then
- saveToLog("craftMiningTurtle: removing diamond pickaxe from storagePickaxes", true)
- getItemsFromStorage{fromStore = storagePickaxes, item1 = "diamond pickaxe"}
- end
- end
- turnRight(2)
- forward(2, 1) -- storageRedstone 12
- saveToLog("craftMiningTurtle: storing redstone in storageRedstone", true)
- storeItem{toStore = storageRedstone, item = "redstone", quantity = 0, updateSlot = true, doSort = true}
- forward(10, 1) -- storageSigns 2
- getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
- storeItem{toStore = storageSigns, item = "signs", quantity = turtle.getItemCount(slot:getItemSlot("signs")) - 1, updateSlot = true, doSort = false}
- forward(3, 1)
- turnRight(2)
- saveToLog("craftMiningTurtle: removing fuel from furnace", true)
- up(1, 1)
- turtle.select(getFirstEmptySlot())
- if turtle.suck() then
- turtle.refuel()
- end
- down(1)
- forward(1, 1)
- sortInventory(true)
- --destroy furnace
- storeItem{toStore = storageTorches, item = "planks", quantity = 0, updateSlot = true, doSort = true}
- saveToLog("craftMiningTurtle: breaking furnace", true)
- dig.digNew{self = dig, direction = "up", expectedItem = "furnaces", callFrom = "craftMiningTurtle"}
- sortInventory(true)
- saveToLog("craftMiningTurtle: storing furnace in storageTorches", true)
- storeItem{toStore = storageTorches, item = "furnaces", quantity = 0, updateSlot = true, doSort = true}
- --program mining turtle
- saveToLog("craftMiningTurtle: placing disk drive", true)
- turtle.select(slot:getItemSlot("disk drive"))
- turtle.place()
- slot.update{self = slot, item = "disk drive", delete = true}
- sortInventory(true)
- saveToLog("craftMiningTurtle: inserting floppy disk", true)
- turtle.select(slot:getItemSlot("floppy disk"))
- turtle.drop()
- slot.update{self = slot, item = "floppy disk", delete = true}
- sortInventory(true)
- up(1)
- forward(1) --now on top of disk drive
- if disk.isPresent("bottom") then
- local filepath = shell.getRunningProgram()
- local filename = fs.getName(filepath)
- if fs.getFreeSpace("/") > 9000000 then --config modified. use filecopy
- saveToLog("craftMiningTurtle: copying files to floppy disk", true)
- fs.copy(filepath, "/disk/"..filename)
- end
- --[[
- This script is a file called startup stored on the floppy disk
- When a turtle is placed next to the disk drive, it reads this script
- which opens 'minerList.txt' and sets the label to Miner2, (as '2' is stored in this file)
- Either copies start.lua to the turtle then modifies 'minerList.txt' to '3'
- or if on a server, requests the start.lua file via http from pastebin
- so the name Miner3 given for the second turtle, (if constructed)
- ]]--
- -- create/overwrite 'minerList.txt' on floppy disk
- startFile = fs.open("/disk/minerList.txt", "w")
- startFile.writeLine("2")
- startFile.close()
- -- create/overwrite startup
- startFile = fs.open("/disk/startup", "w")
- startFile.writeLine('function get(name, code)')
- startFile.writeLine(' local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))')
- startFile.writeLine(' if response then')
- startFile.writeLine(' local sCode = response.readAll()')
- startFile.writeLine(' if sCode ~= nil and sCode ~= "" then')
- startFile.writeLine(' local file = fs.open( name, "w" )')
- startFile.writeLine(' response.close()')
- startFile.writeLine(' file.write(sCode)')
- startFile.writeLine(' file.close()')
- startFile.writeLine(' return true')
- startFile.writeLine(' end')
- startFile.writeLine(' end')
- startFile.writeLine(' return false')
- startFile.writeLine('end')
- startFile.writeLine('if fs.exists("/disk/minerList.txt") then')
- startFile.writeLine(' textFile = fs.open("/disk/minerList.txt", "r")')
- startFile.writeLine(' textIn = textFile.readAll()')
- startFile.writeLine(' minerName = "Miner"..textIn')
- startFile.writeLine(' textFile.close()')
- startFile.writeLine(' textOut = tostring(tonumber(textIn) + 1)')
- startFile.writeLine('else')
- startFile.writeLine(' minerName = "Miner2"')
- startFile.writeLine(' textOut = "3"')
- startFile.writeLine('end')
- startFile.writeLine('textFile = fs.open("/disk/minerList.txt", "w")')
- startFile.writeLine('textFile.writeLine(textOut)')
- startFile.writeLine('textFile.close()')
- startFile.writeLine('if os.getComputerLabel() == nil or string.find(os.getComputerLabel(),"Miner") == nil then')
- startFile.writeLine(' os.setComputerLabel(minerName)')
- startFile.writeLine('end')
- startFile.writeLine('print("Hello, my name is "..os.getComputerLabel())')
- startFile.writeLine('if fs.exists("/disk/start.lua") then')
- startFile.writeLine(' fs.copy("/disk/start.lua", "start.lua")')
- startFile.writeLine(' print("Replication program copied")')
- startFile.writeLine('else')
- startFile.writeLine(' get("start.lua", "'..pastebinCode..'")')
- startFile.writeLine('end')
- startFile.writeLine('print("use start.lua to begin self replicating ")')
- startFile.close()
- end
- back(1)
- down (1)
- turnRight(1)
- forward(1)
- turnLeft(1)
- saveToLog("craftMiningTurtle: placing crafty mining turtle", true)
- turtle.select(slot:getItemSlot("crafty mining turtle"))
- turtle.place() --next to disk drive
- slot.update{self = slot, item = "crafty mining turtle", delete = true}
- sortInventory(true)
- turnLeft(1)
- if numTurtles == 2 then
- craft{craftItem = "crafty mining turtle", craftQuantity = 1, sourceItem1 = "crafting table", sourceItem2 = "diamond pickaxe", sourceItem3 = "turtle", destSlot = 0}
- forward(1)
- turnRight(1)
- up(1)
- saveToLog("craftMiningTurtle: placing second crafty mining turtle", true)
- turtle.select(slot:getItemSlot("crafty mining turtle"))
- turtle.place() --on top of disk drive
- down(1, 1)
- saveToLog("Two Crafty Mining Turtles placed. All objectives acheived!", true)
- else
- forward(1)
- turnRight(1)
- saveToLog("Crafty Mining Turtle placed. Insufficient diamonds found to create two", true)
- end
- up(1)
- if slot:getItemCount("signs") > 0 then
- back(1)
- turtle.select(slot:getItemSlot("signs"))
- turtle.placeDown("Mission\nComplete!")
- forward(1)
- end
- if numTurtles == 2 then
- saveToLog("Mission successful. I have replicated myself twice", true)
- saveToLog("Right-Click on my offspring to check their status...", true)
- else
- saveToLog("Mission partially successful. I have replicated myself once", true)
- saveToLog("Right-Click on my offspring to check its status...", true)
- end
- else
- saveToLog("Insufficient diamonds found in this area. Please break turtle , reposition and restart", true)
- end
- end
- function craftSigns()
- local woodAvailable = 0
- local wood2Available = 0
- local woodType = "wood"
- local planksType = "planks"
- local success = false
- local numSignsOnboard = 0
- local numWoodOnboard = 0
- local numWoodStored = 0
- local numSignsStored = 0
- local overStore = true
- --always make 3 signs only , need 8 planks, leaves 3 sticks
- --only attempt when over storageTorches
- saveToLog("craftSigns: started, crafting 3 signs", true)
- -- will always be over storageTorches unless not yet placed
- if not placeStorage:getStoragePlaced("torches") then
- overStore = false
- end
- numSignsStored = storageSigns:getItemCount("signs")
- numWoodStored = storageWood:getItemCount("wood")
- if slot:getItemSlot("signs") > 0 then
- numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
- end
- if slot:getItemSlot("wood") > 0 then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- if slot:getItemSlot("wood2") > 0 then
- if turtle.getItemCount(slot:getItemSlot("wood2")) > numWoodOnboard then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood2"))
- woodType = "wood2"
- planksType = "planks2"
- end
- end
- end
- if overStore then
- if numSignsStored > 0 then
- forward(2, 1)
- saveToLog("craftSigns: "..numSignsStored.." signs removed from storage", true)
- getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
- numSignsOnboard = turtle.getItemCount(slot:getItemSlot("signs"))
- back(2)
- end
- if slot:getItemSlot("wood") == 0 then --wood in storage, needed for signs
- forward(6, 1)
- saveToLog("craftTorches: "..numWoodStored.." wood removed from storage", true)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- end
- if numWoodOnboard >= 3 then --enough to make signs
- saveToLog("craftSigns: crafting planks from "..woodType, false)
- craft{craftItem = planksType, craftQuantity = 8, sourceItem1 = woodType, destSlot = 0, doSort = true}
- saveToLog("craftSigns: crafting sticks from "..planksType, false)
- craft{craftItem = "sticks", craftQuantity = 4, sourceItem1 = planksType, destSlot = 0, doSort = true}
- saveToLog("craftSigns: crafting signs from "..planksType.." and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
- if craft{craftItem = "signs", craftQuantity = 3, sourceItem1 = "sticks", sourceItem2 = planksType, destSlot = 0, doSort = true} then
- success = true
- end
- else
- saveToLog("craftSigns: insufficient wood available", true)
- end
- return success
- end
- function craftSticks(quantity)
- local success = false
- local makePlanks = false
- local woodType = "wood"
- local planksType = "planks"
- local woodNeeded = 0
- local planksNeeded = 0
- local success = false
- local numWoodOnboard = 0
- local numPlanksOnboard = 0
- if quantity <= 4 then
- quantity = 4
- planksNeeded = 4
- woodNeeded = 1
- elseif quantity <= 8 then
- quantity = 8
- planksNeeded = 4
- woodNeeded = 1
- elseif quantity <= 12 then
- quantity = 12
- planksNeeded = 8
- woodNeeded = 2
- else
- quantity = 16
- planksNeeded = 8
- woodNeeded = 2
- end
- if slot:getItemSlot("planks") > 0 then
- numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks"))
- end
- if slot:getItemSlot("planks2") > 0 then
- numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks2"))
- planksType = "planks2"
- end
- if numPlanksOnboard < planksNeeded then
- makePlanks = true
- saveToLog("craftSticks: started, crafting planks", true)
- --woodNeeded already calculated
- else
- woodNeeded = 0 --reset
- saveToLog("craftSticks: started, using "..planksType.." already onboard", true)
- end
- if makePlanks then --need wood
- if slot:getItemSlot("wood") > 0 then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- if slot:getItemSlot("wood2") > 0 then
- if turtle.getItemCount(slot:getItemSlot("wood2")) > numWoodOnboard then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood2"))
- woodType = "wood2"
- planksType = "planks2"
- end
- end
- end
- if slot:getItemSlot("wood") == 0 and storageWood:getItemCount("wood") > 0 then --wood in storage
- if makePlanks then --wood needed for planks so go get
- forward(6, 1)
- saveToLog("craftSticks: "..storageWood:getItemCount("wood").." wood removed from storage", true)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- end
- if numWoodOnboard >= woodNeeded then
- --planksType will be "planks" unless planks2 are in stock
- saveToLog("craftSticks: crafting "..planksType.." for sticks", true)
- craft{craftItem = planksType, craftQuantity = planksNeeded, sourceItem1 = woodType, destSlot = 0, doSort = true}
- end
- end
- saveToLog("craftSticks: crafting "..quantity.." sticks from "..planksType, true)
- if craft{craftItem = "sticks", craftQuantity = quantity, sourceItem1 = planksType, destSlot = 0, doSort = true} then
- objectives["craft sticks"] = true
- success = true
- end
- return success
- end
- function craftTorches(quantity)
- local headType = "charcoal"
- local headQuantity = 0
- local makeCharcoal = false
- local makePlanks = false
- local makeSticks =false
- local woodType = "wood"
- local planksType = "planks"
- local numWoodNeeded = 0
- local planksNeeded = 0
- local sticksNeeded = 0
- local success = false
- local numSticksOnboard = 0
- local numTorchesOnboard = 0
- local numWoodOnboard = 0
- local numTorchesStored = 0
- local overStore = true
- local doContinue = true
- -- will always be over storageTorches unless not yet placed
- --[[if not placeStorage:getStoragePlaced("torches") then
- overStore = false
- end]]--
- if slot:getItemSlot("torches") > 0 then
- numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
- end
- if slot:getItemSlot("sticks") > 0 then
- numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
- end
- if slot:getItemSlot("wood") > 0 then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- if slot:getItemSlot("wood2") > 0 then
- if turtle.getItemCount(slot:getItemSlot("wood2")) > numWoodOnboard then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood2"))
- woodType = "wood2"
- planksType = "planks2"
- end
- end
- end
- -- 4 torches min : 1 head + 1 stick = 4 torches. Min sticks = 4, min planks = 4
- -- torches head planks sticks
- -- 4 1 4 4
- -- 8 2 4 4
- -- 12 3 4 4
- -- 16 4 4 4
- -- 20 5 4 8
- -- 24 6 4 8
- -- 28 7 4 8
- -- 32 8 4 8
- -- 36 9 8 12
- -- 40 10 8 12
- -- 44 11 8 12
- -- 48 12 8 12
- -- 52 13 8 16
- -- 56 14 8 16
- -- 60 15 8 16
- -- 64 16 8 16
- quantity = math.floor(quantity / 4) * 4
- if quantity == 0 then
- quantity = 4 -- torches
- headQuantity = 1 -- coal /charcoal
- planksNeeded = 4 -- 1 wood
- sticksNeeded = 4 -- 2 planks
- numWoodNeeded = 1
- elseif quantity <= 16 then-- 4, 8, 12, 16
- headQuantity = quantity / 4 -- coal, charcoal
- planksNeeded = 4 -- 8 planks = 16 sticks
- sticksNeeded = 4 -- 4 sticks
- numWoodNeededwoodNeeded = 1
- elseif quantity <= 32 then-- 4, 8, 12, 16
- headQuantity = quantity / 4 -- coal, charcoal
- planksNeeded = 4 -- 8 planks = 16 sticks
- sticksNeeded = 8 -- 8 sticks
- numWoodNeeded = 2
- elseif quantity <= 48 then-- 4, 8, 12, 16
- headQuantity = quantity / 4 -- coal, charcoal
- planksNeeded = 8 -- 8 planks = 16 sticks
- sticksNeeded = 12 -- 12 sticks
- numWoodNeeded = 2
- else
- headQuantity = quantity / 4 -- coal, charcoal
- planksNeeded = 8 -- 8 planks = 16 sticks
- sticksNeeded = 16 -- 16 sticks
- numWoodNeeded = 2
- end
- saveToLog("craftTorches: quantity = "..quantity, true)
- --need either coal or charcoal
- if slot:getItemSlot("coal") > 0 then
- if turtle.getItemCount(slot:getItemSlot("coal")) - 1 >= headQuantity then
- headType = "coal"
- saveToLog("craftTorches: using coal", true)
- end
- end
- if headType == "charcoal" then --default value
- if slot:getItemSlot("charcoal") > 0 then
- if turtle.getItemCount(slot:getItemSlot("charcoal")) < headQuantity then
- makeCharcoal = true
- saveToLog("craftTorches: using charcoal", true)
- end
- else
- saveToLog("craftTorches: charcoal on crafting list", true)
- makeCharcoal = true
- end
- end
- if storageTorches:getItemCount("torches") > 0 then
- saveToLog("craftTorches: "..storageTorches:getItemCount("torches").." torches removed from storage", true)
- getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
- numTorchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
- end
- if storageSticks:getItemCount("sticks") > 0 then
- forward(4, 1)
- saveToLog("craftTorches: "..storageSticks:getItemCount("sticks").." sticks removed from storage", true)
- getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
- numSticksOnboard = turtle.getItemCount(slot:getItemSlot("sticks"))
- turnRight(2)
- forward(4, 1)
- turnRight(2)
- end
- if slot:getItemSlot("wood") == 0 and makeCharcoal then --wood in storage, needed for charcoal
- forward(6, 1)
- saveToLog("craftTorches: "..storageWood:getItemCount("wood").." wood removed from storage", true)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- if numSticksOnboard == 0 or numSticksOnboard < headQuantity then
- makeSticks = true
- saveToLog("craftTorches: insufficient sticks in stock, on crafting list", true)
- else
- saveToLog("craftTorches: enough sticks in stock", true)
- end
- if makeSticks then
- woodType = getWoodAvailable{woodNeeded = 1}
- if woodType ~= "none" then
- saveToLog("craftTorches: crafting sticks")
- doContinue = false
- if craftSticks(sticksNeeded) then
- doContinue = true
- end
- end
- end
- woodType = getWoodAvailable{woodNeeded = 1}
- if woodType == "none" then
- doContinue = false
- saveToLog("craftTorches: insufficient wood available", false, true)
- end
- if doContinue then
- if makeCharcoal then
- saveToLog("craftTorches: smelting charcoal")
- if smelt("charcoal", headQuantity) then
- saveToLog("craftTorches: charcoal smelted", true)
- else
- saveToLog("craftTorches: charcoal smelting failed", true)
- doContinue = false
- end
- end
- if doContinue then
- saveToLog("craftTorches: crafting torches", true)
- --make quantity torches
- if craft{craftItem = "torches", craftQuantity = quantity, sourceItem1 = "sticks", sourceItem2 = headType, destSlot = 0, doSort = true} then
- saveToLog("craftTorches: "..quantity.." torches made", true)
- success = true
- if placeStorage:getStoragePlaced("torches") then
- if not placeStorage:getMarkerPlaced("torches") then
- if slot:getItemSlot("torches") > 0 then
- if turtle.getItemCount(slot:getItemSlot("torches")) > 0 then
- forward(1, 1)
- dig.digNew{self = dig, direction = "down", callFrom = "craftTorches"}
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- placeStorage:setMarkerPlaced("torches")
- back(1, 1)
- end
- end
- end
- end
- else
- saveToLog("craftTorches: error: no torches made", true)
- error()
- end
- end
- end
- return success
- end
- function createBedrockMine()
- local tempYcoord = location:getY()
- local torchesAt = {}
- local sTime = os.time()
- saveToLog("createBedrockMine: starting..", true)
- restoreTorches("bothsides", 1)
- --move to entrance
- forward(16, 1)
- --rewrite sign
- turtle.select(16)
- if turtle.dig() then
- turtle.place("Diamond Mine\nMining Bedrock\nSetting Up\n time: "..textutils.formatTime(os.time(), true))
- end
- turnRight(2)
- while location:getY() > 14 do
- down(1, 1)
- end
- down(7, 1) --now at level 7
- for i = 1, 33 do
- torchesAt[i] = false
- end
- -- now under N side of previous mining area dig 33 x 2 corridor
- torchesAt[1] = true
- torchesAt[9] = true
- torchesAt[17] = true
- torchesAt[25] = true
- torchesAt[33] = true
- for i = 1, 33 do
- mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
- if i < 33 then
- forward(1, 1)
- end
- end
- turnRight(2)
- forward(16, 1)
- --should be at centre now
- turnRight(1)
- for i = 1, 17 do
- mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
- if i < 17 then
- forward(1, 1)
- end
- end
- turnRight(2)
- forward(16, 1)
- for i = 1, 17 do
- mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
- if i < 17 then
- forward(1, 1)
- end
- end
- turnRight(2)
- forward(16, 1)
- turnLeft(1)
- up(7,1)
- forward(16, 1)
- --should be at start now
- turnRight(2)
- while location:getY() < tempYcoord do
- up(1, 1)
- end --at surface now
- forward(16, 1)
- turnRight(2)
- saveToLog("createBedrockMine: started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), true)
- end
- function createChestObject()
- -- chest object, used to keep track of stored items
- clsChest = {} -- the table representing the class, which will double as the metatable for any instances
- clsChest.__index = clsChest -- failed table lookups on the instances should fallback to the class table, to get methods
- function clsChest.new(storeNo, setStoreName) --equivalent to VB class initialise
- local self = setmetatable({}, clsChest)
- self.items = {"wood", "wood2","dirt","cobble","stone","saplings","saplings2","seeds","sand","gravel","clay",
- "apples","coal","charcoal","ironore","?ironore","goldore","?goldore","iron","?iron","gold","?gold",
- "redstone","redstone block","?diamonds","diamonds","emeralds","sugar cane","planks","planks2","chests","sticks",
- "torches","furnaces","signs","item1","item2","item3","item4","item5","item6","item7","item8",
- "fences","nuggets","buckets","paper","computer","crafting table","diamond pickaxe","glass","glass panes","floppy disk",
- "disk drive","turtle","crafty mining turtle","?moss stone","moss stone","walls","obsidian","sandstone",
- "?lapis","lapis","lapis block","flowers","roses","red mushrooms","brown mushrooms"}
- self.value = storeNo
- self.slotContains = {}
- self.slotCount = {}
- self.itemSlot = {}
- self.turtleSlot = {}
- self.index = 1
- self.name = setStoreName
- -- initialise variables
- for i = 1, 16 do
- self.slotContains[i] = ""
- self.slotCount[i] = 0
- self.turtleSlot[i] = 0
- end
- for key, value in ipairs(self.items) do
- self.itemSlot[value] = 0
- end
- return self
- end
- function clsChest.resetVariables(self)
- for i = 1, 16 do
- self.slotContains[i] = ""
- self.slotCount[i] = 0
- self.turtleSlot[i] = 0
- end
- for key, value in ipairs(self.items) do
- self.itemSlot[value] = 0
- end
- self.index = 1
- end
- function clsChest.getValue(self) --property get in VB
- return self.value
- end
- --craftingChest.getIndex(craftingChest)
- function clsChest.getIndex(self)
- return self.index
- end
- function clsChest.setSlotCount(self, slotNo, newVal) --property let in VB
- self.slotCount[slotNo] = newVal
- end
- function clsChest.getStoreName(self)
- return self.name
- end
- function clsChest.getSlotCount(self, slotNo)
- return self.slotCount[slotNo]
- end
- function clsChest.getSlotContains(self, slotNo)
- return self.slotContains[slotNo]
- end
- function clsChest.getStoreContains(self)
- -- return contents of first slot
- return self.slotContains[1]
- end
- function clsChest.getItemSlot(self, item)
- local result = 0
- if item ~= nil and item ~= "" then
- result = self.itemSlot[item]
- end
- return result
- end
- function clsChest.getTurtleSlot(self, slotNo )
- return self.turtleSlot[slotNo]
- end
- function clsChest.getItemCount(self, item)
- local result = 0
- if item ~= nil and item ~= "" then
- if self.itemSlot[item] > 0 then
- result = self.slotCount[self.itemSlot[item]]
- end
- end
- return result
- end
- function clsChest.getNoOfItems(self)
- return self.index - 1
- end
- function clsChest.isItemInChest(self, item)
- local success = false
- if item ~= nil then
- if self.itemSlot[item] > 0 then
- success = true
- end
- end
- return success
- end
- function clsChest.addItem(self, item, quantity, turtleSlot)
- --storageSticks:addItem("sticks", 2, 14)
- -- addItem(arg.toStore, arg.item, arg.quantity)
- local slotNo = 0
- local newItemFound = false
- local newIndex = 1
- if turtleSlot == nil then
- turtleSlot = 0
- end
- if item == nil or item == "" then
- item = "item8"
- saveToLog(" clsChest.addItem Error, unknown item found", true)
- end
- if quantity == nil then
- quantity = 0
- saveToLog(" clsChest.addItem Error, nil quantity passed", true)
- end
- --unknown item only dropped in full, not added to
- slotNo = self.itemSlot[item] -- 0 by default, unless items already in chest
- if slotNo == 0 then --none of this item in chest
- self.itemSlot[item] = self.index --default value 1
- slotNo = self.itemSlot[item]
- self.slotContains[self.index] = item
- self.slotCount[self.index] = quantity
- self.turtleSlot[self.index] = turtleSlot
- self.index = self.index + 1 --ready for next item
- else --already in chest, update quantity
- turtleSlot = self.turtleSlot[self.itemSlot[item]]
- self.slotCount[slotNo] = self.slotCount[slotNo] + quantity
- if self.slotCount[slotNo] > 64 then
- self.slotCount[slotNo + 1] = self.slotCount[slotNo] - 64
- self.slotContains[slotNo + 1] = item
- self.turtleSlot[slotNo + 1] = turtleSlot
- self.slotCount[slotNo] = 64
- end
- end
- saveToLog("addItem: "..self.name.." added "..quantity.." of "..item.." from turtle slot "..turtleSlot, false)
- end
- --create 5 chest objects, 1 used for storage while crafting, others in the ground
- craftingChest = clsChest.new(0, "craftingChest")
- storageTorches = clsChest.new(1, "storageTorches") -- placed under furnace
- storageSigns = clsChest.new(2, "storageSigns") -- placed Relative North of furnace, on path to tree farm, block 2 - placeStorage()
- storageSticks = clsChest.new(3, "storageSticks") -- placed Relative North of furnace, on path to tree farm, block 4 - placeStorage()
- storageWood = clsChest.new(4, "storageWood") -- placed Relative North of furnace, on path to tree farm, block 6 - placeStorage()
- storageIronore = clsChest.new(5, "storageIronore")
- storageSand = clsChest.new(6, "storageSand")
- storageRedstone = clsChest.new(7, "storageRedstone")
- storagePickaxes = clsChest.new(8, "storagePickaxes")
- storageSaplings = clsChest.new(9, "storageSaplings")
- extraStorage1 = clsChest.new(10, "extraStorage1")
- extraStorage2 = clsChest.new(11, "extraStorage2")
- extraStorage3 = clsChest.new(12, "extraStorage3")
- extraStorage4 = clsChest.new(13, "extraStorage4")
- extraStorage5 = clsChest.new(14, "extraStorage5")
- end
- function createCoordObject()
- --[[
- 0 = go south (z increases)
- 1 = go west (x decreases)
- 2 = go north (z decreases
- 3 = go east (x increases)
- compass[0] = "south"
- compass[1] = "west"
- compass[2] = "north"
- compass[3] = "east"]]--
- clsCoord = {} -- the table representing the class, which will double as the metatable for any instances
- clsCoord.__index = clsCoord -- failed table lookups on the instances should fallback to the class table, to get methods
- function clsCoord.new(coordName) --equivalent to VB class initialise
- local self = setmetatable({}, clsCoord)
- self.name = coordName
- self.x = 0
- self.y = 0
- self.z = 0
- self.facing = 0
- self.compass = "south"
- return self
- end
- function clsCoord.getX(self)
- return self.x
- end
- function clsCoord.setX(self, newVal) -- property let in VB
- self.x = newVal
- end
- function clsCoord.getY(self) -- property get in VB
- return self.y
- end
- function clsCoord.setY(self, newVal)
- self.y = newVal
- end
- function clsCoord.getZ(self)
- return self.z
- end
- function clsCoord.setZ(self, newVal)
- self.z = newVal
- end
- function clsCoord.getFacing(self)
- return self.facing
- end
- function clsCoord.setFacing(self, newVal) --property let in VB
- self.facing = newVal
- if self.facing < 0 then
- self.facing = 3
- elseif self.facing > 3 then
- self.facing = 0
- end
- if self.facing == 0 then
- self.compass = "south"
- elseif self.facing == 1 then
- self.compass = "west"
- elseif self.facing == 2 then
- self.compass = "north"
- else
- self.compass = "east"
- end
- end
- function clsCoord.getCompass(self)
- return self.compass
- end
- function clsCoord.setCompass(self, newVal) --property let in VB
- self.compass = newVal
- if self.compass == "south" then
- self.facing = 0
- elseif self.compass == "west" then
- self.facing = 1
- elseif self.compass == "north" then
- self.facing = 2
- elseif self.compass == "east" then
- self.facing = 3
- end
- end
- function clsCoord.goUp(blocks)
- blocks = blocks or 1
- self.y = self.y + blocks
- end
- function clsCoord.goDown(blocks)
- blocks = blocks or 1
- self.y = self.y - blocks
- end
- --[[uses:
- location:getX() get current turtle x coordinate
- location:getY() get current turtle y coordinate
- location:getZ() get current turtle z coordinate
- location:setX(xCoord) set current turtle x coordinate eg location:setX(-235)
- location:setY(yCoord) set current turtle y coordinate eg location:setY(66)
- location:setZ(zCoord) set current turtle z coordinate eg location:setZ(125)
- location:getFacing() returns a number 0 - 3 representing direction of player
- location:setFacing(facing) sets direction eg location:setFacing(1) (West)
- location:getCompass() returns direction as text eg "north"
- location:setCompass("X") sets direction using text eg location:setCompass("south")
- location:goUp(X) increases Y coord by X
- location:goDown(X) decreases Y coord by X
- ]]--
- location = clsCoord.new("currentLocation")
- coordHome = clsCoord.new("homeLocation")
- mineEntrance = clsCoord.new("mineEntrance")
- end
- function createDigObject()
- -- dig{suck = true} use suck instead of dig
- -- success, item, slotNo = dig.digNew{self = dig, direction = "forward", slotNo = 1, expectedItem = "wood", waitForGravel = true, checkForItems = "saplings", callFrom = "firstTree"}
- -- dig item in specified direction
- -- if item not known allocate "item1-8"
- -- if checkForItems ~= "" then check eg for saplings, coal etc
- -- if item known, leave in selected slot
- -- if max quantity reached, dump excess
- -- return success, dugItem, dugSlot eg true, "wood", 1
- clsDig = {}
- clsDig.__index = clsDig
- function clsDig.create(self) --creates new instance of dig object
- local self = setmetatable({}, clsDig)
- -- setup variables
- self.callFrom = ""
- self.checkForItems = ""
- self.cobblePlaced = false
- self.detect = false
- self.direction = ""
- self.doSort = false
- self.dugAmount = 0
- self.dugItem = ""
- self.dugSlot = 0
- self.dugSlot2 = 0
- self.emptySlot = 0
- self.expectedItem = ""
- self.flintSlot = 0
- self.isGravel = false
- self.itemKnown = false
- self.itemCount = {}
- self.newItemFound = false
- self.oldItem = ""
- self.slotNo = 1
- self.success = false
- self.suck = false
- self.updateType =""
- self.useItem = ""
- self.useSlot = 0
- self.waitForGravel = false
- self.itemCount = {}
- -- set up array of current quantities in each turtle slot
- for i = 1, 16 do
- self.itemCount[i] = turtle.getItemCount(i)
- end
- return self
- end
- function clsDig.digNew(arg)
- -- reset variables
- local self = arg.self
- self.callFrom = ""
- self.checkForItems = ""
- self.cobblePlaced = false
- self.detect = false
- self.direction = "forward"
- self.doSort = false
- self.dugAmount = 0
- self.dugItem = ""
- self.dugSlot = 0
- self.dugSlot2 = 0
- self.emptySlot = 0
- self.expectedItem = ""
- self.flintSlot = 0
- self.isGravel = false
- self.itemCount = {}
- self.itemKnown = false
- self.newItemFound = false
- self.oldItem = ""
- self.slotNo = 1
- self.success = false
- self.suck = false
- self.updateType = ""
- self.useItem = ""
- self.useSlot = 0
- self.waitForGravel = false
- local logText = "dig.newDig: "
- local result = false
- --[[examples
- dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
- dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
- dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "firstTree"}
- ]]--
- -- set up array of current quantities in each turtle slot
- for i = 1, 16 do
- self.itemCount[i] = turtle.getItemCount(i)
- end
- self.direction = arg.direction or "forward"
- self.slotNo = arg.slotNo or 1
- self.expectedItem = arg.expectedItem or ""
- self.checkForItems = arg.checkForItems or ""
- self.callFrom = arg.callFrom or ""
- self.waitForGravel = arg.waitForGravel or false
- self.suck = arg.suck or false
- if self.suck then --turtle is sucking
- clsDig.suck(self) -- will set self.success
- else -- turtle is digging, not sucking, so check for mobs
- --remove any mobs
- clsDig.attack(self)
- --mobs disposed of, now get on with digging
- clsDig.doDig(self) -- will set self.success
- end
- --dig/suck successful if suck large quantity then 2 slots will be updated, original + next available
- if self.success then --process dig/suck
- saveToLog("dig.newDig: direction = "..self.direction..
- " slotNo = "..self.slotNo..
- " expectedItem = "..self.expectedItem..
- " checkForItems = "..self.checkForItems..
- " callFrom = "..self.callFrom, false, true)
- clsDig.setDugSlotAndItem(self) --sets self.dugSlot, self.dugSlot2, self.dugItem and self.dugAmount. itemX unless already identified
- if self.dugSlot > 0 then --item dug, could be into existing slot, empty slot, or specified slot
- if clsDig.confirmGravel(self) == "flint" then -- item identified by confirmGravel only
- turtle.select(self.dugSlot)
- while turtle.drop() do
- saveToLog("dig.digNew: flint in slot "..self.dugSlot.. " emptied out", false, true)
- end
- self.dugItem = ""
- self.dugSlot = 0
- end
- if self.dugSlot > 0 then -- not flint. self.dugItem = itemX, or known item. expectedItem already set
- -- self.updateType set in setDugSlotAndItem() and/or in checkForItem()
- if string.find(self.dugItem, "item") ~= nil and self.checkForItems ~= "" then --check for item(s) when only 1 block available
- clsDig.checkForItem(self) -- itemX changed to known item after checkForItem
- end
- -- item will be itemX or known item by now, slot.update needed
- if self.updateType == "newItem" then
- saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
- slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
- elseif self.updateType == "updateItem" then
- saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
- slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
- elseif self.updateType == "changeItem" then
- saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." old item = "..self.oldItem.." tested - dugItem = "..self.dugItem, false, true)
- slot.update{self = slot, slotNo = self.dugSlot, newItem = self.dugItem}
- elseif self.updateType == "deleteItem" then
- saveToLog("dig.newDig: calling slot.update dugSlot = "..self.dugSlot.." delete item = "..self.dugItem, false, true)
- slot.update{self = slot, slotNo = self.dugSlot, delete = true}
- end
- if clsDig.checkSlotQuantity(self) then
- slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
- end
- if self.waitForGravel then
- clsDig.waitForGravel(self)
- end
- end
- if self.dugSlot > 0 then
- result = true
- end
- end
- else
- if self.detect then --dig not successful, if detected must be bedrock
- saveToLog("dig.newDig: did not succeed after detect(), bedrock reached", true)
- self.dugItem = "bedrock"
- self.dugSlot = 0
- else
- --saveToLog("dig.newDig: no block detected, nothing dug", false, true)
- end
- result = false
- end
- if self.doSort then
- saveToLog("dig.newDig: sorting inventory after identifying new item", true)
- sortInventory(true)
- end
- --if not fso:getUseVerbose() then --not using verbose logfile, so print summary only
- if self.dugItem ~= "" then
- saveToLog("dig = "..self.dugAmount.." of "..self.dugItem.." into slot "..self.dugSlot.." at x="..location:getX()..",y="..location:getY()..",z="..location:getZ(), true, false)
- end
- --end
- return result, self.dugItem, self.dugSlot
- end
- function clsDig.attack(self) --attack any nearby mobs
- local mobAttacked = false
- --first get rid of any mobs
- --saveToLog("dig.attack: attack started", false, true)
- turtle.select(1)
- if turtle.attack() then
- saveToLog("dig.attack: Mob attacked in front!", true, false)
- sleep(1.5)
- while turtle.attack() do --in case mob in front
- saveToLog("dig.attack: Mob attacked in front again!", true, false)
- sleep(1.5)
- end
- mobAttacked = true
- end
- if turtle.attackUp() then
- saveToLog("dig.attack: Mob attacked above!", true, false)
- sleep(1.5)
- while turtle.attackUp() do --in case mob in front
- saveToLog("dig.attack: Mob attacked above again!", true, false)
- sleep(1.5)
- end
- mobAttacked = true
- end
- if turtle.attackDown() then
- saveToLog("dig.attack: Mob attacked below!", true, false)
- sleep(1.5)
- while turtle.attackDown() do --in case mob in front
- saveToLog("dig.attack: Mob attacked below again!", true, false)
- sleep(1.5)
- end
- mobAttacked = true
- end
- if mobAttacked then --remove any mob drops
- for i = 1, 16 do
- if self.itemCount[i] == 0 then --check only previously empty slots
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- while turtle.dropUp() do
- saveToLog ("dig.attack: dumping mob drops", true, false)
- end
- end
- end
- end
- end
- end
- function clsDig.suck(self) -- carry out suck operation
- self.success = false
- turtle.select(self.slotNo)
- if self.direction == "up" then
- if turtle.suckUp() then
- self.success = true
- end
- elseif self.direction == "down" then
- if turtle.suckDown() then
- self.success = true
- end
- else
- if turtle.suck() then
- self.success = true
- end
- end
- if self.success then
- saveToLog("dig.suck: suck items into slot "..self.slotNo, false, true)
- end
- return self.success
- end
- function clsDig.doDig(self) -- check if next block is gravel, then carry out dig operation
- local lookForGravel = false
- local testDig = false
- self.success = false
- --[[examples
- dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
- dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
- ]]--
- --[[
- saveToLog("dig.doDig: started, direction = "..self.direction..
- " expectedItem = "..self.expectedItem..
- " slotNo = "..self.slotNo, false, true)]]--
- turtle.select(self.slotNo)
- -- grass: turtle.detect() = false, turtle.dig() = true, nothing returned, occasional seeds
- -- leaves: turtle.detect() = true, turtle.dig() = true, nothing returned
- -- water: turtle.detect() = false, turtle.dig() = false, nothing returned cc >= 1.6
- -- lava: turtle.detect() = false, turtle.dig() = false, nothing returned cc >= 1.6
- --Compare next block with gravel (if already found)
- if slot:getItemSlot("gravel") > 0 then
- turtle.select(slot:getItemSlot("gravel"))
- lookForGravel = true
- end
- if self.direction == "up" then
- if turtle.detectUp() then
- self.detect = true
- if turtle.compareUp() and lookForGravel then --compare gravel with block above
- self.isGravel = true
- end
- end
- turtle.select(self.slotNo)
- if turtle.digUp() then
- self.success = true
- end
- elseif self.direction == "down" then
- if turtle.detectDown() then
- self.detect = true
- if turtle.compareDown() and lookForGravel then
- self.isGravel = true
- end
- end
- turtle.select(self.slotNo)
- if turtle.digDown() then
- self.success = true
- end
- else
- if turtle.detect() then
- self.detect = true
- if turtle.compare() and lookForGravel then
- self.isGravel = true
- end
- end
- turtle.select(self.slotNo)
- if turtle.dig() then
- self.success = true
- end
- end
- if not self.detect then
- --saveToLog("dig.doDig: no block detected "..self.direction, false, true)
- end
- if self.isGravel then
- saveToLog("dig.doDig: gravel found on current dig, will give flint or gravel", false, true)
- end
- return self.success --turtle.digX() succeeded but may not have increased items onboard.
- --if isGravel then new block is either gravel or flint. success = false: bedrock
- end
- function clsDig.setDugSlotAndItem(self) -- set self.dugSlot (0 = no dig) self.dugItem ("" = no dig)
- local success = false
- self.dugSlot = 0
- self.dugItem = ""
- self.dugAmount = 0
- self.dugSlot2 = 0
- self.updateType = ""
- for i = 1, 16 do --find slot item dug into (if any) by comparing array set up in digNew()
- if turtle.getItemCount(i) > self.itemCount[i] then --check if dugItem in slot(s)
- if self.dugSlot == 0 then -- default value
- self.dugSlot = i --first slot with additional items in it
- self.dugAmount = turtle.getItemCount(i) - self.itemCount[i] -- amount dug
- else
- self.dugSlot2 = i --another slot with additional items
- end
- end
- end
- --dugSlot set above
- if self.dugSlot > 0 then--check if additional items
- success = true
- if self.suck then -- suck, not dig
- -- set dugSlot2 to slot number with added item in it
- if self.dugSlot2 > 0 then --eg suck items has filled original slot, then moved into next available, or overfilled slot
- saveToLog("dig.setDugSlotAndItem: second slot used "..self.dugSlot2, false, true)
- turtle.select(self.dugSlot2)
- for i = 1, 16 do
- if i ~= self.dugSlot2 then
- if turtle.compareTo(i) then --compare if excess material
- while turtle.dropDown() do
- saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " emptied out", true)
- end
- if turtle.getItemCount(i) >= 54 then
- saveToLog ("dig.setDugSlotAndItem: excess items dumped from "..i, true)
- turtle.select(i)
- turtle.dropDown(turtle.getItemCount(i) - 54)
- end
- self.dugSlot2 = 0
- break
- end
- end
- end
- end
- if slot:getSlotContains(self.dugSlot) == "" then
- turtle.select(self.dugSlot)
- if turtle.refuel(0) then
- if slot:getItemSlot("saplings") == 0 then
- self.dugItem = "saplings"
- self.updateType = "newItem"
- else
- if slot:getItemSlot("saplings2") == 0 then
- self.dugItem = "saplings2"
- self.updateType = "newItem"
- else
- turtle.refuel()
- self.dugSlot = 0
- self.dugItem = ""
- success = false
- end
- end
- else
- while turtle.dropDown() do
- saveToLog("dig.setDugSlotAndItem: suck unknown item into slot "..self.dugSlot.. " emptied out", true)
- end
- self.dugSlot = 0
- success = false
- end
- else --item dug already identified or given itemX
- self.dugItem = slot:getSlotContains(self.dugSlot)
- self.itemKnown = true
- self.updateType = "updateItem"
- end
- else -- dig, not suck
- if self.slotNo > 1 or self.expectedItem ~= "" then --dug into specific slot, so could match existing item, or placed and re-dug
- -- eg dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
- -- check if out of place
- turtle.select(self.dugSlot)
- for i = 1, 16 do
- if i ~= self.dugSlot then
- if turtle.compareTo(i) then --matches existing item
- self.dugItem = slot:getSlotContains(i)
- turtle.transferTo(i)
- self.dugSlot = i
- self.itemKnown = true
- self.updateType = "updateItem"
- break
- end
- end
- end
- if self.updateType ~= "updateItem" then -- not out of place
- if self.expectedItem ~= "" then --allocate dugItem to expectedItem
- self.dugItem = self.expectedItem
- saveToLog("dig.checkExpectedItem: expected item '"..self.expectedItem.."' dug into slot "..self.dugSlot, false, true)
- self.itemKnown = true
- self.updateType = "updateItem"
- else -- ? placed and re-dug
- if slot:getSlotContains(self.slotNo) == "" then
- self.itemKnown = false
- self.updateType = "newItem"
- self.dugItem = slot:getFreeUnknownItem()
- else
- self.itemKnown = true
- self.updateType = "updateItem"
- end
- end
- end
- else -- set to dig into slot 1 and self.expectedItem = ""
- if self.dugAmount > 2 then
- --can only be melon, clay, lapis, redstone as more than 1 mined from one block
- if location:getY() > 35 then --must be clay or melon
- turtle.select(self.dugSlot)
- while turtle.dropUp() do
- saveToLog("dig.setDugSlotAndItem:: clay or melon dug: dumping..", false, true)
- end
- self.dugItem = ""
- self.dugSlot = 0
- success = false
- else
- if slot:getSlotContains(self.dugSlot) == "" then
- if slot:getItemSlot("redstone") > 0 then --redstone already found
- self.dugItem = "lapis"
- success = true
- self.itemKnown = true
- self.updateType = "newItem"
- saveToLog("dig.setDugSlotAndItem: lapis identified as redstone already known", false, true)
- else --no redstone found so far
- self.dugItem = slot:getFreeUnknownItem()
- self.itemKnown = false
- self.updateType = "newItem"
- end
- else
- self.dugItem = slot:getSlotContains(self.dugSlot)
- newItemFound = false
- self.updateType = "updateItem"
- self.itemKnown = true
- end
- end
- else -- dugAmount = 1
- if self.dugSlot2 > 0 then -- dug items has filled original slot, then moved into next available, or redstone, lapis, coal overfilled slot
- saveToLog("dig.setDugSlotAndItem: second slot used "..self.dugSlot2, false, true)
- turtle.select(self.dugSlot2)
- for i = 1, 16 do
- if i ~= self.dugSlot2 then
- if turtle.compareTo(i) then --compare if excess material
- while turtle.drop() do
- saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " emptied out", true)
- end
- if turtle.getItemCount(i) >= 54 then
- saveToLog ("dig.setDugSlotAndItem: excess items dumped from "..i, true)
- turtle.select(i)
- turtle.drop(turtle.getItemCount(i) - 54)
- end
- self.dugSlot2 = 0
- break
- end
- end
- end
- end
- if slot:getSlotContains(self.dugSlot) == "" then
- self.dugItem = slot:getFreeUnknownItem()
- --saveToLog("dig.setDugSlotAndItem: new dugItem = "..self.dugItem, false)
- self.itemKnown = false
- self.updateType = "newItem"
- else
- self.dugItem = slot:getSlotContains(self.dugSlot)
- --saveToLog("dig.setDugSlotAndItem: existing dugItem = "..self.dugItem, false)
- newItemFound = false
- self.updateType = "updateItem"
- if string.find(self.dugItem, "item") ~= nil then -- itemX found
- self.itemKnown = false
- else
- self.itemKnown = true
- end
- end
- success = true -- item dug
- end
- end
- end
- else
- self.dugItem = "leaves"
- end
- saveToLog ("dig.setDugSlotAndItem: dugSlot = "..self.dugSlot.." dugItem = "..self.dugItem, false, true)
- return success
- -- self.dugSlot, self.dugSlot2, self.dugAmount and self.dugItem have been set in this routine
- -- if self.dugSlot = 0, self.dugItem will only be set if = "water" or "leaves"
- -- self.updateType will be newItem or updateItem
- end
- function clsDig.confirmGravel(self) -- self.isGravel = true, either flint or gravel dug
- local item = ""
- if self.isGravel then --block just dug confirmed as gravel by comparison
- if self.dugSlot ~= slot:getItemSlot("gravel") then --must be flint
- saveToLog("dig.confirmGravel: flint dug when gravel detected", false, true)
- self.dugItem = "flint"
- item = "flint"
- else
- self.dugItem = "gravel"
- self.itemKnown = true
- item = "gravel"
- end
- end
- return item
- end
- function clsDig.checkSandGravel(self, useSlot, useItem)
- local result = ""
- local runTest = true
- local flintSlot = 0
- local dirtPlaced = false
- local numTries = 0
- local startLevel = location:getY()
- local itemCount = {}
- if turtle.detectDown() then --probably in mine going down. block found below
- turtle.select(1)
- for i = 1, 2 do --move up 2 places
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.5)
- end
- up(1)
- end
- else --no block below. probably going up. plug with dirt
- saveToLog("clsDig.checkSandGravel: checking for gravel placing dirt plug down", false)
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- dirtPlaced = true
- turtle.select(1)
- for i = 1, 2 do --move up 2 places
- while turtle.detectUp() do
- saveToLog("clsDig.checkSandGravel: digging block above", false)
- turtle.digUp()
- sleep(0.5)
- end
- up(1)
- end
- end --now clear below 2 blocks, could have added more flint/gravel to turtle while clearing above
- turtle.select(useSlot)
- if turtle.placeDown() then --solid block or sand/ gravel
- sleep(0.5)
- if turtle.detectDown() then --not dropped into pit so not sand/gravel
- turtle.digDown()
- --down(2, 1)
- if slot:getItemSlot("cobble") == 0 then
- saveToLog("clsDig.checkSandGravel: first cobble found", false)
- result = "cobble"
- else
- result = useItem
- end
- else
- saveToLog("clsDig.checkSandGravel: gravel or sand dropped, moving down", false)
- while not turtle.detectDown() do
- down(1)
- end
- if slot:getItemSlot("gravel") > 0 then --gravel found already
- saveToLog("checkSandGravel: checking for gravel/sand - found sand in slot "..useSlot, false)
- result = "sand"
- runTest = false
- turtle.digDown()
- elseif slot:getItemSlot("sand") > 0 then --sand found already
- saveToLog("checkSandGravel: checking for gravel/sand - found gravel in slot "..useSlot, false)
- result = "gravel"
- end
- if runTest then -- only test to confirm gravel
- if slot:getItemSlot("sand") > 0 then --sand found already
- saveToLog("clsDig.checkSandGravel: sand already identified. Gravel dropped, digging up to 100x to check for flint", false)
- else
- saveToLog("clsDig.checkSandGravel: sand / gravel dropped, digging up to 100x to check for flint", false)
- end
- for i = 1, 16 do
- itemCount[i] = turtle.getItemCount(i)
- end
- for i = 1, 100 do
- turtle.select(1) --if gravel or flint dug, goes into correct slot
- if turtle.digDown() then
- numTries = numTries + 1
- if turtle.getItemCount(useSlot) == itemCount[useSlot] then --flint found as ? gravel slot did not increase
- for k = 1, 16 do
- if turtle.getItemCount(k) > itemCount[k] then --check if dugItem in slot(s)
- flintSlot = k -- could be flint in an empty slot or add to existing item
- break
- end
- end
- saveToLog("clsDig.checkSandGravel: useSlot not used flint found in slot "..flintSlot, false)
- --delete flint after updating gravel slot, else itemX order will change
- if flintSlot > 0 then
- result = "gravel"
- break
- end
- else
- turtle.select(useSlot)
- turtle.placeDown()
- end
- end
- end
- if flintSlot == 0 then --sand found
- saveToLog("clsDig.checkSandGravel: checking for gravel - sand found in slot "..useSlot, false)
- result = "sand"
- else
- saveToLog("clsDig.checkSandGravel: gravel check took "..numTries.." attempts", false)
- end
- end
- down(1, 1)
- if flintSlot > 0 then --flint found, so dump it
- turtle.select(flintSlot)
- while turtle.dropUp() do
- saveToLog("clsDig.checkSandGravel: dumping flint found in slot "..flintSlot, false)
- end
- slot.update{self = slot, slotNo = flintSlot, delete = true}
- end
- end
- else --did not placeDown() = coal, flint
- turtle.select(useSlot)
- if turtle.refuel(0) then --coal found
- saveToLog("clsDig.checkSandGravel: coal found in slot "..useSlot, false)
- result = "coal"
- end
- down(2, 1)
- end
- if dirtPlaced then
- turtle.select(1)
- turtle.digDown()
- end
- while location:getY() < startLevel do
- up(1)
- end
- return result
- end
- function clsDig.checkForItem(self)
- local useItem = ""
- local slotCount = 0
- local itemCompare = false
- local useSlot = 0
- local itemStatus = ""
- local result = false
- local flintSlot = 0
- saveToLog("dig.checkForItem: checkForItems = "..self.checkForItems.." call from = "..self.callFrom, false)
- -- will only get here if item dug but not identified self.dugItem = "itemX"
- -- see if any instructions to test for new items, when first identified as itemX
- if self.dugAmount > 2 then --multiple items from dig
- --can only be melon, clay, lapis, redstone as more than 1 mined from one block
- if location:getY() > 35 then --must be clay or melon
- turtle.select(self.dugSlot)
- while turtle.dropUp() do
- saveToLog("dig.checkForItem: clay or melon dug: dumping..", false, true)
- end
- self.dugItem = ""
- self.dugSlot = 0
- itemStatus = "deleteItem"
- else --below level 35 must be lapis or redstone
- if slot:getItemSlot("redstone") > 0 then
- self.dugItem = "lapis"
- itemStatus = "newItem"
- saveToLog("dig.checkForItem: lapis identified as redstone already known", false, true)
- else
- if turtle.detectDown() then
- back(1)
- turtle.select(self.dugSlot)
- if turtle.place() then --redstone
- if not turtle.compare() then
- saveToLog("dig.checkForItem: dugAmount = "..self.dugAmount.." item in slot "..self.dugSlot.." placed but not compared - redstone", false)
- self.dugItem = "redstone"
- turtle.dig()
- itemStatus = "newItem"
- end
- else --lapis (dug > 1, no place)
- self.dugItem = "lapis"
- itemStatus = "newItem"
- end
- end
- end
- end
- else -- single item from dig
- -- check for coal, flint, saplings, saplings2, wood2, flowers, mushrooms if only one item in slot
- if turtle.getItemCount(self.dugSlot) == 1 then --first time itemX dug
- turtle.select(self.dugSlot)
- if turtle.refuel(0) then --saplings, saplings2, wood2, coal
- itemStatus = "newItem"
- saveToLog("dig.checkForItem: new item dug - can refuel", false, true)
- if string.find("checkBedrock,findCobble,goMining1,goMining2,goMining3,mineCorridor,mineForward", self.callFrom) ~= nil then
- if slot:getItemSlot("coal") == 0 then
- self.dugItem = "coal"
- saveToLog("dig.checkForItem: coal found", false, true)
- else -- if coal already known must be wood product found in a disused mine
- saveToLog("dig.checkForItem: wooden item found underground - using for fuel", false, true)
- turtle.refuel() -- use for fuel
- self.dugItem = ""
- self.dugSlot = 0
- itemStatus = "deleteItem"
- end
- else
- if slot:getItemSlot("wood2") == 0 then -- wood2 not known
- if turtle.detectUp() then
- turtle.select(1)
- turtle.digUp()
- turtle.select(self.dugSlot)
- end
- if turtle.placeUp() then --wood2
- turtle.digUp()
- self.dugItem = "wood2"
- saveToLog("dig.checkForItem: wood2 found", false)
- else
- if slot:getItemSlot("saplings") > 0 then
- self.dugItem = "saplings2"
- saveToLog("dig.checkForItem: saplings2 found", false)
- else
- self.dugItem = "saplings"
- saveToLog("dig.checkForItem: saplings found", false)
- end
- end
- else --wood2 known, must be sapling
- if slot:getItemSlot("saplings") > 0 then
- self.dugItem = "saplings2"
- saveToLog("dig.checkForItem: saplings2 found", false)
- else
- self.dugItem = "saplings"
- saveToLog("dig.checkForItem: saplings found", false)
- end
- end
- end
- else --did not refuel, check for flint, flowers, seeds, mushrooms, sugar cane, diamonds
- saveToLog("dig.checkForItem: new item - cannot refuel", false, true)
- useItem = self.dugItem
- if self.callFrom == "reposition" then
- if turtle.placeUp() then -- not dirt, cobble, could be ironore, sand, gravel
- turtle.digUp() --put back into dugSlot. If gravel dug, could turn to flint, leave for time being
- else
- while turtle.dropUp() do
- saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
- end
- self.dugSlot = 0
- self.dugItem = ""
- itemStatus = "deleteItem"
- end
- elseif self.callFrom == "checkSugarCane" then
- --will only be if digDown() called from checkSugarCane()
- --has to be sugar cane, sand, gravel, flowers, mushrooms
- saveToLog("dig.checkForItem: checking for sugar cane", false)
- if not turtle.detectDown() then --block below to check
- down(1)
- end
- turtle.select(self.dugSlot)
- if not turtle.compareDown() then -- does not compare, could be sugar cane, or flower, grass below
- saveToLog ("dig.checkForItem: dugItem does not compare with block below", false)
- for i = 1, 16 do --reset item checker
- self.itemCount[i] = turtle.getItemCount(i)
- end
- turtle.select(1)
- turtle.digDown() --if sugar cane will add to slot,
- if turtle.getItemCount(self.dugSlot) > self.itemCount[self.dugSlot] then --confirms sugar cane, as did not compare,dirt and cobble known
- saveToLog ("dig.checkForItem: sugar cane confirmed", false)
- self.dugItem = "sugar cane"
- itemStatus = "newItem"
- down(1)
- else --find out whether block just dug was existing item, or new
- self.dugSlot2 = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) > self.itemCount[i] then
- self.dugSlot2 = i
- break
- end
- end
- if self.dugSlot2 > 0 then
- if slot:getSlotContains(self.dugSlot2) == "" then -- second new item dug
- saveToLog ("clsDig.checkForItem: checking for sugar cane, new item found", false)
- else --dirt/cobble/sand/gravel
- --saveToLog ("clsDig.checkForItem: checking for sugar cane, "..slot:getSlotContains(self.dugSlot2).." found and replaced", false)
- --turtle.select(self.dugSlot2)
- --turtle.placeDown()
- saveToLog ("clsDig.checkForItem: checking for sugar cane, flowers, seeds or mushrooms found and dropped", false)
- turtle.select(self.dugSlot)
- turtle.dropUp()
- self.dugItem = slot:getSlotContains(self.dugSlot2)
- self.dugSlot = self.dugSlot2
- itemStatus = "updateItem"
- end
- end
- end
- end
- elseif string.find("checkBedrock,findCobble,goMining1,goMining2,goMining3,mineCorridor,mineForward", self.callFrom) ~= nil then --look for diamonds
- saveToLog("dig.checkForItem: new item cannot be identified in slot "..self.dugSlot, false)
- itemStatus = "updateItem"
- else --called from any other function
- --test if can be placed and re-dug. saplings already tested
- turtle.select(1)
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.5)
- end
- turtle.select(self.dugSlot)
- if turtle.placeUp() then -- not dirt, cobble, could be ironore, sand, gravel
- turtle.digUp() --put back into dugSlot. If gravel dug, could turn to flint, leave for time being
- else
- while turtle.dropUp() do
- saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
- end
- self.dugSlot = 0
- self.dugItem = ""
- itemStatus = "deleteItem"
- end
- end
- end
- elseif turtle.getItemCount(self.dugSlot) == 2 then -- 2 in slot
- saveToLog("dig.checkDugItem: checking slot with 2 items of "..self.dugItem, false, true)
- if string.find(self.dugItem, "item") ~= nil then --only checks item1, item2 etc. coal, gravel etc if already found do not get here
- if self.dugAmount > 2 and slot:getItemSlot("redstone") > 0 and slot:getItemSlot("coal") > 0 then
- self.oldItem = self.dugItem
- self.dugItem = "lapis"
- itemStatus = "newItem"
- else
- --check for sand, gravel, needs min 2 blocks
- turtle.select(self.dugSlot)
- if self.callFrom == "findCobble" then --cobble not yet found, coal and flint tested, could be cobble, sand gravel
- result = clsDig.checkSandGravel(self, self.dugSlot, self.dugItem)
- self.oldItem = self.dugItem
- self.dugItem = result
- itemStatus = "newItem"
- else
- if not slot:getItemTested(self.dugSlot, "gravel") then -- check if new item is gravel, sand, coal or flint
- if string.find(self.checkForItems, "gravel") ~= nil or string.find(self.checkForItems, "sand") ~= nil then
- result = clsDig.checkSandGravel(self, self.dugSlot, self.dugItem)
- if result == "sand" or result == "gravel" then
- self.oldItem = self.dugItem
- self.dugItem = result
- itemStatus = "newItem"
- elseif result == "flint" then
- while turtle.dropUp() do
- saveToLog("dig.checkDugItem: dropping ? flint from slot "..self.dugSlot, false, true)
- end
- slot.update{self = slot, slotNo = self.dugSlot, delete = true}
- self.dugItem = ""
- self.dugSlot = 0
- elseif result == self.dugItem then --not gravel or sand
- slot:setItemTested(self.dugSlot, "gravel")
- slot:setItemTested(self.dugSlot, "sand")
- slot:setItemTested(self.dugSlot, "flint")
- else --could be ironore
- if slot:getItemSlot("ironore") == 0 and string.find("goMining1,goMining2,goMining3", self.callFrom) ~= nil then
- saveToLog("dig.checkDugItem: assuming ?ironore found",true)
- self.oldItem = self.dugItem
- self.dugItem = "?ironore"
- self.doSort = true
- itemStatus = "newItem"
- end
- end
- end
- end
- end
- end
- end
- if self.dugItem ~= "" then --tested items may have been dumped. will now be itemX or named item
- if self.oldItem == "" then --new item found
- saveToLog("dig.checkDugItem: final confirmation of new item found", false, true)
- --slot.update{self = slot, slotNo = self.dugSlot, item = self.dugItem}
- else --itemX tested - new item confirmed
- saveToLog("dig.checkDugItem: changing item in "..self.dugSlot.." ("..self.oldItem..") after testing: dugItem = "..self.dugItem, false, true)
- itemStatus = "changeItem"
- self.doSort = true
- end
- end
- end
- end
- return itemStatus
- end
- function clsDig.checkSlotQuantity(self)
- local success = false
- --Check quantity and dump excess
- --redstone drops 4-5, lapis 4-8
- if self.dugSlot > 0 then
- if string.find(self.dugItem, "item") ~= nil or self.dugItem == "redstone" or self.dugItem == "lapis" then --unknown, lapis, redstone
- if turtle.getItemCount(self.dugSlot) >= 54 then
- saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." dumped from "..self.dugSlot, false, true)
- turtle.select(self.dugSlot)
- turtle.dropDown(turtle.getItemCount(self.dugSlot) - 54)
- success = true
- end
- elseif self.dugItem == "wood" or self.dugItem == "wood2" then
- if turtle.getItemCount(self.dugSlot) >= 60 then
- saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." used for fuel", false, true)
- turtle.select(self.dugSlot)
- turtle.refuel(1)
- success = true
- end
- else --all other known items
- if turtle.getItemCount(self.dugSlot) >= 62 then
- saveToLog ("dig.checkSlotQuantity: excess "..self.dugItem.." dumped from "..self.dugSlot, false, true)
- turtle.select(self.dugSlot)
- turtle.dropDown(1)
- success = true
- end
- if self.dugItem == "sand" and turtle.getItemCount(self.dugSlot) >= 7 then
- objectives["complete sand"] = true
- saveToLog("dig.checkSlotQuantity: objectives['complete sand'] = true", false, true)
- if turtle.getItemCount(self.dugSlot) > 62 then
- turtle.select(self.dugSlot)
- turtle.dropDown(turtle.getItemCount(self.dugSlot) - 54)
- success = true
- end
- end
- if self.dugItem == "sugar cane" and turtle.getItemCount(self.dugSlot) >= 4 then
- objectives["complete sugar cane"] = true
- saveToLog("dig.checkSlotQuantity: objectives['complete sugar cane'] = true", false, true)
- end
- end
- end
- return success
- end
- function clsDig.waitForGravel(self)
- local useItem = ""
- local useSlot = 0
- for i = 1, 16 do
- self.itemCount[i] = turtle.getItemCount(i)
- end
- turtle.select(1)
- --sleep(0.5)
- if self.direction == "up" then
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.5)
- end
- elseif self.direction == "forward" then
- while turtle.detect() do
- turtle.dig()
- sleep(0.5)
- end
- end
- --now check if gravel/sand has fallen
- for i = 1, 16 do
- if turtle.getItemCount(i) > self.itemCount[i] then --check if dugItem in slot
- useSlot = i
- break
- end
- end
- if useSlot > 0 then --sand/gravel fallen
- useItem = slot:getSlotContains(useSlot) --could be "", "itemX", "sand", "gravel"
- if string.find(useItem, "item") ~= nil or useItem == "" then --unknown must be sand or gravel
- if self.itemCount[useSlot] > 1 then -- can be tested for sand/gravel
- useItem = clsDig.checkSandGravel(self, useSlot, useItem)
- if useItem ~= "" then
- saveToLog("dig.waitForGravel: "..useItem.." found in slot "..useSlot, false, true)
- end
- else -- store as "itemx"
- useItem = slot:getFreeUnknownItem()
- saveToLog ("dig.waitForGravel: sand/gravel found. given id = "..useItem.." in slot no = "..self.useSlot, false, true)
- slot.update{self = slot, slotNo = self.useSlot, item = useItem}
- end
- end
- end
- end
- function clsDig.getDugSlot(self)
- return self.dugSlot
- end
- function clsDig.getDugItem(self)
- return self.dugItem
- end
- function clsDig.getDugAmount(self)
- return self.dugAmount
- end
- function clsDig.getSuccess(self)
- return self.success
- end
- --create initial dig object
- dig = clsDig.create()
- -- use in future:
- -- dig.digNew{self = dig, slotNo = 1, direction = "down", checkForItems = "sand", callFrom = "checkSugarCane"}
- end
- function createFurnaceObject()
- clsFurnace = {} -- the table representing the class, which will double as the metatable for any instances
- clsFurnace.__index = clsFurnace -- failed table lookups on the instances should fallback to the class table, to get methods
- function clsFurnace.new(furnaceName) --equivalent to VB class initialise
- local self = setmetatable({}, clsFurnace)
- self.name = furnaceName
- self.smeltIn = ""
- self.smeltItemCount = 0
- self.smeltOut = ""
- self.smeltOutItemCount = 0
- self.fuelType = ""
- self.fuelCount = 0
- self.smeltSuccess = false
- return self
- end
- function clsFurnace.reset(self)
- self.smeltIn = "" -- item placed into top slot e.g. "wood", "?ironore" (turtle on top)
- self.smeltInItemCount = 0 -- amount placed into top slot
- self.smeltOut = "" -- expected item produced if successful eg "charcoal", "iron" (turtle underneath)
- self.smeltOutItemCount = 0 -- amount of smelted product
- self.fuelType = "" -- fuel used in bottom slot e.g "planks" (turtle at any side)
- self.fuelCount = 0 -- amount of fuel placed
- self.smeltSuccess = false -- set to true if smelt succeeded
- end
- function clsFurnace.getFurnaceName(self)
- return self.name
- end
- function clsFurnace.getSmeltInItem(self)
- return self.smeltIn
- end
- function clsFurnace.setSmeltInItem(self, newVal)
- self.smeltIn = newVal
- end
- function clsFurnace.getSmeltInItemCount(self)
- return self.smeltInItemCount
- end
- function clsFurnace.setSmeltInItemCount(self, newVal)
- self.smeltInItemCount = newVal
- end
- function clsFurnace.getSmeltOutItem(self)
- return self.smeltOut
- end
- function clsFurnace.setSmeltOutItem(self, newVal)
- self.smeltOut = newVal
- end
- function clsFurnace.getSmeltOutItemCount(self)
- return self.smeltOutItemCount
- end
- function clsFurnace.setSmeltOutItemCount(self, newVal)
- self.smeltOutItemCount = newVal
- end
- function clsFurnace.getFuelType(self)
- return self.fuelType
- end
- function clsFurnace.setFuelType(self, newVal)
- self.fuelType = newVal
- end
- function clsFurnace.getFuelCount(self)
- return self.fuelCount
- end
- function clsFurnace.setFuelCount(self, newVal)
- self.fuelCount = newVal
- end
- function clsFurnace.getSmeltSuccess(self)
- return self.smeltSuccess
- end
- function clsFurnace.setSmeltSuccess(self, newVal)
- self.smeltSuccess = newVal
- end
- furnace1 = clsFurnace.new("Main Furnace")
- end
- function createIsland()
- if getCobbleStock() >= 5 then
- down(1)
- turtle.select(slot:getItemSlot("cobble"))
- for i = 1, 4 do
- if not turtle.detect() then
- turtle.place()
- end
- turnRight(1)
- end
- up(1)
- turtle.placeDown()
- elseif getDirtStock() >= 5 then
- down(1)
- turtle.select(slot:getItemSlot("dirt"))
- for i = 1, 4 do
- if not turtle.detect() then
- turtle.place()
- end
- turnRight(1)
- end
- up(1)
- turtle.placeDown()
- end
- end
- function createLogfileObject()
- clsLogfile = {} -- the table representing the class, which will double as the metatable for any instances
- clsLogfile.__index = clsLogfile -- failed table lookups on the instances should fallback to the class table, to get methods
- --[[
- Store details in "start.ini" on the local computer:
- onServer=true/false
- usePastebin=true/false
- configModified=true/false
- useLogfiles=true/false
- ]]--
- function clsLogfile.new() --equivalent to VB class initialise
- local self = setmetatable({}, clsLogfile)
- local iniText = ""
- local iniFile = ""
- self.size = 0
- self.useLog = false
- self.numLogFiles = 17 -- change if more logfiles needed
- self.logFiles = {}
- self.logFileExists = {}
- self.boolIniFileExists = false
- self.useVerbose = false
- self.onServer = false
- self.usePastebin = false
- self.configModified = false
- self.logFiles[1] = "logStart.txt"
- self.logFiles[2] = "logHarvestRight1.txt"
- self.logFiles[3] = "logHarvestRight2.txt"
- self.logFiles[4] = "logHarvestRight3.txt"
- self.logFiles[5] = "logHarvestRight4.txt"
- self.logFiles[6] = "logHarvestRight5.txt"
- self.logFiles[7] = "logHarvestRight6.txt"
- self.logFiles[8] = "logHarvestLeft1.txt"
- self.logFiles[9] = "logHarvestLeft2.txt"
- self.logFiles[10] = "logHarvestLeft3.txt"
- self.logFiles[11] = "logHarvestLeft4.txt"
- self.logFiles[12] = "logCreateMine.txt"
- self.logFiles[13] = "logMiningLayer1.txt"
- self.logFiles[14] = "logMiningLayer2.txt"
- self.logFiles[15] = "logMiningLayer3.txt"
- self.logFiles[16] = "logMiningBedrock.txt"
- self.logFiles[17] = "logCraftMiningTurtle.txt"
- for i = 1, self.numLogFiles do
- self.logFileExists[i] = false
- end
- self.currentFileName = ""
- self.currentFileNameIndex = 0
- self.startFileNameIndex = 1
- for i = 1, self.numLogFiles do
- if fs.exists(self.logFiles[i]) then
- self.logFileExists[i] = true
- end
- end
- if fs.exists("start.ini") and fs.getSize("start.ini") > 0 then
- self.boolIniFileExists = true
- iniFile = fs.open("start.ini", "r")
- iniText = iniFile.readLine()
- self.onServer = string.sub(iniText, string.find(iniText, "onServer=") + 9)
- iniText = iniFile.readLine()
- if string.sub(iniText, string.find(iniText, "usePastebin=") + 12) == "true" then
- self.usePastebin = true
- end
- iniText = iniFile.readLine()
- if string.sub(iniText, string.find(iniText, "configModified=") + 15) == "true" then
- self.configModified = true
- end
- iniText = iniFile.readLine()
- if string.sub(iniText, string.find(iniText, "useLog=") + 7) == "true" then
- self.useLog = true
- end
- iniText = iniFile.readLine()
- if string.sub(iniText, string.find(iniText, "useVerbose=") + 11) == "true" then
- self.useVerbose = true
- end
- iniFile.close()
- end
- return self
- end
- function clsLogfile.setUseVerbose(self, use)
- self.useVerbose = use
- end
- function clsLogfile.getUseVerbose(self)
- return self.useVerbose
- end
- function clsLogfile.getUseLog(self)
- return self.useLog
- end
- function clsLogfile.setUseLog(self, use)
- self.useLog = use
- self.writeIniFile(self)
- end
- function clsLogfile.checkLogExists(self)
- local result = false
- for i = 1, self.numLogFiles do
- if self.logFileExists[i] then
- result = true
- end
- end
- return result
- end
- function clsLogfile.getCurrentFileName(self)
- return self.currentFileName
- end
- function clsLogfile.getCurrentFileSize(self)
- return fs.getSize(self.currentFileName)
- end
- function clsLogfile.getCurrentFileNameIndex(self)
- return self.currentFileNameIndex
- end
- --use if one logfile not used, eg harvestallTrees only on right side
- function clsLogfile.setCurrentFileNameIndex(self, indexNo)
- self.currentFileNameIndex = indexNo
- end
- function clsLogfile.getStartFileNameIndex(self)
- return self.startFileNameIndex
- end
- --use during debugging to set logfile to appropriate stage
- function clsLogfile.setStartFileNameIndex(self, indexNo)
- self.startFileNameIndex = indexNo
- end
- function clsLogfile.deleteLog(self)
- local result = false
- for i = 1, self.numLogFiles do
- if fs.exists(self.logFiles[i]) then
- fs.delete(self.logFiles[i])
- end
- self.logFileExists[i] = false
- result = true
- end
- return result
- end
- function clsLogfile.renameLog(self)
- local result = false
- for i = 1, self.numLogFiles do
- if fs.exists(self.logFiles[i]) then
- if fs.exists(self.logFiles[i]..".bak") then
- fs.delete(self.logFiles[i]..".bak")
- end
- fs.move(self.logFiles[i], self.logFiles[i]..".bak")
- --fs.rename(self.logFiles[i], self.logFiles[i]..".bak")
- end
- self.logFileExists[i] = false
- result = true
- end
- return result
- end
- function clsLogfile.getNextFileName(self)
- local tempIndex = self.numLogFiles
- if self.currentFileNameIndex < self.numLogFiles then
- tempIndex = self.currentFileNameIndex + 1
- end
- return self.logFiles[tempIndex]
- end
- function clsLogfile.useFileName(self, fileName)
- local result = false
- for i = 1, self.numLogFiles do
- if self.logFiles[i] == fileName then
- result = true
- self.currentFileName = self.logFiles[i]
- self.currentFileNameIndex = i
- if not fs.exists(self.currentFileName) then
- logFile = fs.open(self.currentFileName, "w")
- logFile.writeLine("LogFile "..self.currentFileName.." started at "..textutils.formatTime(os.time(), true))
- logFile.close()
- end
- break
- end
- end
- return result
- end
- function clsLogfile.useNextFileName(self)
- local result = false
- local logFile = ""
- self.currentFileNameIndex = self.currentFileNameIndex + 1
- if self.currentFileNameIndex > self.numLogFiles then
- self.currentFileNameIndex = self.numLogFiles
- result = true
- end
- self.currentFileName = self.logFiles[self.currentFileNameIndex]
- if not fs.exists(self.currentFileName) then
- logFile = fs.open(self.currentFileName, "w")
- logFile.writeLine("LogFile "..self.currentFileName.." started at "..textutils.formatTime(os.time(), true))
- logFile.close()
- end
- return result
- end
- function clsLogfile.appendLine(self, newText, verbose)
- local logFile = ""
- local doWrite = false
- -- use verbose flag to write line ONLY if useVerbose method has been chosen
- if verbose then
- if self.useVerbose then
- doWrite = true
- end
- else
- doWrite = true
- end
- if doWrite then
- if self.currentFileName == "" then --no logfile written yet
- self.currentFileNameIndex = self.startFileNameIndex --default 1 unless set first for debugging
- self.currentFileName = self.logFiles[self.startFileNameIndex] -- set filename
- end
- if not fs.exists(self.currentFileName) then --logfile not created
- logFile = fs.open(self.currentFileName, "w") --create file
- logFile.writeLine(newText)
- logFile.close()
- else
- logFile = fs.open(self.currentFileName, "a")
- logFile.writeLine(newText)
- logFile.close()
- end
- end
- end
- function clsLogfile.iniFileExists(self)
- return self.boolIniFileExists
- end
- function clsLogfile.getOnServer(self)
- return self.onServer
- end
- function clsLogfile.setOnServer(self, setting)
- self.onServer = setting
- self.writeIniFile(self)
- end
- function clsLogfile.getUsePastebin(self)
- return self.usePastebin
- end
- function clsLogfile.setUsePastebin(self, setting)
- self.usePastebin = setting
- self.writeIniFile(self)
- end
- function clsLogfile.getConfigModified(self)
- return self.configModified
- end
- function clsLogfile.setConfigModified(self, setting)
- self.configModified = setting
- self.writeIniFile(self)
- end
- function clsLogfile.getUseVerbose(self)
- return self.useVerbose
- end
- function clsLogfile.setUseVerbose(self, setting)
- self.useVerbose = setting
- self.writeIniFile(self)
- end
- function clsLogfile.writeIniFile(self)
- local iniFile = ""
- iniFile = fs.open("start.ini", "w")
- iniFile.writeLine("onServer=".. tostring(self.onServer))
- iniFile.writeLine("usePastebin=".. tostring(self.usePastebin))
- iniFile.writeLine("configModified=".. tostring(self.configModified))
- iniFile.writeLine("useLog=".. tostring(self.useLog))
- iniFile.writeLine("useVerbose=".. tostring(self.useVerbose))
- iniFile.close()
- end
- function clsLogfile.deleteIniFile(self)
- if fs.exists("start.ini") then
- fs.delete("start.ini")
- self.boolIniFileExists = false
- end
- end
- --uses:
- --[[
- fso:setStartFileNameIndex(2) -- debugging using harvestTrees log
- local fileName = fso:getCurrentFileName()
- local fileNameIndex = fso:getCurrentFileNameIndex()
- fso:useNextFileName()
- fso:useFileName("logMiningLayer1.txt")
- if fso:checkLogExists() then
- fso:deleteLog()
- end
- ]]--
- --(fso is a reference to VB6 FileSystemObject :) old habits die hard)
- fso = clsLogfile.new()
- end
- function createMine()
- local tempYcoord = location:getY()
- local numNeeded = 0
- local itemsNeeded = {}
- local itemList = ""
- local torchesAt = {}
- local sTime = os.time()
- for i = 1, 33 do
- torchesAt[i] = false
- end
- saveToLog("createMine: starting..", true)
- --level out mine entrance and mark with a sign
- --go down to level 15 and create + shape with firstTree at centre, 16 blocks length each arm
- --torches at each end, and half way along
- forward(16, 1) --now over original mineshaft
- while turtle.detectUp() do
- up(1, 1)
- end
- up(1) --so can dig any existing torch/ sign
- while location:getY() >= tempYcoord - 1 do
- for i = 1,4 do
- dig.digNew{self = dig, callFrom = "createMine"}
- turnRight(1)
- end
- down(1, 1)
- end
- up(1)
- turtle.select(slot:getItemSlot("cobble"))
- for i = 1,4 do
- turtle.place()
- turnRight(1)
- end
- up(1)
- if slot.getItemSlot(slot, "signs") > 0 then
- turtle.select(slot.getItemSlot(slot, "signs"))
- saveToLog("createMine: placing sign Diamond Mine\nPreparing for\nextensive operations\n"..textutils.formatTime(os.time(), true), true)
- turtle.place("Diamond Mine\nPreparing for\nDeep Excavation\n"..textutils.formatTime(os.time(), true))
- slot.update{self = slot, item = "signs", delete = true}
- if slot:getItemSlot("signs") == 0 then
- sortInventory(true)
- end
- end
- turnRight(2)
- mineEntrance:setX(location:getX())
- mineEntrance:setY(location:getY())
- mineEntrance:setZ(location:getZ())
- mineEntrance:setFacing(location:getFacing())
- saveToLog("createMine: xCoordMine = "..mineEntrance:getX(), true)
- saveToLog("createMine: yCoordMine = "..mineEntrance:getY(), true)
- saveToLog("createMine: zCoordMine = "..mineEntrance:getZ(), true)
- saveToLog("createMine: facingMine = "..mineEntrance:getFacing(), true)
- -- need to find redstone, lapis, (obsidian), diamond, emerald
- -- go down to level 15, dig 16X2 corridoor, by digging down on 14, digging forward on 15, placing ceiling on 16
- itemsNeeded, numNeeded = getItemsNeeded(3) --eg {"diamond","redstone"}, 2
- saveToLog("createMine: no. items needed = "..numNeeded, true)
- if numNeeded > 0 then
- for i = 1, numNeeded do
- if i == 1 then
- itemList = itemList.."'"..tostring(itemsNeeded[i]).."'"
- else
- itemList = itemList..",'"..tostring(itemsNeeded[i]).."'"
- end
- saveToLog("createMine, item "..i.." needed = "..itemsNeeded[i], true)
- end
- end
- while location:getY() > 14 do
- down(1, 1, itemList)
- end
- refuel(0)
- -- eg itemList = 'redstone','ironore','diamonds'
- -- now dig 32 x 2 corridor
- -- mineCorridor(length, torchesAt, doNotPlug, doReturn)
- for i = 1, 3 do
- torchesAt[1] = false
- torchesAt[9] = true
- torchesAt[17] = false
- torchesAt[25] = true
- torchesAt[33] = true
- --do first corridor
- --mineCorridor(length = 33, torchesAt, doNotPlug entance, do not return)
- mineCorridor(33, torchesAt, true, false, itemList) --mineCorridor(length, torchesAt, doNotPlug, doReturn, itemList)
- turnRight(2) -- at end of first corridor, facing entrance (N)
- forward(16, 1)
- turnRight(1) --now in centre, facing E
- torchesAt[1] = false
- torchesAt[17] = true
- --mineCorridor(length = 17, torchesAt 9, 17, doNotPlug entrance, return 17 blocks and turn 180 deg)
- mineCorridor(17, torchesAt, false, false, itemList) --now in centre, facing E
- refuel(0)
- turnRight(2)
- forward(16, 1) --now in centre, facing W
- mineCorridor(17, torchesAt, false, false, itemList) --now in centre, facing W
- turnRight(2)
- forward(16, 1)
- turnLeft(1) --now in centre, facing N
- forward(16, 1)
- turnRight(2)
- if i < 3 then
- down(3, 1)
- end
- end
- refuel(0)
- while location:getY() < mineEntrance:getY() do
- up(1, 1)
- end --at surface now
- forward(5, 1)
- forward(1, 1) -- over markerSand
- storeUnknownItem("sand")
- forward(11, 1)
- turnRight(2)
- saveToLog("createMine: started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), true)
- end
- function createMinePlug()
- dig.digNew{self = dig, direction = "down"}
- down(1)
- for i = 1, 4 do
- if checkWater("forward") then
- if getCobbleStock() > 0 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.place()
- elseif getDirtStock() > 0 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.place()
- end
- end
- turnRight(1)
- end
- up(1)
- if getCobbleStock() > 0 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- elseif getDirtStock() > 0 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- end
- function createPlaceStorageObject()
- clsPlaceStorage = {} -- the table representing the class, which will double as the metatable for any instances
- clsPlaceStorage.__index = clsPlaceStorage -- failed table lookups on the instances should fallback to the class table, to get methods
- function clsPlaceStorage.initialise() --equivalent to VB class initialise
- local self = setmetatable({}, clsPlaceStorage)
- self.storage = {}
- self.marker = {}
- self.storage["torches"] = false
- self.storage["sticks"] = false
- self.storage["signs"] = false
- self.storage["wood"] = false
- self.storage["ironore"] = false
- self.storage["sand"] = false
- self.storage["redstone"] = false
- self.storage["pickaxes"] = false
- self.storage["saplings"] = false
- self.marker["torches"] = false
- self.marker["signs"] = false
- self.marker["sticks"] = false
- self.marker["wood"] = false
- self.marker["ironore"] = false
- self.marker["sand"] = false
- self.marker["redstone"] = false
- self.marker["lapis"] = false
- return self
- end
- function clsPlaceStorage.place(self, storageName, markerOnly)
- --assume placed under furnace, facing tree farm
- local woodAvailable = 0
- local wood2Available = 0
- local woodType = ""
- local planksType = ""
- local numChestsNeeded = 0
- local doContinue = false
- local placeChest = false
- local placeMarker = true
- local success = false
- local planksType = {}
- changeDirection("faceMine")
- planksType["wood"] = "planks"
- planksType["wood2"] = "planks2"
- -- placed relative North of furnace, on path to tree farm, every other block, 8 chests in all
- -- storageTorches, storageSticks, storageSigns, storageWood, storageIronore, storageSand, storageRedstone, storagePickaxe
- if markerOnly then
- doContinue = true
- placeChest = false
- else
- placeChest = true
- saveToLog("placeStorage"..storageName..": checking if enough wood or chests")
- numChestsNeeded = 2 - turtle.getItemCount(slot:getItemSlot("chests"))
- if numChestsNeeded < 0 then
- numChestsNeeded = 0
- else
- saveToLog("placeStorage"..storageName..": need 1 chest crafted")
- end
- if storageName == "torches" then
- --check if enough wood to make chest(s)
- saveToLog("placeStorage: checking if enough wood / wood2...")
- woodType, woodAvailable, wood2Available = getWoodAvailable{woodNeeded = 0} --need 2 for chest, 1 for sticks, 1 for charcoal
- if woodType ~= "none" then --enough to continue
- doContinue = true
- placeChest = true
- else
- placeMarker = false
- end
- else
- if numChestsNeeded > 0 then
- --make planks first
- woodType = getWoodAvailable{woodNeeded = 2 * numChestsNeeded + 2} --2 per chest + 2 spare
- if woodType ~= "none" then --enough to continue with 2 spare
- if woodType == "wood" then
- saveToLog("placeStorage: "..woodAvailable.." of "..woodType.." available.", false)
- else
- saveToLog("placeStorage: "..wood2Available.." of "..woodType.." available.", false)
- end
- saveToLog("placeStorage: crafting "..planksType[woodType].." from "..woodType)
- craft{craftItem = planksType[woodType], craftQuantity = 8 * numChestsNeeded, sourceItem1 = woodType, destSlot = 0, doSort = true}
- saveToLog("placeStorage: crafting chests from "..planksType[woodType])
- craft{craftItem = "chests", craftQuantity = numChestsNeeded, sourceItem1 = planksType[woodType], destSlot = 0, doSort = true}
- doContinue = true
- end
- else
- saveToLog("placeStorage: enough chests already in stock")
- doContinue = true
- end
- end
- end
- if doContinue then -- chests available/ markerOnly
- if storageName == "torches" then
- saveToLog("placeStorage: woodAvailable = "..woodAvailable.." of "..woodType, true)
- saveToLog("placeStorage: crafting "..planksType[woodType].." from "..woodType, true)
- craft{craftItem = planksType[woodType], craftQuantity = 12, sourceItem1 = woodType, destSlot = 0, doSort = true}
- saveToLog("placeStorage: crafting chests from "..planksType[woodType], true)
- craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = planksType[woodType], destSlot = 0, doSort = true}
- craftSticks(4)
- craftTorches(4)
- smelt("cobble", 1) --use last planks, unless coal already found and used
- turnRight(1)
- turtle.select(slot:getItemSlot("torches"))
- saveToLog("placeStorage: placing torch", true)
- turtle.place() --next to chest/furnace
- turnLeft(1)--back to start
- success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 0)
- elseif storageName == "signs" then
- success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 2)
- elseif storageName == "sticks" then
- if slot:getItemSlot("sticks") > 0 then
- if turtle.getItemCount(slot:getItemSlot("sticks")) >= 6 then
- if not craft{craftItem = "fences", craftQuantity = 2, sourceItem1 = "sticks", destSlot = 0} then
- placeMarker = false
- end
- else
- if storageSticks:getItemCount("sticks") >= 6 then
- forward(4, 1)
- turnRight(2)
- getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
- forward(4, 1)
- turnRight(2)
- if not craft{craftItem = "fences", craftQuantity = 2, sourceItem1 = "sticks", destSlot = 0} then
- placeMarker = false
- end
- end
- end
- end
- success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 4)
- elseif storageName == "wood" then
- success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 6)
- elseif storageName == "ironore" then
- if slot:getItemSlot("ironore") > 0 then
- if turtle.getItemCount(slot:getItemSlot("ironore")) < 2 then --not enough ironore to mark
- placeMarker = false
- end
- end
- success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 8)
- elseif storageName == "sand" then
- success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 10)
- elseif storageName == "redstone" then -- 1 chests to place
- if slot:getItemSlot("redstone") > 0 then
- if turtle.getItemCount(slot:getItemSlot("redstone")) > 9 then
- if craft{craftItem = "redstone block", craftQuantity = 1, sourceItem1 = "redstone", destSlot = 0} then
- placeMarker = true
- else
- placeMarker = false
- end
- else
- placeMarker = false
- end
- else
- placeMarker = false
- end
- success = placeStorage.doTask(self, storageName, placeChest, placeMarker, 12)
- elseif storageName == "pickaxes" then
- success = placeStorage.doTask(self, storageName, placeChest, false, 14)
- elseif storageName == "saplings" then
- forward(2, 1)
- turnRight(1)
- forward(2)
- turnLeft(1)
- dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.place"}
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeDown()
- saveToLog("placeStorage: StorageSaplings placed")
- self.storage["saplings"] = true
- turnLeft(1)
- forward(2, 1)
- turnLeft(1)
- forward(2, 1)
- turnRight(2)
- success = true
- end
- else
- saveToLog("placeStorage: not enough wood to place storage"..
- string.upper(string.sub(storageName, 1, 1))..
- string.sub(storageName, 2), false, true)
- end
- -- placeStorage:place("signs", false, 1")
- -- placeStorage:place(storageName, false, 1")
- return success
- end
- function clsPlaceStorage.doTask(self, storageName, placeChest, placeMarker, moveForward)
- local success = false
- local storagePlacedName = storageName
- local markerPlacedName = storageName
- local storageLogText = "placeStorage: storage"..
- string.upper(string.sub(storageName, 1, 1))..
- string.sub(storageName, 2).." placed"
- local markerLogText = "placeStorage: placing marker "..storageName
- changeDirection("faceMine")
- forward(moveForward, 1)
- if placeChest then
- dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
- down(1)
- dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- up(1)
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeDown()
- saveToLog(storageLogText)
- self.storage[storagePlacedName] = true
- forward(1, 1)
- dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
- down(1)
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- up(1)
- turtle.placeDown()
- success = true
- else
- forward(1, 1)
- end
- if placeMarker then
- if storageName == "sticks" then
- storageName = "fences"
- elseif storageName == "redstone" then
- storageName = "redstone block"
- end
- if slot:getItemSlot(storageName) > 0 then
- if storageName == "wood" then
- if turtle.getItemCount(slot:getItemSlot(storageName)) < 2 then
- if slot:getItemSlot("wood2") > 0 then
- if turtle.getItemCount(slot:getItemSlot(storageName)) < 2 then
- placeMarker = false
- else
- storageName ="wood2"
- end
- end
- end
- end
- if placeMarker then -- ok for wood/wood2
- dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
- down(1)
- dig.digNew{self = dig, direction = "down", callFrom = "clsPlaceStorage.doTask"}
- if getCobbleStock() > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- else
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.placeDown()
- up(1)
- saveToLog(markerLogText, true, false)
- turtle.select(slot:getItemSlot(storageName))
- if storageName == "signs" then
- turtle.placeDown("You are \nStanding on \nstorageSigns \nGET OFF!")
- else
- turtle.placeDown()
- end
- self.marker[markerPlacedName] = true
- success = true
- if storageName == "fences" then
- turtle.refuel() --destroy remaining fence
- end
- slot.update{self = slot, item = storageName, delete = true}
- end
- end
- end
- --return to furnace
- turnRight(2)
- forward(moveForward + 1, 1)
- turnRight(2)
- return success
- end
- function clsPlaceStorage.getMarkerPlaced(self, markerName)
- return self.marker[markerName] or false
- end
- function clsPlaceStorage.getStoragePlaced(self, storageName)
- --placeStorage:getStoragePlaced("sand")
- return self.storage[storageName] or false
- end
- function clsPlaceStorage.setMarkerPlaced(self, markerName)
- --placeStorage:getMarkerPlaced("sand")
- self.marker[markerName] = true
- end
- function clsPlaceStorage.setStoragePlaced(self, storageName)
- self.storage[storageName] = true
- end
- placeStorage = clsPlaceStorage.initialise()
- end
- function createTreeFarmObject()
- clsTreeFarm = {} -- the table representing the class, which will double as the metatable for any instances
- clsTreeFarm.__index = clsTreeFarm -- failed table lookups on the instances should fallback to the class table, to get methods
- function clsTreeFarm.new() --equivalent to VB class initialise
- local self = setmetatable({}, clsTreeFarm)
- self.numSaplings = 0
- self.timePlanted = 0
- self.dayPlanted = 0
- self.timeHarvested = 0
- self.woodHarvested = 0
- self.saplingPattern = {}
- self.saplingPattern[1] = 0
- self.saplingPattern[2] = 1
- self.saplingPattern[3] = 2
- self.saplingPattern[4] = 0
- self.saplingPattern[5] = 1
- self.saplingPattern[6] = 2
- self.saplingPattern[7] = 0
- self.saplingPattern[8] = 1
- self.saplingPattern[9] = 2
- self.saplingPattern[10] = 0
- self.saplingPattern[11] = 1
- self.saplingPattern[12] = 2
- self.saplingPattern[13] = 0
- self.saplingPattern[14] = 0
- self.saplingPattern[15] = 0
- return self
- end
- function clsTreeFarm.reset(self)
- self.numSaplings = 0
- self.timePlanted = 0
- self.dayPlanted = 0
- self.timeHarvested = 0
- self.woodHarvested = 0
- end
- function clsTreeFarm.getNumSaplings(self)
- return self.numSaplings
- end
- function clsTreeFarm.setNumSaplings(self, num)
- self.numSaplings = num
- end
- function clsTreeFarm.addSapling(self)
- self.numSaplings = self.numSaplings + 1
- end
- function clsTreeFarm.getTimePlanted(self)
- return self.timePlanted
- end
- function clsTreeFarm.setTimePlanted(self, num)
- self.timePlanted = num
- self.dayPlanted = os.day()
- end
- function clsTreeFarm.getTimeHarvested(self)
- return self.timeHarvested
- end
- function clsTreeFarm.setTimeHarvested(self, num)
- self.timeHarvested = num
- end
- function clsTreeFarm.getWoodHarvested(self)
- return self.woodHarvested
- end
- function clsTreeFarm.setWoodHarvested(self, num)
- self.woodHarvested = num
- end
- function clsTreeFarm.getSaplingPattern(self, index)
- return self.saplingPattern[index]
- end
- function clsTreeFarm.getMaxHarvest(self)
- return self.numSaplings * 5
- end
- function clsTreeFarm.setDebug(self, saplings, trees)
- --use if debugging and treeFarm already planted
- self.numSaplings = saplings + trees
- self.timePlanted = os.time()
- if trees >= 6 then
- self.dayPlanted = os.day() - 3
- end
- end
- function clsTreeFarm.getPotentialHarvest(self)
- local currentDay = os.day()
- local currentTime = os.time()
- local numDays = 0
- local numHours = 0
- local harvest = 0
- --days measured from world creation
- numDays = currentDay - self.dayPlanted
- --time 0 - 24 then resets
- if currentTime > self.timePlanted then -- eg 13.5 > 7.5 (13.5 - 7.5 = 6 hours)
- numHours = currentTime - self.timePlanted
- else -- eg 6.465 < 21.454
- numHours = 24 - self.timePlanted + currentTime -- eg 24 - 21.500 + 6.500 = 9 hours
- end
- -- 8 trees planted, half done in 12-14 minecraft hours
- -- 1 tree 4.5 hrs
- -- 2 trees 7 hrs
- -- 3 trees 8 hrs
- -- 4 trees 13 hrs
- -- 5 trees 20 hrs
- -- 6 trees 30 hours
- -- estimate as half no of saplings per 12 hours
- if numDays == 0 then
- harvest = math.ceil((5 * self.numSaplings) / 2)
- elseif numDays == 1 then
- harvest = math.ceil(((5 * self.numSaplings) * 2) / 3)
- else -- > 2 days probably most trees * 5 wood
- harvest = 5 * self.numSaplings
- end
- return harvest
- end
- treeFarm = clsTreeFarm.new()
- end
- function createSlotObject()
- -- slot variables
- clsSlot = {}
- clsSlot.__index = clsSlot
- function clsSlot.new(self) --creates new instance of slot object
- local self = setmetatable({}, clsSlot)
- self.slotContains = {}
- self.slotCount = {}
- self.slotStatus = {}
- self.index = 1
- self.itemTested = {}
- self.firstUnknownItem = ""
- self.lastUnknownItem = ""
- self.freeUnknownItem = "item1"
- self.numUnknownItems = 0
- -- initialise variables
- for i = 1, 16 do
- self.slotContains[i] = ""
- self.slotCount[i] = 0
- self.slotStatus[i] = "empty"
- self.itemTested[i] = ""
- end
- return self
- end
- function clsSlot.resetVariables(self) --resets all variables (properties) of slot object
- for i = 1, 16 do
- self.slotContains[i] = ""
- self.slotCount[i] = 0
- self.slotStatus[i] = "empty"
- self.itemTested[i] = ""
- end
- self.firstUnknownItem = ""
- self.lastUnknownItem = ""
- self.freeUnknownItem = "item1"
- self.index = 1
- self.numUnknownItems = 0
- end
- function clsSlot.getUnknownItemCount(self) --returns no of unknown items eg "item1", "item2" count = 2
- local amount = 0
- local tempSlot = 0
- for i = 1, 16 do
- if self.slotStatus[i] == "unknown" then
- amount = amount + 1
- end
- end
- return amount--0 if no unknown item slots
- end
- function clsSlot.getSlotContains(self, slotNo) --return contents of selected slotNo eg slot 4 = "dirt"
- return self.slotContains[slotNo]
- end
- function clsSlot.getItemCount(self, item) --return number of items in turtle using item name eg "dirt" = 58
- local result = 0
- for i = 1, 16 do
- if self.slotContains[i] == item then
- result = self.slotCount[i]
- break
- end
- end
- return result
- end
- function clsSlot.getSlotCount(self, slotNo) --return number of items in turtle slot = turtle.getItemCount()
- return self.slotCount[slotNo]
- end
- function clsSlot.getSlotStatus(self, slotNo) --return "empty", "known", "unknown"
- return self.slotStatus[slotNo]
- end
- function clsSlot.setSlotCount(self, slotNo, count) --set number of items in turtle slot
- self.slotCount[slotNo] = count
- end
- function clsSlot.getItemSlot(self, item) --return slot number of selected item eg dirt in slot 4
- local result = 0
- for i = 1, 16 do
- if self.slotContains[i] == item then
- result = i
- break
- end
- end
- return result
- end
- function clsSlot.getItemTested(self, slotNo, item) --VB Property Get return if eg slot 11 has been tested for "gravel"
- local result = false
- if self.itemTested[slotNo] ~= "" then
- if string.find(self.itemTested[slotNo], item) ~= nil then
- result = true
- end
- end
- return result
- end
- function clsSlot.setItemTested(self, slotNo, item) --VB Property Let equivalent used to see if an unknown item has been checked
- --self.itemTested[item1] = "coal,flint,gravel"
- local result = false
- if self.itemTested[slotNo] == "" then
- self.itemTested[slotNo] = item
- else
- if string.find(self.itemTested[slotNo], item) == nil then
- self.itemTested[slotNo] = self.itemTested[slotNo]..","..item
- result = true
- end
- end
- return result
- end
- function clsSlot.resetUnknownItems(self) --reset "item1", "item2" etc to default
- local index = 0
- local firstItem = ""
- local lastItem = ""
- self.numUnknownItems = 0
- for i = 1, 16 do
- if self.slotStatus[i] == "unknown" then
- index = index + 1
- self.numUnknownItems = self.numUnknownItems + 1
- self.slotContains[i] = "item"..index
- if firstItem == "" then
- firstItem = "item"..index
- end
- lastItem = "item"..index
- firstFreeItem = "item"..index + 1
- end
- end
- if index > 0 then --at least 1 unknown item
- self.firstUnknownItem = firstItem
- self.lastUnknownItem = lastItem
- self.freeUnknownItem = firstFreeItem
- else
- self.firstUnknownItem = ""
- self.lastUnknownItem = ""
- self.freeUnknownItem = "item1"
- end
- end
- function clsSlot.getFirstUnknownItem(self) --returns eg item1 or ""
- return self.firstUnknownItem
- end
- function clsSlot.getLastUnknownItem(self) --returns eg "item3"
- return self.lastUnknownItem
- end
- function clsSlot.getFreeUnknownItem(self) --returns first unused itemX eg "item4"
- return self.freeUnknownItem
- end
- function clsSlot.getMostUnknownItem(self) --returns itemX with largest itemCount eg "item3"
- local mostItem = ""
- local amount = 0
- local tempSlot = 0
- for i = 1, 16 do
- if self.slotStatus[i] == "unknown" then
- if self.slotCount[i] > amount then
- amount = self.slotCount[i]
- mostItem = self.slotContains[i]
- end
- end
- end
- return mostItem --"" if no unknown items
- end
- function clsSlot.getMostUnknownItemSlot(self) --returns itemX with largest itemCount eg "item3"
- local amount = 0
- local useSlot = 0
- for i = 1, 16 do
- if self.slotStatus[i] == "unknown" then
- if self.slotCount[i] > amount then
- amount = self.slotCount[i]
- useSlot = i
- end
- end
- end
- return useSlot --0 if no unknown items
- end
- function clsSlot.getLeastUnknownItemSlot(self) --returns slotNo with smallest itemCount eg "item1" - slot 7
- local amount = 65
- local useSlot = 0
- for i = 1, 16 do
- if self.slotStatus[i] == "unknown" then
- if self.slotCount[i] < amount then
- amount = self.slotCount[i]
- useSlot = i
- end
- end
- end
- return useSlot --0 if no unknown items
- end
- function clsSlot.transfer(arg)
- local self = arg.self
- --slot.transfer{self = slot, fromSlot, transferTo = 16}
- --slot.transfer{self = slot, item = "wood", transferTo = 16}
- --slot.transfer{self = slot, item = arg.sourceItem1, transferTo = 16}
- if arg.fromSlot == nil then --arg.item provided
- for i = 1, 16 do
- if self.slotContains[i] == arg.item then
- arg.fromSlot = i
- break
- end
- end
- --arg.fromSlot = self.itemSlot[arg.item] --itemSlot["wood"] (1)
- end
- if arg.item == nil then
- arg.item = self.slotContains[arg.fromSlot]
- end
- self.slotCount[arg.transferTo] = self.slotCount[arg.fromSlot]
- self.slotCount[arg.fromSlot] = 0
- self.slotStatus[arg.transferTo] = self.slotStatus[arg.fromSlot]
- self.slotStatus[arg.fromSlot] = "empty"
- self.itemTested[arg.transferTo] = self.itemTested[arg.fromSlot]
- self.itemTested[arg.fromSlot] = ""
- self.slotContains[arg.transferTo] = self.slotContains[arg.fromSlot]
- self.slotContains[arg.fromSlot] = ""
- --saveToLog("transfer: from slot "..arg.fromSlot.." contains "..self.slotCount[arg.fromSlot].." "..self.slotContains[arg.fromSlot])
- --saveToLog("transfer: to slot "..arg.transferTo.." contains "..self.slotCount[arg.transferTo].." "..self.slotContains[arg.transferTo])
- end
- --slot.update{self = slot, slotNo = dugSlot, item = "item1"}
- --slot.update{self = slot, item = arg.sourceItem1, delete = true}
- --slot.update{self = slot, item = "planks", delete = true}
- --slot.update{self = slot, item = "?iron", newItem = "iron"}
- --slot.update{self = slot, item = "furnaces", delete = true}
- function clsSlot.update(arg) -- update turtle slot with item contents
- local quantity = 0
- local self = arg.self
- local tempItem = ""
- local tempSlotNo = arg.slotNo
- local count = 0
- --arg.self
- --arg.item
- --arg.slotNo
- --arg.delete
- --arg.deleteSlot
- --arg.newItem
- -- if slotNo given use it, or use existing slot
- if arg.slotNo == nil then --slot not provided, default to 0
- arg.slotNo = 0
- else --slotNo provided
- if arg.item == nil then --get item from slotNo
- arg.item = self.slotContains[arg.slotNo]
- end
- end
- if arg.item ~= nil then --item name provided
- if arg.slotNo == 0 then
- for i = 1, 16 do
- if self.slotContains[i] == arg.item then
- arg.slotNo = i
- break
- end
- end
- end
- end
- arg.delete = arg.delete or false
- arg.doReset = arg.doReset or false --used when crafting in fillChest
- -- item given. If not already in stock, use next available
- if arg.newItem ~= nil then -- naming unknown. delete is assumed, arg.slotNo, arg.item calculated above
- --slot.update{self = slot, slotNo = dugSlot, newItem = "coal"}
- self.itemTested[arg.slotNo] = ""
- self.slotContains[arg.slotNo] = arg.newItem
- self.slotCount[arg.slotNo] = turtle.getItemCount(arg.slotNo)
- if string.find(arg.newItem, "item") ~= nil then
- self.slotStatus[arg.slotNo] = "unknown"
- else
- self.slotStatus[arg.slotNo] = "known"
- objectives[arg.newItem] = true
- end
- saveToLog(" slot.update: Slot "..tostring(tempSlotNo) .." contains "..tostring(self.slotCount[tempSlotNo]).." "..arg.newItem.." "..arg.item.." deleted", false, true)
- else --not changing an item, just update
- quantity = turtle.getItemCount(arg.slotNo)
- if quantity == 0 and arg.delete then --turtle slot empty - remove all references
- self.slotContains[arg.slotNo] = ""
- self.itemTested[arg.slotNo] = ""
- self.slotCount[arg.slotNo] = 0
- self.slotStatus[arg.slotNo] = "empty"
- saveToLog(" slot.update: Deleting "..arg.item.." Slot "..arg.slotNo.." is empty", false, true)
- else --create or maintain selected slot, eg when wood2 detected and about to be harvested
- self.slotCount[arg.slotNo] = quantity
- --saveToLog(" slot.update: "..arg.item.." quantity updated to "..quantity.." in slot "..arg.slotNo, false)
- self.slotContains[arg.slotNo] = arg.item
- if string.find(arg.item, "item") ~= nil then --"item1", item2 etc
- self.slotStatus[arg.slotNo] = "unknown"
- else
- self.slotStatus[arg.slotNo] = "known"
- objectives[arg.item] = true
- end
- saveToLog(" slot.update: Slot "..arg.slotNo.." contains "..self.slotCount[arg.slotNo].." "..arg.item.." status = "..self.slotStatus[arg.slotNo], false)
- end
- end
- --update next free item, eg item1 just been allocated, make item2 the next free one
- for i = 1, 16 do
- if string.find(self.slotContains[i], "item") ~= nil then
- count = count + 1
- end
- end
- self.freeUnknownItem = "item"..count + 1
- if count > 0 then
- self.firstUnknownItem = "item1"
- self.lastUnknownItem = "item"..count
- self.freeUnknownItem = "item"..count + 1
- end
- --[[for i = 1, 16 do
- saveToLog(" slot.update: slot "..i.." status = "..self.slotStatus[i])
- end]]--
- --saveToLog(" slot.update:unknownItemCount = "..self.numUnknownItems)
- if arg.doReset then
- if clsSlot:getUnknownItemCount() > 0 then
- --saveToLog(" slot.update: resetting unknown items", false)
- clsSlot:resetUnknownItems()
- end
- end
- --[[debug log
- local printSlot = 0
- for i = 1, 16 do
- if self.slotCount[i] > 0 then
- saveToLog(" slot.update: debug - Slot "..i.." contains "..self.slotCount[i].." "..self.slotContains[i])
- end
- end
- for i = 1, 16 do
- if self.slotStatus[i] == "unknown" then
- saveToLog(" slotUpdate: debug - item"..i.." slot is "..self.slotContains[i])
- end
- end]]--
- end
- slot = clsSlot.new()
- end
- function down(steps, useSlot, itemList, calledFrom)
- local maxTries = 10
- local success = false
- steps = steps or 1
- useSlot = useSlot or 1
- itemList = itemList or ""
- calledFrom = calledFrom or ""
- for i = 1, steps do
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = "down", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
- if turtle.down() then
- success = true
- end
- else --move down unless mob in the way
- while not turtle.down() do
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = calledFrom}
- --if turtle.attackDown() then
- if attack() then
- saveToLog("down: mob attacked below!", true)
- end
- maxTries = maxTries - 1
- if maxTries <= 0 then
- if turtle.getFuelLevel() == 0 then
- if refuel(0) then
- maxTries = 10
- else
- saveToLog("forward: Out of fuel. Game Over!", true)
- error()
- end
- end
- end
- end
- success = true
- end
- if success then
- location:setY(location:getY() - 1)
- end
- end
- return success
- end
- function emptyChest(direction)
- local useItem = ""
- local turtleSlot = 0
- local success = false
- for i = 1, 16 do
- useItem = craftingChest:getSlotContains(i) --eg "dirt", "item"
- if useItem ~= "" then --"item", "dirt" etc
- turtleSlot = craftingChest:getTurtleSlot(i)
- turtle.select(turtleSlot)
- if direction == "up" then
- if turtle.suckUp() then
- success = true
- end
- elseif direction == "down" then
- if turtle.suckDown() then
- success = true
- end
- else
- if turtle.suck() then
- success = true
- end
- end
- if success then
- --saveToLog("emptyChest: Removed "..craftingChest.getSlotCount(craftingChest, i).." "..useItem.." from chest into turtle slot "..turtleSlot, false)
- slot.update{self = slot, slotNo = turtleSlot, item = useItem}
- else
- saveToLog("emptyChest: failed to remove "..useItem.." from chest", false)
- end
- end
- end
- --clear arrays on emptychest routine only
- craftingChest.resetVariables(craftingChest)
- saveToLog("emptyChest completed.", false, true)
- --unable to test for empty chest so return default
- return true
- end
- function fillChest(arg)
- -- fillChest{direction = arg.direction, exceptSlot1 = sourceSlot1, exceptSlot2 = sourceSlot2, exceptSlot3 = sourceSlot3} -- fill except chosen item(s)
- -- fillChest{direction = "forward", addItem = storedItem, addAmount = storeCount, fromSlot = 16, originalSlot = 0}
- -- fillChest{direction = arg.direction, addItem = sourceItem3, addAmount = sourceQuantity3, fromSlot = 14} use this for single items
- -- turtle must be sorted first, so unknown items are after identified items
- local chestIndex = 1
- local useItem = ""
- local quantity = 0
- local itemAlreadyIn = false
- if arg.deleteFromTurtle == nil then
- arg.deleteFromTurtle = true
- end
- if arg.doReset == nil then
- arg.doReset = false
- end
- saveToLog("fillChest: arg.direction = "..tostring(arg.direction)..
- " arg.exceptSlot1 = "..tostring(arg.exceptSlot1)..
- " arg.exceptSlot2 = "..tostring(arg.exceptSlot2)..
- " arg.exceptSlot3 = "..tostring(arg.exceptSlot3)..
- " arg.addItem = "..tostring(arg.addItem)..
- " arg.addAmount = "..tostring(arg.addAmount)..
- " arg.fromSlot = "..tostring(arg.fromSlot)..
- " arg.originalSlot = "..tostring(arg.originalSlot), false)
- if arg.addItem == nil then -- single item not defined, so empty all turtle except chosen items. could be failed craft attempt
- for i = 1, 16 do
- if arg.exceptSlot1 > 0 then --at least 1 craft item needed
- if i == arg.exceptSlot1 or i == arg.exceptSlot2 or i == arg.exceptSlot3 then
- saveToLog("Leaving crafting item "..slot.getSlotContains(slot, i).." in turtle slot "..i, false)
- else
- useItem = slot.getSlotContains(slot, i) --eg "", "item", "wood"
- quantity = turtle.getItemCount(i)
- if quantity > 0 then
- --saveToLog("Adding ".. quantity.." "..useItem.." from turtle slot "..i.." to chest slot "..craftingChest.getIndex(craftingChest), false)
- turtle.select(i)
- craftingChest.addItem(craftingChest, useItem, quantity, i)
- if arg.direction == "up" then
- turtle.dropUp()
- elseif arg.direction == "down" then
- turtle.dropDown()
- else
- turtle.drop()
- end
- --update turtle slot
- if arg.deleteFromTurtle then
- slot.update{self = slot, slotNo = i, item = useItem, delete = true, doReset = arg.doReset}
- end
- end
- end
- else --failed craft, empty turtle completely. cannot update quantities, items will go into existing slots, or form a new one
- if turtle.getItemCount(i) > 0 then
- if arg.direction == "up" then
- turtle.dropUp()
- elseif arg.direction == "down" then
- turtle.dropDown()
- else
- turtle.drop()
- end
- end
- end
- end
- else -- arg.addItem is "chests" or "stone"
- if arg.fromSlot ~= nil then
- turtle.select(arg.fromSlot)
- end
- if arg.originalSlot == nil then
- arg.originalSlot = arg.fromSlot
- end
- --clsChest.addItem(self, item, quantity, turtleSlot)
- --craftingChest.addItem(craftingChest, arg.addItem, arg.addAmount, arg.fromSlot)
- craftingChest:addItem(arg.addItem, arg.addAmount, arg.originalSlot)
- saveToLog("fillChest: Adding "..tostring(arg.addAmount).." "..tostring(arg.addItem).." to chest from slot "..tostring(arg.fromSlot).." (/"..tostring(arg.originalSlot)..")", false)
- if arg.direction == "up" then
- turtle.dropUp()
- elseif arg.direction == "down" then
- turtle.dropDown()
- else
- turtle.drop()
- end
- end
- local slotsEmptied = true
- for i = 1, 16 do
- if arg.exceptSlot1 ~= i and arg.exceptSlot2 ~= i and arg.exceptSlot3 ~= i then --miss out exceptSlots
- if turtle.getItemCount(i) > 0 then
- slotsEmptied = false
- break
- end
- end
- end
- return slotsEmptied
- end
- function findCobble()
- --dirt known, so dig down vertically for 10 blocks only to save time
- local sTime = os.time()
- local startyCoord = location:getY() --eg start at 68, go down to 58
- local endyCoord = location:getY() - 10
- local missionComplete = false
- local changeDirection = false
- local direction = "down"
- local testSlot = 0
- term.clear()
- term.setCursorPos(1, 1)
- saveToLog("findCobble: mining for cobble", true)
- down(1, 1, "coal,gravel", "findCobble")
- --cobble will be stored as item1, gravel as item2, coal as item3 or similar combo
- repeat
- --turtle.select(1)
- if direction == "down" then
- dig.digNew{self = dig, direction = "down", checkForItems = "coal,gravel", callFrom = "findCobble"}
- down(1, 1, "coal,gravel", "findCobble")
- else
- dig.digNew{self = dig, direction = "up", checkForItems = "coal,gravel", callFrom = "findCobble"}
- up(1, 1, "coal,gravel", "findCobble")
- end
- for i = 1, 4 do
- dig.digNew{self = dig, direction = "forward", checkForItems = "coal,gravel", callFrom = "findCobble"}
- turnRight(1)
- end
- if direction == "down" then --check amount of ? cobble collected
- testSlot = slot:getMostUnknownItemSlot()
- if testSlot > 0 then -- at least 1 ? cobble collected
- if turtle.getItemCount(testSlot) > 30 then --need at least 25 cobble before returning
- changeDirection = true
- end
- end
- end
- if location:getY() <= endyCoord then
- if direction == "down" then
- changeDirection = true
- end
- end
- if changeDirection then --will not be true if already direction = "up"
- saveToLog("findCobble: Cobble, gravel and others may be at y = "..location:getY().." Moving forward and returning", true)
- for i = 1, 3 do
- dig.digNew{self = dig, direction = "forward", checkForItems = "coal,gravel", callFrom = "findCobble"}
- forward(1, 1, "coal,gravel", "findCobble")
- end
- saveToLog("Moved forward 3 blocks", false)
- turtle.select(1)
- direction = "up"
- changeDirection = false
- end
- if location:getY() == startyCoord - 5 and direction == "up" then
- missionComplete = true
- end
- until missionComplete
- turnRight(2)
- --if on hillside, will have left a hole behind, so fill it
- for i = 1, 3 do
- if not turtle.detectUp() then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- forward(1, 1, "coal,gravel", "findCobble")
- end
- up(5, 1, "coal,gravel", "findCobble")
- turnRight(2) --now facing same direction as started
- --cant compare stone at this stage so return true
- --check area in front is clear
- if turtle.detect() then
- turtle.select(16)
- if turtle.dig() then
- turtle.dropUp()
- end
- end
- saveToLog("findCobble started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
- return true
- end
- function firstTree()
- local quantity = 0
- local tempyCoord = 0
- local emptySlot = 0
- local dugItem = ""
- local dugSlot = 0
- local success = false
- local isJungle = false --check if next to a jungle tree
- local isBranched = false
- local numLogs = 0
- local treeSide = ""
- local sTime = os.time()
- term.clear()
- term.setCursorPos(1,1)
- --break lower block of tree
- saveToLog("firstTree: Chopping tree base", true)
- dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
- --craft to 4 planks in slot 2
- saveToLog("firstTree: Crafting planks for fuel", true)
- craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 2, chest = "doNotUse", doSort = false}
- --use all planks as fuel
- turtle.select(slot:getItemSlot("planks"))
- saveToLog("firstTree: "..turtle.getItemCount(2).." planks used for fuel", true)
- turtle.refuel(turtle.getItemCount(2))
- slot.update{self = slot, item = "planks", delete = true}
- forward(1) --now under tree trunk - on level 0
- tempyCoord = location:getY() -- remember ground level
- saveToLog("firstTree: harvesting wood above", true)
- --original wood used to make planks, so turtle now empty, use expectedItem
- dig.digNew{self = dig, direction = "up", expectedItem = "wood", callFrom = "firstTree"}
- turtle.select(1)
- saveToLog("firstTree: checking if jungle tree", true, false)
- if turtle.compare() then
- saveToLog("firstTree: harvesting jungle tree", true, false)
- isJungle = true
- turnRight(1)
- if turtle.compare() then
- treeSide = "left" -- on left side of tree
- end
- turnLeft(2)
- if turtle.compare() then
- treeSide = "right" -- on right side of tree
- end
- turnRight(1)
- end
- if isJungle then
- saveToLog("firstTree: harvesting jungle wood")
- end
- up(1) -- 1 more wood. - on level 1
- dig.digNew{self = dig, direction = "up", callFrom = "firstTree"} -- dig wood from level 2
- --make 8 planks in slot 2 from wood in slot 1. Sorting will not be done as "doNotUse" chest
- saveToLog("firstTree: Crafting planks for crafting chest", true)
- craft{craftItem = "planks", craftQuantity = 8, sourceItem1 = "wood", destSlot = 2, chest = "doNotUse", doSort = false }
- saveToLog("firstTree: Crafting chest", true)
- craft{craftItem = "chests", craftQuantity = 1, sourceItem1 = "planks", destSlot = 2, chest = "doNotUse", doSort = false}
- down(1) --go down and get dirt - on level 0
- saveToLog("firstTree: digging first dirt into slot 3", true)
- dig.digNew{self = dig, direction = "down", slotNo = 3, expectedItem = "dirt", callFrom = "firstTree"}
- saveToLog("firstTree: Harvesting remaining first tree", true)
- --up(1) -- on level 1, wood above already dug
- if isJungle then
- dig.digNew{self = dig, expectedItem = "wood", callFrom = "firstTree"}
- for i = 1, 3 do
- dig.digNew{self = dig, callFrom = "firstTree"}
- if treeside == "left" then
- turnRight(1)
- else
- turnLeft(1)
- end
- dig.digNew{self = dig, callFrom = "firstTree"}
- if treeside == "left" then
- turnLeft(1)
- else
- turnRight(1)
- end
- forward(1)
- if treeside == "left" then
- turnRight(1)
- else
- turnLeft(1)
- end
- dig.digNew{self = dig, callFrom = "firstTree"}
- if treeside == "left" then
- turnLeft(1)
- else
- turnRight(1)
- end
- back(1)
- if i < 3 then
- up(1)
- end
- end -- on level 1
- else
- up(2) -- on level 2
- dig.digNew{self = dig, direction = "up", expectedItem = "wood", callFrom = "firstTree"}
- up(1)
- end
- --slot1 = wood, slot2 = chests, slot3 = dirt
- while turtle.detectUp() do
- --leaves at tree top could give saplings
- dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "firstTree"}
- isBranched = checkTree("wood", isBranched, "firstTree") --look for more saplings from leaves or branches
- up(1, 1, "saplings", "firstTree")
- end
- --drop down to start
- while tempyCoord < location:getY() do -- move to ground level
- down(1, 1)
- end
- if slot:getItemSlot("saplings") > 0 then
- for i = 1, 4 do --suck any saplings
- dig.digNew{self = dig, slotNo = 1, suck = true, checkForItems = "saplings", callFrom = "firstTree"} --forward, slot1 by default
- turnRight(1)
- end
- end
- saveToLog("firstTree: Crafting planks for fuel", true)
- turnRight(2) -- face starting point
- --if non branched tree, expect 4 logs, turn all into fuel, else preserve some logs
- numLogs = turtle.getItemCount(slot:getItemSlot("wood")) - 1
- if numLogs < 1 then
- saveToLog("firstTree: Only "..numLogs.." log available. Not enough to continue. Re-start at a taller tree", true)
- error()
- end
- if numLogs > 4 then
- numLogs = 4
- end
- if craft{craftItem = "planks", craftQuantity = numLogs * 4, sourceItem1 = "wood", destSlot = 0, doSort = false} then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- else
- saveToLog("firstTree: Failed to craft planks. Check if more than 1 wood available", true)
- error()
- end
- saveToLog("firstTree: Fuel level: "..turtle.getFuelLevel(), true)
- saveToLog("firstTree: harvested all wood, now at ground level x="..location:getX().." y="..location:getY().." z="..location:getZ().." facing "..location:getCompass(), false)
- saveToLog("firstTree: Fuel level: "..turtle.getFuelLevel(), true)
- turnRight(2)
- saveToLog("firstTree started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true), false)
- end
- function forward(steps, useSlot, itemList, calledFrom)
- local maxTries = 10
- useSlot = useSlot or 1
- itemList = itemList or ""
- calledFrom = calledFrom or ""
- if steps > 0 then
- for i = 1, steps do
- if turtle.detect() then
- while not turtle.forward() do
- dig.digNew{self = dig, slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
- maxTries = maxTries - 1
- if maxTries <= 0 then
- maxTries = 10
- saveToLog("forward: maxTries reached. Unable to move forward check for bedrock", true)
- break
- end
- end
- else --move unless mob in the way
- while not turtle.forward() do
- dig.digNew{self = dig, slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
- --if turtle.attack() then
- if attack() then
- saveToLog("forward: mob attacked in front!", true)
- end
- maxTries = maxTries - 1
- if maxTries <= 0 then
- if turtle.getFuelLevel() == 0 then
- if refuel(0) then
- maxTries = 10
- else
- saveToLog("forward: Out of fuel. Game Over!", true)
- error()
- end
- end
- end
- end
- end
- changeCoords("forward")
- end
- end
- end
- function forwardSafe(steps)
- local amount = 0
- local useSlot = 0
- local useItem = ""
- local success = false
- local dugItem = ""
- local dugSlot = 0
- local checkList = {}
- local startLevel = location:getY()
- checkList[1] = "wood"
- checkList[2] = "wood2"
- checkList[3] = "sand"
- checkList[4] = "item"
- saveToLog("forwardSafe("..steps..") steps started, checking fuel level", true)
- refuel(0)
- turtle.select(1)
- for i = 1, steps do --each step should move 1 block forward only
- checkDigDown() -- either goes downwards, digs sand or any unknown matching item
- if location:getY() ~= startLevel then --moved down
- while checkWater("forward") do
- up(1)
- end
- startLevel = location:getY()
- end
- checkForLooseSaplings() -- pick up loose saplings
- if turtle.detect() then
- --see if block in front is known
- useSlot, amount, useItem = compareBlock("forward", checkList, 4) --does block in front match wood,wood2,sand
- if useSlot > 0 then --block recognised, must be wood,wood2,sand, or itemX
- if useItem =="wood" or useItem == "wood2" then
- saveToLog("forwardSafe: Harvesting tree")
- harvestTree(useItem)
- elseif useItem == "sand" then
- if turtle.getItemCount(slot:getItemSlot("sand")) < 8 then
- dig.digNew{self = dig, direction = "forward", callFrom = "forwardSafe"}
- else
- checkSugarCane()
- end
- else --itemX
- dig.digNew{self = dig, direction = "forward", checkForItems = "sand,gravel", callFrom = "forwardSafe"}
- end
- else --block not recognised, - grass/stone/coal/leaves or not yet found sand, gravel, cobble, flowers, sugar cane
- --if sugar cane dug, it scatters everywhere, so check first
- dugItem = checkSugarCane() -- if dugItem = wood/wood2 then tree found, else unknown
- isTreeInFront(dugItem)
- end
- --block may be in front, so go up until space in front
- --checkDigUp() --will clear blocks above and go up or harvest tree if found
- forward(1, 1, "sand,gravel", "forwardSafe")
- else --not blocked so move forward 1 step
- forward(1, 1, "sand,gravel", "forwardSafe")
- checkForLooseSaplings()--pick up loose saplings
- end
- --1 step forward now taken
- --detect trees on both sides
- turnRight(1)
- checkForLooseSaplings()
- if turtle.detect() then
- dugItem = checkSugarCane(true)
- isTreeInFront(dugItem)
- end
- turnLeft(2)
- checkForLooseSaplings()
- if turtle.detect() then
- dugItem = checkSugarCane(true)
- isTreeInFront(dugItem)
- end
- turnRight(1)
- saveToLog("forwardSafe step "..i.." completed. Coords: "..location:getX()..", "..location:getY()..", "..location:getZ()..", "..location:getCompass())
- if turtle.getFuelLevel() < 100 then
- refuel(0) --refuel chest in already checked position
- end
- --now facing forward, moved 1 block
- sleep(0.5) --stops random "Too long without yielding" errors
- end
- saveToLog("forwardSafe finished at "..textutils.formatTime(os.time(), true))
- end
- function getCobbleStock()
- local result = 0
- if slot:getItemSlot("cobble") > 0 then
- result = turtle.getItemCount(slot:getItemSlot("cobble")) - 1
- end
- return result
- end
- function getCoords()
- --get world coordinates from player
- local coord = 0
- local response = ""
- local continue = true
- local event = ""
- local param1 = ""
- local useIniFile = false
- term.clear()
- term.setCursorPos(1,1)
- if fso:iniFileExists() then --read preferences, so only one question asked before starting
- print("Current settings:")
- print()
- print(" Playing on a server = "..tostring(fso:getOnServer()))
- print(" Create logfiles = "..tostring(fso:getUseLog()))
- print(" Use verbose logfiles = "..tostring(fso:getUseVerbose()))
- print(" Use pastebin for logfiles = "..tostring(fso:getUsePastebin()))
- print(" CC config file edited = "..tostring(fso:getConfigModified()))
- print()
- print("Press E to edit")
- print()
- print("Any other key to continue")
- event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
- term.clear()
- term.setCursorPos(1,1)
- if param1 == "e" or param1 == "E" then -- if the returned value was 'e'
- fso:deleteIniFile()
- else
- useIniFile = true
- end
- end
- if not useIniFile then -- no prefs loaded from file, or file deleted
- fso:setOnServer(false)
- fso:setUsePastebin(false)
- fso:setConfigModified(false)
- fso:setUseLog(false)
- term.clear()
- term.setCursorPos(1,1)
- print(version)
- print()
- print("Are you playing on a server")
- print("where you cannot alter the")
- print("config files? Y/N")
- event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
- if param1 == "y" or param1 == "Y" then -- if the returned value was 'y'
- fso:setOnServer(true)
- term.clear()
- term.setCursorPos(1,1)
- print("Do you want to send logfiles")
- print("to your pastebin account? Y/N")
- event, param1 = os.pullEvent ("char")
- term.clear()
- term.setCursorPos(1,1)
- if param1 == "y" or param1 == "Y" then
- print("Sorry, pastebin is work in progress")
- print("No logfiles will be produced")
- else
- print("No logfiles will be produced")
- end
- print()
- print("Preparing to start....")
- sleep(4)
- --break
- else --not on server
- if fs.getFreeSpace("/") < 9000000 then --config not modified
- while continue do
- continue = false
- print()
- print("CompterCraft.cfg file needs modifying")
- print("to allow logfiles up to 6MB in size")
- print(" Press H for help")
- print(" Press N if config can't be edited")
- print()
- print(" Press Q to quit and edit")
- event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
- if param1 == "q" or param1 == "Q" then -- if the returned value was 'q'
- term.clear()
- term.setCursorPos(1,1)
- print("Restart when config file has been changed")
- error()
- elseif param1 == "h" or param1 == "H" then -- if the returned value was 'h'
- term.clear()
- term.setCursorPos(1,1)
- print("Open this file in notepad:")
- print(".minecraft/config/ComputerCraft.cfg")
- print("In 'general' section find:")
- print("# The disk space limit for comput..")
- print("Change the following line to:")
- print("I:computerSpaceLimit=10000000")
- print("# The disk space limit for floppy..")
- print("Change the following line to:")
- print("I:floppySpaceLimit=500000")
- print("Save and re-start Minecraft")
- print()
- print("Press any key to continue")
- event, param1 = os.pullEvent ("char")
- continue = true
- --elseif param1 == "n" or param1 == "N" then
- --fso:setUseLog(false)
- --fso:setConfigModified(false)
- end
- end
- else --config modified ask if logfiles required
- fso:setConfigModified(true)
- term.clear()
- term.setCursorPos(1,1)
- print("Logfile will be created automatically")
- print()
- print("Press 'N' to prevent logfiles")
- print()
- print("Press 'M' for minimal logfiles")
- print()
- print("Any other key for default (verbose)")
- print()
- event, param1 = os.pullEvent ("char")
- if param1 == "n" or param1 == "N" then
- print("logfiles will not be created")
- elseif param1 == "m" or param1 == "M" then
- fso:setUseVerbose(false)
- fso:setUseLog(true)
- print("minimal logfiles will be created")
- else
- fso:setUseVerbose(true)
- fso:setUseLog(true)
- print("verbose logfiles will be created")
- end
- sleep(2)
- if fso:checkLogExists() then
- term.clear()
- term.setCursorPos(1,1)
- print("Logfile(s) already exist")
- print()
- print("Press 'Y' to keep existing logs")
- print("Any other key to delete logs")
- event, param1 = os.pullEvent ("char")
- if param1 == "y" or param1 == "Y" then
- fso:renameLog()
- else
- fso:deleteLog()
- end
- end
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- print("IMPORTANT! Stand directly behind turtle")
- print("Press F3 to read coordinates")
- print()
- continue = true
- while continue do
- print("Please enter your X coordinate")
- write(" x = ")
- --term.setCursorPos(term.getCursorPos())
- coord = nil
- while coord == nil do
- coord = tonumber(read())
- if coord == nil then
- term.clear()
- term.setCursorPos(1,1)
- print("Incorrect input. Use numbers only!")
- print()
- print("Please enter your X coordinate")
- write(" x = ")
- --term.setCursorPos(term.getCursorPos())
- end
- end
- location:setX(coord)
- term.clear()
- term.setCursorPos(1,1)
- print("Please enter your Y coordinate")
- write(" y = ")
- coord = nil
- while coord == nil do
- coord = tonumber(read())
- if coord == nil then
- term.clear()
- term.setCursorPos(1,1)
- print("Incorrect input. Use numbers only")
- print()
- print("Please enter your y coordinate")
- write(" y = ")
- end
- end
- location:setY(coord)
- term.clear()
- term.setCursorPos(1,1)
- print("Please enter your Z coordinate")
- write(" z = ")
- coord = nil
- while coord == nil do
- coord = tonumber(read())
- if coord == nil then
- term.clear()
- term.setCursorPos(1,1)
- print("Incorrect input. Use numbers only")
- print()
- print("Please enter your z coordinate")
- write(" z = ")
- end
- end
- location:setZ(coord)
- response = true
- while response do
- response = false
- term.clear()
- term.setCursorPos(1,1)
- print("Enter Direction you are facing:")
- print(" 0,1,2,3 (s,w,n,e)")
- print()
- print( " Direction = ")
- event, param1 = os.pullEvent ("char")
- --response = read()
- if param1 == "s" or param1 == "S" then
- coord = 0
- elseif param1 == "w" or param1 == "W" then
- coord = 1
- elseif param1 == "n" or param1 == "N" then
- coord = 2
- elseif param1 == "e" or param1 == "E" then
- coord = 3
- elseif param1 == "0" or param1 == "1" or param1 == "2" or param1 == "3" then
- coord = tonumber(param1)
- else
- print()
- print("Incorrect input: "..param1)
- print()
- print("Use 0,1,2,3,n,s,w,e")
- sleep(2)
- response = true
- end
- end
- location:setFacing(coord)
- term.clear()
- term.setCursorPos(1,1)
- print("Your current location is:")
- print()
- print(" x = "..location:getX())
- print(" y = "..location:getY())
- print(" z = "..location:getZ())
- print(" facing "..location:getCompass().." ("..location:getFacing()..")")
- print()
- write("Is this correct? (y/n)")
- event, param1 = os.pullEvent ("char")
- if param1 == "y" or param1 == "Y" then
- continue = false
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- --[[ correct coords to compensate for player standing position
- facing: Change:
- 0 (S) z+1
- 1 (W) x-1
- 2 (N) z-1
- 3 (E) x+1
- ]]--
- if location:getFacing() == 0 then
- location:setZ(location:getZ() + 1)
- elseif location:getFacing() == 1 then
- location:setX(location:getX() - 1)
- elseif location:getFacing() == 2 then
- location:setZ(location:getZ() - 1)
- elseif location:getFacing() == 3 then
- location:setX(location:getX() + 1)
- end
- coordHome:setX(location:getX())
- coordHome:setY(location:getY())
- coordHome:setZ(location:getZ())
- coordHome:setFacing(location:getFacing())
- startTime = os.time()
- saveToLog("Starting replication at x="..location:getX().." y="..location:getY().." z="..location:getZ().." facing "..location:getCompass(), true)
- end
- function getDirtStock()
- local result = 0
- if slot:getItemSlot("dirt") > 0 then
- result = turtle.getItemCount(slot:getItemSlot("dirt")) - 1
- end
- return result
- end
- function getFirstEmptySlot(doSort)
- local checkSlot = 0
- local lastFullSlot = 0
- if doSort == nil then
- doSort = false
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- if checkSlot == 0 then
- checkSlot = i
- end
- else
- lastFullSlot = i
- end
- end
- if lastFullSlot + 1 ~= checkSlot and doSort then
- saveToLog("getFirstEmptySlot: first empty = "..checkSlot.." lastFullSlot = "..lastFullSlot)
- sortInventory(true)
- checkSlot = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- if checkSlot == 0 then
- checkSlot = i
- break
- end
- end
- end
- end
- return checkSlot
- end
- function getEmptyStorage()
- local emptyStore = {}
- local firstEmptyBlock = 0
- for k = 1, 9, 2 do
- if k == 1 then
- emptyStore = extraStorage1
- elseif k == 3 then
- emptyStore = extraStorage2
- elseif k == 5 then
- emptyStore = extraStorage3
- elseif k == 7 then
- emptyStore = extraStorage4
- elseif k == 9 then
- emptyStore = extraStorage5
- end
- if emptyStore:getStoreContains() == "" then
- firstEmptyBlock = k
- break
- end
- end
- return firstEmptyBlock, emptyStore
- end
- function getFuelAvailable()
- local tFuelAvailable = 0
- local fAvailableCoal = 0
- local fAvailableCharcoal = 0
- local fAvailablePlanks = 0
- local fAvailablePlanks2 = 0
- local fAvailableWood = 0
- local fAvailableWood2 = 0
- local fFuelAvailableAllWood = 0
- local result = {}
- if slot:getItemSlot("coal") > 0 then
- fAvailableCoal = (turtle.getItemCount(slot:getItemSlot("coal")) - 1) * 80
- end
- if slot:getItemSlot("charcoal") > 0 then
- fAvailableCharcoal = (turtle.getItemCount(slot:getItemSlot("charcoal"))) * 80
- end
- if slot:getItemSlot("planks") > 0 then
- fAvailablePlanks = (turtle.getItemCount(slot:getItemSlot("planks"))) * 15
- end
- if slot:getItemSlot("planks2") > 0 then
- fAvailablePlanks2 = (turtle.getItemCount(slot:getItemSlot("planks2"))) * 15
- end
- if slot:getItemSlot("wood") > 0 then
- fAvailableWood = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 60
- end
- if slot:getItemSlot("wood2") > 0 then
- fAvailableWood2 = (turtle.getItemCount(slot:getItemSlot("wood2")) - 1) * 60
- end
- fFuelAvailableAllWood = fAvailableWood + fAvailableWood2
- tFuelAvailable = fAvailableCoal + fAvailableCharcoal + fAvailablePlanks + fAvailablePlanks2 + fAvailableWood + fAvailableWood2
- saveToLog("getFuelAvailable: coal = "..fAvailableCoal..
- " charcoal = "..fAvailableCharcoal..
- " planks = "..fAvailablePlanks..
- " planks2 = "..fAvailablePlanks2..
- " wood = "..fAvailableWood..
- " wood2 = "..fAvailableWood2, false, true)
- result = {totalFuelAvailable = tFuelAvailable,
- fuelAvailableCoal = fAvailableCoal,
- fuelAvailableCharcoal = fAvailableCharcoal,
- fuelAvailablePlanks = fAvailablePlanks,
- fuelAvailablePlanks2 = fAvailablePlanks2,
- fuelAvailableWood = fAvailableWood,
- fuelAvailableWood2 = fAvailableWood2,
- fuelAvailableAllWood = fFuelAvailableAllWood}
- return result
- end
- function getItemsFromStorage(arg)
- -- getItemsFromStorage{fromStore = storageIronore, item1 = "ironore", item2 = "buckets"}
- -- getItemsFromStorage{fromStore = storageTorches, itemList = getList} -- eg 'sticks,torches'
- -- getItemsFromStorage{fromStore = storagePickaxe, item1 = "diamond pickaxe"} -- eg 'sticks,torches'
- local itemInStorage = false
- local storeSlot = 0
- local storeCount = 0
- local storedItem = ""
- local emptySlot = 0
- local keepItems = {}
- local returnItems = {}
- local numItems = -1
- local logText = ""
- local tempChestItem = {}
- local tempIndex = 0
- local useCraftingChest = false
- local keepThis = false
- local useItem = ""
- local existingItem = false
- local start = 0
- local length = 0
- --put crafting chest in front if not enough slots for stored items, remove items from storage one by one, either to keep, or transfer to crafting chest
- --when required items are retrieved, put excess back into storage chest
- if arg.itemList ~= nil then
- arg.item1 = arg.itemList[1]
- arg.item2 = arg.itemList[2]
- arg.item3 = arg.itemList[3]
- saveToLog("getItemsFromStorage: itemList[1] = "..tostring(arg.itemList[1]).." itemList[2] = "..tostring(arg.itemList[2]).." itemList[3] = "..tostring(arg.itemList[3]))
- else
- -- get no of items
- for key, value in pairs(arg) do
- if key == "fromStore" then
- saveToLog("getItemsFromStorage: key = "..key.." value = "..value.getStoreName(value), false)
- else
- saveToLog("getItemsFromStorage: key = "..key.." value = "..tostring(value), false)
- end
- --key = "fromStore", value = storageTorches
- --key = "item1", value = "sticks"
- --key = "itemList", value = ',sticks,torches'
- numItems = numItems + 1 --starts at -1 minimum 2 args
- end
- end
- keepItems[arg.item1] = 0 --default value of return
- if arg.item2 ~= nil then
- keepItems[arg.item2] = 0
- end
- if arg.item3 ~= nil then
- keepItems[arg.item3] = 0
- end
- saveToLog("getItemsFromStorage: checking for "..numItems.." items out of "..arg.fromStore.getNoOfItems(arg.fromStore))
- storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item1)
- --check if items needed are in storage
- if storeSlot > 0 then
- storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
- saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item1.." in storage slot "..storeSlot)
- itemInStorage = true
- end
- if arg.item2 ~= nil and arg.item2 ~= "" then
- storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item2)
- if storeSlot > 0 then
- storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
- saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item2.." in storage slot "..storeSlot)
- itemInStorage = true
- end
- end
- if arg.item3 ~= nil and arg.item3 ~= "" then
- storeSlot = arg.fromStore.getItemSlot(arg.fromStore, arg.item3)
- if storeSlot > 0 then
- storeCount = arg.fromStore.getSlotCount(arg.fromStore, storeSlot)
- saveToLog("getItemsFromStorage: found "..storeCount.." of "..arg.item3.." in storage slot "..storeSlot)
- itemInStorage = true
- end
- end
- if itemInStorage then
- emptySlot = 16
- --place crafting chest if needed
- if arg.fromStore.getNoOfItems(arg.fromStore) > getNoOfEmptySlots() then
- turtle.select(slot.getItemSlot(slot, "chests"))
- turtle.place()
- saveToLog("getItemsFromStorage: crafting chest placed ready for "..arg.fromStore.getNoOfItems(arg.fromStore).." items")
- useCraftingChest = true
- else
- saveToLog("getItemsFromStorage: crafting chest not required ("..arg.fromStore.getNoOfItems(arg.fromStore)..") items in storage")
- end
- for i = 1, 16 do
- --use storeContains NOT slotContains
- storedItem = arg.fromStore:getSlotContains(i) --eg "sticks"
- if arg.item1 == storedItem or arg.item2 == storedItem or arg.item3 == storedItem then
- keepThis = true
- else
- keepThis = false
- end
- storeCount = arg.fromStore:getSlotCount(i) --eg 1
- if storeCount == nil then
- storeCount = 0
- end
- if storeCount > 0 then
- saveToLog("getItemsFromStorage: found "..storeCount.." of "..storedItem.." in storage")
- existingItem = false
- if slot:getItemSlot(storedItem) > 0 then --item already in turtle
- emptySlot = slot:getItemSlot(storedItem)
- keepThis = true
- existingItem = true
- end
- turtle.select(emptySlot)
- saveToLog("getItemsFromStorage: Removing "..storeCount.." "..storedItem.." from storage chest into turtle slot "..emptySlot)
- if turtle.suckDown() then --removed item from storage
- saveToLog("getItemsFromStorage: removed "..turtle.getItemCount(emptySlot).." "..storedItem)
- if keepThis then
- if not existingItem then
- keepItems[storedItem] = turtle.getItemCount(emptySlot)
- saveToLog("getItemsFromStorage: "..keepItems[storedItem].." of "..storedItem.." retrieved. Updating slot")
- emptySlot = getFirstEmptySlot(false)
- if emptySlot > 0 then --turtle not full
- turtle.select(16)
- turtle.transferTo(emptySlot)
- else
- emptySlot = 16
- end
- end
- slot.update{self = slot, slotNo = emptySlot, item = storedItem}
- else --not required, place in crafting chest if present
- if useCraftingChest then
- tempIndex = tempIndex + 1
- fillChest{direction = "forward", addItem = storedItem, addAmount = storeCount, fromSlot = 16, originalSlot = tempIndex, deleteFromTurtle = false}
- else
- tempIndex = tempIndex + 1 --started at 0, so first =1
- returnItems[tempIndex] = storedItem --returnItems[1] = "iron"
- saveToLog("getItemsFromStorage: "..returnItems[tempIndex].." retrieved. Updating slot before returning to storage")
- emptySlot = getFirstEmptySlot(false)
- if emptySlot > 0 then --turtle not full
- turtle.select(16)
- turtle.transferTo(emptySlot)
- else
- emptySlot = 16
- end
- slot.update{self = slot, slotNo = emptySlot, item = storedItem}
- end
- end
- emptySlot = 16
- end
- else
- break
- end
- end
- --reset storage variables
- saveToLog("getItemsFromStorage: resetting storage object", false)
- arg.fromStore.resetVariables(arg.fromStore)
- -- now remove items from temp chest/turtle back into storage
- if tempIndex > 0 then
- if useCraftingChest then
- for i = 1, tempIndex do
- useItem = craftingChest:getSlotContains(i) --eg "dirt", "item"
- turtle.select(16)
- if turtle.suck() then --item removed from crafting chest into slot 16
- saveToLog("getItemsFromStorage: returning "..useItem.." to ".. arg.fromStore:getStoreName())
- storeItem{toStore = arg.fromStore, item = useItem, slotNo = 16, quantity = turtle.getItemCount(16)}
- else
- break
- end
- end
- --reset chest variables
- craftingChest:resetVariables()
- --dig chest back. may not be a chest onboard
- dig.digNew{self = dig, direction = "forward", expectedItem = "chests", callFrom = "getItemsFromStorage"}
- else
- for i = 1, tempIndex do
- saveToLog("getItemsFromStorage: returning "..returnItems[i].." to "..arg.fromStore:getStoreName())
- storeItem{toStore = arg.fromStore, item = returnItems[i], quantity = 0, updateSlot = true, doSort = true}
- end
- end
- end
- sortInventory(true)
- else
- saveToLog("getItemsFromStorage: itemInStorage = false")
- end
- return keepItems
- end
- function getItemsNeeded(stage)
- --itemsNeeded, numNeeded = getItemsNeeded(3)
- local needed = {}
- local index = 0
- if stage == 1 then
- if slot:getItemSlot("dirt") == 0 then
- index = index + 1
- needed[index] = "dirt"
- end
- if slot:getItemSlot("cobble") == 0 then
- index = index + 1
- needed[index] = "cobble"
- end
- if not objectives["ironore"] then
- index = index + 1
- needed[index] = "ironore"
- end
- if not objectives["gravel"] then
- index = index + 1
- needed[index] = "gravel"
- end
- if not objectives["sand"] then
- index = index + 1
- needed[index] = "sand"
- end
- if slot:getItemSlot("coal") == 0 then
- index = index + 1
- needed[index] = "coal"
- end
- end
- if stage == 2 then
- if slot:getItemSlot("coal") == 0 then
- index = index + 1
- needed[index] = "coal"
- end
- if not objectives["ironore"] then
- index = index + 1
- needed[index] = "ironore"
- end
- end
- if stage == 3 then
- --[[if not objectives["goldore"] then
- index = index + 1
- needed[index] = "goldore"
- end]]--
- if not objectives["redstone"] then
- index = index + 1
- needed[index] = "redstone"
- end
- if not objectives["lapis"] then
- index = index + 1
- needed[index] = "lapis"
- end
- if not objectives["diamonds"] then
- index = index + 1
- needed[index] = "diamonds"
- end
- end
- return needed, index --eg {"coal", "ironore"}, 2
- end
- function getNoOfEmptySlots(excludeTorches)
- local result = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- result = result + 1
- end
- end
- if excludeTorches then
- if slot:getItemSlot("torches") > 0 then
- result = result - 1
- end
- if slot:getItemSlot("signs") > 0 then
- result = result - 1
- end
- end
- return result
- end
- function getPlanksAvailable(checkPlanksType)
- local planksAvailable = 0
- local planks2Available = 0
- local planksType = "planks"
- if checkPlanksType == nil then
- checkPlanksType = ""
- end
- if slot:getItemSlot("planks") > 0 then
- planksAvailable = turtle.getItemCount(slot:getItemSlot("planks"))
- end
- if slot:getItemSlot("planks2") > 0 then
- planks2Available = turtle.getItemCount(slot:getItemSlot("planks2"))
- end
- if checkPlanksType == "planks" then
- planksType = "planks"
- elseif checkPlanksType == "planks2" then
- planksType = "planks2"
- planksAvailable = planks2Available
- else
- if planks2Available > planksAvailable then
- planksType = "planks2"
- planksAvailable = planks2Available
- end
- end
- -- return amount available if type specified
- -- else return planks type largest amount
- return planksAvailable, planksType
- end
- function getItemsAvailable(itemName, recover)
- -- getItemsAvailable("signs", true)
- local onBoard = 0
- local inStore = 0
- recover = recover or false
- if itemName == "torches" then
- if slot:getItemSlot("torches") > 0 then
- onBoard = turtle.getItemCount(slot:getItemSlot("torches"))
- end
- inStore = storageTorches:getItemCount("torches")
- if recover and inStore > 0 then
- inStore = 0
- getItemsFromStorage{fromStore = storageTorches, item1 = "torches"}
- if slot:getItemSlot("torches") > 0 then
- onBoard = turtle.getItemCount(slot:getItemSlot("torches"))
- end
- end
- elseif itemName == "signs" then
- if slot:getItemSlot("signs") > 0 then
- onBoard = turtle.getItemCount(slot:getItemSlot("signs"))
- end
- inStore = storageSigns:getItemCount("signs")
- if recover and inStore > 0 then
- forward(2, 1)
- turnRight(2)
- inStore = 0
- getItemsFromStorage{fromStore = storageSigns, item1 = "signs"}
- if slot:getItemSlot("signs") > 0 then
- onBoard = turtle.getItemCount(slot:getItemSlot("signs"))
- end
- forward(2, 1)
- turnRight(2)
- end
- elseif itemName == "sticks" then
- if slot:getItemSlot("sticks") > 0 then
- onBoard = turtle.getItemCount(slot:getItemSlot("sticks"))
- end
- inStore = storageSticks:getItemCount("sticks")
- if recover and inStore > 0 then
- forward(4, 1)
- turnRight(2)
- inStore = 0
- getItemsFromStorage{fromStore = storageSticks, item1 = "sticks"}
- if slot:getItemSlot("sticks") > 0 then
- onBoard = turtle.getItemCount(slot:getItemSlot("sticks"))
- end
- forward(4, 1)
- turnRight(2)
- end
- end
- return onBoard + inStore
- end
- function getWoodAvailable(arg)
- local woodAvailable = 0
- local wood2Available = 0
- local mostWoodType = "none"
- -- getWoodAvailable{woodNeeded = 2} --checks if at least 2 wood of any type available
- arg.checkWoodType = arg.checkWoodType or "any"
- arg.woodNeeded = arg.woodNeeded or 0
- if slot:getItemSlot("wood") > 0 then --wood onboard
- woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 1
- if slot:getItemSlot("wood2") > 0 then
- wood2Available = turtle.getItemCount(slot:getItemSlot("wood2")) - 1
- end
- if arg.woodNeeded > 0 then --checking if enough wood for the purpose
- if arg.checkWoodType == "any" then
- if woodAvailable >= arg.woodNeeded then
- mostWoodType = "wood"
- else
- if wood2Available >= arg.woodNeeded then
- mostWoodType = "wood2"
- end
- end
- elseif arg.checkWoodType == "wood" then --check specific wood
- if woodAvailable >= arg.woodNeeded then
- mostWoodType = "wood"
- end
- elseif arg.checkWoodType == "wood2" then --check specific wood2
- if wood2Available >= arg.woodNeeded then
- mostWoodType = "wood2"
- end
- end
- else --no specific amount required
- if arg.checkWoodType == "wood" then
- if woodAvailable >= 1 then
- mostWoodType = "wood"
- end
- elseif arg.checkWoodType == "wood2" then
- if wood2Available >= 1 then
- mostWoodType = "wood2"
- end
- else --woodType not specified
- if woodAvailable >= 1 then
- mostWoodType = "wood"
- end
- if wood2Available > woodAvailable then
- mostWoodType = "wood2"
- end
- end
- end
- else --wood may be in storageWood
- if storageWood:getItemSlot("wood") > 0 then --wood in store
- mostWoodType = "store"
- woodAvailable = storageWood:getItemCount("wood")
- else
- mostWoodType = "none"
- end
- end
- return mostWoodType, woodAvailable, wood2Available -- ("none","wood","wood2", "store"), X, 0
- end
- function goMining1(direction, fuelLevel, mineExitY)
- local continue = true
- local numNeeded = 0
- local itemsNeeded = {}
- local success = false
- local inWater = false
- local useSlot = 0
- local itemList = ""
- local dugItem = ""
- local dugSlot = 0
- objectives["goMining1"] = true
- saveToLog("goMining1 total fuel available = "..fuelLevel, true, false)
- if fuelLevel > 100 then
- yCoordMine = location:getY()
- itemsNeeded, numNeeded = getItemsNeeded(1) --eg {"gravel","coal"}, 2
- if numNeeded > 0 then
- for i = 1, numNeeded do
- if i == 1 then
- itemList = itemList.."'"..itemsNeeded[i].."'"
- else
- itemList = itemList..",'"..itemsNeeded[i].."'"
- end
- saveToLog("First stage mining, item "..i.." needed = "..itemsNeeded[i])
- end
- else
- saveToLog("First stage mining, no items needed")
- end
- --eg itemList = 'gravel','coal','ironore'
- saveToLog("goMining1: going down...")
- --start at ground level
- for i = 1, 2 do
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "goMining1"}
- --could be on corner next to water, and water now pouring in
- down(1, 1)
- for i = 1, 4 do
- turtle.select(slot:getItemSlot("dirt"))
- if not turtle.detect() then --air or water next door
- turtle.place()
- else --check if dirt
- if not turtle.compare() then --not dirt in front
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.compare() then --not cobble in front
- dig.digNew{self = dig, checkForItems = itemList, callFrom = "goMining1"}
- turtle.select(slot:getItemSlot("dirt"))
- turtle.place()
- end
- end
- end
- turnRight(1)
- end
- end
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "goMining1"}
- down(1, 1, itemList, "goMining1")
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- -- go down to level 40 only
- -- need to have or find: dirt, cobble, (stone), gravel, sand, coal, iron ore, gold ore
- refuel(0)
- --repair damage after refuel
- for i = 1, 4 do
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.detect() then --air or water next door
- turtle.place()
- end
- turnRight(1)
- end
- saveToLog("Commencing mining run")
- --sortInventory(true)
- mineDown(40, itemList, "goMining1") --mine down to level 40
- sortInventory(true)
- saveToLog("goMining1: lastNewItemSlot = "..slot.getMostUnknownItemSlot(slot)..", firstNewItemSlot = "..slot.getLeastUnknownItemSlot(slot)..", numNeeded = "..numNeeded)
- repositionUnderground("FFF", itemList, "goMining1")
- saveToLog("goMining1: going up...", false, true)
- --mineUp(yCoordMine - 5, itemList, "goMining1")
- mineUp(mineExitY, itemList, "goMining1")
- success = true
- else
- saveToLog("goMining1: insufficient fuel. Abandoning mining run")
- end
- return success
- end
- function goMining2(level, sector)
- -- start top middle, go back towards first tree, do top right quarter
- -- start top middle, go back towards first tree, do top left quarter
- -- etc, return to base to replant tree farm/ process materials/ reset torch
- local continue = true
- local numNeeded = 0
- local itemsNeeded = {}
- local turnDirection = ""
- local itemList = ""
- local digSuccess = false
- local digItem = ""
- local digSlot = 0
- local sTime = os.time()
- --[[
- 4|3|1|2
- -|-|-|-
- 8|7|5|6
- ]]--
- --need to find redstone, lapis, (obsidian), diamond, emerald
- --go down to level 15, dig 16X8 rectangle, by digging down on 14, digging forward on 15, placing ceiling on 16
- itemsNeeded, numNeeded = getItemsNeeded(3) --eg {"diamond","redstone"}, 2
- saveToLog("goMining2: no. items needed = "..numNeeded)
- if numNeeded > 0 then
- for i = 1, numNeeded do
- if i == 1 then
- itemList = itemList.."'"..tostring(itemsNeeded[i]).."'"
- else
- itemList = itemList..",'"..tostring(itemsNeeded[i]).."'"
- end
- saveToLog("Third stage mining, item "..i.." needed = "..itemsNeeded[i])
- end
- end
- while location:getY() > 14 do
- down(1, 1, itemList)
- end
- forward(16, 1)
- if level == 2 then
- down(3, 1, itemList)
- elseif level == 3 then
- down(6, 1, itemList)
- end
- if sector == 1 then
- turnLeft(1)
- forward(1)
- turnLeft(1)
- turnDirection = "right"
- elseif sector == 2 then
- turnLeft(1)
- forward(9, 1)
- turnLeft(1)
- turnDirection = "right"
- elseif sector == 3 then
- turnRight(1)
- forward(1)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 4 then
- turnRight(1)
- forward(9)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 5 then
- turnLeft(1)
- forward(1)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 6 then
- turnLeft(1)
- forward(9, 1)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 7 then
- turnRight(1)
- forward(1)
- turnLeft(1)
- turnDirection = "right"
- elseif sector == 8 then
- turnRight(1)
- forward(9, 1)
- turnLeft(1)
- turnDirection = "right"
- end
- --eg itemList = 'redstone','ironore','diamonds'
- -- now dig 8 x 16 rectangle
- for sq = 1, 8 do
- refuel(0)
- for i = 1, 16 do
- if turtle.detectUp() then --block above
- if isValuable("up") then --could be useful, so dig and replace with cobble
- --digSuccess, digItem, digSlot = dig{direction = "up", checkForItems = itemList}
- while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "goMining2"} do --allow for gravel
- sleep(.5)
- if not turtle.detectUp() then --water or lava falling down
- break
- end
- end
- turtle.select(slot:getItemSlot("cobble"))
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- turtle.placeUp()
- end
- end
- else --no block above or lava/water, so place cobble
- turtle.select(slot:getItemSlot("cobble"))
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- turtle.placeUp()
- end
- end
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "goMining2"}
- else --nothing below could be air, water, lava
- if slot:getItemSlot("buckets") > 0 then
- turtle.select(slot:getItemSlot("buckets"))
- if turtle.placeDown() then --bucket filled water or lava
- if not turtle.refuel() then --water
- turtle.placeDown() --empty bucket out
- end
- end
- end
- end
- if sq == 8 and (i == 9 or i == 1) then
- if slot:getItemCount("torches") > 1 then
- turtle.select(slot:getItemSlot("torches"))
- if not turtle.placeDown() then
- turtle.select(slot:getItemSlot("cobble"))
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- down(1)
- turtle.placeDown()
- up(1)
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- end
- end
- end
- end
- if turtle.detect() then --dig and drop non-valuables
- while dig.digNew{self = dig, checkForItems = itemList, callFrom = "goMining2"} do
- sleep(.5)
- if not turtle.detect() then --water or lava falling down
- break
- end
- end
- end
- forward(1, 1, itemList)
- end
- if sq < 8 then
- if turnDirection == "right" then
- turnRight(1)
- mineForward(1, true, itemList)
- turnRight(1)
- turnDirection = "left"
- else
- turnLeft(1)
- mineForward(1, true, itemList)
- turnLeft(1)
- turnDirection = "right"
- end
- else
- if sector == 1 then
- turnRight(1)
- forward(8, 1)
- turnRight(1)
- elseif sector == 2 then
- turnRight(1)
- forward(16, 1)
- turnRight(1)
- elseif sector == 3 then
- turnLeft(1)
- forward(8, 1)
- turnLeft(1)
- elseif sector == 4 then
- turnLeft(1)
- forward(16, 1)
- turnLeft(1)
- elseif sector == 5 then
- --forward(1)
- turnLeft(1)
- forward(8, 1)
- turnRight(1)
- elseif sector == 6 then
- --forward(1)
- turnLeft(1)
- forward(16, 1)
- turnRight(1)
- elseif sector == 7 then
- --forward(1)
- turnRight(1)
- forward(8, 1)
- turnLeft(1)
- elseif sector == 8 then
- --forward(1)
- turnRight(1)
- forward(16, 1)
- turnLeft(1)
- end
- end
- end
- -- 8x16 rectangle mined out, now go home
- forward(16, 1)
- while location:getY() < mineEntrance:getY() do
- up(1, 1)
- end
- turnLeft(2) --over mine entrance, facing furnace
- forward(4, 1) --over storageRedstone
- storeRedstone()
- forward(1, 1) -- over markerSand
- storeUnknownItem("sand")
- forward(4, 1) -- over markerWood
- storeUnknownItem("wood")
- forward(4, 1) -- over markerSigns
- storeUnknownItem("signs")
- forward(2, 1) -- over markerTorches
- storeUnknownItem("torches")
- forward(1, 1)
- turnRight(2)
- saveToLog("goMining2 started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
- sortInventory(true)
- --should end with gold redstone, maybe lapis, diamond if lucky
- end
- function goMining3(sector)
- -- start top middle, go back towards first tree, do top right quarter
- -- start top middle, go back towards first tree, do top left quarter
- -- etc, return to base to replant tree farm/ process materials/ reset torch
- local numNeeded = 0
- local itemsNeeded = {}
- local turnDirection = ""
- local itemList = ""
- local digSuccess = false
- local digItem = ""
- local digSlot = 0
- local torchesAt = {}
- local sTime = os.time()
- --[[
- 4|3|1|2
- -|-|-|-
- 8|7|5|6
- ]]--
- --need to find redstone, lapis, (obsidian), diamond, emerald
- --go down to level 15, dig 16X8 rectangle, by digging down on 14, digging forward on 15, placing ceiling on 16
- itemsNeeded, numNeeded = getItemsNeeded(3) --eg {"diamond","redstone"}, 2
- saveToLog("goMining3: no. items needed = "..numNeeded)
- if numNeeded > 0 then
- for i = 1, numNeeded do
- if i == 1 then
- itemList = itemList.."'"..tostring(itemsNeeded[i]).."'"
- else
- itemList = itemList..",'"..tostring(itemsNeeded[i]).."'"
- end
- saveToLog("Third stage mining, item "..i.." needed = "..itemsNeeded[i])
- end
- end
- while location:getY() > 14 do
- down(1, 1, itemList)
- end
- forward(16, 1)
- down(7, 1)
- for i = 1, 33 do
- torchesAt[i] = false
- end
- -- now under far side of previous mining area dig 33 x 2 corridor
- torchesAt[1] = true
- torchesAt[9] = true
- torchesAt[17] = true
- torchesAt[25] = true
- torchesAt[33] = true
- if sector == 1 then
- turnLeft(1)
- forward(1)
- turnLeft(1)
- turnDirection = "right"
- elseif sector == 2 then
- turnLeft(1)
- forward(9, 1)
- turnLeft(1)
- turnDirection = "right"
- elseif sector == 3 then
- turnRight(1)
- forward(1)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 4 then
- turnRight(1)
- forward(9)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 5 then
- turnLeft(1)
- forward(1)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 6 then
- turnLeft(1)
- forward(9, 1)
- turnRight(1)
- turnDirection = "left"
- elseif sector == 7 then
- turnRight(1)
- forward(1)
- turnLeft(1)
- turnDirection = "right"
- elseif sector == 8 then
- turnRight(1)
- forward(9, 1)
- turnLeft(1)
- turnDirection = "right"
- end
- --eg itemList = 'redstone','ironore','diamonds'
- -- now dig 8 x 16 rectangle
- for sq = 1, 8 do
- for i = 1, 17 do
- if sq == 8 then --torches needed here
- torchesAt[1] = true
- torchesAt[9] = true
- torchesAt[17] = true
- if i == 1 or i == 9 or i == 17 then
- up(1)
- dig.digNew{self = dig, callFrom = "goMining3"}
- down(1)
- end
- else
- torchesAt[1] = false
- torchesAt[9] = false
- torchesAt[17] = false
- end
- mineToBedrock(torchesAt[i]) --goes down and returns, digs out blocks in front
- if i < 17 then
- forward(1, 1)
- end
- end
- if slot:getItemSlot("coal") > 0 then
- if turtle.getItemCount(slot:getItemSlot("coal")) > 1 then
- saveToLog("goMining3: fuel level = "..turtle.getFuelLevel().." using coal")
- turtle.select(slot:getItemSlot("coal"))
- turtle.refuel(turtle.getItemCount(slot:getItemSlot("coal")) - 1)
- slot.update{self = slot, item = "coal"}
- saveToLog("goMining3: fuel level = "..turtle.getFuelLevel().." after using available coal")
- end
- end
- if sq < 8 then
- torchesAt[1] = false
- torchesAt[9] = false
- torchesAt[17] = false
- if turnDirection == "right" then
- turnRight(1)
- mineToBedrock(torchesAt[i])
- forward(1,1)
- turnRight(1)
- turnDirection = "left"
- else
- turnLeft(1)
- up(1)
- dig.digNew{self = dig, callFrom = "goMining3"}
- down(1)
- mineToBedrock(torchesAt[i])
- forward(1,1)
- turnLeft(1)
- turnDirection = "right"
- end
- else
- torchesAt[1] = true
- torchesAt[9] = true
- torchesAt[17] = true
- mineToBedrock(true)
- if sector == 1 then
- turnRight(1)
- forward(8, 1)
- turnRight(1)
- elseif sector == 2 then
- turnRight(1)
- forward(16, 1)
- turnRight(1)
- elseif sector == 3 then
- turnLeft(1)
- forward(8, 1)
- turnLeft(1)
- elseif sector == 4 then
- turnLeft(1)
- forward(16, 1)
- turnLeft(1)
- elseif sector == 5 then
- --forward(1)
- turnLeft(1)
- forward(8, 1)
- turnRight(1)
- elseif sector == 6 then
- --forward(1)
- turnLeft(1)
- forward(16, 1)
- turnRight(1)
- elseif sector == 7 then
- --forward(1)
- turnRight(1)
- forward(8, 1)
- turnLeft(1)
- elseif sector == 8 then
- --forward(1)
- turnRight(1)
- forward(16, 1)
- turnLeft(1)
- end
- end
- end
- -- 8x16 rectangle mined out, now go home
- forward(16, 1)
- while location:getY() < mineEntrance:getY() do
- up(1, 1)
- end
- turnLeft(2) --over mine entrance, facing furnace
- forward(4, 1) --over storageRedstone
- storeRedstone()
- forward(1, 1) -- over markerSand
- storeUnknownItem("sand")
- forward(4, 1) -- over markerWood
- storeUnknownItem("wood")
- forward(4, 1) -- over markerSigns
- storeUnknownItem("signs")
- forward(2, 1) -- over markerTorches
- storeUnknownItem("torches")
- forward(1, 1)
- turnRight(2)
- saveToLog("goMining3 started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
- sortInventory(true)
- --should end with gold redstone, maybe lapis, diamond if lucky
- end
- function harvestAllTrees()
- local sTime = os.time()
- local continue = true
- local colNo = 0
- local torchesNeeded = 0
- local mineshaftIndex = 0
- local logText = ""
- term.clear()
- term.setCursorPos(1, 1)
- --long code version - easier to debug. For/next loop version at end of this function.
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestRight1.txt")
- colNo = 1 --first harvest run. Unlikely enough resources to make torches etc. on this run
- mineshaftIndex = 1
- direction = "right"
- torchesNeeded = restoreTorches("rightside", 0) --check for torches and signs before starting
- if checkObjectives(colNo, "rightside", torchesNeeded) then
- continue = false
- end
- harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 1/2
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestRight2.txt")
- colNo = 3
- mineshaftIndex = 2
- direction = "right"
- torchesNeeded = restoreTorches("rightside", 3) --check for torches and signs before starting
- if checkObjectives(colNo, "rightside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 3, "rightside") -- move to correct starting position
- harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 3/4
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestRight3.txt")
- colNo = 5
- mineshaftIndex = 3
- direction = "left"
- torchesNeeded = restoreTorches("rightside", 6) --check for torches and signs before starting
- if checkObjectives(colNo, "rightside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 6, "rightside") -- move to correct starting position
- harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 5/6
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestRight4.txt")
- colNo = 7
- mineshaftIndex = 4
- direction = "right"
- torchesNeeded = restoreTorches("rightside", 9) --check for torches and signs before starting
- if checkObjectives(colNo, "rightside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 9, "rightside") -- move to correct starting position
- harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 7/8
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestRight5.txt")
- colNo = 9
- mineshaftIndex = 5
- direction = "left"
- torchesNeeded = restoreTorches("rightside", 12) --check for torches and signs before starting
- if checkObjectives(colNo, "rightside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 12, "rightside") -- move to correct starting position
- harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 9/10
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestRight6.txt")
- colNo = 11
- mineshaftIndex = 6
- direction = "right"
- torchesNeeded = restoreTorches("rightside", 15) --check for torches and signs before starting
- if checkObjectives(colNo, "rightside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 15, "rightside") -- move to correct starting position
- harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area run 11/12
- --Debug use only:
- --REMOVE
- --continue = true
- --/Debug use
- if continue then
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestLeft1.txt")
- colNo = 3
- mineshaftIndex = 7
- direction = "right"
- torchesNeeded = restoreTorches("leftside", 3) --check for torches and signs before starting
- if checkObjectives(colNo, "leftside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 3, "leftside") -- move to correct starting position
- harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 1/2
- if continue then
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestLeft2.txt")
- colNo = 5
- mineshaftIndex = 8
- direction = "left"
- torchesNeeded = restoreTorches("leftside", 6) --check for torches and signs before starting
- if checkObjectives(colNo, "leftside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 6, "leftside") -- move to correct starting position
- harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 3/4
- end
- if continue then
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestLeft3.txt")
- colNo = 7
- mineshaftIndex = 9
- direction = "right"
- torchesNeeded = restoreTorches("leftside", 9) --check for torches and signs before starting
- if checkObjectives(colNo, "leftside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 9, "leftside") -- move to correct starting position
- harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 5/6
- end
- if continue then
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logHarvestLeft4.txt")
- colNo = 9
- mineshaftIndex = 10
- direction = "left"
- torchesNeeded = restoreTorches("leftside", 12) --check for torches and signs before starting
- if checkObjectives(colNo, "leftside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, 12, "leftside") -- move to correct starting position
- harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area run 7/8
- end
- end
- --Alternative to above using for/next loops
- --[[
- local fileIndex = 0
- colNo = 1
- mineshaftIndex = 0
- for i = 0, 15, 3 do --no of blocks after reset torch(6, 9, 12, 15)
- if i == 6 or i == 12 then
- direction = "left"
- else
- direction = "right"
- end
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fileIndex = fileIndex + 1
- fso:useFileName("logHarvestRight"..fileIndex..".txt")
- mineshaftIndex = mineshaftIndex + 1
- torchesNeeded = restoreTorches("rightside", i) --check for torches and signs before starting
- if checkObjectives(colNo, "rightside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, i, "rightside") -- move to correct starting position
- harvestArea(colNo, "right", "rightside", mineshaftIndex) --harvest trees in 16 x 6 area runs 5 / 7 /9 /11
- colNo = colNo + 2
- end
- --cleared first half, now do second if needed
- if continue then
- fileIndex = 0
- colNo = 3
- for i = 3, 12, 3 do
- if i == 3 or i == 9 then
- direction = "right"
- else
- direction = "left"
- end
- logText = "harvestAllTrees: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fileIndex = fileIndex + 1
- fso:useFileName("logHarvestLeft"..fileIndex..".txt")
- mineshaftIndex = mineshaftIndex + 1
- torchesNeeded = restoreTorches("leftside", i)
- if checkObjectives(colNo, "leftside", torchesNeeded) then
- continue = false
- end
- prepareHarvest(direction, i, "leftside") -- move to correct starting position
- harvestArea(colNo, "left", "leftside", mineshaftIndex) --harvest trees in 16 x 6 area runs 5 / 7 /9 /11
- colNo = colNo + 2
- end
- end]]--
- --[[
- end of this function will have found:
- wood, (wood2 if available),saplings, (saplings2 if available), dirt, cobble, gravel, sand
- coal, sugar cane, ironore (or suspected ironore: ?ironore, needs smelting and making into buckets to prove)
- found and discarded: seeds, flint, flowers
- will have crafted planks, chests, furnaces, sticks, torches, signs, (buckets if 4+ ironore found)
- will have smelted: charcoal, stone, (iron if 4+ ?ironore)
- cleared an area ready for a tree farm and planted it
- ]]--
- end
- function harvestArea(colNo, direction, side, mineshaftIndex)
- local sTime = os.time()
- local dugItem = ""
- local fuelLevel = 0
- local fuelStats = {}
- local mineEntranceY = 0
- local mineExitY = 0
- --move forward 16 blocks first side of square
- saveToLog("harvestArea: colNo = "..colNo.." direction = "..direction..", side = "..side, true)
- forwardSafe(16)
- saveToLog("harvestArea: checking if tree present around mineshaft area", false, true)
- checkMineEntranceForTree(direction)
- --place cobble at mine entrance
- if checkWater("down") then
- --create an island with hole in centre
- saveToLog("harvestArea: creating island around mineshaft entrance", false, true)
- createIsland()
- else
- saveToLog("harvestArea: placing plug on mineshaft entrance", false, true)
- createMinePlug()
- end
- if direction == "right" then
- turnRight(1)
- else
- turnLeft(1)
- end
- mineEntranceY = location:getY()
- --still at mine entrance point
- fuelStats = getFuelAvailable()
- fuelLevel = fuelStats.fuelAvailableAllWood
- saveToLog("harvestArea: fuel available from wood = "..fuelLevel, false, true)
- if not objectives["gravel"] or not objectives["ironore"] or getCobbleStock() < 25 then --gravel/ iron ore not found
- -- still at mine entrance. check out exit to see if over water, level ground, or stepped level
- forwardSafe(3)
- --now at mine exit point
- mineExitY = location:getY()
- checkMineEntranceForTree(direction)
- turnRight(2)
- forwardSafe(3)
- turnRight(2)
- if fuelLevel > 100 then
- placeMarker(direction, "Danger!\nMineshaft", "sign/torch placed at mineshaft entrance")
- else
- placeMarker(direction, "Safe Area\nNo Mineshaft\nNot enough\nfuel", "sign/torch placed at mineshaft entrance")
- end
- if goMining1(direction, fuelLevel, mineExitY) then --no mining if fuel level too low
- --mining complete, now back on surface of ground or water
- mineshaft[mineshaftIndex] = true
- --check if on water
- if checkWater("down") then
- --create an island with hole in centre
- saveToLog("harvestArea: creating island around mineshaft exit", false, true)
- createIsland()
- else
- saveToLog("harvestArea: placing plug on mineshaft exit", false, true)
- createMinePlug()
- end
- for i = 1, 4 do
- turnRight(1)
- isTreePresent()
- end
- if not objectives["sand"] or not objectives["gravel"] then
- checkGravel()
- end
- placeMarker(direction, "", "Torch placed at mineshaft exit")
- else --not enough fuel, so no mining
- forwardSafe(3)
- checkMineEntranceForTree(direction)
- --now at turning point ready to head home
- if checkWater("down") then
- --create an island with hole in centre
- saveToLog("harvestArea: creating island around mineshaft exit", false, true)
- createIsland()
- else
- saveToLog("harvestArea: placing plug on mineshaft exit", false, true)
- createMinePlug()
- end
- placeMarker(direction, "", "Torch placed at mineshaft exit")
- end
- else -- gravel and iron ore found so no more mining
- placeMarker(direction, "Safe Area\nNo Mineshaft", "sign/torch placed at turning point")
- forwardSafe(3)
- --now at turning point ready to head home
- checkMineEntranceForTree(direction)
- if checkWater("down") then -- water found.
- --create an island with hole in centre
- saveToLog("harvestArea: creating island around mineshaft exit", false, true)
- createIsland()
- else
- saveToLog("harvestArea: placing plug on mineshaft exit", false, true)
- createMinePlug()
- end
- -- place torch if onboard
- placeMarker(direction, "", "Torch placed at mineshaft exit")
- end
- if direction == "right" then
- turnRight(1)
- else
- turnLeft(1)
- end
- sTime = os.time()
- if colNo == 3 and side == "rightside" then
- forwardSafe(13)
- else
- forwardSafe(16)
- end
- --end of 16 x 6 block, return to furnace
- if side == "rightside" then --harvesting right side of 33 x 33 square from centre
- --end up facing furnace on edge of 5x5 cobble square except col 3, which goes direct
- if colNo == 1 then
- turnRight(1)
- returnToGroundLevel()
- checkForLooseSaplings()
- forward(1, 1)
- elseif colNo == 3 then
- --no need to turn as end up under furnace
- returnToGroundLevel()
- forward(3, 1)
- else
- if colNo == 5 then
- turnRight(1)
- forwardSafe(6)
- elseif colNo == 7 then
- turnLeft(1)
- forwardSafe(3)
- elseif colNo == 9 then
- turnRight(1)
- forwardSafe(12)
- elseif colNo == 11 then
- turnLeft(1)
- forwardSafe(9)
- end
- returnToGroundLevel()
- forward(1, 1)
- end
- else --harvesting left side of 33 x 33 square from centre
- --return side of area
- if colNo == 3 then --3,5,7,9
- turnLeft(1)
- forwardSafe(3, 1)
- elseif colNo == 5 then
- turnRight(1)
- --back(1)
- elseif colNo == 7 then
- turnLeft(1)
- forwardSafe(9, 1)
- elseif colNo == 9 then
- turnRight(1)
- forwardSafe(6, 1)
- end
- returnToGroundLevel()
- forward(1, 1)
- end
- if colNo == 3 and side == "rightside" then
- saveToLog("harvestArea: second harvest run, checkTorch not called, started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
- else
- checkTorch("home")
- saveToLog("harvestArea: run "..colNo + 1 .." direction = "..direction.." side = "..side.." started at "..textutils.formatTime(sTime, true).." finished at "..textutils.formatTime(os.time(), true))
- end
- end
- function harvestTree(woodType)
- --only called if block known to be wood
- --break lower block of tree
- local tempYstart = 0
- local emptySlot = 0
- local isIdentified = false
- local dirtPlaced = false
- local useSlot = slot:getItemSlot(woodType)
- local isBranched = false
- saveToLog("harvestTree: woodType = "..woodType)
- dig.digNew{self = dig, slotNo = 1, expectedItem = woodType, callFrom = "harvestTree"}
- forward(1, 1)
- tempYstart = location:getY()
- --check down first
- turtle.select(slot:getItemSlot(woodType))
- while turtle.compareDown() do
- dig.digNew{self = dig, direction = "down", callFrom = "harvestTree"}
- down(1, 1)
- turtle.select(slot:getItemSlot(woodType))
- end
- while location:getY() < tempYstart do
- up(1, 1)
- end
- --now back at starting position
- turtle.select(slot:getItemSlot(woodType))
- while turtle.detectUp() do
- --leaves at tree top could give saplings
- dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "harvestTree"}
- up(1, 1)
- isBranched = checkTree(woodType, isBranched, "harvestTree")
- turtle.select(slot:getItemSlot(woodType))
- end
- --drop down until yCoord = tempYstart
- while location:getY() > tempYstart do
- down(1, 1)
- end
- --move back to starting point
- back(1)
- refuel(0)
- end
- function harvestTreeFarm(checkTorches)
- local useSlot = 0
- if checkTorches == nil then
- checkTorches = true
- end
- --move forward and harvest all trees
- saveToLog("harvestTreeFarm: started")
- forward(2, 1)
- turnRight(1)
- forward(2, 1)
- turnLeft(1)
- refuel(0)
- getItemsFromStorage{fromStore = storageSaplings, item1 = "saplings"}
- --for i = 1, 3 do
- for i = 1, 2 do
- for j = 1, 4 do
- if turtle.detect() then
- useSlot = slot:getItemSlot("saplings")
- turtle.select(useSlot)
- if turtle.compare() then
- saveToLog("harvestTreeFarm: dormant sapling harvested")
- dig.digNew{self = dig, slot = useSlot, callFrom = "harvestTreeFarm"}
- else
- turtle.select(slot:getItemSlot("wood"))
- saveToLog("harvestTreeFarm: tree harvest started")
- harvestTree("wood")
- end
- end
- forward(3, 1)
- end
- if i == 1 then
- turnRight(1)
- forward(3, 1)
- turnRight(1)
- end
- end
- -- now return to furnace
- forward(2, 1)
- turnRight(1)
- forward(3, 1)
- if checkTorches then
- checkTorch("home")
- else
- forward(2, 1)
- turnRight(1)
- end
- --back under furnace
- end
- function identifyItems()
- local doRepeat = false
- local testAmount = 0
- local checkForDiamonds = false
- local tempItem = ""
- -- called when under furnace and facing deep mineshaft. called after checkMineStores() to eliminate known items
- -- if new items identified then re-run checkMineStores()
- sortInventory(true)
- changeDirection("faceMine")
- -- check for wood, fencing, planks,
- if slot:getItemSlot("coal") > 0 then
- repeat
- doRepeat = false
- if slot:getUnknownItemCount() > 0 then
- saveToLog("identifyItems: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- -- check if any items burn
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- turtle.select(i)
- saveToLog("identifyItems: checking wooden items"..slot:getSlotContains(i), true)
- if turtle.refuel() then
- slot.update{self = slot, slotNo = i, delete = true}
- doRepeat = true
- sortInventory(true)
- break
- end
- end
- end
- until not doRepeat
- else --coal not identified yet
- if slot:getUnknownItemCount() > 0 then
- saveToLog("identifyItems: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- -- check if any items burn
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- if turtle.getItemCount(i) > 1 then
- turtle.select(i)
- saveToLog("identifyItems: checking "..slot:getSlotContains(i).." for coal", true)
- if turtle.refuel(0) then
- slot.update{self = slot, slotNo = i, newItem = "coal"}
- sortInventory(true)
- break
- end
- end
- end
- end
- end
- --now see if items place()
- if slot:getUnknownItemCount() > 0 then
- --need sticks to test for diamonds
- checkItems{keepTorches = 0, keepSigns = 0, keepSticks = 2, keepWood = 0}
- repeat
- if slot:getUnknownItemCount() == 0 then
- break
- end
- doRepeat = false
- saveToLog("identifyItems: slot with most items = "..slot:getMostUnknownItemSlot().." with "..turtle.getItemCount(slot:getMostUnknownItemSlot()).." items", true)
- for i = slot:getMostUnknownItemSlot(), slot:getLeastUnknownItemSlot(), - 1 do
- if turtle.getItemCount(i) > 1 then
- turtle.select(i)
- tempItem = slot:getSlotContains(i)
- testAmount = turtle.getItemCount(i)
- saveToLog("identifyItems: checking "..slot:getSlotContains(i).." place()", true)
- changeDirection("faceExtraStorage") -- face away from marker torches
- if turtle.place() then -- redstone, goldore, ironore, obsidian, moss stone
- if slot:getItemSlot("redstone") == 0 then --redstone not found
- if turtle.compare() then
- saveToLog("identifyItems: redstone not found", true)
- turtle.dig()
- else -- redstone
- saveToLog("identifyItems: redstone confirmed", true)
- slot.update{self = slot, slotNo = i, newItem = "redstone"}
- objectives["redstone"] = true
- doRepeat = true
- turtle.dig()
- sortInventory(true)
- break
- end
- changeDirection("faceMine") -- face mine
- end
- --only got here if not redstone
- if slot:getItemSlot("ironore") == 0 or not objectives["goldore"] then --ironore/goldore not found
- dig.digNew{self = dig, slotNo = i, callFrom = "identifyItems"}
- saveToLog("identifyItems: checking for goldore and ironore", true)
- turnRight(2) -- face mine
- if checkMetal(i) ~= "" then --new item identified
- doRepeat = true
- sortInventory(true)
- break
- else -- not goldore, must be obsidian or moss stone
- saveToLog("identifyItems: checking for goldore/ironore completed not identified", true)
- end
- end
- -- only here if ironore and goldore known or eliminated
- saveToLog("identifyItems: testing "..tempItem.." in slot "..i, true)
- dig.digNew{self = dig, slotNo = i, callFrom = "identifyItems"}
- changeDirection("faceMine") -- face mine
- if slot:getItemSlot("moss stone") == 0 then
- if testAmount > 6 then
- saveToLog("identifyItems: checking for moss stone", true)
- slot.update{self = slot, slotNo = i, newItem = "?moss stone"}
- if craft{craftItem = "walls", craftQuantity = 6, sourceItem1 = "?moss stone", destSlot = 0} then
- doRepeat = true
- saveToLog("identifyItems: moss stone found", true)
- slot.update{self = slot, item = "?moss stone", newItem = "moss stone"}
- sortInventory(true)
- break
- else
- slot.update{self = slot, item = "?moss stone", newItem = tempItem}
- saveToLog("identifyItems: obsidian found", true)
- slot.update{self = slot, slotNo = i, newItem = "obsidian"}
- doRepeat = true
- break
- end
- end
- end
- else --lapis, redstone, coal, diamonds, emeralds
- changeDirection("faceMine") -- face mine
- if slot:getItemSlot("redstone") > 0 and slot:getItemSlot("coal") > 0 then --lapis, diamond, emerald
- if objectives["lapis"] then -- lapis found and in storage, would have been found so not needing testing
- if testAmount > 1 then -- diamond, as emerald so rare, not likely to have 2
- saveToLog("identifyItems: diamonds found", true)
- slot.update{self = slot, slotNo = i, newItem = "diamonds"}
- objectives["diamonds"] = true
- doRepeat = true
- end
- else
- if not objectives["diamonds"] then
- checkForDiamonds = true
- end
- end
- else
- if not objectives["diamonds"] then
- checkForDiamonds = true
- end
- end
- end
- if checkForDiamonds then
- if testAmount > 3 then --test for diamond
- slot.update{self = slot, slotNo = i, newItem = "?diamonds"}
- if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "?diamonds", destSlot = 0} then
- saveToLog("identifyItems: pickaxe crafted from ?diamonds", true)
- slot.update{self = slot, item = "?diamonds", newItem = "diamonds"}
- objectives["diamonds"] = true
- objectives["diamond pickaxe"] = true
- doRepeat = true
- sortInventory(true)
- break
- else --diamonds not found
- slot.update{self = slot, slotNo = i, newItem = tempItem}
- end
- end
- else
- if objectives["diamonds"] then
- if not objectives["diamond pickaxe"] then
- if turtle.getItemCount(slot:getItemSlot("diamonds")) > 3 then
- if craft{craftItem = "diamond pickaxe", craftQuantity = 1, sourceItem1 = "sticks", sourceItem2 = "diamonds", destSlot = 0} then
- saveToLog("identifyItems: pickaxe crafted from diamonds", true)
- objectives["diamond pickaxe"] = true
- doRepeat = true
- break
- end
- end
- end
- end
- end
- end
- end
- until not doRepeat
- --return sticks if still onboard
- changeDirection("faceMine")
- if slot:getItemSlot("sticks") > 0 then
- forward(4, 1)
- turnRight(2)
- saveToLog("identifyItems: storing sticks in storageSticks", true)
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- forward(4, 1)
- turnRight(2)
- end
- if slot:getItemSlot("diamond pickaxe") > 0 then
- saveToLog("identifyItems: storing diamond pickaxe in storagePickaxes",true)
- forward(14, 1)
- turnRight(2)
- storeItem{toStore = storagePickaxes, item = "diamond pickaxe", quantity = 0, updateSlot = true, doSort = true}
- forward(14, 1)
- turnRight(2)
- end
- else
- saveToLog("identifyItems: no unknown items to check", true)
- end
- end
- function isTreeAbove(woodType)
- local startyCoord = location:getY() + 1 --return to base of tree when finished
- local isTree = false
- local useSlot = 0
- local success = false
- local dugItem = ""
- local dugSlot = 0
- useSlot = slot:getItemSlot("wood")
- turtle.select(slot:getItemSlot("wood")) --wood
- if turtle.compareUp() then --wood above
- isTree = true
- saveToLog("isTreeAbove checking wood")
- while turtle.compareUp() do
- up(1, useSlot)
- end
- else --may be wood2
- if slot:getItemSlot("wood2") > 0 then --wood2 found
- useSlot = slot:getItemSlot("wood2")
- turtle.select(useSlot) --wood
- if turtle.compareUp() then --wood above
- isTree = true
- saveToLog("isTreeAbove checking wood2")
- while turtle.compareUp() do
- up(1, useSlot)
- end
- end
- end
- end
- if isTree then
- while location:getY() > startyCoord do
- down(1, useSlot)
- end
- else --check for leaves
- dig.digNew{self = dig, direction = "up", checkForItems = "saplings", callFrom = "isTreeAbove"}
- if dig:getDugItem() == "leaves" or dig:getDugItem() == "saplings" then
- isTree = true
- end
- end
- return isTree
- end
- function isTreeBelow(woodType)
- local startyCoord = location:getY()
- local isTree = false
- local useSlot = 0
- useSlot = slot.getItemSlot(slot, woodType)
- turtle.select(useSlot) --wood
- if turtle.compareDown() then --wood/wood2 below
- isTree = true
- saveToLog("isTreeBelow checking wood")
- while turtle.compareDown() do
- down(1, 1)
- end
- end
- if isTree then
- while location:getY() < startyCoord do
- up(1, useSlot)
- end
- end
- return isTree
- end
- function isTreePresent()
- local isTree = false
- local compareSlot = 0
- local compareAmount = 0
- local compareItem = ""
- if turtle.detect() then --block in front: check if tree
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemCount(i) > 0 then
- if turtle.compare() then
- compareItem = slot:getSlotContains(i)
- break
- end
- else
- break
- end
- end
- if compareItem == "wood" or compareItem == "wood2" then
- saveToLog("isTreePresent: match block in front to "..compareItem, false, true)
- isTree = true
- -- wood / wood2 so harvest
- saveToLog("isTreePresent: Harvesting tree type "..compareItem)
- harvestTree(compareItem)
- end
- end
- return isTree
- end
- function isTreeInFront(woodType)
- local success = false
- local dugSlot = 0
- if woodType == "tree" then --leaves/saplings dug above, may be next to a tree
- if slot:getItemSlot("wood2") == 0 then
- dig.digNew{self = dig, slotNo = 1, waitForGravel = true, checkForItems = "wood2", callFrom = "isTreeInFront"}
- else
- dig.digNew{self = dig, waitForGravel = true, callFrom = "isTreeInFront"}
- end
- if dig:getSuccess() then
- if dig:getDugItem() == "dirt" or dig:getDugItem() == "cobble" then
- turtle.select(dig:getDugSlot())
- turtle.place()
- turtle.select(1)
- elseif dig:getDugItem() == "wood2" then
- isTreeBelow("wood2")
- harvestTree("wood2")
- end
- end
- elseif woodType == "wood" or woodType == "wood2" then --found tree with base above current ground level
- harvestTree(woodType)
- end
- end
- function isValuable(direction)
- local itemlist = {}
- local test = true
- local blockType = ""
- itemlist[1] = "dirt"
- itemlist[2] = "stone"
- itemlist[3] = "gravel"
- itemlist[4] = "cobble"
- --itemlist[4] = "sand"
- if direction == "up" then
- if turtle.detectUp() then
- for i = 1, 4 do
- if slot.getItemSlot(slot,itemlist[i]) > 0 then--eg gravel may not be known
- turtle.select(slot.getItemSlot(slot,itemlist[i])) -- select compare blocks
- if turtle.compareUp() then -- compare block above to dirt, stone, gravel in turn
- test = false
- blockType = itemlist[i]
- end
- end
- end
- else
- test = false
- end
- elseif direction == "down" then
- if turtle.detectDown() then
- for i = 1, 4 do
- if slot.getItemSlot(slot, itemlist[i]) > 0 then--eg gravel may not be known
- turtle.select(slot.getItemSlot(slot, itemlist[i])) -- select compare blocks
- if turtle.compareDown() then -- compare block below to selected
- test = false
- blockType = itemlist[i]
- end
- end
- end
- else
- test = false
- end
- elseif direction == "side" then
- if turtle.detect() then
- for i = 1, 4 do
- if slot:getItemSlot(itemlist[i]) > 0 then--eg gravel may not be known
- turtle.select(slot:getItemSlot(itemlist[i])) -- select compare blocks
- if turtle.compare() then -- compare front block to selected
- test = false
- blockType = itemlist[i]
- break
- end
- end
- end
- else
- test = false
- end
- end
- return test, blockType --true only if block does not match those in slots 1,2,3, eg dirt, stone, gravel, cobble
- end
- function mineCorridor(length, torchesAt, doNotPlug, doReturn, itemList)
- local maxTries = 10
- -- torchesAt[1] = true, torchesAt[2] = false
- -- length = length of corridor, torchesAt = position of torches, doNotPlug = place block above entrance to corridor
- -- doReturn = return same distance as length
- for i = 1, length do
- if turtle.detectUp() then --block above
- if isValuable("up") then --could be useful, so dig and replace with cobble
- while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor"} do --allow for gravel
- sleep(.5)
- if not turtle.detectUp() then --water or lava falling down
- break
- end
- maxTries = maxTries - 1
- if maxTries <= 0 then
- maxTries = 10
- break
- end
- end
- if getCobbleStock() > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getDirtStock() > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- end
- else --no block above or lava/water, so place cobble
- if doNotPlug and i == 1 then --dont plug mine entrance
- saveToLog("mineCorridor: Entrance left open")
- else
- if getCobbleStock() > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getDirtStock() > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- end
- end
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
- else --nothing below could be air, water, lava
- if slot:getItemSlot("buckets") > 0 then
- turtle.select(slot:getItemSlot("buckets"))
- if turtle.placeDown() then --bucket filled water or lava
- saveToLog("mineCorridor: water or lava detected")
- if turtle.refuel() then --lava
- saveToLog("mineCorridor: refuelled with lava to "..turtle.getFuelLevel())
- sleep(2)
- while turtle.placeDown() do
- if turtle.refuel() then --lava
- saveToLog("mineCorridor: refuelling with lava again to "..turtle.getFuelLevel())
- else
- break
- end
- sleep(2)
- if turtle.getFuelLevel() > 10000 then
- break
- end
- end
- else
- turtle.placeDown() --empty bucket
- end
- end
- end
- end
- if torchesAt[i] then
- if slot:getItemCount("torches") > 1 then
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- end
- end
- if i < length then
- if turtle.detect() then --dig and drop non-valuables
- while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineCorridor"} do
- sleep(.5)
- if not turtle.detect() then --water or lava falling down
- break
- end
- maxTries = maxTries - 1
- if maxTries <= 0 then
- maxTries = 10
- break
- end
- end
- end
- forward(1, 1, itemList, "mineCorridor")
- end
- end
- if doReturn then
- turnRight(2)
- forward(length, 1, itemList, "mineCorridor")
- turnRight(2)
- end
- end
- function mineDown(toLevel, itemList, calledFrom)
- while location:getY() >= toLevel do --mine down either to bedrock, or to selected level.
- checkWalls(itemList, calledFrom)
- if isValuable("down") then
- saveToLog("mineDown: valuable item detected below x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
- end
- down(1, 1, itemList, calledFrom)
- end
- checkWalls(itemList, calledFrom) --final check at base of chosen depth
- end
- function mineForDiamonds()
- local doContinue = true
- local numDiamonds = 0
- local logText = ""
- local fuelLevel = 0
- local debugLevel = 1 --1 is default value. Alter if needing to debug a particular level
- local debugSector = 1 -- 1 is default value. Alter if needing to debug a particular sector
- -- mine 8 sectors per layer. 3 layers, then down to bedrock
- saveToLog("mineForDiamonds: ready to mine. fuel level = "..turtle.getFuelLevel(), true, false)
- fuelLevel = turtle.getFuelLevel()
- checkMineStores() -- in case anything useful found during prepareMining()
- identifyItems()
- checkMineStores() --after checking for redstone, lapis, obsidian, diamonds
- refuelMining(300)
- checkItems{keepWood = 0, keepTorches = 16}
- --do 8 sectors per layer
- for i = debugLevel, 3 do
- logText = "prepareMining: changing logFile Name from "..tostring(fso:getCurrentFileName()).." to "..tostring(fso:getNextFileName()).." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logMiningLayer"..i..".txt")
- for j = debugSector, 8 do
- fuelLevel = turtle.getFuelLevel()
- saveToLog("mineForDiamonds: fuel level sector "..j.." = "..turtle.getFuelLevel(), true, false)
- if debugSector > 1 then --reset debug: eg testing out on level 2 sector 4, but level 3 should start at sector 1
- debugSector = 1
- end
- restoreTorches("bothsides", 1)
- --move to entrance
- forward(16, 1)
- --rewrite sign
- turtle.select(16)
- if turtle.dig() then
- turtle.place("Diamond Mine\nMining Level "..i.."\nsector "..j.."\n time: "..textutils.formatTime(os.time(), true))
- end
- turnRight(2)
- goMining2(i, j) --level 1, sector 1, will return here, with new items onboard
- --check anything onboard already in extended storage
- checkMineStores()
- identifyItems()
- checkMineStores() --after checking for redstone, lapis, obsidian, diamonds
- numDiamonds = checkDiamonds()
- if turtle.getItemCount(slot:getItemSlot("ironore")) >= 14 then
- if numDiamonds >= 6 then
- doContinue = false -- at least 6 diamonds found
- saveToLog("prepareMining: Mission accomplished "..numDiamonds.." diamonds found", true)
- break
- elseif numDiamonds >= 3 then
- saveToLog("prepareMining: Mission partly finished "..numDiamonds.." diamonds found", true)
- else
- saveToLog("prepareMining: Insufficient diamonds found so far", true)
- end
- else
- saveToLog("prepareMining: Mission almost complete "..numDiamonds.." diamonds found, but more ironore needed", true)
- end
- if not objectives["pave extendedStorage"] then
- objectives["pave extendedStorage"] = paveExtendedStorage()
- end
- refuelMining(250)
- --checkItems{keepWood = 0, keepTorches = 16}
- saveToLog("mineForDiamonds: sector "..j.." completed. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
- end
- if not doContinue then
- break
- end
- checkItems{keepWood = 0, keepTorches = 16}
- end
- if doContinue then
- logText = "prepareMining: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logMiningBedrock.txt")
- createBedrockMine()
- checkItems{keepWood = 0, keepTorches = 16}
- for i = 1, 8 do
- fuelLevel = turtle.getFuelLevel()
- saveToLog("mineForDiamonds: fuel level sector "..i.." = "..turtle.getFuelLevel(), true, false)
- restoreTorches("bothsides", 1)
- --move to entrance
- forward(16, 1)
- --rewrite sign
- turtle.select(16)
- if turtle.dig() then
- turtle.place("Diamond Mine\nBedrock Mining\nSector "..i.."\n time: "..textutils.formatTime(os.time(), true))
- end
- turnRight(2)
- goMining3(i) --level 1, sector 1, will return here, with new items onboard
- checkMineStores()
- identifyItems()
- checkMineStores() --after checking for redstone, lapis, obsidian, diamonds
- numDiamonds = checkDiamonds()
- if numDiamonds >= 6 then
- saveToLog("prepareMining: Mission accomplished "..numDiamonds.." diamonds found", true)
- break
- elseif numDiamonds >= 3 then
- saveToLog("prepareMining: Mission partly finished "..numDiamonds.." diamonds found", true)
- else
- saveToLog("prepareMining: Insufficient diamonds found so far", true)
- end
- refuelMining(400)
- checkItems{keepWood = 0, keepTorches = 16}
- saveToLog("mineForDiamonds: sector "..i.." completed. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
- end
- end
- end
- function mineForward(steps, clear, itemList)
- for i = 1, steps do
- turnLeft(1)
- if turtle.detect() then
- if isValuable("side") then
- dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineForward"}
- end
- end
- turnRight(2)
- if turtle.detect() then
- if isValuable("side") then
- dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineForward"}
- end
- end
- turnLeft(1)
- if turtle.detectUp() then
- if isValuable("up") then
- while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineForward"} do
- sleep(.5)
- if not turtle.detectUp() then --water or lava falling down
- break
- end
- end
- turtle.select(slot:getItemSlot("cobble"))
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- turtle.placeUp()
- end
- end
- else --fill above with cobble
- turtle.select(slot:getItemSlot("cobble"))
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- turtle.placeUp()
- end
- end
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineForward"}
- end
- if turtle.detect() then
- dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineForward"}
- end
- forward(1, 1, itemList)
- end
- end
- function mineToBedrock(placeTorch)
- local tempYCoord = 0
- tempYCoord = location:getY()
- --[[if placeTorch then --collect original torch
- up(1)
- dig{}
- down(1)
- end]]--
- dig.digNew{self = dig, callFrom = "mineToBedrock"}
- while down(1, 1) do
- dig.digNew{self = dig, callFrom = "mineToBedrock"}
- if location:getY() == 1 then
- break
- end
- end
- if placeTorch then
- up(1)
- if slot:getItemSlot("torches") > 0 then
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- slot.update{self = slot, item = "torches", delete = true}
- end
- end
- while location:getY() < tempYCoord do
- up(1, 1)
- end
- end
- function mineUp(toLevel, itemList, calledFrom)
- while location:getY() < toLevel - 5 do --mine up to selected level - 5
- checkWalls(itemList, calledFrom)
- up(1, 1, itemList, calledFrom)
- end
- checkWalls(itemList, calledFrom) --final check at top of chosen height
- while location:getY() < toLevel do --mine up to selected level - 5
- up(1, 1, itemList, calledFrom)
- end
- end
- function mineItem(itemList, calledFrom)
- --RECURSIVE FUNCTION - BEWARE!
- local doContinue = false
- --check if block in front is valuable. If so mine it
- if isValuable("side") then
- dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom}
- if dig:getDugSlot() > 0 then --did not get leaves, seeds
- if dig:getDugItem() == "dirt" or dig:getDugItem() == "coal" then --allows for grass in front, max 50 items
- if turtle.getItemCount(dig:getDugSlot()) < 50 then
- doContinue = true
- end
- elseif dig:getDugItem() == "sand" or dig:getDugItem() == "gravel" then
- if turtle.getItemCount(dig:getDugSlot()) < 8 then
- doContinue = true
- end
- elseif dig:getDugItem() == "cobble" then -- only on goMining1 before stone has been smelted
- if turtle.getItemCount(dig:getDugSlot()) >= 60 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.place()
- end
- else
- if turtle.getItemCount(dig:getDugSlot()) < 20 then --need 18 iron ore
- doContinue = true
- end
- end
- if doContinue then
- if turtle.getFuelLevel() < 10 then
- saveToLog("mineItem: refuelling")
- refuel(0)
- end
- forward(1, 1, itemList, calledFrom)
- if isValuable("side") then
- mineItem(itemList, calledFrom)
- end
- turnLeft(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom)
- end
- turnRight(2)
- if isValuable("side") then
- mineItem(itemList, calledFrom)
- end
- turnLeft(1)
- back(1)
- end
- end
- end
- end
- function paveExtendedStorage()
- local result = false
- if getCobbleStock() > 20 then
- forward(1,1)
- turnLeft(1)
- forward(1, 1)
- turnRight(1)
- forward(1, 1)
- for k = 1, 9 do
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", callFrom = "paveExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "paveExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- end
- turnRight(1)
- forward(2, 1)
- turnRight(1)
- dig.digNew{self = dig, direction = "up", callFrom = "paveExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "paveExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- for k = 1, 8 do
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", callFrom = "paveExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "paveExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- end
- forward(2, 1)
- turnRight(1)
- forward(1, 1)
- turnLeft(1)
- forward(1)
- result = true
- end
- return result
- end
- function paveTreeFarm()
- success = false
- --add decorative cobble to sides of storage chests
- if getCobbleStock() > 20 then
- saveToLog("paveTreeFarm: using cobble to pimp the treefarm")
- forward(3, 1)
- turnLeft(1)
- forward(1)
- turnRight(1)
- for i = 1, 13 do
- dig.digNew{self = dig, direction = "down", callFrom = "paveTreeFarm"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- if i < 13 then
- forward(1, 1)
- end
- end
- turnRight(2)
- forward(13, 1)
- turnLeft(1)
- forward(1)
- turnRight(1)
- forward(2, 1)
- turnRight(2)
- success = true
- else
- saveToLog("paveTreeFarm: insufficient cobble to pimp the treefarm")
- end
- return success
- end
- function placeBlock(blockType, columnType, blockNo)
- local success = false
- local chestBelow = false
- local dirtCount = 0
- local replaceBlock = false
- -- placeBlock("cobble", "storage", 1)
- if columnType == "storage" then
- if blockNo == 1 then -- storageTorches marker
- if not placeStorage:getMarkerPlaced("torches") then
- replaceBlock = true
- end
- elseif blockNo == 2 then -- storageSigns
- if not placeStorage:getStoragePlaced("signs") then
- replaceBlock = true
- end
- elseif blockNo == 3 then -- storageSigns marker
- if not placeStorage:getMarkerPlaced("signs") then
- replaceBlock = true
- end
- elseif blockNo == 4 then -- storageSticks
- if not placeStorage:getStoragePlaced("sticks") then
- replaceBlock = true
- end
- elseif blockNo == 5 then -- storageSticks marker
- if not placeStorage:getMarkerPlaced("sticks") then
- replaceBlock = true
- end
- elseif blockNo == 6 then -- storageWood
- if not placeStorage:getStoragePlaced("wood") then
- replaceBlock = true
- end
- elseif blockNo == 7 then -- storageWood marker
- if not placeStorage:getMarkerPlaced("wood") then
- replaceBlock = true
- end
- elseif blockNo == 8 then -- storageIronore
- if not placeStorage:getStoragePlaced("ironore") then
- replaceBlock = true
- end
- elseif blockNo == 9 then -- storageIronore marker
- if not placeStorage:getMarkerPlaced("ironore") then
- replaceBlock = true
- end
- elseif blockNo == 10 then -- storageSand
- if not placeStorage:getStoragePlaced("sand") then
- replaceBlock = true
- end
- elseif blockNo == 11 then -- storageSand marker
- if not placeStorage:getMarkerPlaced("sand") then
- replaceBlock = true
- end
- elseif blockNo == 12 then -- storageRedstone
- if not placeStorage:getStoragePlaced("redstone") then
- replaceBlock = true
- end
- elseif blockNo == 13 then -- storageRedstone marker
- if not placeStorage:getMarkerPlaced("redstone") then
- replaceBlock = true
- end
- elseif blockNo == 14 then -- storagePickaxes
- if not placeStorage:getStoragePlaced("pickaxes") then
- replaceBlock = true
- end
- elseif blockNo == 15 then
- replaceBlock = true
- end
- if replaceBlock then
- dig.digNew{self = dig, direction = "down", checkForItems = "flowers", callFrom = "placeBlock"}
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- saveToLog("placeBlock: Replacing cobble", false)
- turtle.placeDown()
- success = true
- end
- end
- else
- turtle.select(slot.getItemSlot(slot, "chests"))
- if turtle.compareDown() then
- chestBelow = true
- end
- turtle.select(slot.getItemSlot(slot, "dirt"))
- if chestBelow then
- saveToLog("placeBlock: chest below, leaving alone....")
- else
- if dig.digNew{self = dig, direction = "down", checkForItems = "flowers", callFrom = "placeBlock"} then
- if turtle.getItemCount(slot:getItemSlot(blockType)) > 1 then
- turtle.select(slot:getItemSlot(blockType))
- saveToLog("placeBlock: Replacing "..blockType, false)
- turtle.placeDown()
- else
- saveToLog("Placing "..blockType.." failed - no "..blockType, false)
- end
- success = true
- else --dig not succeed - air/water/lava
- if turtle.getItemCount(slot:getItemSlot(blockType)) > 1 then
- turtle.select(slot:getItemSlot(blockType))
- saveToLog("Placing "..blockType.." over air/water", false)
- turtle.placeDown()
- else
- saveToLog("Placing "..blockType.." over air/water failed - no "..blockType, false)
- end
- success = true
- end
- end
- end
- return success
- end
- function placeExtendedStorage()
- local numChests = turtle.getItemCount(slot:getItemSlot("chests"))
- saveToLog("placeExtendedStorage: starting...")
- if numChests < 6 then
- numChests = 6 - numChests
- if slot:getItemSlot("wood") == 0 then --no wood in stock, so must be in storage
- saveToLog("placeExtendedStorage: getting wood from storage")
- forward(6,1)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- if slot:getItemSlot("wood") > 0 then
- if turtle.getItemCount(slot:getItemSlot("wood")) < 11 then --not enough wood in storage (10 needed)
- saveToLog("placeExtendedStorage: harvesting tree farm")
- harvestTreeFarm()
- saveToLog("placeExtendedStorage: replanting tree farm")
- replantTreeFarm(true, true)
- end
- end
- end
- --should now have at least 11 wood
- saveToLog("placeExtendedStorage: making chests")
- craft{craftItem = "planks", craftQuantity = numChests * 8, sourceItem1 = "wood", destSlot = 0, doSort = true}
- craft{craftItem = "chests", craftQuantity = numChests, sourceItem1 = "planks", destSlot = 0, doSort = true}
- end
- turnRight(2)
- forward(1, 1)
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- forward(1, 1)
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeDown()
- for k = 1, 4 do
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeDown()
- end
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 22 then
- forward(1,1)
- turnLeft(1)
- dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- turnLeft(1)
- for k = 1, 8 do
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- end
- turnLeft(1)
- forward(2, 1)
- turnLeft(1)
- dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- for k = 1, 8 do
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", callFrom = "placeExtendedStorage"}
- dig.digNew{self = dig, direction = "down", callFrom = "placeExtendedStorage"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- end
- turnLeft(1)
- forward(1, 1)
- turnLeft(1)
- forward(17)
- else
- turnRight(2)
- forward(16)
- end
- objectives["place extended storage"] = true
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- objectives["pave extendedStorage"] = true
- end
- function placeMarker(direction, signText, logText)
- local doSort = false
- if direction == "right" then
- turnLeft(1)
- else
- turnRight(1)
- end
- forward(1, 1)
- --if not turtle.detectDown() then
- dig.digNew{self = dig, direction = "down"}
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- --end
- up(1)
- if slot:getItemSlot("signs") > 0 and signText ~= "" then
- turtle.select(slot:getItemSlot("signs"))
- turtle.placeDown(signText)
- if turtle.getItemCount(slot:getItemSlot("signs")) == 0 then
- doSort = true
- end
- slot.update{self = slot, item = "signs", delete = true}
- saveToLog("placeMarker: "..logText, false, true)
- elseif slot:getItemSlot("torches") > 0 then
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- if turtle.getItemCount(slot:getItemSlot("torches")) == 0 then
- doSort = true
- end
- slot.update{self = slot, item = "torches", delete = true}
- saveToLog("placeMarker: "..logText, false, true)
- else
- saveToLog("placeMarker: No signs or torches to place...", false, true)
- end
- if doSort then
- saveToLog("placeMarker: Sorting Inventory", false, true)
- sortInventory(true)
- end
- turnRight(2)
- forward(1, 1)
- down(1)
- if direction == "right" then
- turnLeft(1)
- else
- turnRight(1)
- end
- end
- function plantSaplings()
- for i = 1, 13 do
- if treeFarm:getSaplingPattern(i) == 1 then --plant sapling on ground level
- if turtle.getItemCount(slot:getItemSlot("saplings")) > 1 then
- turtle.select(slot:getItemSlot("saplings"))
- if turtle.placeDown() then
- treeFarm:addSapling()
- end
- end
- elseif treeFarm:getSaplingPattern(i) == 2 then --dig pit, place torch
- down(1, 1) --ground level
- dig.digNew{self = dig, direction = "down", callFrom = "plantSaplings"} --dig pit
- down(1)
- if not turtle.detectDown() then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- up(1)
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown() --place torch in pit
- up(1, 1) --ready for sapling
- end
- if i < 13 then
- forward(1, 1)
- end
- end
- treeFarm:setTimePlanted(os.time())
- end
- function prepareHarvest(direction, steps, side)
- local dugItem = ""
- local doContinue = true
- if side == "rightside" then
- if steps == 0 then --not first run on right side
- doContinue = false
- else
- turnRight(1)
- end
- elseif side == "leftside" then
- turnLeft(1)
- end
- --checking before harvestArea()
- if doContinue then --not first run on right side
- if placeStorage:getMarkerPlaced("torches") then --torches made
- saveToLog("prepareHarvest: checking if torch still present", true)
- if not turtle.detect() then --torch missing
- if slot:getItemCount("torches") > 0 then
- turtle.select(slot:getItemSlot("torches"))
- turtle.place() --replace torch
- end
- end
- dig.digNew{self = dig, expectedItem = "torches", callFrom = "prepareHarvest"}
- forward(2, 1)
- turnLeft(2)
- turtle.select(slot.getItemSlot(slot, "torches"))
- turtle.place() --replace torch
- turnRight(2)
- forward(1, 1, "flowers", "forwardSafe")
- slot.update{self = slot, item = "torches", delete = true}
- saveToLog("prepareHarvest: complete, moving forward "..steps - 3 .." before next harvesting run", true)
- else --no torches yet
- saveToLog("prepareHarvest: no torches made yet", true)
- forward(3, 1, "flowers", "forwardSafe")
- end
- if steps > 3 then
- forwardSafe(steps - 3)
- end
- if turtle.detect() then
- saveToLog("prepareHarvest: block in front at turning point, investigating", true)
- dugItem = checkSugarCane()
- isTreeInFront(dugItem)
- end
- if direction == "right" then
- turnRight(1)
- else
- turnLeft(1)
- end
- end
- end
- function prepareMining()
- local woodNeeded = 0
- local planksNeeded = 0
- local sticksNeeded = 0
- local chestsNeeded = 7 --should already have storageTorches, storageSigns, storageSticks, storageIronore, storageWood, storageSand
- local chestsAvailable = 0
- local woodAvailable = 0
- local woodGrowing = 0
- local maxWood = treeFarm:getMaxHarvest()
- local logText = ""
- local fuelLevel = 0
- -- start here after harvestAllTrees
- --[[object wood planks
- 1 sign 2 8
- 8 sticks 1 4 4 for pickaxes 4 spare
- 16 sticks 2 8 for 48 torches
- 16 charcoal 16 0 for 48 torches
- 16 furnace fuel 4 16 for 16 charcoal
- 7 chests 14 56
- 500 turtle fuel 9 36
- total 47 wood 124 planks 24 sticks
- ]]--
- fuelLevel = turtle.getFuelLevel()
- if fso:getCurrentFileNameIndex() == 2 then --left side of tree harvest not done
- fso:setCurrentFileNameIndex(3)
- end
- logText = "prepareMining: changing logFile Name from "..fso:getCurrentFileName().." to "..fso:getNextFileName().." END OF LOGFILE!"
- saveToLog(logText, true, false)
- fso:useFileName("logCreateMine.txt")
- saveToLog("prepareMining: fuel level = "..fuelLevel, true, false)
- -- check tree farm, continue until wood grown
- -- store signs, sticks and torches first
- saveToLog("prepareMining: storing torches in storageTorches")
- storeItem{toStore = storageTorches, item = "torches", quantity = 0, updateSlot = true, doSort = true}
- forward(2)
- saveToLog("prepareMining: storing signs in storageSigns")
- storeItem{toStore = storageSigns, item = "signs", quantity = 0, updateSlot = true, doSort = true}
- forward(2)
- saveToLog("prepareMining: storing sticks in storageSticks")
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(4)
- turnRight(2)
- while not objectives["choose wood"] do
- objectives["choose wood"] = chooseWood() --identify type of trees farmed, and refuel, make chests and/or charcoal from remainder
- end
- woodNeeded = 48
- planksNeeded = 128
- sticksNeeded = 24
- if placeStorage:getStoragePlaced("redstone") then -- need 1 less chests
- chestsNeeded = chestsNeeded - 1
- woodNeeded = woodNeeded - 2
- planksNeeded = planksNeeded - 8
- end
- if placeStorage:getStoragePlaced("pickaxes") then -- need 1 less chests
- chestsNeeded = chestsNeeded - 1
- woodNeeded = woodNeeded - 2
- planksNeeded = planksNeeded - 8
- end
- chestsAvailable = turtle.getItemCount(slot:getItemSlot("chests")) - 1
- chestsNeeded = chestsNeeded - chestsAvailable
- if chestsNeeded < 0 then
- chestsNeeded = 0
- end
- woodNeeded = woodNeeded - chestsAvailable * 2
- planksNeeded = planksNeeded - chestsAvailable * 8
- --if 1 sign available 8 planks less required
- if storageSigns:getItemCount("signs") > 0 then
- woodNeeded = woodNeeded - 2
- planksNeeded = planksNeeded - 8
- end
- woodGrowing = treeFarm:getPotentialHarvest()
- woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 1
- woodNeeded = woodNeeded - woodAvailable
- if woodNeeded > 0 then
- saveToLog("prepareMining: wood growing approx "..woodGrowing, true)
- saveToLog("prepareMining: wood needed ("..woodNeeded..") harvesting tree farm", true)
- while woodGrowing < woodNeeded do
- sleep(10)
- -- 10 secs = 12 minecraft minutes
- -- 1 min = 1 hour 12 minutes
- -- 20 mins = 1 minecraft day
- woodGrowing = treeFarm:getPotentialHarvest()
- --will be equal to maxHarvest after 2 days
- saveToLog("prepareMining: waiting for tree farm to grow. potential harvest = "..woodGrowing.." from "..maxWood)
- if woodGrowing == maxWood then
- saveToLog("prepareMining: harvesting tree farm. Expecting "..maxWood.." wood")
- break
- end
- end
- harvestTreeFarm()
- replantTreeFarm(true, true)
- else -- remove negative
- woodNeeded = 0
- end
- saveToLog("prepareMining: storing saplings in storageSaplings")
- forward(2)
- turnRight(1)
- forward(2)
- storeItem{toStore = storageSaplings, item = "saplings", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(2)
- turnLeft(1)
- forward(2)
- turnRight(2)
- if chestsNeeded > 0 then --still need more chests made
- if planksNeeded > 64 then --craft chests first, then remaining planks
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
- craft{craftItem = "chests", craftQuantity = chestsNeeded, sourceItem1 = "planks", destSlot = 0, doSort = true}
- planksNeeded = planksNeeded - 64
- craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
- else
- craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
- end
- else --no chests, only charcoal and sticks needed
- if planksNeeded > 64 then
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
- else
- craft{craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
- end
- end
- craft{craftItem = "sticks", craftQuantity = sticksNeeded, sourceItem1 = "planks", destSlot = 0, doSort = true}
- if not placeStorage:getStoragePlaced("redstone") then
- placeStorage:place("redstone", false)
- end
- if slot:getItemCount("charcoal") < 17 then -- not enough charcoal to make 48 torches
- smelt("charcoal", 16 - slot:getItemCount("charcoal"))
- end
- --make torches, wood has already been decided, wood2 turned to charcoal
- --make sticks for storage, pickaxe(s) and later torch making
- --1 wood = 4 planks = 8 sticks = 32 torches
- craft{craftItem = "torches", craftQuantity = 48, sourceItem1 = "sticks", sourceItem2 = "charcoal", destSlot = 0, doSort = true}
- -- if any sticks/planks left over make signs
- if slot:getItemCount("sticks") > 0 and slot:getItemCount("planks") >= 6 then
- craft{craftItem = "signs", craftQuantity = 3, sourceItem1 = "sticks", sourceItem2 = "planks", destSlot = 0, doSort = true}
- end
- checkItems{keepSticks = 0, keepTorches = 48, keepSigns = 1}
- --cleanup any remaining wood, planks, sticks by putting them in storage
- if slot:getItemSlot("sticks") > 0 then
- forward(4, 1)
- storeItem{toStore = storageSticks, item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- back(4)
- end
- if slot:getItemSlot("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- end
- if slot:getItemSlot("planks2") > 0 then
- turtle.select(slot:getItemSlot("planks2"))
- turtle.refuel()
- slot.update{self = slot, item = "planks2", delete = true}
- end
- --use remaining charcoal for fuel
- if slot:getItemSlot("charcoal") > 0 then
- turtle.select(slot:getItemSlot("charcoal"))
- turtle.refuel()
- slot.update{self = slot, item = "charcoal", delete = true}
- sortInventory(true)
- saveToLog("prepareMining: refuelled with charcoal to "..turtle.getFuelLevel())
- end
- --use remaining planks for fuel
- if slot:getItemSlot("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- sortInventory(true)
- saveToLog("prepareMining: refuelled with planks to "..turtle.getFuelLevel())
- end
- placeExtendedStorage()
- if turtle.getFuelLevel() < 500 then
- saveToLog("prepareMining: fuel level = "..turtle.getFuelLevel().." not enough for mining", true)
- -- more wood needed, harvest tree farm
- saveToLog("prepareMining: not enough fuel, harvesting tree farm", true)
- harvestTreeFarm()
- replantTreeFarm(true, true)
- forward(2)
- turnRight(1)
- forward(2)
- saveToLog("prepareMining: storing saplings in storageSaplings")
- storeItem{toStore = storageSaplings, item = "saplings", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(2)
- turnLeft(1)
- forward(2)
- turnRight(2)
- end
- saveToLog("prepareMining: storing wood in storageWood")
- forward(6, 1)
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- forward(2)
- saveToLog("prepareMining: removing iron ore and buckets from storageIronore")
- getItemsFromStorage{fromStore = storageIronore, item1 = "ironore", item2 = "buckets"}
- sortInventory(true)
- saveToLog("prepareMining: storing sugar cane in storageSand")
- forward(2, 1)
- storeItem{toStore = storageSand, item = "sugar cane", quantity = 0, updateSlot = true, doSort = true}
- --forward(2)
- saveToLog("prepareMining: storing sand in storageSand")
- storeItem{toStore = storageSand, item = "sand", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(10)
- turnRight(2)
- saveToLog("prepareMining: ready to create mine. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
- fuelLevel = turtle.getFuelLevel()
- createMine() --mine structure in place ready for mining sectors 12 x 16 block corridors takes 24 hrs
- saveToLog("prepareMining: mine created. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
- end
- function refuel(count)
- --[[1 wood block = 15 fuel
- 1 planks = 15, 4 = 60
- 1 stick = 5, 8 = 40
- planks in slot 3
- use coal> planks> wood2> wood]]--
- local refuel = false
- local startLevel = 0
- local emptySlot = 0
- local fuelStats = {}
- local turns = 0
- if count == nil then
- count = 0
- end
- --count is units of 15, 6 = 1 coal/charcoal 6 blocks of wood or 6 planks
- startLevel = turtle.getFuelLevel()
- if startLevel < 100 or count > 0 then -- refuel by 1 coal, 3 planks/wood2/wood
- saveToLog("refuel: current level = "..startLevel, true)
- --sortInventory(true)
- fuelStats = getFuelAvailable()
- saveToLog("refuel: fuelStats total fuel available = "..fuelStats.totalFuelAvailable)
- if fuelStats.fuelAvailableCoal > 0 then
- turtle.select(slot:getItemSlot("coal"))
- if turtle.refuel(1) then
- refuel = true
- slot.update{self = slot, item = "coal"}
- saveToLog("Refuelled with coal to "..turtle.getFuelLevel().." (start = "..startLevel..")", true)
- else
- saveToLog("Refuel: failed with coal")
- end
- end
- if not refuel and fuelStats.fuelAvailablePlanks2 > 0 then
- saveToLog("refuel: fuelStats fuelAvailablePlanks2 = "..fuelStats.fuelAvailablePlanks2)
- turtle.select(slot.getItemSlot(slot, "planks2"))
- if turtle.refuel() then
- refuel = true
- slot.update{self = slot, item = "planks2", delete = true}
- saveToLog("Refuelled with planks2 to "..turtle.getFuelLevel().." (start = "..startLevel..")")
- sortInventory(true)
- else
- saveToLog("Refuel: failed with planks2")
- end
- end
- if not refuel and fuelStats.fuelAvailablePlanks > 0 then
- saveToLog("refuel: fuelStats fuelAvailablePlanks = "..fuelStats.fuelAvailablePlanks)
- turtle.select(slot.getItemSlot(slot, "planks"))
- if turtle.refuel() then
- refuel = true
- slot.update{self = slot, item = "planks", delete = true}
- saveToLog("Refuelled with planks to "..turtle.getFuelLevel().." (start = "..startLevel..")")
- sortInventory(true)
- else
- saveToLog("Refuel: failed with planks")
- end
- end
- if not refuel and (fuelStats.fuelAvailableWood > 0 or fuelStats.fuelAvailableWood2 > 0 ) then
- if fuelStats.fuelAvailableWood > 0 then --use this to make planks
- saveToLog("refuel: No planks for fuel. Attempting to craft planks from wood")
- if turtle.detect() then
- for i = 1, 4 do
- if turtle.detect() then
- turnRight(1)
- turns = turns + 1
- else
- break
- end
- end
- end
- -- bug in cc 1.6.3 place() can put chest 1 block away if grass is in the way
- dig.digNew{self = dig}
- if craft{craftItem = "planks", craftQuantity = 4, sourceItem1 = "wood", destSlot = 0, doSort = true} then
- fuelStats.fuelAvailablePlanks = turtle.getItemCount(slot.getItemSlot(slot, "planks")) * 15
- turtle.select(slot.getItemSlot(slot, "planks"))
- if turtle.refuel() then
- refuel = true
- slot.update{self = slot, item = "planks", delete = true}
- saveToLog("Refuelled with planks to "..turtle.getFuelLevel().." (start = "..startLevel..")")
- end
- else
- saveToLog("Refuel: error crafting planks from wood")
- error()
- end
- if turns > 0 then
- turnLeft(turns)
- end
- else --check if wood2 can be harvested for fuel as last resort
- if fuelStats.fuelAvailableWood2 > 0 then
- saveToLog("No planks for fuel. attempting to craft from wood 2")
- if turtle.detect() then
- for i = 1, 4 do
- if turtle.detect() then
- turnRight(1)
- turns = turns + 1
- else
- break
- end
- end
- end
- craft{craftItem = "planks2", craftQuantity = 4, sourceItem1 = "wood2", destSlot = 0, doSort = true}
- fuelStats.fuelAvailablePlanks2 = turtle.getItemCount(slot.getItemSlot(slot, "planks2")) * 15
- turtle.select(slot.getItemSlot(slot, "planks2"))
- if turtle.refuel() then
- refuel = true
- slot.update{self = slot, item = "planks2", delete = true}
- saveToLog("Refuelled with planks2 (start = "..startLevel..")")
- end
- if turns > 0 then
- turnLeft(turns)
- end
- else
- saveToLog("No materials left for fuel")
- end
- end
- if refuel then
- sortInventory(true)
- end
- end
- if not refuel and fuelStats.fuelAvailableCharcoal > 0 then
- turtle.select(slot.getItemSlot(slot, "charcoal"))
- if turtle.refuel(fuelStats.fuelAvailableCharcoal/80) then
- refuel = true
- slot.update{self = slot, item = "charcoal", delete = true}
- saveToLog("Refuelled with charcoal to "..turtle.getFuelLevel().." (start = "..startLevel..")")
- sortInventory(true)
- else
- saveToLog("Refuel: failed with charcoal")
- end
- end
- else
- --saveToLog("refuel: "..startLevel.." is sufficient, refuel not required",true)
- end --fuel > 100
- return refuel
- end
- function refuelMining(checkLevel)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel < checkLevel then
- if slot:getItemSlot("coal") > 0 then
- if turtle.getItemCount(slot:getItemSlot("coal")) > 1 then
- saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." using coal")
- turtle.select(slot:getItemSlot("coal"))
- turtle.refuel(turtle.getItemCount(slot:getItemSlot("coal")) - 1)
- slot.update{self = slot, item = "coal"}
- saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." after using available coal")
- end
- end
- fuelLevel = turtle.getFuelLevel()
- if fuelLevel < checkLevel then
- --check if enough wood in storage
- if storageWood:getItemCount("wood") > 16 then --enough wood in store already
- forward(6, 1)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- if turtle.getItemCount(slot:getItemSlot("wood")) > 16 then
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
- else
- craft{craftItem = "planks", craftQuantity = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
- end
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- saveToLog("refuelMining: storing wood in storageWood after refuelling", false)
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- else
- saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." harvesting tree farm", true)
- if storageWood:getItemCount("wood") > 0 then
- forward(6, 1)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- harvestTreeFarm()
- replantTreeFarm(true, true)
- if turtle.getItemCount(slot:getItemSlot("wood")) > 32 then
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
- elseif turtle.getItemCount(slot:getItemSlot("wood")) > 16 then
- craft{craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood", destSlot = 0, doSort = true}
- else
- craft{craftItem = "planks", craftQuantity = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
- end
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- saveToLog("refuelMining: storing wood in storageWood after refuelling", false)
- forward(6, 1)
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- else
- saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." refuel with coal sufficient", true)
- end
- else
- saveToLog("refuelMining: fuel level = "..turtle.getFuelLevel().." refuel not required", true)
- end
- end
- function replantSaplings(treeRow)
- --[[local saplingPattern = {}
- saplingPattern[1] = 0
- saplingPattern[2] = 1
- saplingPattern[3] = 2
- saplingPattern[4] = 0
- saplingPattern[5] = 1
- saplingPattern[6] = 2
- saplingPattern[7] = 0
- saplingPattern[8] = 1
- saplingPattern[9] = 2
- saplingPattern[10] = 0
- saplingPattern[11] = 1
- saplingPattern[12] = 2
- saplingPattern[13] = 0]]--
- if treeRow == true then
- -- use sapling pattern to check position
- for i = 15, 1, -1 do
- turtle.suckDown()
- turnLeft(1)
- checkForLooseSaplings()
- turnRight(2)
- checkForLooseSaplings()
- turnLeft(1) --now facing forward, saplings checked left and right
- --if saplingPattern[i - 1] == 1 and i < 15 then --sapling to be planted on current block
- if treeFarm:getSaplingPattern(i) - 1 == 1 and i < 15 then --sapling to be planted on current block
- forward(1)
- turnRight(1)
- checkForLooseSaplings()
- turnRight(1)
- checkForLooseSaplings()
- turtle.select(slot.getItemSlot(slot, "saplings"))
- if turtle.getItemCount(slot.getItemSlot(slot, "saplings")) > 1 then
- turtle.place()
- end
- turnRight(1)
- checkForLooseSaplings()
- turnRight(1)
- checkForLooseSaplings()
- else
- if i > 1 then
- forward(1)
- end
- end
- end
- else
- for i = 1, 15 do -- row 1 at ground level
- turnRight(1)
- checkForLooseSaplings()
- turnLeft(2)
- checkForLooseSaplings()
- turnRight(1)
- checkForLooseSaplings()
- if i < 15 then
- forward(1, 1)
- end
- end
- end
- end
- function replantTreeFarm(checkTorches, storeSaplings)
- local plant = false
- local direction = "right"
- if checkTorches == nil then
- checkTorches = true
- end
- if storeSaplings == nil then
- storeSaplings = true
- end
- saveToLog("replantTreeFarm started y = "..location:getY())
- forward(1)
- turtle.select(slot:getItemSlot("saplings"))
- saveToLog("replantTreeFarm - now facing farm on left edge. x = "..location:getX().." y = "..location:getY().." z = "..location:getZ())
- for i = 1, 4 do
- if i == 2 or i == 3 then
- plant = true
- else
- plant = false
- end
- replantSaplings(plant)
- if i < 4 then
- if direction == "right" then
- turnRight(1)
- forward(2, 1)
- turnRight(1)
- direction = "left"
- else
- turnLeft(1)
- forward(3, 1)
- turnLeft(1)
- direction = "right"
- end
- end
- end
- if storeSaplings then
- turnRight(1)
- forward(5, 1)
- turnRight(1)
- forward(1, 1)
- storeItem{toStore = storageSaplings, item = "saplings", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(2)
- turnRight(1)
- else
- turnRight(1)
- forward(5, 1)
- turnLeft(1)
- forward(1, 1)
- turnRight(1)
- end
- if checkTorches then
- checkTorch("home")
- else
- forward(2, 1)
- turnRight(1)
- end
- end
- function reposition(direction, itemList, fillCobble)
- --used on suface to reposition turtle while clearing blocks above as well to create space for player
- saveToLog("reposition "..direction.." at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass(), false)
- for i = 1, string.len(direction) do
- move = string.sub(direction, i, i)
- if move == "R" then
- turnRight(1)
- saveToLog("reposition turnRight. Facing "..location:getCompass(), false)
- elseif move == "L" then
- turnLeft(1)
- saveToLog("reposition turnLeft. Facing "..location:getCompass(), false)
- elseif move == "B" then
- back(1)
- saveToLog("reposition turn round. Facing "..location:getCompass(), false)
- else --F if none of above
- saveToLog("reposition at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass(), false)
- dig.digNew{self = dig, direction = "up", slotNo = 1, checkForItems = itemList, callFrom = "reposition"}
- checkForLooseSaplings()
- if dig:getSuccess() then --success digging. If dirt/grass itemcount ++
- --see if 1 item in slot 16 matches anything
- if dig:getDugItem() == "wood2" then
- saveToLog("reposition: harvesting new tree type, wood2")
- harvestTree("wood2")
- end
- end
- dig.digNew{self = dig, checkForItems = "flowers,sand", callFrom = "reposition"}
- if fillCobble then
- dig.digNew{self = dig, direction = "down", checkForItems = "flowers,sand", callFrom = "reposition"}
- turtle.select(slot:getItemSlot("cobble"))
- if turtle.getItemCount(slot:getItemSlot("cobble")) > 1 then
- turtle.placeDown()
- end
- end
- forward(1, 1, itemList, "reposition")
- end
- end
- end
- function repositionUnderground(direction, itemList, calledFrom)
- saveToLog("repositionUnderground "..direction.." at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
- if location:getY() < 5 then
- up(6 - location:getY(), 1)
- saveToLog("repositionUnderground up ".. 6 - location:getY().." now at y = "..location:getY())
- end
- for i = 1, string.len(direction) do
- move = string.sub(direction, i, i)
- if move == "R" then
- turnRight(1)
- saveToLog("repositionUnderground turnRight. Facing "..location:getCompass())
- elseif move == "L" then
- turnLeft(1)
- saveToLog("repositionUnderground turnLeft. Facing "..location:getCompass())
- elseif move == "B" then
- turnLeft(2)
- saveToLog("repositionUnderground turn round. Facing "..location:getCompass())
- else --F if none of above
- if not checkBedrock(itemList) then
- dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
- forward(1, 1)
- dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
- else --is bedrock
- saveToLog("repositionUnderground bedrock in front, Facing "..location:getCompass())
- up(1, 1, itemList, calledFrom)
- forward(1, 1, itemList, calledFrom)
- dig.digNew{self = dig, direction = "up", callFrom = calledFrom}
- end
- saveToLog("repositionUnderground at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
- end
- end
- end
- function restoreTorches(side, colNo)
- local torchesNeeded = 0
- local torchesOnboard = 0
- local torchPlaced = false
- local doContinue = true
- changeDirection("faceMine")
- -- checking before harvestArea()
- -- if torch(es) not present on correct side(s), need to add to onboard stock
- saveToLog("restoreTorches: started. side = "..side.." colNo = "..colNo, false, true)
- if side == "rightside" and colNo == 0 then --first run on right - no torches needed
- doContinue = false
- end
- if doContinue then
- if placeStorage:getMarkerPlaced("torches") then --torches made already
- getItemsFromStorage{fromStore = storageTorches, item1 = "torches"} --get any out of store
- if slot:getItemCount("torches") > 0 then
- torchesOnboard = turtle.getItemCount(slot:getItemSlot("torches"))
- end
- turnRight(1)
- if not turtle.detect() then --torch missing
- torchesNeeded = 1
- end
- turnLeft(1)
- if side == "bothsides" or side == "leftside" then --left side harvest / diamond mining only
- turnLeft(1)
- if not turtle.detect() then --torch missing
- torchesNeeded = torchesNeeded + 1
- end
- turnRight(1)
- end
- saveToLog("restoreTorches: torches needed = "..torchesNeeded.." Onboard = "..torchesOnboard)
- if torchesNeeded > 0 then
- if torchesOnboard >= torchesNeeded then
- torchesNeeded = 0
- turtle.select(slot:getItemSlot("torches"))
- turnRight(1)
- if not turtle.detect() then --torch missing
- turtle.place()
- torchPlaced = true
- end
- turnLeft(1)
- if side == "bothsides" or side == "leftside" then --left side harvest / diamond mining only
- turnLeft(1)
- if not turtle.detect() then --torch missing
- turtle.place()
- torchPlaced = true
- end
- turnRight(1)
- end
- end
- end
- if torchPlaced then
- slot.update{self = slot, item = "torches", delete = true}
- if slot:getItemSlot("torches") == 0 then
- sortInventory(true)
- end
- end
- else --no torches yet
- saveToLog("restoreTorches: no torches made yet", true)
- end
- end
- return torchesNeeded
- end
- function returnToGroundLevel()
- while location:getY() > coordHome:getY() do
- down(1, 1)
- end
- while location:getY() < coordHome:getY() do
- up(1, 1)
- end
- end
- function saveToLog(text, toScreen, verbose)
- toScreen = toScreen or true
- verbose = verbose or false
- text = text or "Nil instead of text supplied, check your code!"
- if toScreen then
- print(text)
- end
- if fso:getUseLog() then
- fso:appendLine(text, verbose)
- end
- end
- function selectPlanks(planksType)
- local usePlanks = ""
- local woodAvailable = 0
- local wood2Available = 0
- local woodType = ""
- woodType, woodAvailable, wood2Available = getWoodAvailable{}
- if planksType == "planks" then
- if woodAvailable > 0 then
- usePlanks = "planks"
- else
- turtle.select(slot.getItemSlot(slot, "planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- --updateSlot(itemSlot["planks"], "planks", true)
- if woodType == "wood2" then
- usePlanks = "planks2"
- end
- end
- elseif planksType == "planks2" then
- if wood2Available > 0 then
- usePlanks = "planks2"
- else
- turtle.select(slot.getItemSlot(slot, "planks2"))
- turtle.refuel()
- slot.update{self = slot, item = "planks2", delete = true}
- --updateSlot(itemSlot["planks2"], "planks2", true)
- if woodType == "wood" then
- usePlanks = "planks"
- end
- end
- else --planksType = ""
- if woodType == "wood" then
- usePlanks = "planks"
- else
- usePlanks = "planks2"
- end
- end
- return usePlanks --empty string if not enough wood
- end
- function smelt(oreType, quantity, woodType)
- --charcoal, iron, stone, glass
- --assume function called with turtle under furnace, at ground level
- --move next to furnace to place planks
- --move to top of furnace to place wood
- --move under furnace to remove charcoal, stone, glass, iron
- local smeltType = ""
- local continue = true
- local waitTime = 0
- local planksNeeded = 0 --total needed for smelting
- local woodNeeded = 0
- local woodAvailable = 0
- local wood2Available = 0
- local planksAvailable = 0
- local planks2Available = 0
- local collectionSlot = 0
- local planksType = "planks"
- local success = false
- local checkPlanksAvailable = 0
- local checkPlanksType = ""
- local emptySlot = 0
- local returnToStore = false
- local getItem = {}
- if woodType == nil then --use any wood available
- woodType, woodAvailable, wood2Available = getWoodAvailable{}
- end
- if woodType == "store" then --no wood onboard, get from store
- saveToLog("smelt: fetching wood from storageWood")
- forward(6, 1)
- getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- woodType = "wood"
- planksType = "planks"
- returnToStore = true
- else
- if woodType == "wood" then--use specific wood
- woodAvailable = turtle.getItemCount(slot:getItemSlot("wood")) - 1
- planksType = "planks"
- elseif woodType == "wood2" then
- woodAvailable = turtle.getItemCount(slot:getItemSlot("wood2")) - 1
- planksType = "planks2"
- end
- end
- saveToLog("smelt: oreType = "..oreType.." quantity = "..quantity.." woodType = "..woodType.." planksType = "..planksType, false, true)
- if furnace1:getSmeltOutItemCount() > 0 then
- if dig.digNew{self = dig, suck = true, direction = "up"} then--could be charcoal, iron, stone, glass
- saveToLog("smelt: "..furnace1:getSmeltOutItem().." removed from furnace")
- furnace1:setSmeltOutItem("")
- furnace1:setSmeltOutItemCount(0)
- --sortInventory(true)
- end
- end
- back(1)
- up(2, 1)
- forward(1, 1) --now on top
- planksNeeded = quantity
- if oreType == "charcoal" then --eg quantity = 2, needs 2 wood/wood2 + 2 planks ASSUME only called if wood in stock
- smeltType = "charcoal"
- if quantity == 0 then -- smelt all available wood/wood2
- --calculate amount of wood needed to convert to planks and burn
- woodAvailable = woodAvailable + 1 -- need to use all supplies
- planksNeeded = math.ceil(woodAvailable / 3) * 2 --eg 43 wood needs 43 / 3 (15) * 2 = 30 planks
- quantity = woodAvailable - math.ceil(planksNeeded / 4) --eg 30/4 (8) 43 - 8 = 35 wood to be converted
- planksNeeded = math.ceil(planksNeeded / 4) * 4
- turtle.select(slot.getItemSlot(slot, woodType))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = woodType, delete = true}
- elseif quantity == 64 then -- smelt all available wood, but leave 1 behind
- --calculate amount of wood needed to convert to planks and burn
- --woodAvailable = woodAvailable - 1 -- need to use all supplies
- planksNeeded = math.ceil(woodAvailable / 3) * 2 --eg 43 wood needs 43 / 3 (15) * 2 = 30 planks
- quantity = woodAvailable - math.ceil(planksNeeded / 4) --eg 30/4 (8) 43 - 8 = 35 wood to be converted
- planksNeeded = math.ceil(planksNeeded / 4) * 4
- turtle.select(slot:getItemSlot(woodType))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = woodType, delete = true}
- else --smelt fixed amount
- saveToLog("smelt: dropping "..quantity.." "..woodType.." into furnace")
- turtle.select(slot.getItemSlot(slot, woodType))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = woodType, delete = true}
- end
- furnace1:setSmeltOutItem("charcoal")
- elseif oreType == "cobble"then
- smeltType = "stone"
- turtle.select(slot.getItemSlot(slot, "cobble"))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = "cobble"}
- saveToLog("smelt: "..quantity.." cobble dropped into furnace")
- furnace1:setSmeltOutItem("stone")
- elseif oreType == "?ironore" then
- smeltType = "?iron"
- turtle.select(slot.getItemSlot(slot, "?ironore"))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = "?ironore"}
- saveToLog("smelt: "..quantity.." provisional iron ore dropped into furnace")
- furnace1:setSmeltOutItem("iron")
- elseif oreType == "ironore" then
- smeltType = "iron"
- turtle.select(slot.getItemSlot(slot, "ironore"))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = "ironore"}
- saveToLog("smelt: "..quantity.." iron ore dropped into furnace")
- --[[if not objectives["smelt iron"] then
- objectives["smelt iron"] = true
- saveToLog('OBJECTIVES["smelt iron"] = true')
- end]]--
- elseif oreType == "?goldore" then
- smeltType = "?gold"
- turtle.select(slot.getItemSlot(slot, "?goldore"))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = "?goldore"}
- saveToLog("smelt: "..quantity.." ?gold ore dropped into furnace")
- furnace1:setSmeltOutItem("?gold")
- elseif oreType == "goldore" then
- smeltType = "gold"
- turtle.select(slot.getItemSlot(slot, "goldore"))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = "goldore"}
- saveToLog("smelt: "..quantity.." gold ore dropped into furnace")
- furnace1:setSmeltOutItem("gold")
- elseif oreType == "sand" then
- smeltType = "glass"
- turtle.select(slot.getItemSlot(slot, "sand"))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = "sand"}
- saveToLog("smelt: "..quantity.." sand dropped into furnace")
- furnace1:setSmeltOutItem("glass")
- end
- furnace1:setSmeltInItem(oreType)
- furnace1:setSmeltInItemCount(quantity)
- furnace1:setSmeltOutItemCount(quantity)
- back(1)
- down(1, 1)--in front of furnace
- saveToLog("smelt: furnace1:getFuelCount() = "..tostring(furnace1:getFuelCount()))
- if furnace1:getFuelCount() < planksNeeded then --not enough for smelting so refuel turtle
- turtle.select(16)
- turtle.suck()
- turtle.refuel()
- furnace1:setFuelCount(0)
- furnace1:setFuelType("")
- elseif furnace1:getFuelCount() >= planksNeeded then
- planksNeeded = 0
- end
- saveToLog("smelt: planksNeeded = "..planksNeeded)
- if planksNeeded > 0 then
- checkPlanksAvailable, checkPlanksType = getPlanksAvailable()
- if checkPlanksAvailable >= planksNeeded then
- turtle.select(slot:getItemSlot(checkPlanksType))
- turtle.drop(planksNeeded)
- slot.update{self = slot, item = checkPlanksType, delete = true}
- saveToLog("smelt: "..planksNeeded.." planks added to fuel furnace")
- saveToLog("Smelting will take "..planksNeeded * 10 .." secs")
- planksNeeded = 0
- furnace1:setFuelType(checkPlanksType)
- furnace1:setFuelCount(planksNeeded)
- end
- end
- if planksNeeded > 0 then --reset to 0 only if planks already in furnace
- -- check if wood in stock
- if slot:getItemSlot("wood") == 0 then --get wood from storage
- saveToLog("smelt: getting wood from storage")
- down(1, 1)
- forward(1, 1) --back under furnace
- forward(6, 1)
- getItem = getItemsFromStorage{fromStore = storageWood, item1 = "wood"}
- turnRight(2)
- forward(6)
- turnRight(2)
- woodType = "wood"
- back(1,1)
- up(1, 1)
- end
- planksNeeded = math.ceil(planksNeeded / 4) * 4 --eg 1/4 = 1 ,*4 = 4 planks
- saveToLog("smelt: planksNeeded = "..planksNeeded.." crafting more")
- turnRight(1) --side on to furnace
- craft{craftItem = planksType, craftQuantity = planksNeeded, sourceItem1 = woodType, destSlot = 0, doSort = true}
- turnLeft(1)
- turtle.select(slot:getItemSlot(planksType))
- turtle.drop() --drop existing planks
- slot.update{self = slot, item = planksType, delete = true}
- furnace1:setFuelType(planksType)
- furnace1:setFuelCount(planksNeeded)
- end
- down(1, 1)
- forward(1, 1) --back under furnace
- collectionSlot = getFirstEmptySlot()
- if slot.getItemSlot(slot, smeltType) > 0 then
- collectionSlot = slot.getItemSlot(slot, smeltType)
- end
- turtle.select(collectionSlot)
- repeat
- waitTime = waitTime + 1
- sleep(1)
- if waitTime == 10 then --every 10 secs check if any output
- if turtle.suckUp() then --continue to wait
- continue = true
- waitTime = 0
- else --either no product or all done
- continue = false
- end
- end
- until not continue
- if turtle.getItemCount(collectionSlot) > 0 then
- success = true
- slot.update{self = slot, slotNo = collectionSlot, item = smeltType}
- else --item did not smelt
- back(1)
- up(2, 1)
- forward(1)
- turtle.select(1)
- turtle.suckDown()
- back(1)
- down(2)
- forward(1)
- end
- --check fuel status
- emptySlot = getFirstEmptySlot()
- if emptySlot > 0 then
- back(1)
- up(1)
- turtle.select(emptySlot)
- turtle.suck()
- if turtle.getItemCount(emptySlot) == 0 then
- furnace1:setFuelType("")
- end
- furnace1:setFuelCount(turtle.getItemCount(emptySlot))
- turtle.drop()
- down(1)
- forward(1)
- end
- furnace1:setSmeltInItem("")
- furnace1:setSmeltInItemCount(0)
- furnace1:setSmeltOutItem("")
- furnace1:setSmeltOutItemCount(0)
- if returnToStore then
- saveToLog("smelt: returning wood from storageWood")
- forward(6, 1)
- storeItem{toStore = storageWood, item = "wood", quantity = 0, updateSlot = true, doSort = true}
- turnRight(2)
- forward(6, 1)
- turnRight(2)
- end
- saveToLog("smelt: success = "..tostring(success))
- return success
- end
- function sortInventory(moveItems)
- local useSlot = 0
- local usedSlots = 0
- local emptySlot = 0
- local checkItem = ""
- local compareItem = ""
- local moveAmount = 0
- local excessAmount = 0
- local amount = 65
- local firstSlot = 0
- local lastSlot = 16
- local toSlotNo = 0
- local fromSlotNo = 0
- local slotState = {}
- local unknownSlotCount = {}
- local count = 0
- local index = 0
- local fromAmount = 0
- local toAmount = 0
- local doAgain = false
- if moveItems == nil then
- moveItems = false
- end
- firstNewItemSlot = 0
- lastNewItemSlot = 0
- saveToLog("sortInventory: Sorting started, moveItems = "..tostring(moveItems), false)
- turnRight(2) --so if items are dropped do not get sucked up
- --gather like items together first
- for i = 1, 16 do
- checkItem = slot:getSlotContains(i) -- eg "", "item1", "signs" contents of slot in current loop
- if turtle.getItemCount(i) > 0 then
- turtle.select(i) --now compare with slots above
- for j = i + 1, 15 do
- compareItem = slot:getSlotContains(j) -- eg "", "item1", "signs" contents of slot being compared
- if turtle.compareTo(j) then --more of the same in slot j
- if slot:getSlotStatus(i) == "known" then
- --if checkItem ~= "" and string.find(checkItem, "item") ~= nil then --known item in i
- moveAmount = 63 - turtle.getItemCount(i) --40 dirt in slot i, 40 dirt in slot j, moveAmount = 23
- turtle.select(j)
- if moveAmount > 0 then
- turtle.transferTo(i, moveAmount) --move up to 63
- end
- excessAmount = turtle.getItemCount(j)
- if excessAmount > 0 then
- if checkItem == "wood" or checkItem =="wood2" then
- turtle.refuel()
- saveToLog("sortInventory: refuelling with excess "..checkItem)
- else
- while turtle.drop() do
- saveToLog("sortInventory: dumping "..excessAmount.." of excess "..checkItem, true)
- end
- end
- slot.update{self = slot, slotNo = j, delete = true}
- end
- slot.update{self = slot, slotNo = i, item = checkItem}
- else --"itemX" or ""
- moveAmount = 63 - turtle.getItemCount(i)
- turtle.select(j)
- if moveAmount > 0 then
- turtle.transferTo(i, moveAmount)
- end
- excessAmount = turtle.getItemCount(j)
- if excessAmount > 0 then
- while turtle.drop() do
- saveToLog("sortInventory: dumping "..excessAmount.." of excess "..checkItem)
- end
- --slot.update{self = slot, item = "item", slotNo = j, delete = true}
- end
- slot.update{self = slot, slotNo = i, item = checkItem}
- end
- end
- end
- end
- checkItem = slot:getSlotContains(i) -- re-check, anything not identified dump
- if checkItem == "" then
- turtle.select(i)
- while turtle.drop() do
- saveToLog(" sortInventory: dumping unknown item in slot "..i, true)
- end
- end
- end
- --loose items sorted into correct slots, excess dumped
- --use coal for fuel if above 24
- useSlot = slot.getItemSlot(slot, "coal")
- if useSlot > 0 then
- if turtle.getItemCount(useSlot) > 24 then
- turtle.select(useSlot)
- turtle.refuel(turtle.getItemCount(useSlot) - 24)
- saveToLog("sortInventory coal used for fuel. Level = "..turtle.getFuelLevel())
- slot.update{self = slot, item = "coal"}
- end
- end
- --[[saveToLog("sortInventory: start - debug log", false)
- for i = 1, 16 do
- saveToLog(" slotStatus["..i.."] = "..slot:getSlotStatus(i), false)
- end]]--
- if moveItems then
- --check for empty
- repeat
- doAgain = false
- for i = 1, 15 do
- if slot:getSlotStatus(i) == "empty" then
- if slot:getSlotStatus(i + 1) ~= "empty" then --move known item
- turtle.select(i + 1)
- turtle.transferTo(i)
- slot.transfer{self = slot, fromSlot = i + 1, transferTo = i}
- doAgain = true
- end
- end
- end
- until not doAgain
- --move known into correct place
- repeat
- doAgain = false
- for i = 16, 1, -1 do
- if slot:getSlotStatus(i) == "known" then
- if slot:getSlotStatus(i - 1) == "unknown" then --move known item
- turtle.select(i)
- turtle.transferTo(16)
- slot.transfer{self = slot, fromSlot = i, transferTo = 16}
- turtle.select(i - 1)
- turtle.transferTo(i)
- slot.transfer{self = slot, fromSlot = i - 1, transferTo = i}
- turtle.select(16)
- turtle.transferTo(i - 1)
- slot.transfer{self = slot, fromSlot = 16, transferTo = i - 1}
- doAgain = true
- end
- end
- end
- until not doAgain
- --move unknown depending on itemCount
- repeat
- doAgain = false
- for i = 16, 1, -1 do
- if slot:getSlotStatus(i) == "unknown" then
- fromAmount = turtle.getItemCount(i)
- if slot:getSlotStatus(i - 1) == "unknown" then --move item
- toAmount = turtle.getItemCount(i - 1)
- if fromAmount < toAmount then
- turtle.select(i)
- turtle.transferTo(16)
- slot.transfer{self = slot, fromSlot = i, transferTo = 16}
- turtle.select(i - 1)
- turtle.transferTo(i)
- slot.transfer{self = slot, fromSlot = i - 1, transferTo = i}
- turtle.select(16)
- turtle.transferTo(i - 1)
- slot.transfer{self = slot, fromSlot = 16, transferTo = i - 1}
- doAgain = true
- end
- end
- end
- end
- until not doAgain
- saveToLog("sortInventory: resetting unknown items", false)
- slot:resetUnknownItems()
- end
- saveToLog("sortInventory: debug log", false)
- for i = 1, 16 do
- saveToLog(" sortInventory: slot "..i.." slotCount = "..slot:getSlotCount(i).. " of "..slot:getSlotContains(i).." slotState = "..slot:getSlotStatus(i), false)
- end
- turnRight(2)
- turtle.select(1)
- end
- function storeItem(arg)
- -- storeItem{toStore = useStore, item = "moss stone", quantity = 0, updateSlot = true, doSort = true}
- local storeIndex = 0
- local slotNum = 0
- if arg.doSort == nil then
- arg.doSort = false
- end
- if arg.updateSlot == nil then
- arg.updateSlot = false
- end
- if arg.slotNo ~= nil then
- if arg.quantity == nil or arg.quantity == 0 then
- arg.quantity = turtle.getItemCount(arg.slotNo)
- end
- saveToLog("Storing "..arg.quantity.." "..arg.item.." from turtle slot "..arg.slotNo.." to chest "..arg.toStore:getStoreName(), false)
- turtle.select(arg.slotNo)
- turtle.dropDown()
- arg.toStore.addItem(arg.toStore, arg.item, arg.quantity)
- else
- if slot:getItemSlot(arg.item) ~= nil then --itemSlot["moss stone"] exists
- slotNum = slot:getItemSlot(arg.item) --eg turtle slot 14 contains moss stone
- if slotNum > 0 then --eg sticks in turtle slot 8
- if arg.quantity == 0 then --drop all
- arg.quantity = turtle.getItemCount(slotNum)
- end
- if arg.quantity > 0 then
- saveToLog("storeItem: arg.quantity = "..arg.quantity, false)
- saveToLog("storeItem: arg.item = "..arg.item, false)
- saveToLog("storeItem: slotNum = "..slotNum, false)
- saveToLog("storeItem: arg.toStore:getStoreName() = "..tostring(arg.toStore.getStoreName(arg.toStore)), false)
- saveToLog("Storing "..arg.quantity.." "..arg.item.." from turtle slot "..slotNum.." to chest "..arg.toStore:getStoreName(), false)
- turtle.select(slotNum)
- turtle.dropDown(arg.quantity)
- arg.toStore.addItem(arg.toStore, arg.item, arg.quantity, slot:getItemSlot(arg.item))
- end
- if arg.updateSlot then
- slot.update{self = slot, item = arg.item, delete = true}
- end
- if arg.doSort then
- sortInventory(true)
- end
- end
- end
- end
- end
- function storeRedstone()
- --called while returning from mineshaft to furnace
- if slot:getItemSlot("redstone") > 0 then
- if not placeStorage:getMarkerPlaced("redstone") then
- if turtle.getItemCount(slot:getItemSlot("redstone")) > 9 then
- if craft{craftItem = "redstone block", craftQuantity = 1, sourceItem1 = "redstone", destSlot = 0} then
- back(1)
- dig.digNew{self = dig, direction = "down", callFrom = "storeRedstone"}
- turtle.select(slot:getItemSlot("redstone block"))
- turtle.placeDown()
- slot.update{self = slot, item = "redstone block", delete = true}
- placeStorage:setMarkerPlaced("redstone")
- saveToLog("storeRedstone: marker redstone placed")
- forward(1)
- end
- end
- end
- if turtle.getItemCount(slot:getItemSlot("redstone")) > 1 then
- saveToLog("storeRedstone: storing redstone in storageRedstone")
- storeItem{toStore = storageRedstone, item = "redstone", quantity = turtle.getItemCount(slot:getItemSlot("redstone")) - 1, updateSlot = true, doSort = false}
- end
- end
- end
- function storeUnknownItem(checkItem)
- -- if sand, torches, wood or signs collected during mining but not identified are deposited here
- local tempStore = {}
- if checkItem == "sand" then
- tempStore = storageSand
- elseif checkItem == "wood" then
- tempStore = storageWood
- elseif checkItem == "signs" then
- tempStore = storageSigns
- elseif checkItem == "torches" then
- tempStore = storageTorches
- end
- if slot:getItemSlot(checkItem) > 0 then
- if checkItem ~= "torches" then
- saveToLog("storeUnknownItem('"..checkItem.."'): already onboard, storing in "..tempStore:getStoreName())
- forward(1, 1)
- storeItem{toStore = tempStore, item = checkItem, quantity = 0, updateSlot = true, doSort = true}
- back(1)-- over markerItem
- end
- else
- if placeStorage:getMarkerPlaced(checkItem) then
- for i = 1, 16 do
- if slot:getSlotStatus() == "unknown" then
- turtle.select(i)
- if turtle.compareDown() then --sand, wood, signs, torches found
- slot.update{self = slot, slotNo = i, newItem = checkItem}
- sortInventory(true)
- forward(1, 1)
- storeItem{toStore = tempStore, item = checkItem, quantity = 0, updateSlot = true, doSort = true}
- back(1)-- over markerItem
- end
- end
- end
- else --marker not placed
- saveToLog("storeUnknownItem('"..checkItem.."'): marker not placed")
- end
- end
- end
- function turnLeft(steps)
- for i = 1, steps do
- turtle.turnLeft()
- location:setFacing(location:getFacing() - 1)
- end
- end
- function turnRight(steps)
- for i = 1, steps do
- turtle.turnRight()
- location:setFacing(location:getFacing() + 1)
- end
- end
- function up(steps, useSlot, itemList, calledFrom)
- local success = false
- local maxTries = 10
- useSlot = useSlot or 1
- for i = 1,steps do
- if turtle.detectUp() then
- while turtle.detectUp() do
- dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
- --allow for sand and gravel
- sleep(.7)
- end
- turtle.up()
- else
- while not turtle.up() do
- dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
- --if turtle.attackUp() then
- if attack() then
- saveToLog("up: mob attacked above!", true)
- end
- maxTries = maxTries - 1
- if maxTries <= 0 then
- if turtle.getFuelLevel() == 0 then
- if refuel(0) then
- maxTries = 10
- else
- saveToLog("forward: Out of fuel. Game Over!", true)
- error()
- end
- end
- end
- end
- success = true
- end
- location:setY(location:getY() + 1)
- success = true
- end
- return success
- end
- function deBug(stage)
- --use this function to start at different stages while debugging
- if stage == "logFile" then
- --fso:setStartFileNameIndex(1) -- debugging using "logStart.txt" logFile
- --fso:setStartFileNameIndex(2) -- debugging using "logHarvestRight1.txt" logFile
- --fso:setStartFileNameIndex(3) -- debugging using "logHarvestRight2.txt" logFile
- --fso:setStartFileNameIndex(4) -- debugging using "logHarvestRight3.txt" logFile
- --fso:setStartFileNameIndex(5) -- debugging using "logHarvestRight4.txt" logFile
- --fso:setStartFileNameIndex(6) -- debugging using "logHarvestRight5.txt" logFile
- --fso:setStartFileNameIndex(7) -- debugging using "logHarvestRight6.txt" logFile
- --fso:setStartFileNameIndex(8) -- debugging using "logHarvestLeft1.txt" logFile
- --fso:setStartFileNameIndex(9) -- debugging using "logHarvestLeft2.txt" logFile
- --fso:setStartFileNameIndex(10) -- debugging using "logHarvestLeft3.txt" logFile
- --fso:setStartFileNameIndex(11) -- debugging using "logHarvestLeft4.txt" logFile
- fso:setStartFileNameIndex(12) -- debugging using "logCreateMine.txt" logFile
- --fso:setStartFileNameIndex(13) -- debugging using "logMiningLayer1.txt" logFile
- --fso:setStartFileNameIndex(14) -- debugging using "logMiningLayer2.txt" logFile
- --fso:setStartFileNameIndex(15) -- debugging using "logMiningLayer3.txt" logFile
- --fso:setStartFileNameIndex(16) -- debugging using "logMiningBedrock.txt" logFile
- --fso:setStartFileNameIndex(17) -- debugging using "logCraftMiningTurtle.txt" logFile
- else
- --set appropriate values if starting from different stages during debug process
- print("setting debug variables")
- slot.update{self = slot, slotNo = 1, item = "chests"}
- slot.update{self = slot, slotNo = 2, item = "dirt"}
- slot.update{self = slot, slotNo = 3, item = "cobble"}
- slot.update{self = slot, slotNo = 4, item = "gravel"}
- slot.update{self = slot, slotNo = 5, item = "stone"}
- slot.update{self = slot, slotNo = 6, item = "coal"}
- slot.update{self = slot, slotNo = 7, item = "ironore"}
- slot.update{self = slot, slotNo = 8, item = "buckets"}
- slot.update{self = slot, slotNo = 9, item = "item1"}
- slot.update{self = slot, slotNo = 10, item = "item2"}
- --slot.update{self = slot, slotNo = 11, item = "signs"}
- --slot.update{self = slot, slotNo = 12, item = "signs"}
- --slot.update{self = slot, slotNo = 13, item = "item3"}
- --slot.update{self = slot, slotNo = 14, item = "signs"}
- sortInventory(true)
- storageTorches:addItem("torches", 30, 1)
- storageSigns:addItem("signs", 3, 1)
- storageSticks:addItem("sticks", 20, 1)
- storageWood:addItem("wood", 42, 9)
- storageIronore:addItem("iron", 1, 1)
- --storageIronore:addItem("buckets", 1, 1)
- --storageSaplings:addItem("saplings", 23, 15)
- storageSand:addItem("sugar cane", 4, 1)
- storageSand:addItem("sand", 18, 14)
- --storageRedstone:addItem("redstone", 64, 15)
- --storageRedstone:addItem("redstone", 10, 15)
- --storagePickaxes:addItem("diamond pickaxe", 1, 14)
- --extraStorage1:addItem("goldore", 27, 14)
- --extraStorage1:addItem("nuggets", 9, 14)
- --extraStorage2:addItem("moss stone", 22, 14)
- --extraStorage2:addItem("walls", 6, 14)
- --extraStorage3:addItem("lapis", 41, 14)
- --mineshaft[1] = true
- --mineshaft[2] = true
- --mineshaft[3] = true
- --mineshaft[4] = true
- --mineshaft[5] = true
- --mineshaft[6] = true
- --mineshaft[7] = true
- --mineshaft[8] = true
- --mineshaft[9] = true
- --mineshaft[10] = true
- objectives["smelt iron"] = true
- --objectives["smelt gold"] = true
- placeStorage:setStoragePlaced("torches")
- placeStorage:setStoragePlaced("signs")
- placeStorage:setStoragePlaced("sticks")
- placeStorage:setStoragePlaced("saplings")
- placeStorage:setStoragePlaced("wood")
- placeStorage:setStoragePlaced("ironore")
- placeStorage:setStoragePlaced("sand")
- placeStorage:setStoragePlaced("redstone")
- placeStorage:setStoragePlaced("pickaxes")
- placeStorage:setMarkerPlaced("torches")
- placeStorage:setMarkerPlaced("signs")
- placeStorage:setMarkerPlaced("sticks")
- placeStorage:setMarkerPlaced("wood")
- placeStorage:setMarkerPlaced("ironore")
- placeStorage:setMarkerPlaced("sand")
- --placeStorage:setMarkerPlaced("redstone")
- --placeStorage:setMarkerPlaced("lapis")
- objectives["place extended storage"] = true
- objectives["choose wood"] = true
- objectives["goMining1"] = true
- objectives["clear treefarm"] = true
- objectives["pave treefarm"] = true
- --objectives["pave extendedStorage"] = true
- objectives["complete sand"] = true
- objectives["complete sugar cane"] = true
- --objectives["diamond pickaxe"] = true
- objectives["gravel"] = true
- objectives["sand"] = true
- objectives["sugar cane"] = true
- objectives["ironore"] = true
- --objectives["goldore"] = true
- --objectives["redstone"] = false
- --objectives["lapis"] = true
- --objectives["diamonds"] = true
- furnace1:setFuelType("planks")
- furnace1:setFuelCount(5)
- treeFarm:setDebug(0, 8) --x saplings y trees
- --if createMine() already done, put correct coords in here
- mineEntrance:setX(183)
- mineEntrance:setY(64)
- mineEntrance:setZ(275)
- end
- end
- function main()
- initialise() -- Setup global variables
- --deBug("logFile") -- uncomment for debugging and starting at different stages
- getCoords() -- Get input from player
- firstTree() -- Harvests first tree, fuels, makes a chest
- findCobble() -- Dig down far enough to find cobble
- checkGravel() -- If gravel found, test to make sure
- confirmCobble() -- Suspected cobble found so confirm, and place furnace
- clearBase() -- Clear area around first tree 4X4
- --deBug("AllTrees") -- uncomment for debugging and starting at different stages
- harvestAllTrees() -- Harvest trees in 32 x 32 area until enough resources found for deep mining
- prepareMining() -- Enough resources found to go diamond mining. Prepare mining area.
- mineForDiamonds() -- Mining area prepared. Mine 3 layers, then to bedrock until min 6 diamonds found
- craftMiningTurtle() -- All resources mined. Build new turtle(s) if minimum 3 diamonds found
- end
- --**************Functions and classes/objects above***************--
- --****************************************************************--
- --***********************PROGRAM START****************************--
- main() -- Entire Program runs from here
Add Comment
Please, Sign In to add comment