-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpathfinding.lua
627 lines (512 loc) · 14.3 KB
/
pathfinding.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
-----------------
-- Pathfinding --
-----------------
local a_star_alloted_time = tonumber(minetest.settings:get("creatura_a_star_alloted_time")) or 500
local theta_star_alloted_time = tonumber(minetest.settings:get("creatura_theta_star_alloted_time")) or 700
creatura.pathfinder = {}
local max_open = 300
-- Math
local floor = math.floor
local abs = math.abs
local vec_add, vec_dist, vec_new, vec_round = vector.add, vector.distance, vector.new, vector.round
local function vec_raise(v, n)
return {x = v.x, y = v.y + n, z = v.z}
end
-- Heuristic
local function get_distance(start_pos, end_pos)
local distX = abs(start_pos.x - end_pos.x)
local distZ = abs(start_pos.z - end_pos.z)
if distX > distZ then
return 14 * distZ + 10 * (distX - distZ)
else
return 14 * distX + 10 * (distZ - distX)
end
end
local function get_distance_to_neighbor(start_pos, end_pos)
local distX = abs(start_pos.x - end_pos.x)
local distY = abs(start_pos.y - end_pos.y)
local distZ = abs(start_pos.z - end_pos.z)
if distX > distZ then
return (14 * distZ + 10 * (distX - distZ)) * (distY + 1)
else
return (14 * distX + 10 * (distZ - distX)) * (distY + 1)
end
end
-- Blocked Movement Checks
local is_blocked = creatura.is_blocked
local function get_line_of_sight(a, b)
local steps = floor(vec_dist(a, b))
local line = {}
for i = 0, steps do
local pos
if steps > 0 then
pos = {
x = a.x + (b.x - a.x) * (i / steps),
y = a.y + (b.y - a.y) * (i / steps),
z = a.z + (b.z - a.z) * (i / steps)
}
else
pos = a
end
table.insert(line, pos)
end
if #line < 1 then
return false
else
for i = 1, #line do
local node = minetest.get_node(line[i])
if creatura.get_node_def(node.name).walkable then
return false
end
end
end
return true
end
local function is_on_ground(pos)
local ground = {
x = pos.x,
y = pos.y - 1,
z = pos.z
}
if creatura.get_node_def(ground).walkable then
return true
end
return false
end
-- Neighbor Check Grids
local neighbor_grid = {
{x = 1, y = 0, z = 0},
{x = 1, y = 0, z = 1},
{x = 0, y = 0, z = 1},
{x = -1, y = 0, z = 1},
{x = -1, y = 0, z = 0},
{x = -1, y = 0, z = -1},
{x = 0, y = 0, z = -1},
{x = 1, y = 0, z = -1}
}
local neighbor_grid_climb = {
{x = 1, y = 0, z = 0},
{x = 1, y = 0, z = 1},
{x = 0, y = 0, z = 1},
{x = -1, y = 0, z = 1},
{x = -1, y = 0, z = 0},
{x = -1, y = 0, z = -1},
{x = 0, y = 0, z = -1},
{x = 1, y = 0, z = -1},
{x = 0, y = 1, z = 0},
{x = 0, y = -1, z = 0}
}
local neighbor_grid_3d = {
-- Central
{x = 1, y = 0, z = 0},
{x = 0, y = 0, z = 1},
{x = -1, y = 0, z = 0},
{x = 0, y = 0, z = -1},
-- Directly Up or Down
{x = 0, y = 1, z = 0},
{x = 0, y = -1, z = 0}
}
-- Get Neighbors
local function get_neighbors(pos, width, height, open, closed, parent, evaluated)
local result = {}
local neighbor
local can_move
local hashed_pos
local step
for i = 1, #neighbor_grid do
neighbor = vec_add(pos, neighbor_grid[i])
can_move = get_line_of_sight({x = pos.x, y = neighbor.y, z = pos.z}, neighbor)
hashed_pos = minetest.hash_node_position(neighbor)
if parent
and vec_dist(parent, neighbor) < vec_dist(pos, neighbor) then
can_move = false
end
if open[hashed_pos]
or closed[hashed_pos]
or evaluated[hashed_pos] then
can_move = false
elseif can_move then
can_move = not is_blocked(neighbor, width, height)
if not can_move then -- Step Up
step = vec_raise(neighbor, 1)
can_move = not is_blocked(vec_round(step), width, height)
neighbor = vec_round(step)
else
step = creatura.get_ground_level(vec_new(neighbor), 1)
if step.y < neighbor.y
and not is_blocked(vec_round(step), width, height) then
neighbor = step
end
end
end
if can_move then
table.insert(result, neighbor)
end
evaluated[hashed_pos] = true
end
return result
end
function creatura.pathfinder.get_neighbors_climb(pos, width, height, open, closed)
local result = {}
local neighbor
local can_move
local hashed_pos
local step
for i = 1, #neighbor_grid_climb do
neighbor = vec_add(pos, neighbor_grid_climb[i])
can_move = get_line_of_sight({x = pos.x, y = neighbor.y, z = pos.z}, neighbor)
hashed_pos = minetest.hash_node_position(neighbor)
if open[hashed_pos]
or closed[hashed_pos] then
can_move = false
elseif can_move then
can_move = not is_blocked(neighbor, width, height)
if not can_move then -- Step Up
step = vec_raise(neighbor, 1)
can_move = not is_blocked(vec_round(step), width, height)
neighbor = vec_round(step)
elseif i < 9 then
step = creatura.get_ground_level(vec_new(neighbor), 1)
if step.y < neighbor.y
and not is_blocked(vec_round(step), width, height) then
neighbor = step
end
end
end
if can_move then
table.insert(result, neighbor)
end
end
return result
end
function creatura.pathfinder.get_neighbors_fly(pos, width, height, open, closed, parent)
local result = {}
local neighbor
local can_move
local hashed_pos
for i = 1, #neighbor_grid_3d do
neighbor = vec_add(pos, neighbor_grid_3d[i])
can_move = get_line_of_sight({x = pos.x, y = pos.y, z = pos.z}, neighbor)
hashed_pos = minetest.hash_node_position(neighbor)
if parent
and vec_dist(parent, neighbor) < vec_dist(pos, neighbor) then
can_move = false
end
if open[hashed_pos]
or closed[hashed_pos] then
can_move = false
elseif can_move then
can_move = not is_blocked(neighbor, width, height)
end
if can_move then
table.insert(result, neighbor)
end
end
return result, true
end
function creatura.pathfinder.get_neighbors_swim(pos, width, height, open, closed, parent)
local result = {}
local neighbor
local can_move
local hashed_pos
for i = 1, #neighbor_grid_3d do
neighbor = vec_add(pos, neighbor_grid_3d[i])
can_move = get_line_of_sight({x = pos.x, y = pos.y, z = pos.z}, neighbor)
hashed_pos = minetest.hash_node_position(neighbor)
if (parent
and vec_dist(parent, neighbor) < vec_dist(pos, neighbor))
or creatura.get_node_def(neighbor).drawtype ~= "liquid" then
can_move = false
end
if open[hashed_pos]
or closed[hashed_pos] then
can_move = false
elseif can_move then
can_move = not is_blocked(neighbor, width, height)
end
if can_move then
table.insert(result, neighbor)
end
end
return result, true
end
-- A*
function creatura.pathfinder.find_path(self, pos1, pos2, neighbor_func)
local us_time = minetest.get_us_time()
local check_vertical = false
neighbor_func = neighbor_func or get_neighbors
local start = self._path_data.start or {
x = floor(pos1.x + 0.5),
y = floor(pos1.y + 0.5),
z = floor(pos1.z + 0.5)
}
local goal = {
x = floor(pos2.x + 0.5),
y = floor(pos2.y + 0.5),
z = floor(pos2.z + 0.5)
}
self._path_data.start = start
if goal.x == start.x
and goal.z == start.z then -- No path can be found
self._path_data = {}
return
end
local openSet = self._path_data.open or {}
local closedSet = self._path_data.closed or {}
local evaluated = {}
local start_index = minetest.hash_node_position(start)
openSet[start_index] = {
pos = start,
parent = nil,
gScore = 0,
fScore = get_distance(start, goal)
}
local count = self._path_data.count or 1
local current_id, current
local adjacent
local neighbor
local temp_gScore
local new_gScore
local hCost
local hashed_pos
local parent_open
local parent_closed
while count > 0 do
-- Initialize ID and data
current_id, current = next(openSet)
-- Find lowest f cost
for i, v in pairs(openSet) do
if v.fScore < current.fScore then
current_id = i
current = v
end
end
if not current_id then self._path_data = {} return end -- failsafe
-- Add lowest fScore to closedSet and remove from openSet
openSet[current_id] = nil
closedSet[current_id] = current
if ((check_vertical or is_on_ground(goal))
and current_id == minetest.hash_node_position(goal))
or ((not check_vertical and not is_on_ground(goal))
and goal.x == current.pos.x
and goal.z == current.pos.z) then
local path = {}
local fail_safe = 0
for _ in pairs(closedSet) do
fail_safe = fail_safe + 1
end
repeat
if not closedSet[current_id] then self._path_data = {} return end
table.insert(path, closedSet[current_id].pos)
current_id = closedSet[current_id].parent
until current_id == start_index or #path >= fail_safe
if not closedSet[current_id] then self._path_data = {} return nil end
table.insert(path, closedSet[current_id].pos)
local reverse_path = {}
repeat table.insert(reverse_path, table.remove(path)) until #path == 0
self._path_data = {}
return reverse_path
end
parent_open = openSet[current.parent]
parent_closed = closedSet[current.parent]
adjacent, check_vertical = neighbor_func(
current.pos,
self.width,
self.height,
openSet,
closedSet,
(parent_closed and parent_closed.pos) or (parent_open and parent_open.pos),
evaluated
)
-- Fly, Swim, and Climb all return true for check_vertical to properly check if goal has been reached
-- Go through neighboring nodes
for i = 1, #adjacent do
neighbor = {
pos = adjacent[i],
parent = current_id,
gScore = 0,
fScore = 0
}
temp_gScore = current.gScore + get_distance_to_neighbor(current.pos, neighbor.pos)
new_gScore = 0
hashed_pos = minetest.hash_node_position(neighbor.pos)
if openSet[hashed_pos] then
new_gScore = openSet[hashed_pos].gScore
end
if (temp_gScore < new_gScore
or not openSet[hashed_pos])
and not closedSet[hashed_pos] then
if not openSet[hashed_pos] then
count = count + 1
end
hCost = get_distance_to_neighbor(neighbor.pos, goal)
neighbor.gScore = temp_gScore
neighbor.fScore = temp_gScore + hCost
openSet[hashed_pos] = neighbor
end
end
if minetest.get_us_time() - us_time > a_star_alloted_time then
self._path_data = {
start = start,
open = openSet,
closed = closedSet,
count = count
}
return
end
if count > (max_open or 100) then
self._path_data = {}
return
end
end
end
-- Theta*
function creatura.pathfinder.find_path_theta(self, pos1, pos2, neighbor_func)
local us_time = minetest.get_us_time()
local check_vertical = false
neighbor_func = neighbor_func or get_neighbors
local start = self._path_data.start or {
x = floor(pos1.x + 0.5),
y = floor(pos1.y + 0.5),
z = floor(pos1.z + 0.5)
}
local goal = {
x = floor(pos2.x + 0.5),
y = floor(pos2.y + 0.5),
z = floor(pos2.z + 0.5)
}
self._path_data.start = start
if goal.x == start.x
and goal.z == start.z then -- No path can be found
return
end
local openSet = self._path_data.open or {}
local closedSet = self._path_data.closed or {}
local evaluated = {}
local start_index = minetest.hash_node_position(start)
openSet[start_index] = {
pos = start,
parent = nil,
gScore = 0,
fScore = get_distance(start, goal)
}
local count = self._path_data.count or 1
local current_id, current
local current_parent
local adjacent
local neighbor
local temp_gScore
local new_gScore
local hCost
local hashed_pos
local parent_open
local parent_closed
while count > 0 do
-- Initialize ID and data
current_id, current = next(openSet)
-- Find lowest f cost
for i, v in pairs(openSet) do
if v.fScore < current.fScore then
current_id = i
current = v
end
end
if not current_id then return end -- failsafe
-- Add lowest fScore to closedSet and remove from openSet
openSet[current_id] = nil
closedSet[current_id] = current
if ((check_vertical or is_on_ground(goal))
and current_id == minetest.hash_node_position(goal))
or ((not check_vertical and not is_on_ground(goal))
and goal.x == current.pos.x
and goal.z == current.pos.z) then
local path = {}
local fail_safe = 0
for _ in pairs(closedSet) do
fail_safe = fail_safe + 1
end
repeat
if not closedSet[current_id] then return end
table.insert(path, closedSet[current_id].pos)
current_id = closedSet[current_id].parent
until current_id == start_index or #path >= fail_safe
if not closedSet[current_id] then self._path_data = {} return nil end
table.insert(path, closedSet[current_id].pos)
local reverse_path = {}
repeat table.insert(reverse_path, table.remove(path)) until #path == 0
self._path_data = {}
return reverse_path
end
parent_open = openSet[current.parent]
parent_closed = closedSet[current.parent]
adjacent, check_vertical = neighbor_func(
current.pos,
self.width,
self.height,
openSet,
closedSet,
(parent_closed and parent_closed.pos) or (parent_open and parent_open.pos),
evaluated
)
-- Fly, Swim, and Climb all return true for check_vertical to properly check if goal has been reached
-- Go through neighboring nodes
for i = 1, #adjacent do
neighbor = {
pos = adjacent[i],
parent = current_id,
gScore = 0,
fScore = 0
}
hashed_pos = minetest.hash_node_position(neighbor.pos)
if not openSet[hashed_pos]
and not closedSet[hashed_pos] then
current_parent = closedSet[current.parent] or closedSet[start_index]
if not current_parent then
current_parent = openSet[current.parent] or openSet[start_index]
end
if current_parent
and get_line_of_sight(current_parent.pos, neighbor.pos) then
temp_gScore = current_parent.gScore + get_distance_to_neighbor(current_parent.pos, neighbor.pos)
new_gScore = 999
if openSet[hashed_pos] then
new_gScore = openSet[hashed_pos].gScore
end
if temp_gScore < new_gScore then
hCost = get_distance_to_neighbor(neighbor.pos, goal)
neighbor.gScore = temp_gScore
neighbor.fScore = temp_gScore + hCost
neighbor.parent = minetest.hash_node_position(current_parent.pos)
openSet[hashed_pos] = neighbor
count = count + 1
end
else
temp_gScore = current.gScore + get_distance_to_neighbor(current_parent.pos, neighbor.pos)
new_gScore = 999
if openSet[hashed_pos] then
new_gScore = openSet[hashed_pos].gScore
end
if temp_gScore < new_gScore then
hCost = get_distance_to_neighbor(neighbor.pos, goal)
neighbor.gScore = temp_gScore
neighbor.fScore = temp_gScore + hCost
openSet[hashed_pos] = neighbor
count = count + 1
end
end
end
end
if minetest.get_us_time() - us_time > theta_star_alloted_time then
self._path_data = {
start = start,
open = openSet,
closed = closedSet,
count = count
}
return
end
if count > (max_open or 100) then
self._path_data = {}
return
end
end
end