OpenTTD
highscore_gui.cpp
Go to the documentation of this file.
1 /* $Id: highscore_gui.cpp 26871 2014-09-21 09:12:04Z peter1138 $ */
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 
27 
28 #include "safeguards.h"
29 
31  uint32 background_img;
32  int8 rank;
33 
35  {
36  this->InitNested();
38  ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
39  }
40 
41  /* Always draw a maximized window and within it the centered background */
42  void SetupHighScoreEndWindow()
43  {
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);
46 
47  this->DrawWidgets();
48 
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));
56  }
57  }
58 
60  Point GetTopLeft(int x, int y)
61  {
62  Point pt = {max(0, (_screen.width / 2) - (x / 2)), max(0, (_screen.height / 2) - (y / 2))};
63  return pt;
64  }
65 
66  virtual void OnClick(Point pt, int widget, int click_count)
67  {
68  delete this;
69  }
70 
71  virtual EventState OnKeyPress(WChar key, uint16 keycode)
72  {
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;
77 
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  delete this;
84  return ES_HANDLED;
85 
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;
91  }
92  }
93 };
94 
98  {
99  /* Pause in single-player to have a look at the highscore at your own leisure */
101 
102  this->background_img = SPR_TYCOON_IMG1_BEGIN;
103 
105  const Company *c = Company::Get(_local_company);
107  this->background_img = SPR_TYCOON_IMG2_BEGIN;
108  }
109  }
110 
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) {
115  this->rank = SaveHighScoreValueNetwork();
116  } else {
117  /* in single player _local company is always valid */
118  const Company *c = Company::Get(_local_company);
119  this->window_number = SP_CUSTOM;
120  this->rank = SaveHighScoreValue(c);
121  }
122 
124  }
125 
126  ~EndGameWindow()
127  {
128  if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
129  ShowHighscoreTable(this->window_number, this->rank);
130  }
131 
132  virtual void OnPaint()
133  {
134  this->SetupHighScoreEndWindow();
135  Point pt = this->GetTopLeft(640, 480);
136 
138  if (c == NULL) return;
139 
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 + 15, pt.x + 640 - 25, pt.y + 90, pt.y + 160, STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
147  } else {
148  SetDParam(0, c->index);
149  SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
150  DrawStringMultiLine(pt.x + 36, pt.x + 640, pt.y + 140, pt.y + 206, STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
151  }
152  }
153 };
154 
157 
158  HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
159  {
160  /* pause game to show the chart */
163 
164  /* Close all always on-top windows to get a clean screen */
165  if (_game_mode != GM_MENU) HideVitalWindows();
166 
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;
171  }
172 
173  ~HighScoreWindow()
174  {
175  if (_game_mode != GM_MENU) ShowVitalWindows();
176 
177  if (!_networking && !this->game_paused_by_player) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
178  }
179 
180  virtual void OnPaint()
181  {
182  const HighScore *hs = _highscore_table[this->window_number];
183 
184  this->SetupHighScoreEndWindow();
185  Point pt = this->GetTopLeft(640, 480);
186 
188  DrawStringMultiLine(pt.x + 70, pt.x + 570, pt.y, pt.y + 140, !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
189 
190  /* Draw Highscore peepz */
191  for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
192  SetDParam(0, i + 1);
193  DrawString(pt.x + 40, pt.x + 600, pt.y + 140 + (i * 55), STR_HIGHSCORE_POSITION);
194 
195  if (hs[i].company[0] != '\0') {
196  TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
197 
198  SetDParamStr(0, hs[i].company);
199  DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + (i * 55), STR_JUST_BIG_RAW_STRING, colour);
200  SetDParam(0, hs[i].title);
201  SetDParam(1, hs[i].score);
202  DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + FONT_HEIGHT_LARGE + (i * 55), STR_HIGHSCORE_STATS, colour);
203  }
204  }
205  }
206 };
207 
208 static const NWidgetPart _nested_highscore_widgets[] = {
209  NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
210 };
211 
212 static WindowDesc _highscore_desc(
213  WDP_MANUAL, NULL, 0, 0,
215  0,
216  _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
217 );
218 
219 static WindowDesc _endgame_desc(
220  WDP_MANUAL, NULL, 0, 0,
222  0,
223  _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
224 );
225 
231 void ShowHighscoreTable(int difficulty, int8 ranking)
232 {
234  new HighScoreWindow(&_highscore_desc, difficulty, ranking);
235 }
236 
242 {
243  /* Dedicated server doesn't need the highscore window and neither does -v null. */
245 
248  new EndGameWindow(&_endgame_desc);
249 }