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(); Players[jump.PlayerId].Controlled.Jump();
} else if (packet is PlayerSyncPacket) { } else if (packet is PlayerSyncPacket) {
PlayerSyncPacket syncPckt = (PlayerSyncPacket)packet; 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; PlayerSyncPacket syncPckt = (PlayerSyncPacket)packet;
if (Players[conn.uid].Controlled != null) { if (Players[conn.uid].Controlled != null) {
syncPckt.PlayerId = conn.uid; 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 class NetPlayer {
public ulong Id; public ulong Id;
public Player Controlled; public Player Controlled;
public bool Unsynced = false;
public NetPlayer(ulong id, Player obj = null) { public NetPlayer(ulong id, Player obj = null) {
Id = id; Id = id;

View File

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

View File

@ -55,25 +55,29 @@ namespace NeonTea.Quakeball.Networking.Packets {
public class PlayerSyncPacket : Packet { public class PlayerSyncPacket : Packet {
public ulong PlayerId; public ulong PlayerId;
public bool Unsynced;
public Vector3 Location; public Vector3 Location;
public Vector3 GroundVelocity; public Vector3 GroundVelocity;
public PlayerSyncPacket() { } public PlayerSyncPacket() { }
public PlayerSyncPacket(ulong id, Vector3 location, Vector3 groundVelocity) { public PlayerSyncPacket(ulong id, bool unsynced, Vector3 location, Vector3 groundVelocity) {
PlayerId = id; PlayerId = id;
Unsynced = unsynced;
Location = location; Location = location;
GroundVelocity = groundVelocity; GroundVelocity = groundVelocity;
} }
public override void Read(ByteBuffer buffer) { public override void Read(ByteBuffer buffer) {
PlayerId = buffer.ReadULong(); PlayerId = buffer.ReadULong();
Unsynced = buffer.ReadBool();
Location = buffer.ReadVector3(); Location = buffer.ReadVector3();
GroundVelocity = buffer.ReadVector3(); GroundVelocity = buffer.ReadVector3();
} }
public override void Write(ByteBuffer buffer) { public override void Write(ByteBuffer buffer) {
buffer.ReadULong(); buffer.Write(PlayerId);
buffer.Write(Unsynced);
buffer.Write(Location); buffer.Write(Location);
buffer.Write(GroundVelocity); 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> /// <summary>Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server.</summary>
public PlayerSyncPacket CreateSyncPacket(ulong id = 0) { public PlayerSyncPacket CreateSyncPacket(ulong id = 0, bool unsynced = false) {
return new PlayerSyncPacket(id, transform.position, GroundVelocity); return new PlayerSyncPacket(id, unsynced, transform.position, GroundVelocity);
} }
/// <summary>Applies the sync packet, checking it for cheatiness if shouldApply is false.</summary> /// <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; bool ShouldApply = shouldApplyWithoutInspection;
if (!shouldApplyWithoutInspection) { if (!shouldApplyWithoutInspection) {
// TODO: Gaze into the crystal ball to determine the nefariousness level of the packet and update ShouldApply accordingly. // 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; transform.position = syncPckt.Location;
GroundVelocity = 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> /// <summary>The normal of the ground below the player. If there is no ground, it's <c>Vector3.up</c> by default.</summary>