Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- selfReplicate.lua
- -- Using CC 1.65 to 1.79 on Minecraft 1.7.10 and 1.8.9
- -- Takes advantage of turtle.inspect() and turtle.getItemDetail()
- -- pastebin http://pastebin.com/2Krm3Yva
- -- multiline comments not used as Pastebin trashes the appearance of the code!
- -- functions in alphabetical order except main()
- -- classes declared after functions
- -- CraftOS 1.6 names: (altered in main() if using CraftOS 1.7)
- ccComputer = "ComputerCraft:CC-Computer"
- ccDisk = "ComputerCraft:disk"
- ccTurtle = "ComputerCraft:CC-Turtle"
- ccCraftyMiningTurtle = "ComputerCraft:CC-TurtleExpanded"
- ccDiskDrive = "ComputerCraft:CC-Peripheral"
- function checkTreeFarm()
- -- will only be called if bucket already onboard
- -- do not bother if no saplings
- if not treeFarm:getPondFilled() then --pond not filled yet
- fillPond() -- use iron ore from creating mine for bucket, fill pond with water
- end
- if treeFarm:getPondFilled() then --pond filled
- if not treeFarm:getFarmCreated() then --treefarm not yet made
- clearTreeFarm() -- clear 11 x 10 area to right of base and plant 4 saplings
- end
- if treeFarm:getFarmCreated() then -- treefarm built
- if not treeFarm:getHopperPlaced() then --hopper not yet built
- craftHopper()
- end
- end
- if treeFarm:getFarmCreated() and treeFarm:getHopperPlaced() then --treefarm built, hopper placed
- if not treeFarm:getFarmFlooded() then
- floodTreeFarm() -- Use bucket and pond to flood tree farm base ready for sapling collection
- end
- end
- end
- end
- function checkWaterCoordsOK(x,z)
- local result = true
- -- 0 = go south (z increases)
- -- 1 = go west (x decreases)
- -- 2 = go north (z decreases
- -- 3 = go east (x increases)
- -- check if water coords are within storage placement area
- if coordHome:getFacing() == 0 then
- --zCoord = zCoord + 1
- if coordHome:getZ() + z > coordHome:getZ() then --in same direction. Check x+1,x-1
- if coordHome:getX() == x or coordHome:getX() + 1 == x or coordHome:getX() - 1 == x then
- result = false
- end
- end
- elseif coordHome:getFacing() == 1 then
- --xCoord = xCoord - 1
- if coordHome:getX() + x < coordHome:getX() then --in same direction. Check z+1,z-1
- if coordHome:getZ() == z or coordHome:getZ() + 1 == z or coordHome:getZ() - 1 == z then
- result = false
- end
- end
- elseif coordHome:getFacing() == 2 then
- --zCoord = zCoord - 1
- if coordHome:getZ() + z < coordHome:getZ() then --in same direction. Check x+1,x-1
- if coordHome:getX() == x or coordHome:getX() + 1 == x or coordHome:getX() - 1 == x then
- result = false
- end
- end
- else
- --xCoord = xCoord + 1
- if coordHome:getX() + x > coordHome:getX() then --in same direction. Check z+1,z-1
- if coordHome:getZ() == z or coordHome:getZ() + 1 == z or coordHome:getZ() - 1 == z then
- result = false
- end
- end
- end
- return result
- end
- function clearBase()
- local goUp = 0
- saveToLog("clearBase() Starting...", true)
- emptyAfterHarvest() --empty trash
- --clear area around first tree 5 X 5 square
- --craft furnace
- T:craft("minecraft:furnace", 1, "minecraft:cobblestone", nil, nil, false)
- T:go("FuxCL")
- for i = 1, 3 do
- T:go("Fu*")
- end
- T:go("Fu*L")
- for i = 1, 2 do
- T:go("Fu*")
- end
- T:go("Fu*L")
- for i = 1, 5 do
- T:go("Fu*")
- end
- T:go("Fu*L Fu*")
- T:go("Fu*DDxCUU")
- T:go("Fu*L Fu*L FuxC FuxCR")
- T:go("FuxC FuxC")
- T:go("FuDxC FuxCR FuxCR FuxC")
- T:go("FuxCUxCFL")
- --put furnace above
- turtle.select(T:getItemSlot("minecraft:furnace"))
- turtle.placeUp()
- saveToLog("clearBase() Completed. Saving Status '4'", true)
- saveStatus("4")
- end
- function clearTreeFarm()
- local stockData = {}
- stockData = T:getStock("minecraft:log")
- local numLogs = stockData.count
- stockData = T:getStock("minecraft:torch")
- local numTorches = stockData.count
- local torchesNeeded = 10 - numTorches
- -- turtle class .go() function
- -- R L F B U D + - = Right, Left, Forward, Back, Up, Down, up while detect, down while not detect
- -- d u x M N O Q = dIG, DIGuP, x-cavate (digDown), mineItem "up" and close with cobble, digUp if valuable and close, digDown if valuable and close, digDown if valuable
- -- c h k C H r S T P = cHECK/ChECK/CHECk place Cobble down/forward/up if no block, place cobblestone/dirt, cHest, Sapling, Torch, DIrT, hoPper
- saveToLog("clearTreeFarm() starting")
- --clear farm area
- treeFarm:reset()
- T:sortInventory()
- if torchesNeeded < 0 then
- torchesNeeded = 0
- else
- if numLogs > 4 then
- craftTorches(8) -- craft 6 torches for tree farm
- end
- end
- emptyTurtle(true)
- -- get saplings
- T:go("RFFDRR")
- while turtle.suckDown() do end
- stockData = T:getStock("minecraft:sapling")
- if stockData.count > 0 then
- T:go("UFFR")
- --get cobble/dirt
- T:go("FFFFRR")
- while turtle.suckDown() do end
- T:go("FFFFRR")
- T:go("RFFLU") -- on water enclosure wall
- -- build outer wall
- for i = 1, 9 do --|
- T:go("xC+-F") --|
- end --col 1, row 1 --|
- T:turnRight(1)
- for i = 1, 9 do --_________
- T:go("xC+-F")
- end --col 10, row 1
- T:turnRight(1)
- for i = 1, 10 do --|
- T:go("xC+-F") --|
- end --col 10, row 19--|
- T:turnRight(1)
- for i = 1, 9 do --_________
- T:go("xC+-F")
- end --col 1, row 19
- T:turnRight(1)
- T:go("xC+-F")
- --clear ground within outer wall
- T:go("RFDL")
- for i = 1, 8 do
- T:go("c+-F")
- end -- col 2, row 2
- T:turnRight(1)
- for j = 1, 4 do
- for i = 1, 7 do
- T:go("c+-F")
- end
- T:go("Rc+-FR")
- for i = 1, 7 do
- T:go("c+-F")
- end
- T:go("Lc+-FL")
- end
- for i = 1, 7 do
- T:go("c+-F")
- end -- col 9, row 9 on right side of farm
- -- channel across end of farm
- T:go("c+-LD") -- stay at bottom end
- T:go("RdCRhRhxC FxCLhR FxCLhR FxCLhR FxCLhR FxCLhR FxCLhR FxCLhR FudCRR FFF ULFFF") --on top of bottom wall left
- --place dirt and torches
- for i = 1, 4 do
- T:go("rBuTFFFFR") --place dirt, Back, digUp, place Torch, forward 3, turn right
- end
- T:go("BBUUUFF") --dodge torch on first block
- T:go("xS FFFSR FFFSR FFFS FFFDDDR FFFF FFFFR")
- treeFarm:setFarmCreated(true)
- treeFarm:setTimePlanted(os.time())
- saveStatus("treeFarm")
- emptyTurtle(false)
- else -- no saplings abandon tree farm
- T:go("UFFR")
- saveToLog("No saplings: treeFarm abandoned")
- end
- end
- function completeMining(status)
- local level = 14
- local woodNeeded = 0
- stockData = {}
- if status == 8 then
- level = 14
- elseif status == 9 then
- level = 11
- elseif status == 10 then
- level = 8
- end
- saveToLog("completeMining() Starting level "..level, true)
- emptyTurtle(false)
- checkTreeFarm()
- repeat --start at level 14, then 11, and finally 8
- if T:getFuelStock() + turtle.getFuelLevel() < 1500 then
- woodNeeded = math.ceil((1500 - T:getFuelStock() - turtle.getFuelLevel()) / 60 )
- saveToLog("logs needed = "..woodNeeded.." Waiting for tree farm")
- waitForTreeFarm(woodNeeded)
- end
- goMining(level)
- level = level - 3
- emptyTurtle(false)
- checkTreeFarm()
- if level < 8 then
- break
- end
- stockData = T:getStock("minecraft:diamond")
- if level == 14 then
- saveToLog("completeMining() level 14 complete. Saving Status '9'", true)
- saveStatus("9")
- elseif level == 11 then
- saveToLog("completeMining() level 11 complete. Saving Status '10'", true)
- saveStatus("10")
- elseif level == 8 then
- saveToLog("completeMining() level 8 complete. Saving Status '11'", true)
- saveStatus("11")
- end
- until stockData.count >= 6
- --all simple areas mined out or >7 diamonds found. turtle back at furnace
- end
- function craftHopper()
- local doContinue = false
- local stockData = {}
- saveToLog("craftHopper() started")
- stockData = T:getStock("minecraft:iron_ore")
- if stockData.count >= 5 then
- stockData = T:getStock("minecraft:chest")
- if stockData.count < 2 then
- if T:craftChests(1, false) >= 1 then
- doContinue = true
- end
- end
- if doContinue then --chests x 1 available
- doContinue = false
- if smelt("minecraft:iron_ore", 5) then
- doContinue = true
- end
- end
- if doContinue then -- chests x 1, and iron x 5 available
- doContinue = false
- if T:craft("minecraft:hopper", 1, "minecraft:iron_ingot", "minecraft:chest", nil, false) then
- doContinue = true
- end
- end
- if doContinue then -- hopper available
- T:go("RFFFFRRDDd")
- turtle.select(T:getItemSlot("minecraft:hopper"))
- turtle.place()
- T:go("UxCUFFFFR")
- treeFarm:setHopperPlaced(true)
- saveStatus("treeFarm")
- end
- end
- saveToLog("craftHopper() completed")
- return doContinue
- end
- function craftMiningTurtle(numTurtles)
- local itemSlot = 0
- local keepNum = 0
- local logType = "minecraft:log"
- local logsNeeded = 0
- local numSticks = 0
- local sticksNeeded = 0
- local startFile = ""
- local itemInStock = 0
- local maxItemSlot = 0
- local minItemSlot = 0
- local stockData = {}
- local doContinue = false
- -- chest1 = sticks
- -- chest2 = logs (oak/birch/jungle etc)
- -- chest3 = cobblestone, dirt
- -- chest4 = sand
- -- chest5 = iron_ore
- -- chest6 = redstone
- -- chest7 = diamond
- -- chest8 = gold_ore, lapis, mossy_cobblestone, obsidian
- -- chest9 = saplings (oak/birch,jungle)
- -- Logs for Items needed:
- -- 1 turtle 2 turtles
- -- 6 sand 2 logs 2 logs
- -- disk dr: 7 cobble 2 logs 2 logs
- -- turtle: 7 cobble 2 logs 4 logs
- -- ironore: 7 iron ore 2 logs 4 logs
- -- crafting: 1 log 2 logs
- -- chests: 2 logs 4 logs
- -- pickaxe: 1 log 1 log
- -- Min 13 wood Max 21 wood
- saveToLog("craftMiningTurtle() started")
- -- start under furnace / over chest1
- if numTurtles == 0 then
- term.clear()
- saveToLog("Not enough diamonds in this area.\nMission has failed", true)
- error()
- elseif numTurtles == 1 then -- 0 - 2 diamonds + pickaxe
- sticksNeeded = 2
- logsNeeded = 20
- elseif numTurtles == 2 then
- sticksNeeded = 4
- logsNeeded = 28
- end
- T:sortInventory()
- T:refuel(60)
- --empty turtle into correct storage
- dropItem("minecraft:stick", 0)
- T:forward(2) -- logs
- dropItem("minecraft:log", 0)
- dropItem("minecraft:log2", 0)
- T:forward(2) --dirt cobble
- dropItem("minecraft:cobblestone", 0)
- dropItem("minecraft:dirt", 0)
- T:forward(2) --sand reeds
- dropItem("minecraft:sand", 0)
- T:forward(2) --iron ore
- dropItem("minecraft:iron_ore", 0)
- dropItem("minecraft:bucket", 0)
- T:forward(2) --redstone
- dropItem("minecraft:redstone", 0)
- T:forward(2) -- diamond
- dropItem("minecraft:diamond", 0)
- T:forward(2)
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- if T:getItemName(i) ~= "minecraft:chest" then
- dropItem(T:getItemName(i))
- end
- end
- end
- -- only chest(s) left behind in turtle
- itemSlot = T:getItemSlot("minecraft:chest")
- if itemSlot ~= 1 then
- turtle.select(itemSlot)
- turtle.transferTo(1)
- end
- -- go back and remove supplies
- T:go("RRFF") -- diamonds
- turtle.select(1)
- turtle.suckDown()
- itemSlot = T:getItemSlot("minecraft:diamond")
- keepNum = numTurtles * 3
- turtle.select(itemSlot)
- if turtle.getItemCount(itemSlot) > keepNum then
- turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
- end
- T:forward(2) --redstone
- turtle.select(1)
- turtle.suckDown()
- itemSlot = T:getItemSlot("minecraft:redstone")
- keepNum = numTurtles + 3
- turtle.select(itemSlot)
- if turtle.getItemCount(itemSlot) > keepNum then
- turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
- end
- T:forward(2) --iron ore
- turtle.select(1)
- turtle.suckDown()
- itemSlot = T:getItemSlot("minecraft:iron_ore")
- keepNum = numTurtles * 7
- turtle.select(itemSlot)
- if turtle.getItemCount(itemSlot) > keepNum then
- turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
- end
- T:forward(2) --sand and reeds
- turtle.select(1)
- while turtle.suckDown() do end
- itemSlot = T:getItemSlot("minecraft:sand")
- keepNum = 6
- turtle.select(itemSlot)
- if turtle.getItemCount(itemSlot) > keepNum then
- turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
- end
- itemSlot = T:getItemSlot("minecraft:reeds")
- turtle.select(itemSlot)
- keepNum = 3
- if turtle.getItemCount(itemSlot) > keepNum then
- turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
- end
- T:forward(2) --cobblestone
- keepNum = numTurtles * 7 + 7
- repeat
- turtle.suckDown()
- stockData = T:getStock("minecraft:cobblestone")
- until stockData.count >= keepNum
- itemSlot = T:getItemSlot("minecraft:cobblestone")
- dropItem("minecraft:cobblestone", turtle.getItemCount(itemSlot) - keepNum)
- dropItem("minecraft:dirt", 0)
- T:forward(2) --logs
- while turtle.suckDown() do
- stockData = T:getStock("minecraft:log")
- if stockData.count >= logsNeeded then
- break
- end
- stockData = T:getStock("minecraft:log2")
- if stockData.count >= logsNeeded then
- break
- end
- end
- T:forward(2) -- sticks
- T:turnRight(2)
- --empty furnace
- T:go("BU")
- turtle.select(1)
- turtle.suck()
- T:go("DF")
- itemSlot = T:getItemSlot("minecraft:planks")
- if itemSlot > 0 then
- stockData = T:getStock("minecraft:planks")
- if stockData.count >= 2 then
- craftSticks(4)
- end
- itemSlot = T:getItemSlot("minecraft:planks")
- if itemSlot > 0 then
- turtle.select(itemSlot)
- turtle.refuel()
- end
- end
- turtle.select(1)
- turtle.suckDown()
- itemSlot = T:getItemSlot("minecraft:stick")
- keepNum = sticksNeeded
- if itemSlot > 0 then
- if turtle.getItemCount(itemSlot) > keepNum then
- turtle.select(itemSlot)
- turtle.dropDown(turtle.getItemCount(itemSlot) - keepNum)
- sticksNeeded = 0
- end
- end
- -- logs: crafting table = 2, sticks = 1, smelt
- -- planks: crafting table = 8, sticks = 2, smelt cobble = 21, smelt ironore = 14, smelt sand = 6, total 64
- stockData = T:getStock("minecraft:log")
- if stockData.count == 0 then
- stockData = T:getStock("minecraft:log2")
- logType = "minecraft:log2"
- end
- if stockData.count < logsNeeded then --not enough logs on board
- harvestTreeFarm()
- stockData = T:getStock("minecraft:log")
- logType = "minecraft:log"
- if stockData.count == 0 then
- stockData = T:getStock("minecraft:log2")
- logType = "minecraft:log2"
- end
- end
- if sticksNeeded > 0 then
- craftSticks(4)
- end
- T:craft("minecraft:diamond_pickaxe", 1, "minecraft:stick", "minecraft:diamond", nil, false)
- if numTurtles == 2 then --make 2 diamond pickaxe
- T:craft("minecraft:diamond_pickaxe", 1, "minecraft:stick", "minecraft:diamond", nil, false)
- end
- smelt("minecraft:cobblestone", 7 * numTurtles + 7)
- smelt("minecraft:iron_ore", 7 * numTurtles)
- smelt("minecraft:sand", 6)
- T:craft("minecraft:planks", 12 * numTurtles, logType, nil, nil, false)
- T:craftChests(numTurtles)
- T:craft("minecraft:crafting_table", numTurtles, "minecraft:planks", nil, nil, false)
- itemSlot = T:getItemSlot("minecraft:planks")
- if itemSlot > 0 then
- turtle.select(itemSlot)
- turtle.refuel()
- end
- T:craft("minecraft:paper", 3, "minecraft:reeds", nil, nil, false)
- T:craft("minecraft:glass_pane", 16, "minecraft:glass", nil, nil, false)
- T:craft(ccDisk, 1, "minecraft:paper", "minecraft:redstone", nil, false)
- T:craft(ccComputer, numTurtles, "minecraft:glass_pane", "minecraft:redstone", "minecraft:stone", false)
- T:craft(ccTurtle, numTurtles, "minecraft:chest", ccComputer, "minecraft:iron_ingot", false)
- T:craft(ccCraftyMiningTurtle, 1, "minecraft:crafting_table", "minecraft:diamond_pickaxe", ccTurtle, false)
- T:craft(ccDiskDrive, 1, "minecraft:redstone", "minecraft:stone", nil, false)
- T:go("RRFFFRR")
- --program mining turtle
- turtle.select(T:getItemSlot(ccDiskDrive))
- turtle.place()
- turtle.select(T:getItemSlot(ccDisk))
- turtle.drop()
- T:go("UF") --now on top of disk drive
- if disk.isPresent("bottom") then
- local filepath = shell.getRunningProgram()
- local filename = fs.getName(filepath)
- if fs.getFreeSpace("/disk/") > 130000 then -- use filecopy
- fs.copy(filepath, "/disk/"..filename)
- saveToLog("selfReplicate.lua copied to floppy disk", true)
- else
- local text = "Config file not modified on this system."
- text = text.."\nUnable to copy 150kB file."
- text = text.."\nMy offspring will download files from"
- text = text.."\nPastebin if connected to the internet"
- text = text.."\nLast resort:\ncopy selfReplicate.lua\nusing Windows file explorer"
- saveToLog(text, true)
- end
- --This script is a file called startup stored on the floppy disk
- --When a turtle is placed next to the disk drive, it reads this script
- --which opens 'minerList.txt' and sets the label to Miner2, (as '2' is stored in this file)
- --Either copies start.lua to the turtle then modifies 'minerList.txt' to '3'
- --or if on a server, requests the start.lua file via http from pastebin
- --so the name Miner3 given for the second turtle, (if constructed)
- -- create/overwrite 'minerList.txt' on floppy disk
- startFile = fs.open("/disk/minerList.txt", "w")
- startFile.writeLine("2")
- startFile.close()
- -- create/overwrite startup
- startFile = fs.open("/disk/startup", "w")
- startFile.writeLine('function get(name, code)')
- startFile.writeLine(' local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))')
- startFile.writeLine(' if response then')
- startFile.writeLine(' local sCode = response.readAll()')
- startFile.writeLine(' if sCode ~= nil and sCode ~= "" then')
- startFile.writeLine(' local file = fs.open( name, "w" )')
- startFile.writeLine(' response.close()')
- startFile.writeLine(' file.write(sCode)')
- startFile.writeLine(' file.close()')
- startFile.writeLine(' return true')
- startFile.writeLine(' end')
- startFile.writeLine(' end')
- startFile.writeLine(' return false')
- startFile.writeLine('end')
- startFile.writeLine('if fs.exists("/disk/minerList.txt") then')
- startFile.writeLine(' textFile = fs.open("/disk/minerList.txt", "r")')
- startFile.writeLine(' textIn = textFile.readAll()')
- startFile.writeLine(' minerName = "Miner"..textIn')
- startFile.writeLine(' textFile.close()')
- startFile.writeLine(' textOut = tostring(tonumber(textIn) + 1)')
- startFile.writeLine('else')
- startFile.writeLine(' minerName = "Miner2"')
- startFile.writeLine(' textOut = "3"')
- startFile.writeLine('end')
- startFile.writeLine('textFile = fs.open("/disk/minerList.txt", "w")')
- startFile.writeLine('textFile.writeLine(textOut)')
- startFile.writeLine('textFile.close()')
- startFile.writeLine('if os.getComputerLabel() == nil or string.find(os.getComputerLabel(),"Miner") == nil then')
- startFile.writeLine(' os.setComputerLabel(minerName)')
- startFile.writeLine('end')
- startFile.writeLine('print("Hello, my name is "..os.getComputerLabel())')
- startFile.writeLine('if fs.exists("/disk/selfReplicate.lua") then')
- startFile.writeLine(' fs.copy("/disk/selfReplicate.lua", "selfReplicate.lua")')
- startFile.writeLine(' print("Replication program copied")')
- startFile.writeLine('else')
- startFile.writeLine(' get("selfReplicate.lua", "'..pastebinCode..'")')
- startFile.writeLine('end')
- startFile.writeLine('print("Break me and move to a tree > 33 blocks away")')
- startFile.writeLine('print("use selfReplicate.lua to begin self replicating ")')
- startFile.close()
- end
- T:go("BDRFL")
- turtle.select(T:getItemSlot(ccCraftyMiningTurtle))
- turtle.place() --next to disk drive
- T:turnLeft(1)
- if numTurtles == 2 then
- T:craft(ccCraftyMiningTurtle, 1, "minecraft:crafting_table", "minecraft:diamond_pickaxe", ccTurtle, false)
- T:go("FRU")
- turtle.select(T:getItemSlot(ccCraftyMiningTurtle))
- turtle.place() --next to disk drive
- T:down(1)
- else
- T:go("FR")
- end
- term.clear()
- if numTurtles == 2 then
- saveToLog("Mission successful.\nI have replicated myself twice.\n\nRight-Click on my offspring to check their status...")
- else
- saveToLog("Mission partially successful.\nI have replicated myself once\n\nRight-Click on my offspring to check its status...")
- end
- end
- function craftBucket()
- local stockData = {}
- local success = false
- -- called only when under furnace
- stockData = T:getStock("minecraft:bucket")
- if stockData.count == 0 then
- stockData = T:getStock("minecraft:iron_ore")
- if stockData.count >= 3 then
- if smelt("minecraft:iron_ore", 3) then
- if T:craft("minecraft:bucket", 1, "minecraft:iron_ingot", nil, nil, false) then
- success = true
- end
- end
- end
- else
- success = true
- end
- return success
- end
- function craftSigns(signsNeeded)
- local success = false
- local woodNeeded = 0
- local woodInStock = 0
- local numPlanksNeeded = 0
- local logType = "minecraft:log"
- local stockData = {}
- turtle.suckDown() --get any sticks in chest below
- signsNeeded = signsNeeded or 3
- if signsNeeded > 12 then
- signsNeeded = 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
- if signsNeeded == 3 then
- woodNeeded = 2
- numPlanksNeeded = 8
- elseif signsNeeded == 6 then
- woodNeeded = 4
- numPlanksNeeded = 16
- elseif signsNeeded == 9 then
- woodNeeded = 5
- numPlanksNeeded = 20
- else
- woodNeeded = 7
- numPlanksNeeded = 28
- end
- woodInStock = T:getLogStock()
- if woodInStock >= woodNeeded then
- stockData = T:getStock("minecraft:log")
- if stockData.count < woodNeeded then
- stockData = T:getStock("minecraft:log2")
- if stockData.count >= woodNeeded then
- logType = "minecraft:log2"
- end
- end
- T:craft("minecraft:planks", numPlanksNeeded, logType, nil, nil, false)
- stockData = T:getStock("minecraft:stick")
- if stockData.count < 1 then
- T:craft("minecraft:stick", 4, "minecraft:planks", nil, nil, false)
- end
- if T:craft("minecraft:sign", signsNeeded, "minecraft:stick", "minecraft:planks", nil, false) then
- success = true
- end
- stockData = T:getStock("minecraft:planks")
- if stockData.count > 0 then
- turtle.select(stockData.primarySlot)
- turtle.refuel()
- end
- stockData = T:getStock("minecraft:stick")
- if stockData.count > 0 then
- turtle.select(stockData.primarySlot)
- turtle.dropDown()
- end
- end
- return success
- end
- function craftSticks(sticksNeeded)
- local success = false
- local makePlanks = false
- local woodNeeded = 0
- local planksNeeded = 0
- local doContinue = true
- local stockData = {}
- if sticksNeeded <= 4 then
- sticksNeeded = 4
- planksNeeded = 4
- woodNeeded = 1
- elseif sticksNeeded <= 8 then
- sticksNeeded = 8
- planksNeeded = 4
- woodNeeded = 1
- elseif sticksNeeded <= 12 then
- sticksNeeded = 12
- planksNeeded = 8
- woodNeeded = 2
- else
- sticksNeeded = 16
- planksNeeded = 8
- woodNeeded = 2
- end
- stockData = T:getStock("minecraft:planks")
- if stockData.count < planksNeeded then
- makePlanks = true
- else
- woodNeeded = 0 --reset
- end
- if makePlanks then --need wood
- stockData = T:getStock("minecraft:log")
- if stockData.count >= woodNeeded then
- T:craft("minecraft:planks", planksNeeded, T:getItemName(stockData.primarySlot), nil, nil, false)
- else
- doContinue = false
- end
- end
- if doContinue then
- if T:craft("minecraft:stick", sticksNeeded, "minecraft:planks", nil, nil, false) then
- success = true
- end
- end
- return success
- end
- function craftTorches(torchesNeeded)
- -- 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
- local logType = "minecraft:log"
- local headModifier = 1 --charcoal
- local headQuantity = 0
- local makeCharcoal = false
- local makePlanks = false
- local success = false
- local doContinue = true
- local woodNeeded = 0
- local planksNeeded = 0
- local sticksNeeded = 0
- local coalInStock = 0
- local charcoalInStock = 0
- local sticksInStock = 0
- local planksInStock = 0
- local stockData = {}
- saveToLog("craftTorches("..torchesNeeded..")")
- --turtle will be above storage chest to run this function
- turtle.select(1)
- --get sticks from storage
- while turtle.suckDown() do end
- stockData = T:getStock("minecraft:stick")
- sticksInStock = stockData.count
- torchesNeeded = math.floor(torchesNeeded / 4) * 4 -- correct torchesNeeded to multiple of 4
- headQuantity = torchesNeeded / 4 -- coal, charcoal
- stockData = T:getStock("minecraft:planks")
- planksInStock = stockData.count
- -- calculate sticks/planks/logs needed
- if torchesNeeded == 0 then
- torchesNeeded = 4 -- torches min 4
- if sticksInStock < 4 then
- planksNeeded = 4 -- 1 wood
- sticksNeeded = 4 -- 2 planks
- woodNeeded = 1
- end
- elseif torchesNeeded <= 16 then-- 4, 8, 12, 16
- if sticksInStock < 4 then
- planksNeeded = 4 -- 8 planks = 16 sticks
- sticksNeeded = 4 -- 4 sticks
- woodNeeded = 1
- end
- elseif torchesNeeded <= 32 then-- 4, 8, 12, 16
- if sticksInStock < 8 then
- planksNeeded = 4 -- 8 planks = 16 sticks
- sticksNeeded = 8 -- 8 sticks
- woodNeeded = 2
- end
- elseif torchesNeeded <= 48 then-- 4, 8, 12, 16
- if sticksInStock < 12 then
- planksNeeded = 8 -- 8 planks = 16 sticks
- sticksNeeded = 12 -- 12 sticks
- woodNeeded = 2
- end
- else
- if sticksInStock < 16 then
- planksNeeded = 8 -- 8 planks = 16 sticks
- sticksNeeded = 16 -- 16 sticks
- woodNeeded = 2
- end
- end
- --need either coal or charcoal
- stockData = T:getStock("minecraft:coal", 0)
- coalInStock = stockData.count
- stockData = T:getStock("minecraft:coal", 1)
- charcoalInStock = stockData.count
- if coalInStock >= headQuantity then
- headModifier = 0 --use coal
- else
- headModifier = 1 -- use charcoal
- end
- if headModifier == 1 then --use charcoal
- if charcoalInStock < headQuantity then
- makeCharcoal = true
- end
- -- extra logs needed for charcoal production
- woodNeeded = woodNeeded + headQuantity
- -- extra logs needed for charcoal fuel
- woodNeeded = woodNeeded + torchesNeeded / 4
- end
- saveToLog("craftTorches("..torchesNeeded..") coal="..coalInStock..", charcoal="..charcoalInStock..", planksNeeded="..planksNeeded..", woodNeeded="..woodNeeded)
- -- amount of logs needed known
- stockData = T:getStock("minecraft:log")
- if stockData.count < woodNeeded then
- stockData = T:getStock("minecraft:log2")
- if stockData.count >= woodNeeded then
- logType = "minecraft:log2"
- else -- not enough log/log2 onboard
- getItemFromStorage("minecraft:log", true) -- get all types of log from storage
- stockData = T:getStock("minecraft:log")
- if stockData.count < woodNeeded then
- stockData = T:getStock("minecraft:log2")
- if stockData.count >= woodNeeded then
- logType = "minecraft:log2"
- else -- not enough logs to make torches
- doContinue = false
- end
- else
- logType = "minecraft:log"
- end
- end
- end
- saveToLog("craftTorches("..torchesNeeded..") using "..logType)
- if doContinue then --enough raw materials onboard
- if sticksInStock == 0 or sticksInStock < headQuantity then
- stockData = T:getStock("minecraft:stick")
- if stockData.count < sticksNeeded then
- doContinue = false
- saveToLog("craftTorches("..torchesNeeded..") crafting sticks")
- if craftSticks(sticksNeeded) then
- doContinue = true
- end
- end
- end
- if doContinue then
- if makeCharcoal then
- doContinue = false
- saveToLog("craftTorches("..torchesNeeded..") smelting charcoal")
- if smelt(logType, headQuantity) then
- doContinue = true
- end
- end
- if doContinue then
- --make torches
- saveToLog("craftTorches("..torchesNeeded..") crafting torches...")
- if T:craft("minecraft:torch", torchesNeeded, "minecraft:stick", "minecraft:coal", nil, false) then
- success = true
- end
- end
- end
- stockData = T:getStock("minecraft:stick")
- if stockData.count > 0 then
- saveToLog("craftTorches("..torchesNeeded..") storing sticks")
- turtle.select(stockData.primarySlot)
- turtle.dropDown()
- end
- end
- return success
- end
- function createMine(status)
- -- initial excavation in centre complete.
- saveToLog("createMine() Starting...", true)
- if status == 5 then
- createMinePrepare(14)
- saveToLog("createMine() Starting Level 14", true)
- createMineLevel(14)
- status = 6
- saveToLog("createMine() Level 14 complete. Saving Status '6'", true)
- saveStatus("6")
- end
- if status == 6 then
- createMinePrepare(11)
- saveToLog("createMine() Starting Level 11", true)
- createMineLevel(11)
- status = 7
- saveToLog("createMine() Level 11 complete. Saving Status '7'", true)
- saveStatus("7")
- end
- if status == 7 then
- createMinePrepare(8)
- saveToLog("createMine() Starting Level 8", true)
- createMineLevel(8)
- status = 8
- saveToLog("createMine() Level 8 complete. Saving Status '8'", true)
- saveStatus("8")
- end
- saveToLog("createMine() Completed...", true)
- end
- function createMinePrepare(level)
- local stockData = {}
- local logsRequired = 0
- stockData = T:getStock("minecraft:coal") --either coal or charcoal
- local numCoal = stockData.count
- local numLogs = T:getLogStock()
- emptyTurtle(false) -- keeps iron_ore, cobble, dirt, bucket, torches, signs, log, log2
- -- may have iron ore so attempt bucket crafting / check if bucket onboard
- if craftBucket() then -- checks or crafts a bucket
- checkTreeFarm()
- end
- -- wood needed for level 14 signs: 2 for signs
- -- wood needed for all levels: 5 for fuel, 3 for torches, 2 for chests
- if level == 14 then
- logsRequired = 12
- else
- logsRequired = 10
- end
- if numCoal >= 2 then
- logsRequired = logsRequired - 2
- end
- if numLogs < logsRequired then
- if treeFarm:getFarmCreated() then
- waitForTreeFarm(10)
- end
- end
- numLogs = T:getLogStock()
- if numLogs >= 2 then -- make 1 chest if 2 logs
- T:craftChests(1, true)
- numLogs = T:getLogStock()
- end
- if numLogs >= 4 or (numLogs >= 2 and numCoal >= 2) then
- craftTorches(8) -- craft 8 torches 3 logs
- numLogs = T:getLogStock()
- end
- if level == 14 then
- if numLogs >= 2 then
- craftSigns(3) -- craft 3 signs 2 logs
- end
- end
- stockData = T:getStock("minecraft:sign")
- saveToLog("createMinePrepare("..level..") Signs avaiable: "..stockData.count, true)
- stockData = T:getStock("minecraft:torch")
- saveToLog("createMinePrepare("..level..") Torches avaiable: "..stockData.count, true)
- stockData = T:getStock("minecraft:chest")
- saveToLog("createMinePrepare("..level..") Chests avaiable: "..stockData.count - 1, true)
- end
- function createMineLevel(level)
- local tempYcoord = T:getY()
- local doContinue = false
- local data = {}
- local stockData = {}
- -- turtle class .go() function
- -- R L F B U D + - = Right, Left, Forward, Back, Up, Down, up while detect, down while not detect
- -- d u x M N O = dIG, DIGuP, x-cavate (digDown), mineItem "up" and close with cobble, digUp if valuable and close, digDown if valuable and close
- -- c h k C H r S T P = cHECK/ChECK/CHECk place Cobble down/forward/up if no block, place cobblestone/dirt, cHest, Sapling, Torch, DIrT, hoPper
- -- go down to level 14, 11, 8 and create + shape with firstTree at centre, 16 blocks length each arm
- -- torches at each end, and half way along
- T:forward(16) --now over mineshaft site
- if level == 14 then --first level so create mineshaft
- -- level out mine entrance and mark with a sign
- while turtle.detectUp() do
- T:up(1)
- end
- turtle.select(1)
- while T:getY() >= tempYcoord - 1 do
- for i = 1, 4 do
- turtle.dig()
- T:turnRight(1)
- end
- T:down(1)
- end
- T:up(1)
- turtle.select(T:getItemSlot("minecraft:cobblestone"))
- for i = 1,4 do
- turtle.place()
- T:turnRight(1)
- end
- T:up(1)
- if T:getItemSlot("minecraft:sign") > 0 then
- turtle.select(1)
- turtle.dig()
- turtle.select(T:getItemSlot("minecraft:sign"))
- turtle.place("Diamond Mine\nPreparing for\nDeep Excavation\nLevel "..tostring(level))
- --dump remaining signs
- turtle.dropDown()
- end
- else
- turtle.select(1)
- turtle.dig() -- dig existing sign
- if T:getItemSlot("minecraft:sign") > 0 then
- turtle.select(T:getItemSlot("minecraft:sign"))
- turtle.place("Diamond Mine\nPreparing for\nDeep Excavation\nLevel "..tostring(level))
- end
- end
- mineEntrance:setX(T:getX())
- mineEntrance:setY(T:getY())
- mineEntrance:setZ(T:getZ())
- mineEntrance:setFacing(T:getFacing())
- T:turnRight(2)
- while T:getY() > level do
- T:down(1)
- if level == 14 then
- for i = 1, 4 do
- if T:isValuable("forward") then
- mineItem(true, "forward")
- else
- data = T:getBlockType("forward")
- if data.name == "minecraft:water" or data.name == "minecraft:flowing_water" then
- T:go("dC")
- end
- end
- T:turnRight(1)
- end
- end
- end
- T:go("xRRdCDdCRRU")
- if level == 14 then
- for i = 1, 32 do --create 32 block corridor and check ceiling
- T:go("FxN")
- end
- else
- for i = 1, 32 do
- T:go("Fx")
- end
- end
- T:go("dCDdCRROUxTFDO") -- create end of corridor
- for i = 1, 6 do -- check floor of 16 blocks
- T:go("FOLhRRhL")
- end
- T:go("FOLhRRhLUxTFDOLhRRhL")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FUR") -- create RH branch at ceiling level
- if level == 14 then
- for i = 1, 16 do
- T:go("FxN")
- end
- else
- for i = 1, 16 do
- T:go("Fx")
- end
- end
- T:go("dCDdCRROUxTFDO") --create end of RH branch
- for i = 1, 6 do -- create 16 block floor
- T:go("FOLhRRhL")
- end
- T:go("FOLhRRhLUxTFDOLhRRhL")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FU")
- if level == 14 then -- create LH branch at ceiling level
- for i = 1, 16 do
- T:go("FxN")
- end
- else
- for i = 1, 16 do
- T:go("Fx")
- end
- end
- T:go("dCDdCRROUxTFDOLhRRhL") -- create corridor end
- for i = 1, 6 do -- create 16 block floor
- T:go("FOLhRRhL")
- end
- T:go("FOLhRRhLUxTFDOLhRRhL")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FLx")
- stockData = T:getStock("minecraft:chest")
- if stockData.count > 1 then
- turtle.select(stockData.primarySlot)
- turtle.placeDown()
- end
- T:up(1)
- T:emptyTrash("down")
- T:down(1)
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FOLhRRhLUxTFDxCLhRRhL")
- for i = 1, 7 do
- T:go("FOLhRRhL")
- end
- T:go("FRR")
- while T:getY() < mineEntrance:getY() do
- T:up(1)
- end --at surface now
- T:forward(16)
- T:turnRight(2)
- end
- function dropItem(item, keepAmount)
- local itemSlot = 0
- local stockData = {}
- keepAmount = keepAmount or 0
- if keepAmount == 0 then -- drop everything
- itemSlot = T:getItemSlot(item)
- while itemSlot > 0 do
- turtle.select(itemSlot)
- turtle.dropDown()
- itemSlot = T:getItemSlot(item)
- end
- elseif keepAmount >= 64 then -- drop everything else
- stockData = T:getStock(item)
- while stockData.count > keepAmount do
- if stockData.primarySlot ~= stockData.finalSlot then
- turtle.select(stockData.finalSlot)
- else --only one slot but more than required in it
- turtle.select(stockData.primarySlot)
- end
- turtle.dropDown()
- stockData = T:getStock(item)
- end
- else --store specific amount
- itemSlot = T:getItemSlot(item)
- if itemSlot > 0 then
- turtle.select(itemSlot)
- turtle.dropDown(turtle.getItemCount(itemSlot) - keepAmount)
- end
- end
- end
- function emptyAfterHarvest()
- local item = ""
- local keepit = false
- local keepItems = { "minecraft:dirt", "minecraft:cobblestone", "minecraft:chest", "minecraft:log", "minecraft:log2", "minecraft:coal",
- "minecraft:sapling", "minecraft:iron_ore", "minecraft:diamond", "minecraft:redstone", "minecraft:sign", "minecraft:torch",
- "minecraft:water_bucket", "minecraft:lava_bucket", "minecraft:bucket", "minecraft:planks",
- "minecraft:sand", "minecraft:reeds", "minecraft:iron_ingot", "minecraft:emerald"}
- -- just finished harvesting all trees. get rid of apples, seeds any other junk
- -- or finished finding cobble after going into a disused mine
- for i = 1, 16 do
- keepit = false
- if turtle.getItemCount(i) > 0 then
- item = T:getItemName(i)
- for _,v in pairs(keepItems) do
- if v == item then
- keepit = true
- break
- end
- end
- if not keepit then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- end
- function emptyTurtle(keepAll)
- -- empty out turtle except for logs, coal, torches, signs, bucket, cobble, dirt,diamonds
- -- chest1 = sticks
- -- chest2 = logs (oak/birch/jungle etc)
- -- chest3 = cobblestone, dirt
- -- chest4 = sand
- -- chest5 = iron_ore
- -- chest6 = redstone
- -- chest7 = diamond
- -- chest8 = gold_ore, lapis, mossy_cobblestone, obsidian
- -- chest9 = saplings (oak/birch,jungle)
- local itemSlot = 0
- local stockData = {}
- T:sortInventory()
- stockData = T:getStock("minecraft:planks")
- if stockData.count > 0 then
- turtle.select(stockData.primarySlot)
- turtle.refuel()
- end
- --sapling chest
- T:go("RFFDRR")
- while turtle.suckDown() do end --remove any junk in the sapling chest as well
- dropItem("minecraft:sapling", 0)-- now only saplings in there
- T:go("UFFR")
- -- chest1 sticks only
- dropItem("minecraft:stick", 0)
- T:forward(2) -- chest2 logs of any type, keep 1 stack
- dropItem("minecraft:log", 64)
- dropItem("minecraft:log2", 64)
- T:forward(2) -- chest3 dirt and cobble
- if not keepAll then -- keep max 1 stack cobble
- dropItem("minecraft:cobblestone", 64)
- dropItem("minecraft:dirt", 64)
- end
- T:forward(2) -- chest4 sand and reeds
- dropItem("minecraft:sand", 0)
- dropItem("minecraft:reeds", 0)
- T:forward(2) -- chest5 iron ore only
- dropItem("minecraft:iron_ore", 64)
- T:forward(2) -- redstone
- dropItem("minecraft:redstone", 0)
- T:forward(4)
- -- chest8 gold ore, mossy cobblestone, obsidian, lapis, mob drops, misc items
- for i = 1, 16 do
- itemName = T:getItemName(i)
- if itemName ~= ""
- and itemName ~= "minecraft:chest"
- and itemName ~= "minecraft:torch"
- and itemName ~= "minecraft:iron_ore"
- and itemName ~= "minecraft:cobblestone"
- and itemName ~= "minecraft:dirt"
- and itemName ~= "minecraft:log"
- and itemName ~= "minecraft:log2"
- and itemName ~= "minecraft:sign"
- and itemName ~= "minecraft:coal"
- and itemName ~= "minecraft:diamond"
- and itemName ~= "minecraft:bucket" then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- T:turnRight(2)
- T:forward(14)
- T:turnRight(2)
- end
- function fillPond()
- local doContinue = false
- local fileHandle = ""
- local strText = ""
- local waterFound = false
- local numArms = 0
- local startFile = ""
- local itemSlot = 0
- local stockData = {}
- -- read 'waterCoords.txt' from file
- if fs.exists("waterCoords.txt") then
- fileHandle = fs.open("waterCoords.txt", "r")
- strText = fileHandle.readLine()
- water:setX(tonumber(string.sub(strText, 3)))
- strText = fileHandle.readLine()
- water:setY(tonumber(string.sub(strText, 3)))
- strText = fileHandle.readLine()
- water:setZ(tonumber(string.sub(strText, 3)))
- fileHandle.close()
- saveToLog("Water coords from file x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
- end
- T:sortInventory()
- stockData = T:getStock("minecraft:bucket")
- if stockData.count == 0 then
- if craftBucket() then
- doContinue = true
- end
- else
- doContinue = true
- end
- if doContinue then -- bucket available
- -- fetch 2 buckets water and charge water pool
- -- place 4 buckets water in tree farm corners
- -- dig 4 corner blocks in treefarm to allow water flow down to hoppers
- goToWater("x") -- calls safeRun which constantly checks for water
- goToWater("z") -- may have already collected water
- if T:getItemSlot("minecraft:water_bucket") == 0 then --no water found yet
- while not turtle.inspectDown() do -- must be air below
- T:down(1)
- end
- safeRun(1)
- T:turnRight(1)
- safeRun(1)
- T:turnRight(1) --now facing south, ready to continue spiral
- end
- if T:getItemSlot("minecraft:water_bucket") == 0 then
- numArms = 4
- for i = 2, numArms, 2 do
- safeRun(i)
- T:turnRight(1)
- safeRun(i)
- T:turnRight(1)
- safeRun(i + 1)
- T:turnRight(1)
- if i + 1 < numArms then
- safeRun(i + 1)
- T:turnRight(1)
- else
- --return to starting position
- safeRun(i / 2)
- T:turnRight(1)
- safeRun(i / 2 + 1)
- end
- end
- --changeDirection()
- if T:getItemSlot("minecraft:water_bucket") > 0 then
- startFile = fs.open("waterCoords.txt", "w")
- startFile.writeLine("x="..T:getX())
- startFile.writeLine("y="..T:getY())
- startFile.writeLine("z="..T:getZ())
- startFile.close()
- end
- end
- itemSlot = T:getItemSlot("minecraft:water_bucket")
- if itemSlot > 0 then
- treeFarm:setWaterFound(true)
- returnHome("x")
- returnHome("z")
- returnHome("facing")
- T:go("LFFF")
- turtle.select(T:getItemSlot("minecraft:water_bucket"))
- if turtle.placeDown() then
- saveToLog("Water added to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- T:go("RRFFFL")
- else
- T:go("LFLF")
- if turtle.placeDown() then
- saveToLog("Water added (after moving) to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- T:go("LFRFFL")
- end
- goToWater("x")
- goToWater("z")
- returnHome("x")
- returnHome("z")
- returnHome("facing")
- T:go("LFFLF")
- turtle.select(T:getItemSlot("minecraft:water_bucket"))
- if turtle.placeDown() then
- saveToLog("Water added to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- T:go("RRFRFFL")
- else
- T:go("RFRF")
- if turtle.placeDown() then
- saveToLog("Water added (after moving) to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- T:go("RFFFL")
- end
- else
- print("Water not found")
- error()
- end
- end
- treeFarm:setPondFilled(true)
- saveStatus("treeFarm")
- end
- function findCobble()
- local atBedrock = false
- local blockType = ""
- local tempY = T:getY()
- saveToLog("findCobble() Starting...", true)
- --just finished harvesting all trees. get rid of apples, seeds any other junk
- emptyAfterHarvest()
- T:down(1)
- repeat
- turtle.select(1)
- if not T:down(1) then
- atBedrock = true
- end
- for i = 1, 4 do
- if T:isValuable("forward") then
- -- call recursive function!!!
- mineItem(true, "forward")
- else
- turtle.dig()
- if T:getItemSlot("minecraft:gravel") > 0 then
- turtle.select(T:getItemSlot("minecraft:gravel"))
- turtle.drop()
- elseif T:getItemSlot("minecraft:flint") > 0 then
- turtle.select(T:getItemSlot("minecraft:flint"))
- turtle.drop()
- end
- end
- T:turnRight(1)
- turtle.select(1)
- end
- until atBedrock
- repeat
- T:up(1)
- until T:getY() == tempY - 4
- turtle.select(T:getItemSlot("minecraft:cobblestone"))
- for i = 1, 3 do
- for j = 1, 4 do
- turtle.select(T:getItemSlot("minecraft:cobblestone"))
- turtle.place()
- T:turnRight(1)
- end
- T:up(1)
- end
- T:up(1)
- saveToLog("findCobble() Completed. Saving status '3'", true)
- saveStatus("3")
- end
- function floodTreeFarm()
- saveToLog("floodTreeFarm() started")
- T:go("LFFRR") -- go to pond
- for i = 1, 9, 2 do --flood farm except for drainage channel
- turtle.select(T:getItemSlot("minecraft:bucket"))
- turtle.placeDown() -- Fill 1
- if i < 9 then
- T:forward(4 + i)
- else
- T:forward(12) -- far right side of farm
- end
- T:go("L FFFF FFFF U")
- turtle.select(T:getItemSlot("minecraft:water_bucket"))
- turtle.placeDown()
- T:turnLeft(1)
- if i < 9 then -- return to left wall of farm
- T:forward(i)
- else
- T:forward(8)
- end
- T:turnLeft(1)
- T:go("FFFF FFFF DR")
- T:forward(4)
- T:turnRight(2)
- -- ready for next bucket fill
- end
- turtle.select(T:getItemSlot("minecraft:bucket"))
- turtle.placeDown()
- T:forward(12)
- turtle.select(T:getItemSlot("minecraft:water_bucket"))
- turtle.placeDown()
- T:go("RR FFFF FFFF FFR")
- treeFarm:setFarmFlooded(true)
- saveStatus("treeFarm")
- saveToLog("floodTreeFarm() completed")
- end
- function getCoords()
- --get world coordinates from player
- local coord = 0
- local response = ""
- local continue = true
- local event = ""
- local param1 = ""
- term.clear()
- term.setCursorPos(1,1)
- if fs.exists("homeCoords.txt") then
- fileHandle = fs.open("homeCoords.txt", "r")
- strText = fileHandle.readLine()
- T:setX(tonumber(string.sub(strText, 3)))
- coordHome:setX(T:getX())
- strText = fileHandle.readLine()
- T:setY(tonumber(string.sub(strText, 3)))
- coordHome:setY(T:getY())
- strText = fileHandle.readLine()
- T:setZ(tonumber(string.sub(strText, 3)))
- strText = fileHandle.readLine()
- coordHome:setZ(T:getZ())
- T:setFacing(tonumber(string.sub(strText, 3)))
- coordHome:setFacing(T:getFacing())
- fileHandle.close()
- saveToLog("Coordinates loaded from file:", true)
- saveToLog("x = "..T:getX()..", y = "..T:getY()..", z = "..T:getZ()..", f = "..T:getFacing(), true)
- print()
- print("Miner1 assumed to be at Home Location")
- print()
- print("starting in 3 seconds")
- print("STAND CLEAR!")
- os.sleep(3)
- else
- 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 = ")
- 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 = ")
- end
- end
- T:setX(coord)
- term.clear()
- term.setCursorPos(1,1)
- print("Please enter your Y coordinate")
- write(" y = ")
- coord = nil
- while coord == nil do
- coord = tonumber(read())
- if coord == nil then
- term.clear()
- term.setCursorPos(1,1)
- print("Incorrect input. Use numbers only")
- print()
- print("Please enter your y coordinate")
- write(" y = ")
- end
- end
- T:setY(coord)
- term.clear()
- term.setCursorPos(1,1)
- print("Please enter your Z coordinate")
- write(" z = ")
- coord = nil
- while coord == nil do
- coord = tonumber(read())
- if coord == nil then
- term.clear()
- term.setCursorPos(1,1)
- print("Incorrect input. Use numbers only")
- print()
- print("Please enter your z coordinate")
- write(" z = ")
- end
- end
- T:setZ(coord)
- response = true
- while response do
- response = false
- term.clear()
- term.setCursorPos(1,1)
- print("Enter Direction you are facing:")
- print(" 0,1,2,3 (s,w,n,e)")
- print()
- print( " Direction = ")
- event, param1 = os.pullEvent ("char")
- --response = read()
- if param1 == "s" or param1 == "S" then
- coord = 0
- elseif param1 == "w" or param1 == "W" then
- coord = 1
- elseif param1 == "n" or param1 == "N" then
- coord = 2
- elseif param1 == "e" or param1 == "E" then
- coord = 3
- elseif param1 == "0" or param1 == "1" or param1 == "2" or param1 == "3" then
- coord = tonumber(param1)
- else
- print()
- print("Incorrect input: "..param1)
- print()
- print("Use 0,1,2,3,n,s,w,e")
- sleep(2)
- response = true
- end
- end
- T:setFacing(coord)
- term.clear()
- term.setCursorPos(1,1)
- print("Your current location is:")
- print()
- print(" x = "..T:getX())
- print(" y = "..T:getY())
- print(" z = "..T:getZ())
- print(" facing "..T:getCompass().." ("..T:getFacing()..")")
- print()
- write("Is this correct? (y/n)")
- event, param1 = os.pullEvent ("char")
- if param1 == "y" or param1 == "Y" then
- continue = false
- end
- end
- -- correct coords to compensate for player standing position
- -- First tree is considered as point zero, on startup, turtle is in front of this tree
- -- Player is behind turtle, use 2 blocks to compensate
- -- facing: Change:
- -- 0 (S) z+1
- -- 1 (W) x-1
- -- 2 (N) z-1
- -- 3 (E) x+1
- if T:getFacing() == 0 then
- T:setZ(T:getZ() + 2)
- elseif T:getFacing() == 1 then
- T:setX(T:getX() - 2)
- elseif T:getFacing() == 2 then
- T:setZ(T:getZ() - 2)
- elseif T:getFacing() == 3 then
- T:setX(T:getX() + 2)
- end
- coordHome:setX(T:getX())
- coordHome:setY(T:getY())
- coordHome:setZ(T:getZ())
- coordHome:setFacing(T:getFacing())
- -- create/overwrite 'homeCoords.txt'
- local fileHandle = fs.open("homeCoords.txt", "w")
- fileHandle.writeLine("x="..T:getX())
- fileHandle.writeLine("y="..T:getY())
- fileHandle.writeLine("z="..T:getZ())
- fileHandle.writeLine("f="..T:getFacing())
- fileHandle.close()
- saveToLog("homeCoords.txt file created", true)
- saveToLog("x = "..T:getX()..", y = "..T:getY()..", z = "..T:getZ()..", f = "..T:getFacing(), false)
- print()
- print("Press esc within 2 secs to watch the action!")
- sleep(2)
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Here we go...")
- sleep(2)
- term.clear()
- term.setCursorPos(1,1)
- end
- function getItemFromStorage(item, allItems)
- local success = false
- local moves = 0
- allItems = allItems or false
- if item == "minecraft:log" or item == "minecraft:log2" then
- moves = 2
- elseif item == "minecraft:cobblestone" or item == "minecraft:dirt"then
- moves = 4
- elseif item == "minecraft:sand" or item == "minecraft:reeds" then
- moves = 6
- elseif item == "minecraft:iron_ore" then
- moves = 8
- numIronore = 0
- elseif item == "minecraft:redstone" then
- moves = 10
- elseif item == "minecraft:diamond" then
- moves = 12
- end
- T:forward(moves)
- turtle.select(1)
- if allItems then
- while turtle.suckDown() do -- all items
- success = true
- end
- if T:getFirstEmptySlot() == 0 then
- turtle.select(T:getItemSlot("minecraft:dirt"))
- turtle.dropDown()
- end
- else
- if turtle.suckDown() then -- item removed from chest
- success = true
- end
- end
- T:turnRight(2)
- T:forward(moves)
- T:turnRight(2)
- return success
- end
- function goMining(level)
- stockData = {}
- -- start at furnace
- stockData = T:getStock("minecraft:torch")
- if stockData.count < 24 then
- craftTorches(24)
- end
- T:forward(16)
- turtle.dig()
- saveToLog("goMining() Level "..level.." started", true)
- if T:getItemSlot("minecraft:sign") > 0 then
- turtle.select(T:getItemSlot("minecraft:sign"))
- turtle.place("Diamond Mine\nMining for\nDiamonds\nLevel "..level)
- turtle.select(1)
- end
- T:turnRight(2)
- while T:getY() > level do
- T:down(1)
- end
- -- R L F B U D + - = Right, Left, Forward, Back, Up, Down, up while detect, down while not detect
- -- d u x M N O Q = dIG, DIGuP, digDown (x-cavate ?!), MineItem Up and close with cobble, DigUp if valuable and close, digDown if valuable and close, digDown if valuable
- -- c h k C H r S T P = cHECK/ChECK/CHECk place Cobble down/forward/up if no block, place cobblestone/dirt, cHest, Sapling, Torch, DIrT, hoPper
- --clear 33 x 33 square at ceiling level
- T:turnLeft(1)
- for i = 1, 16 do
- if level == 14 then
- T:go("FMxLhR")
- else
- T:go("FxkLhR")
- end
- end
- for j = 1, 3 do
- T:turnRight(1)
- for i = 1, 32 do
- if level == 14 then
- T:go("FMxLhR")
- else
- T:go("FxkLhR")
- end
- end
- end
- T:turnRight(1)
- for i = 1, 15 do
- if level == 14 then
- T:go("FMxLhR")
- else
- T:go("FxkLhR")
- end
- end
- T:go("FxD")
- --clear 33 x 33 square at floor level and place torches
- for i = 1, 7 do
- T:go("FOLhRRhL")
- end
- T:go("FOLhRUxTFDOLhRRhL")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- for j = 1, 3 do
- T:go("FOLhRRhLUxTRFDO")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FOLhRUxTFDOLhRRhL") --torch in mid path
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FOLhRUxTFDOLhR")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FOLhRUxTFDOLhRRhL")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- end
- T:go("FOLhRRhLUxTRFDO")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:go("FOLhRUxTFDOLhRRhL")
- for i = 1, 6 do
- T:go("FOLhRRhL")
- end
- T:forward(1)
- --empty rubbish
- T:up(1)
- T:turnRight(1)
- T:forward(16)
- T:emptyTrash("down")
- T:go("LFL")
- -- mine 1 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- T:go("FRFR")
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- if j < 8 then
- T:go("FLFL")
- end
- end
- T:go("FR")
- T:forward(16)
- T:emptyTrash("down")
- T:go("FR")
- -- mine 2 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- T:go("FLFL")
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- if j < 8 then
- T:go("FRFR")
- end
- end
- T:go("FL")
- T:forward(16)
- T:emptyTrash("down")
- T:go("FR")
- -- mine 3 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- T:go("FLFL")
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- if j < 8 then
- T:go("FRFR")
- end
- end
- T:go("FL")
- T:forward(16)
- T:emptyTrash("down")
- T:go("FL")
- -- mine 4 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- T:go("FRFR")
- for i = 1, 15 do
- T:forward(1)
- T:go("N")
- T:go("Q")
- end
- if j < 8 then
- T:go("FLFL")
- end
- end
- T:go("FR")
- T:forward(16)
- T:emptyTrash("down")
- T:turnLeft(1)
- T:forward(16)
- T:turnRight(2)
- while T:getY() < coordHome:getY() do
- T:up(1)
- end
- T:forward(16)
- T:turnRight(2)
- saveToLog("goMining() Level "..level.." completed", true)
- end
- function goToWater(goDirection)
- local startX = T:getX()
- local startZ = T:getZ()
- local itemSlot = 0
- saveToLog("goToWater() started", true)
- --x=-154 y=64 z=-2 f=3
- --water x=-149 y=63 z=-8
- if goDirection == "x" then
- if T:getX() < water:getX() then
- while T:getFacing() ~= 3 do
- T:turnRight(1)
- end
- while T:getX() < water:getX() do
- safeRun(1)
- itemSlot = T:getItemSlot("minecraft:water_bucket")
- if itemSlot > 0 then
- saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- if T:getX() < startX then --going wrong way
- T:turnRight(2)
- end
- end
- elseif T:getX() > water:getX() then
- while T:getFacing() ~= 1 do
- T:turnRight(1)
- end
- while T:getX() > water:getX() do
- safeRun(1)
- itemSlot = T:getItemSlot("minecraft:water_bucket")
- if itemSlot > 0 then
- saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- if T:getX() > startX then --going wrong way
- T:turnRight(2)
- end
- end
- end
- elseif goDirection == "y" then
- while T:getY() > water:getY() do
- T:down(1)
- end
- while T:getY() < water:getY() do --
- T:up(1) -- back to ground level at home
- end
- elseif goDirection == "z" then
- if T:getZ() > water:getZ() then -- go north
- while T:getFacing() ~= 2 do
- T:turnRight(1)
- end
- while T:getZ() > water:getZ() do
- safeRun(1)
- itemSlot = T:getItemSlot("minecraft:water_bucket")
- if itemSlot > 0 then
- saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- if T:getZ() > startZ then --going wrong way
- T:turnRight(2)
- end
- end
- elseif T:getZ() < water:getZ() then
- while T:getFacing() ~= 0 do
- T:turnRight(1)
- end
- while T:getZ() < water:getZ() do
- safeRun(1)
- itemSlot = T:getItemSlot("minecraft:water_bucket")
- if itemSlot > 0 then
- saveToLog("Water collected at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- if T:getZ() < startZ then --going wrong way
- T:turnRight(2)
- end
- end
- end
- end
- saveToLog("goToWater() completed", true)
- end
- function harvestAllTrees()
- -- create a 33 x 33 square with base camp in the centre
- -- - make sure on solid ground
- -- - If wood in front harvest tree.
- -- - else if leaves in front dig then move
- -- - else any other block climb up and over
- -- move in a spiral
- local numReeds = 0
- local numSand = 0
- local numLogs = 0
- local numSaplings = 0
- local waterFound = false
- local stockData = {}
- local doContinue = true
- saveToLog("harvestAllTrees() Starting...", true)
- harvestRun(1) -- move 1 blocks
- T:turnRight(1)
- harvestRun(1, true)
- T:turnRight(1) --now facing south, ready to continue spiral
- numArms = 32 --32 for full version 4 for testing
- for i = 2, numArms, 2 do -- 2,4,6,8...32
- if i < 5 then
- harvestRun(i, true)
- else
- harvestRun(i)
- end
- T:turnRight(1)
- if i < 5 then
- harvestRun(i, true)
- else
- harvestRun(i)
- end
- T:turnRight(1)
- if i < 5 then
- harvestRun(i + 1, true)
- else
- harvestRun(i + 1)
- end
- T:turnRight(1) --full square completed. Can return if supplies sufficient
- if i + 1 < numArms then
- -- toDo
- -- choose point to break
- saveToLog("harvestAllTrees() spiral arm no: "..i, true)
- stockData = T:getStock("minecraft:reeds")
- numReeds = stockData.count
- stockData = T:getStock("minecraft:sand")
- numSand = stockData.count
- stockData = T:getStock("minecraft:sapling")
- numSaplings = stockData.count
- numLogs = T:getLogStock()
- waterFound = treeFarm:getWaterFound()
- saveToLog("reeds = "..numReeds..", sand = "..numSand..", logs = "..numLogs..", Water found = "..tostring(waterFound),true)
- if numReeds >= 3 and numSand >= 6 and numLogs >= 40 and treeFarm:getWaterFound() then
- if (numLogs >= 30 and numSaplings >=4) or (numLogs > 60 and numSaplings == 0)then
- doContinue = false
- saveToLog("harvestAllTrees() Abandoned after "..i.." spiral arms: Supplies sufficient", true)
- --return to starting position
- harvestRun(i / 2)
- T:turnRight(1)
- harvestRun(i / 2 + 1)
- T:turnRight(2)
- break
- end
- end
- if i < 2 then
- harvestRun(i + 1, true)
- else
- harvestRun(i + 1)
- end
- T:turnRight(1)
- else -- i + 1 >= 32 (i = 32)
- --return to starting position
- harvestRun(i / 2)
- T:turnRight(1)
- harvestRun(i / 2 + 1)
- end
- end
- if doContinue then
- T:turnRight(2)
- saveToLog("harvestAllTrees() Completed", true)
- end
- saveToLog("Saving Status: '2'", true)
- saveStatus("2")
- end
- function harvestAndReplant()
- if T:isValuable("forward") then -- log in front
- harvestTree()
- else
- turtle.select(1)
- T:forward(1) --sapling harvested if still present
- end
- itemSlot = T:getItemSlot("minecraft:sapling")
- if itemSlot > 0 then -- move up, plant sapling, move forward and down
- T:up(1)
- turtle.select(itemSlot)
- turtle.placeDown()
- T:forward(1)
- T:down(1)
- else
- T:forward(1) --no sapling so simply move forward
- end
- end
- function harvestFirstTree()
- local treeHeight = 0
- local isBranched = false
- -- craft a chest first, then continue to harvest leaves and logs
- saveToLog("harvestFirstTree() Starting...", true)
- turtle.select(1) -- select slot 1
- turtle.dig() -- dig base of tree
- turtle.craft() -- craft log to planks
- turtle.refuel() --
- T:forward(1) -- go under tree
- -- dig 2 more logs to make a chest
- T:up(1)
- treeHeight = treeHeight + 1
- turtle.digUp()
- turtle.craft() -- make logs into planks
- turtle.transferTo(2,1) -- move planks into correct slots
- turtle.transferTo(3,1)
- turtle.transferTo(5,1)
- turtle.transferTo(7,1)
- turtle.transferTo(9,1)
- turtle.transferTo(10,1)
- turtle.transferTo(11,1)
- turtle.craft() --create chest
- treeHeight, isBranched = harvestTrunk(treeHeight, isBranched)
- --repeat to move up 3 more blocks in case branched tree has gaps
- if isBranched then
- treeHeight, isBranched = harvestTrunk(treeHeight, isBranched)
- treeHeight, isBranched = harvestTrunk(treeHeight, isBranched)
- treeHeight, isBranched = harvestTrunk(treeHeight, isBranched)
- end
- -- At top of the tree. New loop to return to ground
- while not turtle.detectDown() do -- While nothing detected below
- T:down(1) -- Go down
- end
- if isBranched then
- harvestTreeBranches(treeHeight)
- end
- saveToLog("harvestFirstTree() Completed. Saving status '1'", true)
- saveStatus("1")
- end
- function harvestRun(runLength, digDirt)
- local itemSlot = 0
- local logType = ""
- local startFile = ""
- local data = {} --initialise empty table variable
- local success = false
- local relX = 0
- local relZ = 0
- local data = {}
- turtle.select(1)
- for i = 1, runLength do
- while not turtle.inspectDown() do -- must be air below
- turtle.suckDown()
- T:down(1)
- end
- data = T:getBlockType("down")
- if data.name == "minecraft:sand" then
- itemSlot = T:getItemSlot("minecraft:sand")
- if itemSlot > 0 then
- if turtle.getItemCount(itemSlot) < 6 then
- harvestSand()
- end
- else
- harvestSand()
- end
- elseif data.name == "minecraft:flowing_water" or data.name == "minecraft:water" then
- if data.modifier == 0 then -- source block
- if water:getX() == 0 then -- water coords not yet found
- saveToLog("Water source found: checking suitability", true)
- relX = math.abs(math.abs(T:getX()) - math.abs(coordHome:getX()))
- relZ = math.abs(math.abs(T:getZ()) - math.abs(coordHome:getZ()))
- saveToLog("Water found: x="..T:getX().."rel X="..relX..", z="..T:getZ()..", rel Z="..relZ, true)
- if relX > 5 and relZ > 5 then
- if checkWaterCoordsOK(T:getX(),T:getZ()) then
- water:setX(T:getX())
- water:setY(T:getY())
- water:setZ(T:getZ())
- water:setFacing(T:getFacing())
- -- create/overwrite 'waterCoords.txt'
- startFile = fs.open("waterCoords.txt", "w")
- startFile.writeLine("x="..T:getX())
- startFile.writeLine("y="..T:getY())
- startFile.writeLine("z="..T:getZ())
- startFile.close()
- treeFarm:setWaterFound(true)
- saveToLog("Water confirmed and saved to file x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
- else
- saveToLog("Water coords not suitable x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
- end
- else
- saveToLog("Water found: too close to base camp (rel X="..relX..", rel Z="..relZ..")", true)
- end
- end
- end
- end
- data = T:getBlockType("up")
- if data.name == "minecraft:log" then
- T:back(1)
- harvestTree()
- end
- if digDirt and i < 5 then
- turtle.select(1)
- if T:getY() == coordHome:getY() then
- turtle.digUp()
- turtle.digDown()
- end
- end
- turtle.suck()
- if turtle.detect() then --can't move forward
- data = T:getBlockType("forward")
- if data.name == "minecraft:log" then
- harvestTree()
- T:back(1)
- elseif data.name == "minecraft:reeds" then
- saveToLog("Reeds found", true)
- while data.name == "minecraft:reeds" do -- continue loop while reeds detected in front
- T:up(1) -- Move up
- data = T:getBlockType("forward")
- end
- -- At top of the Reeds/Sugar Cane.
- T:forward(1)
- -- New loop to return to ground
- data = T:getBlockType("down")
- while data.name == "minecraft:reeds" do -- While sugar cane detected below
- turtle.digDown()
- T:down(1)
- data = T:getBlockType("down")
- end
- T:back(1)
- -- check if leaves or grass in front, dig through
- else --any block except log or reeds
- if isVegetation(data.name) then -- tallgrass,flower, vine etc
- turtle.dig()
- else
- while turtle.detect() do
- T:up(1)
- data = T:getBlockType("forward")
- if isVegetation(data.name) then -- tallgrass,flower, vine etc
- break
- elseif data.name == "minecraft:log" then
- harvestTree()
- T:back(1)
- break
- end
- end
- end
- end
- end
- T:forward(1)
- -- check if wood below eg tree harvest started mid - trunk
- data = T:getBlockType("down")
- while data.name == "minecraft:log" or data.name == "minecraft:log2" do
- T:down(1)
- data = T:getBlockType("down")
- end
- end
- end
- function harvestSand()
- local depth = 0
- local itemSlot = 0
- local data = {}
- saveToLog("harvestSand() started", true)
- data = T:getBlockType("down")
- while data.name == "minecraft:sand" do -- While sand detected below
- turtle.digDown()
- T:down(1)
- depth = depth - 1
- data = T:getBlockType("down")
- end
- while depth < 0 do
- T:up(1)
- depth = depth + 1
- itemSlot = T:getItemSlot("minecraft:dirt")
- if itemSlot > 0 then
- turtle.select(itemSlot)
- turtle.placeDown()
- turtle.select(1)
- end
- end
- saveToLog("harvestSand() completed", true)
- end
- function harvestTree()
- turtle.select(1)
- turtle.dig() -- dig base of tree
- T:forward(1) -- go under tree
- local treeHeight, isBranched = harvestTrunk(0, false)
- --repeat to move up 3 more blocks in case branched tree has gaps
- if isBranched then
- treeHeight, isBranched = harvestTrunk(treeHeight, isBranched)
- treeHeight, isBranched = harvestTrunk(treeHeight, isBranched)
- treeHeight, isBranched = harvestTrunk(treeHeight, isBranched)
- end
- -- At top of the tree. New loop to return to ground
- while not turtle.detectDown() do -- While nothing detected below
- T:down(1) -- Go down
- end
- if isBranched then
- harvestTreeBranches(treeHeight)
- end
- end
- function harvestTreeBranches(treeHeight)
- T:up(treeHeight)
- while not turtle.detectDown() do
- for i = 1, 4 do
- if T:isLog("forward") then -- check if log in front
- harvestWholeTree("forward")
- end
- T:turnRight(1)
- end
- T:down(1)
- end
- end
- function harvestTrunk(treeHeight, isBranched)
- local data = {}
- repeat
- -- Inner loop to check for leaves
- for i = 1, 4 do
- if T:isLog("forward") then -- check if log in front
- isBranched = true
- else
- data = T:getBlockType("forward")
- if data.name == "minecraft:leaves" then
- turtle.dig()
- end
- end
- T:turnRight(1)
- end
- T:up(1) -- Move up
- treeHeight = treeHeight + 1
- until not turtle.detectUp() -- continue loop while block detected above
- return treeHeight, isBranched
- end
- function harvestWholeTree(direction)
- --RECURSIVE FUNCTION - BEWARE!
- local data = {}
- if direction == "up" then
- if turtle.getFuelLevel() < 10 then
- turtle.select(1)
- end
- if T:isLog("up") then
- T:up(1)
- if T:isLog("up") then
- harvestWholeTree("up")
- end
- end
- for i = 1, 4 do
- -- check all round
- if T:isLog("forward") then
- harvestWholeTree("forward")
- else
- data = T:getBlockType("forward")
- if data.name == "minecraft:leaves" then
- T:forward(1)
- harvestWholeTree("forward")
- T:back(1)
- end
- end
- T:turnRight(1)
- end
- T:down(1)
- for i = 1, 4 do
- -- check all round
- if T:isLog("forward") then
- harvestWholeTree("forward")
- else
- data = T:getBlockType("forward")
- if data.name == "minecraft:leaves" then
- T:forward(1)
- harvestWholeTree("forward")
- T:back(1)
- end
- end
- T:turnRight(1)
- end
- elseif direction == "forward" then
- if T:isLog("forward") then
- if turtle.getFuelLevel() < 10 then
- turtle.select(1)
- end
- T:forward(1)
- if turtle.detectUp() then
- turtle.digUp()
- end
- if T:isLog("forward") then
- harvestWholeTree("forward")
- end
- --check left side
- T:turnLeft(1)
- if T:isLog("forward") then
- harvestWholeTree("forward")
- end
- -- check right side
- T:turnRight(2)
- if T:isLog("forward") then
- harvestWholeTree("forward")
- end
- T:turnLeft(1)
- T:back(1)
- end
- end
- end
- function harvestTreeFarm()
- local itemSlot = 0
- saveToLog("harvestTreeFarm() started", true)
- treeFarm:reset()
- T:go("RFFD")
- turtle.select(1)
- while turtle.suckDown() do -- get saplings from chest
- end
- T:go("UUUFFFLFF")
- harvestAndReplant()
- T:forward(1)
- harvestAndReplant()
- T:go("RFFFR")
- harvestAndReplant()
- T:forward(1)
- harvestAndReplant()
- T:go("FFRFFFFFFDDD")
- --store any remaining saplings
- dropItem("minecraft:sapling", 0)
- T:go("UFFR")
- --get rid of any apples
- if T:getItemSlot("minecraft:apple") > 0 then
- turtle.select(T:getItemSlot("minecraft:apple"))
- turtle.drop()
- end
- treeFarm:setTimePlanted(os.time())
- saveStatus("treeFarm")
- saveToLog("harvestTreeFarm() completed", true)
- end
- function initialise()
- -- create classes/objects
- T = classTurtleObject() -- usage: T:forward(5), T:up(2), T:go("RRFFFFFDDx")
- coordHome = classCoordObject("homeLocation")
- mineEntrance = classCoordObject("mineEntrance")
- water = classCoordObject("water")
- fso = classLogfileObject("selfReplicateLog.txt")
- fso:setUseLog(true)
- saveToLog("Log File Created", true)
- treeFarm = classTreeFarmObject()
- loadStatus("treeFarm")
- -- set global variables.
- pastebinCode = "2Krm3Yva"
- end
- function isVegetation(blockName)
- local isVeg = false
- local vegList = {"minecraft:tallgrass", "minecraft:deadbush", "minecraft:cactus", "minecraft:leaves",
- "minecraft:pumpkin", "minecraft:melon_block", "minecraft:vine", "minecraft:mycelium", "minecraft:waterliliy",
- "minecraft:cocoa", "minecraft:double_plant", "minecraft:sponge", "minecraft:wheat"}
- -- check for flower, mushroom
- if string.find(blockName, "flower") ~= nil or string.find(blockName, "mushroom") ~= nil then
- isVeg = true
- end
- if not isVeg then
- for _,v in pairs(vegList) do
- if v == blockName then
- isVeg = true
- break
- end
- end
- end
- return isVeg
- end
- function loadStatus(obj)
- local fileHandle = ""
- local strText = ""
- if obj == "treeFarm" then
- if fs.exists("treeFarm.txt") then
- fileHandle = fs.open("treeFarm.txt", "r")
- strText = fileHandle.readLine()
- if strText == "true" then
- treeFarm:setPondFilled(true)
- end
- strText = fileHandle.readLine()
- if strText == "true" then
- treeFarm:setFarmCreated(true)
- end
- strText = fileHandle.readLine()
- if strText == "true" then
- treeFarm:setHopperPlaced(true)
- end
- strText = fileHandle.readLine()
- if strText == "true" then
- treeFarm:setFarmFlooded(true)
- end
- strText = fileHandle.readLine()
- treeFarm:setTimePlanted(tonumber(strText))
- fileHandle.close()
- end
- else
- -- obj = "currentStatus.txt"
- strText = "0"
- if fs.exists(obj) then
- fileHandle = fs.open(obj, "r")
- strText = fileHandle.readLine()
- fileHandle.close()
- end
- end
- return strText
- end
- function mineCorridor(length, torchesAt, doNotPlug, doReturn)
- local placeBlock = false
- local data = {}
- local stockData = {}
- -- torchesAt[1] = true, torchesAt[2] = false
- -- length = length of corridor, torchesAt = position of torches, doNotPlug = place block above entrance to corridor
- -- doReturn = return same distance as length
- for i = 1, length do
- if turtle.detectUp() then --block above
- if T:isValuable("up") then --could be useful, so dig and replace with cobble
- mineItem(true, "up")
- placeBlock = true
- end
- else --no block above or lava/water, so place cobble
- placeBlock = true
- if doNotPlug and i == 1 then --don't plug mine entrance
- placeBlock = false
- end
- end
- if placeBlock then
- stockData = T:getStock("minecraft:cobblestone")
- if stockData.count > 0 then
- turtle.select(stockData.primarySlot)
- turtle.placeUp()
- else
- stockData = T:getStock("minecraft:dirt")
- if stockData.count > 0 then
- turtle.select(stockData.primarySlot)
- turtle.placeUp()
- end
- end
- placeBlock = false
- end
- turtle.select(1)
- if turtle.detectDown() then
- turtle.digDown()
- else --nothing below could be air, water, lava
- if T:getItemSlot("minecraft:bucket") > 0 then
- data = T:getBlockType("down")
- if data.name == "minecraft:flowing_lava" then
- turtle.select(T:getItemSlot("minecraft:bucket"))
- if turtle.placeDown() then --bucket filled lava
- if turtle.refuel() then --lava
- sleep(2)
- while turtle.placeDown() do
- if not turtle.refuel() then --lava
- break
- end
- sleep(2)
- if turtle.getFuelLevel() > 10000 then
- break
- end
- end
- end
- end
- end
- end
- end
- turtle.select(1)
- if torchesAt[i] then
- if T:getItemSlot("minecraft:torch") > 0 then
- T:go("DxCUxT")
- end
- end
- if i < length then
- if T:isValuable("forward") then
- mineItem(true, "forward")
- end
- T:forward(1)
- end
- end
- if doReturn then
- T:turnRight(2)
- T:forward(length)
- T:turnRight(2)
- end
- end
- function mineItem(do3D, direction)
- local doContinue = true
- local mineIt = false
- local blockType = ""
- --RECURSIVE FUNCTION - BEWARE!
- -- check if block in front is valuable. If so mine it
- -- direction only up/down if already called recursively
- if direction == "up" then
- mineIt, blockType = T:isValuable("up")
- if mineIt then
- turtle.select(1)
- turtle.digUp()
- sleep(0.5)
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.5)
- end
- end
- -- move up into space dug out
- T:up(1)
- -- check if item in front is valuable
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --check right side
- T:turnRight(1)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --check behind
- T:turnRight(1)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --check left side
- T:turnRight(1)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --return to front
- T:turnRight(1)
- T:down(1)
- end
- if direction == "down" then
- mineIt, blockType = T:isValuable("down")
- if mineIt then
- turtle.select(1)
- turtle.digDown()
- end
- -- move down into space dug out
- T:down(1)
- -- check if item in front is valuable
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --check right side
- T:turnRight(1)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --check behind
- T:turnRight(1)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --check left side
- T:turnRight(1)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --return to front
- T:turnRight(1)
- T:up(1)
- end
- if direction == "forward" then
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- T:forward(1)
- mineIt, blockType = T:isValuable("up")
- if mineIt then
- if do3D then
- mineItem(do3D, "up")
- else
- turtle.select(1)
- turtle.digUp()
- end
- end
- mineIt, blockType = T:isValuable("down")
- if mineIt then
- if do3D then
- mineItem(do3D, "down")
- else
- turtle.select(1)
- turtle.digDown()
- end
- end
- -- check if item in front is valuable
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- --check left side
- T:turnLeft(1)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- -- check right side
- T:turnRight(2)
- mineIt, blockType = T:isValuable("forward")
- if mineIt then
- mineItem(do3D, "forward")
- end
- T:turnLeft(1)
- T:back(1)
- end
- end
- end
- function mineToBedrock()
- local aboveBedrock = true
- local stockData = {}
- saveToLog("mineToBedrock() started", true)
- -- start at furnace
- T:sortInventory()
- emptyTurtle(false)
- stockData = T:getStock("minecraft:torch")
- if stockData.count < 24 then
- craftTorches(24)
- end
- T:forward(16)
- turtle.dig()
- if T:getItemSlot("minecraft:sign") > 0 then
- turtle.select(T:getItemSlot("minecraft:sign"))
- turtle.place("Diamond Mine\nMining for\nDiamonds\nTo Bedrock")
- turtle.select(1)
- end
- T:turnRight(2)
- while T:getY() > 5 do
- T:down(1)
- end
- turtle.digDown()
- --create cross shape
- for i = 1, 32 do
- mineToBedrockSub01(i)
- end
- T:turnRight(2)
- T:forward(16)
- T:turnRight(1) --back at centre
- for i = 1, 16 do
- mineToBedrockSub01(i)
- end
- T:turnRight(2)
- T:forward(16)
- for i = 1, 16 do
- mineToBedrockSub01(i)
- end
- T:turnRight(2)
- T:forward(16)
- T:emptyTrash("up")
- T:turnLeft(1)
- T:forward(16)
- T:turnRight(1)
- -- make 33 x 33 square
- --clear top right corner
- for i = 1, 16 do
- mineToBedrockSub01(i)
- end
- T:turnRight(1)
- for i = 1, 32 do
- mineToBedrockSub01(i)
- end
- T:turnRight(1)
- for i = 1, 32 do
- mineToBedrockSub01(i)
- end
- T:turnRight(1)
- for i = 1, 32 do
- mineToBedrockSub01(i)
- end
- T:turnRight(1)
- for i = 1, 16 do
- mineToBedrockSub01(i)
- end
- T:turnRight(1)
- T:forward(16)
- T:emptyTrash("up")
- T:go("LFL")
- -- mine 1 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FRFR")
- if j < 8 then
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FLFL")
- else
- T:forward(16)
- end
- end
- T:turnRight(1)
- T:forward(16)
- T:emptyTrash("up")
- T:go("FR")
- -- mine 2 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FLFL")
- if j < 8 then
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FRFR")
- else
- T:forward(16)
- end
- end
- T:turnLeft(1)
- T:forward(16)
- T:emptyTrash("up")
- T:go("FR")
- -- mine 3 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FLFL")
- if j < 8 then
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FRFR")
- else
- T:forward(16)
- end
- end
- T:turnLeft(1)
- T:forward(16)
- T:emptyTrash("up")
- T:go("FL")
- -- mine 4 of 4 quarters
- for j = 1, 8 do
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FRFR")
- if j < 8 then
- for i = 1, 15 do --14 lines
- mineToBedrockSub02(i)
- end
- T:go("FLFL")
- else
- T:forward(16)
- end
- end
- T:turnRight(1)
- T:forward(16)
- T:emptyTrash("up")
- T:turnLeft(1)
- T:forward(16)
- T:turnRight(2)
- while T:getY() < coordHome:getY() do
- T:up(1)
- end
- T:forward(16)
- T:turnRight(2)
- saveToLog("mineToBedrock() complete. Saving Status '12'", true)
- saveStatus("12")
- end
- function mineToBedrockSub01(i)
- local aboveBedrock = false
- T:forward(1)
- turtle.select(1)
- repeat
- T:isValuable("down")
- aboveBedrock = T:down(1)
- until not aboveBedrock
- while T:getY() < 5 do
- T:up(1)
- if T:getY() <= 4 then
- if T:getItemSlot("minecraft:cobblestone") > 0 then
- turtle.select(T:getItemSlot("minecraft:cobblestone"))
- turtle.placeDown()
- end
- end
- end
- if i == 8 or i == 16 or i == 24 or i == 32 then
- turtle.select(T:getItemSlot("minecraft:torch"))
- turtle.placeDown()
- end
- end
- function mineToBedrockSub02(i)
- local aboveBedrock = true
- T:forward(1)
- if turtle.detectUp() then
- if T:isValuable("up") then
- turtle.select(1)
- turtle.digUp()
- if T:getItemSlot("minecraft:cobblestone") > 0 then
- turtle.select(T:getItemSlot("minecraft:cobblestone"))
- turtle.placeUp()
- end
- end
- else
- if T:getItemSlot("minecraft:cobblestone") > 0 then
- turtle.select(T:getItemSlot("minecraft:cobblestone"))
- turtle.placeUp()
- end
- end
- turtle.select(1)
- repeat
- T:isValuable("down") --clear space for valuable items
- aboveBedrock = T:down(1)
- until not aboveBedrock
- while T:getY() < 5 do
- T:up(1)
- if T:getY() <= 4 then
- if T:getItemSlot("minecraft:cobblestone") > 0 then
- turtle.select(T:getItemSlot("minecraft:cobblestone"))
- turtle.placeDown()
- end
- end
- end
- end
- function placeStorage()
- -- Chest1 sticks
- -- Chest2 logs
- -- chest3 cobblestone
- -- chest4 sand, reeds
- -- chest5 iron ore
- -- chest6 redstone
- -- chest7 diamond
- -- chest8 dirt, misc
- -- chest9 saplings (placed right side of furnace)
- -- make 9 chests need 18 logs
- -- this function runs when all trees harvested locally
- saveToLog("placeStorage() Starting...", true)
- T:craftChests(9, false)
- T:go("RFFDxHURRFFR")
- T:go("xH FuxC")
- for i = 2, 8 do
- T:go("FuxH FuxC")
- end
- T:go("FuxC RFuxCR")
- for i = 1, 14 do
- T:go("FuxC")
- end
- T:go("RFFRuxC")
- for i = 1, 14 do
- T:go("FuxC")
- end
- T:go("RFL")
- T:turnRight(2)
- T:forward(16)
- T:turnRight(2)
- saveToLog("placeStorage() Completed. Saving Status '5'", true)
- saveStatus("5")
- end
- function returnHome(goDirection)
- local startX = T:getX()
- local startZ = T:getZ()
- saveToLog("returnHome() started", true)
- if goDirection == "x" then
- if T:getX() <= coordHome:getX() then
- while T:getFacing() ~= 3 do
- T:turnRight(1)
- end
- while T:getX() < coordHome:getX() do
- safeRun(1)
- end
- elseif T:getX() > coordHome:getX() then -- eg -200 current -211 home
- while T:getFacing() ~= 1 do
- T:turnRight(1)
- end
- while T:getX() > coordHome:getX() do
- safeRun(1)
- end
- end
- elseif goDirection == "y" then
- while T:getY() > coordHome:getY() do
- T:down(1)
- end
- while T:getY() < coordHome:getY() do --
- T:up(1) -- back to ground level at home
- end
- elseif goDirection == "z" then
- if T:getZ() >= coordHome:getZ() then -- go north
- while T:getFacing() ~= 2 do
- T:turnRight(1)
- end
- while T:getZ() > coordHome:getZ() do
- safeRun(1)
- end
- elseif T:getZ() < coordHome:getZ() then
- while T:getFacing() ~= 0 do
- T:turnRight(1)
- end
- while T:getZ() < coordHome:getZ() do
- safeRun(1)
- end
- end
- elseif goDirection == "facing" then
- while T:getFacing() ~= coordHome:getFacing() do
- T:turnRight(1)
- end
- end
- saveToLog("returnHome() completed", true)
- end
- function safeRun(runLength)
- local itemSlot = 0
- local success = false
- local relX = 0
- local relZ = 0
- local data = {}
- turtle.select(1)
- for i = 1, runLength do
- if turtle.detect() then --can't move forward
- data = T:getBlockType("forward")
- if data.name ~= "minecraft:leaves" and data.name ~= "minecraft:tallgrass"
- and data.name ~= "minecraft:double_plant" then
- while turtle.detect() do
- T:up(1)
- data = T:getBlockType("forward")
- if data.name == "minecraft:leaves"
- or data.name == "minecraft:tallgrass"
- or data.name == "minecraft:double_plant" then
- break
- end
- end
- end
- end
- T:forward(1)
- while not turtle.inspectDown() do -- must be air below
- T:down(1)
- end
- data = T:getBlockType("down")
- if data.name == "minecraft:flowing_water" or data.name == "minecraft:water" then
- if data.modifier == 0 then -- source block
- --check not found recently placed water!
- relX = math.abs(math.abs(T:getX()) - math.abs(coordHome:getX()))
- relZ = math.abs(math.abs(T:getZ()) - math.abs(coordHome:getZ()))
- if relX > 5 and relZ > 5 then
- if T:getItemSlot("minecraft:bucket") > 0 then
- if not treeFarm:getWaterFound() then
- water:setX(T:getX())
- water:setY(T:getY())
- water:setZ(T:getZ())
- end
- treeFarm:setWaterFound(true)
- saveToLog("Water bucket filled x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
- turtle.select(T:getItemSlot("minecraft:bucket"))
- turtle.placeDown()
- success = true
- end
- end
- end
- end
- end
- return success
- end
- function saveStatus(obj)
- local fileHandle = ""
- if obj == "treeFarm" then
- fileHandle = fs.open("treeFarm.txt", "w")
- fileHandle.writeLine(tostring(treeFarm:getPondFilled()))
- fileHandle.writeLine(tostring(treeFarm:getFarmCreated()))
- fileHandle.writeLine(tostring(treeFarm:getHopperPlaced()))
- fileHandle.writeLine(tostring(treeFarm:getFarmFlooded()))
- fileHandle.writeLine(tostring(treeFarm:getTimePlanted()))
- else
- -- from e.g. saveStatus("harvestFirstTree")
- fileHandle = fs.open("currentStatus.txt", "w")
- fileHandle.writeLine(obj)
- end
- fileHandle.close()
- end
- function saveToLog(text, toScreen)
- if toScreen == nil then
- toScreen = true
- end
- text = text or "Nil instead of text supplied, check your code!"
- if toScreen then
- print(text)
- end
- if fso:getUseLog() then
- fso:appendLine(text)
- end
- end
- function smelt(oreType, quantity)
- --log->charcoal, iron_ore->iron, cobblestone->stone, sand->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
- saveToLog("smelt("..oreType..", "..quantity..") started", true)
- local smeltType = ""
- local continue = true
- local waitTime = 0
- local planksNeeded = 0 --total needed for smelting
- local woodNeeded = 0
- local woodAvailable = 0
- local success = false
- local stockData = {}
- stockData = T:getStock("minecraft:planks")
- if stockData.count > 0 then
- turtle.select(stockData.primarySlot)
- turtle.refuel()
- end
- woodAvailable = T:getLogStock()
- planksNeeded = quantity
- if oreType == "minecraft:log" then
- woodNeeded = quantity + math.ceil(quantity / 4)
- end
- if woodNeeded > woodAvailable then
- getItemFromStorage("minecraft:log", false)
- woodAvailable = T:getLogStock()
- end
- T:go("BUUF") --now on top
- if oreType == "minecraft:log" or oreType == "minecraft:log2" then --eg quantity = 2, needs 2 wood + 2 planks ASSUME only called if wood in stock
- smeltType = "minecraft:coal"
- elseif oreType == "minecraft:cobblestone"then
- smeltType = "minecraft:stone"
- elseif oreType == "minecraft:iron_ore" then
- smeltType = "minecraft:iron_ingot"
- elseif oreType == "minecraft:sand" then
- smeltType = "minecraft:glass"
- end
- turtle.select(T:getItemSlot(oreType))
- turtle.dropDown(quantity)
- T:go("BD") --in front of furnace, remove any existing planks and refuel
- turtle.select(16)
- if turtle.suck() then
- if T:getItemSlot("minecraft:planks") > 0 then
- turtle.select(T:getItemSlot("minecraft:planks"))
- turtle.refuel()
- end
- end
- planksNeeded = math.ceil(planksNeeded / 4) * 4 --eg 1/4 = 1 ,*4 = 4 planks
- T:turnRight(1) --side on to furnace
- T:craft("minecraft:planks", planksNeeded, "minecraft:log", nil, nil, false)
- T:turnLeft(1)
- turtle.select(T:getItemSlot("minecraft:planks"))
- turtle.drop() --drop planks into furnace
- T:go("DF") --back under furnace
- turtle.select(1)
- 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 T:getItemSlot(smeltType) > 0 then
- success = true
- saveToLog("smelt("..oreType..", "..quantity..") completed", true)
- end
- return success
- end
- function waitForTreeFarm(woodNeeded)
- local woodGrowing = 0
- local maxWood = 0
- if woodNeeded > 0 and treeFarm:getFarmCreated() then
- woodGrowing = treeFarm:getPotentialHarvest()
- treeFarm:getMaxHarvest()
- while woodGrowing < woodNeeded do
- sleep(10)
- -- 10 secs = 12 minecraft minutes
- -- 1 min = 1 hour 12 minutes
- -- 20 mins = 1 minecraft day
- woodGrowing = treeFarm:getPotentialHarvest()
- --will be equal to maxHarvest after 2 days
- saveToLog("waiting for tree farm to grow. potential harvest = "..woodGrowing.." from "..maxWood, true)
- if woodGrowing >= maxWood then
- break
- end
- end
- harvestTreeFarm()
- end
- end
- function classCoordObject(coordName)
- -- 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
- local self = setmetatable({}, clsCoord)
- self.name = coordName
- self.x = 0
- self.y = 0
- self.z = 0
- self.facing = 0
- self.compass = "south"
- function clsCoord.getX(self)
- return self.x
- end
- function clsCoord.setX(self, newVal) -- setter
- self.x = newVal
- end
- function clsCoord.getY(self) -- getter
- 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) -- setter
- 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) -- setter
- 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
- return self
- end
- function classLogfileObject(fileName)
- 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
- local self = setmetatable({}, clsLogfile)
- local iniText = ""
- local iniFile = ""
- self.size = 0
- self.useLog = false
- self.logFile = fileName
- self.logFileExists = false
- function clsLogfile.getUseLog(self)
- return self.useLog
- end
- function clsLogfile.setUseLog(self, use)
- self.useLog = use
- end
- function clsLogfile.checkLogExists(self)
- return self.logFileExists
- end
- function clsLogfile.getLogFileName(self)
- if self.logFileExists then
- return self.logFile
- end
- end
- function clsLogfile.getCurrentFileSize(self)
- if self.logFileExists then
- return fs.getSize(self.logFile)
- else
- return 0
- end
- end
- function clsLogfile.deleteLog(self)
- if fs.exists(self.logFile) then
- fs.delete(self.logFile)
- end
- self.logFileExists = false
- return true
- end
- function clsLogfile.appendLine(self, newText)
- local handle = ""
- if fs.exists(self.logFile) then --logFile already created
- handle = fs.open(self.logFile, "a")
- else
- handle = fs.open(self.logFile, "w") --create file
- end
- self.logFileExists = true
- handle.writeLine(newText)
- handle.close()
- end
- return self
- end
- function classTreeFarmObject()
- clsTreeFarm = {} -- the table representing the class, which will double as the metatable for any instances
- clsTreeFarm.__index = clsTreeFarm -- failed table lookups on the instances should fallback to the class table, to get methods
- local self = setmetatable({}, clsTreeFarm)
- self.numSaplings = 0
- self.timePlanted = 0
- self.dayPlanted = 0
- self.timeHarvested = 0
- self.woodHarvested = 0
- self.pondFilled = false
- self.hopperPlaced = false
- self.farmCreated = false
- self.farmFlooded = false
- self.waterFound = false
- function clsTreeFarm.reset(self)
- self.numSaplings = 0
- self.timePlanted = 0
- self.dayPlanted = 0
- self.timeHarvested = 0
- self.woodHarvested = 0
- end
- function clsTreeFarm.getPondFilled(self)
- return self.pondFilled
- end
- function clsTreeFarm.setPondFilled(self, yesNo)
- self.pondFilled = yesNo
- end
- function clsTreeFarm.getWaterFound(self)
- return self.waterFound
- end
- function clsTreeFarm.setWaterFound(self, yesNo)
- self.waterFound = yesNo
- end
- function clsTreeFarm.getHopperPlaced(self)
- return self.hopperPlaced
- end
- function clsTreeFarm.setHopperPlaced(self, yesNo)
- self.hopperPlaced = yesNo
- end
- function clsTreeFarm.getFarmCreated(self)
- return self.farmCreated
- end
- function clsTreeFarm.setFarmCreated(self, yesNo)
- self.farmCreated = yesNo
- end
- function clsTreeFarm.getFarmFlooded(self)
- return self.farmFlooded
- end
- function clsTreeFarm.setFarmFlooded(self, yesNo)
- self.farmFlooded = yesNo
- end
- function clsTreeFarm.getNumSaplings(self)
- return self.numSaplings
- end
- function clsTreeFarm.setNumSaplings(self, num)
- self.numSaplings = num
- end
- function clsTreeFarm.addSapling(self)
- self.numSaplings = self.numSaplings + 1
- end
- function clsTreeFarm.getTimePlanted(self)
- return self.timePlanted
- end
- function clsTreeFarm.setTimePlanted(self, num)
- self.timePlanted = num
- self.dayPlanted = os.day()
- end
- function clsTreeFarm.getTimeHarvested(self)
- return self.timeHarvested
- end
- function clsTreeFarm.setTimeHarvested(self, num)
- self.timeHarvested = num
- end
- function clsTreeFarm.getWoodHarvested(self)
- return self.woodHarvested
- end
- function clsTreeFarm.setWoodHarvested(self, num)
- self.woodHarvested = num
- end
- function clsTreeFarm.getMaxHarvest(self)
- return self.numSaplings * 5
- end
- function clsTreeFarm.setDebug(self, saplings, trees)
- --use if debugging and treeFarm already planted
- self.numSaplings = saplings + trees
- self.timePlanted = os.time()
- if trees >= 6 then
- self.dayPlanted = os.day() - 3
- end
- end
- function clsTreeFarm.getPotentialHarvest(self)
- local currentDay = os.day()
- local currentTime = os.time()
- local numDays = 0
- local numHours = 0
- local harvest = 0
- --days measured from world creation
- numDays = currentDay - self.dayPlanted
- --time 0 - 24 then resets
- if currentTime > self.timePlanted then -- eg 13.5 > 7.5 (13.5 - 7.5 = 6 hours)
- numHours = currentTime - self.timePlanted
- else -- eg 6.465 < 21.454
- numHours = 24 - self.timePlanted + currentTime -- eg 24 - 21.500 + 6.500 = 9 hours
- end
- -- 8 trees planted, half done in 12-14 minecraft hours
- -- 1 tree 4.5 hrs
- -- 2 trees 7 hrs
- -- 3 trees 8 hrs
- -- 4 trees 13 hrs
- -- 5 trees 20 hrs
- -- 6 trees 30 hours
- -- estimate as half no of saplings per 12 hours
- if numDays == 0 then
- harvest = math.ceil((5 * self.numSaplings) / 2)
- elseif numDays == 1 then
- harvest = math.ceil(((5 * self.numSaplings) * 2) / 3)
- else -- > 2 days probably most trees * 5 wood
- harvest = 5 * self.numSaplings
- end
- return harvest
- end
- return self
- end
- function classTurtleObject()
- clsTurtle = {} -- the table representing the class, which will double as the metatable for any instances
- clsTurtle.__index = clsTurtle -- failed table lookups on the instances should fallback to the class table, to get methods
- local self = setmetatable({}, clsTurtle)
- self.x = 0
- self.y = 0
- self.z = 0
- self.facing = 0
- self.compass = "south"
- self.numDiamondsStored = 0
- function clsTurtle.getDiamondsStored(self)
- local stockData = {}
- stockData = clsTurtle.getStock(self, "minecraft:diamond", 0)
- return self.numDiamondsStored + stockData.count
- end
- function clsTurtle.setDiamondsStored(self, newVal) -- setter
- self.numDiamondsStored = newVal
- end
- function clsTurtle.getX(self)
- return self.x
- end
- function clsTurtle.setX(self, newVal) -- setter
- self.x = newVal
- end
- function clsTurtle.getY(self) -- getter
- return self.y
- end
- function clsTurtle.setY(self, newVal)
- self.y = newVal
- end
- function clsTurtle.getZ(self)
- return self.z
- end
- function clsTurtle.setZ(self, newVal)
- self.z = newVal
- end
- function clsTurtle.getFacing(self)
- return self.facing
- end
- function clsTurtle.setFacing(self, newVal) --property let in VB
- local direction = {"south","west","north","east"}
- self.facing = newVal
- if self.facing < 0 then
- self.facing = 3
- elseif self.facing > 3 then
- self.facing = 0
- end
- self.compass = direction[self.facing + 1]
- end
- function clsTurtle.getCompass(self)
- return self.compass
- end
- function clsTurtle.attack(self)
- -- minecart with chest is not detected, but responds to attack without breaking!
- local slot = turtle.getSelectedSlot()
- local numAttacks = 0
- local attackOK = false
- turtle.select(1)
- while turtle.attack() do --in case mob in front
- sleep(1.5)
- numAttacks = numAttacks + 1
- if numAttacks > 10 then -- may be a minecart due to bug
- numAttacks = 0
- turtle.dig()
- end
- attackOK = true
- end
- while turtle.attackUp() do --in case mob above
- sleep(1.5)
- numAttacks = numAttacks + 1
- if numAttacks > 10 then -- may be a minecart due to bug
- numAttacks = 0
- turtle.digUp()
- end
- attackOK = true
- end
- while turtle.attackDown() do --in case mob below
- sleep(1.5)
- numAttacks = numAttacks + 1
- if numAttacks > 10 then -- may be a minecart due to bug
- numAttacks = 0
- turtle.digDown()
- end
- attackOK = true
- end
- turtle.select(slot)
- return attackOK
- end
- function clsTurtle.back(self, steps)
- steps = steps or 1
- local slot = turtle.getSelectedSlot()
- local success = true
- clsTurtle.refuel(self, steps)
- turtle.select(1)
- for i = 1, steps do
- if not turtle.back() then --cant move back
- clsTurtle.turnRight(self, 2) --face backward direction
- while turtle.dig() do
- sleep(0.5)
- end
- if not clsTurtle.forward(self, 1) then -- will also attack mobs if in the way
- success = false
- break
- end
- clsTurtle.turnRight(self, 2)
- end
- if success then
- clsTurtle.changeCoords(self, "back")
- end
- end
- turtle.select(slot)
- return success
- end
- function clsTurtle.changeCoords(self, 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 self.facing == 0 then
- self.z = self.z + 1
- elseif self.facing == 1 then
- self.x = self.x - 1
- elseif self.facing == 2 then
- self.z = self.z - 1
- else
- self.x = self.x + 1
- end
- elseif direction == "back" then
- if self.facing == 0 then
- self.z = self.z - 1
- elseif self.facing == 1 then
- self.x = self.x + 1
- elseif self.facing == 2 then
- self.z = self.z + 1
- else
- self.x = self.x - 1
- end
- end
- end
- function clsTurtle.changeDirection(self)
- while self.facing ~= coordHome:getFacing() do
- clsTurtle.turnRight(self, 1)
- end
- end
- function clsTurtle.clear(self)
- term.clear()
- term.setCursorPos(1,1)
- end
- function clsTurtle.craft(self, craftItem, craftQuantity, sourceItem1, sourceItem2, sourceItem3, refuel)
- --Examples:
- --make planks: T:craft("minecraft:planks", 8, "minecraft:log", nil, nil, false)
- --make planks: clsTurtle.craft(self, "minecraft:planks", 8, "minecraft:log", nil, nil, false)
- --make planks: T:craft{craftItem = "minecraft:planks", craftQuantity = 8, sourceItem1 = "minecraft:log"}
- --make 1 chest: T:craft{craftItem = "minecraft:chest", craftQuantity = 1, sourceItem1 = "minecraft:planks"}
- --late stage: T:craft{craftItem = "ccComputer", craftQuantity = 1, sourceItem1 = "minecraft:glass_pane", sourceItem2 = "minecraft:redstone", sourceItem3 = "minecraft:stone"}
- local itemSlot = 0
- local sourceSlot1 = 0
- local sourceSlot2 = 0
- local sourceSlot3 = 0
- local sourceSlot4 = 0
- local success = false
- local emptySlot = 0
- local turns = 0
- local doContinue = true
- local data = {}
- local stockData = {}
- --clsTurtle.saveToLog(self, "craft args "..arg.craftItem..", "..arg.craftQuantity..", "..arg.sourceItem1, true)
- -- check if at least 1 spare slot
- if clsTurtle.getFirstEmptySlot(self) == 0 then
- clsTurtle.emptyTrash(self, "down")
- if clsTurtle.getFirstEmptySlot(self) == 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.dropUp()
- end
- end
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:chest") --get the slot number containing a chest
- if itemSlot == 0 then -- chest not found
- doContinue = false
- end
- if doContinue then
- if sourceItem1 == "minecraft:log" or sourceItem1 == "minecraft:log2" then
- stockData = clsTurtle.getLogData(self)
- saveToLog("craft(): sd.pSlot="..stockData.primarySlot..", sd.pCount="..stockData.primaryCount..", sd.fSlot="..stockData.finalSlot..", sd.fCount="..stockData.finalCount)
- if refuel then
- sourceSlot1 = stockData.finalSlot
- saveToLog("craft(): slot no: "..stockData.finalSlot..", log count:"..stockData.finalCount)
- else
- if craftItem == "minecraft:planks" then
- if stockData.finalCount >= craftQuantity / 4 then
- sourceSlot1 = stockData.finalSlot
- saveToLog("craft(): slot no: "..stockData.finalSlot..", log count:"..stockData.finalCount)
- else
- sourceSlot1 = stockData.primarySlot
- saveToLog("craft(): slot no: "..stockData.primarySlot..", log count:"..stockData.primaryCount)
- end
- end
- end
- else
- sourceSlot1 = clsTurtle.getItemSlot(self, sourceItem1)
- end
- if sourceSlot1 == 0 then
- doContinue = false
- end
- if sourceItem2 == nil then -- no second recipe item needed
- sourceItem2 = ""
- sourceSlot2 = 0
- else
- sourceSlot2 = clsTurtle.getItemSlot(self, sourceItem2) -- find slot number of second recipe item
- if sourceSlot2 == 0 then
- doContinue = false
- end
- end
- if sourceItem3 == nil then -- no third recipe item needed
- sourceItem3 = ""
- sourceSlot3 = 0
- else
- sourceSlot3 = clsTurtle.getItemSlot(self, sourceItem3) -- find slot number of third recipe item
- if sourceSlot3 == 0 then
- doContinue = false
- end
- end
- end
- if doContinue then
- while turtle.detect() do --check for clear space to place chest
- clsTurtle.turnRight(self, 1)
- turns = turns + 1
- if turns == 4 then
- turns = 0
- break
- end
- end
- turtle.select(1)
- while turtle.detect() do --clear space in front. Use loop in case of sand/gravel falling
- turtle.dig()
- sleep(.5)
- end
- turtle.select(itemSlot) --should be slot with chest
- while not turtle.place() do
- while clsTurtle.attack(self) do --in case mobs in front
- end
- end
- --check if chest placed
- data = clsTurtle.getBlockType(self, "forward")
- if data.name ~= "minecraft:chest" then
- turtle.select(1)
- turtle.dig()
- turtle.select(itemSlot)
- while not turtle.place() do
- while clsTurtle.attack(self) do --in case mobs in front
- end
- end
- end
- -- fill chest with everything except required items
- for i = 1, 16 do
- if i ~= sourceSlot1 and i ~= sourceSlot2 and i ~= sourceSlot3 then
- turtle.select(i)
- turtle.drop()
- end
- end
- --turtle emptied out except for crafting items, so move them to slots 16,15,14,13. Start by moving them all to empty slots
- emptySlot = clsTurtle.getFirstEmptySlot(self)
- turtle.select(sourceSlot1)
- turtle.transferTo(emptySlot) --move selected first item to empty slot
- sourceSlot1 = emptySlot
- if sourceSlot2 > 0 then
- emptySlot = clsTurtle.getFirstEmptySlot(self)
- turtle.select(sourceSlot2)
- turtle.transferTo(emptySlot) --move selected first item to empty slot
- sourceSlot2 = emptySlot
- end
- if sourceSlot3 > 0 then
- emptySlot = clsTurtle.getFirstEmptySlot(self)
- turtle.select(sourceSlot3)
- turtle.transferTo(emptySlot) --move selected first item to empty slot
- sourceSlot3 = emptySlot
- end
- if sourceSlot4 > 0 then
- emptySlot = clsTurtle.getFirstEmptySlot(self)
- turtle.select(sourceSlot4)
- turtle.transferTo(emptySlot) --move selected first item to empty slot
- sourceSlot4 = emptySlot
- end
- --now move to 16, (15,14,13)
- turtle.select(sourceSlot1)
- turtle.transferTo(16) --move selected first item to empty slot
- sourceSlot1 = 16
- if sourceSlot2 > 0 then
- turtle.select(sourceSlot2)
- turtle.transferTo(15) --move selected first item to empty slot
- sourceSlot1 = 15
- end
- if sourceSlot3 > 0 then
- turtle.select(sourceSlot3)
- turtle.transferTo(14) --move selected first item to empty slot
- sourceSlot1 = 14
- end
- if sourceSlot4 > 0 then
- turtle.select(sourceSlot4)
- turtle.transferTo(13) --move selected first item to empty slot
- sourceSlot1 = 13
- end
- --ready to craft
- turtle.select(16)
- if craftItem == ccComputer then --T:craft{craftItem = ccComputer, craftQuantity = 1, sourceItem1 = "minecraft:glass_panes", sourceItem2 = "minecraft:redstone",sourceItem3 = "minecraft:stone"}
- -- 1 glass_panes, 1 redstone, 7 stone
- turtle.select(16)
- turtle.transferTo(10, craftQuantity) --move glass panes to 10
- turtle.select(15)
- turtle.transferTo(6, craftQuantity) --move redstone to 6
- turtle.select(14) --stone
- turtle.transferTo(1, craftQuantity) --move stone to 6
- turtle.transferTo(2, craftQuantity)
- turtle.transferTo(3, craftQuantity)
- turtle.transferTo(5, craftQuantity)
- turtle.transferTo(7, craftQuantity)
- turtle.transferTo(9, craftQuantity)
- turtle.transferTo(11, craftQuantity)
- elseif craftItem == ccDisk then --T:craft{craftItem = ccDisk, craftQuantity = 1, sourceItem1 = "minecraft:paper", sourceItem2 = "minecraft:redstone"}
- -- 1 paper, 1 redstone
- turtle.select(16)
- turtle.transferTo(5, craftQuantity) --move paper to 5
- turtle.select(15)
- turtle.transferTo(1, craftQuantity) --move redstone to 1
- elseif craftItem == ccDiskDrive then --T:craft{craftItem = ccDiskDrive, craftQuantity = 1, sourceItem1 = "minecraft:redstone", sourceItem2 = "minecraft:stone"}
- -- 2 redstone, 7 stone
- turtle.select(16)
- turtle.transferTo(6, craftQuantity) --move to 6
- turtle.transferTo(10, craftQuantity) --move to 10
- turtle.select(15)
- turtle.transferTo(1, craftQuantity) --move stone to 6
- turtle.transferTo(2, craftQuantity)
- turtle.transferTo(3, craftQuantity)
- turtle.transferTo(5, craftQuantity)
- turtle.transferTo(7, craftQuantity)
- turtle.transferTo(9, craftQuantity)
- turtle.transferTo(11, craftQuantity)
- elseif craftItem == ccTurtle then --T:craft{craftItem = ccTurtle, craftQuantity = 1, sourceItem1 = "minecraft:chest", sourceItem2 = ccComputer,sourceItem3 = "minecraft:iron_ingot"}
- -- 1 chest, 1 computer, 7 iron
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) --computer
- turtle.transferTo(15, craftQuantity)
- end
- if sourceSlot3 ~= 14 then
- turtle.select(sourceSlot3) --iron
- turtle.transferTo(14)
- end
- turtle.select(16)
- turtle.transferTo(10, craftQuantity)
- turtle.select(15) --computer
- turtle.transferTo(6, craftQuantity)
- turtle.select(14) --iron
- turtle.transferTo(1, craftQuantity) --move iron
- turtle.transferTo(2, craftQuantity)
- turtle.transferTo(3, craftQuantity)
- turtle.transferTo(5, craftQuantity)
- turtle.transferTo(7, craftQuantity)
- turtle.transferTo(9, craftQuantity)
- turtle.transferTo(11, craftQuantity)
- elseif craftItem == ccCraftyMiningTurtle then --T:craft{craftItem = ccCraftyMiningTurtle, craftQuantity = 1, sourceItem1 = "minecraft:crafting_table", sourceItem2 = "minecraft:diamond_pickaxe",sourceItem3 = ccTurtle}
- -- 1 crafting table, 1 diamond pickaxe, 1 turtle
- turtle.select(16)
- turtle.transferTo(1, craftQuantity) --move crafting table to 1
- turtle.select(15)
- turtle.transferTo(3, craftQuantity) --move pickaxe to 3
- turtle.select(14)
- turtle.transferTo(2, craftQuantity) --move turtle to 2
- elseif craftItem == "minecraft:bucket" then --T:craft{craftItem = "minecraft:bucket", craftQuantity = 1, sourceItem1 = "minecraft:iron_ingot"}
- turtle.transferTo(1, craftQuantity)
- turtle.transferTo(3, craftQuantity)
- turtle.transferTo(6, craftQuantity)
- elseif craftItem == "minecraft:chest" then --T:craft{craftItem = "minecraft:chest", craftQuantity = 1, sourceItem1 = "minecraft:planks"}
- --8 planks = 1 chest
- turtle.transferTo(1, craftQuantity)
- turtle.transferTo(2, craftQuantity)
- turtle.transferTo(3, craftQuantity)
- turtle.transferTo(5, craftQuantity)
- turtle.transferTo(7, craftQuantity)
- turtle.transferTo(9, craftQuantity)
- turtle.transferTo(10, craftQuantity)
- turtle.transferTo(11, craftQuantity)
- elseif craftItem == "minecraft:crafting_table" then --T:craft{craftItem = "minecraft:crafting_table", craftQuantity = 1, sourceItem1 = "minecraft:planks"}
- -- 4 planks = 1 table
- turtle.transferTo(1, craftQuantity)
- turtle.transferTo(2, craftQuantity)
- turtle.transferTo(5, craftQuantity)
- turtle.transferTo(6, craftQuantity)
- elseif craftItem == "minecraft:diamond_pickaxe" then --T:craft{craftItem = "minecraft:diamond_pickaxe", craftQuantity = 1, sourceItem1 = "minecraft:stick", sourceItem2 = "minecraft:diamond"}
- --2 sticks + 3 diamonds = 1 axe
- turtle.select(16)
- turtle.transferTo(6, craftQuantity) --move stick to 6
- turtle.transferTo(10, craftQuantity) --move stick to 10
- turtle.select(15)
- turtle.transferTo(1, craftQuantity) --move diamond to 1
- turtle.transferTo(2, craftQuantity) --move diamond to 2
- turtle.transferTo(3, craftQuantity) --move diamond to 3
- elseif craftItem == "minecraft:fence" then --T:craft{craftItem = "minecraft:fence", craftQuantity = 2, sourceItem1 = "minecraft:stick"}
- -- 6 stick = 2 fences
- turtle.transferTo(5, craftQuantity / 2)
- turtle.transferTo(6, craftQuantity / 2)
- turtle.transferTo(7, craftQuantity / 2)
- turtle.transferTo(9, craftQuantity / 2)
- turtle.transferTo(10, craftQuantity / 2)
- turtle.transferTo(11, craftQuantity / 2)
- elseif craftItem == "minecraft:furnace" then --T:craft{craftItem = "minecraft:furnace", craftQuantity = 1, sourceItem1 = "minecraft:cobblestone"}
- -- 8 cobblestone = 1 furnace
- turtle.transferTo(1, craftQuantity)
- turtle.transferTo(2, craftQuantity)
- turtle.transferTo(3, craftQuantity)
- turtle.transferTo(5, craftQuantity)
- turtle.transferTo(7, craftQuantity)
- turtle.transferTo(9, craftQuantity)
- turtle.transferTo(10, craftQuantity)
- turtle.transferTo(11, craftQuantity)
- elseif craftItem == "minecraft:glass_pane" then --T:craft{craftItem = "minecraft:glass_pane", craftQuantity = 16, sourceItem1 = "minecraft:glass"}
- -- 6 glass = 16 panes
- turtle.transferTo(5, craftQuantity / 16)
- turtle.transferTo(6, craftQuantity / 16)
- turtle.transferTo(7, craftQuantity / 16)
- turtle.transferTo(9, craftQuantity / 16)
- turtle.transferTo(10, craftQuantity / 16)
- turtle.transferTo(11, craftQuantity / 16)
- elseif craftItem == "minecraft:hopper" then --T:craft{craftItem = "minecraft:hopper", craftQuantity = 1, sourceItem1 = "minecraft:iron_ingot", sourceItem2 = "minecraft:chest"}
- -- 5 iron, 1 chest
- turtle.select(16)
- turtle.transferTo(1, craftQuantity) --move iron to 1
- turtle.transferTo(3, craftQuantity)
- turtle.transferTo(5, craftQuantity)
- turtle.transferTo(7, craftQuantity)
- turtle.transferTo(10, craftQuantity)
- turtle.select(15)
- turtle.transferTo(6, craftQuantity) --move chest to 6
- elseif craftItem == "minecraft:ladder" then --T:craft{craftItem = "minecraft:ladder", craftQuantity = 3, sourceItem1 = "minecraft:stick"}
- -- 7 stick = 3 ladder
- turtle.transferTo(1, craftQuantity / 3)
- turtle.transferTo(3, craftQuantity / 3)
- turtle.transferTo(5, craftQuantity / 3)
- turtle.transferTo(6, craftQuantity / 3)
- turtle.transferTo(7, craftQuantity / 3)
- turtle.transferTo(9, craftQuantity / 3)
- turtle.transferTo(11, craftQuantity / 3)
- elseif craftItem == "minecraft:paper" then --T:craft{craftItem = "minecraft:paper", craftQuantity = 3, sourceItem1 = "minecraft:reeds"}
- --3 reeds = 3 paper
- turtle.transferTo(1, craftQuantity / 3)
- turtle.transferTo(2, craftQuantity / 3)
- turtle.transferTo(3, craftQuantity / 3)
- elseif craftItem == "minecraft:planks" then --T:craft{craftItem = "minecraft:planks", craftQuantity = 4, sourceItem1 = "minecraft:log"}
- turtle.transferTo(1, craftQuantity / 4)
- elseif craftItem == "minecraft:sign" then --T:craft{craftItem = "minecraft:sign", craftQuantity = 3, sourceItem1 = "minecraft:stick", sourceItem2 = "minecraft:planks"}
- -- 1 stick + 6 planks = 3 sign
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) -- move planks to 15
- turtle.transferTo(15)
- end
- turtle.select(16)
- turtle.transferTo(10, craftQuantity / 3) --move sticks to 5
- turtle.select(15)
- turtle.transferTo(1, craftQuantity / 3) --move planks to 1
- turtle.transferTo(2, craftQuantity / 3) --move planks to 2
- turtle.transferTo(3, craftQuantity / 3) --move planks to 3
- turtle.transferTo(5, craftQuantity / 3) --move planks to 5
- turtle.transferTo(6, craftQuantity / 3) --move planks to 6
- turtle.transferTo(7, craftQuantity / 3) --move planks to 7
- elseif craftItem == "minecraft:stair" then --T:craft{craftItem = "minecraft:stair", craftQuantity = 4, sourceItem1 = "minecraft:cobblestone"}
- --6 cobblestone = 4 stair
- turtle.transferTo(1, craftQuantity / 4)
- turtle.transferTo(5, craftQuantity / 4)
- turtle.transferTo(6, craftQuantity / 4)
- turtle.transferTo(9, craftQuantity / 4)
- turtle.transferTo(10, craftQuantity / 4)
- turtle.transferTo(11, craftQuantity / 4)
- elseif craftItem == "minecraft:stick" then --T:craft{craftItem = "stick", craftQuantity = 4, sourceItem1 = "minecraft:planks"}
- -- 2 planks gives 4 sticks
- turtle.transferTo(1, craftQuantity / 4)
- turtle.transferTo(5, craftQuantity / 4)
- elseif craftItem == "minecraft:torch" then --T:craft{craftItem = "minecraft:torch", craftQuantity = 4, sourceItem1 = "minecraft:stick", sourceItem2 = "minecraft:coal"}
- -- 1 stick + 1 coal/charcoal = 4 torch
- if sourceSlot2 ~= 15 then
- turtle.select(sourceSlot2) -- move coal/charcoal to 15
- turtle.transferTo(15)
- end
- turtle.select(16)
- turtle.transferTo(5, craftQuantity / 4) --move sticks to 5
- turtle.select(15)
- turtle.transferTo(1, craftQuantity / 4) --move coal/charcoal to
- end
- for i = 13, 16 do -- empty remaining recipe items into chest
- turtle.select(i)
- turtle.drop()
- end
- -- Attempt to craft item into slot 16
- turtle.select(16)
- if turtle.craft() then
- success = true
- --now put crafted item in chest first, so will mix with any existing similar items
- if refuel then
- turtle.select(16)
- turtle.refuel()
- else
- turtle.select(16)
- turtle.drop()
- end
- else --crafting not successful, so empty out all items into chest
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- turtle.select(1) --empty chest into slot 1 onwards
- while turtle.suck() do
- end
- turtle.dig() -- collect chest
- if turns > 0 then --return to original position
- clsTurtle.turnLeft(self, turns)
- end
- else
- saveToLog("Missing crafting ingredients.", true)
- success = false
- end
- return success
- end
- function clsTurtle.craftChests(self, quantity, orLess)
- local chestsCrafted = 0
- local logNum = 0
- local stockData = {}
- stockData = clsTurtle.getStock(self, "minecraft:log")
- if stockData.count >= 2 then -- at least one chest can be made
- if turtle.getItemCount(stockData.primarySlot) >= quantity * 2 then
- if quantity > 8 then
- clsTurtle.craft(self, "minecraft:planks", 64, clsTurtle.getItemName(self, stockData.primarySlot), nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", 8, "minecraft:planks", nil, nil, false)
- quantity = quantity - 8
- end
- stockData = clsTurtle.getStock(self, "minecraft:log")
- clsTurtle.craft(self, "minecraft:planks", quantity * 8, clsTurtle.getItemName(self, stockData.primarySlot), nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", quantity, "minecraft:planks", nil, nil, false)
- chestsCrafted = quantity
- else -- less than required logs
- if orLess then
- logNum = turtle.getItemCount(stockData.primarySlot)
- logNum = (math.floor(logNum / 2)) * 2
- logNeeded = quantity * 2 - logNum
- clsTurtle.craft(self, "minecraft:planks", logNum * 4, clsTurtle.getItemName(self, stockData.primarySlot), nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", logNum / 2, "minecraft:planks", nil, nil, false)
- stockData = clsTurtle.getStock(self, "minecraft:log")
- logNum = turtle.getItemCount(stockData.primarySlot)
- logNum = (math.floor(logNum / 2)) * 2
- if logNum >= logNeeded then
- clsTurtle.craft(self, "minecraft:planks", logNeeded * 4, clsTurtle.getItemName(self, stockData.primarySlot), nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", logNeeded / 2, "minecraft:planks", nil, nil, false)
- else
- clsTurtle.craft(self, "minecraft:planks", logNum * 4, clsTurtle.getItemName(self, stockData.primarySlot), nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", logNum / 2, "minecraft:planks", nil, nil, false)
- end
- chestsCrafted = logNeeded / 2
- end
- end
- end
- return chestsCrafted
- end
- function clsTurtle.down(self, steps)
- steps = steps or 1
- --local slot = turtle.getSelectedSlot()
- local success = true
- local data = {}
- clsTurtle.refuel(self, steps)
- turtle.select(1)
- for i = 1, steps do
- if turtle.detectDown() then
- data = clsTurtle.getBlockType(self, "down")
- if data.name == "minecraft:bedrock" then
- success = false
- break
- else
- turtle.digDown()
- end
- end --move down unless mob in the way
- while not turtle.down() do
- clsTurtle.attack(self)
- end
- self.y = self.y - 1
- end
- --turtle.select(slot)
- return success
- end
- function clsTurtle.emptyTrashItem(self, direction, item, maxAmount)
- local stockData = {}
- stockData = clsTurtle.getStock(self, item)
- while stockData.count > maxAmount and stockData.primarySlot > 0 do
- turtle.select(stockData.primarySlot)
- if direction == "up" then
- if not turtle.dropUp() then
- turtle.dropDown()
- end
- else
- if not turtle.dropDown() then
- turtle.dropUp()
- end
- end
- stockData = clsTurtle.getStock(self, item)
- end
- end
- function clsTurtle.emptyTrash(self, direction)
- direction = direction or "down"
- local slotData = {}
- local item = ""
- local move = false
- --local slot = turtle.getSelectedSlot()
- local keepItems = {"minecraft:cobblestone", "minecraft:dirt", "minecraft:redstone", "minecraft:sand",
- "minecraft:chest", "minecraft:log", "minecraft:log2", "minecraft:iron_ore", "minecraft:reeds", "minecraft:sapling",
- "minecraft:bucket", "minecraft:lava_bucket", "minecraft:water_bucket", "minecraft:torch", "minecraft:diamond",
- "minecraft:coal", "minecraft:iron_ingot", "minecraft:emerald"}
- local keepit = false
- -- empty excess cobble, dirt, all gravel, unknown minerals
- --keep max of 1 stack
- clsTurtle.sortInventory(self)
- if direction == "down" then
- saveToLog("EmptyTrash():moving down ", true) --debug here
- if clsTurtle.down(self, 1) then
- saveToLog("EmptyTrash():down success ", true) --debug here
- move = true
- end
- end
- for i = 1, 16 do
- keepit = false
- if turtle.getItemCount(i) > 0 then
- item = clsTurtle.getItemName(self, i)
- for _,v in pairs(keepItems) do
- if v == item then
- keepit = true
- break
- end
- end
- if not keepit then
- saveToLog("EmptyTrash():Dropping "..item, true)
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- clsTurtle.sortInventory(self)
- clsTurtle.emptyTrashItem(self, "down", "minecraft:cobblestone", 128)
- clsTurtle.emptyTrashItem(self, "down", "minecraft:dirt", 64)
- clsTurtle.emptyTrashItem(self, "down", "minecraft:redstone", 64)
- slotData = clsTurtle.getStock(self, "minecraft:coal", 0)
- if slotData.count > 64 then
- if slotData.primarySlot ~= slotData.finalSlot and slotData.finalSlot ~= 0 then
- turtle.select(slotData.finalSlot)
- turtle.refuel()
- end
- end
- if direction == "down" and move then
- clsTurtle.up(self, 1)
- end
- turtle.select(1)
- end
- function clsTurtle.equip(self, side, useItem, useDamage)
- useDamage = useDamage or 0
- local slot, damage = clsTurtle.getItemSlot(self, useItem)
- local success = false
- --minecraft:crafting_table
- --minecraft:diamond_pickaxe
- --minecraft:diamond_sword
- --minecraft:diamond_shovel
- --minecraft:diamond_hoe
- --minecraft:diamond_axe
- --wireless modem = ComputerCraft:CC-Peripheral, damage = 1
- if slot > 0 and damage == useDamage then
- turtle.select(slot)
- if side == "right" then
- if turtle.equipRight() then
- success = true
- end
- else
- if turtle.equipLeft() then
- success = true
- end
- end
- end
- return success
- end
- function clsTurtle.forward(self, steps)
- steps = steps or 1
- --local slot = turtle.getSelectedSlot()
- local success = true
- local data = {}
- clsTurtle.refuel(self, steps)
- turtle.select(1)
- for i = 1, steps do
- while turtle.detect() do
- if turtle.dig() then
- sleep(0.5)
- else
- data = clsTurtle.getBlockType(self, "forward")
- if data.name == "minecraft:bedrock" then
- success = false
- break
- end
- end
- end
- while not turtle.forward() do
- clsTurtle.attack(self)
- end
- clsTurtle.changeCoords(self, "forward")
- end
- turtle.select(1)
- return success
- end
- function clsTurtle.getBlockType(self, direction)
- -- turtle.inspect() returns two values
- -- 1) boolean (true/false) success
- -- 2) table with two values:
- -- .name (string) e.g. "minecraft:log"
- -- .metadata (integer) e.g. 0
- -- oak has metadata of 0, spruce 1, birch 2 etc
- local success = false
- local data = {} --initialise empty table variable
- local rt = {}
- rt.name = ""
- rt.modifier = 0
- if direction == "up" then
- success, data = turtle.inspectUp() -- store information about the block above in a table
- elseif direction == "down" then
- success, data = turtle.inspectDown() -- store information about the block below in a table
- else
- success, data = turtle.inspect() -- store information about the block ahead in a table
- end
- if success then -- block found
- rt.name = data.name -- eg "minecraft:log"
- rt.modifier = data.metadata
- end
- return rt -- return a table {rt.name, rt.modifier}
- end
- function clsTurtle.getFirstEmptySlot(self)
- local slot = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- slot = i
- break
- end
- end
- return slot
- end
- function clsTurtle.getFuelStock(self)
- local fuelAmount = 0
- local stockData = {}
- stockData = clsTurtle.getStock(self, "minecraft:coal", 0)
- if stockData.count > 0 then
- fuelAmount = fuelAmount + 80 * stockData.count
- end
- stockData = clsTurtle.getStock(self, "minecraft:coal", 1) --charcoal
- if stockData.count > 0 then
- fuelAmount = fuelAmount + 80 * stockData.count
- end
- stockData = clsTurtle.getStock(self, "minecraft:log", 0)
- if stockData.count > 0 then
- fuelAmount = fuelAmount + 60 * stockData.count
- end
- stockData = clsTurtle.getStock(self, "minecraft:log", 1)
- if stockData.count > 0 then
- fuelAmount = fuelAmount + 60 * stockData.count
- end
- stockData = clsTurtle.getStock(self, "minecraft:log2", 0)
- if stockData.count > 0 then
- fuelAmount = fuelAmount + 60 * stockData.count
- end
- stockData = clsTurtle.getStock(self, "minecraft:log2", 1)
- if stockData.count > 0 then
- fuelAmount = fuelAmount + 60 * stockData.count
- end
- return fuelAmount
- end
- function clsTurtle.getItemName(self, slot)
- local data = {} --initialise empty table variable
- data.name = ""
- if turtle.getItemCount(slot) > 0 then
- data = turtle.getItemDetail(slot)
- end
- return data.name
- end
- function clsTurtle.getItemSlot(self, item, useDamage)
- --useDamage = useDamage or 0
- local data = {} --initialise empty table variable
- local slot = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- data = turtle.getItemDetail(i)
- if data.name == item then
- if useDamage == nil then -- only looking for item name
- slot = i
- break
- else
- if data.damage == useDamage then
- slot = i
- break
- end
- end
- end
- end
- end
- return slot
- end
- function clsTurtle.getLogData(self)
- -- get the slot number and count of each type of log max and min only
- local maxCount = 0
- local minCount = 64
- local data = {}
- local logData = {}
- logData.count = 0
- logData.primarySlot = 0
- logData.primaryCount = 0
- logData.primaryModifier = 0
- logData.primaryName = ""
- logData.finalSlot = 0
- logData.finalCount = 0
- logData.finalModifier = 0
- logData.finalName = ""
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then -- not an empty slot
- data = turtle.getItemDetail(i)
- if data.name == "minecraft:log" or data.name == "minecraft:log2" then
- logData.count = logData.count + data.count
- if data.count > maxCount then
- maxCount = data.count
- logData.primarySlot = i
- logData.primaryCount = data.count
- logData.primaryModifier = data.damage
- logData.primaryName = data.name
- end
- if data.count < minCount then
- minCount = data.count
- logData.finalSlot = i
- logData.finalCount = data.count
- logData.finalModifier = data.damage
- logData.finalName = data.name
- end
- end
- end
- end
- return logData
- end
- function clsTurtle.getLogStock(self)
- local logAmount = 0
- local stockData = {}
- stockData = clsTurtle.getStock(self, "minecraft:log") --oak, spruce, birch, jungle
- logAmount = logAmount + stockData.count
- stockData = clsTurtle.getStock(self, "minecraft:log2") --acacia, dark oak
- logAmount = logAmount + stockData.count
- --[[
- logAmount = logAmount + clsTurtle.getStock(self, "minecraft:log", 0) --oak
- logAmount = logAmount + clsTurtle.getStock(self, "minecraft:log", 1) --spruce
- logAmount = logAmount + clsTurtle.getStock(self, "minecraft:log", 2) --birch
- logAmount = logAmount + clsTurtle.getStock(self, "minecraft:log", 3) -- jungle
- logAmount = logAmount + clsTurtle.getStock(self, "minecraft:log2", 0) --acacia
- logAmount = logAmount + clsTurtle.getStock(self, "minecraft:log2", 1) --dark oak
- ]]--
- return logAmount
- end
- function clsTurtle.getSlotContains(self, slotNo)
- local data = {} --initialise empty table variable
- local rt = {}
- rt.count = 0
- rt.name = ""
- rt.modifier = 0
- if turtle.getItemCount(slotNo) > 0 then
- data = turtle.getItemDetail(slotNo)
- rt.count = data.count
- rt.name = data.name
- rt.modifier = data.damage
- end
- return rt
- end
- function clsTurtle.getStock(self, item, modifier)
- -- return total units and slot numbers of max and min amounts
- local minAmount = 64
- local maxAmount = 0
- local data = {} --initialise empty table variable
- local success = false
- local rt = {}
- rt.count = 0
- rt.primarySlot = 0
- rt.finalSlot = 0
- rt.primaryCount = 0
- rt.finalCount = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- data = turtle.getItemDetail(i)
- if data.name == item then
- success = true
- if modifier == nil then -- only looking for item name (care with log and coal)
- rt.count = rt.count + data.count
- saveToLog("getStock()"..item..", slot="..tostring(i)..", quantity="..data.count)
- if maxAmount <= 64 then
- if data.count >= maxAmount then
- rt.primarySlot = i
- rt.primaryCount = data.count
- maxAmount = data.count
- end
- else
- if data.count <= minAmount then
- minAmount = data.count
- rt.finalSlot = i
- rt.finalCount = data.count
- end
- end
- else --looking for specific block
- if data.damage == modifier then
- rt.count = rt.count + data.count
- saveToLog("getStock()"..item.."("..tostring(modifier)..", slot="..tostring(i)..", quantity="..data.count)
- if maxAmount <= 64 then
- if data.count >= maxAmount then
- rt.primarySlot = i
- rt.primaryCount = data.count
- maxAmount = data.count
- end
- else
- if data.count <= minAmount then
- minAmount = data.count
- rt.finalSlot = i
- rt.finalCount = data.count
- end
- end
- end
- end
- end
- end
- end
- if rt.finalSlot == 0 then
- rt.finalSlot = rt.primarySlot
- end
- if rt.finalCount == 0 then
- rt.finalCount = rt.primaryCount
- end
- if not success then
- if modifier == nil then
- saveToLog("getStock()"..tostring(item).."= not found", true)
- else
- saveToLog("getStock()"..tostring(item).."("..tostring(modifier)..")= not found")
- end
- end
- return rt --{rt.count, rt.primarySlot, rt.finalSlot, rt.primaryCount, rt.finalCount} 0, 0, 0
- end
- function clsTurtle.go(self, path)
- local digDirection = "forward"
- local itemSelected = false
- local move = ""
- local goUp = 0
- local slot = turtle.getSelectedSlot()
- local data = {}
- local stockData = {}
- local currentY = 0
- -- R L F B U D + - = Right, Left, Forward, Back, Up, Down, up while detect, down while not detect
- -- d u x M N O Q = dIG, DIGuP, digDown (x-cavate ?!), MineItem Up and close with cobble, DigUp if valuable and close, digDown if valuable and close, digDown if valuable
- -- c h k C H r S T P = cHECK/ChECK/CHECk place Cobble down/forward/up if no block, place cobblestone/dirt, cHest, Sapling, Torch, DIrT, hoPper
- -- turtle.select(1)
- for i = 1, string.len(path) do
- turtle.select(1)
- itemSlot = 0
- move = string.sub(path, i, i)
- if move == "R" then
- clsTurtle.turnRight(self, 1)
- elseif move == "L" then
- clsTurtle.turnLeft(self, 1)
- elseif move == "B" then
- clsTurtle.back(self, 1)
- elseif move == "F" then
- if clsTurtle.isValuable(self, "forward") then
- mineItem(true, "forward")
- end
- clsTurtle.forward(self, 1)
- elseif move == "U" then
- if clsTurtle.isValuable(self, "up") then
- mineItem(true, "up")
- end
- clsTurtle.up(self, 1)
- elseif move == "D" then
- if clsTurtle.isValuable(self, "down") then
- mineItem(true, "down")
- end
- clsTurtle.down(self, 1)
- elseif move == "+" then
- while turtle.detectUp() do
- clsTurtle.up(self, 1)
- end
- elseif move == "-" then
- while not turtle.inspectDown() do
- if currentY == 0 then
- clsTurtle.down(self, 1)
- else
- while T:getY() > currentY do
- clsTurtle.down(self, 1)
- end
- currentY = 0
- end
- end
- --make sure grass, dirt, stone below
- data = clsTurtle.getBlockType(self, "down")
- if data.name ~= "minecraft:grass" and data.name ~= "minecraft:dirt" and data.name ~= "minecraft:stone" and data.name ~= "minecraft:cobblestone" then
- stockData = clsTurtle.getStock(self, "minecraft:cobblestone")
- if stockData.count > 0 then
- turtle.select(stockData.primarySlot)
- else
- stockData = clsTurtle.getStock(self, "minecraft:dirt")
- if stockData.count > 10 then
- turtle.select(stockData.primarySlot)
- turtle.placeDown()
- end
- end
- end
- elseif move == "*" then
- digDirection = "down"
- while not turtle.inspectDown() do
- clsTurtle.down(self, 1)
- goUp = goUp + 1
- end
- if goUp > 0 then
- for i = 1, goUp do
- clsTurtle.up(self, 1)
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.placeDown()
- else
- if clsTurtle.getItemSlot(self, "minecraft:dirt") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:dirt"))
- turtle.placeDown()
- end
- end
- end
- goUp = 0
- else
- turtle.digDown()
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.placeDown()
- end
- end
- elseif move == "u" then
- digDirection = "up"
- if clsTurtle.isValuable(self, "up") then
- mineItem(true, "up")
- end
- while turtle.detectUp() do
- if turtle.digUp() then
- --allow for sand and gravel
- sleep(.7)
- end
- end
- elseif move == "M" then
- digDirection = "up"
- if clsTurtle.isValuable(self, "up") then
- mineItem(true, "up")
- end
- if not turtle.detectUp() then
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.placeUp()
- else
- if clsTurtle.getItemSlot(self, "minecraft:dirt") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:dirt"))
- turtle.placeUp()
- end
- end
- end
- elseif move == "N" then
- digDirection = "up"
- if clsTurtle.isValuable(self, "up") then
- turtle.digUp()
- end
- if not turtle.detectUp() then
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.placeUp()
- else
- if clsTurtle.getItemSlot(self, "minecraft:dirt") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:dirt"))
- turtle.placeUp()
- end
- end
- end
- elseif move == "O" then
- digDirection = "down"
- if clsTurtle.isValuable(self, "down") then
- turtle.digDown()
- end
- if not turtle.detectDown() then
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.placeDown()
- else
- if clsTurtle.getItemSlot(self, "minecraft:dirt") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:dirt"))
- turtle.placeDown()
- end
- end
- end
- elseif move == "Q" then
- digDirection = "down"
- if clsTurtle.isValuable(self, "down") then
- turtle.digDown()
- end
- elseif move == "d" then
- digDirection = "forward"
- if clsTurtle.isValuable(self, "forward") then
- mineItem(true, "forward")
- end
- while turtle.detect() do
- if turtle.dig() then
- sleep(.7)
- end
- end
- elseif move == "x" then
- digDirection = "down"
- data = clsTurtle.getBlockType(self, "down")
- if data.name ~= "minecraft:torch" then
- if clsTurtle.isValuable(self, "down") then
- mineItem(true, "down")
- else
- turtle.digDown()
- end
- end
- elseif move == "c" then
- currentY = 0
- if clsTurtle.isValuable(self, "down") then
- mineItem(true, "down")
- end
- if turtle.detectDown() then
- --check if vegetation and remove
- data = clsTurtle.getBlockType(self, "down")
- if data.name ~= "minecraft:dirt" and data.name ~= "minecraft:stone" and data.name ~= "minecraft:cobblestone" and data.name ~= "minecraft:grass" then
- turtle.digDown()
- end
- end
- if not turtle.detectDown() then
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- if not turtle.placeDown() then
- currentY = T:getY()
- end
- else
- if clsTurtle.getItemSlot(self, "minecraft:dirt") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:dirt"))
- if not turtle.placeDown() then
- currentY = T:getY()
- end
- end
- end
- end
- elseif move == "h" then
- if clsTurtle.isValuable(self, "forward") then
- mineItem(true, "forward")
- end
- if not turtle.detect() then
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.place()
- else
- if clsTurtle.getItemSlot(self, "minecraft:dirt") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:dirt"))
- turtle.place()
- end
- end
- end
- elseif move == "k" then
- if clsTurtle.isValuable(self, "up") then
- --mineItem(true, "up")
- turtle.digUp()
- end
- if not turtle.detectUp() then
- if clsTurtle.getItemSlot(self, "minecraft:cobblestone") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:cobblestone"))
- turtle.placeUp()
- else
- if clsTurtle.getItemSlot(self, "minecraft:dirt") > 0 then
- turtle.select(clsTurtle.getItemSlot(self, "minecraft:dirt"))
- turtle.placeUp()
- end
- end
- end
- elseif move == "C" or move == "H" or move == "S" or move == "T" or move == "r" or move == "P" then
- itemSelected = false
- if digDirection == "forward" then
- if clsTurtle.isValuable(self, "forward") then
- mineItem(true, "forward")
- end
- while turtle.detect() do
- if turtle.dig() then
- --allow for sand and gravel
- sleep(.7)
- end
- end
- elseif digDirection == "up" then
- if clsTurtle.isValuable(self, "up") then
- mineItem(true, "up")
- end
- while turtle.detectUp() do
- if turtle.digUp() then
- --allow for sand and gravel
- sleep(.7)
- end
- end
- elseif digDirection == "down" then
- if clsTurtle.isValuable(self, "down") then
- mineItem(true, "down")
- end
- if turtle.detectDown() then
- turtle.digDown()
- end
- end
- if move == "C" then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:cobblestone")
- if itemSlot == 0 then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:dirt")
- end
- elseif move == "H" then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:chest")
- elseif move == "S" then
- stockData = clsTurtle.getStock(self, "minecraft:sapling")
- if stockData.primarySlot > 0 then
- itemSlot = stockData.primarySlot
- treeFarm:addSapling()
- end
- elseif move == "T" then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:torch")
- elseif move == "r" then
- digDirection = "up"
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:dirt")
- elseif move == "P" then
- digDirection = "down"
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:hopper")
- end
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if digDirection == "forward" then
- turtle.place()
- elseif digDirection == "up" then
- turtle.placeUp()
- elseif digDirection == "down" then
- turtle.placeDown()
- end
- end
- end
- end
- turtle.select(slot)
- end
- function clsTurtle.isLog(self, direction)
- local success = false
- local data = {}
- local valuableItems = "minecraft:log minecraft:log2"
- if direction == "up" then
- if turtle.detectUp() then
- data = clsTurtle.getBlockType(self, "up")
- end
- elseif direction == "down" then
- if turtle.detectDown() then
- data = clsTurtle.getBlockType(self, "down")
- end
- elseif direction == "forward" then
- if turtle.detect() then
- data = clsTurtle.getBlockType(self, "forward")
- end
- end
- if data.name ~= nil then
- if string.find(valuableItems, data.name) ~= nil then
- success = true
- end
- end
- return success
- end
- function clsTurtle.isValuable(self, direction)
- local success = false
- local data = {}
- local valuableItems = "minecraft:coal_ore minecraft:redstone_ore minecraft:iron_ore minecraft:diamond_ore minecraft:log "
- valuableItems = valuableItems.."minecraft:log2 minecraft:gold_ore minecraft:reeds minecraft:emerald_ore"
- if direction == "up" then
- if turtle.detectUp() then
- data = clsTurtle.getBlockType(self, "up")
- end
- elseif direction == "down" then
- if turtle.detectDown() then
- data = clsTurtle.getBlockType(self, "down")
- end
- elseif direction == "forward" then
- if turtle.detect() then
- data = clsTurtle.getBlockType(self, "forward")
- end
- end
- if data.name ~= nil then
- if string.find(valuableItems, data.name) ~= nil then
- success = true
- --clsTurtle.saveToLog(self, data.name .. " found at "..self.x..", " ..self.y..", "..self.z, true)
- if clsTurtle.getItemSlot(self, data.name) == 0 then
- if clsTurtle.getFirstEmptySlot(self) == 0 then -- no spare slots
- clsTurtle.saveToLog(self, "Dumping excess blocks", true)
- clsTurtle.emptyTrash(self, "up")
- end
- end
- end
- end
- return success, data.name
- end
- function clsTurtle.place(self, blockType, modifier, direction)
- local data = {}
- -- check if current slot is empty
- if turtle.getItemCount(turtle.getSelectedSlot()) == 0 then
- -- Find first non-empty slot
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- break
- end
- end
- end
- data = clsTurtle.getSlotContains(self, turtle.getSelectedSlot())
- if data.name ~= blockType or data.modifier ~= modifier then
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- data = clsTurtle.getSlotContains(self, i)
- if data.name == blockType and data.modifier == modifier then
- turtle.select(i)
- break
- end
- end
- end
- end
- if direction == "down" then
- turtle.placeDown()
- elseif direction == "up" then
- turtle.placeUp()
- else
- turtle.place()
- end
- end
- function clsTurtle.refuel(self, minLevel, recursiveCall)
- minLevel = minLevel or 15
- if recursiveCall == nil then
- recursiveCall = false
- end
- local itemSlot = 0
- local count = 0
- local item = ""
- local damage = 0
- local logData = {}
- local stockData = {}
- local success = false
- if turtle.getFuelLevel() < minLevel then
- --clsTurtle.saveToLog(self, "Refuel needed", true)
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:lava_bucket")
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if turtle.refuel() then
- saveToLog("refuel() lava used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- end
- if not success then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:coal", 1) --charcoal
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if turtle.refuel() then --use all charcoal
- saveToLog("refuel() all charcoal used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- end
- end
- if not success then
- stockData = clsTurtle.getStock(self, "minecraft:coal", 0)
- --itemSlot = clsTurtle.getItemSlot(self, "minecraft:coal", 0)
- if stockData.count > 0 then
- turtle.select(stockData.finalSlot)
- if turtle.refuel(1) then
- saveToLog("refuel() 1 coal used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- end
- end
- if not success then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:planks")
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if turtle.refuel() then
- success = true
- end
- end
- end
- if not success then
- logData = clsTurtle.getLogData(self)
- if logData.count > 0 then --logs onboard
- itemSlot = logData.finalSlot
- clsTurtle.saveToLog(self, "Refuelling with log slot "..tostring(itemSlot)..", crafting planks", true)
- if not clsTurtle.craft(self, "minecraft:planks", 4, logData.finalName, nil, nil, true) then
- print("refuel: error crafting planks")
- else
- success = true
- end
- end
- end
- end
- if not recursiveCall then -- only runs once
- if not success then
- while turtle.getFuelLevel() < minLevel do
- term.clear()
- term.setCursorPos(1,1)
- print("Unable to refuel: "..turtle.getFuelLevel().." fuel remaining")
- print("Please add any fuel to any slot")
- clsTurtle.refuel(self, minLevel, true) -- recursive function, flag set
- sleep(1)
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- end
- return success
- end
- function clsTurtle.saveToLog(self, text, toScreen)
- if toScreen == nil then
- toScreen = true
- end
- text = text or "Nil instead of text supplied, check your code!"
- if toScreen then
- print(text)
- end
- --error here if fso object not created
- if fso:getUseLog() then
- fso:appendLine(text)
- end
- end
- function clsTurtle.sortInventory(self)
- local turns = 0
- local itemSlot = 0
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:chest") --get the slot number containing a chest
- if itemSlot > 0 then -- chest found
- while turtle.detect() do --check for clear space to place chest
- clsTurtle.turnRight(self, 1)
- turns = turns + 1
- if turns == 4 then
- turns = 0
- break
- end
- end
- turtle.select(1)
- while turtle.detect() do --clear space in front. Use loop in case of sand/gravel falling
- turtle.dig()
- sleep(.5)
- end
- turtle.select(itemSlot) --should be slot with chest
- while not turtle.place() do
- while clsTurtle.attack(self) do --in case mobs in front
- print("Mob attacked!")
- end
- end
- -- fill chest with everything
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1) --empty chest into slot 1 onwards
- while turtle.suck() do
- end
- turtle.dig() -- collect chest
- if turns > 0 then --return to original position
- clsTurtle.turnLeft(self, turns)
- end
- end
- end
- function clsTurtle.turnLeft(self, steps)
- steps = steps or 1
- for i = 1, steps do
- turtle.turnLeft()
- self.facing = self.facing - 1
- if self.facing < 0 then
- self.facing = 3
- end
- end
- end
- function clsTurtle.turnRight(self, steps)
- steps = steps or 1
- for i = 1, steps do
- turtle.turnRight()
- self.facing = self.facing + 1
- if self.facing > 3 then
- self.facing = 0
- end
- end
- end
- function clsTurtle.up(self, steps)
- steps = steps or 1
- local success = true
- local data = {}
- clsTurtle.refuel(self, steps)
- turtle.select(1)
- for i = 1, steps do
- if turtle.detectUp() then -- block above
- if turtle.digUp() then -- block above dug, may allow sand or gravel to drop
- sleep(0.5) -- allow time for sand/gravel to drop
- while turtle.digUp() do
- sleep(0.5)
- end
- else --may be bedrock above
- data = clsTurtle.getBlockType(self, "up")
- if data.name == "minecraft:bedrock" then
- success = false
- break
- end
- end
- end --move up unless mob in the way
- while not turtle.up() do
- clsTurtle.attack(self)
- end
- self.y = self.y + 1
- end
- return success
- end
- return self
- end
- function main()
- local stockData = {}
- local numIronOre = 0
- local functionList = {"harvestFirstTree()", "harvestAllTrees()", "findCobble()", "clearBase()", "placeStorage()",
- "createMine(14)", "createMine(11)", "createMine(8)",
- "completeMining(14)", "completeMining(11)", "completeMining(8)",
- "mineToBedrock()", "craftMiningTurtle()"}
- if os.version() == "CraftOS 1.7" then
- ccComputer = "computercraft:CC-Computer"
- ccDisk = "computercraft:disk"
- ccTurtle = "computercraft:CC-Turtle"
- ccCraftyMiningTurtle = "computercraft:CC-TurtleExpanded"
- ccDiskDrive = "computercraft:CC-Peripheral"
- end
- initialise() -- Set up global variables and create objects
- getCoords() -- Get current coordinates from player
- local status = tonumber(loadStatus("currentStatus.txt")) -- if continuing from previous run (debugging aid)
- saveToLog("Starting at function "..functionList[status + 1], true)
- if status == 0 then
- harvestFirstTree() -- harvest first tree and make chest
- status = 1
- end
- if status <= 1 then
- harvestAllTrees() -- harvest remaining trees in 33 x 33 square
- status = 2
- end
- if status <= 2 then
- findCobble() -- get cobble and any incidental coal or iron ore
- status = 3
- end
- if status <= 3 then
- clearBase() -- create cobble base station including 2x2 pond area
- status = 4
- end
- if status <= 4 then
- placeStorage() -- craft chests and place in ground for storage
- status = 5
- end
- if status <= 7 then
- createMine(status) -- create basic 3 layer mine minimal structure and collect iron ore
- status = 8
- end
- stockData = T:getStock("minecraft:iron_ore") -- check enough iron ore found
- numIronOre = stockData.count
- stockData = T:getStock("minecraft:diamond", 0) -- just possible 6 diamonds found
- if stockData.count < 6 or numIronOre < 14 then
- if status <= 10 then
- completeMining(status) -- fully mine each layer constructed above. collect remaining supplies including min 3 diamonds
- status = 11
- end
- stockData = T:getStock("minecraft:diamond", 0)
- if status <= 11 then
- if stockData.count < 6 then -- 3 levels mined out, need more
- mineToBedrock()
- status = 12
- end
- end
- end
- if status <= 12 then
- stockData = T:getStock("minecraft:diamond", 0)
- if stockData.count < 3 then
- craftMiningTurtle(0)
- elseif stockData.count < 6 then
- craftMiningTurtle(1)
- else
- craftMiningTurtle(2)
- end
- end
- end
- --*********************Program runs from here*****************
- main()
Add Comment
Please, Sign In to add comment