LANAPICallbacks.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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 (m_currentGame)
  180. {
  181. LANPreferences pref;
  182. AsciiString option;
  183. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getPlayerTemplate());
  184. pref["PlayerTemplate"] = option;
  185. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getColor());
  186. pref["Color"] = option;
  187. if (m_currentGame->amIHost())
  188. {
  189. pref["Map"] = AsciiStringToQuotedPrintable(m_currentGame->getMap());
  190. pref.setSuperweaponRestricted( m_currentGame->getSuperweaponRestriction() > 0 );
  191. pref.setStartingCash( m_currentGame->getStartingCash() );
  192. }
  193. pref.write();
  194. m_isInLANMenu = FALSE;
  195. //m_currentGame->startGame(0);
  196. // Set up the game network
  197. DEBUG_ASSERTCRASH(TheNetwork == NULL, ("For some reason TheNetwork isn't NULL at the start of this game. Better look into that."));
  198. if (TheNetwork != NULL) {
  199. delete TheNetwork;
  200. TheNetwork = NULL;
  201. }
  202. // Time to initialize TheNetwork for this game.
  203. TheNetwork = NetworkInterface::createNetwork();
  204. TheNetwork->init();
  205. TheNetwork->setLocalAddress(m_localIP, 8088);
  206. TheNetwork->initTransport();
  207. TheNetwork->parseUserList(m_currentGame);
  208. if (TheGameLogic->isInGame())
  209. TheGameLogic->clearGameData();
  210. Bool filesOk = DoAnyMapTransfers(m_currentGame);
  211. // see if we really have the map. if not, back out.
  212. TheMapCache->updateCache();
  213. if (!filesOk || TheMapCache->findMap(m_currentGame->getMap()) == NULL)
  214. {
  215. DEBUG_LOG(("After transfer, we didn't really have the map. Bailing...\n"));
  216. OnPlayerLeave(m_name);
  217. removeGame(m_currentGame);
  218. m_currentGame = NULL;
  219. m_inLobby = TRUE;
  220. if (TheNetwork != NULL) {
  221. delete TheNetwork;
  222. TheNetwork = NULL;
  223. }
  224. OnChat(UnicodeString::TheEmptyString, 0, TheGameText->fetch("GUI:CouldNotTransferMap"), LANCHAT_SYSTEM);
  225. return;
  226. }
  227. m_currentGame->startGame(0);
  228. // shutdown the top, but do not pop it off the stack
  229. //TheShell->hideShell();
  230. // setup the Global Data with the Map and Seed
  231. TheWritableGlobalData->m_pendingFile = m_currentGame->getMap();
  232. // send a message to the logic for a new game
  233. GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
  234. msg->appendIntegerArgument(GAME_LAN);
  235. TheWritableGlobalData->m_useFpsLimit = false;
  236. // Set the random seed
  237. InitGameLogicRandom( m_currentGame->getSeed() );
  238. DEBUG_LOG(("InitGameLogicRandom( %d )\n", m_currentGame->getSeed()));
  239. }
  240. }
  241. void LANAPI::OnGameOptions( UnsignedInt playerIP, Int playerSlot, AsciiString options )
  242. {
  243. if (!m_currentGame)
  244. return;
  245. if (m_currentGame->getIP(playerSlot) != playerIP)
  246. return; // He's not in our game?!?
  247. if (m_currentGame->isGameInProgress())
  248. return; // we don't want to process any game options while in game.
  249. if (playerSlot == 0 && !m_currentGame->amIHost())
  250. {
  251. m_currentGame->setLastHeard(timeGetTime());
  252. AsciiString oldOptions = GameInfoToAsciiString(m_currentGame); // save these off for if we get booted
  253. if(ParseGameOptionsString(m_currentGame,options))
  254. {
  255. lanUpdateSlotList();
  256. updateGameOptions();
  257. }
  258. Bool booted = true;
  259. for(Int player = 1; player< MAX_SLOTS; player++)
  260. {
  261. if(m_currentGame->getIP(player) == m_localIP)
  262. {
  263. booted = false;
  264. break;
  265. }
  266. }
  267. if(booted)
  268. {
  269. // restore the options with us in so we can save prefs
  270. ParseGameOptionsString(m_currentGame, oldOptions);
  271. OnPlayerLeave(m_name);
  272. }
  273. }
  274. else
  275. {
  276. // Check for user/host updates
  277. {
  278. AsciiString key;
  279. AsciiString munkee = options;
  280. munkee.nextToken(&key, "=");
  281. //DEBUG_LOG(("GameOpt request: key=%s, val=%s from player %d\n", key.str(), munkee.str(), playerSlot));
  282. LANGameSlot *slot = m_currentGame->getLANSlot(playerSlot);
  283. if (!slot)
  284. return;
  285. if (key == "User")
  286. {
  287. slot->setLogin(munkee.str()+1);
  288. return;
  289. }
  290. else if (key == "Host")
  291. {
  292. slot->setHost(munkee.str()+1);
  293. return;
  294. }
  295. }
  296. // Parse player requests (side, color, etc)
  297. if( AmIHost() && m_localIP != playerIP)
  298. {
  299. if (options.compare("HELLO") == 0)
  300. {
  301. m_currentGame->setPlayerLastHeard(playerSlot, timeGetTime());
  302. }
  303. else
  304. {
  305. m_currentGame->setPlayerLastHeard(playerSlot, timeGetTime());
  306. Bool change = false;
  307. Bool shouldUnaccept = false;
  308. AsciiString key;
  309. options.nextToken(&key, "=");
  310. Int val = atoi(options.str()+1);
  311. DEBUG_LOG(("GameOpt request: key=%s, val=%s from player %d\n", key.str(), options.str(), playerSlot));
  312. LANGameSlot *slot = m_currentGame->getLANSlot(playerSlot);
  313. if (!slot)
  314. return;
  315. if (key == "Color")
  316. {
  317. if (val >= -1 && val < TheMultiplayerSettings->getNumColors() && val != slot->getColor() && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
  318. {
  319. Bool colorAvailable = TRUE;
  320. if(val != -1 )
  321. {
  322. for(Int i=0; i <MAX_SLOTS; i++)
  323. {
  324. LANGameSlot *checkSlot = m_currentGame->getLANSlot(i);
  325. if(val == checkSlot->getColor() && slot != checkSlot)
  326. {
  327. colorAvailable = FALSE;
  328. break;
  329. }
  330. }
  331. }
  332. if(colorAvailable)
  333. slot->setColor(val);
  334. change = true;
  335. }
  336. else
  337. {
  338. DEBUG_LOG(("Rejecting invalid color %d\n", val));
  339. }
  340. }
  341. else if (key == "PlayerTemplate")
  342. {
  343. if (val >= PLAYERTEMPLATE_MIN && val < ThePlayerTemplateStore->getPlayerTemplateCount() && val != slot->getPlayerTemplate())
  344. {
  345. slot->setPlayerTemplate(val);
  346. if (val == PLAYERTEMPLATE_OBSERVER)
  347. {
  348. slot->setColor(-1);
  349. slot->setStartPos(-1);
  350. slot->setTeamNumber(-1);
  351. }
  352. change = true;
  353. shouldUnaccept = true;
  354. }
  355. else
  356. {
  357. DEBUG_LOG(("Rejecting invalid PlayerTemplate %d\n", val));
  358. }
  359. }
  360. else if (key == "StartPos" && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
  361. {
  362. if (val >= -1 && val < MAX_SLOTS && val != slot->getStartPos())
  363. {
  364. Bool startPosAvailable = TRUE;
  365. if(val != -1)
  366. for(Int i=0; i <MAX_SLOTS; i++)
  367. {
  368. LANGameSlot *checkSlot = m_currentGame->getLANSlot(i);
  369. if(val == checkSlot->getStartPos() && slot != checkSlot)
  370. {
  371. startPosAvailable = FALSE;
  372. break;
  373. }
  374. }
  375. if(startPosAvailable)
  376. slot->setStartPos(val);
  377. change = true;
  378. shouldUnaccept = true;
  379. }
  380. else
  381. {
  382. DEBUG_LOG(("Rejecting invalid startPos %d\n", val));
  383. }
  384. }
  385. else if (key == "Team")
  386. {
  387. if (val >= -1 && val < MAX_SLOTS/2 && val != slot->getTeamNumber() && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
  388. {
  389. slot->setTeamNumber(val);
  390. change = true;
  391. shouldUnaccept = true;
  392. }
  393. else
  394. {
  395. DEBUG_LOG(("Rejecting invalid team %d\n", val));
  396. }
  397. }
  398. else if (key == "NAT")
  399. {
  400. if ((val >= FirewallHelperClass::FIREWALL_TYPE_SIMPLE) &&
  401. (val <= FirewallHelperClass::FIREWALL_TYPE_DESTINATION_PORT_DELTA))
  402. {
  403. slot->setNATBehavior((FirewallHelperClass::FirewallBehaviorType)val);
  404. DEBUG_LOG(("NAT behavior set to %d for player %d\n", val, playerSlot));
  405. change = true;
  406. }
  407. else
  408. {
  409. DEBUG_LOG(("Rejecting invalid NAT behavior %d\n", (Int)val));
  410. }
  411. }
  412. if (change)
  413. {
  414. if (shouldUnaccept)
  415. m_currentGame->resetAccepted();
  416. RequestGameOptions(GenerateGameOptionsString(), true);
  417. lanUpdateSlotList();
  418. DEBUG_LOG(("Slot value is color=%d, PlayerTemplate=%d, startPos=%d, team=%d\n",
  419. slot->getColor(), slot->getPlayerTemplate(), slot->getStartPos(), slot->getTeamNumber()));
  420. DEBUG_LOG(("Slot list updated to %s\n", GenerateGameOptionsString().str()));
  421. }
  422. }
  423. }
  424. }
  425. }
  426. /*
  427. void LANAPI::OnSlotList( ReturnType ret, LANGameInfo *theGame )
  428. {
  429. if (!theGame || theGame != m_currentGame)
  430. return;
  431. Bool foundMe = false;
  432. for (int player = 0; player < MAX_SLOTS; ++player)
  433. {
  434. if (m_currentGame->getIP(player) == m_localIP)
  435. {
  436. foundMe = true;
  437. break;
  438. }
  439. }
  440. if (!foundMe)
  441. {
  442. // I've been kicked - back to the lobby for me!
  443. // We're counting on the fact that OnPlayerLeave winds up calling reset on TheLAN.
  444. OnPlayerLeave(m_name);
  445. return;
  446. }
  447. lanUpdateSlotList();
  448. }
  449. */
  450. void LANAPI::OnPlayerJoin( Int slot, UnicodeString playerName )
  451. {
  452. if (m_currentGame && m_currentGame->getIP(0) == m_localIP)
  453. {
  454. // Someone New Joined.. lets reset the accepts
  455. m_currentGame->resetAccepted();
  456. // Send out the game options
  457. RequestGameOptions(GenerateGameOptionsString(), true);
  458. }
  459. lanUpdateSlotList();
  460. }
  461. void LANAPI::OnGameJoin( ReturnType ret, LANGameInfo *theGame )
  462. {
  463. if (ret == RET_OK)
  464. {
  465. LANbuttonPushed = true;
  466. TheShell->push( AsciiString("Menus/LanGameOptionsMenu.wnd") );
  467. //lanUpdateSlotList();
  468. LANPreferences pref;
  469. AsciiString options;
  470. options.format("PlayerTemplate=%d", pref.getPreferredFaction());
  471. RequestGameOptions(options, true);
  472. options.format("Color=%d", pref.getPreferredColor());
  473. RequestGameOptions(options, true);
  474. options.format("User=%s", m_userName.str());
  475. RequestGameOptions( options, true );
  476. options.format("Host=%s", m_hostName.str());
  477. RequestGameOptions( options, true );
  478. options.format("NAT=%d", FirewallHelperClass::FIREWALL_TYPE_SIMPLE); // BGC: This is a LAN game, so there is no firewall.
  479. RequestGameOptions( options, true );
  480. }
  481. else if (ret != RET_BUSY)
  482. {
  483. /// @todo: re-enable lobby controls? Error msgs?
  484. UnicodeString title, body;
  485. title = TheGameText->fetch("LAN:JoinFailed");
  486. body = getErrorStringFromReturnType(ret);
  487. MessageBoxOk(title, body, NULL);
  488. }
  489. }
  490. void LANAPI::OnHostLeave( void )
  491. {
  492. DEBUG_ASSERTCRASH(!m_inLobby && m_currentGame, ("Game info is gone!"));
  493. if (m_inLobby || !m_currentGame)
  494. return;
  495. LANbuttonPushed = true;
  496. DEBUG_LOG(("Host left - popping to lobby\n"));
  497. TheShell->pop();
  498. }
  499. void LANAPI::OnPlayerLeave( UnicodeString player )
  500. {
  501. DEBUG_ASSERTCRASH(!m_inLobby && m_currentGame, ("Game info is gone!"));
  502. if (m_inLobby || !m_currentGame || m_currentGame->isGameInProgress())
  503. return;
  504. if (m_name.compare(player) == 0)
  505. {
  506. // We're leaving. Save options and Pop the shell up a screen.
  507. //DEBUG_ASSERTCRASH(false, ("Slot is %d\n", m_currentGame->getLocalSlotNum()));
  508. if (m_currentGame && m_currentGame->isInGame() && m_currentGame->getLocalSlotNum() >= 0)
  509. {
  510. LANPreferences pref;
  511. AsciiString option;
  512. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getPlayerTemplate());
  513. pref["PlayerTemplate"] = option;
  514. option.format("%d", m_currentGame->getLANSlot( m_currentGame->getLocalSlotNum() )->getColor());
  515. pref["Color"] = option;
  516. if (m_currentGame->amIHost())
  517. pref["Map"] = AsciiStringToQuotedPrintable(m_currentGame->getMap());
  518. pref.write();
  519. }
  520. LANbuttonPushed = true;
  521. DEBUG_LOG(("OnPlayerLeave says we're leaving! pop away!\n"));
  522. TheShell->pop();
  523. }
  524. else
  525. {
  526. if (m_currentGame && m_currentGame->getIP(0) == m_localIP)
  527. {
  528. // Force a new slotlist send
  529. m_lastResendTime = 0;
  530. lanUpdateSlotList();
  531. RequestGameOptions( GenerateGameOptionsString(), true );
  532. }
  533. }
  534. }
  535. void LANAPI::OnGameList( LANGameInfo *gameList )
  536. {
  537. if (m_inLobby)
  538. {
  539. LANDisplayGameList(listboxGames, gameList);
  540. }
  541. }//void LANAPI::OnGameList( LANGameInfo *gameList )
  542. void LANAPI::OnGameCreate( ReturnType ret )
  543. {
  544. if (ret == RET_OK)
  545. {
  546. LANbuttonPushed = true;
  547. TheShell->push( AsciiString("Menus/LanGameOptionsMenu.wnd") );
  548. RequestLobbyLeave( false );
  549. //RequestGameAnnounce( ); // can't do this here, since we don't have a map set
  550. }
  551. else
  552. {
  553. if(m_inLobby)
  554. {
  555. switch( ret )
  556. {
  557. case RET_GAME_EXISTS:
  558. GadgetListBoxAddEntryText(listboxChatWindow, TheGameText->fetch("LAN:ErrorGameExists"), chatSystemColor, -1, -1);
  559. break;
  560. case RET_BUSY:
  561. GadgetListBoxAddEntryText(listboxChatWindow, TheGameText->fetch("LAN:ErrorBusy"), chatSystemColor, -1, -1);
  562. break;
  563. default:
  564. GadgetListBoxAddEntryText(listboxChatWindow, TheGameText->fetch("LAN:ErrorUnknown"), chatSystemColor, -1, -1);
  565. }
  566. }
  567. }
  568. }//void OnGameCreate( ReturnType ret )
  569. void LANAPI::OnPlayerList( LANPlayer *playerList )
  570. {
  571. if (m_inLobby)
  572. {
  573. UnsignedInt selectedIP = 0;
  574. Int selectedIndex = -1;
  575. Int indexToSelect = -1;
  576. GadgetListBoxGetSelected(listboxPlayers, &selectedIndex);
  577. if (selectedIndex != -1 )
  578. selectedIP = (UnsignedInt) GadgetListBoxGetItemData(listboxPlayers, selectedIndex, 0);
  579. GadgetListBoxReset(listboxPlayers);
  580. LANPlayer *player = m_lobbyPlayers;
  581. while (player)
  582. {
  583. Int addedIndex = GadgetListBoxAddEntryText(listboxPlayers, player->getName(), playerColor, -1, -1);
  584. GadgetListBoxSetItemData(listboxPlayers, (void *)player->getIP(),addedIndex, 0 );
  585. if (selectedIP == player->getIP())
  586. indexToSelect = addedIndex;
  587. player = player->getNext();
  588. }
  589. if (indexToSelect >= 0)
  590. GadgetListBoxSetSelected(listboxPlayers, indexToSelect);
  591. }
  592. }
  593. void LANAPI::OnNameChange( UnsignedInt IP, UnicodeString newName )
  594. {
  595. OnPlayerList(m_lobbyPlayers);
  596. }
  597. void LANAPI::OnInActive(UnsignedInt IP) {
  598. }
  599. void LANAPI::OnChat( UnicodeString player, UnsignedInt ip, UnicodeString message, ChatType format )
  600. {
  601. GameWindow *chatWindow = NULL;
  602. if (m_inLobby)
  603. {
  604. chatWindow = listboxChatWindow;
  605. }
  606. else if( m_currentGame && m_currentGame->isGameInProgress() && TheShell->isShellActive())
  607. {
  608. chatWindow = listboxChatWindowScoreScreen;
  609. }
  610. else if( m_currentGame && !m_currentGame->isGameInProgress())
  611. {
  612. chatWindow = listboxChatWindowLanGame;
  613. }
  614. if (chatWindow == NULL)
  615. return;
  616. Int index = -1;
  617. UnicodeString unicodeChat;
  618. switch (format)
  619. {
  620. case LANAPIInterface::LANCHAT_SYSTEM:
  621. unicodeChat = L"";
  622. unicodeChat.concat(message);
  623. unicodeChat.concat(L"");
  624. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatSystemColor, -1, -1);
  625. break;
  626. case LANAPIInterface::LANCHAT_EMOTE:
  627. unicodeChat = player;
  628. unicodeChat.concat(L' ');
  629. unicodeChat.concat(message);
  630. if (ip == m_localIP)
  631. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatLocalActionColor, -1, -1);
  632. else
  633. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatActionColor, -1, -1);
  634. break;
  635. case LANAPIInterface::LANCHAT_NORMAL:
  636. default:
  637. {
  638. // Do the language filtering.
  639. TheLanguageFilter->filterLine(message);
  640. Color chatColor = GameMakeColor(255, 255, 255, 255);
  641. if (m_currentGame)
  642. {
  643. Int slotNum = m_currentGame->getSlotNum(player);
  644. // it'll be -1 if its invalid.
  645. if (slotNum >= 0) {
  646. GameSlot *gs = m_currentGame->getSlot(slotNum);
  647. if (gs) {
  648. Int colorIndex = gs->getColor();
  649. MultiplayerColorDefinition *def = TheMultiplayerSettings->getColor(colorIndex);
  650. if (def)
  651. chatColor = def->getColor();
  652. }
  653. }
  654. }
  655. unicodeChat = L"[";
  656. unicodeChat.concat(player);
  657. unicodeChat.concat(L"] ");
  658. unicodeChat.concat(message);
  659. if (ip == m_localIP)
  660. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatColor, -1, -1);
  661. else
  662. index =GadgetListBoxAddEntryText(chatWindow, unicodeChat, chatColor, -1, -1);
  663. break;
  664. }
  665. }
  666. GadgetListBoxSetItemData(chatWindow, (void *)-1, index);
  667. }