Skip to content

Commit de822d0

Browse files
authored
Merge branch 'master' into master
2 parents 42e5c74 + 1f28a34 commit de822d0

6 files changed

Lines changed: 114 additions & 115 deletions

File tree

CentrED/Map/MapManager.cs

Lines changed: 51 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -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"))

CentrED/UI/Windows/ConnectWindow.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,12 @@ protected override void InternalDraw()
117117
if (ImGui.Button(LangManager.Get(DISCONNECT)))
118118
{
119119
CEDClient.Disconnect();
120-
CEDGame.MapManager.Reset();
121120
}
122121
}
123122
else
124123
{
125124
if (ImGui.Button(LangManager.Get(CONNECT)) || ImGui.IsWindowFocused() && ImGui.IsKeyPressed(ImGuiKey.Enter))
126125
{
127-
CEDGame.MapManager.Reset();
128126
_buttonDisabled = true;
129127
new Task
130128
(

CentrED/UI/Windows/MinimapWindow.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ protected override void InternalDraw()
155155
}
156156

157157
var rect = CEDGame.MapManager.ViewRange;
158-
var center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
159-
var p1 = currentPos + new Vector2(rect.Left / 8, center.Y / 8);
160-
var p2 = currentPos + new Vector2(center.X / 8, rect.Top / 8);
161-
var p3 = currentPos + new Vector2(rect.Right / 8, center.Y / 8);
162-
var p4 = currentPos + new Vector2(center.X / 8, rect.Bottom / 8);
158+
var center = new Point(rect.X1 + rect.Width / 2, rect.Y1 + rect.Height / 2);
159+
var p1 = currentPos + new Vector2(rect.X1 / 8, center.Y / 8);
160+
var p2 = currentPos + new Vector2(center.X / 8, rect.Y1 / 8);
161+
var p3 = currentPos + new Vector2(rect.X2 / 8, center.Y / 8);
162+
var p4 = currentPos + new Vector2(center.X / 8, rect.Y2 / 8);
163163
ImGui.GetWindowDrawList().AddQuad(p1, p2, p3, p4, ImGui.GetColorU32(ImGuiColor.Red));
164164
CEDGame.UIManager.GetWindow<LSOWindow>().DrawArea(currentPos);
165165
CEDGame.UIManager.GetWindow<ServerAdminWindow>().DrawArea(currentPos);

Client/CentrEDClient.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,25 @@ public void LoadBlocks(RectU16 areaInfo)
200200

201201
public void RequestBlocks(RectU16 areaInfo)
202202
{
203-
List<PointU16> requested = new List<PointU16>();
204-
for (var x = areaInfo.X1 / 8; x <= areaInfo.X2 / 8; x++)
203+
List<PointU16> toRequest = new List<PointU16>();
204+
foreach (var (x,y) in areaInfo / 8)
205205
{
206-
for (var y = areaInfo.Y1 / 8; y <= areaInfo.Y2 / 8; y++)
207-
{
208-
requested.Add(new PointU16((ushort)x, (ushort)y));
209-
}
206+
if(!IsValidX(x) || !IsValidY(y))
207+
continue;
208+
if(Landscape.BlockCache.Contains(Block.Id(x, y)))
209+
continue;
210+
211+
var chunk = new PointU16(x, y);
212+
if(RequestedBlocks.Contains(chunk))
213+
continue;
214+
215+
toRequest.Add(chunk);
210216
}
211-
212-
ResizeCache(Math.Max(1, areaInfo.Width * areaInfo.Height / 8));
213-
RequestBlocks(requested);
214-
}
215-
216-
public void RequestBlocks(List<PointU16> blockCoords)
217-
{
218-
var filteredBlockCoords = blockCoords.FindAll
219-
(b => !Landscape.BlockCache.Contains(Block.Id(b.X, b.Y)) && !RequestedBlocks.Contains(b) && IsValidX(b.X) && IsValidY(b.Y));
217+
218+
Landscape.BlockCache.Grow(Math.Max(1, areaInfo.Width * areaInfo.Height / 8));
220219

221-
filteredBlockCoords.ForEach(b => RequestedBlocks.Add(b));
222-
filteredBlockCoords.ForEach(b => RequestedBlocksQueue.Enqueue(b));;
220+
toRequest.ForEach(b => RequestedBlocks.Add(b));
221+
toRequest.ForEach(b => RequestedBlocksQueue.Enqueue(b));;
223222
}
224223

225224
private void UpdateRequestedBlocks()
@@ -246,11 +245,21 @@ public bool IsValidX(int x)
246245
return x >= 0 && x < WidthInTiles;
247246
}
248247

248+
public ushort ClampX(int x)
249+
{
250+
return (ushort)Math.Clamp(x, 0, WidthInTiles - 1);
251+
}
252+
249253
public bool IsValidY(int y)
250254
{
251255
return y >= 0 && y < HeightInTiles;
252256
}
253257

258+
public ushort ClampY(int y)
259+
{
260+
return (ushort)Math.Clamp(y, 0, HeightInTiles - 1);
261+
}
262+
254263
public bool InternalSetPos(ushort x, ushort y)
255264
{
256265
if (x == X && y == Y)
@@ -343,11 +352,7 @@ public void SendWithUndo(Packet p)
343352
public void ResetCache()
344353
{
345354
Landscape?.BlockCache.Reset();
346-
}
347-
348-
public void ResizeCache(int newSize)
349-
{
350-
Landscape?.BlockCache.Resize(newSize);
355+
Landscape?.BlockCache.Resize(Math.Max(Width, Height) + 1);
351356
}
352357

353358
public void Flush()
@@ -462,7 +467,7 @@ internal void InitLandscape(ushort width, ushort height)
462467
{
463468
Landscape = new ClientLandscape(this, width, height);
464469
Landscape.RegisterPacketHandlers(NetState!);
465-
Landscape.BlockCache.Resize(Math.Max(width, height) + 1);
470+
ResetCache();
466471
Connected?.Invoke();
467472
State = ClientState.Running;
468473
if (AccessLevel == AccessLevel.Administrator)

Shared/BlockCache.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ private bool Dequeue(out Block? block)
6060
return true;
6161
}
6262

63+
public void Grow(int newSize)
64+
{
65+
if (newSize > _maxSize)
66+
Resize(newSize);
67+
}
68+
6369
public void Resize(int newSize)
6470
{
6571
if (newSize < 0)

0 commit comments

Comments
 (0)