From 231d94aa17c7d959f251d62c4efe36b7c6a440a5 Mon Sep 17 00:00:00 2001
From: johns <>
Date: Thu, 28 Mar 2002 02:18:41 +0000
Subject: [PATCH] Renamed FrameCounter to GameCycle. Network delay debug added.

---
 src/ai/ai.cpp               |  4 ++--
 src/ai/old_ai.cpp           |  6 +++---
 src/game/trigger.cpp        |  4 ++--
 src/include/actions.h       |  2 +-
 src/include/ai.h            |  2 +-
 src/include/network.h       |  1 +
 src/include/player.h        |  4 ++--
 src/include/trigger.h       |  2 +-
 src/network/network.cpp     | 27 ++++++++++++++++++++++++---
 src/stratagus/mainloop.cpp  | 32 ++++++++++++++++++++++++++++++--
 src/stratagus/stratagus.cpp |  5 +++++
 src/ui/interface.cpp        |  2 +-
 12 files changed, 73 insertions(+), 18 deletions(-)

diff --git a/src/ai/ai.cpp b/src/ai/ai.cpp
index b06f59a49..4c267cd0f 100644
--- a/src/ai/ai.cpp
+++ b/src/ai/ai.cpp
@@ -809,11 +809,11 @@ global void AiResearchComplete(Unit* unit,const Upgrade* what)
 }
 
 /**
-**	This is called for each player, each frame.
+**	This is called for each player, each game cycle.
 **
 **	@param player	The player structure pointer.
 */
-global void AiEachFrame(Player* player __attribute__((unused)))
+global void AiEachCycle(Player* player __attribute__((unused)))
 {
 }
 
diff --git a/src/ai/old_ai.cpp b/src/ai/old_ai.cpp
index 309c735c0..30743254e 100644
--- a/src/ai/old_ai.cpp
+++ b/src/ai/old_ai.cpp
@@ -163,7 +163,7 @@ local AiCommand AiTable1[] = {
 
 /**
 **	Define the AI goals.
-**	All conditions the AI must check each frame.
+**	All conditions the AI must check each game cycle.
 */
 typedef struct _ai_goal_ AiGoal;
 
@@ -1564,11 +1564,11 @@ global void AiResearchComplete(Unit* unit,const Upgrade* what)
 }
 
 /**
-**      This is called for each player, each frame.
+**      This is called for each player, each game cycle.
 **
 **      @param player   The player structure pointer.
 */
-global void AiEachFrame(Player * player)
+global void AiEachCycle(Player * player)
 {
     AiCommand command;
 
diff --git a/src/game/trigger.cpp b/src/game/trigger.cpp
index 47a917342..45894934c 100644
--- a/src/game/trigger.cpp
+++ b/src/game/trigger.cpp
@@ -433,9 +433,9 @@ local SCM CclAddTrigger(SCM condition,SCM action)
 }
 
 /**
-**	Check trigger each frame.
+**	Check trigger each game cycle.
 */
