highscore.cpp

Go to the documentation of this file.
00001 /* $Id: highscore.cpp 18809 2010-01-15 16:41:15Z 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 "variables.h"
00022 #include "debug.h"
00023 
00024 HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
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[_settings_game.difficulty.diff_level];
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 
00085 /* Save the highscores in a network game when it has ended */
00086 #define LAST_HS_ITEM lengthof(_highscore_table) - 1
00087 int8 SaveHighScoreValueNetwork()
00088 {
00089   const Company *c;
00090   const Company *cl[MAX_COMPANIES];
00091   uint count = 0;
00092   int8 company = -1;
00093 
00094   /* Sort all active companies with the highest score first */
00095   FOR_ALL_COMPANIES(c) cl[count++] = c;
00096 
00097   QSortT(cl, count, &HighScoreSorter);
00098 
00099   {
00100     uint i;
00101 
00102     memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
00103 
00104     /* Copy over Top5 companies */
00105     for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
00106       HighScore *hs = &_highscore_table[LAST_HS_ITEM][i];
00107 
00108       SetDParam(0, cl[i]->index);
00109       SetDParam(1, cl[i]->index);
00110       GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
00111       hs->score = cl[i]->old_economy[0].performance_history;
00112       hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
00113 
00114       /* get the ranking of the local company */
00115       if (cl[i]->index == _local_company) company = i;
00116     }
00117   }
00118 
00119   /* Add top5 companys to highscore table */
00120   return company;
00121 }
00122 
00124 void SaveToHighScore()
00125 {
00126   FILE *fp = fopen(_highscore_file, "wb");
00127 
00128   if (fp != NULL) {
00129     uint i;
00130     HighScore *hs;
00131 
00132     for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
00133       for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
00134         /* First character is a command character, so strlen will fail on that */
00135         byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
00136 
00137         if (fwrite(&length, sizeof(length), 1, fp)       != 1 || // write away string length
00138             fwrite(hs->company, length, 1, fp)           >  1 || // Yes... could be 0 bytes too
00139             fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
00140             fwrite("  ", 2, 1, fp)                       != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
00141           DEBUG(misc, 1, "Could not save highscore.");
00142           i = LAST_HS_ITEM;
00143           break;
00144         }
00145       }
00146     }
00147     fclose(fp);
00148   }
00149 }
00150 
00152 void LoadFromHighScore()
00153 {
00154   FILE *fp = fopen(_highscore_file, "rb");
00155 
00156   memset(_highscore_table, 0, sizeof(_highscore_table));
00157 
00158   if (fp != NULL) {
00159     uint i;
00160     HighScore *hs;
00161 
00162     for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
00163       for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
00164         byte length;
00165         if (fread(&length, sizeof(length), 1, fp)       !=  1 ||
00166             fread(hs->company, length, 1, fp)           >   1 || // Yes... could be 0 bytes too
00167             fread(&hs->score, sizeof(hs->score), 1, fp) !=  1 ||
00168             fseek(fp, 2, SEEK_CUR)                      == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
00169           DEBUG(misc, 1, "Highscore corrupted");
00170           i = LAST_HS_ITEM;
00171           break;
00172         }
00173         *lastof(hs->company) = '\0';
00174         hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
00175       }
00176     }
00177     fclose(fp);
00178   }
00179 }

Generated on Thu Feb 4 17:20:24 2010 for OpenTTD by  doxygen 1.5.6