2 * $Logfile: /Freespace2/code/Network/multi_ping.cpp $
8 * Revision 1.2 2002/05/07 03:16:47 theoddone33
9 * The Great Newline Fix
11 * Revision 1.1.1.1 2002/05/03 03:28:10 root
15 * 5 8/19/99 10:59a Dave
16 * Packet loss detection.
18 * 4 11/17/98 11:12a Dave
19 * Removed player identification by address. Now assign explicit id #'s.
21 * 3 11/05/98 5:55p Dave
22 * Big pass at reducing #includes
24 * 2 10/07/98 10:53a Dave
27 * 1 10/07/98 10:50a Dave
29 * 4 6/30/98 2:17p Dave
30 * Revised object update system. Removed updates for all weapons. Put
31 * button info back into control info packet.
33 * 3 6/12/98 2:49p Dave
36 * 2 3/15/98 4:17p Dave
37 * Fixed oberver hud problems. Put in handy netplayer macros. Reduced size
38 * of network orientation matrices.
40 * 1 3/03/98 5:09p Dave
46 #include "multi_ping.h"
47 #include "multimsgs.h"
50 // ------------------------------------------------------------------------------------
51 // MULTIPLAYER PING DEFINES/VARS
55 // ------------------------------------------------------------------------------------
56 // MULTIPLAYER PING FUNCTIONS
59 // initialize all player ping times
60 void multi_ping_reset_players()
64 // reset the pings for all players
65 for(idx=0;idx<MAX_PLAYERS;idx++){
66 multi_ping_reset(&Net_players[idx].s_info.ping);
70 // initialize the given ping struct
71 void multi_ping_reset(ping_struct *ps)
73 // blast the struct clear
74 memset(ps,0,sizeof(ping_struct));
78 // set the ping start to be -1
79 ps->ping_start = -1.0f;
86 // evaluate a pong return on the given struct
87 void multi_ping_eval_pong(ping_struct *ps)
92 // if the ping technically hasn't started,
93 if(ps->ping_start < 0.0f){
94 nprintf(("Network","Processing pong for ping which hasn't started yet!\n"));
98 // if we still have room to add a ping
99 if(ps->num_pings < MAX_PINGS){
100 ps->ping_times[ps->ping_add++] = f2fl(timer_get_fixed_seconds()) - ps->ping_start;
103 // otherwise if we've wrapped around
105 // increment the place to add the ping time
106 if(ps->ping_add >= MAX_PINGS - 1){
112 ps->ping_times[ps->ping_add] = f2fl(timer_get_fixed_seconds()) - ps->ping_start;
115 // calculate the average ping time
117 for(idx=0;idx<ps->num_pings;idx++){
118 ping_sum += ps->ping_times[idx];
120 ps->ping_avg = (int)(1000.0f * (ping_sum / (float)ps->num_pings));
123 // start a ping - call this when sending a ping packet
124 void multi_ping_start(ping_struct *ps)
126 ps->ping_start = f2fl(timer_get_fixed_seconds());
129 // send a ping to a specific player
130 void multi_ping_send(net_player *p)
132 multi_ping_start(&p->s_info.ping);
133 send_ping(&p->p_info.addr);
136 // send a ping to the specified address
137 void multi_ping_send(net_addr *addr,ping_struct *ps)
139 multi_ping_start(ps);
143 // send a ping to all players
144 void multi_ping_send_all()
147 for(idx = 0;idx < MAX_PLAYERS;idx++){
148 if(MULTI_CONNECTED(Net_players[idx]) && (Net_player != NULL) && (Net_player->player_id != Net_players[idx].player_id)){
150 multi_ping_start(&Net_players[idx].s_info.ping);
152 // send the ping packet
153 send_ping(&Net_players[idx].p_info.addr);
158 // get the lowest existing ping in the ping struct, returning -1 if no pings
159 int multi_ping_get_lowest(ping_struct *ps)
161 int idx, lowest_index;
164 // if there are no recorded pings, return -1
165 if(ps->num_pings == 0){
169 // otherwise find the lowest
171 for(idx=0;idx<ps->num_pings;idx++){
172 // if we found a lower value
173 if((lowest == -1) || (ps->ping_times[idx] < lowest)){
174 lowest = ps->ping_times[idx];
179 // return the lowest val
180 return lowest < 0.0f ? -1 : (int)(lowest * 1000.0f)/2;
183 // (average ping + lowest ping)/2
184 int multi_ping_lowest_avg(ping_struct *ps)
186 int lowest = multi_ping_get_lowest(ps);
188 // if we couldn't get a valid lowest ping, bail
193 // if we have no average ping, bail
194 if(ps->ping_avg == -1){
198 // otherwise return the average of the 2
199 return (lowest + ps->ping_avg)/2;