Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#include "DetourTileCache.h"
#include "DetourTileCacheBuilder.h"
#include "DetourNavMeshBuilder.h"
#include "DetourNavMesh.h"
#include "DetourCommon.h"
#include "DetourMath.h"
#include "DetourAlloc.h"
#include "DetourAssert.h"
#include <string.h>
#include <new>

dtTileCache* dtAllocTileCache()
{
    void* mem = dtAlloc(sizeof(dtTileCache), DT_ALLOC_PERM);
    if (!mem) return 0;
    return new(mem) dtTileCache;
}

void dtFreeTileCache(dtTileCache* tc)
{
    if (!tc) return;
    tc->~dtTileCache();
    dtFree(tc);
}

static bool contains(const dtCompressedTileRef* a, const int n, const dtCompressedTileRef v)
{
    for (int i = 0; i < n; ++i)
        if (a[i] == v)
            return true;
    return false;
}

inline int computeTileHash(int x, int y, const int mask)
{
    const unsigned int h1 = 0x8da6b343; // Large multiplicative constants;
    const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes
    unsigned int n = h1 * x + h2 * y;
    return (int)(n & mask);
}


struct BuildContext
{
    inline BuildContext(struct dtTileCacheAlloc* a) : layer(0), lcset(0), lmesh(0), alloc(a) {}
    inline ~BuildContext() { purge(); }
    void purge()
    {
        dtFreeTileCacheLayer(alloc, layer);
        layer = 0;
        dtFreeTileCacheContourSet(alloc, lcset);
        lcset = 0;
        dtFreeTileCachePolyMesh(alloc, lmesh);
        lmesh = 0;
    }
    struct dtTileCacheLayer* layer;
    struct dtTileCacheContourSet* lcset;
    struct dtTileCachePolyMesh* lmesh;
    struct dtTileCacheAlloc* alloc;
};


dtTileCache::dtTileCache() :
    m_tileLutSize(0),
    m_tileLutMask(0),
    m_posLookup(0),
    m_nextFreeTile(0),    
    m_tiles(0),    
    m_saltBits(0),
    m_tileBits(0),
    m_talloc(0),
    m_tcomp(0),
    m_tmproc(0),
    m_obstacles(0),
    m_nextFreeObstacle(0),
    m_nreqs(0),
    m_nupdate(0)
{
    memset(&m_params, 0, sizeof(m_params));
}
    
dtTileCache::~dtTileCache()
{
    for (int i = 0; i < m_params.maxTiles; ++i)
    {
        if (m_tiles[i].flags & DT_COMPRESSEDTILE_FREE_DATA)
        {
            dtFree(m_tiles[i].data);
            m_tiles[i].data = 0;
        }
    }
    dtFree(m_obstacles);
    m_obstacles = 0;
    dtFree(m_posLookup);
    m_posLookup = 0;
    dtFree(m_tiles);
    m_tiles = 0;
    m_nreqs = 0;
    m_nupdate = 0;
}

const dtCompressedTile* dtTileCache::getTileByRef(dtCompressedTileRef ref) const
{
    if (!ref)
        return 0;
    unsigned int tileIndex = decodeTileIdTile(ref);
    unsigned int tileSalt = decodeTileIdSalt(ref);
    if ((int)tileIndex >= m_params.maxTiles)
        return 0;
    const dtCompressedTile* tile = &m_tiles[tileIndex];
    if (tile->salt != tileSalt)
        return 0;
    return tile;
}


dtStatus dtTileCache::init(const dtTileCacheParams* params,
                           dtTileCacheAlloc* talloc,
                           dtTileCacheCompressor* tcomp,
                           dtTileCacheMeshProcess* tmproc)
{
    m_talloc = talloc;
    m_tcomp = tcomp;
    m_tmproc = tmproc;
    m_nreqs = 0;
    memcpy(&m_params, params, sizeof(m_params));
    
    // Alloc space for obstacles.
    m_obstacles = (dtTileCacheObstacle*)dtAlloc(sizeof(dtTileCacheObstacle)*m_params.maxObstacles, DT_ALLOC_PERM);
    if (!m_obstacles)
        return DT_FAILURE | DT_OUT_OF_MEMORY;
    memset(m_obstacles, 0, sizeof(dtTileCacheObstacle)*m_params.maxObstacles);
    m_nextFreeObstacle = 0;
    for (int i = m_params.maxObstacles-1; i >= 0; --i)
    {
        m_obstacles[i].salt = 1;
        m_obstacles[i].next = m_nextFreeObstacle;
        m_nextFreeObstacle = &m_obstacles[i];
    }
    
    // Init tiles
    m_tileLutSize = dtNextPow2(m_params.maxTiles/4);
    if (!m_tileLutSize) m_tileLutSize = 1;
    m_tileLutMask = m_tileLutSize-1;
    
    m_tiles = (dtCompressedTile*)dtAlloc(sizeof(dtCompressedTile)*m_params.maxTiles, DT_ALLOC_PERM);
    if (!m_tiles)
        return DT_FAILURE | DT_OUT_OF_MEMORY;
    m_posLookup = (dtCompressedTile**)dtAlloc(sizeof(dtCompressedTile*)*m_tileLutSize, DT_ALLOC_PERM);
    if (!m_posLookup)
        return DT_FAILURE | DT_OUT_OF_MEMORY;
    memset(m_tiles, 0, sizeof(dtCompressedTile)*m_params.maxTiles);
    memset(m_posLookup, 0, sizeof(dtCompressedTile*)*m_tileLutSize);
    m_nextFreeTile = 0;
    for (int i = m_params.maxTiles-1; i >= 0; --i)
    {
        m_tiles[i].salt = 1;
        m_tiles[i].next = m_nextFreeTile;
        m_nextFreeTile = &m_tiles[i];
    }
    
    // Init ID generator values.
    m_tileBits = dtIlog2(dtNextPow2((unsigned int)m_params.maxTiles));
    // Only allow 31 salt bits, since the salt mask is calculated using 32bit uint and it will overflow.
    m_saltBits = dtMin((unsigned int)31, 32 - m_tileBits);
    if (m_saltBits < 10)
        return DT_FAILURE | DT_INVALID_PARAM;
    
    return DT_SUCCESS;
}

int dtTileCache::getTilesAt(const int tx, const int ty, dtCompressedTileRef* tiles, const int maxTiles) const 
{
    int n = 0;
    
    // Find tile based on hash.
    int h = computeTileHash(tx,ty,m_tileLutMask);
    dtCompressedTile* tile = m_posLookup[h];
    while (tile)
    {
        if (tile->header &&
            tile->header->tx == tx &&
            tile->header->ty == ty)
        {
            if (n < maxTiles)
                tiles[n++] = getTileRef(tile);
        }
        tile = tile->next;
    }
    
    return n;
}

dtCompressedTile* dtTileCache::getTileAt(const int tx, const int ty, const int tlayer)
{
    // Find tile based on hash.
    int h = computeTileHash(tx,ty,m_tileLutMask);
    dtCompressedTile* tile = m_posLookup[h];
    while (tile)
    {
        if (tile->header &&
            tile->header->tx == tx &&
            tile->header->ty == ty &&
            tile->header->tlayer == tlayer)
        {
            return tile;
        }
        tile = tile->next;
    }
    return 0;
}

dtCompressedTileRef dtTileCache::getTileRef(const dtCompressedTile* tile) const
{
    if (!tile) return 0;
    const unsigned int it = (unsigned int)(tile - m_tiles);
    return (dtCompressedTileRef)encodeTileId(tile->salt, it);
}

dtObstacleRef dtTileCache::getObstacleRef(const dtTileCacheObstacle* ob) const
{
    if (!ob) return 0;
    const unsigned int idx = (unsigned int)(ob - m_obstacles);
    return encodeObstacleId(ob->salt, idx);
}

const dtTileCacheObstacle* dtTileCache::getObstacleByRef(dtObstacleRef ref)
{
    if (!ref)
        return 0;
    unsigned int idx = decodeObstacleIdObstacle(ref);
    if ((int)idx >= m_params.maxObstacles)
        return 0;
    const dtTileCacheObstacle* ob = &m_obstacles[idx];
    unsigned int salt = decodeObstacleIdSalt(ref);
    if (ob->salt != salt)
        return 0;
    return ob;
}

dtStatus dtTileCache::addTile(unsigned char* data, const int dataSize, unsigned char flags, dtCompressedTileRef* result)
{
    // Make sure the data is in right format.
    dtTileCacheLayerHeader* header = (dtTileCacheLayerHeader*)data;
    if (header->magic != DT_TILECACHE_MAGIC)
        return DT_FAILURE | DT_WRONG_MAGIC;
    if (header->version != DT_TILECACHE_VERSION)
        return DT_FAILURE | DT_WRONG_VERSION;
    
    // Make sure the location is free.
    if (getTileAt(header->tx, header->ty, header->tlayer))
        return DT_FAILURE;
    
    // Allocate a tile.
    dtCompressedTile* tile = 0;
    if (m_nextFreeTile)
    {
        tile = m_nextFreeTile;
        m_nextFreeTile = tile->next;
        tile->next = 0;
    }
    
    // Make sure we could allocate a tile.
    if (!tile)
        return DT_FAILURE | DT_OUT_OF_MEMORY;
    
    // Insert tile into the position lut.
    int h = computeTileHash(header->tx, header->ty, m_tileLutMask);
    tile->next = m_posLookup[h];
    m_posLookup[h] = tile;
    
    // Init tile.
    const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader));
    tile->header = (dtTileCacheLayerHeader*)data;
    tile->data = data;
    tile->dataSize = dataSize;
    tile->compressed = tile->data + headerSize;
    tile->compressedSize = tile->dataSize - headerSize;
    tile->flags = flags;
    
    if (result)
        *result = getTileRef(tile);
    
    return DT_SUCCESS;
}

dtStatus dtTileCache::removeTile(dtCompressedTileRef ref, unsigned char** data, int* dataSize)
{
    if (!ref)
        return DT_FAILURE | DT_INVALID_PARAM;
    unsigned int tileIndex = decodeTileIdTile(ref);
    unsigned int tileSalt = decodeTileIdSalt(ref);
    if ((int)tileIndex >= m_params.maxTiles)
        return DT_FAILURE | DT_INVALID_PARAM;
    dtCompressedTile* tile = &m_tiles[tileIndex];
    if (tile->salt != tileSalt)
        return DT_FAILURE | DT_INVALID_PARAM;
    
    // Remove tile from hash lookup.
    const int h = computeTileHash(tile->header->tx,tile->header->ty,m_tileLutMask);
    dtCompressedTile* prev = 0;
    dtCompressedTile* cur = m_posLookup[h];
    while (cur)
    {
        if (cur == tile)
        {
            if (prev)
                prev->next = cur->next;
            else
                m_posLookup[h] = cur->next;
            break;
        }
        prev = cur;
        cur = cur->next;
    }
    
    // Reset tile.
    if (tile->flags & DT_COMPRESSEDTILE_FREE_DATA)
    {
        // Owns data
        dtFree(tile->data);
        tile->data = 0;
        tile->dataSize = 0;
        if (data) *data = 0;
        if (dataSize) *dataSize = 0;
    }
    else
    {
        if (data) *data = tile->data;
        if (dataSize) *dataSize = tile->dataSize;
    }
    
    tile->header = 0;
    tile->data = 0;
    tile->dataSize = 0;
    tile->compressed = 0;
    tile->compressedSize = 0;
    tile->flags = 0;
    
    // Update salt, salt should never be zero.
    tile->salt = (tile->salt+1) & ((1<<m_saltBits)-1);
    if (tile->salt == 0)
        tile->salt++;
    
    // Add to free list.
    tile->next = m_nextFreeTile;
    m_nextFreeTile = tile;
    
    return DT_SUCCESS;
}


dtObstacleRef dtTileCache::addObstacle(const float* pos, const float radius, const float height, dtObstacleRef* result)
{
    if (m_nreqs >= MAX_REQUESTS)
        return DT_FAILURE | DT_BUFFER_TOO_SMALL;
    
    dtTileCacheObstacle* ob = 0;
    if (m_nextFreeObstacle)
    {
        ob = m_nextFreeObstacle;
        m_nextFreeObstacle = ob->next;
        ob->next = 0;
    }
    if (!ob)
        return DT_FAILURE | DT_OUT_OF_MEMORY;
    
    unsigned short salt = ob->salt;
    memset(ob, 0, sizeof(dtTileCacheObstacle));
    ob->salt = salt;
    ob->state = DT_OBSTACLE_PROCESSING;
    dtVcopy(ob->pos, pos);
    ob->radius = radius;
    ob->height = height;
    
    ObstacleRequest* req = &m_reqs[m_nreqs++];
    memset(req, 0, sizeof(ObstacleRequest));
    req->action = REQUEST_ADD;
    req->ref = getObstacleRef(ob);
    
    if (result)
        *result = req->ref;
    
    return DT_SUCCESS;
}

dtObstacleRef dtTileCache::removeObstacle(const dtObstacleRef ref)
{
    if (!ref)
        return DT_SUCCESS;
    if (m_nreqs >= MAX_REQUESTS)
        return DT_FAILURE | DT_BUFFER_TOO_SMALL;
    
    ObstacleRequest* req = &m_reqs[m_nreqs++];
    memset(req, 0, sizeof(ObstacleRequest));
    req->action = REQUEST_REMOVE;
    req->ref = ref;
    
    return DT_SUCCESS;
}

dtStatus dtTileCache::queryTiles(const float* bmin, const float* bmax,
                                 dtCompressedTileRef* results, int* resultCount, const int maxResults) const 
{
    const int MAX_TILES = 32;
    dtCompressedTileRef tiles[MAX_TILES];
    
    int n = 0;
    
    const float tw = m_params.width * m_params.cs;
    const float th = m_params.height * m_params.cs;
    const int tx0 = (int)dtMathFloorf((bmin[0]-m_params.orig[0]) / tw);
    const int tx1 = (int)dtMathFloorf((bmax[0]-m_params.orig[0]) / tw);
    const int ty0 = (int)dtMathFloorf((bmin[2]-m_params.orig[2]) / th);
    const int ty1 = (int)dtMathFloorf((bmax[2]-m_params.orig[2]) / th);
    
    for (int ty = ty0; ty <= ty1; ++ty)
    {
        for (int tx = tx0; tx <= tx1; ++tx)
        {
            const int ntiles = getTilesAt(tx,ty,tiles,MAX_TILES);
            
            for (int i = 0; i < ntiles; ++i)
            {
                const dtCompressedTile* tile = &m_tiles[decodeTileIdTile(tiles[i])];
                float tbmin[3], tbmax[3];
                calcTightTileBounds(tile->header, tbmin, tbmax);
                
                if (dtOverlapBounds(bmin,bmax, tbmin,tbmax))
                {
                    if (n < maxResults)
                        results[n++] = tiles[i];
                }
            }
        }
    }
    
    *resultCount = n;
    
    return DT_SUCCESS;
}

dtStatus dtTileCache::update(const float /*dt*/, dtNavMesh* navmesh)
{
    if (m_nupdate == 0)
    {
        // Process requests.
        for (int i = 0; i < m_nreqs; ++i)
        {
            ObstacleRequest* req = &m_reqs[i];
            
            unsigned int idx = decodeObstacleIdObstacle(req->ref);
            if ((int)idx >= m_params.maxObstacles)
                continue;
            dtTileCacheObstacle* ob = &m_obstacles[idx];
            unsigned int salt = decodeObstacleIdSalt(req->ref);
            if (ob->salt != salt)
                continue;
            
            if (req->action == REQUEST_ADD)
            {
                // Find touched tiles.
                float bmin[3], bmax[3];
                getObstacleBounds(ob, bmin, bmax);

                int ntouched = 0;
                queryTiles(bmin, bmax, ob->touched, &ntouched, DT_MAX_TOUCHED_TILES);
                ob->ntouched = (unsigned char)ntouched;
                // Add tiles to update list.
                ob->npending = 0;
                for (int j = 0; j < ob->ntouched; ++j)
                {
                    if (m_nupdate < MAX_UPDATE)
                    {
                        if (!contains(m_update, m_nupdate, ob->touched[j]))
                            m_update[m_nupdate++] = ob->touched[j];
                        ob->pending[ob->npending++] = ob->touched[j];
                    }
                }
            }
            else if (req->action == REQUEST_REMOVE)
            {
                // Prepare to remove obstacle.
                ob->state = DT_OBSTACLE_REMOVING;
                // Add tiles to update list.
                ob->npending = 0;
                for (int j = 0; j < ob->ntouched; ++j)
                {
                    if (m_nupdate < MAX_UPDATE)
                    {
                        if (!contains(m_update, m_nupdate, ob->touched[j]))
                            m_update[m_nupdate++] = ob->touched[j];
                        ob->pending[ob->npending++] = ob->touched[j];
                    }
                }
            }
        }
        
        m_nreqs = 0;
    }
    
    // Process updates
    if (m_nupdate)
    {
        // Build mesh
        const dtCompressedTileRef ref = m_update[0];
        dtStatus status = buildNavMeshTile(ref, navmesh);
        m_nupdate--;
        if (m_nupdate > 0)
            memmove(m_update, m_update+1, m_nupdate*sizeof(dtCompressedTileRef));

        // Update obstacle states.
        for (int i = 0; i < m_params.maxObstacles; ++i)
        {
            dtTileCacheObstacle* ob = &m_obstacles[i];
            if (ob->state == DT_OBSTACLE_PROCESSING || ob->state == DT_OBSTACLE_REMOVING)
            {
                // Remove handled tile from pending list.
                for (int j = 0; j < (int)ob->npending; j++)
                {
                    if (ob->pending[j] == ref)
                    {
                        ob->pending[j] = ob->pending[(int)ob->npending-1];
                        ob->npending--;
                        break;
                    }
                }
                
                // If all pending tiles processed, change state.
                if (ob->npending == 0)
                {
                    if (ob->state == DT_OBSTACLE_PROCESSING)
                    {
                        ob->state = DT_OBSTACLE_PROCESSED;
                    }
                    else if (ob->state == DT_OBSTACLE_REMOVING)
                    {
                        ob->state = DT_OBSTACLE_EMPTY;
                        // Update salt, salt should never be zero.
                        ob->salt = (ob->salt+1) & ((1<<16)-1);
                        if (ob->salt == 0)
                            ob->salt++;
                        // Return obstacle to free list.
                        ob->next = m_nextFreeObstacle;
                        m_nextFreeObstacle = ob;
                    }
                }
            }
        }
            
        if (dtStatusFailed(status))
            return status;
    }
    
    return DT_SUCCESS;
}


dtStatus dtTileCache::buildNavMeshTilesAt(const int tx, const int ty, dtNavMesh* navmesh)
{
    const int MAX_TILES = 32;
    dtCompressedTileRef tiles[MAX_TILES];
    const int ntiles = getTilesAt(tx,ty,tiles,MAX_TILES);
    
    for (int i = 0; i < ntiles; ++i)
    {
        dtStatus status = buildNavMeshTile(tiles[i], navmesh);
        if (dtStatusFailed(status))
            return status;
    }
    
    return DT_SUCCESS;
}

dtStatus dtTileCache::buildNavMeshTile(const dtCompressedTileRef ref, dtNavMesh* navmesh)
{    
    dtAssert(m_talloc);
    dtAssert(m_tcomp);
    
    unsigned int idx = decodeTileIdTile(ref);
    if (idx > (unsigned int)m_params.maxTiles)
        return DT_FAILURE | DT_INVALID_PARAM;
    const dtCompressedTile* tile = &m_tiles[idx];
    unsigned int salt = decodeTileIdSalt(ref);
    if (tile->salt != salt)
        return DT_FAILURE | DT_INVALID_PARAM;
    
    m_talloc->reset();
    
    BuildContext bc(m_talloc);
    const int walkableClimbVx = (int)(m_params.walkableClimb / m_params.ch);
    dtStatus status;
    
    // Decompress tile layer data. 
    status = dtDecompressTileCacheLayer(m_talloc, m_tcomp, tile->data, tile->dataSize, &bc.layer);
    if (dtStatusFailed(status))
        return status;
    
    // Rasterize obstacles.
    for (int i = 0; i < m_params.maxObstacles; ++i)
    {
        const dtTileCacheObstacle* ob = &m_obstacles[i];
        if (ob->state == DT_OBSTACLE_EMPTY || ob->state == DT_OBSTACLE_REMOVING)
            continue;
        if (contains(ob->touched, ob->ntouched, ref))
        {
            dtMarkCylinderArea(*bc.layer, tile->header->bmin, m_params.cs, m_params.ch,
                               ob->pos, ob->radius, ob->height, 0);
        }
    }
    
    // Build navmesh
    status = dtBuildTileCacheRegions(m_talloc, *bc.layer, walkableClimbVx);
    if (dtStatusFailed(status))
        return status;
    
    bc.lcset = dtAllocTileCacheContourSet(m_talloc);
    if (!bc.lcset)
        return status;
    status = dtBuildTileCacheContours(m_talloc, *bc.layer, walkableClimbVx,
                                      m_params.maxSimplificationError, *bc.lcset);
    if (dtStatusFailed(status))
        return status;
    
    bc.lmesh = dtAllocTileCachePolyMesh(m_talloc);
    if (!bc.lmesh)
        return status;
    status = dtBuildTileCachePolyMesh(m_talloc, *bc.lcset, *bc.lmesh);
    if (dtStatusFailed(status))
        return status;
    
    // Early out if the mesh tile is empty.
    if (!bc.lmesh->npolys)
        return DT_SUCCESS;
    
    dtNavMeshCreateParams params;
    memset(&params, 0, sizeof(params));
    params.verts = bc.lmesh->verts;
    params.vertCount = bc.lmesh->nverts;
    params.polys = bc.lmesh->polys;
    params.polyAreas = bc.lmesh->areas;
    params.polyFlags = bc.lmesh->flags;
    params.polyCount = bc.lmesh->npolys;
    params.nvp = DT_VERTS_PER_POLYGON;
    params.walkableHeight = m_params.walkableHeight;
    params.walkableRadius = m_params.walkableRadius;
    params.walkableClimb = m_params.walkableClimb;
    params.tileX = tile->header->tx;
    params.tileY = tile->header->ty;
    params.tileLayer = tile->header->tlayer;
    params.cs = m_params.cs;
    params.ch = m_params.ch;
    params.buildBvTree = false;
    dtVcopy(params.bmin, tile->header->bmin);
    dtVcopy(params.bmax, tile->header->bmax);
    
    if (m_tmproc)
    {
        m_tmproc->process(&params, bc.lmesh->areas, bc.lmesh->flags);
    }
    
    unsigned char* navData = 0;
    int navDataSize = 0;
    if (!dtCreateNavMeshData(&params, &navData, &navDataSize))
        return DT_FAILURE;

    // Remove existing tile.
    navmesh->removeTile(navmesh->getTileRefAt(tile->header->tx,tile->header->ty,tile->header->tlayer),0,0);

    // Add new tile, or leave the location empty.
    if (navData)
    {
        // Let the navmesh own the data.
        status = navmesh->addTile(navData,navDataSize,DT_TILE_FREE_DATA,0,0);
        if (dtStatusFailed(status))
        {
            dtFree(navData);
            return status;
        }
    }
    
    return DT_SUCCESS;
}

void dtTileCache::calcTightTileBounds(const dtTileCacheLayerHeader* header, float* bmin, float* bmax) const
{
    const float cs = m_params.cs;
    bmin[0] = header->bmin[0] + header->minx*cs;
    bmin[1] = header->bmin[1];
    bmin[2] = header->bmin[2] + header->miny*cs;
    bmax[0] = header->bmin[0] + (header->maxx+1)*cs;
    bmax[1] = header->bmax[1];
    bmax[2] = header->bmin[2] + (header->maxy+1)*cs;
}

void dtTileCache::getObstacleBounds(const struct dtTileCacheObstacle* ob, float* bmin, float* bmax) const
{
    bmin[0] = ob->pos[0] - ob->radius;
    bmin[1] = ob->pos[1];
    bmin[2] = ob->pos[2] - ob->radius;
    bmax[0] = ob->pos[0] + ob->radius;
    bmax[1] = ob->pos[1] + ob->height;
    bmax[2] = ob->pos[2] + ob->radius;    
}

Commits for Divide-Framework/trunk/Source Code/Libs/src/ReCast/DetourTileCache/Source/DetourTileCache.cpp

Diff revisions: vs.
Revision Author Commited Message
337 Diff Diff IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)

315 Diff Diff IonutCava picture IonutCava Mon 25 Aug, 2014 15:23:33 +0000

[Ionut]
- Update ReCast to the latest version available on GitHub
- Improved nav mesh target point selection
- Added basic “go to enemy flag” logic for the WarScene teams

260 Diff Diff k1ngp1n picture k1ngp1n Sat 03 May, 2014 19:33:41 +0000

