OpenTTD
highscore_gui.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #include "stdafx.h"
13 #include "highscore.h"
14 #include "table/strings.h"
15 #include "gfx_func.h"
16 #include "table/sprites.h"
17 #include "window_gui.h"
18 #include "window_func.h"
19 #include "network/network.h"
20 #include "command_func.h"
21 #include "company_func.h"
22 #include "company_base.h"
23 #include "strings_func.h"
24 #include "hotkeys.h"
25 #include "zoom_func.h"
26 
28 
29 #include "safeguards.h"
30 
32  uint32 background_img;
33  int8 rank;
34 
36  {
37  this->InitNested();
39  ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
40  }
41 
42  /* Always draw a maximized window and within it the centered background */
43  void SetupHighScoreEndWindow()
44  {
45  /* Resize window to "full-screen". */
46  if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
47 
48  this->DrawWidgets();
49 
50  /* Standard background slices are 50 pixels high, but it's designed
51  * for 480 pixels total. 96% of 500 is 480. */
52  Dimension dim = GetSpriteSize(this->background_img);
53  Point pt = this->GetTopLeft(dim.width, dim.height * 96 / 10);
54  /* Center Highscore/Endscreen background */
55  for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
56  DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * dim.height));
57  }
58  }
59 
61  Point GetTopLeft(int x, int y)
62  {
63  Point pt = {max(0, (_screen.width / 2) - (x / 2)), max(0, (_screen.height / 2) - (y / 2))};
64  return pt;
65  }
66 
67  virtual void OnClick(Point pt, int widget, int click_count)
68  {
69  delete this;
70  }
71 
72  virtual EventState OnKeyPress(WChar key, uint16 keycode)
73  {
74  /* All keys are 'handled' by this window but we want to make
75  * sure that 'quit' still works correctly. Not handling the
76  * quit key is enough so the main toolbar can handle it. */
77  if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
78 
79  switch (keycode) {
80  /* Keys for telling we want to go on */
81  case WKC_RETURN:
82  case WKC_ESC:
83  case WKC_SPACE:
84  delete this;
85  return ES_HANDLED;
86 
87  default:
88  /* We want to handle all keys; we don't want windows in
89  * the background to open. Especially the ones that do
90  * locate themselves based on the status-/toolbars. */
91  return ES_HANDLED;
92  }
93  }
94 };
95 
99  {
100  /* Pause in single-player to have a look at the highscore at your own leisure */
102 
103  this->background_img = SPR_TYCOON_IMG1_BEGIN;
104 
106  const Company *c = Company::Get(_local_company);
108  this->background_img = SPR_TYCOON_IMG2_BEGIN;
109  }
110  }
111 
112  /* In a network game show the endscores of the custom difficulty 'network' which is
113  * a TOP5 of that game, and not an all-time TOP5. */
114  if (_networking) {
116  this->rank = SaveHighScoreValueNetwork();
117  } else {
118  /* in single player _local company is always valid */
119  const Company *c = Company::Get(_local_company);
120  this->window_number = SP_CUSTOM;
121  this->rank = SaveHighScoreValue(c);
122  }
123 
125  }
126 
127  ~EndGameWindow()
128  {
129  if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
130  ShowHighscoreTable(this->window_number, this->rank);
131  }
132 
133  virtual void OnPaint()
134  {
135  this->SetupHighScoreEndWindow();
136  Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
137 
139  if (c == NULL) return;
140 
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);
152  }
153  }
154 };
155 
158 
159  HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
160  {
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) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
164 
165  /* Close all always on-top windows to get a clean screen */
166  if (_game_mode != GM_MENU) HideVitalWindows();
167 
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;
172  }
173 
174  ~HighScoreWindow()
175  {
176  if (_game_mode != GM_MENU) ShowVitalWindows();
177 
178  if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
179  }
180 
181  virtual void OnPaint()
182  {
183  const HighScore *hs = _highscore_table[this->window_number];
184 
185  this->SetupHighScoreEndWindow();
186  Point pt = this->GetTopLeft(ScaleGUITrad(640), ScaleGUITrad(480));
187 
189  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);
190 
191  /* Draw Highscore peepz */
192  for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
193  SetDParam(0, i + 1);
194  DrawString(pt.x + ScaleGUITrad(40), pt.x + ScaleGUITrad(600), pt.y + ScaleGUITrad(140 + i * 55), STR_HIGHSCORE_POSITION);
195 
196  if (hs[i].company[0] != '\0') {
197  TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
198 
199  SetDParamStr(0, hs[i].company);
200  DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140 + i * 55), STR_JUST_BIG_RAW_STRING, colour);
201  SetDParam(0, hs[i].title);
202  SetDParam(1, hs[i].score);
203  DrawString(pt.x + ScaleGUITrad(71), pt.x + ScaleGUITrad(569), pt.y + ScaleGUITrad(140) + FONT_HEIGHT_LARGE + ScaleGUITrad(i * 55), STR_HIGHSCORE_STATS, colour);
204  }
205  }
206  }
207 };
208 
209 static const NWidgetPart _nested_highscore_widgets[] = {
210  NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
211 };
212 
213 static WindowDesc _highscore_desc(
214  WDP_MANUAL, NULL, 0, 0,
216  0,
217  _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
218 );
219 
220 static WindowDesc _endgame_desc(
221  WDP_MANUAL, NULL, 0, 0,
223  0,
224  _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
225 );
226 
232 void ShowHighscoreTable(int difficulty, int8 ranking)
233 {
235  new HighScoreWindow(&_highscore_desc, difficulty, ranking);
236 }
237 
243 {
244  /* Dedicated server doesn't need the highscore window and neither does -v null. */
246 
249  new EndGameWindow(&_endgame_desc);
250 }
EventState
State of handling an event.
Definition: window_type.h:713
Functions related to OTTD&#39;s strings.
virtual void OnClick(Point pt, int widget, int click_count)
A click with the left mouse button has been made on the window.
int8 SaveHighScoreValueNetwork()
Save the highscores in a network game when it has ended.
Definition: highscore.cpp:91
Definition of stuff that is very close to a company, like the company struct itself.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
bool _networking
are we in networking mode?
Definition: network.cpp:56
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:930
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1851
A game normally paused.
Definition: openttd.h:59
High level window description.
Definition: window_gui.h:168
WindowFlags flags
Window flags.
Definition: window_gui.h:312
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:604
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Hotkey related functions.
int32 performance_history
Company score (scale 0-1000)
Definition: company_base.h:27
The passed event is not handled.
Definition: window_type.h:715
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:82
HighScore _highscore_table[SP_HIGHSCORE_END][5]
various difficulty-settings; top 5
Definition: highscore.cpp:25
No profile, special "custom" highscore.
Definition: settings_type.h:35
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Types related to the highscore widgets.
The client is spectating.
Definition: company_type.h:37
virtual void OnPaint()
The window must be repainted.
virtual EventState OnKeyPress(WChar key, uint16 keycode)
A key has been pressed.
#define CLRBITS(x, y)
Clears several bits in a variable.
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
Functions, definitions and such used only by the GUI.
Special "multiplayer" highscore. Not saved, always specific to the current game.
Definition: settings_type.h:38
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:59
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Data structure for an opened window.
Definition: window_gui.h:278
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1841
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:282
void HideVitalWindows()
Delete all always on-top windows to get an empty screen.
Definition: window.cpp:3429
Functions related to the gfx engine.
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:947
Definition of base types and functions in a cross-platform compatible way.
Center both horizontally and vertically.
Definition: gfx_func.h:108
A number of safeguards to prevent using unsafe methods.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
Simple depressed panel.
Definition: widget_type.h:50
End game window shown at the end of the game.
virtual void OnPaint()
The window must be repainted.
void ShowEndGameChart()
Show the endgame victory screen in 2050.
Highscore; Window numbers:
Definition: window_type.h:645
bool game_paused_by_player
True if the game was paused by the player when the highscore window was opened.
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:500
Basic functions/variables used all over the place.
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:531
#define FONT_HEIGHT_LARGE
Height of characters in the large (FS_LARGE) font.
Definition: gfx_func.h:183
Point GetTopLeft(int x, int y)
Return the coordinate of the screen such that a window of 640x480 is centered at the screen...
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
PauseModeByte _pause_mode
The current pause mode.
Definition: gfx.cpp:48
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1159
The max score that can be in the performance history.
Definition: economy_type.h:52
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
Functions related to companies.
int8 SaveHighScoreValue(const Company *c)
Save the highscore for the company.
Definition: highscore.cpp:55
Declaration of functions and types defined in highscore.h and highscore_gui.h.
bool IsQuitKey(uint16 keycode)
Does the given keycode match one of the keycodes bound to &#39;quit game&#39;?
Definition: main_gui.cpp:552
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
Functions related to zooming.
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition: company_base.h:96
Functions related to commands.
Coordinates of a point in 2D.
void ShowHighscoreTable(int difficulty, int8 ranking)
Show the highscore table for a given difficulty.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:769
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-NULL) Titem.
Definition: pool_type.hpp:235
Endscreen; Window numbers:
Definition: window_type.h:651
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:321
Background of the window.
The passed event is handled.
Definition: window_type.h:714
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:314
Window functions not directly related to making/drawing windows.
Manually align the window (so no automatic location finding)
Definition: window_gui.h:155
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2126
Window white border counter bit mask.
Definition: window_gui.h:242
void ShowVitalWindows()
Show the vital in-game windows.
Definition: main_gui.cpp:592
Dimensions (a width and height) of a rectangle in 2D.
This file contains all sprite-related enums and defines.
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:834
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1463
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:621
static const Year ORIGINAL_END_YEAR
The original ending year.
Definition: date_type.h:53
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201
pause the game
Definition: command_type.h:255