File: navigation_recast_debug.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (451 lines) | stat: -rw-r--r-- 15,035 bytes parent folder | download
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
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "navigation_recast_debug.h"
#include "navigation_recast_helpers.h"
#include "navigation_recast_config.h"
#include "navigation_recast_config_ext.h"
#include "navigation_recast_load.h"
#include "navigation_bsp.h"
#include "../qcommon/vector.h"
#include "navigate.h"
#include "debuglines.h"
#include "entity.h"
#include "level.h"

#include "DetourCrowd.h"
#include "DetourNavMesh.h"
#include "DetourNavMeshQuery.h"
#include "Recast.h"

NavigationMapDebug navigationMapDebug;

NavigationMapDebug::NavigationMapDebug()
{
    crowd       = NULL;
    agentId     = -1;
    ai_numPaths = 0;
    ai_lastpath = 0;
}

NavigationMapDebug::~NavigationMapDebug()
{
    if (crowd) {
        dtFreeCrowd(crowd);
    }
}

void NavigationMapDebug::TestAgent(const Vector& start, const Vector& end, Vector *paths, int *numPaths, int maxPaths)
{
    vec3_t        half = {64, 64, 64};
    vec3_t        startNav;
    vec3_t        endNav;
    dtQueryFilter filter;
    dtPolyRef     nearestStartRef, nearestEndRef;
    vec3_t        nearestStartPt, nearestEndPt;

    ConvertGameToRecastCoord(start, startNav);
    ConvertGameToRecastCoord(end, endNav);

    if (!crowd) {
        crowd = dtAllocCrowd();
        crowd->init(MAX_CLIENTS, NavigationMapConfiguration::agentRadius, navigationMap.GetNavMesh());
    }

    if (agentId != -1) {
        crowd->removeAgent(agentId);
    }

    dtCrowdAgentParams ap {0};

    ap.radius                = NavigationMapConfiguration::agentRadius;
    ap.height                = NavigationMapConfiguration::agentHeight;
    ap.maxAcceleration       = 8.0f;
    ap.maxSpeed              = 3.5f;
    ap.collisionQueryRange   = ap.radius * 12.0f;
    ap.pathOptimizationRange = ap.radius * 30.0f;
    ap.updateFlags           = 0;
    agentId                  = crowd->addAgent(startNav, &ap);

    navigationMap.GetNavMeshQuery()->findNearestPoly(startNav, half, &filter, &nearestStartRef, nearestStartPt);
    navigationMap.GetNavMeshQuery()->findNearestPoly(endNav, half, &filter, &nearestEndRef, nearestEndPt);

    crowd->requestMoveTarget(agentId, nearestEndRef, nearestEndPt);

    dtPolyRef polys[256];
    int       nPolys;
    navigationMap.GetNavMeshQuery()->findPath(
        nearestStartRef, nearestEndRef, nearestStartPt, nearestEndPt, &filter, polys, &nPolys, 256
    );
    navigationMap.GetNavMeshQuery()->findStraightPath(
        nearestStartPt, nearestEndPt, polys, nPolys, (float *)paths, NULL, NULL, numPaths, maxPaths
    );

    for (int i = 0; i < *numPaths; i++) {
        Vector converted;

        ConvertRecastToGameCoord(paths[i], converted);
        paths[i] = converted;
    }
}

void NavigationMapDebug::DrawModel(
    const Vector& origin, const navModel_t& model, float maxDistSquared, const Vector& offset
)
{
    int i, j;

    for (i = 1; i <= model.surfaces.NumObjects(); i++) {
        const navSurface_t& surface = model.surfaces.ObjectAt(i);

        for (j = 0; j < surface.indices.NumObjects(); j += 3) {
            const navVertice_t& v1 = surface.vertices[surface.indices[j + 0]];
            const navVertice_t& v2 = surface.vertices[surface.indices[j + 1]];
            const navVertice_t& v3 = surface.vertices[surface.indices[j + 2]];

            for (int k = 0; k < 3; ++k) {
                const Vector delta = (offset + surface.vertices[surface.indices[j + k]].xyz) - origin;

                if (delta.lengthSquared() >= maxDistSquared) {
                    continue;
                }

                G_DebugLine(
                    offset + surface.vertices[surface.indices[j + k]].xyz,
                    offset + surface.vertices[surface.indices[j + ((k + 1) % 3)]].xyz,
                    0,
                    1,
                    0,
                    1
                );
            }
        }
    }
}

