CCTEN.CPP 22 KB

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