3 * Iter Vehemens ad Necem (IVAN)
4 * Copyright (C) Timo Kiviluoto
5 * Released under the GNU General
8 * See LICENSING which should be included
9 * along with this file for more details
20 highscore::highscore (cfestring
&File
) : LastAdd(0xFF), Version(HIGH_SCORE_VERSION
) {
25 festring
highscore::genFileName (const festring
&fname
) const {
26 if (fname
[0] != '/') {
28 return inputfile::GetMyDir()+'/'+fname
;
31 nname
<< getenv("HOME") << '/' << fname
;
39 truth
highscore::Add (sLong NewScore
, cfestring
&NewEntry
, time_t NewTime
, sLong NewRandomID
) {
40 for (uInt c
= 0; c
< Score
.size(); ++c
) {
41 if (Score
[c
] < NewScore
) {
42 Entry
.insert(Entry
.begin()+c
, NewEntry
);
43 Score
.insert(Score
.begin()+c
, NewScore
);
44 Time
.insert(Time
.begin()+c
, NewTime
);
45 RandomID
.insert(RandomID
.begin()+c
, NewRandomID
);
46 if (Score
.size() > MAX_HIGHSCORES
) {
47 Entry
.resize(MAX_HIGHSCORES
, festring());
48 Score
.resize(MAX_HIGHSCORES
);
49 Time
.resize(MAX_HIGHSCORES
);
50 RandomID
.resize(MAX_HIGHSCORES
);
56 if (Score
.size() < MAX_HIGHSCORES
) {
57 LastAdd
= Score
.size();
58 Entry
.push_back(NewEntry
);
59 Score
.push_back(NewScore
);
60 Time
.push_back(NewTime
);
61 RandomID
.push_back(NewRandomID
);
64 LastAdd
= MAX_HIGHSCORES
;
69 void highscore::Draw () const {
71 iosystem::TextScreen(CONST_S("There are no entries yet. Play a game to correct this."));
74 if (GetVersion() != HIGH_SCORE_VERSION
) {
75 iosystem::TextScreen(CONST_S("The highscore file is for an other version of IVAN."));
78 felist
List(CONST_S("Adventurers' Hall of Fame"));
80 for (uInt c
= 0; c
< Score
.size(); ++c
) {
87 List
.AddEntry(Desc
, c
== uInt(LastAdd
) ? WHITE
: LIGHT_GRAY
, 13);
90 List
.SetPageLength(26);
95 void highscore::Save (cfestring
&File
) const {
96 outputfile
HighScore(genFileName(File
), true);
97 if (!HighScore
.IsOpen()) return;
98 sLong CheckSum
= HIGH_SCORE_VERSION
+LastAdd
;
99 for (uShort c
= 0; c
< Score
.size(); ++c
) {
100 CheckSum
+= Score
[c
]+Entry
[c
].GetCheckSum()+RandomID
[c
];
103 uShort(HIGH_SCORE_VERSION
)
113 /* This function needs much more error handling */
114 void highscore::Load (cfestring
&File
) {
116 inputfile
HighScore(genFileName(File
), false);
117 if (!HighScore
.IsOpen()) return;
119 if (HighScore
.Eof()) return;
122 inputfile
HighScore(genFileName(File
), false);
123 HighScore
>> Version
;
124 HighScore
>> Score
>> Entry
>> Time
>> RandomID
>> LastAdd
>> cs1
;
125 sLong CheckSum
= HIGH_SCORE_VERSION
+LastAdd
;
126 for (uShort c
= 0; c
< Score
.size(); ++c
) {
127 CheckSum
+= Score
[c
]+Entry
[c
].GetCheckSum()+RandomID
[c
];
129 if (cs1
!= CheckSum
) Clear();
133 truth
highscore::MergeToFile (highscore
*To
) const {
134 truth MergedSomething
= false;
135 for (uInt c
= 0; c
< Score
.size(); ++c
) {
136 if (!To
->Find(Score
[c
], Entry
[c
], Time
[c
], RandomID
[c
])) {
137 To
->Add(Score
[c
], Entry
[c
], Time
[c
], RandomID
[c
]);
138 MergedSomething
= true;
141 return MergedSomething
;
145 truth
highscore::Add (sLong NewScore
, cfestring
&NewEntry
) {
146 return Add(NewScore
, NewEntry
, time(0), RAND());
150 /* Because of major stupidity this return the number of NEXT from the right entry, 0 = not found */
151 int highscore::Find (sLong AScore
, cfestring
&AEntry
, time_t ATime
, sLong ARandomID
) {
152 for (uInt c
= 0; c
< Score
.size(); ++c
) {
153 if (AScore
== Score
[c
] && Entry
[c
] == AEntry
&& ATime
== Time
[c
] && ARandomID
== RandomID
[c
])
160 void highscore::Clear () {
165 Version
= HIGH_SCORE_VERSION
;