OpenTTD Source 20250523-master-g321f7e8683
highscore.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "highscore.h"
12#include "company_base.h"
13#include "company_func.h"
14#include "cheat_func.h"
15#include "fileio_func.h"
16#include "string_func.h"
17#include "strings_func.h"
18#include "debug.h"
19
20#include "table/strings.h"
21
22#include "safeguards.h"
23
25std::string _highscore_file;
26
27static const StringID _endgame_perf_titles[] = {
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_BUSINESSMAN,
33 STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
34 STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
35 STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
36 STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
37 STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
38 STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
39 STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
40 STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
41 STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
42 STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
43 STR_HIGHSCORE_PERFORMANCE_TITLE_TYCOON_OF_THE_CENTURY
44};
45
46StringID EndGameGetPerformanceTitleFromValue(uint value)
47{
48 value = std::min<uint>(value / 64, lengthof(_endgame_perf_titles) - 1);
49
50 return _endgame_perf_titles[value];
51}
52
59{
60 auto &highscores = _highscore_table[SP_CUSTOM];
61 uint16_t score = c->old_economy[0].performance_history;
62
63 auto it = std::ranges::find_if(highscores, [&score](auto &highscore) { return highscore.score <= score; });
64
65 /* If we cannot find it, our score is not high enough. */
66 if (it == highscores.end()) return -1;
67
68 /* Move all elements one down starting from the replaced one */
69 std::move_backward(it, highscores.end() - 1, highscores.end());
70
71 /* Fill the elements. */
72 it->name = GetString(STR_HIGHSCORE_NAME, c->index, c->index); // get manager/company name string
73 it->score = score;
74 it->title = EndGameGetPerformanceTitleFromValue(score);
75 return std::distance(highscores.begin(), it);
76}
77
79static bool HighScoreSorter(const Company * const &a, const Company * const &b)
80{
81 return b->old_economy[0].performance_history < a->old_economy[0].performance_history;
82}
83
89{
90 const Company *cl[MAX_COMPANIES];
91 size_t count = 0;
92 int8_t local_company_place = -1;
93
94 /* Sort all active companies with the highest score first */
95 for (const Company *c : Company::Iterate()) cl[count++] = c;
96
97 std::sort(std::begin(cl), std::begin(cl) + count, HighScoreSorter);
98
99 /* Clear the high scores from the previous network game. */
100 auto &highscores = _highscore_table[SP_MULTIPLAYER];
101 std::fill(highscores.begin(), highscores.end(), HighScore{});
102
103 for (size_t i = 0; i < count && i < highscores.size(); i++) {
104 const Company *c = cl[i];
105 auto &highscore = highscores[i];
106 highscore.name = GetString(STR_HIGHSCORE_NAME, c->index, c->index); // get manager/company name string
107 highscore.score = c->old_economy[0].performance_history;
108 highscore.title = EndGameGetPerformanceTitleFromValue(highscore.score);
109
110 if (c->index == _local_company) local_company_place = static_cast<int8_t>(i);
111 }
112
113 return local_company_place;
114}
115
118{
119 auto ofp = FileHandle::Open(_highscore_file, "wb");
120 if (!ofp.has_value()) return;
121 auto &fp = *ofp;
122
123 /* Does not iterate through the complete array!. */
124 for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
125 for (HighScore &hs : _highscore_table[i]) {
126 /* This code is weird and old fashioned to keep compatibility with the old high score files. */
127 uint8_t name_length = ClampTo<uint8_t>(hs.name.size());
128 if (fwrite(&name_length, sizeof(name_length), 1, fp) != 1 || // Write the string length of the name
129 fwrite(hs.name.data(), name_length, 1, fp) > 1 || // Yes... could be 0 bytes too
130 fwrite(&hs.score, sizeof(hs.score), 1, fp) != 1 ||
131 fwrite(" ", 2, 1, fp) != 1) { // Used to be hs.title, not saved anymore; compatibility
132 Debug(misc, 1, "Could not save highscore.");
133 return;
134 }
135 }
136 }
137}
138
141{
142 std::fill(_highscore_table.begin(), _highscore_table.end(), HighScores{});
143
144 auto ofp = FileHandle::Open(_highscore_file, "rb");
145 if (!ofp.has_value()) return;
146 auto &fp = *ofp;
147
148 /* Does not iterate through the complete array!. */
149 for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
150 for (HighScore &hs : _highscore_table[i]) {
151 /* This code is weird and old fashioned to keep compatibility with the old high score files. */
152 uint8_t name_length;
153 char buffer[std::numeric_limits<decltype(name_length)>::max() + 1];
154
155 if (fread(&name_length, sizeof(name_length), 1, fp) != 1 ||
156 fread(buffer, name_length, 1, fp) > 1 || // Yes... could be 0 bytes too
157 fread(&hs.score, sizeof(hs.score), 1, fp) != 1 ||
158 fseek(fp, 2, SEEK_CUR) == -1) { // Used to be hs.title, not saved anymore; compatibility
159 Debug(misc, 1, "Highscore corrupted");
160 return;
161 }
162 hs.name = StrMakeValid(std::string_view(buffer, name_length));
163 hs.title = EndGameGetPerformanceTitleFromValue(hs.score);
164 }
165 }
166}
Functions related to cheating.
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
Definition fileio.cpp:1168
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(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
Functions 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.
Definition highscore.cpp:88
void SaveToHighScore()
Save HighScore table to file.
static bool HighScoreSorter(const Company *const &a, const Company *const &b)
Sort all companies given their performance.
Definition highscore.cpp:79
HighScoresTable _highscore_table
Table with all the high scores.
Definition highscore.cpp:24
std::string _highscore_file
The file to store the highscore data in.
Definition highscore.cpp:25
int8_t SaveHighScoreValue(const Company *c)
Save the highscore for the company.
Definition highscore.cpp:58
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.
Definition highscore.h:24
std::array< HighScore, 5 > HighScores
Record 5 high scores.
Definition highscore.h:23
A number of safeguards to prevent using unsafe methods.
@ 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.
Definition stdafx.h:271
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:117
Functions related to low-level strings.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:415
Functions related to OTTD's strings.
uint32_t 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.
std::string name
Name of the company if the user changed it.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Tindex index
Index of this pool item.