View difference between Paste ID: XtndSkSp and QWgvVuJT
SHOW: | | - or go back to the newest paste.
1
modem = peripheral.wrap("left")
2
modem.open(5555)
3
4
function setup()
5
    modem.open(403)
6
7
    accepted = false
8
    fullSlotCounter = 0
9
    temp = 0
10
    awaitVar = true
11
end
12
13
14
function await(x)
15
    awaitVar = true
16
    while awaitVar == true do
17
        event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
18
        if message == x then
19
            awaitVar = false
20
        end
21
    end
22
end
23
24
25
function getValues()  
26
    event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
27
    width = tonumber(message)
28
29
    event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
30
    height = tonumber(message)
31
32
    event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
33
    depth = tonumber(message)
34
end
35
36
37
function calculateWidth()
38
    print("calculating")
39
40-
    if width % 80 == 0 then
40+
	slaveAmount = turtle.getItemCount(13)
41-
        slaveWidth = width / 80
41+
42
    if width % slaveAmount == 0 then
43-
        slaveAmount = 80
43+
        slaveWidth = width / slaveAmount
44-
    elseif width < 81 then
44+
45-
        slaveWidth = 1
45+
    elseif width < slaveAmount then
46
        i = 0
47-
        slaveAmount = width
47+
		while not (((width - i) % slaveAmount - 1) == 0) do
48-
    else 
48+
			i = i + 1
49
		end
50-
        slaveWidth = (width - (width % i)) / i
50+
		lastSlaveWidth = slaveWidth + i
51-
        lastSlaveWidth = slaveWidth + width % i
51+
52-
        if slaveWidth > lastSlaveWidth then
52+
53-
            temp = slaveWidth
53+
54
    print(slaveWidth)
55
    print(lastSlaveWidth)
56-
        for i = 1, slaveAmount, 1 do
56+
57-
            
57+
58-
            slaveWidth = (width - (width % i)) / i
58+
59-
            lastSlaveWidth = slaveWidth + width % i
59+
60
end
61-
            if slaveWidth > lastSlaveWidth then
61+
62-
                temp = slaveWidth
62+
63-
            end
63+
64-
            
64+
65-
            
65+
66-
            
66+
67-
            if width - (width % i) / i < temp then
67+
68-
                slaveWidth = (width - (width % i)) / i
68+
69-
                lastSlaveWidth = slaveWidth + width % i
69+
70
    if turtle.getFuelLevel() < 801 then
71-
                if slaveWidth > lastSlaveWidth then
71+
72-
                    temp = slaveWidth
72+
73-
                else 
73+
74-
                    temp = lastSlaveWidth
74+
75-
                end
75+
76-
            end
76+
77-
            
77+
78
function checkInv()
79
    if turtle.getItemCount(14) < 8 then
80
        enderChests()
81
    elseif turtle.getItemCount(8) > 0 then
82
        enderChests()
83
    end
84
        
85
        
86
    fullSlotCounter = 0
87
    checkFuel()
88
end
89
90
91
92
function enderChests()
93
    -- Unload slot 1 - 10
94
    turtle.dig()
95
    turtle.select(15)
96
    turtle.place()
97
    for i = 1, 10 , 1 do
98
        turtle.select(i)
99
        turtle.drop()
100
    end
101
    turtle.select(15)
102
    turtle.dig()
103
104
    -- Load Slot 14 with coal
105
    turtle.select(16)
106
    turtle.place()
107
    
108
    if turtle.getItemCount(14) < 64 then
109
        turtle.select(14)
110
        turtle.suck(64 - turtle.getItemCount(14))
111
    end
112
    
113
    turtle.select(16)
114
    turtle.dig()
115
end
116
117
118
function buildSpwan()
119
    print("building spawn...")
120
    turtle.select(1)
121
    turtle.turnLeft()
122
    turtle.dig()
123
    turtle.forward()
124
    turtle.turnRight()
125
    turtle.turnRight()
126
    turtle.digUp()
127
    turtle.digDown()
128
    turtle.select(12)
129
    turtle.placeDown()
130
    turtle.select(11)
131
    turtle.dropDown()
132
    turtle.up()
133
end
134
135
136
function summonSlaves()
137
    print("summoning slaves...")
138
    print(slaveAmount)
139
140
    for i = 1, slaveAmount, 1 do
141
        checkInv()
142
		turtle.select(13)
143
        turtle.placeDown()
144
145
        print("slave summoned")
146
147
        peripheral.call('bottom','turnOn')
148
149
        sleep(1.5)
150
        turtle.select(15)
151
        turtle.dropDown(1)
152
        sleep(.5)
153
        turtle.select(16)
154
        turtle.dropDown(1)
155
        sleep(.5)
156
        turtle.select(14)
157
        turtle.dropDown(4)
158
        
159
160
        await("listening-port")
161
        print("accept")
162
        modem.transmit(403, 403, 100 + i)
163
164
        if i == 1 then
165
            distanceToGo = width - lastSlaveWidth
166
        else
167
            distanceToGo = width - lastSlaveWidth - (slaveWidth * (i - 1)) 
168
        end
169
170
        print(distanceToGo)
171
172-
        turtle.select(13)
172+
173
        modem.transmit(100 + i, 100 + i, distanceToGo)
174
        
175
        if i == 1 then
176
            sleep((width / 3) * 2)
177
        end
178
        
179
        sleep(slaveWidth + 5)
180
    end
181
182
    sleep(15)
183
end
184
185
186
function destroySpawn()
187
    print("destroying spawn...")
188
    turtle.down()
189
    turtle.select(11)
190
    turtle.suckDown()
191
    turtle.select(12)
192
    turtle.digDown()
193
    checkInv()
194
end
195
196
197
function giveOrder()
198
    print("sending parameters")
199
    for i = 1, slaveAmount, 1 do
200
        modem.transmit(100 + i, 100 + i, "values-incoming")
201
        
202
        if i == 1 then
203
            modem.transmit(100 + i, 100 + i, lastSlaveWidth)
204
            modem.transmit(100 + i, 100 + i, height)
205
            modem.transmit(100 + i, 100 + i, depth)
206-
            sleep(width)
206+
207
            modem.transmit(100 + i, 100 + i, slaveWidth)
208
            modem.transmit(100 + i, 100 + i, height)
209-
        sleep(2)
209+
210
        end
211
    end
212-
    sleep(width + 3)
212+
213
214
function eatSlaves()
215
    for i = 1, slaveAmount, 1 do
216
        await("arrived-home")
217
    end
218
    
219
    for i = 1, width, 1 do
220
        turtle.forward()
221
        turtle.dig()
222
    end
223
    turtle.turnRight()
224
    turtle.turnRight()
225
    for i = 1, width, 1 do
226
        turtle.forward()
227
        turtle.dig()
228
    end
229
    turtle.turnRight()
230
end
231
232
233
234
-- Actual program
235
236
await("start-mining")
237
238
getValues()
239
calculateWidth()
240
approximitTime()
241
242
checkInv()
243
setup()
244
245
buildSpwan()
246
summonSlaves()
247
    
248
giveOrder()
249
sleep(15)
250
destroySpawn()
251
252
253
eatSlaves()
254
checkInv()
255
sleep(5)