Advertisement
KaosKlaus

TreeFarm2

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