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/>.
8 /** @file highscore.cpp Definition of functions used for highscore handling */
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"
21 #include "safeguards.h"
23 HighScoresTable _highscore_table
; ///< Table with all the high scores.
24 std::string _highscore_file
; ///< The file to store the highscore data in.
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
];
53 * Save the highscore for the company
54 * @param c The company to insert.
55 * @return The index the company got in the high score table, or -1 when it did not end up in the table.
57 int8_t SaveHighScoreValue(const Company
*c
)
59 auto &highscores
= _highscore_table
[SP_CUSTOM
];
60 uint16_t score
= c
->old_economy
[0].performance_history
;
62 auto it
= std::find_if(highscores
.begin(), highscores
.end(), [&score
](auto &highscore
) { return highscore
.score
<= score
; });
64 /* If we cannot find it, our score is not high enough. */
65 if (it
== highscores
.end()) return -1;
67 /* Move all elements one down starting from the replaced one */
68 std::move_backward(it
, highscores
.end() - 1, highscores
.end());
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
75 it
->title
= EndGameGetPerformanceTitleFromValue(score
);
76 return std::distance(highscores
.begin(), it
);
79 /** Sort all companies given their performance */
80 static bool HighScoreSorter(const Company
* const &a
, const Company
* const &b
)
82 return b
->old_economy
[0].performance_history
< a
->old_economy
[0].performance_history
;
86 * Save the highscores in a network game when it has ended
87 * @return Position of the local company in the highscore list.
89 int8_t SaveHighScoreValueNetwork()
91 const Company
*cl
[MAX_COMPANIES
];
93 int8_t local_company_place
= -1;
95 /* Sort all active companies with the highest score first */
96 for (const Company
*c
: Company::Iterate()) cl
[count
++] = c
;
98 std::sort(std::begin(cl
), std::begin(cl
) + count
, HighScoreSorter
);
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
{});
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
);
113 if (c
->index
== _local_company
) local_company_place
= static_cast<int8_t>(i
);
116 return local_company_place
;
119 /** Save HighScore table to file */
120 void SaveToHighScore()
122 auto ofp
= FileHandle::Open(_highscore_file
, "wb");
123 if (!ofp
.has_value()) return;
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.");
142 /** Initialize the highscore table to 0 and if any file exists, load in values */
143 void LoadFromHighScore()
145 std::fill(_highscore_table
.begin(), _highscore_table
.end(), HighScores
{});
147 auto ofp
= FileHandle::Open(_highscore_file
, "rb");
148 if (!ofp
.has_value()) return;
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. */
156 char buffer
[std::numeric_limits
<decltype(name_length
)>::max() + 1];
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");
165 hs
.name
= StrMakeValid(std::string_view(buffer
, name_length
));
166 hs
.title
= EndGameGetPerformanceTitleFromValue(hs
.score
);