@@ -174,10 +174,7 @@ private void OnConnected()
174174
175175 private void OnDisconnected ( )
176176 {
177- LandTiles = new LandObject [ 0 , 0 ] ;
178- LandTilesIdDictionary . Clear ( ) ;
179- GhostLandTiles . Clear ( ) ;
180- StaticsManager . Clear ( ) ;
177+ Reset ( ) ;
181178 }
182179
183180 public void EnableBlockLoading ( )
@@ -504,7 +501,7 @@ public IEnumerable<TileObject> GetTiles(TileObject? t1, TileObject? t2, bool top
504501
505502 private MouseState _prevMouseState = Mouse . GetState ( ) ;
506503 private bool _IsMouseDragging ;
507- public Rectangle ViewRange { get ; private set ; }
504+ public RectU16 ViewRange { get ; private set ; }
508505
509506 public void Update ( GameTime gameTime , bool isActive , bool processMouse , bool processKeyboard )
510507 {
@@ -702,26 +699,16 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
702699 Camera . Update ( ) ;
703700 if ( Client . Running )
704701 {
705- CalculateViewRange ( Camera , out var newViewRange ) ;
702+ var newViewRange = CalculateViewRange ( Camera ) ;
706703 if ( ViewRange != newViewRange )
707704 {
708- List < PointU16 > requested = new List < PointU16 > ( ) ;
709- for ( var x = newViewRange . Left / 8 ; x <= newViewRange . Right / 8 ; x ++ )
710- {
711- for ( var y = newViewRange . Top / 8 ; y <= newViewRange . Bottom / 8 ; y ++ )
712- {
713- requested . Add ( new PointU16 ( ( ushort ) x , ( ushort ) y ) ) ;
714- }
715- }
716-
717705 ViewRange = newViewRange ;
718- Client . ResizeCache ( ViewRange . Width * ViewRange . Height / 8 ) ;
719- Client . RequestBlocks ( requested ) ;
706+ Client . RequestBlocks ( ViewRange ) ;
720707 }
721708 }
722709 else
723710 {
724- ViewRange = Rectangle . Empty ;
711+ ViewRange = default ;
725712 }
726713 if ( Client . Running && AnimatedStatics )
727714 {
@@ -755,7 +742,7 @@ public void Reset()
755742 RealSelected = null ;
756743 GhostLandTiles . Clear ( ) ;
757744 StaticsManager . Clear ( ) ;
758- ViewRange = Rectangle . Empty ;
745+ ViewRange = default ;
759746 Client . ResetCache ( ) ;
760747 }
761748
@@ -805,7 +792,7 @@ private void UpdateMouseSelection(int x, int y)
805792 }
806793 }
807794
808- private void CalculateViewRange ( Camera camera , out Rectangle rect )
795+ private RectU16 CalculateViewRange ( Camera camera )
809796 {
810797 float zoom = camera . Zoom ;
811798 int screenWidth = camera . ScreenSize . Width ;
@@ -818,13 +805,14 @@ private void CalculateViewRange(Camera camera, out Rectangle rect)
818805 Vector3 center = camera . Position ;
819806
820807 // Render a few extra rows at the top to deal with things at lower z
821- var minTileX = ( int ) Math . Max ( 0 , ( center . X - screenDiamondDiagonal ) / TILE_SIZE - 8 ) ;
822- var minTileY = ( int ) Math . Max ( 0 , ( center . Y - screenDiamondDiagonal ) / TILE_SIZE - 8 ) ;
808+ var minTileX = Client . ClampX ( ( int ) ( ( center . X - screenDiamondDiagonal ) / TILE_SIZE - 8 ) ) ;
809+ var minTileY = Client . ClampY ( ( int ) ( ( center . Y - screenDiamondDiagonal ) / TILE_SIZE - 8 ) ) ;
823810
824811 // Render a few extra rows at the bottom to deal with things at higher z
825- var maxTileX = ( int ) Math . Min ( Client . Width * 8 - 1 , ( center . X + screenDiamondDiagonal ) / TILE_SIZE + 8 ) ;
826- var maxTileY = ( int ) Math . Min ( Client . Height * 8 - 1 , ( center . Y + screenDiamondDiagonal ) / TILE_SIZE + 8 ) ;
827- rect = new Rectangle ( minTileX , minTileY , maxTileX - minTileX , maxTileY - minTileY ) ;
812+ var maxTileX = Client . ClampX ( ( int ) ( ( center . X + screenDiamondDiagonal ) / TILE_SIZE + 8 ) ) ;
813+ var maxTileY = Client . ClampY ( ( int ) ( ( center . Y + screenDiamondDiagonal ) / TILE_SIZE + 8 ) ) ;
814+
815+ return new RectU16 ( minTileX , minTileY , maxTileX , maxTileY ) ;
828816 }
829817
830818 public Vector3 Unproject ( int x , int y , int z )
@@ -1058,24 +1046,21 @@ private void DrawSelectionBuffer()
10581046 _DepthStencilState ,
10591047 BlendState . AlphaBlend
10601048 ) ;
1061- for ( var x = ViewRange . Left ; x < ViewRange . Right ; x ++ )
1049+ foreach ( var ( x , y ) in ViewRange )
10621050 {
1063- for ( var y = ViewRange . Top ; y < ViewRange . Bottom ; y ++ )
1051+ var landTile = LandTiles [ x , y ] ;
1052+ if ( landTile != null )
10641053 {
1065- var landTile = LandTiles [ x , y ] ;
1066- if ( landTile != null )
1067- {
1068- DrawLand ( landTile , landTile . ObjectIdColor ) ;
1069- }
1054+ DrawLand ( landTile , landTile . ObjectIdColor ) ;
1055+ }
10701056
1071- var tiles = StaticsManager . Get ( x , y ) ;
1072- if ( tiles == null ) continue ;
1073- foreach ( var tile in tiles )
1057+ var tiles = StaticsManager . Get ( x , y ) ;
1058+ if ( tiles == null ) continue ;
1059+ foreach ( var tile in tiles )
1060+ {
1061+ if ( tile . CanDraw )
10741062 {
1075- if ( tile . CanDraw )
1076- {
1077- DrawStatic ( tile , tile . ObjectIdColor ) ;
1078- }
1063+ DrawStatic ( tile , tile . ObjectIdColor ) ;
10791064 }
10801065 }
10811066 }
@@ -1115,7 +1100,7 @@ private void DrawLights(Camera camera)
11151100 _mapRenderer . End ( ) ;
11161101 }
11171102
1118- private void DrawLand ( Camera camera , Rectangle viewRange , string technique = "Terrain" )
1103+ private void DrawLand ( Camera camera , RectU16 viewRange , string technique = "Terrain" )
11191104 {
11201105 if ( ! ShowLand )
11211106 {
@@ -1132,23 +1117,21 @@ private void DrawLand(Camera camera, Rectangle viewRange, string technique = "Te
11321117 BlendState . AlphaBlend
11331118 ) ;
11341119
1135- for ( var x = viewRange . Left ; x < viewRange . Right ; x ++ )
1120+ foreach ( var ( x , y ) in viewRange )
11361121 {
1137- for ( var y = viewRange . Top ; y < viewRange . Bottom ; y ++ )
1122+ var tile = LandTiles [ x , y ] ;
1123+ if ( tile != null && tile . CanDraw )
11381124 {
1139- var tile = LandTiles [ x , y ] ;
1140- if ( tile != null && tile . CanDraw )
1125+ var hueOverride = Vector4 . Zero ;
1126+ if ( WalkableSurfaces && ! UoFileManager . TileData . LandData [ tile . LandTile . Id ] . IsWet )
11411127 {
1142- var hueOverride = Vector4 . Zero ;
1143- if ( WalkableSurfaces && ! UoFileManager . TileData . LandData [ tile . LandTile . Id ] . IsWet )
1144- {
1145- hueOverride = IsWalkable ( tile ) ? WalkableHue : NonWalkableHue ;
1128+ hueOverride = IsWalkable ( tile ) ? WalkableHue : NonWalkableHue ;
11461129
1147- }
1148- DrawLand ( tile , hueOverride ) ;
11491130 }
1131+ DrawLand ( tile , hueOverride ) ;
11501132 }
11511133 }
1134+
11521135 foreach ( var tile in GhostLandTiles . Values )
11531136 {
11541137 DrawLand ( tile ) ;
@@ -1165,15 +1148,12 @@ private void DrawLandHeight()
11651148 var font = _fontSystem . GetFont ( 18 * Camera . Zoom ) ;
11661149 var halfTile = TILE_SIZE * 0.5f * Camera . Zoom ;
11671150 _spriteBatch . Begin ( ) ;
1168- for ( var x = ViewRange . Left ; x < ViewRange . Right ; x ++ )
1151+ foreach ( var ( x , y ) in ViewRange )
11691152 {
1170- for ( var y = ViewRange . Top ; y < ViewRange . Bottom ; y ++ )
1153+ var tile = LandTiles [ x , y ] ;
1154+ if ( tile != null && tile . CanDraw )
11711155 {
1172- var tile = LandTiles [ x , y ] ;
1173- if ( tile != null && tile . CanDraw )
1174- {
1175- DrawTileHeight ( tile , font , halfTile ) ;
1176- }
1156+ DrawTileHeight ( tile , font , halfTile ) ;
11771157 }
11781158 }
11791159 foreach ( var tile in GhostLandTiles . Values )
@@ -1200,7 +1180,7 @@ private void DrawTileHeight(LandObject tile, DynamicSpriteFont font, float yOffs
12001180 }
12011181 }
12021182
1203- private void DrawStatics ( Camera camera , Rectangle viewRange )
1183+ private void DrawStatics ( Camera camera , RectU16 viewRange )
12041184 {
12051185 if ( ! ShowStatics )
12061186 {
@@ -1216,23 +1196,20 @@ private void DrawStatics(Camera camera, Rectangle viewRange)
12161196 _DepthStencilState ,
12171197 BlendState . AlphaBlend
12181198 ) ;
1219- for ( var x = viewRange . Left ; x < viewRange . Right ; x ++ )
1199+ foreach ( var ( x , y ) in viewRange )
12201200 {
1221- for ( var y = viewRange . Top ; y < viewRange . Bottom ; y ++ )
1201+ var tiles = StaticsManager . Get ( x , y ) ;
1202+ if ( tiles == null ) continue ;
1203+ foreach ( var tile in tiles )
12221204 {
1223- var tiles = StaticsManager . Get ( x , y ) ;
1224- if ( tiles == null ) continue ;
1225- foreach ( var tile in tiles )
1205+ if ( tile . CanDraw )
12261206 {
1227- if ( tile . CanDraw )
1207+ var hueOverride = Vector4 . Zero ;
1208+ if ( WalkableSurfaces && UoFileManager . TileData . StaticData [ tile . Tile . Id ] . IsSurface )
12281209 {
1229- var hueOverride = Vector4 . Zero ;
1230- if ( WalkableSurfaces && UoFileManager . TileData . StaticData [ tile . Tile . Id ] . IsSurface )
1231- {
1232- hueOverride = IsWalkable ( tile ) ? WalkableHue : NonWalkableHue ;
1233- }
1234- DrawStatic ( tile , hueOverride ) ;
1210+ hueOverride = IsWalkable ( tile ) ? WalkableHue : NonWalkableHue ;
12351211 }
1212+ DrawStatic ( tile , hueOverride ) ;
12361213 }
12371214 }
12381215 }
@@ -1303,18 +1280,8 @@ public void ExportImage(string path, int widthPx, int heightPx, float zoom)
13031280 myCamera . ScreenSize = new Rectangle ( rbounds . X , rbounds . Y , rbounds . Width , rbounds . Height ) ;
13041281 myCamera . Update ( ) ;
13051282
1306- CalculateViewRange ( myCamera , out var bounds ) ;
1307- List < PointU16 > requested = new List < PointU16 > ( ) ;
1308- for ( var x = bounds . Left / 8 ; x <= bounds . Right / 8 ; x ++ )
1309- {
1310- for ( var y = bounds . Top / 8 ; y <= bounds . Bottom / 8 ; y ++ )
1311- {
1312- requested . Add ( new PointU16 ( ( ushort ) x , ( ushort ) y ) ) ;
1313- }
1314- }
1315-
1316- Client . ResizeCache ( bounds . Width * bounds . Height / 8 ) ;
1317- Client . RequestBlocks ( requested ) ;
1283+ var cameraBounds = CalculateViewRange ( myCamera ) ;
1284+ Client . RequestBlocks ( cameraBounds ) ;
13181285 while ( Client . WaitingForBlocks )
13191286 Client . Update ( ) ;
13201287
@@ -1327,8 +1294,8 @@ public void ExportImage(string path, int widthPx, int heightPx, float zoom)
13271294 _mapEffect . WorldViewProj = myCamera . FnaWorldViewProj ;
13281295 DrawLights ( myCamera ) ;
13291296 _mapRenderer . SetRenderTarget ( myRenderTarget , new FNARectangle ( 0 , 0 , widthPx , heightPx ) ) ;
1330- DrawLand ( myCamera , bounds ) ;
1331- DrawStatics ( myCamera , bounds ) ;
1297+ DrawLand ( myCamera , cameraBounds ) ;
1298+ DrawStatics ( myCamera , cameraBounds ) ;
13321299 ApplyLights ( ) ;
13331300 using var fs = new FileStream ( path , FileMode . OpenOrCreate ) ;
13341301 if ( path . EndsWith ( ".png" ) )
0 commit comments