· 4 years ago · May 29, 2021, 09:40 PM
1local function ensure(func, ...) -- Thanks Fatboychummy
2 local args = table.pack(...)
3 while not func() do
4 for i = 1, args.n do
5 args[i]()
6 end
7 end
8end
9
10local function find(item)
11 local slot = 1
12 while slot ~= 17 do
13 local data = turtle.getItemDetail(slot)
14 if data then
15 if data.name == item then return slot end
16 end
17 slot = slot + 1
18 end
19 return false
20end
21
22local function refuel()
23 if turtle.getFuelLevel() == 0 then
24 print("Refueling...")
25 turtle.select(find("minecraft:coal_block") or find("minecraft:coal") or 1)
26 turtle.refuel(1)
27 if turtle.getFuelLevel() == 0 then
28 print("Refuel Needed.")
29 while turtle.getFuelLevel() == 0 do
30 sleep(.5) turtle.select(find("minecraft:coal") or 1) turtle.refuel(1)
31 end
32 end
33 end
34end
35
36local function forward() refuel() ensure(turtle.forward) end
37local function reverse() refuel() ensure(turtle.back) end
38local function turn(d) if d == 1 then ensure(turtle.turnRight) else ensure(turtle.turnLeft) end end
39
40--[[
41
42Only when idle: <<= create the config only when it is time to move
43 - config file does not exist
44Only while moving forward: <<= only edit config when starting to turn
45 - config file exists with:
46 - turned = false
47Only while turning: <<== edit on every stage of turn
48 - config file exists with:
49 - turned = true
50 - stage = x
51
52--]]
53
54local wait = 3600
55local tbl = {waittimes = {wait, 0}, running = false, staging = false, stage = 1, ended = 0}
56
57local function save()
58 local f = fs.open("/ft_config.lua", "w") f.write(textutils.serialize(tbl)) f.close()
59end
60
61if fs.exists("/ft_config.lua") then
62 local f = fs.open("/ft_config.lua", "r")
63 if not f then return printError("Failed to open ft_config.lua") end
64 tbl = textutils.unserialize(f.readAll()) f.close()
65 if type(tbl) ~= "table" then return printError("Failed to read ft_config.lua") end
66end
67
68local stages = {
69 turtle.turnRight,
70 turtle.forward,
71 turtle.turnRight,
72 turtle.turnLeft,
73 turtle.forward,
74 turtle.turnLeft,
75}
76
77
78while true do
79 if tbl.running then
80 while tbl.staging do
81 print("Staging...")
82 ensure(stages[tbl.stage])
83 if tbl.stage == 3 or tbl.stage == 6 then
84 print("Staging complete!")
85 tbl.staging = false
86 end
87 tbl.stage = tbl.stage + 1
88 if tbl.stage > #stages then
89 tbl.stage = 1
90 elseif tbl.stage == 2 or tbl.stage == 5 then
91 if turtle.detect() then
92 print("Journey ended.")
93 tbl.staging = false
94 tbl.ended = 1 -- has started it's 1st phase of returning
95 turn(1)
96 end
97 end
98 save()
99 end
100 while not tbl.staging do
101 if not turtle.detect() then
102 print("Moving...")
103 local a,data = turtle.inspectDown()
104 if a then
105 print(data.name .. " - " .. data.metadata)
106 if data.name == "minecraft:wheat" and data.metadata == 7 then
107 turtle.digDown()
108 local a = find("minecraft:wheat_seeds")
109 if a ~= false then
110 turtle.select(a)
111 turtle.placeDown()
112 end
113 end
114 else
115 local a = find("minecraft:wheat_seeds")
116 if a ~= false then turtle.select(a) turtle.placeDown() end
117 end
118 forward()
119 else
120 print("Block (1)")
121 if tbl.ended == 1 then
122 tbl.ended = 2 -- has started it's last phase of returning
123 turn(1)
124 elseif tbl.ended == 2 then
125 turn() -- ENDED
126 for i=1, 16 do
127 local a = turtle.getItemDetail(i)
128 if a then if a.name ~= "minecraft:coal_block" and a.name ~= "minecraft:coal" then turtle.select(i) turtle.drop(64) end end
129 end turn() turn()
130 tbl = {waittimes = {wait, 0}, running = false, staging = false, stage = 1, ended = 0}
131 save() print("Finalized.") break
132 else
133 tbl.staging = true
134 end
135 end
136 end
137 else
138 print("Sleeping...")
139 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()
140 end
141end
142