View difference between Paste ID: YPXegXsH and DYm4u5VN
SHOW: | | - or go back to the newest paste.
1
--startup--
2
3
--variables--
4
5
local port = "right"
6
local rednetTimeout =0.5
7
local pingListenerEnabled = true
8
local unloadPosition = {x=3,y=-1,z=2}
9
local sleepTimer = 0.5
10
local storageChestCount = 4 --change also if you're using item collectors instead of chests
11
12
--initialize wlan--
13
14
rednet.open(port)
15
16
17
18
19
20
21
--functions--
22
23
function GetUnloadPosition()
24
    if unloadPosition.d ~= nil then
25
        return unloadPosition.x, unloadPosition.y, unloadPosition.z, unloadPosition.d
26
    else 
27
        return unloadPosition.x, unloadPosition.y, unloadPosition.z
28
    end
29
end
30
31
--Setup--
32
33
function SetupToNearestMasterPC() 
34
	masterID = nil
35
    ownerID = -1
36
	while masterID == nil do
37
    rednet.broadcast("pa setup locate")
38
        local startedTime = os.clock()
39
        while (os.clock() - startedTime) <= rednetTimeout do
40
            local id, msg, dis = rednet.receive(rednetTimeout)
41
            print("1")
42
            if (msg == "pa setup masterHere") then
43
                print(msg)
44
                if ownerID == -1 or smallestDis > dis then 
45
                    smallestDis = dis
46
                    ownerID = id
47
                    masterID = id
48
                end
49
            end
50
        end
51
    end
52
	if (smallestDis == 0 or ownerID == -1) then
53
		return false
54
	else return ownerID end
55
end
56
function GetJob()
57-
    local msg = nil
57+
    --local msg = nil
58-
    while msg == nil do
58+
    --while msg == nil do
59-
        local msg = SendAndGetResponse(masterID, "pa job request")
59+
    local msg = SendAndGetResponse(masterID, "pa job request")
60
    --end
61
    local splitMessage = SplitMessage(msg)
62
    if splitMessage[1] == "pa" then
63
        if splitMessage[2] == "job" then
64
            if splitMessage[3] == "do" then
65
                if splitMessage[4] == "TurtlePlacer" then
66
                    print("JobTurtlePlacer()")
67
                    SendMessage(masterID, "pa job confirm") 
68
                    while true do pcall(JobTurtlePlacer()) end
69
                
70
                elseif splitMessage[4] == "Digger" or splitMessage[4] == "Miner" then
71
                    print(msg)
72
                    jobx, joby, jobz, jobd, length = tonumber(splitMessage[5]), tonumber(splitMessage[6]), tonumber(splitMessage[7]), splitMessage[8], tonumber(splitMessage[9])
73
                    print("JobDigger(" .. jobx .. ", " .. joby .. ", " .. jobz .. ", "  .. jobd .. ", " .. length .. ")") 
74
                    SendMessage(masterID, "pa job confirm") 
75
                    if splitMessage[4] == "Digger" then 
76
                        JobDigger(jobx, joby, jobz, jobd, length)
77
                    else JobMiner(jobx, joby, jobz, jobd, length)
78
                    end
79
                end
80
            end
81
        end
82
    end
83
end
84
function DetermineDirection()
85
    if peripheral.getType("front") == "computer" then richtung = "-y"
86
    elseif peripheral.getType("left") == "computer" then richtung = "-x"
87
    elseif peripheral.getType("back") == "computer" then richtung = "y"
88
    elseif peripheral.getType("left") == "turtle" then richtung = "x"
89
    else
90
        turtle.turnRight()
91
        if peripheral.getType("front") == "computer" then richtung = "-y"
92
        else return
93
        end
94
    end
95
end
96
function GetFreshFuelFromChest(_count) --calcs var fuelvalue
97
    fuelSlot = 1
98
    Select(1)
99
    turtle.suck()
