X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=Project06%2FCS%203505%20Project%2006%2FCS%203505%20Project%2006%2FNetworkGame.cs;h=869812e58ad6147528dfc35b5770d72b38625c23;hb=2069ccccbb9dc0007f36555862615e684e93a8b2;hp=c69e2fb71fc6f2beb58cf66f0baed10eec3b2535;hpb=d4d977b0086ecf7ac950863cb0ecdd7e42804c43;p=chaz%2Fcarfire diff --git a/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs index c69e2fb..869812e 100644 --- a/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs +++ b/Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs @@ -1,4 +1,9 @@ -using System; + +// Make sure DEBUG is undefined when turning in the project +// or the grader will wonder why it's so laggy. +#undef DEBUG + +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -279,12 +284,17 @@ namespace CS_3505_Project_06 } else if (mNetworkSession.SessionState == NetworkSessionState.Playing) { - if (mGame.IsGameOver(LocalGamerInfo) || mGame.IsTerminated(LocalGamerInfo)) + if (mGame.IsTerminated(LocalGamerInfo)) { - // TODO: Should support moving back to the session lobby. LeaveSession(); return; } + else if (mGame.IsGameOver(LocalGamerInfo)) + { + ApplyEvents(LocalGamerInfo, GetEventsFromInput()); + mGame.Update(mTargetTimeSpan); + return; + } if (HaveNeededEvents) { @@ -297,24 +307,24 @@ namespace CS_3505_Project_06 mLocalEvents.AddRange(GetEventsFromInput()); SendLocalEvents(); ApplyEvents(); + +#if DEBUG + Console.WriteLine("HASH: " + mGame.CurrentFrameNumber + "\t" + mGame.CurrentChecksum); +#endif + mGame.Update(mTargetTimeSpan); } else // Stall! { - if (mStallCount == 0) - { - Console.WriteLine("===== STALL ====="); - } - else if (mStallCount % 60 == 0) - { - Console.WriteLine("Stalled for " + mStallCount + " frames."); - } - mStallCount++; // Send a reliable event packet to each stalled gamer. if (mStallCount == 1) { +#if DEBUG + Console.WriteLine("STAL: ===="); +#endif + foreach (GamerInfo gamerInfo in GamerArray) { if (gamerInfo.HighestFrameNumber < mGame.CurrentFrameNumber) @@ -323,19 +333,11 @@ namespace CS_3505_Project_06 } } } - - /*if (mStallCount > StallTimeout) - { - DropLostGamers(); - mStallCount = 0; - } - else if (mStallCount == 1) + else if (mStallCount > 600) { - SendLocalEvents + Console.WriteLine("One or more players have stalled excessively. Leaving session..."); + LeaveSession(); } - else if (mStallCount % 60 == 0) - { - } TODO */ } } } @@ -439,8 +441,9 @@ namespace CS_3505_Project_06 int mLastStallCount; int mAverageOwd; - // DEBUG +#if DEBUG bool mDontSendEvents; +#endif TimeSpan mTargetTimeSpan = new TimeSpan(166666); public TimeSpan TargetTimeSpan @@ -481,8 +484,7 @@ namespace CS_3505_Project_06 enum PacketType { Chat = 1, - Event = 2, - Stall = 3 + Event = 2 } enum EventType @@ -575,7 +577,7 @@ namespace CS_3505_Project_06 class GamerInfo { public NetworkGamer Gamer; - public long HighestFrameNumber = -1; + public long HighestFrameNumber = 0; public int StallCount = 0; public int AverageOwd = 0; public int NextStallCount = 0; @@ -604,7 +606,7 @@ namespace CS_3505_Project_06 void Reset() { mLatency = 1; - mHighestFrameNumber = -1; + mHighestFrameNumber = 0; mNextLatencyAdjustmentFrame = 1; mStallCount = 0; mLastStallCount = 0; @@ -629,7 +631,6 @@ namespace CS_3505_Project_06 NetworkGamer sender; localGamer.ReceiveData(mPacketReader, out sender); - GamerInfo senderInfo = mGamers[sender.Id]; PacketType packetId = (PacketType)mPacketReader.ReadByte(); switch (packetId) @@ -645,17 +646,38 @@ namespace CS_3505_Project_06 case PacketType.Event: + GamerInfo senderInfo = mGamers[sender.Id]; + int stallCount = mPacketReader.ReadInt16(); int averageOwd = mPacketReader.ReadInt16(); int frameNumber = mPacketReader.ReadInt32(); int numEvents = mPacketReader.ReadByte(); + if (frameNumber <= mNextLatencyAdjustmentFrame) + { + senderInfo.StallCount = stallCount; + senderInfo.AverageOwd = averageOwd; + } + else + { + senderInfo.NextStallCount = stallCount; + senderInfo.NextAverageOwd = averageOwd; + } + if (frameNumber <= senderInfo.HighestFrameNumber) { +#if DEBUG + Console.WriteLine("SKP" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t<=\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents); +#endif + // we know about all these events, so don't bother reading them break; } +#if DEBUG + Console.WriteLine(" GOT" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t>\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents); +#endif + for (int i = 0; i < numEvents; i++) { EventInfo eventInfo = ReadEvent(mPacketReader, sender); @@ -668,27 +690,9 @@ namespace CS_3505_Project_06 } } - if (frameNumber <= mNextLatencyAdjustmentFrame) - { - senderInfo.StallCount = stallCount; - senderInfo.AverageOwd = averageOwd; - } - else - { - senderInfo.NextStallCount = stallCount; - senderInfo.NextAverageOwd = averageOwd; - } senderInfo.HighestFrameNumber = frameNumber; break; - case PacketType.Stall: - - byte numStalledPeers = mPacketReader.ReadByte(); - byte[] stalledPeers = mPacketReader.ReadBytes(numStalledPeers); - - // TODO - break; - default: Console.WriteLine("Received unknown packet type: " + (int)packetId); @@ -756,12 +760,12 @@ namespace CS_3505_Project_06 mPacketWriter.Write(message.ToCharArray()); } - void WriteEventPacket(List events) + void WriteEventPacket(List events, long highestFrameNumber) { mPacketWriter.Write((byte)PacketType.Event); mPacketWriter.Write((short)mLastStallCount); mPacketWriter.Write((short)mAverageOwd); - mPacketWriter.Write((int)(mGame.CurrentFrameNumber + mLatency)); + mPacketWriter.Write((int)highestFrameNumber); mPacketWriter.Write((byte)events.Count); foreach (EventInfo eventInfo in events) @@ -806,6 +810,13 @@ namespace CS_3505_Project_06 { Debug.Assert(IsLatencyAdjustmentFrame); +#if DEBUG + if (mStallCount > 0) + { + Console.WriteLine("STL#: " + mGame.CurrentFrameNumber + "\t" + mStallCount); + } +#endif + int maxStallCount = 0; int maxAverageOwd = 0; @@ -818,8 +829,9 @@ namespace CS_3505_Project_06 gamerInfo.AverageOwd = gamerInfo.NextAverageOwd; } - // DEBUG +#if DEBUG int prevLatency = mLatency; +#endif if (maxStallCount > 0) { @@ -827,15 +839,16 @@ namespace CS_3505_Project_06 } else { - mLatency = (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0); + mLatency -= (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0); } - // DEBUG OUTPUT - if (prevLatency != mLatency) Console.WriteLine("Latency readjusted to " + mLatency); - if (mLatency < 1) mLatency = 1; if (mLatency > MaximumLatency) mLatency = MaximumLatency; +#if DEBUG + if (prevLatency != mLatency) Console.WriteLine("NLAG: " + mLatency); +#endif + mNextLatencyAdjustmentFrame = mGame.CurrentFrameNumber + mLatency; mAverageOwd = CurrentAverageOneWayDelay; @@ -878,9 +891,10 @@ namespace CS_3505_Project_06 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, false)); } - // DEBUG +#if DEBUG if (pressedKeys.Contains(Keys.Escape)) mDontSendEvents = true; if (releasedKeys.Contains(Keys.Escape)) mDontSendEvents = false; +#endif // 2. Find the mouse differences. @@ -938,20 +952,22 @@ namespace CS_3505_Project_06 void SendLocalEvents(NetworkGamer recipient) { - // DEBUG +#if DEBUG if (mDontSendEvents) return; +#endif List events = new List(mLocalEvents); events.AddRange(mLastLocalEvents); - WriteEventPacket(events); - if (recipient != null && !recipient.IsDisposed) { + // if there is a recipient, we are resending old events + WriteEventPacket(events, mGame.CurrentFrameNumber - 1); LocalGamer.SendData(mPacketWriter, SendDataOptions.Reliable, recipient); } else { + WriteEventPacket(events, mGame.CurrentFrameNumber + mLatency); LocalGamer.SendData(mPacketWriter, SendDataOptions.None); } } @@ -965,6 +981,7 @@ namespace CS_3505_Project_06 foreach (GamerInfo gamerInfo in mGamers.Values) { + if (mGame.IsGameOver(gamerInfo)) continue; if (gamerInfo.HighestFrameNumber < currentFrame) return false; } @@ -979,35 +996,47 @@ namespace CS_3505_Project_06 foreach (GamerInfo gamerInfo in GamerArray) { if (gamerInfo.Events[index] == null) continue; + ApplyEvents(gamerInfo, gamerInfo.Events[index]); + gamerInfo.Events[index] = null; + } + } - foreach (EventInfo eventInfo in gamerInfo.Events[index]) + void ApplyEvents(GamerInfo gamerInfo, List events) + { + foreach (EventInfo eventInfo in events) + { + KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo; + if (keyboardEventInfo != null) { - KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo; - if (keyboardEventInfo != null) - { - Console.WriteLine(keyboardEventInfo.FrameOfApplication + " KEY: " + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown); - mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown); - continue; - } +#if DEBUG + Console.WriteLine(" KEY: " + keyboardEventInfo.FrameOfApplication + "\t" + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown); +#endif - MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo; - if (mouseButtonEventInfo != null) - { - mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown); - Console.WriteLine(mouseButtonEventInfo.FrameOfApplication + " BTN: " + mouseButtonEventInfo.IsButtonDown); - continue; - } + mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown); + continue; + } - MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo; - if (mouseMotionEventInfo != null) - { - mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y); - Console.WriteLine(mouseMotionEventInfo.FrameOfApplication + " MMV: " + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y); - continue; - } + MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo; + if (mouseButtonEventInfo != null) + { +#if DEBUG + Console.WriteLine(" BTN: " + mouseButtonEventInfo.FrameOfApplication + "\t" + mouseButtonEventInfo.IsButtonDown); +#endif + + mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown); + continue; } - gamerInfo.Events[index] = null; + MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo; + if (mouseMotionEventInfo != null) + { +#if DEBUG + Console.WriteLine(" MMV: " + mouseMotionEventInfo.FrameOfApplication + "\t" + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y); +#endif + + mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y); + continue; + } } }