@@ -1102,26 +1102,66 @@ public virtual bool ShouldDrawName
11021102
11031103 public virtual HashSet < Entity > ? DetermineRenderOrder ( HashSet < Entity > ? existingRenderSet , IMapInstance ? map )
11041104 {
1105- _ = ( existingRenderSet ? . Remove ( this ) ) ;
1105+ _ = existingRenderSet ? . Remove ( this ) ;
1106+
1107+ if ( map == null )
1108+ {
1109+ return null ;
1110+ }
1111+
1112+ var mapGrid = Globals . MapGrid ;
1113+ if ( mapGrid == null )
1114+ {
1115+ return null ;
1116+ }
1117+
1118+ var renderingEntities = Graphics . RenderingEntities ;
1119+ if ( renderingEntities == null )
1120+ {
1121+ return null ;
1122+ }
11061123
11071124 var playerMap = Globals . Me ? . MapInstance ;
1108- if ( map == default || playerMap == default || Globals . MapGrid == default )
1125+ if ( playerMap == default )
11091126 {
11101127 return default ;
11111128 }
11121129
11131130 var gridX = playerMap . GridX ;
11141131 var gridY = playerMap . GridY ;
1115- for ( var x = gridX - 1 ; x <= gridX + 1 ; x ++ )
1132+ var startX = Math . Max ( 0 , gridX - 1 ) ;
1133+ var startY = Math . Max ( 0 , gridY - 1 ) ;
1134+
1135+ var mapGridWidth = Globals . MapGridWidth ;
1136+ var mapGridHeight = Globals . MapGridHeight ;
1137+ var mapGridLimitX = mapGrid . GetLength ( 0 ) ;
1138+ var mapGridLimitY = mapGrid . GetLength ( 1 ) ;
1139+ var endX = Math . Min ( Math . Min ( mapGridWidth , mapGridLimitX ) - 1 , gridX + 1 ) ;
1140+ var endY = Math . Min ( Math . Min ( mapGridHeight , mapGridLimitY ) - 1 , gridY + 1 ) ;
1141+
1142+ if ( mapGridWidth != mapGridLimitX )
1143+ {
1144+ ApplicationContext . CurrentContext . Logger . LogWarning (
1145+ "Possible map grid race condition detected, MapGrid's X dimension is {XLength} but MapGridWidth is {Width}" ,
1146+ mapGridLimitX ,
1147+ mapGridWidth
1148+ ) ;
1149+ }
1150+
1151+ if ( mapGridHeight != mapGridLimitY )
11161152 {
1117- for ( var y = gridY - 1 ; y <= gridY + 1 ; y ++ )
1118- {
1119- if ( x < 0 || x >= Globals . MapGridWidth || y < 0 || y >= Globals . MapGridHeight )
1120- {
1121- continue ;
1122- }
1153+ ApplicationContext . CurrentContext . Logger . LogWarning (
1154+ "Possible map grid race condition detected, MapGrid's Y dimension is {YLength} but MapGridHeight is {Height}" ,
1155+ mapGridLimitY ,
1156+ mapGridHeight
1157+ ) ;
1158+ }
11231159
1124- var mapIdOnGrid = Globals . MapGrid [ x , y ] ;
1160+ for ( var x = startX ; x <= endX ; ++ x )
1161+ {
1162+ for ( var y = startY ; y <= endY ; ++ y )
1163+ {
1164+ var mapIdOnGrid = mapGrid [ x , y ] ;
11251165 if ( mapIdOnGrid != MapId )
11261166 {
11271167 continue ;
@@ -1151,7 +1191,6 @@ public virtual bool ShouldDrawName
11511191
11521192 entityY = ( int ) Math . Floor ( entityY + OffsetY / TileHeight ) ;
11531193
1154- var renderingEntities = Graphics . RenderingEntities ;
11551194 if ( priority >= renderingEntities . GetLength ( 0 ) || entityY >= renderingEntities . GetLength ( 1 ) )
11561195 {
11571196 return renderSet ;
0 commit comments