LANAPICallbacks.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. ** Command & Conquer Generals(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. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. ///////////////////////////////////////////////////////////////////////////////////////
  24. // FILE: LANAPICallbacks.cpp
  25. // Author: Chris Huybregts, October 2001
  26. // Description: LAN API Callbacks
  27. ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "strtok_r.h"
  30. #include "Common/GameEngine.h"
  31. #include "Common/GlobalData.h"
  32. #include "Common/MessageStream.h"
  33. #include "Common/MultiplayerSettings.h"
  34. #include "Common/PlayerTemplate.h"
  35. #include "Common/QuotedPrintable.h"
  36. #include "Common/RandomValue.h"
  37. #include "Common/UserPreferences.h"
  38. #include "GameClient/GameText.h"
  39. #include "GameClient/LanguageFilter.h"
  40. #include "GameClient/MapUtil.h"
  41. #include "GameClient/MessageBox.h"
  42. #include "GameLogic/GameLogic.h"
  43. #include "GameNetwork/FileTransfer.h"
  44. #include "GameNetwork/LANAPICallbacks.h"
  45. #include "GameNetwork/NetworkUtil.h"
  46. LANAPI *TheLAN = NULL;
  47. extern Bool LANbuttonPushed;
  48. #ifdef _INTERNAL
  49. // for occasional debugging...
  50. //#pragma optimize("", off)
  51. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  52. #endif
  53. //Colors used for the chat dialogs
  54. const Color playerColor = GameMakeColor(255,255,255,255);
  55. const Color gameColor = GameMakeColor(255,255,255,255);
  56. const Color gameInProgressColor = GameMakeColor(128,128,128,255);
  57. const Color chatNormalColor = GameMakeColor(50,215,230,255);
  58. const Color chatActionColor = GameMakeColor(255,0,255,255);
  59. const Color chatLocalNormalColor = GameMakeColor(255,128,0,255);
  60. const Color chatLocalActionColor = GameMakeColor(128,255,255,255);
  61. const Color chatSystemColor = GameMakeColor(255,255,255,255);
  62. const Color acceptTrueColor = GameMakeColor(0,255,0,255);
  63. const Color acceptFalseColor = GameMakeColor(255,0,0,255);
  64. UnicodeString LANAPIInterface::getErrorStringFromReturnType( ReturnType ret )
  65. {
  66. switch (ret)
  67. {
  68. case RET_OK:
  69. return TheGameText->fetch("LAN:OK");
  70. case RET_TIMEOUT:
  71. return TheGameText->fetch("LAN:ErrorTimeout");
  72. case RET_GAME_FULL:
  73. return TheGameText->fetch("LAN:ErrorGameFull");
  74. case RET_DUPLICATE_NAME:
  75. return TheGameText->fetch("LAN:ErrorDuplicateName");
  76. case RET_CRC_MISMATCH:
  77. return TheGameText->fetch("LAN:ErrorCRCMismatch");
  78. case RET_GAME_STARTED:
  79. return TheGameText->fetch("LAN:ErrorGameStarted");
  80. case RET_GAME_EXISTS:
  81. return TheGameText->fetch("LAN:ErrorGameExists");
  82. case RET_GAME_GONE:
  83. return TheGameText->fetch("LAN:ErrorGameGone");
  84. case RET_BUSY:
  85. return TheGameText->fetch("LAN:ErrorBusy");
  86. case RET_SERIAL_DUPE:
  87. return TheGameText->fetch("WOL:ChatErrorSerialDup");
  88. default:
  89. return TheGameText->fetch("LAN:ErrorUnknown");
  90. }
  91. }
  92. // On functions are (generally) the result of network traffic
  93. void LANAPI::OnAccept( UnsignedInt playerIP, Bool status )
  94. {
  95. if( AmIHost() )
  96. {
  97. for (Int i = 0; i < MAX_SLOTS; i++)
  98. {
  99. if (m_currentGame->getIP(i) == playerIP)
  100. {
  101. if(status)
  102. m_currentGame->getLANSlot(i)->setAccept();
  103. else
  104. m_currentGame->getLANSlot(i)->unAccept();
  105. break;
  106. }// if
  107. }// for
  108. if (i != MAX_SLOTS )
  109. {
  110. RequestGameOptions( GenerateGameOptionsString(), false );
  111. lanUpdateSlotList();
  112. }
  113. }//if
  114. else
  115. {
  116. //i'm not the host but if the accept came from the host...
  117. if( m_currentGame->getIP(0) == playerIP )
  118. {
  119. UnicodeString text;
  120. text = TheGameText->fetch("GUI:HostWantsToStart");
  121. OnChat(UnicodeString(L"SYSTEM"), m_localIP, text, LANCHAT_SYSTEM);
  122. }
  123. }
  124. }// void LANAPI::OnAccept( UnicodeString player, Bool status )
  125. void LANAPI::OnHasMap( UnsignedInt playerIP, Bool status )
  126. {
  127. if( AmIHost() )
  128. {
  129. for (Int i = 0; i < MAX_SLOTS; i++)
  130. {
  131. if (m_currentGame->getIP(i) == playerIP)
  132. {
  133. m_currentGame->getLANSlot(i)->setMapAvailability( status );
  134. break;
  135. }// if
  136. }// for
  137. if (i != MAX_SLOTS )
  138. {
  139. UnicodeString mapDisplayName;
  140. const MapMetaData *mapData = TheMapCache->findMap( m_currentGame->getMap() );
  141. Bool willTransfer = TRUE;
  142. if (mapData)
  143. {
  144. mapDisplayName.format(L"%ls", mapData->m_displayName.str());
  145. if (mapData->m_isOfficial)
  146. willTransfer = FALSE;
  147. }
  148. else
  149. {
  150. mapDisplayName.format(L"%hs", m_currentGame->getMap().str());
  151. willTransfer = WouldMapTransfer(m_currentGame->getMap());
  152. }
  153. if (!status)
  154. {
  155. UnicodeString text;
  156. if (willTransfer)
  157. text.format(TheGameText->fetch("GUI:PlayerNoMapWillTransfer"), m_currentGame->getLANSlot(i)->getName().str(), mapDisplayName.str());
  158. else
  159. text.format(TheGameText->fetch("GUI:PlayerNoMap"), m_currentGame->getLANSlot(i)->getName().str(), mapDisplayName.str());
  160. OnChat(UnicodeString(L"SYSTEM"), m_localIP, text, LANCHAT_SYSTEM);
  161. }
  162. lanUpdateSlotList();
  163. }
  164. }//if
  165. }// void LANAPI::OnHasMap( UnicodeString player, Bool status )
  166. void LANAPI::OnGameStartTimer( Int seconds )
  167. {
  168. UnicodeString text;
  169. if (seconds == 1)
  170. text.format(TheGameText->fetch("LAN:GameStartTimerSingular"), seconds);
  171. else
  172. text.format(TheGameText->fetch("LAN:GameStartTimerPlural"), seconds);
  173. OnChat(UnicodeString(L"SYSTEM"), m_localIP, text, LANCHAT_SYSTEM);
  174. }
  175. void LANAPI::OnGameStart( void )
  176. {
  177. //DEBUG_LOG(("Map is '%s', preview is '%s'\n", m_currentGame->getMap().str(), GetPreviewFromMap(m_currentGame->getMap()).str()));
  178. //DEBUG_LOG(("Map is '%s', INI is '%s'\n", m_currentGame->getMap().str(), GetINIFromMap(m_currentGame->getMap()).str()));
  179. #if !defined(_PLAYTEST)
  180. if (m_currentGame)
  181. {
  182. LANPreferences pref;
  183. AsciiString option;
  184. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getPlayerTemplate());
  185. pref["PlayerTemplate"] = option;
  186. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getColor());
  187. pref["Color"] = option;
  188. if (m_currentGame->amIHost())
  189. pref["Map"] = AsciiStringToQuotedPrintable(m_currentGame->getMap());
  190. pref.write();
  191. m_isInLANMenu = FALSE;
  192. //m_currentGame->startGame(0);
  193. // Set up the game network
  194. DEBUG_ASSERTCRASH(TheNetwork == NULL, ("For some reason TheNetwork isn't NULL at the start of this game. Better look into that."));
  195. if (TheNetwork != NULL) {
  196. delete TheNetwork;
  197. TheNetwork = NULL;
  198. }
  199. // Time to initialize TheNetwork for this game.
  200. TheNetwork = NetworkInterface::createNetwork();
  201. TheNetwork->init();
  202. TheNetwork->setLocalAddress(m_localIP, 8088);
  203. TheNetwork->initTransport();
  204. TheNetwork->parseUserList(m_currentGame);
  205. if (TheGameLogic->isInGame())
  206. TheGameLogic->clearGameData();
  207. Bool filesOk = DoAnyMapTransfers(m_currentGame);
  208. // see if we really have the map. if not, back out.
  209. TheMapCache->updateCache();
  210. if (!filesOk || TheMapCache->findMap(m_currentGame->getMap()) == NULL)
  211. {
  212. DEBUG_LOG(("After transfer, we didn't really have the map. Bailing...\n"));
  213. OnPlayerLeave(m_name);
  214. removeGame(m_currentGame);
  215. m_currentGame = NULL;
  216. m_inLobby = TRUE;
  217. if (TheNetwork != NULL) {
  218. delete TheNetwork;
  219. TheNetwork = NULL;
  220. }
  221. OnChat(UnicodeString::TheEmptyString, 0, TheGameText->fetch("GUI:CouldNotTransferMap"), LANCHAT_SYSTEM);
  222. return;
  223. }
  224. m_currentGame->startGame(0);
  225. // shutdown the top, but do not pop it off the stack
  226. //TheShell->hideShell();
  227. // setup the Global Data with the Map and Seed
  228. TheWritableGlobalData->m_pendingFile = m_currentGame->getMap();
  229. // send a message to the logic for a new game
  230. GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
  231. msg->appendIntegerArgument(GAME_LAN);
  232. TheWritableGlobalData->m_useFpsLimit = false;
  233. // Set the random seed
  234. InitGameLogicRandom( m_currentGame->getSeed() );
  235. DEBUG_LOG(("InitGameLogicRandom( %d )\n", m_currentGame->getSeed()));
  236. }
  237. #endif
  238. }
  239. void LANAPI::OnGameOptions( UnsignedInt playerIP, Int playerSlot, AsciiString options )
  240. {
  241. if (!m_currentGame)
  242. return;
  243. if (m_currentGame->getIP(playerSlot) != playerIP)
  244. return; // He's not in our game?!?
  245. if (m_currentGame->isGameInProgress())
  246. return; // we don't want to process any game options while in game.
  247. if (playerSlot == 0 && !m_currentGame->amIHost())
  248. {
  249. m_currentGame->setLastHeard(timeGetTime());
  250. AsciiString oldOptions = GameInfoToAsciiString(m_currentGame); // save these off for if we get booted
  251. if(ParseGameOptionsString(m_currentGame,options))
  252. {
  253. lanUpdateSlotList();
  254. updateGameOptions();
  255. }
  256. Bool booted = true;
  257. for(Int player = 1; player< MAX_SLOTS; player++)
  258. {
  259. if(m_currentGame->getIP(player) == m_localIP)
  260. {
  261. booted = false;
  262. break;
  263. }
  264. }
  265. if(booted)
  266. {
  267. // restore the options with us in so we can save prefs
  268. ParseGameOptionsString(m_currentGame, oldOptions);
  269. OnPlayerLeave(m_name);
  270. }
  271. }
  272. else
  273. {
  274. // Check for user/host updates
  275. {
  276. AsciiString key;
  277. AsciiString munkee = options;
  278. munkee.nextToken(&key, "=");
  279. //DEBUG_LOG(("GameOpt request: key=%s, val=%s from player %d\n", key.str(), munkee.str(), playerSlot));
  280. LANGameSlot *slot = m_currentGame->getLANSlot(playerSlot);
  281. if (!slot)
  282. return;
  283. if (key == "User")
  284. {
  285. slot->setLogin(munkee.str()+1);
  286. return;
  287. }
  288. else if (key == "Host")
  289. {
  290. slot->setHost(munkee.str()+1);
  291. return;
  292. }
  293. }
  294. // Parse player requests (side, color, etc)
  295. if( AmIHost() && m_localIP != playerIP)
  296. {
  297. if (options.compare("HELLO") == 0)
  298. {
  299. m_currentGame->setPlayerLastHeard(playerSlot, timeGetTime());
  300. }
  301. else
  302. {
  303. m_currentGame->setPlayerLastHeard(playerSlot, timeGetTime());
  304. Bool change = false;
  305. Bool shouldUnaccept = false;
  306. AsciiString key;
  307. options.nextToken(&key, "=");
  308. Int val = atoi(options.str()+1);
  309. DEBUG_LOG(("GameOpt request: key=%s, val=%s from player %d\n", key.str(), options.str(), playerSlot));
  310. LANGameSlot *slot = m_currentGame->getLANSlot(playerSlot);
  311. if (!slot)
  312. return;
  313. if (key == "Color")
  314. {
  315. if (val >= -1 && val < TheMultiplayerSettings->getNumColors() && val != slot->getColor() && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
  316. {
  317. Bool colorAvailable = TRUE;
  318. if(val != -1 )
  319. {
  320. for(Int i=0; i <MAX_SLOTS; i++)
  321. {
  322. LANGameSlot *checkSlot = m_currentGame->getLANSlot(i);
  323. if(val == checkSlot->getColor() && slot != checkSlot)
  324. {
  325. colorAvailable = FALSE;
  326. break;
  327. }
  328. }
  329. }
  330. if(colorAvailable)
  331. slot->setColor(val);
  332. change = true;
  333. }
  334. else
  335. {
  336. DEBUG_LOG(("Rejecting invalid color %d\n", val));
  337. }
  338. }
  339. else if (key == "PlayerTemplate")
  340. {
  341. if (val >= PLAYERTEMPLATE_MIN && val < ThePlayerTemplateStore->getPlayerTemplateCount() && val != slot->getPlayerTemplate())
  342. {
  343. slot->setPlayerTemplate(val);
  344. if (val == PLAYERTEMPLATE_OBSERVER)
  345. {
  346. slot->setColor(-1);
  347. slot->setStartPos(-1);
  348. slot->setTeamNumber(-1);
  349. }
  350. change = true;
  351. shouldUnaccept = true;
  352. }
  353. else
  354. {
  355. DEBUG_LOG(("Rejecting invalid PlayerTemplate %d\n", val));
  356. }
  357. }
  358. else if (key == "StartPos" && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
  359. {
  360. if (val >= -1 && val < MAX_SLOTS && val != slot->getStartPos())
  361. {
  362. Bool startPosAvailable = TRUE;
  363. if(val != -1)
  364. for(Int i=0; i <MAX_SLOTS; i++)
  365. {
  366. LANGameSlot *checkSlot = m_currentGame->getLANSlot(i);
  367. if(val == checkSlot->getStartPos() && slot != checkSlot)
  368. {
  369. startPosAvailable = FALSE;
  370. break;
  371. }
  372. }
  373. if(startPosAvailable)
  374. slot->setStartPos(val);
  375. change = true;
  376. shouldUnaccept = true;
  377. }
  378. else
  379. {
  380. DEBUG_LOG(("Rejecting invalid startPos %d\n", val));
  381. }
  382. }
  383. else if (key == "Team")
  384. {
  385. if (val >= -1 && val < MAX_SLOTS/2 && val != slot->getTeamNumber() && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
  386. {
  387. slot->setTeamNumber(val);
  388. change = true;
  389. shouldUnaccept = true;
  390. }
  391. else
  392. {
  393. DEBUG_LOG(("Rejecting invalid team %d\n", val));
  394. }
  395. }
  396. else if (key == "NAT")
  397. {
  398. if ((val >= FirewallHelperClass::FIREWALL_TYPE_SIMPLE) &&
  399. (val <= FirewallHelperClass::FIREWALL_TYPE_DESTINATION_PORT_DELTA))
  400. {
  401. slot->setNATBehavior((FirewallHelperClass::FirewallBehaviorType)val);
  402. DEBUG_LOG(("NAT behavior set to %d for player %d\n", val, playerSlot));
  403. change = true;
  404. }
  405. else
  406. {
  407. DEBUG_LOG(("Rejecting invalid NAT behavior %d\n", (Int)val));
  408. }
  409. }
  410. if (change)
  411. {
  412. if (shouldUnaccept)
  413. m_currentGame->resetAccepted();
  414. RequestGameOptions(GenerateGameOptionsString(), true);
  415. lanUpdateSlotList();
  416. DEBUG_LOG(("Slot value is color=%d, PlayerTemplate=%d, startPos=%d, team=%d\n",
  417. slot->getColor(), slot->getPlayerTemplate(), slot->getStartPos(), slot->getTeamNumber()));
  418. DEBUG_LOG(("Slot list updated to %s\n", GenerateGameOptionsString().str()));
  419. }
  420. }
  421. }
  422. }
  423. }
  424. /*
  425. void LANAPI::OnSlotList( ReturnType ret, LANGameInfo *theGame )
  426. {
  427. if (!theGame || theGame != m_currentGame)
  428. return;
  429. Bool foundMe = false;
  430. for (int player = 0; player < MAX_SLOTS; ++player)
  431. {
  432. if (m_currentGame->getIP(player) == m_localIP)
  433. {
  434. foundMe = true;
  435. break;
  436. }
  437. }
  438. if (!foundMe)
  439. {
  440. // I've been kicked - back to the lobby for me!
  441. // We're counting on the fact that OnPlayerLeave winds up calling reset on TheLAN.
  442. OnPlayerLeave(m_name);
  443. return;
  444. }
  445. lanUpdateSlotList();
  446. }
  447. */
  448. void LANAPI::OnPlayerJoin( Int slot, UnicodeString playerName )
  449. {
  450. if (m_currentGame && m_currentGame->getIP(0) == m_localIP)
  451. {
  452. // Someone New Joined.. lets reset the accepts
  453. m_currentGame->resetAccepted();
  454. // Send out the game options
  455. RequestGameOptions(GenerateGameOptionsString(), true);
  456. }
  457. lanUpdateSlotList();
  458. }
  459. void LANAPI::OnGameJoin( ReturnType ret, LANGameInfo *theGame )
  460. {
  461. if (ret == RET_OK)
  462. {
  463. LANbuttonPushed = true;
  464. TheShell->push( AsciiString("Menus/LanGameOptionsMenu.wnd") );
  465. //lanUpdateSlotList();
  466. LANPreferences pref;
  467. AsciiString options;
  468. options.format("PlayerTemplate=%d", pref.getPreferredFaction());
  469. RequestGameOptions(options, true);
  470. options.format("Color=%d", pref.getPreferredColor());
  471. RequestGameOptions(options, true);
  472. options.format("User=%s", m_userName.str());
  473. RequestGameOptions( options, true );
  474. options.format("Host=%s", m_hostName.str());
  475. RequestGameOptions( options, true );
  476. options.format("NAT=%d", FirewallHelperClass::FIREWALL_TYPE_SIMPLE); // BGC: This is a LAN game, so there is no firewall.
  477. RequestGameOptions( options, true );
  478. }
  479. else if (ret != RET_BUSY)
  480. {
  481. /// @todo: re-enable lobby controls? Error msgs?
  482. UnicodeString title, body;
  483. title = TheGameText->fetch("LAN:JoinFailed");
  484. body = getErrorStringFromReturnType(ret);
  485. MessageBoxOk(title, body, NULL);
  486. }
  487. }
  488. void LANAPI::OnHostLeave( void )
  489. {
  490. DEBUG_ASSERTCRASH(!m_inLobby && m_currentGame, ("Game info is gone!"));
  491. if (m_inLobby || !m_currentGame)
  492. return;
  493. LANbuttonPushed = true;
  494. DEBUG_LOG(("Host left - popping to lobby\n"));
  495. TheShell->pop();
  496. }
  497. void LANAPI::OnPlayerLeave( UnicodeString player )
  498. {
  499. DEBUG_ASSERTCRASH(!m_inLobby && m_currentGame, ("Game info is gone!"));
  500. if (m_inLobby || !m_currentGame || m_currentGame->isGameInProgress())
  501. return;
  502. if (m_name.compare(player) == 0)
  503. {
  504. // We're leaving. Save options and Pop the shell up a screen.
  505. //DEBUG_ASSERTCRASH(false, ("Slot is %d\n", m_currentGame->getLocalSlotNum()));
  506. if (m_currentGame && m_currentGame->isInGame() && m_currentGame->getLocalSlotNum() >= 0)
  507. {
  508. LANPreferences pref;
  509. AsciiString option;
  510. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getPlayerTemplate());
  511. pref["PlayerTemplate"] = option;
  512. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getColor());
  513. pref["Color"] = option;
  514. if (m_currentGame->amIHost())
  515. pref["Map"] = AsciiStringToQuotedPrintable(m_currentGame->getMap());
  516. pref.write();
  517. }
  518. LANbuttonPushed = true;
  519. DEBUG_LOG(("OnPlayerLeave says we're leaving! pop away!\n"));
  520. TheShell->pop();
  521. }
  522. else
  523. {
  524. if (m_currentGame && m_currentGame->getIP(0) == m_localIP)
  525. {
  526. // Force a new slotlist send
  527. m_lastResendTime = 0;
  528. lanUpdateSlotList();
  529. RequestGameOptions( GenerateGameOptionsString(), true );
  530. }
  531. }
  532. }
  533. void LANAPI::OnGameList( LANGameInfo *gameList )
  534. {
  535. if (m_inLobby)
  536. {
  537. LANDisplayGameList(listboxGames, gameList);
  538. }
  539. }//void LANAPI::OnGameList( LANGameInfo *gameList )
  540. void LANAPI::OnGameCreate( ReturnType ret )
  541. {
  542. if (ret == RET_OK)
  543. {
  544. LANbuttonPushed = true;
  545. TheShell->push( AsciiString("Menus/LanGameOptionsMenu.wnd") );
  546. RequestLobbyLeave( false );
  547. //RequestGameAnnounce( ); // can't do this here, since we don't have a map set
  548. }
  549. else
  550. {
  551. if(m_inLobby)
  552. {
  553. switch( ret )
  554. {
  555. case RET_GAME_EXISTS:
  556. GadgetListBoxAddEntryText(listboxChatWindow, TheGameText->fetch("LAN:ErrorGameExists"), chatSystemColor, -1, -1);
  557. break;
  558. case RET_BUSY:
  559. GadgetListBoxAddEntryText(listboxChatWindow, TheGameText->fetch("LAN:ErrorBusy"), chatSystemColor, -1, -1);
  560. break;
  561. default:
  562. GadgetListBoxAddEntryText(listboxChatWindow, TheGameText->fetch("LAN:ErrorUnknown"), chatSystemColor, -1, -1);
  563. }
  564. }
  565. }
  566. }//void OnGameCreate( ReturnType ret )
  567. void LANAPI::OnPlayerList( LANPlayer *playerList )
  568. {
  569. if (m_inLobby)
  570. {
  571. UnsignedInt selectedIP = 0;
  572. Int selectedIndex = -1;
  573. Int indexToSelect = -1;
  574. GadgetListBoxGetSelected(listboxPlayers, &selectedIndex);
  575. if (selectedIndex != -1 )
  576. selectedIP = (UnsignedInt) GadgetListBoxGetItemData(listboxPlayers, selectedIndex, 0);
  577. GadgetListBoxReset(listboxPlayers);
  578. LANPlayer *player = m_lobbyPlayers;
  579. while (player)
  580. {
  581. Int addedIndex = GadgetListBoxAddEntryText(listboxPlayers, player->getName(), playerColor, -1, -1);
  582. GadgetListBoxSetItemData(listboxPlayers, (void *)player->getIP(),addedIndex, 0 );
  583. if (selectedIP == player->getIP())
  584. indexToSelect = addedIndex;
  585. player = player->getNext();
  586. }
  587. if (indexToSelect >= 0)
  588. GadgetListBoxSetSelected(listboxPlayers, indexToSelect);
  589. }
  590. }
  591. void LANAPI::OnNameChange( UnsignedInt IP, UnicodeString newName )
  592. {
  593. OnPlayerList(m_lobbyPlayers);
  594. }
  595. void LANAPI::OnInActive(UnsignedInt IP) {
  596. }
  597. void LANAPI::OnChat( UnicodeString player, UnsignedInt ip, UnicodeString message, ChatType format )
  598. {
  599. GameWindow *chatWindow = NULL;
  600. if (m_inLobby)
  601. {
  602. chatWindow = listboxChatWindow;
  603. }
  604. else if( m_currentGame && m_currentGame->isGameInProgress() && TheShell->isShellActive())
  605. {
  606. chatWindow = listboxChatWindowScoreScreen;
  607. }
  608. else if( m_currentGame && !m_currentGame->isGameInProgress())
  609. {
  610. chatWindow = listboxChatWindowLanGame;
  611. }
  612. if (chatWindow == NULL)
  613. return;
  614. Int index = -1;
  615. UnicodeString unicodeChat;
  616. switch (format)
  617. {
  618. case LANAPIInterface::LANCHAT_SYSTEM:
  619. unicodeChat = L"";
  620. unicodeChat.concat(message);
  621. unicodeChat.concat(L"");
  622. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatSystemColor, -1, -1);
  623. break;
  624. case LANAPIInterface::LANCHAT_EMOTE:
  625. unicodeChat = player;
  626. unicodeChat.concat(L' ');
  627. unicodeChat.concat(message);
  628. if (ip == m_localIP)
  629. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatLocalActionColor, -1, -1);
  630. else
  631. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatActionColor, -1, -1);
  632. break;
  633. case LANAPIInterface::LANCHAT_NORMAL:
  634. default:
  635. {
  636. // Do the language filtering.
  637. TheLanguageFilter->filterLine(message);
  638. Color chatColor = GameMakeColor(255, 255, 255, 255);
  639. if (m_currentGame)
  640. {
  641. Int slotNum = m_currentGame->getSlotNum(player);
  642. // it'll be -1 if its invalid.
  643. if (slotNum >= 0) {
  644. GameSlot *gs = m_currentGame->getSlot(slotNum);
  645. if (gs) {
  646. Int colorIndex = gs->getColor();
  647. MultiplayerColorDefinition *def = TheMultiplayerSettings->getColor(colorIndex);
  648. if (def)
  649. chatColor = def->getColor();
  650. }
  651. }
  652. }
  653. unicodeChat = L"[";
  654. unicodeChat.concat(player);
  655. unicodeChat.concat(L"] ");
  656. unicodeChat.concat(message);
  657. if (ip == m_localIP)
  658. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatColor, -1, -1);
  659. else
  660. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatColor, -1, -1);
  661. break;
  662. }
  663. }
  664. GadgetListBoxSetItemData(chatWindow, (void *)-1, index);
  665. }