Add support for wheel mouse
[hex-a-hop.git] / savestate.h
blob0851f41dfe7976234499683960d882b46e92af0b
1 /*
2 Copyright (C) 2005-2007 Tom Beaumont
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
20 struct HexPuzzle;
22 class LevelSave
24 friend struct HexPuzzle;
26 char * bestSolution;
27 int bestSolutionLength;
28 int bestScore;
29 #define NUM_LAST_SCORES 19
30 int lastScores[NUM_LAST_SCORES];
31 int unlocked;
32 public:
33 LevelSave()
35 Clear();
37 void Clear()
39 unlocked = 0;
40 bestSolution = 0;
41 bestScore = 0;
42 bestSolutionLength = 0;
43 memset(lastScores, 0, sizeof(lastScores));
45 void LoadSave(FILE* f, bool save)
47 typedef unsigned int _fn(void*, unsigned int, unsigned int, FILE*);
48 _fn * fn = save ? (_fn*)fwrite : (_fn*)fread;
50 fn(&bestSolutionLength, sizeof(bestSolutionLength), 1, f);
51 fn(&bestScore, sizeof(bestScore), 1, f);
52 fn(&lastScores, sizeof(lastScores), 1, f);
53 fn(&unlocked, sizeof(unlocked), 1, f);
55 if (bestSolutionLength)
57 if (!save) SetSolution(bestSolutionLength);
58 fn(bestSolution, sizeof(bestSolution[0]), bestSolutionLength, f);
62 void Dump()
64 for (int j=1; j<NUM_LAST_SCORES; j++)
65 if (lastScores[j]==lastScores[0])
66 lastScores[j] = 0;
68 /* for (int i=0; i<NUM_LAST_SCORES && lastScores[i]; i++)
69 if (lastScores[i] != bestScore)
70 printf("\t% 8d\n", lastScores[i]);*/
72 bool Completed()
74 return bestScore != 0;
76 bool IsNewCompletionBetter(int score)
78 for (int i=0; i<NUM_LAST_SCORES; i++)
80 if (lastScores[i]==0)
81 lastScores[i] = score;
82 if (lastScores[i]==score)
83 break;
86 if (!Completed())
87 return true;
89 return score <= bestScore;
91 bool PassesPar(int par)
93 if (!Completed())
94 return false;
95 return bestScore <= par;
97 int GetScore()
99 return bestScore;
101 void SetScore(int s)
103 bestScore = s;
105 void SetSolution(int l) {
106 delete [] bestSolution;
107 bestSolutionLength = l;
108 bestSolution = new char [ l ];
110 void SetSolutionStep(int pos, int val)
112 bestSolution[pos] = val;
116 class SaveState
118 struct X : public LevelSave
120 X* next;
121 char* name;
123 X(const char* n, X* nx=0) : next(nx)
125 name = new char[strlen(n)+1];
126 strcpy(name, n);
128 ~X()
130 delete [] name;
134 struct General {
135 int scoringOn;
136 int hintFlags;
137 int completionPercentage;
138 int endSequence;
139 int masteredPercentage;
140 int pad[6];
143 X* first;
145 void ClearRaw()
147 memset(&general, 0, sizeof(general));
148 general.hintFlags = 1<<31 | 1<<30;
150 X* x=first;
151 while (x)
153 X* nx = x->next;
154 delete x;
155 x = nx;
157 first = 0;
161 public:
163 General general;
166 SaveState() : first(0)
168 Clear();
171 ~SaveState()
173 ClearRaw();
176 void Clear()
178 ClearRaw();
179 ApplyStuff();
182 void GetStuff();
183 void ApplyStuff();
185 void LoadSave(FILE* f, bool save)
187 if (save)
189 GetStuff();
191 //printf("----\n");
193 fputc('2', f);
194 fwrite(&general, sizeof(general), 1, f);
195 for(X* x=first; x; x=x->next)
197 short len = strlen(x->name);
198 fwrite(&len, sizeof(len), 1, f);
199 fwrite(x->name, len, 1, f);
201 x->LoadSave(f,save);
203 if (x->Completed())
205 //printf("% 8d %s\n", x->GetScore(), x->name);
206 x->Dump();
210 else
212 ClearRaw();
213 int v = fgetc(f);
214 if (v=='2')
216 fread(&general, sizeof(general), 1, f);
217 v = '1';
219 if (v=='1')
221 while(!feof(f))
223 char temp[1000];
224 short len;
225 fread(&len, sizeof(len), 1, f);
226 if (feof(f)) break;
227 fread(temp, len, 1, f);
228 temp[len] = 0;
229 first = new X(temp, first);
231 first->LoadSave(f,save);
235 ApplyStuff();
239 LevelSave* GetLevel(const char * name, bool create)
241 char * l = strstr(name, "Levels");
242 if (l)
243 name = l;
245 X* x = first;
246 if (x && strcmp(name, x->name)==0) return x;
247 while (x && x->next)
249 if (strcmp(name, x->next->name)==0) return x->next;
250 x = x->next;
252 if (create)
254 X* n = new X(name);
255 if (x)
256 x->next = n;
257 else
258 first = n;
259 return n;
261 else
263 return 0;