-global void TriggersEachFrame(void)
+global void TriggersEachCycle(void)
 {
     SCM pair;
     SCM val;
diff --git a/src/include/actions.h b/src/include/actions.h
index bbb7abb38..da0b05420 100644
--- a/src/include/actions.h
+++ b/src/include/actions.h
@@ -174,7 +174,7 @@ extern void HandleActionSpellCast(Unit* unit);
 
     /// handle the animation of a unit
 extern int UnitShowAnimation(Unit* unit,const Animation* animation);
-    /// Handle the actions of all units each frame
+    /// Handle the actions of all units each game cycle
 extern void UnitActions(void);
 
 //@}
diff --git a/src/include/ai.h b/src/include/ai.h
index 54d44beb7..680c14012 100644
--- a/src/include/ai.h
+++ b/src/include/ai.h
@@ -46,7 +46,7 @@ extern int AiCostFactor;		/// Adjust the AI costs
 --	Functions
 ----------------------------------------------------------------------------*/
 
-extern void AiEachFrame(Player* player);/// Called each frame
+extern void AiEachCycle(Player* player);/// Called each game cycle
 extern void AiEachSecond(Player* player);/// Called each second
 
 extern void AiInit(Player* player);	/// Init AI for this player
diff --git a/src/include/network.h b/src/include/network.h
index cc086cbb0..6ad7fd37c 100644
--- a/src/include/network.h
+++ b/src/include/network.h
@@ -140,6 +140,7 @@ extern int NetworkFildes;		/// Network file descriptor
 extern int NetworkInSync;		/// Network is in sync
 extern int NetworkUpdates;		/// Network update each # game cycles
 extern int NetworkLag;			/// Network lag (# game cycles)
+extern int NetworkStatus[PlayerMax];	/// Network status
 
 /*----------------------------------------------------------------------------
 --	Functions
diff --git a/src/include/player.h b/src/include/player.h
index 45541103a..c8bc966d6 100644
--- a/src/include/player.h
+++ b/src/include/player.h
@@ -488,8 +488,8 @@ extern int HaveUnitTypeByIdent(const Player* player,const char* ident);
 
     /// Initialize the computer opponent AI
 extern void PlayersInitAi(void);
-    /// Called each frame for player handlers (AI)
-extern void PlayersEachFrame(void);
+    /// Called each game cycle for player handlers (AI)
+extern void PlayersEachCycle(void);
     /// Called each second for player handlers (AI)
 extern void PlayersEachSecond(void);
 
diff --git a/src/include/trigger.h b/src/include/trigger.h
index 69105be52..da8983a60 100644
--- a/src/include/trigger.h
+++ b/src/include/trigger.h
@@ -40,7 +40,7 @@
 --	Functions
 ----------------------------------------------------------------------------*/
 
-extern void TriggersEachFrame(void);	/// test triggers
+extern void TriggersEachCycle(void);	/// test triggers
 
 extern void TriggerCclRegister(void);	/// Register ccl features
 extern void SaveTriggers(FILE*);	/// Save the trigger module
diff --git a/src/network/network.cpp b/src/network/network.cpp
index fbdac828a..04b350da3 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -267,6 +267,7 @@ global int NetworkFildes = -1;		/// Network file descriptor
 global int NetworkInSync = 1;		/// Network is in sync
 global int NetworkUpdates = 5;		/// Network update each # game cycles
 global int NetworkLag = 5;		/// Network lag in # game cycles
+global int NetworkStatus[PlayerMax];	/// Network status
 
 local char NetMsgBuf[128][PlayerMax];	/// Chat message buffers
 local int NetMsgBufLen[PlayerMax];	/// Stored chat message length
@@ -286,6 +287,7 @@ local int NetworkReceivedPackets;	/// Packets received packets
 local int NetworkReceivedEarly;		/// Packets received too early
 local int NetworkReceivedLate;		/// Packets received too late
 local int NetworkReceivedDups;		/// Packets received as duplicates
+local int NetworkReceivedLost;		/// Packets received packet lost
 
 local int NetworkSendPackets;		/// Packets send packets
 local int NetworkSendResend;		/// Packets send to resend
@@ -459,9 +461,9 @@ global void ExitNetwork1(void)
 	return;
     }
 #ifdef DEBUG
-    DebugLevel0("Received: %d packets, %d early, %d late, %d dups\n",
+    DebugLevel0("Received: %d packets, %d early, %d late, %d dups, %d lost.\n",
 	    NetworkReceivedPackets, NetworkReceivedEarly, NetworkReceivedLate,
-	    NetworkReceivedDups);
+	    NetworkReceivedDups,NetworkReceivedLost);
     DebugLevel0("Send: %d packets, %d resend\n",
 	    NetworkSendPackets, NetworkSendResend);
 #endif
