/
solution.h
192 lines (158 loc) · 5.53 KB
/
solution.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* solution.c: Functions for reading and writing the solution files.
*
* Copyright (C) 2001-2006 by Brian Raiter, under the GNU General Public
* License. No warranty. See COPYING for details.
*/
#ifndef _solution_h_
#define _solution_h_
#include "fileio.h"
/* The standard Boolean values.
*/
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/* The four directions plus one non-direction.
*/
#define NIL 0
#define NORTH 1
#define WEST 2
#define SOUTH 4
#define EAST 8
/* Translating directions to and from a two-bit representation. (Note
* that NIL will map to the same value as NORTH.)
*/
#define diridx(dir) ((0x30210 >> ((dir) * 2)) & 3)
#define idxdir(idx) (1 << ((idx) & 3))
/* The frequency of the gameplay timer. Note that "seconds" refers to
* seconds in the game, which are not necessarily the same length as
* real-time seconds.
*/
#define TICKS_PER_SECOND 20
/* The gameplay timer's value is forced to remain within 23 bits.
* Thus, gameplay of a single level cannot exceed 4 days 20 hours 30
* minutes and 30.4 seconds.
*/
#define MAXIMUM_TICK_COUNT 0x7FFFFF
/* A magic number used to indicate an undefined time value.
*/
#define TIME_NIL 0x7FFFFFFF
/*
* Miscellaneous definitions.
*/
/* The various rulesets the program can emulate.
*/
enum {
Ruleset_None = 0,
Ruleset_Lynx = 1,
Ruleset_MS = 2,
Ruleset_Count
};
/* Pseudorandom number generators.
*/
typedef struct prng {
unsigned long initial; /* initial seed value */
unsigned long value; /* latest random value */
char shared; /* FALSE if independent sequence */
} prng;
/*
* Definitions used in game play.
*/
/* Turning macros.
*/
#define left(dir) ((((dir) << 1) | ((dir) >> 3)) & 15)
#define back(dir) ((((dir) << 2) | ((dir) >> 2)) & 15)
#define right(dir) ((((dir) << 3) | ((dir) >> 1)) & 15)
/* A move is specified by its direction and when it takes place.
*/
typedef struct action { unsigned int when:23, dir:9; } action;
/* A structure for managing the memory holding the moves of a game.
*/
typedef struct actlist {
int allocated; /* number of elements allocated */
int count; /* size of the actual array */
action *list; /* the array */
} actlist;
/* A structure holding all the data needed to reconstruct a solution.
*/
typedef struct solutioninfo {
actlist moves; /* the actual moves of the solution */
unsigned long rndseed; /* the PRNG's initial seed */
unsigned long flags; /* other flags (currently unused) */
unsigned char rndslidedir; /* random slide's initial direction */
signed char stepping; /* the timer offset */
} solutioninfo;
/* The range of relative mouse moves is a 19x19 square around Chip.
* (Mouse moves are stored as a relative offset in order to fit all
* possible moves in nine bits.)
*/
#define MOUSERANGEMIN -9
#define MOUSERANGEMAX +9
#define MOUSERANGE 19
enum { CmdKeyMoveLast = NORTH | WEST | SOUTH | EAST };
/* True if cmd is a simple directional command, i.e. a single
* orthogonal or diagonal move (or CmdNone).
*/
#define directionalcmd(cmd) (((cmd) & ~CmdKeyMoveLast) == 0)
/* The collection of data maintained for each level.
*/
typedef struct gamesetup {
int number; /* numerical ID of the level */
int time; /* no. of seconds allotted */
int besttime; /* time (in ticks) of best solution */
int sgflags; /* saved-game flags (see below) */
int levelsize; /* size of the level data */
int solutionsize; /* size of the saved solution data */
unsigned char *leveldata; /* the data defining the level */
unsigned char *solutiondata; /* the player's best solution so far */
unsigned long levelhash; /* the level data's hash value */
char const *unsolvable; /* why level is unsolvable, or NULL */
char name[256]; /* name of the level */
char passwd[256]; /* the level's password */
} gamesetup;
/* Flags associated with a saved game.
*/
#define SGF_HASPASSWD 0x0001 /* player knows the level's password */
#define SGF_REPLACEABLE 0x0002 /* solution is marked as replaceable */
#define SGF_SETNAME 0x0004 /* internal to solution.c */
/*
* Solution functions.
*/
/* Initialize or reinitialize list as empty.
*/
extern void initmovelist(actlist *list);
/* Append move to the end of list.
*/
extern void addtomovelist(actlist *list, action move);
/* Make to an independent copy of from.
*/
extern void copymovelist(actlist *to, actlist const *from);
/* Deallocate list.
*/
extern void destroymovelist(actlist *list);
/* Read the header bytes of the given solution file. flags receives
* the option bytes (bytes 5-6). extra receives any bytes in the
* header that this code doesn't recognize.
*/
extern int readsolutionheader(fileinfo *file, int *ruleset, int *flags,
int *extrasize, unsigned char *extra);
/* Read the data of a one complete solution from the given file into
* a gamesetup structure.
*/
extern int readsolution(fileinfo *file, gamesetup* game);
/* Free all memory allocated for storing a solution.
*/
extern void clearsolution(gamesetup *game);
/* Expand a level's solution data into the actual solution, including
* the full list of moves. FALSE is returned if the solution is
* invalid or absent.
*/
extern int expandsolution(solutioninfo *solution, gamesetup const *game);
/* Take the given solution and compress it, storing the compressed
* data as part of the level's setup. FALSE is returned if an error
* occurs. (It is not an error to compress the null solution.)
*/
extern int contractsolution(solutioninfo const *solution, gamesetup *game);
#endif