Skip to content

Commit

Permalink
Add routing bucket detection
Browse files Browse the repository at this point in the history
Used state bag functionality to transfer routing bucket information to clients.

Now clients only update players that are in the same routing bucket.
  • Loading branch information
DefinitelyNotNoah committed Nov 12, 2023
1 parent e74d25c commit b88a650
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 17 additions & 2 deletions Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,28 @@ public void ReceivePlayerDataClientEvent(ExpandoObject playerData)
// Loop through all the positions that are being sent to the client every x amount of seconds.
foreach (var entry in playerData)
{
object[]? entryData = (object[])entry.Value;

if (Game.Player.State["bucket"] != Convert.ToInt32(entryData[4]))
{
// In the event that the player's routing bucket changes while they exist in _blipData,
// we'll search for them and remove them.
if (_blipData.TryGetValue(Convert.ToInt32(entry.Key), out BlipData player))
{
int coordBlip = player.CoordBlip;
Natives.RemoveBlip(ref coordBlip);
_blipData.Remove(Convert.ToInt32(entry.Key));
}
continue;
}

// If the current iterated player is equal to the client's handle, skip the current loop and continue.
// We will also ensure that we're only checking for players inside the client's routing bucket.
if (Convert.ToInt32(entry.Key) == Game.Player.ServerId)
{
continue;
}

object[]? entryData = (object[])entry.Value;

Vector3 position = (Vector3)entryData[0];
float heading = (float)entryData[1];
string name = (string)entryData[2];
Expand Down
4 changes: 4 additions & 0 deletions Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ private void SendPlayerData()
player.Character.Heading,
player.Name,
Natives.GetEntityModel(vehicle),
Natives.GetEntityRoutingBucket(player.Character.Handle)
};

// Update player state bags to make routing buckets known client-side.
player.State["bucket"] = Natives.GetEntityRoutingBucket(player.Character.Handle);
}
Events.TriggerAllClientsEvent("PlayerBlips-Client:ReceivePlayerData", playerData);
}
Expand Down

0 comments on commit b88a650

Please sign in to comment.