@@ -548,7 +550,7 @@ global void NetworkSendCommand(int command, const Unit *unit, int x, int y,
 
 /**
 **	Called if message for the network is ready.
-**	(by WaitEventsAndKeepSync)
+**	(by WaitEventsOneFrame)
 **
 **	@todo
 **		NetworkReceivedEarly NetworkReceivedLate NetworkReceivedDups
@@ -677,6 +679,25 @@ global void NetworkEvent(void)
 		    nc->Type, n, nc->Cycle);
 	}
 
+	// Receive statistic
+	if( n>NetworkStatus[player] ) {
+	    NetworkStatus[player] = n;
+	}
+#ifdef DEBUG
+	if( i ) {
+	    // Not in first slot and not got packet lost!
+	    // FIXME: if more than 1 in sequence is lost, I count one to much!
+	    if( NetworkIn[nc->Cycle][player].Time != n ) {
+		++NetworkReceivedLost;
+	    }
+	} else {
+	    // FIXME: more network statistic
+	    if( NetworkIn[nc->Cycle][player].Time == n ) {
+		++NetworkReceivedDups;
+	    }
+	}
+#endif
+
 	// Place in network in
 	NetworkIn[nc->Cycle][player].Time = n;
 	NetworkIn[nc->Cycle][player].Data = *nc;
diff --git a/src/stratagus/mainloop.cpp b/src/stratagus/mainloop.cpp
index c00b12c27..e82b1b0b4 100644
--- a/src/stratagus/mainloop.cpp
+++ b/src/stratagus/mainloop.cpp
@@ -313,8 +313,36 @@ global void UpdateDisplay(void)
 	if( f<0 || f>168 ) {
 	    f=168;
 	}
-	VideoDrawHLine(ColorGreen,TheUI.MenuButtonX,TheUI.MenuButtonY,f);
-	VideoDrawHLine(ColorRed,TheUI.MenuButtonX+f,TheUI.MenuButtonY,168-f);
+	if( f ) {
+	    VideoDrawHLine(ColorGreen,TheUI.MenuButtonX,TheUI.MenuButtonY,f);
+	}
+	if( 168-f ) {
+	    VideoDrawHLine(ColorRed,TheUI.MenuButtonX+f,TheUI.MenuButtonY,168-f);
+	}
+	}
+	//
+	//	Draw line for network speed.
+	//
+	{
+	int i;
+	int f;
+
+	if( NetworkLag ) {
+	    for( i=0; i<PlayerMax; ++i ) {
+		f=16-(16*(NetworkStatus[i]-GameCycle))/(NetworkLag*2);
+		if( f<0 || f>16 ) {
+		    f=16;
+		}
+		if( f ) {
+		    VideoDrawHLine(ColorRed,
+			TheUI.MenuButtonX,TheUI.MenuButtonY+1+i,f);
+		}
+		if( 16-f ) {
+		    VideoDrawHLine(ColorGreen,
+			    TheUI.MenuButtonX+f,TheUI.MenuButtonY+1+i,16-f);
+		}
+	    }
+	}
 	}
 #endif
     }
diff --git a/src/stratagus/stratagus.cpp b/src/stratagus/stratagus.cpp
index ff7d81d64..12954d13d 100644
--- a/src/stratagus/stratagus.cpp
+++ b/src/stratagus/stratagus.cpp
@@ -830,6 +830,11 @@ global int main(int argc,char** argv)
 
 	    case 'L':
 		NetworkLag=atoi(optarg);
+		if( !NetworkLag ) {
+		    fprintf(stderr,"FIXME: zero lag not supported\n");
+		    Usage();
+		    exit(-1);
+		}
 		continue;
 	    case 'U':
 		NetworkUpdates=atoi(optarg);
diff --git a/src/ui/interface.cpp b/src/ui/interface.cpp
index 945fbadb5..b31fe1bb6 100644
--- a/src/ui/interface.cpp
+++ b/src/ui/interface.cpp
@@ -1022,7 +1022,7 @@ global void InputMouseExit(const EventCallback* callbacks,
 }
 
 /**
-**	Called each frame to handle mouse time outs.
+**	Called each frame to handle mouse timeouts.
 **
 **	@param callbacks	Callback structure for events.
 **	@param ticks		Denotes time-stamp of video-system