1 // _________ __ __ 2 // / _____// |_____________ _/ |______ ____ __ __ ______ 3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 6 // \/ \/ \//_____/ \/ 7 // ______________________ ______________________ 8 // T H E W A R B E G I N S 9 // Stratagus - A free fantasy real time strategy game engine 10 // 11 /**@name net_message.h - The network message header file. */ 12 // 13 // (c) Copyright 2013 by Joris Dauphin 14 // 15 // This program is free software; you can redistribute it and/or modify 16 // it under the terms of the GNU General Public License as published by 17 // the Free Software Foundation; only version 2 of the License. 18 // 19 // This program is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU General Public License for more details. 23 // 24 // You should have received a copy of the GNU General Public License 25 // along with this program; if not, write to the Free Software 26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 27 // 02111-1307, USA. 28 29 #ifndef NET_MESSAGE_H 30 #define NET_MESSAGE_H 31 32 //@{ 33 34 #include <stdint.h> 35 #include <vector> 36 37 /*---------------------------------------------------------------------------- 38 -- Declarations 39 ----------------------------------------------------------------------------*/ 40 41 /** 42 * Number of bytes in the name of a network player, 43 * including the terminating null character. 44 */ 45 #define NetPlayerNameSize 16 46 47 #define MaxNetworkCommands 9 /// Max Commands In A Packet 48 49 /** 50 ** Network systems active in current game. 51 */ 52 class CNetworkHost 53 { 54 public: CNetworkHost()55 CNetworkHost() { Clear(); } 56 size_t Serialize(unsigned char *buf) const; 57 size_t Deserialize(const unsigned char *buf); 58 void Clear(); Size()59 static size_t Size() { return 4 + 2 + 2 + NetPlayerNameSize; } 60 61 void SetName(const char *name); 62 63 uint32_t Host; /// Host address 64 uint16_t Port; /// Port on host 65 uint16_t PlyNr; /// Player number 66 char PlyName[NetPlayerNameSize]; /// Name of player 67 }; 68 69 /** 70 ** Multiplayer game setup menu state 71 */ 72 class CServerSetup 73 { 74 public: CServerSetup()75 CServerSetup() { Clear(); } 76 size_t Serialize(unsigned char *p) const; 77 size_t Deserialize(const unsigned char *p); Size()78 static size_t Size() { return 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 * PlayerMax + 1 * PlayerMax + 1 * PlayerMax; } 79 void Clear(); 80 81 bool operator == (const CServerSetup &rhs) const; 82 bool operator != (const CServerSetup &rhs) const { return !(*this == rhs); } 83 public: 84 uint8_t ResourcesOption; /// Resources option 85 uint8_t UnitsOption; /// Unit # option 86 uint8_t FogOfWar; /// Fog of war option 87 //Wyrmgus start 88 // uint8_t Inside; /// Inside option 89 uint8_t NoRandomness; /// No randomness option 90 //Wyrmgus end 91 uint8_t RevealMap; /// Reveal all the map 92 uint8_t TilesetSelection; /// Tileset select option 93 uint8_t GameTypeOption; /// Game type option 94 uint8_t Difficulty; /// Difficulty option 95 uint8_t MapRichness; /// Map richness option 96 uint8_t Opponents; /// Number of AI opponents 97 uint8_t CompOpt[PlayerMax]; /// Free slot option selection {"Available", "Computer", "Closed" } 98 uint8_t Ready[PlayerMax]; /// Client ready state 99 uint8_t Race[PlayerMax]; /// Client race selection 100 // Fill in here... 101 }; 102 103 /** 104 ** Network init config message subtypes (menu state machine). 105 */ 106 enum _ic_message_subtype_ { 107 ICMHello, /// Client Request 108 ICMConfig, /// Setup message configure clients 109 110 ICMEngineMismatch, /// Stratagus engine version doesn't match 111 ICMProtocolMismatch, /// Network protocol version doesn't match 112 ICMEngineConfMismatch, /// UNUSED:Engine configuration isn't identical 113 ICMMapUidMismatch, /// MAP UID doesn't match 114 115 ICMGameFull, /// No player slots available 116 ICMWelcome, /// Acknowledge for new client connections 117 118 ICMWaiting, /// Client has received Welcome and is waiting for Map/State 119 ICMMap, /// MapInfo (and Mapinfo Ack) 120 ICMState, /// StateInfo 121 ICMResync, /// Ack StateInfo change 122 123 ICMServerQuit, /// Server has quit game 124 ICMGoodBye, /// Client wants to leave game 125 ICMSeeYou, /// Client has left game 126 127 ICMGo, /// Client is ready to run 128 129 ICMAYT, /// Server asks are you there 130 ICMIAH /// Client answers I am here 131 }; 132 133 class CInitMessage_Header 134 { 135 public: CInitMessage_Header()136 CInitMessage_Header() {} CInitMessage_Header(unsigned char type,unsigned char subtype)137 CInitMessage_Header(unsigned char type, unsigned char subtype) : 138 type(type), 139 subtype(subtype) 140 {} 141 GetType()142 unsigned char GetType() const { return type; } GetSubType()143 unsigned char GetSubType() const { return subtype; } 144 145 size_t Serialize(unsigned char *p) const; 146 size_t Deserialize(const unsigned char *p); Size()147 static size_t Size() { return 2; } 148 private: 149 unsigned char type; 150 unsigned char subtype; 151 }; 152 153 class CInitMessage_Hello 154 { 155 public: CInitMessage_Hello()156 CInitMessage_Hello() {} 157 explicit CInitMessage_Hello(const char *name); GetHeader()158 const CInitMessage_Header &GetHeader() const { return header; } 159 const unsigned char *Serialize() const; 160 void Deserialize(const unsigned char *p); Size()161 static size_t Size() { return CInitMessage_Header::Size() + NetPlayerNameSize + 2 * 4; } 162 private: 163 CInitMessage_Header header; 164 public: 165 char PlyName[NetPlayerNameSize]; /// Name of player 166 int32_t Stratagus; /// Stratagus engine version 167 int32_t Version; /// Network protocol version 168 }; 169 170 class CInitMessage_Config 171 { 172 public: 173 CInitMessage_Config(); GetHeader()174 const CInitMessage_Header &GetHeader() const { return header; } 175 const unsigned char *Serialize() const; 176 void Deserialize(const unsigned char *p); Size()177 static size_t Size() { return CInitMessage_Header::Size() + 4 + PlayerMax * CNetworkHost::Size(); } 178 private: 179 CInitMessage_Header header; 180 public: 181 uint8_t clientIndex; /// index of receiver in hosts[] 182 uint8_t hostsCount; /// Number of hosts 183 CNetworkHost hosts[PlayerMax]; /// Participant information 184 }; 185 186 class CInitMessage_EngineMismatch 187 { 188 public: 189 CInitMessage_EngineMismatch(); GetHeader()190 const CInitMessage_Header &GetHeader() const { return header; } 191 const unsigned char *Serialize() const; 192 void Deserialize(const unsigned char *p); Size()193 static size_t Size() { return CInitMessage_Header::Size() + 4; } 194 private: 195 CInitMessage_Header header; 196 public: 197 int32_t Stratagus; /// Stratagus engine version 198 }; 199 200 class CInitMessage_ProtocolMismatch 201 { 202 public: 203 CInitMessage_ProtocolMismatch(); GetHeader()204 const CInitMessage_Header &GetHeader() const { return header; } 205 const unsigned char *Serialize() const; 206 void Deserialize(const unsigned char *p); Size()207 static size_t Size() { return CInitMessage_Header::Size() + 4; } 208 private: 209 CInitMessage_Header header; 210 public: 211 int32_t Version; /// Network protocol version 212 }; 213 214 class CInitMessage_Welcome 215 { 216 public: 217 CInitMessage_Welcome(); GetHeader()218 const CInitMessage_Header &GetHeader() const { return header; } 219 const unsigned char *Serialize() const; 220 void Deserialize(const unsigned char *p); Size()221 static size_t Size() { return CInitMessage_Header::Size() + PlayerMax * CNetworkHost::Size() + 2 * 4; } 222 private: 223 CInitMessage_Header header; 224 public: 225 CNetworkHost hosts[PlayerMax]; /// Participants information 226 int32_t Lag; /// Lag time 227 int32_t gameCyclesPerUpdate; /// Update frequency 228 }; 229 230 class CInitMessage_Map 231 { 232 public: CInitMessage_Map()233 CInitMessage_Map() {} 234 CInitMessage_Map(const char *path, uint32_t mapUID); GetHeader()235 const CInitMessage_Header &GetHeader() const { return header; } 236 const unsigned char *Serialize() const; 237 void Deserialize(const unsigned char *p); Size()238 static size_t Size() { return CInitMessage_Header::Size() + 256 + 4; } 239 private: 240 CInitMessage_Header header; 241 public: 242 char MapPath[256]; 243 uint32_t MapUID; /// UID of map to play. 244 }; 245 246 class CInitMessage_State 247 { 248 public: CInitMessage_State()249 CInitMessage_State() {} 250 CInitMessage_State(int type, const CServerSetup &data); GetHeader()251 const CInitMessage_Header &GetHeader() const { return header; } 252 const unsigned char *Serialize() const; 253 void Deserialize(const unsigned char *p); Size()254 static size_t Size() { return CInitMessage_Header::Size() + CServerSetup::Size(); } 255 private: 256 CInitMessage_Header header; 257 public: 258 CServerSetup State; /// Server Setup State information 259 }; 260 261 class CInitMessage_Resync 262 { 263 public: 264 CInitMessage_Resync(); GetHeader()265 const CInitMessage_Header &GetHeader() const { return header; } 266 const unsigned char *Serialize() const; 267 void Deserialize(const unsigned char *p); Size()268 static size_t Size() { return CInitMessage_Header::Size() + CNetworkHost::Size() * PlayerMax; } 269 private: 270 CInitMessage_Header header; 271 public: 272 CNetworkHost hosts[PlayerMax]; /// Participant information 273 }; 274 275 /** 276 ** Network message types. 277 ** 278 ** @todo cleanup the message types. 279 */ 280 enum _message_type_ { 281 MessageNone, /// When Nothing Is Happening 282 MessageInit_FromClient, /// Start connection 283 MessageInit_FromServer, /// Connection reply 284 285 MessageSync, /// Heart beat 286 MessageSelection, /// Update a Selection from Team Player 287 MessageQuit, /// Quit game 288 MessageResend, /// Resend message 289 290 MessageChat, /// Chat message 291 292 MessageCommandStop, /// Unit command stop 293 MessageCommandStand, /// Unit command stand ground 294 MessageCommandDefend, /// Unit command defend 295 MessageCommandFollow, /// Unit command follow 296 MessageCommandMove, /// Unit command move 297 //Wyrmgus start 298 MessageCommandPickUp, /// Unit command pick up 299 //Wyrmgus end 300 MessageCommandRepair, /// Unit command repair 301 MessageCommandAutoRepair, /// Unit command autorepair 302 MessageCommandAttack, /// Unit command attack 303 MessageCommandGround, /// Unit command attack ground 304 //Wyrmgus start 305 MessageCommandUse, /// Unit command use 306 MessageCommandTrade, /// Unit command trade 307 //Wyrmgus end 308 MessageCommandPatrol, /// Unit command patrol 309 MessageCommandBoard, /// Unit command board 310 MessageCommandUnload, /// Unit command unload 311 MessageCommandBuild, /// Unit command build building 312 MessageCommandDismiss, /// Unit command dismiss unit 313 MessageCommandResourceLoc, /// Unit command resource location 314 MessageCommandResource, /// Unit command resource 315 MessageCommandReturn, /// Unit command return goods 316 MessageCommandTrain, /// Unit command train 317 MessageCommandCancelTrain, /// Unit command cancel training 318 MessageCommandUpgrade, /// Unit command upgrade 319 MessageCommandCancelUpgrade, /// Unit command cancel upgrade 320 MessageCommandResearch, /// Unit command research 321 MessageCommandCancelResearch, /// Unit command cancel research 322 //Wyrmgus start 323 MessageCommandLearnAbility, /// Unit command learn ability 324 MessageCommandQuest, /// Unit command quest 325 MessageCommandBuy, /// Unit command buy 326 MessageCommandProduceResource, /// Unit command produce resource 327 MessageCommandSellResource, /// Unit command sell resource 328 MessageCommandBuyResource, /// Unit command buy resource 329 //Wyrmgus end 330 331 MessageExtendedCommand, /// Command is the next byte 332 333 // ATTN: __MUST__ be last due to spellid encoding!!! 334 MessageCommandSpellCast /// Unit command spell cast 335 }; 336 337 /** 338 ** Network extended message types. 339 */ 340 enum _extended_message_type_ { 341 ExtendedMessageDiplomacy, /// Change diplomacy 342 //Wyrmgus start 343 // ExtendedMessageSharedVision /// Change shared vision 344 ExtendedMessageSharedVision, /// Change shared vision 345 ExtendedMessageSetFaction, /// Change faction 346 ExtendedMessageAutosellResource /// Autosell resource 347 //Wyrmgus end 348 }; 349 350 /** 351 ** Network command message. 352 */ 353 class CNetworkCommand 354 { 355 public: CNetworkCommand()356 CNetworkCommand() : Unit(0), X(0), Y(0), Dest(0) {} Clear()357 void Clear() { this->Unit = this->X = this->Y = this->Dest = 0; } 358 359 size_t Serialize(unsigned char *buf) const; 360 size_t Deserialize(const unsigned char *buf); Size()361 static size_t Size() { return 2 + 2 + 2 + 2; } 362 363 public: 364 uint16_t Unit; /// Command for unit 365 uint16_t X; /// Map position X 366 uint16_t Y; /// Map position Y 367 uint16_t Dest; /// Destination unit 368 }; 369 370 /** 371 ** Extended network command message. 372 */ 373 class CNetworkExtendedCommand 374 { 375 public: CNetworkExtendedCommand()376 CNetworkExtendedCommand() : ExtendedType(0), Arg1(0), Arg2(0), Arg3(0), Arg4(0) {} 377 378 size_t Serialize(unsigned char *buf) const; 379 size_t Deserialize(const unsigned char *buf); Size()380 static size_t Size() { return 1 + 1 + 2 + 2 + 2; } 381 382 uint8_t ExtendedType; /// Extended network command type 383 uint8_t Arg1; /// Argument 1 384 uint16_t Arg2; /// Argument 2 385 uint16_t Arg3; /// Argument 3 386 uint16_t Arg4; /// Argument 4 387 }; 388 389 /** 390 ** Network chat message. 391 */ 392 class CNetworkChat 393 { 394 public: 395 size_t Serialize(unsigned char *buf) const; 396 size_t Deserialize(const unsigned char *buf); 397 size_t Size() const; 398 399 public: 400 std::string Text; /// Message bytes 401 }; 402 403 /** 404 ** Network sync message. 405 */ 406 class CNetworkCommandSync 407 { 408 public: CNetworkCommandSync()409 CNetworkCommandSync() : syncSeed(0), syncHash(0) {} 410 size_t Serialize(unsigned char *buf) const; 411 size_t Deserialize(const unsigned char *buf); Size()412 static size_t Size() { return 4 + 4; }; 413 414 public: 415 uint32_t syncSeed; 416 uint32_t syncHash; 417 }; 418 419 /** 420 ** Network quit message. 421 */ 422 class CNetworkCommandQuit 423 { 424 public: CNetworkCommandQuit()425 CNetworkCommandQuit() : player(0) {} 426 size_t Serialize(unsigned char *buf) const; 427 size_t Deserialize(const unsigned char *buf); Size()428 static size_t Size() { return 2; }; 429 430 public: 431 uint16_t player; 432 }; 433 434 /** 435 ** Network Selection Update 436 */ 437 class CNetworkSelection 438 { 439 public: CNetworkSelection()440 CNetworkSelection() : player(0) {} 441 442 size_t Serialize(unsigned char *buf) const; 443 size_t Deserialize(const unsigned char *buf); 444 size_t Size() const; 445 446 public: 447 uint16_t player; 448 std::vector<uint16_t> Units; /// Selection Units 449 }; 450 451 /** 452 ** Network packet header. 453 ** 454 ** Header for the packet. 455 */ 456 class CNetworkPacketHeader 457 { 458 public: CNetworkPacketHeader()459 CNetworkPacketHeader() 460 { 461 Cycle = 0; 462 memset(Type, 0, sizeof(Type)); 463 OrigPlayer = 255; 464 } 465 466 size_t Serialize(unsigned char *buf) const; 467 size_t Deserialize(const unsigned char *buf); Size()468 static size_t Size() { return 1 + 1 + 1 * MaxNetworkCommands; } 469 470 uint8_t Type[MaxNetworkCommands]; /// Commands in packet 471 uint8_t Cycle; /// Destination game cycle 472 uint8_t OrigPlayer; /// Host address 473 }; 474 475 /** 476 ** Network packet. 477 ** 478 ** This is sent over the network. 479 */ 480 class CNetworkPacket 481 { 482 public: 483 size_t Serialize(unsigned char *buf, int numcommands) const; 484 void Deserialize(const unsigned char *buf, unsigned int len, int *numcommands); 485 size_t Size(int numcommands) const; 486 487 CNetworkPacketHeader Header; /// Packet Header Info 488 std::vector<unsigned char> Command[MaxNetworkCommands]; 489 }; 490 491 //@} 492 493 #endif // !NET_MESSAGE_H 494