Skip to content

Commit

Permalink
fix: flip incorrect condition and actually bulk-request updates, get …
Browse files Browse the repository at this point in the history
…rid of a lot of horrid lookups (#2037)
  • Loading branch information
lodicolo authored Nov 5, 2023
1 parent e211256 commit 95f14b9
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 355 deletions.
20 changes: 1 addition & 19 deletions Intersect.Client/Core/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,7 @@ private static void ProcessGame()
}
else
{
if (MapInstance.TryGet(Globals.Me.MapId, out var mapInstance))
{
var gridX = mapInstance.GridX;
var gridY = mapInstance.GridY;
for (int x = gridX - 1; x <= gridX + 1; x++)
{
for (int y = gridY - 1; y <= gridY + 1; y++)
{
if (x >= 0 &&
x < Globals.MapGridWidth &&
y >= 0 &&
y < Globals.MapGridHeight &&
Globals.MapGrid[x, y] != Guid.Empty)
{
PacketSender.SendNeedMap(Globals.MapGrid[x, y]);
}
}
}
}
PacketSender.SendNeedMapForGrid();
}

if (!Globals.NeedsMaps)
Expand Down
20 changes: 1 addition & 19 deletions Intersect.Client/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2327,25 +2327,7 @@ public void FetchNewMaps()
return;
}

if (Maps.MapInstance.TryGet(Globals.Me.MapId, out var mapInstance))
{
var gridX = mapInstance.GridX;
var gridY = mapInstance.GridY;
for (var x = gridX - 1; x <= gridX + 1; x++)
{
for (var y = gridY - 1; y <= gridY + 1; y++)
{
if (x >= 0 &&
x < Globals.MapGridWidth &&
y >= 0 &&
y < Globals.MapGridHeight &&
Globals.MapGrid[x, y] != Guid.Empty)
{
PacketSender.SendNeedMap(Globals.MapGrid[x, y]);
}
}
}
}
PacketSender.SendNeedMapForGrid();
}

public override void DrawEquipment(string filename, Color renderColor)
Expand Down
39 changes: 18 additions & 21 deletions Intersect.Client/Networking/PacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,36 +205,33 @@ public void HandlePacket(IPacketSender packetSender, MapAreaPacket packet)
private void HandleMap(IPacketSender packetSender, MapPacket packet)
{
var mapId = packet.MapId;
var map = MapInstance.Get(mapId);
if (map != null)
if (MapInstance.TryGet(mapId, out var mapInstance))
{
if (packet.Revision == map.Revision)
if (packet.Revision == mapInstance.Revision)
{
return;
}
else
{
map.Dispose(false, false);
}

mapInstance.Dispose(false, false);
}

map = new MapInstance(mapId);
MapInstance.Lookup.Set(mapId, map);
lock (map.MapLock)
mapInstance = new MapInstance(mapId);
MapInstance.Lookup.Set(mapId, mapInstance);
lock (mapInstance.MapLock)
{
map.Load(packet.Data);
map.LoadTileData(packet.TileData);
map.AttributeData = packet.AttributeData;
map.CreateMapSounds();
mapInstance.Load(packet.Data);
mapInstance.LoadTileData(packet.TileData);
mapInstance.AttributeData = packet.AttributeData;
mapInstance.CreateMapSounds();
if (mapId == Globals.Me.MapId)
{
Audio.PlayMusic(map.Music, ClientConfiguration.Instance.MusicFadeTimer, ClientConfiguration.Instance.MusicFadeTimer, true);
Audio.PlayMusic(mapInstance.Music, ClientConfiguration.Instance.MusicFadeTimer, ClientConfiguration.Instance.MusicFadeTimer, true);
}

map.GridX = packet.GridX;
map.GridY = packet.GridY;
map.CameraHolds = packet.CameraHolds;
map.Autotiles.InitAutotiles(map.GenerateAutotileGrid());
mapInstance.GridX = packet.GridX;
mapInstance.GridY = packet.GridY;
mapInstance.CameraHolds = packet.CameraHolds;
mapInstance.Autotiles.InitAutotiles(mapInstance.GenerateAutotileGrid());

//Process Entities and Items if provided in this packet
if (packet.MapEntities != null)
Expand All @@ -251,14 +248,14 @@ private void HandleMap(IPacketSender packetSender, MapPacket packet)
{
foreach (var evt in Globals.PendingEvents[mapId])
{
map.AddEvent(evt.Key, evt.Value);
mapInstance.AddEvent(evt.Key, evt.Value);
}

Globals.PendingEvents[mapId].Clear();
}
}

MapInstance.OnMapLoaded?.Invoke(map);
MapInstance.OnMapLoaded?.Invoke(mapInstance);
}

