forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiopt.cpp
518 lines (436 loc) · 13.5 KB
/
multiopt.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2019 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
*/
/*
* multiopt.cpp
*
* Alex Lee,97/98, Pumpkin Studios
*
* Routines for setting the game options and starting the init process.
*/
#include "lib/framework/frame.h" // for everything
#include "lib/framework/file.h"
#include "lib/framework/wzapp.h"
#include "lib/ivis_opengl/piestate.h"
#include "map.h"
#include "game.h" // for loading maps
#include "message.h" // for clearing messages.
#include "main.h"
#include "display3d.h" // for changing the viewpoint
#include "power.h"
#include "lib/widget/widget.h"
#include "lib/gamelib/gtime.h"
#include "lib/netplay/netplay.h"
#include "hci.h"
#include "configuration.h" // lobby cfg.
#include "clparse.h"
#include "component.h"
#include "console.h"
#include "multiplay.h"
#include "lib/sound/audio.h"
#include "multijoin.h"
#include "frontend.h"
#include "levels.h"
#include "loadsave.h"
#include "modding.h"
#include "multistat.h"
#include "multiint.h"
#include "multilimit.h"
#include "multigifts.h"
#include "multiint.h"
#include "multirecv.h"
#include "scriptfuncs.h"
#include "template.h"
// send complete game info set!
void sendOptions()
{
unsigned int i;
if (!NetPlay.isHost || !bHosted) // Only host should act, and only if the game hasn't started yet.
{
ASSERT(false, "Host only routine detected for client or not hosting yet!");
return;
}
game.modHashes = getModHashList();
NETbeginEncode(NETbroadcastQueue(), NET_OPTIONS);
// First send information about the game
NETuint8_t(&game.type);
NETstring(game.map, 128);
NETbin(game.hash.bytes, game.hash.Bytes);
uint32_t modHashesSize = game.modHashes.size();
NETuint32_t(&modHashesSize);
for (auto &hash : game.modHashes)
{
NETbin(hash.bytes, hash.Bytes);
}
NETuint8_t(&game.maxPlayers);
NETstring(game.name, 128);
NETuint32_t(&game.power);
NETuint8_t(&game.base);
NETuint8_t(&game.alliance);
NETbool(&game.scavengers);
NETbool(&game.isMapMod);
NETuint32_t(&game.techLevel);
for (i = 0; i < MAX_PLAYERS; i++)
{
NETuint8_t(&game.skDiff[i]);
}
// Send the list of who is still joining
for (i = 0; i < MAX_PLAYERS; i++)
{
NETbool(&ingame.JoiningInProgress[i]);
}
// Same goes for the alliances
for (i = 0; i < MAX_PLAYERS; i++)
{
unsigned int j;
for (j = 0; j < MAX_PLAYERS; j++)
{
NETuint8_t(&alliances[i][j]);
}
}
// Send the number of structure limits to expect
NETuint32_t(&ingame.numStructureLimits);
debug(LOG_NET, "(Host) Structure limits to process on client is %u", ingame.numStructureLimits);
// Send the structures changed
for (i = 0; i < ingame.numStructureLimits; i++)
{
NETuint32_t(&ingame.pStructureLimits[i].id);
NETuint32_t(&ingame.pStructureLimits[i].limit);
}
updateLimitFlags();
NETuint8_t(&ingame.flags);
NETend();
}
// ////////////////////////////////////////////////////////////////////////////
// options for a game. (usually recvd in frontend)
void recvOptions(NETQUEUE queue)
{
unsigned int i;
debug(LOG_NET, "Receiving options from host");
NETbeginDecode(queue, NET_OPTIONS);
// Get general information about the game
NETuint8_t(&game.type);
NETstring(game.map, 128);
NETbin(game.hash.bytes, game.hash.Bytes);
uint32_t modHashesSize;
NETuint32_t(&modHashesSize);
ASSERT_OR_RETURN(, modHashesSize < 1000000, "Way too many mods %u", modHashesSize);
game.modHashes.resize(modHashesSize);
for (auto &hash : game.modHashes)
{
NETbin(hash.bytes, hash.Bytes);
}
NETuint8_t(&game.maxPlayers);
NETstring(game.name, 128);
NETuint32_t(&game.power);
NETuint8_t(&game.base);
NETuint8_t(&game.alliance);
NETbool(&game.scavengers);
NETbool(&game.isMapMod);
NETuint32_t(&game.techLevel);
for (i = 0; i < MAX_PLAYERS; i++)
{
NETuint8_t(&game.skDiff[i]);
}
// Send the list of who is still joining
for (i = 0; i < MAX_PLAYERS; i++)
{
NETbool(&ingame.JoiningInProgress[i]);
}
// Alliances
for (i = 0; i < MAX_PLAYERS; i++)
{
unsigned int j;
for (j = 0; j < MAX_PLAYERS; j++)
{
NETuint8_t(&alliances[i][j]);
}
}
netPlayersUpdated = true;
// Free any structure limits we may have in-place
if (ingame.numStructureLimits)
{
ingame.numStructureLimits = 0;
free(ingame.pStructureLimits);
ingame.pStructureLimits = nullptr;
}
// Get the number of structure limits to expect
NETuint32_t(&ingame.numStructureLimits);
debug(LOG_NET, "Host is sending us %u structure limits", ingame.numStructureLimits);
// If there were any changes allocate memory for them
if (ingame.numStructureLimits)
{
ingame.pStructureLimits = (MULTISTRUCTLIMITS *)malloc(ingame.numStructureLimits * sizeof(MULTISTRUCTLIMITS));
}
for (i = 0; i < ingame.numStructureLimits; i++)
{
NETuint32_t(&ingame.pStructureLimits[i].id);
NETuint32_t(&ingame.pStructureLimits[i].limit);
}
NETuint8_t(&ingame.flags);
NETend();
// Do the skirmish slider settings if they are up
for (i = 0; i < MAX_PLAYERS; i++)
{
if (widgGetFromID(psWScreen, MULTIOP_SKSLIDE + i))
{
widgSetSliderPos(psWScreen, MULTIOP_SKSLIDE + i, game.skDiff[i]);
}
}
debug(LOG_INFO, "Rebuilding map list");
// clear out the old level list.
levShutDown();
levInitialise();
rebuildSearchPath(mod_multiplay, true); // MUST rebuild search path for the new maps we just got!
buildMapList();
bool haveData = true;
auto requestFile = [&haveData](Sha256 &hash, char const *filename) {
if (std::any_of(NetPlay.wzFiles.begin(), NetPlay.wzFiles.end(), [&hash](WZFile const &file) { return file.hash == hash; }))
{
debug(LOG_INFO, "Already requested file, continue waiting.");
haveData = false;
return false; // Downloading the file already
}
if (!PHYSFS_exists(filename))
{
debug(LOG_INFO, "Creating new file %s", filename);
}
else if (findHashOfFile(filename) != hash)
{
debug(LOG_INFO, "Overwriting old incomplete or corrupt file %s", filename);
}
else
{
return false; // Have the file already.
}
NetPlay.wzFiles.emplace_back(PHYSFS_openWrite(filename), hash);
// Request the map/mod from the host
NETbeginEncode(NETnetQueue(NET_HOST_ONLY), NET_FILE_REQUESTED);
NETbin(hash.bytes, hash.Bytes);
NETend();
haveData = false;
return true; // Starting download now.
};
LEVEL_DATASET *mapData = levFindDataSet(game.map, &game.hash);
// See if we have the map or not
if (mapData == nullptr)
{
char mapName[256];
sstrcpy(mapName, game.map);
removeWildcards(mapName);
if (strlen(mapName) >= 3 && mapName[strlen(mapName) - 3] == '-' && mapName[strlen(mapName) - 2] == 'T' && unsigned(mapName[strlen(mapName) - 1] - '1') < 3)
{
mapName[strlen(mapName) - 3] = '\0'; // Cut off "-T1", "-T2" or "-T3".
}
char filename[256];
ssprintf(filename, "maps/%dc-%s-%s.wz", game.maxPlayers, mapName, game.hash.toString().c_str()); // Wonder whether game.maxPlayers is initialised already?
if (requestFile(game.hash, filename))
{
debug(LOG_INFO, "Map was not found, requesting map %s from host, type %d", game.map, game.isMapMod);
addConsoleMessage("MAP REQUESTED!", DEFAULT_JUSTIFY, SYSTEM_MESSAGE);
}
else
{
debug(LOG_FATAL, "Can't load map %s, even though we downloaded %s", game.map, filename);
abort();
}
}
for (Sha256 &hash : game.modHashes)
{
char filename[256];
ssprintf(filename, "mods/downloads/%s", hash.toString().c_str());
if (requestFile(hash, filename))
{
debug(LOG_INFO, "Mod was not found, requesting mod %s from host", hash.toString().c_str());
addConsoleMessage("MOD REQUESTED!", DEFAULT_JUSTIFY, SYSTEM_MESSAGE);
}
}
if (mapData && CheckForMod(mapData->realFileName))
{
char const *str = game.isMapMod ?
_("Warning, this is a map-mod, it could alter normal gameplay.") :
_("Warning, HOST has altered the game code, and can't be trusted!");
addConsoleMessage(str, DEFAULT_JUSTIFY, NOTIFY_MESSAGE);
game.isMapMod = true;
}
if (mapData)
{
loadMapPreview(false);
}
}
// ////////////////////////////////////////////////////////////////////////////
// Host Campaign.
bool hostCampaign(char *sGame, char *sPlayer)
{
PLAYERSTATS playerStats;
UDWORD i;
debug(LOG_WZ, "Hosting campaign: '%s', player: '%s'", sGame, sPlayer);
freeMessages();
if (!NEThostGame(sGame, sPlayer, game.type, 0, 0, 0, game.maxPlayers))
{
return false;
}
for (i = 0; i < MAX_PLAYERS; i++)
{
if (NetPlay.bComms)
{
game.skDiff[i] = 0; // disable AI
}
}
NetPlay.players[selectedPlayer].ready = false;
ingame.localJoiningInProgress = true;
ingame.JoiningInProgress[selectedPlayer] = true;
bMultiPlayer = true;
bMultiMessages = true; // enable messages
loadMultiStats(sPlayer, &playerStats); // stats stuff
setMultiStats(selectedPlayer, playerStats, false);
setMultiStats(selectedPlayer, playerStats, true);
// load AI values of challenge files for getAIName()
setupChallengeAIs();
// ensure all players have a name in One Player Skirmish games
if (!NetPlay.bComms)
{
sstrcpy(NetPlay.players[0].name, sPlayer);
for (unsigned i = 1; i < MAX_PLAYERS; ++i)
{
sstrcpy(NetPlay.players[i].name, getAIName(i));
}
}
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// Tell the host we are leaving the game 'nicely', (we wanted to) and not
// because we have some kind of error. (dropped or disconnected)
bool sendLeavingMsg()
{
debug(LOG_NET, "We are leaving 'nicely'");
NETbeginEncode(NETnetQueue(NET_HOST_ONLY), NET_PLAYER_LEAVING);
{
bool host = NetPlay.isHost;
uint32_t id = selectedPlayer;
NETuint32_t(&id);
NETbool(&host);
}
NETend();
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// called in Init.c to shutdown the whole netgame gubbins.
bool multiShutdown()
{
// shut down netplay lib.
debug(LOG_MAIN, "shutting down networking");
NETshutdown();
debug(LOG_MAIN, "free game data (structure limits)");
if (ingame.numStructureLimits)
{
ingame.numStructureLimits = 0;
free(ingame.pStructureLimits);
ingame.pStructureLimits = nullptr;
}
return true;
}
// ////////////////////////////////////////////////////////////////////////////
static bool gameInit()
{
UDWORD player;
for (player = 1; player < MAX_PLAYERS; player++)
{
// we want to remove disabled AI & all the other players that don't belong
if ((game.skDiff[player] == 0 || player >= game.maxPlayers) && player != scavengerPlayer())
{
clearPlayer(player, true); // do this quietly
debug(LOG_NET, "removing disabled AI (%d) from map.", player);
}
}
if (game.scavengers) // FIXME - not sure if we still need this hack - Per
{
// ugly hack for now
game.skDiff[scavengerPlayer()] = DIFF_SLIDER_STOPS / 2;
}
unsigned playerCount = 0;
for (int index = 0; index < game.maxPlayers; ++index)
{
playerCount += NetPlay.players[index].ai >= 0 || NetPlay.players[index].allocated;
}
playerResponding(); // say howdy!
return true;
}
// ////////////////////////////////////////////////////////////////////////////
// say hi to everyone else....
void playerResponding()
{
ingame.startTime = gameTime;
ingame.localJoiningInProgress = false; // No longer joining.
ingame.JoiningInProgress[selectedPlayer] = false;
// Home the camera to the player
cameraToHome(selectedPlayer, false);
// Tell the world we're here
NETbeginEncode(NETbroadcastQueue(), NET_PLAYERRESPONDING);
NETuint32_t(&selectedPlayer);
NETend();
}
// ////////////////////////////////////////////////////////////////////////////
//called when the game finally gets fired up.
bool multiGameInit()
{
UDWORD player;
for (player = 0; player < MAX_PLAYERS; player++)
{
openchannels[player] = true; //open comms to this player.
}
gameInit();
msgStackReset(); //for multiplayer msgs, reset message stack
return true;
}
////////////////////////////////
// at the end of every game.
bool multiGameShutdown()
{
PLAYERSTATS st;
uint32_t time;
debug(LOG_NET, "%s is shutting down.", getPlayerName(selectedPlayer));
sendLeavingMsg(); // say goodbye
updateMultiStatsGames(); // update games played.
st = getMultiStats(selectedPlayer); // save stats
saveMultiStats(getPlayerName(selectedPlayer), getPlayerName(selectedPlayer), &st);
// if we terminate the socket too quickly, then, it is possible not to get the leave message
time = wzGetTicks();
while (wzGetTicks() - time < 1000)
{
wzYieldCurrentThread(); // TODO Make a wzDelay() function?
}
// close game
NETclose();
NETremRedirects();
if (ingame.numStructureLimits)
{
ingame.numStructureLimits = 0;
free(ingame.pStructureLimits);
ingame.pStructureLimits = nullptr;
}
ingame.flags = 0;
ingame.localJoiningInProgress = false; // Clean up
ingame.localOptionsReceived = false;
ingame.bHostSetup = false; // Dont attempt a host
ingame.TimeEveryoneIsInGame = 0;
ingame.startTime = 0;
NetPlay.isHost = false;
bMultiPlayer = false; // Back to single player mode
bMultiMessages = false;
selectedPlayer = 0; // Back to use player 0 (single player friendly)
return true;
}