# highscore.cpp

Go to the documentation of this file.

    00001 /* $Id: highscore.cpp 26062 2013-11-23 13:18:29Z rubidium $ */
    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 "stdafx.h"
    00013 #include "highscore.h"
    00014 #include "company_base.h"
    00015 #include "company_func.h"
    00016 #include "cheat_func.h"
    00017 #include "string_func.h"
    00018 #include "strings_func.h"
    00019 #include "table/strings.h"
    00020 #include "core/sort_func.hpp"
    00021 #include "debug.h"
    00022
    00023 HighScore _highscore_table[SP_HIGHSCORE_END][5];
    00024 char *_highscore_file;
    00025
    00026 static const StringID _endgame_perf_titles[] = {
    00027   STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
    00028   STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
    00029   STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
    00030   STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
    00031   STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
    00032   STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
    00033   STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
    00034   STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
    00035   STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
    00036   STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
    00037   STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
    00038   STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
    00039   STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
    00040   STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
    00041   STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
    00042   STR_HIGHSCORE_PERFORMANCE_TITLE_TYCOON_OF_THE_CENTURY
    00043 };
    00044
    00045 StringID EndGameGetPerformanceTitleFromValue(uint value)
    00046 {
    00047   value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
    00048
    00049   return _endgame_perf_titles[value];
    00050 }
    00051
    00053 int8 SaveHighScoreValue(const Company *c)
    00054 {
    00055   HighScore *hs = _highscore_table[SP_CUSTOM];
    00056   uint i;
    00057   uint16 score = c->old_economy[0].performance_history;
    00058
    00059   /* Exclude cheaters from the honour of being in the highscore table */
    00060   if (CheatHasBeenUsed()) return -1;
    00061
    00062   for (i = 0; i < lengthof(_highscore_table[0]); i++) {
    00063     /* You are in the TOP5. Move all values one down and save us there */
    00064     if (hs[i].score <= score) {
    00065       /* move all elements one down starting from the replaced one */
    00066       memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
    00067       SetDParam(0, c->index);
    00068       SetDParam(1, c->index);
    00069       GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
    00070       hs[i].score = score;
    00071       hs[i].title = EndGameGetPerformanceTitleFromValue(score);
    00072       return i;
    00073     }
    00074   }
    00075
    00076   return -1; // too bad; we did not make it into the top5
    00077 }
    00078
    00080 static int CDECL HighScoreSorter(const Company * const *a, const Company * const *b)
    00081 {
    00082   return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
    00083 }
    00084
    00089 int8 SaveHighScoreValueNetwork()
    00090 {
    00091   const Company *c;
    00092   const Company *cl[MAX_COMPANIES];
    00093   uint count = 0;
    00094   int8 company = -1;
    00095
    00096   /* Sort all active companies with the highest score first */
    00097   FOR_ALL_COMPANIES(c) cl[count++] = c;
    00098
    00099   QSortT(cl, count, &HighScoreSorter);
    00100
    00101   {
    00102     uint i;
    00103
    00104     memset(_highscore_table[SP_MULTIPLAYER], 0, sizeof(_highscore_table[SP_MULTIPLAYER]));
    00105
    00106     /* Copy over Top5 companies */
    00107     for (i = 0; i < lengthof(_highscore_table[SP_MULTIPLAYER]) && i < count; i++) {
    00108       HighScore *hs = &_highscore_table[SP_MULTIPLAYER][i];
    00109
    00110       SetDParam(0, cl[i]->index);
    00111       SetDParam(1, cl[i]->index);
    00112       GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
    00113       hs->score = cl[i]->old_economy[0].performance_history;
    00114       hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
    00115
    00116       /* get the ranking of the local company */
    00117       if (cl[i]->index == _local_company) company = i;
    00118     }
    00119   }
    00120
    00121   /* Add top5 companies to highscore table */
    00122   return company;
    00123 }
    00124
    00126 void SaveToHighScore()
    00127 {
    00128   FILE *fp = fopen(_highscore_file, "wb");
    00129
    00130   if (fp != NULL) {
    00131     uint i;
    00132     HighScore *hs;
    00133
    00134     for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
    00135       for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
    00136         /* First character is a command character, so strlen will fail on that */
    00137         byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
    00138
    00139         if (fwrite(&length, sizeof(length), 1, fp)       != 1 || // write away string length
    00140             fwrite(hs->company, length, 1, fp)           >  1 || // Yes... could be 0 bytes too
    00141             fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
    00142             fwrite("  ", 2, 1, fp)                       != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
    00143           DEBUG(misc, 1, "Could not save highscore.");
    00144           i = SP_SAVED_HIGHSCORE_END;
    00145           break;
    00146         }
    00147       }
    00148     }
    00149     fclose(fp);
    00150   }
    00151 }
    00152
    00154 void LoadFromHighScore()
    00155 {
    00156   FILE *fp = fopen(_highscore_file, "rb");
    00157
    00158   memset(_highscore_table, 0, sizeof(_highscore_table));
    00159
    00160   if (fp != NULL) {
    00161     uint i;
    00162     HighScore *hs;
    00163
    00164     for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
    00165       for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
    00166         byte length;
    00167         if (fread(&length, sizeof(length), 1, fp)                              !=  1 ||
    00168             fread(hs->company, min<int>(lengthof(hs->company), length), 1, fp) >   1 || // Yes... could be 0 bytes too
    00169             fread(&hs->score, sizeof(hs->score), 1, fp)                        !=  1 ||
    00170             fseek(fp, 2, SEEK_CUR)                                             == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
    00171           DEBUG(misc, 1, "Highscore corrupted");
    00172           i = SP_SAVED_HIGHSCORE_END;
    00173           break;
    00174         }
    00175         str_validate(hs->company, lastof(hs->company), SVS_NONE);
    00176         hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
    00177       }
    00178     }
    00179     fclose(fp);
    00180   }
    00181 }