· 6 years ago · Dec 03, 2019, 07:58 PM
1os.loadAPI("api/filesystem") -- loading Filesystem API, for easy store and load tables
2rednet.open("back")
3TurtleID = 27 -- ID of the Felling-Turtle assigned to this Farm
4turtle = {}
5turtle.fuel = 67
6turtle.state = "not connected"
7turtle.x = -64
8turtle.y = 72
9turtle.z = -1174
10turtle.inv = {}
11for i = 1, 16 do
12 turtle.inv[i] = {
13 name = "empty";
14 amount = 0;
15 }
16end
17
18growInfo = "Please select a tree"
19
20-- ===================== INITIATION OF "tree" - Object =================
21local tree = {
22 tree_defaults = {
23 __index = {
24 state = "planted";
25 growTime = 0;
26 plantTime = 0;
27 };
28 };
29 mt = {
30 __call = function(self)
31
32 end;
33
34 __newindex = function(t, key, value)
35 assert(type(value)=="table", "Requires a table")
36 assert(value.x, "Requires inital x")
37 assert(value.y, "Requires inital y")
38 assert(value.z, "Requires inital z")
39 assert(value.f, "Requires inital f")
40 setmetatable(value, t.tree_defaults)
41 rawset(t.trees, key, value)
42 end;
43 };
44 trees = {};
45 saveTrees = function(self)
46 filesystem.save(self.trees,"treefarm/trees.cfg")
47 end;
48 loadTrees = function(self)
49 self.trees = filesystem.load("treefarm/trees.cfg")
50 end;
51
52}
53setmetatable(tree, tree.mt)
54-- ==================== INITIATION OF "buttons" - Object =======================
55local button = {
56 button_defaults = {
57 __index = {
58 color_bg = colors.orange;
59 color_cl = colors.blue;
60 color_txt = colors.black;
61 height = 3;
62 padding = 2;
63 isClicked = false;
64 active = true;
65 id = -1
66 };
67 };
68 mt = {
69 __call = function(self)
70 for index, btn in pairs(self.buttons) do
71 if btn.active then
72 local color = btn.isClicked and btn.color_cl or btn.color_bg
73 term.setBackgroundColor(color)
74 term.setTextColor(btn.color_txt)
75 for yPos = btn.y, btn.bounds.y2 do
76 term.setCursorPos(btn.x, yPos)
77 term.write(string.rep(" ", btn.width))
78 end
79 local text = btn.isClicked and btn.clickText or btn.text
80 term.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
81 term.write(text)
82 end
83 end
84 end;
85
86 __newindex = function(t, key, value)
87 assert(type(value)=="table", "Requires a table")
88 assert(value.x, "Requires inital x")
89 assert(value.y, "Requires inital y")
90 assert(value.text, "Requires text value")
91 setmetatable(value, t.button_defaults)
92 value.width = #value.text + (value.padding * 2)
93 value.bounds = {
94 x1 = value.x;
95 y1 = value.y;
96 x2 = value.x + value.width - 1;
97 y2 = value.y + value.height - 1;
98 }
99
100 rawset(t.buttons, key, value)
101 end;
102 };
103
104 checkClick = function(self,x,y)
105 for index, btn in pairs(self.buttons) do
106 if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 and btn.active then
107 btn.isClicked = true
108 if btn.onClick then
109 btn:onClick()
110 end
111 return index
112 end
113 end
114 end;
115 buttons = {};
116}
117setmetatable(button, button.mt)
118
119-- ================== DATASET OF BUTTONS ================================
120
121
122button[1] = {
123 x = 3;
124 y = 5;
125 text = "TreeOverview";
126 onClick = function()
127 currentMenu = "TreeOverview"
128 end;
129 active = false;
130 menu = "main";
131}
132button[2] = {
133 x = 20;
134 y = 5;
135 text = "TurtleOverview";
136 onClick = function()
137 if turtle.fuel <= 50 then
138 button.buttons[23].color_txt = colors.red
139 end
140 currentMenu = "TurtleOverview"
141 end;
142 active = false;
143 menu = "main";
144}
145button[3] = {
146 x = 2;
147 y = 17;
148 text = "MainMenu";
149 onClick = function()
150 currentMenu = "main"
151 end;
152 active = false;
153 menu = "subs";
154}
155button[4] = {
156 x = 2;
157 y = 5;
158 id = 1;
159 height = 3;
160 width = 4;
161 text = "T 1";
162 active = false;
163 menu = "TreeOverview";
164 padding = 1;
165 color_bg = colors.lightGray;
166 color_txt = colors.black;
167 color_cl = colors.lime;
168 onClick = function()
169 growInfo = "Tree 1 .. in state .. "..tree.trees[1].state
170 end;
171}
172button[5] = {
173 x = 9;
174 y = 5;
175 id = 2;
176 height = 3;
177 width = 4;
178 text = "T 2";
179 active = false;
180 menu = "TreeOverview";
181 padding = 1;
182 color_bg = colors.lightGray;
183 color_txt = colors.black;
184 color_cl = colors.lime;
185 onClick = function()
186 growInfo = "Tree 2 .. in state .. "..tree.trees[2].state
187 end;
188}
189button[6] = {
190 x = 16;
191 y = 5;
192 id = 3;
193 height = 3;
194 width = 4;
195 text = "T 3";
196 active = false;
197 menu = "TreeOverview";
198 padding = 1;
199 color_bg = colors.lightGray;
200 color_txt = colors.black;
201 color_cl = colors.lime;
202 onClick = function()
203 growInfo = "Tree 3 .. in state .. "..tree.trees[3].state
204 end;
205}
206button[7] = {
207 x = 23;
208 y = 5;
209 id = 4;
210 height = 3;
211 width = 4;
212 text = "T 4";
213 active = false;
214 menu = "TreeOverview";
215 padding = 1;
216 color_bg = colors.lightGray;
217 color_txt = colors.black;
218 color_cl = colors.lime;
219 onClick = function()
220 growInfo = "Tree 4 .. in state .. "..tree.trees[4].state
221 end;
222}
223button[8] = {
224 x = 30;
225 y = 5;
226 id = 5;
227 height = 3;
228 width = 4;
229 text = "T 5";
230 active = false;
231 menu = "TreeOverview";
232 padding = 1;
233 color_bg = colors.lightGray;
234 color_txt = colors.black;
235 color_cl = colors.lime;
236 onClick = function()
237 growInfo = "Tree 5 .. in state .. "..tree.trees[5].state
238 end;
239}
240button[9] = {
241 x = 37;
242 y = 5;
243 id = 6;
244 height = 3;
245 width = 4;
246 text = "T 6";
247 active = false;
248 menu = "TreeOverview";
249 padding = 1;
250 color_bg = colors.lightGray;
251 color_txt = colors.black;
252 color_cl = colors.lime;
253 onClick = function()
254 growInfo = "Tree 6 .. in state .. "..tree.trees[6].state
255 end;
256}
257button[10] = {
258 x = 2;
259 y = 9;
260 id = 7;
261 height = 3;
262 width = 4;
263 text = "T 7";
264 active = false;
265 menu = "TreeOverview";
266 padding = 1;
267 color_bg = colors.lightGray;
268 color_txt = colors.black;
269 color_cl = colors.lime;
270 onClick = function()
271 growInfo = "Tree 7 .. in state .. "..tree.trees[7].state
272 end;
273}
274button[11] = {
275 x = 9;
276 y = 9;
277 id = 8;
278 height = 3;
279 width = 4;
280 text = "T 8";
281 active = false;
282 menu = "TreeOverview";
283 padding = 1;
284 color_bg = colors.lightGray;
285 color_txt = colors.black;
286 color_cl = colors.lime;
287 onClick = function()
288 growInfo = "Tree 8 .. in state .. "..tree.trees[8].state
289 end;
290}
291button[12] = {
292 x = 16;
293 y = 9;
294 id = 9;
295 height = 3;
296 width = 4;
297 text = "T 9";
298 active = false;
299 menu = "TreeOverview";
300 padding = 1;
301 color_bg = colors.lightGray;
302 color_txt = colors.black;
303 color_cl = colors.lime;
304 onClick = function()
305 growInfo = "Tree 9 .. in state .. "..tree.trees[9].state
306 end;
307}
308button[13] = {
309 x = 23;
310 y = 9;
311 id = 10;
312 height = 3;
313 width = 4;
314 text = "T10";
315 active = false;
316 menu = "TreeOverview";
317 padding = 1;
318 color_bg = colors.lightGray;
319 color_txt = colors.black;
320 color_cl = colors.lime;
321 onClick = function()
322 growInfo = "Tree 10 .. in state .. "..tree.trees[10].state
323 end;
324}
325button[14] = {
326 x = 30;
327 y = 9;
328 id = 11;
329 height = 3;
330 width = 4;
331 text = "T11";
332 active = false;
333 menu = "TreeOverview";
334 padding = 1;
335 color_bg = colors.lightGray;
336 color_txt = colors.black;
337 color_cl = colors.lime;
338 onClick = function()
339 growInfo = "Tree 11 .. in state .. "..tree.trees[11].state
340 end;
341}
342button[15] = {
343 x = 37;
344 y = 9;
345 id = 12;
346 height = 3;
347 width = 4;
348 text = "T12";
349 active = false;
350 menu = "TreeOverview";
351 padding = 1;
352 color_bg = colors.lightGray;
353 color_txt = colors.black;
354 color_cl = colors.lime;
355 onClick = function()
356 growInfo = "Tree 12 .. in state .. "..tree.trees[12].state
357 end;
358}
359button[16] = {
360 x = 2;
361 y = 13;
362 id = 13;
363 height = 3;
364 width = 4;
365 text = "T13";
366 active = false;
367 menu = "TreeOverview";
368 padding = 1;
369 color_bg = colors.lightGray;
370 color_txt = colors.black;
371 color_cl = colors.lime;
372 onClick = function()
373 growInfo = "Tree 13 .. in state .. "..tree.trees[13].state
374 end;
375}
376button[17] = {
377 x = 9;
378 y = 13;
379 id = 14;
380 height = 3;
381 width = 4;
382 text = "T14";
383 active = false;
384 menu = "TreeOverview";
385 padding = 1;
386 color_bg = colors.lightGray;
387 color_txt = colors.black;
388 color_cl = colors.lime;
389 onClick = function()
390 growInfo = "Tree 14 .. in state .. "..tree.trees[14].state
391 end;
392}
393button[18] = {
394 x = 16;
395 y = 13;
396 id = 15;
397 height = 3;
398 width = 4;
399 text = "T15";
400 active = false;
401 menu = "TreeOverview";
402 padding = 1;
403 color_bg = colors.lightGray;
404 color_txt = colors.black;
405 color_cl = colors.lime;
406 onClick = function()
407 growInfo = "Tree 15 .. in state .. "..tree.trees[15].state
408 end;
409}
410button[19] = {
411 x = 23;
412 y = 13;
413 id = 16;
414 height = 3;
415 width = 4;
416 text = "T16";
417 active = false;
418 menu = "TreeOverview";
419 padding = 1;
420 color_bg = colors.lightGray;
421 color_txt = colors.black;
422 color_cl = colors.lime;
423 onClick = function()
424 growInfo = "Tree 16 .. in state .. "..tree.trees[16].state
425 end;
426}
427button[20] = {
428 x = 30;
429 y = 13;
430 id = 17;
431 height = 3;
432 width = 4;
433 text = "T17";
434 active = false;
435 menu = "TreeOverview";
436 padding = 1;
437 color_bg = colors.lightGray;
438 color_txt = colors.black;
439 color_cl = colors.lime;
440 onClick = function()
441 growInfo = "Tree 17 .. in state .. "..tree.trees[17].state
442 end;
443}
444button[21] = {
445 x = 37;
446 y = 13;
447 id = 18;
448 height = 3;
449 width = 4;
450 text = "T18";
451 active = false;
452 menu = "TreeOverview";
453 padding = 1;
454 color_bg = colors.lightGray;
455 color_txt = colors.black;
456 color_cl = colors.lime;
457 onClick = function()
458 growInfo = "Tree 18 .. in state .. "..tree.trees[18].state
459 end;
460}
461button[22] = {
462 x = 35;
463 y = 9;
464 text = "View Inventory";
465 padding = 1;
466 active = false;
467 menu = "TurtleOverview";
468 color_bg = colors.lightGray;
469 color_txt = colors.black;
470 color_cl = colors.lime;
471}
472button[23] = {
473 x = 35;
474 y = 5;
475 text = "Take Fuel";
476 padding = 1;
477 active = false;
478 menu = "TurtleOverview";
479 color_bg = colors.lightGray;
480 color_txt = colors.black;
481 color_cl = colors.lime;
482 onClick = function()
483 msg = {}
484 msg.owner = "turtle"
485 msg.subject = "take fuel"
486 send(msg)
487 end;
488}
489button[24] = {
490 x = 1;
491 y = 18;
492 text = "Quit";
493 active = false;
494 menu = "main";
495 color_bg = colors.lightGray;
496 color_txt = colors.black;
497 color_cl = colors.lime;
498 onClick = function()
499 os.reboot()
500 end;
501 padding = 1;
502 height = 3;
503}
504-- ================ DATASET OF TREES =========================================
505tree[1] = {
506 x = -63;
507 y = 72;
508 z = -1174;
509 f = 0;
510 state = "grown";
511}
512tree[2] = {
513 x = -63;
514 y = 72;
515 z = -1169;
516 f = 0;
517 state = "growing";
518}
519tree[3] = {
520 x = -63;
521 y = 72;
522 z = -1164;
523 f = 0;
524}
525tree[4] = {
526 x = -68;
527 y = 72;
528 z = -1174;
529 f = 0;
530}
531tree[5] = {
532 x = -68;
533 y = 72;
534 z = -1169;
535 f = 0;
536}
537tree[6] = {
538 x = -68;
539 y = 72;
540 z = -1164;
541 f = 0;
542}
543tree[7] = {
544 x = -73;
545 y = 72;
546 z = -1174;
547 f = 0;
548}
549tree[8] = {
550 x = -73;
551 y = 72;
552 z = -1169;
553 f = 0;
554}
555tree[9] = {
556 x = -73;
557 y = 72;
558 z = -1164;
559 f = 0;
560}
561tree[10] = {
562 x = -78;
563 y = 72;
564 z = -1174;
565 f = 0;
566}
567tree[11] = {
568 x = -78;
569 y = 72;
570 z = -1169;
571 f = 0;
572}
573tree[12] = {
574 x = -78;
575 y = 72;
576 z = -1164;
577 f = 0;
578}
579tree[13] = {
580 x = -83;
581 y = 72;
582 z = -1174;
583 f = 0;
584}
585tree[14] = {
586 x = -83;
587 y = 72;
588 z = -1169;
589 f = 0;
590}
591tree[15] = {
592 x = -83;
593 y = 72;
594 z = -1164;
595 f = 0;
596}
597tree[16] = {
598 x = -88;
599 y = 72;
600 z = -1174;
601 f = 0;
602}
603tree[17] = {
604 x = -88;
605 y = 72;
606 z = -1169;
607 f = 0;
608}
609tree[18] = {
610 x = -88;
611 y = 72;
612 z = -1164;
613 f = 0;
614}
615
616-- ============= INITIAL SECTION ABOVE =========================================
617
618function mainMenu()
619 term.setTextColour(colours.yellow)
620 width,height = term.getSize()
621 term.clear()
622 term.setCursorPos(1,1)
623 for i = 1,width do
624 term.write("#")
625 end
626 term.setCursorPos(1,3)
627 for i = 1,width do
628 term.write("#")
629 end
630 title = " TreeFarm Server > Main Menu "
631 term.setCursorPos(width/2-#title/2,2)
632 term.setTextColour(colours.cyan)
633 term.write(title)
634 term.setTextColour(colours.yellow)
635 term.setCursorPos(1,2)
636 term.write("#")
637 term.setCursorPos(width,2)
638 term.write("#")
639 term.setCursorPos(1,5)
640 for index, btn in pairs(button.buttons) do
641 if btn.menu == "main" then
642 btn.active = true
643 else
644 btn.active = false
645 end
646 end
647
648end
649
650
651
652
653function TurtleOverview()
654 term.setTextColour(colours.yellow)
655 width,height = term.getSize()
656 term.clear()
657 term.setCursorPos(1,1)
658 for i = 1,width do
659 term.write("#")
660 end
661 term.setCursorPos(1,3)
662 for i = 1,width do
663 term.write("#")
664 end
665 title = " TreeFarm Server > TurtleOverview"
666 term.setCursorPos(width/2-#title/2,2)
667 term.setTextColour(colours.cyan)
668 term.write(title)
669 term.setTextColour(colours.yellow)
670 term.setCursorPos(1,2)
671 term.write("#")
672 term.setCursorPos(width,2)
673 term.write("#")
674 term.setCursorPos(3,5)
675 term.setTextColor(colors.white)
676 term.write("Fuel in turtle: ")
677 if turtle.fuel <= 50 then
678 term.setTextColor(colors.red)
679 else
680 term.setTextColor(colors.lime)
681 end
682 term.write(turtle.fuel)
683 local AllItems = 0
684 local EmptySlots = 0
685 for i = 1,16 do
686 AllItems = turtle.inv[i].amount + AllItems
687 if turtle.inv[i].amount == 0 then
688 EmptySlots = EmptySlots + 1
689 end
690 end
691 term.setCursorPos(3,7)
692 term.setTextColor(colors.white)
693 term.write("Total Count of Items: ")
694 term.setTextColor(colors.lime)
695 term.write(AllItems)
696 term.setCursorPos(3,9)
697 term.setTextColor(colors.white)
698 term.write("Amount of Empty Slots: ")
699 term.setTextColor(colors.lime)
700 term.write(EmptySlots)
701 term.setCursorPos(3,11)
702 term.setTextColor(colors.white)
703 term.write("X: ")
704 term.setTextColor(colors.lime)
705 term.write(turtle.x)
706 term.setTextColor(colors.white)
707 term.write(" Y: ")
708 term.setTextColor(colors.lime)
709 term.write(turtle.y)
710 term.setTextColor(colors.white)
711 term.write(" Z: ")
712 term.setTextColor(colors.lime)
713 term.write(turtle.z)
714 term.setCursorPos(3,13)
715 term.setTextColor(colors.white)
716 term.write("Current state: ")
717 term.setTextColor(colors.lime)
718 term.write(turtle.state)
719 for index, btn in pairs(button.buttons) do
720 if btn.menu == "TurtleOverview" or btn.menu == "subs" then
721 btn.active = true
722 else
723 btn.active = false
724 end
725 end
726end
727
728function TreeOverview()
729 term.setTextColour(colours.yellow)
730 width,height = term.getSize()
731 term.clear()
732 term.setCursorPos(1,1)
733 for i = 1,width do
734 term.write("#")
735 end
736 term.setCursorPos(1,3)
737 for i = 1,width do
738 term.write("#")
739 end
740 title = " TreeFarm Server > TreeOverview "
741 term.setCursorPos(width/2-#title/2,2)
742 term.setTextColour(colours.cyan)
743 term.write(title)
744 term.setTextColour(colours.yellow)
745 term.setCursorPos(1,2)
746 term.write("#")
747 term.setCursorPos(width,2)
748 term.write("#")
749 term.setCursorPos(15,17)
750 term.setTextColor(colors.white)
751 term.write(growInfo)
752 for index, btn in pairs(button.buttons) do
753 if btn.menu == "TreeOverview" or btn.menu == "subs" then
754 btn.active = true
755 else
756 btn.active = false
757 end
758 end
759end
760
761function turtleDetail()
762 term.setTextColour(colours.yellow)
763 width,height = term.getSize()
764 term.clear()
765 term.setCursorPos(1,1)
766 for i = 1,width do
767 term.write("#")
768 end
769 term.setCursorPos(1,3)
770 for i = 1,width do
771 term.write("#")
772 end
773 term.setCursorPos(width/2-7,2)
774 term.setTextColour(colours.cyan)
775 term.write("TreeFarm Server")
776 term.setTextColour(colours.yellow)
777 term.setCursorPos(1,2)
778 term.write("#")
779 term.setCursorPos(width,2)
780 term.write("#")
781 term.setCursorPos(1,5)
782 for index, btn in pairs(button.buttons) do
783 if btn.menu == "main" then
784 btn.active = true
785 else
786 btn.active = false
787 end
788 end
789end
790
791function treeDetail()
792 term.setTextColour(colours.yellow)
793 width,height = term.getSize()
794 term.clear()
795 term.setCursorPos(1,1)
796 for i = 1,width do
797 term.write("#")
798 end
799 term.setCursorPos(1,3)
800 for i = 1,width do
801 term.write("#")
802 end
803 term.setCursorPos(width/2-7,2)
804 term.setTextColour(colours.cyan)
805 term.write("TreeFarm Server")
806 term.setTextColour(colours.yellow)
807 term.setCursorPos(1,2)
808 term.write("#")
809 term.setCursorPos(width,2)
810 term.write("#")
811 term.setCursorPos(1,5)
812 for index, btn in pairs(button.buttons) do
813 if btn.menu == "main" then
814 btn.active = true
815 else
816 btn.active = false
817 end
818 end
819end
820
821function send(msg)
822 if msg.owner == "turtle" then
823 rednet.send(TurtleID,msg)
824 end
825end
826
827function recieve(id, data)
828 if id == TurtleID then -- check if the msg is from the Felling-Turtle
829 if data.subject == "turtleState" then -- checking the SUBJECT of the MSG
830 turtle.state = data.state -- saving turtle-Stats
831 turtle.x = data.x
832 turtle.y = data.y
833 turtle.z = data.z
834 turtle.fuel = data.fuel
835 turtle.inv = data.inv
836 elseif data.subject == "treeState" then
837 for index,baum in pairs(tree.trees) do -- if it's a tree, find the specified in the DATASET
838 if index == data.index then
839 baum.state = data.state or baum.state -- if no state, then take known state
840 if baum.state == "planted" then
841 baum.state = "growing" -- if a planted tree checked twice, set state to "growing"
842 end
843 if baum.state == "new planted" then
844 baum.plantTime = os.clock() -- if it's fresh planted, then save the time of planting
845 baum.state = "planted" -- set to planted for display!
846 end
847 baum.growTime = os.clock() - baum.plantTime
848 if baum.state == "grown" then
849 local file = fs.open("treefarm/tree.log","a")
850 file.write("Tree felled after "..baum.growTime.." seconds of growing!")
851 file.close() -- save the time of every tree to grown
852 harvest(index) -- this will send the turtle to the tree to harvest...
853 end
854 end
855 end
856 end
857 end
858end
859
860local timer = {
861 index = false;
862 timer = false;
863}
864
865function drawMenu(menu)
866 if menu == "main" then
867 mainMenu()
868 elseif menu == "turtleDetail" then
869 turtleDetail()
870 elseif menu == "TurtleOverview" then
871 TurtleOverview()
872 elseif menu == "TreeOverview" then
873 TreeOverview()
874 elseif menu == "treeDetail" then
875 treeDetail()
876 end
877end
878
879function harvest(id)
880 local msg = {}
881 msg.owner = "turtle"
882 msg.subject = "harvest"
883 msg.x = tree.trees[id].x
884 msg.y = tree.trees[id].y
885 msg.z = tree.trees[id].z
886 msg.f = tree.trees[id].f
887 if turtle.state == "waiting for command" then
888 send(msg)
889 end
890end
891
892currentMenu = "main"
893while true do
894 term.setBackgroundColor(colors.black)
895 term.clear()
896 drawMenu(currentMenu)
897 button()
898 local e = {os.pullEvent()}
899 if e[1] == "mouse_click" then
900 local index = button:checkClick(e[3], e[4])
901 if index then
902 timer.index = index
903 timer.timer = os.startTimer(0.2)
904 end
905 elseif e[1] == "timer" and e[2] == timer.timer then
906 button.buttons[timer.index].isClicked = false
907 timer = {}
908 elseif e[1] == "rednet_message" then
909 recieve(e[2],e[3])
910 end
911end