highscore.cpp

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

Generated on Tue Jul 21 18:48:23 2009 for OpenTTD by  doxygen 1.5.6