View difference between Paste ID: R2qhvY2r and FpcY1361
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(5)
10+
modem.open(11)
11-
modem.open(6)
11+
modem.open(12)
12-
modem.open(7)
12+
modem.open(13)
13-
modem.open(8)
13+
modem.open(14)
14-
print("Channel 5 and 6 are open, as well as 7 and 8")
14+
print("Channel 11 and 12 are open, as well as 13 and 14")
15
16
-- Setup sending wireless messages
17
function SendMessage(message)
18-
	modem.transmit(5, 6, message)
18+
	modem.transmit(11, 12, message)
19
end
20
function SendMessageToPocketComputer(message)
21-
	modem.transmit(7, 7, message)
21+
	modem.transmit(13, 13, 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-
-- Begin when user is ready
66+
67-
printf("Press any key when ready to begin >>")
67+
printf("Sending reset Y signal")
68-
read()
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
    modem.transmit(8, 8, "Starting mine cycle #" .. tostring(cycleCount))
89
    SendMessage("_StartNextCycle")
90
    printf("Waiting for turtles to be ready for next cycle...")
91
    readyCount = 0
92
    while true do
93
    	WaitForMessage("_ReadyForNextCycle")
94
    	readyCount = readyCount + 1
95
    	printf("A turtle is ready. Ready count is " .. tostring(readyCount))
96
		if(readyCount == turtleCount) then
97
        	printf("All turtles ready")
98
            break 
99
        end
100
	end
101
    if(cycleCount == desiredCycleCount) then 
102
        printf("All cycle complete")
103
        break
104
    end
105
	cycleCount = cycleCount + 1
106
    printf(">>")
107
    printf(">>")
108
    printf(">>")
109
end
110
111
-- Done
112
printf("Goodbye >>")
113
read()