1 /*
2    Copyright (C) 2004 by James Gregory
3    Part of the GalaxyHack project
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.
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY.
9 
10    See the COPYING file for more details.
11 */
12 
13 #include "Score.h"
14 #include "Globals.h"
15 #include "RTS.h"
16 #include "PreBattle.h"
17 #include "Random.h"
18 
19 namespace Score {
20 
Score_State()21 Score_State::Score_State() {
22 	int x = 50;
23 	int y = 100;
24 
25 	for (int i = 0; i != sides.size(); ++i) {
26 		myWindows.push_back(GenWindow(x, y, RTS_SideStats, i, 0, 0));
27 
28 		x+= 250;
29 	}
30 
31 	JSDL.LoadMusic(globalSettings.bdp + "music/fission.ogg");
32 	JSDL.PlayMusic();
33 }
34 
~Score_State()35 Score_State::~Score_State() {
36 	if (gsTo == GST_PreBattle)
37 		RTS::RestartPreBattle();
38 	else
39 		PreBattle::Unload();
40 	KillAllWindows();
41 }
42 
Main()43 void Score_State::Main() {
44 	DrawWorld();
45 }
46 
MouseD(Uint8 button,Uint16 x,Uint16 y)47 void Score_State::MouseD(Uint8 button, Uint16 x, Uint16 y) {
48 	if (button == SDL_BUTTON_RIGHT)
49 		myWindows.push_back(GenWindow(x, y, Score_BasePU, 0, 0, 0));
50 }
51 
Keyboard(SDL_keysym & keysym)52 void Score_State::Keyboard(SDL_keysym& keysym) {
53 	if (keysym.sym == SDLK_ESCAPE)
54 		gsTo = GST_MainMenu;
55 }
56 
DrawWorld()57 void DrawWorld() {
58 	JSDL.BltFill(screenRect, 0);
59 
60 	int winner = -1;
61 	int sidesLeft = 0;
62 	string result;
63 
64 	for (int i  = 0; i != sides.size(); ++i) {
65 		int sideTotalCS = sides[i].GetTotalCapShips();
66 
67 		if (sideTotalCS) {
68 			++sidesLeft;
69 			winner = i;
70 		}
71 	}
72 
73 	if (winner == -1)
74 		result = "The battle must be declared a draw, as against all odds\nthe last remaining capital ships were destroyed at the exact same moment.";
75 	else if (sidesLeft > 1)
76 		result = "The battle must be declared a draw, as the maximum frame limit was exceeded.";
77 	else {
78 		result = "The battle has ended. ";
79 		result += sides[winner].name;
80 		if (sides[winner].name[sides[winner].name.size() - 1] == 's')
81 			result += " are ";
82 		else
83 			result += " is ";
84 		result += "victorious.";
85 	}
86 
87 	bool toFile = false;
88 	if (globalSettings.batch)
89 		toFile = true;
90 
91 	WriteText("Battle result:", toFile);
92 	for (int i = 0; i != sides.size(); ++i) {
93 		char output[256];
94 		sprintf(output, "%s: Points: %d Remaining capital ships: %d Remaining non capital ships: %d Total remaining health: %d", sides[i].name.c_str(), sides[i].myPoints, sides[i].GetTotalCapShips(), sides[i].GetTotalUnits() - sides[i].GetTotalCapShips(), sides[i].GetTotalHealth());
95 		WriteText(output, toFile);
96 	}
97 	WriteText(result, toFile);
98 	char output[100];
99 	sprintf(output, "Frames elapsed: %d", frameCounter);
100 	WriteText(output, toFile);
101 	WriteText("", toFile);
102 
103 	if (globalSettings.batch)
104 		gsTo = GST_TheOS;
105 
106 	boldFonts.BlitString(50, 50, 0, result);
107 
108 	DrawAllWindows();
109 }
110 
BasePU(int ix,int iy)111 BasePU::BasePU(int ix, int iy):
112 PopupMenu(ix, iy, none_constant, 0) {
113 	MenuItem tempItem;
114 
115 	tempItem.desc = "Restart battle";
116 	tempItem.choice = Score_Restart;
117 	AddItem(tempItem);
118 
119 	tempItem.desc = "Main menu";
120 	tempItem.choice = WC_Quit;
121 	AddItem(tempItem);
122 
123 	InitRects();
124 }
125 
SwitchOnChoice(Uint16 x,Uint16 y)126 bool BasePU::SwitchOnChoice(Uint16 x, Uint16 y) {
127 	if (currentSelection.choiceType == MCT_LeftCursor) {
128 		switch (currentSelection.choice) {
129 		case Score_Restart:
130 			gsTo = GST_PreBattle;
131 			break;
132 		case WC_Quit:
133 			gsTo = GST_MainMenu;
134 			closed = true;
135 			break;
136 		}
137 	}
138 
139 	return false;
140 }
141 
142 } //end namespace
143 
144 
145