100
    local fuelLevel1 = turtle.getFuelLevel() 
101
    turtle.refuel(1)
102
    fuelValue = turtle.getFuelLevel() - fuelLevel1
103
    fuelCount = turtle.getItemCount(1)
104
end
105
106
--communication--
107
108
function SendAndGetResponse(_recID, _message) 
109
	SendMessage(_recID, _message)
110
	local startedTime = os.clock()
111
	while (os.clock() - startedTime) <= rednetTimeout do
112
		local id, msg, dis = rednet.receive(rednetTimeout)
113
		if id == _recID or _recID == 0 then
114
			local splitMessage = Split(msg, " ")
115
			if (splitMessage and splitMessage[1] == "pa") then
116
				return msg
117
			end
118
		end
119
	end
120
	return false
121
end
122
function SendAndGetResponseNoTimeout(_recID, _message) 
123
	sendMessage(_recID ,_message)
124
	while true do
125
		local id, msg, dis = rednet.receive(0)
126
		if id == _recID or _recID == 0 then
127
			local splitMessage = split(msg, " ")
128
			if (splitMessage[1] == "pa") then
129
				return msg
130
			end
131
		end
132
	end
133
end
134
function SendMessage(_recID, _message) 
135
	rednet.send(_recID, _message)
136
end
137
138
--fuelmanagement--
139
140
function Refuel() --inactive--
141
    --[[if fuelNeeded >= fuelCount * fuelValue * 0.6 then 
142
        turtle.fuelNeeded = true
143
        GoToStorage()
144
        TurtleAtStorageManager()
145
        GoToLastPlace()
146
    else
147
        if turtle.getFuelLevel() == 0 then
148
            Select(fuelSlot)
149
            if turtle.refuel(1) == true then
150
                fuelCount = fuelCount - 1
151
            end
152
            return true
153
        else 
154
            return false
155
        end
156
    end]]
157
    local refueled = OnlyRefuel()
158
    if refueled == true then 
159
        return true 
160
    else 
161
        return false
162
    end
163
end
164
function OnlyRefuel()
165
    if turtle.getFuelLevel() == 0 then
166
        Select(fuelSlot)
167
        if turtle.refuel(1) == true then
168
            fuelCount = fuelCount - 1
169
        return true
170
        end
171
    else 
172
        return false
173
    end
174
end
175
176
--processing--
177
178
function Split(str, pat)
179-
   local t = {}
179+
    if str then
180-
   local fpat = "(.-)" .. pat
180+
        local t = {}
181-
   local last_end = 1
181+
        local fpat = "(.-)" .. pat
182-
   local s, e, cap = str:find(fpat, 1)
182+
        local last_end = 1
183-
   while s do
183+
        local s, e, cap = str:find(fpat, 1)
184-
      if s ~= 1 or cap ~= "" then
184+
        while s do
185-
         table.insert(t,cap)
185+
            if s ~= 1 or cap ~= "" then
186-
      end
186+
                table.insert(t,cap)
187-
      last_end = e+1
187+
188-
      s, e, cap = str:find(fpat, last_end)
188+
            last_end = e+1
189-
   end
189+
            s, e, cap = str:find(fpat, last_end)
190-
   if last_end <= #str then
190+
191-
      cap = str:sub(last_end)
191+
        if last_end <= #str then
192-
      table.insert(t, cap)
192+
            cap = str:sub(last_end)
193-
   end
193+
            table.insert(t, cap)
194-
   return t
194+
195
        return t
196
    else
197
        return false
198
    end
199
end
200
function SplitMessage(_msg)
201
    return Split(_msg, " ")
202
end
203
204
--movement--
205
206
function Forward()
207
    while turtle.forward() == false do
208
        Refuel()
209
        if turtle.detect() == true then
210
            sleep(sleepTimer)
211
        end
212
    end
213
    if richtung == "x" then
214
        xPos = xPos + 1
215
    elseif richtung == "-x" then
