Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Replacement for code originally pasted in 2014
- -- selfReplicate2018.lua last modified 12/12/2018
- -- 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/2Zf5an8H
- -- multiline comments not used as Pastebin trashes the appearance of the code!
- -- functions in alphabetical order except main()
- -- classes declared before functions
- -- CraftOS 1.6 names: (altered in main() if using CraftOS 1.7)
- -- global variables
- local ccComputer = "ComputerCraft:CC-Computer"
- local ccDisk = "ComputerCraft:disk"
- local ccTurtle = "ComputerCraft:CC-Turtle"
- local ccCraftyMiningTurtle = "ComputerCraft:CC-TurtleExpanded"
- local ccDiskDrive = "ComputerCraft:CC-Peripheral"
- local pastebinCode = "2Zf5an8H"
- -- classes
- function classCoord(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 classTreeFarm()
- 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 classTurtle()
- 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)
- -- class scope variables
- self.x = 0
- self.y = 0
- self.z = 0
- self.facing = 0
- self.compass = ""
- self.equippedLeft = ""
- self.equippedRight = ""
- self.placeItem = ""
- -- logging variables
- self.useLog = false
- self.logFileName = ""
- self.logFileExists = false
- self.mobAttack = false
- -- helper function for iterating lists
- function clsTurtle.values(self,t) -- general diy iterator
- local i = 0
- return function() i = i + 1; return t[i] end
- end
- -- logging methods
- function clsTurtle.getUseLog(self)
- return self.useLog
- end
- function clsTurtle.setUseLog(self, use)
- self.useLog = use
- return use
- end
- function clsTurtle.getLogExists(self)
- exists = false
- if fs.exists(self.logFileName) then
- exists = true
- end
- return exists
- end
- function clsTurtle.getLogFileName(self)
- return self.logFileName
- end
- function clsTurtle.setLogFileName(self, value)
- self.logFileName = value
- end
- function clsTurtle.getCurrentFileSize(self)
- if self.logFileExists then
- return fs.getSize(self.logFileName)
- else
- return 0
- end
- end
- function clsTurtle.deleteLog(self)
- if fs.exists(self.logFileName) then
- fs.delete(self.logFileName)
- end
- self.logFileExists = false
- return true
- end
- function clsTurtle.appendLine(self, newText)
- local handle = ""
- if fs.exists(self.logFileName) then --logFile already created
- handle = fs.open(self.logFileName, "a")
- else
- handle = fs.open(self.logFileName, "w") --create file
- end
- self.logFileExists = true
- handle.writeLine(newText)
- handle.close()
- end
- function clsTurtle.saveToLog(self, text, toScreen)
- if toScreen == nil then
- toScreen = true
- end
- if text ~= "" and text ~= nil then
- if toScreen then
- print(text)
- end
- if self.useLog then
- clsTurtle.appendLine(self, text)
- end
- end
- end
- -- getters and setters
- function clsTurtle.getX(self) return self.x end
- function clsTurtle.setX(self, newVal) self.x = newVal end
- function clsTurtle.getY(self) 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)
- 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.getPlaceItem(self) return self.placeItem end
- function clsTurtle.setPlaceItem(self, item, useDamage)
- local success = false
- local slot = clsTurtle.getItemSlot(self, item, useDamage)
- if slot > 0 then
- self.placeItem = item
- end
- end
- function clsTurtle.getEquipped(self, side)
- retValue = ""
- if side == "left" then
- retValue = self.equippedLeft
- else
- retValue = self.equippedRight
- end
- return retValue
- end
- function clsTurtle.setEquipped(self, side, value)
- if side == "left" then
- self.equippedLeft = value
- elseif side == "right" then
- self.equippedRight = value
- end
- end
- -- change direction and movement methods
- function clsTurtle.attack(self, direction)
- direction = direction or "forward"
- local slot = turtle.getSelectedSlot()
- turtle.select(1)
- local success = false
- local attackLimit = 10 -- won't get in infinite loop attacking a minecart chest
- if direction == "up" then
- while turtle.attackUp() do --in case mob above
- sleep(1.5)
- attackLimit = attackLimit - 1
- if attackLimit <= 0 then
- break
- end
- end
- elseif direction == "down" then
- while turtle.attackDown() do --in case mob below
- sleep(1.5)
- attackLimit = attackLimit - 1
- if attackLimit <= 0 then
- break
- end
- end
- else
- while turtle.attack() do --in case mob in front
- sleep(1.5)
- attackLimit = attackLimit - 1
- if attackLimit <= 0 then
- break
- end
- end
- end
- if attackLimit > 0 then
- success = true
- end
- turtle.select(slot)
- return success
- end
- function clsTurtle.back(self, steps)
- steps = steps or 1
- local success = false
- 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
- if clsTurtle.forward(self, 1) then-- will also attack mobs if in the way
- success = true
- clsTurtle.changeCoords(self, "back")
- end
- clsTurtle.turnRight(self, 2)
- end
- end
- 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.down(self, steps)
- steps = steps or 1
- local success = false
- clsTurtle.refuel(self, steps)
- turtle.select(1)
- for i = 1, steps do
- if turtle.detectDown() then -- block below
- if clsTurtle.getBlockType(self, "down") == "minecraft:bedrock" then
- break
- else
- turtle.digDown()
- end
- end
- if turtle.down() then --move down unless mob in the way
- success = true
- else -- not bedrock, could be mob or minecart
- if clsTurtle.attack(self, "down") then -- attack succeeded
- if turtle.down() then
- success = true
- end
- end
- end
- if success then
- self.y = self.y - 1
- end
- end
- return success
- end
- function clsTurtle.forward(self, steps)
- steps = steps or 1
- local result, data
- local success = true
- local retryMove = 10
- clsTurtle.refuel(self, steps)
- turtle.select(1)
- for i = 1, steps do
- while turtle.detect() do -- allow for sand/gravel falling
- if not clsTurtle.dig(self, "forward") then-- cannot dig. either bedrock or in server protected area
- success = false
- if clsTurtle.getBlockType(self, "forward") == "minecraft:bedrock" then
- print("Bedrock in front:function exit")
- end
- break
- end
- end
- success = false
- if turtle.forward() then
- success = true
- else
- if self.mobAttack then
- if clsTurtle.attack(self, "forward") then
- if turtle.forward() then
- success = true
- end
- else
- result, data = turtle.forward()
- print(data..":function exit")
- break
- end
- else
- while not turtle.forward() do
- clsTurtle.saveToLog(self, "Waiting for mob to leave...", true)
- sleep(5)
- retryMove = retryMove - 1
- if retryMove <= 0 then
- break
- end
- end
- if retryMove <= 0 then
- if clsTurtle.attack(self, "forward") then
- if turtle.forward() then
- success = true
- end
- else
- result, data = turtle.forward()
- print(data..":function exit")
- break
- end
- end
- end
- end
- if success then
- clsTurtle.changeCoords(self, "forward")
- end
- end
- return success
- 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 = false
- clsTurtle.refuel(self, steps)
- turtle.select(1)
- for i = 1, steps do
- if turtle.detectUp() then -- block above
- if clsTurtle.getBlockType(self, "up") == "minecraft:bedrock" then
- print("Bedrock above:function exit")
- break
- else
- clsTurtle.dig(self, "up")
- end
- end
- if turtle.up() then --move up unless mob in the way
- success = true
- else
- if clsTurtle.attack(self, "up") then -- attack succeeded
- if turtle.up() then
- success = true
- end
- end
- end
- if success then
- self.y = self.y + 1
- end
- end
- return success
- end
- -- other methods
- function clsTurtle.clear(self)
- term.clear()
- term.setCursorPos(1,1)
- end
- function clsTurtle.checkInventoryForItem(self, item, minQuantity, altItem, altMinQuantity)
- -- request player to add items to turtle inventory
- clsTurtle.clear(self)
- minQuantity = minQuantity or 1
- altItem = altItem or ""
- altMinQuantity = altMinQuantity or 0
- local gotItemSlot = 0
- local gotAltItemSlot = 0
- local damage = 0
- local count = 0
- print("Add "..item.." to any single slot "..minQuantity.." needed")
- if altItem ~= "" then
- print("Or add "..altItem.." to any slot "..altMinQuantity.." needed")
- end
- while (count < minQuantity + altMinQuantity) do
- gotItemSlot, damage, count = clsTurtle.getItemSlot(self, item, -1) -- 0 if not present. return slotData.leastSlot, slotData.leastSlotDamage, total, slotData -- first slot no, integer, integer, table
- if gotItemSlot > 0 and count >= minQuantity then
- break
- else
- sleep(0.5)
- end
- if altItem ~= "" then
- gotAltItemSlot, damage, count = clsTurtle.getItemSlot(self, altItem, -1)
- if gotAltItemSlot > 0 and count >= altMinQuantity then
- break
- else
- sleep(0.5)
- end
- end
- coroutine.yield()
- end
- 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 sourceSlot1 = 0
- local sourceSlot2 = 0
- local sourceSlot3 = 0
- local sourceSlot4 = 0
- local success = false
- local emptySlot = 0
- local turns = 0
- local doContinue = true
- local blockType = ""
- local stockData = {}
- local slot = turtle.getSelectedSlot()
- local facing = self.facing
- local chestDirection = "forward"
- turtle.select(1)
- -- 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
- if clsTurtle.getItemSlot(self, "minecraft:chest", -1) == 0 then -- chest not found
- if craftItem == "minecraft:chest" then --make a chest
- sourceSlot1 = clsTurtle.getItemSlot(self, sourceItem1)
- turtle.select(1)
- if turtle.craft() then
- turtle.transferTo(2, 1)
- turtle.transferTo(3, 1)
- turtle.transferTo(5, 1)
- turtle.transferTo(7, 1)
- turtle.transferTo(9, 1)
- turtle.transferTo(10, 1)
- turtle.transferTo(11, 1)
- if turtle.craft() then
- clsTurtle:saveToLog("First Chest crafted")
- end
- end
- end
- doContinue = false
- end
- if doContinue then
- if sourceItem1 == "minecraft:log" or sourceItem1 == "minecraft:log2" then
- stockData = clsTurtle.getLogData(self) --{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
- clsTurtle:saveToLog("craft(): stock.mSlot="..stockData.mostSlot..", stock.mCount="..stockData.mostCount..", stock.lSlot="..stockData.leastSlot..", stock.lCount="..stockData.leastCount)
- if refuel then
- sourceSlot1 = stockData.leastSlot
- clsTurtle:saveToLog("craft(): slot no: "..stockData.leastSlot..", log count:"..stockData.leastCount)
- else
- if craftItem == "minecraft:planks" then
- if stockData.leastCount >= craftQuantity / 4 then -- check if enough in lowest slot
- sourceSlot1 = stockData.leastSlot
- clsTurtle.saveToLog(self, "craft(): slot no: "..stockData.leastSlot..", log count:"..stockData.leastCount)
- else
- sourceSlot1 = stockData.mostSlot
- clsTurtle.saveToLog(self, "craft(): slot no: "..stockData.mostSlot..", log count:"..stockData.mostCount)
- 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
- chestDirection = clsTurtle.getPlaceChestDirection(self)
- blockType = clsTurtle.getBlockType(self, chestDirection)
- while blockType ~= "minecraft:chest" do --check if chest placed eg mob in the way
- clsTurtle.dig(self, chestDirection)
- if not clsTurtle.place(self, "minecraft:chest", -1, chestDirection) then
- sleep(1) -- wait for mob to move
- chestDirection = clsTurtle.getPlaceChestDirection(self)
- end
- blockType = clsTurtle.getBlockType(self, chestDirection)
- end
- -- fill chest with everything except required items
- for i = 1, 16 do
- if i ~= sourceSlot1 and i ~= sourceSlot2 and i ~= sourceSlot3 then
- clsTurtle.drop(self, i, chestDirection)
- 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
- clsTurtle.drop(self, i, chestDirection)
- 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
- clsTurtle.drop(self, 16, chestDirection)
- end
- else --crafting not successful, so empty out all items into chest
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- clsTurtle.drop(self, i, chestDirection)
- end
- end
- end
- while clsTurtle.suck(self, chestDirection) do end
- clsTurtle.dig(self, chestDirection) -- collect chest
- while facing ~= self.facing do --return to original position
- clsTurtle.turnLeft(self, 1)
- end
- else
- clsTurtle.saveToLog(self, "Missing crafting ingredients.", true)
- success = false
- end
- turtle.select(slot)
- return success
- end
- function clsTurtle.craftChests(self, quantity, orLess)
- local chestsCrafted = 0
- local stockData = {}
- stockData = clsTurtle.getLogData(self)--{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
- local logNum = stockData.total
- if logNum >= 2 then -- at least one chest can be made
- if stockData.mostCount >= quantity * 2 then
- if quantity > 8 then
- clsTurtle.craft(self, "minecraft:planks", 64, stockData.mostName, nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", 8, "minecraft:planks", nil, nil, false)
- quantity = quantity - 8
- end
- stockData = clsTurtle.getLogData(self)
- clsTurtle.craft(self, "minecraft:planks", quantity * 8, stockData.mostName, 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 = (math.floor(stockData.mostCount / 2)) * 2
- logNeeded = quantity * 2 - logNum
- clsTurtle.craft(self, "minecraft:planks", logNum * 4, stockData.mostName, nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", logNum / 2, "minecraft:planks", nil, nil, false)
- stockData = clsTurtle.getLogData(self)
- logNum = (math.floor(stockData.mostCount / 2)) * 2
- if logNum >= logNeeded then
- clsTurtle.craft(self, "minecraft:planks", logNeeded * 4, stockData.mostName, nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", logNeeded / 2, "minecraft:planks", nil, nil, false)
- else
- clsTurtle.craft(self, "minecraft:planks", logNum * 4, stockData.mostName, 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.dig(self, direction)
- direction = direction or "forward"
- local success = false
- turtle.select(1)
- if direction == "up" then
- while turtle.digUp() do
- sleep(0.7)
- success = true
- end
- elseif direction == "down" then
- if turtle.digDown() then
- success = true
- end
- else -- forward by default
- while turtle.dig() do
- sleep(0.7)
- success = true
- end
- end
- return success
- end
- function clsTurtle.drop(self, slot, direction, amount)
- direction = direction or "forward"
- turtle.select(slot)
- local success = false
- if direction == "up" then
- if amount == nil then
- if turtle.dropUp() then
- success = true
- end
- else
- if turtle.dropUp(amount) then
- success = true
- end
- end
- elseif direction == "down" then
- if amount == nil then
- if turtle.dropDown() then
- success = true
- end
- else
- if turtle.dropDown(amount) then
- success = true
- end
- end
- else
- if amount == nil then
- if turtle.drop() then
- success = true
- end
- else
- if turtle.drop(amount) then
- success = true
- end
- end
- end
- return success
- end
- function clsTurtle.dropItem(self, item, keepAmount, direction)
- keepAmount = keepAmount or 0
- direction = direction or "down"
- local itemSlot = 0
- local stockData = {}
- if keepAmount <= 0 then -- drop everything
- itemSlot = clsTurtle.getItemSlot(self, item)
- while itemSlot > 0 do
- clsTurtle.drop(self, itemSlot, direction)
- itemSlot = clsTurtle.getItemSlot(self, item)
- end
- elseif keepAmount >= 64 then -- drop everything else
- stockData = clsTurtle.getStock(self, item)
- if item == "minecraft:log" then
- if stockData.mostSlot ~= stockData.leastSlot then
- clsTurtle.drop(self, stockData.leastSlot, direction)
- end
- else
- while stockData.total > keepAmount do
- if stockData.mostSlot ~= stockData.leastSlot then
- clsTurtle.drop(self, stockData.leastSlot, direction)
- else --only one slot but more than required in it
- clsTurtle.drop(self, stockData.mostSlot, direction)
- end
- stockData = clsTurtle.getStock(self, item)
- end
- end
- else --store specific amount
- itemSlot = clsTurtle.getItemSlot(self, item)
- local dropCount = turtle.getItemCount(itemSlot) - keepAmount
- if itemSlot > 0 then
- clsTurtle.drop(self, itemSlot, direction, dropCount)
- end
- end
- end
- function clsTurtle.dumpRefuse(self)
- --dump dirt, cobble, sand, gravel
- local itemlist = {}
- local blockType = ""
- local blockModifier
- local slotCount
- local cobbleCount = 0
- local dirtCount = 0
- itemlist[1] = "minecraft:gravel"
- itemlist[2] = "minecraft:stone" --includes andesite, diorite etc
- itemlist[3] = "minecraft:dirt"
- itemlist[4] = "minecraft:flint"
- for x = 1, 15 do
- for i = x + 1 , 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.compareTo(x) then
- turtle.transferTo(x)
- end
- end
- end
- end
- for i = 1, 16 do
- slotCount, blockType, blockModifier = clsTurtle.getSlotContains(self, i)
- if blockType == "minecraft:cobblestone" then
- if cobbleCount > 0 then
- turtle.select(i)
- turtle.drop()
- else
- cobbleCount = cobbleCount + 1
- end
- end
- for j = 1, 4 do
- if blockType == itemlist[j] then
- turtle.select(i)
- turtle.drop()
- break
- end
- end
- end
- turtle.select(1)
- end
- function clsTurtle.emptyTrash(self, direction)
- direction = direction or "down"
- local slotData = {}
- local item = ""
- local move = false
- local keepItems = {"minecraft:cobblestone", "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"}
- local keepit = false
- -- empty excess cobble, dirt, all gravel, unknown minerals
- --keep max of 1 stack
- clsTurtle.sortInventory(self)
- if direction == "down" then
- if clsTurtle.down(self, 1) then
- 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
- clsTurtle.saveToLog(self, "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:redstone", 64)
- slotData = clsTurtle.getStock(self, "minecraft:coal", 0)
- if slotData.total > 64 then
- if slotData.mostSlot ~= slotData.leastSlot and slotData.leastSlot ~= 0 then
- turtle.select(slotData.leastSlot)
- turtle.refuel()
- clsTurtle.saveToLog(self, "EmptyTrash():xs coal used for fuel", true)
- end
- end
- if direction == "down" and move then
- clsTurtle.up(self, 1)
- end
- turtle.select(1)
- end
- function clsTurtle.emptyTrashItem(self, direction, item, maxAmount)
- local stockData = clsTurtle.getStock(self, item)
- while stockData.total > maxAmount and stockData.mostSlot > 0 do
- turtle.select(stockData.mostSlot)
- if direction == "up" then
- if not turtle.dropUp() then
- if not turtle.dropDown() then
- clsTurtle.saveToLog(self, "EmptyTrashItem():error ", true)
- break
- end
- end
- else
- if not turtle.dropDown() then
- if not turtle.dropUp() then
- clsTurtle.saveToLog(self, "EmptyTrashItem():error ", true)
- break
- end
- end
- end
- stockData = clsTurtle.getStock(self, item)
- end
- end
- function clsTurtle.equip(self, side, useItem, useDamage)
- useDamage = useDamage or 0
- local slot, damage = clsTurtle.getItemSlot(self, useItem)
- local currentSlot = turtle.getSelectedSlot()
- self.success = false
- if slot > 0 and damage == useDamage then
- turtle.select(slot)
- if side == "right" then
- if turtle.equipRight() then
- self.success = true
- self.equippedRight = useItem
- end
- else
- if turtle.equipLeft() then
- self.success = true
- self.equippedLeft = useItem
- end
- end
- end
- turtle.select(currentSlot)
- return self.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 data = {} --initialise empty table variable
- 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
- return data.name, data.metadata -- returns nil, nil if no block
- end
- function clsTurtle.getFirstEmptySlot(self)
- local emptySlot = 0
- for i = 1,16 do
- if turtle.getItemCount(i) == 0 then
- emptySlot = i
- break
- end
- end
- return emptySlot
- 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)
- -- return slot no with least count, damage(modifier) and total count available
- -- along with a table of mostSlot, mostCount, leastSlot, leastCount
- useDamage = useDamage or -1 -- -1 damage means is not relevant
- local data = {} --initialise empty table variable
- local slotData = {}
- local total = 0
- -- setup return table
- slotData.mostSlot = 0
- slotData.mostName = ""
- slotData.mostCount = 0
- slotData.mostModifier = 0
- slotData.leastSlot = 0
- slotData.leastName = ""
- slotData.leastCount = 0
- slotData.leastModifier = 0
- for i = 1, 16 do
- local count = turtle.getItemCount(i)
- if count > 0 then
- data = turtle.getItemDetail(i)
- if data.name == item and (data.damage == useDamage or useDamage == -1) then
- total = total + count
- if count > slotData.mostCount then--
- slotData.mostSlot = i
- slotData.mostName = data.name
- slotData.mostCount = count
- slotData.mostModifier = data.damage
- end
- if count < slotData.leastCount then--
- slotData.leastSlot = i
- slotData.leastName = data.name
- slotData.leastCount = count
- slotData.leastModifier = data.damage
- end
- end
- end
- end
- if slotData.mostSlot > 0 then
- if slotData.leastSlot == 0 then
- slotData.leastSlot = slotData.mostSlot
- slotData.leastName = slotData.mostName
- slotData.leastCount = slotData.mostCount
- slotData.leastModifier = slotData.mostModifier
- end
- end
- return slotData.leastSlot, slotData.leastModifier, total, slotData -- first slot no, integer, integer, table
- end
- function clsTurtle.getLogData(self)
- -- get the slot number and count of each type of log max and min only
- local leastSlot, leastDamage, total, total2
- local logData = {}
- local logData2 = {}
- local retData = {}
- leastSlot, leastDamage, total, logData = clsTurtle.getItemSlot(self, "minecraft:log", -1)
- leastSlot, leastDamage, total2, logData2 = clsTurtle.getItemSlot(self, "minecraft:log2", -1)
- retData.total = total + total2
- if logData.mostCount > logData2.mostCount then
- retData.mostSlot = logData.mostSlot
- retData.mostCount = logData.mostCount
- retData.mostModifier = logData.mostModifier
- retData.mostName = logData.mostName
- if logData2.leastCount > 0 then
- retData.leastSlot = logData2.leastSlot
- retData.leastCount = logData2.leastCount
- retData.leastModifier = logData2.leastModifier
- retData.leastName = logData2.leastName
- else
- retData.leastSlot = logData.leastSlot
- retData.leastCount = logData.leastCount
- retData.leastModifier = logData.leastModifier
- retData.leastName = logData.leastName
- end
- else
- retData.mostSlot = logData2.mostSlot
- retData.mostCount = logData2.mostCount
- retData.mostModifier = logData2.mostModifier
- retData.mostName = logData2.mostName
- if logData.mostCount > 0 then
- retData.leastSlot = logData.leastSlot
- retData.leastCount = logData.leastCount
- retData.leastModifier = logData.leastModifier
- retData.leastName = logData.leastName
- else
- retData.leastSlot = logData2.leastSlot
- retData.leastCount = logData2.leastCount
- retData.leastModifier = logData2.leastModifier
- retData.leastName = logData2.leastName
- end
- end
- return retData --{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
- end
- function clsTurtle.getLogStock(self)
- local logAmount = 0
- local stockData = clsTurtle.getStock(self, "minecraft:log") --oak, spruce, birch, jungle
- logAmount = logAmount + stockData.total
- stockData = clsTurtle.getStock(self, "minecraft:log2") --acacia, dark oak
- logAmount = logAmount + stockData.total
- return logAmount
- end
- function clsTurtle.getPlaceChestDirection(self)
- local facing = self.facing
- local chestDirection = "forward"
- while turtle.detect() do --check for clear space to place chest
- clsTurtle.turnRight(self, 1)
- if facing == self.facing then -- full circle
- --check up and down
- if turtle.detectDown() then -- no space below
- if turtle.detectUp() then
- if clsTurtle.dig(self, "up") then
- chestDirection = "up"
- end
- else
- chestDirection = "up"
- end
- else
- chestDirection = "down"
- end
- break
- end
- end
- return chestDirection
- end
- function clsTurtle.getSlotContains(self, slotNo)
- local data = {} --initialise empty table variable
- if turtle.getItemCount(slotNo) > 0 then
- data = turtle.getItemDetail(slotNo)
- else
- data.count = 0
- data.name = ""
- data.damage = 0
- end
- return data.count, data.name, data.damage
- end
- function clsTurtle.getStock(self, item, modifier)
- -- return total units and slot numbers of max and min amounts
- local slot, damage, total, slotData = clsTurtle.getItemSlot(self, item, modifier) --return .leastSlot, .leastModifier, total, slotData
- local rt = {}
- rt.total = total
- rt.mostSlot = slotData.mostSlot
- rt.leastSlot = slotData.leastSlot
- rt.mostCount = slotData.mostCount
- rt.leastCount = slotData.leastCount
- if slot == 0 then
- if modifier == nil then
- clsTurtle.saveToLog(self, "getStock()"..tostring(item).."= not found", true)
- else
- clsTurtle.saveToLog(self, "getStock()"..tostring(item).."("..tostring(modifier)..")= not found")
- end
- end
- return rt --{rt.total, rt.mostSlot, rt.leastSlot, rt.mostCount, rt.leastCount}
- end
- function clsTurtle.go(self, path, useTorch, torchInterval)
- useTorch = useTorch or false -- used in m an M to place torches in mines
- torchInterval = torchInterval or 8
- local intervalList = {1, torchInterval * 1 + 1,
- torchInterval * 2 + 1,
- torchInterval * 3 + 1,
- torchInterval * 4 + 1,
- torchInterval * 5 + 1,
- torchInterval * 6 + 1}
- local slot = turtle.getSelectedSlot()
- turtle.select(1)
- local commandList = {}
- local command = ""
- -- make a list of commands from path string eg "x0F12U1" = x0, F12, U1
- for i = 1, string.len(path) do
- local character = string.sub(path, i, i) -- examine each character in the string
- if tonumber(character) == nil then -- char is NOT a number
- if command ~= "" then -- command is NOT empty eg "x0"
- table.insert(commandList, command) -- add to table eg "x0"
- end
- command = character -- replace command with new character eg "F"
- else -- char IS a number
- command = command..character -- add eg 1 to F = F1, 2 to F1 = F12
- if i == string.len(path) then -- last character in the string
- table.insert(commandList, command)
- end
- end
- end
- -- R# L# F# B# U# D# +0 -0 = Right, Left, Forward, Back, Up, Down, up while detect and return, down while not detect
- -- dig: x0,x1,x2 (up/fwd/down)
- -- suck: s0,s1,s2
- -- place chest: H0,H1,H2
- -- place sapling: S0,S1,S2
- -- place Torch: T0,T1,T2
- -- place Hopper: P0,P1,P2
- -- mine floor: m# = mine # blocks above and below, checking for valuable items below, and filling space with cobble or dirt
- -- mine ceiling: M# = mine # blocks, checking for valuable items above, and filling space with cobble or dirt
- -- mine ceiling: N# same as M but not mining block below unless valuable
- -- place: C,H,r,S,T,P = Cobble / cHest / DIrT / Sapling / Torch / hoPper in direction 0/1/2 (up/fwd/down) eg C2 = place cobble down
- clsTurtle.refuel(self, 15)
- turtle.select(1)
- for cmd in clsTurtle.values(self, commandList) do -- eg F12 or x1
- local move = string.sub(cmd, 1, 1)
- local modifier = tonumber(string.sub(cmd, 2))
- if move == "R" then
- clsTurtle.turnRight(self, modifier)
- elseif move == "L" then
- clsTurtle.turnLeft(self, modifier)
- elseif move == "B" then
- clsTurtle.back(self, modifier)
- elseif move == "F" then
- clsTurtle.forward(self, modifier)
- elseif move == "U" then
- clsTurtle.up(self, modifier)
- elseif move == "D" then
- clsTurtle.down(self, modifier)
- elseif move == "+" then
- local height = 0
- while turtle.detectUp() do
- clsTurtle.up(self, 1)
- height = height + 1
- end
- clsTurtle.down(self, height)
- elseif move == "-" then
- while not turtle.inspectDown() do
- clsTurtle.down(self, 1)
- end
- elseif move == "x" then
- if modifier == 0 then
- clsTurtle.dig(self, "up")
- elseif modifier == 1 then
- clsTurtle.dig(self, "forward")
- elseif modifier == 2 then
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- elseif move == "s" then
- if modifier == 0 then
- while turtle.suckUp() do end
- elseif modifier == 1 then
- while turtle.suck() do end
- elseif modifier == 2 then
- while turtle.suckDown() do end
- end
- elseif move == "m" then --mine block below and/or fill void
- for i = 1, modifier + 1 do --eg m8 run loop 9 x
- turtle.select(1)
- if clsTurtle.isValuable(self, "down") then
- turtle.digDown() -- dig if valuable
- elseif clsTurtle.getBlockType(self, "down") == "minecraft:gravel" then
- turtle.digDown() -- dig if gravel
- else --check for lava
- if clsTurtle.getBlockType(self, "down") == "minecraft:lava" then
- clsTurtle.place(self, "minecraft:bucket", 0, "down")
- end
- end
- clsTurtle.dig(self, "up") -- create player coridoor
- if not turtle.detectDown() then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
- clsTurtle.place(self, "minecraft:dirt", -1, "down")
- end
- end
- if i <= modifier then -- m8 = move forward 8x
- if useTorch then
- if i == intervalList[1] or
- i == intervalList[2] or
- i == intervalList[3] or
- i == intervalList[4] or
- i == intervalList[5] then
- clsTurtle.up(self, 1)
- clsTurtle.place(self, "minecraft:torch", -1, "down")
- clsTurtle.forward(self, 1)
- clsTurtle.down(self, 1)
- else
- clsTurtle.forward(self, 1)
- end
- else
- clsTurtle.forward(self, 1)
- end
- end
- end
- elseif move == "n" then --mine block below and/or fill void + check left side
- for i = 1, modifier + 1 do --eg m8 run loop 9 x
- turtle.select(1)
- if clsTurtle.isValuable(self, "down") then
- turtle.digDown() -- dig if valuable
- elseif clsTurtle.getBlockType(self, "down") == "minecraft:gravel" then
- turtle.digDown() -- dig if gravel
- else --check for lava
- if clsTurtle.getBlockType(self, "down") == "minecraft:lava" then
- clsTurtle.place(self, "minecraft:bucket", 0, "down")
- end
- end
- clsTurtle.dig(self, "up") -- create player coridoor
- if not turtle.detectDown() then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
- clsTurtle.place(self, "minecraft:dirt", -1, "down")
- end
- end
- clsTurtle.turnLeft(self, 1)
- if clsTurtle.isValuable(self, "forward") then
- turtle.dig() -- dig if valuable
- end
- if not turtle.detect() then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "forward") then
- clsTurtle.place(self, "minecraft:dirt", -1, "forward")
- end
- end
- clsTurtle.turnRight(self, 1)
- if i <= modifier then -- m8 = move forward 8x
- if useTorch then
- if i == intervalList[1] or
- i == intervalList[2] or
- i == intervalList[3] or
- i == intervalList[4] or
- i == intervalList[5] then
- clsTurtle.up(self, 1)
- clsTurtle.place(self, "minecraft:torch", -1, "down")
- clsTurtle.forward(self, 1)
- clsTurtle.down(self, 1)
- else
- clsTurtle.forward(self, 1)
- end
- else
- clsTurtle.forward(self, 1)
- end
- end
- end
- elseif move == "M" then --mine block above and/or fill void
- for i = 1, modifier + 1 do
- turtle.select(1)
- if clsTurtle.isValuable(self, "up") then
- clsTurtle.dig(self, "up")
- else --check for lava
- if clsTurtle.getBlockType(self, "up") == "minecraft:lava" then
- clsTurtle.place(self, "minecraft:bucket", 0, "up")
- end
- end
- if not turtle.detectUp() then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "up") then
- clsTurtle.place(self, "minecraft:dirt", -1, "up")
- end
- end
- if i <= modifier then -- will not move forward if modifier = 0
- if useTorch then
- if i == intervalList[1] or
- i == intervalList[2] or
- i == intervalList[3] or
- i == intervalList[4] or
- i == intervalList[5] then
- clsTurtle.place(self, "minecraft:torch", 0, "down")
- end
- end
- clsTurtle.forward(self, 1)
- end
- end
- elseif move == "N" then --mine block above and/or fill void + mine block below if valuable
- for i = 1, modifier + 1 do
- turtle.select(1)
- if clsTurtle.isValuable(self, "up") then
- clsTurtle.dig(self, "up")
- else --check for lava
- if clsTurtle.getBlockType(self, "up") == "minecraft:lava" then
- clsTurtle.place(self, "minecraft:bucket", 0, "up")
- end
- end
- if not turtle.detectUp() then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "up") then
- clsTurtle.place(self, "minecraft:dirt", -1, "up")
- end
- end
- turtle.select(1)
- if clsTurtle.isValuable(self, "down") then
- turtle.digDown()
- end
- if i <= modifier then
- clsTurtle.forward(self, 1)
- end
- end
- elseif move == "Q" then --mine block above and/or fill void + mine block below if valuable + left side
- for i = 1, modifier + 1 do
- turtle.select(1)
- if clsTurtle.isValuable(self, "up") then
- clsTurtle.dig(self, "up")
- else --check for lava
- if clsTurtle.getBlockType(self, "up") == "minecraft:lava" then
- clsTurtle.place(self, "minecraft:bucket", 0, "up")
- end
- end
- if not turtle.detectUp() then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "up") then
- clsTurtle.place(self, "minecraft:dirt", -1, "up")
- end
- end
- clsTurtle.turnLeft(self, 1)
- if clsTurtle.isValuable(self, "forward") then
- turtle.dig() -- dig if valuable
- end
- if not turtle.detect() then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "forward") then
- clsTurtle.place(self, "minecraft:dirt", -1, "forward")
- end
- end
- clsTurtle.turnRight(self, 1)
- if clsTurtle.isValuable(self, "down") then
- turtle.digDown()
- end
- if i <= modifier then
- clsTurtle.forward(self, 1)
- end
- end
- elseif move == "C" or move == "H" or move == "r" or move == "T" or move == "S" then --place item up/forward/down
- local placeItem = "minecraft:cobblestone"
- local direction = {"up", "forward", "down"}
- if move == "H" then
- placeItem = "minecraft:chest"
- elseif move == "r" then
- placeItem = "minecraft:dirt"
- elseif move == "T" then
- placeItem = "minecraft:torch"
- elseif move == "S" then
- placeItem = "minecraft:sapling"
- end
- if modifier == 0 then
- clsTurtle.dig(self, "up")
- elseif modifier == 1 then
- clsTurtle.dig(self, "forward")
- else
- turtle.digDown()
- end
- if move == "C" then
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, direction[modifier + 1]) then
- clsTurtle.place(self, "minecraft:dirt", -1, direction[modifier + 1])
- end
- else
- clsTurtle.place(self, placeItem, -1, direction[modifier + 1])
- end
- elseif move == "*" then
- local goUp = 0
- 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 not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
- clsTurtle.place(self, "minecraft:dirt", -1, "down")
- end
- end
- goUp = 0
- else
- turtle.digDown()
- if not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
- clsTurtle.place(self, "minecraft:dirt", -1, "down")
- end
- end
- elseif move == "c" then
- 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 not clsTurtle.place(self, "minecraft:cobblestone", -1, "down") then
- clsTurtle.place(self, "minecraft:dirt", -1, "down")
- end
- end
- elseif move == "Z" then -- mine to bedrock
- for i = 1, modifier + 1 do
- turtle.select(1)
- local goUp = 0
- while clsTurtle.down(self, 1) do
- goUp = goUp + 1
- end
- for j = goUp, 1, -1 do
- for k = 1, 4 do
- clsTurtle.turnRight(self, 1)
- if clsTurtle.isValuable(self, "forward") then
- clsTurtle.place(self, "minecraft:cobblestone", -1, "forward")
- end
- end
- clsTurtle.up(self, 1)
- clsTurtle.place(self, "minecraft:cobblestone", -1, "down")
- turtle.select(1)
- end
- if i <= modifier then
- clsTurtle.forward(self, 2)
- end
- end
- end
- end
- turtle.select(slot)
- end
- function clsTurtle.harvestTree(self, extend)
- extend = extend or false
- local treeHeight = 0
- local addHeight = 0
- local onLeft = true
- local logType = clsTurtle.getBlockType(self, "forward")
- local slot = turtle.getSelectedSlot()
- clsTurtle.dig(self, "forward")
- clsTurtle.forward(self, 1) -- will force refuel if first tree
- clsTurtle.go(self, "L1") -- dig base of tree, go under tree
- -- check if on r or l of double width tree
- if clsTurtle.getBlockType(self, "forward", -1) == "minecraft:log" or clsTurtle.getBlockType(self, "forward", -1) == "minecraft:log2" then
- onLeft = false -- placed on right side of 2 block tree
- end
- clsTurtle.turnRight(self, 1)
- --craft chest if none onboard
- if clsTurtle.getItemSlot(self, "minecraft:chest", -1) == 0 then
- clsTurtle.go(self, "U2")
- clsTurtle.craft(self, "minecraft:planks", 8, logType, nil, nil, false)
- clsTurtle.craft(self, "minecraft:chest", 1, "minecraft:planks", nil, nil, false)
- else
- clsTurtle.go(self, "U2")
- end
- treeHeight = treeHeight + 2
- -- Loop to climb up tree and harvest trunk and surrounding leaves
- while clsTurtle.dig(self, "up") do -- continue loop while block detected above
- if clsTurtle.up(self, 1) then -- Move up
- treeHeight = treeHeight + 1
- end
- -- Inner loop to check for leaves
- for i = 1, 4 do
- if turtle.detect() then -- check if leaves in front
- turtle.dig() --Dig leaves
- end
- clsTurtle.turnRight(self, 1)
- end
- end
- -- At top of the tree. New loop to return to ground
- if extend then
- if onLeft then
- clsTurtle.go(self, "F1R1F1R2")
- else
- clsTurtle.go(self, "F1L1F1R2")
- end
- while turtle.detectUp() do
- clsTurtle.up(self, 1)
- addHeight = addHeight + 1
- end
- if addHeight > 0 then
- clsTurtle.down(self, addHeight)
- end
- end
- for i = 1, treeHeight do
- clsTurtle.down(self, 1)
- end
- if extend then
- if onLeft then
- clsTurtle.go(self, "F1L1F1R2")
- else
- clsTurtle.go(self, "F1R1F1R2")
- end
- end
- turtle.select(slot)
- end
- function clsTurtle.initialise(self)
- clsTurtle.clear(self)
- if os.getComputerLabel() == nil then
- while name == "" do
- clsTurtle.clear(self)
- print("Type a name for this turtle")
- local name = read()
- end
- os.setComputerLabel(name)
- clsTurtle.clear(self)
- end
- print("Checking equipment...\nDO NOT REMOVE ANY ITEMS!")
- self.equippedRight, self.equippedLeft = clsTurtle.setEquipment(self) -- set in equipped items
- local message = os.getComputerLabel().." is equipped with:\n"
- if self.equippedLeft ~= "" and self.equippedRight ~= "" then
- message = message.."Left: "..self.equippedLeft.." and Right: "..self.equippedRight
- elseif self.equippedLeft ~= "" then
- message = message.."Left: "..self.equippedLeft
- elseif self.equippedRight ~= "" then
- message = message.."Right: "..self.equippedRight
- end
- print(message)
- print("\n\nDo you want to create a logfile? (y/n)")
- local response = read()
- if response == "y" then
- self.useLog = true
- self.logFileName = "turtleLog.txt"
- end
- end
- function clsTurtle.isValuable(self, direction)
- local success = false
- local blockType = ""
- local blockModifier
- local itemList = "minecraft:dirt,minecraft:grass,minecraft:stone,minecraft:gravel,minecraft:chest,"..
- "minecraft:cobblestone,minecraft:sand,minecraft:torch,minecraft:bedrock"
- if direction == "up" then
- if turtle.detectUp() then
- blockType, blockModifier = clsTurtle.getBlockType(self, "up")
- end
- elseif direction == "down" then
- if turtle.detectDown() then
- blockType, blockModifier = clsTurtle.getBlockType(self, "down")
- end
- elseif direction == "forward" then
- if turtle.detect() then
- blockType, blockModifier = clsTurtle.getBlockType(self, "forward")
- end
- end
- if blockType ~= "" then --block found
- success = true
- if string.find(itemList, blockType) ~= nil then
- success = false
- end
- end
- return success
- end
- function clsTurtle.isVegetation(self, blockName)
- blockName = blockName or ""
- 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 clsTurtle.place(self, blockType, damageNo, direction)
- local success = false
- local slot = slot or clsTurtle.getItemSlot(self, blockType, damageNo)
- if slot > 0 then
- if direction == "down" then
- clsTurtle.dig(self, "down")
- turtle.select(slot)
- if turtle.placeDown() then
- if blockType == "minecraft:bucket" then
- if turtle.refuel() then
- clsTurtle.saveToLog("Refuelled with lava. Current fuel level: "..turtle.getFuelLevel())
- end
- end
- success = true
- else
- if not clsTurtle.attack(self, "down") then
- clsTurtle.saveToLog("Error placing "..blockType.." ? chest or minecart below")
- end
- end
- elseif direction == "up" then
- clsTurtle.dig(self, "up")
- turtle.select(slot)
- if turtle.placeUp() then
- if blockType == "minecraft:bucket" then
- if turtle.refuel() then
- clsTurtle.saveToLog("Refuelled with lava. Current fuel level: "..turtle.getFuelLevel())
- end
- end
- success = true
- else
- clsTurtle.saveToLog("Error placing "..blockType.." ? chest or minecart above")
- end
- else
- clsTurtle.dig(self, "forward")
- turtle.select(slot)
- if turtle.place() then
- if blockType == "minecraft:bucket" then
- if turtle.refuel() then
- clsTurtle.saveToLog("Refuelled with lava. Current fuel level: "..turtle.getFuelLevel())
- end
- end
- success = true
- else
- clsTurtle.saveToLog("Error placing "..blockType.." ? chest or minecart ahead")
- end
- end
- end
- turtle.select(1)
- return success
- end
- function clsTurtle.refuel(self, minLevel)
- minLevel = minLevel or 15
- local itemSlot = 0
- local slot = turtle.getSelectedSlot()
- local count = 0
- local item = ""
- local damage = 0
- local success = false
- local data = {}
- if turtle.getFuelLevel() < minLevel then
- -- check each slot for fuel item
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:lava_bucket", -1) --return slotData.leastSlot, slotData.leastSlotDamage, total, slotData
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if turtle.refuel() then
- clsTurtle.saveToLog(self, "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
- clsTurtle.saveToLog(self, "refuel() all charcoal used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- end
- end
- if not success then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:coal", 0)
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if turtle.refuel(1) then
- clsTurtle.saveToLog(self, "refuel() 1 coal used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- end
- end
- if not success then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:planks", -1)
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if turtle.refuel() then
- clsTurtle.saveToLog(self, "refuel() planks used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- end
- end
- if not success then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:log", -1)
- if itemSlot > 0 then --logs onboard
- clsTurtle.saveToLog(self, "Refuelling with log slot "..tostring(itemSlot)..", crafting planks", true)
- if turtle.getItemCount(itemSlot) == 1 then
- turtle.select(itemSlot)
- turtle.craft()
- if turtle.refuel() then
- clsTurtle.saveToLog(self, "refuel() planks used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- else
- if clsTurtle.craft(self, "minecraft:planks", 4, "minecraft:log", nil, nil, true) then
- success = true
- else
- clsTurtle.saveToLog(self, "refuel() error crafting planks", true)
- end
- end
- end
- end
- if not success then
- itemSlot = clsTurtle.getItemSlot(self, "minecraft:log2", -1)
- if itemSlot > 0 then --logs onboard
- clsTurtle.saveToLog(self, "Refuelling with log2 slot "..tostring(itemSlot)..", crafting planks", true)
- if turtle.getItemCount(itemSlot) == 1 then
- turtle.select(itemSlot)
- turtle.craft()
- if turtle.refuel() then
- clsTurtle.saveToLog(self, "refuel() planks used. level="..turtle.getFuelLevel(),true)
- success = true
- end
- else
- if clsTurtle.craft(self, "minecraft:planks", 4, "minecraft:log2", nil, nil, true) then
- success = true
- else
- clsTurtle.saveToLog(self, "refuel() error crafting planks", true)
- end
- end
- end
- end
- if not success then
- term.clear()
- term.setCursorPos(1,1)
- print("Unable to refuel: "..turtle.getFuelLevel().." fuel remaining")
- error()
- 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
- turtle.select(slot)
- return success
- end
- function clsTurtle.setEquipment(self)
- -- if contains a crafting table, puts it on the right. Any other tool on the left
- local emptySlotR = clsTurtle:getFirstEmptySlot() -- first empty slot
- local emptySlotL = 0 -- used later
- local eqRight = ""
- local eqLeft = ""
- local count = 0
- local damage = 0
- if emptySlotR > 0 then -- empty slot found
- turtle.select(emptySlotR)
- if turtle.equipRight() then -- remove tool on the right
- count, eqRight, damage = clsTurtle.getSlotContains(self, emptySlotR) -- eqRight contains name of tool from right side
- emptySlotL = clsTurtle.getFirstEmptySlot(self) -- get next empty slot
- else
- emptySlotL = emptySlotR
- end
- if emptySlotL > 0 then -- empty slot found
- turtle.select(emptySlotL)
- if turtle.equipLeft() then
- count, eqLeft, damage = clsTurtle.getSlotContains(self, emptySlotL) -- eqLeft contains name of tool from left side
- end
- end
- if eqRight == "minecraft:crafting_table" then
- turtle.select(emptySlotR)
- turtle.equipRight()
- self.equippedRight = eqRight
- eqRight = ""
- elseif eqLeft == "minecraft:crafting_table" then
- turtle.select(emptySlotL)
- turtle.equipRight()
- self.equippedRight = eqLeft
- eqLeft = ""
- end
- if eqRight ~= "" then -- still contains a tool
- turtle.select(emptySlotR)
- turtle.equipLeft()
- self.equippedLeft = eqRight
- end
- if eqLeft ~= "" then
- turtle.select(emptySlotL)
- turtle.equipLeft()
- self.equippedLeft = eqLeft
- end
- end
- turtle.select(1)
- return self.equippedRight, self.equippedLeft
- end
- function clsTurtle.sortInventory(self)
- local turns = 0
- local chestSlot = clsTurtle.getItemSlot(self, "minecraft:chest", -1) --get the slot number containing a chest
- local blockType, blockModifier
- local facing = self.facing
- clsTurtle.saveToLog(self, "clsTurtle:sortInventory() started chest found in slot "..chestSlot, true)
- if chestSlot > 0 then -- chest found
- -- find empty block to place it.
- local chestDirection = clsTurtle.getPlaceChestDirection(self)
- blockType, blockModifier = clsTurtle.getBlockType(self, chestDirection)
- clsTurtle.saveToLog(self, "clsTurtle:sortInventory() looking "..chestDirection.." for chest...", true)
- while blockType ~= "minecraft:chest" do --check if chest placed eg mob in the way
- if clsTurtle.place(self, "minecraft:chest", -1, chestDirection) then
- clsTurtle.saveToLog(self, "clsTurtle:sortInventory() chest placed:"..chestDirection, true)
- break
- else
- clsTurtle.saveToLog(self, "clsTurtle:sortInventory() chest NOT placed:"..chestDirection, true)
- clsTurtle.dig(self, chestDirection) -- will force wait for mob
- chestDirection = clsTurtle.getPlaceChestDirection(self)
- end
- blockType, blockModifier = clsTurtle.getBlockType(self, chestDirection)
- end
- -- fill chest with everything
- clsTurtle.saveToLog(self, "clsTurtle:sortInventory() emptying turtle:"..chestDirection, true)
- for i = 1, 16 do
- clsTurtle.drop(self, i, chestDirection)
- end
- -- remove everything
- clsTurtle.saveToLog(self, "clsTurtle:sortInventory() refilling turtle:"..chestDirection, true)
- while clsTurtle.suck(self, chestDirection) do end
- clsTurtle.dig(self, chestDirection) -- collect chest
- clsTurtle.saveToLog(self, "clsTurtle:sortInventory() chest collected("..chestDirection..")", true)
- while facing ~= self.facing do --return to original position
- clsTurtle.turnLeft(self, 1)
- end
- end
- end
- function clsTurtle.suck(self, direction)
- direction = direction or "forward"
- turtle.select(1)
- local success = false
- if direction == "up" then
- if turtle.suckUp() then
- success = true
- end
- elseif direction == "down" then
- if turtle.suckDown() then
- success = true
- end
- else
- if turtle.suck() then
- success = true
- end
- end
- return success
- end
- clsTurtle.initialise(self)
- return self
- end
- -- local functions
- 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 = false
- -- 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
- local relX = x - coordHome:getX() -- eg facing e -400 - -407 = +7
- local relZ = z - coordHome:getZ() -- eg facing e 270 - 278 = -8 (behind)
- if coordHome:getFacing() == 0 then
- --xCoord = xCoord + 1
- if relZ >= 3 or relZ <= -18 or relX >= 5 or relX <= -12 then --too far behind / in front to left/right
- result = true
- else
- if relZ < 3 or relZ > -18 then
- if relX >= 5 or relX <= -12 then
- result = true
- end
- elseif relX < 5 or relX > -12 then
- if relZ >= 3 or relZ <= -18 then
- result = true
- end
- end
- end
- elseif coordHome:getFacing() == 1 then --facing west
- --xCoord = xCoord - 1
- if relX <= -3 or relX >= 18 or relZ <= -5 or relZ >= 12 then --too far behind / in front to left/right
- result = true
- else
- if relX > -3 or relX < 18 then
- if relZ <= -5 or relZ >= 12 then
- result = true
- end
- elseif relZ > -5 or relZ < 12 then
- if relX <= -3 or relX >= 18 then
- result = true
- end
- end
- end
- elseif coordHome:getFacing() == 2 then
- --zCoord = zCoord - 1
- if relZ <= -3 or relZ >= 18 or relX <= -5 or relX >= 12 then --too far behind / in front to left/right
- result = true
- else
- if relZ > -3 or relZ < 18 then
- if relX <= -5 or relX >= 12 then
- result = true
- end
- elseif relX > -5 or relX < 12 then
- if relZ <= -3 or relZ >= 18 then
- result = true
- end
- end
- end
- elseif coordHome:getFacing() == 3 then --facing east
- --xCoord = xCoord + 1
- if relX >= 3 or relX <= -18 or relZ >= 5 or relZ <= -12 then --too far behind / in front to left/right
- result = true
- else
- if relX < 3 or relX > -18 then
- if relZ >= 5 or relZ <= -12 then
- result = true
- end
- elseif relZ < 5 or relZ > -12 then
- if relX >= 3 or relX <= -18 then
- result = true
- end
- end
- end
- end
- return result
- end
- function clearBase()
- local goUp = 0
- T:saveToLog("clearBase() Starting...", true)
- emptyAfterHarvest() --empty trash
- --craft furnace
- T:craft("minecraft:furnace", 1, "minecraft:cobblestone", nil, nil, false)
- --clear area around first tree 5 X 5 square
- T:go("F1x0C2L1")
- for i = 1, 3 do
- T:go("F1x2*0")
- end
- T:go("F1x0*2L1")
- for i = 1, 2 do
- T:go("F1x0*2")
- end
- T:go("F1x0*2L1")
- for i = 1, 5 do
- T:go("F1x0*2")
- end
- T:go("F1x0*2L1F1x0*2")
- T:go("F1x0*2D2x2C2U2")
- T:go("F1x0*2L1F1x0*2L1F1x0x2C2F1x0x2C2R1")
- T:go("F1x0C2F1x0C2")
- T:go("F1x0D1C2F1x0C2R1F1x0C2R1F1x0C2")
- T:go("F1x0C2U1C2F1L1")
- --put furnace above
- turtle.select(T:getItemSlot("minecraft:furnace"))
- turtle.placeUp()
- end
- function clearTreeFarm()
- local stockData = {}
- stockData = T:getStock("minecraft:log", -1)
- local numLogs = stockData.total
- stockData = T:getStock("minecraft:torch")
- local numTorches = stockData.total
- local torchesNeeded = 10 - numTorches
- T:saveToLog("clearTreeFarm() starting", true)
- --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("R1F2D1R2")
- while turtle.suckDown() do end
- stockData = T:getStock("minecraft:sapling", -1)
- if stockData.total > 0 then
- T:go("U1F2R1F4R2")
- --get cobble/dirt
- while turtle.suckDown() do end
- T:go("F4L1F2L1U1")-- on water enclosure wall
- -- build outer wall
- for i = 1, 9 do
- T:go("C2+0-0F1")
- end
- T:turnRight(1)
- for i = 1, 9 do
- T:go("C2+0-0F1")
- end
- T:turnRight(1)
- for i = 1, 10 do
- T:go("C2+0-0F1")
- end
- T:turnRight(1)
- for i = 1, 9 do
- T:go("C2+0-0F1")
- end
- T:turnRight(1)
- T:go("C2+0-0F1R1F1D1L1")
- --clear ground within outer wall
- for i = 1, 8 do -- left side
- T:go("c0+0-0F1")
- end -- col 2, row 2
- T:go("R1C2F1")
- for j = 1, 4 do
- for i = 1, 6 do
- T:go("c0+0-0F1")
- end
- T:go("R1c0+0-0F1R1")
- for i = 1, 6 do
- T:go("c0+0-0F1")
- end
- T:go("L1c0+0-0F1L1")
- end
- for i = 1, 6 do
- T:go("c0+0-0F1")
- end -- col 9, row 9 on right side of farm
- -- channel across end of farm
- T:go("c0+0-0L1D1") -- stay at bottom end
- T:go("R1x1C1R1C1R1C1C2F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1C2L1C1R1F1x0C1R2F3U1L1F3")
- --place dirt and torches
- for i = 1, 4 do
- T:go("r0B1T0F4R1") --place dirt, Back, digUp, place Torch, forward 3, turn right
- end
- T:go("B2U3F2") --dodge torch on first block
- T:go("S2F3S2R1F3S2R1F3S2F3D3R1F8R1")
- treeFarm:setFarmCreated(true)
- treeFarm:setTimePlanted(os.time())
- saveStatus("treeFarm")
- emptyTurtle(false)
- else -- no saplings abandon tree farm
- T:go("U1F2R1")
- T:saveToLog("No saplings: treeFarm abandoned")
- end
- end
- function craftBucket()
- local stockData = {}
- local success = false
- -- called only when under furnace
- stockData = T:getStock("minecraft:bucket", -1)
- if stockData.total == 0 then
- stockData = T:getStock("minecraft:iron_ore", -1)
- if stockData.total >= 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 craftHopper()
- local doContinue = false
- local stockData = {}
- T:saveToLog("craftHopper() started")
- stockData = T:getStock("minecraft:iron_ore", -1)
- if stockData.total >= 5 then
- stockData = T:getStock("minecraft:chest")
- if stockData.total < 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("R1F4R2D2")
- T:place("minecraft:hopper", -1, "forward")
- T:go("U1C2U1F4R1")
- treeFarm:setHopperPlaced(true)
- saveStatus("treeFarm")
- end
- end
- T: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
- T:saveToLog("craftMiningTurtle() started")
- -- start under furnace / over chest1
- if numTurtles == 0 then
- term.clear()
- T: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()
- --use coal for fuel
- itemSlot = T:getItemSlot("minecraft:coal", -1)
- while itemSlot > 0 do
- turtle.select(itemSlot)
- turtle.refuel()
- itemSlot = T:getItemSlot("minecraft:coal", -1)
- end
- --empty turtle into correct storage
- T:dropItem("minecraft:stick", 0)
- T:forward(2) -- logs
- T:dropItem("minecraft:log", 0)
- T:dropItem("minecraft:log2", 0)
- T:forward(2) --dirt cobble
- T:dropItem("minecraft:cobblestone", 0)
- T:dropItem("minecraft:dirt", 0)
- T:forward(2) --sand reeds
- T:dropItem("minecraft:sand", 0)
- T:forward(2) --iron ore
- T:dropItem("minecraft:iron_ore", 0)
- T:dropItem("minecraft:bucket", 0)
- T:forward(2) --redstone
- T:dropItem("minecraft:redstone", 0)
- T:forward(2) -- diamond
- T: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
- T:dropItem(T:getItemName(i))
- end
- end
- end
- -- only chest(s) left behind in turtle
- itemSlot = T:getItemSlot("minecraft:chest", 0)
- if itemSlot ~= 1 then
- turtle.select(itemSlot)
- turtle.transferTo(1)
- end
- -- go back and remove supplies
- T:go("R2F2") -- diamonds
- turtle.select(1)
- while turtle.suckDown() do end
- itemSlot = T:getItemSlot("minecraft:diamond", 0)
- 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.total >= keepNum
- itemSlot = T:getItemSlot("minecraft:cobblestone")
- T:dropItem("minecraft:cobblestone", turtle.getItemCount(itemSlot) - keepNum)
- T:dropItem("minecraft:dirt", 0)
- T:forward(2) --logs
- while turtle.suckDown() do
- stockData = T:getStock("minecraft:log")
- if stockData.total >= logsNeeded then
- break
- end
- stockData = T:getStock("minecraft:log2")
- if stockData.total >= logsNeeded then
- break
- end
- end
- --empty furnace
- turtle.select(1)
- T:go("F2R2B1U1s1D1F1")
- itemSlot = T:getItemSlot("minecraft:planks")
- if itemSlot > 0 then
- stockData = T:getStock("minecraft:planks")
- if stockData.total >= 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.total == 0 then
- stockData = T:getStock("minecraft:log2")
- logType = "minecraft:log2"
- end
- if stockData.total < logsNeeded then --not enough logs on board
- harvestTreeFarm()
- stockData = T:getStock("minecraft:log")
- logType = "minecraft:log"
- if stockData.total == 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("R2F3R2")
- programTurtles(numTurtles)
- end
- function programTurtles(numTurtles)
- --program mining turtle
- turtle.select(T:getItemSlot(ccDiskDrive))
- turtle.place()
- turtle.select(T:getItemSlot(ccDisk))
- turtle.drop()
- T:go("U1F1") --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)
- T: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"
- T: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("B1D1R1F1L1")
- 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("F1R1U1")
- turtle.select(T:getItemSlot(ccCraftyMiningTurtle))
- turtle.place() --next to disk drive
- T:down(1)
- else
- T:go("F1R1")
- end
- term.clear()
- if numTurtles == 2 then
- T:saveToLog("Mission successful.\nI have replicated myself twice.\n\nRight-Click on my offspring to check their status...")
- else
- T:saveToLog("Mission partially successful.\nI have replicated myself once\n\nRight-Click on my offspring to check its status...")
- end
- 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.total < woodNeeded then
- stockData = T:getStock("minecraft:log2")
- if stockData.total >= woodNeeded then
- logType = "minecraft:log2"
- end
- end
- T:craft("minecraft:planks", numPlanksNeeded, logType, nil, nil, false)
- stockData = T:getStock("minecraft:stick")
- if stockData.total < 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.total > 0 then
- turtle.select(stockData.mostSlot)
- turtle.refuel()
- end
- stockData = T:getStock("minecraft:stick")
- if stockData.total > 0 then
- turtle.select(stockData.mostSlot)
- 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.total < planksNeeded then
- makePlanks = true
- else
- woodNeeded = 0 --reset
- end
- if makePlanks then --need wood
- stockData = T:getLogData()
- if stockData.total >= woodNeeded then
- T:craft("minecraft:planks", planksNeeded, stockData.mostName, 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 = {}
- T: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.total
- torchesNeeded = math.floor(torchesNeeded / 4) * 4 -- correct torchesNeeded to multiple of 4
- headQuantity = torchesNeeded / 4 -- coal, charcoal
- stockData = T:getStock("minecraft:planks", -1)
- planksInStock = stockData.total
- -- 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.total
- stockData = T:getStock("minecraft:coal", 1)
- charcoalInStock = stockData.total
- 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
- T:saveToLog("craftTorches("..torchesNeeded..") coal="..coalInStock..", charcoal="..charcoalInStock..", planksNeeded="..planksNeeded..", woodNeeded="..woodNeeded)
- -- amount of logs needed known
- stockData = T:getStock("minecraft:log", -1)
- if stockData.total < woodNeeded then
- stockData = T:getStock("minecraft:log2")
- if stockData.total >= 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.total < woodNeeded then
- stockData = T:getStock("minecraft:log2")
- if stockData.total >= woodNeeded then
- logType = "minecraft:log2"
- else -- not enough logs to make torches
- doContinue = false
- end
- else
- logType = "minecraft:log"
- end
- end
- end
- T: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.total < sticksNeeded then
- doContinue = false
- T:saveToLog("craftTorches("..torchesNeeded..") crafting sticks")
- if craftSticks(sticksNeeded) then
- doContinue = true
- end
- end
- end
- if doContinue then
- if makeCharcoal then
- doContinue = false
- T:saveToLog("craftTorches("..torchesNeeded..") smelting charcoal")
- if smelt(logType, headQuantity) then
- doContinue = true
- end
- end
- if doContinue then
- --make torches
- T: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", -1)
- if stockData.total > 0 then
- T:saveToLog("craftTorches("..torchesNeeded..") storing sticks")
- turtle.select(stockData.mostSlot)
- turtle.dropDown()
- end
- end
- return success
- end
- function createMine(status)
- -- initial excavation in centre complete.
- T:saveToLog("createMine() Starting...", true)
- if status == 5 then
- createMinePrepare(14)
- T:saveToLog("createMine() Starting Level 14", true)
- createMineLevel(14)
- status = 6
- T:saveToLog("createMine() Level 14 complete. Saving Status '6'", true)
- end
- if status == 6 then
- createMinePrepare(11)
- T:saveToLog("createMine() Starting Level 11", true)
- createMineLevel(11)
- status = 7
- T:saveToLog("createMine() Level 11 complete. Saving Status '7'", true)
- end
- if status == 7 then
- createMinePrepare(8)
- T:saveToLog("createMine() Starting Level 8", true)
- createMineLevel(8)
- status = 8
- T:saveToLog("createMine() Level 8 complete. Saving Status '8'", true)
- end
- return status
- end
- function createMinePrepare(level)
- local logsRequired = 0
- local stockData = T:getStock("minecraft:coal", -1) --either coal or charcoal
- local numCoal = stockData.total
- 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()
- local slot, modifier, total = T:getItemSlot("minecraft:chest", -1)
- if total < 2 and numLogs >= 2 then -- make 1 chest if 2 logs
- T:craftChests(1, true)
- numLogs = T:getLogStock()
- end
- slot, modifier, total = T:getItemSlot("minecraft:torch", -1)
- if total < 24 then
- if numLogs >= 4 or (numLogs >= 2 and numCoal >= 2) then
- craftTorches(24 - total) -- craft 24 torches 3 logs
- numLogs = T:getLogStock()
- end
- end
- if level == 14 then
- slot, modifier, total = T:getItemSlot("minecraft:sign", -1)
- if total < 1 and numLogs >= 2 then
- craftSigns(3) -- craft 3 signs 2 logs
- end
- end
- stockData = T:getStock("minecraft:log")
- if stockData.mostSlot ~= stockData.leastSlot then -- 2 types of log, so craft planks and refuel
- if stockData.leastCount <= 16 then
- T:craft("minecraft:planks", stockData.leastCount * 4, "minecraft:log", nil, nil, true)
- end
- end
- stockData = T:getStock("minecraft:sign")
- T:saveToLog("createMinePrepare("..level..") Signs avaiable: "..stockData.total, true)
- stockData = T:getStock("minecraft:torch")
- T:saveToLog("createMinePrepare("..level..") Torches avaiable: "..stockData.total, true)
- stockData = T:getStock("minecraft:chest")
- T:saveToLog("createMinePrepare("..level..") Chests avaiable: "..stockData.total - 1, true)
- end
- function createMineLevel(level)
- local tempYcoord = T:getY()
- local doContinue = false
- local blockType = ""
- local stockData = {}
- -- 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
- T:go("+0")
- while T:getY() >= tempYcoord - 1 do
- T:go("x1R1x1R1x1R1x1R1D1")
- end
- T:go("U1C1R1C1R1C1R1C1R1U1")
- if T:getItemSlot("minecraft:sign") > 0 then
- T:dig("forward")
- turtle.select(T:getItemSlot("minecraft:sign"))
- turtle.place("Diamond Mine\nPreparing for\nDeep Excavation\nLevel "..tostring(level))
- --dump remaining signs
- turtle.dropUp()
- end
- else
- T:dig("forward") -- 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 -- shaft mining, check for valuables
- for i = 1, 4 do
- if T:isValuable("forward") then
- mineItem(true, "forward")
- else
- blockType = T:getBlockType("forward")
- if blockType == "minecraft:water" or
- blockType == "minecraft:flowing_water" or
- blockType == "minecraft:lava" or
- blockType == "minecraft:flowing_lava" then
- T:go("C1")
- end
- end
- T:turnRight(1)
- end
- end
- end
- T:go("x2R2C1D1C1R2")
- --ready to create mine at level 14 37x37 square
- T:go("m32U1R2M16D1", true, 8) -- mine ground level, go up, reverse and mine ceiling to mid-point, drop to ground
- T:place("minecraft:chest", -1, "down") --place chest in ground
- T:up(1) -- ready for emptyTrash() which moves down/up automatically
- T:emptyTrash("down")
- T:go("D1R1m16U1R2M16", true, 8) -- mine floor/ceiling of right side branch
- T:emptyTrash("down")
- T:go("D1m16U1R2M16", true, 8) -- mine floor/ceiling of left side branch
- T:emptyTrash("down")
- T:go("L1M15F1R1D1", true, 8) -- mine ceiling of entry coridoor, turn right
- T:go("n16R1n32R1n32R1n32R1n16U1", true, 8)-- mine floor of 36 x 36 square coridoor
- T:go("R1F16R2") --return to centre
- T:emptyTrash("down")
- T:go("F16R1") --return to entry shaft
- T:go("Q16R1Q32R1Q32R1Q32R1Q16x0F1R1", true, 8) --mine ceiling of 36x36 square coridoor. return to entry shaft + 1
- -- get rid of any remaining torches
- while T:getItemSlot("minecraft:torch", -1) > 0 do
- turtle.select(T:getItemSlot("minecraft:torch", -1))
- turtle.drop()
- end
- for i = 1, 8 do
- T:go("N32L1F1L1", true)
- T:dumpRefuse()
- T:go("N32R1F1R1", true)
- T:dumpRefuse()
- end
- T:go("R1F1R2C1R2F17L1") -- close gap in wall, return
- for i = 1, 8 do
- T:go("N32R1F1R1", true)
- T:dumpRefuse()
- T:go("N32L1F1L1", true)
- T:dumpRefuse()
- end
- T:go("L1F1R2C1R2F16R1") -- close gap in wall, return
- --return to surface
- while T:getY() < mineEntrance:getY() do
- T:up(1)
- end --at surface now
- T:forward(16)
- T:turnRight(2)
- 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", -1)
- if stockData.total > 0 then
- turtle.select(stockData.leastSlot)
- turtle.refuel()
- end
- --sapling chest
- T:go("R1F2D1R2")
- while turtle.suckDown() do end --remove any junk in the sapling chest as well
- T:dropItem("minecraft:sapling", 0, "down")-- now only saplings in there
- T:go("U1F2R1")
- -- chest1 sticks only
- T:dropItem("minecraft:stick", 0, "down")
- T:forward(2) -- chest2 logs of any type, keep 1 stack
- T:dropItem("minecraft:log", 64, "down")
- T:dropItem("minecraft:log2", 64, "down")
- T:forward(2) -- chest3 dirt and cobble
- if not keepAll then -- keep max 1 stack cobble
- T:dropItem("minecraft:cobblestone", 64, "down")
- T:dropItem("minecraft:dirt", 64, "down")
- end
- T:forward(2) -- chest4 sand and reeds
- T:dropItem("minecraft:sand", 0, "down")
- T:dropItem("minecraft:reeds", 0, "down")
- T:forward(2) -- chest5 iron ore only
- T:dropItem("minecraft:iron_ore", 64, "down")
- T:forward(2) -- redstone
- T:dropItem("minecraft:redstone", 0, "down")
- 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:go("R2F14R2")
- end
- function fillPond()
- local doContinue = false
- local fileHandle = ""
- local strText = ""
- local waterFound = false
- local numArms = 0
- local startFile = ""
- local itemSlot = 0
- local stockData = {}
- T:saveToLog("fillPond() started", true)
- -- reset turtle coordinates from file in case error has occurred with current location
- getCoords(true)
- -- 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()
- T:saveToLog("Water coords from file x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
- end
- --T:sortInventory()
- stockData = T:getStock("minecraft:bucket", -1)
- if stockData.total == 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() -- calls safeRun which constantly checks for 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, "minecraft:water")
- T:turnRight(1)
- safeRun(1, "minecraft:water")
- 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, "minecraft:water")
- T:turnRight(1)
- safeRun(i, "minecraft:water")
- T:turnRight(1)
- safeRun(i + 1, "minecraft:water")
- T:turnRight(1)
- if i + 1 < numArms then
- safeRun(i + 1, "minecraft:water")
- T:turnRight(1)
- else
- --return to starting position
- safeRun(i / 2, "minecraft:water")
- T:turnRight(1)
- safeRun(i / 2 + 1, "minecraft:water")
- 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", -1)
- if itemSlot > 0 then
- treeFarm:setWaterFound(true)
- returnHome()
- T:go("L1F3")
- turtle.select(T:getItemSlot("minecraft:water_bucket"))
- if turtle.placeDown() then
- T:saveToLog("Water added to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- T:go("R2F3L1")
- else
- T:go("L1F1L1F1")
- if turtle.placeDown() then
- T:saveToLog("Water added (after moving) to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- T:go("L1F1R1F2L1")
- end
- goToWater()
- returnHome()
- T:go("L1F2L1F1")
- itemSlot = T:getItemSlot("minecraft:water_bucket")
- if itemSlot > 0 then
- turtle.select(itemSlot)
- if turtle.placeDown() then
- T:saveToLog("Water added to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- T:go("R2F1R1F2L1")
- else
- T:go("R1F1R1F1")
- if turtle.placeDown() then
- T:saveToLog("Water added (after moving) to pond at: x="..T:getX()..", y="..T:getY()..", z="..T:getZ(),true)
- end
- T:go("R1F3L1")
- end
- else
- returnHome()
- 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()
- T: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
- T:dig("forward")
- 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)
- end
- function floodTreeFarm()
- local slot = 0
- local success = true
- T:saveToLog("floodTreeFarm() started", true)
- T:go("L1F2R2") -- go to pond
- for i = 1, 9, 2 do --flood farm except for drainage channel
- slot = T:getItemSlot("minecraft:bucket")
- if slot > 0 then
- turtle.select(slot)
- turtle.placeDown() -- Fill 1
- end
- if i < 9 then
- T:forward(4 + i)
- else
- T:forward(12) -- far right side of farm
- end
- T:go("L1F8U1") --T:go("L FFFF FFFF U")
- slot = T:getItemSlot("minecraft:water_bucket")
- if slot > 0 then
- turtle.select(slot)
- turtle.placeDown()
- end
- T:turnLeft(1)
- if i < 9 then -- return to left wall of farm
- T:forward(i)
- else
- T:forward(8)
- end
- T:go("L1F8D1R1F4R2")
- -- ready for next bucket fill
- end
- slot = T:getItemSlot("minecraft:bucket")
- if slot > 0 then
- turtle.select(slot)
- turtle.placeDown()
- end
- T:forward(12)
- slot = T:getItemSlot("minecraft:water_bucket")
- if slot > 0 then
- turtle.select(slot)
- turtle.placeDown()
- T:saveToLog("floodTreeFarm() completed")
- else
- T:saveToLog("floodTreeFarm() failed: no water obtained", true)
- success = false
- end
- T:go("R2F10R1")
- treeFarm:setFarmFlooded(true)
- saveStatus("treeFarm")
- return success
- end
- function getCoords(fromFile)
- fromFile = fromFile or false
- --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()
- T:saveToLog("Coordinates loaded from file:", true)
- T:saveToLog("x = "..T:getX()..", y = "..T:getY()..", z = "..T:getZ()..", f = "..T:getFacing(), true)
- if not fromFile then
- print()
- print(os.getComputerLabel().." assumed to be at Home Location")
- print()
- print("starting in 3 seconds")
- print("STAND CLEAR!")
- os.sleep(3)
- end
- 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")
- 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()
- T:saveToLog("homeCoords.txt file created", true)
- T: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
- if not fromFile then
- term.clear()
- term.setCursorPos(1,1)
- print("Here we go...")
- sleep(2)
- term.clear()
- term.setCursorPos(1,1)
- end
- 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:go("R2F"..moves.."R2")
- return success
- end
- function goToWater()
- local startX = T:getX()
- local startZ = T:getZ()
- local itemSlot = 0
- local waterCollected = false
- local nearHome = true
- T:saveToLog("goToWater() started.", true)
- if T:getX() < water:getX() then
- while T:getFacing() ~= 3 do
- T:turnRight(1)
- end
- while T:getX() < water:getX() do
- if nearHome then
- T:forward(5)
- nearHome = false
- else
- safeRun(1, "minecraft:water")
- if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
- waterCollected = true
- T: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 T:getX() > water:getX() then
- while T:getFacing() ~= 1 do
- T:turnRight(1)
- end
- while T:getX() > water:getX() do
- if nearHome then
- T:forward(5)
- nearHome = false
- else
- safeRun(1, "minecraft:water")
- if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
- waterCollected = true
- T: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
- end
- if T:getZ() > water:getZ() then -- go north
- while T:getFacing() ~= 2 do
- T:turnRight(1)
- end
- while T:getZ() > water:getZ() do
- if nearHome then
- T:forward(5)
- nearHome = false
- else
- safeRun(1, "minecraft:water")
- if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
- waterCollected = true
- T: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
- elseif T:getZ() < water:getZ() then
- while T:getFacing() ~= 0 do
- T:turnRight(1)
- end
- while T:getZ() < water:getZ() do
- if nearHome then
- T:forward(5)
- nearHome = false
- else
- safeRun(1, "minecraft:water")
- if T:getItemSlot("minecraft:water_bucket", -1) > 0 then
- waterCollected = true
- T: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
- if not waterCollected then
- T:saveToLog("goToWater() completed: no water", true)
- else
- T:saveToLog("goToWater() completed: water collected", true)
- end
- 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
- T: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
- T:saveToLog("harvestAllTrees() spiral arm no: "..i, true)
- stockData = T:getStock("minecraft:reeds")
- numReeds = stockData.total
- stockData = T:getStock("minecraft:sand")
- numSand = stockData.total
- stockData = T:getStock("minecraft:sapling")
- numSaplings = stockData.total
- numLogs = T:getLogStock()
- waterFound = treeFarm:getWaterFound()
- T:saveToLog("reeds = "..numReeds..", sand = "..numSand..", logs = "..numLogs..", Water found = "..tostring(waterFound),true)
- print("harvestAllTrees() spiral arm no: "..i)
- 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
- T: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)
- end
- end
- function harvestRun(runLength, digDirt)
- local itemSlot = 0
- local logType = ""
- local startFile = ""
- local success = false
- local blockType = ""
- local blockModifier
- turtle.select(1)
- for i = 1, runLength do
- while not turtle.inspectDown() do -- must be air below
- turtle.suckDown() --pick any saplings
- T:down(1)
- end
- blockType, blockModifier = T:getBlockType("down")
- if blockType == "minecraft:sand" then
- itemSlot, blockModifier, total = T:getItemSlot("minecraft:sand", -1)
- if total < 6 then
- harvestSand()
- end
- elseif blockType == "minecraft:flowing_water" or blockType == "minecraft:water" then
- if blockModifier == 0 then -- source block
- if water:getX() == 0 then -- water coords not yet found
- --T:saveToLog("Water source found: checking suitability", true)
- --local relX = math.abs(math.abs(T:getX()) - math.abs(coordHome:getX()))
- --local relZ = math.abs(math.abs(T:getZ()) - math.abs(coordHome:getZ()))
- T:saveToLog("Water found, checking suitability: x="..T:getX()..", z="..T:getZ(), true)
- --if relX > 5 or 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)
- T:saveToLog("Water confirmed and saved to file x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
- else
- T:saveToLog("Water too close to base camp x="..water:getX()..", y="..water:getY()..", z="..water:getZ(), true)
- end
- --else
- --T:saveToLog("Water found: too close to base camp (rel X="..relX..", rel Z="..relZ..")", true)
- --end
- end
- end
- end
- blockType, blockModifier = T:getBlockType("up")
- if blockType == "minecraft:log" or blockType == "minecraft:log2" then
- T:back(1)
- T:harvestTree()
- end
- if digDirt and i < 5 then
- turtle.select(1)
- if T:getY() == coordHome:getY() then
- T:dig("up")
- T:dig("down")
- end
- end
- turtle.suck()
- if turtle.detect() then --can't move forward
- blockType, blockModifier = T:getBlockType("forward")
- if blockType == "minecraft:log" then
- T:harvestTree()
- T:back(1)
- elseif blockType == "minecraft:reeds" then
- T:saveToLog("Reeds found", true)
- while blockType == "minecraft:reeds" do -- continue loop while reeds detected in front
- T:up(1) -- Move up
- blockType, blockModifier = T:getBlockType("forward")
- end
- -- At top of the Reeds/Sugar Cane.
- T:forward(1)
- -- New loop to return to ground
- blockType, blockModifier = T:getBlockType("down")
- while blockType == "minecraft:reeds" do -- While sugar cane detected below
- T:down(1)
- blockType, blockModifier = T:getBlockType("down")
- end
- T:back(1)
- -- check if leaves or grass in front, dig through
- else --any block except log or reeds
- if T:isVegetation(blockType) then -- tallgrass,flower, vine etc
- T:dig("forward")
- else
- while turtle.detect() do
- T:up(1)
- blockType, blockModifier = T:getBlockType("forward")
- if T:isVegetation(blockType) then -- tallgrass,flower, vine etc
- break
- elseif blockType == "minecraft:log" or blockType == "minecraft:log2" then
- T:harvestTree()
- T:back(1)
- break
- end
- end
- end
- end
- end
- T:forward(1)
- -- check if wood below eg tree harvest started mid - trunk
- blockType, blockModifier = T:getBlockType("down")
- while blockType == "minecraft:log" or blockType == "minecraft:log2" do
- T:down(1)
- blockType, blockModifier = T:getBlockType("down")
- end
- end
- end
- function harvestSand()
- local depth = 0
- T:saveToLog("harvestSand() started", true)
- local data = T:getBlockType("down")
- while data == "minecraft:sand" do -- While sand detected below
- T:down(1)
- depth = depth - 1
- data = T:getBlockType("down")
- end
- while depth < 0 do
- T:up(1)
- depth = depth + 1
- T:place("minecraft:dirt", -1, "down")
- end
- T:saveToLog("harvestSand() completed", true)
- end
- function harvestAndReplant()
- if T:isValuable("forward") then -- log in front
- T:harvestTree()
- else
- turtle.select(1)
- T:forward(1) --sapling harvested if still present
- end
- itemSlot = T:getItemSlot("minecraft:sapling", -1)
- 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 harvestTreeFarm()
- local itemSlot = 0
- T:saveToLog("harvestTreeFarm() started", true)
- treeFarm:reset()
- T:go("R1F2D1")
- turtle.select(1)
- while turtle.suckDown() do end-- get saplings from chest
- T:go("U3F3L1F2")
- harvestAndReplant()
- T:forward(1)
- harvestAndReplant()
- T:go("R1F3R1")
- harvestAndReplant()
- T:forward(1)
- harvestAndReplant()
- T:go("F2R1F6D3")
- --store any remaining saplings
- T:dropItem("minecraft:sapling", 0)
- T:go("U1F2R1")
- --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")
- T:saveToLog("harvestTreeFarm() completed", true)
- end
- function initialise()
- -- create classes/objects
- T = classTurtle()
- coordHome = classCoord("homeLocation")
- mineEntrance = classCoord("mineEntrance")
- water = classCoord("water")
- treeFarm = classTreeFarm()
- loadStatus("treeFarm")
- 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
- strText = "0"
- if fs.exists(obj) then
- fileHandle = fs.open(obj, "r")
- strText = fileHandle.readLine()
- fileHandle.close()
- end
- end
- return strText
- 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
- T:dig("up")
- 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
- T:dig("up")
- 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(status)
- local returnPath = ""
- local stockData = {}
- T:saveToLog("mineToBedrock() started", true)
- -- start at furnace
- T:sortInventory()
- emptyTurtle(false)
- 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\nSector "..status)
- turtle.select(1)
- end
- mineEntrance:setX(T:getX())
- mineEntrance:setY(T:getY())
- mineEntrance:setZ(T:getZ())
- mineEntrance:setFacing(T:getFacing())
- T:turnRight(2)
- while T:getY() > 8 do
- T:down(1)
- end
- -- now at base of shaft, above torch, level 7
- while status < 12 do -- break when status = 12 or enough diamonds are found
- stockData = T:getStock("minecraft:diamond", 0) -- check numbers
- if stockData.total >= 6 then
- break
- end
- if status <= 8 then -- no sectors mined
- returnPath = "L1U1F15L1"
- T:go("F1L1F1R1D1")
- status = 9
- elseif status <= 9 then -- lower left sector mined: do upper left
- returnPath = "L1U1F15R1F16R2"
- T:go("F17L1F1R1D1")
- status = 10
- elseif status <= 10 then -- both left sectors mined: do lower right
- returnPath = "R1U1F1R1"
- T:go("R1F15L1F1D1")
- status = 11
- elseif status <= 11 then -- both left + lower right sectors mined: do upper right
- returnPath = "U1F16R1F1R1"
- T:go("F16R1F15L1F1D1")
- status = 12
- end
- for i = 1, 4 do
- T:go("Z7U1L1F2L1F1D1Z7")
- if i < 4 then
- T:go("U1R1F2R1F1D1") --ready for next run
- else
- T:go(returnPath) -- return to shaft
- end
- T:dumpRefuse()
- end
- end
- -- go home
- while T:getY() < mineEntrance:getY() do
- T:up(1)
- end --at surface now
- T:forward(16)
- T:turnRight(2)
- return status
- 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
- T:saveToLog("placeStorage() Starting...", true)
- T:craftChests(9, false)
- --remove furnace at start
- T:go("x0R1F2D1x2H2U1R2F2R1")
- T:go("H2F1x0C2")
- for i = 2, 8 do
- T:go("F1x0H2F1x0C2")
- end
- T:go("F1x0C2R1F1x0C2R1")
- for i = 1, 14 do
- T:go("F1x0C2")
- end
- T:go("R1F2R1x0C2")
- for i = 1, 14 do
- T:go("F1x0C2")
- end
- T:go("R1F1L1R2F16R2")
- --put furnace back
- turtle.select(T:getItemSlot("minecraft:furnace"))
- turtle.placeUp()
- end
- function relocateHome()
- local numArms = 4
- local gotHome = false
- for i = 2, numArms, 2 do
- if safeRun(i, "minecraft:furnace") then --furnace found
- gotHome = true
- break
- else
- T:turnRight(1)
- if safeRun(i, "minecraft:furnace") then
- gotHome = true
- break
- else
- T:turnRight(1)
- if safeRun(i + 1, "minecraft:furnace") then
- gotHome = true
- break
- else
- T:turnRight(1)
- if i + 1 < numArms then
- if safeRun(i + 1, "minecraft:furnace") then
- gotHome = true
- break
- else
- T:turnRight(1)
- end
- else
- --return to starting position
- safeRun(i / 2, "minecraft:furnace")
- T:turnRight(1)
- safeRun(i / 2 + 1, "minecraft:furnace")
- end
- end
- end
- end
- end
- return gotHome
- end
- function returnHome()
- local startX = T:getX()
- local startZ = T:getZ()
- T:saveToLog("returnHome() started", true)
- if T:getX() <= coordHome:getX() then
- while T:getFacing() ~= 3 do
- T:turnRight(1)
- end
- while T:getX() < coordHome:getX() do
- safeRun(1, "minecraft:furnace")
- 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, "minecraft:furnace")
- end
- end
- 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, "minecraft:furnace")
- end
- elseif T:getZ() < coordHome:getZ() then
- while T:getFacing() ~= 0 do
- T:turnRight(1)
- end
- while T:getZ() < coordHome:getZ() do
- safeRun(1, "minecraft:furnace")
- end
- end
- while T:getFacing() ~= coordHome:getFacing() do
- T:turnRight(1)
- end
- if T:getBlockType("up") == "minecraft:furnace" then
- T:saveToLog("returnHome() completed", true)
- getCoords(true)
- else
- if relocateHome() then
- T:saveToLog("returnHome() completed", true)
- getCoords(true)
- else
- T:saveToLog("returnHome() failed: I am lost!", true)
- error()
- end
- end
- end
- function safeRun(runLength, actionBlock)
- actionBlock = actionBlock or ""
- local itemSlot = 0
- local success = false
- local doContinue = true
- local blockType, modifier
- turtle.select(1)
- for i = 1, runLength do
- if actionBlock == "minecraft:furnace" then
- blockType, modifier = T:getBlockType("up")
- if blockType == "minecraft:furnace" then
- success = true
- doContinue = false
- break
- end
- elseif actionBlock == "minecraft:water" then
- blockType, modifier = T:getBlockType("down")
- if blockType == "minecraft:flowing_water" or blockType == "minecraft:water" then
- if T:getItemSlot("minecraft:bucket", -1) > 0 then --water not yet found
- if T:place("minecraft:bucket", -1, "down") then
- T:saveToLog("Water bucket filled x="..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
- success = true
- else
- T:saveToLog("Water found, but not source block "..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
- end
- end
- end
- end
- if doContinue then
- if turtle.detect() then --can't move forward
- blockType, modifier = T:getBlockType("forward")
- if blockType == "minecraft:leaves" or blockType == "minecraft:tallgrass" or blockType == "minecraft:double_plant" then
- turtle.dig()
- else -- not veg so climb up
- while turtle.detect() do
- T:up(1)
- blockType, modifier = T:getBlockType("forward")
- if blockType == "minecraft:leaves"
- or blockType == "minecraft:tallgrass"
- or blockType == "minecraft:double_plant" then
- turtle.dig()
- break
- end
- end
- end
- end
- T:forward(1)
- while not turtle.inspectDown() do -- must be air below
- T:down(1)
- end
- if actionBlock == "minecraft:furnace" then
- blockType, modifier = T:getBlockType("up")
- if blockType == actionBlock then
- success = true
- break
- end
- elseif actionBlock == "minecraft:water" then
- blockType, modifier = T:getBlockType("down")
- if blockType == "minecraft:flowing_water" or blockType == "minecraft:water" then
- if T:getItemSlot("minecraft:bucket", -1) > 0 then --water not yet found
- if T:place("minecraft:bucket", -1, "down") then
- T:saveToLog("Water bucket filled x="..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
- success = true
- else
- T:saveToLog("Water found, but not source block "..T:getX()..", y="..T:getY()..", z="..T:getZ(), true)
- end
- end
- end
- end
- end
- end
- return success
- end
- function saveStatus(text)
- local fileHandle = ""
- T:saveToLog("Saving Status: '"..text.."'", true)
- if text == "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(text)
- end
- fileHandle.close()
- 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
- T: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 = {}
- local logData = T:getLogData() --{.total, .mostSlot, .mostCount, .mostModifier, mostName, .leastSlot, .leastCount, .leastModifier, leastName}
- stockData = T:getStock("minecraft:planks", -1)
- if stockData.total > 0 then
- turtle.select(stockData.mostSlot)
- turtle.refuel()
- if stockData.leastSlot ~= stockData.mostSlot then
- turtle.select(stockData.leastSlot)
- turtle.refuel()
- end
- end
- woodAvailable = logData.total
- planksNeeded = quantity
- if oreType == "minecraft:log" then
- woodNeeded = quantity + math.ceil(quantity / 4)
- end
- if woodNeeded > woodAvailable then
- getItemFromStorage("minecraft:log", false)
- logData = T:getLogData()
- woodAvailable = logData.total
- end
- T:go("B1U2F1") --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("B1D1") --in front of furnace, remove any existing planks and refuel
- turtle.select(16)
- if turtle.suck() then
- turtle.refuel()
- 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, logData.mostName, nil, nil, false)
- T:turnLeft(1)
- turtle.select(T:getItemSlot("minecraft:planks"))
- turtle.drop() --drop planks into furnace
- T:go("D1F1") --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, -1) > 0 then
- success = true
- T: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
- T:saveToLog("waiting for tree farm to grow. potential harvest = "..woodGrowing.." from "..maxWood, true)
- if woodGrowing >= maxWood then
- break
- end
- end
- harvestTreeFarm()
- end
- end
- function main()
- local functionList = {"T:harvestFirstTree()", "harvestAllTrees()", "findCobble()", "clearBase()", "placeStorage()",
- "createMine(14)", "createMine(11)", "createMine(8)",
- "mineToBedrock(1/4)", "mineToBedrock(2/4)", "mineToBedrock(3/4)",
- "mineToBedrock(4/4)", "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(false) -- Get/load current coordinates from player
- local status = tonumber(loadStatus("currentStatus.txt")) -- if continuing from previous run (debugging aid)
- T:saveToLog("Starting at function "..functionList[status + 1], true)
- if status == 0 then
- T:harvestTree(true) -- harvest first tree and make chest
- T:saveToLog("harvestFirstTree() Completed.", true)
- status = 1
- saveStatus(tostring(status))
- end
- if status <= 1 then
- harvestAllTrees() -- harvest remaining trees in 33 x 33 square
- T:saveToLog("harvestAllTrees() Completed.", true)
- status = 2
- saveStatus(tostring(status))
- end
- if status <= 2 then
- findCobble() -- get cobble and any incidental coal or iron ore
- T:saveToLog("findCobble() Completed.", true)
- status = 3
- saveStatus(tostring(status))
- end
- if status <= 3 then
- clearBase() -- create cobble base station including 2x2 pond area
- T:saveToLog("clearBase() Completed.", true)
- status = 4
- saveStatus(tostring(status))
- end
- if status <= 4 then
- placeStorage() -- craft chests and place in ground for storage
- T:saveToLog("placeStorage() Completed.", true)
- status = 5
- saveStatus(tostring(status))
- end
- -- status 5 = no mine yet; status 6 = mined level 14/13; status 7 = mined level 11/10; status 8 = mined level 8/7
- if status <= 7 then
- status = createMine(status) -- create basic 3 layer mine minimal structure and collect iron ore
- saveStatus(tostring(status))
- end
- local stockData = T:getStock("minecraft:iron_ore") -- check enough iron ore found
- local numIronOre = stockData.total
- stockData = T:getStock("minecraft:diamond", 0)
- if stockData.total < 6 or numIronOre < 14 then
- if status <= 11 then
- if stockData.total < 6 then -- 3 levels mined out, need more
- status = mineToBedrock(status) -- 4 stages: status 9, 10, 11, 12
- saveStatus(tostring(status))
- end
- end
- end
- if status <= 12 then
- stockData = T:getStock("minecraft:diamond", 0)
- if stockData.total < 3 then
- craftMiningTurtle(0)
- elseif stockData.total < 6 then
- craftMiningTurtle(1)
- else
- craftMiningTurtle(2)
- end
- end
- end
- --*********************Program runs from here*****************
- main()
Add Comment
Please, Sign In to add comment