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

Generated on Wed Dec 30 20:40:02 2009 for OpenTTD by  doxygen 1.5.6