-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
multiplay.h
343 lines (286 loc) · 13.1 KB
/
multiplay.h
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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2020 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
*/
/*
* Alex Lee 1997/98, Pumpkin Studios, Bath.
*/
#ifndef __INCLUDED_SRC_MULTIPLAY_H__
#define __INCLUDED_SRC_MULTIPLAY_H__
#include "lib/framework/frame.h"
#include "lib/framework/types.h"
#include "lib/framework/vector.h"
#include "lib/framework/crc.h"
#include "lib/netplay/nettypes.h"
#include "multiplaydefs.h"
#include "orderdef.h"
#include "stringdef.h"
#include "messagedef.h"
#include "levels.h"
#include "console.h"
#include "multirecv.h"
#include <vector>
#include <string>
#include <chrono>
#include <nonstd/optional.hpp>
using nonstd::optional;
using nonstd::nullopt;
#include <nlohmann/json_fwd.hpp>
class DROID_GROUP;
struct BASE_OBJECT;
struct DROID;
struct DROID_TEMPLATE;
struct FEATURE;
struct INITIAL_DROID_ORDERS;
struct STRUCTURE;
// /////////////////////////////////////////////////////////////////////////////////////////////////
// Game Options Structure. Enough info to completely describe the static stuff in a multiplayer game.
// Also used for skirmish games. And sometimes for campaign. Should be moved somewhere else.
struct MULTIPLAYERGAME
{
LEVEL_TYPE type; // DMATCH/CAMPAIGN/SKIRMISH/TEAMPLAY etc...
uint8_t scavengers; // ultimate scavengers, scavengers, no scavengers
char map[128]; // name of multiplayer map being used.
uint8_t maxPlayers; // max players to allow
char name[128]; // game name (to be used)
Sha256 hash; ///< Hash of map file. Zero if built in.
std::vector<Sha256> modHashes; // Hash of mods.
uint32_t power; // power level for arena game
uint8_t base; // clean/base/base&defence
uint8_t alliance; // no/yes/locked
bool mapHasScavengers;
bool isMapMod; // if a map has mods
bool isRandom; // If a map is non-static.
uint32_t techLevel; // what technology level is being used
uint32_t inactivityMinutes; // The number of minutes without active play before a player should be considered "inactive". (0 = disable activity alerts)
uint32_t gameTimeLimitMinutes; // The number of minutes before the game automatically ends (0 = disable time limit)
PLAYER_LEAVE_MODE playerLeaveMode; // The behavior used for when players leave a game
// NOTE: If adding to this struct, a lot of things probably require changing
// (send/recvOptions? loadMainFile/writeMainFile? to/from_json in multiint.h.cpp?)
};
struct MULTISTRUCTLIMITS
{
uint32_t id;
uint32_t limit;
inline bool operator==(const MULTISTRUCTLIMITS& a) const
{
return (id == a.id && limit == a.limit);
}
};
// The side we are *configured* as. Used to indicate whether we are the server or the client. Note that when
// playing singleplayer, we are running as a host, not a client. Values are booleans as this replaces the old
// `bool bHostSetup`.
enum class InGameSide : bool {
HOST_OR_SINGLEPLAYER = true,
MULTIPLAYER_CLIENT = false
};
// info used inside games.
struct MULTIPLAYERINGAME
{
UDWORD PingTimes[MAX_CONNECTED_PLAYERS]; // store for pings.
int LagCounter[MAX_CONNECTED_PLAYERS];
bool VerifiedIdentity[MAX_CONNECTED_PLAYERS]; // if the multistats identity has been verified.
bool localOptionsReceived; // used to show if we have game options yet..
bool localJoiningInProgress; // used before we know our player number.
bool JoiningInProgress[MAX_CONNECTED_PLAYERS];
bool PendingDisconnect[MAX_CONNECTED_PLAYERS]; // used to mark players who have disconnected after the game has "fired up" but before it actually starts (i.e. pre-game / loading phase) - UI only
bool DataIntegrity[MAX_CONNECTED_PLAYERS];
InGameSide side;
optional<int32_t> TimeEveryoneIsInGame;
bool isAllPlayersDataOK;
std::chrono::steady_clock::time_point startTime;
optional<std::chrono::steady_clock::time_point> endTime;
std::chrono::steady_clock::time_point lastLagCheck;
optional<std::chrono::steady_clock::time_point> lastSentPlayerDataCheck2[MAX_CONNECTED_PLAYERS] = {};
std::chrono::steady_clock::time_point lastPlayerDataCheck2;
bool muteChat[MAX_CONNECTED_PLAYERS] = {false};
std::vector<MULTISTRUCTLIMITS> structureLimits;
uint8_t flags; ///< Bitmask, shows which structures are disabled.
#define MPFLAGS_NO_TANKS 0x01 ///< Flag for tanks disabled
#define MPFLAGS_NO_CYBORGS 0x02 ///< Flag for cyborgs disabled
#define MPFLAGS_NO_VTOLS 0x04 ///< Flag for VTOLs disabled
#define MPFLAGS_NO_UPLINK 0x08 ///< Flag for Satellite Uplink disabled
#define MPFLAGS_NO_LASSAT 0x10 ///< Flag for Laser Satellite Command Post disabled
#define MPFLAGS_FORCELIMITS 0x20 ///< Flag to force structure limits
#define MPFLAGS_MAX 0x3f
std::vector<MULTISTRUCTLIMITS> lastAppliedStructureLimits; // a bit of a hack to retain the structureLimits used when loading / starting a game
};
struct NetworkTextMessage
{
/**
* Sender can be a player index, SYSTEM_MESSAGE or NOTIFY_MESSAGE.
**/
int32_t sender;
char text[MAX_CONSOLE_STRING_LENGTH];
bool teamSpecific = false;
NetworkTextMessage() {}
NetworkTextMessage(int32_t messageSender, char const *messageText);
void enqueue(NETQUEUE queue);
bool receive(NETQUEUE queue);
};
enum STRUCTURE_INFO
{
STRUCTUREINFO_MANUFACTURE,
STRUCTUREINFO_CANCELPRODUCTION,
STRUCTUREINFO_HOLDPRODUCTION,
STRUCTUREINFO_RELEASEPRODUCTION,
STRUCTUREINFO_HOLDRESEARCH,
STRUCTUREINFO_RELEASERESEARCH
};
// ////////////////////////////////////////////////////////////////////////////
// Game Options and stats.
extern MULTIPLAYERGAME game; // the game description.
extern MULTIPLAYERINGAME ingame; // the game description.
extern bool bMultiPlayer; // true when more than 1 player.
extern bool bMultiMessages; // == bMultiPlayer unless multi messages are disabled
extern bool openchannels[MAX_CONNECTED_PLAYERS]; // outgoing message channels (i.e. who you are willing to send messages to)
extern UBYTE bDisplayMultiJoiningStatus; // draw load progress?
#define RUN_ONLY_ON_SIDE(_side) \
if (ingame.side != _side) \
{ \
return; \
}
// ////////////////////////////////////////////////////////////////////////////
// defines
// Bitmask for client lobby
#define NO_VTOLS 1
#define NO_TANKS 2
#define NO_BORGS 4
#define ANYPLAYER 99
#define NO_SCAVENGERS 0
#define SCAVENGERS 1
#define ULTIMATE_SCAVENGERS 2
#define CAMP_CLEAN 0 // campaign subtypes
#define CAMP_BASE 1
#define CAMP_WALLS 2
#define PING_LIMIT 4000 // If ping is bigger than this, then worry and panic, and don't even try showing the ping.
#define LEV_LOW 0
#define LEV_MED 1
#define LEV_HI 2
#define TECH_1 1
#define TECH_2 2
#define TECH_3 3
#define TECH_4 4
#define MAX_KICK_REASON 1024 // max array size for the reason your kicking someone
// functions
WZ_DECL_WARN_UNUSED_RESULT BASE_OBJECT *IdToPointer(UDWORD id, UDWORD player);
WZ_DECL_WARN_UNUSED_RESULT STRUCTURE *IdToStruct(UDWORD id, UDWORD player);
WZ_DECL_WARN_UNUSED_RESULT DROID *IdToDroid(UDWORD id, UDWORD player);
WZ_DECL_WARN_UNUSED_RESULT DROID *IdToMissionDroid(UDWORD id, UDWORD player);
WZ_DECL_WARN_UNUSED_RESULT FEATURE *IdToFeature(UDWORD id, UDWORD player);
WZ_DECL_WARN_UNUSED_RESULT DROID_TEMPLATE *IdToTemplate(UDWORD tempId, UDWORD player);
const char *getPlayerName(int player);
bool setPlayerName(int player, const char *sName);
void clearPlayerName(unsigned int player);
const char *getPlayerColourName(int player);
bool isHumanPlayer(int player); //to tell if the player is a computer or not.
bool myResponsibility(int player);
bool responsibleFor(int player, int playerinquestion);
int whosResponsible(int player);
bool canGiveOrdersFor(int player, int playerInQuestion);
int scavengerSlot(); // Returns the player number that scavengers would have if they were enabled.
int scavengerPlayer(); // Returns the player number that the scavengers have, or -1 if disabled.
Vector3i cameraToHome(UDWORD player, bool scroll);
bool multiPlayerLoop(); // for loop.c
enum class HandleMessageAction
{
Process_Message,
Silently_Ignore,
Disallow_And_Kick_Sender
};
HandleMessageAction getMessageHandlingAction(NETQUEUE& queue, uint8_t type);
bool shouldProcessMessage(NETQUEUE& queue, uint8_t type);
bool recvMessage();
bool SendResearch(uint8_t player, uint32_t index, bool trigger);
void printInGameTextMessage(NetworkTextMessage const &message);
void sendInGameSystemMessage(const char *text);
int32_t findPlayerIndexByPosition(uint32_t position);
void printConsoleNameChange(const char *oldName, const char *newName); ///< Print message to console saying a name changed.
void turnOffMultiMsg(bool bDoit);
void sendMap();
bool multiplayerWinSequence(bool firstCall);
/////////////////////////////////////////////////////////
// definitions of functions in multiplay's other c files.
// Buildings . multistruct
bool SendDestroyStructure(STRUCTURE *s);
bool SendBuildFinished(STRUCTURE *psStruct);
bool sendLasSat(UBYTE player, STRUCTURE *psStruct, BASE_OBJECT *psObj);
void sendStructureInfo(STRUCTURE *psStruct, STRUCTURE_INFO structureInfo, DROID_TEMPLATE *psTempl);
// droids . multibot
bool SendDroid(DROID_TEMPLATE *pTemplate, uint32_t x, uint32_t y, uint8_t player, uint32_t id, const INITIAL_DROID_ORDERS *initialOrders);
bool SendDestroyDroid(const DROID *psDroid);
void sendQueuedDroidInfo(); ///< Actually sends the droid orders which were queued by SendDroidInfo.
void sendDroidInfo(DROID *psDroid, DroidOrder const &order, bool add);
bool sendDroidSecondary(const DROID *psDroid, SECONDARY_ORDER sec, SECONDARY_STATE state);
bool sendDroidDisembark(DROID const *psTransporter, DROID const *psDroid);
// Startup. mulitopt
bool multiShutdown();
bool sendLeavingMsg();
bool hostCampaign(const char *SessionName, char *hostPlayerName, bool spectatorHost, bool skipResetAIs);
struct JoinConnectionDescription
{
public:
JoinConnectionDescription() { }
JoinConnectionDescription(const std::string& host, uint32_t port)
: host(host)
, port(port)
{ }
public:
std::string host;
uint32_t port = 0;
};
std::vector<JoinConnectionDescription> findLobbyGame(const std::string& lobbyAddress, unsigned int lobbyPort, uint32_t lobbyGameId);
enum class JoinGameResult {
FAILED,
JOINED,
PENDING_PASSWORD
};
JoinGameResult joinGame(const char *connectionString, bool asSpectator = false);
JoinGameResult joinGame(const char *host, uint32_t port, bool asSpectator = false);
JoinGameResult joinGame(const std::vector<JoinConnectionDescription>& connection_list, bool asSpectator = false);
void playerResponding();
bool multiGameInit();
bool multiGameShutdown();
bool multiStartScreenInit();
// syncing.
bool sendScoreCheck(); //score check only(frontend)
void multiSyncResetAllChallenges();
void multiSyncResetPlayerChallenge(uint32_t playerIdx);
void multiSyncPlayerSwap(uint32_t playerIndexA, uint32_t playerIndexB);
bool sendPing(); // allow game to request pings.
void HandleBadParam(const char *msg, const int from, const int actual);
// multijoin
bool sendResearchStatus(const STRUCTURE *psBuilding, UDWORD index, UBYTE player, bool bStart);
bool sendBeacon(int32_t locX, int32_t locY, int32_t forPlayer, int32_t sender, const char *pStr);
void startMultiplayerGame();
void resetReadyStatus(bool bSendOptions, bool ignoreReadyReset = false);
STRUCTURE *findResearchingFacilityByResearchIndex(unsigned player, unsigned index);
void sendSyncRequest(int32_t req_id, int32_t x, int32_t y, const BASE_OBJECT *psObj, const BASE_OBJECT *psObj2);
bool sendBeaconToPlayer(SDWORD locX, SDWORD locY, SDWORD forPlayer, SDWORD sender, const char *beaconMsg);
MESSAGE *findBeaconMsg(UDWORD player, SDWORD sender);
VIEWDATA *CreateBeaconViewData(SDWORD sender, UDWORD LocX, UDWORD LocY);
void setPlayerMuted(uint32_t playerIdx, bool muted);
bool isPlayerMuted(uint32_t sender);
bool setGameStoryLogPlayerDataValue(uint32_t playerIndex, const std::string& key_str, const std::string& value_str);
bool makePlayerSpectator(uint32_t player_id, bool removeAllStructs = false, bool quietly = false);
class WZGameReplayOptionsHandler : public ReplayOptionsHandler
{
virtual bool saveOptions(nlohmann::json& object) const override;
virtual bool saveMap(EmbeddedMapData& mapData) const override;
virtual bool restoreOptions(const nlohmann::json& object, EmbeddedMapData&& embeddedMapData, uint32_t replay_netcodeMajor, uint32_t replay_netcodeMinor) override;
virtual size_t desiredBufferSize() const override;
virtual size_t maximumEmbeddedMapBufferSize() const override;
};
#endif // __INCLUDED_SRC_MULTIPLAY_H__