Don't force double-buffering for sd devices. They apparently are not faster with...
[kugel-rb.git] / apps / plugins / sokoban.c
blob8672249bdc9aed50e6727af9606964287b3757c7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Eric Linenberg
11 * February 2003: Robert Hak performs a cleanup/rewrite/feature addition.
12 * Eric smiles. Bjorn cries. Linus say 'huh?'.
13 * March 2007: Sean Morrisey performs a major rewrite/feature addition.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "plugin.h"
25 #include "lib/playback_control.h"
27 PLUGIN_HEADER
29 #define SOKOBAN_TITLE "Sokoban"
31 #define SOKOBAN_LEVELS_FILE PLUGIN_GAMES_DIR "/sokoban.levels"
32 #define SOKOBAN_SAVE_FILE PLUGIN_GAMES_DIR "/sokoban.save"
33 #define SOKOBAN_SAVE_FOLDER "/games"
35 #include "pluginbitmaps/sokoban_tiles.h"
36 #define SOKOBAN_TILESIZE BMPWIDTH_sokoban_tiles
38 /* If tilesize is 0 (which it is during dependency generation) gcc will abort
39 (div by 0) and this plugin won't get any dependencies
41 #if SOKOBAN_TILESIZE < 1
42 #define SOKOBAN_TILESIZE 10
43 #endif
45 /* SOKOBAN_TILESIZE is the number of pixels for each block.
46 * Set dynamically so all targets can support levels
47 * that fill their entire screen, less the stat box.
48 * 16 rows & 20 cols minimum */
49 #if LCD_WIDTH > LCD_HEIGHT /* horizontal layout*/
50 #define ROWS (LCD_HEIGHT/SOKOBAN_TILESIZE)
51 #if (LCD_WIDTH+4) >= (20*SOKOBAN_TILESIZE+40) /* wide or narrow stats box */
52 #define COLS ((LCD_WIDTH-40)/SOKOBAN_TILESIZE)
53 #else
54 #define COLS ((LCD_WIDTH-32)/SOKOBAN_TILESIZE)
55 #endif
56 #else /* vertical layout*/
57 #define ROWS ((LCD_HEIGHT-25)/SOKOBAN_TILESIZE)
58 #define COLS (LCD_WIDTH/SOKOBAN_TILESIZE)
59 #endif
61 /* Use either all but 16k of the plugin buffer for level data
62 * or 128k, which ever is less */
63 #if PLUGIN_BUFFER_SIZE - 0x4000 < 0x20000
64 #define MAX_LEVEL_DATA (PLUGIN_BUFFER_SIZE - 0x4000)
65 #else
66 #define MAX_LEVEL_DATA 0x20000
67 #endif
69 /* Number of levels for which to allocate buffer indexes */
70 #define MAX_LEVELS MAX_LEVEL_DATA/70
72 /* Use 4k plus remaining plugin buffer (-12k for prog) for undo, up to 64k */
73 #if PLUGIN_BUFFER_SIZE - MAX_LEVEL_DATA - 0x3000 > 0x10000
74 #define MAX_UNDOS 0x10000
75 #else
76 #define MAX_UNDOS (PLUGIN_BUFFER_SIZE - MAX_LEVEL_DATA - 0x3000)
77 #endif
79 /* Move/push definitions for undo */
80 #define SOKOBAN_PUSH_LEFT 'L'
81 #define SOKOBAN_PUSH_RIGHT 'R'
82 #define SOKOBAN_PUSH_UP 'U'
83 #define SOKOBAN_PUSH_DOWN 'D'
84 #define SOKOBAN_MOVE_LEFT 'l'
85 #define SOKOBAN_MOVE_RIGHT 'r'
86 #define SOKOBAN_MOVE_UP 'u'
87 #define SOKOBAN_MOVE_DOWN 'd'
89 #define SOKOBAN_MOVE_DIFF (SOKOBAN_MOVE_LEFT-SOKOBAN_PUSH_LEFT)
90 #define SOKOBAN_MOVE_MIN SOKOBAN_MOVE_DOWN
92 /* variable button definitions */
93 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
94 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
95 #define SOKOBAN_LEFT BUTTON_LEFT
96 #define SOKOBAN_RIGHT BUTTON_RIGHT
97 #define SOKOBAN_UP BUTTON_UP
98 #define SOKOBAN_DOWN BUTTON_DOWN
99 #define SOKOBAN_MENU BUTTON_OFF
100 #define SOKOBAN_UNDO BUTTON_ON
101 #define SOKOBAN_REDO BUTTON_PLAY
102 #define SOKOBAN_LEVEL_DOWN BUTTON_F1
103 #define SOKOBAN_LEVEL_REPEAT BUTTON_F2
104 #define SOKOBAN_LEVEL_UP BUTTON_F3
105 #define SOKOBAN_PAUSE BUTTON_PLAY
106 #define BUTTON_SAVE BUTTON_ON
107 #define BUTTON_SAVE_NAME "ON"
109 #elif CONFIG_KEYPAD == ONDIO_PAD
110 #define SOKOBAN_LEFT BUTTON_LEFT
111 #define SOKOBAN_RIGHT BUTTON_RIGHT
112 #define SOKOBAN_UP BUTTON_UP
113 #define SOKOBAN_DOWN BUTTON_DOWN
114 #define SOKOBAN_MENU BUTTON_OFF
115 #define SOKOBAN_UNDO_PRE BUTTON_MENU
116 #define SOKOBAN_UNDO (BUTTON_MENU | BUTTON_REL)
117 #define SOKOBAN_REDO (BUTTON_MENU | BUTTON_DOWN)
118 #define SOKOBAN_LEVEL_DOWN (BUTTON_MENU | BUTTON_LEFT)
119 #define SOKOBAN_LEVEL_REPEAT (BUTTON_MENU | BUTTON_UP)
120 #define SOKOBAN_LEVEL_UP (BUTTON_MENU | BUTTON_RIGHT)
121 #define SOKOBAN_PAUSE BUTTON_MENU
122 #define BUTTON_SAVE BUTTON_MENU
123 #define BUTTON_SAVE_NAME "MENU"
125 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
126 (CONFIG_KEYPAD == IRIVER_H300_PAD)
127 #define SOKOBAN_LEFT BUTTON_LEFT
128 #define SOKOBAN_RIGHT BUTTON_RIGHT
129 #define SOKOBAN_UP BUTTON_UP
130 #define SOKOBAN_DOWN BUTTON_DOWN
131 #define SOKOBAN_MENU BUTTON_OFF
132 #define SOKOBAN_UNDO BUTTON_REC
133 #define SOKOBAN_REDO BUTTON_MODE
134 #define SOKOBAN_LEVEL_DOWN (BUTTON_ON | BUTTON_DOWN)
135 #define SOKOBAN_LEVEL_REPEAT BUTTON_ON
136 #define SOKOBAN_LEVEL_UP (BUTTON_ON | BUTTON_UP)
137 #define SOKOBAN_PAUSE BUTTON_ON
138 #define BUTTON_SAVE BUTTON_MODE
139 #define BUTTON_SAVE_NAME "MODE"
141 #define SOKOBAN_RC_MENU BUTTON_RC_STOP
143 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
144 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
145 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
146 #define SOKOBAN_LEFT BUTTON_LEFT
147 #define SOKOBAN_RIGHT BUTTON_RIGHT
148 #define SOKOBAN_UP BUTTON_MENU
149 #define SOKOBAN_DOWN BUTTON_PLAY
150 #define SOKOBAN_MENU (BUTTON_SELECT | BUTTON_MENU)
151 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
152 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
153 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_PLAY)
154 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_LEFT)
155 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_RIGHT)
156 #define SOKOBAN_PAUSE BUTTON_SELECT
157 #define BUTTON_SAVE BUTTON_SELECT
158 #define BUTTON_SAVE_NAME "SELECT"
160 /* FIXME: if/when simultaneous button presses work for X5/M5,
161 * add level up/down */
162 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
163 #define SOKOBAN_LEFT BUTTON_LEFT
164 #define SOKOBAN_RIGHT BUTTON_RIGHT
165 #define SOKOBAN_UP BUTTON_UP
166 #define SOKOBAN_DOWN BUTTON_DOWN
167 #define SOKOBAN_MENU BUTTON_POWER
168 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
169 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
170 #define SOKOBAN_LEVEL_REPEAT BUTTON_REC
171 #define SOKOBAN_REDO BUTTON_PLAY
172 #define SOKOBAN_PAUSE BUTTON_PLAY
173 #define BUTTON_SAVE BUTTON_SELECT
174 #define BUTTON_SAVE_NAME "SELECT"
176 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
177 #define SOKOBAN_LEFT BUTTON_LEFT
178 #define SOKOBAN_RIGHT BUTTON_RIGHT
179 #define SOKOBAN_UP BUTTON_SCROLL_UP
180 #define SOKOBAN_DOWN BUTTON_SCROLL_DOWN
181 #define SOKOBAN_MENU BUTTON_POWER
182 #define SOKOBAN_UNDO_PRE BUTTON_REW
183 #define SOKOBAN_UNDO (BUTTON_REW | BUTTON_REL)
184 #define SOKOBAN_REDO BUTTON_FF
185 #define SOKOBAN_LEVEL_DOWN (BUTTON_PLAY | BUTTON_SCROLL_DOWN)
186 #define SOKOBAN_LEVEL_REPEAT (BUTTON_PLAY | BUTTON_RIGHT)
187 #define SOKOBAN_LEVEL_UP (BUTTON_PLAY | BUTTON_SCROLL_UP)
188 #define SOKOBAN_PAUSE BUTTON_PLAY
189 #define BUTTON_SAVE BUTTON_PLAY
190 #define BUTTON_SAVE_NAME "PLAY"
192 #elif CONFIG_KEYPAD == GIGABEAT_PAD
193 #define SOKOBAN_LEFT BUTTON_LEFT
194 #define SOKOBAN_RIGHT BUTTON_RIGHT
195 #define SOKOBAN_UP BUTTON_UP
196 #define SOKOBAN_DOWN BUTTON_DOWN
197 #define SOKOBAN_MENU BUTTON_POWER
198 #define SOKOBAN_UNDO BUTTON_SELECT
199 #define SOKOBAN_REDO BUTTON_A
200 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
201 #define SOKOBAN_LEVEL_REPEAT BUTTON_MENU
202 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
203 #define SOKOBAN_PAUSE BUTTON_SELECT
204 #define BUTTON_SAVE BUTTON_SELECT
205 #define BUTTON_SAVE_NAME "SELECT"
207 #elif CONFIG_KEYPAD == SANSA_E200_PAD
208 #define SOKOBAN_LEFT BUTTON_LEFT
209 #define SOKOBAN_RIGHT BUTTON_RIGHT
210 #define SOKOBAN_UP BUTTON_UP
211 #define SOKOBAN_DOWN BUTTON_DOWN
212 #define SOKOBAN_MENU BUTTON_POWER
213 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
214 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
215 #define SOKOBAN_REDO BUTTON_REC
216 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
217 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
218 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
219 #define SOKOBAN_PAUSE BUTTON_SELECT
220 #define BUTTON_SAVE BUTTON_SELECT
221 #define BUTTON_SAVE_NAME "SELECT"
223 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
224 #define SOKOBAN_LEFT BUTTON_LEFT
225 #define SOKOBAN_RIGHT BUTTON_RIGHT
226 #define SOKOBAN_UP BUTTON_UP
227 #define SOKOBAN_DOWN BUTTON_DOWN
228 #define SOKOBAN_MENU (BUTTON_HOME|BUTTON_REPEAT)
229 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
230 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
231 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_LEFT)
232 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
233 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
234 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
235 #define SOKOBAN_PAUSE BUTTON_SELECT
236 #define BUTTON_SAVE BUTTON_SELECT
237 #define BUTTON_SAVE_NAME "SELECT"
239 #elif CONFIG_KEYPAD == SANSA_C200_PAD
240 #define SOKOBAN_LEFT BUTTON_LEFT
241 #define SOKOBAN_RIGHT BUTTON_RIGHT
242 #define SOKOBAN_UP BUTTON_UP
243 #define SOKOBAN_DOWN BUTTON_DOWN
244 #define SOKOBAN_MENU BUTTON_POWER
245 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
246 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
247 #define SOKOBAN_REDO BUTTON_REC
248 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
249 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
250 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
251 #define SOKOBAN_PAUSE BUTTON_SELECT
252 #define BUTTON_SAVE BUTTON_SELECT
253 #define BUTTON_SAVE_NAME "SELECT"
255 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
256 #define SOKOBAN_LEFT BUTTON_LEFT
257 #define SOKOBAN_RIGHT BUTTON_RIGHT
258 #define SOKOBAN_UP BUTTON_UP
259 #define SOKOBAN_DOWN BUTTON_DOWN
260 #define SOKOBAN_MENU BUTTON_POWER
261 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
262 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
263 #define SOKOBAN_REDO BUTTON_HOME
264 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
265 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
266 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
267 #define SOKOBAN_PAUSE BUTTON_SELECT
268 #define BUTTON_SAVE BUTTON_SELECT
269 #define BUTTON_SAVE_NAME "SELECT"
271 #elif CONFIG_KEYPAD == SANSA_M200_PAD
272 #define SOKOBAN_LEFT BUTTON_LEFT
273 #define SOKOBAN_RIGHT BUTTON_RIGHT
274 #define SOKOBAN_UP BUTTON_UP
275 #define SOKOBAN_DOWN BUTTON_DOWN
276 #define SOKOBAN_MENU BUTTON_POWER
277 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
278 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
279 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_UP)
280 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
281 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
282 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
283 #define SOKOBAN_PAUSE BUTTON_SELECT
284 #define BUTTON_SAVE BUTTON_SELECT
285 #define BUTTON_SAVE_NAME "SELECT"
287 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
288 #define SOKOBAN_LEFT BUTTON_LEFT
289 #define SOKOBAN_RIGHT BUTTON_RIGHT
290 #define SOKOBAN_UP BUTTON_UP
291 #define SOKOBAN_DOWN BUTTON_DOWN
292 #define SOKOBAN_MENU BUTTON_MENU
293 #define SOKOBAN_UNDO BUTTON_VOL_UP
294 #define SOKOBAN_REDO BUTTON_VOL_DOWN
295 #define SOKOBAN_LEVEL_DOWN BUTTON_PREV
296 #define SOKOBAN_LEVEL_REPEAT BUTTON_PLAY
297 #define SOKOBAN_LEVEL_UP BUTTON_NEXT
298 #define SOKOBAN_PAUSE BUTTON_SELECT
299 #define BUTTON_SAVE BUTTON_SELECT
300 #define BUTTON_SAVE_NAME "SELECT"
302 #elif CONFIG_KEYPAD == MROBE100_PAD
303 #define SOKOBAN_LEFT BUTTON_LEFT
304 #define SOKOBAN_RIGHT BUTTON_RIGHT
305 #define SOKOBAN_UP BUTTON_UP
306 #define SOKOBAN_DOWN BUTTON_DOWN
307 #define SOKOBAN_MENU BUTTON_POWER
308 #define SOKOBAN_UNDO BUTTON_SELECT
309 #define SOKOBAN_REDO BUTTON_MENU
310 #define SOKOBAN_LEVEL_DOWN (BUTTON_DISPLAY | BUTTON_DOWN)
311 #define SOKOBAN_LEVEL_REPEAT (BUTTON_DISPLAY | BUTTON_RIGHT)
312 #define SOKOBAN_LEVEL_UP (BUTTON_DISPLAY | BUTTON_UP)
313 #define SOKOBAN_PAUSE BUTTON_SELECT
314 #define BUTTON_SAVE BUTTON_SELECT
315 #define BUTTON_SAVE_NAME "SELECT"
317 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
318 #define SOKOBAN_LEFT BUTTON_RC_REW
319 #define SOKOBAN_RIGHT BUTTON_RC_FF
320 #define SOKOBAN_UP BUTTON_RC_VOL_UP
321 #define SOKOBAN_DOWN BUTTON_RC_VOL_DOWN
322 #define SOKOBAN_MENU BUTTON_RC_REC
323 #define SOKOBAN_UNDO BUTTON_RC_MODE
324 #define SOKOBAN_REDO BUTTON_RC_MENU
325 #define SOKOBAN_PAUSE BUTTON_RC_PLAY
326 #define BUTTON_SAVE BUTTON_RC_PLAY
327 #define BUTTON_SAVE_NAME "PLAY"
329 #define SOKOBAN_RC_MENU BUTTON_REC
331 #elif CONFIG_KEYPAD == COWOND2_PAD
332 #define SOKOBAN_MENU BUTTON_MENU
333 #define SOKOBAN_MENU_NAME "[MENU]"
335 #elif CONFIG_KEYPAD == IAUDIO67_PAD
336 #define SOKOBAN_LEFT BUTTON_LEFT
337 #define SOKOBAN_RIGHT BUTTON_RIGHT
338 #define SOKOBAN_UP BUTTON_STOP
339 #define SOKOBAN_DOWN BUTTON_PLAY
340 #define SOKOBAN_MENU BUTTON_MENU
341 #define SOKOBAN_UNDO BUTTON_VOLDOWN
342 #define SOKOBAN_REDO BUTTON_VOLUP
343 #define SOKOBAN_PAUSE (BUTTON_MENU|BUTTON_LEFT)
344 #define BUTTON_SAVE (BUTTON_MENU|BUTTON_PLAY)
345 #define BUTTON_SAVE_NAME "MENU+PLAY"
347 #define SOKOBAN_RC_MENU (BUTTON_MENU|BUTTON_STOP)
349 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
350 #define SOKOBAN_LEFT BUTTON_LEFT
351 #define SOKOBAN_RIGHT BUTTON_RIGHT
352 #define SOKOBAN_UP BUTTON_UP
353 #define SOKOBAN_DOWN BUTTON_DOWN
354 #define SOKOBAN_MENU BUTTON_MENU
355 #define SOKOBAN_UNDO BUTTON_BACK
356 #define SOKOBAN_REDO (BUTTON_BACK | BUTTON_PLAY)
357 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
358 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
359 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
360 #define SOKOBAN_PAUSE BUTTON_PLAY
361 #define BUTTON_SAVE BUTTON_CUSTOM
362 #define BUTTON_SAVE_NAME "CUSTOM"
364 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
365 #define SOKOBAN_LEFT BUTTON_LEFT
366 #define SOKOBAN_RIGHT BUTTON_RIGHT
367 #define SOKOBAN_UP BUTTON_UP
368 #define SOKOBAN_DOWN BUTTON_DOWN
369 #define SOKOBAN_MENU BUTTON_MENU
370 #define SOKOBAN_UNDO BUTTON_VIEW
371 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_VIEW)
372 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
373 #define SOKOBAN_LEVEL_REPEAT BUTTON_POWER
374 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
375 #define SOKOBAN_PAUSE BUTTON_SELECT
376 #define BUTTON_SAVE BUTTON_PLAYLIST
377 #define BUTTON_SAVE_NAME "PLAYLIST"
379 #elif CONFIG_KEYPAD == ONDAVX747_PAD
380 #define SOKOBAN_MENU BUTTON_MENU
381 #define SOKOBAN_MENU_NAME "[MENU]"
383 #elif CONFIG_KEYPAD == MROBE500_PAD
385 #define SOKOBAN_MENU BUTTON_POWER
386 #define SOKOBAN_MENU_NAME "[POWER]"
388 #else
389 #error No keymap defined!
390 #endif
392 #ifdef HAVE_TOUCHSCREEN
393 #ifndef SOKOBAN_LEFT
394 #define SOKOBAN_LEFT BUTTON_MIDLEFT
395 #endif
396 #ifndef SOKOBAN_RIGHT
397 #define SOKOBAN_RIGHT BUTTON_MIDRIGHT
398 #endif
399 #ifndef SOKOBAN_UP
400 #define SOKOBAN_UP BUTTON_TOPMIDDLE
401 #endif
402 #ifndef SOKOBAN_DOWN
403 #define SOKOBAN_DOWN BUTTON_BOTTOMMIDDLE
404 #endif
405 #ifndef SOKOBAN_MENU
406 #define SOKOBAN_MENU BUTTON_TOPLEFT
407 #define SOKOBAN_MENU_NAME "[TOPLEFT]"
408 #endif
409 #ifndef SOKOBAN_UNDO
410 #define SOKOBAN_UNDO BUTTON_BOTTOMRIGHT
411 #define SOKOBAN_UNDO_NAME "[BOTTOMRIGHT]"
412 #endif
413 #ifndef SOKOBAN_REDO
414 #define SOKOBAN_REDO BUTTON_BOTTOMLEFT
415 #define SOKOBAN_REDO_NAME "[BOTTOMLEFT]"
416 #endif
417 #ifndef SOKOBAN_PAUSE
418 #define SOKOBAN_PAUSE BUTTON_CENTER
419 #define SOKOBAN_PAUSE_NAME "[CENTER]"
420 #endif
421 #ifndef SOKOBAN_LEVEL_REPEAT
422 #define SOKOBAN_LEVEL_REPEAT BUTTON_TOPRIGHT
423 #define SOKOBAN_LEVEL_REPEAT_NAME "[TOPRIGHT]"
424 #endif
425 #ifndef BUTTON_SAVE
426 #define BUTTON_SAVE BUTTON_CENTER
427 #define BUTTON_SAVE_NAME "CENTER"
428 #endif
429 #endif
431 #define SOKOBAN_FONT FONT_SYSFIXED
434 /* The Location, Undo and LevelInfo structs are OO-flavored.
435 * (oooh!-flavored as Schnueff puts it.) It makes more you have to know,
436 * but the overall data layout becomes more manageable. */
438 /* Level data & stats */
439 struct LevelInfo {
440 int index; /* Level index (level number - 1) */
441 int moves; /* Moves & pushes for the stats */
442 int pushes;
443 short boxes_to_go; /* Number of unplaced boxes remaining in level */
444 short height; /* Height & width for centering level display */
445 short width;
448 struct Location {
449 short row;
450 short col;
453 /* Our full undo history */
454 static struct UndoInfo {
455 int count; /* How many undos have been done */
456 int current; /* Which history is the current undo */
457 int max; /* Which history is the max redoable */
458 char history[MAX_UNDOS];
459 } undo_info;
461 /* Our playing board */
462 static struct BoardInfo {
463 char board[ROWS][COLS]; /* The current board data */
464 struct LevelInfo level; /* Level data & stats */
465 struct Location player; /* Where the player is */
466 int max_level; /* The number of levels we have */
467 } current_info;
469 static struct BufferedBoards {
470 char filename[MAX_PATH]; /* Filename of the levelset we're using */
471 char data[MAX_LEVEL_DATA]; /* Buffered level data */
472 int index[MAX_LEVELS + 1]; /* Where each buffered board begins & ends */
473 int start; /* Index of first buffered board */
474 int end; /* Index of last buffered board */
475 short prebuffered_boards; /* Number of boards before current to store */
476 } buffered_boards;
479 static char buf[ROWS*(COLS + 1)]; /* Enough for a whole board or a filename */
482 static void init_undo(void)
484 undo_info.count = 0;
485 undo_info.current = 0;
486 undo_info.max = 0;
489 static void get_delta(char direction, short *d_r, short *d_c)
491 switch (direction) {
492 case SOKOBAN_PUSH_LEFT:
493 case SOKOBAN_MOVE_LEFT:
494 *d_r = 0;
495 *d_c = -1;
496 break;
497 case SOKOBAN_PUSH_RIGHT:
498 case SOKOBAN_MOVE_RIGHT:
499 *d_r = 0;
500 *d_c = 1;
501 break;
502 case SOKOBAN_PUSH_UP:
503 case SOKOBAN_MOVE_UP:
504 *d_r = -1;
505 *d_c = 0;
506 break;
507 case SOKOBAN_PUSH_DOWN:
508 case SOKOBAN_MOVE_DOWN:
509 *d_r = 1;
510 *d_c = 0;
514 static bool undo(void)
516 char undo;
517 short r, c;
518 short d_r = 0, d_c = 0; /* delta row & delta col */
519 char *space_cur, *space_next, *space_prev;
520 bool undo_push = false;
522 /* If no more undos or we've wrapped all the way around, quit */
523 if (undo_info.count == 0 || undo_info.current - 1 == undo_info.max)
524 return false;
526 /* Move to previous undo in the list */
527 if (undo_info.current == 0 && undo_info.count > 1)
528 undo_info.current = MAX_UNDOS - 1;
529 else
530 undo_info.current--;
532 undo_info.count--;
534 undo = undo_info.history[undo_info.current];
536 if (undo < SOKOBAN_MOVE_MIN)
537 undo_push = true;
539 get_delta(undo, &d_r, &d_c);
541 r = current_info.player.row;
542 c = current_info.player.col;
544 /* Give the 3 spaces we're going to use better names */
545 space_cur = &current_info.board[r][c];
546 space_next = &current_info.board[r + d_r][c + d_c];
547 space_prev = &current_info.board[r - d_r][c - d_c];
549 /* Update board info */
550 if (undo_push) {
551 /* Moving box from goal to floor */
552 if (*space_next == '*' && *space_cur == '@')
553 current_info.level.boxes_to_go++;
554 /* Moving box from floor to goal */
555 else if (*space_next == '$' && *space_cur == '+')
556 current_info.level.boxes_to_go--;
558 /* Move box off of next space... */
559 *space_next = (*space_next == '*' ? '.' : ' ');
560 /* ...and on to current space */
561 *space_cur = (*space_cur == '+' ? '*' : '$');
563 current_info.level.pushes--;
564 } else
565 /* Just move player off of current space */
566 *space_cur = (*space_cur == '+' ? '.' : ' ');
567 /* Move player back to previous space */
568 *space_prev = (*space_prev == '.' ? '+' : '@');
570 /* Update position */
571 current_info.player.row -= d_r;
572 current_info.player.col -= d_c;
574 current_info.level.moves--;
576 return true;
579 static void add_undo(char undo)
581 undo_info.history[undo_info.current] = undo;
583 /* Wrap around if MAX_UNDOS exceeded */
584 if (undo_info.current < (MAX_UNDOS - 1))
585 undo_info.current++;
586 else
587 undo_info.current = 0;
589 if (undo_info.count < MAX_UNDOS)
590 undo_info.count++;
593 static bool move(char direction, bool redo)
595 short r, c;
596 short d_r = 0, d_c = 0; /* delta row & delta col */
597 char *space_cur, *space_next, *space_beyond;
598 bool push = false;
600 get_delta(direction, &d_r, &d_c);
602 r = current_info.player.row;
603 c = current_info.player.col;
605 /* Check for out-of-bounds */
606 if (r + 2*d_r < 0 || r + 2*d_r >= ROWS ||
607 c + 2*d_c < 0 || c + 2*d_c >= COLS)
608 return false;
610 /* Give the 3 spaces we're going to use better names */
611 space_cur = &current_info.board[r][c];
612 space_next = &current_info.board[r + d_r][c + d_c];
613 space_beyond = &current_info.board[r + 2*d_r][c + 2*d_c];
615 if (*space_next == '$' || *space_next == '*') {
616 /* Change direction from move to push for undo */
617 if (direction >= SOKOBAN_MOVE_MIN)
618 direction -= SOKOBAN_MOVE_DIFF;
619 push = true;
621 else if (direction < SOKOBAN_MOVE_MIN)
622 /* Change back to move if redo/solution playback push is invalid */
623 direction += SOKOBAN_MOVE_DIFF;
625 /* Update board info */
626 if (push) {
627 /* Moving box from goal to floor */
628 if (*space_next == '*' && *space_beyond == ' ')
629 current_info.level.boxes_to_go++;
630 /* Moving box from floor to goal */
631 else if (*space_next == '$' && *space_beyond == '.')
632 current_info.level.boxes_to_go--;
633 /* Check for invalid move */
634 else if (*space_beyond != '.' && *space_beyond != ' ')
635 return false;
637 /* Move player onto next space */
638 *space_next = (*space_next == '*' ? '+' : '@');
639 /* Move box onto space beyond next */
640 *space_beyond = (*space_beyond == '.' ? '*' : '$');
642 current_info.level.pushes++;
643 } else {
644 /* Check for invalid move */
645 if (*space_next != '.' && *space_next != ' ')
646 return false;
648 /* Move player onto next space */
649 *space_next = (*space_next == '.' ? '+' : '@');
651 /* Move player off of current space */
652 *space_cur = (*space_cur == '+' ? '.' : ' ');
654 /* Update position */
655 current_info.player.row += d_r;
656 current_info.player.col += d_c;
658 current_info.level.moves++;
660 /* Update undo_info.max to current on every normal move,
661 * except if it's the same as a redo. */
662 /* normal move and either */
663 if (!redo &&
664 /* moves have been undone... */
665 ((undo_info.max != undo_info.current &&
666 /* ...and the current move is NOT the same as the one in history */
667 undo_info.history[undo_info.current] != direction) ||
668 /* or moves have not been undone */
669 undo_info.max == undo_info.current)) {
670 add_undo(direction);
671 undo_info.max = undo_info.current;
672 } else /* redo move or move was same as redo */
673 add_undo(direction); /* add_undo to update current */
675 return true;
678 #ifdef SOKOBAN_REDO
679 static bool redo(void)
681 /* If no moves have been undone, quit */
682 if (undo_info.current == undo_info.max)
683 return false;
685 return move(undo_info.history[(undo_info.current < MAX_UNDOS ?
686 undo_info.current : 0)], true);
688 #endif
690 static void init_boards(void)
692 rb->strncpy(buffered_boards.filename, SOKOBAN_LEVELS_FILE, MAX_PATH);
694 current_info.level.index = 0;
695 current_info.player.row = 0;
696 current_info.player.col = 0;
697 current_info.max_level = 0;
699 buffered_boards.start = 0;
700 buffered_boards.end = 0;
701 buffered_boards.prebuffered_boards = 0;
703 init_undo();
706 static bool read_levels(bool initialize)
708 int fd = 0;
709 short len;
710 short lastlen = 0;
711 short row = 0;
712 int level_count = 0;
714 int i = 0;
715 int level_len = 0;
716 bool index_set = false;
718 /* Get the index of the first level to buffer */
719 if (current_info.level.index > buffered_boards.prebuffered_boards &&
720 !initialize)
721 buffered_boards.start = current_info.level.index -
722 buffered_boards.prebuffered_boards;
723 else
724 buffered_boards.start = 0;
726 if ((fd = rb->open(buffered_boards.filename, O_RDONLY)) < 0) {
727 rb->splashf(HZ*2, "Unable to open %s", buffered_boards.filename);
728 return false;
731 do {
732 len = rb->read_line(fd, buf, sizeof(buf));
734 /* Correct len when trailing \r's or \n's are counted */
735 if (len > 2 && buf[len - 2] == '\0')
736 len -= 2;
737 else if (len > 1 && buf[len - 1] == '\0')
738 len--;
740 /* Skip short lines & lines with non-level data */
741 if (len >= 3 && ((buf[0] >= '1' && buf[0] <= '9') || buf[0] == '#' ||
742 buf[0] == ' ' || buf[0] == '-' || buf[0] == '_')) {
743 if (level_count >= buffered_boards.start) {
744 /* Set the index of this level */
745 if (!index_set &&
746 level_count - buffered_boards.start < MAX_LEVELS) {
747 buffered_boards.index[level_count - buffered_boards.start]
748 = i;
749 index_set = true;
751 /* Copy buffer to board data */
752 if (i + level_len + len < MAX_LEVEL_DATA) {
753 rb->memcpy(&buffered_boards.data[i + level_len], buf, len);
754 buffered_boards.data[i + level_len + len] = '\n';
757 level_len += len + 1;
758 row++;
760 /* If newline & level is tall enough or is RLE */
761 } else if (buf[0] == '\0' && (row > 2 || lastlen > 22)) {
762 level_count++;
763 if (level_count >= buffered_boards.start) {
764 i += level_len;
765 if (i < MAX_LEVEL_DATA)
766 buffered_boards.end = level_count;
767 else if (!initialize)
768 break;
770 row = 0;
771 level_len = 0;
772 index_set = false;
774 } else if (len > 22)
775 len = 1;
777 } while ((lastlen = len));
779 /* Set the index of the end of the last level */
780 if (level_count - buffered_boards.start < MAX_LEVELS)
781 buffered_boards.index[level_count - buffered_boards.start] = i;
783 if (initialize) {
784 current_info.max_level = level_count;
785 buffered_boards.prebuffered_boards = buffered_boards.end/2;
788 rb->close(fd);
790 return true;
793 static void load_level(void)
795 int c, r;
796 int i, n;
797 int level_size;
798 int index = current_info.level.index - buffered_boards.start;
799 char *level;
801 /* Get the buffered board index of the current level */
802 if (current_info.level.index < buffered_boards.start ||
803 current_info.level.index >= buffered_boards.end) {
804 read_levels(false);
805 if (current_info.level.index > buffered_boards.prebuffered_boards)
806 index = buffered_boards.prebuffered_boards;
807 else
808 index = current_info.level.index;
810 level = &buffered_boards.data[buffered_boards.index[index]];
812 /* Reset level info */
813 current_info.level.moves = 0;
814 current_info.level.pushes = 0;
815 current_info.level.boxes_to_go = 0;
816 current_info.level.width = 0;
818 /* Clear board */
819 for (r = 0; r < ROWS; r++)
820 for (c = 0; c < COLS; c++)
821 current_info.board[r][c] = 'X';
823 level_size = buffered_boards.index[index + 1] -
824 buffered_boards.index[index];
826 for (r = 0, c = 0, n = 1, i = 0; i < level_size; i++) {
827 if (level[i] == '\n' || level[i] == '|') {
828 if (c > 3) {
829 /* Update max width of level & go to next row */
830 if (c > current_info.level.width)
831 current_info.level.width = c;
832 c = 0;
833 r++;
834 if (r >= ROWS)
835 break;
837 } else if (c < COLS) {
838 /* Read RLE character's length into n */
839 if (level[i] >= '0' && level[i] <= '9') {
840 n = level[i++] - '0';
841 if (level[i] >= '0' && level[i] <= '9')
842 n = n*10 + level[i++] - '0';
845 /* Cleanup & replace */
846 if (level[i] == '%')
847 level[i] = '*';
848 else if (level[i] == '-' || level[i] == '_')
849 level[i] = ' ';
851 if (n > 1) {
852 if (c + n >= COLS)
853 n = COLS - c;
855 if (level[i] == '.')
856 current_info.level.boxes_to_go += n;
858 /* Put RLE character n times */
859 while (n--)
860 current_info.board[r][c++] = level[i];
861 n = 1;
863 } else {
864 if (level[i] == '.' || level[i] == '+')
865 current_info.level.boxes_to_go++;
867 if (level[i] == '@' ||level[i] == '+') {
868 current_info.player.row = r;
869 current_info.player.col = c;
872 current_info.board[r][c++] = level[i];
877 current_info.level.height = r;
879 #if LCD_DEPTH > 2
880 /* Fill in blank space outside level on color targets */
881 for (r = 0; r < ROWS; r++)
882 for (c = 0; current_info.board[r][c] == ' ' && c < COLS; c++)
883 current_info.board[r][c] = 'X';
885 for (c = 0; c < COLS; c++) {
886 for (r = 0; (current_info.board[r][c] == ' ' ||
887 current_info.board[r][c] == 'X') && r < ROWS; r++)
888 current_info.board[r][c] = 'X';
889 for (r = ROWS - 1; (current_info.board[r][c] == ' ' ||
890 current_info.board[r][c] == 'X') && r >= 0; r--)
891 current_info.board[r][c] = 'X';
893 #endif
896 static void update_screen(void)
898 int c, r;
899 int rows, cols;
901 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) < 32
902 #define STAT_HEIGHT 25
903 #define STAT_X (LCD_WIDTH - 120)/2
904 #define STAT_Y (LCD_HEIGHT - STAT_HEIGHT)
905 #define BOARD_WIDTH LCD_WIDTH
906 #define BOARD_HEIGHT (LCD_HEIGHT - STAT_HEIGHT)
907 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 4, "Level");
908 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
909 rb->lcd_putsxy(STAT_X + 7, STAT_Y + 14, buf);
910 rb->lcd_putsxy(STAT_X + 41, STAT_Y + 4, "Moves");
911 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
912 rb->lcd_putsxy(STAT_X + 44, STAT_Y + 14, buf);
913 rb->lcd_putsxy(STAT_X + 79, STAT_Y + 4, "Pushes");
914 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
915 rb->lcd_putsxy(STAT_X + 82, STAT_Y + 14, buf);
917 rb->lcd_drawrect(STAT_X, STAT_Y, 38, STAT_HEIGHT);
918 rb->lcd_drawrect(STAT_X + 37, STAT_Y, 39, STAT_HEIGHT);
919 rb->lcd_drawrect(STAT_X + 75, STAT_Y, 45, STAT_HEIGHT);
920 #else
921 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) > 40
922 #define STAT_X (LCD_WIDTH - 40)
923 #else
924 #define STAT_X COLS*SOKOBAN_TILESIZE
925 #endif
926 #define STAT_Y (LCD_HEIGHT - 64)/2
927 #define STAT_WIDTH (LCD_WIDTH - STAT_X)
928 #define BOARD_WIDTH (LCD_WIDTH - STAT_WIDTH)
929 #define BOARD_HEIGHT LCD_HEIGHT
930 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 2, "Level");
931 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
932 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 12, buf);
933 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 23, "Moves");
934 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
935 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 33, buf);
936 #if STAT_WIDTH < 38
937 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Push");
938 #else
939 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Pushes");
940 #endif
941 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
942 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 54, buf);
944 rb->lcd_drawrect(STAT_X, STAT_Y + 0, STAT_WIDTH, 64);
945 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 21);
946 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 42);
948 #endif
950 /* load the board to the screen */
951 for (rows = 0; rows < ROWS; rows++) {
952 for (cols = 0; cols < COLS; cols++) {
953 c = cols*SOKOBAN_TILESIZE +
954 (BOARD_WIDTH - current_info.level.width*SOKOBAN_TILESIZE)/2;
955 r = rows*SOKOBAN_TILESIZE +
956 (BOARD_HEIGHT - current_info.level.height*SOKOBAN_TILESIZE)/2;
958 switch(current_info.board[rows][cols]) {
959 case 'X': /* blank space outside of level */
960 break;
962 case ' ': /* floor */
963 rb->lcd_bitmap_part(sokoban_tiles, 0, 0*SOKOBAN_TILESIZE,
964 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
965 SOKOBAN_TILESIZE);
966 break;
968 case '#': /* wall */
969 rb->lcd_bitmap_part(sokoban_tiles, 0, 1*SOKOBAN_TILESIZE,
970 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
971 SOKOBAN_TILESIZE);
972 break;
974 case '$': /* box */
975 rb->lcd_bitmap_part(sokoban_tiles, 0, 2*SOKOBAN_TILESIZE,
976 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
977 SOKOBAN_TILESIZE);
978 break;
980 case '*': /* box on goal */
981 rb->lcd_bitmap_part(sokoban_tiles, 0, 3*SOKOBAN_TILESIZE,
982 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
983 SOKOBAN_TILESIZE);
984 break;
986 case '.': /* goal */
987 rb->lcd_bitmap_part(sokoban_tiles, 0, 4*SOKOBAN_TILESIZE,
988 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
989 SOKOBAN_TILESIZE);
990 break;
992 case '@': /* player */
993 rb->lcd_bitmap_part(sokoban_tiles, 0, 5*SOKOBAN_TILESIZE,
994 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
995 SOKOBAN_TILESIZE);
996 break;
998 case '+': /* player on goal */
999 rb->lcd_bitmap_part(sokoban_tiles, 0, 6*SOKOBAN_TILESIZE,
1000 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
1001 SOKOBAN_TILESIZE);
1002 break;
1007 /* print out the screen */
1008 rb->lcd_update();
1011 static void draw_level(void)
1013 load_level();
1014 rb->lcd_clear_display();
1015 update_screen();
1018 static bool save(char *filename, bool solution)
1020 int fd;
1021 char *loc;
1022 DIR *dir;
1023 char dirname[MAX_PATH];
1025 rb->splash(0, "Saving...");
1027 /* Create dir if it doesn't exist */
1028 if ((loc = rb->strrchr(filename, '/')) != NULL) {
1029 rb->strncpy(dirname, filename, loc - filename);
1030 dirname[loc - filename] = '\0';
1031 if(!(dir = rb->opendir(dirname)))
1032 rb->mkdir(dirname);
1033 else
1034 rb->closedir(dir);
1037 if (filename[0] == '\0' ||
1038 (fd = rb->open(filename, O_WRONLY|O_CREAT|O_TRUNC)) < 0) {
1039 rb->splashf(HZ*2, "Unable to open %s", filename);
1040 return false;
1043 /* Sokoban: S/P for solution/progress : level number : current undo */
1044 rb->snprintf(buf, sizeof(buf), "Sokoban:%c:%d:%d\n", (solution ? 'S' : 'P'),
1045 current_info.level.index + 1, undo_info.current);
1046 rb->write(fd, buf, rb->strlen(buf));
1048 /* Filename of levelset */
1049 rb->write(fd, buffered_boards.filename,
1050 rb->strlen(buffered_boards.filename));
1051 rb->write(fd, "\n", 1);
1053 /* Full undo history */
1054 rb->write(fd, undo_info.history, undo_info.max);
1056 rb->close(fd);
1058 return true;
1061 static bool load(char *filename, bool silent)
1063 int fd;
1064 int i, n;
1065 int len;
1066 int button;
1067 bool play_solution;
1068 bool paused = false;
1069 unsigned short speed = 2;
1070 int delay[] = {HZ/2, HZ/3, HZ/4, HZ/6, HZ/8, HZ/12, HZ/16, HZ/25};
1072 if (filename[0] == '\0' || (fd = rb->open(filename, O_RDONLY)) < 0) {
1073 if (!silent)
1074 rb->splashf(HZ*2, "Unable to open %s", filename);
1075 return false;
1078 /* Read header, level number, & current undo */
1079 rb->read_line(fd, buf, sizeof(buf));
1081 /* If we're opening a level file, not a solution/progress file */
1082 if (rb->strncmp(buf, "Sokoban", 7) != 0) {
1083 rb->close(fd);
1085 rb->strncpy(buffered_boards.filename, filename, MAX_PATH);
1086 if (!read_levels(true))
1087 return false;
1089 current_info.level.index = 0;
1090 load_level();
1092 /* If there aren't any boxes to go or the player position wasn't set,
1093 * the file probably wasn't a Sokoban level file */
1094 if (current_info.level.boxes_to_go == 0 ||
1095 current_info.player.row == 0 || current_info.player.col == 0) {
1096 if (!silent)
1097 rb->splash(HZ*2, "File is not a Sokoban level file");
1098 return false;
1101 } else {
1103 /* Read filename of levelset */
1104 rb->read_line(fd, buffered_boards.filename,
1105 sizeof(buffered_boards.filename));
1107 /* Read full undo history */
1108 len = rb->read_line(fd, undo_info.history, MAX_UNDOS);
1110 /* Correct len when trailing \r's or \n's are counted */
1111 if (len > 2 && undo_info.history[len - 2] == '\0')
1112 len -= 2;
1113 else if (len > 1 && undo_info.history[len - 1] == '\0')
1114 len--;
1116 rb->close(fd);
1118 /* Check to see if we're going to play a solution or resume progress */
1119 play_solution = (buf[8] == 'S');
1121 /* Get level number */
1122 for (n = 0, i = 10; buf[i] >= '0' && buf[i] <= '9' && i < 15; i++)
1123 n = n*10 + buf[i] - '0';
1124 current_info.level.index = n - 1;
1126 /* Get undo index */
1127 for (n = 0, i++; buf[i] >= '0' && buf[i] <= '9' && i < 21; i++)
1128 n = n*10 + buf[i] - '0';
1129 if (n > len)
1130 n = len;
1131 undo_info.max = len;
1133 if (current_info.level.index < 0) {
1134 if (!silent)
1135 rb->splash(HZ*2, "Error loading level");
1136 return false;
1138 if (!read_levels(true))
1139 return false;
1140 if (current_info.level.index >= current_info.max_level) {
1141 if (!silent)
1142 rb->splash(HZ*2, "Error loading level");
1143 return false;
1146 load_level();
1148 if (play_solution) {
1149 rb->lcd_clear_display();
1150 update_screen();
1151 rb->sleep(2*delay[speed]);
1153 /* Replay solution until menu button is pressed */
1154 i = 0;
1155 while (true) {
1156 if (i < len) {
1157 if (!move(undo_info.history[i], true)) {
1158 n = i;
1159 break;
1161 rb->lcd_clear_display();
1162 update_screen();
1163 i++;
1164 } else
1165 paused = true;
1167 rb->sleep(delay[speed]);
1169 while ((button = rb->button_get(false)) || paused) {
1170 switch (button) {
1171 case SOKOBAN_MENU:
1172 /* Pretend the level is complete so we'll quit */
1173 current_info.level.boxes_to_go = 0;
1174 return true;
1176 case SOKOBAN_PAUSE:
1177 /* Toggle pause state */
1178 paused = !paused;
1179 break;
1181 case SOKOBAN_LEFT:
1182 case SOKOBAN_LEFT | BUTTON_REPEAT:
1183 /* Go back one move */
1184 if (paused) {
1185 if (undo())
1186 i--;
1187 rb->lcd_clear_display();
1188 update_screen();
1190 break;
1192 case SOKOBAN_RIGHT:
1193 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1194 /* Go forward one move */
1195 if (paused) {
1196 if (redo())
1197 i++;
1198 rb->lcd_clear_display();
1199 update_screen();
1201 break;
1203 case SOKOBAN_UP:
1204 case SOKOBAN_UP | BUTTON_REPEAT:
1205 /* Speed up */
1206 if (speed < sizeof(delay)/sizeof(int) - 1)
1207 speed++;
1208 break;
1210 case SOKOBAN_DOWN:
1211 case SOKOBAN_DOWN | BUTTON_REPEAT:
1212 /* Slow down */
1213 if (speed > 0)
1214 speed--;
1217 if (paused)
1218 rb->sleep(HZ/33);
1222 /* If level is complete, wait for keypress before quitting */
1223 if (current_info.level.boxes_to_go == 0)
1224 rb->button_get(true);
1226 } else {
1227 /* Advance to current undo */
1228 for (i = 0; i < n; i++) {
1229 if (!move(undo_info.history[i], true)) {
1230 n = i;
1231 break;
1235 rb->button_clear_queue();
1236 rb->lcd_clear_display();
1239 undo_info.current = n;
1242 return true;
1245 static int sokoban_menu(void)
1247 int button;
1248 int selection = 0;
1249 int i;
1250 bool menu_quit;
1251 int start_selected = 0;
1252 int prev_level = current_info.level.index;
1254 MENUITEM_STRINGLIST(menu, "Sokoban Menu", NULL,
1255 "Resume", "Select Level", "Audio Playback", "Keys",
1256 "Load Default Level Set", "Quit Without Saving",
1257 "Save Progress & Quit");
1259 do {
1260 menu_quit = true;
1261 selection = rb->do_menu(&menu, &start_selected, NULL, false);
1263 switch (selection) {
1264 case 0: /* Resume */
1265 break;
1267 case 1: /* Select level */
1268 current_info.level.index++;
1269 rb->set_int("Select Level", "", UNIT_INT,
1270 &current_info.level.index, NULL, 1, 1,
1271 current_info.max_level, NULL);
1272 current_info.level.index--;
1273 if (prev_level != current_info.level.index) {
1274 init_undo();
1275 draw_level();
1276 } else
1277 menu_quit = false;
1278 break;
1280 case 2: /* Audio playback control */
1281 playback_control(NULL);
1282 menu_quit = false;
1283 break;
1285 case 3: /* Keys */
1286 FOR_NB_SCREENS(i)
1287 rb->screens[i]->clear_display();
1288 rb->lcd_setfont(SOKOBAN_FONT);
1290 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
1291 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
1292 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1293 rb->lcd_putsxy(3, 16, "[ON] Undo");
1294 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1295 rb->lcd_putsxy(3, 36, "[F1] Down a Level");
1296 rb->lcd_putsxy(3, 46, "[F2] Restart Level");
1297 rb->lcd_putsxy(3, 56, "[F3] Up a Level");
1298 #elif CONFIG_KEYPAD == ONDIO_PAD
1299 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1300 rb->lcd_putsxy(3, 16, "[MODE] Undo");
1301 rb->lcd_putsxy(3, 26, "[MODE+DOWN] Redo");
1302 rb->lcd_putsxy(3, 36, "[MODE+LEFT] Previous Level");
1303 rb->lcd_putsxy(3, 46, "[MODE+UP] Restart Level");
1304 rb->lcd_putsxy(3, 56, "[MODE+RIGHT] Up Level");
1305 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
1306 (CONFIG_KEYPAD == IRIVER_H300_PAD)
1307 rb->lcd_putsxy(3, 6, "[STOP] Menu");
1308 rb->lcd_putsxy(3, 16, "[REC] Undo");
1309 rb->lcd_putsxy(3, 26, "[MODE] Redo");
1310 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1311 rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
1312 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1313 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1314 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1315 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1316 rb->lcd_putsxy(3, 6, "[SELECT+MENU] Menu");
1317 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1318 rb->lcd_putsxy(3, 26, "[SELECT+PLAY] Redo");
1319 rb->lcd_putsxy(3, 36, "[SELECT+LEFT] Previous Level");
1320 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Next Level");
1321 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
1322 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1323 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1324 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1325 rb->lcd_putsxy(3, 36, "[REC] Restart Level");
1326 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1327 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1328 rb->lcd_putsxy(3, 16, "[REW] Undo");
1329 rb->lcd_putsxy(3, 26, "[FF] Redo");
1330 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1331 rb->lcd_putsxy(3, 46, "[PLAY+RIGHT] Restart Level");
1332 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1333 #elif CONFIG_KEYPAD == GIGABEAT_PAD
1334 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1335 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1336 rb->lcd_putsxy(3, 26, "[A] Redo");
1337 rb->lcd_putsxy(3, 36, "[VOL-] Previous Level");
1338 rb->lcd_putsxy(3, 46, "[MENU] Restart Level");
1339 rb->lcd_putsxy(3, 56, "[VOL+] Next Level");
1340 #elif CONFIG_KEYPAD == SANSA_E200_PAD
1341 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1342 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1343 rb->lcd_putsxy(3, 26, "[REC] Redo");
1344 rb->lcd_putsxy(3, 36, "[SELECT+DOWN] Previous Level");
1345 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Restart Level");
1346 rb->lcd_putsxy(3, 56, "[SELECT+UP] Next Level");
1347 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
1348 rb->lcd_putsxy(3, 6, "[MENU] Menu");
1349 rb->lcd_putsxy(3, 16, "[VOL+] Undo");
1350 rb->lcd_putsxy(3, 26, "[VOL-] Redo");
1351 rb->lcd_putsxy(3, 36, "[PREV] Previous Level");
1352 rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
1353 rb->lcd_putsxy(3, 56, "[NEXT] Next Level");
1354 #endif
1356 #ifdef HAVE_TOUCHSCREEN
1357 rb->lcd_putsxy(3, 6, SOKOBAN_MENU_NAME " Menu");
1358 rb->lcd_putsxy(3, 16, SOKOBAN_UNDO_NAME " Undo");
1359 rb->lcd_putsxy(3, 26, SOKOBAN_REDO_NAME " Redo");
1360 rb->lcd_putsxy(3, 36, SOKOBAN_PAUSE_NAME " Pause");
1361 rb->lcd_putsxy(3, 46, SOKOBAN_LEVEL_REPEAT_NAME " Restart Level");
1362 #endif
1364 FOR_NB_SCREENS(i)
1365 rb->screens[i]->update();
1367 /* Display until keypress */
1368 do {
1369 rb->sleep(HZ/20);
1370 button = rb->button_get(false);
1371 } while (!button || button & BUTTON_REL ||
1372 button & BUTTON_REPEAT);
1374 menu_quit = false;
1375 break;
1377 case 4: /* Load default levelset */
1378 init_boards();
1379 if (!read_levels(true))
1380 return 5; /* Quit */
1381 load_level();
1382 break;
1384 case 5: /* Quit */
1385 break;
1387 case 6: /* Save & quit */
1388 save(SOKOBAN_SAVE_FILE, false);
1389 rb->reload_directory();
1392 } while (!menu_quit);
1394 /* Restore font */
1395 rb->lcd_setfont(SOKOBAN_FONT);
1397 FOR_NB_SCREENS(i) {
1398 rb->screens[i]->clear_display();
1399 rb->screens[i]->update();
1402 return selection;
1405 static bool sokoban_loop(void)
1407 bool moved;
1408 int i = 0, button = 0, lastbutton = 0;
1409 short r = 0, c = 0;
1410 int w, h;
1411 char *loc;
1413 while (true) {
1414 moved = false;
1416 r = current_info.player.row;
1417 c = current_info.player.col;
1419 button = rb->button_get(true);
1421 switch(button)
1423 #ifdef SOKOBAN_RC_MENU
1424 case SOKOBAN_RC_MENU:
1425 #endif
1426 case SOKOBAN_MENU:
1427 switch (sokoban_menu()) {
1428 case 5: /* Quit */
1429 case 6: /* Save & quit */
1430 return PLUGIN_OK;
1432 update_screen();
1433 break;
1435 case SOKOBAN_UNDO:
1436 #ifdef SOKOBAN_UNDO_PRE
1437 if (lastbutton != SOKOBAN_UNDO_PRE)
1438 break;
1439 #else /* repeat can't work here for Ondio, iPod, et al */
1440 case SOKOBAN_UNDO | BUTTON_REPEAT:
1441 #endif
1442 undo();
1443 rb->lcd_clear_display();
1444 update_screen();
1445 break;
1447 #ifdef SOKOBAN_REDO
1448 case SOKOBAN_REDO:
1449 case SOKOBAN_REDO | BUTTON_REPEAT:
1450 moved = redo();
1451 rb->lcd_clear_display();
1452 update_screen();
1453 break;
1454 #endif
1456 #ifdef SOKOBAN_LEVEL_UP
1457 case SOKOBAN_LEVEL_UP:
1458 case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
1459 /* next level */
1460 init_undo();
1461 if (current_info.level.index + 1 < current_info.max_level)
1462 current_info.level.index++;
1464 draw_level();
1465 break;
1466 #endif
1468 #ifdef SOKOBAN_LEVEL_DOWN
1469 case SOKOBAN_LEVEL_DOWN:
1470 case SOKOBAN_LEVEL_DOWN | BUTTON_REPEAT:
1471 /* previous level */
1472 init_undo();
1473 if (current_info.level.index > 0)
1474 current_info.level.index--;
1476 draw_level();
1477 break;
1478 #endif
1480 #ifdef SOKOBAN_LEVEL_REPEAT
1481 case SOKOBAN_LEVEL_REPEAT:
1482 case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
1483 /* same level */
1484 init_undo();
1485 draw_level();
1486 break;
1487 #endif
1489 case SOKOBAN_LEFT:
1490 case SOKOBAN_LEFT | BUTTON_REPEAT:
1491 moved = move(SOKOBAN_MOVE_LEFT, false);
1492 break;
1494 case SOKOBAN_RIGHT:
1495 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1496 moved = move(SOKOBAN_MOVE_RIGHT, false);
1497 break;
1499 case SOKOBAN_UP:
1500 case SOKOBAN_UP | BUTTON_REPEAT:
1501 moved = move(SOKOBAN_MOVE_UP, false);
1502 break;
1504 case SOKOBAN_DOWN:
1505 case SOKOBAN_DOWN | BUTTON_REPEAT:
1506 moved = move(SOKOBAN_MOVE_DOWN, false);
1507 break;
1509 default:
1510 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
1511 return PLUGIN_USB_CONNECTED;
1512 break;
1515 lastbutton = button;
1517 if (moved) {
1518 rb->lcd_clear_display();
1519 update_screen();
1522 /* We have completed this level */
1523 if (current_info.level.boxes_to_go == 0) {
1525 if (moved) {
1526 rb->lcd_clear_display();
1528 /* Show level complete message & stats */
1529 rb->snprintf(buf, sizeof(buf), "Level %d Complete!",
1530 current_info.level.index + 1);
1531 rb->lcd_getstringsize(buf, &w, &h);
1532 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h*3, buf);
1534 rb->snprintf(buf, sizeof(buf), "%4d Moves ",
1535 current_info.level.moves);
1536 rb->lcd_getstringsize(buf, &w, &h);
1537 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h, buf);
1539 rb->snprintf(buf, sizeof(buf), "%4d Pushes",
1540 current_info.level.pushes);
1541 rb->lcd_getstringsize(buf, &w, &h);
1542 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2, buf);
1544 if (undo_info.count < MAX_UNDOS) {
1545 rb->snprintf(buf, sizeof(buf), "%s: Save solution",
1546 BUTTON_SAVE_NAME);
1547 rb->lcd_getstringsize(buf, &w, &h);
1548 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + h*2, buf);
1551 rb->lcd_update();
1552 rb->sleep(HZ/4);
1553 rb->button_clear_queue();
1555 /* Display for 4 seconds or until new keypress */
1556 for (i = 0; i < 80; i++) {
1557 rb->sleep(HZ/20);
1558 button = rb->button_get(false);
1559 if (button && !(button & BUTTON_REL) &&
1560 !(button & BUTTON_REPEAT))
1561 break;
1564 if (button == BUTTON_SAVE) {
1565 if (undo_info.count < MAX_UNDOS) {
1566 /* Set filename to current levelset plus level number
1567 * and .sok extension. Use SAVE_FOLDER if using the
1568 * default levelset, since it's in a hidden folder. */
1569 if (rb->strcmp(buffered_boards.filename,
1570 SOKOBAN_LEVELS_FILE) == 0) {
1571 rb->snprintf(buf, sizeof(buf),
1572 "%s/sokoban.%d.sok",
1573 SOKOBAN_SAVE_FOLDER,
1574 current_info.level.index + 1);
1575 } else {
1576 if ((loc = rb->strrchr(buffered_boards.filename,
1577 '.')) != NULL)
1578 *loc = '\0';
1579 rb->snprintf(buf, sizeof(buf), "%s.%d.sok",
1580 buffered_boards.filename,
1581 current_info.level.index + 1);
1582 if (loc != NULL)
1583 *loc = '.';
1586 if (!rb->kbd_input(buf, MAX_PATH))
1587 save(buf, true);
1588 } else
1589 rb->splash(HZ*2, "Solution too long to save");
1591 rb->lcd_setfont(SOKOBAN_FONT); /* Restore font */
1595 FOR_NB_SCREENS(i) {
1596 rb->screens[i]->clear_display();
1597 rb->screens[i]->update();
1600 current_info.level.index++;
1602 /* clear undo stats */
1603 init_undo();
1605 if (current_info.level.index >= current_info.max_level) {
1606 /* Show levelset complete message */
1607 rb->snprintf(buf, sizeof(buf), "You WIN!!");
1608 rb->lcd_getstringsize(buf, &w, &h);
1609 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, buf);
1611 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1612 /* Display for 4 seconds or until keypress */
1613 for (i = 0; i < 80; i++) {
1614 rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
1615 rb->lcd_update();
1616 rb->sleep(HZ/10);
1618 button = rb->button_get(false);
1619 if (button && !(button & BUTTON_REL))
1620 break;
1622 rb->lcd_set_drawmode(DRMODE_SOLID);
1624 /* Reset to first level & show quit menu */
1625 current_info.level.index = 0;
1627 switch (sokoban_menu()) {
1628 case 5: /* Quit */
1629 case 6: /* Save & quit */
1630 return PLUGIN_OK;
1634 load_level();
1635 update_screen();
1638 } /* end while */
1640 return PLUGIN_OK;
1644 enum plugin_status plugin_start(const void* parameter)
1646 int w, h;
1648 (void)(parameter);
1650 rb->lcd_setfont(SOKOBAN_FONT);
1652 rb->lcd_clear_display();
1653 rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
1654 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, SOKOBAN_TITLE);
1655 rb->lcd_update();
1656 rb->sleep(HZ); /* Show title for 1 second */
1658 init_boards();
1660 if (parameter == NULL) {
1661 /* Attempt to resume saved progress, otherwise start at beginning */
1662 if (!load(SOKOBAN_SAVE_FILE, true)) {
1663 init_boards();
1664 if (!read_levels(true))
1665 return PLUGIN_OK;
1666 load_level();
1669 } else {
1670 /* The plugin is being used to open a file */
1671 if (load((char*) parameter, false)) {
1672 /* If we loaded & played a solution, quit */
1673 if (current_info.level.boxes_to_go == 0)
1674 return PLUGIN_OK;
1675 } else
1676 return PLUGIN_OK;
1679 rb->lcd_clear_display();
1680 update_screen();
1682 return sokoban_loop();