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 PassesPar(int par
)
95 return bestScore
<= par
;
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
;
118 struct X
: public LevelSave
123 X(const char* n
, X
* nx
=0) : next(nx
)
125 name
= new char[strlen(n
)+1];
137 int completionPercentage
;
139 int masteredPercentage
;
147 memset(&general
, 0, sizeof(general
));
148 general
.hintFlags
= 1<<31 | 1<<30;
166 SaveState() : first(0)
185 void LoadSave(FILE* f
, bool save
)
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
);
205 //printf("% 8d %s\n", x->GetScore(), x->name);
216 fread(&general
, sizeof(general
), 1, f
);
225 fread(&len
, sizeof(len
), 1, f
);
227 fread(temp
, len
, 1, f
);
229 first
= new X(temp
, first
);
231 first
->LoadSave(f
,save
);
239 LevelSave
* GetLevel(const char * name
, bool create
)
241 char * l
= strstr(name
, "Levels");
246 if (x
&& strcmp(name
, x
->name
)==0) return x
;
249 if (strcmp(name
, x
->next
->name
)==0) return x
->next
;