OpenTTD Source: src/highscore.cpp Source File

OpenTTD Source 15.0-beta1

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 "table/strings.h"
19#include "debug.h"
20
21#include "safeguards.h"
22
23 HighScoresTable _highscore_table;
24std::string _highscore_file;
25
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
43};
44
45 StringID EndGameGetPerformanceTitleFromValue(uint value)
46{
47 value = std::min<uint>(value / 64, lengthof(_endgame_perf_titles) - 1);
48
49 return _endgame_perf_titles[value];
50}
51
57int8_t SaveHighScoreValue(const Company *c)
58{
59 auto &highscores = _highscore_table[SP_CUSTOM];
60 uint16_t score = c->old_economy[0].performance_history;
61
62 auto it = std::ranges::find_if(highscores, [&score](auto &highscore) { return highscore.score <= score; });
63
64 /* If we cannot find it, our score is not high enough. */
65 if (it == highscores.end()) return -1;
66
67 /* Move all elements one down starting from the replaced one */
68 std::move_backward(it, highscores.end() - 1, highscores.end());
69
70 /* Fill the elements. */
71 SetDParam(0, c->index);
72 SetDParam(1, c->index);
73 it->name = GetString(STR_HIGHSCORE_NAME); // get manager/company name string
74 it->score = score;
75 it->title = EndGameGetPerformanceTitleFromValue(score);
76 return std::distance(highscores.begin(), it);
77}
78
80 static bool HighScoreSorter(const Company * const &a, const Company * const &b)
81{
82 return b->old_economy[0].performance_history < a->old_economy[0].performance_history;
83}
84
89int8_t SaveHighScoreValueNetwork()
90{
91 const Company *cl[MAX_COMPANIES];
92 size_t count = 0;
93 int8_t local_company_place = -1;
94
95 /* Sort all active companies with the highest score first */
96 for (const Company *c : Company::Iterate()) cl[count++] = c;
97
98 std::sort(std::begin(cl), std::begin(cl) + count, HighScoreSorter);
99
100 /* Clear the high scores from the previous network game. */
101 auto &highscores = _highscore_table[SP_MULTIPLAYER];
102 std::fill(highscores.begin(), highscores.end(), HighScore{});
103
104 for (size_t i = 0; i < count && i < highscores.size(); i++) {
105 const Company *c = cl[i];
106 auto &highscore = highscores[i];
107 SetDParam(0, c->index);
108 SetDParam(1, c->index);
109 highscore.name = GetString(STR_HIGHSCORE_NAME); // get manager/company name string
110 highscore.score = c->old_economy[0].performance_history;
111 highscore.title = EndGameGetPerformanceTitleFromValue(highscore.score);
112
113 if (c->index == _local_company) local_company_place = static_cast<int8_t>(i);
114 }
115
116 return local_company_place;
117}
118
120 void SaveToHighScore()
121{
122 auto ofp = FileHandle::Open(_highscore_file, "wb");
123 if (!ofp.has_value()) return;
124 auto &fp = *ofp;
125
126 /* Does not iterate through the complete array!. */
127 for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
128 for (HighScore &hs : _highscore_table[i]) {
129 /* This code is weird and old fashioned to keep compatibility with the old high score files. */
130 uint8_t name_length = ClampTo<uint8_t>(hs.name.size());
131 if (fwrite(&name_length, sizeof(name_length), 1, fp) != 1 || // Write the string length of the name
132 fwrite(hs.name.data(), name_length, 1, fp) > 1 || // Yes... could be 0 bytes too
133 fwrite(&hs.score, sizeof(hs.score), 1, fp) != 1 ||
134 fwrite(" ", 2, 1, fp) != 1) { // Used to be hs.title, not saved anymore; compatibility
135 Debug(misc, 1, "Could not save highscore.");
136 return;
137 }
138 }
139 }
140}
141
143 void LoadFromHighScore()
144{
145 std::fill(_highscore_table.begin(), _highscore_table.end(), HighScores{});
146
147 auto ofp = FileHandle::Open(_highscore_file, "rb");
148 if (!ofp.has_value()) return;
149 auto &fp = *ofp;
150
151 /* Does not iterate through the complete array!. */
152 for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
153 for (HighScore &hs : _highscore_table[i]) {
154 /* This code is weird and old fashioned to keep compatibility with the old high score files. */
155 uint8_t name_length;
156 char buffer[std::numeric_limits<decltype(name_length)>::max() + 1];
157
158 if (fread(&name_length, sizeof(name_length), 1, fp) != 1 ||
159 fread(buffer, name_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) { // Used to be hs.title, not saved anymore; compatibility
162 Debug(misc, 1, "Highscore corrupted");
163 return;
164 }
165 hs.name = StrMakeValid(std::string_view(buffer, name_length));
166 hs.title = EndGameGetPerformanceTitleFromValue(hs.score);
167 }
168 }
169}
cheat_func.h
Functions related to cheating.
FileHandle::Open
static std::optional< FileHandle > Open(const std::string &filename, const std::string &mode)
Open an RAII file handle if possible.
Definition fileio.cpp:1170
company_base.h
Definition of stuff that is very close to a company, like the company struct itself.
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition company_cmd.cpp:52
company_func.h
Functions related to companies.
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition company_type.h:23
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
fileio_func.h
Functions for Standard In/Out file operations.
LoadFromHighScore
void LoadFromHighScore()
Initialize the highscore table to 0 and if any file exists, load in values.
Definition highscore.cpp:143
SaveHighScoreValueNetwork
int8_t SaveHighScoreValueNetwork()
Save the highscores in a network game when it has ended.
Definition highscore.cpp:89
SaveToHighScore
void SaveToHighScore()
Save HighScore table to file.
Definition highscore.cpp:120
HighScoreSorter
static bool HighScoreSorter(const Company *const &a, const Company *const &b)
Sort all companies given their performance.
Definition highscore.cpp:80
_highscore_table
HighScoresTable _highscore_table
Table with all the high scores.
Definition highscore.cpp:23
_highscore_file
std::string _highscore_file
The file to store the highscore data in.
Definition highscore.cpp:24
SaveHighScoreValue
int8_t SaveHighScoreValue(const Company *c)
Save the highscore for the company.
Definition highscore.cpp:57
highscore.h
Declaration of functions and types defined in highscore.h and highscore_gui.h.
HighScoresTable
std::array< HighScores, SP_HIGHSCORE_END > HighScoresTable
Record high score for each of the difficulty levels.
Definition highscore.h:24
HighScores
std::array< HighScore, 5 > HighScores
Record 5 high scores.
Definition highscore.h:23
safeguards.h
A number of safeguards to prevent using unsafe methods.
SP_CUSTOM
@ SP_CUSTOM
No profile, special "custom" highscore.
Definition settings_type.h:46
SP_MULTIPLAYER
@ SP_MULTIPLAYER
Special "multiplayer" highscore. Not saved, always specific to the current game.
Definition settings_type.h:49
SP_SAVED_HIGHSCORE_END
@ SP_SAVED_HIGHSCORE_END
End of saved highscore tables.
Definition settings_type.h:47
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:280
StrMakeValid
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition string.cpp:107
string_func.h
Functions related to low-level strings.
SetDParam
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition strings.cpp:104
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition strings.cpp:333
strings_func.h
Functions related to OTTD's strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
CompanyEconomyEntry::performance_history
int32_t performance_history
Company score (scale 0-1000)
Definition company_base.h:28
CompanyProperties::old_economy
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition company_base.h:116
Company
Definition company_base.h:133
HighScore
Definition highscore.h:17
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:238
Pool::PoolItem<&_company_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:388