//MapPacket
Expand Down
20 changes: 14 additions & 6 deletions Intersect.Client/Networking/PacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void SendLogout(bool characterSelect = false)
public static void SendNeedMap(params Guid[] mapIds)
{
var validMapIds = mapIds.Where(
mapId => mapId != default && MapInstance.TryGet(mapId, out _) && MapInstance.MapNotRequested(mapId)
mapId => mapId != default && !MapInstance.TryGet(mapId, out _) && MapInstance.MapNotRequested(mapId)
)
.ToArray();
if (validMapIds.Length < 1)
Expand All @@ -45,7 +45,7 @@ public static void SendNeedMap(params Guid[] mapIds)
MapInstance.UpdateMapRequestTime(validMapIds);
}

public static void SendNeedMapForGrid(MapInstance? mapInstance)
public static void SendNeedMapForGrid(MapInstance? mapInstance = default)
{
if (mapInstance == default && !MapInstance.TryGet(Globals.Me.MapId, out mapInstance))
{
Expand All @@ -56,18 +56,26 @@ public static void SendNeedMapForGrid(MapInstance? mapInstance)
var gridY = mapInstance.GridY;
var minX = Math.Max(0, gridX - 1);
var minY = Math.Max(0, gridY - 1);
var maxX = Math.Max(Globals.MapGridWidth, gridX + 2);
var maxY = Math.Max(Globals.MapGridHeight, gridY + 2);
var maxX = Math.Min(Globals.MapGridWidth, gridX + 2);
var maxY = Math.Min(Globals.MapGridHeight, gridY + 2);

List<Guid> idsToFetch = new(9);

for (int x = minX; x < maxX; x++)
{
for (int y = minY; y < maxY; y++)
{
if (Globals.MapGrid[x, y] != Guid.Empty)
var mapId = Globals.MapGrid[x, y];
if (mapId == Guid.Empty)
{
PacketSender.SendNeedMap(Globals.MapGrid[x, y]);
continue;
}

idsToFetch.Add(mapId);
}
}

SendNeedMap(idsToFetch.ToArray());
}

public static void SendMove()
Expand Down
56 changes: 29 additions & 27 deletions Intersect.Server.Core/Database/DbInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,38 +1565,34 @@ public static void GenerateMapGrids()
mapGrids.Clear();
foreach (var map in MapController.Lookup.Values)
{
if (mapGrids.Count == 0)
if (mapGrids.Count < 1)
{
mapGrids.Add(new MapGrid(map.Id, 0));
continue;
}
else

for (var y = 0; y < mapGrids.Count; y++)
{
for (var y = 0; y < mapGrids.Count; y++)
if (!mapGrids[y].Contains(map.Id))
{
if (!mapGrids[y].HasMap(map.Id))
if (y != mapGrids.Count - 1)
{
if (y != mapGrids.Count - 1)
{
continue;
}

mapGrids.Add(new MapGrid(map.Id, mapGrids.Count));

break;
}
else
{
break;
continue;
}

mapGrids.Add(new MapGrid(map.Id, mapGrids.Count));
}

break;
}
}

foreach (MapController map in MapController.Lookup.Values)
{
lock (map.GetMapLock())
{
var myGrid = map.MapGrid;
var gridIndex = map.MapGrid;
var grid = mapGrids[gridIndex];
var surroundingMapIds = new List<Guid>();
var surroundingMaps = new List<MapController>();
for (var x = map.MapGridX - 1; x <= map.MapGridX + 1; x++)
Expand All @@ -1608,15 +1604,21 @@ public static void GenerateMapGrids()
continue;
}

if (x >= mapGrids[myGrid].XMin &&
x < mapGrids[myGrid].XMax &&
y >= mapGrids[myGrid].YMin &&
y < mapGrids[myGrid].YMax &&
mapGrids[myGrid].MyGrid[x, y] != Guid.Empty)
if (x < grid.XMin || x >= grid.XMax || y < grid.YMin || y >= grid.YMax)
{
continue;
}

if (grid.MapIdGrid[x, y] == Guid.Empty)
{
continue;
}

surroundingMapIds.Add(mapGrids[myGrid].MyGrid[x, y]);
surroundingMaps.Add(MapController.Get(mapGrids[myGrid].MyGrid[x, y]));
var idFromGrid = grid.MapIdGrid[x, y];
surroundingMapIds.Add(idFromGrid);
if (MapController.TryGet(idFromGrid, out var mapOnGrid))
{
surroundingMaps.Add(mapOnGrid);
}
}
}
Expand All @@ -1625,9 +1627,9 @@ public static void GenerateMapGrids()
}
}