216
        xPos = xPos - 1
217
    elseif richtung == "y" then
218
        yPos = yPos + 1   
219
    elseif richtung == "-y" then
220
        yPos = yPos - 1
221
    end
222
    return not turtle.detect()
223
end
224
function ForwardDig()
225
    while turtle.forward() == false do
226
        Refuel()
227
        if turtle.detect() == true then
228
            if peripheral.getType("front") == "turtle" then
229
                while peripheral.getType("front") == "turtle" do
230
                    sleep(sleepTimer)
231
                end
232
            else 
233
                Dig()
234
            end
235
        end
236
    end
237
    if richtung == "x" then
238
        xPos = xPos + 1
239
    elseif richtung == "-x" then
240
        xPos = xPos - 1
241
    elseif richtung == "y" then
242
        yPos = yPos + 1   
243
    elseif richtung == "-y" then
244
        yPos = yPos - 1
245
    end
246
    return not turtle.detect()
247
end
248
function Backward()
249
    Refuel()
250
    if turtle.back() == false then
251
        sleep(sleepTimer)
252
    end
253
    if richtung == "x" then
254
        xPos = xPos - 1
255
    elseif richtung == "-x" then
256
        xPos = xPos + 1
257
    elseif richtung == "y" then
258
        yPos = yPos - 1   
259
    elseif richtung == "-y" then
260
        yPos = yPos + 1
261
    end
262
end
263
function BackwardDig()
264
    Refuel()
265
    if turtle.back() == false then
266
        Right()
267
        Right()
268
        while turtle.forward() == false do
269
            if peripheral.getType("front") == "turtle" then
270
                while peripheral.getType("front") == "turtle" do
271
                    sleep(sleepTimer)
272
                end
273
            else 
274
                Dig()
275
            end
276
        end
277
        Right()
278
        Right()
279
    end
280
    if richtung == "x" then
281
        xPos = xPos - 1
282
    elseif richtung == "-x" then
283
        xPos = xPos + 1
284
    elseif richtung == "y" then
285
        yPos = yPos - 1   
286
    elseif richtung == "-y" then
287
        yPos = yPos + 1
288
    end
289
end
290
function Up()
291
    while turtle.up() == false do
292
        Refuel()
293
        sleep(sleepTimer)
294
    end
295
    zPos = zPos + 1
296
end
297
function UpDig() 
298
    Refuel()
299
    while turtle.up() == false do
300
        Refuel()
301
        if turtle.detectUp() == true then
302
            if peripheral.getType("top") == "turtle" then
303
                while peripheral.getType("top") == "turtle" do
304
                    sleep(sleepTimer)
305
                end
306
            else 
307
                DigUp()
308
            end
309
        end
310
    end
311
    zPos = zPos + 1
312
end
313
function Down()
314
    while turtle.down() == false do
315
        Refuel()
316
        sleep(sleepTimer)
317
    end
318
    zPos = zPos - 1
319
end
320
function DownDig()
321
    while turtle.down() == false do
322
        Refuel()
323
        if turtle.detectDown() == true then
324
            if peripheral.getType("bottom") == "turtle" then
325
                while peripheral.getType("bottom") == "turtle" do
326
                    sleep(sleepTimer)
327
                end
328
            else 
329
                DigDown()
330
            end
331
        end
332
    end
333
    zPos = zPos - 1
334
end
335
function Left()
336
    turtle.turnLeft()
337
    if richtung == "x" then
338
        richtung = "y"
339
    elseif richtung == "y" then
340
        richtung = "-x"
341
    elseif richtung == "-x" then
342
        richtung = "-y"
343
    else richtung = "x"
344
    end
345
end
346
function Right()
347
    turtle.turnRight()
348
    if richtung == "x" then
349
        richtung = "-y"
350
    elseif richtung == "y" then
351
        richtung = "x"
352
    elseif richtung == "-x" then
353
        richtung = "y"
