Guest User

logger

a guest
Sep 22nd, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.01 KB | None | 0 0
  1. --Logger 2.0
  2. --A complete redesign of my earlier logger program
  3. --Goal designs
  4. --A startup program
  5. --Uses harvested wood to refuel
  6. --Session persistence
  7. --Interfaces with the same enderchest for supply and deposit, or maybe not because openperipherals is being weird
  8.  
  9. local version = "2.4"
  10. local tArgs = {...}
  11. local tree = tArgs[1]
  12. local fuelReq
  13. local bChest = 1
  14. local bMealSlot = 2
  15. local compSlot = 4
  16. local sapSlot = 3
  17. local data = {}
  18. local fuelBurn = 15 -- computercraft figures for wooden logs fuel value
  19. local maxIT
  20. local position = "home"
  21. local treeSupport = {"birch", "rubber", "spruce", "force tree", "dark wood", "magic wood", "fir tree", "apple tree", "origin tree", "maple", "jacaranda", "autumn trees"}
  22. local truth = nil
  23. local countDown
  24. local treeCount
  25. ----   
  26.  
  27. function checkInv()
  28.     for i = 1,3 do
  29.         local slot = math.floor((i*1.2)+0.6)
  30.         if turtle.getItemCount(slot) == 0 then
  31.             resetScreen(1,1)
  32.             fs.delete("saveData")
  33.             error("Required Items Not Detected In Slot: "..slot)
  34.         end
  35.     end
  36. end
  37.  
  38. function save(data)
  39.     data = textutils.serialize(data)
  40.     local file = fs.open("saveData", "w")
  41.     file.write(data)
  42.     file.close()
  43. end
  44.  
  45. function load(str)
  46.     local file = fs.open(str, "r")
  47.     local data = file.readAll()
  48.     file.close()
  49.     return data
  50. end
  51.  
  52. function setup()
  53.     resetScreen(1,2)
  54.     centre("CasLogger, Version: "..version)
  55.     newLine(1,2)
  56.     print("Welcome to the CasLogger configuration screen")
  57.     newLine(2,0)
  58.     write("Please enter your preferred settings:")
  59.     newLine(1,2)
  60.     write("What tree are you farming? ")
  61.     local f,g = term.getCursorPos()
  62.     newLine()
  63.     write("Controlled loop or infinite loop?\n")
  64.     write("Please enter either \"c\" or \"i\"")
  65.     local h,j = term.getCursorPos()
  66.     term.setCursorPos(f,g)
  67.     tree = read()
  68.     term.setCursorPos(h,j)
  69.     loop = read()
  70.     if loop == "c" then
  71.         screenClear(g)
  72.         term.setCursorPos(1,g)
  73.         print("You have requested a for loop")
  74.         write("Please enter your desired maximum iteration. ")
  75.         maxIt = read()
  76.     end
  77.     newLine()
  78.     centre("Thank you")
  79.     newLine()
  80.     centre("Press any key to continue")
  81.     os.pullEvent("key")
  82.     resetScreen(1,1)
  83.     newLine(3,1)
  84.     print([["REMINDER: This setup relies on conveyor belts and and an item collector to collect saplings.
  85.     Surround the planting square with at least a 9*9 of conveyor belts, all pointing to an item collector.
  86.     The collector should be behind the turtle. The planting square must be in front of the turtle.
  87.     Put the import chest below the turtle."]])
  88.     os.pullEvent("key")
  89.     resetScreen(1,2)
  90.     centre("CasLogger, Version: "..version)
  91.     newLine(1,2)
  92.     tab(2)
  93.     print("Instructions:")
  94.     tab()
  95.     print("1: Place a bonemeal enderchest in slot one.")
  96.     tab()
  97.     print("2: Leave slot two vacant.")
  98.     tab()
  99.     print("3: Place saplings in slot three.")
  100.     tab()
  101.     print("4. Put one comparison log of the same type as the tree you are harvesting in slot 4")
  102.     newLine()
  103.     os.pullEvent("key")
  104.     resetScreen(1,3)
  105.     centre("Set up complete")
  106.     newLine(1,2)
  107.     centre("Thank you for using")
  108.     newLine()
  109.     centre("CasLogger Version "..version)
  110.     newLine(1,2)
  111.     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")
  112.     centre("Press any key to launch program")
  113.     os.pullEvent("key")
  114.     checkInv()
  115.     maxIt = tonumber(maxIt)
  116.     treeCount = -1
  117.     data = {tree, loop, maxIt, "home", treeCount}
  118.     data = textutils.serialize(data)
  119.     save(data) 
  120. end
  121.  
  122. function returnHome()
  123.     data = load("saveData")
  124.     data = textutils.unserialize(data)
  125.     print(data[4])
  126.     local condition = data[4]
  127.     if condition == "sapPlant" then
  128.         if fertilise() == false then
  129.             repeat
  130.                 --local interval = 300
  131.                 sleep(300)
  132.                 turtle.select(compSlot)
  133.                 local checkIt = turtle.compare()
  134.             until checkIt == true
  135.         end
  136.         harvest()
  137.         cleanInv()
  138.         autoRefuel()
  139.         deposit()
  140.     elseif condition == "enderUp" then
  141.         turtle.select(1)
  142.         turtle.digUp()
  143.         if fertilise() == false then
  144.             repeat
  145.                 --local interval = 300
  146.                 sleep(300)
  147.                 turtle.select(compSlot)
  148.                 local checkIt = turtle.compare()
  149.             until checkIt == true
  150.         end
  151.         harvest()
  152.         cleanInv()
  153.         autoRefuel()
  154.         deposit()
  155.     elseif condition == "harvestUp" then
  156.         truth = "sauce"
  157.         harvest(truth)
  158.         cleanInv()
  159.         autoRefuel()
  160.         deposit()
  161.     elseif condition == "harvestFin" then
  162.         while turtle.detectDown~= true do
  163.             turtle.down()
  164.         end
  165.         turtle.back()
  166.     elseif condition == "deposit" then
  167.         cleanInv()
  168.         autoRefuel()
  169.         deposit()
  170.     end
  171. end
  172.  
  173. 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
  174.     if maxi == nil then
  175.         local x,y = term.getSize()
  176.         maxi = y
  177.     end
  178.     for i = mini, maxi do
  179.         term.setCursorPos(1,i)
  180.         term.clearLine()
  181.     end
  182. end
  183.  
  184. 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.
  185.     if num1 == nil then
  186.         num1 = 1
  187.     end
  188.     if num2 == nil then
  189.         num2 = 1
  190.     end
  191.     local x,y = term.getCursorPos()
  192.     term.setCursorPos(num1, y+num2)
  193. end
  194.  
  195. 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
  196.     if x == nil then
  197.         x = 1
  198.     end
  199.     if y == nil then
  200.         y = 1
  201.     end
  202.     term.clear()
  203.     term.setCursorPos(x,y)
  204. end
  205.  
  206. function centre(str) -- pass a string to centre and it will write it to the centre of the screen
  207.     local x,y = term.getCursorPos()
  208.     local mx, my = term.getSize()
  209.     x = math.floor((mx/2)-(string.len(str)/2))
  210.     term.setCursorPos(x,y)
  211.     write(str)
  212. end
  213.  
  214. function tab(num)
  215.     if num == nil then
  216.         num = 3
  217.     end
  218.     local x,y = term.getCursorPos()
  219.     term.setCursorPos(x+num, y)
  220. end
  221.  
  222. function treeHelp()
  223.     function returnMenu()
  224.         print("Options")
  225.         tab(2)
  226.         print("Press enter to return to tree help")
  227.         tab(2)
  228.         print("Press q to quit the program")
  229.         tab(2)
  230.         print("press r to return to main program")
  231.         local key = os.pullEvent("key")
  232.         return key
  233.     end
  234.    
  235.     term.clear()
  236.     term.setCursorPos(1,1)
  237.     print("List of tree Support")
  238.     local x,y = term.getSize()
  239.     local support = {Birch, Spruce, Rubber}
  240.     for i,v in pairs(support) do
  241.         print(v)
  242.         if i >= (y-1) then
  243.             write("Press any key to continue")
  244.             os.pullEvent("key")
  245.             term.clearLine()
  246.         end
  247.     end
  248. end
  249.  
  250. function treeType(tree)
  251.     local height
  252.     tree = string.lower(tree)
  253.     if tree == "spruce" or tree == "dark wood" or tree == "fir tree" then
  254.         height = 17
  255.     elseif tree == "birch" or tree == "rubber" or tree == "apple" or tree == "origin" or tree == "maple" then
  256.         height = 7
  257.     elseif tree == "force" then
  258.         height = 8
  259.     elseif tree == "jacaranda" then
  260.         height = 4
  261.     elseif tree == "magic wood" then
  262.         height = 30
  263.     elseif tree == "autumn" then
  264.         height = 9
  265.     else
  266.         print("Unrecognised tree: Setting Default fuel consumption")
  267.         height = 20
  268.     end
  269.     fuelReq = math.ceil(((2*height)+2)+3)
  270.     return fuelReq
  271. end
  272.  
  273. function autoRefuel()
  274.     fuelReq = treeType(tree)   
  275.     if turtle.getFuelLevel() <= fuelReq then
  276.         fuelReq = math.ceil(fuelReq/fuelBurn)
  277.         local iterator = 5
  278.         local progress = 0
  279.         repeat
  280.             turtle.select(iterator)
  281.             if turtle.compareTo(compSlot) == true then
  282.                 local measure = turtle.getItemCount(iterator)
  283.                 progress = measure + progress
  284.                 if measure > fuelReq then
  285.                     measure = fuelReq
  286.                 end
  287.                 turtle.refuel(measure)
  288.             end
  289.             iterator = iterator+1
  290.         until progress >= fuelReq
  291.     end
  292. end
  293.  
  294. function fertilise()
  295.     local checker = 4
  296.     if turtle.getItemCount(bMealSlot) <= 6 then
  297.         data = {tree, loop, maxIt, "enderUp", treeCount}
  298.         save(Data)
  299.         turtle.digUp()
  300.         turtle.select(bChest)
  301.         turtle.placeUp()
  302.         turtle.select(bMealSlot)
  303.         turtle.suckUp(32)
  304.         turtle.select(bChest)
  305.         turtle.digUp()
  306.         data = {tree, loop, maxIt, "sapPlant", treeCount}
  307.         save(data)
  308.     end
  309.     local iterator = 1
  310.     if turtle.getItemCount == 0 then
  311.         print("Slow Mode")
  312.         return false
  313.     else
  314.         repeat
  315.             turtle.select(bMealSlot)
  316.             turtle.place()
  317.             turtle.select(compSlot)
  318.             if turtle.compare() == true then
  319.                 checker = true
  320.             end
  321.             iterator = iterator+1
  322.             local slotCheck = turtle.getItemCount(bMealSlot)
  323.         until checker == true or iterator == 6 or slotCheck == 0
  324.     end
  325.     return true
  326. end
  327.  
  328. function plant()
  329.     turtle.select(sapSlot)
  330.     turtle.place()
  331.     data = {tree, loop, maxIt, "sapPlant", treeCount}
  332.     save(data)
  333. end
  334.  
  335. function harvest(truth)
  336.     turtle.select(compSlot)
  337.     if truth == nil then
  338.     repeat
  339.         if turtle.compare() == false then
  340.             sleep(15)
  341.         end
  342.     until turtle.compare() == true
  343.     data = {tree, loop, maxIt, "forward", treeCount}
  344.     save(data)
  345.         turtle.dig()
  346.         turtle.forward()
  347.     end
  348.     truth = nil
  349.     data = {tree, loop, maxIt, "harvestUp", treeCount}
  350.     save(data)
  351.     while turtle.compareUp() do
  352.         turtle.digUp()
  353.         turtle.up()
  354.     end
  355.     data = {tree, loop, maxIt, "harvestUp", treeCount}
  356.     save(data)
  357.     while turtle.detectDown() == false do
  358.         turtle.down()
  359.     end
  360.     turtle.back()
  361.     data = {tree, loop, maxIt, "deposit", treeCount}
  362.     save(data)
  363. end
  364.  
  365. function cleanInv()
  366.     local compCheck = turtle.getItemCount(compSlot)
  367.     if compCheck > 1 then
  368.         turtle.select(compSlot)
  369.         iterator = 5
  370.         repeat
  371.             turtle.transferTo(iterator, (compCheck-1))
  372.             iterator = iterator +1
  373.             compCheck = turtle.getItemCount(compSlot)
  374.         until compCheck == 1 or iterator == 17
  375.     end
  376.     turtle.select(sapSlot)
  377.     for i = 1,16 do
  378.         if turtle.compareTo(i) == true then
  379.             turtle.select(i)
  380.             turtle.transferTo(sapSlot)
  381.             turtle.select(sapSlot)
  382.         end
  383.     end    
  384. end
  385.  
  386. function deposit()
  387.     turtle.suck()
  388.     turtle.suckUp()
  389.     for i = 5,16 do
  390.         if turtle.getItemCount(i) > 0 then
  391.             turtle.select(i)
  392.             turtle.dropDown()
  393.         end
  394.     end
  395. end
  396.  
  397. function copyStartup()
  398.     print("Placeholder")
  399. end
  400.  
  401. function titleScreen(countDown)
  402.     resetScreen(1,1)
  403.     print("CasLogger Version "..version)
  404.     newLine(4)
  405.     local treePrint = (string.gsub(tree, "^%l", string.upper))
  406.     print("Tree Type: ".. treePrint)
  407.     tab()
  408.     local loopType
  409.     if loop == "i" then
  410.         loopType = "While Loop"
  411.         print("Loop Mode: ".. loopType)
  412.     elseif loop == "c" then
  413.         loopType = ("For Loop")
  414.         write("Loop Mode: ".. loopType)
  415.         if  countDown > 1 then
  416.             print(", with ")
  417.             tab(7)
  418.             print(countDown.." Iterations Remaining")
  419.             --newLine()
  420.         end
  421.     end
  422.     local level = turtle.getFuelLevel()
  423.     newLine(4,2)
  424.     print("Fuel Level: "..level)
  425.     newLine(4)
  426.     write("Trees chopped since last reset: "..treeCount)
  427. end
  428.  
  429. function mainProg()
  430.     titleScreen(countDown) 
  431.     deposit()
  432.     plant()
  433.     if fertilise() == false then
  434.         repeat
  435.             --local interval = 300
  436.             sleep(300)
  437.             turtle.select(compSlot)
  438.             local checkIt = turtle.compare()
  439.         until checkIt == true
  440.     end
  441.     harvest()
  442.     cleanInv()
  443.     autoRefuel()
  444.     deposit()
  445.     data = {tree, loop, maxIt, "home", treeCount}
  446.     save(data)
  447.     sleep(30)
  448. end
  449.  
  450. --------
  451.  
  452. if fs.exists("saveData") == false or tArgs[1] == "reset" then
  453.     setup()
  454. elseif fs.exists("saveData") == true and tArgs[1] ~= "reset" then
  455.     data = load("saveData")
  456.     data = textutils.unserialize(data)
  457.     tree = data[1]
  458.     loop = data[2]
  459.     maxIt = data[3]
  460.     position = data[4]
  461.     treeCount = data[5]
  462. end
  463.  
  464. if position ~= "home" then
  465.     returnHome()
  466. end
  467.  
  468. for i, v in pairs(treeSupport) do
  469.     if v == tArgs[1] then
  470.         tree = v
  471.         break
  472.     end
  473. end
  474.  
  475. if tArgs[1] ~= nil then
  476.     if tArgs[1] == string.match(tArgs[1], "%d") and string.len(tArgs[1]) == 1 then
  477.         print("true")
  478.         print(maxIt)
  479.         maxIt = tArgs[1]
  480.         print(maxIt..": "..type(maxIt))
  481.         maxIt = tonumber(maxIt)
  482.     elseif tArgs[1] ~= "%d" then
  483.         print("false")
  484.     end
  485. end
  486.  
  487. if loop == "i" then
  488.     while true do
  489.         treeCount = treeCount+1
  490.         checkInv()
  491.         mainProg()
  492.     end
  493. elseif loop == "c" then
  494.     countDown = maxIt+1
  495.     for i =  1, maxIt do
  496.         countDown = countDown - 1
  497.         treeCount = treeCount+1
  498.         checkInv()
  499.         mainProg(countDown)
  500.     end
  501.     --section
  502.         local e,r = term.getCursorPos()
  503.         local len = string.len(tostring(treeCount))
  504.         term.setCursorPos((e-len), r)
  505.         write(treeCount+1)
  506.     --section end
  507.     newLine(1,2)
  508.     centre("Finished Cycle\n")
  509. end
Advertisement
Add Comment
Please, Sign In to add comment