File: navigation_recast_obstacle.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 (540 lines) | stat: -rw-r--r-- 14,094 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
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
/*
===========================================================================
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
===========================================================================
*/

#pragma once

#include "g_local.h"
#include "navigation_recast_obstacle.h"
#include "navigation_recast_load.h"
#include "navigation_recast_config.h"
#include "navigation_recast_helpers.h"
#include "entity.h"
#include "trigger.h"

#include "DetourNavMeshQuery.h"

NavigationObstacleMap navigationObstacleMap;

/**
 * @brief Iterate through polys and set flags
 * 
 */
class NavigationObstacleQueryBase : public dtPolyQuery
{
protected:
    const dtNavMesh         *navMesh;
    NavigationObstacleTiles& tiles;

public:
    float maxRadiusSqr;

public:
    NavigationObstacleQueryBase(const dtNavMesh *inNavMesh, NavigationObstacleTiles& inTiles)
        : navMesh(inNavMesh)
        , tiles(inTiles)
    {}

    float getPolySizeSqr(const dtMeshTile *tile, const dtPoly *poly, Vector& centroid) const
    {
        Vector rcBounds[2];
        Vector rcCentroid;
        Vector size;
        int    i;

        //
        // Calculate the polys bounds
        //

        ClearBounds(rcBounds[0], rcBounds[1]);

        for (i = 0; i < poly->vertCount; i++) {
            const float *pv = &tile->verts[poly->verts[i] * 3];

            AddPointToBounds(pv, rcBounds[0], rcBounds[1]);
        }

        size       = rcBounds[1] - rcBounds[0];
        rcCentroid = (rcBounds[1] + rcBounds[0]) * 0.5;
        ConvertRecastToGameCoord(rcCentroid, centroid);

        return size.lengthSquared();
    }
};

class NavigationObstacleQueryOccupied : public NavigationObstacleQueryBase
{
private:
    int complexEntityNum;

public:
    NavigationObstacleQueryOccupied(
        const dtNavMesh *inNavMesh, NavigationObstacleTiles& inTiles, int inComplexEntityNum
    )
        : NavigationObstacleQueryBase(inNavMesh, inTiles)
        , complexEntityNum(inComplexEntityNum)
    {}

    void process(const dtMeshTile *tile, dtPoly **polys, dtPolyRef *refs, int count) override
    {
        const int tileNum = tile - navMesh->getTile(0);
        int       i;

        for (i = 0; i < count; i++) {
            const int   polyNum = polys[i] - tile->polys;
            Vector      centroid;
            const float radiusSqr = getPolySizeSqr(tile, polys[i], centroid);

            if (radiusSqr > maxRadiusSqr) {
                continue;
            }

            // Still count the poly even if it collides with an object
            // This is because we don't keep history of collision when releasing
            tiles.polysRef[tileNum][polyNum]++;

            if (complexEntityNum != -1) {
                trace_t trace;
                Vector  mins(MINS_X * 0.75, MINS_Y * 0.75, MINS_Z);
                Vector  maxs(MAXS_X * 0.75, MAXS_Y * 0.75, MAXS_Z);

                trace = G_Trace(
                    centroid, mins, maxs, centroid, NULL, CONTENTS_SOLID, qfalse, "NavigationObstacleQueryOccupied"
                );

                if (trace.entityNum != complexEntityNum && trace.entityNum != ENTITYNUM_WORLD) {
                    // No valid collision
                    continue;
                }
            }

            // Mark as busy as long as the poly size is below the allowed radius
            polys[i]->flags |= RECAST_POLYFLAG_BUSY;
        }

        tiles.tilesRef[tileNum]++;
    }
};

class NavigationObstacleQueryReleased : public NavigationObstacleQueryBase
{
public:
    NavigationObstacleQueryReleased(const dtNavMesh *inNavMesh, NavigationObstacleTiles& inTiles)
        : NavigationObstacleQueryBase(inNavMesh, inTiles)
    {}

    void process(const dtMeshTile *tile, dtPoly **polys, dtPolyRef *refs, int count) override
    {
        const int tileNum = tile - navMesh->getTile(0);
        int       i;

        for (i = 0; i < count; i++) {
            const int   polyNum = polys[i] - tile->polys;
            Vector      centroid;
            const float radiusSqr = getPolySizeSqr(tile, polys[i], centroid);

            if (radiusSqr > maxRadiusSqr) {
                continue;
            }

            // Last poly, release it
            if (tiles.polysRef[tileNum][polyNum] == 1) {
                polys[i]->flags &= ~RECAST_POLYFLAG_BUSY;
            }

            assert(tiles.polysRef[tileNum][polyNum] > 0);
            if (tiles.polysRef[tileNum][polyNum] > 0) {
                tiles.polysRef[tileNum][polyNum]--;
            }
        }

        assert(tiles.tilesRef[tileNum] > 0);
        if (tiles.tilesRef[tileNum] > 0) {
            tiles.tilesRef[tileNum]--;
        }
    }
};

