Advertisement
joppeaa

boom.lua

May 9th, 2024
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | None | 0 0
  1. local State = {INIT = "1",
  2.                 WAIT = "2",
  3.                 GET_TREE = "3",
  4.                 REPLANT = "4"}
  5.                
  6. local currentState = State.INIT
  7. local treeCNT = 0
  8.  
  9.  
  10. disp = peripheral.wrap("bottom")
  11.  
  12. function serialize(data, name)
  13.   if not fs.exists('/data') then
  14.       fs.makeDir('/data')
  15.   end
  16.   local f = fs.open('/data/'..name, 'w')
  17.   f.write(textutils.serialize(data))
  18.   f.close()
  19. end
  20.  
  21. function unserialize(name)
  22.   if fs.exists('/data/'..name) then
  23.       local f = fs.open('/data/'..name, 'r')
  24.       data = textutils.unserialize(f.readAll())
  25.       f.close()
  26.   end
  27.   return data
  28. end
  29.  
  30. function getTree()
  31.     turtle.dig()
  32.     turtle.forward()
  33.     while not(turtle.up()) do
  34.         turtle.digUp()
  35.         turtle.up()
  36.     end
  37.     while not (turtle.detectDown()) do
  38.         turtle.down()
  39.     end
  40.    
  41.     turtle.back()
  42.     treeCNT = treeCNT + 1
  43.     disp.clear()
  44.     disp.setCursorPos(1,1)
  45.     disp.write(tostring(treeCNT).." bomen")
  46.     serialize(treeCNT, "memTreeCount")
  47.     currentState = State.REPLANT
  48. end
  49.  
  50. function replant()
  51.     turtle.select(3)
  52.     if (turtle.getItemCount() < 10) then
  53.         print("Running low on Saplings")
  54.         turtle.suckDown(30)
  55.     end
  56.     turtle.place()  
  57.     currentState = State.WAIT
  58. end
  59.  
  60. function waitForGrow()
  61.     local inspectedBlock, data = turtle.inspect()
  62.     if data.name == "minecraft:spruce_log" then
  63.         currentState = State.GET_TREE
  64.     end
  65. end
  66.  
  67. while (1) do
  68.     if currentState == State.INIT then
  69.       treeCNT = unserialize("memTreeCount")
  70.     end
  71.    
  72.     if currentState == State.WAIT then
  73.         waitForGrow()
  74.     end
  75.    
  76.     if currentState == State.REPLANT then
  77.         replant()        
  78.     end
  79.    
  80.     if currentState == State.GET_TREE then
  81.         getTree()
  82.     end
  83. end
  84.  
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement