-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathMapBuilder.cpp
More file actions
1273 lines (1093 loc) · 47.3 KB
/
MapBuilder.cpp
File metadata and controls
1273 lines (1093 loc) · 47.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
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "MMapCommon.h"
#include "MapBuilder.h"
#include "MapTree.h"
#include "ModelInstance.h"
#include "DetourNavMeshBuilder.h"
#include "DetourCommon.h"
#include <climits>
#include <fstream>
#include <future>
using namespace VMAP;
void rcModAlmostUnwalkableTriangles(rcContext* ctx, const float walkableSlopeAngle,
const float* verts, int /*nv*/,
const int* tris, int nt,
unsigned char* areas)
{
rcIgnoreUnused(ctx);
const float walkableThr = cosf(walkableSlopeAngle / 180.0f * RC_PI);
float norm[3];
for (int i = 0; i < nt; ++i)
{
if (areas[i] & RC_WALKABLE_AREA)
{
const int* tri = &tris[i * 3];
float e0[3], e1[3];
rcVsub(e0, &verts[tri[1] * 3], &verts[tri[0] * 3]);
rcVsub(e1, &verts[tri[2] * 3], &verts[tri[0] * 3]);
rcVcross(norm, e0, e1);
rcVnormalize(norm);
// Check if the face is walkable.
if (norm[1] <= walkableThr)
areas[i] = NAV_AREA_GROUND_STEEP; //Slopes between 50 and 60. Walkable for mobs, unwalkable for players.
}
}
}
void from_json(const json& j, rcConfig& config)
{
config.tileSize = MMAP::VERTEX_PER_TILE;
config.borderSize = j["borderSize"].get<int>();
config.cs = MMAP::BASE_UNIT_DIM;
config.ch = MMAP::BASE_UNIT_DIM;
config.walkableSlopeAngle = j["walkableSlopeAngle"].get<float>();
config.walkableHeight = j["walkableHeight"].get<int>();
config.walkableClimb = j["walkableClimb"].get<int>();
config.walkableRadius = j["walkableRadius"].get<int>();
config.maxEdgeLen = j["maxEdgeLen"].get<int>();
config.maxSimplificationError = j["maxSimplificationError"].get<float>();
config.minRegionArea = rcSqr(j["minRegionArea"].get<int>());
config.mergeRegionArea = rcSqr(j["mergeRegionArea"].get<int>());
config.maxVertsPerPoly = DT_VERTS_PER_POLYGON;
config.detailSampleDist = j["detailSampleDist"].get<float>();
config.detailSampleMaxError = j["detailSampleMaxError"].get<float>();
config.liquidFlagMergeThreshold = j["liquidFlagMergeThreshold"].get<float>();
}
namespace MMAP
{
inline char const* GetDTErrorReason(dtStatus status) {
if ((status & DT_WRONG_MAGIC) != 0)
return "Reason: 'Input data is not recognized'";
if ((status & DT_WRONG_VERSION) != 0)
return "Reason: 'Input data is in wrong version'";
if ((status & DT_OUT_OF_MEMORY) != 0)
return "Reason: 'Operation ran out of memory'";
if ((status & DT_INVALID_PARAM) != 0)
return "Reason: 'An input parameter was invalid'";
if ((status & DT_BUFFER_TOO_SMALL) != 0)
return "Reason: 'Result buffer for the query was too small to store all results'";
if ((status & DT_OUT_OF_NODES) != 0)
return "Reason: 'Query ran out of nodes during search'";
if ((status & DT_PARTIAL_RESULT) != 0)
return "Reason: 'Query did not reach the end location, returning best guess'";
if ((status & DT_ALREADY_OCCUPIED) != 0)
return "Reason: 'A tile has already been assigned to the given x,y coordinate'";
}
MapBuilder::MapBuilder(const char* configInputPath, int threads, bool skipLiquid, bool skipContinents, bool skipJunkMaps,
bool skipBattlegrounds, bool debug, const char* offMeshFilePath, const char* workdir) :
m_taskQueue(new TaskQueue(this, threads)),
m_debug(debug),
m_skipContinents(skipContinents),
m_skipJunkMaps(skipJunkMaps),
m_skipBattlegrounds(skipBattlegrounds),
m_offMeshFilePath(offMeshFilePath),
m_workdir(workdir)
{
std::ifstream jsonConfig(configInputPath);
if (jsonConfig)
m_config = json::parse(jsonConfig);
m_terrainBuilder = new TerrainBuilder(skipLiquid, workdir);
m_rcContext = new rcContext(false);
printf("Using %d thread(s) for processing.\n", threads);
discoverTiles();
}
/**************************************************************************/
MapBuilder::~MapBuilder()
{
delete m_terrainBuilder;
delete m_rcContext;
}
/**************************************************************************/
void MapBuilder::BuildMaps(std::vector<uint32>& ids)
{
if (ids.empty())
{
for (auto tileItr : m_tiles)
{
uint32 const& mapID = tileItr.first;
if (!shouldSkipMap(mapID))
buildMap(mapID);
m_mapDone.insert(mapID);
}
}
else
{
for (auto& mapId : ids)
{
if (!shouldSkipMap(mapId))
buildMap(mapId);
m_mapDone.insert(mapId);
}
}
// Wait all work to be done
m_taskQueue->WaitAll();
}
/**************************************************************************/
void MapBuilder::discoverTiles()
{
std::vector<std::string> files;
uint32 mapID, tileX, tileY, tileID, count = 0;
char filter[12];
char maps_dir[1024];
char vmaps_dir[1024];
printf("Discovering maps... ");
sprintf(maps_dir, "%s/maps", m_workdir);
getDirContents(files, maps_dir);
for (uint32 i = 0; i < files.size(); ++i)
{
mapID = uint32(atoi(files[i].substr(0, 3).c_str()));
if (m_tiles.find(mapID) == m_tiles.end())
{
m_tiles.emplace(mapID, std::set<uint32>{});
count++;
}
}
files.clear();
sprintf(vmaps_dir, "%s/vmaps", m_workdir);
getDirContents(files, vmaps_dir, "*.vmtree");
for (uint32 i = 0; i < files.size(); ++i)
{
mapID = uint32(atoi(files[i].substr(0, 3).c_str()));
if (m_tiles.find(mapID) == m_tiles.end())
{
m_tiles.emplace(mapID, std::set<uint32>{});
count++;
}
}
printf("found %u.\n", count);
count = 0;
printf("Discovering tiles... ");
for (TileList::iterator itr = m_tiles.begin(); itr != m_tiles.end(); ++itr)
{
std::set<uint32>& tiles = (*itr).second;
mapID = (*itr).first;
sprintf(filter, "%03u*.vmtile", mapID);
files.clear();
getDirContents(files, vmaps_dir, filter);
for (uint32 i = 0; i < files.size(); ++i)
{
tileX = uint32(atoi(files[i].substr(7, 2).c_str()));
tileY = uint32(atoi(files[i].substr(4, 2).c_str()));
tileID = StaticMapTree::packTileID(tileY, tileX);
tiles.insert(tileID);
count++;
}
sprintf(filter, "%03u*", mapID);
files.clear();
getDirContents(files, maps_dir, filter);
for (uint32 i = 0; i < files.size(); ++i)
{
tileY = uint32(atoi(files[i].substr(3, 2).c_str()));
tileX = uint32(atoi(files[i].substr(5, 2).c_str()));
tileID = StaticMapTree::packTileID(tileX, tileY);
if (tiles.insert(tileID).second)
count++;
}
}
printf("found %u.\n\n", count);
}
/**************************************************************************/
std::set<uint32>& MapBuilder::getTileList(uint32 mapID)
{
TileList::iterator itr = m_tiles.find(mapID);
if (itr != m_tiles.end())
return (*itr).second;
return m_tiles.emplace(mapID, std::set<uint32>{}).first->second;
}
/**************************************************************************/
bool MapBuilder::IsMapDone(uint32 mapId) const
{
auto itr = std::find(m_mapDone.begin(), m_mapDone.end(), mapId);
return itr != m_mapDone.end();
}
/**************************************************************************/
void MapBuilder::buildGameObject(std::string modelName, uint32 displayId)
{
std::string fullName(m_workdir);
fullName += "/vmaps/" + modelName;
printf("Building GameObject model %s\n", modelName.c_str());
WorldModel m;
MeshData meshData;
if (!m.readFile(fullName))
{
printf("* Unable to open file\n");
return;
}
// Load model data into navmesh
vector<GroupModel> groupModels;
m.getGroupModels(groupModels);
// all M2s need to have triangle indices reversed
bool isM2 = modelName.find(".m2") != modelName.npos || modelName.find(".M2") != modelName.npos;
for (vector<GroupModel>::iterator it = groupModels.begin(); it != groupModels.end(); ++it)
{
// transform data
vector<Vector3> tempVertices;
vector<MeshTriangle> tempTriangles;
WmoLiquid* liquid = nullptr;
(*it).getMeshData(tempVertices, tempTriangles, liquid);
int offset = meshData.solidVerts.size() / 3;
TerrainBuilder::copyVertices(tempVertices, meshData.solidVerts);
TerrainBuilder::copyIndices(tempTriangles, meshData.solidTris, offset, isM2);
}
// if there is no data, give up now
if (!meshData.solidVerts.size())
{
printf("* no solid vertices found\n");
return;
}
TerrainBuilder::cleanVertices(meshData.solidVerts, meshData.solidTris);
// gather all mesh data for final data check, and bounds calculation
G3D::Array<float> allVerts;
allVerts.append(meshData.solidVerts);
if (!allVerts.size())
return;
printf("* Model opened (%u vertices)\n", allVerts.size());
float* tVerts = meshData.solidVerts.getCArray();
int tVertCount = meshData.solidVerts.size() / 3;
int* tTris = meshData.solidTris.getCArray();
int tTriCount = meshData.solidTris.size() / 3;
// get bounds of current tile
rcConfig config;
memset(&config, 0, sizeof(rcConfig));
config = getDefaultConfig();
config.detailSampleDist = config.cs * 6.0f;
config.minRegionArea = config.minRegionArea / 2;
// this sets the dimensions of the heightfield - should maybe happen before border padding
rcCalcBounds(tVerts, tVertCount, config.bmin, config.bmax);
rcCalcGridSize(config.bmin, config.bmax, config.cs, &config.width, &config.height);
Tile tile;
buildCommonTile(modelName.data(), tile, config, tVerts, tVertCount, tTris, tTriCount, nullptr, 0, nullptr, 0, 0);
IntermediateValues iv;
iv.polyMesh = tile.pmesh;
iv.polyMeshDetail = tile.dmesh;
for (int i = 0; i < iv.polyMesh->npolys; ++i)
{
if (uint8 area = iv.polyMesh->areas[i] & NAV_AREA_ALL_MASK)
{
if (area >= NAV_AREA_MIN_VALUE)
iv.polyMesh->flags[i] = 1 << (NAV_AREA_MAX_VALUE - area);
else
iv.polyMesh->flags[i] = NAV_GROUND;
}
}
// Will be deleted by IntermediateValues
tile.pmesh = nullptr;
tile.dmesh = nullptr;
// setup mesh parameters
dtNavMeshCreateParams params;
memset(¶ms, 0, sizeof(params));
params.verts = iv.polyMesh->verts;
params.vertCount = iv.polyMesh->nverts;
params.polys = iv.polyMesh->polys;
params.polyAreas = iv.polyMesh->areas;
params.polyFlags = iv.polyMesh->flags;
params.polyCount = iv.polyMesh->npolys;
params.nvp = iv.polyMesh->nvp;
params.detailMeshes = iv.polyMeshDetail->meshes;
params.detailVerts = iv.polyMeshDetail->verts;
params.detailVertsCount = iv.polyMeshDetail->nverts;
params.detailTris = iv.polyMeshDetail->tris;
params.detailTriCount = iv.polyMeshDetail->ntris;
params.walkableHeight = BASE_UNIT_DIM * config.walkableHeight;
params.walkableRadius = BASE_UNIT_DIM * config.walkableRadius;
params.walkableClimb = BASE_UNIT_DIM * config.walkableClimb;
rcVcopy(params.bmin, iv.polyMesh->bmin);
rcVcopy(params.bmax, iv.polyMesh->bmax);
params.cs = config.cs;
params.ch = config.ch;
params.buildBvTree = true;
unsigned char* navData = nullptr;
int navDataSize = 0;
printf("* Building navmesh tile [%f %f %f to %f %f %f]\n",
params.bmin[0], params.bmin[1], params.bmin[2],
params.bmax[0], params.bmax[1], params.bmax[2]);
printf(" %u triangles (%u vertices)\n", params.polyCount, params.vertCount);
printf(" %u polygons (%u vertices)\n", params.detailTriCount, params.detailVertsCount);
if (params.nvp > DT_VERTS_PER_POLYGON)
{
printf("Invalid verts-per-polygon value! \n");
return;
}
if (params.vertCount >= 0xffff)
{
printf("Too many vertices! (0x%8x) \n", params.vertCount);
return;
}
if (!params.vertCount || !params.verts)
{
printf("No vertices to build tile! \n");
return;
}
if (!params.polyCount || !params.polys)
{
// we have flat tiles with no actual geometry - don't build those, its useless
// keep in mind that we do output those into debug info
// drop tiles with only exact count - some tiles may have geometry while having less tiles
printf("No polygons to build on tile! \n");
return;
}
if (!params.detailMeshes || !params.detailVerts || !params.detailTris)
{
printf("No detail mesh to build tile! \n");
return;
}
if (!dtCreateNavMeshData(¶ms, &navData, &navDataSize))
{
printf("Failed building navmesh tile! \n");
return;
}
// TODO: extract additional data that will enable RecastDemo viewing of transport mmaps
// navmesh creation params
//dtNavMeshParams navMeshParams;
//memset(&navMeshParams, 0, sizeof(dtNavMeshParams));
//navMeshParams.tileWidth = GRID_SIZE;
//navMeshParams.tileHeight = GRID_SIZE;
//rcVcopy(navMeshParams.orig, config.bmin);
//navMeshParams.maxTiles = 1;
//navMeshParams.maxPolys = 1 << DT_POLY_BITS;
//dtNavMesh* navMesh = nullptr;
//navMesh = dtAllocNavMesh();
//printf("[Map %03i] Creating navMesh... \r", displayId);
//if (!navMesh->init(&navMeshParams))
//{
// printf("[Map %03i] Failed creating navmesh! \n", displayId);
// return;
//}
//sprintf(fileName, "mmaps/%03u.mmap", displayId);
//FILE* file = fopen(fileName, "wb");
//if (!file)
//{
// dtFreeNavMesh(navMesh);
// char message[1024];
// sprintf(message, "[Map %03i] Failed to open %s for writing! \n", displayId, fileName);
// perror(message);
// return;
//}
//// now that we know navMesh params are valid, we can write them to file
//fwrite(&navMeshParams, sizeof(dtNavMeshParams), 1, file);
//fclose(file);
char fileName[255];
sprintf(fileName, "%s/mmaps/go%04u.mmtile", m_workdir, displayId);
FILE* file = fopen(fileName, "wb");
if (!file)
{
char message[1024];
sprintf(message, "Failed to open %s for writing!\n", fileName);
perror(message);
return;
}
printf("* Writing to file \"%s\" [size=%u]\n", fileName, navDataSize);
// write header
MmapTileHeader header;
header.usesLiquids = false;
header.size = uint32(navDataSize);
fwrite(&header, sizeof(MmapTileHeader), 1, file);
// write data
fwrite(navData, sizeof(unsigned char), navDataSize, file);
fclose(file);
if (m_debug)
{
iv.generateObjFile(modelName, meshData);
// Write navmesh data
std::string fname = "/meshes/" + modelName + ".nav";
fname = m_workdir + fname;
FILE* file = fopen(fname.c_str(), "wb");
if (file)
{
fwrite(&navDataSize, sizeof(uint32), 1, file);
fwrite(navData, sizeof(unsigned char), navDataSize, file);
fclose(file);
}
}
}
void MapBuilder::buildTransports()
{
// List of MO Transport gameobjects
buildGameObject("Transportship.wmo.vmo", 3015);
buildGameObject("Transport_Zeppelin.wmo.vmo", 3031);
buildGameObject("Transportship_Ne.wmo.vmo", 7087);
// List of Transport gameobjects
buildGameObject("Elevatorcar.m2.vmo", 360);
buildGameObject("Undeadelevator.m2.vmo", 455);
// buildGameObject("Undeadelevatordoor.m2.vmo", 462); // no model on which to path
buildGameObject("Ironforgeelevator.m2.vmo", 561);
// buildGameObject("Ironforgeelevatordoor.m2.vmo", 562); // no model on which to path
buildGameObject("Gnomeelevatorcar01.m2.vmo", 807);
buildGameObject("Gnomeelevatorcar02.m2.vmo", 808);
buildGameObject("Gnomeelevatorcar03.m2.vmo", 827); // missing vmap - reusing 03
buildGameObject("Gnomeelevatorcar03.m2.vmo", 852);
buildGameObject("Gnomehutelevator.m2.vmo", 1587);
buildGameObject("Burningsteppselevator.m2.vmo", 2454);
buildGameObject("Subwaycar.m2.vmo", 3831);
// TBC+
buildGameObject("Ancdrae_Elevatorpiece.m2.vmo", 7026);
buildGameObject("Mushroombase_Elevator.m2.vmo", 7028);
buildGameObject("Cf_Elevatorplatform.m2.vmo", 7043);
buildGameObject("Cf_Elevatorplatform_Small.m2.vmo", 7060);
buildGameObject("Factoryelevator.m2.vmo", 7077);
buildGameObject("Ancdrae_Elevatorpiece_Netherstorm.m2.vmo", 7163);
}
/**************************************************************************/
void MapBuilder::getGridBounds(uint32 mapID, uint32& minX, uint32& minY, uint32& maxX, uint32& maxY)
{
maxX = INT_MAX;
maxY = INT_MAX;
minX = INT_MIN;
minY = INT_MIN;
float bmin[3] = { 0, 0, 0 };
float bmax[3] = { 0, 0, 0 };
float lmin[3] = { 0, 0, 0 };
float lmax[3] = { 0, 0, 0 };
MeshData meshData;
// make sure we process maps which don't have tiles
// initialize the static tree, which loads WDT models
if (!m_terrainBuilder->loadVMap(mapID, 64, 64, meshData))
return;
// get the coord bounds of the model data
if (meshData.solidVerts.size() + meshData.liquidVerts.size() == 0)
return;
// get the coord bounds of the model data
if (meshData.solidVerts.size() && meshData.liquidVerts.size())
{
rcCalcBounds(meshData.solidVerts.getCArray(), meshData.solidVerts.size() / 3, bmin, bmax);
rcCalcBounds(meshData.liquidVerts.getCArray(), meshData.liquidVerts.size() / 3, lmin, lmax);
rcVmin(bmin, lmin);
rcVmax(bmax, lmax);
}
else if (meshData.solidVerts.size())
rcCalcBounds(meshData.solidVerts.getCArray(), meshData.solidVerts.size() / 3, bmin, bmax);
else
rcCalcBounds(meshData.liquidVerts.getCArray(), meshData.liquidVerts.size() / 3, lmin, lmax);
// convert coord bounds to grid bounds
maxX = 32 - bmin[0] / GRID_SIZE;
maxY = 32 - bmin[2] / GRID_SIZE;
minX = 32 - bmax[0] / GRID_SIZE;
minY = 32 - bmax[2] / GRID_SIZE;
}
/**************************************************************************/
void MapBuilder::buildSingleTile(uint32 mapID, uint32 tileX, uint32 tileY)
{
dtNavMesh* navMesh = NULL;
buildNavMesh(mapID, navMesh);
if (!navMesh)
{
printf("[Map %03i] Failed creating navmesh! \n", mapID);
return;
}
buildTile(mapID, tileX, tileY, navMesh, 1, 1);
dtFreeNavMesh(navMesh);
}
/**************************************************************************/
void MapBuilder::buildMap(uint32 mapID)
{
printf("Building map %03u: \n", mapID);
std::set<uint32>& tiles = getTileList(mapID);
// make sure we process maps which don't have tiles
if (!tiles.size())
{
// convert coord bounds to grid bounds
uint32 minX, minY, maxX, maxY;
getGridBounds(mapID, minX, minY, maxX, maxY);
// add all tiles within bounds to tile list.
for (uint32 i = minX; i <= maxX; ++i)
for (uint32 j = minY; j <= maxY; ++j)
tiles.insert(StaticMapTree::packTileID(i, j));
}
if (!tiles.size())
return;
// build navMesh
dtNavMesh* navMesh = nullptr;
buildNavMesh(mapID, navMesh);
if (!navMesh)
{
printf("[Map %03i] Failed creating navmesh! \n", mapID);
return;
}
// now start building mmtiles for each tile
printf("[Map %03i] We have %u tiles. \n", mapID, uint32(tiles.size()));
uint32 currentTile = 0;
for (std::set<uint32>::iterator it = tiles.begin(); it != tiles.end(); ++it)
{
currentTile++;
uint32 tileX, tileY;
// unpack tile coords
StaticMapTree::unpackTileID((*it), tileX, tileY);
if (shouldSkipTile(mapID, tileX, tileY))
continue;
// Make a copy of the original navMesh object to work on a separate
// thread since "the data should not be reused in other nav meshes"
// (see dtNavMesh::addTile description)
dtNavMesh* navMeshCopy = dtAllocNavMesh();
dtStatus dtResult = navMeshCopy->init(navMesh->getParams());
if (dtStatusFailed(dtResult))
{
printf("[Map %03i] Failed to copy navmesh! \n", mapID);
printf("%s\n", GetDTErrorReason(dtResult));
continue;
}
// passing by value
auto builder = [=]()
{
// build tile with copy version of the navmesh
buildTile(mapID, tileX, tileY, navMeshCopy, currentTile, uint32(tiles.size()));
// free this navmesh
dtFreeNavMesh(navMeshCopy);
};
m_taskQueue->PushWork(builder, mapID);
}
dtFreeNavMesh(navMesh);
}
/**************************************************************************/
void MapBuilder::buildTile(uint32 mapID, uint32 tileX, uint32 tileY, dtNavMesh* navMesh, uint32 curTile, uint32 tileCount)
{
printf("[Map %03i] Building tile [%02u,%02u] (%02u / %02u) \n", mapID, tileX, tileY, curTile, tileCount);
MeshData meshData;
// get heightmap data
m_terrainBuilder->loadMap(mapID, tileX, tileY, meshData);
// get model data
m_terrainBuilder->loadVMap(mapID, tileY, tileX, meshData);
// if there is no data, give up now
if (!meshData.solidVerts.size() && !meshData.liquidVerts.size())
return;
// remove unused vertices
TerrainBuilder::cleanVertices(meshData.solidVerts, meshData.solidTris);
TerrainBuilder::cleanVertices(meshData.liquidVerts, meshData.liquidTris);
// gather all mesh data for final data check, and bounds calculation
G3D::Array<float> allVerts;
allVerts.append(meshData.liquidVerts);
allVerts.append(meshData.solidVerts);
if (!allVerts.size())
return;
// get bounds of current tile
float bmin[3], bmax[3];
getTileBounds(tileX, tileY, allVerts.getCArray(), allVerts.size() / 3, bmin, bmax);
m_terrainBuilder->loadOffMeshConnections(mapID, tileX, tileY, meshData, m_offMeshFilePath);
// build navmesh tile
buildMoveMapTile(mapID, tileX, tileY, meshData, bmin, bmax, navMesh);
}
/**************************************************************************/
void MapBuilder::buildNavMesh(uint32 mapID, dtNavMesh*& navMesh)
{
std::set<uint32>& tiles = getTileList(mapID);
// old code for non-statically assigned bitmask sizes:
///*** calculate number of bits needed to store tiles & polys ***/
//int tileBits = dtIlog2(dtNextPow2(tiles.size()));
//if (tileBits < 1) tileBits = 1; // need at least one bit!
//int polyBits = sizeof(dtPolyRef)*8 - SALT_MIN_BITS - tileBits;
int polyBits = DT_POLY_BITS;
int maxTiles = tiles.size();
int maxPolysPerTile = 1 << polyBits;
/*** calculate bounds of map ***/
uint32 tileXMin = 64, tileYMin = 64, tileXMax = 0, tileYMax = 0, tileX, tileY;
for (std::set<uint32>::iterator it = tiles.begin(); it != tiles.end(); ++it)
{
StaticMapTree::unpackTileID((*it), tileX, tileY);
if (tileX > tileXMax)
tileXMax = tileX;
else if (tileX < tileXMin)
tileXMin = tileX;
if (tileY > tileYMax)
tileYMax = tileY;
else if (tileY < tileYMin)
tileYMin = tileY;
}
// use Max because '32 - tileX' is negative for values over 32
float bmin[3], bmax[3];
getTileBounds(tileXMax, tileYMax, NULL, 0, bmin, bmax);
/*** now create the navmesh ***/
// navmesh creation params
dtNavMeshParams navMeshParams;
memset(&navMeshParams, 0, sizeof(dtNavMeshParams));
navMeshParams.tileWidth = GRID_SIZE;
navMeshParams.tileHeight = GRID_SIZE;
rcVcopy(navMeshParams.orig, bmin);
navMeshParams.maxTiles = maxTiles;
navMeshParams.maxPolys = maxPolysPerTile;
navMesh = dtAllocNavMesh();
printf("[Map %03i] Creating navMesh... \r", mapID);
dtStatus dtResult = navMesh->init(&navMeshParams);
if (dtStatusFailed(dtResult))
{
printf("[Map %03i] Failed creating navmesh! \n", mapID);
printf("%s\n", GetDTErrorReason(dtResult));
return;
}
char fileName[1024];
sprintf(fileName, "%s/mmaps/%03u.mmap", m_workdir, mapID);
FILE* file = fopen(fileName, "wb");
if (!file)
{
dtFreeNavMesh(navMesh);
char message[1024];
sprintf(message, "[Map %03i] Failed to open %s for writing! \n", mapID, fileName);
perror(message);
return;
}
// now that we know navMesh params are valid, we can write them to file
fwrite(&navMeshParams, sizeof(dtNavMeshParams), 1, file);
fclose(file);
}
/**************************************************************************/
void MapBuilder::buildMoveMapTile(uint32 mapID, uint32 tileX, uint32 tileY,
MeshData& meshData, float bmin[3], float bmax[3],
dtNavMesh* navMesh)
{
// console output
char tileString[20];
sprintf(tileString, "[Map %03i] [%02i,%02i]: ", mapID, tileX, tileY);
printf("%s Building movemap tiles... \r", tileString);
IntermediateValues iv(m_workdir);
float* tVerts = meshData.solidVerts.getCArray();
int tVertCount = meshData.solidVerts.size() / 3;
int* tTris = meshData.solidTris.getCArray();
int tTriCount = meshData.solidTris.size() / 3;
float* lVerts = meshData.liquidVerts.getCArray();
int lVertCount = meshData.liquidVerts.size() / 3;
int* lTris = meshData.liquidTris.getCArray();
int lTriCount = meshData.liquidTris.size() / 3;
uint8* lTriFlags = meshData.liquidType.getCArray();
rcConfig config;
memset(&config, 0, sizeof(rcConfig));
config = getTileConfig(mapID, tileX, tileY);
rcVcopy(config.bmin, bmin);
rcVcopy(config.bmax, bmax);
// this sets the dimensions of the heightfield - should maybe happen before border padding
rcCalcGridSize(config.bmin, config.bmax, config.cs, &config.width, &config.height);
// allocate subregions : tiles
Tile* tiles = new Tile[TILES_PER_MAP * TILES_PER_MAP];
// Initialize per tile config.
rcConfig tileCfg;
memcpy(&tileCfg, &config, sizeof(rcConfig));
tileCfg.width = config.tileSize + config.borderSize * 2;
tileCfg.height = config.tileSize + config.borderSize * 2;
// build all tiles
for (int y = 0; y < TILES_PER_MAP; ++y)
{
for (int x = 0; x < TILES_PER_MAP; ++x)
{
Tile& tile = tiles[x + y * TILES_PER_MAP];
// Calculate the per tile bounding box.
tileCfg.bmin[0] = config.bmin[0] + x * float(config.tileSize * config.cs);
tileCfg.bmin[2] = config.bmin[2] + y * float(config.tileSize * config.cs);
tileCfg.bmax[0] = config.bmin[0] + (x + 1) * float(config.tileSize * config.cs);
tileCfg.bmax[2] = config.bmin[2] + (y + 1) * float(config.tileSize * config.cs);
tileCfg.bmin[0] -= tileCfg.borderSize * tileCfg.cs;
tileCfg.bmin[2] -= tileCfg.borderSize * tileCfg.cs;
tileCfg.bmax[0] += tileCfg.borderSize * tileCfg.cs;
tileCfg.bmax[2] += tileCfg.borderSize * tileCfg.cs;
buildCommonTile(tileString, tile, tileCfg, tVerts, tVertCount, tTris, tTriCount, lVerts, lVertCount, lTris, lTriCount, lTriFlags);
}
}
// merge per tile poly and detail meshes
rcPolyMesh** pmmerge = new rcPolyMesh*[TILES_PER_MAP * TILES_PER_MAP];
rcPolyMeshDetail** dmmerge = new rcPolyMeshDetail*[TILES_PER_MAP * TILES_PER_MAP];
int nmerge = 0;
for (int y = 0; y < TILES_PER_MAP; ++y)
{
for (int x = 0; x < TILES_PER_MAP; ++x)
{
Tile& tile = tiles[x + y * TILES_PER_MAP];
if (tile.pmesh)
{
pmmerge[nmerge] = tile.pmesh;
dmmerge[nmerge] = tile.dmesh;
nmerge++;
}
}
}
iv.polyMesh = rcAllocPolyMesh();
if (!iv.polyMesh)
{
printf("%s alloc iv.polyMesh FIALED! \r", tileString);
delete[] pmmerge;
delete[] dmmerge;
delete[] tiles;
return;
}
rcMergePolyMeshes(m_rcContext, pmmerge, nmerge, *iv.polyMesh);
iv.polyMeshDetail = rcAllocPolyMeshDetail();
if (!iv.polyMeshDetail)
{
printf("%s alloc m_dmesh FIALED! \r", tileString);
delete[] pmmerge;
delete[] dmmerge;
delete[] tiles;
return;
}
rcMergePolyMeshDetails(m_rcContext, dmmerge, nmerge, *iv.polyMeshDetail);
// free things up
delete [] pmmerge;
delete [] dmmerge;
delete [] tiles;
// set polygons as walkable
// TODO: special flags for DYNAMIC polygons, ie surfaces that can be turned on and off
for (int i = 0; i < iv.polyMesh->npolys; ++i)
{
if (uint8 area = iv.polyMesh->areas[i] & NAV_AREA_ALL_MASK)
{
if (area >= NAV_AREA_MIN_VALUE)
iv.polyMesh->flags[i] = 1 << (NAV_AREA_MAX_VALUE - area);
else
iv.polyMesh->flags[i] = NAV_GROUND; // TODO: these will be dynamic in future
}
}
// setup mesh parameters
dtNavMeshCreateParams params;
memset(¶ms, 0, sizeof(params));
params.verts = iv.polyMesh->verts;
params.vertCount = iv.polyMesh->nverts;
params.polys = iv.polyMesh->polys;
params.polyAreas = iv.polyMesh->areas;
params.polyFlags = iv.polyMesh->flags;
params.polyCount = iv.polyMesh->npolys;
params.nvp = iv.polyMesh->nvp;
params.detailMeshes = iv.polyMeshDetail->meshes;
params.detailVerts = iv.polyMeshDetail->verts;
params.detailVertsCount = iv.polyMeshDetail->nverts;
params.detailTris = iv.polyMeshDetail->tris;
params.detailTriCount = iv.polyMeshDetail->ntris;
params.offMeshConVerts = meshData.offMeshConnections.getCArray();
params.offMeshConCount = meshData.offMeshConnections.size() / 6;
params.offMeshConRad = meshData.offMeshConnectionRads.getCArray();
params.offMeshConDir = meshData.offMeshConnectionDirs.getCArray();
params.offMeshConAreas = meshData.offMeshConnectionsAreas.getCArray();
params.offMeshConFlags = meshData.offMeshConnectionsFlags.getCArray();
params.walkableHeight = BASE_UNIT_DIM * config.walkableHeight;
params.walkableRadius = BASE_UNIT_DIM * config.walkableRadius;
params.walkableClimb = BASE_UNIT_DIM * config.walkableClimb;
params.tileX = (((bmin[0] + bmax[0]) / 2) - navMesh->getParams()->orig[0]) / GRID_SIZE;
params.tileY = (((bmin[2] + bmax[2]) / 2) - navMesh->getParams()->orig[2]) / GRID_SIZE;
rcVcopy(params.bmin, bmin);
rcVcopy(params.bmax, bmax);
params.cs = config.cs;
params.ch = config.ch;
params.tileLayer = 0;
params.buildBvTree = true;
// will hold final navmesh
unsigned char* navData = NULL;
int navDataSize = 0;
do
{
// these values are checked within dtCreateNavMeshData - handle them here
// so we have a clear error messages
if (params.nvp > DT_VERTS_PER_POLYGON)
{
printf("%s Invalid verts-per-polygon value! \n", tileString);
continue;
}
if (params.vertCount >= 0xffff)
{
printf("%s Too many vertices! \n", tileString);
continue;
}
if (!params.vertCount || !params.verts)
{
// occurs mostly when adjacent tiles have models
// loaded but those models don't span into this tile
// message is an annoyance
//printf("%sNo vertices to build tile! \n", tileString);
continue;
}
if (!params.polyCount || !params.polys ||
TILES_PER_MAP * TILES_PER_MAP == params.polyCount)
{
// we have flat tiles with no actual geometry - don't build those, its useless
// keep in mind that we do output those into debug info
// drop tiles with only exact count - some tiles may have geometry while having less tiles
printf("%s No polygons to build on tile! \n", tileString);
continue;
}
if (!params.detailMeshes || !params.detailVerts || !params.detailTris)
{
printf("%s No detail mesh to build tile! \n", tileString);
continue;
}
printf("%s Building navmesh tile... \r", tileString);
if (!dtCreateNavMeshData(¶ms, &navData, &navDataSize))
{
printf("%s Failed building navmesh tile! \n", tileString);
continue;
}
dtTileRef tileRef = 0;
printf("%s Adding tile to navmesh... \r", tileString);
// DT_TILE_FREE_DATA tells detour to unallocate memory when the tile
// is removed via removeTile()
dtStatus dtResult = navMesh->addTile(navData, navDataSize, DT_TILE_FREE_DATA, 0, &tileRef);
if (!tileRef || dtStatusFailed(dtResult))
{
printf("%s Failed adding tile to navmesh! \n", tileString);
printf("%s\n", GetDTErrorReason(dtResult));
continue;
}
// file output
char fileName[1024];
sprintf(fileName, "%s/mmaps/%03u%02i%02i.mmtile", m_workdir, mapID, tileY, tileX);
FILE* file = fopen(fileName, "wb");
if (!file)
{
char message[1024];
sprintf(message, "[Map %03i] Failed to open %s for writing! \n", mapID, fileName);
perror(message);
navMesh->removeTile(tileRef, NULL, NULL);
continue;
}
printf("%s Writing to file... \r", tileString);
// write header
MmapTileHeader header;
header.size = uint32(navDataSize);
header.usesLiquids = m_terrainBuilder->usesLiquids() ? 1 : 0;
fwrite(&header, sizeof(MmapTileHeader), 1, file);
// write data
fwrite(navData, sizeof(unsigned char), navDataSize, file);
fclose(file);