· 4 years ago · Jun 03, 2021, 07:20 PM
1local clr, cp = term.clear, term.setCursorPos
2
3local 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
10end
11
12local 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
22end
23
24local 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
36end
37
38local function forward() refuel() write("0") ensure(turtle.forward) end
39local function reverse() refuel() write("1") ensure(turtle.back) end
40local function turn(d) write("2") if d == 1 then ensure(turtle.turnRight) else ensure(turtle.turnLeft) end end
41
42--[[
43
44Only when idle: <<= create the config only when it is time to move
45 - config file does not exist
46Only while moving forward: <<= only edit config when starting to turn
47 - config file exists with:
48 - turned = false
49Only while turning: <<== edit on every stage of turn
50 - config file exists with:
51 - turned = true
52 - stage = x
53
54--]]
55
56local wait = 1800
57local tbl = {}
58local function reset()
59 local d = tbl.data or {farmed = 0}
60 tbl = {waittimes = {wait, 0}, running = false, staging = false, stage = 0, ended = 0, data=d}
61end reset()
62
63local function save()
64 write("*")
65 local f = fs.open("/ft_config.lua", "w") f.write(textutils.serialize(tbl)) f.close()
66end
67
68if fs.exists("/ft_config.lua") then
69 local f = fs.open("/ft_config.lua", "r")
70 if not f then return printError("Failed to open ft_config.lua") end
71 tbl = textutils.unserialize(f.readAll()) f.close()
72 if type(tbl) ~= "table" then return printError("Failed to read ft_config.lua") end
73end
74
75clr()
76print("Rebooted:")
77print(textutils.serialize(tbl)) sleep(3) clr()
78
79
80local stages = {
81 turtle.turnRight,
82 forward,
83 turtle.turnRight,
84 turtle.turnLeft,
85 forward,
86 turtle.turnLeft,
87}
88
89--[[
90tbl.stage = tbl.stage + 1 save()
91 if tbl.stage > #stages then
92 tbl.stage = 0
93 elseif tbl.stage == 2 or tbl.stage == 5 then
94 if turtle.detect() then
95 print("Journey ended.")
96 tbl.staging = false
97 tbl.ended = 1 -- has started it's 1st phase of returning
98 end
99 end
100--]]
101
102
103local function doStage()
104 tbl.stage = tbl.stage + 1
105 if turtle.detect() then
106 if tbl.stage == 2 or tbl.stage == 5 then tbl.staging, tbl.ended = false, 1 return end
107 end
108 if tbl.stage == 6 then tbl.staging = false end
109 if tbl.stage > 6 then tbl.stage = 1 end
110 local tmp = tbl.stage
111 if tbl.stage == 3 then tbl.staging = false end save()
112 if tbl.ended ~= 1 then write("4["..tbl.stage.."]") stages[tmp]() end
113end
114
115
116while true do
117 if tbl.running then
118 while tbl.staging do
119 doStage() if tbl.ended == 1 then turn(1) end
120 end
121 while not tbl.staging do
122 if not turtle.detect() then
123 local a,data = turtle.inspectDown()
124 if a then
125 if data.name == "minecraft:wheat" and data.metadata == 7 then
126 turtle.digDown() tbl.data.farmed = tbl.data.farmed + 1 save()
127 local a = find("minecraft:wheat_seeds")
128 if a ~= false then
129 turtle.select(a)
130 turtle.placeDown()
131 end
132 end
133 else
134 local a = find("minecraft:wheat_seeds")
135 if a ~= false then turtle.select(a) turtle.placeDown() end
136 end
137 forward()
138 else
139 write("5")
140 if tbl.ended == 1 then
141 tbl.ended = 2 -- has started it's last phase of returning
142 turn(1)
143 elseif tbl.ended == 2 then
144 turn() -- ENDED
145 for i=1, 16 do
146 local a = turtle.getItemDetail(i)
147 if a then if a.name ~= "minecraft:coal_block" and a.name ~= "minecraft:coal" then turtle.select(i) turtle.drop(64) end end
148 end turn() turn()
149 reset() save() clr() break
150 else
151 tbl.staging = true
152 end
153 end
154 end
155 else
156 cp(1,1) print("Time left: "..tbl.waittimes[1]-tbl.waittimes[2].." ")
157 print("Starts in "..math.ceil((tbl.waittimes[1]-tbl.waittimes[2])/60).." minutes ") print("Farmed: "..tbl.data.farmed)
158 sleep(1) tbl.waittimes[2] = tbl.waittimes[2] + 1 if tbl.waittimes[2] >= tbl.waittimes[1] then tbl.waittimes[2] = 0 tbl.running = true end save()
159 end
160end