1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-13 17:49:10 +00:00
Files
bin
docs
media
os
projects
src
3rdparty
ai
blitter
core
depend
lang
misc
music
network
os
pathfinder
saveload
script
sound
spriteloader
strgen
table
thread
video
widgets
aircraft.h
aircraft_cmd.cpp
aircraft_gui.cpp
airport.cpp
airport.h
airport_gui.cpp
animated_tile.cpp
animated_tile_func.h
articulated_vehicles.cpp
articulated_vehicles.h
autoreplace.cpp
autoreplace_base.h
autoreplace_cmd.cpp
autoreplace_func.h
autoreplace_gui.cpp
autoreplace_gui.h
autoreplace_type.h
autoslope.h
base_media_base.h
base_media_func.h
base_station_base.h
bmp.cpp
bmp.h
bridge.h
bridge_gui.cpp
bridge_map.cpp
bridge_map.h
build_vehicle_gui.cpp
cargo_type.h
cargopacket.cpp
cargopacket.h
cargotype.cpp
cargotype.h
cheat.cpp
cheat_func.h
cheat_gui.cpp
cheat_type.h
clear_cmd.cpp
clear_func.h
clear_map.h
cmd_helper.h
command.cpp
command_func.h
command_type.h
company_base.h
company_cmd.cpp
company_func.h
company_gui.cpp
company_gui.h
company_manager_face.h
company_type.h
console.cpp
console_cmds.cpp
console_func.h
console_gui.cpp
console_gui.h
console_internal.h
console_type.h
crashlog.cpp
crashlog.h
currency.cpp
currency.h
date.cpp
date_func.h
date_gui.cpp
date_gui.h
date_type.h
debug.cpp
debug.h
dedicated.cpp
depot.cpp
depot_base.h
depot_cmd.cpp
depot_func.h
depot_gui.cpp
depot_map.h
depot_type.h
direction_func.h
direction_type.h
disaster_cmd.cpp
dock_gui.cpp
driver.cpp
driver.h
economy.cpp
economy_base.h
economy_func.h
economy_type.h
effectvehicle.cpp
effectvehicle_base.h
effectvehicle_func.h
elrail.cpp
elrail_func.h
endian_check.cpp
engine.cpp
engine_base.h
engine_func.h
engine_gui.cpp
engine_gui.h
engine_type.h
fileio.cpp
fileio_func.h
fileio_type.h
fios.cpp
fios.h
fios_gui.cpp
fontcache.cpp
fontcache.h
gamelog.cpp
gamelog.h
gamelog_internal.h
genworld.cpp
genworld.h
genworld_gui.cpp
gfx.cpp
gfx_func.h
gfx_type.h
gfxinit.cpp
gfxinit.h
graph_gui.cpp
graph_gui.h
ground_vehicle.cpp
ground_vehicle.hpp
group.h
group_cmd.cpp
group_gui.cpp
group_gui.h
group_type.h
gui.h
heightmap.cpp
heightmap.h
highscore.cpp
highscore.h
highscore_gui.cpp
hotkeys.cpp
hotkeys.h
house.h
house_type.h
industry.h
industry_cmd.cpp
industry_gui.cpp
industry_map.h
industry_type.h
industrytype.h
ini.cpp
ini_type.h
intro_gui.cpp
landscape.cpp
landscape.h
landscape_type.h
language.h
livery.h
main_gui.cpp
map.cpp
map_func.h
map_type.h
misc.cpp
misc_cmd.cpp
misc_gui.cpp
mixer.cpp
mixer.h
music.cpp
music_gui.cpp
newgrf.cpp
newgrf.h
newgrf_airport.cpp
newgrf_airport.h
newgrf_airporttiles.cpp
newgrf_airporttiles.h
newgrf_animation_base.h
newgrf_animation_type.h
newgrf_callbacks.h
newgrf_canal.cpp
newgrf_canal.h
newgrf_cargo.cpp
newgrf_cargo.h
newgrf_class.h
newgrf_class_func.h
newgrf_commons.cpp
newgrf_commons.h
newgrf_config.cpp
newgrf_config.h
newgrf_debug.h
newgrf_debug_gui.cpp
newgrf_engine.cpp
newgrf_engine.h
newgrf_generic.cpp
newgrf_generic.h
newgrf_gui.cpp
newgrf_house.cpp
newgrf_house.h
newgrf_industries.cpp
newgrf_industries.h
newgrf_industrytiles.cpp
newgrf_industrytiles.h
newgrf_object.cpp
newgrf_object.h
newgrf_properties.h
newgrf_railtype.cpp
newgrf_railtype.h
newgrf_sound.cpp
newgrf_sound.h
newgrf_spritegroup.cpp
newgrf_spritegroup.h
newgrf_station.cpp
newgrf_station.h
newgrf_storage.cpp
newgrf_storage.h
newgrf_text.cpp
newgrf_text.h
newgrf_town.cpp
newgrf_town.h
newgrf_townname.cpp
newgrf_townname.h
news_func.h
news_gui.cpp
news_gui.h
news_type.h
object.h
object_base.h
object_cmd.cpp
object_gui.cpp
object_map.h
object_type.h
openttd.cpp
openttd.h
order_backup.cpp
order_backup.h
order_base.h
order_cmd.cpp
order_func.h
order_gui.cpp
order_type.h
os_timer.cpp
osk_gui.cpp
pbs.cpp
pbs.h
querystring_gui.h
rail.cpp
rail.h
rail_cmd.cpp
rail_gui.cpp
rail_gui.h
rail_map.h
rail_type.h
rev.cpp.in
rev.h
road.cpp
road_cmd.cpp
road_cmd.h
road_func.h
road_gui.cpp
road_gui.h
road_internal.h
road_map.cpp
road_map.h
road_type.h
roadstop.cpp
roadstop_base.h
roadveh.h
roadveh_cmd.cpp
roadveh_gui.cpp
screenshot.cpp
screenshot.h
sdl.cpp
sdl.h
settings.cpp
settings_func.h
settings_gui.cpp
settings_internal.h
settings_type.h
ship.h
ship_cmd.cpp
ship_gui.cpp
signal.cpp
signal_func.h
signal_type.h
signs.cpp
signs_base.h
signs_cmd.cpp
signs_func.h
signs_gui.cpp
signs_type.h
slope_func.h
slope_type.h
smallmap_gui.cpp
smallmap_gui.h
sortlist_type.h
sound.cpp
sound_func.h
sound_type.h
sprite.cpp
sprite.h
spritecache.cpp
spritecache.h
station.cpp
station_base.h
station_cmd.cpp
station_func.h
station_gui.cpp
station_gui.h
station_map.h
station_type.h
statusbar_gui.cpp
statusbar_gui.h
stdafx.h
string.cpp
string_func.h
string_type.h
strings.cpp
strings_func.h
strings_type.h
subsidy.cpp
subsidy_base.h
subsidy_func.h
subsidy_gui.cpp
subsidy_type.h
tar_type.h
terraform_cmd.cpp
terraform_gui.cpp
terraform_gui.h
textbuf_gui.h
texteff.cpp
texteff.hpp
tgp.cpp
tgp.h
tile_cmd.h
tile_map.cpp
tile_map.h
tile_type.h
tilearea.cpp
tilearea_type.h
tilehighlight_func.h
tilehighlight_type.h
timetable.h
timetable_cmd.cpp
timetable_gui.cpp
toolbar_gui.cpp
toolbar_gui.h
town.h
town_cmd.cpp
town_gui.cpp
town_map.h
town_type.h
townname.cpp
townname_func.h
townname_type.h
track_func.h
track_type.h
train.h
train_cmd.cpp
train_gui.cpp
transparency.h
transparency_gui.cpp
transparency_gui.h
transport_type.h
tree_cmd.cpp
tree_gui.cpp
tree_map.h
tunnel_map.cpp
tunnel_map.h
tunnelbridge.h
tunnelbridge_cmd.cpp
tunnelbridge_map.h
vehicle.cpp
vehicle_base.h
vehicle_cmd.cpp
vehicle_func.h
vehicle_gui.cpp
vehicle_gui.h
vehicle_gui_base.h
vehicle_type.h
vehiclelist.cpp
vehiclelist.h
viewport.cpp
viewport_func.h
viewport_gui.cpp
viewport_type.h
void_cmd.cpp
void_map.h
water.h
water_cmd.cpp
water_map.h
waypoint.cpp
waypoint_base.h
waypoint_cmd.cpp
waypoint_func.h
waypoint_gui.cpp
widget.cpp
widget_type.h
window.cpp
window_func.h
window_gui.h
window_type.h
zoom_func.h
zoom_type.h
.gitignore
.hgignore
COPYING
Doxyfile
Makefile.bundle.in
Makefile.grf.in
Makefile.in
Makefile.lang.in
Makefile.msvc
Makefile.src.in
changelog.txt
config.lib
configure
findversion.sh
known-bugs.txt
readme.txt
source.list
OpenTTD/src/highscore.cpp

