-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMp3s Pathfinding Module
More file actions
448 lines (375 loc) · 10.7 KB
/
Mp3s Pathfinding Module
File metadata and controls
448 lines (375 loc) · 10.7 KB
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
--!strict
--?> Mp3s Pathfinding Module
type Pathfinding = {
Timeout: number,
NodesPerFrame: number,
NodeStep: number,
MaxNodes: number,
SmoothPath: boolean,
Ignore: { Instance },
Debug: boolean,
FindPath: (Start: Vector3, End: Vector3, Step: number?, AgentRadius: number?) -> { Vector3 }?,
Visualize: (Path: { Vector3 }, ParentInfo: Instance?) -> nil,
DebugStart: () -> nil,
}
type Node = {
Position: Vector3,
G: number,
H: number,
F: number,
Parent: Node?,
HeapIndex: number,
}
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local Pathfinding = {} :: Pathfinding
Pathfinding.__index = Pathfinding
Pathfinding.Timeout = 10.0 --?> Max total seconds
Pathfinding.NodesPerFrame = 30 --?> Max nodes to process per frame (lower = smoother FPS)
Pathfinding.NodeStep = 3 --?> Grid size (larger = faster but less precise)
Pathfinding.MaxNodes = 5000 --?> Safety limit
Pathfinding.SmoothPath = true --?> Allow path smoothing
Pathfinding.Ignore = {} :: { Instance } --?> Ignore parts
Pathfinding.Debug = false --?> Disable debug outputs for performance
local NEIGHBORS = {}
for x = -1, 1 do
for y = -1, 1 do
for z = -1, 1 do
if not (x == 0 and y == 0 and z == 0) then
table.insert(NEIGHBORS, Vector3.new(x, y, z))
end
end
end
end
local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Exclude
local OverlapParamsObj = OverlapParams.new()
OverlapParamsObj.FilterType = Enum.RaycastFilterType.Exclude
--?> Heap
local Heap = {}
Heap.__index = Heap
function Heap.new()
return setmetatable({ Items = {}, Count = 0 }, Heap)
end
function Heap:Add(Item: Node)
self.Count += 1
self.Items[self.Count] = Item
Item.HeapIndex = self.Count
self:SortUp(Item)
end
function Heap:RemoveFirst(): Node
local First = self.Items[1]
local Last = self.Items[self.Count]
self.Items[1] = Last
self.Items[self.Count] = nil
self.Count -= 1
if self.Count > 0 then
Last.HeapIndex = 1
self:SortDown(Last)
end
First.HeapIndex = 0
return First
end
function Heap:UpdateItem(Item: Node)
self:SortUp(Item)
end
function Heap:Contains(Item: Node): boolean
return Item.HeapIndex > 0 and self.Items[Item.HeapIndex] == Item
end
function Heap:SortUp(Item: Node)
local ParentIndex = math.floor(Item.HeapIndex / 2)
while ParentIndex >= 1 do
local ParentItem = self.Items[ParentIndex]
if ParentItem and Item.F < ParentItem.F then
self:Swap(Item, ParentItem)
ParentIndex = math.floor(Item.HeapIndex / 2)
else
break
end
end
end
function Heap:SortDown(Item: Node)
while true do
local LeftChildIndex = Item.HeapIndex * 2
local RightChildIndex = Item.HeapIndex * 2 + 1
local SwapIndex = 0
if LeftChildIndex <= self.Count then
SwapIndex = LeftChildIndex
if RightChildIndex <= self.Count then
if self.Items[RightChildIndex].F < self.Items[LeftChildIndex].F then
SwapIndex = RightChildIndex
end
end
if self.Items[SwapIndex].F < Item.F then
self:Swap(Item, self.Items[SwapIndex])
else
break
end
else
break
end
end
end
function Heap:Swap(ItemA: Node, ItemB: Node)
self.Items[ItemA.HeapIndex], self.Items[ItemB.HeapIndex] = ItemB, ItemA
local TempIndex = ItemA.HeapIndex
ItemA.HeapIndex = ItemB.HeapIndex
ItemB.HeapIndex = TempIndex
end
local function GridSnap(Pos: Vector3, Step: number): Vector3
return Vector3.new(
math.round(Pos.X / Step) * Step,
math.round(Pos.Y / Step) * Step,
math.round(Pos.Z / Step) * Step
)
end
local function Heuristic(A: Vector3, B: Vector3): number
return (A - B).Magnitude
end
--?> Check if a position is walkable (not inside geometry)
local function IsWalkable(Pos: Vector3, Radius: number): boolean
return #workspace:GetPartBoundsInRadius(Pos, Radius, OverlapParamsObj) == 0
end
--?> Smooth path by removing unnecessary waypoints (string pulling)
local function SmoothPath(Path: { Vector3 }, Radius: number): { Vector3 }
if #Path <= 2 then
return Path
end
local Smoothed = { Path[1] }
local Current = 1
while Current < #Path do
local Farthest = Current + 1
for i = #Path, Current + 2, -1 do
local From = Path[Current]
local To = Path[i]
local Direction = To - From
local Hit = workspace:Raycast(From, Direction, RayParams)
if not Hit then
local MidPoint = From + Direction * 0.5
if IsWalkable(MidPoint, Radius) then
Farthest = i
break
end
end
end
table.insert(Smoothed, Path[Farthest])
Current = Farthest
end
return Smoothed
end
function Pathfinding.FindPath(Start: Vector3, End: Vector3, Step: number?, AgentRadius: number?)
local NodeStep = Step or Pathfinding.NodeStep
local Radius = AgentRadius or 1.5
RayParams.FilterDescendantsInstances = Pathfinding.Ignore
OverlapParamsObj.FilterDescendantsInstances = Pathfinding.Ignore
local StartPos = GridSnap(Start, NodeStep)
local EndPos = GridSnap(End, NodeStep)
if Pathfinding.Debug then
print("[MPM]: Start:", StartPos, "End:", EndPos, "Step:", NodeStep)
end
--?> Validate start position
if not IsWalkable(StartPos, Radius) then
--?> If not valid, try to find a nearby valid start
for _, Offset in NEIGHBORS do
local TestPos = StartPos + (Offset * NodeStep)
if IsWalkable(TestPos, Radius) then
StartPos = TestPos
if Pathfinding.Debug then
print("[MPM]: Adjusted start to:", StartPos)
end
break
end
end
--?> Still not valid? Fail fast
if not IsWalkable(StartPos, Radius) then
warn("[MPM]: Start position is blocked!")
return nil
end
end
--?> Validate end position
if not IsWalkable(EndPos, Radius) then
--?> If not valid, try to find a nearby valid end
for _, Offset in NEIGHBORS do
local TestPos = EndPos + (Offset * NodeStep)
if IsWalkable(TestPos, Radius) then
EndPos = TestPos
if Pathfinding.Debug then
print("[MPM]: Adjusted end to:", EndPos)
end
break
end
end
end
local AllNodes: { [Vector3]: Node } = {}
local OpenSet = Heap.new()
local ClosedSet: { [Vector3]: boolean } = {}
local StartNode: Node = {
Position = StartPos,
G = 0,
H = Heuristic(StartPos, EndPos),
F = 0,
Parent = nil,
HeapIndex = 0,
}
StartNode.F = StartNode.G + StartNode.H
OpenSet:Add(StartNode)
AllNodes[StartPos] = StartNode
local TotalStartTime = os.clock()
local NodesProcessed = 0
local NodesThisFrame = 0
local LastDebugTime = os.clock()
while OpenSet.Count > 0 do
--?> Total timeout
local Elapsed = os.clock() - TotalStartTime
if Elapsed > Pathfinding.Timeout then
if Pathfinding.Debug then
warn("[MPM]: Timed out after", NodesProcessed, "nodes")
end
return nil
end
--?> Max nodes safety
if NodesProcessed >= Pathfinding.MaxNodes then
if Pathfinding.Debug then
warn("[MPM]: Hit max node limit:", Pathfinding.MaxNodes)
end
return nil
end
--?> Yield every N nodes
NodesThisFrame += 1
if NodesThisFrame >= Pathfinding.NodesPerFrame then
task.wait()
NodesThisFrame = 0
end
--?> Debug progress every second
if Pathfinding.Debug and (os.clock() - LastDebugTime) > 1.0 then
print(
string.format(
"[MPM]: Progress: %d nodes, OpenSet: %d, Elapsed: %.1fs",
NodesProcessed,
OpenSet.Count,
Elapsed
)
)
LastDebugTime = os.clock()
end
local CurrentNode = OpenSet:RemoveFirst()
ClosedSet[CurrentNode.Position] = true
NodesProcessed += 1
--?> Check if reached destination
local DistToEnd = (CurrentNode.Position - EndPos).Magnitude
if DistToEnd < NodeStep * 1.5 then
local Path = {}
local RetraceNode: Node? = CurrentNode
while RetraceNode do
table.insert(Path, 1, RetraceNode.Position)
RetraceNode = RetraceNode.Parent
end
--?> Smooth the path to remove unnecessary waypoints
if Pathfinding.SmoothPath then
Path = SmoothPath(Path, Radius)
end
if Pathfinding.Debug then
print("[MPM]: Success! Nodes:", NodesProcessed, "Path length:", #Path)
end
return Path
end
--?> Process neighbors
for _, Offset in NEIGHBORS do
local NeighborPos = CurrentNode.Position + (Offset * NodeStep)
if ClosedSet[NeighborPos] then
continue
end
if not IsWalkable(NeighborPos, Radius) then
ClosedSet[NeighborPos] = true
continue
end
local RayDirection = NeighborPos - CurrentNode.Position
if workspace:Raycast(CurrentNode.Position, RayDirection, RayParams) then
ClosedSet[NeighborPos] = true
continue
end
local MoveCost = CurrentNode.G + NodeStep
local NeighborNode = AllNodes[NeighborPos]
if not NeighborNode then
NeighborNode = {
Position = NeighborPos,
G = MoveCost,
H = Heuristic(NeighborPos, EndPos),
F = 0,
Parent = CurrentNode,
HeapIndex = 0,
}
NeighborNode.F = NeighborNode.G + NeighborNode.H
AllNodes[NeighborPos] = NeighborNode
OpenSet:Add(NeighborNode)
elseif MoveCost < NeighborNode.G then
NeighborNode.G = MoveCost
NeighborNode.Parent = CurrentNode
NeighborNode.F = NeighborNode.G + NeighborNode.H
if OpenSet:Contains(NeighborNode) then
OpenSet:UpdateItem(NeighborNode)
else
OpenSet:Add(NeighborNode)
end
end
end
end
if Pathfinding.Debug then
warn("[MPM]: No path exists (exhausted all options after", NodesProcessed, "nodes)")
end
return nil
end
function Pathfinding.Visualize(Path: { Vector3 }, ParentInfo: Instance?)
local Parent = ParentInfo or workspace
local Old = Parent:FindFirstChild("PathVisualization")
if Old then
Old:Destroy()
end
local Folder = Instance.new("Folder")
Folder.Name = "PathVisualization"
Folder.Parent = Parent
for i, Pos in ipairs(Path) do
local Part = Instance.new("Part")
Part.Size = Vector3.new(1, 1, 1)
Part.Anchored = true
Part.CanCollide = false
Part.Material = Enum.Material.Neon
Part.Color = Color3.fromHSV(i / #Path * 0.3, 1, 1)
Part.Position = Pos
Part.Shape = Enum.PartType.Ball
Part.Parent = Folder
end
Debris:AddItem(Folder, 10)
end
function Pathfinding.DebugStart()
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
if not Player.Character then
return nil
end
print("[MPM]: Click anywhere to pathfind.")
Mouse.Button1Down:Connect(function()
local Character = Player.Character
if not Character or not Character:FindFirstChild("HumanoidRootPart") then
return nil
end
if not table.find(Pathfinding.Ignore, Character) then
table.insert(Pathfinding.Ignore, Character)
end
local Start = Character.HumanoidRootPart.Position
local End = Mouse.Hit.Position
task.spawn(function()
print("[MPM]: Starting pathfind from", Start, "to", End)
local StartTime = os.clock()
local Path = Pathfinding.FindPath(Start, End)
local Elapsed = os.clock() - StartTime
if Path then
print(string.format("[MPM]: Complete! %d nodes in %.0fms", #Path, Elapsed * 1000))
Pathfinding.Visualize(Path, workspace)
else
warn("[MPM]: Failed after", string.format("%.0fms", Elapsed * 1000))
end
end)
end)
end
return Pathfinding