for (var i = 0; i < mapGrids.Count; i++)
for (var gridIndex = 0; gridIndex < mapGrids.Count; gridIndex++)
{
PacketSender.SendMapGridToAll(i);
PacketSender.SendMapGridToAll(gridIndex);
}
}
}
Expand All @@ -1644,7 +1646,7 @@ public static bool GridsContain(Guid id)
{
lock (mapGrids)
{
return mapGrids.Any(g => g.HasMap(id));
return mapGrids.Any(mapGrid => mapGrid.Contains(id));
}
}

Expand Down
4 changes: 2 additions & 2 deletions Intersect.Server.Core/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,8 @@ public Direction GetDirectionTo(Entity target)
continue;
}

if (grid.MyGrid[x, y] != Guid.Empty &&
grid.MyGrid[x, y] == target.MapId)
if (grid.MapIdGrid[x, y] != Guid.Empty &&
grid.MapIdGrid[x, y] == target.MapId)
{
xDiff = (x - map.MapGridX) * Options.MapWidth + target.X - X;
yDiff = (y - map.MapGridY) * Options.MapHeight + target.Y - Y;
Expand Down
6 changes: 3 additions & 3 deletions Intersect.Server.Core/Entities/Pathfinding/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public PathfinderResult Update(long timeMs)
continue;
}

if (grid.MyGrid[x, y] != Guid.Empty)
if (grid.MapIdGrid[x, y] != Guid.Empty)
{
if (grid.MyGrid[x, y] == mTarget.TargetMapId)
if (grid.MapIdGrid[x, y] == mTarget.TargetMapId)
{
targetX = (x - gridX + 1) * Options.MapWidth + mTarget.TargetX;
targetY = (y - gridY + 1) * Options.MapHeight + mTarget.TargetY;
Expand Down Expand Up @@ -179,7 +179,7 @@ public PathfinderResult Update(long timeMs)
continue;
}

if (MapController.TryGetInstanceFromMap(grid.MyGrid[x, y], mEntity.MapInstanceId, out var instance))
if (MapController.TryGetInstanceFromMap(grid.MapIdGrid[x, y], mEntity.MapInstanceId, out var instance))
{
//Copy the cached array of tile blocks

Expand Down
Loading

0 comments on commit 95f14b9

Please sign in to comment.