mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: use std::array and std::string for high scores
This commit is contained in:
parent
c158089eff
commit
15c75e6f45
|
@ -12,6 +12,7 @@
|
||||||
#include "company_base.h"
|
#include "company_base.h"
|
||||||
#include "company_func.h"
|
#include "company_func.h"
|
||||||
#include "cheat_func.h"
|
#include "cheat_func.h"
|
||||||
|
#include "fileio_func.h"
|
||||||
#include "string_func.h"
|
#include "string_func.h"
|
||||||
#include "strings_func.h"
|
#include "strings_func.h"
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
@ -19,7 +20,7 @@
|
||||||
|
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
|
|
||||||
HighScore _highscore_table[SP_HIGHSCORE_END][5]; ///< various difficulty-settings; top 5
|
HighScoresTable _highscore_table; ///< Table with all the high scores.
|
||||||
std::string _highscore_file; ///< The file to store the highscore data in.
|
std::string _highscore_file; ///< The file to store the highscore data in.
|
||||||
|
|
||||||
static const StringID _endgame_perf_titles[] = {
|
static const StringID _endgame_perf_titles[] = {
|
||||||
|
@ -48,31 +49,34 @@ StringID EndGameGetPerformanceTitleFromValue(uint value)
|
||||||
return _endgame_perf_titles[value];
|
return _endgame_perf_titles[value];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Save the highscore for the company */
|
/**
|
||||||
int8 SaveHighScoreValue(const Company *c)
|
* Save the highscore for the company
|
||||||
|
* @param c The company to insert.
|
||||||
|
* @return The index the company got in the high score table, or -1 when it did not end up in the table.
|
||||||
|
*/
|
||||||
|
int8_t SaveHighScoreValue(const Company *c)
|
||||||
{
|
{
|
||||||
HighScore *hs = _highscore_table[SP_CUSTOM];
|
|
||||||
uint i;
|
|
||||||
uint16 score = c->old_economy[0].performance_history;
|
|
||||||
|
|
||||||
/* Exclude cheaters from the honour of being in the highscore table */
|
/* Exclude cheaters from the honour of being in the highscore table */
|
||||||
if (CheatHasBeenUsed()) return -1;
|
if (CheatHasBeenUsed()) return -1;
|
||||||
|
|
||||||
for (i = 0; i < lengthof(_highscore_table[0]); i++) {
|
auto &highscores = _highscore_table[SP_CUSTOM];
|
||||||
/* You are in the TOP5. Move all values one down and save us there */
|
uint16 score = c->old_economy[0].performance_history;
|
||||||
if (hs[i].score <= score) {
|
|
||||||
/* move all elements one down starting from the replaced one */
|
|
||||||
memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
|
|
||||||
SetDParam(0, c->index);
|
|
||||||
SetDParam(1, c->index);
|
|
||||||
GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
|
|
||||||
hs[i].score = score;
|
|
||||||
hs[i].title = EndGameGetPerformanceTitleFromValue(score);
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1; // too bad; we did not make it into the top5
|
auto it = std::find_if(highscores.begin(), highscores.end(), [&score](auto &highscore) { return highscore.score <= score; });
|
||||||
|
|
||||||
|
/* If we cannot find it, our score is not high enough. */
|
||||||
|
if (it == highscores.end()) return -1;
|
||||||
|
|
||||||
|
/* Move all elements one down starting from the replaced one */
|
||||||
|
std::move_backward(it, highscores.end() - 1, highscores.end());
|
||||||
|
|
||||||
|
/* Fill the elements. */
|
||||||
|
SetDParam(0, c->index);
|
||||||
|
SetDParam(1, c->index);
|
||||||
|
it->name = GetString(STR_HIGHSCORE_NAME); // get manager/company name string
|
||||||
|
it->score = score;
|
||||||
|
it->title = EndGameGetPerformanceTitleFromValue(score);
|
||||||
|
return std::distance(highscores.begin(), it);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sort all companies given their performance */
|
/** Sort all companies given their performance */
|
||||||
|
@ -85,95 +89,82 @@ static bool HighScoreSorter(const Company * const &a, const Company * const &b)
|
||||||
* Save the highscores in a network game when it has ended
|
* Save the highscores in a network game when it has ended
|
||||||
* @return Position of the local company in the highscore list.
|
* @return Position of the local company in the highscore list.
|
||||||
*/
|
*/
|
||||||
int8 SaveHighScoreValueNetwork()
|
int8_t SaveHighScoreValueNetwork()
|
||||||
{
|
{
|
||||||
const Company *cl[MAX_COMPANIES];
|
const Company *cl[MAX_COMPANIES];
|
||||||
uint count = 0;
|
size_t count = 0;
|
||||||
int8 company = -1;
|
int8_t local_company_place = -1;
|
||||||
|
|
||||||
/* Sort all active companies with the highest score first */
|
/* Sort all active companies with the highest score first */
|
||||||
for (const Company *c : Company::Iterate()) cl[count++] = c;
|
for (const Company *c : Company::Iterate()) cl[count++] = c;
|
||||||
|
|
||||||
std::sort(std::begin(cl), std::begin(cl) + count, HighScoreSorter);
|
std::sort(std::begin(cl), std::begin(cl) + count, HighScoreSorter);
|
||||||
|
|
||||||
{
|
/* Clear the high scores from the previous network game. */
|
||||||
uint i;
|
auto &highscores = _highscore_table[SP_MULTIPLAYER];
|
||||||
|
std::fill(highscores.begin(), highscores.end(), HighScore{});
|
||||||
|
|
||||||
memset(_highscore_table[SP_MULTIPLAYER], 0, sizeof(_highscore_table[SP_MULTIPLAYER]));
|
for (size_t i = 0; i < count && i < highscores.size(); i++) {
|
||||||
|
const Company *c = cl[i];
|
||||||
|
auto &highscore = highscores[i];
|
||||||
|
SetDParam(0, c->index);
|
||||||
|
SetDParam(1, c->index);
|
||||||
|
highscore.name = GetString(STR_HIGHSCORE_NAME); // get manager/company name string
|
||||||
|
highscore.score = c->old_economy[0].performance_history;
|
||||||
|
highscore.title = EndGameGetPerformanceTitleFromValue(highscore.score);
|
||||||
|
|
||||||
/* Copy over Top5 companies */
|
if (c->index == _local_company) local_company_place = static_cast<int8_t>(i);
|
||||||
for (i = 0; i < lengthof(_highscore_table[SP_MULTIPLAYER]) && i < count; i++) {
|
|
||||||
HighScore *hs = &_highscore_table[SP_MULTIPLAYER][i];
|
|
||||||
|
|
||||||
SetDParam(0, cl[i]->index);
|
|
||||||
SetDParam(1, cl[i]->index);
|
|
||||||
GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
|
|
||||||
hs->score = cl[i]->old_economy[0].performance_history;
|
|
||||||
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
|
|
||||||
|
|
||||||
/* get the ranking of the local company */
|
|
||||||
if (cl[i]->index == _local_company) company = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add top5 companies to highscore table */
|
return local_company_place;
|
||||||
return company;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Save HighScore table to file */
|
/** Save HighScore table to file */
|
||||||
void SaveToHighScore()
|
void SaveToHighScore()
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(_highscore_file.c_str(), "wb");
|
std::unique_ptr<FILE, FileDeleter> fp(fopen(_highscore_file.c_str(), "wb"));
|
||||||
|
if (fp == nullptr) return;
|
||||||
|
|
||||||
if (fp != nullptr) {
|
/* Does not iterate through the complete array!. */
|
||||||
uint i;
|
for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
|
||||||
HighScore *hs;
|
for (HighScore &hs : _highscore_table[i]) {
|
||||||
|
/* This code is weird and old fashioned to keep compatibility with the old high score files. */
|
||||||
for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
|
byte name_length = ClampTo<byte>(hs.name.size());
|
||||||
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
|
if (fwrite(&name_length, sizeof(name_length), 1, fp.get()) != 1 || // Write the string length of the name
|
||||||
/* First character is a command character, so strlen will fail on that */
|
fwrite(hs.name.data(), name_length, 1, fp.get()) > 1 || // Yes... could be 0 bytes too
|
||||||
byte length = ClampTo<byte>(std::min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : strlen(&hs->company[1]) + 1));
|
fwrite(&hs.score, sizeof(hs.score), 1, fp.get()) != 1 ||
|
||||||
|
fwrite(" ", 2, 1, fp.get()) != 1) { // Used to be hs.title, not saved anymore; compatibility
|
||||||
if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length
|
Debug(misc, 1, "Could not save highscore.");
|
||||||
fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
|
return;
|
||||||
fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
|
|
||||||
fwrite(" ", 2, 1, fp) != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
|
|
||||||
Debug(misc, 1, "Could not save highscore.");
|
|
||||||
i = SP_SAVED_HIGHSCORE_END;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initialize the highscore table to 0 and if any file exists, load in values */
|
/** Initialize the highscore table to 0 and if any file exists, load in values */
|
||||||
void LoadFromHighScore()
|
void LoadFromHighScore()
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(_highscore_file.c_str(), "rb");
|
std::fill(_highscore_table.begin(), _highscore_table.end(), HighScores{});
|
||||||
|
|
||||||
memset(_highscore_table, 0, sizeof(_highscore_table));
|
std::unique_ptr<FILE, FileDeleter> fp(fopen(_highscore_file.c_str(), "rb"));
|
||||||
|
if (fp == nullptr) return;
|
||||||
|
|
||||||
if (fp != nullptr) {
|
/* Does not iterate through the complete array!. */
|
||||||
uint i;
|
for (int i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
|
||||||
HighScore *hs;
|
for (HighScore &hs : _highscore_table[i]) {
|
||||||
|
/* This code is weird and old fashioned to keep compatibility with the old high score files. */
|
||||||
|
byte name_length;
|
||||||
|
char buffer[std::numeric_limits<decltype(name_length)>::max() + 1];
|
||||||
|
|
||||||
for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
|
if (fread(&name_length, sizeof(name_length), 1, fp.get()) != 1 ||
|
||||||
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
|
fread(buffer, name_length, 1, fp.get()) > 1 || // Yes... could be 0 bytes too
|
||||||
byte length;
|
fread(&hs.score, sizeof(hs.score), 1, fp.get()) != 1 ||
|
||||||
if (fread(&length, sizeof(length), 1, fp) != 1 ||
|
fseek(fp.get(), 2, SEEK_CUR) == -1) { // Used to be hs.title, not saved anymore; compatibility
|
||||||
fread(hs->company, std::min<int>(lengthof(hs->company), length), 1, fp) > 1 || // Yes... could be 0 bytes too
|
Debug(misc, 1, "Highscore corrupted");
|
||||||
fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
|
return;
|
||||||
fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
|
|
||||||
Debug(misc, 1, "Highscore corrupted");
|
|
||||||
i = SP_SAVED_HIGHSCORE_END;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
StrMakeValidInPlace(hs->company, lastof(hs->company), SVS_NONE);
|
|
||||||
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
|
|
||||||
}
|
}
|
||||||
|
hs.name = StrMakeValid(std::string_view(buffer, name_length));
|
||||||
|
hs.title = EndGameGetPerformanceTitleFromValue(hs.score);
|
||||||
}
|
}
|
||||||
fclose(fp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,23 +15,20 @@
|
||||||
#include "settings_type.h"
|
#include "settings_type.h"
|
||||||
|
|
||||||
struct HighScore {
|
struct HighScore {
|
||||||
/**
|
std::string name; ///< The name of the companyy and president.
|
||||||
* The name of the company and president.
|
StringID title = INVALID_STRING_ID; ///< NOSAVE, has troubles with changing string-numbers.
|
||||||
* The + 5 is for the comma and space or possibly other characters
|
uint16 score = 0; ///< The score for this high score. Do NOT change type, will break hs.dat
|
||||||
* that join the two names in this single string and the '\0'.
|
|
||||||
*/
|
|
||||||
char company[(MAX_LENGTH_COMPANY_NAME_CHARS + MAX_LENGTH_PRESIDENT_NAME_CHARS + 5) * MAX_CHAR_LENGTH];
|
|
||||||
StringID title; ///< NOSAVE, has troubles with changing string-numbers.
|
|
||||||
uint16 score; ///< The score for this high score. Do NOT change type, will break hs.dat
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern HighScore _highscore_table[SP_HIGHSCORE_END][5];
|
using HighScores = std::array<HighScore, 5>; ///< Record 5 high scores
|
||||||
|
using HighScoresTable = std::array<HighScores, SP_HIGHSCORE_END>; ///< Record high score for each of the difficulty levels
|
||||||
|
extern HighScoresTable _highscore_table;
|
||||||
|
|
||||||
void SaveToHighScore();
|
void SaveToHighScore();
|
||||||
void LoadFromHighScore();
|
void LoadFromHighScore();
|
||||||
int8 SaveHighScoreValue(const Company *c);
|
int8_t SaveHighScoreValue(const Company *c);
|
||||||
int8 SaveHighScoreValueNetwork();
|
int8_t SaveHighScoreValueNetwork();
|
||||||
StringID EndGameGetPerformanceTitleFromValue(uint value);
|
StringID EndGameGetPerformanceTitleFromValue(uint value);
|
||||||
void ShowHighscoreTable(int difficulty = SP_CUSTOM, int8 rank = -1);
|
void ShowHighscoreTable(int difficulty = SP_CUSTOM, int8_t rank = -1);
|
||||||
|
|
||||||
#endif /* HIGHSCORE_H */
|
#endif /* HIGHSCORE_H */
|
||||||
|
|
|
@ -184,7 +184,7 @@ struct HighScoreWindow : EndGameHighScoreBaseWindow {
|
||||||
|
|
||||||
void OnPaint() override
|
void OnPaint() override
|
||||||
{
|
{
|
||||||
const HighScore *hs = _highscore_table[this->window_number];
|
const auto &hs = _highscore_table[this->window_number];
|
||||||
|
|
||||||
this->SetupHighScoreEndWindow();
|
this->SetupHighScoreEndWindow();
|
||||||
Point pt = this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
|
Point pt = this->GetTopLeft(ScaleSpriteTrad(640), ScaleSpriteTrad(480));
|
||||||
|
@ -193,14 +193,14 @@ struct HighScoreWindow : EndGameHighScoreBaseWindow {
|
||||||
DrawStringMultiLine(pt.x + ScaleSpriteTrad(70), pt.x + ScaleSpriteTrad(570), pt.y, pt.y + ScaleSpriteTrad(140), !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
|
DrawStringMultiLine(pt.x + ScaleSpriteTrad(70), pt.x + ScaleSpriteTrad(570), pt.y, pt.y + ScaleSpriteTrad(140), !_networking ? STR_HIGHSCORE_TOP_COMPANIES_WHO_REACHED : STR_HIGHSCORE_TOP_COMPANIES_NETWORK_GAME, TC_FROMSTRING, SA_CENTER);
|
||||||
|
|
||||||
/* Draw Highscore peepz */
|
/* Draw Highscore peepz */
|
||||||
for (uint8 i = 0; i < lengthof(_highscore_table[0]); i++) {
|
for (uint8_t i = 0; i < ClampTo<uint8_t>(hs.size()); i++) {
|
||||||
SetDParam(0, i + 1);
|
SetDParam(0, i + 1);
|
||||||
DrawString(pt.x + ScaleSpriteTrad(40), pt.x + ScaleSpriteTrad(600), pt.y + ScaleSpriteTrad(140 + i * 55), STR_HIGHSCORE_POSITION);
|
DrawString(pt.x + ScaleSpriteTrad(40), pt.x + ScaleSpriteTrad(600), pt.y + ScaleSpriteTrad(140 + i * 55), STR_HIGHSCORE_POSITION);
|
||||||
|
|
||||||
if (hs[i].company[0] != '\0') {
|
if (!hs[i].name.empty()) {
|
||||||
TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
|
TextColour colour = (this->rank == i) ? TC_RED : TC_BLACK; // draw new highscore in red
|
||||||
|
|
||||||
SetDParamStr(0, hs[i].company);
|
SetDParamStr(0, hs[i].name);
|
||||||
DrawString(pt.x + ScaleSpriteTrad(71), pt.x + ScaleSpriteTrad(569), pt.y + ScaleSpriteTrad(140 + i * 55), STR_JUST_BIG_RAW_STRING, colour);
|
DrawString(pt.x + ScaleSpriteTrad(71), pt.x + ScaleSpriteTrad(569), pt.y + ScaleSpriteTrad(140 + i * 55), STR_JUST_BIG_RAW_STRING, colour);
|
||||||
SetDParam(0, hs[i].title);
|
SetDParam(0, hs[i].title);
|
||||||
SetDParam(1, hs[i].score);
|
SetDParam(1, hs[i].score);
|
||||||
|
|
Loading…
Reference in New Issue