OpenTTD
highscore.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #include "stdafx.h"
13 #include "highscore.h"
14 #include "company_base.h"
15 #include "company_func.h"
16 #include "cheat_func.h"
17 #include "string_func.h"
18 #include "strings_func.h"
19 #include "table/strings.h"
20 #include "debug.h"
21 
22 #include "safeguards.h"
23 
26 
27 static 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 
46 StringID EndGameGetPerformanceTitleFromValue(uint value)
47 {
48  value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
49 
50  return _endgame_perf_titles[value];
51 }
52 
55 {
56  HighScore *hs = _highscore_table[SP_CUSTOM];
57  uint i;
58  uint16 score = c->old_economy[0].performance_history;
59 
60  /* Exclude cheaters from the honour of being in the highscore table */
61  if (CheatHasBeenUsed()) return -1;
62 
63  for (i = 0; i < lengthof(_highscore_table[0]); i++) {
64  /* You are in the TOP5. Move all values one down and save us there */
65  if (hs[i].score <= score) {
66  /* move all elements one down starting from the replaced one */
67  memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
68  SetDParam(0, c->index);
69  SetDParam(1, c->index);
70  GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
71  hs[i].score = score;
72  hs[i].title = EndGameGetPerformanceTitleFromValue(score);
73  return i;
74  }
75  }
76 
77  return -1; // too bad; we did not make it into the top5
78 }
79 
81 static bool HighScoreSorter(const Company * const &a, const Company * const &b)
82 {
84 }
85 
91 {
92  const Company *c;
93  const Company *cl[MAX_COMPANIES];
94  uint count = 0;
95  int8 company = -1;
96 
97  /* Sort all active companies with the highest score first */
98  FOR_ALL_COMPANIES(c) cl[count++] = c;
99 
100  std::sort(std::begin(cl), std::begin(cl) + count, HighScoreSorter);
101 
102  {
103  uint i;
104 
105  memset(_highscore_table[SP_MULTIPLAYER], 0, sizeof(_highscore_table[SP_MULTIPLAYER]));
106 
107  /* Copy over Top5 companies */
108  for (i = 0; i < lengthof(_highscore_table[SP_MULTIPLAYER]) && i < count; i++) {
109  HighScore *hs = &_highscore_table[SP_MULTIPLAYER][i];
110 
111  SetDParam(0, cl[i]->index);
112  SetDParam(1, cl[i]->index);
113  GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
114  hs->score = cl[i]->old_economy[0].performance_history;
115  hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
116 
117  /* get the ranking of the local company */
118  if (cl[i]->index == _local_company) company = i;
119  }
120  }
121 
122  /* Add top5 companies to highscore table */
123  return company;
124 }
125 
128 {
129  FILE *fp = fopen(_highscore_file, "wb");
130 
131  if (fp != nullptr) {
132  uint i;
133  HighScore *hs;
134 
135  for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
136  for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
137  /* First character is a command character, so strlen will fail on that */
138  byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
139 
140  if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length
141  fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
142  fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
143  fwrite(" ", 2, 1, fp) != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
144  DEBUG(misc, 1, "Could not save highscore.");
146  break;
147  }
148  }
149  }
150  fclose(fp);
151  }
152 }
153 
156 {
157  FILE *fp = fopen(_highscore_file, "rb");
158 
159  memset(_highscore_table, 0, sizeof(_highscore_table));
160 
161  if (fp != nullptr) {
162  uint i;
163  HighScore *hs;
164 
165  for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
166  for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
167  byte length;
168  if (fread(&length, sizeof(length), 1, fp) != 1 ||
169  fread(hs->company, min<int>(lengthof(hs->company), length), 1, fp) > 1 || // Yes... could be 0 bytes too
170  fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
171  fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
172  DEBUG(misc, 1, "Highscore corrupted");
174  break;
175  }
177  hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
178  }
179  }
180  fclose(fp);
181  }
182 }
Functions related to OTTD&#39;s strings.
int8 SaveHighScoreValueNetwork()
Save the highscores in a network game when it has ended.
Definition: highscore.cpp:90
Definition of stuff that is very close to a company, like the company struct itself.
static uint minu(const uint a, const uint b)
Returns the minimum of two unsigned integers.
Definition: math_func.hpp:70
bool CheatHasBeenUsed()
Return true if any cheat has been used, false otherwise.
Definition: cheat.cpp:30
int32 performance_history
Company score (scale 0-1000)
Definition: company_base.h:27
Functions related to debugging.
StringID title
NOSAVE, has troubles with changing string-numbers.
Definition: highscore.h:26
HighScore _highscore_table[SP_HIGHSCORE_END][5]
various difficulty-settings; top 5
Definition: highscore.cpp:24
static bool HighScoreSorter(const Company *const &a, const Company *const &b)
Sort all companies given their performance.
Definition: highscore.cpp:81
No profile, special "custom" highscore.
Definition: settings_type.h:35
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
Allow nothing and replace nothing.
Definition: string_type.h:51
Special "multiplayer" highscore. Not saved, always specific to the current game.
Definition: settings_type.h:38
Functions related to low-level strings.
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:196
char * _highscore_file
The file to store the highscore data in.
Definition: highscore.cpp:25
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
char company[(MAX_LENGTH_COMPANY_NAME_CHARS+MAX_LENGTH_PRESIDENT_NAME_CHARS+5) *MAX_CHAR_LENGTH]
The name of the company and president.
Definition: highscore.h:25
Functions related to cheating.
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
uint16 score
The score for this high score. Do NOT change type, will break hs.dat.
Definition: highscore.h:27
Functions related to companies.
int8 SaveHighScoreValue(const Company *c)
Save the highscore for the company.
Definition: highscore.cpp:54
Declaration of functions and types defined in highscore.h and highscore_gui.h.
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
End of saved highscore tables.
Definition: settings_type.h:36
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition: company_base.h:99
Maximum number of companies.
Definition: company_type.h:25
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
End of highscore tables.
Definition: settings_type.h:39
void SaveToHighScore()
Save HighScore table to file.
Definition: highscore.cpp:127
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
void LoadFromHighScore()
Initialize the highscore table to 0 and if any file exists, load in values.
Definition: highscore.cpp:155
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201