Basic player input syncing

There's currently a bug where the inputs might be read out of order or more than once, which leads to desync. I'll feel really dumb when I find what's going on here.

Also, I'm using placeholder player spawning because CharacterInfo doesn't seem to be fully functional yet.
This commit is contained in:
juanjp600
2016-09-22 21:20:46 -03:00
parent edab86f730
commit 44e12ffed2
6 changed files with 300 additions and 36 deletions

View File

@@ -485,6 +485,10 @@ namespace Barotrauma.Networking
{
SendLobbyUpdate();
}
else
{
SendIngameUpdate();
}
// Update current time
updateTimer = DateTime.Now + updateInterval;
@@ -513,6 +517,9 @@ namespace Barotrauma.Networking
case ServerPacketHeader.UPDATE_LOBBY:
ReadLobbyUpdate(inc);
break;
case ServerPacketHeader.UPDATE_INGAME:
ReadIngameUpdate(inc);
break;
case ServerPacketHeader.QUERY_STARTGAME:
string subName = inc.ReadString();
string subHash = inc.ReadString();
@@ -560,6 +567,9 @@ namespace Barotrauma.Networking
bool respawnAllowed = inc.ReadBoolean();
float posX = inc.ReadFloat();
float posY = inc.ReadFloat();
GameModePreset gameMode = GameModePreset.list.Find(gm => gm.Name == modeName);
if (gameMode == null)
@@ -591,6 +601,10 @@ namespace Barotrauma.Networking
GameMain.GameScreen.Select();
DebugConsole.NewMessage(Convert.ToString(posX) + "," + Convert.ToString(posY), Color.Lime);
Character myChar = Character.Create(Character.HumanConfigFile, new Vector2(posX, posY), null, true, false);
Character.Controlled = myChar;
yield return CoroutineStatus.Success;
}
@@ -661,6 +675,32 @@ namespace Barotrauma.Networking
}
}
private void ReadIngameUpdate(NetIncomingMessage inc)
{
ServerNetObject objHeader;
while ((objHeader = (ServerNetObject)inc.ReadByte()) != ServerNetObject.END_OF_MESSAGE)
{
switch (objHeader)
{
case ServerNetObject.SYNC_IDS:
lastSentChatMsgID = inc.ReadUInt32();
break;
case ServerNetObject.CHARACTER_POSITION:
bool dead = inc.ReadBoolean();
inc.ReadPadBits();
if (Character.Controlled != null)
{
if (dead && !Character.Controlled.IsDead)
Character.Controlled.Kill(CauseOfDeath.Damage);
}
break;
case ServerNetObject.CHAT_MESSAGE:
ChatMessage.ClientRead(inc);
break;
}
}
}
private void SendLobbyUpdate()
{
NetOutgoingMessage outmsg = client.CreateMessage();
@@ -683,6 +723,34 @@ namespace Barotrauma.Networking
client.SendMessage(outmsg, NetDeliveryMethod.Unreliable);
}
private void SendIngameUpdate()
{
NetOutgoingMessage outmsg = client.CreateMessage();
outmsg.Write((byte)ClientPacketHeader.UPDATE_INGAME);
outmsg.Write((byte)ClientNetObject.SYNC_IDS);
outmsg.Write(GameMain.NetLobbyScreen.LastUpdateID);
outmsg.Write(ChatMessage.LastID);
ChatMessage removeMsg;
while ((removeMsg = chatMsgQueue.Find(cMsg => cMsg.NetStateID <= lastSentChatMsgID)) != null)
{
chatMsgQueue.Remove(removeMsg);
}
foreach (ChatMessage cMsg in chatMsgQueue)
{
cMsg.ClientWrite(outmsg);
}
if (Character.Controlled != null)
{
Character.Controlled.ClientWrite(outmsg);
}
outmsg.Write((byte)ClientNetObject.END_OF_MESSAGE);
client.SendMessage(outmsg, NetDeliveryMethod.Unreliable);
}
public override void SendChatMessage(string message, ChatMessageType? type = null)
{
if (client.ServerConnection == null) return;