2323#include " VMapDefinitions.h"
2424#include " WorldModel.h"
2525#include < G3D/Vector3.h>
26- #include < string>
27-
28- using G3D::Vector3;
2926
3027namespace VMAP
3128{
@@ -34,6 +31,12 @@ namespace VMAP
3431 public:
3532 explicit ManagedModel (VMapManager2& mgr, std::string const & name) : _mgr(mgr), _name(name) { }
3633
34+ ManagedModel (ManagedModel const &) = delete ;
35+ ManagedModel (ManagedModel&&) = delete ;
36+
37+ ManagedModel& operator =(ManagedModel const &) = delete ;
38+ ManagedModel& operator =(ManagedModel&&) = delete ;
39+
3740 ~ManagedModel ()
3841 {
3942 _mgr.releaseModelInstance (_name);
@@ -52,18 +55,16 @@ namespace VMAP
5255 return memcmp (dest, compare, len) == 0 ;
5356 }
5457
55- VMapManager2::VMapManager2 ()
58+ VMapManager2::VMapManager2 () :
59+ iEnableLineOfSightCalc (true ),
60+ iEnableHeightCalc (true ),
61+ thread_safe_environment (true ),
62+ GetLiquidFlagsPtr ([](uint32 /* liquidTypeId*/ ) { return 0u ; }),
63+ IsVMAPDisabledForPtr ([](uint32 /* mapId*/ , uint8 /* disableFlags*/ ) { return false ; })
5664 {
57- GetLiquidFlagsPtr = &GetLiquidFlagsDummy;
58- IsVMAPDisabledForPtr = &IsVMAPDisabledForDummy;
59- thread_safe_environment = true ;
6065 }
6166
62- VMapManager2::~VMapManager2 ()
63- {
64- for (std::pair<uint32 const , StaticMapTree*>& iInstanceMapTree : iInstanceMapTrees)
65- delete iInstanceMapTree.second ;
66- }
67+ VMapManager2::~VMapManager2 () = default ;
6768
6869 InstanceTreeMap::const_iterator VMapManager2::GetMapTree (uint32 mapId) const
6970 {
@@ -78,11 +79,11 @@ namespace VMAP
7879 void VMapManager2::InitializeThreadUnsafe (std::unordered_map<uint32, std::vector<uint32>> const & mapData)
7980 {
8081 // the caller must pass the list of all mapIds that will be used in the VMapManager2 lifetime
81- for (std::pair<uint32 const , std::vector<uint32>> const & mapId : mapData)
82+ for (auto const & [ mapId, childMapIds] : mapData)
8283 {
83- iInstanceMapTrees. insert ( InstanceTreeMap::value_type ( mapId. first , nullptr )) ;
84- for (uint32 childMapId : mapId. second )
85- iParentMapData[childMapId] = mapId. first ;
84+ iInstanceMapTrees[ mapId] = nullptr ;
85+ for (uint32 childMapId : childMapIds )
86+ iParentMapData[childMapId] = mapId;
8687 }
8788
8889 thread_safe_environment = false ;
@@ -95,24 +96,28 @@ namespace VMAP
9596 iParentMapData[mapId] = parentMapId;
9697 }
9798
98- Vector3 VMapManager2:: convertPositionToInternalRep (float x, float y, float z) const
99+ inline static G3D::Vector3 convertPositionToInternalRep (float x, float y, float z)
99100 {
100- Vector3 pos;
101- const float mid = 0 .5f * 64 .0f * 533 .33333333f ;
101+ G3D:: Vector3 pos;
102+ constexpr float mid = 0 .5f * 64 .0f * 533 .33333333f ;
102103 pos.x = mid - x;
103104 pos.y = mid - y;
104105 pos.z = z;
105106
106107 return pos;
107108 }
108109
109- // move to MapTree too?
110- std::string VMapManager2::getMapFileName (unsigned int mapId)
110+ std::string VMapManager2::getMapFileName (uint32 mapId)
111111 {
112112 return Trinity::StringFormat (" {:04}/{:04}.vmtree" , mapId, mapId);
113113 }
114114
115- LoadResult VMapManager2::loadMap (char const * basePath, unsigned int mapId, int x, int y)
115+ std::string VMapManager2::getTileFileName (uint32 mapID, uint32 tileX, uint32 tileY, std::string_view extension)
116+ {
117+ return Trinity::StringFormat (" {:04}/{:04}_{:02}_{:02}.{}" , mapID, mapID, tileY, tileX, extension);
118+ }
119+
120+ LoadResult VMapManager2::loadMap (std::string const & basePath, uint32 mapId, uint32 x, uint32 y)
116121 {
117122 if (!isMapLoadingEnabled ())
118123 return LoadResult::DisabledInConfig;
@@ -130,57 +135,49 @@ namespace VMAP
130135 if (!instanceTree->second )
131136 {
132137 std::string mapFileName = getMapFileName (mapId);
133- StaticMapTree* newTree = new StaticMapTree (mapId, basePath);
138+ std::unique_ptr< StaticMapTree> newTree = std::make_unique< StaticMapTree> (mapId, basePath);
134139 LoadResult treeInitResult = newTree->InitMap (mapFileName);
135140 if (treeInitResult != LoadResult::Success)
136- {
137- delete newTree;
138141 return treeInitResult;
139- }
140- instanceTree->second = newTree;
142+
143+ instanceTree->second = std::move ( newTree) ;
141144 }
142145
143146 return instanceTree->second ->LoadMapTile (x, y, this );
144147 }
145148
146- void VMapManager2::unloadMap (unsigned int mapId, int x, int y)
149+ void VMapManager2::unloadMap (uint32 mapId, uint32 x, uint32 y)
147150 {
148151 auto instanceTree = iInstanceMapTrees.find (mapId);
149152 if (instanceTree != iInstanceMapTrees.end () && instanceTree->second )
150153 {
151154 instanceTree->second ->UnloadMapTile (x, y, this );
152155 if (instanceTree->second ->numLoadedTiles () == 0 )
153- {
154- delete instanceTree->second ;
155156 instanceTree->second = nullptr ;
156- }
157157 }
158158 }
159159
160- void VMapManager2::unloadMap (unsigned int mapId)
160+ void VMapManager2::unloadMap (uint32 mapId)
161161 {
162162 auto instanceTree = iInstanceMapTrees.find (mapId);
163163 if (instanceTree != iInstanceMapTrees.end () && instanceTree->second )
164164 {
165165 instanceTree->second ->UnloadMap ();
166166 if (instanceTree->second ->numLoadedTiles () == 0 )
167- {
168- delete instanceTree->second ;
169167 instanceTree->second = nullptr ;
170- }
171168 }
172169 }
173170
174- bool VMapManager2::isInLineOfSight (unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2, ModelIgnoreFlags ignoreFlags)
171+ bool VMapManager2::isInLineOfSight (uint32 mapId, float x1, float y1, float z1, float x2, float y2, float z2, ModelIgnoreFlags ignoreFlags)
175172 {
176173 if (!isLineOfSightCalcEnabled () || IsVMAPDisabledForPtr (mapId, VMAP_DISABLE_LOS))
177174 return true ;
178175
179176 auto instanceTree = GetMapTree (mapId);
180177 if (instanceTree != iInstanceMapTrees.end ())
181178 {
182- Vector3 pos1 = convertPositionToInternalRep (x1, y1, z1);
183- Vector3 pos2 = convertPositionToInternalRep (x2, y2, z2);
179+ G3D:: Vector3 pos1 = convertPositionToInternalRep (x1, y1, z1);
180+ G3D:: Vector3 pos2 = convertPositionToInternalRep (x2, y2, z2);
184181 if (pos1 != pos2)
185182 return instanceTree->second ->isInLineOfSight (pos1, pos2, ignoreFlags);
186183 }
@@ -192,16 +189,16 @@ namespace VMAP
192189 get the hit position and return true if we hit something
193190 otherwise the result pos will be the dest pos
194191 */
195- bool VMapManager2::getObjectHitPos (unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2, float & rx, float &ry, float & rz, float modifyDist)
192+ bool VMapManager2::getObjectHitPos (uint32 mapId, float x1, float y1, float z1, float x2, float y2, float z2, float & rx, float &ry, float & rz, float modifyDist)
196193 {
197194 if (isLineOfSightCalcEnabled () && !IsVMAPDisabledForPtr (mapId, VMAP_DISABLE_LOS))
198195 {
199196 auto instanceTree = GetMapTree (mapId);
200197 if (instanceTree != iInstanceMapTrees.end ())
201198 {
202- Vector3 pos1 = convertPositionToInternalRep (x1, y1, z1);
203- Vector3 pos2 = convertPositionToInternalRep (x2, y2, z2);
204- Vector3 resultPos;
199+ G3D:: Vector3 pos1 = convertPositionToInternalRep (x1, y1, z1);
200+ G3D:: Vector3 pos2 = convertPositionToInternalRep (x2, y2, z2);
201+ G3D:: Vector3 resultPos;
205202 bool result = instanceTree->second ->getObjectHitPos (pos1, pos2, resultPos, modifyDist);
206203 resultPos = convertPositionToInternalRep (resultPos.x , resultPos.y , resultPos.z );
207204 rx = resultPos.x ;
@@ -222,14 +219,14 @@ namespace VMAP
222219 get height or INVALID_HEIGHT if no height available
223220 */
224221
225- float VMapManager2::getHeight (unsigned int mapId, float x, float y, float z, float maxSearchDist)
222+ float VMapManager2::getHeight (uint32 mapId, float x, float y, float z, float maxSearchDist)
226223 {
227224 if (isHeightCalcEnabled () && !IsVMAPDisabledForPtr (mapId, VMAP_DISABLE_HEIGHT))
228225 {
229226 auto instanceTree = GetMapTree (mapId);
230227 if (instanceTree != iInstanceMapTrees.end ())
231228 {
232- Vector3 pos = convertPositionToInternalRep (x, y, z);
229+ G3D:: Vector3 pos = convertPositionToInternalRep (x, y, z);
233230 float height = instanceTree->second ->getHeight (pos, maxSearchDist);
234231 if (!(height < G3D::finf ()))
235232 return height = VMAP_INVALID_HEIGHT_VALUE; // No height
@@ -241,13 +238,13 @@ namespace VMAP
241238 return VMAP_INVALID_HEIGHT_VALUE;
242239 }
243240
244- bool VMapManager2::getAreaAndLiquidData (unsigned int mapId, float x, float y, float z, Optional<uint8> reqLiquidType, AreaAndLiquidData& data) const
241+ bool VMapManager2::getAreaAndLiquidData (uint32 mapId, float x, float y, float z, Optional<uint8> reqLiquidType, AreaAndLiquidData& data) const
245242 {
246243 InstanceTreeMap::const_iterator instanceTree = GetMapTree (mapId);
247244 if (instanceTree != iInstanceMapTrees.end ())
248245 {
249246 LocationInfo info;
250- Vector3 pos = convertPositionToInternalRep (x, y, z);
247+ G3D:: Vector3 pos = convertPositionToInternalRep (x, y, z);
251248 if (instanceTree->second ->GetLocationInfo (pos, info))
252249 {
253250 data.floorZ = info.ground_Z ;
@@ -275,7 +272,7 @@ namespace VMAP
275272 std::shared_ptr<ManagedModel> worldmodel; // this is intentionally declared before lock so that it is destroyed after it to prevent deadlocks in releaseModelInstance
276273
277274 // ! Critical section, thread safe access to iLoadedModelFiles
278- std::lock_guard<std::mutex> lock (LoadedModelFilesLock);
275+ std::lock_guard lock (LoadedModelFilesLock);
279276
280277 auto & [key, model] = *iLoadedModelFiles.try_emplace (filename).first ;
281278 worldmodel = model.lock ();
@@ -298,7 +295,7 @@ namespace VMAP
298295 void VMapManager2::releaseModelInstance (std::string const & filename)
299296 {
300297 // ! Critical section, thread safe access to iLoadedModelFiles
301- std::lock_guard<std::mutex> lock (LoadedModelFilesLock);
298+ std::lock_guard lock (LoadedModelFilesLock);
302299
303300 TC_LOG_DEBUG (" maps" , " VMapManager2: unloading file '{}'" , filename);
304301
@@ -307,14 +304,18 @@ namespace VMAP
307304 TC_LOG_ERROR (" misc" , " VMapManager2: trying to unload non-loaded file '{}'" , filename);
308305 }
309306
310- LoadResult VMapManager2::existsMap (char const * basePath, unsigned int mapId, int x, int y)
307+ LoadResult VMapManager2::existsMap (std::string const & basePath, uint32 mapId, uint32 x, uint32 y)
311308 {
312- return StaticMapTree::CanLoadMap (std::string ( basePath) , mapId, x, y, this );
309+ return StaticMapTree::CanLoadMap (basePath, mapId, x, y, this );
313310 }
314311
315- void VMapManager2::getInstanceMapTree (InstanceTreeMap &instanceMapTree)
312+ std::span<ModelInstance const > VMapManager2::getModelsOnMap (uint32 mapId) const
316313 {
317- instanceMapTree = iInstanceMapTrees;
314+ InstanceTreeMap::const_iterator mapTree = GetMapTree (mapId);
315+ if (mapTree != iInstanceMapTrees.end ())
316+ return mapTree->second ->getModelInstances ();
317+
318+ return {};
318319 }
319320
320321 int32 VMapManager2::getParentMapId (uint32 mapId) const
0 commit comments