Make syncing go brr

This commit is contained in:
Sofia 2020-08-08 01:21:02 +03:00
parent 1f001c03b7
commit 3b2c3adf35
6 changed files with 23 additions and 13 deletions

View File

@ -81,7 +81,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
Players[jump.PlayerId].Controlled.Jump();
} else if (packet is PlayerSyncPacket) {
PlayerSyncPacket syncPckt = (PlayerSyncPacket)packet;
Players[syncPckt.PlayerId].Controlled.ProcessSyncPacket(syncPckt);
if (syncPckt.Unsynced || syncPckt.PlayerId != LocalPlayer.Id) {
Players[syncPckt.PlayerId].Controlled.ProcessSyncPacket(syncPckt);
}
}
}

View File

@ -92,7 +92,9 @@ namespace NeonTea.Quakeball.Networking.Instances {
PlayerSyncPacket syncPckt = (PlayerSyncPacket)packet;
if (Players[conn.uid].Controlled != null) {
syncPckt.PlayerId = conn.uid;
Players[conn.uid].Controlled.ProcessSyncPacket(syncPckt);
if (!Players[conn.uid].Controlled.ProcessSyncPacket(syncPckt)) {
Players[conn.uid].Unsynced = true;
}
}
}
}

View File

@ -5,6 +5,7 @@ namespace NeonTea.Quakeball.Networking {
public class NetPlayer {
public ulong Id;
public Player Controlled;
public bool Unsynced = false;
public NetPlayer(ulong id, Player obj = null) {
Id = id;

View File

@ -39,7 +39,7 @@ namespace NeonTea.Quakeball.Networking.Packets {
public class MultipleSyncsPckt : Packet {
public List<PlayerSyncPacket> Updates = new List<PlayerSyncPacket>();
public List<PlayerSyncPacket> Syncs = new List<PlayerSyncPacket>();
public MultipleSyncsPckt() { }
@ -48,23 +48,23 @@ namespace NeonTea.Quakeball.Networking.Packets {
if (p.Controlled == null) {
continue;
}
//Updates.Add(p.Controlled.CreatePacket(p.Id));
Syncs.Add(p.Controlled.CreateSyncPacket(p.Id, p.Unsynced));
}
}
public override void Read(ByteBuffer buffer) {
Updates.Clear();
Syncs.Clear();
int count = buffer.ReadInt();
for (int i = 0; i < count; i++) {
PlayerSyncPacket pckt = new PlayerSyncPacket();
pckt.Read(buffer);
Updates.Add(pckt);
Syncs.Add(pckt);
}
}
public override void Write(ByteBuffer buffer) {
buffer.Write(Updates.Count);
foreach (PlayerSyncPacket p in Updates) {
buffer.Write(Syncs.Count);
foreach (PlayerSyncPacket p in Syncs) {
p.Write(buffer);
}
}

View File

@ -55,25 +55,29 @@ namespace NeonTea.Quakeball.Networking.Packets {
public class PlayerSyncPacket : Packet {
public ulong PlayerId;
public bool Unsynced;
public Vector3 Location;
public Vector3 GroundVelocity;
public PlayerSyncPacket() { }
public PlayerSyncPacket(ulong id, Vector3 location, Vector3 groundVelocity) {
public PlayerSyncPacket(ulong id, bool unsynced, Vector3 location, Vector3 groundVelocity) {
PlayerId = id;
Unsynced = unsynced;
Location = location;
GroundVelocity = groundVelocity;
}
public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong();
Unsynced = buffer.ReadBool();
Location = buffer.ReadVector3();
GroundVelocity = buffer.ReadVector3();
}
public override void Write(ByteBuffer buffer) {
buffer.ReadULong();
buffer.Write(PlayerId);
buffer.Write(Unsynced);
buffer.Write(Location);
buffer.Write(GroundVelocity);
}

View File

@ -75,12 +75,12 @@ namespace NeonTea.Quakeball.Players {
}
/// <summary>Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server.</summary>
public PlayerSyncPacket CreateSyncPacket(ulong id = 0) {
return new PlayerSyncPacket(id, transform.position, GroundVelocity);
public PlayerSyncPacket CreateSyncPacket(ulong id = 0, bool unsynced = false) {
return new PlayerSyncPacket(id, unsynced, transform.position, GroundVelocity);
}
/// <summary>Applies the sync packet, checking it for cheatiness if shouldApply is false.</summary>
public void ProcessSyncPacket(PlayerSyncPacket syncPckt, bool shouldApplyWithoutInspection = true) {
public bool ProcessSyncPacket(PlayerSyncPacket syncPckt, bool shouldApplyWithoutInspection = true) {
bool ShouldApply = shouldApplyWithoutInspection;
if (!shouldApplyWithoutInspection) {
// TODO: Gaze into the crystal ball to determine the nefariousness level of the packet and update ShouldApply accordingly.
@ -89,6 +89,7 @@ namespace NeonTea.Quakeball.Players {
transform.position = syncPckt.Location;
GroundVelocity = syncPckt.Location;
}
return ShouldApply;
}
/// <summary>The normal of the ground below the player. If there is no ground, it's <c>Vector3.up</c> by default.</summary>