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/>.
8 /** @file highscore_gui.cpp Definition of the HighScore and EndGame windows */
11 #include "highscore.h"
12 #include "table/strings.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"
23 #include "zoom_func.h"
25 #include "timer/timer.h"
26 #include "timer/timer_game_calendar.h"
28 #include "widgets/highscore_widget.h"
30 #include "safeguards.h"
32 struct EndGameHighScoreBaseWindow
: Window
{
33 uint32_t background_img
;
36 EndGameHighScoreBaseWindow(WindowDesc
&desc
) : Window(desc
)
39 CLRBITS(this->flags
, WF_WHITE_BORDER
);
40 ResizeWindow(this, _screen
.width
- this->width
, _screen
.height
- this->height
);
43 /* Always draw a maximized window and within it the centered background */
44 void SetupHighScoreEndWindow()
46 /* Resize window to "full-screen". */
47 if (this->width
!= _screen
.width
|| this->height
!= _screen
.height
) ResizeWindow(this, _screen
.width
- this->width
, _screen
.height
- this->height
);
51 /* Standard background slices are 50 pixels high, but it's designed
52 * for 480 pixels total. 96% of 500 is 480. */
53 Dimension dim
= GetSpriteSize(this->background_img
);
54 Point pt
= this->GetTopLeft(dim
.width
, dim
.height
* 96 / 10);
55 /* Center Highscore/Endscreen background */
56 for (uint i
= 0; i
< 10; i
++) { // the image is split into 10 50px high parts
57 DrawSprite(this->background_img
+ i
, PAL_NONE
, pt
.x
, pt
.y
+ (i
* dim
.height
));
61 /** Return the coordinate of the screen such that a window of 640x480 is centered at the screen. */
62 Point
GetTopLeft(int x
, int y
)
64 Point pt
= {std::max(0, (_screen
.width
/ 2) - (x
/ 2)), std::max(0, (_screen
.height
/ 2) - (y
/ 2))};
68 void OnClick([[maybe_unused
]] Point pt
, [[maybe_unused
]] WidgetID widget
, [[maybe_unused
]] int click_count
) override
73 EventState
OnKeyPress([[maybe_unused
]] char32_t key
, uint16_t keycode
) override
75 /* All keys are 'handled' by this window but we want to make
76 * sure that 'quit' still works correctly. Not handling the
77 * quit key is enough so the main toolbar can handle it. */
78 if (IsQuitKey(keycode
)) return ES_NOT_HANDLED
;
81 /* Keys for telling we want to go on */
89 /* We want to handle all keys; we don't want windows in
90 * the background to open. Especially the ones that do
91 * locate themselves based on the status-/toolbars. */
97 /** End game window shown at the end of the game */
98 struct EndGameWindow
: EndGameHighScoreBaseWindow
{
99 EndGameWindow(WindowDesc
&desc
) : EndGameHighScoreBaseWindow(desc
)
101 /* Pause in single-player to have a look at the highscore at your own leisure */
102 if (!_networking
) Command
<CMD_PAUSE
>::Post(PM_PAUSED_NORMAL
, true);
104 this->background_img
= SPR_TYCOON_IMG1_BEGIN
;
106 if (_local_company
!= COMPANY_SPECTATOR
) {
107 const Company
*c
= Company::Get(_local_company
);
108 if (c
->old_economy
[0].performance_history
== SCORE_MAX
) {
109 this->background_img
= SPR_TYCOON_IMG2_BEGIN
;
113 /* In a network game show the endscores of the custom difficulty 'network' which is
114 * a TOP5 of that game, and not an all-time TOP5. */
116 this->window_number
= SP_MULTIPLAYER
;
117 this->rank
= SaveHighScoreValueNetwork();
119 /* in singleplayer mode _local company is always valid */
120 const Company
*c
= Company::Get(_local_company
);
121 this->window_number
= SP_CUSTOM
;
122 this->rank
= SaveHighScoreValue(c
);
125 MarkWholeScreenDirty();
128 void Close([[maybe_unused
]] int data
= 0) override
130 if (!_networking
) Command
<CMD_PAUSE
>::Post(PM_PAUSED_NORMAL
, false); // unpause
131 if (_game_mode
!= GM_MENU
&& !_exit_game
) ShowHighscoreTable(this->window_number
, this->rank
);
132 this->EndGameHighScoreBaseWindow::Close();
135 void OnPaint() override
137 this->SetupHighScoreEndWindow();
138 Point pt
= this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
140 const Company
*c
= Company::GetIfValid(_local_company
);
141 if (c
== nullptr) return;
143 /* We need to get performance from last year because the image is shown
144 * at the start of the new year when these things have already been copied */
145 if (this->background_img
== SPR_TYCOON_IMG2_BEGIN
) { // Tycoon of the century \o/
146 SetDParam(0, c
->index
);
147 SetDParam(1, c
->index
);
148 SetDParam(2, EndGameGetPerformanceTitleFromValue(c
->old_economy
[0].performance_history
));
149 DrawStringMultiLine(pt
.x
+ ScaleSpriteTrad(15), pt
.x
+ ScaleSpriteTrad(640) - ScaleSpriteTrad(25), pt
.y
+ ScaleSpriteTrad(90), pt
.y
+ ScaleSpriteTrad(160), STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS
, TC_FROMSTRING
, SA_CENTER
);
151 SetDParam(0, c
->index
);
152 SetDParam(1, EndGameGetPerformanceTitleFromValue(c
->old_economy
[0].performance_history
));
153 DrawStringMultiLine(pt
.x
+ ScaleSpriteTrad(36), pt
.x
+ ScaleSpriteTrad(640), pt
.y
+ ScaleSpriteTrad(140), pt
.y
+ ScaleSpriteTrad(206), STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS
, TC_FROMSTRING
, SA_CENTER
);
158 struct HighScoreWindow
: EndGameHighScoreBaseWindow
{
159 bool game_paused_by_player
; ///< True if the game was paused by the player when the highscore window was opened.
161 HighScoreWindow(WindowDesc
&desc
, int difficulty
, int8_t ranking
) : EndGameHighScoreBaseWindow(desc
)
163 /* pause game to show the chart */
164 this->game_paused_by_player
= _pause_mode
== PM_PAUSED_NORMAL
;
165 if (!_networking
&& !this->game_paused_by_player
) Command
<CMD_PAUSE
>::Post(PM_PAUSED_NORMAL
, true);
167 /* Close all always on-top windows to get a clean screen */
168 if (_game_mode
!= GM_MENU
) HideVitalWindows();
170 MarkWholeScreenDirty();
171 this->window_number
= difficulty
; // show highscore chart for difficulty...
172 this->background_img
= SPR_HIGHSCORE_CHART_BEGIN
; // which background to show
173 this->rank
= ranking
;
176 void Close([[maybe_unused
]] int data
= 0) override
178 if (_game_mode
!= GM_MENU
) ShowVitalWindows();
180 if (!_networking
&& !this->game_paused_by_player
) Command
<CMD_PAUSE
>::Post(PM_PAUSED_NORMAL
, false); // unpause
182 this->EndGameHighScoreBaseWindow::Close();
185 void OnPaint() override
187 const auto &hs
= _highscore_table
[this->window_number
];
189 this->SetupHighScoreEndWindow();
190 Point pt
= this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
192 /* Draw the title. */
193 DrawStringMultiLine(pt
.x
+ ScaleSpriteTrad(70), pt
.x
+ ScaleSpriteTrad(570), pt
.y
, pt
.y
+ ScaleSpriteTrad(140), STR_HIGHSCORE_TOP_COMPANIES
, TC_FROMSTRING
, SA_CENTER
);
195 /* Draw Highscore peepz */
196 for (uint8_t i
= 0; i
< ClampTo
<uint8_t>(hs
.size()); i
++) {
198 DrawString(pt
.x
+ ScaleSpriteTrad(40), pt
.x
+ ScaleSpriteTrad(600), pt
.y
+ ScaleSpriteTrad(140 + i
* 55), STR_HIGHSCORE_POSITION
);
200 if (!hs
[i
].name
.empty()) {
201 TextColour colour
= (this->rank
== i
) ? TC_RED
: TC_BLACK
; // draw new highscore in red
203 SetDParamStr(0, hs
[i
].name
);
204 DrawString(pt
.x
+ ScaleSpriteTrad(71), pt
.x
+ ScaleSpriteTrad(569), pt
.y
+ ScaleSpriteTrad(140 + i
* 55), STR_JUST_BIG_RAW_STRING
, colour
);
205 SetDParam(0, hs
[i
].title
);
206 SetDParam(1, hs
[i
].score
);
207 DrawString(pt
.x
+ ScaleSpriteTrad(71), pt
.x
+ ScaleSpriteTrad(569), pt
.y
+ ScaleSpriteTrad(140) + GetCharacterHeight(FS_LARGE
) + ScaleSpriteTrad(i
* 55), STR_HIGHSCORE_STATS
, colour
);
213 static constexpr NWidgetPart _nested_highscore_widgets
[] = {
214 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_H_BACKGROUND
), SetResize(1, 1), EndContainer(),
217 static WindowDesc
_highscore_desc(
218 WDP_MANUAL
, nullptr, 0, 0,
219 WC_HIGHSCORE
, WC_NONE
,
221 _nested_highscore_widgets
224 static WindowDesc
_endgame_desc(
225 WDP_MANUAL
, nullptr, 0, 0,
226 WC_ENDSCREEN
, WC_NONE
,
228 _nested_highscore_widgets
232 * Show the highscore table for a given difficulty. When called from
233 * endgame ranking is set to the top5 element that was newly added
234 * and is thus highlighted
236 void ShowHighscoreTable(int difficulty
, int8_t ranking
)
238 CloseWindowByClass(WC_HIGHSCORE
);
239 new HighScoreWindow(_highscore_desc
, difficulty
, ranking
);
243 * Show the endgame victory screen in 2050. Update the new highscore
244 * if it was high enough
246 void ShowEndGameChart()
248 /* Dedicated server doesn't need the highscore window and neither does -v null. */
249 if (_network_dedicated
|| (!_networking
&& !Company::IsValidID(_local_company
))) return;
252 CloseWindowByClass(WC_ENDSCREEN
);
253 new EndGameWindow(_endgame_desc
);
256 static IntervalTimer
<TimerGameCalendar
> _check_end_game({TimerGameCalendar::YEAR
, TimerGameCalendar::Priority::NONE
}, [](auto)
259 if (_settings_game
.game_creation
.ending_year
== 0) return;
261 /* Show the end-game chart at the end of the ending year (hence the + 1). */
262 if (TimerGameCalendar::year
== _settings_game
.game_creation
.ending_year
+ 1) {