aboutsummaryrefslogtreecommitdiff
path: root/src/common/Collision
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2021-04-24 23:28:42 +0200
committerShauren <shauren.trinity@gmail.com>2021-04-25 00:11:36 +0200
commitbb8f22ed2013f8cb6b9d61c738f2ebd96b83722f (patch)
tree37e089d220d7c5967266fc71b30ef36de81e67c3 /src/common/Collision
parent4e00cb7c157a0bd25a0b48a80a1f71a1817010bf (diff)
Core/Vmaps: Reduce memory used by vmaps (and their size, slightly)
Diffstat (limited to 'src/common/Collision')
-rw-r--r--src/common/Collision/Management/VMapManager2.cpp32
-rw-r--r--src/common/Collision/Management/VMapManager2.h16
-rw-r--r--src/common/Collision/Maps/MapTree.cpp16
-rw-r--r--src/common/Collision/Models/GameObjectModel.cpp3
-rw-r--r--src/common/Collision/Models/GameObjectModel.h2
-rw-r--r--src/common/Collision/Models/ModelInstance.cpp12
-rw-r--r--src/common/Collision/Models/ModelInstance.h26
-rw-r--r--src/common/Collision/Models/WorldModel.h3
8 files changed, 59 insertions, 51 deletions
diff --git a/src/common/Collision/Management/VMapManager2.cpp b/src/common/Collision/Management/VMapManager2.cpp
index ed4e5535250..3a45a12e556 100644
--- a/src/common/Collision/Management/VMapManager2.cpp
+++ b/src/common/Collision/Management/VMapManager2.cpp
@@ -32,6 +32,18 @@ using G3D::Vector3;
namespace VMAP
{
+ class ManagedModel
+ {
+ public:
+ ManagedModel() : iRefCount(0) { }
+ WorldModel* getModel() { return &iModel; }
+ void incRefCount() { ++iRefCount; }
+ int decRefCount() { return --iRefCount; }
+ protected:
+ WorldModel iModel;
+ int iRefCount;
+ };
+
bool readChunk(FILE* rf, char* dest, const char* compare, uint32 len)
{
if (fread(dest, sizeof(char), len, rf) != len) return false;
@@ -51,7 +63,7 @@ namespace VMAP
delete i->second;
for (auto i = iLoadedModelFiles.begin(); i != iLoadedModelFiles.end(); ++i)
- delete i->second.getModel();
+ delete i->second;
}
InstanceTreeMap::const_iterator VMapManager2::GetMapTree(uint32 mapId) const
@@ -358,8 +370,8 @@ namespace VMAP
auto model = iLoadedModelFiles.find(filename);
if (model == iLoadedModelFiles.end())
{
- WorldModel* worldmodel = new WorldModel();
- if (!worldmodel->readFile(basepath + filename + ".vmo"))
+ ManagedModel* worldmodel = new ManagedModel();
+ if (!worldmodel->getModel()->readFile(basepath + filename + ".vmo"))
{
TC_LOG_ERROR("misc", "VMapManager2: could not load '%s%s.vmo'", basepath.c_str(), filename.c_str());
delete worldmodel;
@@ -367,13 +379,13 @@ namespace VMAP
}
TC_LOG_DEBUG("maps", "VMapManager2: loading file '%s%s'", basepath.c_str(), filename.c_str());
- worldmodel->Flags = flags;
+ worldmodel->getModel()->SetName(filename);
+ worldmodel->getModel()->Flags = flags;
- model = iLoadedModelFiles.insert(std::pair<std::string, ManagedModel>(filename, ManagedModel())).first;
- model->second.setModel(worldmodel);
+ model = iLoadedModelFiles.insert(std::pair<std::string, ManagedModel*>(filename, worldmodel)).first;
}
- model->second.incRefCount();
- return model->second.getModel();
+ model->second->incRefCount();
+ return model->second->getModel();
}
void VMapManager2::releaseModelInstance(const std::string &filename)
@@ -387,10 +399,10 @@ namespace VMAP
TC_LOG_ERROR("misc", "VMapManager2: trying to unload non-loaded file '%s'", filename.c_str());
return;
}
- if (model->second.decRefCount() == 0)
+ if (model->second->decRefCount() == 0)
{
TC_LOG_DEBUG("maps", "VMapManager2: unloading file '%s'", filename.c_str());
- delete model->second.getModel();
+ delete model->second;
iLoadedModelFiles.erase(model);
}
}
diff --git a/src/common/Collision/Management/VMapManager2.h b/src/common/Collision/Management/VMapManager2.h
index 024c1619a2f..bd039877533 100644
--- a/src/common/Collision/Management/VMapManager2.h
+++ b/src/common/Collision/Management/VMapManager2.h
@@ -47,24 +47,12 @@ namespace G3D
namespace VMAP
{
+ class ManagedModel;
class StaticMapTree;
class WorldModel;
- class TC_COMMON_API ManagedModel
- {
- public:
- ManagedModel() : iModel(nullptr), iRefCount(0) { }
- void setModel(WorldModel* model) { iModel = model; }
- WorldModel* getModel() { return iModel; }
- void incRefCount() { ++iRefCount; }
- int decRefCount() { return --iRefCount; }
- protected:
- WorldModel* iModel;
- int iRefCount;
- };
-
typedef std::unordered_map<uint32, StaticMapTree*> InstanceTreeMap;
- typedef std::unordered_map<std::string, ManagedModel> ModelFileMap;
+ typedef std::unordered_map<std::string, ManagedModel*> ModelFileMap;
enum DisableTypes
{
diff --git a/src/common/Collision/Maps/MapTree.cpp b/src/common/Collision/Maps/MapTree.cpp
index 7249a0cf1a2..d72d555caab 100644
--- a/src/common/Collision/Maps/MapTree.cpp
+++ b/src/common/Collision/Maps/MapTree.cpp
@@ -16,17 +16,17 @@
*/
#include "MapTree.h"
-#include "ModelInstance.h"
-#include "VMapManager2.h"
-#include "VMapDefinitions.h"
-#include "Log.h"
#include "Errors.h"
+#include "Log.h"
#include "Metric.h"
-
-#include <string>
-#include <sstream>
+#include "ModelInstance.h"
+#include "VMapDefinitions.h"
+#include "VMapManager2.h"
+#include "WorldModel.h"
#include <iomanip>
#include <limits>
+#include <sstream>
+#include <string>
using G3D::Vector3;
@@ -355,7 +355,7 @@ namespace VMAP
{
iTreeValues[i->first].setUnloaded();
for (uint32 refCount = 0; refCount < i->second; ++refCount)
- vm->releaseModelInstance(iTreeValues[i->first].name);
+ vm->releaseModelInstance(iTreeValues[i->first].getWorldModel()->GetName());
}
iLoadedSpawns.clear();
iLoadedTiles.clear();
diff --git a/src/common/Collision/Models/GameObjectModel.cpp b/src/common/Collision/Models/GameObjectModel.cpp
index 148ecff9066..65a530e7c0e 100644
--- a/src/common/Collision/Models/GameObjectModel.cpp
+++ b/src/common/Collision/Models/GameObjectModel.cpp
@@ -98,7 +98,7 @@ void LoadGameObjectModelList(std::string const& dataPath)
GameObjectModel::~GameObjectModel()
{
if (iModel)
- ((VMAP::VMapManager2*)VMAP::VMapFactory::createOrGetVMapManager())->releaseModelInstance(name);
+ ((VMAP::VMapManager2*)VMAP::VMapFactory::createOrGetVMapManager())->releaseModelInstance(iModel->GetName());
}
bool GameObjectModel::initialize(std::unique_ptr<GameObjectModelOwnerBase> modelOwner, std::string const& dataPath)
@@ -120,7 +120,6 @@ bool GameObjectModel::initialize(std::unique_ptr<GameObjectModelOwnerBase> model
if (!iModel)
return false;
- name = it->second.name;
iPos = modelOwner->GetPosition();
iScale = modelOwner->GetScale();
iInvScale = 1.f / iScale;
diff --git a/src/common/Collision/Models/GameObjectModel.h b/src/common/Collision/Models/GameObjectModel.h
index 38406b12701..c10a8bfdea2 100644
--- a/src/common/Collision/Models/GameObjectModel.h
+++ b/src/common/Collision/Models/GameObjectModel.h
@@ -57,8 +57,6 @@ class TC_COMMON_API GameObjectModel /*, public Intersectable*/
{
GameObjectModel() : _collisionEnabled(false), iInvScale(0), iScale(0), iModel(nullptr), isWmo(false) { }
public:
- std::string name;
-
const G3D::AABox& getBounds() const { return iBound; }
~GameObjectModel();
diff --git a/src/common/Collision/Models/ModelInstance.cpp b/src/common/Collision/Models/ModelInstance.cpp
index 385702a25ad..28a0bd51d25 100644
--- a/src/common/Collision/Models/ModelInstance.cpp
+++ b/src/common/Collision/Models/ModelInstance.cpp
@@ -24,9 +24,9 @@ using G3D::Ray;
namespace VMAP
{
- ModelInstance::ModelInstance(ModelSpawn const& spawn, WorldModel* model) : ModelSpawn(spawn), iModel(model)
+ ModelInstance::ModelInstance(ModelSpawn const& spawn, WorldModel* model) : ModelMinimalData(spawn), iModel(model)
{
- iInvRot = G3D::Matrix3::fromEulerAnglesZYX(G3D::pif() * iRot.y / 180.f, G3D::pif() * iRot.x / 180.f, G3D::pif() * iRot.z / 180.f).inverse();
+ iInvRot = G3D::Matrix3::fromEulerAnglesZYX(G3D::pif() * spawn.iRot.y / 180.f, G3D::pif() * spawn.iRot.x / 180.f, G3D::pif() * spawn.iRot.z / 180.f).inverse();
iInvScale = 1.f / iScale;
}
@@ -152,7 +152,7 @@ namespace VMAP
bool ModelSpawn::readFromFile(FILE* rf, ModelSpawn& spawn)
{
uint32 check = 0, nameLen;
- check += fread(&spawn.flags, sizeof(uint32), 1, rf);
+ check += fread(&spawn.flags, sizeof(uint8), 1, rf);
// EoF?
if (!check)
{
@@ -160,7 +160,7 @@ namespace VMAP
std::cout << "Error reading ModelSpawn!\n";
return false;
}
- check += fread(&spawn.adtId, sizeof(uint16), 1, rf);
+ check += fread(&spawn.adtId, sizeof(uint8), 1, rf);
check += fread(&spawn.ID, sizeof(uint32), 1, rf);
check += fread(&spawn.iPos, sizeof(float), 3, rf);
check += fread(&spawn.iRot, sizeof(float), 3, rf);
@@ -198,8 +198,8 @@ namespace VMAP
bool ModelSpawn::writeToFile(FILE* wf, ModelSpawn const& spawn)
{
uint32 check = 0;
- check += fwrite(&spawn.flags, sizeof(uint32), 1, wf);
- check += fwrite(&spawn.adtId, sizeof(uint16), 1, wf);
+ check += fwrite(&spawn.flags, sizeof(uint8), 1, wf);
+ check += fwrite(&spawn.adtId, sizeof(uint8), 1, wf);
check += fwrite(&spawn.ID, sizeof(uint32), 1, wf);
check += fwrite(&spawn.iPos, sizeof(float), 3, wf);
check += fwrite(&spawn.iRot, sizeof(float), 3, wf);
diff --git a/src/common/Collision/Models/ModelInstance.h b/src/common/Collision/Models/ModelInstance.h
index b342a14fdc6..bba48669d00 100644
--- a/src/common/Collision/Models/ModelInstance.h
+++ b/src/common/Collision/Models/ModelInstance.h
@@ -39,28 +39,35 @@ namespace VMAP
MOD_PARENT_SPAWN = 1 << 2
};
- class TC_COMMON_API ModelSpawn
+ struct ModelMinimalData
{
- public:
- //mapID, tileX, tileY, Flags, ID, Pos, Rot, Scale, Bound_lo, Bound_hi, name
- uint32 flags;
- uint16 adtId;
+ //Flags, ID, Pos, Rot, Scale, Bound_lo, Bound_hi
+ uint8 flags;
+ uint8 adtId;
uint32 ID;
G3D::Vector3 iPos;
G3D::Vector3 iRot;
float iScale;
G3D::AABox iBound;
+#ifdef VMAP_DEBUG
std::string name;
- bool operator==(ModelSpawn const& other) const { return ID == other.ID; }
- //uint32 hashCode() const { return ID; }
- // temp?
+#endif
+
+ bool operator==(ModelMinimalData const& other) const { return ID == other.ID; }
G3D::AABox const& getBounds() const { return iBound; }
+ };
+
+ struct TC_COMMON_API ModelSpawn : public ModelMinimalData
+ {
+#ifndef VMAP_DEBUG
+ std::string name;
+#endif
static bool readFromFile(FILE* rf, ModelSpawn& spawn);
static bool writeToFile(FILE* rw, ModelSpawn const& spawn);
};
- class TC_COMMON_API ModelInstance : public ModelSpawn
+ class TC_COMMON_API ModelInstance : public ModelMinimalData
{
public:
ModelInstance() : iInvScale(0.0f), iModel(nullptr) { }
@@ -70,6 +77,7 @@ namespace VMAP
void intersectPoint(G3D::Vector3 const& p, AreaInfo& info) const;
bool GetLocationInfo(G3D::Vector3 const& p, LocationInfo& info) const;
bool GetLiquidLevel(G3D::Vector3 const& p, LocationInfo& info, float& liqHeight) const;
+ G3D::Matrix3 const& GetInvRot() { return iInvRot; }
WorldModel* getWorldModel() { return iModel; }
protected:
G3D::Matrix3 iInvRot;
diff --git a/src/common/Collision/Models/WorldModel.h b/src/common/Collision/Models/WorldModel.h
index 093a4ea84ed..8e2c97501e1 100644
--- a/src/common/Collision/Models/WorldModel.h
+++ b/src/common/Collision/Models/WorldModel.h
@@ -117,11 +117,14 @@ namespace VMAP
bool writeFile(const std::string &filename);
bool readFile(const std::string &filename);
void getGroupModels(std::vector<GroupModel>& outGroupModels);
+ std::string const& GetName() const { return name; }
+ void SetName(std::string newName) { name = std::move(newName); }
uint32 Flags;
protected:
uint32 RootWMOID;
std::vector<GroupModel> groupModels;
BIH groupTree;
+ std::string name;
};
} // namespace VMAP