Skip to content

Commit eb99b75

Browse files
authored
Merge pull request #711 from Csantucci/fix-precipitation-out-of-bounds
Bug fix for https://bugs.launchpad.net/or/+bug/1989426 Out of bounds crash in Precipitation.cs
2 parents 44180b5 + f6ca333 commit eb99b75

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Source/RunActivity/Viewer3D/Precipitation.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,19 @@ public float GetHeight(WorldLocation location, TileManager tiles, SceneryDrawer
446446
var x = (int)((location.Location.X + 1024) / BlockSize);
447447
var z = (int)((location.Location.Z + 1024) / BlockSize);
448448

449+
// Trace the case where x or z are out of bounds and fix
450+
var xSize = tile.Height.GetLength(0);
451+
var zSize = tile.Height.GetLength(1);
452+
if (x < 0 || x >= xSize || z < 0 || z >= zSize)
453+
{
454+
Trace.TraceWarning("At least one precipitation index is out of bounds: x = {0}, z = {1}, Location.X = {2}, Location.Z = {3}, BlockSize = {4}, HeightDimensionX = {5}, HeightDimensionZ = {6} ; fixing it",
455+
x, z, location.Location.X, location.Location.Z, BlockSize, xSize, zSize);
456+
if (x >= xSize) x = xSize - 1;
457+
if (z >= zSize) z = zSize - 1;
458+
if (x < 0) x = 0;
459+
if (z < 0) z = 0;
460+
}
461+
449462
// If we don't have it cached, load it.
450463
if (tile.Height[x, z] == float.MinValue)
451464
{

0 commit comments

Comments
 (0)