1
/* $Id: highscore.cpp 15299 2009-01-31 20:16:06Z smatz $ */
3
/** @file highscore.cpp Definition of functions used for highscore handling */
6
#include "settings_type.h"
7
#include "company_base.h"
8
#include "company_func.h"
9
#include "cheat_func.h"
10
#include "string_func.h"
11
#include "strings_func.h"
12
#include "table/strings.h"
13
#include "core/sort_func.hpp"
14
#include "variables.h"
17
HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
19
static const StringID _endgame_perf_titles[] = {
25
STR_0214_ENTREPRENEUR,
26
STR_0214_ENTREPRENEUR,
27
STR_0215_INDUSTRIALIST,
28
STR_0215_INDUSTRIALIST,
35
STR_0219_TYCOON_OF_THE_CENTURY
38
StringID EndGameGetPerformanceTitleFromValue(uint value)
40
value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
42
return _endgame_perf_titles[value];
45
/** Save the highscore for the company */
46
int8 SaveHighScoreValue(const Company *c)
48
HighScore *hs = _highscore_table[_settings_game.difficulty.diff_level];
50
uint16 score = c->old_economy[0].performance_history;
52
/* Exclude cheaters from the honour of being in the highscore table */
53
if (CheatHasBeenUsed()) return -1;
55
for (i = 0; i < lengthof(_highscore_table[0]); i++) {
56
/* You are in the TOP5. Move all values one down and save us there */
57
if (hs[i].score <= score) {
58
/* move all elements one down starting from the replaced one */
59
memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
60
SetDParam(0, c->index);
61
SetDParam(1, c->index);
62
GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
64
hs[i].title = EndGameGetPerformanceTitleFromValue(score);
69
return -1; // too bad; we did not make it into the top5
72
/** Sort all companies given their performance */
73
static int CDECL HighScoreSorter(const Company * const *a, const Company * const *b)
75
return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
78
/* Save the highscores in a network game when it has ended */
79
#define LAST_HS_ITEM lengthof(_highscore_table) - 1
80
int8 SaveHighScoreValueNetwork()
83
const Company *cl[MAX_COMPANIES];
87
/* Sort all active companies with the highest score first */
88
FOR_ALL_COMPANIES(c) cl[count++] = c;
90
QSortT(cl, count, &HighScoreSorter);
95
memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
97
/* Copy over Top5 companies */
98
for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
99
HighScore *hs = &_highscore_table[LAST_HS_ITEM][i];
101
SetDParam(0, cl[i]->index);
102
SetDParam(1, cl[i]->index);
103
GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
104
hs->score = cl[i]->old_economy[0].performance_history;
105
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
107
/* get the ranking of the local company */
108
if (cl[i]->index == _local_company) company = i;
112
/* Add top5 companys to highscore table */
116
/** Save HighScore table to file */
117
void SaveToHighScore()
119
FILE *fp = fopen(_highscore_file, "wb");
125
for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
126
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
127
/* First character is a command character, so strlen will fail on that */
128
byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
130
if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length
131
fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
132
fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
133
fwrite(" ", 2, 1, fp) != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
134
DEBUG(misc, 1, "Could not save highscore.");
144
/** Initialize the highscore table to 0 and if any file exists, load in values */
145
void LoadFromHighScore()
147
FILE *fp = fopen(_highscore_file, "rb");
149
memset(_highscore_table, 0, sizeof(_highscore_table));
155
for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
156
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
158
if (fread(&length, sizeof(length), 1, fp) != 1 ||
159
fread(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
160
fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
161
fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
162
DEBUG(misc, 1, "Highscore corrupted");
166
*lastof(hs->company) = '\0';
167
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);