1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Martiño Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #include "battle_end.h"
13 
14 #include "main_menu.h"
15 #include "program.h"
16 #include "core_data.h"
17 #include "lang.h"
18 #include "util.h"
19 #include "renderer.h"
20 #include "main_menu.h"
21 #include "sound_renderer.h"
22 #include "components.h"
23 #include "metrics.h"
24 #include "stats.h"
25 #include "auto_test.h"
26 #include "video_player.h"
27 #include "game.h"
28 #include "game_settings.h"
29 #include "leak_dumper.h"
30 
31 using namespace Shared::Util;
32 
33 namespace Glest{ namespace Game{
34 
35 // =====================================================
36 // 	class BattleEnd
37 // =====================================================
38 
BattleEnd(Program * program,const Stats * stats,ProgramState * originState)39 BattleEnd::BattleEnd(Program *program, const Stats *stats,ProgramState *originState) :
40 		ProgramState(program), menuBackgroundVideo(NULL), gameSettings(NULL) {
41 
42 	containerName= "BattleEnd";
43 
44 	SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] stats = %p\n",__FILE__,__FUNCTION__,__LINE__,stats);
45 
46 	this->originState = originState;
47 	if(stats != NULL) {
48 		this->stats= *stats;
49 	}
50 	if(originState != NULL) {
51 		Game *game = dynamic_cast<Game *>(originState);
52 		if(game != NULL) {
53 			gameSettings = new GameSettings();
54 			*gameSettings = *(game->getGameSettings());
55 		}
56 	}
57 	mouseX = 0;
58 	mouseY = 0;
59 	mouse2d = 0;
60 	renderToTexture = NULL;
61 	renderToTextureCount = 0;
62 
63 	const Metrics &metrics= Metrics::getInstance();
64 	Lang &lang= Lang::getInstance();
65 	int buttonWidth = 125;
66 	int xLocation = (metrics.getVirtualW() / 2) - (buttonWidth / 2);
67 	buttonExit.init(xLocation, 80, buttonWidth);
68 	buttonExit.setText(lang.getString("Exit"));
69 
70 	//mesage box
71 	mainMessageBox.init(lang.getString("Yes"), lang.getString("No"));
72 	mainMessageBox.setEnabled(false);
73 
74 	SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
75 
76 	if(stats->getVictory(stats->getThisFactionIndex())==true){
77 		if (Config::getInstance().getString("InternetGamesBlockScenario", "") != ""
78 				&& Config::getInstance().getBool("InternetGamesAllowed", "false") == false) {
79 			if(Config::getInstance().getString("InternetGamesBlockScenario")==gameSettings->getScenario()){
80 				Config::getInstance().setBool("InternetGamesAllowed",true);
81 				Config::getInstance().save();
82 			}
83 		}
84 	}
85 
86 	initBackgroundVideo();
87 	initBackgroundMusic();
88 
89 	GraphicComponent::applyAllCustomProperties(containerName);
90 }
91 
reloadUI()92 void BattleEnd::reloadUI() {
93 	Lang &lang= Lang::getInstance();
94 
95 	buttonExit.setText(lang.getString("Exit"));
96 	mainMessageBox.init(lang.getString("Yes"), lang.getString("No"));
97 
98 	GraphicComponent::reloadFontsForRegisterGraphicComponents(containerName);
99 }
100 
~BattleEnd()101 BattleEnd::~BattleEnd() {
102 	SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
103 
104 	if(menuBackgroundVideo != NULL) {
105 		menuBackgroundVideo->closePlayer();
106 		delete menuBackgroundVideo;
107 		menuBackgroundVideo = NULL;
108 	}
109 
110 	delete gameSettings;
111 	gameSettings = NULL;
112 
113 	delete originState;
114 	originState = NULL;
115 
116 	if(CoreData::getInstance().hasMainMenuVideoFilename() == false) {
117 		SoundRenderer::getInstance().playMusic(CoreData::getInstance().getMenuMusic());
118 	}
119 
120 	SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
121 
122 	delete renderToTexture;
123 	renderToTexture = NULL;
124 
125 	SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
126 }
127 
getBattleEndVideo(bool won)128 std::pair<string,string> BattleEnd::getBattleEndVideo(bool won) {
129 	std::pair<string,string> result;
130 
131 	string factionVideoUrl = "";
132 	string factionVideoUrlFallback = "";
133 
134 	if(gameSettings != NULL) {
135 		string currentTechName_factionPreview	 = gameSettings->getTech();
136 		string currentFactionName_factionPreview = gameSettings->getFactionTypeName(stats.getThisFactionIndex());
137 
138 		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 tech [%s] faction [%s] won = %d\n",currentTechName_factionPreview.c_str(),currentFactionName_factionPreview.c_str(),won);
139 
140 		string factionDefinitionXML = Game::findFactionLogoFile(gameSettings, NULL,currentFactionName_factionPreview + ".xml");
141 		if(factionDefinitionXML != "" && currentFactionName_factionPreview != GameConstants::RANDOMFACTION_SLOTNAME &&
142 				currentFactionName_factionPreview != GameConstants::OBSERVER_SLOTNAME && fileExists(factionDefinitionXML) == true) {
143 
144 			if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#2 tech [%s] faction [%s]\n",currentTechName_factionPreview.c_str(),currentFactionName_factionPreview.c_str());
145 
146 			XmlTree	xmlTree;
147 			std::map<string,string> mapExtraTagReplacementValues;
148 			xmlTree.load(factionDefinitionXML, Properties::getTagReplacementValues(&mapExtraTagReplacementValues));
149 			const XmlNode *factionNode= xmlTree.getRootNode();
150 			if(won == true) {
151 				if(factionNode->hasAttribute("battle-end-win-video") == true) {
152 					factionVideoUrl = factionNode->getAttribute("battle-end-win-video")->getValue();
153 				}
154 			}
155 			else {
156 				if(factionNode->hasAttribute("battle-end-lose-video") == true) {
157 					factionVideoUrl = factionNode->getAttribute("battle-end-lose-video")->getValue();
158 				}
159 			}
160 
161 			if(factionVideoUrl != "" && fileExists(factionVideoUrl) == false) {
162 				string techTreePath = "";
163 				string factionPath = "";
164 				std::vector<std::string> factionPartsList;
165 				Tokenize(factionDefinitionXML,factionPartsList,"factions/");
166 				if(factionPartsList.size() > 1) {
167 					techTreePath = factionPartsList[0];
168 
169 					string factionPath = techTreePath + "factions/" + currentFactionName_factionPreview;
170 					endPathWithSlash(factionPath);
171 					factionVideoUrl = factionPath + factionVideoUrl;
172 				}
173 			}
174 
175 			if(won == true) {
176 				factionVideoUrlFallback = Game::findFactionLogoFile(gameSettings, NULL,"battle_end_win_video.*");
177 			}
178 			else {
179 				factionVideoUrlFallback = Game::findFactionLogoFile(gameSettings, NULL,"battle_end_lose_video.*");
180 			}
181 
182 			if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#3 factionVideoUrl [%s] factionVideoUrlFallback [%s]\n",factionVideoUrl.c_str(),factionVideoUrlFallback.c_str());
183 			//printf("#3 factionVideoUrl [%s] factionVideoUrlFallback [%s]\n",factionVideoUrl.c_str(),factionVideoUrlFallback.c_str());
184 
185 			if(factionVideoUrl == "") {
186 				factionVideoUrl = factionVideoUrlFallback;
187 				factionVideoUrlFallback = "";
188 			}
189 		}
190 		//printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str());
191 	}
192 
193 	if(factionVideoUrl != "") {
194 		result.first = factionVideoUrl;
195 		result.second = factionVideoUrlFallback;
196 	}
197 	else {
198 		result.first = CoreData::getInstance().getBattleEndVideoFilename(won);
199 		result.second = CoreData::getInstance().getBattleEndVideoFilenameFallback(won);
200 	}
201 
202 	return result;
203 }
204 
getBattleEndMusic(bool won)205 string BattleEnd::getBattleEndMusic(bool won) {
206 	string result="";
207 	string resultFallback="";
208 
209 	if(gameSettings != NULL) {
210 		string currentTechName_factionPreview	 = gameSettings->getTech();
211 		string currentFactionName_factionPreview = gameSettings->getFactionTypeName(stats.getThisFactionIndex());
212 
213 		if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 tech [%s] faction [%s] won = %d\n",currentTechName_factionPreview.c_str(),currentFactionName_factionPreview.c_str(),won);
214 
215 		string factionDefinitionXML = Game::findFactionLogoFile(gameSettings, NULL,currentFactionName_factionPreview + ".xml");
216 		if(factionDefinitionXML != "" && currentFactionName_factionPreview != GameConstants::RANDOMFACTION_SLOTNAME &&
217 				currentFactionName_factionPreview != GameConstants::OBSERVER_SLOTNAME && fileExists(factionDefinitionXML) == true) {
218 
219 			if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#2 tech [%s] faction [%s]\n",currentTechName_factionPreview.c_str(),currentFactionName_factionPreview.c_str());
220 
221 			XmlTree	xmlTree;
222 			std::map<string,string> mapExtraTagReplacementValues;
223 			xmlTree.load(factionDefinitionXML, Properties::getTagReplacementValues(&mapExtraTagReplacementValues));
224 			const XmlNode *factionNode= xmlTree.getRootNode();
225 			if(won == true) {
226 				if(factionNode->hasAttribute("battle-end-win-music") == true) {
227 					result = factionNode->getAttribute("battle-end-win-music")->getValue();
228 				}
229 			}
230 			else {
231 				if(factionNode->hasAttribute("battle-end-lose-music") == true) {
232 					result = factionNode->getAttribute("battle-end-lose-music")->getValue();
233 				}
234 			}
235 
236 			if(result != "" && fileExists(result) == false) {
237 				string techTreePath = "";
238 				string factionPath = "";
239 				std::vector<std::string> factionPartsList;
240 				Tokenize(factionDefinitionXML,factionPartsList,"factions/");
241 				if(factionPartsList.size() > 1) {
242 					techTreePath = factionPartsList[0];
243 
244 					string factionPath = techTreePath + "factions/" + currentFactionName_factionPreview;
245 					endPathWithSlash(factionPath);
246 					result = factionPath + result;
247 				}
248 			}
249 
250 			if(won == true) {
251 				resultFallback = Game::findFactionLogoFile(gameSettings, NULL,"battle_end_win_music.*");
252 			}
253 			else {
254 				resultFallback = Game::findFactionLogoFile(gameSettings, NULL,"battle_end_lose_music.*");
255 			}
256 
257 			if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#3 result [%s] resultFallback [%s]\n",result.c_str(),resultFallback.c_str());
258 			//printf("#3 result [%s] resultFallback [%s]\n",result.c_str(),resultFallback.c_str());
259 
260 			if(result == "") {
261 				result = resultFallback;
262 			}
263 		}
264 		//printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str());
265 	}
266 
267 	if(result == "") {
268 		result = CoreData::getInstance().getBattleEndMusicFilename(won);
269 	}
270 
271 	return result;
272 }
273 
initBackgroundMusic()274 void BattleEnd::initBackgroundMusic() {
275 	string music = "";
276 
277 	if(stats.getThisFactionIndex() >= 0 && stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) {
278 		if(stats.getVictory(stats.getThisFactionIndex())){
279 			//header += lang.getString("Victory");
280 			music = getBattleEndMusic(true);
281 		}
282 		else{
283 			//header += lang.getString("Defeat");
284 			music = getBattleEndMusic(false);
285 		}
286 
287 		if(music != "" && fileExists(music) == true) {
288 			printf("music [%s] \n",music.c_str());
289 
290 			battleEndMusic.open(music);
291 			battleEndMusic.setNext(&battleEndMusic);
292 
293 			SoundRenderer &soundRenderer= SoundRenderer::getInstance();
294 			soundRenderer.playMusic(&battleEndMusic);
295 		}
296 	}
297 }
298 
initBackgroundVideo()299 void BattleEnd::initBackgroundVideo() {
300 	if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false &&
301 		::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == true) {
302 
303 		if(menuBackgroundVideo != NULL) {
304 			menuBackgroundVideo->closePlayer();
305 			delete menuBackgroundVideo;
306 			menuBackgroundVideo = NULL;
307 		}
308 
309 		string videoFile = "";
310 		string videoFileFallback = "";
311 
312 		if(stats.getThisFactionIndex() >= 0 && stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) {
313 			if(stats.getVictory(stats.getThisFactionIndex())){
314 				//header += lang.getString("Victory");
315 
316 				//videoFile = CoreData::getInstance().getBattleEndVideoFilename(true);
317 				//videoFileFallback = CoreData::getInstance().getBattleEndVideoFilenameFallback(true);
318 				std::pair<string,string> wonVideos = getBattleEndVideo(true);
319 				videoFile = wonVideos.first;
320 				videoFileFallback = wonVideos.second;
321 			}
322 			else{
323 				//header += lang.getString("Defeat");
324 				//videoFile = CoreData::getInstance().getBattleEndVideoFilename(false);
325 				//videoFileFallback = CoreData::getInstance().getBattleEndVideoFilenameFallback(false);
326 				std::pair<string,string> lostVideos = getBattleEndVideo(false);
327 				videoFile = lostVideos.first;
328 				videoFileFallback = lostVideos.second;
329 			}
330 		}
331 		else {
332 			//header += "Observer";
333 		}
334 
335 		if(fileExists(videoFile) || fileExists(videoFileFallback)) {
336 			printf("videoFile [%s] videoFileFallback [%s]\n",videoFile.c_str(),videoFileFallback.c_str());
337 
338 			Context *c= GraphicsInterface::getInstance().getCurrentContext();
339 			PlatformContextGl *glCtx = static_cast<ContextGl*>(c)->getPlatformContextGlPtr();
340 			SDL_Window *window = glCtx->getScreenWindow();
341 			SDL_Surface *screen = glCtx->getScreenSurface();
342 
343 			string vlcPluginsPath = Config::getInstance().getString("VideoPlayerPluginsPath","");
344 			//printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel);
345 			menuBackgroundVideo = new VideoPlayer(
346 					&Renderer::getInstance(),
347 					videoFile,
348 					videoFileFallback,
349 					window,
350 					0,0,
351 					screen->w,
352 					screen->h,
353 					screen->format->BitsPerPixel,
354 					true,
355 					vlcPluginsPath,
356 					SystemFlags::VERBOSE_MODE_ENABLED);
357 			menuBackgroundVideo->initPlayer();
358 		}
359 	}
360 }
361 
update()362 void BattleEnd::update() {
363 	if(Config::getInstance().getBool("AutoTest")){
364 		AutoTest::getInstance().updateBattleEnd(program);
365 		return;
366 	}
367 	mouse2d= (mouse2d+1) % Renderer::maxMouse2dAnim;
368 
369 	if(this->stats.getIsMasterserverMode() == true) {
370 		if(program->getWantShutdownApplicationAfterGame() == true) {
371 			program->setShutdownApplicationEnabled(true);
372 			return;
373 		}
374 		SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
375 		//program->setState(new MainMenu(program));
376 		program->initServer(program->getWindow(),false,true,true);
377 		return;
378 	}
379 }
380 
render()381 void BattleEnd::render() {
382 	if(this->stats.getIsMasterserverMode() == true) {
383 		return;
384 	}
385 	Renderer &renderer= Renderer::getInstance();
386 	//CoreData &coreData= CoreData::getInstance();
387 
388 	canRender();
389 	incrementFps();
390 
391 	//printf("In [%s::%s Line: %d] renderToTexture [%p]\n",__FILE__,__FUNCTION__,__LINE__,renderToTexture);
392 	if(menuBackgroundVideo == NULL && renderToTexture != NULL) {
393 		//printf("Rendering from texture!\n");
394 
395 		renderer.clearBuffers();
396 		renderer.reset3dMenu();
397 		renderer.clearZBuffer();
398 
399 		renderer.reset2d();
400 
401 		renderer.renderBackground(renderToTexture);
402 
403 		renderer.renderButton(&buttonExit);
404 
405 		//exit message box
406 		if(mainMessageBox.getEnabled() && mainMessageBox.getVisible()) {
407 			renderer.renderMessageBox(&mainMessageBox);
408 		}
409 
410 	    renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim);
411 	}
412 	else {
413 		//printf("Rendering to texture!\n");
414 
415 		if(menuBackgroundVideo == NULL && renderToTextureCount >= 300) {
416 			renderer.beginRenderToTexture(&renderToTexture);
417 		}
418 
419 		TextRenderer2D *textRenderer2D	= renderer.getTextRenderer();
420 		TextRenderer3D *textRenderer3D	= renderer.getTextRenderer3D();
421 		TextRenderer *textRenderer		= NULL;
422 
423 		if(Renderer::renderText3DEnabled == true) {
424 			textRenderer= textRenderer3D;
425 		}
426 		else {
427 			textRenderer= textRenderer2D;
428 		}
429 
430 		Lang &lang= Lang::getInstance();
431 
432 		renderer.clearBuffers();
433 		renderer.reset3dMenu();
434 		renderer.clearZBuffer();
435 		renderer.reset2d();
436 
437 		if(menuBackgroundVideo != NULL) {
438 			//printf("Rendering video not null!\n");
439 
440 			if(menuBackgroundVideo->isPlaying() == true) {
441 				menuBackgroundVideo->playFrame(false);
442 
443 				//printf("Rendering video playing!\n");
444 			}
445 			else {
446 				if(menuBackgroundVideo != NULL) {
447 					//initBackgroundVideo();
448 					menuBackgroundVideo->RestartVideo();
449 				}
450 			}
451 		}
452 		else {
453 			renderer.renderBackground(CoreData::getInstance().getBackgroundTexture());
454 		}
455 
456 		//int winnerIndex 	= -1;
457 		int bestScore 		= -1;
458 		//int mostKillsIndex 	= -1;
459 		int bestKills 		= -1;
460 		//int mostEnemyKillsIndex = -1;
461 		int bestEnemyKills 		= -1;
462 		//int leastDeathsIndex 	= -1;
463 		int leastDeaths			= -1;
464 		//int mostUnitsProducedIndex 	= -1;
465 		int bestUnitsProduced 		= -1;
466 		//int mostResourcesHarvestedIndex 	= -1;
467 		int bestResourcesHarvested 			= -1;
468 
469 		for(int i=0; i<stats.getFactionCount(); ++i) {
470 			if(stats.getTeam(i) == GameConstants::maxPlayers -1 + fpt_Observer) {
471 				continue;
472 			}
473 
474 			//int team= stats.getTeam(i) + 1;
475 			int kills= stats.getKills(i);
476 			if(kills > bestKills) {
477 				bestKills 	= kills;
478 				//mostKillsIndex = i;
479 			}
480 
481 			int enemykills= stats.getEnemyKills(i);
482 			if(enemykills > bestEnemyKills) {
483 				bestEnemyKills 	= enemykills;
484 				//mostEnemyKillsIndex = i;
485 			}
486 
487 			int deaths= stats.getDeaths(i);
488 			if(deaths < leastDeaths || leastDeaths < 0) {
489 				leastDeaths 	 = deaths;
490 				//leastDeathsIndex = i;
491 			}
492 
493 			int unitsProduced= stats.getUnitsProduced(i);
494 			if(unitsProduced > bestUnitsProduced) {
495 				bestUnitsProduced 	   = unitsProduced;
496 				//mostUnitsProducedIndex = i;
497 			}
498 
499 			int resourcesHarvested = stats.getResourcesHarvested(i);
500 			if(resourcesHarvested > bestResourcesHarvested) {
501 				bestResourcesHarvested 	   = resourcesHarvested;
502 				//mostResourcesHarvestedIndex = i;
503 			}
504 
505 			int score= enemykills*100 + unitsProduced*50 + resourcesHarvested/10;
506 
507 			if(score > bestScore) {
508 				bestScore 	= score;
509 				//winnerIndex = i;
510 			}
511 		}
512 
513 		bool disableStatsColorCoding = Config::getInstance().getBool("DisableBattleEndColorCoding","false");
514 
515 		if(Renderer::renderText3DEnabled == true) {
516 			textRenderer3D->begin(CoreData::getInstance().getMenuFontNormal3D());
517 		}
518 		else {
519 			textRenderer2D->begin(CoreData::getInstance().getMenuFontNormal());
520 		}
521 
522 		int lm= 20;
523 		int bm= 100;
524 
525 		int realPlayerCount = 0;
526 		for(int i = 0; i < stats.getFactionCount(); ++i) {
527 			if(stats.getTeam(i) == GameConstants::maxPlayers -1 + fpt_Observer) {
528 				continue;
529 			}
530 
531 			realPlayerCount++;
532 			int textX= lm + 60 + (realPlayerCount*100);
533 			int team= stats.getTeam(i) + 1;
534 			int kills= stats.getKills(i);
535 			int enemykills= stats.getEnemyKills(i);
536 			int deaths= stats.getDeaths(i);
537 			int unitsProduced= stats.getUnitsProduced(i);
538 			int resourcesHarvested= stats.getResourcesHarvested(i);
539 
540 			int score= enemykills*100 + unitsProduced*50 + resourcesHarvested/10;
541 			string controlString;
542 
543 			if(stats.getPersonalityType(i) == fpt_Observer) {
544 				controlString= GameConstants::OBSERVER_SLOTNAME;
545 			}
546 			else {
547 				switch(stats.getControl(i)) {
548 				case ctCpuEasy:
549 					controlString= lang.getString("CpuEasy");
550 					break;
551 				case ctCpu:
552 					controlString= lang.getString("Cpu");
553 					break;
554 				case ctCpuUltra:
555 					controlString= lang.getString("CpuUltra");
556 					break;
557 				case ctCpuMega:
558 					controlString= lang.getString("CpuMega");
559 					break;
560 				case ctNetwork:
561 					controlString= lang.getString("Network");
562 					break;
563 				case ctHuman:
564 					controlString= lang.getString("Human");
565 					break;
566 
567 				case ctNetworkCpuEasy:
568 					controlString= lang.getString("NetworkCpuEasy");
569 					break;
570 				case ctNetworkCpu:
571 					controlString= lang.getString("NetworkCpu");
572 					break;
573 				case ctNetworkCpuUltra:
574 					controlString= lang.getString("NetworkCpuUltra");
575 					break;
576 				case ctNetworkCpuMega:
577 					controlString= lang.getString("NetworkCpuMega");
578 					break;
579 
580 				default:
581 					printf("Error control = %d for i = %d\n",stats.getControl(i),i);
582 					assert(false);
583 					break;
584 				};
585 			}
586 
587 			if(stats.getControl(i) != ctHuman && stats.getControl(i) != ctNetwork ) {
588 				controlString += "\nx " + floatToStr(stats.getResourceMultiplier(i),1);
589 			}
590 			else if(stats.getPlayerLeftBeforeEnd(i)==true){
591 				controlString += "\n" +lang.getString("CpuUltra")+"\nx "+floatToStr(stats.getResourceMultiplier(i),1);
592 			}
593 
594 			if(score == bestScore && stats.getVictory(i)) {
595 				if(CoreData::getInstance().getGameWinnerTexture() != NULL) {
596 					renderer.renderTextureQuad(textX, bm+420,-1,-1,CoreData::getInstance().getGameWinnerTexture(),0.7f);
597 				}
598 			}
599 
600 			Vec3f color = stats.getPlayerColor(i);
601 			if(stats.getPlayerName(i) != "") {
602 				string textToRender=stats.getPlayerName(i).substr(0,11);
603 				if(stats.getPlayerLeftBeforeEnd(i)==true){
604 					textToRender+="\n("+getTimeDuationString(stats.getTimePlayerLeft(i),GameConstants::updateFps) + ")";
605 				}
606 
607 				textRenderer->render(textToRender.c_str(), textX, bm+400, false, &color);
608 			}
609 			else {
610 				textRenderer->render((lang.getString("Player") + " " + intToStr(i+1)).c_str(), textX, bm+400,false, &color);
611 			}
612 
613 			Vec3f highliteColor = Vec3f(WHITE.x,WHITE.y,WHITE.z);
614 			if(disableStatsColorCoding == false) {
615 				highliteColor.x = 0.85f;
616 				highliteColor.y = 0.8f;
617 				highliteColor.z = 0.07f;
618 			}
619 
620 			if(stats.getPersonalityType(i) == fpt_Observer) {
621 				textRenderer->render(lang.getString("GameOver").c_str(), textX, bm+360);
622 			}
623 			else {
624 				if(stats.getVictory(i)) {
625 					textRenderer->render(stats.getVictory(i)? lang.getString("Victory").c_str(): lang.getString("Defeat").c_str(), textX, bm+360, false, &highliteColor);
626 				}
627 				else {
628 					textRenderer->render(stats.getVictory(i)? lang.getString("Victory").c_str(): lang.getString("Defeat").c_str(), textX, bm+360);
629 				}
630 			}
631 
632 			textRenderer->render(controlString, textX, bm+320);
633 			textRenderer->render(stats.getFactionTypeName(i), textX, bm+280);
634 			textRenderer->render(intToStr(team).c_str(), textX, bm+240);
635 
636 			if(kills == bestKills) {
637 				textRenderer->render(intToStr(kills).c_str(), textX, bm+200, false,&highliteColor);
638 			}
639 			else {
640 				textRenderer->render(intToStr(kills).c_str(), textX, bm+200);
641 			}
642 			if(enemykills == bestEnemyKills) {
643 				textRenderer->render(intToStr(enemykills).c_str(), textX, bm+180, false , &highliteColor);
644 			}
645 			else {
646 				textRenderer->render(intToStr(enemykills).c_str(), textX, bm+180);
647 			}
648 			if(deaths == leastDeaths) {
649 				textRenderer->render(intToStr(deaths).c_str(), textX, bm+160,false,&highliteColor);
650 			}
651 			else {
652 				textRenderer->render(intToStr(deaths).c_str(), textX, bm+160);
653 			}
654 			if(unitsProduced == bestUnitsProduced) {
655 				textRenderer->render(intToStr(unitsProduced).c_str(), textX, bm+120,false,&highliteColor);
656 			}
657 			else {
658 				textRenderer->render(intToStr(unitsProduced).c_str(), textX, bm+120);
659 			}
660 			if(resourcesHarvested == bestResourcesHarvested) {
661 				textRenderer->render(intToStr(resourcesHarvested).c_str(), textX, bm+80,false,&highliteColor);
662 			}
663 			else {
664 				textRenderer->render(intToStr(resourcesHarvested).c_str(), textX, bm+80);
665 			}
666 			if(score == bestScore) {
667 				textRenderer->render(intToStr(score).c_str(), textX, bm+20,false,&highliteColor);
668 			}
669 			else {
670 				textRenderer->render(intToStr(score).c_str(), textX, bm+20);
671 			}
672 		}
673 
674 		textRenderer->render("\n"+(lang.getString("LeftAt")), lm, bm+400);
675 		textRenderer->render(lang.getString("Result"), lm, bm+360);
676 		textRenderer->render(lang.getString("Control"), lm, bm+320);
677 		textRenderer->render(lang.getString("Faction"), lm, bm+280);
678 		textRenderer->render(lang.getString("Team"), lm, bm+240);
679 		textRenderer->render(lang.getString("Kills"), lm, bm+200);
680 		textRenderer->render(lang.getString("EnemyKills"), lm, bm+180);
681 		textRenderer->render(lang.getString("Deaths"), lm, bm+160);
682 		textRenderer->render(lang.getString("UnitsProduced"), lm, bm+120);
683 		textRenderer->render(lang.getString("ResourcesHarvested"), lm, bm+80);
684 		textRenderer->render(lang.getString("Score"), lm, bm+20);
685 
686 		textRenderer->end();
687 
688 		if(Renderer::renderText3DEnabled == true) {
689 			textRenderer3D->begin(CoreData::getInstance().getMenuFontVeryBig3D());
690 		}
691 		else {
692 			textRenderer2D->begin(CoreData::getInstance().getMenuFontVeryBig());
693 		}
694 
695 		string header = stats.getDescription() + " - ";
696 
697 		if(stats.getThisFactionIndex() >= 0 && stats.getTeam(stats.getThisFactionIndex()) != GameConstants::maxPlayers -1 + fpt_Observer) {
698 			if(stats.getVictory(stats.getThisFactionIndex())){
699 				header += lang.getString("Victory");
700 			}
701 			else{
702 				header += lang.getString("Defeat");
703 			}
704 		}
705 		else {
706 			header += "Observer";
707 		}
708 		textRenderer->render(header, lm+250, bm+550);
709 
710 		//GameConstants::updateFps
711 		//string header2 = lang.getString("GameDurationTime","",true) + " " + floatToStr(stats.getWorldTimeElapsed() / 24.0,2);
712 
713 		string header2 = lang.getString("GameDurationTime","",true) + ": " + getTimeDuationString(stats.getFramesToCalculatePlaytime(),GameConstants::updateFps);
714 		textRenderer->render(header2, lm+250, bm+530);
715 
716 		header2 = lang.getString("GameMaxConcurrentUnitCount") + ": " + intToStr(stats.getMaxConcurrentUnitCount());
717 		textRenderer->render(header2, lm+250, bm+510);
718 
719 		header2 = lang.getString("GameTotalEndGameConcurrentUnitCount") + ": " + intToStr(stats.getTotalEndGameConcurrentUnitCount());
720 		textRenderer->render(header2, lm+250, bm+490);
721 
722 		textRenderer->end();
723 
724 		renderer.renderButton(&buttonExit);
725 
726 		//exit message box
727 		if(mainMessageBox.getEnabled() && renderToTextureCount < 300){
728 			renderer.renderMessageBox(&mainMessageBox);
729 		}
730 
731 		if(menuBackgroundVideo == NULL && renderToTexture == NULL) {
732 			renderer.renderMouse2d(mouseX, mouseY, mouse2d, 0.f);
733 		}
734 
735 		if(menuBackgroundVideo == NULL && renderToTextureCount >= 300) {
736 			renderer.endRenderToTexture(&renderToTexture);
737 		}
738 
739 		if(menuBackgroundVideo == NULL) {
740 			if(renderToTexture == NULL && renderToTextureCount < 300) {
741 				renderToTextureCount++;
742 			}
743 		}
744 	}
745 
746 	renderer.renderFPSWhenEnabled(lastFps);
747 
748 	renderer.swapBuffers();
749 }
750 
keyDown(SDL_KeyboardEvent key)751 void BattleEnd::keyDown(SDL_KeyboardEvent key){
752 	SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
753 	if(isKeyPressed(SDLK_ESCAPE,key) == true) {
754 		//program->setState(new MainMenu(program));
755 
756 		if(mainMessageBox.getEnabled()) {
757 			mainMessageBox.setEnabled(false);
758 		}
759 		else {
760 			Lang &lang= Lang::getInstance();
761 			showMessageBox(lang.getString("ExitToRootMenu"), "", true);
762 		}
763 	}
764 	else if(isKeyPressed(SDLK_RETURN,key) && mainMessageBox.getEnabled()) {
765 		SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
766 		program->setState(new MainMenu(program));
767 	}
768 }
769 
mouseDownLeft(int x,int y)770 void BattleEnd::mouseDownLeft(int x, int y){
771 	SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
772 	//program->setState(new MainMenu(program));
773 
774 	if(buttonExit.mouseClick(x,y)) {
775 		SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
776 		program->setState(new MainMenu(program));
777 	}
778 	else if(mainMessageBox.getEnabled()) {
779 		int button= 0;
780 		if(mainMessageBox.mouseClick(x, y, button)) {
781 			if(button==0) {
782 				SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
783 				program->setState(new MainMenu(program));
784 			}
785 			else {
786 				SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
787 				//close message box
788 				mainMessageBox.setEnabled(false);
789 			}
790 		}
791 	}
792 
793 }
794 
mouseMove(int x,int y,const MouseState * ms)795 void BattleEnd::mouseMove(int x, int y, const MouseState *ms){
796 	mouseX = x;
797 	mouseY = y;
798 
799 	buttonExit.mouseMove(x, y);
800 	if (mainMessageBox.getEnabled()) {
801 		mainMessageBox.mouseMove(x, y);
802 	}
803 
804 }
805 
showMessageBox(const string & text,const string & header,bool toggle)806 void BattleEnd::showMessageBox(const string &text, const string &header, bool toggle) {
807 	if(toggle == false) {
808 		mainMessageBox.setEnabled(false);
809 	}
810 
811 	if(mainMessageBox.getEnabled() == false) {
812 		mainMessageBox.setText(text);
813 		mainMessageBox.setHeader(header);
814 		mainMessageBox.setEnabled(true);
815 	}
816 	else {
817 		mainMessageBox.setEnabled(false);
818 	}
819 }
820 
821 }}//end namespace
822