180 lines
6.1 KiB
C++

/* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file highscore.cpp Definition of functions used for highscore handling */
#include "stdafx.h"
#include "highscore.h"
#include "company_base.h"
#include "company_func.h"
#include "cheat_func.h"
#include "string_func.h"
#include "strings_func.h"
#include "table/strings.h"
#include "core/sort_func.hpp"
#include "debug.h"
HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
char *_highscore_file; ///< The file to store the highscore data in.
static const StringID _endgame_perf_titles[] = {
STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN,
STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR,
STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
STR_HIGHSCORE_PERFORMANCE_TITLE_INDUSTRIALIST,
STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
STR_HIGHSCORE_PERFORMANCE_TITLE_CAPITALIST,
STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE,
STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL,
STR_HIGHSCORE_PERFORMANCE_TITLE_TYCOON_OF_THE_CENTURY
};
StringID EndGameGetPerformanceTitleFromValue(uint value)
{
value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
return _endgame_perf_titles[value];
}
/** Save the highscore for the company */
int8 SaveHighScoreValue(const Company *c)
{
HighScore *hs = _highscore_table[_settings_game.difficulty.diff_level];
uint i;
uint16 score = c->old_economy[0].performance_history;
/* Exclude cheaters from the honour of being in the highscore table */
if (CheatHasBeenUsed()) return -1;
for (i = 0; i < lengthof(_highscore_table[0]); i++) {
/* You are in the TOP5. Move all values one down and save us there */
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
}
/** Sort all companies given their performance */
static int CDECL HighScoreSorter(const Company * const *a, const Company * const *b)
{
return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
}
/* Save the highscores in a network game when it has ended */
#define LAST_HS_ITEM lengthof(_highscore_table) - 1
int8 SaveHighScoreValueNetwork()
{
const Company *c;
const Company *cl[MAX_COMPANIES];
uint count = 0;
int8 company = -1;
/* Sort all active companies with the highest score first */
FOR_ALL_COMPANIES(c) cl[count++] = c;
QSortT(cl, count, &HighScoreSorter);
{
uint i;
memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
/* Copy over Top5 companies */
for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
HighScore *hs = &_highscore_table[LAST_HS_ITEM][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 company;
}
/** Save HighScore table to file */
void SaveToHighScore()
{
FILE *fp = fopen(_highscore_file, "wb");
if (fp != NULL) {
uint i;
HighScore *hs;
for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
/* First character is a command character, so strlen will fail on that */
byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length
fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
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 = LAST_HS_ITEM;
break;
}
}
}
fclose(fp);
}
}
/** Initialize the highscore table to 0 and if any file exists, load in values */
void LoadFromHighScore()
{
FILE *fp = fopen(_highscore_file, "rb");
memset(_highscore_table, 0, sizeof(_highscore_table));
if (fp != NULL) {
uint i;
HighScore *hs;
for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
byte length;
if (fread(&length, sizeof(length), 1, fp) != 1 ||
fread(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too
fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
DEBUG(misc, 1, "Highscore corrupted");
i = LAST_HS_ITEM;
break;
}
*lastof(hs->company) = '\0';
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
}
}
fclose(fp);
}
}