JustDoesGames

Felling Program

Jun 1st, 2021
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. local clr, cp = term.clear, term.setCursorPos
  2.  
  3. local function ensure(func, ...) -- Thanks Fatboychummy
  4.   local args = table.pack(...)
  5.   while not func() do
  6.     for i = 1, args.n do
  7.       args[i]()
  8.     end
  9.   end
  10. end
  11.  
  12. local function find(item)
  13.   local slot = 1
  14.   while slot ~= 17 do
  15.     local data = turtle.getItemDetail(slot)
  16.     if data then
  17.       if data.name == item then return slot end
  18.     end
  19.     slot = slot + 1
  20.   end
  21.   return false
  22. end
  23.  
  24. local function refuel()
  25.   if turtle.getFuelLevel() == 0 then
  26.     print("Refueling...")
  27.     turtle.select(find("minecraft:coal_block") or find("minecraft:coal") or 1)
  28.     turtle.refuel(1)
  29.     if turtle.getFuelLevel() == 0 then
  30.       print("Refuel Needed.")
  31.       while turtle.getFuelLevel() == 0 do
  32.         sleep(.5) turtle.select(find("minecraft:coal") or 1) turtle.refuel(1)
  33.       end
  34.     end
  35.   end
  36. end
  37.  
  38. local function forward() refuel() write("F") ensure(turtle.forward) end
  39. local function reverse() refuel() write("B") ensure(turtle.back) end
  40. local function up() refuel() write("U") ensure(turtle.up) end
  41. local function down() refuel() write("D") ensure(turtle.down) end
  42. local function turn(d) write("2") if d == 1 then ensure(turtle.turnRight) else ensure(turtle.turnLeft) end end
  43.  
  44. --[[
  45.  
  46. Only when idle: <<= create the config only when it is time to move
  47.   - config file does not exist
  48. Only while moving forward: <<= only edit config when starting to turn
  49.   - config file exists with:
  50.     - turned = false
  51. Only while turning: <<== edit on every stage of turn
  52.   - config file exists with:
  53.     - turned = true
  54.     - stage = x
  55.  
  56. --]]
  57.  
  58. local wait = 5
  59. local tbl = {}
  60. local function reset()
  61.   local d = tbl.data or {farmed = 0}
  62.   tbl = {waittimes = {wait, 0}, running = false, staging = false, stage = 0, ended = 0, data=d}
  63. end reset()
  64.  
  65. local function save()
  66.   write("*")
  67.   local f = fs.open("/ft_config.lua", "w") f.write(textutils.serialize(tbl)) f.close()
  68. end
  69.  
  70. if fs.exists("/ft_config.lua") then
  71.   local f = fs.open("/ft_config.lua", "r")
  72.   if not f then return printError("Failed to open ft_config.lua") end
  73.   tbl = textutils.unserialize(f.readAll()) f.close()
  74.   if type(tbl) ~= "table" then return printError("Failed to read ft_config.lua") end
  75. end
  76.  
  77. clr()
  78. print("Rebooted:")
  79. print(textutils.serialize(tbl)) sleep(3) clr()
  80.  
  81.  
  82. local stages = {
  83.   turtle.dig,
  84.   forward,
  85.   turtle.digUp,
  86.   up,
  87.   turtle.digUp,
  88.   up,
  89.   turtle.digUp,
  90.   up,
  91.   turtle.digUp,
  92.   up,
  93.   turtle.digUp,
  94.   down,
  95.   down,
  96.   down,
  97.   down,
  98.   reverse,
  99. }
  100.  
  101. --[[
  102. if turtle.detect() then
  103.     if tbl.stage == 2 or tbl.stage == 5 then tbl.staging, tbl.ended = false, 1 return end
  104.   end
  105.   if tbl.stage == 6 then tbl.staging = false end
  106.   if tbl.stage > 6 then tbl.stage = 1 end
  107.   local tmp = tbl.stage
  108.   if tbl.stage == 3 then tbl.staging = false end save()
  109.   if tbl.ended ~= 1 then write("4["..tbl.stage.."]") stages[tmp]() end
  110. --]]
  111.  
  112.  
  113. local function doStage()
  114.   tbl.stage = tbl.stage + 1
  115.   if tbl.stage > #stages then tbl.stage, tbl.running = 0, false end save()
  116.   if tbl.running then stages[tbl.stage]() else
  117.     clr() local b,c = false, false
  118.     for i=1, 16 do
  119.       local a = turtle.getItemDetail(i)
  120.       if a then
  121.         if a.name == "minecraft:sapling" and not b then
  122.           b=true
  123.           turtle.select(i)
  124.           turtle.transferTo(2,64)
  125.         elseif a.name == "minecraft:coal_block" and not c then
  126.           c=true
  127.           turtle.select(i)
  128.           turtle.transferTo(1,64)
  129.         else
  130.           turtle.select(i)
  131.           turtle.dropDown(64)
  132.         end
  133.       end
  134.     end
  135.   end
  136. end
  137.  
  138.  
  139. while true do
  140.   if tbl.running then
  141.       doStage()
  142.   else
  143.     cp(1,1) print("Farmed: "..tbl.data.farmed)
  144.     sleep(1) local a,b = turtle.inspect() if a then if b.name == "minecraft:log" then tbl.running = true end else local a = find("minecraft:sapling") if a ~= false then turtle.select(a) turtle.place() end end
  145.     turtle.suckUp()
  146.     turtle.suck()
  147.   end
  148. end
  149.  
Advertisement
Add Comment
Please, Sign In to add comment