More Cleanups

This commit is contained in:
hondacrx
2021-06-08 12:56:09 -04:00
parent 302a1f293c
commit 52e43853fe
58 changed files with 223 additions and 257 deletions
@@ -33,7 +33,7 @@ namespace Game.Collision
void InitEmpty()
{
tree= new uint[3];
objects = new uint[0];
objects = Array.Empty<uint>();
// create space for the first node
tree[0] = (3u << 30); // dummy leaf
}
@@ -30,7 +30,7 @@ namespace Game.Collision
public void Remove(T obj)
{
++unbalanced_times;
uint Idx = 0;
uint Idx;
if (m_obj2Idx.TryGetValue(obj, out Idx))
m_objects[(int)Idx] = null;
else
+3 -3
View File
@@ -51,7 +51,7 @@ namespace Game.Collision
impl.Update(diff);
}
public bool GetIntersectionTime(Ray ray, Vector3 endPos, PhaseShift phaseShift, float maxDist)
public bool GetIntersectionTime(Ray ray, Vector3 endPos, PhaseShift phaseShift, ref float maxDist)
{
float distance = maxDist;
DynamicTreeIntersectionCallback callback = new(phaseShift);
@@ -63,7 +63,7 @@ namespace Game.Collision
public bool GetObjectHitPos(Vector3 startPos, Vector3 endPos, ref Vector3 resultHitPos, float modifyDist, PhaseShift phaseShift)
{
bool result = false;
bool result;
float maxDist = (endPos - startPos).magnitude();
// valid map coords should *never ever* produce float overflow, but this would produce NaNs too
Cypher.Assert(maxDist < float.MaxValue);
@@ -76,7 +76,7 @@ namespace Game.Collision
Vector3 dir = (endPos - startPos) / maxDist; // direction with length of 1
Ray ray = new(startPos, dir);
float dist = maxDist;
if (GetIntersectionTime(ray, endPos, phaseShift, dist))
if (GetIntersectionTime(ray, endPos, phaseShift, ref dist))
{
resultHitPos = startPos + dir * dist;
if (modifyDist < 0)
+61 -65
View File
@@ -121,60 +121,58 @@ namespace Game.Collision
if (fileResult.File != null)
{
result = LoadResult.Success;
using (BinaryReader reader = new(fileResult.File))
using BinaryReader reader = new(fileResult.File);
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
result = LoadResult.VersionMismatch;
if (result == LoadResult.Success)
{
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
result = LoadResult.VersionMismatch;
if (result == LoadResult.Success)
uint numSpawns = reader.ReadUInt32();
for (uint i = 0; i < numSpawns && result == LoadResult.Success; ++i)
{
uint numSpawns = reader.ReadUInt32();
for (uint i = 0; i < numSpawns && result == LoadResult.Success; ++i)
// read model spawns
if (ModelSpawn.ReadFromFile(reader, out ModelSpawn spawn))
{
// read model spawns
if (ModelSpawn.ReadFromFile(reader, out ModelSpawn spawn))
{
// acquire model instance
WorldModel model = vm.AcquireModelInstance(spawn.name, spawn.flags);
if (model == null)
Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : could not acquire WorldModel [{0}, {1}]", tileX, tileY);
// acquire model instance
WorldModel model = vm.AcquireModelInstance(spawn.name, spawn.flags);
if (model == null)
Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : could not acquire WorldModel [{0}, {1}]", tileX, tileY);
// update tree
if (iSpawnIndices.ContainsKey(spawn.Id))
// update tree
if (iSpawnIndices.ContainsKey(spawn.Id))
{
uint referencedVal = iSpawnIndices[spawn.Id];
if (!iLoadedSpawns.ContainsKey(referencedVal))
{
uint referencedVal = iSpawnIndices[spawn.Id];
if (!iLoadedSpawns.ContainsKey(referencedVal))
if (referencedVal >= iNTreeValues)
{
if (referencedVal >= iNTreeValues)
{
Log.outError(LogFilter.Maps, "StaticMapTree.LoadMapTile() : invalid tree element ({0}/{1}) referenced in tile {2}", referencedVal, iNTreeValues, fileResult.Name);
continue;
}
iTreeValues[referencedVal] = new ModelInstance(spawn, model);
iLoadedSpawns[referencedVal] = 1;
Log.outError(LogFilter.Maps, "StaticMapTree.LoadMapTile() : invalid tree element ({0}/{1}) referenced in tile {2}", referencedVal, iNTreeValues, fileResult.Name);
continue;
}
else
++iLoadedSpawns[referencedVal];
}
else if (iMapID == fileResult.UsedMapId)
{
// unknown parent spawn might appear in because it overlaps multiple tiles
// in case the original tile is swapped but its neighbour is now (adding this spawn)
// we want to not mark it as loading error and just skip that model
Log.outError(LogFilter.Maps, $"StaticMapTree.LoadMapTile() : invalid tree element (spawn {spawn.Id}) referenced in tile fileResult.Name{fileResult.Name} by map {iMapID}");
result = LoadResult.ReadFromFileFailed;
iTreeValues[referencedVal] = new ModelInstance(spawn, model);
iLoadedSpawns[referencedVal] = 1;
}
else
++iLoadedSpawns[referencedVal];
}
else
else if (iMapID == fileResult.UsedMapId)
{
Log.outError(LogFilter.Maps, $"StaticMapTree.LoadMapTile() : cannot read model from file (spawn index {i}) referenced in tile {fileResult.Name} by map {iMapID}");
// unknown parent spawn might appear in because it overlaps multiple tiles
// in case the original tile is swapped but its neighbour is now (adding this spawn)
// we want to not mark it as loading error and just skip that model
Log.outError(LogFilter.Maps, $"StaticMapTree.LoadMapTile() : invalid tree element (spawn {spawn.Id}) referenced in tile fileResult.Name{fileResult.Name} by map {iMapID}");
result = LoadResult.ReadFromFileFailed;
}
}
else
{
Log.outError(LogFilter.Maps, $"StaticMapTree.LoadMapTile() : cannot read model from file (spawn index {i}) referenced in tile {fileResult.Name} by map {iMapID}");
result = LoadResult.ReadFromFileFailed;
}
}
iLoadedTiles[PackTileID(tileX, tileY)] = true;
}
iLoadedTiles[PackTileID(tileX, tileY)] = true;
}
else
{
@@ -197,38 +195,36 @@ namespace Game.Collision
TileFileOpenResult fileResult = OpenMapTileFile(VMapManager.VMapPath, iMapID, tileX, tileY, vm);
if (fileResult.File != null)
{
using (BinaryReader reader = new(fileResult.File))
using BinaryReader reader = new(fileResult.File);
bool result = true;
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
result = false;
uint numSpawns = reader.ReadUInt32();
for (uint i = 0; i < numSpawns && result; ++i)
{
bool result = true;
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
result = false;
uint numSpawns = reader.ReadUInt32();
for (uint i = 0; i < numSpawns && result; ++i)
// read model spawns
ModelSpawn spawn;
result = ModelSpawn.ReadFromFile(reader, out spawn);
if (result)
{
// read model spawns
ModelSpawn spawn;
result = ModelSpawn.ReadFromFile(reader, out spawn);
if (result)
{
// release model instance
vm.ReleaseModelInstance(spawn.name);
// release model instance
vm.ReleaseModelInstance(spawn.name);
// update tree
if (iSpawnIndices.ContainsKey(spawn.Id))
// update tree
if (iSpawnIndices.ContainsKey(spawn.Id))
{
uint referencedNode = iSpawnIndices[spawn.Id];
if (!iLoadedSpawns.ContainsKey(referencedNode))
Log.outError(LogFilter.Server, "StaticMapTree.UnloadMapTile() : trying to unload non-referenced model '{0}' (ID:{1})", spawn.name, spawn.Id);
else if (--iLoadedSpawns[referencedNode] == 0)
{
uint referencedNode = iSpawnIndices[spawn.Id];
if (!iLoadedSpawns.ContainsKey(referencedNode))
Log.outError(LogFilter.Server, "StaticMapTree.UnloadMapTile() : trying to unload non-referenced model '{0}' (ID:{1})", spawn.name, spawn.Id);
else if (--iLoadedSpawns[referencedNode] == 0)
{
iTreeValues[referencedNode].SetUnloaded();
iLoadedSpawns.Remove(referencedNode);
}
iTreeValues[referencedNode].SetUnloaded();
iLoadedSpawns.Remove(referencedNode);
}
else if (iMapID == fileResult.UsedMapId) // logic documented in StaticMapTree::LoadMapTile
result = false;
}
else if (iMapID == fileResult.UsedMapId) // logic documented in StaticMapTree::LoadMapTile
result = false;
}
}
}
+18 -20
View File
@@ -189,30 +189,28 @@ namespace Game.Collision
}
try
{
using (BinaryReader reader = new(new FileStream(filename, FileMode.Open, FileAccess.Read)))
using BinaryReader reader = new(new FileStream(filename, FileMode.Open, FileAccess.Read));
string magic = reader.ReadStringFromChars(8);
if (magic != MapConst.VMapMagic)
{
string magic = reader.ReadStringFromChars(8);
if (magic != MapConst.VMapMagic)
{
Log.outError(LogFilter.Misc, $"File '{filename}' has wrong header, expected {MapConst.VMapMagic}.");
return;
}
Log.outError(LogFilter.Misc, $"File '{filename}' has wrong header, expected {MapConst.VMapMagic}.");
return;
}
long length = reader.BaseStream.Length;
while (true)
{
if (reader.BaseStream.Position >= length)
break;
long length = reader.BaseStream.Length;
while (true)
{
if (reader.BaseStream.Position >= length)
break;
uint displayId = reader.ReadUInt32();
bool isWmo = reader.ReadBoolean();
int name_length = reader.ReadInt32();
string name = reader.ReadString(name_length);
Vector3 v1 = reader.Read<Vector3>();
Vector3 v2 = reader.Read<Vector3>();
uint displayId = reader.ReadUInt32();
bool isWmo = reader.ReadBoolean();
int name_length = reader.ReadInt32();
string name = reader.ReadString(name_length);
Vector3 v1 = reader.Read<Vector3>();
Vector3 v2 = reader.Read<Vector3>();
StaticModelList.models.Add(displayId, new GameobjectModelData(name, v1, v2, isWmo));
}
StaticModelList.models.Add(displayId, new GameobjectModelData(name, v1, v2, isWmo));
}
}
catch (EndOfStreamException ex)
+26 -28
View File
@@ -371,40 +371,38 @@ namespace Game.Collision
{
if (!File.Exists(filename))
{
filename = filename + ".vmo";
filename += ".vmo";
if (!File.Exists(filename))
return false;
}
using (BinaryReader reader = new(new FileStream(filename, FileMode.Open, FileAccess.Read)))
using BinaryReader reader = new(new FileStream(filename, FileMode.Open, FileAccess.Read));
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
return false;
if (reader.ReadStringFromChars(4) != "WMOD")
return false;
reader.ReadUInt32(); //chunkSize notused
RootWMOID = reader.ReadUInt32();
// read group models
if (reader.ReadStringFromChars(4) != "GMOD")
return false;
uint count = reader.ReadUInt32();
for (var i = 0; i < count; ++i)
{
if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
return false;
if (reader.ReadStringFromChars(4) != "WMOD")
return false;
reader.ReadUInt32(); //chunkSize notused
RootWMOID = reader.ReadUInt32();
// read group models
if (reader.ReadStringFromChars(4) != "GMOD")
return false;
uint count = reader.ReadUInt32();
for (var i = 0; i < count; ++i)
{
GroupModel group = new();
group.ReadFromFile(reader);
groupModels.Add(group);
}
// read group BIH
if (reader.ReadStringFromChars(4) != "GBIH")
return false;
return groupTree.ReadFromFile(reader);
GroupModel group = new();
group.ReadFromFile(reader);
groupModels.Add(group);
}
// read group BIH
if (reader.ReadStringFromChars(4) != "GBIH")
return false;
return groupTree.ReadFromFile(reader);
}
}
}