354
    else richtung = "-x"
355
    end
356
end
357
function Dig()
358
    if peripheral.getType("front") == "turtle" then
359
        return turtle.detect()
360
    else 
361
        while turtle.dig() do
362
            if peripheral.getType("front") == "turtle" then 
363
                break 
364
            end
365
            sleep(0.5)
366
            if not turtle.detect() then
367
                return true
368
            end
369
        end
370
        return not turtle.detect()
371
    end
372
end
373
function DigUp()
374
    if peripheral.getType("top") == "turtle" then
375
        return turtle.detectUp()
376
    else 
377
        while turtle.digUp() do
378
            if peripheral.getType("top") == "turtle" then return turtle.detect() end
379
            sleep(0.5)
380
            if not turtle.detectUp() then
381
                return true
382
            end
383
        end
384
        return not turtle.detectUp()
385
    end
386
end
387
function DigDown()
388
    if peripheral.getType("bottom") == "turtle" then
389
        return turtle.detectDown()
390
    else
391
        turtle.digDown()
392
    end
393
    return not turtle.detectDown()
394
end
395
function LookDirection(_direction)
396
    if ((_direction == "x" and richtung == "-y") or (_direction == "-x" and richtung == "y") or (_direction == "y" and richtung == "x") or (_direction == "-y" and richtung == "-x")) then
397
        Left()
398
    elseif ((_direction == "x" and richtung == "-x") or (_direction == "-x" and richtung == "x") or (_direction == "y" and richtung == "-y") or (_direction == "-y" and richtung == "y")) then
399
        Left()
400
        Left()
401
    elseif((_direction == "x" and richtung == "x") or (_direction == "-x" and richtung == "-x") or (_direction == "y" and richtung == "y") or (_direction == "-y" and richtung == "-y")) then
402
        
403
    else
404
        Right()
405
    end
406
end
407
function GoTo(_destxPos, _destyPos, _destzPos, _destDirection) --only works from 0, -1, 0
408
    local startPos = {x = xPos, y = yPos, z = zPos}
409
    print("GoTo(" .. _destxPos .. ", " .. _destyPos .. ", " .. _destzPos .. ")")
410
    if _destDirection ~= nil then print(_destDirection) end
411
    if (startPos.x == 0 and startPos.y == -1 and startPos.z == 0) then
412
        LookDirection("y")
413
        UpDig()
414
        ForwardDig()
415
    end
416
    if xPos < _destxPos then
417
        LookDirection("x")
418
        while xPos < _destxPos do 
419
            ForwardDig()
420
        end
421
    elseif xPos > _destxPos then
422
        LookDirection("-x")
423
        while xPos > _destxPos do
424
            ForwardDig()
425
        end
426
    else end            
427
    if yPos < _destyPos then
428
        LookDirection("y")
429
        while yPos < _destyPos do 
430
            ForwardDig()
431
        end
432
    elseif yPos > _destyPos then
433
        LookDirection("-y")
434
        while yPos > _destyPos do
435
            ForwardDig()
436
        end
437
    else end
438
    if zPos < _destzPos then
439
        while zPos < _destzPos do 
440
            UpDig()
441
        end
442
    elseif zPos > _destzPos then
443
        while zPos > _destzPos do
444
            DownDig()
445
        end
446
    else end        
447
    if _destDirection ~= nil then
448
        LookDirection(_destDirection)
449
    end
450
end
451
452
--inventory Interaction--
453
454
function Unload(_direction)
455
    local onChest = false
456
    while yPos > -storageChestCount do
457
        if turtle.detectDown() == false then 
458
            Down()
459
            break
460
        elseif turtle.detect() == false then 
461
            LookDirection("-y")
462
            Forward()
463
        else
464
            sleep(0.2)
465
        end
466
        if yPos == -storageChestCount then
467
            Down()
468
        end
469
    end
470
471
    for i=2, 16 do 
