-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_chamber.lua
527 lines (435 loc) · 11.6 KB
/
run_chamber.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
os.loadAPI("build");
-- API Aliases
function dirMap(turns) return build.dirMap(turns); end
function fif(cond, a, b) return build.fif(cond, a, b); end
dofile("config");
dofile("blocks");
local D, U, N, S, W, E = dirMap(0);
local compy = build.Coord:get();
local center = compy:off(N, 8 + 3):up();
local lockPos = compy:off(N):up();
grow = false;
local radius = 8;
count = codesPerRad;
local currModIdx = 1;
local currSpeciesIdx = 1;
local arrowL = string.char(17);
local arrowR = string.char(16);
monitor = peripheral.find("monitor");
monitor.setBackgroundColor(colors.black);
monitor.clear();
mainTimer = 0;
function startTimer()
mainTimer = os.startTimer(0.5);
end
function stopTimer()
os.cancelTimer(mainTimer);
end
function waitOn(func)
parallel.waitForAll(func);
end
function pad(str, len, chr)
chr = chr or " ";
local miss = len - string.len(str);
if miss > 0 then
str = str .. string.rep(chr, miss);
end
if miss < 0 then
str = string.sub(str, 1, len);
end
return str;
end
function padNum(num, len, chr)
chr = chr or " ";
local str = tonumber(num);
local miss = len - string.len(str);
if miss > 0 then
str = string.rep(chr, miss) .. str;
end
return str;
end
function sound(snd)
commands.playsound(snd, "block", "@a", compy.x, compy.y, compy.z);
end
function clicksound()
sound("block.stone_button.click_on");
end
function getKeys(tab)
local keyset={}
for k,v in pairs(tab) do
keyset[#keyset+1]=k
end
return keyset;
end
function getSpeciesList()
local _, list = commands.dt("registry", "species", "list", true)
return list;
end
local trees = {};
for k, v in pairs(getSpeciesList()) do
local mod, species = string.match(v, "([^,]+):([^,]+)");
if trees[mod] == nil then
trees[mod] = {};
end
local s = trees[mod];
s[#s+1]=species;
end
local mods = getKeys(trees);
table.sort(mods);
for k, v in pairs(trees) do
table.sort(v);
end
----------------------------------------------------------------
-- BUTTONS --
----------------------------------------------------------------
Button = {}
Button.__index = Button;
function Button:new(x, y, w, text, color)
color = color or colors.black;
local button = {}; -- our new object
setmetatable(button, Button); -- make handle lookup
button.coords = build.Coord:new(x, y, 0);
button.width = w;
button.text = text;
button.color = color;
button.monitor = monitor;
return button;
end
function Button:setMonitor(m)
self.monitor = m;
return self;
end
function Button:draw()
mon = self.monitor;
mon.setCursorPos(self.coords.x, self.coords.y);
-- mon.clearLine();
mon.setBackgroundColor(self.color);
-- mon.clearLine();
mon.write(pad(self.text, self.width, " "));
return self;
end
function Button:setText(text)
if text ~= self.text then
self.text = text;
self:draw();
end
return self;
end
function Button:setColor(color)
self.color = color;
self:draw();
return self;
end
function Button:click(x, y)
if self:isInside(x, y) then
if self.action ~= nil then
clicksound();
self.action(self, x, y);
end
end
end
function Button:isInside(x, y)
return y == self.coords.y and x >= self.coords.x and x < self.coords.x + self.width;
end
function Button:setAction(action)
self.action = action;
return self;
end
function open() waitOn(
function ()
sound("block.piston.extend");
if radius <= 8 and radius >= 2 then
center:up():cylinder(air, radius, height);
end
build.waitAsync();
end);
end
function close() waitOn(
function ()
sound("block.piston.contract");
if radius <= 8 and radius >= 2 then
center:up():cylinder(wallBlock, radius, height);
end
build.waitAsync();
end);
end
function kill() waitOn(
function ()
commands.dt("killtree", center.x, center.y, center.z);
center:up():cub():erase();-- In the case of a sapling
center:cub():fill(dirt); -- Place dirt over the rooty soil just in case
commands.kill("@e[type=item]"); -- Gets rid of dropped seeds
end);
end
function plant()
commands.dt("setcoordxor", math.random(0, 65535));
commands.dt("settree", center.x, center.y, center.z, tree, "P"); -- synchronous
setFertility(15); -- synchronous
end
function purge() waitOn(
function ()
center:up():cub():grohor(radius):gro(U, height):erase();
build.waitAsync();
end);
end
function push()
if file_exists(treeFilename()) then
print("pastebin: " .. treeFilename());
shell.run("pastebin", "put", treeFilename());
else
print("error: " .. treeFilename() .. " not found");
end
end
function setRadius(rad)
open();
if rad >= 8 then
rad = 8;
elseif rad <= 2 then
rad = 2;
end
if rad ~= radius then
radius = rad;
count = codesPerRad;
countButton:setCount(count);
radiusButton:setRadius(radius);
end
end
function radiusInc()
setRadius(radius + 1);
end
function radiusDec()
setRadius(radius - 1);
end
function treeFilename()
local mod = mods[currModIdx];
local spc = trees[mod][currSpeciesIdx];
return mod .. "/" .. spc .. ".txt";
end
openButton = Button:new(2, 2, 7, "[OPEN ]", colors.purple):setAction( open );
closeButton = Button:new(2, 3, 7, "[CLOSE]", colors.blue):setAction( close );
plantButton = Button:new(2, 4, 7, "[PLANT]", colors.lime):setAction( plant );
killButton = Button:new(2, 5, 7, "[KILL ]", colors.orange):setAction( function() kill(); grow = false; end );
purgeButton = Button:new(2, 6, 7, "[PURGE]", colors.brown):setAction( purge );
modButton = Button:new(2, 8, 27, " ", colors.gray);
treeButton = Button:new(2, 9, 27, " ", colors.gray);
radiusButton = Button:new(12, 2, 7, " ", colors.gray);
growButton = Button:new(12, 3, 7, "[START]", colors.green);
soilButton = Button:new(12, 4, 11, " ", colors.gray);
countButton = Button:new(12, 5, 11, " ", colors.gray);
pushButton = Button:new(12, 6, 7, "[PUSH ]", colors.cyan):setAction( push );
growButton.stop = function(b) b:setColor(colors.green); b:setText("[START]"); grow = false; end
growButton.start = function(b) b:setColor(colors.red); b:setText("[STOP ]") grow = true; close(); end
growButton:setAction( function(b) if grow then b:stop(); else b:start(); end; end);
countButton.setCount = function(b, c) b:setText("REMAIN:" .. padNum(c, 4)); end;
countButton:setAction( function(b) count = codesPerRad; b:setCount(count); end );
countButton:setCount(count);
-- Radius Button Setup
radiusButton.setRadius = function(b, r) b:setText(arrowL .. arrowR .. " " .. "R:" .. r); end;
radiusButton.click =
function(b, x, y)
if b:isInside(x, y) then
x = x - b.coords.x + 1;
if x == 1 then
clicksound();
radiusDec();
elseif x == 2 then
clicksound();
radiusInc();
end
end
end;
radiusButton:setRadius(radius);
function updateTree()
local mod = mods[currModIdx];
local spc = trees[mod][currSpeciesIdx];
local newTree = mod .. ":" .. spc;
if newTree ~= tree then
tree = newTree;
kill();
end
end
-- Tree Button Setup
treeButton.setIdx =
function(b, idx)
local arr = trees[mods[currModIdx]];
idx = ((idx - 1) % #arr) + 1;
b:setText(arrowL .. arrowR .. " " .. arr[idx]);
currSpeciesIdx = idx;
updateTree();
end
treeButton.click =
function(b, x, y)
if b:isInside(x, y) then
x = x - b.coords.x + 1;
if x == 1 then
clicksound();
b:setIdx(currSpeciesIdx - 1);
elseif x == 2 then
clicksound();
b:setIdx(currSpeciesIdx + 1);
end
end
end;
treeButton:setIdx(1);
-- Mod Button Setup
modButton.setIdx =
function(b, idx)
local arr = mods;
idx = ((idx - 1) % #arr) + 1;
b:setText(arrowL .. arrowR .. " " .. arr[idx]);
currModIdx = idx;
treeButton:setIdx(1);
end
modButton.click =
function(b, x, y)
if b:isInside(x, y) then
x = x - b.coords.x + 1;
if x == 1 then
clicksound();
b:setIdx(currModIdx - 1);
elseif x == 2 then
clicksound();
b:setIdx(currModIdx + 1);
end
end
end;
modButton:setIdx(1);
-- fertility Button Setup
soilButton.setFertility =
function(b, life)
local text;
if life ~= nil and life >= 0 then
text = padNum(life, 2, "0") .. "/15";
else
text = "--/--";
end
soilButton:setText("SOIL: " .. text);
end
-- All Buttons
allButtons = { openButton, closeButton, growButton, plantButton, killButton, purgeButton, countButton, modButton, treeButton, radiusButton, soilButton, pushButton };
function drawScreen()
for i, v in ipairs(allButtons) do
v:draw();
end
end
function clickScreen(x, y)
for i, v in ipairs(allButtons) do
v:click(x, y);
end
end
drawScreen();
indicator = false;
function clearVines()
-- Do nothing for now
end
function getFertility()
pass, val = commands.dt("fertility", center.x, center.y, center.z, true);
if (pass) then
return tonumber(val[1]);
end
return -1;
end
function setFertility(life)
pass, val = commands.dt("fertility", center.x, center.y, center.z, life);
end
function getCode()
pass, val = commands.dt("gettree", center.x, center.y, center.z, true);
if (pass) then
return val[1];
end
return nil;
end
function storeCodeHttp(tree, code, radius)
code = string.gsub(code, "+", "%%2b");
http.request("http://127.0.0.1/trees/trees.php?".. "tree="..tree.."&code="..code.."&radius="..radius, nil);
end
function storeCodeConsole(tree, code, radius)
print(tree);
print(radius .. ":" .. code);
print("----------------------");
end
function storeCodeFile(tree, code, radius)
local s = fs.open(treeFilename(), "a");
s.write(radius .. ":" .. code .. "\n");
s.close();
end
function storeCode(tree, code, radius)
-- storeCodeHttp(tree, code, radius);
storeCodeConsole(tree, code, radius);
storeCodeFile(tree, code, radius);
end
function harvest()
code = getCode();
kill();
sound("entity.item.pickup");
local codes = {};
codes[1] = code;
for i = 1,3 do
local _, c = commands.dt("rotatejocode", code, i);
codes[i + 1] = c[1];
end
table.sort(codes);
code = codes[1];
storeCode(tree, code, radius);
end
function file_exists(name)
local f=io.open(name,"r")
if f ~= nil then io.close(f) return true else return false end
end
function finishHarvest()
growButton:stop();
end
function onTimerEvent()
local fertility = getFertility();
soilButton:setFertility(fertility);
if (fertility <= 0 and grow == true) then
if(fertility == 0) then
harvest();
count = count - 1;
if count <= 0 then
if radius > 2 then
radiusDec();
close();
else
finishHarvest(); -- We're done
end
end
countButton:setCount(count);
end
if grow then
plant();
end
end
end
spinner = 0;
spinnerSet = { 129, 130, 136, 259, 144, 132 };
function parallelTimer()
startTimer();
while true do
event = os.pullEvent("timer");
onTimerEvent();
monitor.setCursorPos(28, 2);
spinner = (spinner + 1) % 6;
s = spinnerSet[spinner + 1];
if s > 200 then
s = s - 100;
monitor.setBackgroundColor(colors.orange);
monitor.setTextColor(colors.gray);
else
monitor.setBackgroundColor(colors.gray);
monitor.setTextColor(colors.orange);
end
monitor.write(string.char(s));
monitor.setTextColor(colors.white);
startTimer();
end
end
function parallelClick()
while true do
event, par1, xPos, yPos = os.pullEvent("monitor_touch");
clickScreen(xPos, yPos);
end
end
purge();
close();
parallel.waitForAll(parallelTimer, parallelClick);