const Vector& NavigationObstacleEntities::GetMin(unsigned int entnum) const
{
    return bounds[0][entnum];
}

const Vector& NavigationObstacleEntities::GetMax(unsigned int entnum) const
{
    return bounds[1][entnum];
}

int NavigationObstacleEntities::GetContents(unsigned int entnum) const
{
    return contents[entnum];
}

void NavigationObstacleEntities::SetContents(unsigned int entnum, int c)
{
    contents[entnum] = c;
}

solid_t NavigationObstacleEntities::GetSolidType(unsigned int entnum) const
{
    return solid[entnum];
}

void NavigationObstacleEntities::SetSolidType(unsigned int entnum, solid_t type)
{
    solid[entnum] = type;
}

void NavigationObstacleEntities::SetBounds(unsigned int entnum, const Vector& min, const Vector& max)
{
    bounds[0][entnum] = min;
    bounds[1][entnum] = max;
}

bool NavigationObstacleEntities::IsActive(unsigned int entnum) const
{
    return flag[entnum] & NAVOBS_FLAG_ACTIVE;
}

void NavigationObstacleEntities::Add(unsigned int entnum)
{
    flag[entnum] = NAVOBS_FLAG_ACTIVE;
}

void NavigationObstacleEntities::Set(unsigned int entnum, const Vector& position)
{
    flag[entnum] = NAVOBS_FLAG_ACTIVE;
}

void NavigationObstacleEntities::Remove(unsigned int entnum)
{
    bounds[0][entnum] = vec_zero;
    bounds[1][entnum] = vec_zero;
    flag[entnum]      = 0;
    contents[entnum]  = 0;
    solid[entnum]     = SOLID_NOT;
}

NavigationObstacleTiles::NavigationObstacleTiles()
{
    tilesRef = NULL;
    polysRef = NULL;
}

NavigationObstacleTiles::~NavigationObstacleTiles()
{
    Clear();
}

void NavigationObstacleTiles::Init()
{
    const dtNavMesh *navMesh = navigationMap.GetNavMesh();
    int              numTotalPolys;
    int              i;
    int              totalSize;
    byte            *start;
    byte            *buffer;

    Clear();

    numTiles      = navMesh->getMaxTiles();
    numTotalPolys = 0;

    //
    // Calculate the total number of polys from all tiles
    //

    for (i = 0; i < numTiles; i++) {
        const dtMeshTile *tile = navMesh->getTile(i);
        if (tile && tile->header) {
            numTotalPolys += tile->header->polyCount;
        }
    }

    //
    // Allocate one giant big chunk of memory to hold all that information
    //

    totalSize =
        sizeof(unsigned int) * numTiles + sizeof(unsigned int *) * numTiles + sizeof(unsigned int) * numTotalPolys;
    buffer = start = (byte *)gi.Malloc(totalSize);

    tilesRef = new (buffer) unsigned int[numTiles]();
    buffer += sizeof(unsigned int) * numTiles;

    polysRef = new (buffer) unsigned int *[numTiles];
    buffer += sizeof(unsigned int *) * numTiles;

    for (i = 0; i < numTiles; i++) {
        const dtMeshTile *tile = navMesh->getTile(i);
        if (tile && tile->header) {
            polysRef[i] = new (buffer) unsigned int[tile->header->polyCount]();
            buffer += sizeof(unsigned int) * tile->header->polyCount;
        }
    }

    assert(buffer - start <= totalSize);
}

void NavigationObstacleTiles::Clear()
{
    // Single block of memory
    if (tilesRef) {
        gi.Free(tilesRef);
        tilesRef = NULL;
    }
}

NavigationObstacleEntities::NavigationObstacleEntities()
{
    int i;

    for (i = 0; i < ARRAY_LEN(flag); i++) {
        flag[i] = 0;
    }
}

NavigationObstacleMap::NavigationObstacleMap()
{
    ents = NULL;
}

NavigationObstacleMap::~NavigationObstacleMap()
{
    Clear();
}

void NavigationObstacleMap::Clear()
{
    if (ents) {
        delete ents;
        ents = NULL;
    }

    tiles.Clear();
}

void NavigationObstacleMap::Init()
{
    Clear();

    ents = new NavigationObstacleEntities();
    tiles.Init();
}

void NavigationObstacleMap::Update()
{
    gentity_t *ent;
    int        i;

    if (!ents) {
        return;
    }

    for (i = 0; i < MAX_GENTITIES; i++) {
        ent = &g_entities[i];

        if (i == ENTITYNUM_WORLD) {
            continue;
        }

        if (IsValidEntity(ent)) {
            if (!ents->IsActive(i)) {
                EntityAdded(ent);
            } else if (HasChanged(ent)) {
                EntityChanged(ent);
            }
        } else if (ents->IsActive(i)) {
            EntityRemoved(ent);
        }
    }
}

