Patch the game to show better than par punctuation
[hex-a-hop.git] / savestate.h
bloba49c37d32bc3e20c4544de8251cd98b8f6b2ffef
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
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 BeatsPar(int par)
93 if (!Completed())
94 return false;
95 return bestScore < par;
97 bool PassesPar(int par)
99 if (!Completed())
100 return false;
101 return bestScore <= par;
103 int GetScore()
105 return bestScore;
107 void SetScore(int s)
109 bestScore = s;
111 void SetSolution(int l) {
112 delete [] bestSolution;
113 bestSolutionLength = l;
114 bestSolution = new char [ l ];
116 void SetSolutionStep(int pos, int val)
118 bestSolution[pos] = val;
122 class SaveState
124 struct X : public LevelSave
126 X* next;
127 char* name;
129 X(const char* n, X* nx=0) : next(nx)
131 name = new char[strlen(n)+1];
132 strcpy(name, n);
134 ~X()
136 delete [] name;
140 struct General {
141 int scoringOn;
142 int hintFlags;
143 int completionPercentage;
144 int endSequence;
145 int masteredPercentage;
146 int pad[6];
149 X* first;
151 void ClearRaw()
153 memset(&general, 0, sizeof(general));
154 general.hintFlags = 1<<31 | 1<<30;
156 X* x=first;
157 while (x)
159 X* nx = x->next;
160 delete x;
161 x = nx;
163 first = 0;
167 public:
169 General general;
172 SaveState() : first(0)
174 Clear();
177 ~SaveState()
179 ClearRaw();
182 void Clear()
184 ClearRaw();
185 ApplyStuff();
188 void GetStuff();
189 void ApplyStuff();
191 void LoadSave(FILE* f, bool save)
193 if (save)
195 GetStuff();
197 //printf("----\n");
199 fputc('2', f);
200 fwrite(&general, sizeof(general), 1, f);
201 for(X* x=first; x; x=x->next)
203 short len = strlen(x->name);
204 fwrite(&len, sizeof(len), 1, f);
205 fwrite(x->name, len, 1, f);
207 x->LoadSave(f,save);
209 if (x->Completed())
211 //printf("% 8d %s\n", x->GetScore(), x->name);
212 x->Dump();
216 else
218 ClearRaw();
219 int v = fgetc(f);
220 if (v=='2')
222 fread(&general, sizeof(general), 1, f);
223 v = '1';
225 if (v=='1')
227 while(!feof(f))
229 char temp[1000];
230 short len;
231 fread(&len, sizeof(len), 1, f);
232 if (feof(f)) break;
233 fread(temp, len, 1, f);
234 temp[len] = 0;
235 first = new X(temp, first);
237 first->LoadSave(f,save);
241 ApplyStuff();
245 LevelSave* GetLevel(const char * name, bool create)
247 char * l = strstr(name, "Levels");
248 if (l)
249 name = l;
251 X* x = first;
252 if (x && strcmp(name, x->name)==0) return x;
253 while (x && x->next)
255 if (strcmp(name, x->next->name)==0) return x->next;
256 x = x->next;
258 if (create)
260 X* n = new X(name);
261 if (x)
262 x->next = n;
263 else
264 first = n;
265 return n;
267 else
269 return 0;