1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file highscore_gui.cpp Definition of the HighScore and EndGame windows */
9 
10 #include "stdafx.h"
11 #include "highscore.h"
12 #include "table/strings.h"
13 #include "gfx_func.h"
14 #include "table/sprites.h"
15 #include "window_gui.h"
16 #include "window_func.h"
17 #include "network/network.h"
18 #include "command_func.h"
19 #include "company_func.h"
20 #include "company_base.h"
21 #include "strings_func.h"
22 #include "hotkeys.h"
23 #include "zoom_func.h"
24 
25 #include "widgets/highscore_widget.h"
26 
27 #include "safeguards.h"
28 
29 struct EndGameHighScoreBaseWindow : Window {
30 	uint32 background_img;
31 	int8 rank;
32 
EndGameHighScoreBaseWindowEndGameHighScoreBaseWindow33 	EndGameHighScoreBaseWindow(WindowDesc *desc) : Window(desc)
34 	{
35 		this->InitNested();
36 		CLRBITS(this->flags, WF_WHITE_BORDER);
37 		ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
38 	}
39 
40 	/* Always draw a maximized window and within it the centered background */
SetupHighScoreEndWindowEndGameHighScoreBaseWindow41 	void SetupHighScoreEndWindow()
42 	{
43 		/* Resize window to "full-screen". */
44 		if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
45 
46 		this->DrawWidgets();
47 
48 		/* Standard background slices are 50 pixels high, but it's designed
49 		 * for 480 pixels total. 96% of 500 is 480. */
50 		Dimension dim = GetSpriteSize(this->background_img);
51 		Point pt = this->GetTopLeft(dim.width, dim.height * 96 / 10);
52 		/* Center Highscore/Endscreen background */
53 		for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
54 			DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * dim.height));
55 		}
56 	}
57 
58 	/** Return the coordinate of the screen such that a window of 640x480 is centered at the screen. */
GetTopLeftEndGameHighScoreBaseWindow59 	Point GetTopLeft(int x, int y)
60 	{
61 		Point pt = {std::max(0, (_screen.width / 2) - (x / 2)), std::max(0, (_screen.height / 2) - (y / 2))};
62 		return pt;
63 	}
64 
OnClickEndGameHighScoreBaseWindow65 	void OnClick(Point pt, int widget, int click_count) override
66 	{
67 		this->Close();
68 	}
69 
OnKeyPressEndGameHighScoreBaseWindow70 	EventState OnKeyPress(WChar key, uint16 keycode) override
71 	{
72 		/* All keys are 'handled' by this window but we want to make
73 		 * sure that 'quit' still works correctly. Not handling the
74 		 * quit key is enough so the main toolbar can handle it. */
75 		if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
76 
77 		switch (keycode) {
78 			/* Keys for telling we want to go on */
79 			case WKC_RETURN:
80 			case WKC_ESC:
81 			case WKC_SPACE:
82 				this->Close();
83 				return ES_HANDLED;
84 
85 			default:
86 				/* We want to handle all keys; we don't want windows in
87 				 * the background to open. Especially the ones that do
88 				 * locate themselves based on the status-/toolbars. */
89 				return ES_HANDLED;
90 		}
91 	}
92 };
93 
94 /** End game window shown at the end of the game */
95 struct EndGameWindow : EndGameHighScoreBaseWindow {
EndGameWindowEndGameWindow96 	EndGameWindow(WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
97 	{
98 		/* Pause in single-player to have a look at the highscore at your own leisure */
99 		if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
100 
101 		this->background_img = SPR_TYCOON_IMG1_BEGIN;
102 
103 		if (_local_company != COMPANY_SPECTATOR) {
104 			const Company *c = Company::Get(_local_company);
105 			if (c->old_economy[0].performance_history == SCORE_MAX) {
106 				this->background_img = SPR_TYCOON_IMG2_BEGIN;
107 			}
108 		}
109 
110 		/* In a network game show the endscores of the custom difficulty 'network' which is
111 		 * a TOP5 of that game, and not an all-time TOP5. */
112 		if (_networking) {
113 			this->window_number = SP_MULTIPLAYER;
114 			this->rank = SaveHighScoreValueNetwork();
115 		} else {
116 			/* in singleplayer mode _local company is always valid */
117 			const Company *c = Company::Get(_local_company);
118 			this->window_number = SP_CUSTOM;
119 			this->rank = SaveHighScoreValue(c);
120 		}
121 
122 		MarkWholeScreenDirty();
123 	}
124 
CloseEndGameWindow125 	void Close() override
126 	{
127 		if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
128 		ShowHighscoreTable(this->window_number, this->rank);
129 		this->EndGameHighScoreBaseWindow::Close();
130 	}
131 
OnPaintEndGameWindow132 	void OnPaint() override
133 	{
134 		this->SetupHighScoreEndWindow();
135 		Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
136 
137 		const Company *c = Company::GetIfValid(_local_company);
138 		if (c == nullptr) return;
139 
140 		/* We need to get performance from last year because the image is shown
141 		 * at the start of the new year when these things have already been copied */
142 		if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
143 			SetDParam(0, c->index);
144 			SetDParam(1, c->index);
145 			SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
146 			DrawStringMultiLine(pt.x + ScaleGUITrad(15), pt.x + ScaleGUITrad(640) - ScaleGUITrad(25), pt.y + ScaleGUITrad(90), pt.y + ScaleGUITrad(160), STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
147 		} else {
148 			SetDParam(0, c->index);
149 			SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
150 			DrawStringMultiLine(pt.x + ScaleGUITrad(36), pt.x + ScaleGUITrad(640), pt.y + ScaleGUITrad(140), pt.y + ScaleGUITrad(206), STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
151 		}
152 	}
153 };
154 
155 struct HighScoreWindow : EndGameHighScoreBaseWindow {
156 	bool game_paused_by_player; ///< True if the game was paused by the player when the highscore window was opened.
157 
HighScoreWindowHighScoreWindow158 	HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
159 	{
160 		/* pause game to show the chart */
161 		this->game_paused_by_player = _pause_mode == PM_PAUSED_NORMAL;
162 		if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
163 
164 		/* Close all always on-top windows to get a clean screen */
165 		if (_game_mode != GM_MENU) HideVitalWindows();
166 
167 		MarkWholeScreenDirty();
168 		this->window_number = difficulty; // show highscore chart for difficulty...
169 		this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
170 		this->rank = ranking;
171 	}
172 
CloseHighScoreWindow173 	void Close() override
174 	{
175 		if (_game_mode != GM_MENU) ShowVitalWindows();
176 
177 		if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
178 
179 		this->EndGameHighScoreBaseWindow::Close();
180 	}
181 
OnPaintHighScoreWindow182 	void OnPaint() override
183 	{
184 		const HighScore *hs = _highscore_table[this->window_number];
185 
186 		this->SetupHighScoreEndWindow();
187 		Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
188 
189 		SetDParam(0, _settings_game.game_creation.ending_year);
190 		DrawStringMultiLine(pt.x + ScaleGUITrad(70), pt.x + ScaleGUITrad(570), pt.y, pt.y + ScaleGUITrad(140), !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
191 
192 		/* Draw Highscore peepz */
193 		for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
194 			SetDParam(0, i + 1);
195 			DrawString(pt.x + ScaleGUITrad(40), pt.x + ScaleGUITrad(600), pt.y + ScaleGUITrad(140 + i * 55), STR_HIGHSCORE_POSITION);
196 
197 			if (hs[i].company[0] != '\0') {
198 				TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
199 
200 				SetDParamStr(0, hs[i].company);
201 				DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140 + i * 55), STR_JUST_BIG_RAW_STRING, colour);
202 				SetDParam(0, hs[i].title);
203 				SetDParam(1, hs[i].score);
204 				DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140) + FONT_HEIGHT_LARGE + ScaleGUITrad(i * 55), STR_HIGHSCORE_STATS, colour);
205 			}
206 		}
207 	}
208 };
209 
210 static const NWidgetPart _nested_highscore_widgets[] = {
211 	NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetResize(1, 1), EndContainer(),
212 };
213 
214 static WindowDesc _highscore_desc(
215 	WDP_MANUAL, nullptr, 0, 0,
216 	WC_HIGHSCORE, WC_NONE,
217 	0,
218 	_nested_highscore_widgets, lengthof(_nested_highscore_widgets)
219 );
220 
221 static WindowDesc _endgame_desc(
222 	WDP_MANUAL, nullptr, 0, 0,
223 	WC_ENDSCREEN, WC_NONE,
224 	0,
225 	_nested_highscore_widgets, lengthof(_nested_highscore_widgets)
226 );
227 
228 /**
229  * Show the highscore table for a given difficulty. When called from
230  * endgame ranking is set to the top5 element that was newly added
231  * and is thus highlighted
232  */
ShowHighscoreTable(int difficulty,int8 ranking)233 void ShowHighscoreTable(int difficulty, int8 ranking)
234 {
235 	CloseWindowByClass(WC_HIGHSCORE);
236 	new HighScoreWindow(&_highscore_desc, difficulty, ranking);
237 }
238 
239 /**
240  * Show the endgame victory screen in 2050. Update the new highscore
241  * if it was high enough
242  */
ShowEndGameChart()243 void ShowEndGameChart()
244 {
245 	/* Dedicated server doesn't need the highscore window and neither does -v null. */
246 	if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
247 
248 	HideVitalWindows();
249 	CloseWindowByClass(WC_ENDSCREEN);
250 	new EndGameWindow(&_endgame_desc);
251 }
252