aboutsummaryrefslogtreecommitdiff
path: root/src/core/sok.h
blob: 92fb4e78388053a759814b8155d897d8f9f2aa4f (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#ifndef SOK_H
#define SOK_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <stdint.h> // uint16_t
#include <stddef.h> // size_t

/*********/
/* types */
/*********/

typedef struct {
  uint16_t x, y;
} sok_pos_t;

typedef enum {
  SOK_DIR_RIGHT,
  SOK_DIR_UP,
  SOK_DIR_LEFT,
  SOK_DIR_DOWN,
  SOK_DIR_LAST,
} sok_dir_t;

#define SOK_DIR_TO_CHAR(dir) ( \
  ((dir) == SOK_DIR_RIGHT) ? 'r' : \
  (((dir) == SOK_DIR_UP) ? 'u' : \
  ((((dir) == SOK_DIR_LEFT) ? 'l' : \
  (((((dir) == SOK_DIR_DOWN) ? 'd' : \
  'X' \
)))))))


#define SOK_DIR_TO_STR(dir) ( \
  ((dir) == SOK_DIR_RIGHT) ? "r" : \
  (((dir) == SOK_DIR_UP) ? "u" : \
  ((((dir) == SOK_DIR_LEFT) ? "l" : \
  (((((dir) == SOK_DIR_DOWN) ? "d" : \
  "X" \
)))))))

typedef struct {
  sok_pos_t pos;
  sok_dir_t dir;
  _Bool is_push;
} sok_move_t;

/****************/
/* level parser */
/****************/

typedef struct sok_level_parser_t_ sok_level_parser_t;

typedef _Bool (*sok_level_parser_pos_cb_t)(
  const sok_level_parser_t * const,
  const sok_pos_t
);

typedef _Bool (*sok_level_parser_junk_cb_t)(
  const sok_level_parser_t * const,
  const size_t,
  const char
);

typedef struct {
  sok_level_parser_pos_cb_t on_size,
                            on_home,
                            on_wall,
                            on_goal,
                            on_box;
  sok_level_parser_junk_cb_t on_junk;
} sok_level_parser_cbs_t;

struct sok_level_parser_t_ {
  const sok_level_parser_cbs_t *cbs;
  void *user_data;
};

void sok_level_parser_init(
  sok_level_parser_t * const parser, 
  const sok_level_parser_cbs_t * const cbs,
  void * const user_data
);

_Bool sok_level_parser_parse(
  sok_level_parser_t * const parser,
  const char * const buf,
  const size_t buf_len
);

/*********/
/* level */
/*********/

#define SOK_LEVEL_MAX_WIDTH (1 << 8)
#define SOK_LEVEL_MAX_HEIGHT (1 << 8)
#define SOK_LEVEL_MAX_BOXES 64
#define SOK_LEVEL_MAX_GOALS 64

typedef struct {
  sok_pos_t size;
  _Bool walls[SOK_LEVEL_MAX_WIDTH * SOK_LEVEL_MAX_HEIGHT];

  // player home position
  sok_pos_t home;

  // boxes
  sok_pos_t boxes[SOK_LEVEL_MAX_BOXES];
  size_t num_boxes;

  // goals
  sok_pos_t goals[SOK_LEVEL_MAX_GOALS];
  size_t num_goals;
} sok_level_t;

/***********/
/* context */
/***********/

#define SOK_CTX_MAX_MOVES 1024

typedef struct {
  sok_level_t level;

  // moves
  sok_move_t moves[SOK_CTX_MAX_MOVES];
  size_t num_moves;

  // number of open goals
  size_t num_goals_left;

  // are there boxes in corners?
  // _Bool is_lost;

  // player position
  sok_pos_t home;

  // box positions
  sok_pos_t boxes[SOK_LEVEL_MAX_BOXES];

  // user data
  void *user_data;
} sok_ctx_t;

void sok_ctx_init(sok_ctx_t * const ctx, void *user_data);

_Bool sok_ctx_set_level(sok_ctx_t * const ctx, const char * const level);

_Bool sok_ctx_is_done(const sok_ctx_t * const);
// _Bool sok_ctx_is_lost(const sok_ctx_t * const);

_Bool sok_ctx_move(sok_ctx_t * const, const sok_dir_t);
_Bool sok_ctx_undo(sok_ctx_t * const);

/******************/
/* context walker */
/******************/

typedef _Bool (*sok_ctx_walk_step_cb_t)(
  const sok_ctx_t * const,
  void * const
);

typedef _Bool (*sok_ctx_walk_pos_cb_t)(
  const sok_ctx_t * const,
  const sok_pos_t,
  void * const
);

typedef _Bool (*sok_ctx_walk_tile_cb_t)(
  const sok_ctx_t * const,
  const sok_pos_t,
  const _Bool,
  void * const
);

typedef _Bool (*sok_ctx_walk_goal_cb_t)(
  const sok_ctx_t * const,
  const sok_pos_t,
  const _Bool has_player,
  const _Bool has_box,
  void * const
);

typedef _Bool (*sok_ctx_walk_move_cb_t)(
  const sok_ctx_t * const,
  const sok_move_t,
  void * const
);

typedef struct {
  sok_ctx_walk_pos_cb_t on_size;

  sok_ctx_walk_step_cb_t on_walls_start,
                         on_walls_end;
  sok_ctx_walk_pos_cb_t on_wall;

  sok_ctx_walk_tile_cb_t on_home;

  sok_ctx_walk_step_cb_t on_boxes_start,
                         on_boxes_end;
  sok_ctx_walk_tile_cb_t on_box;

  sok_ctx_walk_step_cb_t on_goals_start,
                         on_goals_end;
  sok_ctx_walk_goal_cb_t on_goal;

  sok_ctx_walk_step_cb_t on_moves_start,
                         on_moves_end;
  sok_ctx_walk_move_cb_t on_move;
} sok_ctx_walk_cbs_t;

_Bool sok_ctx_walk(
  const sok_ctx_t * const,
  const sok_ctx_walk_cbs_t * const,
  void * const
);

/****************/
/* context hash */
/****************/

uint64_t sok_ctx_hash(const sok_ctx_t * const);

/*********/
/* cache */
/*********/

#define SOK_CACHE_DEFAULT_CAPACITY 1024

typedef struct {
  uint64_t *vals;
  size_t num_vals, capacity;
} sok_cache_t;

_Bool sok_cache_init(sok_cache_t * const, const size_t);
void sok_cache_fini(sok_cache_t * const);

_Bool sok_cache_has(const sok_cache_t * const, const sok_ctx_t * const);
_Bool sok_cache_add(sok_cache_t * const, const sok_ctx_t * const);

/*********/
/* solve */
/*********/

typedef struct {
  _Bool (*on_step)(const sok_ctx_t *, void *);
  void (*on_error)(const char * const, void *);
} sok_solve_cbs_t;

_Bool sok_solve(
  sok_ctx_t * const,
  const sok_solve_cbs_t * const,
  void * const
);

#ifdef __cplusplus
};
#endif /* __cplusplus */

#endif /* SOK_H */