CCTEN.CPP 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***************************************************************************
  15. * *
  16. * Project Name : Command & Conquer *
  17. * *
  18. * File Name : CCTEN.CPP *
  19. * *
  20. * Programmer : Bill R. Randolph *
  21. * *
  22. * Start Date : 01/09/96 *
  23. * *
  24. * Last Update : November 27, 1996 [BRR] *
  25. * *
  26. *-------------------------------------------------------------------------*
  27. * Functions: *
  28. * Init_TEN -- Performs TEN-specific initialization *
  29. * Shutdown_TEN -- Shuts down TEN connections *
  30. * Connect_TEN -- Waits for connections to other players *
  31. * Destroy_TEN_Connection -- Destroys the given connection *
  32. * Debug_Mono -- Custom mono prints *
  33. * Send_TEN_Win_Packet -- Sends a win packet to server *
  34. * Send_TEN_Alliance -- Sends an ally/enemy packet to server *
  35. * Send_TEN_Out_Of_Sync -- Announces this game out of sync *
  36. * Send_TEN_Packet_Too_Late -- Announces packet-received-too-late *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "function.h"
  39. #if(TEN)
  40. #ifdef WIN32
  41. #define WINDOWS
  42. #endif
  43. #include "ten.h"
  44. #endif
  45. void Connect_TEN(void);
  46. void Debug_Mono(void);
  47. /***************************************************************************
  48. * Init_TEN -- Performs TEN-specific initialization *
  49. * *
  50. * INPUT: *
  51. * none. *
  52. * *
  53. * OUTPUT: *
  54. * 1 = OK, 0 = error *
  55. * *
  56. * WARNINGS: *
  57. * none. *
  58. * *
  59. * HISTORY: *
  60. * 01/09/1996 BRR : Created. *
  61. *=========================================================================*/
  62. int Init_TEN(void)
  63. {
  64. #if(TEN)
  65. //------------------------------------------------------------------------
  66. // Allocate a packet buffer for TEN's use
  67. //------------------------------------------------------------------------
  68. Session.TenPacket = new char[Session.TenSize];
  69. //------------------------------------------------------------------------
  70. // Read the multiplayer settings from the CONQUER.INI file, and the game
  71. // options from the options file.
  72. //------------------------------------------------------------------------
  73. Session.Read_MultiPlayer_Settings();
  74. if (!Read_TEN_Game_Options()) {
  75. WWMessageBox().Process("Unable to load game settings!");
  76. //Prog_End();
  77. Emergency_Exit(0);
  78. }
  79. //------------------------------------------------------------------------
  80. // Flush all incoming packets
  81. //------------------------------------------------------------------------
  82. Ten->Flush_All();
  83. //------------------------------------------------------------------------
  84. // Form connections to all other players
  85. //------------------------------------------------------------------------
  86. Connect_TEN();
  87. //------------------------------------------------------------------------
  88. // Set multiplayer values for the local system, and timing values.
  89. //------------------------------------------------------------------------
  90. Session.CommProtocol = COMM_PROTOCOL_MULTI_E_COMP;
  91. return (1);
  92. #else
  93. return (1);
  94. #endif
  95. } // end of Init_TEN
  96. /***************************************************************************
  97. * Shutdown_TEN -- Shuts down TEN connections *
  98. * *
  99. * INPUT: *
  100. * none. *
  101. * *
  102. * OUTPUT: *
  103. * none. *
  104. * *
  105. * WARNINGS: *
  106. * none. *
  107. * *
  108. * HISTORY: *
  109. * 01/09/1996 BRR : Created. *
  110. *=========================================================================*/
  111. void Shutdown_TEN(void)
  112. {
  113. #if(TEN)
  114. CDTimerClass<SystemTimerClass> timer;
  115. //------------------------------------------------------------------------
  116. // Wait a full second before exiting, to ensure all packets get sent.
  117. //------------------------------------------------------------------------
  118. timer = 60;
  119. while (timer) ;
  120. //------------------------------------------------------------------------
  121. // Free memory
  122. //------------------------------------------------------------------------
  123. if (Session.TenPacket) {
  124. delete [] Session.TenPacket;
  125. Session.TenPacket = NULL;
  126. }
  127. if (Ten) {
  128. delete Ten;
  129. Ten = NULL;
  130. }
  131. return;
  132. #endif
  133. } // end of Shutdown_TEN
  134. /***************************************************************************
  135. * Connect_TEN -- Waits for connections to other players *
  136. * *
  137. * INPUT: *
  138. * none. *
  139. * *
  140. * OUTPUT: *
  141. * none. *
  142. * *
  143. * WARNINGS: *
  144. * MPlayerCount must have been initialized at this point. *
  145. * *
  146. * HISTORY: *
  147. * 01/10/1996 BRR : Created. *
  148. *=========================================================================*/
  149. void Connect_TEN(void)
  150. {
  151. #if(TEN)
  152. typedef struct ConnectPacketTag {
  153. NetCommandType Dummy; // packet type; set to PING
  154. char Name[MPLAYER_NAME_MAX]; // player's name
  155. HousesType House; // player's ActLike
  156. unsigned char Color; // player's Color
  157. } ConnectPacketType;
  158. int num_players;
  159. int num_found;
  160. ConnectPacketType send_packet;
  161. ConnectPacketType receive_packet;
  162. int address;
  163. int found;
  164. int size;
  165. int i;
  166. CDTimerClass<SystemTimerClass> send_timer;
  167. NodeNameType *who;
  168. enum {
  169. D_TXT6_H = 7,
  170. D_MARGIN = 5,
  171. };
  172. static int x,y,w,h;
  173. char const *buf1;
  174. char const *buf2;
  175. int display = 0;
  176. RemapControlType * scheme = GadgetClass::Get_Color_Scheme();
  177. //
  178. // Clear the Players list
  179. //
  180. while (Session.Players.Count() > 0) {
  181. delete Session.Players[0];
  182. Session.Players.Delete(Session.Players[0]);
  183. }
  184. //
  185. // Add myself to the list first thing
  186. //
  187. who = new NodeNameType;
  188. strcpy(who->Name, Session.Handle);
  189. who->Player.House = Session.House;
  190. who->Player.Color = Session.ColorIdx;
  191. Session.Players.Add (who);
  192. //
  193. // Find out how many players to wait for
  194. //
  195. num_players = Session.NumPlayers - 1;
  196. num_found = 0;
  197. //
  198. // Send out a packet announcing my presence
  199. //
  200. send_packet.Dummy = NET_PING;
  201. strcpy(send_packet.Name, Session.Handle);
  202. send_packet.House = Session.House;
  203. send_packet.Color = Session.ColorIdx;
  204. Ten->Send_Global_Message(&send_packet, sizeof(send_packet), 0, -1);
  205. //
  206. // Start our packet-sending timer
  207. //
  208. send_timer = 240;
  209. //
  210. // Wait for all players to enter the game
  211. //
  212. display = 1;
  213. while (num_found < num_players) {
  214. #ifdef WIN32
  215. /*
  216. ** If we have just received input focus again after running in the background then
  217. ** we need to redraw.
  218. */
  219. if (AllSurfaces.SurfacesRestored) {
  220. AllSurfaces.SurfacesRestored=FALSE;
  221. display = 1;
  222. }
  223. #endif
  224. if (display) {
  225. Fancy_Text_Print("", 0, 0, 0, 0, TPF_TEXT);
  226. buf1 = Text_String(TXT_WAITING_FOR_CONNECTIONS);
  227. buf2 = Text_String(TXT_PRESS_ESC);
  228. w = MAX(String_Pixel_Width(buf1),String_Pixel_Width(buf2));
  229. w += (D_MARGIN * 2);
  230. h = (D_TXT6_H * 2) + (D_MARGIN * 7);
  231. x = 160 - (w / 2);
  232. y = 100 - (h / 2);
  233. Hide_Mouse();
  234. //Set_Logic_Page(SeenBuff);
  235. Dialog_Box(x * RESFACTOR, y * RESFACTOR, w * RESFACTOR, h * RESFACTOR);
  236. Fancy_Text_Print(buf1,
  237. 160 * RESFACTOR,
  238. (y + (D_MARGIN * 2)) * RESFACTOR,
  239. scheme, TBLACK, TPF_CENTER | TPF_TEXT);
  240. Fancy_Text_Print(buf2,
  241. 160 * RESFACTOR,
  242. (y + (D_MARGIN * 2) + D_TXT6_H + D_MARGIN) * RESFACTOR,
  243. scheme, TBLACK, TPF_CENTER | TPF_TEXT);
  244. Show_Mouse();
  245. display = 0;
  246. }
  247. Ten->Service();
  248. //
  249. // Check for an incoming packet; if a PING comes in, see if we already
  250. // have this player in our Players list. If not, add him.
  251. //
  252. if (Ten->Get_Global_Message (&receive_packet, &size, &address) &&
  253. receive_packet.Dummy == NET_PING) {
  254. found = 0;
  255. for (i = 1; i < Session.Players.Count(); i++) {
  256. if (Session.Players[i]->TenAddress == address) {
  257. found = 1;
  258. break;
  259. }
  260. }
  261. //
  262. // Create a new connection and a new node in the list.
  263. //
  264. if (!found) {
  265. who = new NodeNameType;
  266. strcpy(who->Name, receive_packet.Name);
  267. who->TenAddress = address;
  268. who->Player.House = receive_packet.House;
  269. who->Player.Color = (PlayerColorType)receive_packet.Color;
  270. Session.Players.Add (who);
  271. num_found++;
  272. Ten->Send_Global_Message(&send_packet, sizeof(send_packet), 1,
  273. address);
  274. }
  275. }
  276. //
  277. // If the user hits ESC, bail out.
  278. //
  279. if (Keyboard->Check()) {
  280. if (Keyboard->Get() == KN_ESC) {
  281. //Prog_End();
  282. Emergency_Exit(0);
  283. }
  284. }
  285. //
  286. // When our timer expires, re-send the packet.
  287. //
  288. if (!send_timer) {
  289. send_packet.Dummy = NET_PING;
  290. Ten->Send_Global_Message(&send_packet, sizeof(send_packet), 0, -1);
  291. send_timer = 240;
  292. }
  293. }
  294. #else
  295. return;
  296. #endif
  297. }
  298. /***************************************************************************
  299. * Destroy_TEN_Connection -- Destroys the given connection *
  300. * *
  301. * INPUT: *
  302. * id connection ID to destroy (must be a HousesType) *
  303. * error 0 = user signed off; 1 = connection error; otherwise, *
  304. * no error is shown. *
  305. * *
  306. * OUTPUT: *
  307. * none. *
  308. * *
  309. * WARNINGS: *
  310. * none. *
  311. * *
  312. * HISTORY: *
  313. * 01/11/1996 BRR : Created. *
  314. *=========================================================================*/
  315. void Destroy_TEN_Connection(int id, int error)
  316. {
  317. #if(TEN)
  318. int i;
  319. HouseClass *housep;
  320. char txt[80];
  321. //------------------------------------------------------------------------
  322. // Do nothing if the house isn't human.
  323. //------------------------------------------------------------------------
  324. housep = HouseClass::As_Pointer((HousesType)id);
  325. if (!housep || !housep->IsHuman)
  326. return;
  327. /*------------------------------------------------------------------------
  328. Create a message to display to the user
  329. ------------------------------------------------------------------------*/
  330. txt[0] = '\0';
  331. if (error==1) {
  332. sprintf(txt,Text_String(TXT_CONNECTION_LOST),Ten->Connection_Name(id));
  333. } else if (error==0) {
  334. sprintf(txt,Text_String(TXT_LEFT_GAME),Ten->Connection_Name(id));
  335. }
  336. if (strlen(txt)) {
  337. Session.Messages.Add_Message (NULL,0, txt, housep->RemapColor,
  338. TPF_TEXT, Rule.MessageDelay * TICKS_PER_MINUTE);
  339. Map.Flag_To_Redraw(false);
  340. }
  341. //------------------------------------------------------------------------
  342. // Remove this player from the Players vector
  343. //------------------------------------------------------------------------
  344. for (i = 0; i < Session.Players.Count(); i++) {
  345. if (!stricmp(Session.Players[i]->Name,housep->IniName)) {
  346. delete Session.Players[i];
  347. Session.Players.Delete(Session.Players[i]);
  348. break;
  349. }
  350. }
  351. /*------------------------------------------------------------------------
  352. Delete the TEN connection
  353. ------------------------------------------------------------------------*/
  354. Ten->Delete_Connection(id);
  355. //------------------------------------------------------------------------
  356. // Turn the player's house over to the computer's AI
  357. //------------------------------------------------------------------------
  358. housep->IsHuman = false;
  359. housep->IQ = Rule.MaxIQ;
  360. strcpy (housep->IniName,Text_String(TXT_COMPUTER));
  361. Session.NumPlayers--;
  362. /*------------------------------------------------------------------------
  363. If we're the last player left, tell the user.
  364. ------------------------------------------------------------------------*/
  365. if (Session.NumPlayers == 1) {
  366. sprintf(txt,"%s",Text_String(TXT_JUST_YOU_AND_ME));
  367. Session.Messages.Add_Message (NULL, 0, txt, housep->RemapColor,
  368. TPF_TEXT, Rule.MessageDelay * TICKS_PER_MINUTE);
  369. Map.Flag_To_Redraw(false);
  370. }
  371. #else
  372. id = id;
  373. error = error;
  374. #endif
  375. } // end of Destroy_TEN_Connection
  376. /***************************************************************************
  377. * Debug_Mono -- Custom mono prints *
  378. * *
  379. * INPUT: *
  380. * none. *
  381. * *
  382. * OUTPUT: *
  383. * none. *
  384. * *
  385. * WARNINGS: *
  386. * none. *
  387. * *
  388. * HISTORY: *
  389. * 11/27/1996 BRR : Created. *
  390. *=========================================================================*/
  391. void Debug_Mono(void)
  392. {
  393. #if(TEN)
  394. int i;
  395. int id;
  396. Mono_Printf("STATE: # Connections:%d\n",Ten->Num_Connections());
  397. for (i=0;i<Ten->Num_Connections();i++) {
  398. id = Ten->Connection_ID(i);
  399. Mono_Printf("Connection %d: Name:%s, ID:%d, Address:%d\n",
  400. i,
  401. Ten->Connection_Name(id),
  402. Ten->Connection_ID(i),
  403. Ten->Connection_Address(id));
  404. }
  405. #endif
  406. }
  407. /***************************************************************************
  408. * Send_TEN_Win_Packet -- Sends a win packet to server *
  409. * *
  410. * INPUT: *
  411. * none. *
  412. * *
  413. * OUTPUT: *
  414. * none. *
  415. * *
  416. * WARNINGS: *
  417. * none. *
  418. * *
  419. * HISTORY: *
  420. * 11/27/1996 BRR : Created. *
  421. *=========================================================================*/
  422. void Send_TEN_Win_Packet(void)
  423. {
  424. #if(TEN)
  425. char winbuf[80];
  426. char idbuf[20];
  427. int first = 1;
  428. HouseClass *hptr;
  429. int i;
  430. //
  431. // Build a special text buffer to send to the TEN server. Format:
  432. // "winner 'id id'", where 'id' is the Ten Player ID of each player
  433. // on the winning team. (For TEN, the color index is the player ID.)
  434. //
  435. sprintf(winbuf,"winner '");
  436. for (i = 0; i < Session.Players.Count(); i++) {
  437. hptr = HouseClass::As_Pointer(Session.Players[i]->Player.ID);
  438. if (!hptr->IsDefeated) {
  439. if (!first) {
  440. strcat(winbuf," ");
  441. } else {
  442. first = 0;
  443. }
  444. sprintf(idbuf,"%d", Session.Players[i]->Player.Color);
  445. strcat (winbuf, idbuf);
  446. }
  447. }
  448. strcat (winbuf,"' ");
  449. tenArSetPlayerState(winbuf);
  450. #endif // TEN
  451. } // end of Send_TEN_Win_Packet
  452. /***************************************************************************
  453. * Send_TEN_Alliance -- Sends an ally/enemy packet to server *
  454. * *
  455. * INPUT: *
  456. * whom name of player we're allying / enemying with *
  457. * ally 1 = we're allying; 0 = we're breaking the alliance *
  458. * *
  459. * OUTPUT: *
  460. * none. *
  461. * *
  462. * WARNINGS: *
  463. * none. *
  464. * *
  465. * HISTORY: *
  466. * 11/27/1996 BRR : Created. *
  467. *=========================================================================*/
  468. void Send_TEN_Alliance(char *whom, int ally)
  469. {
  470. #if(TEN)
  471. char buf[80];
  472. if (ally) {
  473. sprintf(buf,"ally '%s' ",whom);
  474. } else {
  475. sprintf(buf,"enemy '%s' ",whom);
  476. }
  477. tenArSetPlayerState(buf);
  478. #else
  479. whom = whom;
  480. ally = ally;
  481. #endif // TEN
  482. } // end of Send_TEN_Alliance
  483. /***************************************************************************
  484. * Send_TEN_Out_Of_Sync -- Announces this game out of sync *
  485. * *
  486. * INPUT: *
  487. * none. *
  488. * *
  489. * OUTPUT: *
  490. * none. *
  491. * *
  492. * WARNINGS: *
  493. * none. *
  494. * *
  495. * HISTORY: *
  496. * 11/27/1996 BRR : Created. *
  497. *=========================================================================*/
  498. void Send_TEN_Out_Of_Sync(void)
  499. {
  500. #if(TEN)
  501. tenArSetPlayerState("sync '1' ");
  502. #endif // TEN
  503. } // end of Send_TEN_Out_Of_Sync
  504. /***************************************************************************
  505. * Send_TEN_Packet_Too_Late -- Announces packet-received-too-late *
  506. * *
  507. * INPUT: *
  508. * none. *
  509. * *
  510. * OUTPUT: *
  511. * none. *
  512. * *
  513. * WARNINGS: *
  514. * none. *
  515. * *
  516. * HISTORY: *
  517. * 11/27/1996 BRR : Created. *
  518. *=========================================================================*/
  519. void Send_TEN_Packet_Too_Late(void)
  520. {
  521. #if(TEN)
  522. tenArSetPlayerState("toolate '1' ");
  523. #endif // TEN
  524. } // end of Send_TEN_Packet_Too_Late
  525. /***************************** end of ccten.cpp *****************************/