This repository was archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
Expand file tree
/
Copy pathMyGridHighLevelHelper.cs
More file actions
347 lines (288 loc) · 13.3 KB
/
Copy pathMyGridHighLevelHelper.cs
File metadata and controls
347 lines (288 loc) · 13.3 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
using Sandbox.Game.Entities.Cube;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using VRage;
using VRage.Utils;
using VRage.Voxels;
using VRageMath;
namespace Sandbox.Game.AI.Pathfinding
{
public class MyGridHighLevelHelper
{
private MyGridNavigationMesh m_mesh;
private Vector3I m_cellSize;
private ulong m_packedCoord;
private int m_currentComponentRel;
private List<List<int>> m_currentCellConnections;
private MyVector3ISet m_changedCells;
private MyVector3ISet m_changedCubes;
private Dictionary<Vector3I, List<int>> m_triangleRegistry; // Belongs to the grid navigation mesh
private MyNavmeshComponents m_components;
// Stores triangles of all components for us to be able to set their component index. "null" values separate components (to avoid having to allocate more lists)
private List<MyNavigationTriangle> m_tmpComponentTriangles = new List<MyNavigationTriangle>();
private List<int> m_tmpNeighbors = new List<int>();
private static HashSet<int> m_tmpCellTriangles = new HashSet<int>();
private static MyGridHighLevelHelper m_currentHelper = null;
private static readonly Vector3I CELL_COORD_SHIFT = new Vector3I(1 << 19);
private Predicate<MyNavigationPrimitive> m_processTrianglePredicate = ProcessTriangleForHierarchyStatic;
public bool IsDirty
{
get
{
return !m_changedCells.Empty;
}
}
public MyGridHighLevelHelper(MyGridNavigationMesh mesh, Dictionary<Vector3I, List<int>> triangleRegistry, Vector3I cellSize)
{
m_mesh = mesh;
m_cellSize = cellSize;
m_packedCoord = 0;
m_currentCellConnections = new List<List<int>>();
m_changedCells = new MyVector3ISet();
m_changedCubes = new MyVector3ISet();
m_triangleRegistry = triangleRegistry;
m_components = new MyNavmeshComponents();
}
// Actually, this function marks even cubes around the block to make sure that any changes caused in their triangles
// will be reflected in the navigation mesh.
public void MarkBlockChanged(MySlimBlock block)
{
Vector3I min = block.Min - Vector3I.One;
Vector3I max = block.Max + Vector3I.One;
Vector3I pos = min;
for (var it = new Vector3I_RangeIterator(ref block.Min, ref block.Max); it.IsValid(); it.GetNext(out pos))
{
m_changedCubes.Add(pos);
}
Vector3I minCell = CubeToCell(ref min);
Vector3I maxCell = CubeToCell(ref max);
pos = minCell;
for (var it = new Vector3I_RangeIterator(ref minCell, ref maxCell); it.IsValid(); it.GetNext(out pos))
{
m_changedCells.Add(pos);
}
}
public void ProcessChangedCellComponents()
{
ProfilerShort.Begin("ProcessChangedCellComponents");
m_currentHelper = this;
Vector3I min, max, pos;
List<int> triangles = null;
foreach (var cell in m_changedCells)
{
min = CellToLowestCube(cell);
max = min + m_cellSize - Vector3I.One;
// Save a hashset of all the triangles in the current cell
pos = min;
for (var it = new Vector3I_RangeIterator(ref min, ref max); it.IsValid(); it.GetNext(out pos))
{
if (!m_triangleRegistry.TryGetValue(pos, out triangles)) continue;
foreach (var triIndex in triangles)
{
m_tmpCellTriangles.Add(triIndex);
}
}
if (m_tmpCellTriangles.Count == 0) continue;
MyCellCoord cellCoord = new MyCellCoord(0, cell);
ulong packedCell = cellCoord.PackId64();
m_components.OpenCell(packedCell);
long timeBegin = m_mesh.GetCurrentTimestamp() + 1;
long timeEnd = timeBegin;
m_currentComponentRel = 0;
m_tmpComponentTriangles.Clear();
foreach (var triIndex in m_tmpCellTriangles)
{
// Skip already visited triangles
var triangle = m_mesh.GetTriangle(triIndex);
if (m_currentComponentRel != 0 && m_mesh.VisitedBetween(triangle, timeBegin, timeEnd)) continue;
m_components.OpenComponent();
// Make sure we have place in m_currentCellConnections
if (m_currentComponentRel >= m_currentCellConnections.Count)
{
m_currentCellConnections.Add(new List<int>());
}
// Find connected component from an unvisited triangle and mark its connections
m_components.AddComponentTriangle(triangle, triangle.Center);
triangle.ComponentIndex = m_currentComponentRel;
m_tmpComponentTriangles.Add(triangle);
m_mesh.PrepareTraversal(triangle, null, m_processTrianglePredicate);
m_mesh.PerformTraversal();
m_tmpComponentTriangles.Add(null);
m_components.CloseComponent();
timeEnd = m_mesh.GetCurrentTimestamp();
if (m_currentComponentRel == 0)
{
timeBegin = timeEnd;
}
m_currentComponentRel++;
}
m_tmpCellTriangles.Clear();
MyNavmeshComponents.ClosedCellInfo cellInfo = new MyNavmeshComponents.ClosedCellInfo();
m_components.CloseAndCacheCell(ref cellInfo);
// Renumber triangles from the old indices to the newly assigned index from m_components
int componentIndex = cellInfo.StartingIndex;
foreach (var triangle in m_tmpComponentTriangles)
{
if (triangle == null)
{
componentIndex++;
continue;
}
triangle.ComponentIndex = componentIndex;
}
m_tmpComponentTriangles.Clear();
// Remove old component primitives
if (!cellInfo.NewCell && cellInfo.ComponentNum != cellInfo.OldComponentNum)
{
for (int i = 0; i < cellInfo.OldComponentNum; ++i)
{
m_mesh.HighLevelGroup.RemovePrimitive(cellInfo.OldStartingIndex + i);
}
}
// Add new component primitives
if (cellInfo.NewCell || cellInfo.ComponentNum != cellInfo.OldComponentNum)
{
for (int i = 0; i < cellInfo.ComponentNum; ++i)
{
m_mesh.HighLevelGroup.AddPrimitive(cellInfo.StartingIndex + i, m_components.GetComponentCenter(i));
}
}
// Update existing component primitives
if (!cellInfo.NewCell && cellInfo.ComponentNum == cellInfo.OldComponentNum)
{
for (int i = 0; i < cellInfo.ComponentNum; ++i)
{
var primitive = m_mesh.HighLevelGroup.GetPrimitive(cellInfo.StartingIndex + i);
primitive.UpdatePosition(m_components.GetComponentCenter(i));
}
}
// Connect new components with the others in the neighboring cells
for (int i = 0; i < cellInfo.ComponentNum; ++i)
{
int compIndex = cellInfo.StartingIndex + i;
var primitive = m_mesh.HighLevelGroup.GetPrimitive(compIndex);
primitive.GetNeighbours(m_tmpNeighbors);
// Connect to disconnected components
foreach (var connection in m_currentCellConnections[i])
{
if (!m_tmpNeighbors.Remove(connection))
{
m_mesh.HighLevelGroup.ConnectPrimitives(compIndex, connection);
}
}
// Disconnect neighbors that should be no longer connected
foreach (var neighbor in m_tmpNeighbors)
{
// Only disconnect from the other cell if it is expanded and there was no connection found
var neighborPrimitive = m_mesh.HighLevelGroup.TryGetPrimitive(neighbor);
if (neighborPrimitive != null && neighborPrimitive.IsExpanded)
{
m_mesh.HighLevelGroup.DisconnectPrimitives(compIndex, neighbor);
}
}
m_tmpNeighbors.Clear();
m_currentCellConnections[i].Clear();
}
// Set all the components as expanded
for (int i = 0; i < cellInfo.ComponentNum; ++i)
{
componentIndex = cellInfo.StartingIndex + i;
var component = m_mesh.HighLevelGroup.GetPrimitive(componentIndex);
if (component != null)
{
component.IsExpanded = true;
}
}
}
m_changedCells.Clear();
m_currentHelper = null;
ProfilerShort.End();
}
private static bool ProcessTriangleForHierarchyStatic(MyNavigationPrimitive primitive)
{
ProfilerShort.Begin("ProcessTriangleForHierarchy");
var triangle = primitive as MyNavigationTriangle;
bool retval = m_currentHelper.ProcessTriangleForHierarchy(triangle);
ProfilerShort.End();
return retval;
}
private bool ProcessTriangleForHierarchy(MyNavigationTriangle triangle)
{
// The triangle parent can be wrong when we have multiple navmeshes connected via external edges
if (triangle.Parent != m_mesh)
{
return false;
}
if (m_tmpCellTriangles.Contains(triangle.Index))
{
m_components.AddComponentTriangle(triangle, triangle.Center);
m_tmpComponentTriangles.Add(triangle);
return true;
}
else
{
ulong cellIndex;
// This test succeeds only if the triangle belongs to an unchanged component or to a component that was changed,
// but processed in a different cell already
if (m_components.TryGetComponentCell(triangle.ComponentIndex, out cellIndex))
{
// Save connections to other components. There won't be so many, so we can keep them in a list instead of a HashSet
if (!m_currentCellConnections[m_currentComponentRel].Contains(triangle.ComponentIndex))
{
m_currentCellConnections[m_currentComponentRel].Add(triangle.ComponentIndex);
}
}
}
return false;
}
public MyHighLevelPrimitive GetHighLevelNavigationPrimitive(MyNavigationTriangle triangle)
{
Debug.Assert(triangle != null, "Navigation triangle was null!");
if (triangle == null) return null;
Debug.Assert(triangle.Parent == this.m_mesh, "Finding cell of a navigation triangle in a wrong mesh!");
if (triangle.Parent != this.m_mesh)
{
return null;
}
if (triangle.ComponentIndex != -1)
{
return m_mesh.HighLevelGroup.GetPrimitive(triangle.ComponentIndex);
}
else
{
return null;
}
}
private void TryClearCell(ulong packedCoord)
{
MyNavmeshComponents.CellInfo cellInfo;
if (!m_components.TryGetCell(packedCoord, out cellInfo))
{
return;
}
/*for (int i = 0; i < cellInfo.ComponentNum; ++i)
{
int componentIndex = cellInfo.StartingIndex + i;
m_mesh.HighLevelGroup.RemovePrimitive(componentIndex);
}*/
m_components.ClearCell(packedCoord, ref cellInfo);
}
private Vector3I CubeToCell(ref Vector3I cube)
{
// We have 20 bits per coord for the cell coordinate. If we want signed coords, we have to convert to unsigned by
// adding 2^19. The range will then be <0, 2^20-1> after shift and <-2^19, 2^19-1> before shift. Anything else will
// be reported as overflow by MyCellCoord
Vector3D cubeD = cube;
cubeD = cubeD / m_cellSize;
Vector3I retval;
Vector3I.Floor(ref cubeD, out retval);
retval += CELL_COORD_SHIFT;
return retval;
}
private Vector3I CellToLowestCube(Vector3I cell)
{
return (cell - CELL_COORD_SHIFT) * m_cellSize;
}
}
}