View difference between Paste ID: w0MeL9hp and isFkYnUA
SHOW: | | - or go back to the newest paste.
1
-- api used for 2d pathfinding
2
3
4
-- defining variables of coordinates and direction
5
-- Initialize parameters for the location and direction
6-
x_live_coord = 0
6+
x_live_coord = 1
7-
y_live_coord = 0
7+
y_live_coord = 1
8
z_live_coord = 1
9
live_facing_direction = 0
10
item_name_g = nil
11
 
12
 
13
-- Basic moveset functions turning and forward
14
15
function up_rec()
16
	repeat
17
		turtle.digUp()
18
	until turtle.up()
19
	z_live_coord = z_live_coord + 1
20
end
21
22
function down_rec()
23
	repeat
24
		turtle.digDown()
25
	until turtle.down()
26
	z_live_coord = z_live_coord - 1
27
end
28
29
function goto_z(z_dest)
30
	if z_dest > z_live_coord then
31
		repeat
32
			up_rec()
33
		until z_live_coord == z_dest
34
	elseif z_dest < z_live_coord then
35
		repeat
36
			down_rec()
37
		until z_live_coord == z_dest
38
	end
39
end
40
 
41
function rec_turn_right()
42
    -- action
43
    turtle.turnRight()
44
 
45
    -- recording
46
    live_facing_direction = live_facing_direction + 1
47
    if (live_facing_direction == 4) then
48
        live_facing_direction = 0
49
    end
50
end
51
 
52
function rec_turn_left()
53
    -- action
54
    turtle.turnLeft()
55
 
56
    -- recording
57
    live_facing_direction = live_facing_direction - 1
58
    if (live_facing_direction == -1) then
59
        live_facing_direction = 3
60
    end
61
 
62
end
63
 
64
function rec_forward_firm()
65
    -- action
66
    repeat 
67
        turtle.dig()
68
    until turtle.forward()
69
 
70
    --recording
71
    if (live_facing_direction == 0) then
72
        y_live_coord = y_live_coord + 1
73
    elseif (live_facing_direction == 1) then
74
        x_live_coord = x_live_coord + 1
75
    elseif (live_facing_direction == 2) then
76
        y_live_coord = y_live_coord - 1
77
    elseif (live_facing_direction == 3) then
78
        x_live_coord = x_live_coord - 1
79
    end
80
    print(x_live_coord, y_live_coord)
81
    while turtle.getFuelLevel() < 80 do
82
	print("going out of fuel")
83
	refuel_with("minecraft:coal")
84
    end
85
end
86
87
88
function up_firm()
89
    repeat
90
        turtle.digUp()
91
    until turtle.up()
92
end
93
94
function down_firm()
95
    repeat
96
        turtle.digDown()
97
    until turtle.down()
98
end
99
100
function turtle_face_turn(face)
101
    if (face - live_facing_direction == 1 or face - live_facing_direction == -3) then
102
        rec_turn_right()
103
    elseif(face - live_facing_direction == -1 or face - live_facing_direction == 3) then
104
        rec_turn_left()
105
	elseif(face == live_facing_direction) then
106
		print("not turning")     
107
	else
108
        rec_turn_left()
109
        rec_turn_left()
110
    end
111
end
112
 
113
 
114
-- the GOTO function, give one coordinate and move to the function
115
-- first align the facing direction to 0
116
-- get to the coordinate and return facing direction to 0
117
function turtle_goto(a,b)
118
    -- calculation
119
    delta_x = a - x_live_coord
120
    delta_y = b - y_live_coord
121
 
122
    -- action section below
123
    -- facing the correct direction for x movement
124
    if (delta_x > 0) then
125
        turtle_face_turn(1)
126
    elseif (delta_x < 0) then
127
        turtle_face_turn(3)
128
        delta_x = - delta_x
129
    end
130
 
131
 
132
    -- x movement
133
    for x_movement = 0,delta_x - 1,1 
134
    do
135
        rec_forward_firm()
136
    end 
137
 
138
 
139
    -- facing the correct direction for y movement
140
    if (delta_y > 0) then
141
        turtle_face_turn(0)
142
    elseif (delta_y < 0) then
143
        turtle_face_turn(2)
144
        delta_y = - delta_y
145
    end
146
    -- y movement
147
    for y_movement = 0,delta_y - 1,1 
148
    do
149
        rec_forward_firm()
150
    end 
151
 
152
 
153
    -- returning to the 0 facing direction
154
155
end
156
 
157
-- the pathfinding function, will try to visit all points in an array
158
 
159
-- some utilities
160
 
161
function pathfind_and_action()
162
    for loop_num=1, array_length, 1
163
    do
164
        print("NEXT!!")
165
        near_init_term_num = 1
166
        while visited[near_init_term_num] == 1 do
167
            near_init_term_num = near_init_term_num + 1
168
        end
169
        
170
        nearest_x = x_destinations[near_init_term_num]
