Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Logger 2.0
- --A complete redesign of my earlier logger program
- --Goal designs
- --A startup program
- --Uses harvested wood to refuel
- --Session persistence
- --Interfaces with the same enderchest for supply and deposit, or maybe not because openperipherals is being weird
- local version = "2.4"
- local tArgs = {...}
- local tree = tArgs[1]
- local fuelReq
- local bChest = 1
- local bMealSlot = 2
- local compSlot = 4
- local sapSlot = 3
- local data = {}
- local fuelBurn = 15 -- computercraft figures for wooden logs fuel value
- local maxIT
- local position = "home"
- local treeSupport = {"birch", "rubber", "spruce", "force tree", "dark wood", "magic wood", "fir tree", "apple tree", "origin tree", "maple", "jacaranda", "autumn trees"}
- local truth = nil
- local countDown
- local treeCount
- ----
- function checkInv()
- for i = 1,3 do
- local slot = math.floor((i*1.2)+0.6)
- if turtle.getItemCount(slot) == 0 then
- resetScreen(1,1)
- fs.delete("saveData")
- error("Required Items Not Detected In Slot: "..slot)
- end
- end
- end
- function save(data)
- data = textutils.serialize(data)
- local file = fs.open("saveData", "w")
- file.write(data)
- file.close()
- end
- function load(str)
- local file = fs.open(str, "r")
- local data = file.readAll()
- file.close()
- return data
- end
- function setup()
- resetScreen(1,2)
- centre("CasLogger, Version: "..version)
- newLine(1,2)
- print("Welcome to the CasLogger configuration screen")
- newLine(2,0)
- write("Please enter your preferred settings:")
- newLine(1,2)
- write("What tree are you farming? ")
- local f,g = term.getCursorPos()
- newLine()
- write("Controlled loop or infinite loop?\n")
- write("Please enter either \"c\" or \"i\"")
- local h,j = term.getCursorPos()
- term.setCursorPos(f,g)
- tree = read()
- term.setCursorPos(h,j)
- loop = read()
- if loop == "c" then
- screenClear(g)
- term.setCursorPos(1,g)
- print("You have requested a for loop")
- write("Please enter your desired maximum iteration. ")
- maxIt = read()
- end
- newLine()
- centre("Thank you")
- newLine()
- centre("Press any key to continue")
- os.pullEvent("key")
- resetScreen(1,1)
- newLine(3,1)
- print([["REMINDER: This setup relies on conveyor belts and and an item collector to collect saplings.
- Surround the planting square with at least a 9*9 of conveyor belts, all pointing to an item collector.
- The collector should be behind the turtle. The planting square must be in front of the turtle.
- Put the import chest below the turtle."]])
- os.pullEvent("key")
- resetScreen(1,2)
- centre("CasLogger, Version: "..version)
- newLine(1,2)
- tab(2)
- print("Instructions:")
- tab()
- print("1: Place a bonemeal enderchest in slot one.")
- tab()
- print("2: Leave slot two vacant.")
- tab()
- print("3: Place saplings in slot three.")
- tab()
- print("4. Put one comparison log of the same type as the tree you are harvesting in slot 4")
- newLine()
- os.pullEvent("key")
- resetScreen(1,3)
- centre("Set up complete")
- newLine(1,2)
- centre("Thank you for using")
- newLine()
- centre("CasLogger Version "..version)
- newLine(1,2)
- print("In future, should you wish to change your settings, launch the program in the command line with the argument \"reset\". Alternatively, dynamically change the tree type by changing the argument to a tree type")
- centre("Press any key to launch program")
- os.pullEvent("key")
- checkInv()
- maxIt = tonumber(maxIt)
- treeCount = -1
- data = {tree, loop, maxIt, "home", treeCount}
- data = textutils.serialize(data)
- save(data)
- end
- function returnHome()
- data = load("saveData")
- data = textutils.unserialize(data)
- print(data[4])
- local condition = data[4]
- if condition == "sapPlant" then
- if fertilise() == false then
- repeat
- --local interval = 300
- sleep(300)
- turtle.select(compSlot)
- local checkIt = turtle.compare()
- until checkIt == true
- end
- harvest()
- cleanInv()
- autoRefuel()
- deposit()
- elseif condition == "enderUp" then
- turtle.select(1)
- turtle.digUp()
- if fertilise() == false then
- repeat
- --local interval = 300
- sleep(300)
- turtle.select(compSlot)
- local checkIt = turtle.compare()
- until checkIt == true
- end
- harvest()
- cleanInv()
- autoRefuel()
- deposit()
- elseif condition == "harvestUp" then
- truth = "sauce"
- harvest(truth)
- cleanInv()
- autoRefuel()
- deposit()
- elseif condition == "harvestFin" then
- while turtle.detectDown~= true do
- turtle.down()
- end
- turtle.back()
- elseif condition == "deposit" then
- cleanInv()
- autoRefuel()
- deposit()
- end
- end
- function screenClear(mini, maxi) -- This will wipe every line between mini and maxi, numbers which represent y level. If you leave blank then it will clear the whole screen
- if maxi == nil then
- local x,y = term.getSize()
- maxi = y
- end
- for i = mini, maxi do
- term.setCursorPos(1,i)
- term.clearLine()
- end
- end
- function newLine(num1, num2) -- starts a new line at a specified x value, or if x isn't specified x = 1. The optional num2 allows you to go down y by more than 1.
- if num1 == nil then
- num1 = 1
- end
- if num2 == nil then
- num2 = 1
- end
- local x,y = term.getCursorPos()
- term.setCursorPos(num1, y+num2)
- end
- function resetScreen(x,y) -- Clears the screen and sets Cursor position all in one go. Arguments are numbers, x coordinate and y coordinate. If they are not defined then they both default to 1
- if x == nil then
- x = 1
- end
- if y == nil then
- y = 1
- end
- term.clear()
- term.setCursorPos(x,y)
- end
- function centre(str) -- pass a string to centre and it will write it to the centre of the screen
- local x,y = term.getCursorPos()
- local mx, my = term.getSize()
- x = math.floor((mx/2)-(string.len(str)/2))
- term.setCursorPos(x,y)
- write(str)
- end
- function tab(num)
- if num == nil then
- num = 3
- end
- local x,y = term.getCursorPos()
- term.setCursorPos(x+num, y)
- end
- function treeHelp()
- function returnMenu()
- print("Options")
- tab(2)
- print("Press enter to return to tree help")
- tab(2)
- print("Press q to quit the program")
- tab(2)
- print("press r to return to main program")
- local key = os.pullEvent("key")
- return key
- end
- term.clear()
- term.setCursorPos(1,1)
- print("List of tree Support")
- local x,y = term.getSize()
- local support = {Birch, Spruce, Rubber}
- for i,v in pairs(support) do
- print(v)
- if i >= (y-1) then
- write("Press any key to continue")
- os.pullEvent("key")
- term.clearLine()
- end
- end
- end
- function treeType(tree)
- local height
- tree = string.lower(tree)
- if tree == "spruce" or tree == "dark wood" or tree == "fir tree" then
- height = 17
- elseif tree == "birch" or tree == "rubber" or tree == "apple" or tree == "origin" or tree == "maple" then
- height = 7
- elseif tree == "force" then
- height = 8
- elseif tree == "jacaranda" then
- height = 4
- elseif tree == "magic wood" then
- height = 30
- elseif tree == "autumn" then
- height = 9
- else
- print("Unrecognised tree: Setting Default fuel consumption")
- height = 20
- end
- fuelReq = math.ceil(((2*height)+2)+3)
- return fuelReq
- end
- function autoRefuel()
- fuelReq = treeType(tree)
- if turtle.getFuelLevel() <= fuelReq then
- fuelReq = math.ceil(fuelReq/fuelBurn)
- local iterator = 5
- local progress = 0
- repeat
- turtle.select(iterator)
- if turtle.compareTo(compSlot) == true then
- local measure = turtle.getItemCount(iterator)
- progress = measure + progress
- if measure > fuelReq then
- measure = fuelReq
- end
- turtle.refuel(measure)
- end
- iterator = iterator+1
- until progress >= fuelReq
- end
- end
- function fertilise()
- local checker = 4
- if turtle.getItemCount(bMealSlot) <= 6 then
- data = {tree, loop, maxIt, "enderUp", treeCount}
- save(Data)
- turtle.digUp()
- turtle.select(bChest)
- turtle.placeUp()
- turtle.select(bMealSlot)
- turtle.suckUp(32)
- turtle.select(bChest)
- turtle.digUp()
- data = {tree, loop, maxIt, "sapPlant", treeCount}
- save(data)
- end
- local iterator = 1
- if turtle.getItemCount == 0 then
- print("Slow Mode")
- return false
- else
- repeat
- turtle.select(bMealSlot)
- turtle.place()
- turtle.select(compSlot)
- if turtle.compare() == true then
- checker = true
- end
- iterator = iterator+1
- local slotCheck = turtle.getItemCount(bMealSlot)
- until checker == true or iterator == 6 or slotCheck == 0
- end
- return true
- end
- function plant()
- turtle.select(sapSlot)
- turtle.place()
- data = {tree, loop, maxIt, "sapPlant", treeCount}
- save(data)
- end
- function harvest(truth)
- turtle.select(compSlot)
- if truth == nil then
- repeat
- if turtle.compare() == false then
- sleep(15)
- end
- until turtle.compare() == true
- data = {tree, loop, maxIt, "forward", treeCount}
- save(data)
- turtle.dig()
- turtle.forward()
- end
- truth = nil
- data = {tree, loop, maxIt, "harvestUp", treeCount}
- save(data)
- while turtle.compareUp() do
- turtle.digUp()
- turtle.up()
- end
- data = {tree, loop, maxIt, "harvestUp", treeCount}
- save(data)
- while turtle.detectDown() == false do
- turtle.down()
- end
- turtle.back()
- data = {tree, loop, maxIt, "deposit", treeCount}
- save(data)
- end
- function cleanInv()
- local compCheck = turtle.getItemCount(compSlot)
- if compCheck > 1 then
- turtle.select(compSlot)
- iterator = 5
- repeat
- turtle.transferTo(iterator, (compCheck-1))
- iterator = iterator +1
- compCheck = turtle.getItemCount(compSlot)
- until compCheck == 1 or iterator == 17
- end
- turtle.select(sapSlot)
- for i = 1,16 do
- if turtle.compareTo(i) == true then
- turtle.select(i)
- turtle.transferTo(sapSlot)
- turtle.select(sapSlot)
- end
- end
- end
- function deposit()
- turtle.suck()
- turtle.suckUp()
- for i = 5,16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- function copyStartup()
- print("Placeholder")
- end
- function titleScreen(countDown)
- resetScreen(1,1)
- print("CasLogger Version "..version)
- newLine(4)
- local treePrint = (string.gsub(tree, "^%l", string.upper))
- print("Tree Type: ".. treePrint)
- tab()
- local loopType
- if loop == "i" then
- loopType = "While Loop"
- print("Loop Mode: ".. loopType)
- elseif loop == "c" then
- loopType = ("For Loop")
- write("Loop Mode: ".. loopType)
- if countDown > 1 then
- print(", with ")
- tab(7)
- print(countDown.." Iterations Remaining")
- --newLine()
- end
- end
- local level = turtle.getFuelLevel()
- newLine(4,2)
- print("Fuel Level: "..level)
- newLine(4)
- write("Trees chopped since last reset: "..treeCount)
- end
- function mainProg()
- titleScreen(countDown)
- deposit()
- plant()
- if fertilise() == false then
- repeat
- --local interval = 300
- sleep(300)
- turtle.select(compSlot)
- local checkIt = turtle.compare()
- until checkIt == true
- end
- harvest()
- cleanInv()
- autoRefuel()
- deposit()
- data = {tree, loop, maxIt, "home", treeCount}
- save(data)
- sleep(30)
- end
- --------
- if fs.exists("saveData") == false or tArgs[1] == "reset" then
- setup()
- elseif fs.exists("saveData") == true and tArgs[1] ~= "reset" then
- data = load("saveData")
- data = textutils.unserialize(data)
- tree = data[1]
- loop = data[2]
- maxIt = data[3]
- position = data[4]
- treeCount = data[5]
- end
- if position ~= "home" then
- returnHome()
- end
- for i, v in pairs(treeSupport) do
- if v == tArgs[1] then
- tree = v
- break
- end
- end
- if tArgs[1] ~= nil then
- if tArgs[1] == string.match(tArgs[1], "%d") and string.len(tArgs[1]) == 1 then
- print("true")
- print(maxIt)
- maxIt = tArgs[1]
- print(maxIt..": "..type(maxIt))
- maxIt = tonumber(maxIt)
- elseif tArgs[1] ~= "%d" then
- print("false")
- end
- end
- if loop == "i" then
- while true do
- treeCount = treeCount+1
- checkInv()
- mainProg()
- end
- elseif loop == "c" then
- countDown = maxIt+1
- for i = 1, maxIt do
- countDown = countDown - 1
- treeCount = treeCount+1
- checkInv()
- mainProg(countDown)
- end
- --section
- local e,r = term.getCursorPos()
- local len = string.len(tostring(treeCount))
- term.setCursorPos((e-len), r)
- write(treeCount+1)
- --section end
- newLine(1,2)
- centre("Finished Cycle\n")
- end
Advertisement
Add Comment
Please, Sign In to add comment