Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = "1.0"
- pastebinCode = "4BmfBfp3"
- --[[
- http://pastebin.com/4BmfBfp3
- Start with crafty mining turtle and the following in players inventory:
- 1 chest, 1 dirt, 1 stone, 1 cobble, 1 gravel, 1 bucket, 128 wood
- Place the turtle anywhere you like, even above water or lava.
- Right-click, type in:
- pastebin get 4BmfBfp3 superminer
- (or pastebin get 4BmfBfp3 superminer.lua to make editing easier in Notepad++)
- Type in:
- superminer
- (or superminer.lua if that is how you saved it)
- The script will download a file called 'startup' and reboot
- Check out the startup file here: pastebin.com/DjYnc3QN
- This file allows for restarting the script after reboot.
- Instructions will guide you to place the following into the turtle:
- 1 chest in slot 1
- 1 dirt in slot 2
- 1 cobble in slot 3
- 1 stone in slot 4
- 1 gravel in slot 5
- 1 bucket in slot 6
- 64 wood in slot 7
- 64 wood in slot 8
- Choose whether you want diamonds mined first, and it will start at level 15,
- or just let it run, and it will start at level 51, and finish at bedrock.
- No fuel needed. Allow for 5 x 5 x 2 cobble area to be created as base camp,
- either on surface or below ground. Storage chests will be placed on each layer
- for mining waste. A ladder will be crafted to bedrock.
- 2 cobble staircases will be crafted on opposite sides of the mine.
- Keep an eye out for the turtle returning to the chest marked with a wood block
- If it is there it needs more wood, so put 64 wood in the chest. (NOT the turtle!)
- Check the contents of each waste chest,
- as the contents of any dungeon chests will be stored there.
- Enjoy
- ]]--
- local function initialise()
- local countdown = 10
- local doContinue = true
- if not fs.exists("startup") then
- if getPastebin("startup", "DjYnc3QN") then
- print("startup file obtained. Restarting...")
- sleep(2)
- os.reboot()
- else
- print("Error getting startup file from pastebin")
- print("Get it from http://pastebin.com/DjYnc3QN")
- sleep(5)
- end
- end
- -- set global variables.
- diamondsFirst = true
- diamondsOnly = true
- currentFunction = "initialise"
- callingFunction = "main"
- stage = {}
- --special stage as script progresses:
- stage["startUnderground"] = false
- stage["getPreferences"] = false
- stage["getSupplies"] = false -- confirms player has added required items
- stage["getCoords"] = false -- confirms player has input starting coordinates
- stage["craftChests"] = false -- 8 chests created
- stage["craftSticks"] = false -- sticks crafted and stored in chest
- stage["craftLadders"] = false -- ladders crafted
- stage["craftSigns"] = false
- stage["getReady"] = false -- getReady function completed
- stage["findCobble"] = false -- cobble mined ready for base construction
- stage["craftFurnaces"] = false
- stage["cobblePlaced"] = false
- stage["clearBase"] = false -- clearBase function completed
- stage["completeMineshaft"] = false -- completeMineshaft function completed - dug to bedrock
- for i = 51, 6, -3 do -- mine from level 51 to bedrock in 16 levels
- stage["completeMine"..tostring(i)] = false
- end
- for i = 51, 6, -3 do -- mine from level 51 to bedrock in 16 levels
- stage["createBase"..tostring(i)] = false
- end
- for i = 1, 4 do -- clear mine in 4 sectors
- stage["mineSector"..tostring(i)] = false
- end
- for i = 1, 3 do -- clear each sector in 2 or 3 parts (non-diamond/diamond)
- stage["mineSectorPart"..tostring(i)] = false
- end
- --[[
- createMine51
- createMine48
- createMine45
- ...
- createMine15
- createMine12
- createMine9
- createMine6
- ]]--
- -- create classes/objects
- createCoordObject()
- createChestObject()
- createSlotObject()
- createDigObject()
- createPlaceStorageObject()
- createLogfileObject() --checks if recovery file used, loads if found
- term.clear()
- term.setCursorPos(1,1)
- print("Superminer.lua version: "..version)
- print()
- if os.getComputerLabel() == nil or os.getComputerLabel() ~= "Superminer" then
- os.setComputerLabel("Superminer")
- print("Computer label set to 'Superminer'")
- end
- print()
- print("Checking for recovery file...")
- print()
- if fso:recoveryFileExists() then --could have crashed/chunk unloaded etc
- --[[
- startup file runs this script automatically either when rebooting,
- or after chunk reloading.The recovery file stores current location, current function
- and contents of the turtle.
- ]]--
- print("Recovery File exists")
- repeat
- os.startTimer(2)
- local event, param1 = os.pullEvent()
- if event == "timer" then
- term.setCursorPos(1,6)
- print("loading recovery file in "..countdown.." secs...")
- print()
- print("Press 'x' to abort")
- countdown = countdown - 1
- elseif event == "char" and param1 == "x" then
- print("Loading recovery file aborted")
- doContinue = false
- break
- end
- until countdown == 0
- if doContinue then
- fromRecoveryFile = true
- saveToLog("initialise: using recovery file", false, true)
- --[[resets contents of turtle, and completed functions
- 1)check if chest in front that may be half loaded/emptied during crafting
- 2)move turtle back to starting position
- ]]--
- loadStatus()
- sleep(2)
- else
- saveToLog("initialise: Player cancelled recovery File import", true, true)
- sleep(2)
- end
- else
- print("No Recovery File exists")
- sleep(2)
- saveToLog("initialise: No Recovery File exists", false, true)
- end
- --[[mining levels
- ores levels:
- ceiling = 52 49 46 43 40 37 34 31 28 25 22 19
- turtle = 51 48 45 42 39 36 35 30 27 24 21 18
- floor = 50 47 44 41 38 35 32 29 26 23 20 17
- levels 53+ 50-52 47-49 44-46 41-43 38-40 35-37 32-34 29-31 26-28 23-25 20-22
- Diamond levels:
- ceiling = 16 13 10 7
- turtle = 15 12 9 6
- floor = 14 11 8 5
- levels 17-19 14-16 11-13 8-10
- ]]--
- end
- function attack()
- local numAttacks = 0
- local mobAttacked = false
- local itemCount = {}
- for i = 1, 16 do
- itemCount[i] = turtle.getItemCount(i)
- end
- -- minecart with chest is not detected, but responds to attack without breaking!
- 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)
- numAttacks = numAttacks + 1
- if numAttacks > 10 then
- numAttacks = 0
- saveToLog("attack: digging forward in case of minecart", true, false)
- dig.digNew{self = dig, direction = "forward", callFrom = "attack"}
- end
- 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)
- numAttacks = numAttacks + 1
- if numAttacks > 10 then
- numAttacks = 0
- saveToLog("attack: digging up in case of minecart", true, false)
- dig.digNew{self = dig, direction = "up", callFrom = "attack"}
- end
- 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)
- numAttacks = numAttacks + 1
- if numAttacks > 10 then
- numAttacks = 0
- saveToLog("attack: digging down in case of minecart", true, false)
- dig.digNew{self = dig, direction = "down", callFrom = "attack"}
- end
- 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)
- local previousFunction = currentFunction
- local success = false
- useSlot = useSlot or 1
- itemList = itemList or ""
- if turtle.getFuelLevel() < steps then
- saveToLog(" ***** back: Fuel < "..steps.." *****", true, false)
- if not refuel(0) then
- saveToLog(" ***** back: refuel failed*****", true, false)
- end
- end
- for i = 1, steps do
- if not turtle.back() then --cant move back
- turnRight(2)
- if forward(1, useSlot, itemList) then
- success = true
- end
- turnRight(2)
- end
- changeCoords("back")
- end
- currentFunction = previousFunction
- return success
- end
- function buildStaircase(level, position)
- currentFunction = "buildStaircase"
- callingFunction = "createMine"
- local side = "left"
- -- level decides R or L version of staircase
- -- move to correct position
- if (level / 3) % 2 ~= 0 then -- levels 51, 45, 39, 33, 27, 21, 15, 9
- side = "right"
- end
- saveToLog("buildStaircase: started on side = "..side.." on level "..level.." checking cobble", true, false)
- -- craft stairs
- sortCobble()
- if getStock("cobble-1") > 0 and getStock("cobble-1") < 7 then -- dump it so it does not get stored in crafting chest as "cobble"
- saveToLog("buildStaircase: dumping excess cobble", true, false)
- turtle.select(slot:getItemSlot("cobble-1"))
- turtle.drop()
- slot.update{self = slot, item = "cobble-1", delete = true}
- sortInventory(true)
- end
- if getStock("cobble") > 7 then
- saveToLog("buildStaircase: crafting stairs", true, false)
- craft{craftItem = "stairs", calledFrom = "buildStaircase", craftQuantity = 4, sourceItem1 = "cobble", destSlot = 0, doSort = false}
- if getStock("cobble-1") > 0 and getStock("cobble") < 64 then
- turtle.select(slot:getItemSlot("cobble-1"))
- turtle.transferTo(slot:getItemSlot("cobble"))
- slot.update{self = slot, item = "cobble", delete = true}
- slot.update{self = slot, item = "cobble-1", delete = true}
- sortInventory(true)
- end
- saveToLog("buildStaircase: started on side = "..side.." on level "..level)
- -- move to correct starting position depending on Front/Back staircase and R/L side
- if side == "left" then
- go("LFuC DxC FxC UuC FuCDxCR") -- ready to start to left of door at foot level
- go("FuCxC FuCxC FuCxC FuCxC BdC BdC BdC BdCR FL") -- left side of stairs created, back to door level
- go("FuxC FuxC FuxC FuxCR") -- on back wall, turned right
- go("FuCxC FuCxC FuCxC FuCxC FuCxCR FuCxC FuCxC FuCxC BdC BdC BdC LBdC BdC BdC BdC BdCL BdCR") -- back and right walls of stairs created, back to door level
- go("FuRR")
- if down(1) then
- go("xCUSRR Fux FuCx FuCx BdCRR F F FL FL") -- back row finished.
- else --bedrock
- go("RRFux FuCx FuCx BdCRR F F FL FL") -- back row finished.
- end
- go("FuxC FuxC FuxC FuxCuC BuCdC BdC UxC UxSRR F DRRSRR FL FL D") -- middle row finished
- go("FuxC FuCxC FuCxC FuCxC BdC BdC BdC BdCL") --front row finished
- go("FFdCUdC")
- back(1)
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Staircase\nLevel "..level.."\n"..location:getCompass())
- slot.update{self = slot, item = "signs", delete = true}
- go("RFRdTRFLFFLFFL")
- sortInventory(true)
- else
- go("RFuCDxC FxCUuC FuCDxCL") -- ready to start to left of door at foot level
- go("FuCxC FuCxC FuCxC FuCxC BdC BdC BdC BdCL FR") -- left side of stairs created, back to door level
- go("FuxC FuxC FuxC FuxCL") -- on back wall, turned left
- go("FuCxC FuCxC FuCxC FuCxC FuCxCL FuCxC FuCxC FuCxC BdC BdC BdC RBdC BdC BdC BdC BdCR BdCL") -- back and right walls of stairs created, back to door level
- go("FuxC FuxC FuxC FuxCuC BdCuC BdCUxC UxSRR F DRRSRR FR FR D") -- back row finished
- go("FuRR")
- if down(1) then
- go("xC USRR Fux FuCx FuCx BdCRR F F FR FR") -- middle row finished
- else --bedrock
- go("RRFux FuCx FuCx BdCRR F F FR FR") -- middle row finished
- end
- go("FuxC FuCxC FuCxC FuCxC BdC BdC BdC BdCR") --front row finished
- go("FFdCUdC")
- back(1)
- if getStock("signs") > 0 then
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Staircase\nLevel "..level.."\n"..location:getCompass())
- slot.update{self = slot, item = "signs", delete = true}
- end
- go("LFLdTLFRFFRFFR")
- sortInventory(true)
- end
- if getStock("stairs") > 0 then
- turtle.select(slot:getItemSlot("stairs"))
- turtle.drop()
- slot.update{self = slot, item = "stairs", delete = true}
- sortInventory(true)
- end
- saveToLog("buildStaircase: finished on side = "..side.." on level "..level, true, false)
- else
- saveToLog("buildStaircase: insufficient cobble to build staircase", true, false)
- end
- -- return to starting position, ready to continue with mineCorridor
- if position == "forward" then --front staircase
- turnRight(1)
- else -- rear staircase,
- turnLeft(1)
- 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
- saveStatus()
- end
- function changeDirection(direction)
- -- changeDirection("faceForward")
- -- changeDirection("faceBackward")
- while location:getFacing() ~= coordHome:getFacing() do
- turnRight(1)
- end
- if direction ~= "faceForward" then
- turnRight(2)
- end
- end
- function checkChest(checkDirection)
- local success = false
- currentFunction = "checkChest"
- turtle.select(slot:getItemSlot("chests"))
- if checkDirection == "forward" then
- if turtle.compare() then -- chest in front
- saveToLog("checkChest: chest found ahead, dumping mining waste")
- success = true
- --dump any excess materials
- storeMiningWaste(false)
- refuel(8) --refuel with 8 planks instead of 4
- -- suck out contents and break
- while turtle.suck() do
- saveToLog("checkChest: chest found, removing contents")
- end
- end
- elseif checkDirection == "down" then
- if turtle.compareDown() then -- chest below
- saveToLog("checkChest: chest found below, dumping mining waste")
- success = true
- storeMiningWaste(false)
- refuel(8) --refuel with 8 planks instead of 4
- -- suck out contents and break
- while turtle.suckDown() do
- saveToLog("checkChest: chest found, removing contents")
- end
- end
- elseif checkDirection == "up" then
- if turtle.compareUp() then -- chest above
- saveToLog("checkChest: chest found above, dumping mining waste")
- success = true
- storeMiningWaste(false)
- refuel(8) --refuel with 8 planks instead of 4
- -- suck out contents and break
- while turtle.suckUp() do
- saveToLog("checkChest: chest found, removing contents")
- end
- end
- end
- if success then
- dig.digNew{self = dig, direction = checkDirection, callFrom = "checkChest"}
- end
- return success
- end
- function checkReturnToBase(level, sector)
- local fuelNeeded = 0
- local fuelStats = {}
- local numTorchesNeeded = 0
- local numTorchesToCraft = 0
- currentFunction = "checkReturnToBase"
- if level > 15 then
- if sector == 1 then
- numTorchesNeeded = 22
- numTorchesToCraft = 24
- elseif sector == 2 then
- numTorchesNeeded = 15
- numTorchesToCraft = 16
- elseif sector == 3 then
- numTorchesNeeded = 7
- numTorchesToCraft = 8
- else
- numTorchesNeeded = 0
- end
- else -- diamond levels
- numTorchesNeeded = 22
- if sector == 1 then -- how many torches needed for sector 2 and above
- numTorchesNeeded = 14
- numTorchesToCraft = 16
- elseif sector == 2 then
- numTorchesNeeded = 9
- numTorchesToCraft = 12
- elseif sector == 3 then
- numTorchesNeeded = 4
- numTorchesToCraft = 4
- else
- numTorchesNeeded = 0
- end
- end
- if getNoOfValuableSlots() >= 4 or getNoOfEmptySlots() <= 4 or getStock("torches") < numTorchesNeeded then
- refuel(0)
- saveToLog("createMine: returning to base to empty loot", true, false)
- while location:getY() < coordHome:getY() do
- up(1)
- end
- storeWoodItems()
- storeSand()
- storeOres()
- storeMinerals()
- if getStock("torches") < numTorchesNeeded then
- craftTorches(numTorchesToCraft)
- end
- if not stage["createBase"..tostring(level)] then -- whole mine needs to be done
- fuelNeeded = 1020 -- 17 logs (17 * 60)
- else -- find out how many stages completed
- if not stage["mineSector1"] then
- fuelNeeded = 1020
- elseif not stage["mineSector2"] then
- fuelNeeded = 760
- elseif not stage["mineSector3"] then
- fuelNeeded = 510
- elseif not stage["mineSector4"] then
- fuelNeeded = 260
- end
- end
- fuelStats = getFuelAvailable()
- if fuelStats.totalFuelAvailable + fuelStats.fuelLevel < fuelNeeded then
- goToWoodStore(false)
- end
- while location:getY() > level do
- down(1)
- end
- end
- end
- function checkWaterLava(direction)
- local result = ""
- local slotNo = 0
- currentFunction = "checkWaterLava"
- for i = 16, 1, -1 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 turtle.compareDown() then --no block detected, compares to air(empty slot)
- result = "air"
- else
- result = "liquid"
- end
- end
- elseif direction == "up" then
- if not turtle.detectUp() then
- if turtle.compareUp() then
- result = "air"
- else
- result = "liquid"
- end
- end
- elseif direction == "forward" then
- if not turtle.detect() then
- if turtle.compare() then
- result = "air"
- else
- result = "liquid"
- end
- end
- end
- end
- if result == "liquid" then -- water or lava found
- turtle.select(slot:getItemSlot("buckets"))
- turtle.refuel() --empty bucket if lava left in
- if direction == "down" then
- if turtle.placeDown() then --bucket filled water or lava
- result = "lava"
- if not turtle.refuel() then --water
- turtle.placeDown() --empty bucket out
- result = "water"
- end
- end
- else
- if turtle.place() then --bucket filled water or lava
- result = "lava"
- if not turtle.refuel() then --water
- result = "water"
- turtle.place()--empty bucket out
- end
- end
- end
- end
- return result -- "", "air", "liquid", "water", "lava"
- end
- function checkWalls(itemList, calledFrom, do3D, getCobble)
- currentFunction = "checkWalls"
- for j = 1, 4 do
- mineItem(itemList, calledFrom, do3D, "") --call recursive function
- if turtle.detect() then --nothing found on mineItem
- dig.digNew{self = dig, direction = "forward", checkForItems = "coal,sand", callFrom = "checkWalls"}
- end
- refuel(0)
- turnRight(1)
- end
- return success
- end
- function clearBase()
- currentFunction = "clearBase"
- callingFunction = "main"
- local numLadders = 0
- --[[
- 5 C . C . C
- 4 . . . . .
- 3 C . F . C
- 2 . . X . .
- 1 C . . . C
- A B C D E
- ]]--
- if not stage["clearBase"] then
- -- 9 signs and some ladders left
- saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
- " to logClearBase.txt END OF LOGFILE!", false, false)
- fso:useFileName("logClearBase.txt")
- saveToLog("clearBase: checking if furnace needs crafting", false, false)
- if not stage["craftFurnaces"] then
- if craft{calledFrom = "clearBase", craftItem = "furnaces", craftQuantity = 1, sourceItem1 = "cobble", destSlot = 0, doSort = true} then
- saveToLog("Crafted 1 furnace.")
- else
- saveToLog("Crafting furnace did not succeed")
- error()
- end
- stage["craftFurnaces"] = true
- end
- --clear area around starting point 5 X 5 SQUARE
- if not stage["cobblePlaced"] then
- if stage["startUnderground"] then -- could be underground
- go("FuxC FuxCR FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxCR FuxC FuxC FuxCL FuxC FuxCL FuxC FuxCL FuxCR B", "coal,sand")
- else
- go("FuxC FuxCR FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxC FuxC FuxC FuxCR FuxCR FuxC FuxC FuxCL FuxC FuxCL FuxC FuxCL FuxCR B", "wood,saplings")
- end
- stage["cobblePlaced"] = true
- end
- --place ladder at top of pile
- go("RRDd")
- turtle.select(slot:getItemSlot("ladders"))
- if not turtle.compare() then
- turtle.place()
- end
- go("URR")
- sortInventory(true) --C3
- forward(2) -- 2 blocks in front of furnace site (C5)
- placeChest("down","wood") --C5
- turtle.select(slot:getItemSlot("wood"))
- if not turtle.compareUp() then
- turtle.placeUp()
- end
- go("RFFL") --E5
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.compareUp() then
- turtle.placeUp()
- end
- placeChest("down", "sticks") --E5
- go("RRFF") --E3
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.compareUp() then
- go("URR") --E3
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Sticks\nOnly")
- go("DRRuC") --still on E3
- end
- placeChest("down", "building")
- go("FF") -- 2 blocks behind furnace, 2 to right (E1)
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.compareUp() then
- go("URR") --E1
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Dirt\nCobble\nGravel\nSand")
- go("DRRuC") --still on E1
- end
- placeChest("down", "ores")
- go("RFFURR") -- C1
- if getStock("signs") > 0 then
- turtle.select(slot:getItemSlot("signs"))
- if not turtle.detect() then
- turtle.place("Iron & Gold ores\nObsidian\nMossy Cobble")
- end
- end
- go("RRDFFRFFFF") --A5
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.compareUp() then
- turtle.placeUp()
- end
- placeChest("down", "torches")
- go("BB") --A3
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.compareUp() then
- go("U") --A3
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Ladders, planks\nfences and\nother wooden\nitems only")
- go("DRRuC") --still on A3
- end
- placeChest("down", "minerals")
- go("FFRR") -- A1
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.compareUp() then
- go("U") --A1
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Odds\nand\nSods")
- go("DuC") --still on A1
- end
- placeChest("down", "misc")
- go("RFFURR") --C1
- turtle.select(slot:getItemSlot("signs"))
- if not turtle.compare() then
- turtle.place("Redstone\nLapis\nDiamonds\nCoal")
- end
- go("DRFF") -- C3
- if not turtle.detectUp() then
- up(1)
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("If turtle here\nAdd 64 wood\nto chest.\nNo other items!")
- slot.update{self = slot, item = "signs", delete = true}
- down(1)
- sortInventory(true)
- end
- --put furnace above
- if getStock("furnaces") > 0 then
- turtle.select(slot:getItemSlot("furnaces"))
- turtle.placeUp()
- saveToLog("clearBase: furnace placed", true, true)
- slot.update{self = slot, item = "furnaces", delete = true}
- end
- storeSigns()
- -- should be some ladders in stock. If surface is average level 64 then need say 75 altogether, and torches
- if coordHome:getY() > 54 then --more ladders needed
- numLadders = coordHome:getY() - 54
- end
- if numLadders > 0 then
- craftLadders(math.ceil(numLadders / 3) * 3) -- eg 20 needed, 20 / 3 * 3 = 6 * 3 = 18
- end
- currentFunction = "clearBase"
- craftTorches(8)
- currentFunction = "clearBase"
- go("FR FRUxT FFRxT FFRxT FFRRxT FDLFL") -- place 4 torches
- changeDirection("faceBackward")
- stage["clearBase"] = true
- saveStatus()
- displaySaveBackup()
- end
- end
- function completeMineshaft()
- local dirtAmount = 0
- local waterlava = ""
- local itemList = "coal,redstone,lapis"
- currentFunction = "completeMineshaft"
- callingFunction = "main"
- if not stage["completeMineshaft"] then
- saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
- " to logCompleteMineshaft.txt END OF LOGFILE!", false, false)
- fso:useFileName("logCompleteMineshaft.txt")
- saveToLog("completeMineshaft: starting", false, false)
- --store excess sticks in sticks chest
- storeSticks()
- -- deposit excess dirt, cobble, gravel, sand in chest
- dirtAmount = getStock("dirt")
- if dirtAmount > 40 then
- dirtAmount = dirtAmount - 40
- else
- dirtAmount = 0
- end
- storeBuildingBlocks{dirt = dirtAmount, cobble = 0, gravel = -1, sand = 64}
- -- deposit any unknown items in ore chest
- storeWoodItems()
- storeSand()
- storeOres()
- -- if started below level 19, cobble mined from above
- changeDirection("faceBackward")
- turtle.select(slot:getItemSlot("ladders"))
- down(1)
- while turtle.compare() do
- saveToLog("completeMineshaft: ladder in front")
- for i = 1, 3 do
- turnRight(1)
- if not turtle.detect() then
- waterlava = checkWaterLava("forward")
- if waterlava ~= "air" then --water or lava present
- saveToLog("completeMineshaft: "..waterlava.." in front")
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.place()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.place()
- end
- end
- end
- end
- turnRight(1)
- down(1)
- turtle.select(slot:getItemSlot("ladders"))
- end
- up(1) -- no more ladders in front, move up ready to continue down
- repeat
- waterlava = checkWaterLava("down")
- if waterlava ~= "" and waterlava ~= "air" then -- water, liquid or lava found below
- if down(1) then --not at bedrock
- if waterlava == "lava" then
- --build cobble shield and add ladder
- go("RR") -- face area opposite ladder
- if not turtle.detect() then
- if checkWaterLava("forward") == "lava" then -- lava present
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.place()
- end
- end
- turnRight(1)
- if not turtle.detect() then
- if checkWaterLava("forward") == "lava" then -- lava present
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.place()
- end
- end
- turnLeft(2)
- if not turtle.detect() then
- if checkWaterLava("forward") == "lava" then -- lava present
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.place()
- end
- end
- turnLeft(1)
- if forward(1) then -- in ladder section, below existing ladder
- turnLeft(1)
- if not turtle.detect() then
- if checkWaterLava("forward") == "lava" then -- lava present
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.place()
- end
- end
- turnRight(2)
- if not turtle.detect() then
- if checkWaterLava("forward") == "lava" then -- lava present
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.place()
- end
- end
- turnLeft(1)
- back(1)
- if getStock("ladders") > 1 then
- turtle.select(slot:getItemSlot("ladders"))
- while not turtle.place() do
- while attack() do --in case mobs in front
- saveToLog("Mob attacked again!", false)
- end
- end
- saveToLog("completeMineshaft: ladder placed at "..location:getY())
- end
- end
- end
- else -- hit bedrock
- break
- end
- else -- air or block below
- dig.digNew{self = dig, direction = "down", callFrom = "mineToBedrock", checkForItems = itemList}
- if down(1) then
- saveToLog("completeMineshaft: gone down. placing ladder in front")
- --place ladder
- if turtle.detect() then -- block in front
- if dig.digNew{self = dig, callFrom = "mineToBedrock", checkForItems = itemList} then
- saveToLog("completeMineshaft: block cleared for cobble replacement")
- end
- -- dig = false can be gravel-> flint
- end
- if not turtle.detect() then --block dug successfully
- if forward(1) then --not bedrock
- saveToLog("completeMineshaft: moved forward for placing cobble")
- dig.digNew{self = dig, waitForGravel = true, checkForItems = itemList}
- if not turtle.detect() then
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- while not turtle.place() do
- while attack() do --in case mobs in front
- saveToLog("Mob attacked again!", false)
- end
- end
- saveToLog("completeMineshaft: cobble placed for ladder")
- else
- saveToLog("completeMineshaft: bedrock in front for ladder")
- end
- back(1)
- if getStock("ladders") > 1 then
- turtle.select(slot:getItemSlot("ladders"))
- while not turtle.place() do
- while attack() do --in case mobs in front
- saveToLog("Mob attacked again!", false)
- end
- end
- saveToLog("completeMineshaft: ladder placed at "..location:getY())
- end
- -- now check for water/lava on sides
- for i = 1, 3 do
- turnRight(1)
- if not turtle.detect() then
- waterlava = checkWaterLava("forward")
- if waterlava ~= "air" then --water, liquid or lava present
- saveToLog("completeMineshaft: "..waterlava.." in front")
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.place()
- end
- end
- end
- turnRight(1) --return to correct position
- else
- break
- end
- else --bedrock in front
- break
- end
- else -- hit bedrock
- break
- end
- end
- until location:getY() == 1
- if getStock("torches") > 0 then
- saveToLog("completeMineshaft: at bedrock. placing torch")
- up(1)
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- slot.update{self = slot, item = "torches", delete = true}
- end
- while location:getY() < coordHome:getY() do
- up(1, 1)
- end
- dirtAmount = getStock("dirt")
- if dirtAmount > 40 then
- dirtAmount = dirtAmount - 40
- else
- dirtAmount = 0
- end
- storeBuildingBlocks{dirt = dirtAmount, cobble = 0, gravel = -1, sand = 64}
- storeLadders()
- stage["completeMineshaft"] = true
- saveStatus()
- displaySaveBackup()
- end
- end
- function craft(arg)
- local tempCurrentFunction = currentFunction
- callingFunction = arg.calledFrom
- --[[
- Examples:
- make planks: craft{craftItem = "planks", craftQuantity = 8, sourceItem1 = "wood", destSlot = 3, chest = "doNotUse"}
- 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 = {}
- local turns = 0
- 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
- while turtle.detect() do --check for clear space
- turnRight(1)
- turns = turns + 1
- if turns == 4 then
- turns = 0
- break
- end
- end
- turtle.select(1)
- while turtle.detect() do --clear space in front
- dig.digNew{self = dig, slotNo = 1, callFrom = "craft"}
- end
- while attack() do --in case mobs in front
- saveToLog("Mob attacked again!", false)
- end
- turtle.select(slot:getItemSlot("chests"))
- while not turtle.place() do
- while attack() do --in case mobs in front
- saveToLog("Mob attacked again!", false)
- end
- end
- currentFunction = "craft"
- --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: Filling crafting chest..", false)
- saveStatus()
- fillChest{direction = arg.direction, exceptSlot1 = sourceSlot1, exceptSlot2 = sourceSlot2, exceptSlot3 = sourceSlot3, doReset = false} -- fill except chosen item(s)
- saveStatus()
- 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 == "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 == "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 == "ladders" then --craft{craftItem = "ladders", craftQuantity = 3, sourceItem1 = "sticks", destSlot = 0}
- -- 7 sticks = 3 ladders
- turtle.transferTo(1, arg.craftQuantity / 3)
- turtle.transferTo(3, arg.craftQuantity / 3)
- turtle.transferTo(5, arg.craftQuantity / 3)
- turtle.transferTo(6, arg.craftQuantity / 3)
- turtle.transferTo(7, arg.craftQuantity / 3)
- turtle.transferTo(9, arg.craftQuantity / 3)
- turtle.transferTo(11, arg.craftQuantity / 3)
- gridContents[1] = arg.sourceItem1
- gridContents[3] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridContents[7] = arg.sourceItem1
- gridContents[9] = arg.sourceItem1
- gridContents[11] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[3] = sourceSlot1
- gridSlot[5] = sourceSlot1
- gridSlot[6] = sourceSlot1
- gridSlot[7] = sourceSlot1
- gridSlot[9] = sourceSlot1
- gridSlot[11] = sourceSlot1
- elseif arg.craftItem == "stairs" then --craft{craftItem = "stairs", craftQuantity = 4, sourceItem1 = "cobble", destSlot = 0}
- --6 cobble = 4 stairs
- turtle.transferTo(1, arg.craftQuantity / 4)
- turtle.transferTo(5, arg.craftQuantity / 4)
- turtle.transferTo(6, arg.craftQuantity / 4)
- turtle.transferTo(9, arg.craftQuantity / 4)
- turtle.transferTo(10, arg.craftQuantity / 4)
- turtle.transferTo(11, arg.craftQuantity / 4)
- gridContents[1] = arg.sourceItem1
- gridContents[5] = arg.sourceItem1
- gridContents[6] = arg.sourceItem1
- gridContents[9] = arg.sourceItem1
- gridContents[10] = arg.sourceItem1
- gridContents[11] = arg.sourceItem1
- gridSlot[1] = sourceSlot1
- gridSlot[5] = sourceSlot1
- gridSlot[6] = sourceSlot1
- gridSlot[9] = sourceSlot1
- gridSlot[10] = sourceSlot1
- gridSlot[11] = sourceSlot1
- 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
- saveStatus()
- 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
- saveStatus()
- 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"}
- currentFunction = tempCurrentFunction
- 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.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
- saveStatus()
- if turns > 0 then
- turnLeft(turns)
- end
- saveToLog("craft: function exit, success = "..tostring(success), false)
- return success
- end
- function craftChests(quantity)
- local previousCallingFunction = callingFunction
- local previousFunction = currentFunction
- local success = false
- local makePlanks = false
- local woodNeeded = 0
- local planksNeeded = 0
- local numWoodOnboard = 0
- local numPlanksOnboard = 0
- currentFunction = "craftChests"
- planksNeeded = 8 * quantity
- woodNeeded = 2 * quantity
- changeDirection("faceForward")
- if slot:getItemSlot("planks") > 0 then
- numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks"))
- end
- if numPlanksOnboard < planksNeeded then
- makePlanks = true
- saveToLog("craftChests: started, crafting planks", true)
- --woodNeeded already calculated
- else
- woodNeeded = 0 --reset
- saveToLog("craftChests: started, using planks already onboard", true)
- end
- if makePlanks then --need wood
- if slot:getItemSlot("wood") > 0 then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- if numWoodOnboard - 1 >= woodNeeded then
- saveToLog("craftChests: crafting planks for chests", true)
- craft{calledFrom = "craftChests",craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
- else
- goToWoodStore(false)
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- end
- saveToLog("craftChests: crafting "..quantity.." chests from planks", true)
- if craft{calledFrom = "craftChests", craftItem = "chests", craftQuantity = quantity, sourceItem1 = "planks", destSlot = 0, doSort = true} then
- success = true
- end
- callingFunction = previousCallingFunction
- currentFunction = previousFunction
- return success
- end
- function craftLadders(quantity)
- local woodAvailable = 0
- local success = false
- local numSticksOnboard = 0
- local numLaddersOnboard = 0
- local numWoodOnboard = 0
- local numWood2Onboard = 0
- local numWoodNeeded = 0
- local numRequested = 0
- local previousCallingFunction = callingFunction
- local previousFunction = currentFunction
- currentFunction = "craftLadders"
- if quantity > 27 then
- quantity = 27
- end
- numRequested = quantity
- if quantity <= 21 then
- numWoodNeeded = math.floor(quantity / 3)
- elseif quantity == 24 then
- numWoodNeeded = 7
- else
- numWoodNeeded = 8
- end
- -- 7 sticks = 3 ladders
- -- 1 wood = 4 planks
- -- 4 planks = 8 sticks
- -- 7 sticks = 3 ladders
- -- ladders sticks planks wood
- -- 3 7 4 1
- -- 6 14 8 2
- -- 9 21 12 3
- -- 12 28 14 4
- -- 15 35 18 5
- -- 18 42 22 6
- -- 21 49 26 7
- -- 24 54 28 7
- -- 27 63 32 8
- saveToLog("craftLadders: started, crafting "..quantity.."ladders", true)
- changeDirection("faceForward")
- numSticksOnboard = getStock("sticks")
- numLaddersOnboard = getStock("ladders")
- numWoodOnboard = getStock("wood")
- numWood2Onboard = getStock("wood-1")
- if numWoodOnboard == 0 then --wood in storage, needed for ladders
- goToWoodStore(false)
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- if numWoodOnboard >= 1 or numWood2Onboard >= 1 then --enough to make ladders
- if numWoodNeeded > numWoodOnboard + numWood2Onboard then
- numWoodNeeded = numWoodOnboard + numWood2Onboard
- quantity = numWoodOnboard * 3
- end
- if numWood2Onboard > 0 then
- if numWoodNeeded <= numWood2Onboard then --enough wood-1 for all ladders
- saveToLog("craftLadders: crafting planks from wood-1", false)
- craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = numWoodNeeded * 4, sourceItem1 = "wood-1", destSlot = 0, doSort = true}
- saveToLog("craftLadders: crafting sticks from planks", false)
- craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = numWoodNeeded * 8, sourceItem1 = "planks", destSlot = 0, doSort = true}
- saveToLog("craftLadders: crafting ladders from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
- if craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = quantity, sourceItem1 = "sticks", destSlot = 0, doSort = true} then
- success = true
- end
- else --only enough for some ladders
- --make all wood-1 into sticks
- saveToLog("craftLadders: crafting planks from wood-1", false)
- craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = getStock("wood-1") * 4, sourceItem1 = "wood-1", destSlot = 0, doSort = true}
- saveToLog("craftLadders: crafting sticks from planks", false)
- craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = getStock("planks") * 2, sourceItem1 = "planks", destSlot = 0, doSort = true}
- craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = math.floor(getStock("sticks") / 7) * 3, sourceItem1 = "sticks", destSlot = 0, doSort = true}
- quantity = quantity - getStock("ladders")
- if quantity > 0 then
- numWoodNeeded = math.floor(quantity / 3)
- saveToLog("craftLadders: crafting planks from wood", false)
- craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = numWoodNeeded * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
- saveToLog("craftLadders: crafting sticks from planks", false)
- craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = numWoodNeeded * 8, sourceItem1 = "planks", destSlot = 0, doSort = true}
- saveToLog("craftLadders: crafting ladders from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
- craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = quantity, sourceItem1 = "sticks", destSlot = 0, doSort = true}
- end
- end
- else
- saveToLog("craftLadders: crafting planks from wood", false)
- craft{calledFrom = "craftLadders", craftItem = "planks", craftQuantity = numWoodNeeded * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
- saveToLog("craftLadders: crafting sticks from planks", false)
- craft{calledFrom = "craftLadders", craftItem = "sticks", craftQuantity = numWoodNeeded * 8, sourceItem1 = "planks", destSlot = 0, doSort = true}
- saveToLog("craftLadders: crafting ladders from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
- craft{calledFrom = "craftLadders", craftItem = "ladders", craftQuantity = quantity, sourceItem1 = "sticks", destSlot = 0, doSort = true}
- end
- if getStock("ladders") >= numRequested then
- success = true
- end
- else
- saveToLog("craftLadders: insufficient wood available", true)
- end
- callingFunction = previousCallingFunction
- currentFunction = previousFunction
- return success
- end
- function craftSigns(quantity)
- local success = false
- local numWoodOnboard = 0
- local numWoodNeeded = 0
- local numPlanksNeeded = 0
- local previousCallingFunction = callingFunction
- local previousFunction = currentFunction
- currentFunction = "craftSigns"
- quantity = quantity or 3
- if quantity > 12 then
- quantity = 12
- end
- --make 3 signs by default , need 8 planks, leaves 3 sticks
- --[[
- signs planks sticks wood
- 3 6 + 2 1 2
- 6 12 + 2 2 4
- 9 18 + 2 3 5
- 12 24 + 2 4 7
- ]]--
- saveToLog("craftSigns: started, crafting "..quantity.." signs", true)
- if quantity == 3 then
- numWoodNeeded = 2
- numPlanksNeeded = 8
- elseif quantity == 6 then
- numWoodNeeded = 4
- numPlanksNeeded = 16
- elseif quantity == 9 then
- numWoodNeeded = 5
- numPlanksNeeded = 20
- else
- numWoodNeeded = 7
- numPlanksNeeded = 28
- end
- changeDirection("faceForward")
- numWoodOnboard = getStock("wood")
- if numWoodOnboard < numWoodNeeded then
- saveToLog("craftSigns: insufficient wood available", true)
- goToWoodStore(false)
- end
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- saveToLog("craftSigns: crafting planks from wood", false)
- craft{calledFrom = "craftSigns", craftItem = "planks", craftQuantity = numPlanksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = false}
- saveToLog("craftSigns: crafting sticks from planks", false)
- craft{calledFrom = "craftSigns", craftItem = "sticks", craftQuantity = 4, sourceItem1 = "planks", destSlot = 0, doSort = false}
- saveToLog("craftSigns: crafting signs from planks and sticks from slot "..slot.getItemSlot(slot, "sticks"), false)
- if craft{calledFrom = "craftSigns", craftItem = "signs", craftQuantity = quantity, sourceItem1 = "sticks", sourceItem2 = "planks", destSlot = 0, doSort = true} then
- success = true
- end
- if getStock("sticks") > 0 then
- storeSticks()
- end
- if getStock("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- sortInventory(true)
- end
- callingFunction = previousCallingFunction
- currentFunction = previousFunction
- return success
- end
- function craftSticks(quantity)
- local success = false
- local makePlanks = false
- local woodNeeded = 0
- local planksNeeded = 0
- local numWoodOnboard = 0
- local numPlanksOnboard = 0
- local previousCallingFunction = callingFunction
- local previousFunction = currentFunction
- currentFunction = "craftSticks"
- 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
- changeDirection("faceForward")
- if slot:getItemSlot("planks") > 0 then
- numPlanksOnboard = turtle.getItemCount(slot:getItemSlot("planks"))
- end
- if numPlanksOnboard < planksNeeded then
- makePlanks = true
- saveToLog("craftSticks: started, crafting planks", true)
- --woodNeeded already calculated
- else
- woodNeeded = 0 --reset
- saveToLog("craftSticks: started, using planks already onboard", true)
- end
- if makePlanks then --need wood
- if slot:getItemSlot("wood") > 0 then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- if numWoodOnboard >= woodNeeded then
- saveToLog("craftSticks: crafting planks for sticks", true)
- craft{calledFrom = "craftSticks", craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
- else
- goToWoodStore(false)
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- end
- saveToLog("craftSticks: crafting "..quantity.." sticks from planks", true)
- if craft{calledFrom = "craftSticks", craftItem = "sticks", craftQuantity = quantity, sourceItem1 = "planks", destSlot = 0, doSort = true} then
- stage["craftSticks"] = true
- saveStatus()
- success = true
- end
- callingFunction = previousCallingFunction
- currentFunction = previousFunction
- return success
- end
- function craftTorches(quantity)
- local headType = "charcoal"
- local headQuantity = 0
- local makeCharcoal = false
- local makePlanks = false
- local makeSticks =false
- 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 doContinue = true
- local previousCallingFunction = callingFunction
- local previousFunction = currentFunction
- currentFunction = "craftTorches"
- --changeDirection("faceForward")
- getSticks()
- numTorchesOnboard = getStock("torches")
- numSticksOnboard = getStock("sticks")
- -- 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
- if numSticksOnboard < 4 then
- planksNeeded = 4 -- 1 wood
- sticksNeeded = 4 -- 2 planks
- numWoodNeeded = 1
- end
- elseif quantity <= 16 then-- 4, 8, 12, 16
- headQuantity = quantity / 4 -- coal, charcoal
- if numSticksOnboard < 4 then
- planksNeeded = 4 -- 8 planks = 16 sticks
- sticksNeeded = 4 -- 4 sticks
- numWoodNeededwoodNeeded = 1
- end
- elseif quantity <= 32 then-- 4, 8, 12, 16
- headQuantity = quantity / 4 -- coal, charcoal
- if numSticksOnboard < 8 then
- planksNeeded = 4 -- 8 planks = 16 sticks
- sticksNeeded = 8 -- 8 sticks
- numWoodNeeded = 2
- end
- elseif quantity <= 48 then-- 4, 8, 12, 16
- headQuantity = quantity / 4 -- coal, charcoal
- if numSticksOnboard < 12 then
- planksNeeded = 8 -- 8 planks = 16 sticks
- sticksNeeded = 12 -- 12 sticks
- numWoodNeeded = 2
- end
- else
- headQuantity = quantity / 4 -- coal, charcoal
- if numSticksOnboard < 16 then
- planksNeeded = 8 -- 8 planks = 16 sticks
- sticksNeeded = 16 -- 16 sticks
- numWoodNeeded = 2
- end
- end
- --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
- numWoodNeeded = numWoodNeeded + headQuantity
- end
- numWoodOnboard = getStock("wood")
- if getStock("wood") < 1 + numWoodNeeded then
- goToWoodStore(false)
- end
- saveToLog("craftTorches: quantity = "..quantity, true)
- getSticks()
- if slot:getItemSlot("wood") == 0 and makeCharcoal then --wood in storage, needed for charcoal
- goToWoodStore(false)
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- 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
- if numWoodOnboard > 0 then
- saveToLog("craftTorches: crafting sticks")
- doContinue = false
- if craftSticks(sticksNeeded) then
- doContinue = true
- end
- if slot:getItemSlot("wood") > 0 then
- numWoodOnboard = turtle.getItemCount(slot:getItemSlot("wood"))
- end
- end
- end
- if numWoodOnboard == 0 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{calledFrom = "craftTorches", craftItem = "torches", craftQuantity = quantity, sourceItem1 = "sticks", sourceItem2 = headType, destSlot = 0, doSort = true} then
- saveToLog("craftTorches: "..quantity.." torches made", true)
- success = true
- else
- saveToLog("craftTorches: error: no torches made", true)
- error()
- end
- end
- end
- storeSticks()
- if getStock("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- sortInventory(true)
- end
- callingFunction = previousCallingFunction
- currentFunction = previousFunction
- return success
- 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","ladders","gates","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","cobble-1","cobble-2","cobble-3","cobble-4"
- ,"cobble-5","cobble-6","dirt-1","dirt-2","dirt-3","dirt-4","gravel-1","gravel-2","redstone-1","redstone-2","redstone-3","wood-1", "stairs","coal-1","coal-2"}
- 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.getStatus(self)
- local foundItemSlots = {}
- for key, value in ipairs(self.items) do
- if self.itemSlot[value] > 0 then
- foundItemSlots[value] = self.itemSlot[value]
- end
- end
- return {self.slotContains, self.slotCount, foundItemSlots, self.turtleSlot}
- end
- function clsChest.setStatus(self, status)
- self.slotContains = status[1]
- self.slotCount = status[2]
- for key, value in ipairs(status[3]) do
- self.itemSlot[value] = status[3][value]
- end
- self.turtleSlot = status[4]
- end
- function clsChest.printContents(self)
- --debug log
- local printSlot = 0
- for i = 1, 16 do
- if self.slotCount[i] > 0 then
- saveToLog(" clsChest: Slot "..i.." contains "..self.slotCount[i].." "..self.slotContains[i])
- end
- end
- 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")
- storageWoodItems = clsChest.new(1, "storageWoodItems") -- placed 2 blocks in front and 2 blocks to left of furnace
- storageWood = clsChest.new(2, "storageWood") -- placed 2 blocks in front of furnace
- storageSticks = clsChest.new(3, "storageSticks") -- placed 2 blocks in front and 2 blocks to right of furnace
- storageBuilding = clsChest.new(4, "storageBuilding") -- placed 2 blocks to left of furnace
- storageOres = clsChest.new(5, "storageOres") -- placed 2 blocks to right of furnace
- storageMinerals = clsChest.new(6, "storageMinerals") -- placed 2 blocks behind and 2 blocks to left of furnace
- storageMisc = clsChest.new(7, "storageMisc") -- placed 2 blocks behind and 2 blocks to right of furnace
- end
- function createCoordObject()
- currentFunction = "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")
- mineTopLevel = clsCoord.new("mineTopLevel")
- end
- function createDigObject()
- -- 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.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.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
- --remove any mobs
- clsDig.attack(self)
- --mobs disposed of, now get on with digging
- clsDig.doDig(self) -- will set self.success
- --dig successful if dug large quantity then 2 slots will be updated, original + next available
- if self.success then --process dig
- --[[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 "..self.direction.." , 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.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
- local addQualifier = ""
- local itemTitle = ""
- 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. eg 58 redstone in slot, 4 more dug, fills original slot
- end
- end
- end
- --dugSlot set above
- if self.dugSlot > 0 then --item dug.
- success = true
- if self.slotNo > 1 or self.expectedItem ~= "" then --dug into specific slot, so could match existing item, or placed and re-dug
- -- 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
- if self.dugSlot2 > 0 then -- dug items has filled original slot, then moved into next available, or redstone, lapis, coal overfilled slot
- -- dugAmount = 1 or 63 in slot >- 64 + excess into another 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 --compares to existing item
- saveToLog("dig.setDugSlotAndItem: second slot compares to slot "..i, false, true)
- if string.find(slot:getSlotContains(i), "-") == nil then -- eg "cobble"
- itemTitle = slot:getSlotContains(i)
- else
- itemTitle = string.sub(slot:getSlotContains(i), 1, string.find(slot:getSlotContains(i), "-") - 1)
- end
- for j = 1, 6 do
- if slot:getItemSlot(itemTitle.."-"..j) == 0 then
- addQualifier = tostring("-"..j)
- break
- end
- end
- self.dugItem = itemTitle..addQualifier
- self.itemKnown = true
- self.updateType = "newItem"
- self.dugSlot = self.dugSlot2
- self.dugSlot2 = 0
- saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " contains "..self.dugItem, true)
- break
- end
- end
- end
- else -- not dug into more than 1 slot, but could be 64 in one slot, new item discovered
- if slot:getSlotContains(self.dugSlot) == "" then -- not known. compare to other slots before allocating new item
- turtle.select(self.dugSlot)
- for i = 1, 16 do
- if i ~= self.dugSlot then
- if turtle.compareTo(i) then --compare if excess material just dug
- saveToLog("dig.setDugSlotAndItem: self.dugSlot compares to slot "..i, false, true)
- if string.find(slot:getSlotContains(i), "-") == nil then -- eg "cobble"
- itemTitle = slot:getSlotContains(i)
- else
- itemTitle = string.sub(slot:getSlotContains(i), 1, string.find(slot:getSlotContains(i), "-") - 1)
- end
- for j = 1, 6 do
- if slot:getItemSlot(itemTitle.."-"..j) == 0 then
- addQualifier = tostring("-"..j)
- break
- end
- end
- self.dugItem = itemTitle..addQualifier
- self.itemKnown = true
- self.updateType = "newItem"
- saveToLog("dig.setDugSlotAndItem: second slot "..self.dugSlot2.. " contains "..self.dugItem, true)
- break
- end
- end
- end
- if self.dugItem == "" then
- self.dugItem = slot:getFreeUnknownItem()
- self.itemKnown = false
- self.updateType = "newItem"
- end
- 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
- end
- success = true -- item dug
- 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 or gravel is full
- turtle.select(self.dugSlot)
- if turtle.compareTo(slot:getItemSlot("gravel")) then
- if slot:getItemSlot("gravel-1") > 0 then
- self.dugItem = "gravel-2"
- self.itemKnown = true
- item = "gravel-2"
- else
- self.dugItem = "gravel-1"
- self.itemKnown = true
- item = "gravel-1"
- end
- saveToLog("dig.confirmGravel: "..item.." found", false, true)
- else
- saveToLog("dig.confirmGravel: flint dug when gravel detected", false, true)
- self.dugItem = "flint"
- item = "flint"
- end
- 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
- local usedCobble = false
- -- 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
- saveToLog("dig.checkForItem: checking for redstone or lapis", true, false)
- if slot:getItemSlot("redstone") > 0 then
- self.dugItem = "lapis"
- itemStatus = "newItem"
- saveToLog("dig.checkForItem: lapis identified as redstone already known", false, true)
- else
- if not turtle.detect() then -- space in front
- while not turtle.forward() do
- attack()
- end
- --must have moved forward now
- if not turtle.detectDown() then
- turtle.select(slot:getItemSlot("cobble"))
- while not turtle.placeDown() do
- attack()
- end
- usedCobble = true
- saveToLog("dig.checkForItem: placing cobble down", true, false)
- end
- turtle.turnRight()
- turtle.turnRight()
- while not turtle.forward() do
- attack()
- end
- turtle.turnRight()
- turtle.turnRight()
- 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
- --[[if usedCobble then
- if turtle.forward() then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.digDown()
- turtle.back()
- saveToLog("dig.checkForItem: retrieving cobble ", true, false)
- end
- end]]--
- end
- end
- end
- else -- single item from dig
- -- check for coal, flint, saplings, 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("findCobble, buildStaircase, completeMineshaft", self.callFrom) ~= nil or string.find(self.callFrom, "mine") ~= 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 -- not coal so destroy
- turtle.refuel(0)
- saveToLog("dig.checkForItem: saplings or wood2 found and used for fuel", false)
- self.dugSlot = 0
- self.dugItem = ""
- itemStatus = "deleteItem"
- 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 == "go" 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
- if location:getY() > 51 then
- while turtle.dropUp() do
- saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
- end
- self.dugSlot = 0
- self.dugItem = ""
- itemStatus = "deleteItem"
- else
- itemStatus = "newItem"
- end
- end
- elseif string.find("findCobble, buildStaircase, completeMineshaft", self.callFrom) ~= nil or string.find(self.callFrom, "mine") ~= nil then --look for diamonds
- saveToLog("dig.checkForItem: new item cannot be identified in slot "..self.dugSlot, false)
- itemStatus = "newItem"
- 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
- if location:getY() > 51 then
- while turtle.dropUp() do
- saveToLog("dig.checkForItem: dropping seeds or flowers from slot "..self.dugSlot, false)
- end
- self.dugSlot = 0
- self.dugItem = ""
- itemStatus = "deleteItem"
- else
- itemStatus = "newItem"
- end
- 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
- 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
- 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 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 "superminer.ini" on the local computer:
- onServer=true/false
- usePastebin=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 = 22 -- change if more logfiles needed
- self.logFiles = {}
- self.logFileExists = {}
- self.boolIniFileExists = false
- self.boolRecoveryFileExists = false
- self.useVerbose = false
- self.onServer = false
- self.usePastebin = false
- self.useRecoveryFile = false
- self.logFiles[1] = "logGetSupplies.txt"
- self.logFiles[2] = "logGetCoords.txt"
- self.logFiles[3] = "logGetReady.txt"
- self.logFiles[4] = "logFindCobble.txt"
- self.logFiles[5] = "logClearBase.txt"
- self.logFiles[6] = "logCompleteMineshaft.txt"
- self.logFiles[7] = "logCreateMine51.txt"
- self.logFiles[8] = "logCreateMine48.txt"
- self.logFiles[9] = "logCreateMine45.txt"
- self.logFiles[10] = "logCreateMine42.txt"
- self.logFiles[11] = "logCreateMine39.txt"
- self.logFiles[12] = "logCreateMine36.txt"
- self.logFiles[13] = "logCreateMine33.txt"
- self.logFiles[14] = "logCreateMine30.txt"
- self.logFiles[15] = "logCreateMine27.txt"
- self.logFiles[16] = "logCreateMine24.txt"
- self.logFiles[17] = "logCreateMine21.txt"
- self.logFiles[18] = "logCreateMine18.txt"
- self.logFiles[19] = "logCreateMine15.txt"
- self.logFiles[20] = "logCreateMine12.txt"
- self.logFiles[21] = "logCreateMine9.txt"
- self.logFiles[22] = "logCreateMine6.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("superminer.ini") and fs.getSize("superminer.ini") > 0 then
- self.boolIniFileExists = true
- iniFile = fs.open("superminer.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, "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
- if fs.exists("superminer.recover") and fs.getSize("superminer.recover") > 0 then
- self.useRecoveryFile = true
- self.boolRecoveryFileExists = true
- 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.renameLastLogFile(self, fileIndex)
- if fs.exists("recover."..self.logFiles[fileIndex]) then
- fs.delete("recover."..self.logFiles[fileIndex])
- end
- if fs.exists(self.logFiles[fileIndex]) then
- fs.move(self.logFiles[fileIndex], "recover."..self.logFiles[fileIndex])
- end
- 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.recoveryFileExists(self)
- return self.boolRecoveryFileExists
- 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.getUseVerbose(self)
- return self.useVerbose
- end
- function clsLogfile.setUseVerbose(self, setting)
- self.useVerbose = setting
- self.writeIniFile(self)
- end
- function clsLogfile.getUseRecovery(self)
- return self.useRecoveryFile
- end
- function clsLogfile.setUseRecovery(self, setting)
- self.useRecoveryFile = setting
- end
- function clsLogfile.writeIniFile(self)
- local iniFile = ""
- iniFile = fs.open("superminer.ini", "w")
- iniFile.writeLine("onServer=".. tostring(self.onServer))
- iniFile.writeLine("usePastebin=".. tostring(self.usePastebin))
- iniFile.writeLine("useLog=".. tostring(self.useLog))
- iniFile.writeLine("useVerbose=".. tostring(self.useVerbose))
- iniFile.close()
- end
- function clsLogfile.deleteIniFile(self)
- if fs.exists("superminer.ini") then
- fs.delete("superminer.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("logBranch01.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(level)
- currentFunction = "createMine"
- callingFunction = "mineAll"
- local itemList = "coal,redstone,lapis"
- local torchesAt = {}
- local plugAt = {}
- local do3D = false
- -- if mining levels above 15 use vertical and horizontal recursive mining
- if level > 15 then
- do3D = true
- end
- for i = 1, 16 do
- torchesAt[i] = false
- plugAt[i] = true
- end
- saveToLog("createMine: starting level "..level, true)
- --[[
- NSEW used in relative terms for comments, NOT real compass directions
- go down to chosen level and create + shape,
- 16x2 front/rear arm (N/S)
- 16x1 side arms
- chest in floor to NW of mineshaft
- ladder to S of mineshaft on cobble column
- torches at each end, and half way along
- ]]--
- if not stage["createBase"..level] then --base not created yet
- --clear round mineshaft for player
- refuel(0)
- down(1) --now on floor level
- mineBase(1, itemList, true)
- turnRight(1)
- mineBase(1, itemList, true)
- turnRight(1)
- mineBase(4, itemList, true)
- turnRight(1)
- mineBase(2, itemList, true)
- turnRight(1)
- mineBase(3, itemList, true)
- mineBase(1, itemList, false) -- do not empty chest above
- if level == 6 then -- add another chest in ceiling on level 7
- if location:getY() == 5 then
- up(1)
- end
- go("RFFuRR")
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeUp()
- go("DFFR")
- else
- dig.digNew{self = dig, direction = "down", checkForItems = itemList}
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeDown()
- end
- storeMiningWaste(true)
- go("RFRFLFdCDdCU")
- if getStock("torches") > 1 then
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- slot.update{self = slot, item = "torches", delete = true}
- end
- back(1)
- if getStock("signs") > 0 then
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Level "..level.."\n"..location:getCompass())
- end
- go("LFR FL FdCRdCuCDdCxCLdCU B")
- if getStock("signs") > 0 then
- turtle.select(slot:getItemSlot("signs"))
- turtle.place("Temporary Sign\nStored here for\nnext staircase\nDO NOT REMOVE!")
- end
- go("LFLFLL")
- stage["createBase"..level] = true
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSector1"] then --sector1 of 4 not finished yet
- --NE Sector
- if not stage["mineSectorPart1"] then --sector Part1 not finished
- refuel(0)
- torchesAt[8] = true
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "none", do3D) -- torches 8, mining north (1)
- -- build staircase
- -- check if sign in stock. no sign = stairs already done and this is a resume
- if getStock("signs") > 0 then
- buildStaircase(level, "forward")
- else
- turnRight(1) -- at end of first corridor, facing E
- end
- -- above level 15 mine corridors, below mine flat areas
- if level <= 15 then
- go("FRuCxDxCU")
- mineStrip(level, 12, itemList) -- 1 S
- go("LFLuCxC")
- mineStrip(level, 11, itemList) -- 2 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 14, itemList) -- 3 S
- go("FRuC FuCxDxCU BxDxCdUL FuCxDxCUL FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 4 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 5 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 6 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 7 S
- go("FRRuCxDxCU FL FFFF FFFFL") -- return to chest, face S
- else
- torchesAt[8] = true
- torchesAt[16] = true
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16 moving E on top edge (2)
- turnRight(1)
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8, 15 moving S on right side(3)
- back(1)
- turnRight(1)
- mineCorridor(level, 14, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving W on lower edge (4)
- go("FFFL") -- over chest facing S
- end
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- refuel(8)
- stage["mineSectorPart1"] = true
- checkReturnToBase(level, 1)
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSectorPart2"] then
- if level <= 15 then
- forward(16)
- turnRight(1)
- forward(8)
- go("RxDxCUxT") -- place torch
- mineStrip(level, 15, itemList) -- 8 S
- go("FLxDxCUxT FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 9 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 10 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 11 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 12 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 13 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 14 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 15 N
- go("FRuCxFRxDxCUxT")
- torchesAt[8] = true
- torchesAt[16] = true
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8, 15 moving S on right side(3)
- go("RRFL")
- forward(17)
- turnLeft(1)
- else
- forward(11)
- turnRight(1)
- torchesAt[16] = false
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving E (5a)
- go("RFFFFFR")
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving W (6a)
- go("LFFFFFRFL") -- face S
- storeMiningWaste(true)
- end
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- stage["mineSectorPart2"] = true
- end
- for i = 1, 3 do
- stage["mineSectorPart"..tostring(i)] = false
- end
- stage["mineSector1"] = true
- --? return home first
- checkReturnToBase(level, 1)
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSector2"] then
- --NW Sector
- if not stage["mineSectorPart1"] then
- refuel(0)
- go("LFRF") -- over chest
- if level <= 15 then
- mineStrip(level, 14, itemList) -- 1 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 2 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 3 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 4 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 5 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 6 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 7 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 8 S
- go("FRRuCxDxCU FR FFFF FFFR")
- else
- torchesAt[8] = false
- torchesAt[16] = false
- mineCorridor(level, 15, torchesAt, plugAt, itemList, "none", do3D) -- torches none, mining north (9)
- turnLeft(1) -- at end of first corridor, facing W
- torchesAt[8] = true
- torchesAt[16] = true
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16 moving W on top edge (10)
- turnLeft(1)
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8, 16 moving S on left side(11)
- back(1)
- turnLeft(1)
- mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving E on lower edge (12)
- go("FR") -- face S
- end
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- stage["mineSectorPart1"] = true
- checkReturnToBase(level, 2)
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSectorPart2"] then
- refuel(0)
- if level <= 15 then
- go("LFR FFFF FFFF FFFF FFFFL FFFF FFFFLxDxCUxT")
- mineStrip(level, 15, itemList) -- 9 S
- go("FRxDxCUxTFRuCxDxCU")
- mineStrip(level, 15, itemList) -- 10 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 11 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 12 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 13 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 14 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 15 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 16 N
- go("FLuCxDxCU FLxDxCUxT")
- torchesAt[8] = true
- torchesAt[16] = true
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8, 16 moving S on left side(11)
- go("BLFFFFFFFFFFFFFFFFR")
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- stage["mineSectorPart1"] = true
- else
- go("LFRF") -- over chest
- forward(10)
- turnLeft(1)
- torchesAt[16] = false
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving W (13)
- go("LFFFFFL")
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving E (14)
- while location:getY() < level do
- up(1)
- end
- go("RFFFFF")
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- stage["mineSectorPart2"] = true
- end
- end
- for i = 1, 2 do
- stage["mineSectorPart"..tostring(i)] = false
- end
- stage["mineSector2"] = true
- --? return home first
- checkReturnToBase(level, 2)
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSector3"] then -- SE sector
- if not stage["mineSectorPart1"] then
- refuel(0)
- go("FRFL") --collect sign from wall
- dig.digNew{self = dig, expectedItem = "signs"}
- go("LFFLFFFFLFR") -- move to start
- torchesAt[8] = false
- torchesAt[6] = true
- mineCorridor(level, 14, torchesAt, plugAt, itemList, "none", do3D) -- torches 6, 14 mining S (17)
- torchesAt[6] = false
- torchesAt[8] = true
- torchesAt[16] = false
- -- build staircase
- --check if sign in stock. no sign = stairs already done and this is a resume
- if getStock("signs") > 0 then
- buildStaircase(level, "backward")
- else
- turnLeft(1) -- at end of first corridor, facing E
- end
- if level <= 15 then
- go("FLuCxDxCU")
- mineStrip(level, 13, itemList) -- 1 N
- go("FFFR FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 2 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 3 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 4 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 5 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 6 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 7 N
- go("FRRuCxDxCU FR FFF FFF LFRFFRFFFFRR") -- over chest
- else
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16 moving E on lower edge (18)
- turnLeft(1)
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8 moving N on right side(19)
- back(1)
- turnLeft(1)
- mineCorridor(level, 14, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving W on mid section (20)
- go("FLFRFFRFFFFRR")
- end
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- refuel(8)
- stage["mineSectorPart1"] = true
- checkReturnToBase(level, 3)
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSectorPart2"] then
- refuel(0)
- if level <= 15 then
- go("LFLFFFLFR")
- forward(14)
- turnLeft(1)
- forward(8)
- go("LxDxCUxT")
- mineStrip(level, 15, itemList) -- 8 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 9 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 10 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 11 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 12 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 13 S
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 14 N
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 15 S
- go("FLuCxDxCU FLxDxCUxT")
- torchesAt[8] = true
- torchesAt[16] = false
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "right", do3D) -- torches 8 moving N on right side(19)
- back(1)
- turnLeft(1)
- forward(15)
- go("LFRFFRFFFFRR")
- else
- go("LFLFFFLFR")
- forward(9)
- turnLeft(1)
- torchesAt[16] = false
- mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving E (21a)
- go("FLFFFFFL")
- mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving W (22a)
- while location:getY() < level do
- up(1)
- end
- go("FRFFFFLFRFFFFLL")
- end
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- stage["mineSectorPart2"] = true
- end
- for i = 1, 2 do
- stage["mineSectorPart"..tostring(i)] = false
- end
- stage["mineSector3"] = true
- checkReturnToBase(level, 3)
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSector4"] then
- -- SW sector
- if not stage["mineSectorPart1"] then
- refuel(0)
- go("LFLFFF")
- if level <= 15 then
- mineStrip(level, 13, itemList) -- 1 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 2 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 3 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 4 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 5 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 6 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 7 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 8 N
- go("FRuCxDxCU FFFF FFFL FFLL")
- else
- torchesAt[8] = false
- torchesAt[16] = false
- mineCorridor(level, 14, torchesAt, plugAt, itemList, "none", do3D) -- torches none, mining S (25)
- turnRight(1)
- torchesAt[8] = true
- torchesAt[16] = true
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, 16 moving W on lower edge (26)
- turnRight(1)
- torchesAt[16] = false
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8 moving N on left side(27)
- back(1)
- turnRight(1)
- mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8, moving E on mid section(28)
- go("FLFFFRR")
- end
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- stage["mineSectorPart1"] = true
- checkReturnToBase(level, 4)
- saveStatus()
- displaySaveBackup()
- end
- if not stage["mineSectorPart2"] then
- if level <= 15 then --diamond level
- go("LFL")
- forward(17)
- turnRight(1)
- forward(7)
- go("FRxDxCUxT")
- mineStrip(level, 15, itemList) -- 9 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 10 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 11 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 12 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 13 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 14 S
- go("FRuCxDxCU FRuCxDxCU")
- mineStrip(level, 15, itemList) -- 15 N
- go("FLuCxDxCU FLuCxDxCU")
- mineStrip(level, 15, itemList) -- 16 S
- go("FRuCxFRxDxCUxT")
- torchesAt[8] = true
- torchesAt[16] = false
- mineCorridor(level, 16, torchesAt, plugAt, itemList, "left", do3D) -- torches 8 moving N on left side(27)
- back(1)
- turnRight(1)
- forward(16)
- go("LFFFRR")
- else
- refuel(0)
- go("LFLFFF")
- forward(9)
- turnRight(1)
- torchesAt[16] = false
- mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving W (29a)
- go("FRFFFFFR")
- mineCorridor(level, 15, torchesAt, plugAt, itemList, "both", do3D) -- torches 8 moving E (30a)
- while location:getY() < level do
- up(1)
- end
- go("FLFFFFFFFFRR")
- end
- storeMiningWaste(true)
- go("LFL")
- emptyBucket()
- back(1) -- over mineshaft
- stage["mineSectorPart1"] = true
- end
- stage["mineSector4"] = true
- end
- -- update mine status
- stage["completeMine"..tostring(level)] = true
- -- reset sectors ready for next level
- for i = 1, 4 do
- stage["mineSector"..tostring(i)] = false
- end
- for i = 1, 2 do
- stage["mineSectorPart"..tostring(i)] = false
- end
- saveToLog("createMine: mine at level "..level.." completed", true, true)
- saveStatus()
- displaySaveBackup()
- 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.storage["wood"] = false
- self.storage["torches"] = false
- self.storage["sticks"] = false
- self.storage["building"] = false
- self.storage["ores"] = false
- self.storage["minerals"] = false
- self.storage["misc"] = false
- return self
- end
- function clsPlaceStorage.getStoragePlaced(self, storageName)
- --placeStorage:getStoragePlaced("wood")
- return self.storage[storageName] or false
- end
- function clsPlaceStorage.setStoragePlaced(self, storageName)
- --placeStorage:setStoragePlaced("wood")
- self.storage[storageName] = true
- end
- placeStorage = clsPlaceStorage.initialise()
- end
- function createSlotObject()
- -- slot variables
- clsSlot = {}
- clsSlot.__index = clsSlot
- function clsSlot.new(self) --creates new instance of slot object
- local self = setmetatable({}, clsSlot)
- self.index = 1
- self.slotContains = {}
- self.slotCount = {}
- self.slotStatus = {}
- 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.getStatus(self) -- used to save status
- return {self.slotContains, self.slotCount, self.slotStatus, self.itemTested,
- self.firstUnknownItem, self.lastUnknownItem, self.freeUnknownItem, self.numUnknownItems}
- end
- function clsSlot.setStatus(self, status) -- used to load status
- self.slotContains = status[1]
- self.slotCount = status[2]
- self.slotStatus = status[3]
- self.itemTested = status[4]
- self.firstUnknownItem = status[5]
- self.lastUnknownItem = status[6]
- self.freeUnknownItem = status[7]
- self.numUnknownItems = status[8]
- end
- function clsSlot.printSlotContents(self)
- --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
- 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].." of "..self.slotContains[arg.slotNo], true, 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
- end
- slot = clsSlot.new()
- end
- function displaySaveBackup()
- local numSeconds = 20
- -- during debug, pause game and use Windows explorer to copy current game save
- term.clear()
- for i = 1, 20 do
- term.setCursorPos(1,6)
- print(" Safe to pause and make save backup")
- print()
- if numSeconds > 9 then
- print(" Continuing in "..numSeconds.." seconds")
- else
- print(" Continuing in 0"..numSeconds.." seconds")
- end
- numSeconds = numSeconds - 1
- sleep(1)
- end
- term.clear()
- term.setCursorPos(1, 1)
- end
- function down(steps, useSlot, itemList, calledFrom)
- local maxTries = 2
- local success = false
- local previousFunction = currentFunction
- currentFunction = "down"
- steps = steps or 1
- useSlot = useSlot or 1
- itemList = itemList or ""
- calledFrom = calledFrom or ""
- if turtle.getFuelLevel() < steps then
- saveToLog(" ***** down: Fuel < "..steps.." *****", true, false)
- if not refuel(0) then
- saveToLog(" ***** down: refuel failed*****", true, false)
- end
- end
- 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 = 2
- 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)
- saveStatus()
- end
- end
- currentFunction = previousFunction
- return success
- end
- function emptyBucket()
- turtle.select(slot:getItemSlot("buckets"))
- if turtle.place() then --bucket emptied
- saveToLog("emptyBucket: water emptied from bucket", true, false)
- turtle.select(slot:getItemSlot("cobble"))
- turtle.place()
- sleep(0.2)
- turtle.dig()
- end
- 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()
- currentFunction = "findCobble"
- if not stage["findCobble"] then
- saveToLog("findCobble: changing logFile Name from "..fso:getCurrentFileName()..
- " to logFindCobble.txt END OF LOGFILE!", false, false)
- fso:useFileName("logFindCobble.txt")
- saveToLog("findCobble: starting", false, false)
- turtle.select(slot:getItemSlot("stone"))
- if turtle.compareDown() then --on stone so underground
- stage["startUnderground"] = true
- end
- --dig down vertically until 60 cobble found or bedrock reached then return
- saveToLog("findCobble: mining for cobble", true)
- --dirt, cobble, gravel, known. coal can be identified and used, others
- for i = 1, 2 do -- go down 2 levels, in case on surface and grass causes problems as id = valuable
- dig.digNew{self = dig, direction = "down", checkForItems = "coal,sand", callFrom = "findCobble"}
- down(1, 1, "coal,sand", "findCobble")
- for i = 1, 4 do
- dig.digNew{self = dig, direction = "forward", checkForItems = "coal,sand", callFrom = "findCobble"}
- turnRight(1)
- end
- end
- mineDown(1, "coal,ironore,goldore,redstone,diamonds", "findCobble") --mine down to level 1 or until 60 cobble found
- changeDirection("faceBackward")
- repeat
- forward(1)
- dig.digNew{self = dig, waitForGravel = true}
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.place()
- back(1)
- turtle.select(slot:getItemSlot("ladders"))
- turtle.place()
- up(1, 1, "coal,sand", "findCobble")
- until location:getY() == coordHome:getY()
- -- remove last ladder
- down(1, 1)
- dig.digNew{self = dig}
- up(1, 1)
- if getStock("cobble") < 40 then -- go up to get cobble
- -- dig up until surface reached(64 common) or cobble reaches 60
- mineUp(80, "coal,ironore,goldore,redstone,diamonds", "findCobble", true, true)
- while location:getY() > homeCoord:getY() do
- down(1)
- end
- end
- changeDirection("faceForward")
- saveToLog("findCobble: cobble supplies replenished", true)
- stage["findCobble"] = true
- saveStatus()
- displaySaveBackup()
- end
- return true
- end
- function forward(steps, useSlot, itemList, calledFrom)
- local maxTries = 2
- local success = true
- local previousFunction = currentFunction
- currentFunction = "forward"
- useSlot = useSlot or 1
- itemList = itemList or ""
- calledFrom = calledFrom or ""
- if steps > 0 then
- if turtle.getFuelLevel() < steps then
- saveToLog(" ***** forward: Fuel < "..steps.." *****", true, false)
- if not refuel(0) then
- saveToLog(" ***** forward: refuel failed*****", true, false)
- end
- end
- for i = 1, steps do
- if turtle.detect() then
- while turtle.detect() do
- if dig.digNew{self = dig, direction = "forward", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom} then
- sleep(0.5)
- else
- maxTries = maxTries - 1
- if maxTries <= 0 then --bedrock
- maxTries = 2
- break
- end
- end
- end
- end
- while not turtle.forward() do
- if turtle.detect() then
- if dig.digNew{self = dig, direction = "forward", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom} then
- sleep(0.5)
- else
- success = false
- break
- end
- end
- attack()
- maxTries = maxTries - 1
- if maxTries <= 0 then
- if turtle.getFuelLevel() == 0 then
- if refuel(0) then
- maxTries = 2
- else
- saveToLog("forward: Out of fuel. Game Over!", true)
- error()
- end
- end
- end
- end
- if success then
- changeCoords("forward")
- end
- end
- end
- currentFunction = previousFunction
- return success
- end
- function getCoords()
- currentFunction = "getCoords"
- callingFunction = "main"
- --get world coordinates from player
- local coord = 0
- local response = ""
- local continue = true
- local event = ""
- local param1 = ""
- if not stage["getCoords"] then -- not resuming script
- saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
- " to logGetCoords.txt END OF LOGFILE!", false, false)
- fso:useFileName("logGetCoords.txt")
- saveToLog("getCoords: Starting", false, false)
- 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)
- saveToLog("getCoords: x coord set to "..location:getX(), false, false)
- 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)
- saveToLog("getCoords: y coord set to "..location:getY(), false, false)
- 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)
- saveToLog("getCoords: z coord set to "..location:getZ(), false, false)
- 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)
- saveToLog("getCoords: facing set to "..location:getFacing(), false, false)
- 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()..")")
- --[[if floorToCeiling then
- print(" Mineshaft surface to bedrock")
- else]]--
- print(" Mineshaft from level "..location:getY().." to bedrock")
- --end
- print()
- write("Is this correct? (y/n)")
- event, param1 = os.pullEvent ("char")
- if param1 == "y" or param1 == "Y" then
- continue = false
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- if location:getY() > 20 then
- response = true
- while response do
- response = false
- term.clear()
- term.setCursorPos(1,1)
- print("Which mining mode do you want?")
- print()
- print("1) Diamonds Only")
- print(" Starts @ 15->6 takes 4-5 hours")
- print()
- print("2) Diamonds first, then other ores")
- print(" Starts @ 15, then 51->18. >24 hours")
- print()
- print("3) Ores first")
- print(" Starts @ 51, then 15->6. >24 hours")
- print()
- print(" Choose option: 1, 2, 3")
- event, param1 = os.pullEvent ("char")
- if param1 == "1" then
- diamondsFirst = true
- diamondsOnly = true
- elseif param1 == "2" then
- diamondsFirst = true
- diamondsOnly = false
- elseif param1 == "3" then
- diamondsFirst = false
- diamondsOnly = false
- else
- print()
- print("Incorrect input: "..param1)
- print()
- print("Use 1, 2 or 3")
- sleep(2)
- response = true
- end
- end
- saveToLog("getCoords: mine diamonds only set to "..tostring(diamondsOnly), false, false)
- saveToLog("getCoords: mine diamonds first set to "..tostring(diamondsFirst), false, false)
- 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())
- mineTopLevel:setX(coordHome:getX())
- if coordHome:getY() >= 54 then
- mineTopLevel:setY(51)
- elseif coordHome:getY() < 8 then
- term.clear()
- print("Y coordinate below 8")
- print("Move up above level 7")
- error()
- else -- between level 18 and 53
- mineTopLevel:setY((math.ceil((coordHome:getY() - 2) / 3) * 3)) -- eg 52/51/50 - 2 /3 = 16 * 3 = 48, 47/48/49 = 45
- end
- mineTopLevel:setZ(coordHome:getZ())
- mineTopLevel:setFacing(coordHome:getFacing())
- stage["getCoords"] = true
- saveStatus()
- saveToLog("Completed coordinates registration at x="..location:getX().." y="..location:getY().." z="..location:getZ().." facing "..location:getCompass(), true)
- displaySaveBackup()
- end
- 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 getFuelAvailable()
- local tFuelAvailable = 0
- local fAvailableCoal = 0
- local fAvailableCharcoal = 0
- local fAvailablePlanks = 0
- local fAvailableWood = 0
- local fCurrentFuelLevel = turtle.getFuelLevel()
- 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("wood") > 0 then
- fAvailableWood = (turtle.getItemCount(slot:getItemSlot("wood")) - 1) * 60
- end
- tFuelAvailable = fAvailableCoal + fAvailableCharcoal + fAvailablePlanks + fAvailableWood
- saveToLog("getFuelAvailable: coal = "..fAvailableCoal..
- " charcoal = "..fAvailableCharcoal..
- " planks = "..fAvailablePlanks..
- " wood = "..fAvailableWood, false, true)
- result = {totalFuelAvailable = tFuelAvailable,
- fuelAvailableCoal = fAvailableCoal,
- fuelAvailableCharcoal = fAvailableCharcoal,
- fuelAvailablePlanks = fAvailablePlanks,
- fuelAvailableWood = fAvailableWood,
- fuelLevel = fCurrentFuelLevel}
- 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
- local itemRemoved = false
- arg.direction = arg.direction or "down"
- --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: from "..value.getStoreName(value), false)
- else
- saveToLog("getItemsFromStorage: getting "..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)
- itemRemoved = false
- if arg.direction == "down" then
- if turtle.suckDown() then --removed item from storage
- itemRemoved = true
- end
- elseif arg.direction == "forward" then
- if turtle.suck() then --removed item from storage
- itemRemoved = true
- end
- else
- if turtle.suckUp() then --removed item from storage
- itemRemoved = true
- end
- end
- if itemRemoved then
- 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 getNoOfEmptySlots()
- local result = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- result = result + 1
- end
- end
- return result
- end
- function getNoOfValuableSlots()
- local result = 0
- for i = 1, 16 do
- if string.find(slot:getSlotContains(i),"item") ~= nil then
- result = result + 1
- end
- end
- if slot:getItemSlot("redstone") > 0 then
- result = result + 1
- end
- if slot:getItemSlot("lapis") > 0 then
- result = result + 1
- end
- if slot:getItemSlot("coal") > 0 then
- result = result + 1
- end
- if slot:getItemSlot("coal-1") > 0 then
- result = result + 1
- end
- if slot:getItemSlot("coal-2") > 0 then
- result = result + 1
- end
- return result
- end
- function getPastebin(name, code)
- local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))
- if response then
- local sCode = response.readAll()
- if sCode ~= nil and sCode ~= "" then
- local file = fs.open(name, "w")
- response.close()
- file.write(sCode)
- file.close()
- return true
- end
- end
- return false
- end
- function getPreferences()
- currentFunction = "getPreferences"
- callingFunction = "main"
- if not stage["getPreferences"] then
- local response = ""
- 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()
- 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:setUseLog(false)
- term.clear()
- term.setCursorPos(1,1)
- print("superminer.lua version: "..version)
- print()
- print("Are you playing on a server? 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
- term.clear()
- term.setCursorPos(1,1)
- print("Logfiles can be created for debugging")
- print("Use only if CC config file is modified")
- print("to I:computerSpaceLimit=10000000")
- print()
- print("Press 'V' for verbose logfiles")
- print()
- print("Press 'M' for minimal logfiles")
- print()
- print("Any other key for default (None)")
- print()
- event, param1 = os.pullEvent ("char")
- if param1 == "v" or param1 == "V" then
- fso:setUseVerbose(true)
- fso:setUseLog(true)
- print("verbose logfiles will be created")
- elseif param1 == "m" or param1 == "M" then
- fso:setUseVerbose(false)
- fso:setUseLog(true)
- print("minimal logfiles will be created")
- else
- print("logfiles will not 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
- stage["getPreferences"] = true
- saveStatus()
- saveToLog("Completed preferences registration. LogFiles started", true)
- displaySaveBackup()
- end
- end
- function getReady()
- currentFunction = "getReady"
- callingFunction = "main"
- local numChests = 0
- local findCobbleLevel = 0
- --[[
- 1 chest slot 1
- 1 dirt slot 2
- 1 cobble slot 3
- 1 stone slot 4
- 1 gravel slot 5
- 1 bucket slot 6
- 64 wood slot 7
- ]]--
- if not stage["getReady"] then
- saveToLog("getCoords: changing logFile Name from "..fso:getCurrentFileName()..
- " to logGetReady.txt END OF LOGFILE!", false, false)
- fso:useFileName("logGetReady.txt")
- saveToLog("getReady: starting", false, false)
- if fso:recoveryFileExists() then --use file to update slot object
- saveToLog("using recovery file")
- else
- saveToLog("Initialising slot object")
- 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 = "stone"}
- slot.update{self = slot, slotNo = 5, item = "gravel"}
- slot.update{self = slot, slotNo = 6, item = "buckets"}
- slot.update{self = slot, slotNo = 7, item = "wood"}
- slot.update{self = slot, slotNo = 8, item = "wood-1"}
- end
- sortInventory(true)
- -- wood = 2 x 64. craft will check area in front is clear, else move around
- -- make chests
- numChests = getStock("chests")
- if not stage["craftChests"] then
- if numChests < 25 then
- repeat
- if craft{calledFrom = "getReady", craftItem = "planks", craftQuantity = 64, sourceItem1 = "wood-1", destSlot = 0, doSort = false} then
- saveToLog("Crafted 64 planks.")
- else
- saveToLog("Crafting planks did not succeed")
- error()
- end
- if craft{calledFrom = "getReady", craftItem = "chests", craftQuantity = 8, sourceItem1 = "planks", destSlot = 0, doSort = false} then
- saveToLog("Crafted 8 chests.")
- else
- saveToLog("Crafting chests did not succeed")
- error()
- end
- until getStock("chests") >= 25
- end
- -- 3 x 8 chests crafted, used 48 wood-1 = 16 left
- stage["craftChests"] = true
- end
- saveStatus()
- if not stage["craftLadders"] then
- for i =1, 2 do
- craftLadders(27)
- turtle.select(slot:getItemSlot("sticks"))
- turtle.refuel()
- slot.update{self = slot, item = "sticks", delete = true}
- end
- stage["craftSticks"] = true
- stage["craftLadders"] = true
- end
- saveStatus()
- -- 0 wood-1, 64 wood, 54 ladders, 0 sticks
- if not stage["craftSigns"] then
- if getStock("signs") == 0 then
- craftSigns(9)
- end
- stage["craftSigns"] = true
- end
- -- 62 wood, 54 ladders, 3 signs, 3 sticks
- if getStock("wood-1") > 0 then
- if craft{calledFrom = "getReady", craftItem = "planks", craftQuantity = getStock("wood-1") * 4, sourceItem1 = "wood-1", destSlot = 0, doSort = true} then
- saveToLog("Crafted 32 planks.")
- else
- saveToLog("Crafting planks did not succeed")
- error()
- end
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- sortInventory(true)
- end
- -- craft planks for fuel
- if craft{calledFrom = "getReady", craftItem = "planks", craftQuantity = 32, sourceItem1 = "wood", destSlot = 0, doSort = true} then
- saveToLog("Crafted 32 planks.")
- else
- saveToLog("Crafting planks did not succeed")
- error()
- end
- -- 56 wood left, 54 ladders, 32 planks, 3 signs, 3 sticks
- if slot:getItemSlot("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- sortInventory(true)
- end
- stage["getReady"] = true
- saveStatus()
- displaySaveBackup()
- end
- end
- function getSticks()
- local useSlot = 0
- currentFunction = "getSticks"
- changeDirection("faceForward")
- go("FFRFF")
- saveToLog("getSticks: removing sticks from storage", true, false)
- if getStock("sticks") > 0 then
- useSlot = slot:getItemSlot("sticks")
- else
- useSlot = getFirstEmptySlot(false)
- end
- if turtle.suckDown() then --sticks found
- slot.update{self = slot, slotNo = useSlot, item = "sticks"}
- end
- go("RRFFLFFRR")
- end
- function getStock(ofItem)
- local result = 0
- if slot:getItemSlot(ofItem) > 0 then
- result = turtle.getItemCount(slot:getItemSlot(ofItem))
- end
- return result
- end
- function getSupplies()
- currentFunction = "getSupplies"
- callingFunction = "main"
- local gotChest = false
- local gotDirt = false
- local gotCobble = false
- local gotStone = false
- local gotGravel = false
- local gotBucket = false
- local gotWood = false
- local gotWood2 = false
- local extraSlot = 0
- local fromRecoveryFile = false
- if not stage["getSupplies"] then
- saveToLog("getSupplies: started", false, true)
- repeat
- extraSlot = 0
- for i = 9, 16 do
- if turtle.getItemCount(i) > 0 then
- extraSlot = i
- break
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- if extraSlot > 0 then
- print("Remove items from slot "..extraSlot.."!")
- print()
- else
- print("Add the following supplies:")
- print()
- if not gotChest then
- if turtle.getItemCount(1) == 0 then
- print(" 1 chest in slot 1")
- else --may be reboot, and chest already present
- turtle.select(1)
- if turtle.refuel(0) then
- gotChest = true
- saveToLog("getSupplies: chest added to slot 1", false, true)
- else
- print(" NOT a chest in slot 1! Try again")
- saveToLog("getSupplies: player put non-combustible item in slot 1", false, true)
- end
- end
- else -- player has removed item
- if turtle.getItemCount(1) ~= 1 then
- gotChest = false
- print(" 1 chest in slot 1")
- -- print("Add the following supplies:")
- -- print()
- end
- end
- if not gotDirt then
- if turtle.getItemCount(2) == 0 then
- print(" 1 dirt in slot 2")
- else
- gotDirt = true
- saveToLog("getSupplies: dirt added to slot 2", false, true)
- end
- else
- if turtle.getItemCount(2) ~= 1 then
- gotDirt = false
- print(" 1 dirt in slot 2")
- --print("Add the following supplies:")
- --print()
- end
- end
- if not gotCobble then
- if turtle.getItemCount(3) == 0 then
- print(" 1 cobble in slot 3")
- else
- gotCobble = true
- saveToLog("getSupplies: cobble added to slot 3", false, true)
- end
- else
- if turtle.getItemCount(3) ~= 1 then
- gotCobble = false
- print(" 1 cobble in slot 3")
- --print("Add the following supplies:")
- --print()
- end
- end
- if not gotStone then
- if turtle.getItemCount(4) == 0 then
- print(" 1 stone in slot 4")
- else
- gotStone = true
- saveToLog("getSupplies: stone added to slot 4", false, true)
- end
- else
- if turtle.getItemCount(4) ~= 1 then
- gotStone = false
- print(" 1 stone in slot 4")
- --print("Add the following supplies:")
- --print()
- end
- end
- if not gotGravel then
- if turtle.getItemCount(5) == 0 then
- print(" 1 gravel in slot 5")
- else
- gotGravel = true
- saveToLog("getSupplies: gravel added to slot 5", false, true)
- end
- else
- if turtle.getItemCount(5) ~= 1 then
- gotGravel = false
- print(" 1 gravel in slot 5")
- --print("Add the following supplies:")
- --print()
- end
- end
- if not gotBucket then
- if turtle.getItemCount(6) == 0 then
- print(" 1 bucket in slot 6")
- else
- gotBucket = true
- saveToLog("getSupplies: bucket added to slot 6", false, true)
- end
- else
- if turtle.getItemCount(1) ~= 1 then
- gotBucket = false
- print(" 1 bucket in slot 6")
- --print("Add the following supplies:")
- --print()
- end
- end
- if turtle.getItemCount(7) == 0 then
- print(" 64 wood in slot 7")
- else
- turtle.select(7)
- if turtle.refuel(0) then
- if turtle.getItemCount(7) == 64 then
- if not gotWood then
- saveToLog("getSupplies: 64 wood added to slot 7", false, true)
- end
- gotWood = true
- else
- print(" MORE wood needed in slot 7!")
- end
- else
- print(" NOT wood in slot 7! Try again")
- end
- end
- if turtle.getItemCount(8) == 0 then
- print(" 64 wood in slot 8")
- else
- turtle.select(8)
- if turtle.refuel(0) then
- if turtle.getItemCount(8) == 64 then
- if not gotWood2 then
- saveToLog("getSupplies: 64 wood added to slot 8", false, true)
- end
- gotWood2 = true
- else
- print(" MORE wood needed in slot 8!")
- end
- else
- print(" NOT wood in slot 8! Try again")
- end
- end
- end
- sleep(1)
- until gotChest and gotDirt and gotCobble and gotStone and gotGravel and gotWood and gotWood2
- stage["getSupplies"] = true
- saveStatus()
- saveToLog("getSupplies: completed", false, true)
- displaySaveBackup()
- end
- end
- function go(path, itemList)
- local previousCallingFunction = callingFunction
- local previousFunction = currentFunction
- local digDirection = "forward"
- local itemSelected = false
- itemList = itemList or "coal,redstone,lapis"
- currentFunction = "go"
- -- R L F B U D = Right, Left, Forward, Back, Up, Down
- -- d u x = dig, digUp, digDown (x-cavate ?!)
- -- C S T = place Cobble, Stairs, Torch
- saveToLog("go: (called from "..callingFunction..") "..path.." at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
- for i = 1, string.len(path) do
- move = string.sub(path, i, i)
- if move == "R" then
- turnRight(1)
- -- saveToLog("go: turnRight. Facing "..location:getCompass())
- elseif move == "L" then
- turnLeft(1)
- -- saveToLog("go: turnLeft. Facing "..location:getCompass())
- elseif move == "B" then
- back(1, 1)
- -- saveToLog("go: back 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
- elseif move == "F" then
- forward(1, 1, itemList, "go")
- -- saveToLog("go: forward 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
- elseif move == "u" then
- digDirection = "up"
- while turtle.detectUp() do
- if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
- --allow for sand and gravel
- sleep(.7)
- end
- end
- elseif move == "U" then
- up(1, 1, itemList, "go")
- -- saveToLog("go: up 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
- elseif move == "d" then
- digDirection = "forward"
- while turtle.detect() do
- if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
- --allow for sand and gravel
- sleep(.7)
- end
- end
- elseif move == "D" then
- down(1, 1, itemList, "go")
- -- saveToLog("go: down 1, to x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing.."..location:getCompass())
- elseif move == "x" then
- digDirection = "down"
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- elseif move == "C" then
- if digDirection == "forward" then
- if turtle.detect() then
- while turtle.detect() do
- if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
- --allow for sand and gravel
- sleep(.7)
- end
- end
- end
- elseif digDirection == "up" then
- if turtle.detectUp() then
- while turtle.detectUp() do
- if dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList} then
- --allow for sand and gravel
- sleep(.7)
- end
- end
- end
- elseif digDirection == "down" then
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- end
- end
- itemSelected = false
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- itemSelected = true
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- itemSelected = true
- end
- if itemSelected then
- if digDirection == "forward" then
- turtle.place()
- elseif digDirection == "up" then
- turtle.placeUp()
- elseif digDirection == "down" then
- turtle.placeDown()
- end
- end
- elseif move == "S" then
- if digDirection == "forward" then
- if turtle.detect() then
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- end
- elseif digDirection == "up" then
- if turtle.detectUp() then
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- end
- elseif digDirection == "down" then
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- end
- end
- if getStock("stairs") > 0 then
- turtle.select(slot:getItemSlot("stairs"))
- if digDirection == "forward" then
- turtle.place()
- elseif digDirection == "up" then
- turtle.placeUp()
- elseif digDirection == "down" then
- turtle.placeDown()
- end
- end
- elseif move == "T" then
- if digDirection == "forward" then
- if turtle.detect() then
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- end
- elseif digDirection == "up" then
- if turtle.detectUp() then
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- end
- elseif digDirection == "down" then
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = digDirection, callFrom = "go", checkForItems = itemList}
- end
- end
- if getStock("torches") > 1 then
- turtle.select(slot:getItemSlot("torches"))
- if digDirection == "forward" then
- turtle.place()
- elseif digDirection == "up" then
- turtle.placeUp()
- elseif digDirection == "down" then
- turtle.placeDown()
- end
- end
- end
- end
- saveToLog("go: "..path.." completed at x = "..location:getX().." y = "..location:getY().." z = "..location:getZ().." facing "..location:getCompass())
- callingFunction = previousCallingFunction
- currentFunction = previousFunction
- end
- function goToWoodStore(fromResume)
- currentFunction = "goToWoodStore"
- callingFunction = "goToWoodStore"
- local emptySlot = 0
- local woodSlot = 0
- local waitTime = 0
- if not fromResume then
- changeDirection("faceForward")
- if getStock("wood") >= 2 then
- saveToLog("goToWoodStore: converting excess wood to planks/chests", true, false)
- craft{calledFrom = "goToWoodStore", craftItem = "planks", craftQuantity = getStock("wood") * 4, sourceItem1 = "wood", destSlot = 0, doSort = true}
- if getStock("planks") > 8 then
- craft{calledFrom = "goToWoodStore", craftItem = "chests", craftQuantity = math.floor(getStock("planks") / 8), sourceItem1 = "planks", destSlot = 0, doSort = true}
- end
- if slot:getItemSlot("planks") > 0 then
- saveToLog("goToWoodStore: converting excess planks to fuel", true, false)
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- sortInventory(true)
- end
- end
- forward(2)
- end
- emptySlot = getFirstEmptySlot()
- turtle.select(emptySlot)
- if turtle.suckDown() then -- wood removed from chest
- saveToLog("goToWoodStore: wood removed from chest", true, false)
- slot.update{self = slot, slotNo = emptySlot, item = "wood"}
- else
- while not turtle.suckDown() do
- term.clear()
- term.setCursorPos(1,1)
- print("Please add wood to the chest:")
- print()
- print("I have been waiting for "..waitTime.." seconds")
- print()
- print("Checking for wood in 10 seconds")
- sleep(10)
- waitTime = waitTime + 10
- end
- end
- saveToLog("goToWoodStore: wood removed from chest", true, false)
- slot.update{self = slot, slotNo = emptySlot, item = "wood"}
- turnRight(2)
- forward(2)
- changeDirection("faceForward")
- end
- function isCeilingOk()
- local itemlist = {}
- local success = false
- local previousFunction = currentFunction
- currentFunction = "isCeilingOk"
- itemlist[1] = "dirt"
- itemlist[2] = "stone"
- itemlist[3] = "cobble"
- if turtle.detectUp() then
- for i = 1, 3 do
- turtle.select(slot.getItemSlot(slot,itemlist[i])) -- select compare blocks
- if turtle.compareUp() then -- compare block above to dirt, stone, gravel in turn
- success = true
- end
- end
- else
- success = true -- air
- end
- currentFunction = previousFunction
- return success --true only if block matches those in selected slots eg dirt, stone, cobble
- end
- function isFloorOk()
- local itemlist = {}
- local success = false
- local previousFunction = currentFunction
- currentFunction = "isFloorOk"
- itemlist[1] = "dirt"
- itemlist[2] = "stone"
- itemlist[3] = "cobble"
- if turtle.detectDown() then
- for i = 1, 3 do
- turtle.select(slot.getItemSlot(slot,itemlist[i])) -- select compare blocks
- if turtle.compareDown() then -- compare block below to dirt, stone, gravel in turn
- success = true
- end
- end
- else
- success = true -- air
- end
- currentFunction = previousFunction
- return success --true only if block matches those in selected slots eg dirt, stone, cobble, or no block present
- end
- function isValuable(direction)
- local itemlist = {}
- local success = true
- local blockType = ""
- local previousFunction = currentFunction
- currentFunction = "isValuable"
- itemlist[1] = "dirt"
- itemlist[2] = "stone"
- itemlist[3] = "gravel"
- itemlist[4] = "cobble"
- itemlist[5] = "sand"
- itemlist[6] = "torches"
- if direction == "up" then
- if turtle.detectUp() then
- for i = 1, 6 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
- success = false
- blockType = itemlist[i]
- end
- end
- end
- else
- success = false
- end
- elseif direction == "down" then
- if turtle.detectDown() then
- for i = 1, 6 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
- success = false
- blockType = itemlist[i]
- end
- end
- end
- else
- success = false
- end
- elseif direction == "side" then
- if turtle.detect() then
- for i = 1, 6 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
- success = false
- blockType = itemlist[i]
- break
- end
- end
- end
- else
- success = false
- end
- end
- currentFunction = previousFunction
- return success, blockType --true only if block does not match those in selected slots eg dirt, stone, gravel, cobble
- end
- function loadStatus()
- local numSlots = 0
- local wholeTable = {}
- local sContains = {}
- local sCount = {}
- local sSlot = {}
- local sTurtleSlot = {}
- local wholeFile = ""
- local curLine = ""
- local numTurns = 0
- local emptySlot = 0
- local startLevel = 0
- local diamondStartLevel = 15
- local levelSequence = {}
- local numLevels = 0
- local miningStarted = false
- local functionList = {}
- local onGroundLevel = false
- functionList = {"initialise", "getPreferences", "getSupplies", "getReady", "clearBase"}
- -- read saved status variables from file
- local handle = fs.open("superminer.recover", "r")
- wholeFile = handle.readAll()
- handle.close()
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- --save current logfile with new name, then continue with new version
- fso:renameLastLogFile(tonumber(curLine))
- fso:setStartFileNameIndex(tonumber(curLine))
- saveToLog("recover: fso:setStartFileNameIndex = "..fso:getStartFileNameIndex(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- location:setX(tonumber(curLine))
- saveToLog("recover: Current location x = "..location:getX(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- location:setY(tonumber(curLine))
- saveToLog("recover: Current location y = "..location:getY(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- location:setZ(tonumber(curLine))
- saveToLog("recover: Current location z = "..location:getZ(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- location:setFacing(tonumber(curLine))
- saveToLog("recover: Current location facing = "..location:getFacing(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- coordHome:setX(tonumber(curLine))
- saveToLog("recover: Home location x = "..coordHome:getX(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- coordHome:setY(tonumber(curLine))
- saveToLog("recover: Home location y = "..coordHome:getY(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- coordHome:setZ(tonumber(curLine))
- saveToLog("recover: Home location z = "..coordHome:getZ(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- coordHome:setFacing(tonumber(curLine))
- saveToLog("recover: Home location facing = "..coordHome:getFacing(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- mineTopLevel:setX(tonumber(curLine))
- saveToLog("recover: mineTopLevel x = "..mineTopLevel:getX(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- mineTopLevel:setY(tonumber(curLine))
- saveToLog("recover: mineTopLevel y = "..mineTopLevel:getY(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- mineTopLevel:setZ(tonumber(curLine))
- saveToLog("recover: mineTopLevel z = "..mineTopLevel:getZ(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- mineTopLevel:setFacing(tonumber(curLine))
- saveToLog("recover: mineTopLevel facing = "..mineTopLevel:getFacing(),false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- currentFunction = curLine
- saveToLog("recover: current function = "..currentFunction, false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- callingFunction = curLine
- saveToLog("recover: calling function = "..callingFunction, false, false)
- --curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- --wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- --floorToCeiling = (curLine == "true")
- --saveToLog("recover: floorToCeiling = "..tostring(floorToCeiling), false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- diamondsOnly = (curLine == "true")
- saveToLog("recover: diamondsOnly = "..tostring(diamondsOnly), false, false)
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"\n") - 1)
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"\n") + 1)
- diamondsFirst = (curLine == "true")
- saveToLog("recover: diamondsFirst = "..tostring(diamondsFirst), false, false)
- --get slot object
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"}\n"))
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"}\n") + 2)
- wholeTable = textutils.unserialize(curLine)
- slot:setStatus(wholeTable)
- slot:printSlotContents()
- --get stage table
- curLine = string.sub(wholeFile, 1, string.find(wholeFile,"}\n"))
- wholeFile = string.sub(wholeFile, string.find(wholeFile,"}\n") + 2)
- stage = textutils.unserialize(curLine)
- --get chest object
- curLine = wholeFile
- craftingChest:setStatus(textutils.unserialize(curLine))
- craftingChest:printContents()
- --[[
- 0 = go south (z increases)
- 1 = go west (x decreases)
- 2 = go north (z decreases
- 3 = go east (x increases)
- ]]--
- startLevel = mineTopLevel:getY()
- if diamondsFirst or diamondsOnly then --start at level 15 or lower
- if startLevel > 15 then
- levelSequence[1] = 15
- levelSequence[2] = 12
- levelSequence[3] = 9
- levelSequence[4] = 6
- levelIndex = 4
- for i = startLevel, 18, -3 do
- levelIndex = levelIndex + 1
- levelSequence[levelIndex] = i
- end
- -- eg 15,12,9,6,51,48,45,42...21,18
- else
- for i = startLevel, 6, -3 do
- levelIndex = levelIndex + 1
- levelSequence[levelIndex] = i
- end
- -- eg 12,9,6
- end
- else
- for i = startLevel, 6, -3 do
- levelIndex = levelIndex + 1
- levelSequence[levelIndex] = i
- end
- -- eg 51,48,45,42...15,12,9,6
- end
- numLevels = levelIndex
- levelIndex = 1
- if stage["createBase"..tostring(levelSequence[1])] then --mining started
- repeat
- level = levelSequence[levelIndex]
- if not stage["createBase"..tostring(level)] then --mineXX not completed
- break
- end
- levelIndex = levelIndex + 1
- until levelIndex > numLevels
- level = levelSequence[levelIndex - 1]
- saveToLog("recover: currently mining level "..level, true, false)
- miningStarted = true
- end
- for i = 1, #(functionList) do
- if callingFunction == functionList[i] or currentFunction == functionList[i] then
- onGroundLevel = true
- saveToLog("recover: on ground level", true, false)
- end
- end
- if location:getY() == coordHome:getY() then
- onGroundLevel = true
- saveToLog("recover: on ground level", true, false)
- end
- if currentFunction == "goToWoodStore" or callingFunction == "goToWoodStore" then
- goToWoodStore(true)
- elseif onGroundLevel then
- returnHome("y")
- returnHome("x")
- returnHome("z")
- returnHome("facing")
- elseif currentFunction == "findCobble" then -- somewhere in mineshaft, so return
- returnHome("x")
- returnHome("z")
- returnHome("facing")
- turnRight(2) --face backwards
- returnHome("y")
- returnHome("facing")
- else -- all other functions
- if currentFunction == "craft" or craftingChest:getSlotContains(1) ~= "" then -- check if chest in front
- saveToLog("loadStatus: currentFunction = "..currentFunction.." chest slot 1 contains "..craftingChest:getSlotContains(1), false, false)
- if turtle.getItemCount(slot:getItemSlot("chests")) == 0 then -- chest should be in front
- while not turtle.detect() do
- numTurns = numTurns + 1
- if numTurns == 4 then
- break
- else
- turnRight(1)
- end
- end
- else
- turtle.select(slot:getItemSlot("chests"))
- while not turtle.compare() do
- numTurns = numTurns + 1
- if numTurns == 4 then
- break
- else
- turnRight(1)
- end
- end
- end
- -- assume now facing chest
- -- slot:restoreAllContents(sContains, sCount, sSlot, sTurtleSlot)
- saveToLog("loadStatus: emptying chest back into turtle", false)
- emptyChest("forward")
- if slot:getItemSlot("chests") > 0 then
- emptySlot = slot:getItemSlot("chests")
- else
- emptySlot = getFirstEmptySlot(false)
- end
- dig.digNew{self = dig, slotNo = emptySlot, expectedItem = "chests", callFrom = "loadStatus"}
- slot.update{self = slot, slotNo = emptySlot, item = "chests"}
- end
- -- check if at home location in front of or above furnace
- saveToLog("recover: refuelling", true, false)
- refuel(0)
- if miningStarted then
- while location:getY() > level do
- down(1, 1)
- end
- while location:getY() < level do -- inside mine on ground level or below
- up(1) -- back to eye level
- end
- returnHome("facing") -- now facing home direction
- if location:getX() == coordHome:getX() and location:getZ() == coordHome:getZ() then -- already in shaft
- returnHome("y")
- else
- if coordHome:getFacing() == 0 or coordHome:getFacing() == 2 then -- home Facing S/N
- if location:getX() == coordHome:getX() then -- already in centre
- --move to Right if s, Left if N
- if location:getFacing() == 0 then --south for home and current position
- go("RFR")
- else
- go("LFL")
- end
- -- now in corridor to L of centre
- end
- returnHome("z")
- returnHome("facing")
- -- now lined up with mineshaft in NS direction
- if location:getFacing() == 0 then --move into line 1 ahead of mineshaft
- go("F")
- else
- go("RRFRR")
- end
- returnHome("x")
- else -- home facing 1/3 W/E
- if location:getZ() == coordHome:getZ() then -- already in centre
- --move to Right if s, l if N
- if location:getFacing() == 1 then --east for home and current position
- go("RFR")
- else
- go("LFL")
- end
- -- now in corridor to L of centre
- end
- returnHome("x")
- returnHome("facing")
- -- now lined up with mineshaft in EW direction
- if location:getFacing() == 1 then --move into line 1 ahead of mineshaft
- go("F")
- else
- go("RRFRR")
- end
- returnHome("z")
- end
- --now 1 block in front of mineshaft
- returnHome("facing")
- back(1) --over mineshaft
- returnHome("y")
- end
- else --mining not started. still on surface, or in mineshaft
- returnHome("y")
- end
- -- now continue script. If stage completed, will skip through the functions in main()
- end
- end
- function mineAll()
- currentFunction = "mineAll"
- callingFunction = "main"
- local woodAvailable = 0
- local logText = ""
- local fuelLevel = 0
- local fuelStats = {}
- local startLevel = 0
- local numTorchesNeeded = 0
- local numChestsNeeded = 0
- local fuelNeeded = 0
- local diamondStartLevel = 15
- local fuelStats = {}
- local levelSequence = {}
- local levelIndex = 0
- local numLevels = 0
- local dirtAmount = 0
- startLevel = mineTopLevel:getY()
- if diamondsOnly then --start at level 15 or lower
- levelSequence[1] = 15
- levelSequence[2] = 12
- levelSequence[3] = 9
- levelSequence[4] = 6
- levelIndex = 4
- elseif diamondsFirst then --start at level 15 or lower
- if startLevel > 15 then
- levelSequence[1] = 15
- levelSequence[2] = 12
- levelSequence[3] = 9
- levelSequence[4] = 6
- levelIndex = 4
- for i = startLevel, 18, -3 do
- levelIndex = levelIndex + 1
- levelSequence[levelIndex] = i
- end
- -- eg 15,12,9,6,51,48,45,42...21,18
- else
- for i = startLevel, 6, -3 do
- levelIndex = levelIndex + 1
- levelSequence[levelIndex] = i
- end
- -- eg 12,9,6
- end
- else
- for i = startLevel, 6, -3 do
- levelIndex = levelIndex + 1
- levelSequence[levelIndex] = i
- end
- -- eg 51,48,45,42...15,12,9,6
- end
- numLevels = levelIndex
- -- central control function. From here move turtle to correct level and
- -- mine out basic shape, followed by detailed mining
- -- ensure enough fuel onboard else go to chest on base camp and wait.
- -- make 38 torches per diamond mining level
- -- create 1 level in cross shape with empty chest in between
- -- check if enough fuel onboard, else goToStorageWood
- fuelStats = getFuelAvailable()
- saveToLog("mineAll: fuel level = "..turtle.getFuelLevel())
- saveToLog("mineAll: fuelStats total fuel available = "..fuelStats.totalFuelAvailable)
- if fuelStats.totalFuelAvailable < 600 then
- goToWoodStore(false)
- end
- --in case of re-start
- dirtAmount = getStock("dirt")
- if dirtAmount > 40 then
- dirtAmount = dirtAmount - 40
- else
- dirtAmount = 0
- end
- storeWoodItems()
- storeSand()
- storeOres()
- storeMinerals()
- storeBuildingBlocks{dirt = dirtAmount, cobble = 0, gravel = -1, sand = 64}
- saveToLog("mineAll: changing logFile Name from "..fso:getCurrentFileName().." to logCreateMineXX.txt. END OF LOGFILE!", true, false)
- levelIndex = 1
- repeat
- level = levelSequence[levelIndex]
- levelIndex = levelIndex + 1
- saveToLog("mineAll: checking level "..level.." (index "..levelIndex..") of "..numLevels.." levels", true, false)
- if not stage["completeMine"..tostring(level)] then --mineXX not completed
- saveToLog("mineAll: changing logFile Name from "..fso:getCurrentFileName().." to logCreateMine"..level..".txt. END OF LOGFILE!", true, false)
- fso:useFileName("logCreateMine"..level..".txt")
- -- base and one or more sectors may have been completed
- if level <= 15 then
- numTorchesNeeded = 24
- else
- numTorchesNeeded = 44
- end
- if not stage["createBase"..tostring(level)] then -- whole mine needs to be done
- numChestsNeeded = 1
- fuelNeeded = 1020 -- 17 logs (17 * 60)
- else -- find out how many stages completed
- if not stage["mineSector1"] then
- fuelNeeded = 1020
- elseif not stage["mineSector2"] then
- numTorchesNeeded = numTorchesNeeded - (numTorchesNeeded / 4) -- 18, 33
- fuelNeeded = 760
- elseif not stage["mineSector3"] then
- numTorchesNeeded = numTorchesNeeded - (numTorchesNeeded / 2) -- 12, 22
- fuelNeeded = 510
- elseif not stage["mineSector4"] then
- numTorchesNeeded = numTorchesNeeded - (numTorchesNeeded / 2) - (numTorchesNeeded / 4) -- 6, 11
- fuelNeeded = 260
- end
- end
- if getStock("torches") < numTorchesNeeded then
- craftTorches(numTorchesNeeded - getStock("torches"))
- currentFunction = "mineAll"
- callingFunction = "main"
- end
- if getStock("chests") <= numChestsNeeded then --make more chests
- craftChests(1)
- currentFunction = "mineAll"
- callingFunction = "main"
- end
- if not stage["createBase"..level] then
- if getStock("signs") == 0 then --make signs
- craftSigns(3)
- currentFunction = "mineAll"
- callingFunction = "main"
- end
- end
- fuelStats = getFuelAvailable()
- if fuelStats.totalFuelAvailable + fuelStats.fuelLevel < fuelNeeded then
- goToWoodStore(false)
- end
- if getStock("sticks") > 0 then
- storeSticks()
- end
- if getStock("planks") > 0 then
- turtle.select(slot:getItemSlot("planks"))
- turtle.refuel()
- slot.update{self = slot, item = "planks", delete = true}
- end
- --go down to correct level
- while location:getY() > level do
- down(1)
- end
- createMine(level) --mine structure in place ready for mining sectors 12 x 16 block corridors takes 24 hrs
- while location:getY() < coordHome:getY() do
- up(1)
- end
- saveToLog("prepareMining: mine created. fuel level = "..turtle.getFuelLevel().." used "..fuelLevel - turtle.getFuelLevel(), true, false)
- storeWoodItems()
- storeSand()
- storeOres()
- storeMinerals()
- saveStatus()
- displaySaveBackup()
- else
- saveToLog("mineAll: level "..level.." (index "..levelIndex..") of "..numLevels.." levels already completed", true, false)
- end
- until levelIndex > numLevels
- end
- function mineBase(length, itemList, checkForChest)
- local doPlaceDown = true
- local doPlaceUp = true
- currentFunction = "mineBase"
- callingFunction = "createMine"
- -- starts on ground level to create platform for player
- for i = 1, length do
- if turtle.detect() then
- if checkForChest then
- checkChest("forward")
- end
- while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineBase"} do
- sleep(.5)
- end
- end
- forward(1, 1, itemList, "mineBase")
- up(1)
- if turtle.detectUp() then -- block above
- if checkForChest then
- if checkChest("up") then
- doPlaceUp = true
- end
- end
- else -- no block above
- checkWaterLava("up")
- doPlaceUp = true
- end
- if doPlaceUp then
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- end
- down(1)
- if turtle.detectDown() then
- if checkForChest then
- if checkChest("down") then
- doPlaceDown = true
- end
- end
- if not dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineBase"} then
- doPlaceDown = false --bedrock found
- else
- doPlaceDown = true
- end
- else --nothing below could be air, water, lava
- if checkWaterLava("down") == "lava" then
- turtle.select(slot:getItemSlot("buckets"))
- turtle.refuel()
- saveToLog("mineBase: refuelled with lava to "..turtle.getFuelLevel())
- sleep(2)
- while turtle.placeDown() do
- if turtle.refuel() then --lava
- saveToLog("mineBase: refuelling with lava again to "..turtle.getFuelLevel())
- else
- break
- end
- sleep(2)
- if turtle.getFuelLevel() > 18000 then
- break
- end
- end
- end
- end
- if doPlaceDown then
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- end
- end
- end
- function mineCorridor(topLevel, length, torchesAt, plugAt, itemList, checkSides, do3D)
- local numMoves = 0
- checkSides = checkSides or "none" -- "right", "left", "both", "none"
- -- length = length of corridor, torchesAt = position of torches
- -- moves forward FIRST then mines up/down, checks walls, places torch if required
- -- level 15 and below moves up and down to search both layers
- -- level 17 and above moves on same level and searches only one layer, but calls mineItem with do3D arg
- for i = 1, length do
- -- saveToLog("mineCorridor: i = "..i, false, false)
- -- saveToLog("mineCorridor: "..location:getX()..","..location:getY()..","..location:getZ()..","..location:getFacing(), false, false)
- if topLevel > 15 then -- non diamond levels. move to ceiling level
- while location:getY() < topLevel do
- up(1)
- end
- while location:getY() > topLevel do
- down(1)
- end
- end
- if turtle.detect() then --dig
- -- saveToLog("mineCorridor: detected block in front, digging "..i, false, false)
- checkChest("forward")
- while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineCorridor"} do
- -- if at bedrock will exit
- sleep(.5)
- end
- end
- --check if on level 5, different mining technique used here
- if location:getY() == 5 then -- level 5. may be valuable item below
- go("xC")
- end
- -- and move forward
- while not forward(1, 1, itemList, "mineCorridor") do -- at bedrock
- while not up(1) do
- back(1)
- numMoves = numMoves + 1
- end
- -- go forward again if gone back
- end
- while numMoves > 0 do
- forward(1, 1, itemList, "mineCorridor")
- numMoves = numMoves - 1
- end
- -- moved forward. If at ceiling level move down 1. If at floor level move up 1
- if location:getY() == topLevel then -- at top of mining level, or do3D
- -- check if ceiling in place. If so check if valuable and mine/replace it
- if turtle.detectUp() then --block above
- if checkChest("up") then
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- else
- if isValuable("up") then --could be useful, so dig and replace with cobble
- saveToLog("mineCorridor: block above valuable, digging "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
- while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor", waitForGravel = true} do --allow for gravel
- sleep(.5)
- end
- if plugAt[i] then
- saveToLog("mineCorridor: placing cobble plug above "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- end
- end
- end
- else --no block above or lava/water, so place cobble
- -- saveToLog("mineCorridor: no block detected above "..i, false, false)
- if plugAt[i] then
- saveToLog("mineCorridor: placing cobble plug above "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- end
- end
- -- still at ceiling level. Could be lava below
- if not turtle.detectDown() then --nothing below could be air, water, lava
- if checkWaterLava("down") == "lava" then
- turtle.select(slot:getItemSlot("buckets"))
- turtle.refuel()
- 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() > 18000 then
- break
- end
- end
- end
- end
- -- check if any valuable items before moving down
- if checkSides == "both" or checkSides == "right" then
- turnRight(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check right side "..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- end
- turnLeft(1)
- end
- if checkSides == "both" or checkSides == "left" then
- turnLeft(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check left side"..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- end
- turnRight(1)
- end
- if topLevel <= 6 then -- <= level 6 - mining from toplevel to bedrock
- while down(1) do
- if location:getY() < 4 then
- if turtle.detect() then --check forward
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- turnRight(1)
- if turtle.detect() then
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- turnLeft(2)
- if turtle.detect() then
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- turnRight(1)
- else -- level 5 and 6 only
- if checkSides == "both" or checkSides == "right" then
- turnRight(1)
- if turtle.detect() then
- -- saveToLog("mineCorridor: check right side "..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- turnLeft(1)
- end
- if checkSides == "both" or checkSides == "left" then
- turnLeft(1)
- if turtle.detect() then
- -- saveToLog("mineCorridor: check left side"..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- turnRight(1)
- end
- end
- end
- -- moved down to bedrock, so go up to level 5
- if location:getY() < 5 then
- repeat
- if up(1) then
- if numMoves > 0 then
- if forward(numMoves, 1, itemList, "mineCorridor") then
- numMoves = 0
- end
- end
- else
- back(1)
- numMoves = numMoves + 1
- end
- until location:getY() == 5
- end
- while numMoves > 0 do
- while not forward(1, 1, itemList, "mineCorridor") do -- at bedrock
- while not up(1) do
- back(1)
- numMoves = numMoves + 1
- end
- end
- numMoves = numMoves - 1
- end
- else -- topLevel > 6
- -- move to floor level
- down(1)
- -- at floor level
- if checkSides == "both" or checkSides == "right" then
- turnRight(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check right side "..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- else
- if checkWaterLava("forward") == "lava" then -- block it off
- saveToLog("mineCorridor: blocking lava with plug at "..location:getX()..", "..location:getY() ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.place()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.place()
- end
- end
- end
- turnLeft(1)
- end
- if checkSides == "both" or checkSides == "left" then
- turnLeft(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check left side"..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- else
- if checkWaterLava("forward") == "lava" then -- block it off
- saveToLog("mineCorridor: blocking lava with plug at "..location:getX()..", "..location:getY() ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.place()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.place()
- end
- end
- end
- turnRight(1)
- end
- if turtle.detectDown() then
- if not checkChest("down") then
- if isValuable("down") then
- saveToLog("mineCorridor: block below is valuable, digging "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"} -- not bedrock
- end
- turtle.select(slot:getItemSlot("gravel"))
- if turtle.compareDown() then
- saveToLog("mineCorridor: block below is gravel, digging "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
- end
- end
- else --nothing below could be air, water, lava
- if checkWaterLava("down") == "lava" then
- turtle.select(slot:getItemSlot("buckets"))
- turtle.refuel()
- 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() > 18000 then
- break
- end
- end
- end
- end
- if not turtle.detectDown() then
- saveToLog("mineCorridor: placing floor plug "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- end
- -- moved to floor level
- end
- if torchesAt[i] then
- if slot:getItemCount("torches") > 1 then
- if up(1) then
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- slot.update{self = slot, item = "torches", delete = true}
- saveToLog("mineCorridor: placing torch "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- end
- end
- end
- -- at floor level
- else -- started at floor level
- if turtle.detectDown() then
- -- saveToLog("mineCorridor: block detected below "..i, false, false)
- if not checkChest("down") then
- if isValuable("down") then
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
- end
- turtle.select(slot:getItemSlot("gravel"))
- if turtle.compareDown() then
- saveToLog("mineCorridor: block below is gravel, digging "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineCorridor"}
- end
- end
- else
- if checkWaterLava("down") == "lava" then
- turtle.select(slot:getItemSlot("buckets"))
- turtle.refuel()
- 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() > 18000 then
- break
- end
- end
- end
- end
- if not turtle.detectDown() then
- saveToLog("mineCorridor: placing floor plug "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- end
- if checkSides == "both" or checkSides == "right" then
- turnRight(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check right side "..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- end
- turnLeft(1)
- end
- if checkSides == "both" or checkSides == "left" then
- turnLeft(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check left side"..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- end
- turnRight(1)
- end
- up(1)
- if checkSides == "both" or checkSides == "right" then
- turnRight(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check right side "..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- end
- turnLeft(1)
- end
- if checkSides == "both" or checkSides == "left" then
- turnLeft(1)
- if turtle.detect() then
- if not checkChest("forward") then
- -- saveToLog("mineCorridor: check left side "..i, false, false)
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineCorridor", do3D, "")
- end
- end
- turnRight(1)
- end
- -- check ceiling
- if location:getY() == topLevel then -- at top of mining level
- if turtle.detectUp() then
- if not checkChest("up") then
- if isValuable("up") then --could be useful, so dig and replace with cobble
- while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor", waitForGravel = true} do --allow for gravel
- sleep(.5)
- end
- end
- end
- if (not turtle.detectUp()) and plugAt[i] then
- saveToLog("mineCorridor: placing ceiling plug "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- end
- else
- if plugAt[i] then
- saveToLog("mineCorridor: placing ceiling plug "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeUp()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeUp()
- end
- end
- end
- if torchesAt[i] then
- if slot:getItemCount("torches") > 1 then
- turtle.select(slot:getItemSlot("torches"))
- turtle.placeDown()
- slot.update{self = slot, item = "torches", delete = true}
- saveToLog("mineCorridor: placing torch "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- end
- end
- end
- end
- end
- while location:getY() < topLevel do
- up(1)
- end
- end
- function mineStrip(topLevel, length, itemList)
- local numMoves = 0
- local placeCobble = false
- local lavawater = ""
- local skipColumn = true
- -- length = length of corridor
- -- moves forward FIRST then mines up/down
- -- level 15 and below moves up and down to search both layers
- -- level 17 and above moves on same level and searches only one layer
- for i = 1, length do
- placeCobble = false
- while location:getY() < topLevel do
- up(1)
- end
- while location:getY() > topLevel do
- down(1)
- end
- if turtle.detect() then --dig
- checkChest("forward")
- while dig.digNew{self = dig, checkForItems = itemList, callFrom = "mineStrip"} do
- -- if at bedrock will exit
- sleep(.5)
- end
- end
- -- and move forward
- while not forward(1, 1, itemList, "mineStrip") do -- at bedrock
- while not up(1) do
- back(1)
- numMoves = numMoves + 1
- end
- -- go forward again if gone back
- end
- while numMoves > 0 do
- forward(1, 1, itemList, "mineStrip")
- numMoves = numMoves - 1
- end
- -- moved forward
- skipColumn = not skipColumn
- if turtle.detectUp() then --block above
- if not isCeilingOk() then
- saveToLog("mineStrip: block above valuable, digging "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
- checkChest("up") --empty and collect chest if found
- while dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = "mineCorridor", waitForGravel = true} do --allow for gravel
- sleep(.5)
- end
- placeCobble = true
- end
- else
- lavawater = checkWaterLava("up")
- if lavawater == "lava" or lavawater == "water" or lavawater == "liquid" then
- turtle.select(slot:getItemSlot("buckets"))
- turtle.refuel()
- placeCobble = true
- end
- end --no block above or lava/water, so place cobble
- if placeCobble then
- saveToLog("mineStrip: placing cobble plug above "..location:getX()..", "..location:getY() + 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- else
- sortCobble()
- end
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- end
- turtle.placeUp()
- end
- -- Could be lava below
- if not turtle.detectDown() then --nothing below could be air, water, lava
- if checkWaterLava("down") == "lava" then
- turtle.select(slot:getItemSlot("buckets"))
- turtle.refuel()
- saveToLog("mineStrip: refuelled with lava to "..turtle.getFuelLevel())
- sleep(2)
- while turtle.placeDown() do
- if turtle.refuel() then --lava
- saveToLog("mineStrip: refuelling with lava again to "..turtle.getFuelLevel())
- else
- break
- end
- sleep(2)
- if turtle.getFuelLevel() > 18000 then
- break
- end
- end
- end
- end
- if topLevel <= 6 then -- <= level 6 - mining from toplevel to bedrock
- if not skipColumn then
- while down(1) do
- if location:getY() < 6 then
- placeCobble = false
- if turtle.detect() then --check forward
- mineItem("coal,ironore,goldore,redstone,diamonds,lapis", "mineStrip", do3D, "")
- if not turtle.detect() then --check forward
- placeCobble = true
- end
- end
- end
- if placeCobble then
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- else
- sortCobble()
- end
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.place()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.place()
- end
- end
- end
- -- moved down to bedrock, so go up to level 5
- if location:getY() < 6 then
- repeat
- if up(1) then
- if numMoves > 0 then
- if forward(numMoves, 1, itemList, "mineStrip") then
- numMoves = 0
- end
- end
- else
- back(1)
- numMoves = numMoves + 1
- end
- if getStock("cobble") == 1 then
- sortCobble()
- end
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- until location:getY() == 6
- end
- while numMoves > 0 do
- while not forward(1, 1, itemList, "mineStrip") do -- at bedrock
- while not up(1) do
- back(1)
- numMoves = numMoves + 1
- end
- end
- numMoves = numMoves - 1
- end
- end
- else -- topLevel > 6
- placeCobble = false
- if turtle.detectDown() then
- if not isFloorOk() then --valuable item below
- placeCobble = true
- checkChest("down")
- if turtle.detectDown() then
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = "mineStrip"} -- not bedrock
- end
- end
- else --nothing below could be air, water, lava
- if checkWaterLava("down") == "lava" then
- turtle.refuel()
- saveToLog("mineStrip: refuelled with lava to "..turtle.getFuelLevel())
- sleep(2)
- while turtle.placeDown() do
- if turtle.refuel() then --lava
- saveToLog("mineStrip: refuelling with lava again to "..turtle.getFuelLevel())
- else
- break
- end
- sleep(2)
- if turtle.getFuelLevel() > 18000 then
- break
- end
- end
- end
- end
- if placeCobble then
- saveToLog("mineStrip: placing ground level plug "..location:getX()..", "..location:getY() - 1 ..", "..location:getZ(), false, false)
- if getStock("cobble") == 1 then
- sortCobble()
- end
- if getStock("cobble") > 1 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- elseif getStock("dirt") > 1 then
- turtle.select(slot:getItemSlot("dirt"))
- turtle.placeDown()
- end
- end
- end
- end
- end
- function mineDown(toLevel, itemList, calledFrom)
- currentFunction = "mineDown"
- callingFunction = calledFrom
- saveToLog("mineDown: mining down to level "..toLevel, true)
- while location:getY() >= toLevel do --mine down either to bedrock, or to selected level.
- checkWalls(itemList, calledFrom, true) --check surrounding walls for valuable items, use recursive function to mine them
- if getStock("cobble") > 60 then --enough cobble found
- break
- else
- if not down(1, 1, itemList, calledFrom) then -- bedrock
- break
- end
- end
- end
- checkWalls(itemList, calledFrom, true) --final check at base of chosen depth
- end
- function mineUp(toLevel, itemList, calledFrom, getCobble, stopAtSurface)
- currentFunction = "mineUp"
- callingFunction = calledFrom
- getCobble = getCobble or false
- stopAtSurface = stopAtSurface or false
- saveToLog("mineUp: mining to level "..toLevel, true)
- while location:getY() < toLevel do --mine up to selected level
- checkWalls(itemList, calledFrom, true)
- if getCobble then
- for i = 1, 4 do
- dig.digNew{self = dig, direction = "forward", checkForItems = itemList, callFrom = calledFrom}
- turnRight(1)
- end
- end
- if getStock("cobble") > 60 then --enough cobble found
- break
- end
- if stopAtSurface and location:getY() > 50 and not turtle.detectUp() then
- up(1, 1, itemList, calledFrom)
- saveToLog("mineUp: surface reached at level "..location:getY(), true)
- break
- end
- up(1, 1, itemList, calledFrom)
- end
- checkWalls(itemList, calledFrom, true) --final check at top of chosen height
- end
- function mineItem(itemList, calledFrom, do3D, direction)
- currentFunction = "mineItem"
- callingFunction = calledFrom
- local doContinue = true
- --RECURSIVE FUNCTION - BEWARE!
- -- check if block in front is valuable. If so mine it
- -- saveToLog("mineItem: checking block in front ", false, false)
- if direction == "up" then
- if not checkChest("up") then
- if isValuable("up") then
- if dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom} then --not bedrock
- saveToLog("mineItem: valuable item dug above", false, false)
- if dig:getDugSlot() > 0 then
- if turtle.getItemCount(dig:getDugSlot()) > 58 then
- doContinue = false
- end
- end
- while turtle.detectUp() do
- dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
- sleep(0.5)
- end
- else
- doContinue = false
- end
- end
- else
- doContinue = false
- end
- if turtle.getFuelLevel() < 10 then
- saveToLog("mineItem: refuelling", true, false)
- refuel(0)
- end
- if doContinue then
- -- move up into space dug out
- up(1)
- if isValuable("up") then --experimental
- mineItem(itemList, calledFrom, do3D, "")
- end
- -- check if item in front is valuable
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --check right side
- turnRight(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --check behind
- turnRight(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --check left side
- turnRight(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --return to front
- turnRight(1)
- down(1)
- end
- end
- if direction == "down" then
- if not checkChest("down") then
- if isValuable("down") then
- if dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = calledFrom} then --not bedrock
- saveToLog("mineItem: valuable item dug below", false, false)
- if dig:getDugSlot() > 0 then
- if turtle.getItemCount(dig:getDugSlot()) > 58 then
- doContinue = false
- end
- end
- end
- else
- doContinue = false
- end
- else
- doContinue = false
- end
- if turtle.getFuelLevel() < 10 then
- saveToLog("mineItem: refuelling", true, false)
- refuel(0)
- end
- -- move down into space dug out
- if doContinue then
- down(1)
- if isValuable("down") then --experimental
- mineItem(itemList, calledFrom, do3D, "")
- end
- -- check if item in front is valuable
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --check right side
- turnRight(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --check behind
- turnRight(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --check left side
- turnRight(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --return to front
- turnRight(1)
- up(1)
- end
- end
- if isValuable("side") then
- if not checkChest("forward") then
- dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom}
- if dig:getDugSlot() > 0 then
- saveToLog("mineItem: valuable item dug", false, false)
- if turtle.getItemCount(dig:getDugSlot()) > 58 then
- doContinue = false
- saveToLog("mineItem: slot filled- quitting", false, false)
- end
- end
- else
- doContinue = false
- end
- while turtle.detect() do --clear sand and gravel
- if dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom} then
- sleep(0.5)
- saveToLog("mineItem: clearing sand or gravel", false, false)
- else -- bedrock
- doContinue = false
- break
- end
- end
- if turtle.getFuelLevel() < 10 then
- saveToLog("mineItem: refuelling", true, false)
- refuel(0)
- end
- -- move forward into space dug out
- -- saveToLog("mineItem: debug: about to move forward "..location:getX()..","..location:getY()..","..location:getZ()..","..location:getFacing(), false, false)
- -- forward(1, 1, itemList, calledFrom)
- if doContinue then --not bedrock and < 59 in stock
- while not forward(1) do
- dig.digNew{self = dig, checkForItems = itemList, callFrom = calledFrom}
- saveToLog("mineItem: clearing sand or gravel")
- sleep(0.5)
- end
- -- changeCoords("forward")
- -- saveToLog("mineItem: debug: moved forwards "..location:getX()..","..location:getY()..","..location:getZ()..","..location:getFacing(), false, false)
- if isValuable("up") then --experimental
- checkChest("up")
- if do3D then
- mineItem(itemList, calledFrom, do3D, "up")
- else
- dig.digNew{self = dig, direction = "up", checkForItems = itemList, callFrom = calledFrom}
- end
- end
- if isValuable("down") then --experimental
- checkChest("down")
- if do3D then
- mineItem(itemList, calledFrom, do3D, "down")
- else
- dig.digNew{self = dig, direction = "down", checkForItems = itemList, callFrom = calledFrom}
- end
- end
- -- check if item in front is valuable
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- --check left side
- turnLeft(1)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- -- check right side
- turnRight(2)
- if isValuable("side") then
- mineItem(itemList, calledFrom, do3D, "")
- end
- turnLeft(1)
- back(1)
- end
- end
- end
- function placeChest(direction, storageName)
- currentFunction = "placeChest"
- turtle.select(slot:getItemSlot("chests"))
- if direction == "down" then
- if not turtle.compareDown() then
- down(1, 1)
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- up(1)
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeDown()
- end
- else
- if not turtle.compareUp() then
- up(1)
- turtle.select(slot:getItemSlot("chests"))
- turtle.placeDown()
- up(1)
- turtle.select(slot:getItemSlot("cobble"))
- turtle.placeDown()
- end
- end
- placeStorage:setStoragePlaced(storageName)
- slot.update{self = slot, item = "chests", delete = true}
- slot.update{self = slot, item = "cobble", delete = true}
- end
- function refuel(count)
- local previousFunction = currentFunction
- currentFunction = "refuel"
- --[[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 success = false
- local startLevel = 0
- local emptySlot = 0
- local fuelStats = {}
- local turns = 0
- local goUp = 0
- local waste = {"cobble-6","cobble-5","cobble-4","cobble-3","cobble-2","cobble-1","gravel-2","gravel-1","dirt-4","dirt-3","dirt-2","dirt-1",}
- local numEmptySlots = getNoOfEmptySlots()
- 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/wood
- saveToLog("refuel: current level = "..startLevel, true)
- --sortInventory(true)
- fuelStats = getFuelAvailable()
- saveToLog("refuel: fuelStats total fuel available = "..fuelStats.totalFuelAvailable)
- sortCoal()
- if fuelStats.fuelAvailableCoal > 0 then
- turtle.select(slot:getItemSlot("coal"))
- if turtle.refuel(1) then
- success = 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 success and fuelStats.fuelAvailablePlanks > 0 then
- saveToLog("refuel: fuelStats fuelAvailablePlanks = "..fuelStats.fuelAvailablePlanks)
- turtle.select(slot.getItemSlot(slot, "planks"))
- if turtle.refuel() then
- success = 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 success and (fuelStats.fuelAvailableWood > 0) then
- saveToLog("refuel: No planks for fuel. Attempting to craft planks from wood")
- -- need 2 slots free for crafting
- if numEmptySlots < 2 then
- repeat
- for i = 1, 12 do
- if slot:getItemSlot(waste[i]) > 0 then
- numEmptySlots = numEmptySlots + 1
- saveToLog("refuel: dumping mining waste: "..waste[i], false, true)
- turtle.select(slot:getItemSlot(waste[i]))
- turtle.dropDown()
- slot.update{self = slot, item = waste[i], delete = true}
- sortInventory(true)
- end
- end
- until numEmptySlots >= 2
- end
- if turtle.detect() then
- for i = 1, 4 do
- if turtle.detect() then
- turnRight(1)
- turns = turns + 1
- else
- break
- end
- end
- end
- if turns == 4 and location:getY() < 6 then -- could be at bedrock
- turns = 0
- for i = 1, 4 do
- if not dig.digNew{self = dig} then
- turnRight(1)
- turns = turns + 1
- else
- break
- end
- end
- if turns == 4 then --surrounded by bedrock
- if turtle..getFuelLevel() == 0 then
- if getStock("wood") > 1 then
- turtle.select(slot:getItemSlot("wood"))
- turtle.refuel(1)
- else
- saveToLog("refuel: No wood for fuel. Out of fuel. GAME OVER", true, false)
- error()
- end
- end
- up(1)
- goUp = 1
- turns = 0
- for i = 1, 4 do
- if not dig.digNew{self = dig} then
- turnRight(1)
- turns = turns + 1
- else
- break
- end
- 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 count == 0 then
- count = 4
- end
- if craft{calledFrom = "refuel", craftItem = "planks", craftQuantity = count, 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
- success = 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
- if goUp > 0 then
- down(goUp)
- end
- end
- if not success and fuelStats.fuelAvailableCharcoal > 0 then
- turtle.select(slot.getItemSlot(slot, "charcoal"))
- if turtle.refuel(fuelStats.fuelAvailableCharcoal/80) then
- success = 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
- if success then
- sortInventory(true)
- else
- saveToLog("No materials left for fuel")
- end
- end --fuel > 100
- currentFunction = previousFunction
- return success
- end
- function returnHome(goDirection)
- if goDirection == "x" then
- if location:getX() < coordHome:getX() then
- while location:getFacing() ~= 3 do
- turnRight(1)
- end
- while location:getX() < coordHome:getX() do
- forward(1, 1)
- end
- elseif location:getX() > coordHome:getX() then
- while location:getFacing() ~= 1 do
- turnRight(1)
- end
- while location:getX() > coordHome:getX() do
- forward(1, 1)
- end
- end
- elseif goDirection == "y" then
- while location:getY() > coordHome:getY() do
- down(1, 1)
- end
- while location:getY() < coordHome:getY() do --
- up(1, 1) -- back to ground level at home
- end
- elseif goDirection == "z" then
- if location:getZ() > coordHome:getZ() then -- go north
- while location:getFacing() ~= 2 do
- turnRight(1)
- end
- while location:getZ() > coordHome:getZ() do
- forward(1, 1)
- end
- elseif location:getZ() < coordHome:getZ() then
- while location:getFacing() ~= 0 do
- turnRight(1)
- end
- while location:getZ() < coordHome:getZ() do
- forward(1, 1)
- end
- end
- elseif goDirection == "facing" then
- while location:getFacing() ~= coordHome:getFacing() do
- turnRight(1)
- end
- end
- end
- function saveStatus()
- --[[
- current filename index = #
- x = #
- y = #
- z = #
- facing = #
- HomeX = #
- HomeY = #
- HomeZ = #
- HomeFacing = #
- slot-01 = "item"
- ...
- slot-16 = "item"
- diamondsFirst = true/false
- stage["startUnderground"] = false
- stage["getPreferences"] = true/false
- stage["getSupplies"] = true/false
- stage["getCoords"] = true/false
- stage["craftChests"] = true/false
- stage["craftSticks"] = true/false
- stage["craftLadders"] = true/false
- stage["craftSigns"] = true/false
- stage["cobblePlaced"] = false
- stage["getReady"] = true/false
- stage["findCobble"] = true/false
- stage["craftFurnaces"] = true/false
- stage["clearBase"] = true/false
- stage["completeMineshaft"] = true/false
- ]]--
- local handle = fs.open("superminer.recover", "w")
- handle.write(tostring(fso:getCurrentFileNameIndex()).."\n")
- handle.write(tostring(location:getX()).."\n")
- handle.write(tostring(location:getY()).."\n")
- handle.write(tostring(location:getZ()).."\n")
- handle.write(tostring(location:getFacing()).."\n")
- handle.write(tostring(coordHome:getX()).."\n")
- handle.write(tostring(coordHome:getY()).."\n")
- handle.write(tostring(coordHome:getZ()).."\n")
- handle.write(tostring(coordHome:getFacing()).."\n")
- handle.write(tostring(mineTopLevel:getX()).."\n")
- handle.write(tostring(mineTopLevel:getY()).."\n")
- handle.write(tostring(mineTopLevel:getZ()).."\n")
- handle.write(tostring(mineTopLevel:getFacing()).."\n")
- handle.write(currentFunction.."\n")
- if callingFunction == nil then callingFunction = "" end
- handle.write(callingFunction.."\n")
- handle.write(tostring(diamondsOnly).."\n")
- handle.write(tostring(diamondsFirst).."\n")
- handle.write(textutils.serialize(slot:getStatus()).."\n") -- get all slot object variables
- handle.write(textutils.serialize(stage).."\n")
- handle.write(textutils.serialize(craftingChest:getStatus()).."\n")
- handle.close()
- 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 smelt(oreType, quantity)
- currentFunction = "smelt"
- --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 planksAvailable = 0
- local planksInFurnace = 0
- local woodNeeded = 0
- local woodAvailable = 0
- local collectionSlot = 0
- local success = false
- local emptySlot = 0
- woodAvailable = getStock("wood")
- planksAvailable = getStock("planks")
- planksNeeded = quantity
- if oreType == "charcoal" then
- woodNeeded = quantity + math.ceil(quantity / 4)
- end
- if woodNeeded > woodAvailable then
- goToWoodStore(false)
- woodAvailable = getStock("wood")
- end
- saveToLog("smelt: oreType = "..oreType.." quantity = "..quantity, false, true)
- go("BUUF") --now on top
- if oreType == "charcoal" then --eg quantity = 2, needs 2 wood + 2 planks ASSUME only called if wood in stock
- smeltType = "charcoal"
- saveToLog("smelt: dropping "..quantity.." wood into furnace")
- turtle.select(slot:getItemSlot("wood"))
- turtle.dropDown(quantity)
- slot.update{self = slot, item = "wood", delete = true}
- 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")
- 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")
- 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")
- 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")
- 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")
- 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")
- end
- go("BD") --in front of furnace
- turtle.select(16)
- if turtle.suck() then
- planksInFurnace = turtle.getItemCount(16)
- planksAvailable = planksAvailable + planksInFurnace
- if planksNeeded < 0 then
- planksNeeded = 0
- end
- turtle.drop()
- end
- saveToLog("smelt: planksNeeded = "..planksNeeded)
- if planksInFurnace > planksNeeded then
- planksNeeded = 0
- else
- if getStock("planks") >= planksNeeded then
- turtle.select(slot:getItemSlot("planks"))
- turtle.drop(planksNeeded)
- slot.update{self = slot, item = "planks", delete = true}
- saveToLog("smelt: "..planksNeeded.." planks added to fuel furnace")
- saveToLog("Smelting will take "..planksNeeded * 10 .." secs")
- planksNeeded = 0
- end
- end
- if planksNeeded > 0 then --reset to 0 only if planks already in furnace
- 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{calledFrom = "smelt", craftItem = "planks", craftQuantity = planksNeeded, sourceItem1 = "wood", destSlot = 0, doSort = true}
- currentFunction = "smelt"
- turnLeft(1)
- turtle.select(slot:getItemSlot("planks"))
- turtle.drop() --drop existing planks
- slot.update{self = slot, item = "planks", delete = true}
- end
- go("DF") --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
- go("BUUF")
- turtle.select(1)
- turtle.suckDown()
- go("BDDF")
- end
- saveToLog("smelt: success = "..tostring(success))
- return success
- end
- function sortCoal()
- if getStock("coal-2") > 0 then
- if getStock("coal") < 64 then
- saveToLog("sortCoal: moving coal-2 from "..slot:getItemSlot("coal-2").." to coal in slot"..slot:getItemSlot("coal"), true, false)
- turtle.select(slot:getItemSlot("coal-2"))
- turtle.transferTo(slot:getItemSlot("coal"))
- slot.update{self = slot, item = "coal-2", delete = true}
- sortInventory(true)
- end
- end
- if getStock("coal-1") > 0 then
- if getStock("coal") < 64 then
- saveToLog("sortCoal: moving coal-1 from "..slot:getItemSlot("coal-1").." to coal in slot"..slot:getItemSlot("coal"), true, false)
- turtle.select(slot:getItemSlot("coal-1"))
- turtle.transferTo(slot:getItemSlot("coal"))
- slot.update{self = slot, item = "coal-1", delete = true}
- sortInventory(true)
- slot.update{self = slot, item = "coal", delete = true}
- end
- end
- end
- function sortCobble()
- local cobbleIndex = 1
- if getStock("cobble-4") > 0 then
- if getStock("cobble") < 64 then
- saveToLog("sortCobble: moving cobble-4 from "..slot:getItemSlot("cobble-4").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
- turtle.select(slot:getItemSlot("cobble-4"))
- turtle.transferTo(slot:getItemSlot("cobble"))
- slot.update{self = slot, item = "cobble-4", delete = true}
- sortInventory(true)
- end
- end
- if getStock("cobble-3") > 0 then
- if getStock("cobble") < 64 then
- saveToLog("sortCobble: moving cobble-3 from "..slot:getItemSlot("cobble-3").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
- turtle.select(slot:getItemSlot("cobble-3"))
- turtle.transferTo(slot:getItemSlot("cobble"))
- slot.update{self = slot, item = "cobble-3", delete = true}
- sortInventory(true)
- end
- end
- if getStock("cobble-2") > 0 then
- if getStock("cobble") < 64 then
- saveToLog("sortCobble: moving cobble-2 from "..slot:getItemSlot("cobble-2").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
- turtle.select(slot:getItemSlot("cobble-2"))
- turtle.transferTo(slot:getItemSlot("cobble"))
- slot.update{self = slot, item = "cobble-2", delete = true}
- sortInventory(true)
- end
- end
- if getStock("cobble-1") > 0 then
- if getStock("cobble") < 64 then
- saveToLog("sortCobble: moving cobble-1 from "..slot:getItemSlot("cobble-1").." to cobble in slot"..slot:getItemSlot("cobble"), true, false)
- turtle.select(slot:getItemSlot("cobble-1"))
- turtle.transferTo(slot:getItemSlot("cobble"))
- slot.update{self = slot, item = "cobble-1", delete = true}
- sortInventory(true)
- end
- end
- for i = 6, 1, -1 do
- if getStock("cobble-"..tostring(i)) > 0 then
- cobbleIndex = i
- break
- end
- end
- -- now check for itemX incorrectly identified
- turtle.select(slot:getItemSlot("cobble"))
- for i = 6, 1, -1 do
- if getStock("item"..tostring(i)) > 0 then
- if turtle.compareTo(slot:getItemSlot("item"..tostring(i))) then -- cobble found with wrong id
- turtle.select(slot:getItemSlot("item"..tostring(i)))
- turtle.transferTo(slot:getItemSlot("cobble-"..tostring(cobbleIndex)))
- if turtle.getItemCount(slot:getItemSlot("item"..tostring(i))) > 0 then --still some remaining
- slot.update{self = slot, slotNo = getItemSlot("item"..tostring(i)), newItem = "cobble-"..tostring(cobbleIndex + 1)}
- else
- slot.update{self = slot, slotNo = getItemSlot("item"..tostring(i)), delete = true}
- end
- sortInventory(true)
- break
- end
- end
- end
- 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)
- --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
- moveAmount = 64 - turtle.getItemCount(i) --40 dirt in slot i, 40 dirt in slot j, moveAmount = 24
- turtle.select(j)
- if moveAmount > 0 then
- turtle.transferTo(i, moveAmount) --move up to 64
- 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
- turnRight(2) --so if items are dropped do not get sucked up
- saveToLog("sortInventory: dumping "..excessAmount.." of excess "..checkItem)
- turnRight(2)
- 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
- turnRight(2) --so if items are dropped do not get sucked up
- saveToLog(" sortInventory: dumping unknown item in slot "..i, true)
- turnRight(2)
- end
- end
- end
- --loose items sorted into correct slots, excess dumped
- 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
- if slot:getSlotCount(i) > 0 then
- saveToLog(" sortInventory: slot "..i.." slotCount = "..slot:getSlotCount(i).. " of "..slot:getSlotContains(i), false, false)
- end
- end
- --turnRight(2)
- turtle.select(1)
- end
- function storeBuildingBlocks(arg)
- currentFunction = "storeBuildingBlocks"
- -- storeBuildingBlocks{dirt = -1, cobble = -1, gravel = -1, sand = -1}
- -- 0 = store nothing
- -- 1 to 63 store that number
- -- -1 leave 1 behind
- -- 64 store everything
- arg.dirt = arg.dirt or -1
- arg.cobble = arg.cobble or -1
- arg.gravel = arg.gravel or -1
- arg.sand = arg.sand or -1
- changeDirection("faceForward")
- turnRight(1)
- forward(2)
- saveToLog("storeBuildingBlocks: storing excess blocks", true, false)
- if arg.dirt ~= 0 then
- storeItem{toStore = storageBuilding, item = "dirt", quantity = arg.dirt, updateSlot = true, doSort = false}
- end
- storeExcess("dirt-1")
- storeExcess("dirt-2")
- storeExcess("dirt-3")
- storeExcess("dirt-4")
- if arg.cobble ~= 0 then
- storeItem{toStore = storageBuilding, item = "cobble", quantity = arg.cobble, updateSlot = true, doSort = false}
- end
- storeExcess("cobble-1")
- storeExcess("cobble-2")
- storeExcess("cobble-3")
- storeExcess("cobble-4")
- storeExcess("cobble-5")
- storeExcess("cobble-6")
- if arg.gravel ~= 0 then
- storeItem{toStore = storageBuilding, item = "gravel", quantity = arg.gravel, updateSlot = true, doSort = false}
- end
- storeExcess("gravel-1")
- storeExcess("gravel-2")
- if arg.sand ~= 0 then
- storeItem{toStore = storageBuilding, item = "sand", quantity = arg.sand, updateSlot = true, doSort = false}
- end
- turnRight(2)
- forward(2)
- changeDirection("faceForward")
- end
- function storeExcess(itemTypeQualified)
- if slot:getItemSlot(itemTypeQualified) > 0 then
- saveToLog("Storing excess '"..itemTypeQualified, false, true)
- turtle.select(slot:getItemSlot(itemTypeQualified))
- if not turtle.dropDown() then
- turtle.dropUp()
- end
- slot.update{self = slot, item = itemTypeQualified, delete = true}
- sortInventory(true)
- end
- end
- function storeMinerals()
- currentFunction = "storeMinerals"
- changeDirection("faceForward")
- go("LFFLFFRR")
- saveToLog("storeMinerals: storing mined identified items", true, false)
- storeItem{toStore = storageMinerals, item = "redstone", quantity = 0, updateSlot = true, doSort = false}
- storeItem{toStore = storageMinerals, item = "redstone-1", quantity = 0, updateSlot = true, doSort = false}
- storeItem{toStore = storageMinerals, item = "redstone-2", quantity = 0, updateSlot = true, doSort = false}
- storeItem{toStore = storageMinerals, item = "redstone-3", quantity = 0, updateSlot = true, doSort = false}
- storeItem{toStore = storageMinerals, item = "lapis", quantity = 0, updateSlot = true, doSort = false}
- storeItem{toStore = storageMinerals, item = "coal-1", quantity = 0, updateSlot = true, doSort = false}
- storeItem{toStore = storageMinerals, item = "coal-2", quantity = 0, updateSlot = true, doSort = false}
- if getStock("coal") > 35 then
- turtle.select(slot:getItemSlot("coal"))
- if not turtle.refuel(turtle.getItemCount(slot:getItemSlot("coal")) - 35) then
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("coal")) - 35)
- end
- end
- for i = 6, 1, -1 do
- if getStock(tostring("item"..i)) > 0 then
- storeItem{toStore = storageOres, item = tostring("item"..i), quantity = 0, updateSlot = true, doSort = false}
- end
- end
- sortInventory(true)
- go("FFRFFL")
- end
- function storeOres()
- local redstoneFound = false
- currentFunction = "storeOres"
- changeDirection("faceForward")
- go("RFFRFFRR")
- saveToLog("storeOres: storing mined ores", true, false)
- for i = 6, 1, -1 do
- if getStock(tostring("item"..i)) > 0 then
- turtle.select(slot:getItemSlot(tostring("item"..i)))
- if turtle.place() then --ores, obsidian, mossy cobble, redstone if not identified already
- saveToLog("storeOres: testing item"..i, true, false)
- if turtle.getItemCount(slot:getItemSlot(tostring("item"..i))) > 0 then
- if not turtle.compare() then
- saveToLog("storeOres: redstone identified", true, false)
- redstoneFound = true
- slot.update{self = slot, slotNo = slot:getItemSlot(tostring("item"..i)), newItem = "redstone"}
- end
- end
- turtle.dig()
- if not redstoneFound then
- storeItem{toStore = storageOres, item = tostring("item"..i), quantity = 0, updateSlot = true, doSort = false}
- end
- end
- end
- end
- sortInventory(true)
- go("FFLFFR")
- end
- function storeMiningWaste(overChest)
- local waste = {"dirt-1","dirt-2","dirt-3","dirt-4","cobble-1","cobble-2","cobble-3","cobble-4","cobble-5","cobble-6","gravel-1","gravel-2"}
- local wasteFound = true
- local direction = "down"
- local useOtherChest = false
- local doSort = false
- saveToLog("storeMiningWaste: started")
- if not overChest then --chest found in dungeon, clear some space for items inside
- repeat
- wasteFound = false
- for i = 1, 12 do
- if slot:getItemSlot(waste[i]) > 0 then
- wasteFound = true
- saveToLog("storeMiningWaste: Dumping mining waste: "..waste[i], false, true)
- turtle.select(slot:getItemSlot(waste[i]))
- turtle.dropDown()
- slot.update{self = slot, item = waste[i], delete = true}
- sortInventory(true)
- end
- end
- until not wasteFound
- if getStock("cobble") > 50 then
- turtle.select(slot:getItemSlot("cobble"))
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 50)
- slot.update{self = slot, item = "cobble", delete = true}
- end
- else -- over or under storage chest
- if not turtle.detectDown() then --move down to floor level. could now be on level 5
- down(1)
- end
- if location:getY() == 5 then -- use chest above first. If full move to second chest
- direction = "up"
- up(1)
- for i = 1, 16 do --store unknown items
- if slot:getSlotContains(i) == "" and turtle.getItemCount(i) > 0 then
- doSort = true
- turtle.select(i)
- if not turtle.dropUp() then-- chest full
- if not useOtherChest then -- move to other chest
- useOtherChest = true
- changeDirection("faceBackward")
- go("LFFRR")
- turtle.select(i)
- if not turtle.dropUp() then-- chest full
- turtle.dropDown()
- end
- else -- already under other chest
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- end
- if doSort then
- sortInventory(true)
- end
- if getStock("dirt") > 40 then
- turtle.select(slot:getItemSlot("dirt"))
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("dirt")) - 40) then-- chest full
- if not useOtherChest then
- useOtherChest = true
- changeDirection("faceBackward")
- go("LFFRR")
- turtle.select(slot:getItemSlot("dirt"))
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("dirt")) - 40) then-- chest full
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("dirt")) - 40)
- end
- else
- turtle.select(slot:getItemSlot("dirt"))
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("dirt")) - 40)
- end
- end
- slot.update{self = slot, item = "dirt", delete = true}
- end
- --[[if getStock("cobble") > 35 then
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("cobble")) - 35) then --chest full
- if not useOtherChest then
- changeDirection("faceBackward")
- go("LFFRR")
- useOtherChest = true
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("cobble")) - 35) then --chest full
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 35)
- end
- else
- turtle.select(slot:getItemSlot("cobble"))
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 35)
- end
- end
- slot.update{self = slot, item = "cobble", delete = true}
- end]]--
- if getStock("gravel") > 1 then
- turtle.select(slot:getItemSlot("gravel")) -- could be on second chest already
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("gravel")) - 1) then --chest full
- if not useOtherChest then
- changeDirection("faceBackward")
- go("LFFRR")
- useOtherChest = true
- turtle.select(slot:getItemSlot("gravel"))
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("gravel")) - 1) then --chest full
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("gravel")) - 1)
- end
- else
- turtle.select(slot:getItemSlot("gravel"))
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("gravel")) - 1)
- end
- end
- slot.update{self = slot, item = "gravel", delete = true}
- end
- if getStock("buckets") > 1 then
- turtle.select(slot:getItemSlot("buckets"))
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("buckets")) - 1) then-- chest full
- if not useOtherChest then
- useOtherChest = true
- changeDirection("faceBackward")
- go("LFFRR")
- turtle.select(slot:getItemSlot("buckets"))
- if not turtle.dropUp(turtle.getItemCount(slot:getItemSlot("buckets")) - 1) then-- chest full
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("buckets")) - 1)
- end
- else
- turtle.select(slot:getItemSlot("buckets"))
- turtle.dropDown(turtle.getItemCount(slot:getItemSlot("buckets")) - 1)
- end
- end
- slot.update{self = slot, item = "buckets", delete = true}
- end
- repeat
- wasteFound = false
- for i = 1, 12 do
- if slot:getItemSlot(waste[i]) > 0 then
- wasteFound = true
- saveToLog("Storing mining waste: "..waste[i], false, true)
- turtle.select(slot:getItemSlot(waste[i]))
- if not turtle.dropUp() then --chest is full
- if not useOtherChest then
- changeDirection("faceBackward")
- go("LFFRR")
- useOtherChest = true
- turtle.select(slot:getItemSlot(waste[i]))
- if not turtle.dropUp() then --chest full
- turtle.dropDown()
- end
- else
- turtle.select(slot:getItemSlot(waste[i]))
- turtle.dropDown()
- end
- end
- slot.update{self = slot, item = waste[i], delete = true}
- sortInventory(true)
- end
- end
- until not wasteFound
- if useOtherChest then
- go("FFL")
- end
- else
- for i = 1, 16 do
- if slot:getSlotContains(i) == "" and turtle.getItemCount(i) > 0 then
- doSort = true
- turtle.select(i)
- if not turtle.dropDown() then-- chest full
- turtle.dropUp()
- end
- end
- end
- if doSort then
- sortInventory(true)
- end
- if getStock("dirt") > 40 then
- turtle.select(slot:getItemSlot("dirt"))
- if not turtle.dropDown(turtle.getItemCount(slot:getItemSlot("dirt")) - 40) then-- chest full
- turtle.dropUp(turtle.getItemCount(slot:getItemSlot("dirt")) - 40)
- end
- slot.update{self = slot, item = "dirt", delete = true}
- end
- if getStock("gravel") > 1 then
- turtle.select(slot:getItemSlot("gravel"))
- if not turtle.dropDown(turtle.getItemCount(slot:getItemSlot("gravel")) - 1) then-- chest full
- turtle.dropUp(turtle.getItemCount(slot:getItemSlot("gravel")) - 1)
- end
- slot.update{self = slot, item = "gravel", delete = true}
- end
- --[[if getStock("cobble") > 35 then
- turtle.select(slot:getItemSlot("cobble"))
- if not turtle.dropDown(turtle.getItemCount(slot:getItemSlot("cobble")) - 35) then --chest full
- turtle.dropUp(turtle.getItemCount(slot:getItemSlot("cobble")) - 35)
- end
- slot.update{self = slot, item = "cobble", delete = true}
- end]]--
- repeat
- wasteFound = false
- for i = 1, 12 do
- if slot:getItemSlot(waste[i]) > 0 then
- wasteFound = true
- saveToLog("Storing mining waste: "..waste[i], false, true)
- turtle.select(slot:getItemSlot(waste[i]))
- if not turtle.dropDown() then --chest is full
- turtle.dropUp()
- end
- slot.update{self = slot, item = waste[i], delete = true}
- sortInventory(true)
- end
- end
- until not wasteFound
- end
- if direction == "down" then
- up(1)
- end
- end
- 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
- arg.direction = arg.direction or "down"
- if arg.slotNo ~= nil then
- if arg.quantity == nil or arg.quantity == 0 then
- arg.quantity = turtle.getItemCount(arg.slotNo)
- elseif arg.quantity == -1 then --store all but 1 item
- arg.quantity = turtle.getItemCount(arg.slotNo) - 1
- end
- saveToLog("Storing "..arg.quantity.." "..arg.item.." from turtle slot "..arg.slotNo.." to chest "..arg.toStore:getStoreName(), false)
- turtle.select(arg.slotNo)
- if arg.direction == "down" then
- turtle.dropDown()
- elseif arg.direction == "forward" then
- turtle.drop()
- else
- turtle.dropUp()
- end
- 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)
- elseif arg.quantity == -1 then --store all but 1 item
- arg.quantity = turtle.getItemCount(slotNum) - 1
- 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)
- if arg.direction == "down" then
- turtle.dropDown(arg.quantity)
- elseif arg.direction == "forward" then
- turtle.drop(arg.quantity)
- else
- turtle.dropUp(arg.quantity)
- end
- 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 storeLadders()
- if getStock("ladders") > 0 then
- currentFunction = "storeLadders"
- changeDirection("faceForward")
- go("FFLFFRR")
- saveToLog("storeLadders: storing excess ladders", true, false)
- storeItem{toStore = storageWoodItems, direction = "down", item = "ladders", quantity = 0, updateSlot = true, doSort = true}
- go("FFRFFRR")
- end
- end
- function storeSand()
- local sandFound = false
- local cobbleAmount = 0
- currentFunction = "storeSand"
- if getStock("sand") > 0 then -- already identified
- sandFound = true
- else
- changeDirection("faceBackward")
- go("FFUU")
- saveToLog("storeSand: checking for sand", true, false)
- for i = 1, 16 do
- if string.find(slot:getSlotContains(i), "item") ~= nil then --item1, item 2 etc
- turtle.select(i)
- if turtle.placeDown() then -- only sand, ores, obsidian, mossy cobble
- saveToLog("storeSand: "..slot:getSlotContains(i).." placed down", true, false)
- sleep(0.5)
- if turtle.detectDown() then
- turtle.digDown()
- else --dropped must be sand
- saveToLog("storeSand: "..slot:getSlotContains(i).." not detected below", true, false)
- down(1)
- if turtle.compareDown() then
- saveToLog("storeSand: "..slot:getSlotContains(i).." compares, sand found", true, false)
- turtle.digDown()
- slot.update{self = slot, slotNo = i, newItem = "sand"}
- sandFound = true
- else
- saveToLog("storeSand: "..slot:getSlotContains(i).." does not compare, redstone found", true, false)
- turtle.digDown()
- slot.update{self = slot, slotNo = i, newItem = "redstone"}
- end
- sortInventory(true)
- up(1)
- break
- end
- end
- end
- end
- go("DDRRFF")
- end
- end
- function storeSigns()
- if getStock("signs") > 0 then
- currentFunction = "storeSigns"
- changeDirection("faceForward")
- go("FFLFFRR")
- saveToLog("storeSigns: storing excess signs", true, false)
- storeItem{toStore = storageWoodItems, direction = "down", item = "signs", quantity = 0, updateSlot = true, doSort = true}
- go("FFRFFRR")
- end
- end
- function storeSticks()
- if getStock("sticks") > 0 then
- currentFunction = "storeSticks"
- changeDirection("faceForward")
- go("FFRFF")
- saveToLog("storeSticks: storing excess sticks", true, false)
- storeItem{toStore = storageSticks, direction = "down", item = "sticks", quantity = 0, updateSlot = true, doSort = true}
- go("RRFFLFFRR")
- end
- end
- function storeWoodItems()
- currentFunction = "storeWoodItems"
- changeDirection("faceForward")
- go("FFLFFRR")
- saveToLog("storeWoodItems: storing planks, fences and other wooden items", true, false)
- for i = 1, 16 do
- if string.find(slot:getSlotContains(i), "item") ~= nil then --item1, item 2 etc
- turtle.select(i)
- if turtle.refuel(0) then
- saveToLog("storeWoodItems: storing "..slot:getSlotContains(i), true, false)
- turtle.dropDown()
- slot.update{self = slot, item = slot:getSlotContains(i), delete = true}
- sortInventory(true)
- end
- end
- end
- go("FFRFFRR")
- end
- function turnLeft(steps)
- for i = 1, steps do
- turtle.turnLeft()
- location:setFacing(location:getFacing() - 1)
- saveStatus()
- end
- end
- function turnRight(steps)
- for i = 1, steps do
- turtle.turnRight()
- location:setFacing(location:getFacing() + 1)
- saveStatus()
- end
- end
- function up(steps, useSlot, itemList, calledFrom)
- local success = true
- local maxTries = 2
- local previousFunction = currentFunction
- currentFunction = "up"
- useSlot = useSlot or 1
- if turtle.getFuelLevel() < steps then
- saveToLog(" ***** up: Fuel < "..steps.." *****", true, false)
- if not refuel(0) then
- saveToLog(" ***** up: refuel failed*****", true, false)
- end
- end
- for i = 1, steps do
- if turtle.detectUp() then
- while turtle.detectUp() do
- if dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom} then --not bedrock
- --allow for sand and gravel
- sleep(.7)
- else -- at bedrock
- success = false
- break
- end
- end
- end
- if success then
- while not turtle.up() do
- dig.digNew{self = dig, direction = "up", slotNo = useSlot, checkForItems = itemList, callFrom = calledFrom}
- 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 = 2
- else
- saveToLog("forward: Out of fuel. Game Over!", true)
- error()
- end
- end
- end
- end
- success = true
- location:setY(location:getY() + 1)
- saveStatus()
- end
- end
- currentFunction = previousFunction
- return success
- end
- function main()
- initialise() -- Setup global variables
- getPreferences() -- setup or read logfile preferences
- getSupplies() -- ask player for dirt, cobble, stone, gravel, bucket, wood
- getCoords() -- Get input from player
- getReady() -- Create chests, ladders, signs and refuel
- findCobble() -- Get enough cobble to craft furnace and build base camp
- clearBase() -- Clear area around base, place chests, make torches
- completeMineshaft() -- dig down to bedrock / up to surface
- mineAll() -- go to level 8 below current and begin
- end
- --**************Functions and classes/objects above***************--
- --****************************************************************--
- --***********************PROGRAM START****************************--
- main() -- Entire Program runs from here
Add Comment
Please, Sign In to add comment