/*
============
DebugDraw
============
*/
void NavigationMapDebug::DebugDraw()
{
    Entity               *ent            = g_entities[0].entity;
    const dtNavMesh      *navMeshDt      = navigationMap.GetNavMesh();
    const dtNavMeshQuery *navMeshQuery   = navigationMap.GetNavMeshQuery();
    const dtQueryFilter  *filter         = navigationMap.GetQueryFilter();
    const navMap_t&       navigationData = navigationMap.GetNavigationData();
    const navModel_t&     worldMap       = navigationData.GetWorldMap();

    if (!navMeshDt) {
        return;
    }

    if (!ent) {
        return;
    }

    if (ai_showallnode->integer) {
        for (int i = 0; i < navMeshDt->getMaxTiles(); i++) {
            const dtMeshTile *tile = static_cast<const dtNavMesh *>(navMeshDt)->getTile(i);
            if (!tile || !tile->header) {
                continue;
            }

            for (int j = 0; j < tile->header->vertCount; j++) {
                const float *pvert = &tile->verts[j * 3];

                Vector org;
                ConvertRecastToGameCoord(pvert, org);

                org.z += 16;

                if (org.z < ent->origin.z - 94 || org.z > ent->origin.z + 94) {
                    continue;
                }

                G_DebugBBox(org, Vector(-8, -8, -8), Vector(8, 8, 8), 1.0, 0.0, 0.5, 1.0);
            }
        }

#if 0
        for (int i = 0; i < polyMesh->nverts; ++i) {
            GetPolyMeshVertPosition(polyMesh, i, org);

            org.z += 16;

            if (org.z < ent->origin.z - 94 || org.z > ent->origin.z + 94) {
                continue;
            }

            G_DebugBBox(org, Vector(-8, -8, -8), Vector(8, 8, 8), 1.0, 0.0, 0.5, 1.0);
        }
#endif
    }

    switch (ai_showroutes->integer) {
    case 0:
    default:
        break;
    case 1:
        {
            const float maxDistSquared = Square(ai_showroutes_distance->integer);

            for (int i = 0; i < navMeshDt->getMaxTiles(); i++) {
                const dtMeshTile *tile = static_cast<const dtNavMesh *>(navMeshDt)->getTile(i);
                if (!tile || !tile->header) {
                    continue;
                }

                for (int j = 0; j < tile->header->polyCount; j++) {
                    const dtPoly *poly = &tile->polys[j];
                    const bool pass = navMeshQuery->isValidPolyRef(navMeshDt->encodePolyId(tile->salt, i, j), filter);

                    for (int k = 0; k < poly->vertCount; ++k) {
                        const float *pv1 = &tile->verts[poly->verts[k] * 3];
                        const float *pv2 = &tile->verts[poly->verts[(k + 1) % poly->vertCount] * 3];

                        Vector v1, v2;
                        ConvertRecastToGameCoord(pv1, v1);
                        ConvertRecastToGameCoord(pv2, v2);

                        const Vector delta = v1 - ent->origin;

                        if (delta.lengthSquared() >= maxDistSquared) {
                            continue;
                        }

                        if (pass) {
                            switch(poly->getArea() & 0x3f) {
                                case RC_WALKABLE_AREA:
                                default:
                                    G_DebugLine(v1, v2, 0, 1, 0, 1);
                                    break;
                                case RECAST_AREA_JUMP:
                                    G_DebugLine(v1, v2, 0, 1, 1, 1);
                                    break;
                                case RECAST_AREA_FALL:
                                case RECAST_AREA_MEDIUM_FALL:
                                case RECAST_AREA_HIGH_FALL:
                                    G_DebugLine(v1, v2, 1, 0, 1, 1);
                                    break;
                                case RECAST_AREA_LADDER:
                                    G_DebugLine(v1, v2, 0, 0, 1, 1);
                                    break;
                            }
                        } else {
                            G_DebugLine(v1, v2, 1, 0, 0, 1);
                        }
                    }
                }

                /*
                for (int j = 0; j < tile->header->offMeshConCount; j++) {
                    const dtOffMeshConnection* offMeshCon = &tile->offMeshCons[j];
                    const float* pv1 = &offMeshCon->pos[0];
                    const float* pv2 = &offMeshCon->pos[3];

                    Vector v1, v2;
                    ConvertRecastToGameCoord(pv1, v1);
                    ConvertRecastToGameCoord(pv2, v2);

                    const Vector delta = v1 - ent->origin;

                    if (delta.lengthSquared() >= maxDistSquared) {
                        continue;
                    }

                    G_DebugLine(v1, v2, 0, 1, 1, 1);
                }
                */
            }
#if 0
        for (int i = 0; i < polyMesh->npolys; ++i) {
            const unsigned short *p    = &polyMesh->polys[i * polyMesh->nvp * 2];
            const unsigned char   area = polyMesh->areas[i];

            if (area != RC_WALKABLE_AREA) {
                continue;
            }

            unsigned short vi[3];
            for (int j = 2; j < polyMesh->nvp; ++j) {
                if (p[j] == RC_MESH_NULL_IDX) {
                    break;
                }
                vi[0] = p[0];
                vi[1] = p[j - 1];
                vi[2] = p[j];

                Vector vertices[3];

                for (int k = 0; k < 3; ++k) {
                    GetPolyMeshVertPosition(polyMesh, vi[k], vertices[k]);
                }

                for (int k = 0; k < 3; ++k) {
                    const Vector delta = vertices[k] - ent->origin;

                    if (delta.lengthSquared() >= maxDistSquared) {
                        continue;
                    }

                    G_DebugLine(vertices[k], vertices[(k + 1) % 3], 0, 1, 0, 1);
                }
            }
        }
#endif
        }
        break;
    case 2:
        {
            const float maxDistSquared = Square(ai_showroutes_distance->integer);
            gentity_t  *edict;

            DrawModel(ent->origin, worldMap, maxDistSquared);

            for (edict = active_edicts.next; edict != &active_edicts; edict = edict->next) {
                if (!edict->entity || edict->entity == world) {
                    continue;
                }

                if (edict->s.modelindex < 1 || edict->s.modelindex > navigationData.GetNumSubmodels()) {
                    continue;
                }

                const navModel_t& submodel = navigationData.GetSubmodel(edict->s.modelindex - 1);
                if (!submodel.surfaces.NumObjects()) {
                    // Could be a trigger
                    continue;
                }

                DrawModel(ent->origin, submodel, maxDistSquared, edict->entity->origin);
            }
        }
        break;
    }

    if (ai_showpath->integer && navMeshDt) {
        switch (ai_showpath->integer) {
        default:
        case 0:
            ai_startpath = ai_endpath = vec_zero;
            break;
        case 1:
            ai_startpath = ent->origin;
            break;
        case 2:
            ai_endpath = ent->origin;
            break;
        case 3:
            if (ai_lastpath != ai_showpath->integer) {
                TestAgent(ai_startpath, ai_endpath, ai_pathlist, &ai_numPaths, ARRAY_LEN(ai_pathlist));
            }
            break;
        }

        ai_lastpath = ai_showpath->integer;

        if (crowd) {
            crowd->update(level.frametime, NULL);
        }

        for (int i = 0; i < ai_numPaths - 1; i++) {
            const Vector v1 = ai_pathlist[i] + Vector(0, 0, 16);
            const Vector v2 = ai_pathlist[i + 1] + Vector(0, 0, 16);

            G_DebugLine(v1, v2, 1.0, 0.0, 1.0, 1.0);
        }

#if 0
        if (agentId != -1) {
            const dtCrowdAgent* agent = crowd->getAgent(agentId);

            const dtPolyRef* path = agent->corridor.getPath();
            const int npath = agent->corridor.getPathCount();
            for (int i = 0; i < npath; ++i) {
                const dtMeshTile* tile;
                const dtPoly* poly;
                navMeshDt->getTileAndPolyByRef(path[i], &tile, &poly);

                const unsigned int tileId = (unsigned int)(poly - tile->polys);

                const dtPolyDetail* dm = &tile->detailMeshes[tileId];

                for (int j = 0; j < dm->triCount; ++j)
                {
                    const unsigned char* t = &tile->detailTris[(dm->triBase + i) * 4];
                    for (int k = 0; k < 3; ++k)
                    {
                        const float* pv1;
                        const float* pv2;

                        if (t[k] < poly->vertCount) {
                            pv1 = &tile->verts[poly->verts[t[k]] * 3];
                            pv2 = &tile->verts[poly->verts[t[(k + 1) % 3]] * 3];
                        } else {
                            pv1 = &tile->detailVerts[(dm->vertBase + t[k] - poly->vertCount) * 3];
                            pv2 = &tile->detailVerts[(dm->vertBase + t[(k + 1) % 3] - poly->vertCount) * 3];
                        }

                        Vector v1, v2;
                        ConvertRecastToGameCoord(pv1, v1);
                        ConvertRecastToGameCoord(pv2, v2);

                        G_DebugLine(v1, v2, 0.0, 1.0, 0.0, 1.0);
                    }
                }
            }
        }
#endif

#if 0
        const dtNodePool* pool = navMeshQuery->getNodePool();

        if (pool) {
            for (int i = 0; i < pool->getHashSize(); ++i)
            {
                for (dtNodeIndex j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j))
                {
                    const dtNode* node = pool->getNodeAtIdx(j + 1);
                    if (!node) continue;

                    Vector pos;
                    ConvertRecastToGameCoord(node->pos, pos);

                    G_DebugBBox(pos, Vector(-8, -8, -8), Vector(8, 8, 8), 0.0, 1.0, 0.0, 1.0);
                }
            }
        }
#endif
    }
}

/*
============
G_Navigation_DebugDraw
============
*/
void G_Navigation_DebugDraw()
{
    navigationMapDebug.DebugDraw();
}