OpenTTD
highscore.cpp
Go to the documentation of this file.
1 /* $Id: highscore.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
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 "core/sort_func.hpp"
21 #include "debug.h"
22 
23 #include "safeguards.h"
24 
27 
28 static const StringID _endgame_perf_titles[] = {
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_BUSINESSMAN,
34  STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
35  STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
36  STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
37  STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
38  STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
39  STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
40  STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
41  STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
42  STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
43  STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
44  STR_HIGHSCORE_PERFORMANCE_TITLE_TYCOON_OF_THE_CENTURY
45 };
46 
47 StringID EndGameGetPerformanceTitleFromValue(uint value)
48 {
49  value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
50 
51  return _endgame_perf_titles[value];
52 }
53 
56 {
57  HighScore *hs = _highscore_table[SP_CUSTOM];
58  uint i;
59  uint16 score = c->old_economy[0].performance_history;
60 
61  /* Exclude cheaters from the honour of being in the highscore table */
62  if (CheatHasBeenUsed()) return -1;
63 
64  for (i = 0; i < lengthof(_highscore_table[0]); i++) {
65  /* You are in the TOP5. Move all values one down and save us there */
66  if (hs[i].score <= score) {
67  /* move all elements one down starting from the replaced one */
68  memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
69  SetDParam(0, c->index);
70  SetDParam(1, c->index);
71  GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
72  hs[i].score = score;
73  hs[i].title = EndGameGetPerformanceTitleFromValue(score);
74  return i;
75  }
76  }
77 
78  return -1; // too bad; we did not make it into the top5
79 }
80 
82 static int CDECL HighScoreSorter(const Company * const *a, const Company * const *b)
83 {
84  return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
85 }
86 
92 {
93  const Company *c;
94  const Company *cl[MAX_COMPANIES];
95  uint count = 0;
96  int8 company = -1;
97 
98  /* Sort all active companies with the highest score first */
99  FOR_ALL_COMPANIES(c) cl[count++] = c;
100 
101  QSortT(cl, count, &HighScoreSorter);
102 
103  {
104  uint i;
105 
106  memset(_highscore_table[SP_MULTIPLAYER], 0, sizeof(_highscore_table[SP_MULTIPLAYER]));
107 
108  /* Copy over Top5 companies */
109  for (i = 0; i < lengthof(_highscore_table[SP_MULTIPLAYER]) && i < count; i++) {
110  HighScore *hs = &_highscore_table[SP_MULTIPLAYER][i];
111 
112  SetDParam(0, cl[i]->index);
113  SetDParam(1, cl[i]->index);
114  GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
115  hs->score = cl[i]->old_economy[0].performance_history;
116  hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
117 
118  /* get the ranking of the local company */
119  if (cl[i]->index == _local_company) company = i;
120  }
121  }
122 
123  /* Add top5 companies to highscore table */
124  return company;
125 }
126 
129 {
130  FILE *fp = fopen(_highscore_file, "wb");
131 
132  if (fp != NULL) {
133  uint i;
134  HighScore *hs;
135 
136  for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
137  for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
138  /* First character is a command character, so strlen will fail on that */
139  byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
140 
141  if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length
142  fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
143  fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
144  fwrite(" ", 2, 1, fp) != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
145  DEBUG(misc, 1, "Could not save highscore.");
147  break;
148  }
149  }
150  }
151  fclose(fp);
152  }
153 }
154 
157 {
158  FILE *fp = fopen(_highscore_file, "rb");
159 
160  memset(_highscore_table, 0, sizeof(_highscore_table));
161 
162  if (fp != NULL) {
163  uint i;
164  HighScore *hs;
165 
166  for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
167  for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
168  byte length;
169  if (fread(&length, sizeof(length), 1, fp) != 1 ||
170  fread(hs->company, min<int>(lengthof(hs->company), length), 1, fp) > 1 || // Yes... could be 0 bytes too
171  fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
172  fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
173  DEBUG(misc, 1, "Highscore corrupted");
175  break;
176  }
178  hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
179  }
180  }
181  fclose(fp);
182  }
183 }
Functions related to OTTD&#39;s strings.
int8 SaveHighScoreValueNetwork()
Save the highscores in a network game when it has ended.
Definition: highscore.cpp:91
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:25
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:49
Special "multiplayer" highscore. Not saved, always specific to the current game.
Definition: settings_type.h:38
CompanyByte _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
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:184
char * _highscore_file
The file to store the highscore data in.
Definition: highscore.cpp:26
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
Maximum number of companies.
Definition: company_type.h:25
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
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:55
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
Functions related to sorting operations.
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition: company_base.h:98
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:427
static int CDECL HighScoreSorter(const Company *const *a, const Company *const *b)
Sort all companies given their performance.
Definition: highscore.cpp:82
End of highscore tables.
Definition: settings_type.h:39
void SaveToHighScore()
Save HighScore table to file.
Definition: highscore.cpp:128
static void QSortT(T *base, uint num, int(CDECL *comparator)(const T *, const T *), bool desc=false)
Type safe qsort()
Definition: sort_func.hpp:28
void LoadFromHighScore()
Initialize the highscore table to 0 and if any file exists, load in values.
Definition: highscore.cpp:156
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