~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/highscore.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: highscore.cpp 15299 2009-01-31 20:16:06Z smatz $ */
 
2
 
 
3
/** @file highscore.cpp Definition of functions used for highscore handling */
 
4
 
 
5
#include "highscore.h"
 
6
#include "settings_type.h"
 
7
#include "company_base.h"
 
8
#include "company_func.h"
 
9
#include "cheat_func.h"
 
10
#include "string_func.h"
 
11
#include "strings_func.h"
 
12
#include "table/strings.h"
 
13
#include "core/sort_func.hpp"
 
14
#include "variables.h"
 
15
#include "debug.h"
 
16
 
 
17
HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
 
18
 
 
19
static const StringID _endgame_perf_titles[] = {
 
20
        STR_0213_BUSINESSMAN,
 
21
        STR_0213_BUSINESSMAN,
 
22
        STR_0213_BUSINESSMAN,
 
23
        STR_0213_BUSINESSMAN,
 
24
        STR_0213_BUSINESSMAN,
 
25
        STR_0214_ENTREPRENEUR,
 
26
        STR_0214_ENTREPRENEUR,
 
27
        STR_0215_INDUSTRIALIST,
 
28
        STR_0215_INDUSTRIALIST,
 
29
        STR_0216_CAPITALIST,
 
30
        STR_0216_CAPITALIST,
 
31
        STR_0217_MAGNATE,
 
32
        STR_0217_MAGNATE,
 
33
        STR_0218_MOGUL,
 
34
        STR_0218_MOGUL,
 
35
        STR_0219_TYCOON_OF_THE_CENTURY
 
36
};
 
37
 
 
38
StringID EndGameGetPerformanceTitleFromValue(uint value)
 
39
{
 
40
        value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
 
41
 
 
42
        return _endgame_perf_titles[value];
 
43
}
 
44
 
 
45
/** Save the highscore for the company */
 
46
int8 SaveHighScoreValue(const Company *c)
 
47
{
 
48
        HighScore *hs = _highscore_table[_settings_game.difficulty.diff_level];
 
49
        uint i;
 
50
        uint16 score = c->old_economy[0].performance_history;
 
51
 
 
52
        /* Exclude cheaters from the honour of being in the highscore table */
 
53
        if (CheatHasBeenUsed()) return -1;
 
54
 
 
55
        for (i = 0; i < lengthof(_highscore_table[0]); i++) {
 
56
                /* You are in the TOP5. Move all values one down and save us there */
 
57
                if (hs[i].score <= score) {
 
58
                        /* move all elements one down starting from the replaced one */
 
59
                        memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
 
60
                        SetDParam(0, c->index);
 
61
                        SetDParam(1, c->index);
 
62
                        GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
 
63
                        hs[i].score = score;
 
64
                        hs[i].title = EndGameGetPerformanceTitleFromValue(score);
 
65
                        return i;
 
66
                }
 
67
        }
 
68
 
 
69
        return -1; // too bad; we did not make it into the top5
 
70
}
 
71
 
 
72
/** Sort all companies given their performance */
 
73
static int CDECL HighScoreSorter(const Company * const *a, const Company * const *b)
 
74
{
 
75
        return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history;
 
76
}
 
77
 
 
78
/* Save the highscores in a network game when it has ended */
 
79
#define LAST_HS_ITEM lengthof(_highscore_table) - 1
 
80
int8 SaveHighScoreValueNetwork()
 
81
{
 
82
        const Company *c;
 
83
        const Company *cl[MAX_COMPANIES];
 
84
        uint count = 0;
 
85
        int8 company = -1;
 
86
 
 
87
        /* Sort all active companies with the highest score first */
 
88
        FOR_ALL_COMPANIES(c) cl[count++] = c;
 
89
 
 
90
        QSortT(cl, count, &HighScoreSorter);
 
91
 
 
92
        {
 
93
                uint i;
 
94
 
 
95
                memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
 
96
 
 
97
                /* Copy over Top5 companies */
 
98
                for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
 
99
                        HighScore *hs = &_highscore_table[LAST_HS_ITEM][i];
 
100
 
 
101
                        SetDParam(0, cl[i]->index);
 
102
                        SetDParam(1, cl[i]->index);
 
103
                        GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
 
104
                        hs->score = cl[i]->old_economy[0].performance_history;
 
105
                        hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 
106
 
 
107
                        /* get the ranking of the local company */
 
108
                        if (cl[i]->index == _local_company) company = i;
 
109
                }
 
110
        }
 
111
 
 
112
        /* Add top5 companys to highscore table */
 
113
        return company;
 
114
}
 
115
 
 
116
/** Save HighScore table to file */
 
117
void SaveToHighScore()
 
118
{
 
119
        FILE *fp = fopen(_highscore_file, "wb");
 
120
 
 
121
        if (fp != NULL) {
 
122
                uint i;
 
123
                HighScore *hs;
 
124
 
 
125
                for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
 
126
                        for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
 
127
                                /* First character is a command character, so strlen will fail on that */
 
128
                                byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1);
 
129
 
 
130
                                if (fwrite(&length, sizeof(length), 1, fp)       != 1 || // write away string length
 
131
                                                fwrite(hs->company, length, 1, fp)           >  1 || // Yes... could be 0 bytes too
 
132
                                                fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
 
133
                                                fwrite("  ", 2, 1, fp)                       != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
 
134
                                        DEBUG(misc, 1, "Could not save highscore.");
 
135
                                        i = LAST_HS_ITEM;
 
136
                                        break;
 
137
                                }
 
138
                        }
 
139
                }
 
140
                fclose(fp);
 
141
        }
 
142
}
 
143
 
 
144
/** Initialize the highscore table to 0 and if any file exists, load in values */
 
145
void LoadFromHighScore()
 
146
{
 
147
        FILE *fp = fopen(_highscore_file, "rb");
 
148
 
 
149
        memset(_highscore_table, 0, sizeof(_highscore_table));
 
150
 
 
151
        if (fp != NULL) {
 
152
                uint i;
 
153
                HighScore *hs;
 
154
 
 
155
                for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
 
156
                        for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
 
157
                                byte length;
 
158
                                if (fread(&length, sizeof(length), 1, fp)       !=  1 ||
 
159
                                                fread(hs->company, 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) { // XXX - placeholder for hs->title, not saved anymore; compatibility
 
162
                                        DEBUG(misc, 1, "Highscore corrupted");
 
163
                                        i = LAST_HS_ITEM;
 
164
                                        break;
 
165
                                }
 
166
                                *lastof(hs->company) = '\0';
 
167
                                hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
 
168
                        }
 
169
                }
 
170
                fclose(fp);
 
171
        }
 
172
}