bool NavigationObstacleMap::IsValidEntity(gentity_t *ent) const
{
    if (!ent->inuse || !ent->entity) {
        return false;
    }

    if (!IsSpecialEntity(ent)) {
        if (ent->s.solid == SOLID_NOT || ent->s.solid == SOLID_TRIGGER) {
            return false;
        }

        // Same contents as player's clipmask
        if (!(ent->r.contents & MASK_PLAYERSOLID)) {
            return false;
        }
    }

    // Ignore other sentients
    if (ent->entity->IsSubclassOfSentient()) {
        return false;
    }

    // Ignore doors as they can be interacted
    if (ent->entity->IsSubclassOfDoor()) {
        return false;
    }

    return true;
}

bool NavigationObstacleMap::HasChanged(gentity_t *ent) const
{
    const int entnum = ent - g_entities;

    if (ents->GetMin(entnum) != ent->r.absmin || ents->GetMax(entnum) != ent->r.absmax) {
        return true;
    }

    if (ents->GetSolidType(entnum) != ent->solid) {
        return true;
    }

    if (ents->GetContents(entnum) != ent->r.contents) {
        return true;
    }

    return false;
}

bool NavigationObstacleMap::IsSpecialEntity(gentity_t *ent) const
{
    const Trigger *trig;

    //
    // FIXME: Use an alternative solution other than hardcoding
    //

    if (!ent->entity->isSubclassOf(Trigger)) {
        return false;
    }

    trig = static_cast<Trigger *>(ent->entity);

    if (str::icmp(ent->entity->targetname, "minefield")) {
        return false;
    }

    return true;
}

void NavigationObstacleMap::EntityAdded(gentity_t *ent)
{
    int entnum = ent - g_entities;

    ents->Add(entnum);
    ents->SetBounds(entnum, ent->r.absmin, ent->r.absmax);
    ents->SetSolidType(entnum, ent->solid);
    ents->SetContents(entnum, ent->r.contents);

    // Mark the poly as occupied
    EngagePolysAt(ent, ent->r.absmin, ent->r.absmax);
}

void NavigationObstacleMap::EntityRemoved(gentity_t *ent)
{
    int entnum = ent - g_entities;

    // Release the poly
    ReleasePolysAt(ent, ents->GetMin(entnum), ents->GetMax(entnum));

    ents->Remove(entnum);
}

void NavigationObstacleMap::EntityChanged(gentity_t *ent)
{
    int entnum = ent - g_entities;

    // Release polys at the old location
    ReleasePolysAt(ent, ents->GetMin(entnum), ents->GetMax(entnum));

    // Mark polys as occupied at the new location
    EngagePolysAt(ent, ent->r.absmin, ent->r.absmax);
    ents->SetBounds(entnum, ent->r.absmin, ent->r.absmax);
    ents->SetSolidType(entnum, ent->solid);
    ents->SetContents(entnum, ent->r.contents);
}

void NavigationObstacleMap::EngagePolysAt(gentity_t *ent, const Vector& min, const Vector& max)
{
    dtNavMeshQuery *navMeshQuery = navigationMap.GetNavMeshQuery();
    // Use complex collision for BSP brushes
    NavigationObstacleQueryOccupied query(
        navigationMap.GetNavMesh(), tiles, ent->solid == SOLID_BSP ? ent - g_entities : -1
    );

    Vector center;
    Vector halfExtents;
    Vector size;
    float  radiusSqr;
    center      = (max + min) * 0.5;
    size        = max - min;
    halfExtents = size * 0.5;

    radiusSqr = halfExtents.lengthXYSquared();
    // Minimum size of 100 units (sphere)
    // So objects like barrels that the bot can get around are ignored.
    if (radiusSqr < Square(100)) {
        return;
    }

    // Allow the object to cover polygons up to twice its size
    query.maxRadiusSqr = radiusSqr * Square(1.5);

    float rcCenter[3];
    float rcHalfExtents[3];
    ConvertGameToRecastCoord(center, rcCenter);
    ConvertGameToRecastExtents(halfExtents, rcHalfExtents);

    dtQueryFilter filter;
    navMeshQuery->queryPolygons(rcCenter, rcHalfExtents, &filter, &query);
}

void NavigationObstacleMap::ReleasePolysAt(gentity_t *ent, const Vector& min, const Vector& max)
{
    dtNavMeshQuery                 *navMeshQuery = navigationMap.GetNavMeshQuery();
    NavigationObstacleQueryReleased query(navigationMap.GetNavMesh(), tiles);

    Vector center;
    Vector halfExtents;
    Vector size;
    float  radiusSqr;
    center      = (max + min) * 0.5;
    size        = max - min;
    halfExtents = size * 0.5;

    radiusSqr = halfExtents.lengthXYSquared();
    if (radiusSqr < Square(100)) {
        return;
    }

    query.maxRadiusSqr = radiusSqr * Square(1.5);

    float rcCenter[3];
    float rcHalfExtents[3];
    ConvertGameToRecastCoord(center, rcCenter);
    ConvertGameToRecastExtents(halfExtents, rcHalfExtents);

    dtQueryFilter filter;
    navMeshQuery->queryPolygons(rcCenter, rcHalfExtents, &filter, &query);
}