# highscore_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "highscore.h"
    00013 #include "table/strings.h"
    00014 #include "gfx_func.h"
    00015 #include "table/sprites.h"
    00016 #include "window_gui.h"
    00017 #include "window_func.h"
    00018 #include "network/network.h"
    00019 #include "command_func.h"
    00020 #include "company_func.h"
    00021 #include "company_base.h"
    00022 #include "strings_func.h"
    00023 #include "openttd.h"
    00024
    00025 enum HighscoreWidgets {
    00026   HSW_BACKGROUND,
    00027 };
    00028
    00029 struct EndGameHighScoreBaseWindow : Window {
    00030   uint32 background_img;
    00031   int8 rank;
    00032
    00033   EndGameHighScoreBaseWindow(const WindowDesc *desc) : Window()
    00034   {
    00035     this->InitNested(desc);
    00036     ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
    00037   }
    00038
    00039   /* Always draw a maximized window and within it the centered background */
    00040   void SetupHighScoreEndWindow()
    00041   {
    00042     /* Resize window to "full-screen". */
    00043     if (this->width != _screen.width || this->height != _screen.height) ResizeWindow(this, _screen.width - this->width, _screen.height - this->height);
    00044
    00045     this->DrawWidgets();
    00046
    00047     Point pt = this->GetTopLeft640x480();
    00048     /* Center Highscore/Endscreen background */
    00049     for (uint i = 0; i < 10; i++) { // the image is split into 10 50px high parts
    00050       DrawSprite(this->background_img + i, PAL_NONE, pt.x, pt.y + (i * 50));
    00051     }
    00052   }
    00053
    00055   Point GetTopLeft640x480()
    00056   {
    00057     Point pt = {max(0, (_screen.width  / 2) - (640 / 2)), max(0, (_screen.height / 2) - (480 / 2))};
    00058     return pt;
    00059   }
    00060
    00061   virtual void OnClick(Point pt, int widget)
    00062   {
    00063     delete this;
    00064   }
    00065 };
    00066
    00068 struct EndGameWindow : EndGameHighScoreBaseWindow {
    00069   EndGameWindow(const WindowDesc *desc) : EndGameHighScoreBaseWindow(desc)
    00070   {
    00071     /* Pause in single-player to have a look at the highscore at your own leisure */
    00072     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
    00073
    00074     this->background_img = SPR_TYCOON_IMG1_BEGIN;
    00075
    00076     if (_local_company != COMPANY_SPECTATOR) {
    00077       const Company *c = Company::Get(_local_company);
    00078       if (c->old_economy[0].performance_history == SCORE_MAX) {
    00079         this->background_img = SPR_TYCOON_IMG2_BEGIN;
    00080       }
    00081     }
    00082
    00083     /* In a network game show the endscores of the custom difficulty 'network' which is the last one
    00084      * as well as generate a TOP5 of that game, and not an all-time top5. */
    00085     if (_networking) {
    00086       this->window_number = lengthof(_highscore_table) - 1;
    00087       this->rank = SaveHighScoreValueNetwork();
    00088     } else {
    00089       /* in single player _local company is always valid */
    00090       const Company *c = Company::Get(_local_company);
    00091       this->window_number = _settings_game.difficulty.diff_level;
    00092       this->rank = SaveHighScoreValue(c);
    00093     }
    00094
    00095     MarkWholeScreenDirty();
    00096   }
    00097
    00098   ~EndGameWindow()
    00099   {
    00100     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
    00101     ShowHighscoreTable(this->window_number, this->rank);
    00102   }
    00103
    00104   virtual void OnPaint()
    00105   {
    00106     this->SetupHighScoreEndWindow();
    00107     Point pt = this->GetTopLeft640x480();
    00108
    00109     const Company *c = Company::GetIfValid(_local_company);
    00110     if (c == NULL) return;
    00111
    00112     /* We need to get performance from last year because the image is shown
    00113      * at the start of the new year when these things have already been copied */
    00114     if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/
    00115       SetDParam(0, c->index);
    00116       SetDParam(1, c->index);
    00117       SetDParam(2, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
    00118       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);
    00119     } else {
    00120       SetDParam(0, c->index);
    00121       SetDParam(1, EndGameGetPerformanceTitleFromValue(c->old_economy[0].performance_history));
    00122       DrawStringMultiLine(pt.x + 36, pt.x + 640, pt.y + 140, pt.y + 206, STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS, TC_FROMSTRING, SA_CENTER);
    00123     }
    00124   }
    00125 };
    00126
    00127 struct HighScoreWindow : EndGameHighScoreBaseWindow {
    00128   HighScoreWindow(const WindowDesc *desc, int difficulty, int8 ranking) : EndGameHighScoreBaseWindow(desc)
    00129   {
    00130     /* pause game to show the chart */
    00131     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE);
    00132
    00133     /* Close all always on-top windows to get a clean screen */
    00134     if (_game_mode != GM_MENU) HideVitalWindows();
    00135
    00136     MarkWholeScreenDirty();
    00137     this->window_number = difficulty; // show highscore chart for difficulty...
    00138     this->background_img = SPR_HIGHSCORE_CHART_BEGIN; // which background to show
    00139     this->rank = ranking;
    00140   }
    00141
    00142   ~HighScoreWindow()
    00143   {
    00144     if (_game_mode != GM_MENU) ShowVitalWindows();
    00145
    00146     if (!_networking) DoCommandP(0, PM_PAUSED_NORMAL, 0, CMD_PAUSE); // unpause
    00147   }
    00148
    00149   virtual void OnPaint()
    00150   {
    00151     const HighScore *hs = _highscore_table[this->window_number];
    00152
    00153     this->SetupHighScoreEndWindow();
    00154     Point pt = this->GetTopLeft640x480();
    00155
    00156     SetDParam(0, ORIGINAL_END_YEAR);
    00157     SetDParam(1, this->window_number + STR_DIFFICULTY_LEVEL_EASY);
    00158     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);
    00159
    00160     /* Draw Highscore peepz */
    00161     for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
    00162       SetDParam(0, i + 1);
    00163       DrawString(pt.x + 40, pt.x + 600, pt.y + 140 + (i * 55), STR_HIGHSCORE_POSITION);
    00164
    00165       if (hs[i].company[0] != '\0') {
    00166         TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
    00167
    00168         DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + (i * 55), hs[i].company, colour);
    00169         SetDParam(0, hs[i].title);
    00170         SetDParam(1, hs[i].score);
    00171         DrawString(pt.x + 71, pt.x + 569, pt.y + 140 + FONT_HEIGHT_LARGE + (i * 55), STR_HIGHSCORE_STATS, colour);
    00172       }
    00173     }
    00174   }
    00175 };
    00176
    00177 static const NWidgetPart _nested_highscore_widgets[] = {
    00178   NWidget(WWT_PANEL, COLOUR_END, HSW_BACKGROUND), SetMinimalSize(641, 481), SetResize(1, 1), EndContainer(),
    00179 };
    00180
    00181 static const WindowDesc _highscore_desc(
    00182   WDP_MANUAL, 0, 0,
    00183   WC_HIGHSCORE, WC_NONE,
    00184   0,
    00185   _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
    00186 );
    00187
    00188 static const WindowDesc _endgame_desc(
    00189   WDP_MANUAL, 0, 0,
    00190   WC_ENDSCREEN, WC_NONE,
    00191   0,
    00192   _nested_highscore_widgets, lengthof(_nested_highscore_widgets)
    00193 );
    00194
    00198 void ShowHighscoreTable(int difficulty, int8 ranking)
    00199 {
    00200   DeleteWindowByClass(WC_HIGHSCORE);
    00201   new HighScoreWindow(&_highscore_desc, difficulty, ranking);
    00202 }
    00203
    00206 void ShowEndGameChart()
    00207 {
    00208   /* Dedicated server doesn't need the highscore window and neither does -v null. */
    00209   if (_network_dedicated || (!_networking && !Company::IsValidID(_local_company))) return;
    00210
    00211   HideVitalWindows();
    00212   DeleteWindowByClass(WC_ENDSCREEN);
    00213   new EndGameWindow(&_endgame_desc);
    00214 }