Fix: CmdSetAutoReplace didn't validate group type and engine type match (#9950)
[openttd-github.git] / src / highscore_gui.cpp
blob0ccc288f05deec23a70d0587537365fa57ed20b2
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 */
8 /** @file highscore_gui.cpp Definition of the HighScore and EndGame windows */
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 #include "misc_cmd.h"
26 #include "widgets/highscore_widget.h"
28 #include "safeguards.h"
30 struct EndGameHighScoreBaseWindow : Window {
31 uint32 background_img;
32 int8 rank;
34 EndGameHighScoreBaseWindow(WindowDesc *desc) : Window(desc)
36 this->InitNested();
37 CLRBITS(this->flags, WF_WHITE_BORDER);
38 ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
41 /* Always draw a maximized window and within it the centered background */
42 void SetupHighScoreEndWindow()
44 /* Resize window to "full-screen". */
45 if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
47 this->DrawWidgets();
49 /* Standard background slices are 50 pixels high, but it's designed
50 * for 480 pixels total. 96% of 500 is 480. */
51 Dimension dim = GetSpriteSize(this->background_img);
52 Point pt = this->GetTopLeft(dim.width, dim.height * 96 / 10);
53 /* Center Highscore/Endscreen background */
54 for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
55 DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * dim.height));
59 /** Return the coordinate of the screen such that a window of 640x480 is centered at the screen. */
60 Point GetTopLeft(int x, int y)
62 Point pt = {std::max(0, (_screen.width / 2) - (x / 2)), std::max(0, (_screen.height / 2) - (y / 2))};
63 return pt;
66 void OnClick(Point pt, int widget, int click_count) override
68 this->Close();
71 EventState OnKeyPress(WChar key, uint16 keycode) override
73 /* All keys are 'handled' by this window but we want to make
74 * sure that 'quit' still works correctly. Not handling the
75 * quit key is enough so the main toolbar can handle it. */
76 if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
78 switch (keycode) {
79 /* Keys for telling we want to go on */
80 case WKC_RETURN:
81 case WKC_ESC:
82 case WKC_SPACE:
83 this->Close();
84 return ES_HANDLED;
86 default:
87 /* We want to handle all keys; we don't want windows in
88 * the background to open. Especially the ones that do
89 * locate themselves based on the status-/toolbars. */
90 return ES_HANDLED;
95 /** End game window shown at the end of the game */
96 struct EndGameWindow : EndGameHighScoreBaseWindow {
97 EndGameWindow(WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
99 /* Pause in single-player to have a look at the highscore at your own leisure */
100 if (!_networking) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, true);
102 this->background_img = SPR_TYCOON_IMG1_BEGIN;
104 if (_local_company != COMPANY_SPECTATOR) {
105 const Company *c = Company::Get(_local_company);
106 if (c->old_economy[0].performance_history == SCORE_MAX) {
107 this->background_img = SPR_TYCOON_IMG2_BEGIN;
111 /* In a network game show the endscores of the custom difficulty 'network' which is
112 * a TOP5 of that game, and not an all-time TOP5. */
113 if (_networking) {
114 this->window_number = SP_MULTIPLAYER;
115 this->rank = SaveHighScoreValueNetwork();
116 } else {
117 /* in singleplayer mode _local company is always valid */
118 const Company *c = Company::Get(_local_company);
119 this->window_number = SP_CUSTOM;
120 this->rank = SaveHighScoreValue(c);
123 MarkWholeScreenDirty();
126 void Close() override
128 if (!_networking) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, false); // unpause
129 ShowHighscoreTable(this->window_number, this->rank);
130 this->EndGameHighScoreBaseWindow::Close();
133 void OnPaint() override
135 this->SetupHighScoreEndWindow();
136 Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
138 const Company *c = Company::GetIfValid(_local_company);
139 if (c == nullptr) return;
141 /* We need to get performance from last year because the image is shown
142 * at the start of the new year when these things have already been copied */
143 if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
144 SetDParam(0, c->index);
145 SetDParam(1, c->index);
146 SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
147 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);
148 } else {
149 SetDParam(0, c->index);
150 SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
151 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);
156 struct HighScoreWindow : EndGameHighScoreBaseWindow {
157 bool game_paused_by_player; ///< True if the game was paused by the player when the highscore window was opened.
159 HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
161 /* pause game to show the chart */
162 this->game_paused_by_player = _pause_mode == PM_PAUSED_NORMAL;
163 if (!_networking && !this->game_paused_by_player) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, true);
165 /* Close all always on-top windows to get a clean screen */
166 if (_game_mode != GM_MENU) HideVitalWindows();
168 MarkWholeScreenDirty();
169 this->window_number = difficulty; // show highscore chart for difficulty...
170 this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
171 this->rank = ranking;
174 void Close() override
176 if (_game_mode != GM_MENU) ShowVitalWindows();
178 if (!_networking && !this->game_paused_by_player) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, false); // unpause
180 this->EndGameHighScoreBaseWindow::Close();
183 void OnPaint() override
185 const HighScore *hs = _highscore_table[this->window_number];
187 this->SetupHighScoreEndWindow();
188 Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
190 SetDParam(0, _settings_game.game_creation.ending_year);
191 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);
193 /* Draw Highscore peepz */
194 for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
195 SetDParam(0, i + 1);
196 DrawString(pt.x + ScaleGUITrad(40), pt.x + ScaleGUITrad(600), pt.y + ScaleGUITrad(140 + i * 55), STR_HIGHSCORE_POSITION);
198 if (hs[i].company[0] != '\0') {
199 TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
201 SetDParamStr(0, hs[i].company);
202 DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140 + i * 55), STR_JUST_BIG_RAW_STRING, colour);
203 SetDParam(0, hs[i].title);
204 SetDParam(1, hs[i].score);
205 DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140) + FONT_HEIGHT_LARGE + ScaleGUITrad(i * 55), STR_HIGHSCORE_STATS, colour);
211 static const NWidgetPart _nested_highscore_widgets[] = {
212 NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetResize(1, 1), EndContainer(),
215 static WindowDesc _highscore_desc(
216 WDP_MANUAL, nullptr, 0, 0,
217 WC_HIGHSCORE, WC_NONE,
219 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
222 static WindowDesc _endgame_desc(
223 WDP_MANUAL, nullptr, 0, 0,
224 WC_ENDSCREEN, WC_NONE,
226 _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
230 * Show the highscore table for a given difficulty. When called from
231 * endgame ranking is set to the top5 element that was newly added
232 * and is thus highlighted
234 void ShowHighscoreTable(int difficulty, int8 ranking)
236 CloseWindowByClass(WC_HIGHSCORE);
237 new HighScoreWindow(&_highscore_desc, difficulty, ranking);
241 * Show the endgame victory screen in 2050. Update the new highscore
242 * if it was high enough
244 void ShowEndGameChart()
246 /* Dedicated server doesn't need the highscore window and neither does -v null. */
247 if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
249 HideVitalWindows();
250 CloseWindowByClass(WC_ENDSCREEN);
251 new EndGameWindow(&_endgame_desc);