Advertisement
theoriginalbit

Tree Logger

Jan 28th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.60 KB | None | 0 0
  1. --[[
  2. Author: TheOriginalBIT
  3. Version: 1.0
  4. Created: 13 Dec 2012
  5. Last Update: 13 Dec 2012
  6.  
  7. License:
  8.  
  9. COPYRIGHT NOTICE
  10. Copyright © 2012-2013 Joshua Asbury a.k.a TheOriginalBIT [theoriginalbit@gmail.com]
  11.  
  12. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  13. associated documentation files (the "Software"), to deal in the Software without restriction,
  14. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  16. subject to the following conditions:
  17.  
  18. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  19. -Visible credit is given to the original author.
  20. -The software is distributed in a non-profit way.
  21.  
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  23. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. ]]--
  27.  
  28.  
  29. --[[
  30.  
  31. Variables you may change
  32.  
  33. ]]
  34.  
  35. saplingSlot     = 1
  36. bonemealSlot    = 2 -- This is only compatible with TurtleOS 1.4 and above
  37. woodSlot        = 3 -- This is just starting dig slot, used to compare
  38.  
  39. deployBonemealSide = "bottom"   --[[ Should be: any side without peripheral or the front. Must have wire connecting to something that can deploy bonemeal i.e. RedPower Deployer
  40.                                 This setting is for TurtleOS 1.3 only. ]]
  41.                                  
  42.  
  43. retainItems     = false     -- For TurtleOS 1.3 compatibilit
  44.  
  45. itemDropOffSide = "left"    --[[ Should be: left, right, up, down, back (cannot be front as thats where tree must grow)
  46.                                  For TurtleOS 1.4 can be chest or RedPower Relay, etc, 1.3 must be something such as Buildcraft obsidian pipe or transposer NOT directly infront ]]
  47.  
  48. --[[
  49.  
  50. Program Code
  51.  
  52. Note: Anything you change beyond this point may cause undesireable effects.
  53.  
  54. I (TheOriginalBIT) in no way take responsibility for any damage done by
  55. misuse or changing of this code. But please do feel free to change if you
  56. wish.
  57.  
  58. ]]
  59.  
  60. tArgs = {...}
  61.  
  62. local function checkInventory()
  63.     x, y = term.getCursorPos()
  64.     while turtle.getItemCount(saplingSlot) == 0 do
  65.         term.setCursorPos(x,y)
  66.         print("Waiting for saplings in slot 1")
  67.         sleep(0.1)
  68.     end
  69. end
  70.  
  71. local function place(slot)
  72.     turtle.select(slot)
  73.     turtle.place()
  74. end
  75.  
  76. local function deployBonemeal()
  77.     rs.setOutput(deployBonemealSide, true)
  78.     sleep(0.1)
  79.     rs.setOutput(deployBonemealSide, false)
  80. end
  81.  
  82. local function waitForSaplingGrowth()
  83.     turtle.select(saplingSlot)
  84.     x, y = term.getCursorPos()
  85.     if (turtle.getItemCount(saplingSlot) == 0) then
  86.         print("No sapling to compare to.\nNo way to check growth.\nQuiting")
  87.         error()
  88.     end
  89.    
  90.     while turtle.compare() do
  91.         if (turtle.getItemCount(bonemealSlot) > 0) then
  92.             if (osVersion >= 1.4) then
  93.                 place(bonemealSlot)
  94.             elseif (osVersion == 1.3) then
  95.                 deployBonemeal()
  96.             end
  97.             turtle.select(saplingSlot)
  98.         end
  99.         term.setCursorPos(x,y)
  100.         print("Waiting for sapling to grow.")
  101.         sleep(0.1)
  102.     end
  103.     print("Sapling has grown.")
  104. end
  105.  
  106. local function forward()
  107.     turtle.select(woodSlot)
  108.     turtle.dig()
  109.     if (not turtle.forward() and not turtle.detect()) then
  110.         print("I think I'm out of fuel and for OS compatibility reasons I cant check.")
  111.         error()
  112.     end
  113.    
  114. end
  115.  
  116. local function up()
  117.     turtle.select(woodSlot)
  118.     while turtle.compareUp() do
  119.         turtle.digUp()
  120.         if (not turtle.up() and not turtle.detectUp()) then
  121.             print("I think I'm out of fuel and for OS compatibility reasons I cant check. On next run usage: "..shell.getRunningProgram().." home")
  122.             error()
  123.         end
  124.         sleep(0.1)
  125.     end
  126.     if (turtle.detectUp()) then -- this makes sure the top leaves are removed to allow big tree growth
  127.         turtle.digUp()
  128.     end
  129. end
  130.  
  131. local function removeTree()
  132.     print("Removing tree.")
  133.     forward()
  134.     up()
  135. end
  136.  
  137. local function returnHome()
  138.     while not turtle.detectDown() do
  139.         if (not turtle.down()) then
  140.             print("I think I'm out of fuel and for OS compatibility reasons I cant check. On next run usage: "..shell.getRunningProgram().." home")
  141.             error()
  142.         end
  143.         sleep(0.1)
  144.     end
  145.     if (not turtle.back()) then
  146.         print("I think I'm out of fuel and for OS compatibility reasons I cant check. On next run usage: "..shell.getRunningProgram().." home")
  147.         error()
  148.     end
  149. end
  150.  
  151. local function emptyInventory()
  152.     for i = 1, tonumber(slotCount), 1 do
  153.         if (i ~= saplingSlot or (osVersion == 1.3 and i ~= bonemealSlot)) then
  154.             turtle.select(i)
  155.             turtle.drop()
  156.         end
  157.     end
  158. end
  159.  
  160. local function dropOffItems()
  161.     if (itemDropOffSide == "up") or (itemDropOffSide == "down") then
  162.         emptyInventory()
  163.         return
  164.     end
  165.    
  166.     funcTo      = turtle.turnLeft
  167.     funcBack    = turtle.turnRight
  168.      
  169.     if (itemDropOffSide == "right") then
  170.         funcTo = turtle.turnRight
  171.         funcBack = turtle.turnLeft
  172.     end
  173.    
  174.     if (itemDropOffSide == "back") then funcTo() end
  175.     funcTo()
  176.     emptyInventory()
  177.     if (itemDropOffSide == "back") then funcFrom() end
  178.     funcFrom()
  179. end
  180.  
  181. local function run()
  182.     while true do
  183.         checkInventory()
  184.         place(saplingSlot)
  185.         if (osVersion >= 1.4) then
  186.             place(bonemealSlot)
  187.         elseif (osVersion == 1.3) then
  188.             deployBonemeal()
  189.         end
  190.        
  191.         waitForSaplingGrowth() -- This is just incase its out of bonemeal
  192.        
  193.         removeTree()
  194.        
  195.         returnHome()
  196.        
  197.         if (not retainItems) then
  198.             dropOffItems()
  199.         end
  200.        
  201.         sleep(0.1)
  202.     end
  203. end
  204.  
  205. if (not turtle) then
  206.     print("Sorry but this can only run on a turtle.")
  207.     return
  208. end
  209.  
  210. osVersion = os.version()
  211. osVersion = tonumber(string.sub(osVersion, string.len(osVersion) - 3, string.len(osVersion)))
  212. print("Running on TurtleOS "..osVersion)
  213. if (osVersion < 1.3) then
  214.     print("Unable to run on this OS. Requires TurtleOS 1.3 or higher.")
  215.     return
  216. elseif (osVersion > 1.4) then
  217.     print("Sorry TurtleOS "..osVersion.." is not supported by this version. Please check for an update.")
  218.     return
  219. end
  220.  
  221. if ((#tArgs == 1) and (tArgs[1] == "home")) then
  222.     returnHome()
  223. elseif ((#tArgs == 1) and (tArgs[1] ~= "home")) then
  224.     print("Usage: "..shell.getRunningProgram())
  225.     print("Usage: "..shell.getRunningProgram().." home")
  226.     return
  227. end
  228.  
  229. term.clear()
  230. term.setCursorPos(1,1)
  231.  
  232. slotCount = 9
  233.  
  234. if (osVersion == 1.4) then
  235.     slotCount = 16
  236.     write("Have you put fuel in me? ")
  237.     answer = read()
  238.     if (tolower(answer) ~= "yes") then
  239.         print("\nPlease refuel me and run again.")
  240.         return
  241.     end
  242. end
  243. print(slotCount)
  244. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement