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
24 friend struct HexPuzzle
;
27 int bestSolutionLength
;
29 #define NUM_LAST_SCORES 19
30 int lastScores
[NUM_LAST_SCORES
];
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
);
64 for (int j
=1; j
<NUM_LAST_SCORES
; j
++)
65 if (lastScores
[j
]==lastScores
[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]);*/
74 return bestScore
!= 0;
76 bool IsNewCompletionBetter(int score
)
78 for (int i
=0; i
<NUM_LAST_SCORES
; i
++)
81 lastScores
[i
] = score
;
82 if (lastScores
[i
]==score
)
89 return score
<= bestScore
;
91 bool BeatsPar(int par
)
95 return bestScore
< par
;
97 bool PassesPar(int par
)
101 return bestScore
<= par
;
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
;
124 struct X
: public LevelSave
129 X(const char* n
, X
* nx
=0) : next(nx
)
131 name
= new char[strlen(n
)+1];
143 int completionPercentage
;
145 int masteredPercentage
;
153 memset(&general
, 0, sizeof(general
));
154 general
.hintFlags
= 1<<31 | 1<<30;
172 SaveState() : first(0)
191 void LoadSave(FILE* f
, bool save
)
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
);
211 //printf("% 8d %s\n", x->GetScore(), x->name);
222 fread(&general
, sizeof(general
), 1, f
);
231 fread(&len
, sizeof(len
), 1, f
);
233 fread(temp
, len
, 1, f
);
235 first
= new X(temp
, first
);
237 first
->LoadSave(f
,save
);
245 LevelSave
* GetLevel(const char * name
, bool create
)
247 char * l
= strstr(name
, "Levels");
252 if (x
&& strcmp(name
, x
->name
)==0) return x
;
255 if (strcmp(name
, x
->next
->name
)==0) return x
->next
;