00001
00002
00003
00004
00005
00006
00007
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
00060 if (CheatHasBeenUsed()) return -1;
00061
00062 for (i = 0; i < lengthof(_highscore_table[0]); i++) {
00063
00064 if (hs[i].score <= score) {
00065
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));
00070 hs[i].score = score;
00071 hs[i].title = EndGameGetPerformanceTitleFromValue(score);
00072 return i;
00073 }
00074 }
00075
00076 return -1;
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
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
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));
00113 hs->score = cl[i]->old_economy[0].performance_history;
00114 hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
00115
00116
00117 if (cl[i]->index == _local_company) company = i;
00118 }
00119 }
00120
00121
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
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 ||
00140 fwrite(hs->company, length, 1, fp) > 1 ||
00141 fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
00142 fwrite(" ", 2, 1, fp) != 1) {
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, length, 1, fp) > 1 ||
00169 fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
00170 fseek(fp, 2, SEEK_CUR) == -1) {
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 }