MPStats.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #define MPSTATS_CPP
  2. /*************************************************************************************************\
  3. MPStats.cpp : Implementation of the MPStats component.
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. \*************************************************************************************************/
  8. #include "MPStats.h"
  9. #include "..\resource.h"
  10. #include "MissionBriefingScreen.h"
  11. #include "ChatWindow.h"
  12. #include "MechBayScreen.h"
  13. #include "LogisticsDialog.h"
  14. #define MP_STATS_SAVE 400
  15. MPStats::MPStats( )
  16. {
  17. helpTextArrayID = 8;
  18. status = -1;
  19. bHostLeftDlg = 0;
  20. }
  21. //-------------------------------------------------------------------------------------------------
  22. MPStats::~MPStats()
  23. {
  24. }
  25. int MPStats::init()
  26. {
  27. FitIniFile file;
  28. FullPathFileName path;
  29. path.init( artPath, "mcl_mp_stats", ".fit" );
  30. if ( NO_ERR != file.open( path ) )
  31. {
  32. char buffer2[512];
  33. sprintf( buffer2, "couldn't open file %s", (char*)path );
  34. Assert( 0, 0, buffer2 );
  35. return false;
  36. }
  37. LogisticsScreen::init( file, "Static", "Text", "Rect", "Button", "Edit" );
  38. entries[0].init( );
  39. entries[0].moveTo( rects[1].globalX(), rects[1].globalY() );
  40. entries[0].resize( rects[1].width(), rects[1].height() );
  41. for ( int i = 0; i < MAX_MC_PLAYERS-1; i++ )
  42. {
  43. entries[i+1] = entries[0];
  44. entries[i+1].move( 0, (i+1) * (entries[0].height()+1) );
  45. }
  46. for ( i = 0; i < buttonCount; i++ )
  47. buttons[i].setMessageOnRelease();
  48. bSavingStats = 0;
  49. bHostLeftDlg = 0;
  50. return true;
  51. }
  52. int __cdecl sortStats( const void* pPlayer1, const void* pPlayer2 )
  53. {
  54. MC2Player* player1 = *(MC2Player**)pPlayer1;
  55. MC2Player* player2 = *(MC2Player**)pPlayer2;
  56. if ( player1->rank > player2->rank )
  57. return 1;
  58. else if ( (player1)->rank < (player2)->rank )
  59. return -1;
  60. //else if ( (player1)->kills > (player2)->kills )
  61. // return 1;
  62. //else if ( (player1)->losses > (player2)->losses )
  63. // return 1;
  64. return 0;
  65. }
  66. void MPStats::end()
  67. {
  68. MPlayer->setInProgress(false);
  69. bSavingStats = 0;
  70. }
  71. void MPStats::begin()
  72. {
  73. MPlayer->setMode( MULTIPLAYER_MODE_RESULTS );
  74. bSavingStats = 0;
  75. status = RUNNING;
  76. beginFadeIn( .5 );
  77. for ( int i = 0; i < MAX_MC_PLAYERS; i++ )
  78. entries[i].showGUIWindow( 0 );
  79. statics[15].setColor( 0 );
  80. // need to set up map name
  81. bool bRes = 0;
  82. char text2[256];
  83. char text[256];
  84. cLoadString( IDS_MP_LM_MAP_LIST_MAP_NAME, text, 255 );
  85. sprintf( text2, text, MPlayer->missionSettings.name );
  86. textObjects[1].setText( text2 );
  87. unsigned long type = MPlayer->missionSettings.missionType ;
  88. cLoadString( IDS_MP_LM_MAP_LIST_TYPE, text, 255 );
  89. char mType[128];
  90. cLoadString( IDS_MP_LM_TYPE0 + type, mType, 127 );
  91. sprintf( text2, text, mType );
  92. textObjects[6].setText( text2 );
  93. unsigned long numPlayers = MPlayer->missionSettings.maxPlayers;
  94. cLoadString( IDS_MP_LM_MAP_LIST_MAX_PLAYERS, text, 255 );
  95. sprintf( text2, text, numPlayers );
  96. textObjects[7].setText( text2 );
  97. }
  98. int MPStats::handleMessage( unsigned long what, unsigned long who )
  99. {
  100. if ( who == MP_STATS_SAVE )
  101. {
  102. LogisticsVariantDialog::instance()->beginTranscript();
  103. bSavingStats = true;
  104. }
  105. else if ( who == MB_MSG_NEXT )
  106. {
  107. status = NEXT;
  108. end();
  109. beginFadeOut(.5f);
  110. statics[15].setTexture( (unsigned long)0 );
  111. statics[15].setColor( 0 );
  112. }
  113. return 1;
  114. }
  115. void MPStats::render( int xOffset, int yOffset )
  116. {
  117. LogisticsScreen::render( xOffset, yOffset );
  118. for ( int i = 0; i < MAX_MC_PLAYERS; i++ )
  119. {
  120. entries[i].render( 0, 0 );
  121. }
  122. ChatWindow::instance()->render(xOffset, yOffset);
  123. if ( bSavingStats )
  124. {
  125. LogisticsVariantDialog::instance()->render();
  126. }
  127. if ( bHostLeftDlg )
  128. {
  129. LogisticsOneButtonDialog::instance()->render();
  130. }
  131. }
  132. void MPStats::update()
  133. {
  134. if ( status == RUNNING && !statics[15].getColor() )
  135. {
  136. long textureHandle = MissionBriefingScreen::getMissionTGA( MPlayer->missionSettings.map );
  137. statics[15].setTexture( textureHandle );
  138. statics[15].setUVs( 0, 127, 127, 0 );
  139. statics[15].setColor( 0xffffffff );
  140. long playerCount = 0;
  141. const MC2Player* players = MPlayer->getPlayers(playerCount);
  142. const MC2Player* sorted[MAX_MC_PLAYERS];
  143. int winnerCount = 0;
  144. for ( int j = 0; j < playerCount; j++ )
  145. {
  146. sorted[j] = &players[j];
  147. if ( sorted[j]->rank == 1 )
  148. winnerCount++;
  149. }
  150. bool scoreShown[MAX_MC_PLAYERS]; // keep track of whose shown the score
  151. memset( scoreShown, 0, sizeof( bool ) * MAX_MC_PLAYERS );
  152. unsigned long winnerColor = 0xffFFCC00; // gold
  153. if ( winnerCount > 1 )
  154. winnerColor = 0xffA6A6A6;
  155. qsort( sorted, playerCount, sizeof( MC2Player*), sortStats );
  156. for ( int i = 0; i < MAX_MC_PLAYERS; i++ )
  157. {
  158. if ( i < playerCount )
  159. {
  160. entries[i].setData( sorted[i], winnerColor, !scoreShown[sorted[i]->team] );
  161. scoreShown[sorted[i]->team] = true;
  162. entries[i].showGUIWindow( true );
  163. }
  164. else
  165. entries[i].showGUIWindow( 0 );
  166. }
  167. }
  168. if ( bSavingStats )
  169. {
  170. LogisticsVariantDialog::instance()->update();
  171. if ( LogisticsVariantDialog::instance()->getStatus() == YES )
  172. {
  173. EString str = LogisticsVariantDialog::instance()->getFileName();
  174. FullPathFileName oldPath;
  175. FullPathFileName newPath;
  176. newPath.init( "data\\multiplayer\\transcripts\\", str, ".txt");
  177. oldPath.init( "data\\multiplayer\\transcripts\\", "transcript", ".txt" );
  178. LogisticsVariantDialog::instance()->end();
  179. CopyFile(oldPath, newPath,0);
  180. bSavingStats = 0;
  181. }
  182. else if ( LogisticsVariantDialog::instance()->getStatus() == NO )
  183. {
  184. if ( LogisticsVariantDialog::instance()->isDone() )
  185. bSavingStats = 0;
  186. }
  187. }
  188. else if ( bHostLeftDlg )
  189. {
  190. LogisticsOneButtonDialog::instance()->update();
  191. if ( LogisticsOneButtonDialog::instance()->isDone() )
  192. {
  193. LogisticsOneButtonDialog::instance()->end();
  194. bHostLeftDlg = 0;
  195. }
  196. }
  197. else if ( !ChatWindow::instance()->pointInside(userInput->getMouseX(), userInput->getMouseY()) )
  198. LogisticsScreen::update();
  199. if ( ChatWindow::instance() )
  200. ChatWindow::instance()->update();
  201. }
  202. void MPStats::setHostLeftDlg( const char* hostName )
  203. {
  204. char leaveStr[256];
  205. char formatStr[256];
  206. cLoadString( IDS_PLAYER_LEFT, leaveStr, 255 );
  207. sprintf( formatStr, leaveStr, hostName );
  208. LogisticsOneButtonDialog::instance()->setText( IDS_PLAYER_LEFT,
  209. IDS_DIALOG_OK, IDS_DIALOG_OK );
  210. LogisticsOneButtonDialog::instance()->setText( formatStr );
  211. LogisticsOneButtonDialog::instance()->begin();
  212. bHostLeftDlg = true;
  213. }
  214. //*************************************************************************************************
  215. MPStatsEntry::MPStatsEntry()
  216. {
  217. overlayColor = 0;
  218. }
  219. void MPStatsEntry::render( int x, int y )
  220. {
  221. LogisticsScreen::render( x, y );
  222. }
  223. MPStatsEntry::~MPStatsEntry()
  224. {
  225. }
  226. void MPStatsEntry::init()
  227. {
  228. FitIniFile file;
  229. FullPathFileName path;
  230. path.init( artPath, "mcl_mp_scoreboard", ".fit" );
  231. if ( NO_ERR != file.open( path ) )
  232. {
  233. char buffer2[512];
  234. sprintf( buffer2, "couldn't open file %s", (char*)path );
  235. Assert( 0, 0, buffer2 );
  236. return;
  237. }
  238. LogisticsScreen::init( file, "Static", "Text", "Rect", "Button", "Edit" );
  239. aObject::init( rects[2].left(), rects[2].top(), rects[5].right() - rects[0].left(), rects[2].height() );
  240. overlayColor = 0;
  241. }
  242. void MPStatsEntry::setData(const MC2Player* data, bool bShowScore )
  243. {
  244. long color = data ? MPlayer->colors[data->baseColor[BASECOLOR_TEAM]] : 0x00000000;
  245. long color2 = data ? MPlayer->colors[data->stripeColor] : 0x00000000;
  246. long scoreColor = 0xffffffff;
  247. if ( data && MPlayer->allUnitsDestroyed[data->commanderID] )
  248. scoreColor = 0xff7f7f7f;
  249. rects[0].setColor( color2 );
  250. rects[1].setColor( color );
  251. long textColor = 0xff000000;
  252. if ( ((color & 0xff) + ( (color & 0xff00)>>8 ) + ( (color & 0xff0000)>>16 ))/3 < 85 )
  253. textColor = 0xffffffff;
  254. if ( !data )
  255. {
  256. textObjects[0].setText( IDS_MP_STATS_PLAYER_NAME );
  257. textObjects[1].setText( "" );
  258. textObjects[2].setText( IDS_MP_STATS_SCORE );
  259. textObjects[3].setText( IDS_MP_STATS_KILLS );
  260. textObjects[4].setText( IDS_MP_STATS_LOSSES );
  261. }
  262. else
  263. {
  264. textObjects[1].setColor(scoreColor);
  265. textObjects[2].setColor(scoreColor);
  266. textObjects[3].setColor(scoreColor);
  267. textObjects[4].setColor(scoreColor);
  268. textObjects[0].setColor( textColor );
  269. textObjects[0].setText( data->name );
  270. char team[32];
  271. sprintf( team, "%ld", data->teamSelected + 1 );
  272. textObjects[1].setText( team );
  273. // score needs to do here
  274. sprintf( team, "%ld", data->score );
  275. if ( MPlayer->missionSettings.missionType == MISSION_TYPE_KING_OF_THE_HILL )
  276. {
  277. sprintf( team, "%ld:%.2ld", data->score/60, data->score % 60 );
  278. }
  279. if ( bShowScore )
  280. textObjects[2].setText( team );
  281. else
  282. textObjects[2].setText( "" );
  283. sprintf( team, "%ld", data->kills );
  284. textObjects[3].setText( team );
  285. sprintf( team, "%ld", data->losses );
  286. textObjects[4].setText( team );
  287. }
  288. }
  289. long MPStatsEntry:: getPlayerHeadingX()
  290. {
  291. return location[0].x;
  292. }
  293. long MPStatsEntry:: getRankingX()
  294. {
  295. return textObjects[0].globalX();
  296. }
  297. long MPStatsEntry:: getKillsX()
  298. {
  299. return textObjects[1].globalX();
  300. }
  301. long MPStatsEntry:: getLossesX()
  302. {
  303. return textObjects[2].globalX();
  304. }
  305. //*************************************************************************************************
  306. MPStatsResultsEntry::MPStatsResultsEntry()
  307. {
  308. overlayColor = 0;
  309. }
  310. MPStatsResultsEntry::~MPStatsResultsEntry()
  311. {
  312. }
  313. void MPStatsResultsEntry::render( int x, int y )
  314. {
  315. LogisticsScreen::render( x, y );
  316. //if ( overlayColor )
  317. //{
  318. // GUI_RECT rect = { textObjects[5].left(), rects[0].top(), rects[5].right(), rects[0].y() + rects[0].height() };
  319. // drawRect( rect, overlayColor );
  320. //}
  321. }
  322. void MPStatsResultsEntry::init()
  323. {
  324. FitIniFile file;
  325. FullPathFileName path;
  326. path.init( artPath, "mcl_mp_stats_entry", ".fit" );
  327. if ( NO_ERR != file.open( path ) )
  328. {
  329. char buffer2[512];
  330. sprintf( buffer2, "couldn't open file %s", (char*)path );
  331. Assert( 0, 0, buffer2 );
  332. return;
  333. }
  334. LogisticsScreen::init( file, "Static", "Text", "Rect", "Button", "Edit" );
  335. aObject::init( 0, 0, rects[0].width(), rects[0].height() );
  336. }
  337. void MPStatsResultsEntry::setData(const MC2Player* data, unsigned long laurelColor, bool bShowScore )
  338. {
  339. rects[4].setColor( MPlayer->colors[data->baseColor[BASECOLOR_TEAM]] );
  340. rects[2].setColor( MPlayer->colors[data->stripeColor] );
  341. if ( data && MPlayer->allUnitsDestroyed[data->commanderID] )
  342. overlayColor = 0x7f000000;
  343. else
  344. overlayColor = 0;
  345. long color = MPlayer->colors[data->baseColor[BASECOLOR_TEAM]];
  346. long textColor = 0xff000000;
  347. if ( ((color & 0xff) + ( (color & 0xff00)>>8 ) + ( (color & 0xff0000)>>16 ))/3 < 85 )
  348. textColor = 0xffffffff;
  349. textObjects[3].setText( data->name );
  350. textObjects[4].setText( data->unitName );
  351. textObjects[3].setColor( textColor );
  352. textObjects[4].setColor( textColor );
  353. char text[64];
  354. sprintf( text, "%ld", MPlayer->teamScore[data->team] );
  355. if ( MPlayer->missionSettings.missionType == MISSION_TYPE_KING_OF_THE_HILL )
  356. {
  357. sprintf( text, "%ld:%.2ld", MPlayer->teamScore[data->team]/60, MPlayer->teamScore[data->team] % 60 );
  358. }
  359. if ( bShowScore )
  360. textObjects[0].setText( text );
  361. else
  362. textObjects[0].setText( "" );
  363. sprintf( text, "%ld", data->kills );
  364. textObjects[1].setText( text );
  365. sprintf( text, "%ld", data->losses );
  366. textObjects[2].setText( text );
  367. sprintf( text, "%ld", data->teamSelected + 1);
  368. textObjects[5].setText( text );
  369. char path[256];
  370. strcpy( path, "data\\multiplayer\\insignia\\" );
  371. strcat( path, data->insigniaFile );
  372. if ( data->winner )
  373. {
  374. statics[1].setColor( laurelColor );
  375. rects[7].setColor( laurelColor );
  376. }
  377. else
  378. {
  379. statics[1].setColor( 0 );
  380. rects[7].setColor( 0 );
  381. }
  382. if ( fileExists( path ) )
  383. {
  384. statics[0].setTexture( path );
  385. statics[0].setUVs( 0, 0, 32, 32 );
  386. }
  387. else
  388. {
  389. TGAFileHeader* pData = (TGAFileHeader*)MPlayer->insigniaList[data->commanderID];
  390. if ( pData )
  391. {
  392. int size = pData->pixel_depth/8;
  393. int ID = mcTextureManager->textureFromMemory( (unsigned long*)(pData+1), gos_Texture_Solid, 0, pData->width, size );
  394. statics[0].setTexture( ID );
  395. statics[0].setUVs( 0, 32, 32, 0 );
  396. }
  397. }
  398. }
  399. //*************************************************************************************************
  400. // end of file ( MPStats.cpp )