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 "widgets/highscore_widget.h"
27 #include "safeguards.h"
29 struct EndGameHighScoreBaseWindow
: Window
{
30 uint32 background_img
;
33 EndGameHighScoreBaseWindow(WindowDesc
*desc
) : Window(desc
)
36 CLRBITS(this->flags
, WF_WHITE_BORDER
);
37 ResizeWindow(this, _screen
.width
- this->width
, _screen
.height
- this->height
);
40 /* Always draw a maximized window and within it the centered background */
41 void SetupHighScoreEndWindow()
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
);
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
));
58 /** Return the coordinate of the screen such that a window of 640x480 is centered at the screen. */
59 Point
GetTopLeft(int x
, int y
)
61 Point pt
= {std::max(0, (_screen
.width
/ 2) - (x
/ 2)), std::max(0, (_screen
.height
/ 2) - (y
/ 2))};
65 void OnClick(Point pt
, int widget
, int click_count
) override
70 EventState
OnKeyPress(WChar key
, uint16 keycode
) override
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
;
78 /* Keys for telling we want to go on */
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. */
94 /** End game window shown at the end of the game */
95 struct EndGameWindow
: EndGameHighScoreBaseWindow
{
96 EndGameWindow(WindowDesc
*desc
) : EndGameHighScoreBaseWindow(desc
)
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
);
101 this->background_img
= SPR_TYCOON_IMG1_BEGIN
;
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
;
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. */
113 this->window_number
= SP_MULTIPLAYER
;
114 this->rank
= SaveHighScoreValueNetwork();
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
);
122 MarkWholeScreenDirty();
125 void Close() override
127 if (!_networking
) DoCommandP(0, PM_PAUSED_NORMAL
, 0, CMD_PAUSE
); // unpause
128 ShowHighscoreTable(this->window_number
, this->rank
);
129 this->EndGameHighScoreBaseWindow::Close();
132 void OnPaint() override
134 this->SetupHighScoreEndWindow();
135 Point pt
= this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
137 const Company
*c
= Company::GetIfValid(_local_company
);
138 if (c
== nullptr) return;
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
);
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
);
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.
158 HighScoreWindow(WindowDesc
*desc
, int difficulty
, int8 ranking
) : EndGameHighScoreBaseWindow(desc
)
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
);
164 /* Close all always on-top windows to get a clean screen */
165 if (_game_mode
!= GM_MENU
) HideVitalWindows();
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
;
173 void Close() override
175 if (_game_mode
!= GM_MENU
) ShowVitalWindows();
177 if (!_networking
&& !this->game_paused_by_player
) DoCommandP(0, PM_PAUSED_NORMAL
, 0, CMD_PAUSE
); // unpause
179 this->EndGameHighScoreBaseWindow::Close();
182 void OnPaint() override
184 const HighScore
*hs
= _highscore_table
[this->window_number
];
186 this->SetupHighScoreEndWindow();
187 Point pt
= this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
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
);
192 /* Draw Highscore peepz */
193 for (uint8 i
= 0; i
< lengthof(_highscore_table
[0]); i
++) {
195 DrawString(pt
.x
+ ScaleGUITrad(40), pt
.x
+ ScaleGUITrad(600), pt
.y
+ ScaleGUITrad(140 + i
* 55), STR_HIGHSCORE_POSITION
);
197 if (hs
[i
].company
[0] != '\0') {
198 TextColour colour
= (this->rank
== i
) ? TC_RED
: TC_BLACK
; // draw new highscore in red
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
);
210 static const NWidgetPart _nested_highscore_widgets
[] = {
211 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_H_BACKGROUND
), SetResize(1, 1), EndContainer(),
214 static WindowDesc
_highscore_desc(
215 WDP_MANUAL
, nullptr, 0, 0,
216 WC_HIGHSCORE
, WC_NONE
,
218 _nested_highscore_widgets
, lengthof(_nested_highscore_widgets
)
221 static WindowDesc
_endgame_desc(
222 WDP_MANUAL
, nullptr, 0, 0,
223 WC_ENDSCREEN
, WC_NONE
,
225 _nested_highscore_widgets
, lengthof(_nested_highscore_widgets
)
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
233 void ShowHighscoreTable(int difficulty
, int8 ranking
)
235 CloseWindowByClass(WC_HIGHSCORE
);
236 new HighScoreWindow(&_highscore_desc
, difficulty
, ranking
);
240 * Show the endgame victory screen in 2050. Update the new highscore
241 * if it was high enough
243 void ShowEndGameChart()
245 /* Dedicated server doesn't need the highscore window and neither does -v null. */
246 if (_network_dedicated
|| (!_networking
&& !Company::IsValidID(_local_company
))) return;
249 CloseWindowByClass(WC_ENDSCREEN
);
250 new EndGameWindow(&_endgame_desc
);