19#include "table/strings.h"
26static 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
45StringID EndGameGetPerformanceTitleFromValue(uint value)
47 value = std::min<uint>(value / 64,
lengthof(_endgame_perf_titles) - 1);
49 return _endgame_perf_titles[value];
60 uint16_t score = c->
old_economy[0].performance_history;
62 auto it = std::ranges::find_if(highscores, [&score](
auto &highscore) {
return highscore.score <= score; });
65 if (it == highscores.end())
return -1;
68 std::move_backward(it, highscores.end() - 1, highscores.end());
71 it->name =
GetString(STR_HIGHSCORE_NAME, c->index, c->index);
73 it->title = EndGameGetPerformanceTitleFromValue(score);
74 return std::distance(highscores.begin(), it);
89 std::vector<const Company *> cl;
90 int8_t local_company_place = -1;
99 std::fill(highscores.begin(), highscores.end(),
HighScore{});
101 for (
size_t i = 0; i < cl.size() && i < highscores.size(); i++) {
103 auto &highscore = highscores[i];
104 highscore.name =
GetString(STR_HIGHSCORE_NAME, c->index, c->index);
105 highscore.score = c->
old_economy[0].performance_history;
106 highscore.title = EndGameGetPerformanceTitleFromValue(highscore.score);
108 if (c->index ==
_local_company) local_company_place =
static_cast<int8_t
>(i);
111 return local_company_place;
118 if (!ofp.has_value())
return;
126 if (fwrite(&name_length,
sizeof(name_length), 1, fp) != 1 ||
127 fwrite(hs.name.data(), name_length, 1, fp) > 1 ||
128 fwrite(&hs.score,
sizeof(hs.score), 1, fp) != 1 ||
129 fwrite(
" ", 2, 1, fp) != 1) {
143 if (!ofp.has_value())
return;
151 char buffer[std::numeric_limits<
decltype(name_length)>::max() + 1];
153 if (fread(&name_length,
sizeof(name_length), 1, fp) != 1 ||
154 fread(buffer, name_length, 1, fp) > 1 ||
155 fread(&hs.score,
sizeof(hs.score), 1, fp) != 1 ||
156 fseek(fp, 2, SEEK_CUR) == -1) {
160 hs.name =
StrMakeValid(std::string_view(buffer, name_length));
161 hs.title = EndGameGetPerformanceTitleFromValue(hs.score);
Iterate a range of enum values.
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
Definition of stuff that is very close to a company, like the company struct itself.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Functions related to companies.
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
@ Misc
Misc message facility.
@ Error
Error, but we are recovering.
std::string _highscore_file
The file to store the highscore data in.
Types for standard in/out file operations.
void LoadFromHighScore()
Initialize the highscore table to 0 and if any file exists, load in values.
int8_t SaveHighScoreValueNetwork()
Save the highscores in a network game when it has ended.
void SaveToHighScore()
Save HighScore table to file.
static bool HighScoreSorter(const Company *const &a, const Company *const &b)
Sort all companies given their performance.
HighScoresTable _highscore_table
Table with all the high scores.
int8_t SaveHighScoreValue(const Company *c)
Save the highscore for the company.
Declaration of functions and types defined in highscore.h and highscore_gui.h.
std::array< HighScores, SP_HIGHSCORE_END > HighScoresTable
Record high score for each of the difficulty levels.
std::array< HighScore, 5 > HighScores
Record 5 high scores.
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
A number of safeguards to prevent using unsafe methods.
SettingsProfile
Settings profiles and highscore tables.
@ SP_CUSTOM
No profile, special "custom" highscore.
@ SP_MULTIPLAYER
Special "multiplayer" highscore. Not saved, always specific to the current game.
@ SP_SAVED_HIGHSCORE_END
End of saved highscore tables.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Functions related to low-level strings.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Functions related to OTTD's strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
std::array< CompanyEconomyEntry, MAX_HISTORY_QUARTERS > old_economy
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
static Pool::IterateWrapper< Company > Iterate(size_t from=0)