[Ionut] [[BR]]
- More work on CSM. Reverted back to VSM to allow easier blurring and filtering [[BR]]
- Rewrote FrameBuffer classes to fix MSAA and memory leaks that occurred on resize (double creation of textures) [[BR]]
— Removed FBType. FrameBuffer types depend only on attachment types and if it’s MSAA enabled or not [[BR]]
- Rewrote RenderState classes to be API agnostic [[BR]]
- Bug fixes in math classes [[BR]]
- Changed “resolution change” logic [[BR]]
- Simplified some options in config.xml [[BR]]
- Improved per-light shadow options [[BR]]

152 Diff Diff k1ngp1n picture k1ngp1n Thu 20 Jun, 2013 14:23:31 +0000

[Ionut] [[BR]]
- New rendering pipeline (experimental and unstable): [[BR]]
— OpenGL 3.x clean context (glLight commands are still used for now, but UBO support is there, but not used yet) [[BR]]
— Custom matrix stacks and UBO based shader data (UBOs cause some link errors on some nVidia cards)[[BR]]
— Multi-threaded texture loading and shader compilation (mt shader compilation disabled for now. needs more tests) [[BR]]
— VAO rendering is self contained (no more enable/disable calls before a draw request) [[BR]]
— Render instance class used for rendering information [[BR]]
[[BR]]
- New unique ID generation base class: GUIDWrapper. Each class that derives from this, gets an application wide unique id on construction [[BR]]
- Added base code for an in-game editor using CEGUI [[BR]]
- Replaced some mutex locks with lockless data structures or atomic data [[BR]]
- CEGUI 0.8.2, FTGL-GL3 and GLIM 0.4 modified to support GLEW_MX if defined [[BR]]
- Dropped VS2008 support [[BR]]
- Networking library, server framework and ReCast files are now projects inside the Divide Framework solution [[BR]]
- New console command “setFov” that modifies the horizontal FoV value in realtime [[BR]]
- New console command “recompileShader” that reparses shader files, recompiles shaders and relinks shader programs who’s names match or contain the argument string [[BR]]
- New debug utility: axis angle drawn in corner of the screen if needed [[BR]]
- More ‘const’-s pasted around the code, more loop performance tweaks (allocations before a loop, tighter loops, etc), more utility member functions added to various classes [[BR]]

151 k1ngp1n picture k1ngp1n Tue 07 May, 2013 00:00:52 +0000

[Ionut] [[BR]]
- Better ReCast integration [[BR]]
— Any entity can be used as an obstacle in the NavMesh if that’s specified in each scene’s assets.xml file for said entity [[BR]]
— New console command, “createNavMesh”, that passes geometry data to ReCast [[BR]]
-— NavMesh generation is not yet complete [[BR]]
- Better PhysX integration [[BR]]
— Each scene passes data to and gets data from the Physics API better and safer[[BR]]
- Multi-threaded support extended: [[BR]]
— Event class replaced with Task [[BR]]
— Kernel uses a threadpool for scene tasks [[BR]]
— Each task can be added to a different threadpool if needed [[BR]]
— Most critical sections use the new boost::atomic wrapper for thread safety [[BR]]
- Rendering system updated [[BR]]
— VBO’s keep track of the geometry type internally [[BR]]
— Rendering by specific element list is internal to the VBO [[BR]]
— VBO’s can draw a “lite” version that doesn’t send Tangent, BiTangent and TexCoord data for depth rendering (disabled) [[BR]]
- Reflectors now offer a preview of the generated reflective texture if needed [[BR]]
- Buttons are now added and rendered by CEGUI [[BR]]
[[BR]]
Deprecation: [[BR]]
- Removed fixed pipeline material system [[BR]]
- Removed BoundingBox late update system [[BR]]
- Shadow references in the material system are now depth references [[BR]]
[[BR]]
Bug fixes: [[BR]]
- Problematic FBO double bind check removed [[BR]]
- Faster terrain rendering via chunk size selection through each scene’s terrain.xml[[BR]]
- Rendering is paused when the window is minimized [[BR]]
- CEGUI and impostors now render properly in deferred rendering mode [[BR]]
- Fixed various level 4 (VS2008) warnings [[BR]]
- Better state management and cleaning [[BR]]
- Better SceneGraph node management and trasformation/AABB update calls [[BR]]
- AITenisScene has proper collisions now [[BR]]