forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultisync.cpp
209 lines (175 loc) · 4.86 KB
/
multisync.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* MultiSync.c
*
* synching issues
* This file handles the constant backstream of net info, checking that recvd info
* is concurrent with the local world, and correcting as required. Magic happens here.
*
* All conflicts due to non-guaranteed messaging are detected/resolved here.
*
* Alex Lee, pumpkin Studios, bath.
*/
#include "lib/framework/frame.h"
#include "lib/framework/input.h"
#include "lib/gamelib/gtime.h"
#include "lib/netplay/netplay.h"
#include "multiplay.h"
#include "frontend.h" // for titlemode
#include "multistat.h"
#include "multirecv.h"
// ////////////////////////////////////////////////////////////////////////////
// function definitions
static UDWORD averagePing(void);
#define AV_PING_FREQUENCY 20000 // how often to update average pingtimes. in approx millisecs.
#define PING_FREQUENCY 4000 // how often to update pingtimes. in approx millisecs.
static UDWORD PingSend[MAX_PLAYERS]; //stores the time the ping was called.
// ////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////
// Score
// We use setMultiStats() to broadcast the score when needed.
bool sendScoreCheck(void)
{
// Broadcast any changes in other players, but not in FRONTEND!!!
if (titleMode != MULTIOPTION && titleMode != MULTILIMIT)
{
uint8_t i;
for (i = 0; i < game.maxPlayers; i++)
{
// Host controls AI's scores + his own...
if (myResponsibility(i))
{
// Send score to everyone else
setMultiStats(i, getMultiStats(i), false);
}
}
}
return true;
}
// ////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////
// Pings
static UDWORD averagePing(void)
{
UDWORD i, count = 0, total = 0;
for(i=0;i<MAX_PLAYERS;i++)
{
if(isHumanPlayer(i))
{
total += ingame.PingTimes[i];
count ++;
}
}
return total / MAX(count, 1);
}
bool sendPing(void)
{
bool isNew = true;
uint8_t player = selectedPlayer;
int i;
static UDWORD lastPing = 0; // Last time we sent a ping
static UDWORD lastav = 0; // Last time we updated average
// Only ping every so often
if (lastPing > realTime)
{
lastPing = 0;
}
if (realTime - lastPing < PING_FREQUENCY)
{
return true;
}
lastPing = realTime;
// If host, also update the average ping stat for joiners
if (NetPlay.isHost)
{
if (lastav > realTime)
{
lastav = 0;
}
if (realTime - lastav > AV_PING_FREQUENCY)
{
NETsetGameFlags(2, averagePing());
lastav = realTime;
}
}
/*
* Before we send the ping, if any player failed to respond to the last one
* we should re-enumerate the players.
*/
for (i = 0; i < MAX_PLAYERS; i++)
{
if (isHumanPlayer(i)
&& PingSend[i]
&& ingame.PingTimes[i]
&& i != selectedPlayer)
{
ingame.PingTimes[i] = PING_LIMIT;
}
else if (!isHumanPlayer(i)
&& PingSend[i]
&& ingame.PingTimes[i]
&& i != selectedPlayer)
{
ingame.PingTimes[i] = 0;
}
}
NETbeginEncode(NETbroadcastQueue(), NET_PING);
NETuint8_t(&player);
NETbool(&isNew);
NETend();
// Note when we sent the ping
for (i = 0; i < MAX_PLAYERS; i++)
{
PingSend[i] = realTime;
}
return true;
}
// accept and process incoming ping messages.
bool recvPing(NETQUEUE queue)
{
bool isNew;
uint8_t sender, us = selectedPlayer;
NETbeginDecode(queue, NET_PING);
NETuint8_t(&sender);
NETbool(&isNew);
NETend();
if (sender >= MAX_PLAYERS)
{
debug(LOG_ERROR, "Bad NET_PING packet, sender is %d", (int)sender);
return false;
}
// If this is a new ping, respond to it
if (isNew)
{
NETbeginEncode(NETnetQueue(sender), NET_PING);
// We are responding to a new ping
isNew = false;
NETuint8_t(&us);
NETbool(&isNew);
NETend();
}
// They are responding to one of our pings
else
{
// Work out how long it took them to respond
ingame.PingTimes[sender] = (realTime - PingSend[sender]) / 2;
// Note that we have received it
PingSend[sender] = 0;
}
return true;
}