OpenTTD Source 20260218-master-g2123fca5ea
highscore_gui.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "highscore.h"
12#include "gfx_func.h"
13#include "table/sprites.h"
14#include "window_gui.h"
15#include "window_func.h"
16#include "network/network.h"
17#include "command_func.h"
18#include "company_func.h"
19#include "company_base.h"
20#include "strings_func.h"
21#include "hotkeys.h"
22#include "zoom_func.h"
23#include "misc_cmd.h"
24#include "timer/timer.h"
26
28
29#include "table/strings.h"
30
31#include "safeguards.h"
32
33struct EndGameHighScoreBaseWindow : Window {
34 SpriteID background_img{};
35 int8_t rank = 0;
36
37 EndGameHighScoreBaseWindow(WindowDesc &desc) : Window(desc)
38 {
39 this->InitNested();
40 this->flags.Reset(WindowFlag::WhiteBorder);
41 ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
42 }
43
44 /* Always draw a maximized window and within it the centered background */
45 void SetupHighScoreEndWindow()
46 {
47 /* Resize window to "full-screen". */
48 if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
49
50 this->DrawWidgets();
51
52 /* Fill with the appropriate background colour instead of leaving default window colour */
53 GfxFillRect(Rect{0, 0, this->width, this->height}, PixelColour{105}, FILLRECT_OPAQUE);
54
55 /* Standard background slices are 50 pixels high, but it's designed
56 * for 480 pixels total. 96% of 500 is 480. */
57 Dimension dim = GetSpriteSize(this->background_img);
58 auto total_height = dim.height * 96 / 10;
59 Point pt = this->GetTopLeft(dim.width, total_height);
60
61 /* Original graphics contain some transparency, which assumes a black background. */
62 GfxFillRect(pt.x, pt.y, pt.x + dim.width - 1, pt.y + total_height - 1, PC_BLACK);
63
64 /* Center Highscore/Endscreen background */
65 for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
66 DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * dim.height));
67 }
68 }
69
77 {
78 return {std::max(0, (_screen.width / 2) - (width / 2)), std::max(0, (_screen.height / 2) - (height / 2))};
79 }
80
81 void OnClick([[maybe_unused]] Point pt, [[maybe_unused]] WidgetID widget, [[maybe_unused]] int click_count) override
82 {
83 this->Close();
84 }
85
86 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode) override
87 {
88 /* All keys are 'handled' by this window but we want to make
89 * sure that 'quit' still works correctly. Not handling the
90 * quit key is enough so the main toolbar can handle it. */
91 if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
92
93 switch (keycode) {
94 /* Keys for telling we want to go on */
95 case WKC_RETURN:
96 case WKC_ESC:
97 case WKC_SPACE:
98 this->Close();
99 return ES_HANDLED;
100
101 default:
102 /* We want to handle all keys; we don't want windows in
103 * the background to open. Especially the ones that do
104 * locate themselves based on the status-/toolbars. */
105 return ES_HANDLED;
106 }
107 }
108};
109
111struct EndGameWindow : EndGameHighScoreBaseWindow {
112 EndGameWindow(WindowDesc &desc) : EndGameHighScoreBaseWindow(desc)
113 {
114 /* Pause in single-player to have a look at the highscore at your own leisure */
115 if (!_networking) Command<Commands::Pause>::Post(PauseMode::Normal, true);
116
117 this->background_img = SPR_TYCOON_IMG1_BEGIN;
118
121 if (c->old_economy[0].performance_history == SCORE_MAX) {
122 this->background_img = SPR_TYCOON_IMG2_BEGIN;
123 }
124 }
125
126 /* In a network game show the endscores of the custom difficulty 'network' which is
127 * a TOP5 of that game, and not an all-time TOP5. */
128 if (_networking) {
130 this->rank = SaveHighScoreValueNetwork();
131 } else {
132 /* in singleplayer mode _local company is always valid */
134 this->window_number = SP_CUSTOM;
135 this->rank = SaveHighScoreValue(c);
136 }
137
139 }
140
141 void Close([[maybe_unused]] int data = 0) override
142 {
143 if (!_networking) Command<Commands::Pause>::Post(PauseMode::Normal, false); // unpause
144 if (_game_mode != GM_MENU && !_exit_game) ShowHighscoreTable(this->window_number, this->rank);
146 }
147
148 void OnPaint() override
149 {
150 this->SetupHighScoreEndWindow();
151 Point pt = this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
152
154 if (c == nullptr) return;
155
156 /* We need to get performance from last year because the image is shown
157 * at the start of the new year when these things have already been copied */
158 if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
160 GetString(STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS, c->index, c->index, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history)),
161 TC_FROMSTRING, SA_CENTER);
162 } else {
163 DrawStringMultiLine(pt.x + ScaleSpriteTrad(36), pt.x + ScaleSpriteTrad(640), pt.y + ScaleSpriteTrad(140), pt.y + ScaleSpriteTrad(206),
164 GetString(STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, c->index, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history)),
165 TC_FROMSTRING, SA_CENTER);
166 }
167 }
168};
169
170struct HighScoreWindow : EndGameHighScoreBaseWindow {
172
173 HighScoreWindow(WindowDesc &desc, int difficulty, int8_t ranking) : EndGameHighScoreBaseWindow(desc)
174 {
175 /* pause game to show the chart */
176 this->game_paused_by_player = _pause_mode == PauseMode::Normal;
177 if (!_networking && !this->game_paused_by_player) Command<Commands::Pause>::Post(PauseMode::Normal, true);
178
179 /* Close all always on-top windows to get a clean screen */
180 if (_game_mode != GM_MENU) HideVitalWindows();
181
183 this->window_number = difficulty; // show highscore chart for difficulty...
184 this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
185 this->rank = ranking;
186 }
187
188 void Close([[maybe_unused]] int data = 0) override
189 {
190 if (_game_mode != GM_MENU && !_exit_game) ShowVitalWindows();
191
192 if (!_networking && !this->game_paused_by_player) Command<Commands::Pause>::Post(PauseMode::Normal, false); // unpause
193
195 }
196
197 void OnPaint() override
198 {
199 const auto &hs = _highscore_table[this->window_number];
200
201 this->SetupHighScoreEndWindow();
202 Point pt = this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
203
204 /* Draw the title. */
205 DrawStringMultiLine(pt.x + ScaleSpriteTrad(70), pt.x + ScaleSpriteTrad(570), pt.y, pt.y + ScaleSpriteTrad(140), STR_HIGHSCORE_TOP_COMPANIES, TC_FROMSTRING, SA_CENTER);
206
207 /* Draw Highscore peepz */
208 for (uint8_t i = 0; i < ClampTo<uint8_t>(hs.size()); i++) {
209 DrawString(pt.x + ScaleSpriteTrad(40), pt.x + ScaleSpriteTrad(600), pt.y + ScaleSpriteTrad(140 + i * 55),
210 GetString(STR_HIGHSCORE_POSITION, i + 1));
211
212 if (!hs[i].name.empty()) {
213 TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
214
215 DrawString(pt.x + ScaleSpriteTrad(71), pt.x + ScaleSpriteTrad(569), pt.y + ScaleSpriteTrad(140 + i * 55),
216 GetString(STR_JUST_BIG_RAW_STRING, hs[i].name), colour);
218 GetString(STR_HIGHSCORE_STATS, hs[i].title, hs[i].score), colour);
219 }
220 }
221 }
222};
223
224static constexpr std::initializer_list<NWidgetPart> _nested_highscore_widgets = {
225 NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetResize(1, 1), EndContainer(),
226};
227
228static WindowDesc _highscore_desc(
229 WDP_MANUAL, {}, 0, 0,
231 {},
232 _nested_highscore_widgets
233);
234
235static WindowDesc _endgame_desc(
236 WDP_MANUAL, {}, 0, 0,
238 {},
239 _nested_highscore_widgets
240);
241
249void ShowHighscoreTable(int difficulty, int8_t ranking)
250{
252 new HighScoreWindow(_highscore_desc, difficulty, ranking);
253}
254
260{
261 /* Dedicated server doesn't need the highscore window and neither does -v null. */
263
266 new EndGameWindow(_endgame_desc);
267}
268
270static const IntervalTimer<TimerGameCalendar> _check_end_game({TimerGameCalendar::Trigger::Year, TimerGameCalendar::Priority::None}, [](auto)
271{
272 /* 0 = never */
273 if (_settings_game.game_creation.ending_year == 0) return;
274
275 /* Show the end-game chart at the end of the ending year (hence the + 1). */
276 if (TimerGameCalendar::year == _settings_game.game_creation.ending_year + 1) {
278 }
279});
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
static Year year
Current year, starting at 0.
Functions related to commands.
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Functions related to companies.
static constexpr CompanyID COMPANY_SPECTATOR
The client is spectating.
static constexpr int SCORE_MAX
The max score that can be in the performance history.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:972
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:669
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:1038
PauseModes _pause_mode
The current pause mode.
Definition gfx.cpp:51
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:788
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:116
Functions related to the gfx engine.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ FS_LARGE
Index of the large font in the font tables.
Definition gfx_type.h:251
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:398
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
@ FILLRECT_OPAQUE
Fill rectangle with a single colour.
Definition gfx_type.h:345
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1554
int8_t SaveHighScoreValueNetwork()
Save the highscores in a network game when it has ended.
Definition highscore.cpp:88
HighScoresTable _highscore_table
Table with all the high scores.
Definition highscore.cpp:24
int8_t SaveHighScoreValue(const Company *c)
Save the highscore for the company.
Definition highscore.cpp:58
Declaration of functions and types defined in highscore.h and highscore_gui.h.
void ShowHighscoreTable(int difficulty, int8_t ranking)
Show the highscore table for a given difficulty.
void ShowEndGameChart()
Show the endgame victory screen in 2050.
static const IntervalTimer< TimerGameCalendar > _check_end_game({TimerGameCalendar::Trigger::Year, TimerGameCalendar::Priority::None}, [](auto) { if(_settings_game.game_creation.ending_year==0) return;if(TimerGameCalendar::year==_settings_game.game_creation.ending_year+1) { ShowEndGameChart();} })
Yearly timer to check whether we want to show the end game chart.
Types related to the highscore widgets.
@ WID_H_BACKGROUND
Background of the window.
Hotkey related functions.
bool IsQuitKey(uint16_t keycode)
Does the given keycode match one of the keycodes bound to 'quit game'?
Definition main_gui.cpp:541
#define Point
Macro that prevents name conflicts between included headers.
void ShowVitalWindows()
Show the vital in-game windows.
Definition main_gui.cpp:582
Miscellaneous command definitions.
bool _networking
are we in networking mode?
Definition network.cpp:66
bool _network_dedicated
are we a dedicated server?
Definition network.cpp:69
Basic functions/variables used all over the place.
@ Normal
A game normally paused.
Definition openttd.h:69
static constexpr PixelColour PC_BLACK
Black palette colour.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
@ SP_CUSTOM
No profile, special "custom" highscore.
@ SP_MULTIPLAYER
Special "multiplayer" highscore. Not saved, always specific to the current game.
This file contains all sprite-related enums and defines.
Definition of base types and functions in a cross-platform compatible way.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
Functions related to OTTD's strings.
std::array< CompanyEconomyEntry, MAX_HISTORY_QUARTERS > old_economy
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
T y
Y coordinate.
T x
X coordinate.
Dimensions (a width and height) of a rectangle in 2D.
Point GetTopLeft(int width, int height)
Return the coordinate of the screen such that a window of a given size is centered at the screen.
EventState OnKeyPress(char32_t key, uint16_t keycode) override
A key has been pressed.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
End game window shown at the end of the game.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
void OnPaint() override
The window must be repainted.
void OnPaint() override
The window must be repainted.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
bool game_paused_by_player
True if the game was paused by the player when the highscore window was opened.
Colour for pixel/line drawing.
Definition gfx_type.h:405
static Company * Get(auto index)
static Company * GetIfValid(auto index)
Specification of a rectangle with absolute coordinates of all edges.
High level window description.
Definition window_gui.h:168
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1104
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:766
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1832
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1822
WindowFlags flags
Window flags.
Definition window_gui.h:301
int height
Height of the window (number of pixels down in y direction).
Definition window_gui.h:313
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Definition of Interval and OneShot timers.
Definition of the game-calendar-timer.
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen, bool schedule_resize)
Resize the window.
Definition window.cpp:2099
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
Definition window.cpp:1209
void HideVitalWindows()
Close all always on-top windows to get an empty screen.
Definition window.cpp:3407
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ WhiteBorder
Window white border counter bit mask.
Definition window_gui.h:235
@ WDP_MANUAL
Manually align the window (so no automatic location finding).
Definition window_gui.h:143
int WidgetID
Widget ID.
Definition window_type.h:20
EventState
State of handling an event.
@ ES_HANDLED
The passed event is handled.
@ ES_NOT_HANDLED
The passed event is not handled.
@ WC_HIGHSCORE
Highscore; Window numbers:
@ WC_ENDSCREEN
Endscreen; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:50
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107