]> Dogcows Code - chaz/carfire/commitdiff
Fixed last synchronization problem (I hope).
authorCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Tue, 30 Mar 2010 23:17:45 +0000 (23:17 +0000)
committerCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Tue, 30 Mar 2010 23:17:45 +0000 (23:17 +0000)
git-svn-id: https://bd85.net/svn/cs3505_group@40 92bb83a3-7c8f-8a45-bc97-515c4e399668

Project06/CS 3505 Project 06/CS 3505 Project 06/CS 3505/TestHarness.cs
Project06/CS 3505 Project 06/CS 3505 Project 06/NetworkGame.cs

index b4860a475d6a08d144056c9fbdc6fe8684ac92b9..883e8a09676d15b86615d228fa0a7c12042118af 100644 (file)
@@ -294,10 +294,6 @@ namespace CS_3505_Project_06.CS_3505
 \r
         public long Update(TimeSpan elapsedTime)\r
         {\r
-            // Draw() can be called multiple times between updates, so moved this here to avoid\r
-            // duplicate lines.\r
-            Console.WriteLine("Frame: " + state.frameNumber + " Checksum: " + state.Checksum);\r
-\r
             state.advanceFrame(inputs, elapsedTime.Milliseconds);  // Apply the inputs, advance game state.\r
 \r
             inputs = new NextInputs();  // Start with inputs cleared on the next frame.\r
index c69e2fb71fc6f2beb58cf66f0baed10eec3b2535..bc0617cc7ed9838b7cb952f7c67ee1f644092b5b 100644 (file)
@@ -1,4 +1,7 @@
-using System;\r
+\r
+#undef DEBUG\r
+\r
+using System;\r
 using System.Collections.Generic;\r
 using System.Linq;\r
 using System.Text;\r
@@ -297,17 +300,25 @@ namespace CS_3505_Project_06
                         mLocalEvents.AddRange(GetEventsFromInput());\r
                         SendLocalEvents();\r
                         ApplyEvents();\r
+\r
+#if DEBUG\r
+                        Console.WriteLine("HASH: " + mGame.CurrentFrameNumber + "\t" + mGame.CurrentChecksum);\r
+#endif\r
+\r
                         mGame.Update(mTargetTimeSpan);\r
                     }\r
                     else // Stall!\r
                     {\r
                         if (mStallCount == 0)\r
                         {\r
-                            Console.WriteLine("===== STALL =====");\r
+#if DEBUG\r
+                            Console.WriteLine("STAL: ====");\r
+#endif\r
                         }\r
                         else if (mStallCount % 60 == 0)\r
                         {\r
-                            Console.WriteLine("Stalled for " + mStallCount + " frames.");\r
+                            // DEBUG\r
+                            //Console.WriteLine("Stalled for " + mStallCount + " frames.");\r
                         }\r
 \r
                         mStallCount++;\r
@@ -439,8 +450,9 @@ namespace CS_3505_Project_06
         int mLastStallCount;\r
         int mAverageOwd;\r
 \r
-        // DEBUG\r
+#if DEBUG\r
         bool mDontSendEvents;\r
+#endif\r
 \r
         TimeSpan mTargetTimeSpan = new TimeSpan(166666);\r
         public TimeSpan TargetTimeSpan\r
@@ -575,7 +587,7 @@ namespace CS_3505_Project_06
         class GamerInfo\r
         {\r
             public NetworkGamer Gamer;\r
-            public long HighestFrameNumber = -1;\r
+            public long HighestFrameNumber = 0;\r
             public int StallCount = 0;\r
             public int AverageOwd = 0;\r
             public int NextStallCount = 0;\r
@@ -604,7 +616,7 @@ namespace CS_3505_Project_06
         void Reset()\r
         {\r
             mLatency = 1;\r
-            mHighestFrameNumber = -1;\r
+            mHighestFrameNumber = 0;\r
             mNextLatencyAdjustmentFrame = 1;\r
             mStallCount = 0;\r
             mLastStallCount = 0;\r
@@ -650,12 +662,31 @@ namespace CS_3505_Project_06
                         int frameNumber = mPacketReader.ReadInt32();\r
                         int numEvents = mPacketReader.ReadByte();\r
 \r
+                        if (frameNumber <= mNextLatencyAdjustmentFrame)\r
+                        {\r
+                            senderInfo.StallCount = stallCount;\r
+                            senderInfo.AverageOwd = averageOwd;\r
+                        }\r
+                        else\r
+                        {\r
+                            senderInfo.NextStallCount = stallCount;\r
+                            senderInfo.NextAverageOwd = averageOwd;\r
+                        }\r
+\r
                         if (frameNumber <= senderInfo.HighestFrameNumber)\r
                         {\r
+#if DEBUG\r
+                            Console.WriteLine("SKP" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t<=\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents);\r
+#endif\r
+\r
                             // we know about all these events, so don't bother reading them\r
                             break;\r
                         }\r
 \r
+#if DEBUG\r
+                        Console.WriteLine(" GOT" + (char)sender.Id + ": " + mGame.CurrentFrameNumber + "\t" + frameNumber + "\t>\t" + senderInfo.HighestFrameNumber + "\t#" + numEvents);\r
+#endif\r
+\r
                         for (int i = 0; i < numEvents; i++)\r
                         {\r
                             EventInfo eventInfo = ReadEvent(mPacketReader, sender);\r
@@ -668,16 +699,6 @@ namespace CS_3505_Project_06
                             }\r
                         }\r
 \r
-                        if (frameNumber <= mNextLatencyAdjustmentFrame)\r
-                        {\r
-                            senderInfo.StallCount = stallCount;\r
-                            senderInfo.AverageOwd = averageOwd;\r
-                        }\r
-                        else\r
-                        {\r
-                            senderInfo.NextStallCount = stallCount;\r
-                            senderInfo.NextAverageOwd = averageOwd;\r
-                        }\r
                         senderInfo.HighestFrameNumber = frameNumber;\r
                         break;\r
 \r
@@ -756,12 +777,12 @@ namespace CS_3505_Project_06
             mPacketWriter.Write(message.ToCharArray());\r
         }\r
 \r
-        void WriteEventPacket(List<EventInfo> events)\r
+        void WriteEventPacket(List<EventInfo> events, long highestFrameNumber)\r
         {\r
             mPacketWriter.Write((byte)PacketType.Event);\r
             mPacketWriter.Write((short)mLastStallCount);\r
             mPacketWriter.Write((short)mAverageOwd);\r
-            mPacketWriter.Write((int)(mGame.CurrentFrameNumber + mLatency));\r
+            mPacketWriter.Write((int)highestFrameNumber);\r
             mPacketWriter.Write((byte)events.Count);\r
 \r
             foreach (EventInfo eventInfo in events)\r
@@ -806,6 +827,13 @@ namespace CS_3505_Project_06
         {\r
             Debug.Assert(IsLatencyAdjustmentFrame);\r
 \r
+#if DEBUG\r
+            if (mStallCount > 0)\r
+            {\r
+                Console.WriteLine("STL#: " + mGame.CurrentFrameNumber + "\t" + mStallCount);\r
+            }\r
+#endif\r
+\r
             int maxStallCount = 0;\r
             int maxAverageOwd = 0;\r
 \r
@@ -818,8 +846,9 @@ namespace CS_3505_Project_06
                 gamerInfo.AverageOwd = gamerInfo.NextAverageOwd;\r
             }\r
 \r
-            // DEBUG\r
+#if DEBUG\r
             int prevLatency = mLatency;\r
+#endif\r
 \r
             if (maxStallCount > 0)\r
             {\r
@@ -827,15 +856,16 @@ namespace CS_3505_Project_06
             }\r
             else\r
             {\r
-                mLatency = (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0);\r
+                mLatency -= (int)(0.6 * (double)(mLatency - maxAverageOwd) + 1.0);\r
             }\r
 \r
-            // DEBUG OUTPUT\r
-            if (prevLatency != mLatency) Console.WriteLine("Latency readjusted to " + mLatency);\r
-\r
             if (mLatency < 1) mLatency = 1;\r
             if (mLatency > MaximumLatency) mLatency = MaximumLatency;\r
 \r
+#if DEBUG\r
+            if (prevLatency != mLatency) Console.WriteLine("NLAG: " + mLatency);\r
+#endif\r
+\r
             mNextLatencyAdjustmentFrame = mGame.CurrentFrameNumber + mLatency;\r
             mAverageOwd = CurrentAverageOneWayDelay;\r
 \r
@@ -878,9 +908,10 @@ namespace CS_3505_Project_06
                 events.Add(new KeyboardEventInfo(LocalGamer, frameOfApplication, key, false));\r
             }\r
 \r
-            // DEBUG\r
+#if DEBUG\r
             if (pressedKeys.Contains(Keys.Escape)) mDontSendEvents = true;\r
             if (releasedKeys.Contains(Keys.Escape)) mDontSendEvents = false;\r
+#endif\r
 \r
             // 2. Find the mouse differences.\r
 \r
@@ -938,20 +969,22 @@ namespace CS_3505_Project_06
 \r
         void SendLocalEvents(NetworkGamer recipient)\r
         {\r
-            // DEBUG\r
+#if DEBUG\r
             if (mDontSendEvents) return;\r
+#endif\r
 \r
             List<EventInfo> events = new List<EventInfo>(mLocalEvents);\r
             events.AddRange(mLastLocalEvents);\r
 \r
-            WriteEventPacket(events);\r
-\r
             if (recipient != null && !recipient.IsDisposed)\r
             {\r
+                // if there is a recipient, we are resending old events\r
+                WriteEventPacket(events, mGame.CurrentFrameNumber - 1);\r
                 LocalGamer.SendData(mPacketWriter, SendDataOptions.Reliable, recipient);\r
             }\r
             else\r
             {\r
+                WriteEventPacket(events, mGame.CurrentFrameNumber + mLatency);\r
                 LocalGamer.SendData(mPacketWriter, SendDataOptions.None);\r
             }\r
         }\r
@@ -985,7 +1018,10 @@ namespace CS_3505_Project_06
                     KeyboardEventInfo keyboardEventInfo = eventInfo as KeyboardEventInfo;\r
                     if (keyboardEventInfo != null)\r
                     {\r
-                        Console.WriteLine(keyboardEventInfo.FrameOfApplication + " KEY: " + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);\r
+#if DEBUG\r
+                        Console.WriteLine(" KEY: " + keyboardEventInfo.FrameOfApplication + "\t" + keyboardEventInfo.Key + "," + keyboardEventInfo.IsKeyDown);\r
+#endif\r
+\r
                         mGame.ApplyKeyInput(gamerInfo, keyboardEventInfo.Key, keyboardEventInfo.IsKeyDown);\r
                         continue;\r
                     }\r
@@ -993,16 +1029,22 @@ namespace CS_3505_Project_06
                     MouseButtonEventInfo mouseButtonEventInfo = eventInfo as MouseButtonEventInfo;\r
                     if (mouseButtonEventInfo != null)\r
                     {\r
+#if DEBUG\r
+                        Console.WriteLine(" BTN: " + mouseButtonEventInfo.FrameOfApplication + "\t" + mouseButtonEventInfo.IsButtonDown);\r
+#endif\r
+\r
                         mGame.ApplyMouseButtonInput(gamerInfo, mouseButtonEventInfo.IsButtonDown);\r
-                        Console.WriteLine(mouseButtonEventInfo.FrameOfApplication + " BTN: " + mouseButtonEventInfo.IsButtonDown);\r
                         continue;\r
                     }\r
 \r
                     MouseMotionEventInfo mouseMotionEventInfo = eventInfo as MouseMotionEventInfo;\r
                     if (mouseMotionEventInfo != null)\r
                     {\r
+#if DEBUG\r
+                        Console.WriteLine(" MMV: " + mouseMotionEventInfo.FrameOfApplication + "\t" + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);\r
+#endif\r
+\r
                         mGame.ApplyMouseLocationInput(gamerInfo, mouseMotionEventInfo.X, mouseMotionEventInfo.Y);\r
-                        Console.WriteLine(mouseMotionEventInfo.FrameOfApplication + " MMV: " + mouseMotionEventInfo.X + "," + mouseMotionEventInfo.Y);\r
                         continue;\r
                     }\r
                 }\r
This page took 0.030496 seconds and 4 git commands to generate.