SHOW:
|
|
- or go back to the newest paste.
1 | -- Awake | |
2 | print("Mining server awake: Hello World!") | |
3 | ||
4 | -- Setup wireless | |
5 | local modemSide = "left" | |
6 | local modem = peripheral.wrap(modemSide) | |
7 | -- 5 Server -> Client | |
8 | -- 6 Client -> Server | |
9 | -- 7 Server -> Pocket Computer | |
10 | - | modem.open(11) |
10 | + | modem.open(110) |
11 | - | modem.open(12) |
11 | + | modem.open(120) |
12 | - | modem.open(13) |
12 | + | modem.open(130) |
13 | - | modem.open(14) |
13 | + | modem.open(140) |
14 | - | print("Channel 11 and 12 are open, as well as 13 and 14") |
14 | + | print("Channel 110 and 120 are open, as well as 130 and 140") |
15 | ||
16 | -- Setup sending wireless messages | |
17 | function SendMessage(message) | |
18 | - | modem.transmit(11, 12, message) |
18 | + | modem.transmit(110, 120, message) |
19 | end | |
20 | function SendMessageToPocketComputer(message) | |
21 | - | modem.transmit(13, 13, message) |
21 | + | modem.transmit(130, 130, message) |
22 | end | |
23 | ||
24 | --Pocket computer helper | |
25 | function printf(m) | |
26 | print(m) | |
27 | SendMessageToPocketComputer(m) | |
28 | end | |
29 | printf("Link to pocket computer established. Hello") | |
30 | ||
31 | -- Setup receiving wireless messages | |
32 | function WaitForMessage(messageCompare) | |
33 | while true do | |
34 | local | |
35 | a,b,c,d,e,f = os.pullEvent("modem_message") | |
36 | printf("Message received: " .. tostring(e)) | |
37 | if(e == messageCompare) then | |
38 | break | |
39 | end | |
40 | end | |
41 | end | |
42 | ||
43 | -- Get turtle amount from users | |
44 | printf("How many turtles are mining? >>") | |
45 | local desiredTurtleCount = tonumber(read()) | |
46 | ||
47 | -- Get cycle amount from users | |
48 | printf("How many cycles should they mine for? >>") | |
49 | local desiredCycleCount = tonumber(read()) | |
50 | ||
51 | -- Send awake signal | |
52 | printf("Sending awake signal to all turtles...") | |
53 | SendMessage("_ServerAwake") | |
54 | ||
55 | -- Wait for all turtles to register | |
56 | -- Accept and continue from user | |
57 | printf("Waiting for turtles to register...") | |
58 | local turtleCount = 0 | |
59 | while true do | |
60 | WaitForMessage("_TurtleRegistered") | |
61 | turtleCount = turtleCount + 1 | |
62 | printf("Turtle registered. Count is " .. tostring(turtleCount)) | |
63 | if(turtleCount == desiredTurtleCount) then break end | |
64 | end | |
65 | printf("All turtles registered") | |
66 | ||
67 | printf("Sending reset Y signal") | |
68 | SendMessage("_ResetY") | |
69 | ||
70 | -- Wait for all turtles to reset their y pos | |
71 | printf("Waiting for turtles to be ready to mine...") | |
72 | local turtleCount = 0 | |
73 | while true do | |
74 | WaitForMessage("_TurtleReadyToMine") | |
75 | turtleCount = turtleCount + 1 | |
76 | printf("Turtle ready to mine. Count is " .. tostring(turtleCount)) | |
77 | if(turtleCount == desiredTurtleCount) then break end | |
78 | end | |
79 | printf("All turtles ready to mine") | |
80 | ||
81 | -- Begin managing mine operations | |
82 | printf("Mining...") | |
83 | SendMessage("_Begin") | |
84 | local readyCount = 0 | |
85 | local cycleCount = 1 | |
86 | while true do | |
87 | printf("Starting mine cycle #" .. tostring(cycleCount)) | |
88 | SendMessage("_StartNextCycle") | |
89 | printf("Waiting for turtles to be ready for next cycle...") | |
90 | readyCount = 0 | |
91 | while true do | |
92 | WaitForMessage("_ReadyForNextCycle") | |
93 | readyCount = readyCount + 1 | |
94 | printf("A turtle is ready. Ready count is " .. tostring(readyCount)) | |
95 | if(readyCount == turtleCount) then | |
96 | printf("All turtles ready") | |
97 | break | |
98 | end | |
99 | end | |
100 | if(cycleCount == desiredCycleCount) then | |
101 | printf("All cycle complete") | |
102 | break | |
103 | end | |
104 | cycleCount = cycleCount + 1 | |
105 | printf(">>") | |
106 | printf(">>") | |
107 | printf(">>") | |
108 | end | |
109 | ||
110 | -- Done | |
111 | printf("Goodbye >>") | |
112 | read() |