moved almost all hardcoded constants to "define.dat"
[k8-i-v-a-n.git] / src / felib / hscore.cpp
blobcf7ee84059df8643f57b2c51413cd7b94125cade
1 /*
3 * Iter Vehemens ad Necem (IVAN)
4 * Copyright (C) Timo Kiviluoto
5 * Released under the GNU General
6 * Public License
8 * See LICENSING which should be included
9 * along with this file for more details
13 #include "hscore.h"
14 #include "fesave.h"
15 #include "felist.h"
16 #include "feio.h"
17 #include "femath.h"
20 highscore::highscore (cfestring &File) : LastAdd(0xFF), Version(HIGH_SCORE_VERSION) {
21 Load(File);
25 festring highscore::genFileName (const festring &fname) const {
26 if (fname[0] != '/') {
27 #ifdef LOCAL_SAVES
28 return inputfile::GetMyDir()+'/'+fname;
29 #else
30 festring nname;
31 nname << getenv("HOME") << '/' << fname;
32 return nname;
33 #endif
35 return 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);
52 LastAdd = c;
53 return true;
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);
62 return true;
64 LastAdd = MAX_HIGHSCORES;
65 return false;
69 void highscore::Draw () const {
70 if (Score.empty()) {
71 iosystem::TextScreen(CONST_S("There are no entries yet. Play a game to correct this."));
72 return;
74 if (GetVersion() != HIGH_SCORE_VERSION) {
75 iosystem::TextScreen(CONST_S("The highscore file is for an other version of IVAN."));
76 return;
78 felist List(CONST_S("Adventurers' Hall of Fame"));
79 festring Desc;
80 for (uInt c = 0; c < Score.size(); ++c) {
81 Desc.Empty();
82 Desc << c+1;
83 Desc.Resize(5, ' ');
84 Desc << Score[c];
85 Desc.Resize(13, ' ');
86 Desc << Entry[c];
87 List.AddEntry(Desc, c == uInt(LastAdd) ? WHITE : LIGHT_GRAY, 13);
89 List.SetFlags(FADE);
90 List.SetPageLength(26);
91 List.Draw();
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];
102 HighScore <<
103 uShort(HIGH_SCORE_VERSION)
104 << Score
105 << Entry
106 << Time
107 << RandomID
108 << LastAdd
109 << CheckSum;
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;
118 HighScore.Get();
119 if (HighScore.Eof()) return;
121 sLong cs1;
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])
154 return c+1;
156 return 0;
160 void highscore::Clear () {
161 Entry.clear();
162 Score.clear();
163 Time.clear();
164 RandomID.clear();
165 Version = HIGH_SCORE_VERSION;
166 LastAdd = 0xFF;