-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsokoban.cpp
465 lines (410 loc) · 15.5 KB
/
sokoban.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "brick.h"
#include "cleananimation.h"
#include "winanimation.h"
#include "sokoban_levels.h"
#include <windows.h>
#include <time.h>
/* Interface static function prototypes */
static void sokoban_init(void);
static brick_state_t* sokoban_get_state(void);
static void sokoban_right_key_press(void);
static void sokoban_left_key_press(void);
static void sokoban_right_key_release(void);
static void sokoban_left_key_release(void);
static void sokoban_up_key_press(void);
static void sokoban_up_key_release(void);
static void sokoban_down_key_press(void);
static void sokoban_down_key_release(void);
static void sokoban_next_figure_accepted(void);
static void sokoban_game_loop(void);
/* Other static function prototypes */
static void sokoban_init_after_cleananimation();
static void sokoban_move_right(void);
static void sokoban_move_left(void);
static void sokoban_move_up(void);
static void sokoban_move_down(void);
static void sokoban_restore_storage_locations(void);
/* keyboard states and times */
static char left_key_is_pressed = 0;
static unsigned int left_key_press_start_time = 0;
static unsigned int left_key_repeat = 0;
static char right_key_is_pressed = 0;
static unsigned int right_key_press_start_time = 0;
static char right_key_repeat = 0;
static char up_key_is_pressed = 0;
static unsigned int up_key_press_start_time = 0;
static char up_key_repeat = 0;
static char down_key_is_pressed = 0;
static unsigned int down_key_press_start_time = 0;
static char down_key_repeat = 0;
typedef struct {
unsigned char right;
unsigned char left;
unsigned char up;
unsigned char down;
} keypress_store_t;
static keypress_store_t keypress_store = { 0, 0, 0, 0 };
/* Game states */
#define SOKOBAN_STATE_NORMAL 0
#define SOKOBAN_STATE_CLEANANIMATION 1
#define SOKOBAN_STATE_WINANIMATION 2
typedef struct {
char x;
char y;
char state;
char level_done;
} sokoban_t;
static sokoban_t sokoban = { SOKOBAN_STATE_NORMAL };
/******************************************************************************
* Exported functions.
******************************************************************************/
void sokoban_init_interface(game_interface_t* iface) {
iface->game_init = sokoban_init;
iface->game_right_key_press = sokoban_right_key_press;
iface->game_left_key_press = sokoban_left_key_press;
iface->game_right_key_release = sokoban_right_key_release;
iface->game_left_key_release = sokoban_left_key_release;
iface->game_up_key_press = sokoban_up_key_press;
iface->game_up_key_release = sokoban_up_key_release;
iface->game_down_key_press = sokoban_down_key_press;
iface->game_down_key_release = sokoban_down_key_release;
iface->game_next_figure_accepted = sokoban_next_figure_accepted;
iface->game_loop = sokoban_game_loop;
}
/******************************************************************************
* Static functions.
******************************************************************************/
static brick_state_t* sokoban_get_state(void) {
return &brick_s;
}
static void sokoban_init(void) {
sokoban.state = SOKOBAN_STATE_CLEANANIMATION;
brick_s.level = 1;
brick_s.score = 0;
brick_s.lives = 0;
cleananimation_init();
}
static void sokoban_init_after_cleananimation() {
int i, j;
for (i = 0; i < BRICK_PLAYFIELD_WIDTH; i++) {
for (j = 0; j < BRICK_PLAYFIELD_HEIGHT; j++) {
switch (sokoban_levels[brick_s.level - 1].level[j][i]) {
case 0:
brick_s.playfield[i][j] = BRICK_FIELD_EMPTY;
break;
case 1:
brick_s.playfield[i][j] = BRICK_FIELD_OCCUPIED_OUTER;
break;
case 2:
brick_s.playfield[i][j] = BRICK_FIELD_OCCUPIED_INNER;
break;
}
}
}
for (i = 0; i < sokoban_levels[brick_s.level - 1].storage_locations_num; i++) {
brick_s.playfield[sokoban_levels[brick_s.level - 1].storage_locations[i].x][sokoban_levels[brick_s.level - 1].storage_locations[i].y] = BRICK_FIELD_OCCUPIED_INNER_SMALL;
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
brick_s.next[i][j] = BRICK_FIELD_EMPTY;
}
}
brick_s.score_changed = 1;
brick_s.level_changed = 1;
brick_s.lives_changed = 1;
brick_s.game_over_notification_flag = false;
brick_s.winning_notification_flag = false;
brick_s.rr.left = 0;
brick_s.rr.top = 0;
brick_s.rr.right = BRICK_PLAYFIELD_WIDTH;
brick_s.rr.bottom = BRICK_PLAYFIELD_HEIGHT;
brick_s.rr.clean = 0;
brick_s.next_changed = true;
left_key_is_pressed = 0;
right_key_is_pressed = 0;
up_key_is_pressed = 0;
down_key_is_pressed = 0;
keypress_store.right = 0;
keypress_store.left = 0;
keypress_store.up = 0;
keypress_store.down = 0;
sokoban.x = sokoban_levels[brick_s.level - 1].player_start_location.x;
sokoban.y = sokoban_levels[brick_s.level - 1].player_start_location.y;
sokoban.level_done = 0;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_OCCUPIED_ORANGE;
sokoban.state = SOKOBAN_STATE_NORMAL;
}
static void sokoban_right_key_press(void) {
if (right_key_is_pressed == 0)
right_key_press_start_time = GetTickCount();
right_key_is_pressed = 1;
keypress_store.right = 1;
}
static void sokoban_left_key_press(void) {
if (left_key_is_pressed == 0)
left_key_press_start_time = GetTickCount();
left_key_is_pressed = 1;
keypress_store.left = 1;
}
static void sokoban_right_key_release(void) {
right_key_is_pressed = 0;
right_key_repeat = 0;
}
static void sokoban_left_key_release(void) {
left_key_is_pressed = 0;
left_key_repeat = 0;
}
static void sokoban_up_key_press(void) {
if (up_key_is_pressed == 0)
up_key_press_start_time = GetTickCount();
up_key_is_pressed = 1;
keypress_store.up = 1;
}
static void sokoban_up_key_release(void) {
up_key_is_pressed = 0;
up_key_repeat = 0;
}
static void sokoban_down_key_press(void) {
if (down_key_is_pressed == 0)
down_key_press_start_time = GetTickCount();
down_key_is_pressed = 1;
keypress_store.down = 1;
}
static void sokoban_down_key_release(void) {
down_key_is_pressed = 0;
down_key_repeat = 0;
}
static void sokoban_next_figure_accepted(void) {
brick_s.next_changed = false;
}
static void sokoban_game_loop(void) {
if (sokoban.state == SOKOBAN_STATE_CLEANANIMATION) {
cleananimation();
if (cleananimation() == CLEANANIMATION_DONE)
sokoban_init_after_cleananimation();
return;
}
if (sokoban.state == SOKOBAN_STATE_WINANIMATION) {
winanimation();
return;
}
if (sokoban.level_done) {
brick_s.score += sokoban_levels[brick_s.level - 1].storage_locations_num * 100;
brick_s.score_changed = 1;
if (brick_s.level > SOKOBAN_LEVELS - 1) {
winanimation_init();
sokoban.state = SOKOBAN_STATE_WINANIMATION;
brick_s.winning_notification_flag = true;
return;
}
brick_s.level++;
sokoban.state = SOKOBAN_STATE_CLEANANIMATION;
cleananimation_init();
return;
}
unsigned int t = GetTickCount();
if (left_key_is_pressed) {
if (t > left_key_press_start_time + 50 || left_key_repeat == 0) {
if (left_key_repeat != 1)
sokoban_move_left();
left_key_press_start_time = t;
left_key_repeat++;
keypress_store.left = 0;
}
}
else if (keypress_store.left) {
sokoban_move_left();
keypress_store.left = 0;
}
else if (right_key_is_pressed) {
if (t > right_key_press_start_time + 50 || right_key_repeat == 0) {
if (right_key_repeat != 1)
sokoban_move_right();
right_key_press_start_time = t;
right_key_repeat++;
keypress_store.right = 0;
}
}
else if (keypress_store.right) {
sokoban_move_right();
keypress_store.right = 0;
}
else if (up_key_is_pressed) {
if (t > up_key_press_start_time + 50 || up_key_repeat == 0) {
if (up_key_repeat != 1)
sokoban_move_up();
up_key_press_start_time = t;
up_key_repeat++;
keypress_store.up = 0;
}
}
else if (keypress_store.up) {
sokoban_move_up();
keypress_store.up = 0;
}
else if (down_key_is_pressed) {
if (t > down_key_press_start_time + 50 || down_key_repeat == 0) {
if (down_key_repeat != 1)
sokoban_move_down();
down_key_press_start_time = t;
down_key_repeat++;
keypress_store.down = 0;
}
}
else if (keypress_store.down) {
sokoban_move_down();
keypress_store.down = 0;
}
}
static void sokoban_move_right(void) {
if (sokoban.x == BRICK_PLAYFIELD_WIDTH - 1)
return;
if (brick_s.playfield[sokoban.x + 1][sokoban.y] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x + 1][sokoban.y] == BRICK_FIELD_OCCUPIED_INNER_SMALL) {
// Move the player 1 field to the right
sokoban.x++;
brick_s.playfield[sokoban.x - 1][sokoban.y] = BRICK_FIELD_EMPTY;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.rr.left = sokoban.x - 1;
brick_s.rr.right = sokoban.x;
brick_s.rr.top = brick_s.rr.bottom = sokoban.y;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
if (sokoban.x == BRICK_PLAYFIELD_WIDTH - 2)
return;
if (brick_s.playfield[sokoban.x + 1][sokoban.y] == BRICK_FIELD_OCCUPIED_INNER &&
(brick_s.playfield[sokoban.x + 2][sokoban.y] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x + 2][sokoban.y] == BRICK_FIELD_OCCUPIED_INNER_SMALL)) {
// Push the movable box 1 field to the right
brick_s.playfield[sokoban.x + 2][sokoban.y] = BRICK_FIELD_OCCUPIED_INNER;
brick_s.playfield[sokoban.x + 1][sokoban.y] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_EMPTY;
sokoban.x++;
brick_s.rr.left = sokoban.x - 1;
brick_s.rr.right = sokoban.x + 1;
brick_s.rr.top = brick_s.rr.bottom = sokoban.y;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
}
static void sokoban_move_left(void) {
if (sokoban.x == 0)
return;
if (brick_s.playfield[sokoban.x - 1][sokoban.y] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x - 1][sokoban.y] == BRICK_FIELD_OCCUPIED_INNER_SMALL) {
// Move the player 1 field to the left
sokoban.x--;
brick_s.playfield[sokoban.x + 1][sokoban.y] = BRICK_FIELD_EMPTY;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.rr.left = sokoban.x;
brick_s.rr.right = sokoban.x + 1;
brick_s.rr.top = brick_s.rr.bottom = sokoban.y;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
if (sokoban.x == 1)
return;
if (brick_s.playfield[sokoban.x - 1][sokoban.y] == BRICK_FIELD_OCCUPIED_INNER &&
(brick_s.playfield[sokoban.x - 2][sokoban.y] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x - 2][sokoban.y] == BRICK_FIELD_OCCUPIED_INNER_SMALL)) {
// Push the movable box 1 field to the left
brick_s.playfield[sokoban.x - 2][sokoban.y] = BRICK_FIELD_OCCUPIED_INNER;
brick_s.playfield[sokoban.x - 1][sokoban.y] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_EMPTY;
sokoban.x--;
brick_s.rr.left = sokoban.x - 1;
brick_s.rr.right = sokoban.x + 1;
brick_s.rr.top = brick_s.rr.bottom = sokoban.y;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
}
static void sokoban_move_up(void) {
if (sokoban.y == 0)
return;
if (brick_s.playfield[sokoban.x][sokoban.y - 1] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x][sokoban.y - 1] == BRICK_FIELD_OCCUPIED_INNER_SMALL) {
// Move the player 1 field up
sokoban.y--;
brick_s.playfield[sokoban.x][sokoban.y + 1] = BRICK_FIELD_EMPTY;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.rr.top = sokoban.y;
brick_s.rr.bottom = sokoban.y + 1;
brick_s.rr.right = brick_s.rr.left = sokoban.x;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
if (sokoban.y == 1)
return;
if (brick_s.playfield[sokoban.x][sokoban.y - 1] == BRICK_FIELD_OCCUPIED_INNER &&
(brick_s.playfield[sokoban.x][sokoban.y - 2] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x][sokoban.y - 2] == BRICK_FIELD_OCCUPIED_INNER_SMALL)) {
// Push the movable box 1 field up
brick_s.playfield[sokoban.x][sokoban.y - 2] = BRICK_FIELD_OCCUPIED_INNER;
brick_s.playfield[sokoban.x][sokoban.y - 1] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_EMPTY;
sokoban.y--;
brick_s.rr.top = sokoban.y - 1;
brick_s.rr.bottom = sokoban.y + 1;
brick_s.rr.right = brick_s.rr.left = sokoban.x;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
}
static void sokoban_move_down(void) {
if (sokoban.y == BRICK_PLAYFIELD_HEIGHT - 1)
return;
if (brick_s.playfield[sokoban.x][sokoban.y + 1] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x][sokoban.y + 1] == BRICK_FIELD_OCCUPIED_INNER_SMALL) {
// Move the player 1 field down
sokoban.y++;
brick_s.playfield[sokoban.x][sokoban.y - 1] = BRICK_FIELD_EMPTY;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.rr.top = sokoban.y - 1;
brick_s.rr.bottom = sokoban.y;
brick_s.rr.right = brick_s.rr.left = sokoban.x;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
if (sokoban.y == BRICK_PLAYFIELD_HEIGHT - 2)
return;
if (brick_s.playfield[sokoban.x][sokoban.y + 1] == BRICK_FIELD_OCCUPIED_INNER &&
(brick_s.playfield[sokoban.x][sokoban.y + 2] == BRICK_FIELD_EMPTY ||
brick_s.playfield[sokoban.x][sokoban.y + 2] == BRICK_FIELD_OCCUPIED_INNER_SMALL)) {
// Push the movable box 1 field down
brick_s.playfield[sokoban.x][sokoban.y + 2] = BRICK_FIELD_OCCUPIED_INNER;
brick_s.playfield[sokoban.x][sokoban.y + 1] = BRICK_FIELD_OCCUPIED_ORANGE;
brick_s.playfield[sokoban.x][sokoban.y] = BRICK_FIELD_EMPTY;
sokoban.y++;
brick_s.rr.top = sokoban.y - 1;
brick_s.rr.bottom = sokoban.y + 1;
brick_s.rr.right = brick_s.rr.left = sokoban.x;
brick_s.rr.clean = 0;
sokoban_restore_storage_locations();
return;
}
}
static void sokoban_restore_storage_locations(void) {
int num = sokoban_levels[brick_s.level - 1].storage_locations_num;
int i;
int x, y;
int remaining = num;
for (i = 0; i < num; i++) {
x = sokoban_levels[brick_s.level - 1].storage_locations[i].x;
y = sokoban_levels[brick_s.level - 1].storage_locations[i].y;
if (brick_s.playfield[x][y] == BRICK_FIELD_OCCUPIED_INNER)
remaining--;
else if (brick_s.playfield[x][y] == BRICK_FIELD_EMPTY)
brick_s.playfield[x][y] = BRICK_FIELD_OCCUPIED_INNER_SMALL;
}
if (remaining == 0)
sokoban.level_done = 1;
}