-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphyto_controller.lua
241 lines (207 loc) · 7.08 KB
/
phyto_controller.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
local phyto_list = {peripheral.find("thermal:machine_insolator")}
local drawer = peripheral.find("functionalstorage:framed_1")
local completion = require("cc.completion")
local function fill(x,y,x1,y1,bg,fg,char, target)
local curr_term = term.current()
term.redirect(target or curr_term)
local old_bg = term.getBackgroundColor()
local old_fg = term.getTextColor()
local old_posx,old_posy = term.getCursorPos()
if bg then
term.setBackgroundColor(bg)
end
if fg then
term.setTextColor(fg)
end
for i1=1, (x1-x)+1 do
for i2=1, (y1-y)+1 do
term.setCursorPos(x+i1-1,y+i2-1)
term.write(char or " ")
end
end
term.setTextColor(old_fg)
term.setBackgroundColor(old_bg)
term.setCursorPos(old_posx,old_posy)
term.redirect(curr_term)
end
local function clamp(x,min,max) if x > max then return max elseif x < min then return min else return x end end
local function rect(x,y,x1,y1,bg,fg,char, target)
local curr_term = term.current()
term.redirect(target or curr_term)
local old_bg = term.getBackgroundColor()
local old_fg = term.getTextColor()
local old_posx,old_posy = term.getCursorPos()
if bg then
term.setBackgroundColor(bg)
end
if fg then
term.setTextColor(fg)
end
local sizeX=(x1-x)+1
local sizeY=(y1-y)+1
for i1=1, sizeX do
for i2=1, sizeY do
if i1 == 1 or i1 == sizeX or i2 == 1 or i2 == sizeY then
term.setCursorPos(x+i1-1,y+i2-1)
if char == "keep" then
term.write()
else
term.write(char or " ")
end
end
end
end
term.setTextColor(old_fg)
term.setBackgroundColor(old_bg)
term.setCursorPos(old_posx,old_posy)
term.redirect(curr_term)
end
local function write(x,y,text,bg,fg, target)
local curr_term = term.current()
term.redirect(target or curr_term)
local old_posx,old_posy = term.getCursorPos()
local old_bg = term.getBackgroundColor()
local old_fg = term.getTextColor()
if bg then
term.setBackgroundColor(bg)
end
if fg then
term.setTextColor(fg)
end
term.setCursorPos(x,y)
term.write(text)
term.setTextColor(old_fg)
term.setBackgroundColor(old_bg)
term.setCursorPos(old_posx,old_posy)
term.redirect(curr_term)
end
local function split(s, delimiter)
local result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
term.clear()
term.setCursorPos(1,1)
local width, height = term.getSize()
print("Select a mode:")
print("1. Import seeds")
print("2. Export seeds")
local mode = tonumber(read())
if mode == 1 then
term.clear()
term.setCursorPos(1,1)
write(1,2, "[")
write(width,2, "]")
local current_count = (drawer.getItemDetail(1) or {count=-1}).count
local thread_test = {}
local available_phytos = {}
local progress = 0
for k, phyto in pairs(phyto_list) do
thread_test[#thread_test+1] = function ()
local item = phyto.getItemDetail(1)
if not item then
available_phytos[#available_phytos+1] = phyto
end
progress=progress+1
fill(2,2,1+(width*(progress/#phyto_list))-2,2, colors.black, colors.blue, "#")
write(1,1, "Scanning for free machines.. ("..progress.."/"..(#phyto_list)..")")
sleep(0.1)
end
end
parallel.waitForAll(table.unpack(thread_test))
term.setCursorPos(1,4)
print("Available machines: "..#available_phytos)
print("Confirm import? (y/n)")
if #available_phytos < current_count then
print("(Warning, not enough available machines to import all)")
end
local confirm = read():lower()
if confirm == "y" or confirm == "yes" or confirm == "true" or confirm == "1" then
term.clear()
term.setCursorPos(1,1)
print("Importing seeds..")
write(1,2, "[")
write(width,2, "]")
local transfer_progress = 0
for k,phyto in pairs(available_phytos) do
local stat = phyto.pullItems(peripheral.getName(drawer), 1, 1, 1)
if stat > 0 then
transfer_progress = transfer_progress+1
end
fill(2,2,1+(width*(transfer_progress/#available_phytos))-2,2, colors.black, colors.blue, "#")
if transfer_progress >= current_count then
break
end
end
term.setCursorPos(1,4)
print("Finished!")
end
elseif mode == 2 then
term.clear()
term.setCursorPos(1,1)
if drawer.getItemDetail(1) then
print("Warning, items still inside drawer!")
sleep(2)
term.clear()
term.setCursorPos(1,1)
end
write(1,2, "[")
write(width,2, "]")
local phyto_map = {}
local thread_test = {}
local used_phytos = 0
local item_types_temp = {}
local progress = 0
for k, phyto in pairs(phyto_list) do
thread_test[#thread_test+1] = function ()
local item = phyto.getItemDetail(1)
if item then
if not phyto_map[item.name] then
phyto_map[item.name] = {phyto}
else
phyto_map[item.name][#phyto_map[item.name]+1] = phyto
end
used_phytos = used_phytos + 1
item_types_temp[item.name] = 1
end
progress=progress+1
fill(2,2,1+(width*(progress/#phyto_list))-2,2, colors.black, colors.blue, "#")
write(1,1, "Scanning machines.. ("..progress.."/"..(#phyto_list)..")")
end
end
parallel.waitForAll(table.unpack(thread_test))
local item_types = {}
for name, v in pairs(item_types_temp) do
item_types[#item_types+1] = name
end
term.setCursorPos(1,4)
print("Used machines: "..used_phytos)
print("Different seed types: "..#item_types)
print("Select a seed to export:")
local selected = read(nil, nil, function(text) return completion.choice(text, item_types) end, "")
local selected_data = phyto_map[selected] or {}
print("Select an amount to export:")
local export_size = tonumber(read(nil, nil, function(text) return completion.choice(text, {tostring(#selected_data)}) end, tostring(#selected_data))) or #selected_data
if selected_data then
term.clear()
term.setCursorPos(1,1)
print("Exporting seeds..")
write(1,2, "[")
write(width,2, "]")
local transfer_progress = 0
for k,phyto in pairs(selected_data) do
local stat = phyto.pushItems(peripheral.getName(drawer), 1, 1, 1)
if stat > 0 then
transfer_progress = transfer_progress+1
end
fill(2,2,1+(width*(transfer_progress/export_size))-2,2, colors.black, colors.blue, "#")
if transfer_progress >= export_size then
break
end
end
term.setCursorPos(1,4)
print("Finished! Exported "..transfer_progress.." seeds!")
end
end