171
        nearest_y = y_destinations[near_init_term_num]
172
        min_distance = math.abs(nearest_x - x_live_coord) +  math.abs(nearest_y - y_live_coord) 
173
        
174
        last_item_num = near_init_term_num
175
        for item_num = 1, array_length, 1
176
        do
177
            if visited[item_num] == 0 then
178
                if (math.abs(x_destinations[item_num] - x_live_coord) +  math.abs(y_destinations[item_num] - y_live_coord)) < min_distance then
179
                    nearest_x = x_destinations[item_num]
180
                    nearest_y = y_destinations[item_num]
181
                    min_distance = math.abs(nearest_x - x_live_coord) +  math.abs(nearest_y - y_live_coord)
182
                    last_item_num = item_num
183
                end             
184
            end
185
        end
186
        visited[last_item_num] = 1
187
        print("going to ", nearest_x, nearest_y)
188
        turtle_goto(nearest_x,nearest_y)
189
190
        turtle_action()
191
    end
192
 
193
end
194
 
195
 
196
 
197
-- action loop section
198
 
199
function place_item(item_name)
200
    item_tot = 0
201
    for cell_num = 1,16 do
202
       	turtle.select(cell_num)
203
       	if turtle.getItemDetail() then
204
           	item_detail = turtle.getItemDetail()
205
           	if (item_detail.name == item_name) then
206
               	turtle.placeDown()
207
               	item_tot = item_tot + item_detail.count
208
           	end
209
        end
210
	end
211
	if item_tot < 1 then
212-
		distress_item_request(item_name,4)
212+
		distress_item_request(item_name,4,'worker_to_supply','supply_to_worker')
213
		for cell_num = 1,16 do
214
       		turtle.select(cell_num)
215
       		if turtle.getItemDetail() then
216
           		item_detail = turtle.getItemDetail()
217
           		if (item_detail.name == item_name) then
218
               		turtle.placeDown()
219
               		item_tot = item_tot + item_detail.count
220
           		end
221
        	end
222
		end
223
	end
224
	print("item_left: %d", item_tot)
225
end
226
 
227
function refuel_with(item_name)
228
	item_tot = 0
229
    for cell_num = 1,16 do
230-
		distress_item_request(item_name,1)
230+
231
        if turtle.getItemDetail() then
232
            item_detail = turtle.getItemDetail()
233
            if (item_detail.name == item_name) then
234
                turtle.refuel(1)
235
				item_tot = item_tot + item_detail.count
236
            end
237
        end
238
    end
239
	if item_tot < 1 then
240
		distress_item_request(item_name,1,'worker_to_supply','supply_to_worker')
241
	end
242
end
243
244
245
 
246
-- example code of coordinates given
247
 
248
249
250
function turtle_action()
251
    print('nothing')
252
	place_item(item_name_g)
253
end
254
255
256
function plane_execution(x_destinations_input, y_destinations_input, visited_input, array_length_input, item_name)
257
    x_destinations = x_destinations_input
258
    y_destinations = y_destinations_input
259
	visited = visited_input
260
	array_length = array_length_input
261
	item_name_g = item_name
262
    pathfind_and_action()
263
	turtle_goto(1,1)
264
	turtle_face_turn(0)
265
end
266
267
function squarefill(a,b,s)
268
    array_length = a*b
269
    current_length=1
270
    for xfill=1,a,s
271
    do
272
        for yfill=1,b,s
273
        do
274
            x_destinations[current_length]=xfill
275
            y_destinations[current_length]=yfill
276
            visited[current_length]=0
277
            current_length=current_length + 1
278
        end
279
    end
280
end
281
282
function if_inlist(item_list)
283
    inlist = 0
284
    for num, item_name_l in pairs(item_list) do
285
		item_data = turtle.getItemDetail()
286
		if turtle.getItemDetail() then
287
			if string.match(item_data.name, item_name_l) then
288
	    		inlist = 1
289
			end
290
		end
291
    end 
292
    return inlist
293
end
294
295
function clear_backpack(white_list)
296
    for i=1,16,1 do
297
    	turtle.select(i)
298
		if if_inlist(white_list) == 0 then
299
			turtle.drop()
300
		end
301
    end
302
end
303
304
-- functions in charge of encoding and decoding
305
function plane_encode(x_dests, y_dests, visited, array_l, item_name, protocol)
306
    task_name = 'plane'
307
    x_dests_str = '/'
308
    y_dests_str = '/'
309
    visited_str = '/'
310
    for i=1,array_l do
311
	x_dests_str = x_dests_str .. tostring(x_dests[i]) .. '/'
312
	y_dests_str = y_dests_str .. tostring(y_dests[i]) .. '/'
313
	visited_str = visited_str .. tostring(visited[i]) .. '/'
314
    end
315
    xyv_dests_str = (x_dests_str .. 'x' .. y_dests_str .. 'x' .. visited_str)
