Advertisement
KaosKlaus

TreeFarm

Mar 21st, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.95 KB | None | 0 0
  1. -- ************ TreeFarm **************
  2. -- * 1.6 fixed bug: everthing in slot 1 can be fuel
  3. -- * 1.5 fixed bug: crash when 1x1 tree in 2x2 farm
  4. -- * 1.4 track support in progress (still not functional)
  5. -- * 1.3 fixed bug in go()
  6. -- * 1.2 fixed sapling collection at the sides, code refactor, 2x2 tree girth support
  7. -- * 1.1 inventroy fix, intro sign, wood counter, timer
  8. -- * 1.0 release!
  9. -- ************************************
  10.  
  11. verNum = "v1.6"
  12.  
  13. local startTime = os.clock()
  14.  
  15. local tArgs = { ... }
  16. local delay = 0.25
  17.  
  18. local level = 1
  19. local nextTurn = 0
  20. local mode
  21. local nWood
  22. local screenX, screenY = term.getSize()
  23.  
  24. local tTurns = {
  25.   [0] = turtle.turnRight,
  26.   [1] = turtle.turnLeft,
  27. }
  28.  
  29. local forward = {
  30.   go = turtle.forward,
  31.   dig = turtle.dig,
  32.   detect = turtle.detect,
  33.   inspect = turtle.inspect,
  34.   attack = turtle.attack,
  35. }
  36.  
  37. inspect = function ()
  38.   return {false, ""}
  39. end
  40.  
  41. local back = {
  42.   go = turtle.back,
  43.   inspect = function () return false end,
  44. }
  45.  
  46. local up = {
  47.   go = turtle.up,
  48.   dig = turtle.digUp,
  49.   detect = turtle.detectUp,
  50.   inspect = turtle.inspectUp,
  51.   attack = turtle.attackUp,
  52. }
  53.  
  54. local down = {
  55.   go = turtle.down,
  56.   dig = turtle.digDown,
  57.   detect = turtle.detectDown,
  58.   inspect = turtle.inspectDown,
  59.   attack = turtle.attackDown,
  60.   place = turtle.placeDown,
  61. }
  62.  
  63. local sapling = {
  64.   name = "Sapling",
  65.   ["minecraft:sapling"] = true,
  66.   ["minecraft:golden_rail"] = true,
  67.   ["Railcraft:track"] = true,
  68.   ["IC2:blockRubSapling"] = true,
  69. }
  70.  
  71. local leaves = {
  72.   ["minecraft:leaves"] = true,
  73.   ["IC2:blockRubLeaves"] = true,
  74. }
  75.  
  76. local wood = {
  77.   ["minecraft:log"] = true,
  78.   ["minecraft:log2"] = true,
  79.   ["IC2:blockRubWood"] = true,
  80. }
  81.  
  82. local fuel = {
  83.   ["minecraft:coal"] = 80,
  84.   ["minecraft:log"] = 15,
  85.   ["minecraft:log2"] = 15,
  86.   ["minecraft:stick"] = 5,
  87.   ["IC2:blockRubWood"] = 15,
  88. }
  89.  
  90. container = {
  91.   ["minecraft:chest"] = true,
  92.   ["minecraft:trapped_chest"] = true,
  93.   ["JABBA:barrel"] = true,
  94.   ["IronChest:BlockIronChest"] = true,
  95.   ["EnderStorage:enderChest"] = true,
  96. }
  97.  
  98. local function displayLogo()
  99.   term.clear()
  100.   term.setCursorPos(1,2)
  101.   print("**** KaosKrise "..mode.." TreeFarm "..verNum.." ****")
  102.   print()
  103.   print("Welcom, Gud Sir! Im Gonna be ur guide on dis marvelous advendchur.")
  104.   print()
  105. end
  106.  
  107. local function formatSeconds(seconds)
  108.   if seconds >= 3600 then
  109.     return string.format("%d:%.2d:%.2d", math.floor(seconds/3600), math.floor(seconds/60%60), math.floor(seconds%60))
  110.   else
  111.     return string.format("%d:%.2d", math.floor(seconds/60%60), math.floor(seconds%60))
  112.   end
  113. end
  114.  
  115. local function displayTime()
  116.   local x, y = term.getCursorPos()
  117.   local time = os.clock()
  118.   local runTime = time - startTime
  119.   local string = formatSeconds(runTime)
  120.   term.setCursorPos(screenX -(#string) ,screenY)
  121.   write(string)
  122.   term.setCursorPos(x, y)
  123.   return true
  124. end
  125.  
  126. local function countWood()
  127.   nWood = nWood +1
  128.   local x, y = term.getCursorPos()
  129.   term.setCursorPos(1,screenY)
  130.   write("I has chopd "..nWood.." wood itamz")
  131.   term.setCursorPos(x, y)
  132. end
  133.  
  134. local function isItem(slot)
  135.   slot = slot or turtle.getSelectedSlot()
  136.   local data = turtle.getItemDetail(slot)
  137.  
  138.   if data then
  139.     return data.name
  140.   else
  141.     return false
  142.   end
  143. end
  144.  
  145. local function selectItem(item)
  146.   for i=1,16 do
  147.     if item[isItem(i)] then
  148.       turtle.select(i)
  149.       return true
  150.     end
  151.   end
  152.   return false
  153. end
  154.  
  155. local function inspectBlock(direction)
  156.   if direction == nil then direction = forward end
  157.   local success, data = direction.inspect()
  158.   if success then
  159.     return data.name
  160.   else
  161.     return false
  162.   end
  163. end
  164.  
  165. local function refuel()
  166.   local fuelLevel = turtle.getFuelLevel()
  167.   if fuelLevel == "unlimited" or fuelLevel > 0 then
  168.     return true
  169.   end
  170.  
  171.   local function tryRefuel()
  172.     local bestFuel = 1
  173.     local bestFuelVal = 0
  174.     for i=1,16 do
  175.       local itemInSlot = isItem(i)
  176.       if fuel[itemInSlot] and fuel[itemInSlot] > bestFuelVal then
  177.         bestFuel = i
  178.         bestFuelVal = fuel[itemInSlot]
  179.       end
  180.     end
  181.   if bestFuel then
  182.       turtle.select(bestFuel)
  183.       if turtle.refuel(1) then
  184.         turtle.select(1)
  185.         return true
  186.       end
  187.     end
  188.     return false
  189.   end
  190.  
  191.   if not tryRefuel() then
  192.     displayLogo()
  193.     print( "Add more fuel to continue." )
  194.     while not tryRefuel() do
  195.       displayTime()
  196.       sleep(1)
  197.     end
  198.     displayLogo()
  199.     print( "Resuming..." )
  200.   end
  201.   return true
  202. end
  203.  
  204. local function tryDig(direction, block)
  205.   block = block or inspectBlock(direction)
  206.   turtle.select(1)
  207.   if direction.dig() then
  208.     if wood[block] then
  209.       countWood()
  210.     end
  211.     return true
  212.   end
  213.   return false
  214. end
  215.  
  216. local function goDirection(direction, block)
  217.   block = block or inspectBlock(direction)
  218.   if not refuel() then
  219.     return false
  220.   end
  221.  
  222.   while displayTime() and not direction.go() do
  223.     if block and not tryDig(direction, block) then
  224.       return false
  225.     elseif direction.attack() then
  226.       print("smack!")
  227.     else
  228.       sleep(delay)
  229.     end
  230.   end
  231.  
  232.   return true
  233. end
  234.  
  235. --[[
  236. Syntax:   go( string detectedBlockname)
  237. Returns:  boolean whether the turtle succeeded.
  238. --]]
  239. local function go(block)
  240.   return goDirection(forward, block)
  241. end
  242.  
  243. local function goBack(block)
  244.   return goDirection(back, block)
  245. end
  246.  
  247. local function goSuck(block)
  248.   turtle.suckDown()
  249.   return goDirection(forward, block)
  250. end
  251.  
  252. local function goUp(block)
  253.   level = level + 1
  254.   return goDirection(up, block)
  255. end
  256.  
  257. local function goDown(block)
  258.   level = level -1
  259.   return goDirection(down, block)
  260. end
  261.  
  262. local function plant()
  263.   if selectItem(sapling) and down.place() then
  264.     return true
  265.   else
  266.     return false
  267.   end
  268. end
  269.  
  270. -- Tree Girth: 1x1:
  271. local function chopDownTree_1x1()
  272.   local block = inspectBlock(up)
  273.   while wood[block] do
  274.     goUp(block)
  275.     block = inspectBlock(up)
  276.   end
  277.   while level > 1 do
  278.     goDown()
  279.   end
  280.   tryDig(down)
  281. end
  282.  
  283. -- Tree Girth: 2x2:
  284. local function chopDownTree_2x2()
  285.   local topBlockFound = false
  286.   local turnCount
  287.  
  288.   -- helper function - turn until find wood log and return num of turns
  289.   local function findWood()
  290.     local turnCount = 0
  291.     for i=1,4 do
  292.       if wood[inspectBlock()] then
  293.         return turnCount
  294.       end
  295.       turtle.turnRight()
  296.       turnCount = i
  297.     end
  298.     return turnCount
  299.   end
  300.  
  301.   -- helper function - finding pos at top and choping the lonely top block
  302.   local function findPos()
  303.     local block = inspectBlock()
  304.     if not wood[block] then -- top block found
  305.       topBlockFound = true
  306.       go()
  307.       goDown()
  308.     else -- top block still there
  309.       tryDig(forward, block)
  310.       go()
  311.     end
  312.  
  313.     block = inspectBlock(up)
  314.     if wood[block] then -- top block found
  315.       tryDig(up, block)
  316.       topBlockFound = true
  317.     end
  318.  
  319.     turnCount = findWood()
  320.     if turnCount % 4 == 0 then
  321.       --  local event, key = os.pullEvent( "key" )  -- DEBUG BREAK POINT
  322.       goBack()
  323.       return false
  324.     else
  325.       go()
  326.       tTurns[math.floor(turnCount / 2)]()
  327.     end
  328.  
  329.     if not topBlockFound then
  330.       block = inspectBlock(up)
  331.       if wood[block] then -- top block found
  332.         tryDig(up, block)
  333.         topBlockFound = true
  334.       else
  335.         goUp()
  336.       end
  337.     end
  338.     tryDig(forward)
  339.     return true
  340.   end
  341.  
  342.   -- check if it's a little tree
  343.   if not wood[inspectBlock()] then
  344.     local x, y = term.getCursorPos()
  345.     term.clearLine()
  346.     print("Awwww, it's a little one!")
  347.     term.clearLine()
  348.     term.setCursorPos(x,y)
  349.     return chopDownTree_1x1()
  350.   end
  351.  
  352.   -- going up the tree:
  353.   local blockUp = inspectBlock(up)
  354.   while wood[blockUp] do
  355.     tryDig(forward)
  356.     goUp(blockUp)
  357.     blockUp = inspectBlock(up)
  358.   end
  359.  
  360.   local bigTree = false
  361.   if findPos() then
  362.     bigTree = true
  363.   end
  364.  
  365.   -- going back down
  366.   while level > 1 do
  367.     goDown()
  368.     tryDig(forward)
  369.   end
  370.  
  371.   -- remove the stump and replant
  372.   if bigTree then
  373.     for i=1,3 do
  374.       tryDig(down)
  375.       plant()
  376.       if i >= 2 and i <= 3 then
  377.         tTurns[math.floor(turnCount / 2)]()
  378.       end
  379.       go()
  380.     end
  381.   end
  382.  
  383.   tryDig(down)
  384. end
  385.  
  386.  
  387. local function tryUnload()
  388.   local x, y = term.getCursorPos()
  389.   turtle.select(1)
  390.   term.clearLine()
  391.   print("I has findz chest.")
  392.   term.clearLine()
  393.   print("Will try 2 unload mah stuff...")
  394.  
  395.   local item = isItem(16)
  396.   if "minecraft:coal" ~= item then
  397.     turtle.select(16)
  398.     turtle.dropDown()
  399.   end
  400.  
  401.   for i=14,15 do
  402.     item = isItem(i)
  403.     if item and not sapling[item] then
  404.       turtle.select(i)
  405.       if item == "minecraft:coal" then
  406.         if not turtle.transferTo(16) then
  407.           turtle.dropDown()
  408.         end
  409.       else
  410.         turtle.dropDown()
  411.       end
  412.     end
  413.   end
  414.  
  415.   for i=1,13 do
  416.     item = isItem(i)
  417.     if item then
  418.       turtle.select(i)
  419.       if sapling[item] then
  420.         if not turtle.transferTo(15) then
  421.           if not turtle.transferTo(14) then
  422.             turtle.dropDown()
  423.           end
  424.         end
  425.       elseif item == "minecraft:coal" then
  426.         if not turtle.transferTo(16) then
  427.           turtle.dropDown()
  428.         end
  429.       else
  430.         turtle.dropDown()
  431.       end
  432.     end
  433.   end
  434.  
  435.   term.setCursorPos(x,y+1)
  436.   term.clearLine()
  437.   print("I has putz teh itamz in teh chest. Yay!")
  438.   term.setCursorPos(x,y)
  439. end
  440.  
  441. local function refill()
  442.   if container[inspectBlock(up)] then
  443.     if turtle.getItemSpace(16) > 0 then
  444.       turtle.select(16)
  445.       turtle.suckUp(turtle.getItemSpace())
  446.     end
  447.   end
  448.   turtle.select(1)
  449. end
  450.  
  451. local function turn()
  452.   goSuck()
  453.   local blockDown = inspectBlock(down)
  454.   if container[blockDown] then
  455.     tryUnload()
  456.     refill()
  457.     tTurns[nextTurn]()
  458.     go()
  459.   else
  460.     refill()
  461.     tTurns[nextTurn]()
  462.     goSuck()
  463.   end
  464.   tTurns[nextTurn]()
  465.   blockDown = inspectBlock(down)
  466.   if container[blockDown] then
  467.     tryUnload()
  468.     refill()
  469.     go()
  470.   else
  471.     refill()
  472.     goSuck()
  473.   end
  474.   goSuck()
  475.   local blockDown = inspectBlock(down)
  476.   if blockDown then
  477.     if not sapling[blockDown] then
  478.       if wood[blockDown] then
  479.         chopDownTree()
  480.       elseif leaves[block] then
  481.         tryDig(down)
  482.         plant()
  483.         goSuck()
  484.       else
  485.         tTurns[nextTurn]()
  486.         goSuck()
  487.       end
  488.     end
  489.   end
  490.   nextTurn = 1 - nextTurn
  491. end
  492.  
  493. local function patrol()
  494.   while sapling[inspectBlock(down)] do
  495.     goSuck()
  496.   end
  497. end
  498.  
  499.  
  500. -- ************MAIN************
  501. arg = tArgs[1]
  502. if arg and arg == "2x2" then
  503.   chopDownTree = chopDownTree_2x2
  504.   mode = "2x2"
  505. else
  506.   chopDownTree = chopDownTree_1x1
  507.   mode = ""
  508. end
  509.  
  510. nWood = 0
  511. turtle.select(1)
  512.  
  513. displayLogo()
  514. go()
  515.  
  516. while true do
  517.   patrol()
  518.   local block = inspectBlock(down)
  519.   if block then
  520.     if wood[block] then
  521.       chopDownTree()
  522.       plant()
  523.       goSuck()
  524.     elseif leaves[block] then
  525.       tryDig(down)
  526.       plant()
  527.       goSuck()
  528.     else
  529.       turn()
  530.     end
  531.   else
  532.     plant()
  533.     goSuck()
  534.   end
  535. end
  536.  
  537.  
  538. --  local event, key = os.pullEvent( "key" )  -- DEBUG BREAK POINT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement