2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-03 18:44:20 +00:00
|
|
|
/** @file highscore_gui.cpp Definition of the HighScore and EndGame windows */
|
|
|
|
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "stdafx.h"
|
2009-01-03 18:44:20 +00:00
|
|
|
#include "highscore.h"
|
|
|
|
#include "table/strings.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "window_gui.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
#include "network/network.h"
|
|
|
|
#include "command_func.h"
|
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_base.h"
|
|
|
|
#include "strings_func.h"
|
2010-07-04 11:28:16 +00:00
|
|
|
#include "hotkeys.h"
|
2019-09-01 14:43:32 +00:00
|
|
|
#include "zoom_func.h"
|
2021-10-31 18:39:09 +00:00
|
|
|
#include "misc_cmd.h"
|
2009-01-03 18:44:20 +00:00
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/highscore_widget.h"
|
2009-10-17 20:38:27 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-01-03 18:44:20 +00:00
|
|
|
struct EndGameHighScoreBaseWindow : Window {
|
|
|
|
uint32 background_img;
|
|
|
|
int8 rank;
|
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
EndGameHighScoreBaseWindow(WindowDesc *desc) : Window(desc)
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested();
|
2011-12-15 19:54:23 +00:00
|
|
|
CLRBITS(this->flags, WF_WHITE_BORDER);
|
2009-10-17 20:38:27 +00:00
|
|
|
ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
|
2009-01-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 20:38:27 +00:00
|
|
|
/* Always draw a maximized window and within it the centered background */
|
|
|
|
void SetupHighScoreEndWindow()
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
2009-10-17 20:38:27 +00:00
|
|
|
/* Resize window to "full-screen". */
|
|
|
|
if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
|
2009-01-03 18:44:20 +00:00
|
|
|
|
|
|
|
this->DrawWidgets();
|
|
|
|
|
2014-09-21 09:12:04 +00:00
|
|
|
/* Standard background slices are 50 pixels high, but it's designed
|
|
|
|
* for 480 pixels total. 96% of 500 is 480. */
|
|
|
|
Dimension dim = GetSpriteSize(this->background_img);
|
|
|
|
Point pt = this->GetTopLeft(dim.width, dim.height * 96 / 10);
|
2009-01-03 18:44:20 +00:00
|
|
|
/* Center Highscore/Endscreen background */
|
|
|
|
for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
|
2014-09-21 09:12:04 +00:00
|
|
|
DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * dim.height));
|
2009-01-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 20:38:27 +00:00
|
|
|
/** Return the coordinate of the screen such that a window of 640x480 is centered at the screen. */
|
2014-09-21 09:12:04 +00:00
|
|
|
Point GetTopLeft(int x, int y)
|
2009-10-17 20:38:27 +00:00
|
|
|
{
|
2021-01-08 10:16:18 +00:00
|
|
|
Point pt = {std::max(0, (_screen.width / 2) - (x / 2)), std::max(0, (_screen.height / 2) - (y / 2))};
|
2009-10-17 20:38:27 +00:00
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
2021-05-15 21:12:25 +00:00
|
|
|
this->Close();
|
2009-01-03 18:44:20 +00:00
|
|
|
}
|
2010-01-02 16:05:27 +00:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
EventState OnKeyPress(WChar key, uint16 keycode) override
|
2010-01-02 16:05:27 +00:00
|
|
|
{
|
2010-07-04 11:28:16 +00:00
|
|
|
/* All keys are 'handled' by this window but we want to make
|
|
|
|
* sure that 'quit' still works correctly. Not handling the
|
|
|
|
* quit key is enough so the main toolbar can handle it. */
|
|
|
|
if (IsQuitKey(keycode)) return ES_NOT_HANDLED;
|
|
|
|
|
2010-01-02 16:05:27 +00:00
|
|
|
switch (keycode) {
|
|
|
|
/* Keys for telling we want to go on */
|
|
|
|
case WKC_RETURN:
|
|
|
|
case WKC_ESC:
|
|
|
|
case WKC_SPACE:
|
2021-05-15 21:12:25 +00:00
|
|
|
this->Close();
|
2010-01-02 16:05:27 +00:00
|
|
|
return ES_HANDLED;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* We want to handle all keys; we don't want windows in
|
|
|
|
* the background to open. Especially the ones that do
|
|
|
|
* locate themselves based on the status-/toolbars. */
|
|
|
|
return ES_HANDLED;
|
|
|
|
}
|
|
|
|
}
|
2009-01-03 18:44:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** End game window shown at the end of the game */
|
|
|
|
struct EndGameWindow : EndGameHighScoreBaseWindow {
|
2013-05-26 19:23:42 +00:00
|
|
|
EndGameWindow(WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
|
|
|
/* Pause in single-player to have a look at the highscore at your own leisure */
|
2021-11-23 00:05:58 +00:00
|
|
|
if (!_networking) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, true);
|
2009-01-03 18:44:20 +00:00
|
|
|
|
|
|
|
this->background_img = SPR_TYCOON_IMG1_BEGIN;
|
|
|
|
|
|
|
|
if (_local_company != COMPANY_SPECTATOR) {
|
2009-05-16 23:34:14 +00:00
|
|
|
const Company *c = Company::Get(_local_company);
|
2009-01-03 18:44:20 +00:00
|
|
|
if (c->old_economy[0].performance_history == SCORE_MAX) {
|
|
|
|
this->background_img = SPR_TYCOON_IMG2_BEGIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 19:34:45 +00:00
|
|
|
/* In a network game show the endscores of the custom difficulty 'network' which is
|
|
|
|
* a TOP5 of that game, and not an all-time TOP5. */
|
2009-01-03 18:44:20 +00:00
|
|
|
if (_networking) {
|
2012-12-05 19:34:45 +00:00
|
|
|
this->window_number = SP_MULTIPLAYER;
|
2009-01-03 18:44:20 +00:00
|
|
|
this->rank = SaveHighScoreValueNetwork();
|
|
|
|
} else {
|
2021-01-22 15:24:29 +00:00
|
|
|
/* in singleplayer mode _local company is always valid */
|
2009-05-16 23:34:14 +00:00
|
|
|
const Company *c = Company::Get(_local_company);
|
2012-12-05 19:36:41 +00:00
|
|
|
this->window_number = SP_CUSTOM;
|
2009-01-03 18:44:20 +00:00
|
|
|
this->rank = SaveHighScoreValue(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2021-05-15 21:12:25 +00:00
|
|
|
void Close() override
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
2021-11-23 00:05:58 +00:00
|
|
|
if (!_networking) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, false); // unpause
|
2023-01-17 21:24:34 +00:00
|
|
|
if (_game_mode != GM_MENU) ShowHighscoreTable(this->window_number, this->rank);
|
2021-05-15 21:12:25 +00:00
|
|
|
this->EndGameHighScoreBaseWindow::Close();
|
2009-01-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnPaint() override
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
2009-10-17 20:38:27 +00:00
|
|
|
this->SetupHighScoreEndWindow();
|
2022-09-05 20:05:18 +00:00
|
|
|
Point pt = this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
|
2009-01-03 18:44:20 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
const Company *c = Company::GetIfValid(_local_company);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (c == nullptr) return;
|
2009-01-03 18:44:20 +00:00
|
|
|
|
|
|
|
/* We need to get performance from last year because the image is shown
|
|
|
|
* at the start of the new year when these things have already been copied */
|
|
|
|
if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
|
|
|
|
SetDParam(0, c->index);
|
|
|
|
SetDParam(1, c->index);
|
|
|
|
SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
|
2022-09-05 20:05:18 +00:00
|
|
|
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);
|
2009-01-03 18:44:20 +00:00
|
|
|
} else {
|
|
|
|
SetDParam(0, c->index);
|
|
|
|
SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
|
2022-09-05 20:05:18 +00:00
|
|
|
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);
|
2009-01-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HighScoreWindow : EndGameHighScoreBaseWindow {
|
2013-01-08 21:08:18 +00:00
|
|
|
bool game_paused_by_player; ///< True if the game was paused by the player when the highscore window was opened.
|
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
HighScoreWindow(WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
|
|
|
/* pause game to show the chart */
|
2013-01-08 21:08:18 +00:00
|
|
|
this->game_paused_by_player = _pause_mode == PM_PAUSED_NORMAL;
|
2021-11-23 00:05:58 +00:00
|
|
|
if (!_networking && !this->game_paused_by_player) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, true);
|
2009-01-03 18:44:20 +00:00
|
|
|
|
|
|
|
/* Close all always on-top windows to get a clean screen */
|
|
|
|
if (_game_mode != GM_MENU) HideVitalWindows();
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
this->window_number = difficulty; // show highscore chart for difficulty...
|
|
|
|
this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
|
|
|
|
this->rank = ranking;
|
|
|
|
}
|
|
|
|
|
2021-05-15 21:12:25 +00:00
|
|
|
void Close() override
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
|
|
|
if (_game_mode != GM_MENU) ShowVitalWindows();
|
|
|
|
|
2021-11-23 00:05:58 +00:00
|
|
|
if (!_networking && !this->game_paused_by_player) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, false); // unpause
|
2021-05-15 21:12:25 +00:00
|
|
|
|
|
|
|
this->EndGameHighScoreBaseWindow::Close();
|
2009-01-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnPaint() override
|
2009-01-03 18:44:20 +00:00
|
|
|
{
|
|
|
|
const HighScore *hs = _highscore_table[this->window_number];
|
|
|
|
|
2009-10-17 20:38:27 +00:00
|
|
|
this->SetupHighScoreEndWindow();
|
2022-09-05 20:05:18 +00:00
|
|
|
Point pt = this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
|
2009-01-03 18:44:20 +00:00
|
|
|
|
2021-02-13 21:50:51 +00:00
|
|
|
SetDParam(0, _settings_game.game_creation.ending_year);
|
2022-09-05 20:05:18 +00:00
|
|
|
DrawStringMultiLine(pt.x + ScaleSpriteTrad(70), pt.x + ScaleSpriteTrad(570), pt.y, pt.y + ScaleSpriteTrad(140), !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
|
2009-01-03 18:44:20 +00:00
|
|
|
|
|
|
|
/* Draw Highscore peepz */
|
|
|
|
for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
|
|
|
|
SetDParam(0, i + 1);
|
2022-09-05 20:05:18 +00:00
|
|
|
DrawString(pt.x + ScaleSpriteTrad(40), pt.x + ScaleSpriteTrad(600), pt.y + ScaleSpriteTrad(140 + i * 55), STR_HIGHSCORE_POSITION);
|
2009-01-03 18:44:20 +00:00
|
|
|
|
|
|
|
if (hs[i].company[0] != '\0') {
|
|
|
|
TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
|
|
|
|
|
2013-06-29 12:09:55 +00:00
|
|
|
SetDParamStr(0, hs[i].company);
|
2022-09-05 20:05:18 +00:00
|
|
|
DrawString(pt.x + ScaleSpriteTrad(71), pt.x + ScaleSpriteTrad(569), pt.y + ScaleSpriteTrad(140 + i * 55), STR_JUST_BIG_RAW_STRING, colour);
|
2009-01-03 18:44:20 +00:00
|
|
|
SetDParam(0, hs[i].title);
|
|
|
|
SetDParam(1, hs[i].score);
|
2022-09-05 20:05:18 +00:00
|
|
|
DrawString(pt.x + ScaleSpriteTrad(71), pt.x + ScaleSpriteTrad(569), pt.y + ScaleSpriteTrad(140) + FONT_HEIGHT_LARGE + ScaleSpriteTrad(i * 55), STR_HIGHSCORE_STATS, colour);
|
2009-01-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-11 18:10:32 +00:00
|
|
|
static const NWidgetPart _nested_highscore_widgets[] = {
|
2021-05-08 08:54:32 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_BROWN, WID_H_BACKGROUND), SetResize(1, 1), EndContainer(),
|
2009-04-11 18:10:32 +00:00
|
|
|
};
|
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _highscore_desc(
|
2019-04-10 21:07:06 +00:00
|
|
|
WDP_MANUAL, nullptr, 0, 0,
|
2009-01-03 18:44:20 +00:00
|
|
|
WC_HIGHSCORE, WC_NONE,
|
|
|
|
0,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_highscore_widgets, lengthof(_nested_highscore_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2009-01-03 18:44:20 +00:00
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _endgame_desc(
|
2019-04-10 21:07:06 +00:00
|
|
|
WDP_MANUAL, nullptr, 0, 0,
|
2009-01-03 18:44:20 +00:00
|
|
|
WC_ENDSCREEN, WC_NONE,
|
|
|
|
0,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_highscore_widgets, lengthof(_nested_highscore_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2009-01-03 18:44:20 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Show the highscore table for a given difficulty. When called from
|
2009-01-03 18:44:20 +00:00
|
|
|
* endgame ranking is set to the top5 element that was newly added
|
2010-08-01 19:44:49 +00:00
|
|
|
* and is thus highlighted
|
|
|
|
*/
|
2009-01-03 18:44:20 +00:00
|
|
|
void ShowHighscoreTable(int difficulty, int8 ranking)
|
|
|
|
{
|
2021-05-17 13:46:38 +00:00
|
|
|
CloseWindowByClass(WC_HIGHSCORE);
|
2009-01-03 18:44:20 +00:00
|
|
|
new HighScoreWindow(&_highscore_desc, difficulty, ranking);
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Show the endgame victory screen in 2050. Update the new highscore
|
2010-08-01 19:44:49 +00:00
|
|
|
* if it was high enough
|
|
|
|
*/
|
2009-01-03 18:44:20 +00:00
|
|
|
void ShowEndGameChart()
|
|
|
|
{
|
2009-02-23 01:51:19 +00:00
|
|
|
/* Dedicated server doesn't need the highscore window and neither does -v null. */
|
2009-05-17 01:00:56 +00:00
|
|
|
if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
|
2009-01-03 18:44:20 +00:00
|
|
|
|
|
|
|
HideVitalWindows();
|
2021-05-17 13:46:38 +00:00
|
|
|
CloseWindowByClass(WC_ENDSCREEN);
|
2009-01-03 18:44:20 +00:00
|
|
|
new EndGameWindow(&_endgame_desc);
|
|
|
|
}
|