316
    out_message = (task_name .. 'x' .. xyv_dests_str .. 'x' .. item_name)
317
    -- broadcasting now
318
    rednet.broadcast(out_message, protocol)
319
end
320
321
function vertical_encode(z_dest, protocol)
322
    task_name = 'vert'
323
    out_message = (task_name .. 'x' .. z_dest)
324
    rednet.broadcast(out_message, protocol)
325
end
326
327
328
function worker_decode_and_action(msg)
329
    matched_func = string.gmatch(msg,'([^x]+)')
330
    task_name = matched_func()
331
	print(task_name)
332
    if task_name == 'vert' then
333
    	goto_z(tonumber(matched_func()))
334
    elseif task_name == 'plane' then
335
	x_dests_str = matched_func()	
336
	y_dests_str = matched_func()
337
	visited_str = matched_func()
338
	item_name = matched_func()
339
	-- start filling in
340
	array_length = 0
341
	x_destinations = {}
342
	y_destinations = {}
343
	visited = {}
344
345
	for x_i in string.gmatch(x_dests_str,'([^/]+)')
346
	do
347
	    array_length = array_length + 1
348
	    x_destinations[array_length] = tonumber(x_i)
349
	end
350
	 
351
	for y_i in string.gmatch(y_dests_str,'([^/]+)')
352
	do
353
	    y_destinations[#y_destinations + 1] = tonumber(y_i)
354
	end
355
356
	for v_i in string.gmatch(visited_str,'([^/]+)')
357
	do
358
	    visited[#visited + 1] = tonumber(v_i)
359
	end
360
	plane_execution(x_destinations, y_destinations, visited, array_length, item_name)
361
    end
362
end
363
364
function distress_item_request(item_name,s_number,out_protocol,in_protocol)
365
    -- message format looking like 1x2x3xminecraft:coal
366
    out_message = (x_live_coord .. 'x' .. y_live_coord .. 'x' .. z_live_coord .. 'x' .. item_name .. 'x' .. s_number)
367
    rednet.broadcast(out_message, out_protocol)
368
    -- check if item is received
369
    id, msg = rednet.receive(in_protocol)
370
end
371
372
function count_item(item_name)
373
    item_count = 0
374
    for cell_num = 1,16 do
375
	turtle.select(cell_num)
376
	if turtle.getItemDetail() then
377
            item_detail = turtle.getItemDetail()
378
            if (item_detail.name == item_name) then
379
		item_count = item_count + 1	
380
            end
381
        end
382
    end
383
    return item_count
384
end
385
386
function drop_item(item_name,num_of_stack)
387
    dropped_count = 0
388
    for i=1,num_of_stack,1 do
389
	turtle.select(i)
390-
	
390+
391-
	print(x_dest)
391+
392
end
393
394
function distress_item_decode_action(msg,return_protocol)
395
    matched_func = string.gmatch(msg,'([^x]+)')
396
    x_dest = matched_func()
397
    y_dest = matched_func()
398
    z_dest = matched_func()
399
    item_name = matched_func()
400
    stack_num_r = matched_func()
401
402
403
    -- check if there is enough fuel
404
    -- go to fuel chest
405
    if turtle.getFuelLevel()<4000 then
406
    	turtle_goto(0,findIndex(chest_item,'minecraft:coal'))
407
	turtle.suckDown()
408
	turtle.select(1)
409
	turtle.refuel()
410
    end
411
412
    -- go to item chest
413
414
    turtle_goto(0,findIndex(chest_item,item_name))
415
    for i = 1,stack_num_r do
416
	turtle.suckDown()
417
    end
418
419
    -- go deliver item
420
    goto_z(z_dest+1)
421
    turtle_goto(x_dest,y_dest)
422-
	turtle_goto(0,0)
422+
423
    
424
    -- return and resume
425
    turtle_goto(0,0)
426-
	rednet.broadcast('yay',return_protocol)
426+
427
    turtle_face_turn(0)
428
    -- return underlivered items
429
    turtle_goto(0,findIndex(chest_item,item_name))
430
    drop_item(item_name,16)
431
    -- send message of completion
432
    rednet.broadcast('yay',return_protocol)
433
end
434
435
function findIndex(array, value)
436
    for i, v in ipairs(array) do
437
	if v == value then
438
	    return i
439
	end
440
    end
441
    return nil
442-
chest_item[2] = 'chisel:planks/oak/braced_planks'
442+
443-
chest_item[3] = 'minecraft:glass'
443+
444
445
446
chest_item = {}
447-
    id, message = rednet.receive('worker_to_supply')
447+
448-
	print(message)
448+
chest_item[2] = 'minecraft:cobblestone'
449-
    distress_item_decode_action(message,'supply_to_worker')
449+
450
rednet.open('left')
451
while true do
452
    id, message = rednet.receive('command_to_worker')
453
	print('task received')
454
	worker_decode_and_action(message)
455
	rednet.broadcast(1,'worker_to_command')
456
end
457
458