472
        turtle.select(i)
473
        if _direction == nil then
474
            turtle.drop()
475
        elseif _direction == "up" then
476
            turtle.dropUp()
477
        elseif _direction == "down" then
478
            turtle.dropDown()
479
        else 
480
            return "not allowed parameter"
481
        end
482
    end
483
    Select(selectedSlot)
484
    LookDirection("-x")
485
    Forward()
486
    Right()
487
    while yPos < 0 do Forward() end
488
end
489
function Select(_slot)
490
    if turtle.select(_slot) == true then
491
        selectedSlot = _slot
492
    else return false end
493
end
494
495
--jobs--
496
497
function JobTurtlePlacer()
498
    function TurnWorkerOn()
499
        local worker = peripheral.wrap("front")
500
        worker.turnOn()
501
    end
502
    local function PlaceAndActivateWorkers()
503
        while turtle.getItemCount(2) > 0 do
504
            while (turtle.detect == true) do
505
                sleep(1)
506
            end
507
            sleep(0.5)
508
            turtle.place()
509
            sleep(0.4)
510
            if pcall(TurnWorkerOn) then end
511
        end
512
    end
513
    LookDirection("-x")
514
    while xPos > -1 do ForwardDig() end
515
    while zPos > 0 do DownDig() end
516
    Left()
517
    if turtle.getItemCount(2) ~= 0 then
518
        print("Help me get rid of the Items in Slot 2! :( \n After solving this, wait up to 10 seconds before breaking me")
519
        turtle.drop()
520
        while turtle.getItemCount(2) ~= 0 do
521
            rednet.broadcast("pa help " .. xPos .. " " .. yPos .. " " .. zPos .. " TurtlePlacerHasProblem")
522
            sleep(10)
523
        end
524
    end
525
    Select(2)
526
    turtle.suck()
527
    while turtle.getItemCount(2) == 0 do
528
        sleep(10)
529
        turtle.suck()
530
    end
531
    Left()
532
    Forward()
533
    Right()
534
    PlaceAndActivateWorkers()
535
    
536
    while true do 
537
        Right()
538
        Forward()
539
        Left()
540
        turtle.suck()
541
        while turtle.getItemCount(2) == 0 do
542
            sleep(10)
543
            turtle.suck()
544
        end
545
        Left()
546
        Forward()
547
        Right()
548
        PlaceAndActivateWorkers()
549
    end
550
end
551
function JobDigger(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)
552
    GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
553
    for i=0, _length do 
554
        DigUp()
555
        DigDown()
556
        if i < _length then
557
            ForwardDig()
558
        end
559
    end
560
    Up()
561
    GoTo(GetUnloadPosition())
562
    Unload("down")
563
end
564
function JobMiner(_jobxPos, _jobyPos, _jobzPos, _jobdirection, _length)
565
    GoTo(2,0,-2)
566
    GoTo(_jobxPos, _jobyPos, _jobzPos, _jobdirection)
567
    for i=0, _length do 
568
        if  yPos ~= 0 or _jobzPos > -2 then 
569
            DigUp()
570
        end
571
        if yPos ~= 0 or _jobzPos < -2 then
572
            DigDown()
573
        end
574
        if i < _length then
575
            ForwardDig()
576
        end
577
    end
578
    Up()
579
    GoTo(3,50,2)
580
    GoTo(GetUnloadPosition())
581
    Unload("down")
582
end
583
584
---------------------------------------------------------------------------
585
--wait for id--
586
587
if SetupToNearestMasterPC() == false then
588
    print("I cant find the MasterPC! Set him up first")
589
    return
590
end
591
DetermineDirection()
592
xPos, yPos, zPos = 0, -1, 0
593
LookDirection("x")
594
GetFreshFuelFromChest(32)
595
UpDig()
596-
while true do GetJob() end
596+
597
ForwardDig()
598
Left()
599
ForwardDig()
600
while true do GetJob()