/
DetourNavMesh.go
474 lines (377 loc) · 18.1 KB
/
DetourNavMesh.go
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
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
package detour
/// A handle to a polygon within a navigation mesh tile.
/// @ingroup detour
type DtPolyRef uint32
/// A handle to a tile within a navigation mesh.
/// @ingroup detour
type DtTileRef uint32
/// The maximum number of vertices per navigation polygon.
/// @ingroup detour
const DT_VERTS_PER_POLYGON int32 = 6
/// @{
/// @name Tile Serialization Constants
/// These constants are used to detect whether a navigation tile's data
/// and state format is compatible with the current build.
///
/// A magic number used to detect compatibility of navigation tile data.
const DT_NAVMESH_MAGIC int32 = 'D'<<24 | 'N'<<16 | 'A'<<8 | 'V'
/// A version number used to detect compatibility of navigation tile data.
const DT_NAVMESH_VERSION int32 = 7
/// A magic number used to detect the compatibility of navigation tile states.
const DT_NAVMESH_STATE_MAGIC int32 = 'D'<<24 | 'N'<<16 | 'M'<<8 | 'S'
/// A version number used to detect compatibility of navigation tile states.
const DT_NAVMESH_STATE_VERSION int32 = 1
/// @}
/// A flag that indicates that an entity links to an external entity.
/// (E.g. A polygon edge is a portal that links to another polygon.)
const DT_EXT_LINK uint16 = 0x8000
/// A value that indicates the entity does not link to anything.
const DT_NULL_LINK uint32 = 0xffffffff
/// A flag that indicates that an off-mesh connection can be traversed in both directions. (Is bidirectional.)
const DT_OFFMESH_CON_BIDIR uint8 = 1
/// The maximum number of user defined area ids.
/// @ingroup detour
const DT_MAX_AREAS int = 64
/// Tile flags used for various functions and fields.
/// For an example, see dtNavMesh::addTile().
type DtTileFlags int
const (
/// The navigation mesh owns the tile memory and is responsible for freeing it.
DT_TILE_FREE_DATA DtTileFlags = 0x01
)
/// Vertex flags returned by dtNavMeshQuery::findStraightPath.
type DtStraightPathFlags uint8
const (
DT_STRAIGHTPATH_START DtStraightPathFlags = 0x01 ///< The vertex is the start position in the path.
DT_STRAIGHTPATH_END DtStraightPathFlags = 0x02 ///< The vertex is the end position in the path.
DT_STRAIGHTPATH_OFFMESH_CONNECTION DtStraightPathFlags = 0x04 ///< The vertex is the start of an off-mesh connection.
)
/// Options for dtNavMeshQuery::findStraightPath.
type DtStraightPathOptions int
const (
DT_STRAIGHTPATH_AREA_CROSSINGS DtStraightPathOptions = 0x01 ///< Add a vertex at every polygon edge crossing where area changes.
DT_STRAIGHTPATH_ALL_CROSSINGS DtStraightPathOptions = 0x02 ///< Add a vertex at every polygon edge crossing.
)
/// Options for dtNavMeshQuery::initSlicedFindPath and updateSlicedFindPath
type DtFindPathOptions int
const (
DT_FINDPATH_ANY_ANGLE DtFindPathOptions = 0x02 ///< use raycasts during pathfind to "shortcut" (raycast still consider costs)
)
/// Options for dtNavMeshQuery::raycast
type DtRaycastOptions int
const (
DT_RAYCAST_USE_COSTS DtRaycastOptions = 0x01 ///< Raycast should calculate movement cost along the ray and fill RaycastHit::cost
)
/// Limit raycasting during any angle pahfinding
/// The limit is given as a multiple of the character radius
const DT_RAY_CAST_LIMIT_PROPORTIONS float32 = 50.0
/// Flags representing the type of a navigation mesh polygon.
type DtPolyTypes uint8
const (
/// The polygon is a standard convex polygon that is part of the surface of the mesh.
DT_POLYTYPE_GROUND DtPolyTypes = 0
/// The polygon is an off-mesh connection consisting of two vertices.
DT_POLYTYPE_OFFMESH_CONNECTION DtPolyTypes = 1
)
/// Defines a polygon within a dtMeshTile object.
/// @ingroup detour
type DtPoly struct {
/// Index to first link in linked list. (Or #DT_NULL_LINK if there is no link.)
FirstLink uint32
/// The indices of the polygon's vertices.
/// The actual vertices are located in dtMeshTile::verts.
Verts [DT_VERTS_PER_POLYGON]uint16
/// Packed data representing neighbor polygons references and flags for each edge.
Neis [DT_VERTS_PER_POLYGON]uint16
/// The user defined polygon flags.
Flags uint16
/// The number of vertices in the polygon.
VertCount uint8
/// The bit packed area id and polygon type.
/// @note Use the structure's set and get methods to acess this value.
AreaAndtype uint8
}
/// Sets the user defined area id. [Limit: < #DT_MAX_AREAS]
func (this *DtPoly) SetArea(a uint8) { this.AreaAndtype = (this.AreaAndtype & 0xc0) | (a & 0x3f) }
/// Sets the polygon type. (See: #dtPolyTypes.)
func (this *DtPoly) SetType(t DtPolyTypes) {
this.AreaAndtype = (this.AreaAndtype & 0x3f) | (uint8(t) << 6)
}
/// Gets the user defined area id.
func (this *DtPoly) GetArea() uint8 { return this.AreaAndtype & 0x3f }
/// Gets the polygon type. (See: #dtPolyTypes)
func (this *DtPoly) GetType() DtPolyTypes { return DtPolyTypes(this.AreaAndtype >> 6) }
/// Defines the location of detail sub-mesh data within a dtMeshTile.
type DtPolyDetail struct {
VertBase uint32 ///< The offset of the vertices in the dtMeshTile::detailVerts array.
TriBase uint32 ///< The offset of the triangles in the dtMeshTile::detailTris array.
VertCount uint8 ///< The number of vertices in the sub-mesh.
TriCount uint8 ///< The number of triangles in the sub-mesh.
}
/// Defines a link between polygons.
/// @note This structure is rarely if ever used by the end user.
/// @see dtMeshTile
type DtLink struct {
Ref DtPolyRef ///< Neighbour reference. (The neighbor that is linked to.)
Next uint32 ///< Index of the next link.
Edge uint8 ///< Index of the polygon edge that owns this link.
Side uint8 ///< If a boundary link, defines on which side the link is.
Bmin uint8 ///< If a boundary link, defines the minimum sub-edge area.
Bmax uint8 ///< If a boundary link, defines the maximum sub-edge area.
}
/// Bounding volume node.
/// @note This structure is rarely if ever used by the end user.
/// @see dtMeshTile
type DtBVNode struct {
Bmin [3]uint16 ///< Minimum bounds of the node's AABB. [(x, y, z)]
Bmax [3]uint16 ///< Maximum bounds of the node's AABB. [(x, y, z)]
I int32 ///< The node's index. (Negative for escape sequence.)
}
/// Defines an navigation mesh off-mesh connection within a dtMeshTile object.
/// An off-mesh connection is a user defined traversable connection made up to two vertices.
type DtOffMeshConnection struct {
/// The endpoints of the connection. [(ax, ay, az, bx, by, bz)]
Pos [6]float32
/// The radius of the endpoints. [Limit: >= 0]
Rad float32
/// The polygon reference of the connection within the tile.
Poly uint16
/// Link flags.
/// @note These are not the connection's user defined flags. Those are assigned via the
/// connection's dtPoly definition. These are link flags used for internal purposes.
Flags uint8
/// End point side.
Side uint8
/// The id of the offmesh connection. (User assigned when the navigation mesh is built.)
UserId uint32
}
/// Provides high level information related to a dtMeshTile object.
/// @ingroup detour
type DtMeshHeader struct {
Magic int32 ///< Tile magic number. (Used to identify the data format.)
Version int32 ///< Tile data format version number.
X int32 ///< The x-position of the tile within the dtNavMesh tile grid. (x, y, layer)
Y int32 ///< The y-position of the tile within the dtNavMesh tile grid. (x, y, layer)
Layer int32 ///< The layer of the tile within the dtNavMesh tile grid. (x, y, layer)
UserId uint32 ///< The user defined id of the tile.
PolyCount int32 ///< The number of polygons in the tile.
VertCount int32 ///< The number of vertices in the tile.
MaxLinkCount int32 ///< The number of allocated links.
DetailMeshCount int32 ///< The number of sub-meshes in the detail mesh.
/// The number of unique vertices in the detail mesh. (In addition to the polygon vertices.)
DetailVertCount int32
DetailTriCount int32 ///< The number of triangles in the detail mesh.
BvNodeCount int32 ///< The number of bounding volume nodes. (Zero if bounding volumes are disabled.)
OffMeshConCount int32 ///< The number of off-mesh connections.
OffMeshBase int32 ///< The index of the first polygon which is an off-mesh connection.
WalkableHeight float32 ///< The height of the agents using the tile.
WalkableRadius float32 ///< The radius of the agents using the tile.
WalkableClimb float32 ///< The maximum climb height of the agents using the tile.
Bmin [3]float32 ///< The minimum bounds of the tile's AABB. [(x, y, z)]
Bmax [3]float32 ///< The maximum bounds of the tile's AABB. [(x, y, z)]
/// The bounding volume quantization factor.
BvQuantFactor float32
}
/// Defines a navigation mesh tile.
/// @ingroup detour
type DtMeshTile struct {
Salt uint32 ///< Counter describing modifications to the tile.
LinksFreeList uint32 ///< Index to the next free link.
Header *DtMeshHeader ///< The tile header.
Polys []DtPoly ///< The tile polygons. [Size: dtMeshHeader::polyCount]
Verts []float32 ///< The tile vertices. [Size: dtMeshHeader::vertCount]
Links []DtLink ///< The tile links. [Size: dtMeshHeader::maxLinkCount]
DetailMeshes []DtPolyDetail ///< The tile's detail sub-meshes. [Size: dtMeshHeader::detailMeshCount]
/// The detail mesh's unique vertices. [(x, y, z) * dtMeshHeader::detailVertCount]
DetailVerts []float32
/// The detail mesh's triangles. [(vertA, vertB, vertC) * dtMeshHeader::detailTriCount]
DetailTris []uint8
/// The tile bounding volume nodes. [Size: dtMeshHeader::bvNodeCount]
/// (Will be null if bounding volumes are disabled.)
BvTree []DtBVNode
OffMeshCons []DtOffMeshConnection ///< The tile off-mesh connections. [Size: dtMeshHeader::offMeshConCount]
Data []byte ///< The tile data. (Not directly accessed under normal situations.)
DataSize int32 ///< Size of the tile data.
Flags DtTileFlags ///< Tile flags. (See: #dtTileFlags)
Next *DtMeshTile ///< The next free tile, or the next tile in the spatial grid.
}
/// Configuration parameters used to define multi-tile navigation meshes.
/// The values are used to allocate space during the initialization of a navigation mesh.
/// @see dtNavMesh::init()
/// @ingroup detour
type DtNavMeshParams struct {
Orig [3]float32 ///< The world space origin of the navigation mesh's tile space. [(x, y, z)]
TileWidth float32 ///< The width of each tile. (Along the x-axis.)
TileHeight float32 ///< The height of each tile. (Along the z-axis.)
MaxTiles uint32 ///< The maximum number of tiles the navigation mesh can contain.
MaxPolys uint32 ///< The maximum number of polygons each tile can contain.
}
/// A navigation mesh based on tiles of convex polygons.
/// @ingroup detour
type DtNavMesh struct {
m_params DtNavMeshParams ///< Current initialization params. TODO: do not store this info twice.
m_orig [3]float32 ///< Origin of the tile (0,0)
m_tileWidth, m_tileHeight float32 ///< Dimensions of each tile.
m_maxTiles int32 ///< Max number of tiles.
m_tileLutSize int32 ///< Tile hash lookup size (must be pot).
m_tileLutMask int32 ///< Tile hash lookup mask.
m_posLookup []*DtMeshTile ///< Tile hash lookup.
m_nextFree *DtMeshTile ///< Freelist of tiles.
m_tiles []DtMeshTile ///< List of tiles.
m_saltBits uint32 ///< Number of salt bits in the tile ID.
m_tileBits uint32 ///< Number of tile bits in the tile ID.
m_polyBits uint32 ///< Number of poly bits in the tile ID.
}
/// @{
/// @name Encoding and Decoding
/// These functions are generally meant for internal use only.
/// Derives a standard polygon reference.
/// @note This function is generally meant for internal use only.
/// @param[in] salt The tile's salt value.
/// @param[in] it The index of the tile.
/// @param[in] ip The index of the polygon within the tile.
func (this *DtNavMesh) EncodePolyId(salt, it, ip uint32) DtPolyRef {
return DtPolyRef((salt << (this.m_polyBits + this.m_tileBits)) | (it << this.m_polyBits) | ip)
}
/// Decodes a standard polygon reference.
/// @note This function is generally meant for internal use only.
/// @param[in] ref The polygon reference to decode.
/// @param[out] salt The tile's salt value.
/// @param[out] it The index of the tile.
/// @param[out] ip The index of the polygon within the tile.
/// @see #encodePolyId
func (this *DtNavMesh) DecodePolyId(ref DtPolyRef, salt, it, ip *uint32) {
saltMask := (uint32(1) << this.m_saltBits) - 1
tileMask := (uint32(1) << this.m_tileBits) - 1
polyMask := (uint32(1) << this.m_polyBits) - 1
*salt = ((uint32(ref) >> (this.m_polyBits + this.m_tileBits)) & saltMask)
*it = ((uint32(ref) >> this.m_polyBits) & tileMask)
*ip = (uint32(ref) & polyMask)
}
/// Extracts a tile's salt value from the specified polygon reference.
/// @note This function is generally meant for internal use only.
/// @param[in] ref The polygon reference.
/// @see #encodePolyId
func (this *DtNavMesh) DecodePolyIdSalt(ref DtPolyRef) uint32 {
saltMask := (uint32(1) << this.m_saltBits) - 1
return ((uint32(ref) >> (this.m_polyBits + this.m_tileBits)) & saltMask)
}
/// Extracts the tile's index from the specified polygon reference.
/// @note This function is generally meant for internal use only.
/// @param[in] ref The polygon reference.
/// @see #encodePolyId
func (this *DtNavMesh) DecodePolyIdTile(ref DtPolyRef) uint32 {
tileMask := (uint32(1) << this.m_tileBits) - 1
return ((uint32(ref) >> this.m_polyBits) & tileMask)
}
/// Extracts the polygon's index (within its tile) from the specified polygon reference.
/// @note This function is generally meant for internal use only.
/// @param[in] ref The polygon reference.
/// @see #encodePolyId
func (this *DtNavMesh) DecodePolyIdPoly(ref DtPolyRef) uint32 {
polyMask := (uint32(1) << this.m_polyBits) - 1
return (uint32(ref) & polyMask)
}
/// @}
/// Allocates a navigation mesh object using the Detour allocator.
/// @return A navigation mesh that is ready for initialization, or null on failure.
/// @ingroup detour
func DtAllocNavMesh() *DtNavMesh {
navmesh := &DtNavMesh{}
navmesh.constructor()
return navmesh
}
/// Frees the specified navigation mesh object using the Detour allocator.
/// @param[in] navmesh A navigation mesh allocated using #dtAllocNavMesh
/// @ingroup detour
func DtFreeNavMesh(navmesh *DtNavMesh) {
if navmesh == nil {
return
}
navmesh.destructor()
}
///////////////////////////////////////////////////////////////////////////
// This section contains detailed documentation for members that don't have
// a source file. It reduces clutter in the main section of the header.
/**
@typedef dtPolyRef
@par
Polygon references are subject to the same invalidate/preserve/restore
rules that apply to #dtTileRef's. If the #dtTileRef for the polygon's
tile changes, the polygon reference becomes invalid.
Changing a polygon's flags, area id, etc. does not impact its polygon
reference.
@typedef dtTileRef
@par
The following changes will invalidate a tile reference:
- The referenced tile has been removed from the navigation mesh.
- The navigation mesh has been initialized using a different set
of #dtNavMeshParams.
A tile reference is preserved/restored if the tile is added to a navigation
mesh initialized with the original #dtNavMeshParams and is added at the
original reference location. (E.g. The lastRef parameter is used with
dtNavMesh::addTile.)
Basically, if the storage structure of a tile changes, its associated
tile reference changes.
@var unsigned short dtPoly::neis[DT_VERTS_PER_POLYGON]
@par
Each entry represents data for the edge starting at the vertex of the same index.
E.g. The entry at index n represents the edge data for vertex[n] to vertex[n+1].
A value of zero indicates the edge has no polygon connection. (It makes up the
border of the navigation mesh.)
The information can be extracted as follows:
@code
neighborRef = neis[n] & 0xff; // Get the neighbor polygon reference.
if (neis[n] & #DT_EX_LINK)
{
// The edge is an external (portal) edge.
}
@endcode
@var float dtMeshHeader::bvQuantFactor
@par
This value is used for converting between world and bounding volume coordinates.
For example:
@code
const float cs = 1.0f / tile->header->bvQuantFactor;
const dtBVNode* n = &tile->bvTree[i];
if (n->i >= 0)
{
// This is a leaf node.
float worldMinX = tile->header->bmin[0] + n->bmin[0]*cs;
float worldMinY = tile->header->bmin[0] + n->bmin[1]*cs;
// Etc...
}
@endcode
@struct dtMeshTile
@par
Tiles generally only exist within the context of a dtNavMesh object.
Some tile content is optional. For example, a tile may not contain any
off-mesh connections. In this case the associated pointer will be null.
If a detail mesh exists it will share vertices with the base polygon mesh.
Only the vertices unique to the detail mesh will be stored in #detailVerts.
@warning Tiles returned by a dtNavMesh object are not guarenteed to be populated.
For example: The tile at a location might not have been loaded yet, or may have been removed.
In this case, pointers will be null. So if in doubt, check the polygon count in the
tile's header to determine if a tile has polygons defined.
@var float dtOffMeshConnection::pos[6]
@par
For a properly built navigation mesh, vertex A will always be within the bounds of the mesh.
Vertex B is not required to be within the bounds of the mesh.
*/