18 #include "table/strings.h"
26 static const StringID _endgame_perf_titles[] = {
27 STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
28 STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
29 STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
30 STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
31 STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
32 STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
33 STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
34 STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
35 STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
36 STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
37 STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
38 STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
39 STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
40 STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
41 STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
42 STR_HIGHSCORE_PERFORMANCE_TITLE_TYCOON_OF_THE_CENTURY
45 StringID EndGameGetPerformanceTitleFromValue(uint value)
47 value = std::min<uint>(value / 64,
lengthof(_endgame_perf_titles) - 1);
49 return _endgame_perf_titles[value];
65 auto it = std::find_if(highscores.begin(), highscores.end(), [&score](
auto &highscore) { return highscore.score <= score; });
68 if (it == highscores.end())
return -1;
71 std::move_backward(it, highscores.end() - 1, highscores.end());
78 it->title = EndGameGetPerformanceTitleFromValue(score);
79 return std::distance(highscores.begin(), it);
96 int8_t local_company_place = -1;
105 std::fill(highscores.begin(), highscores.end(),
HighScore{});
107 for (
size_t i = 0; i < count && i < highscores.size(); i++) {
109 auto &highscore = highscores[i];
112 highscore.name =
GetString(STR_HIGHSCORE_NAME);
114 highscore.title = EndGameGetPerformanceTitleFromValue(highscore.score);
119 return local_company_place;
126 if (!ofp.has_value())
return;
133 uint8_t name_length = ClampTo<uint8_t>(hs.name.size());
134 if (fwrite(&name_length,
sizeof(name_length), 1, fp) != 1 ||
135 fwrite(hs.name.data(), name_length, 1, fp) > 1 ||
136 fwrite(&hs.score,
sizeof(hs.score), 1, fp) != 1 ||
137 fwrite(
" ", 2, 1, fp) != 1) {
138 Debug(misc, 1,
"Could not save highscore.");
151 if (!ofp.has_value())
return;
159 char buffer[std::numeric_limits<decltype(name_length)>::max() + 1];
161 if (fread(&name_length,
sizeof(name_length), 1, fp) != 1 ||
162 fread(buffer, name_length, 1, fp) > 1 ||
163 fread(&hs.score,
sizeof(hs.score), 1, fp) != 1 ||
164 fseek(fp, 2, SEEK_CUR) == -1) {
165 Debug(misc, 1,
"Highscore corrupted");
168 hs.name =
StrMakeValid(std::string_view(buffer, name_length));
169 hs.title = EndGameGetPerformanceTitleFromValue(hs.score);