Commit FS#9617 - Keymaps for Plugins fuze by Thomas Martitz.
[kugel-rb.git] / apps / plugins / sokoban.c
blob98faf7961fa591b9d86715de29c327c338ee2bdf
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_POWER
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 #else
365 #error No keymap defined!
366 #endif
368 #ifdef HAVE_TOUCHSCREEN
369 #ifndef SOKOBAN_LEFT
370 #define SOKOBAN_LEFT BUTTON_MIDLEFT
371 #endif
372 #ifndef SOKOBAN_RIGHT
373 #define SOKOBAN_RIGHT BUTTON_MIDRIGHT
374 #endif
375 #ifndef SOKOBAN_UP
376 #define SOKOBAN_UP BUTTON_TOPMIDDLE
377 #endif
378 #ifndef SOKOBAN_DOWN
379 #define SOKOBAN_DOWN BUTTON_BOTTOMMIDDLE
380 #endif
381 #ifndef SOKOBAN_MENU
382 #define SOKOBAN_MENU BUTTON_TOPLEFT
383 #define SOKOBAN_MENU_NAME "[TOPLEFT]"
384 #endif
385 #ifndef SOKOBAN_UNDO
386 #define SOKOBAN_UNDO BUTTON_BOTTOMRIGHT
387 #define SOKOBAN_UNDO_NAME "[BOTTOMRIGHT]"
388 #endif
389 #ifndef SOKOBAN_REDO
390 #define SOKOBAN_REDO BUTTON_BOTTOMLEFT
391 #define SOKOBAN_REDO_NAME "[BOTTOMLEFT]"
392 #endif
393 #ifndef SOKOBAN_PAUSE
394 #define SOKOBAN_PAUSE BUTTON_CENTER
395 #define SOKOBAN_PAUSE_NAME "[CENTER]"
396 #endif
397 #ifndef SOKOBAN_LEVEL_REPEAT
398 #define SOKOBAN_LEVEL_REPEAT BUTTON_TOPRIGHT
399 #define SOKOBAN_LEVEL_REPEAT_NAME "[TOPRIGHT]"
400 #endif
401 #ifndef BUTTON_SAVE
402 #define BUTTON_SAVE BUTTON_CENTER
403 #define BUTTON_SAVE_NAME "CENTER"
404 #endif
405 #endif
407 #define SOKOBAN_FONT FONT_SYSFIXED
410 /* The Location, Undo and LevelInfo structs are OO-flavored.
411 * (oooh!-flavored as Schnueff puts it.) It makes more you have to know,
412 * but the overall data layout becomes more manageable. */
414 /* Level data & stats */
415 struct LevelInfo {
416 int index; /* Level index (level number - 1) */
417 int moves; /* Moves & pushes for the stats */
418 int pushes;
419 short boxes_to_go; /* Number of unplaced boxes remaining in level */
420 short height; /* Height & width for centering level display */
421 short width;
424 struct Location {
425 short row;
426 short col;
429 /* Our full undo history */
430 static struct UndoInfo {
431 int count; /* How many undos have been done */
432 int current; /* Which history is the current undo */
433 int max; /* Which history is the max redoable */
434 char history[MAX_UNDOS];
435 } undo_info;
437 /* Our playing board */
438 static struct BoardInfo {
439 char board[ROWS][COLS]; /* The current board data */
440 struct LevelInfo level; /* Level data & stats */
441 struct Location player; /* Where the player is */
442 int max_level; /* The number of levels we have */
443 } current_info;
445 static struct BufferedBoards {
446 char filename[MAX_PATH]; /* Filename of the levelset we're using */
447 char data[MAX_LEVEL_DATA]; /* Buffered level data */
448 int index[MAX_LEVELS + 1]; /* Where each buffered board begins & ends */
449 int start; /* Index of first buffered board */
450 int end; /* Index of last buffered board */
451 short prebuffered_boards; /* Number of boards before current to store */
452 } buffered_boards;
455 static const struct plugin_api* rb;
456 MEM_FUNCTION_WRAPPERS(rb);
458 static char buf[ROWS*(COLS + 1)]; /* Enough for a whole board or a filename */
461 static void init_undo(void)
463 undo_info.count = 0;
464 undo_info.current = 0;
465 undo_info.max = 0;
468 static void get_delta(char direction, short *d_r, short *d_c)
470 switch (direction) {
471 case SOKOBAN_PUSH_LEFT:
472 case SOKOBAN_MOVE_LEFT:
473 *d_r = 0;
474 *d_c = -1;
475 break;
476 case SOKOBAN_PUSH_RIGHT:
477 case SOKOBAN_MOVE_RIGHT:
478 *d_r = 0;
479 *d_c = 1;
480 break;
481 case SOKOBAN_PUSH_UP:
482 case SOKOBAN_MOVE_UP:
483 *d_r = -1;
484 *d_c = 0;
485 break;
486 case SOKOBAN_PUSH_DOWN:
487 case SOKOBAN_MOVE_DOWN:
488 *d_r = 1;
489 *d_c = 0;
493 static bool undo(void)
495 char undo;
496 short r, c;
497 short d_r = 0, d_c = 0; /* delta row & delta col */
498 char *space_cur, *space_next, *space_prev;
499 bool undo_push = false;
501 /* If no more undos or we've wrapped all the way around, quit */
502 if (undo_info.count == 0 || undo_info.current - 1 == undo_info.max)
503 return false;
505 /* Move to previous undo in the list */
506 if (undo_info.current == 0 && undo_info.count > 1)
507 undo_info.current = MAX_UNDOS - 1;
508 else
509 undo_info.current--;
511 undo_info.count--;
513 undo = undo_info.history[undo_info.current];
515 if (undo < SOKOBAN_MOVE_MIN)
516 undo_push = true;
518 get_delta(undo, &d_r, &d_c);
520 r = current_info.player.row;
521 c = current_info.player.col;
523 /* Give the 3 spaces we're going to use better names */
524 space_cur = &current_info.board[r][c];
525 space_next = &current_info.board[r + d_r][c + d_c];
526 space_prev = &current_info.board[r - d_r][c - d_c];
528 /* Update board info */
529 if (undo_push) {
530 /* Moving box from goal to floor */
531 if (*space_next == '*' && *space_cur == '@')
532 current_info.level.boxes_to_go++;
533 /* Moving box from floor to goal */
534 else if (*space_next == '$' && *space_cur == '+')
535 current_info.level.boxes_to_go--;
537 /* Move box off of next space... */
538 *space_next = (*space_next == '*' ? '.' : ' ');
539 /* ...and on to current space */
540 *space_cur = (*space_cur == '+' ? '*' : '$');
542 current_info.level.pushes--;
543 } else
544 /* Just move player off of current space */
545 *space_cur = (*space_cur == '+' ? '.' : ' ');
546 /* Move player back to previous space */
547 *space_prev = (*space_prev == '.' ? '+' : '@');
549 /* Update position */
550 current_info.player.row -= d_r;
551 current_info.player.col -= d_c;
553 current_info.level.moves--;
555 return true;
558 static void add_undo(char undo)
560 undo_info.history[undo_info.current] = undo;
562 /* Wrap around if MAX_UNDOS exceeded */
563 if (undo_info.current < (MAX_UNDOS - 1))
564 undo_info.current++;
565 else
566 undo_info.current = 0;
568 if (undo_info.count < MAX_UNDOS)
569 undo_info.count++;
572 static bool move(char direction, bool redo)
574 short r, c;
575 short d_r = 0, d_c = 0; /* delta row & delta col */
576 char *space_cur, *space_next, *space_beyond;
577 bool push = false;
579 get_delta(direction, &d_r, &d_c);
581 r = current_info.player.row;
582 c = current_info.player.col;
584 /* Check for out-of-bounds */
585 if (r + 2*d_r < 0 || r + 2*d_r >= ROWS ||
586 c + 2*d_c < 0 || c + 2*d_c >= COLS)
587 return false;
589 /* Give the 3 spaces we're going to use better names */
590 space_cur = &current_info.board[r][c];
591 space_next = &current_info.board[r + d_r][c + d_c];
592 space_beyond = &current_info.board[r + 2*d_r][c + 2*d_c];
594 if (*space_next == '$' || *space_next == '*') {
595 /* Change direction from move to push for undo */
596 if (direction >= SOKOBAN_MOVE_MIN)
597 direction -= SOKOBAN_MOVE_DIFF;
598 push = true;
600 else if (direction < SOKOBAN_MOVE_MIN)
601 /* Change back to move if redo/solution playback push is invalid */
602 direction += SOKOBAN_MOVE_DIFF;
604 /* Update board info */
605 if (push) {
606 /* Moving box from goal to floor */
607 if (*space_next == '*' && *space_beyond == ' ')
608 current_info.level.boxes_to_go++;
609 /* Moving box from floor to goal */
610 else if (*space_next == '$' && *space_beyond == '.')
611 current_info.level.boxes_to_go--;
612 /* Check for invalid move */
613 else if (*space_beyond != '.' && *space_beyond != ' ')
614 return false;
616 /* Move player onto next space */
617 *space_next = (*space_next == '*' ? '+' : '@');
618 /* Move box onto space beyond next */
619 *space_beyond = (*space_beyond == '.' ? '*' : '$');
621 current_info.level.pushes++;
622 } else {
623 /* Check for invalid move */
624 if (*space_next != '.' && *space_next != ' ')
625 return false;
627 /* Move player onto next space */
628 *space_next = (*space_next == '.' ? '+' : '@');
630 /* Move player off of current space */
631 *space_cur = (*space_cur == '+' ? '.' : ' ');
633 /* Update position */
634 current_info.player.row += d_r;
635 current_info.player.col += d_c;
637 current_info.level.moves++;
639 /* Update undo_info.max to current on every normal move,
640 * except if it's the same as a redo. */
641 /* normal move and either */
642 if (!redo &&
643 /* moves have been undone... */
644 ((undo_info.max != undo_info.current &&
645 /* ...and the current move is NOT the same as the one in history */
646 undo_info.history[undo_info.current] != direction) ||
647 /* or moves have not been undone */
648 undo_info.max == undo_info.current)) {
649 add_undo(direction);
650 undo_info.max = undo_info.current;
651 } else /* redo move or move was same as redo */
652 add_undo(direction); /* add_undo to update current */
654 return true;
657 #ifdef SOKOBAN_REDO
658 static bool redo(void)
660 /* If no moves have been undone, quit */
661 if (undo_info.current == undo_info.max)
662 return false;
664 return move(undo_info.history[(undo_info.current < MAX_UNDOS ?
665 undo_info.current : 0)], true);
667 #endif
669 static void init_boards(void)
671 rb->strncpy(buffered_boards.filename, SOKOBAN_LEVELS_FILE, MAX_PATH);
673 current_info.level.index = 0;
674 current_info.player.row = 0;
675 current_info.player.col = 0;
676 current_info.max_level = 0;
678 buffered_boards.start = 0;
679 buffered_boards.end = 0;
680 buffered_boards.prebuffered_boards = 0;
682 init_undo();
685 static bool read_levels(bool initialize)
687 int fd = 0;
688 short len;
689 short lastlen = 0;
690 short row = 0;
691 int level_count = 0;
693 int i = 0;
694 int level_len = 0;
695 bool index_set = false;
697 /* Get the index of the first level to buffer */
698 if (current_info.level.index > buffered_boards.prebuffered_boards &&
699 !initialize)
700 buffered_boards.start = current_info.level.index -
701 buffered_boards.prebuffered_boards;
702 else
703 buffered_boards.start = 0;
705 if ((fd = rb->open(buffered_boards.filename, O_RDONLY)) < 0) {
706 rb->splashf(HZ*2, "Unable to open %s", buffered_boards.filename);
707 return false;
710 do {
711 len = rb->read_line(fd, buf, sizeof(buf));
713 /* Correct len when trailing \r's or \n's are counted */
714 if (len > 2 && buf[len - 2] == '\0')
715 len -= 2;
716 else if (len > 1 && buf[len - 1] == '\0')
717 len--;
719 /* Skip short lines & lines with non-level data */
720 if (len >= 3 && ((buf[0] >= '1' && buf[0] <= '9') || buf[0] == '#' ||
721 buf[0] == ' ' || buf[0] == '-' || buf[0] == '_')) {
722 if (level_count >= buffered_boards.start) {
723 /* Set the index of this level */
724 if (!index_set &&
725 level_count - buffered_boards.start < MAX_LEVELS) {
726 buffered_boards.index[level_count - buffered_boards.start]
727 = i;
728 index_set = true;
730 /* Copy buffer to board data */
731 if (i + level_len + len < MAX_LEVEL_DATA) {
732 rb->memcpy(&buffered_boards.data[i + level_len], buf, len);
733 buffered_boards.data[i + level_len + len] = '\n';
736 level_len += len + 1;
737 row++;
739 /* If newline & level is tall enough or is RLE */
740 } else if (buf[0] == '\0' && (row > 2 || lastlen > 22)) {
741 level_count++;
742 if (level_count >= buffered_boards.start) {
743 i += level_len;
744 if (i < MAX_LEVEL_DATA)
745 buffered_boards.end = level_count;
746 else if (!initialize)
747 break;
749 row = 0;
750 level_len = 0;
751 index_set = false;
753 } else if (len > 22)
754 len = 1;
756 } while ((lastlen = len));
758 /* Set the index of the end of the last level */
759 if (level_count - buffered_boards.start < MAX_LEVELS)
760 buffered_boards.index[level_count - buffered_boards.start] = i;
762 if (initialize) {
763 current_info.max_level = level_count;
764 buffered_boards.prebuffered_boards = buffered_boards.end/2;
767 rb->close(fd);
769 return true;
772 static void load_level(void)
774 int c, r;
775 int i, n;
776 int level_size;
777 int index = current_info.level.index - buffered_boards.start;
778 char *level;
780 /* Get the buffered board index of the current level */
781 if (current_info.level.index < buffered_boards.start ||
782 current_info.level.index >= buffered_boards.end) {
783 read_levels(false);
784 if (current_info.level.index > buffered_boards.prebuffered_boards)
785 index = buffered_boards.prebuffered_boards;
786 else
787 index = current_info.level.index;
789 level = &buffered_boards.data[buffered_boards.index[index]];
791 /* Reset level info */
792 current_info.level.moves = 0;
793 current_info.level.pushes = 0;
794 current_info.level.boxes_to_go = 0;
795 current_info.level.width = 0;
797 /* Clear board */
798 for (r = 0; r < ROWS; r++)
799 for (c = 0; c < COLS; c++)
800 current_info.board[r][c] = 'X';
802 level_size = buffered_boards.index[index + 1] -
803 buffered_boards.index[index];
805 for (r = 0, c = 0, n = 1, i = 0; i < level_size; i++) {
806 if (level[i] == '\n' || level[i] == '|') {
807 if (c > 3) {
808 /* Update max width of level & go to next row */
809 if (c > current_info.level.width)
810 current_info.level.width = c;
811 c = 0;
812 r++;
813 if (r >= ROWS)
814 break;
816 } else if (c < COLS) {
817 /* Read RLE character's length into n */
818 if (level[i] >= '0' && level[i] <= '9') {
819 n = level[i++] - '0';
820 if (level[i] >= '0' && level[i] <= '9')
821 n = n*10 + level[i++] - '0';
824 /* Cleanup & replace */
825 if (level[i] == '%')
826 level[i] = '*';
827 else if (level[i] == '-' || level[i] == '_')
828 level[i] = ' ';
830 if (n > 1) {
831 if (c + n >= COLS)
832 n = COLS - c;
834 if (level[i] == '.')
835 current_info.level.boxes_to_go += n;
837 /* Put RLE character n times */
838 while (n--)
839 current_info.board[r][c++] = level[i];
840 n = 1;
842 } else {
843 if (level[i] == '.' || level[i] == '+')
844 current_info.level.boxes_to_go++;
846 if (level[i] == '@' ||level[i] == '+') {
847 current_info.player.row = r;
848 current_info.player.col = c;
851 current_info.board[r][c++] = level[i];
856 current_info.level.height = r;
858 #if LCD_DEPTH > 2
859 /* Fill in blank space outside level on color targets */
860 for (r = 0; r < ROWS; r++)
861 for (c = 0; current_info.board[r][c] == ' ' && c < COLS; c++)
862 current_info.board[r][c] = 'X';
864 for (c = 0; c < COLS; c++) {
865 for (r = 0; (current_info.board[r][c] == ' ' ||
866 current_info.board[r][c] == 'X') && r < ROWS; r++)
867 current_info.board[r][c] = 'X';
868 for (r = ROWS - 1; (current_info.board[r][c] == ' ' ||
869 current_info.board[r][c] == 'X') && r >= 0; r--)
870 current_info.board[r][c] = 'X';
872 #endif
875 static void update_screen(void)
877 int c, r;
878 int rows, cols;
880 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) < 32
881 #define STAT_HEIGHT 25
882 #define STAT_X (LCD_WIDTH - 120)/2
883 #define STAT_Y (LCD_HEIGHT - STAT_HEIGHT)
884 #define BOARD_WIDTH LCD_WIDTH
885 #define BOARD_HEIGHT (LCD_HEIGHT - STAT_HEIGHT)
886 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 4, "Level");
887 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
888 rb->lcd_putsxy(STAT_X + 7, STAT_Y + 14, buf);
889 rb->lcd_putsxy(STAT_X + 41, STAT_Y + 4, "Moves");
890 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
891 rb->lcd_putsxy(STAT_X + 44, STAT_Y + 14, buf);
892 rb->lcd_putsxy(STAT_X + 79, STAT_Y + 4, "Pushes");
893 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
894 rb->lcd_putsxy(STAT_X + 82, STAT_Y + 14, buf);
896 rb->lcd_drawrect(STAT_X, STAT_Y, 38, STAT_HEIGHT);
897 rb->lcd_drawrect(STAT_X + 37, STAT_Y, 39, STAT_HEIGHT);
898 rb->lcd_drawrect(STAT_X + 75, STAT_Y, 45, STAT_HEIGHT);
899 #else
900 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) > 40
901 #define STAT_X (LCD_WIDTH - 40)
902 #else
903 #define STAT_X COLS*SOKOBAN_TILESIZE
904 #endif
905 #define STAT_Y (LCD_HEIGHT - 64)/2
906 #define STAT_WIDTH (LCD_WIDTH - STAT_X)
907 #define BOARD_WIDTH (LCD_WIDTH - STAT_WIDTH)
908 #define BOARD_HEIGHT LCD_HEIGHT
909 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 2, "Level");
910 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
911 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 12, buf);
912 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 23, "Moves");
913 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
914 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 33, buf);
915 #if STAT_WIDTH < 38
916 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Push");
917 #else
918 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Pushes");
919 #endif
920 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
921 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 54, buf);
923 rb->lcd_drawrect(STAT_X, STAT_Y + 0, STAT_WIDTH, 64);
924 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 21);
925 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 42);
927 #endif
929 /* load the board to the screen */
930 for (rows = 0; rows < ROWS; rows++) {
931 for (cols = 0; cols < COLS; cols++) {
932 c = cols*SOKOBAN_TILESIZE +
933 (BOARD_WIDTH - current_info.level.width*SOKOBAN_TILESIZE)/2;
934 r = rows*SOKOBAN_TILESIZE +
935 (BOARD_HEIGHT - current_info.level.height*SOKOBAN_TILESIZE)/2;
937 switch(current_info.board[rows][cols]) {
938 case 'X': /* blank space outside of level */
939 break;
941 case ' ': /* floor */
942 rb->lcd_bitmap_part(sokoban_tiles, 0, 0*SOKOBAN_TILESIZE,
943 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
944 SOKOBAN_TILESIZE);
945 break;
947 case '#': /* wall */
948 rb->lcd_bitmap_part(sokoban_tiles, 0, 1*SOKOBAN_TILESIZE,
949 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
950 SOKOBAN_TILESIZE);
951 break;
953 case '$': /* box */
954 rb->lcd_bitmap_part(sokoban_tiles, 0, 2*SOKOBAN_TILESIZE,
955 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
956 SOKOBAN_TILESIZE);
957 break;
959 case '*': /* box on goal */
960 rb->lcd_bitmap_part(sokoban_tiles, 0, 3*SOKOBAN_TILESIZE,
961 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
962 SOKOBAN_TILESIZE);
963 break;
965 case '.': /* goal */
966 rb->lcd_bitmap_part(sokoban_tiles, 0, 4*SOKOBAN_TILESIZE,
967 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
968 SOKOBAN_TILESIZE);
969 break;
971 case '@': /* player */
972 rb->lcd_bitmap_part(sokoban_tiles, 0, 5*SOKOBAN_TILESIZE,
973 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
974 SOKOBAN_TILESIZE);
975 break;
977 case '+': /* player on goal */
978 rb->lcd_bitmap_part(sokoban_tiles, 0, 6*SOKOBAN_TILESIZE,
979 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
980 SOKOBAN_TILESIZE);
981 break;
986 /* print out the screen */
987 rb->lcd_update();
990 static void draw_level(void)
992 load_level();
993 rb->lcd_clear_display();
994 update_screen();
997 static bool save(char *filename, bool solution)
999 int fd;
1000 char *loc;
1001 DIR *dir;
1002 char dirname[MAX_PATH];
1004 rb->splash(0, "Saving...");
1006 /* Create dir if it doesn't exist */
1007 if ((loc = rb->strrchr(filename, '/')) != NULL) {
1008 rb->strncpy(dirname, filename, loc - filename);
1009 dirname[loc - filename] = '\0';
1010 if(!(dir = rb->opendir(dirname)))
1011 rb->mkdir(dirname);
1012 else
1013 rb->closedir(dir);
1016 if (filename[0] == '\0' ||
1017 (fd = rb->open(filename, O_WRONLY|O_CREAT|O_TRUNC)) < 0) {
1018 rb->splashf(HZ*2, "Unable to open %s", filename);
1019 return false;
1022 /* Sokoban: S/P for solution/progress : level number : current undo */
1023 rb->snprintf(buf, sizeof(buf), "Sokoban:%c:%d:%d\n", (solution ? 'S' : 'P'),
1024 current_info.level.index + 1, undo_info.current);
1025 rb->write(fd, buf, rb->strlen(buf));
1027 /* Filename of levelset */
1028 rb->write(fd, buffered_boards.filename,
1029 rb->strlen(buffered_boards.filename));
1030 rb->write(fd, "\n", 1);
1032 /* Full undo history */
1033 rb->write(fd, undo_info.history, undo_info.max);
1035 rb->close(fd);
1037 return true;
1040 static bool load(char *filename, bool silent)
1042 int fd;
1043 int i, n;
1044 int len;
1045 int button;
1046 bool play_solution;
1047 bool paused = false;
1048 unsigned short speed = 2;
1049 int delay[] = {HZ/2, HZ/3, HZ/4, HZ/6, HZ/8, HZ/12, HZ/16, HZ/25};
1051 if (filename[0] == '\0' || (fd = rb->open(filename, O_RDONLY)) < 0) {
1052 if (!silent)
1053 rb->splashf(HZ*2, "Unable to open %s", filename);
1054 return false;
1057 /* Read header, level number, & current undo */
1058 rb->read_line(fd, buf, sizeof(buf));
1060 /* If we're opening a level file, not a solution/progress file */
1061 if (rb->strncmp(buf, "Sokoban", 7) != 0) {
1062 rb->close(fd);
1064 rb->strncpy(buffered_boards.filename, filename, MAX_PATH);
1065 if (!read_levels(true))
1066 return false;
1068 current_info.level.index = 0;
1069 load_level();
1071 /* If there aren't any boxes to go or the player position wasn't set,
1072 * the file probably wasn't a Sokoban level file */
1073 if (current_info.level.boxes_to_go == 0 ||
1074 current_info.player.row == 0 || current_info.player.col == 0) {
1075 if (!silent)
1076 rb->splash(HZ*2, "File is not a Sokoban level file");
1077 return false;
1080 } else {
1082 /* Read filename of levelset */
1083 rb->read_line(fd, buffered_boards.filename,
1084 sizeof(buffered_boards.filename));
1086 /* Read full undo history */
1087 len = rb->read_line(fd, undo_info.history, MAX_UNDOS);
1089 /* Correct len when trailing \r's or \n's are counted */
1090 if (len > 2 && undo_info.history[len - 2] == '\0')
1091 len -= 2;
1092 else if (len > 1 && undo_info.history[len - 1] == '\0')
1093 len--;
1095 rb->close(fd);
1097 /* Check to see if we're going to play a solution or resume progress */
1098 play_solution = (buf[8] == 'S');
1100 /* Get level number */
1101 for (n = 0, i = 10; buf[i] >= '0' && buf[i] <= '9' && i < 15; i++)
1102 n = n*10 + buf[i] - '0';
1103 current_info.level.index = n - 1;
1105 /* Get undo index */
1106 for (n = 0, i++; buf[i] >= '0' && buf[i] <= '9' && i < 21; i++)
1107 n = n*10 + buf[i] - '0';
1108 if (n > len)
1109 n = len;
1110 undo_info.max = len;
1112 if (current_info.level.index < 0) {
1113 if (!silent)
1114 rb->splash(HZ*2, "Error loading level");
1115 return false;
1117 if (!read_levels(true))
1118 return false;
1119 if (current_info.level.index >= current_info.max_level) {
1120 if (!silent)
1121 rb->splash(HZ*2, "Error loading level");
1122 return false;
1125 load_level();
1127 if (play_solution) {
1128 rb->lcd_clear_display();
1129 update_screen();
1130 rb->sleep(2*delay[speed]);
1132 /* Replay solution until menu button is pressed */
1133 i = 0;
1134 while (true) {
1135 if (i < len) {
1136 if (!move(undo_info.history[i], true)) {
1137 n = i;
1138 break;
1140 rb->lcd_clear_display();
1141 update_screen();
1142 i++;
1143 } else
1144 paused = true;
1146 rb->sleep(delay[speed]);
1148 while ((button = rb->button_get(false)) || paused) {
1149 switch (button) {
1150 case SOKOBAN_MENU:
1151 /* Pretend the level is complete so we'll quit */
1152 current_info.level.boxes_to_go = 0;
1153 return true;
1155 case SOKOBAN_PAUSE:
1156 /* Toggle pause state */
1157 paused = !paused;
1158 break;
1160 case SOKOBAN_LEFT:
1161 case SOKOBAN_LEFT | BUTTON_REPEAT:
1162 /* Go back one move */
1163 if (paused) {
1164 if (undo())
1165 i--;
1166 rb->lcd_clear_display();
1167 update_screen();
1169 break;
1171 case SOKOBAN_RIGHT:
1172 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1173 /* Go forward one move */
1174 if (paused) {
1175 if (redo())
1176 i++;
1177 rb->lcd_clear_display();
1178 update_screen();
1180 break;
1182 case SOKOBAN_UP:
1183 case SOKOBAN_UP | BUTTON_REPEAT:
1184 /* Speed up */
1185 if (speed < sizeof(delay)/sizeof(int) - 1)
1186 speed++;
1187 break;
1189 case SOKOBAN_DOWN:
1190 case SOKOBAN_DOWN | BUTTON_REPEAT:
1191 /* Slow down */
1192 if (speed > 0)
1193 speed--;
1196 if (paused)
1197 rb->sleep(HZ/33);
1201 /* If level is complete, wait for keypress before quitting */
1202 if (current_info.level.boxes_to_go == 0)
1203 rb->button_get(true);
1205 } else {
1206 /* Advance to current undo */
1207 for (i = 0; i < n; i++) {
1208 if (!move(undo_info.history[i], true)) {
1209 n = i;
1210 break;
1214 rb->button_clear_queue();
1215 rb->lcd_clear_display();
1218 undo_info.current = n;
1221 return true;
1224 static int sokoban_menu(void)
1226 int button;
1227 int selection = 0;
1228 int i;
1229 bool menu_quit;
1230 int start_selected = 0;
1231 int prev_level = current_info.level.index;
1233 MENUITEM_STRINGLIST(menu, "Sokoban Menu", NULL,
1234 "Resume", "Select Level", "Audio Playback", "Keys",
1235 "Load Default Level Set", "Quit Without Saving",
1236 "Save Progress & Quit");
1238 do {
1239 menu_quit = true;
1240 selection = rb->do_menu(&menu, &start_selected, NULL, false);
1242 switch (selection) {
1243 case 0: /* Resume */
1244 break;
1246 case 1: /* Select level */
1247 current_info.level.index++;
1248 rb->set_int("Select Level", "", UNIT_INT,
1249 &current_info.level.index, NULL, 1, 1,
1250 current_info.max_level, NULL);
1251 current_info.level.index--;
1252 if (prev_level != current_info.level.index) {
1253 init_undo();
1254 draw_level();
1255 } else
1256 menu_quit = false;
1257 break;
1259 case 2: /* Audio playback control */
1260 playback_control(rb, NULL);
1261 menu_quit = false;
1262 break;
1264 case 3: /* Keys */
1265 FOR_NB_SCREENS(i)
1266 rb->screens[i]->clear_display();
1267 rb->lcd_setfont(SOKOBAN_FONT);
1269 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
1270 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
1271 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1272 rb->lcd_putsxy(3, 16, "[ON] Undo");
1273 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1274 rb->lcd_putsxy(3, 36, "[F1] Down a Level");
1275 rb->lcd_putsxy(3, 46, "[F2] Restart Level");
1276 rb->lcd_putsxy(3, 56, "[F3] Up a Level");
1277 #elif CONFIG_KEYPAD == ONDIO_PAD
1278 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1279 rb->lcd_putsxy(3, 16, "[MODE] Undo");
1280 rb->lcd_putsxy(3, 26, "[MODE+DOWN] Redo");
1281 rb->lcd_putsxy(3, 36, "[MODE+LEFT] Previous Level");
1282 rb->lcd_putsxy(3, 46, "[MODE+UP] Restart Level");
1283 rb->lcd_putsxy(3, 56, "[MODE+RIGHT] Up Level");
1284 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
1285 (CONFIG_KEYPAD == IRIVER_H300_PAD)
1286 rb->lcd_putsxy(3, 6, "[STOP] Menu");
1287 rb->lcd_putsxy(3, 16, "[REC] Undo");
1288 rb->lcd_putsxy(3, 26, "[MODE] Redo");
1289 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1290 rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
1291 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1292 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1293 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1294 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1295 rb->lcd_putsxy(3, 6, "[SELECT+MENU] Menu");
1296 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1297 rb->lcd_putsxy(3, 26, "[SELECT+PLAY] Redo");
1298 rb->lcd_putsxy(3, 36, "[SELECT+LEFT] Previous Level");
1299 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Next Level");
1300 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
1301 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1302 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1303 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1304 rb->lcd_putsxy(3, 36, "[REC] Restart Level");
1305 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1306 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1307 rb->lcd_putsxy(3, 16, "[REW] Undo");
1308 rb->lcd_putsxy(3, 26, "[FF] Redo");
1309 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1310 rb->lcd_putsxy(3, 46, "[PLAY+RIGHT] Restart Level");
1311 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1312 #elif CONFIG_KEYPAD == GIGABEAT_PAD
1313 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1314 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1315 rb->lcd_putsxy(3, 26, "[A] Redo");
1316 rb->lcd_putsxy(3, 36, "[VOL-] Previous Level");
1317 rb->lcd_putsxy(3, 46, "[MENU] Restart Level");
1318 rb->lcd_putsxy(3, 56, "[VOL+] Next Level");
1319 #elif CONFIG_KEYPAD == SANSA_E200_PAD
1320 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1321 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1322 rb->lcd_putsxy(3, 26, "[REC] Redo");
1323 rb->lcd_putsxy(3, 36, "[SELECT+DOWN] Previous Level");
1324 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Restart Level");
1325 rb->lcd_putsxy(3, 56, "[SELECT+UP] Next Level");
1326 #endif
1328 #ifdef HAVE_TOUCHSCREEN
1329 rb->lcd_putsxy(3, 6, SOKOBAN_MENU_NAME " Menu");
1330 rb->lcd_putsxy(3, 16, SOKOBAN_UNDO_NAME " Undo");
1331 rb->lcd_putsxy(3, 26, SOKOBAN_REDO_NAME " Redo");
1332 rb->lcd_putsxy(3, 36, SOKOBAN_PAUSE_NAME " Pause");
1333 rb->lcd_putsxy(3, 46, SOKOBAN_LEVEL_REPEAT_NAME " Restart Level");
1334 #endif
1336 FOR_NB_SCREENS(i)
1337 rb->screens[i]->update();
1339 /* Display until keypress */
1340 do {
1341 rb->sleep(HZ/20);
1342 button = rb->button_get(false);
1343 } while (!button || button & BUTTON_REL ||
1344 button & BUTTON_REPEAT);
1346 menu_quit = false;
1347 break;
1349 case 4: /* Load default levelset */
1350 init_boards();
1351 if (!read_levels(true))
1352 return 5; /* Quit */
1353 load_level();
1354 break;
1356 case 5: /* Quit */
1357 break;
1359 case 6: /* Save & quit */
1360 save(SOKOBAN_SAVE_FILE, false);
1361 rb->reload_directory();
1364 } while (!menu_quit);
1366 /* Restore font */
1367 rb->lcd_setfont(SOKOBAN_FONT);
1369 FOR_NB_SCREENS(i) {
1370 rb->screens[i]->clear_display();
1371 rb->screens[i]->update();
1374 return selection;
1377 static bool sokoban_loop(void)
1379 bool moved;
1380 int i = 0, button = 0, lastbutton = 0;
1381 short r = 0, c = 0;
1382 int w, h;
1383 char *loc;
1385 while (true) {
1386 moved = false;
1388 r = current_info.player.row;
1389 c = current_info.player.col;
1391 button = rb->button_get(true);
1393 switch(button)
1395 #ifdef SOKOBAN_RC_MENU
1396 case SOKOBAN_RC_MENU:
1397 #endif
1398 case SOKOBAN_MENU:
1399 switch (sokoban_menu()) {
1400 case 5: /* Quit */
1401 case 6: /* Save & quit */
1402 return PLUGIN_OK;
1404 update_screen();
1405 break;
1407 case SOKOBAN_UNDO:
1408 #ifdef SOKOBAN_UNDO_PRE
1409 if (lastbutton != SOKOBAN_UNDO_PRE)
1410 break;
1411 #else /* repeat can't work here for Ondio, iPod, et al */
1412 case SOKOBAN_UNDO | BUTTON_REPEAT:
1413 #endif
1414 undo();
1415 rb->lcd_clear_display();
1416 update_screen();
1417 break;
1419 #ifdef SOKOBAN_REDO
1420 case SOKOBAN_REDO:
1421 case SOKOBAN_REDO | BUTTON_REPEAT:
1422 moved = redo();
1423 rb->lcd_clear_display();
1424 update_screen();
1425 break;
1426 #endif
1428 #ifdef SOKOBAN_LEVEL_UP
1429 case SOKOBAN_LEVEL_UP:
1430 case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
1431 /* next level */
1432 init_undo();
1433 if (current_info.level.index + 1 < current_info.max_level)
1434 current_info.level.index++;
1436 draw_level();
1437 break;
1438 #endif
1440 #ifdef SOKOBAN_LEVEL_DOWN
1441 case SOKOBAN_LEVEL_DOWN:
1442 case SOKOBAN_LEVEL_DOWN | BUTTON_REPEAT:
1443 /* previous level */
1444 init_undo();
1445 if (current_info.level.index > 0)
1446 current_info.level.index--;
1448 draw_level();
1449 break;
1450 #endif
1452 #ifdef SOKOBAN_LEVEL_REPEAT
1453 case SOKOBAN_LEVEL_REPEAT:
1454 case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
1455 /* same level */
1456 init_undo();
1457 draw_level();
1458 break;
1459 #endif
1461 case SOKOBAN_LEFT:
1462 case SOKOBAN_LEFT | BUTTON_REPEAT:
1463 moved = move(SOKOBAN_MOVE_LEFT, false);
1464 break;
1466 case SOKOBAN_RIGHT:
1467 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1468 moved = move(SOKOBAN_MOVE_RIGHT, false);
1469 break;
1471 case SOKOBAN_UP:
1472 case SOKOBAN_UP | BUTTON_REPEAT:
1473 moved = move(SOKOBAN_MOVE_UP, false);
1474 break;
1476 case SOKOBAN_DOWN:
1477 case SOKOBAN_DOWN | BUTTON_REPEAT:
1478 moved = move(SOKOBAN_MOVE_DOWN, false);
1479 break;
1481 default:
1482 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
1483 return PLUGIN_USB_CONNECTED;
1484 break;
1487 lastbutton = button;
1489 if (moved) {
1490 rb->lcd_clear_display();
1491 update_screen();
1494 /* We have completed this level */
1495 if (current_info.level.boxes_to_go == 0) {
1497 if (moved) {
1498 rb->lcd_clear_display();
1500 /* Show level complete message & stats */
1501 rb->snprintf(buf, sizeof(buf), "Level %d Complete!",
1502 current_info.level.index + 1);
1503 rb->lcd_getstringsize(buf, &w, &h);
1504 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h*3, buf);
1506 rb->snprintf(buf, sizeof(buf), "%4d Moves ",
1507 current_info.level.moves);
1508 rb->lcd_getstringsize(buf, &w, &h);
1509 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h, buf);
1511 rb->snprintf(buf, sizeof(buf), "%4d Pushes",
1512 current_info.level.pushes);
1513 rb->lcd_getstringsize(buf, &w, &h);
1514 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2, buf);
1516 if (undo_info.count < MAX_UNDOS) {
1517 rb->snprintf(buf, sizeof(buf), "%s: Save solution",
1518 BUTTON_SAVE_NAME);
1519 rb->lcd_getstringsize(buf, &w, &h);
1520 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + h*2, buf);
1523 rb->lcd_update();
1524 rb->sleep(HZ/4);
1525 rb->button_clear_queue();
1527 /* Display for 4 seconds or until new keypress */
1528 for (i = 0; i < 80; i++) {
1529 rb->sleep(HZ/20);
1530 button = rb->button_get(false);
1531 if (button && !(button & BUTTON_REL) &&
1532 !(button & BUTTON_REPEAT))
1533 break;
1536 if (button == BUTTON_SAVE) {
1537 if (undo_info.count < MAX_UNDOS) {
1538 /* Set filename to current levelset plus level number
1539 * and .sok extension. Use SAVE_FOLDER if using the
1540 * default levelset, since it's in a hidden folder. */
1541 if (rb->strcmp(buffered_boards.filename,
1542 SOKOBAN_LEVELS_FILE) == 0) {
1543 rb->snprintf(buf, sizeof(buf),
1544 "%s/sokoban.%d.sok",
1545 SOKOBAN_SAVE_FOLDER,
1546 current_info.level.index + 1);
1547 } else {
1548 if ((loc = rb->strrchr(buffered_boards.filename,
1549 '.')) != NULL)
1550 *loc = '\0';
1551 rb->snprintf(buf, sizeof(buf), "%s.%d.sok",
1552 buffered_boards.filename,
1553 current_info.level.index + 1);
1554 if (loc != NULL)
1555 *loc = '.';
1558 if (!rb->kbd_input(buf, MAX_PATH))
1559 save(buf, true);
1560 } else
1561 rb->splash(HZ*2, "Solution too long to save");
1563 rb->lcd_setfont(SOKOBAN_FONT); /* Restore font */
1567 FOR_NB_SCREENS(i) {
1568 rb->screens[i]->clear_display();
1569 rb->screens[i]->update();
1572 current_info.level.index++;
1574 /* clear undo stats */
1575 init_undo();
1577 if (current_info.level.index >= current_info.max_level) {
1578 /* Show levelset complete message */
1579 rb->snprintf(buf, sizeof(buf), "You WIN!!");
1580 rb->lcd_getstringsize(buf, &w, &h);
1581 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, buf);
1583 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1584 /* Display for 4 seconds or until keypress */
1585 for (i = 0; i < 80; i++) {
1586 rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
1587 rb->lcd_update();
1588 rb->sleep(HZ/10);
1590 button = rb->button_get(false);
1591 if (button && !(button & BUTTON_REL))
1592 break;
1594 rb->lcd_set_drawmode(DRMODE_SOLID);
1596 /* Reset to first level & show quit menu */
1597 current_info.level.index = 0;
1599 switch (sokoban_menu()) {
1600 case 5: /* Quit */
1601 case 6: /* Save & quit */
1602 return PLUGIN_OK;
1606 load_level();
1607 update_screen();
1610 } /* end while */
1612 return PLUGIN_OK;
1616 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
1618 int w, h;
1620 (void)(parameter);
1621 rb = api;
1623 rb->lcd_setfont(SOKOBAN_FONT);
1625 rb->lcd_clear_display();
1626 rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
1627 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, SOKOBAN_TITLE);
1628 rb->lcd_update();
1629 rb->sleep(HZ); /* Show title for 1 second */
1631 init_boards();
1633 if (parameter == NULL) {
1634 /* Attempt to resume saved progress, otherwise start at beginning */
1635 if (!load(SOKOBAN_SAVE_FILE, true)) {
1636 init_boards();
1637 if (!read_levels(true))
1638 return PLUGIN_OK;
1639 load_level();
1642 } else {
1643 /* The plugin is being used to open a file */
1644 if (load((char*) parameter, false)) {
1645 /* If we loaded & played a solution, quit */
1646 if (current_info.level.boxes_to_go == 0)
1647 return PLUGIN_OK;
1648 } else
1649 return PLUGIN_OK;
1652 rb->lcd_clear_display();
1653 update_screen();
1655 return sokoban_loop();