Core/Maps: Updated vmaps and some cleaups

This commit is contained in:
hondacrx
2018-05-03 00:05:50 -04:00
parent 3ca70aae30
commit 1a7f31d4f2
14 changed files with 204 additions and 269 deletions
@@ -276,14 +276,10 @@ namespace Game.Collision
bounds = new AxisAlignedBox(lo, hi);
uint treeSize = reader.ReadUInt32();
tree = new uint[treeSize];
for (var i = 0; i < treeSize; i++)
tree[i] = reader.ReadUInt32();
tree = reader.ReadArray<uint>(treeSize);
var count = reader.ReadUInt32();
objects = new uint[count];
for (var i = 0; i < count; i++)
objects[i] = reader.ReadUInt32();
objects = reader.ReadArray<uint>(count);
return true;
}
+30 -22
View File
@@ -70,7 +70,7 @@ namespace Game.Collision
{
string filename = VMapPath + getMapFileName(mapId);
StaticMapTree newTree = new StaticMapTree(mapId);
if (!newTree.InitMap(filename, this))
if (!newTree.InitMap(filename))
return false;
iInstanceMapTrees.Add(mapId, newTree);
@@ -235,36 +235,42 @@ namespace Game.Collision
public WorldModel acquireModelInstance(string filename)
{
var model = iLoadedModelFiles.LookupByKey(filename);
if (model == null)
lock (LoadedModelFilesLock)
{
WorldModel worldmodel = new WorldModel();
if (!worldmodel.readFile(VMapPath + filename + ".vmo"))
var model = iLoadedModelFiles.LookupByKey(filename);
if (model == null)
{
Log.outError(LogFilter.Server, "VMapManager: could not load '{0}.vmo'", filename);
return null;
WorldModel worldmodel = new WorldModel();
if (!worldmodel.readFile(VMapPath + filename + ".vmo"))
{
Log.outError(LogFilter.Server, "VMapManager: could not load '{0}.vmo'", filename);
return null;
}
Log.outDebug(LogFilter.Maps, "VMapManager: loading file '{0}'", filename);
iLoadedModelFiles.Add(filename, new ManagedModel());
model = iLoadedModelFiles.LookupByKey(filename);
model.setModel(worldmodel);
}
Log.outDebug(LogFilter.Maps, "VMapManager: loading file '{0}'", filename);
iLoadedModelFiles.Add(filename, new ManagedModel());
model = iLoadedModelFiles.LookupByKey(filename);
model.setModel(worldmodel);
model.incRefCount();
return model.getModel();
}
model.incRefCount();
return model.getModel();
}
public void releaseModelInstance(string filename)
{
var model = iLoadedModelFiles.LookupByKey(filename);
if (model == null)
lock (LoadedModelFilesLock)
{
Log.outError(LogFilter.Server, "VMapManager: trying to unload non-loaded file '{0}'", filename);
return;
}
if (model.decRefCount() == 0)
{
Log.outDebug(LogFilter.Maps, "VMapManager: unloading file '{0}'", filename);
iLoadedModelFiles.Remove(filename);
var model = iLoadedModelFiles.LookupByKey(filename);
if (model == null)
{
Log.outError(LogFilter.Server, "VMapManager: trying to unload non-loaded file '{0}'", filename);
return;
}
if (model.decRefCount() == 0)
{
Log.outDebug(LogFilter.Maps, "VMapManager: unloading file '{0}'", filename);
iLoadedModelFiles.Remove(filename);
}
}
}
@@ -310,6 +316,8 @@ namespace Game.Collision
Dictionary<uint, uint> iParentMapData = new Dictionary<uint, uint>();
bool _enableLineOfSightCalc;
bool _enableHeightCalc;
object LoadedModelFilesLock = new object();
}
public class ManagedModel
+12 -48
View File
@@ -58,47 +58,23 @@ namespace Game.Collision
iMapID = mapId;
}
public bool InitMap(string fname, VMapManager vm)
public bool InitMap(string fname)
{
Log.outDebug(LogFilter.Maps, "StaticMapTree.InitMap() : initializing StaticMapTree '{0}'", fname);
bool success = false;
if (!File.Exists(fname))
return false;
char tiled = '0';
using (BinaryReader reader = new BinaryReader(new FileStream(fname, FileMode.Open, FileAccess.Read)))
{
var magic = reader.ReadStringFromChars(8);
tiled = reader.ReadChar();
var node = reader.ReadStringFromChars(4);
if (magic == MapConst.VMapMagic && node == "NODE" && iTree.readFromFile(reader))
{
iNTreeValues = iTree.primCount();
iTreeValues = new ModelInstance[iNTreeValues];
success = reader.ReadStringFromChars(4) == "GOBJ";
}
iIsTiled = (tiled == 1);
// global model spawns
// only non-tiled maps have them, and if so exactly one (so far at least...)
ModelSpawn spawn;
if (!iIsTiled && ModelSpawn.readFromFile(reader, out spawn))
{
WorldModel model = vm.acquireModelInstance(spawn.name);
Log.outDebug(LogFilter.Maps, "StaticMapTree.InitMap() : loading {0}", spawn.name);
if (model != null)
{
// assume that global model always is the first and only tree value (could be improved...)
iTreeValues[0] = new ModelInstance(spawn, model);
iLoadedSpawns[0] = 1;
}
else
{
success = false;
Log.outError(LogFilter.Server, "StaticMapTree.InitMap() : could not acquire WorldModel for '{0}'", spawn.name);
}
success = true;
}
if (success)
@@ -107,7 +83,7 @@ namespace Game.Collision
if (success)
{
uint spawnIndicesSize = reader.ReadUInt32();
for (uint i = 0; i < spawnIndicesSize && success; ++i)
for (uint i = 0; i < spawnIndicesSize; ++i)
{
uint spawnId = reader.ReadUInt32();
uint spawnIndex = reader.ReadUInt32();
@@ -133,13 +109,6 @@ namespace Game.Collision
public bool LoadMapTile(uint tileX, uint tileY, VMapManager vm)
{
if (!iIsTiled)
{
// currently, core creates grids for all maps, whether it has terrain tiles or not
// so we need "fake" tile loads to know when we can unload map geometry
iLoadedTiles[packTileID(tileX, tileY)] = false;
return true;
}
if (iTreeValues == null)
{
Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : tree has not been initialized [{0}, {1}]", tileX, tileY);
@@ -285,25 +254,21 @@ namespace Game.Collision
{
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
return false;
}
char tiled = reader.ReadChar();
if (tiled == 1)
{
FileStream stream = OpenMapTileFile(vmapPath, mapID, tileX, tileY, vm);
if (stream == null)
return false;
FileStream stream = OpenMapTileFile(vmapPath, mapID, tileX, tileY, vm);
if (stream == null)
return false;
using (BinaryReader reader1 = new BinaryReader(stream))
{
if (reader1.ReadStringFromChars(8) != MapConst.VMapMagic)
return false;
}
}
using (BinaryReader reader = new BinaryReader(stream))
{
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
return false;
}
return true;
}
public static string getTileFileName(uint mapID, uint tileX, uint tileY)
{
return string.Format("{0:D4}_{1:D2}_{2:D2}.vmtile", mapID, tileY, tileX);
@@ -425,7 +390,6 @@ namespace Game.Collision
public int numLoadedTiles() { return iLoadedTiles.Count; }
uint iMapID;
bool iIsTiled;
BIH iTree = new BIH();
ModelInstance[] iTreeValues;
uint iNTreeValues;
@@ -24,8 +24,8 @@ namespace Game.Collision
public enum ModelFlags
{
M2 = 1,
WorldSpawn = 1 << 1,
HasBound = 1 << 2
HasBound = 1 << 1,
ParentSpawn = 1 << 2
}
public class ModelSpawn
+6 -8
View File
@@ -139,14 +139,10 @@ namespace Game.Collision
if (liquid.iTilesX != 0 && liquid.iTilesY != 0)
{
uint size = (liquid.iTilesX + 1) * (liquid.iTilesY + 1);
liquid.iHeight = new float[size];
for (var i = 0; i < size; i++)
liquid.iHeight[i] = reader.ReadSingle();
liquid.iHeight = reader.ReadArray<float>(size);
size = liquid.iTilesX * liquid.iTilesY;
liquid.iFlags = new byte[size];
for (var i = 0; i < size; i++)
liquid.iFlags[i] = reader.ReadByte();
liquid.iFlags = reader.ReadArray<byte>(size);
}
else
{
@@ -210,7 +206,9 @@ namespace Game.Collision
vertices.Clear();
iLiquid = null;
iBound = reader.ReadStruct<AxisAlignedBox>();
var lo = reader.Read<Vector3>();
var hi = reader.Read<Vector3>();
iBound = new AxisAlignedBox(lo, hi);
iMogpFlags = reader.ReadUInt32();
iGroupWMOID = reader.ReadUInt32();
@@ -234,7 +232,7 @@ namespace Game.Collision
count = reader.ReadUInt32();
for (var i = 0; i < count; ++i)
triangles.Add(reader.ReadStruct<MeshTriangle>());
triangles.Add(reader.Read<MeshTriangle>());
// read mesh BIH
if (reader.ReadStringFromChars(4) != "MBIH")