FS#10075 - FUZE: QUIT Plugin by selecting BUTTON_HOME by Johannes Schwarz. Updates...
[kugel-rb/myfork.git] / apps / plugins / sokoban.c
blobd0277941275e75fc66e500af9683a99b4aaeac7f
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 #else
384 #error No keymap defined!
385 #endif
387 #ifdef HAVE_TOUCHSCREEN
388 #ifndef SOKOBAN_LEFT
389 #define SOKOBAN_LEFT BUTTON_MIDLEFT
390 #endif
391 #ifndef SOKOBAN_RIGHT
392 #define SOKOBAN_RIGHT BUTTON_MIDRIGHT
393 #endif
394 #ifndef SOKOBAN_UP
395 #define SOKOBAN_UP BUTTON_TOPMIDDLE
396 #endif
397 #ifndef SOKOBAN_DOWN
398 #define SOKOBAN_DOWN BUTTON_BOTTOMMIDDLE
399 #endif
400 #ifndef SOKOBAN_MENU
401 #define SOKOBAN_MENU BUTTON_TOPLEFT
402 #define SOKOBAN_MENU_NAME "[TOPLEFT]"
403 #endif
404 #ifndef SOKOBAN_UNDO
405 #define SOKOBAN_UNDO BUTTON_BOTTOMRIGHT
406 #define SOKOBAN_UNDO_NAME "[BOTTOMRIGHT]"
407 #endif
408 #ifndef SOKOBAN_REDO
409 #define SOKOBAN_REDO BUTTON_BOTTOMLEFT
410 #define SOKOBAN_REDO_NAME "[BOTTOMLEFT]"
411 #endif
412 #ifndef SOKOBAN_PAUSE
413 #define SOKOBAN_PAUSE BUTTON_CENTER
414 #define SOKOBAN_PAUSE_NAME "[CENTER]"
415 #endif
416 #ifndef SOKOBAN_LEVEL_REPEAT
417 #define SOKOBAN_LEVEL_REPEAT BUTTON_TOPRIGHT
418 #define SOKOBAN_LEVEL_REPEAT_NAME "[TOPRIGHT]"
419 #endif
420 #ifndef BUTTON_SAVE
421 #define BUTTON_SAVE BUTTON_CENTER
422 #define BUTTON_SAVE_NAME "CENTER"
423 #endif
424 #endif
426 #define SOKOBAN_FONT FONT_SYSFIXED
429 /* The Location, Undo and LevelInfo structs are OO-flavored.
430 * (oooh!-flavored as Schnueff puts it.) It makes more you have to know,
431 * but the overall data layout becomes more manageable. */
433 /* Level data & stats */
434 struct LevelInfo {
435 int index; /* Level index (level number - 1) */
436 int moves; /* Moves & pushes for the stats */
437 int pushes;
438 short boxes_to_go; /* Number of unplaced boxes remaining in level */
439 short height; /* Height & width for centering level display */
440 short width;
443 struct Location {
444 short row;
445 short col;
448 /* Our full undo history */
449 static struct UndoInfo {
450 int count; /* How many undos have been done */
451 int current; /* Which history is the current undo */
452 int max; /* Which history is the max redoable */
453 char history[MAX_UNDOS];
454 } undo_info;
456 /* Our playing board */
457 static struct BoardInfo {
458 char board[ROWS][COLS]; /* The current board data */
459 struct LevelInfo level; /* Level data & stats */
460 struct Location player; /* Where the player is */
461 int max_level; /* The number of levels we have */
462 } current_info;
464 static struct BufferedBoards {
465 char filename[MAX_PATH]; /* Filename of the levelset we're using */
466 char data[MAX_LEVEL_DATA]; /* Buffered level data */
467 int index[MAX_LEVELS + 1]; /* Where each buffered board begins & ends */
468 int start; /* Index of first buffered board */
469 int end; /* Index of last buffered board */
470 short prebuffered_boards; /* Number of boards before current to store */
471 } buffered_boards;
474 static char buf[ROWS*(COLS + 1)]; /* Enough for a whole board or a filename */
477 static void init_undo(void)
479 undo_info.count = 0;
480 undo_info.current = 0;
481 undo_info.max = 0;
484 static void get_delta(char direction, short *d_r, short *d_c)
486 switch (direction) {
487 case SOKOBAN_PUSH_LEFT:
488 case SOKOBAN_MOVE_LEFT:
489 *d_r = 0;
490 *d_c = -1;
491 break;
492 case SOKOBAN_PUSH_RIGHT:
493 case SOKOBAN_MOVE_RIGHT:
494 *d_r = 0;
495 *d_c = 1;
496 break;
497 case SOKOBAN_PUSH_UP:
498 case SOKOBAN_MOVE_UP:
499 *d_r = -1;
500 *d_c = 0;
501 break;
502 case SOKOBAN_PUSH_DOWN:
503 case SOKOBAN_MOVE_DOWN:
504 *d_r = 1;
505 *d_c = 0;
509 static bool undo(void)
511 char undo;
512 short r, c;
513 short d_r = 0, d_c = 0; /* delta row & delta col */
514 char *space_cur, *space_next, *space_prev;
515 bool undo_push = false;
517 /* If no more undos or we've wrapped all the way around, quit */
518 if (undo_info.count == 0 || undo_info.current - 1 == undo_info.max)
519 return false;
521 /* Move to previous undo in the list */
522 if (undo_info.current == 0 && undo_info.count > 1)
523 undo_info.current = MAX_UNDOS - 1;
524 else
525 undo_info.current--;
527 undo_info.count--;
529 undo = undo_info.history[undo_info.current];
531 if (undo < SOKOBAN_MOVE_MIN)
532 undo_push = true;
534 get_delta(undo, &d_r, &d_c);
536 r = current_info.player.row;
537 c = current_info.player.col;
539 /* Give the 3 spaces we're going to use better names */
540 space_cur = &current_info.board[r][c];
541 space_next = &current_info.board[r + d_r][c + d_c];
542 space_prev = &current_info.board[r - d_r][c - d_c];
544 /* Update board info */
545 if (undo_push) {
546 /* Moving box from goal to floor */
547 if (*space_next == '*' && *space_cur == '@')
548 current_info.level.boxes_to_go++;
549 /* Moving box from floor to goal */
550 else if (*space_next == '$' && *space_cur == '+')
551 current_info.level.boxes_to_go--;
553 /* Move box off of next space... */
554 *space_next = (*space_next == '*' ? '.' : ' ');
555 /* ...and on to current space */
556 *space_cur = (*space_cur == '+' ? '*' : '$');
558 current_info.level.pushes--;
559 } else
560 /* Just move player off of current space */
561 *space_cur = (*space_cur == '+' ? '.' : ' ');
562 /* Move player back to previous space */
563 *space_prev = (*space_prev == '.' ? '+' : '@');
565 /* Update position */
566 current_info.player.row -= d_r;
567 current_info.player.col -= d_c;
569 current_info.level.moves--;
571 return true;
574 static void add_undo(char undo)
576 undo_info.history[undo_info.current] = undo;
578 /* Wrap around if MAX_UNDOS exceeded */
579 if (undo_info.current < (MAX_UNDOS - 1))
580 undo_info.current++;
581 else
582 undo_info.current = 0;
584 if (undo_info.count < MAX_UNDOS)
585 undo_info.count++;
588 static bool move(char direction, bool redo)
590 short r, c;
591 short d_r = 0, d_c = 0; /* delta row & delta col */
592 char *space_cur, *space_next, *space_beyond;
593 bool push = false;
595 get_delta(direction, &d_r, &d_c);
597 r = current_info.player.row;
598 c = current_info.player.col;
600 /* Check for out-of-bounds */
601 if (r + 2*d_r < 0 || r + 2*d_r >= ROWS ||
602 c + 2*d_c < 0 || c + 2*d_c >= COLS)
603 return false;
605 /* Give the 3 spaces we're going to use better names */
606 space_cur = &current_info.board[r][c];
607 space_next = &current_info.board[r + d_r][c + d_c];
608 space_beyond = &current_info.board[r + 2*d_r][c + 2*d_c];
610 if (*space_next == '$' || *space_next == '*') {
611 /* Change direction from move to push for undo */
612 if (direction >= SOKOBAN_MOVE_MIN)
613 direction -= SOKOBAN_MOVE_DIFF;
614 push = true;
616 else if (direction < SOKOBAN_MOVE_MIN)
617 /* Change back to move if redo/solution playback push is invalid */
618 direction += SOKOBAN_MOVE_DIFF;
620 /* Update board info */
621 if (push) {
622 /* Moving box from goal to floor */
623 if (*space_next == '*' && *space_beyond == ' ')
624 current_info.level.boxes_to_go++;
625 /* Moving box from floor to goal */
626 else if (*space_next == '$' && *space_beyond == '.')
627 current_info.level.boxes_to_go--;
628 /* Check for invalid move */
629 else if (*space_beyond != '.' && *space_beyond != ' ')
630 return false;
632 /* Move player onto next space */
633 *space_next = (*space_next == '*' ? '+' : '@');
634 /* Move box onto space beyond next */
635 *space_beyond = (*space_beyond == '.' ? '*' : '$');
637 current_info.level.pushes++;
638 } else {
639 /* Check for invalid move */
640 if (*space_next != '.' && *space_next != ' ')
641 return false;
643 /* Move player onto next space */
644 *space_next = (*space_next == '.' ? '+' : '@');
646 /* Move player off of current space */
647 *space_cur = (*space_cur == '+' ? '.' : ' ');
649 /* Update position */
650 current_info.player.row += d_r;
651 current_info.player.col += d_c;
653 current_info.level.moves++;
655 /* Update undo_info.max to current on every normal move,
656 * except if it's the same as a redo. */
657 /* normal move and either */
658 if (!redo &&
659 /* moves have been undone... */
660 ((undo_info.max != undo_info.current &&
661 /* ...and the current move is NOT the same as the one in history */
662 undo_info.history[undo_info.current] != direction) ||
663 /* or moves have not been undone */
664 undo_info.max == undo_info.current)) {
665 add_undo(direction);
666 undo_info.max = undo_info.current;
667 } else /* redo move or move was same as redo */
668 add_undo(direction); /* add_undo to update current */
670 return true;
673 #ifdef SOKOBAN_REDO
674 static bool redo(void)
676 /* If no moves have been undone, quit */
677 if (undo_info.current == undo_info.max)
678 return false;
680 return move(undo_info.history[(undo_info.current < MAX_UNDOS ?
681 undo_info.current : 0)], true);
683 #endif
685 static void init_boards(void)
687 rb->strncpy(buffered_boards.filename, SOKOBAN_LEVELS_FILE, MAX_PATH);
689 current_info.level.index = 0;
690 current_info.player.row = 0;
691 current_info.player.col = 0;
692 current_info.max_level = 0;
694 buffered_boards.start = 0;
695 buffered_boards.end = 0;
696 buffered_boards.prebuffered_boards = 0;
698 init_undo();
701 static bool read_levels(bool initialize)
703 int fd = 0;
704 short len;
705 short lastlen = 0;
706 short row = 0;
707 int level_count = 0;
709 int i = 0;
710 int level_len = 0;
711 bool index_set = false;
713 /* Get the index of the first level to buffer */
714 if (current_info.level.index > buffered_boards.prebuffered_boards &&
715 !initialize)
716 buffered_boards.start = current_info.level.index -
717 buffered_boards.prebuffered_boards;
718 else
719 buffered_boards.start = 0;
721 if ((fd = rb->open(buffered_boards.filename, O_RDONLY)) < 0) {
722 rb->splashf(HZ*2, "Unable to open %s", buffered_boards.filename);
723 return false;
726 do {
727 len = rb->read_line(fd, buf, sizeof(buf));
729 /* Correct len when trailing \r's or \n's are counted */
730 if (len > 2 && buf[len - 2] == '\0')
731 len -= 2;
732 else if (len > 1 && buf[len - 1] == '\0')
733 len--;
735 /* Skip short lines & lines with non-level data */
736 if (len >= 3 && ((buf[0] >= '1' && buf[0] <= '9') || buf[0] == '#' ||
737 buf[0] == ' ' || buf[0] == '-' || buf[0] == '_')) {
738 if (level_count >= buffered_boards.start) {
739 /* Set the index of this level */
740 if (!index_set &&
741 level_count - buffered_boards.start < MAX_LEVELS) {
742 buffered_boards.index[level_count - buffered_boards.start]
743 = i;
744 index_set = true;
746 /* Copy buffer to board data */
747 if (i + level_len + len < MAX_LEVEL_DATA) {
748 rb->memcpy(&buffered_boards.data[i + level_len], buf, len);
749 buffered_boards.data[i + level_len + len] = '\n';
752 level_len += len + 1;
753 row++;
755 /* If newline & level is tall enough or is RLE */
756 } else if (buf[0] == '\0' && (row > 2 || lastlen > 22)) {
757 level_count++;
758 if (level_count >= buffered_boards.start) {
759 i += level_len;
760 if (i < MAX_LEVEL_DATA)
761 buffered_boards.end = level_count;
762 else if (!initialize)
763 break;
765 row = 0;
766 level_len = 0;
767 index_set = false;
769 } else if (len > 22)
770 len = 1;
772 } while ((lastlen = len));
774 /* Set the index of the end of the last level */
775 if (level_count - buffered_boards.start < MAX_LEVELS)
776 buffered_boards.index[level_count - buffered_boards.start] = i;
778 if (initialize) {
779 current_info.max_level = level_count;
780 buffered_boards.prebuffered_boards = buffered_boards.end/2;
783 rb->close(fd);
785 return true;
788 static void load_level(void)
790 int c, r;
791 int i, n;
792 int level_size;
793 int index = current_info.level.index - buffered_boards.start;
794 char *level;
796 /* Get the buffered board index of the current level */
797 if (current_info.level.index < buffered_boards.start ||
798 current_info.level.index >= buffered_boards.end) {
799 read_levels(false);
800 if (current_info.level.index > buffered_boards.prebuffered_boards)
801 index = buffered_boards.prebuffered_boards;
802 else
803 index = current_info.level.index;
805 level = &buffered_boards.data[buffered_boards.index[index]];
807 /* Reset level info */
808 current_info.level.moves = 0;
809 current_info.level.pushes = 0;
810 current_info.level.boxes_to_go = 0;
811 current_info.level.width = 0;
813 /* Clear board */
814 for (r = 0; r < ROWS; r++)
815 for (c = 0; c < COLS; c++)
816 current_info.board[r][c] = 'X';
818 level_size = buffered_boards.index[index + 1] -
819 buffered_boards.index[index];
821 for (r = 0, c = 0, n = 1, i = 0; i < level_size; i++) {
822 if (level[i] == '\n' || level[i] == '|') {
823 if (c > 3) {
824 /* Update max width of level & go to next row */
825 if (c > current_info.level.width)
826 current_info.level.width = c;
827 c = 0;
828 r++;
829 if (r >= ROWS)
830 break;
832 } else if (c < COLS) {
833 /* Read RLE character's length into n */
834 if (level[i] >= '0' && level[i] <= '9') {
835 n = level[i++] - '0';
836 if (level[i] >= '0' && level[i] <= '9')
837 n = n*10 + level[i++] - '0';
840 /* Cleanup & replace */
841 if (level[i] == '%')
842 level[i] = '*';
843 else if (level[i] == '-' || level[i] == '_')
844 level[i] = ' ';
846 if (n > 1) {
847 if (c + n >= COLS)
848 n = COLS - c;
850 if (level[i] == '.')
851 current_info.level.boxes_to_go += n;
853 /* Put RLE character n times */
854 while (n--)
855 current_info.board[r][c++] = level[i];
856 n = 1;
858 } else {
859 if (level[i] == '.' || level[i] == '+')
860 current_info.level.boxes_to_go++;
862 if (level[i] == '@' ||level[i] == '+') {
863 current_info.player.row = r;
864 current_info.player.col = c;
867 current_info.board[r][c++] = level[i];
872 current_info.level.height = r;
874 #if LCD_DEPTH > 2
875 /* Fill in blank space outside level on color targets */
876 for (r = 0; r < ROWS; r++)
877 for (c = 0; current_info.board[r][c] == ' ' && c < COLS; c++)
878 current_info.board[r][c] = 'X';
880 for (c = 0; c < COLS; c++) {
881 for (r = 0; (current_info.board[r][c] == ' ' ||
882 current_info.board[r][c] == 'X') && r < ROWS; r++)
883 current_info.board[r][c] = 'X';
884 for (r = ROWS - 1; (current_info.board[r][c] == ' ' ||
885 current_info.board[r][c] == 'X') && r >= 0; r--)
886 current_info.board[r][c] = 'X';
888 #endif
891 static void update_screen(void)
893 int c, r;
894 int rows, cols;
896 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) < 32
897 #define STAT_HEIGHT 25
898 #define STAT_X (LCD_WIDTH - 120)/2
899 #define STAT_Y (LCD_HEIGHT - STAT_HEIGHT)
900 #define BOARD_WIDTH LCD_WIDTH
901 #define BOARD_HEIGHT (LCD_HEIGHT - STAT_HEIGHT)
902 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 4, "Level");
903 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
904 rb->lcd_putsxy(STAT_X + 7, STAT_Y + 14, buf);
905 rb->lcd_putsxy(STAT_X + 41, STAT_Y + 4, "Moves");
906 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
907 rb->lcd_putsxy(STAT_X + 44, STAT_Y + 14, buf);
908 rb->lcd_putsxy(STAT_X + 79, STAT_Y + 4, "Pushes");
909 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
910 rb->lcd_putsxy(STAT_X + 82, STAT_Y + 14, buf);
912 rb->lcd_drawrect(STAT_X, STAT_Y, 38, STAT_HEIGHT);
913 rb->lcd_drawrect(STAT_X + 37, STAT_Y, 39, STAT_HEIGHT);
914 rb->lcd_drawrect(STAT_X + 75, STAT_Y, 45, STAT_HEIGHT);
915 #else
916 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) > 40
917 #define STAT_X (LCD_WIDTH - 40)
918 #else
919 #define STAT_X COLS*SOKOBAN_TILESIZE
920 #endif
921 #define STAT_Y (LCD_HEIGHT - 64)/2
922 #define STAT_WIDTH (LCD_WIDTH - STAT_X)
923 #define BOARD_WIDTH (LCD_WIDTH - STAT_WIDTH)
924 #define BOARD_HEIGHT LCD_HEIGHT
925 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 2, "Level");
926 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
927 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 12, buf);
928 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 23, "Moves");
929 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
930 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 33, buf);
931 #if STAT_WIDTH < 38
932 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Push");
933 #else
934 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Pushes");
935 #endif
936 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
937 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 54, buf);
939 rb->lcd_drawrect(STAT_X, STAT_Y + 0, STAT_WIDTH, 64);
940 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 21);
941 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 42);
943 #endif
945 /* load the board to the screen */
946 for (rows = 0; rows < ROWS; rows++) {
947 for (cols = 0; cols < COLS; cols++) {
948 c = cols*SOKOBAN_TILESIZE +
949 (BOARD_WIDTH - current_info.level.width*SOKOBAN_TILESIZE)/2;
950 r = rows*SOKOBAN_TILESIZE +
951 (BOARD_HEIGHT - current_info.level.height*SOKOBAN_TILESIZE)/2;
953 switch(current_info.board[rows][cols]) {
954 case 'X': /* blank space outside of level */
955 break;
957 case ' ': /* floor */
958 rb->lcd_bitmap_part(sokoban_tiles, 0, 0*SOKOBAN_TILESIZE,
959 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
960 SOKOBAN_TILESIZE);
961 break;
963 case '#': /* wall */
964 rb->lcd_bitmap_part(sokoban_tiles, 0, 1*SOKOBAN_TILESIZE,
965 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
966 SOKOBAN_TILESIZE);
967 break;
969 case '$': /* box */
970 rb->lcd_bitmap_part(sokoban_tiles, 0, 2*SOKOBAN_TILESIZE,
971 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
972 SOKOBAN_TILESIZE);
973 break;
975 case '*': /* box on goal */
976 rb->lcd_bitmap_part(sokoban_tiles, 0, 3*SOKOBAN_TILESIZE,
977 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
978 SOKOBAN_TILESIZE);
979 break;
981 case '.': /* goal */
982 rb->lcd_bitmap_part(sokoban_tiles, 0, 4*SOKOBAN_TILESIZE,
983 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
984 SOKOBAN_TILESIZE);
985 break;
987 case '@': /* player */
988 rb->lcd_bitmap_part(sokoban_tiles, 0, 5*SOKOBAN_TILESIZE,
989 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
990 SOKOBAN_TILESIZE);
991 break;
993 case '+': /* player on goal */
994 rb->lcd_bitmap_part(sokoban_tiles, 0, 6*SOKOBAN_TILESIZE,
995 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
996 SOKOBAN_TILESIZE);
997 break;
1002 /* print out the screen */
1003 rb->lcd_update();
1006 static void draw_level(void)
1008 load_level();
1009 rb->lcd_clear_display();
1010 update_screen();
1013 static bool save(char *filename, bool solution)
1015 int fd;
1016 char *loc;
1017 DIR *dir;
1018 char dirname[MAX_PATH];
1020 rb->splash(0, "Saving...");
1022 /* Create dir if it doesn't exist */
1023 if ((loc = rb->strrchr(filename, '/')) != NULL) {
1024 rb->strncpy(dirname, filename, loc - filename);
1025 dirname[loc - filename] = '\0';
1026 if(!(dir = rb->opendir(dirname)))
1027 rb->mkdir(dirname);
1028 else
1029 rb->closedir(dir);
1032 if (filename[0] == '\0' ||
1033 (fd = rb->open(filename, O_WRONLY|O_CREAT|O_TRUNC)) < 0) {
1034 rb->splashf(HZ*2, "Unable to open %s", filename);
1035 return false;
1038 /* Sokoban: S/P for solution/progress : level number : current undo */
1039 rb->snprintf(buf, sizeof(buf), "Sokoban:%c:%d:%d\n", (solution ? 'S' : 'P'),
1040 current_info.level.index + 1, undo_info.current);
1041 rb->write(fd, buf, rb->strlen(buf));
1043 /* Filename of levelset */
1044 rb->write(fd, buffered_boards.filename,
1045 rb->strlen(buffered_boards.filename));
1046 rb->write(fd, "\n", 1);
1048 /* Full undo history */
1049 rb->write(fd, undo_info.history, undo_info.max);
1051 rb->close(fd);
1053 return true;
1056 static bool load(char *filename, bool silent)
1058 int fd;
1059 int i, n;
1060 int len;
1061 int button;
1062 bool play_solution;
1063 bool paused = false;
1064 unsigned short speed = 2;
1065 int delay[] = {HZ/2, HZ/3, HZ/4, HZ/6, HZ/8, HZ/12, HZ/16, HZ/25};
1067 if (filename[0] == '\0' || (fd = rb->open(filename, O_RDONLY)) < 0) {
1068 if (!silent)
1069 rb->splashf(HZ*2, "Unable to open %s", filename);
1070 return false;
1073 /* Read header, level number, & current undo */
1074 rb->read_line(fd, buf, sizeof(buf));
1076 /* If we're opening a level file, not a solution/progress file */
1077 if (rb->strncmp(buf, "Sokoban", 7) != 0) {
1078 rb->close(fd);
1080 rb->strncpy(buffered_boards.filename, filename, MAX_PATH);
1081 if (!read_levels(true))
1082 return false;
1084 current_info.level.index = 0;
1085 load_level();
1087 /* If there aren't any boxes to go or the player position wasn't set,
1088 * the file probably wasn't a Sokoban level file */
1089 if (current_info.level.boxes_to_go == 0 ||
1090 current_info.player.row == 0 || current_info.player.col == 0) {
1091 if (!silent)
1092 rb->splash(HZ*2, "File is not a Sokoban level file");
1093 return false;
1096 } else {
1098 /* Read filename of levelset */
1099 rb->read_line(fd, buffered_boards.filename,
1100 sizeof(buffered_boards.filename));
1102 /* Read full undo history */
1103 len = rb->read_line(fd, undo_info.history, MAX_UNDOS);
1105 /* Correct len when trailing \r's or \n's are counted */
1106 if (len > 2 && undo_info.history[len - 2] == '\0')
1107 len -= 2;
1108 else if (len > 1 && undo_info.history[len - 1] == '\0')
1109 len--;
1111 rb->close(fd);
1113 /* Check to see if we're going to play a solution or resume progress */
1114 play_solution = (buf[8] == 'S');
1116 /* Get level number */
1117 for (n = 0, i = 10; buf[i] >= '0' && buf[i] <= '9' && i < 15; i++)
1118 n = n*10 + buf[i] - '0';
1119 current_info.level.index = n - 1;
1121 /* Get undo index */
1122 for (n = 0, i++; buf[i] >= '0' && buf[i] <= '9' && i < 21; i++)
1123 n = n*10 + buf[i] - '0';
1124 if (n > len)
1125 n = len;
1126 undo_info.max = len;
1128 if (current_info.level.index < 0) {
1129 if (!silent)
1130 rb->splash(HZ*2, "Error loading level");
1131 return false;
1133 if (!read_levels(true))
1134 return false;
1135 if (current_info.level.index >= current_info.max_level) {
1136 if (!silent)
1137 rb->splash(HZ*2, "Error loading level");
1138 return false;
1141 load_level();
1143 if (play_solution) {
1144 rb->lcd_clear_display();
1145 update_screen();
1146 rb->sleep(2*delay[speed]);
1148 /* Replay solution until menu button is pressed */
1149 i = 0;
1150 while (true) {
1151 if (i < len) {
1152 if (!move(undo_info.history[i], true)) {
1153 n = i;
1154 break;
1156 rb->lcd_clear_display();
1157 update_screen();
1158 i++;
1159 } else
1160 paused = true;
1162 rb->sleep(delay[speed]);
1164 while ((button = rb->button_get(false)) || paused) {
1165 switch (button) {
1166 case SOKOBAN_MENU:
1167 /* Pretend the level is complete so we'll quit */
1168 current_info.level.boxes_to_go = 0;
1169 return true;
1171 case SOKOBAN_PAUSE:
1172 /* Toggle pause state */
1173 paused = !paused;
1174 break;
1176 case SOKOBAN_LEFT:
1177 case SOKOBAN_LEFT | BUTTON_REPEAT:
1178 /* Go back one move */
1179 if (paused) {
1180 if (undo())
1181 i--;
1182 rb->lcd_clear_display();
1183 update_screen();
1185 break;
1187 case SOKOBAN_RIGHT:
1188 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1189 /* Go forward one move */
1190 if (paused) {
1191 if (redo())
1192 i++;
1193 rb->lcd_clear_display();
1194 update_screen();
1196 break;
1198 case SOKOBAN_UP:
1199 case SOKOBAN_UP | BUTTON_REPEAT:
1200 /* Speed up */
1201 if (speed < sizeof(delay)/sizeof(int) - 1)
1202 speed++;
1203 break;
1205 case SOKOBAN_DOWN:
1206 case SOKOBAN_DOWN | BUTTON_REPEAT:
1207 /* Slow down */
1208 if (speed > 0)
1209 speed--;
1212 if (paused)
1213 rb->sleep(HZ/33);
1217 /* If level is complete, wait for keypress before quitting */
1218 if (current_info.level.boxes_to_go == 0)
1219 rb->button_get(true);
1221 } else {
1222 /* Advance to current undo */
1223 for (i = 0; i < n; i++) {
1224 if (!move(undo_info.history[i], true)) {
1225 n = i;
1226 break;
1230 rb->button_clear_queue();
1231 rb->lcd_clear_display();
1234 undo_info.current = n;
1237 return true;
1240 static int sokoban_menu(void)
1242 int button;
1243 int selection = 0;
1244 int i;
1245 bool menu_quit;
1246 int start_selected = 0;
1247 int prev_level = current_info.level.index;
1249 MENUITEM_STRINGLIST(menu, "Sokoban Menu", NULL,
1250 "Resume", "Select Level", "Audio Playback", "Keys",
1251 "Load Default Level Set", "Quit Without Saving",
1252 "Save Progress & Quit");
1254 do {
1255 menu_quit = true;
1256 selection = rb->do_menu(&menu, &start_selected, NULL, false);
1258 switch (selection) {
1259 case 0: /* Resume */
1260 break;
1262 case 1: /* Select level */
1263 current_info.level.index++;
1264 rb->set_int("Select Level", "", UNIT_INT,
1265 &current_info.level.index, NULL, 1, 1,
1266 current_info.max_level, NULL);
1267 current_info.level.index--;
1268 if (prev_level != current_info.level.index) {
1269 init_undo();
1270 draw_level();
1271 } else
1272 menu_quit = false;
1273 break;
1275 case 2: /* Audio playback control */
1276 playback_control(NULL);
1277 menu_quit = false;
1278 break;
1280 case 3: /* Keys */
1281 FOR_NB_SCREENS(i)
1282 rb->screens[i]->clear_display();
1283 rb->lcd_setfont(SOKOBAN_FONT);
1285 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
1286 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
1287 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1288 rb->lcd_putsxy(3, 16, "[ON] Undo");
1289 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1290 rb->lcd_putsxy(3, 36, "[F1] Down a Level");
1291 rb->lcd_putsxy(3, 46, "[F2] Restart Level");
1292 rb->lcd_putsxy(3, 56, "[F3] Up a Level");
1293 #elif CONFIG_KEYPAD == ONDIO_PAD
1294 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1295 rb->lcd_putsxy(3, 16, "[MODE] Undo");
1296 rb->lcd_putsxy(3, 26, "[MODE+DOWN] Redo");
1297 rb->lcd_putsxy(3, 36, "[MODE+LEFT] Previous Level");
1298 rb->lcd_putsxy(3, 46, "[MODE+UP] Restart Level");
1299 rb->lcd_putsxy(3, 56, "[MODE+RIGHT] Up Level");
1300 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
1301 (CONFIG_KEYPAD == IRIVER_H300_PAD)
1302 rb->lcd_putsxy(3, 6, "[STOP] Menu");
1303 rb->lcd_putsxy(3, 16, "[REC] Undo");
1304 rb->lcd_putsxy(3, 26, "[MODE] Redo");
1305 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1306 rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
1307 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1308 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1309 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1310 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1311 rb->lcd_putsxy(3, 6, "[SELECT+MENU] Menu");
1312 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1313 rb->lcd_putsxy(3, 26, "[SELECT+PLAY] Redo");
1314 rb->lcd_putsxy(3, 36, "[SELECT+LEFT] Previous Level");
1315 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Next Level");
1316 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
1317 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1318 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1319 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1320 rb->lcd_putsxy(3, 36, "[REC] Restart Level");
1321 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1322 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1323 rb->lcd_putsxy(3, 16, "[REW] Undo");
1324 rb->lcd_putsxy(3, 26, "[FF] Redo");
1325 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1326 rb->lcd_putsxy(3, 46, "[PLAY+RIGHT] Restart Level");
1327 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1328 #elif CONFIG_KEYPAD == GIGABEAT_PAD
1329 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1330 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1331 rb->lcd_putsxy(3, 26, "[A] Redo");
1332 rb->lcd_putsxy(3, 36, "[VOL-] Previous Level");
1333 rb->lcd_putsxy(3, 46, "[MENU] Restart Level");
1334 rb->lcd_putsxy(3, 56, "[VOL+] Next Level");
1335 #elif CONFIG_KEYPAD == SANSA_E200_PAD
1336 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1337 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1338 rb->lcd_putsxy(3, 26, "[REC] Redo");
1339 rb->lcd_putsxy(3, 36, "[SELECT+DOWN] Previous Level");
1340 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Restart Level");
1341 rb->lcd_putsxy(3, 56, "[SELECT+UP] Next Level");
1342 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
1343 rb->lcd_putsxy(3, 6, "[MENU] Menu");
1344 rb->lcd_putsxy(3, 16, "[VOL+] Undo");
1345 rb->lcd_putsxy(3, 26, "[VOL-] Redo");
1346 rb->lcd_putsxy(3, 36, "[PREV] Previous Level");
1347 rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
1348 rb->lcd_putsxy(3, 56, "[NEXT] Next Level");
1349 #endif
1351 #ifdef HAVE_TOUCHSCREEN
1352 rb->lcd_putsxy(3, 6, SOKOBAN_MENU_NAME " Menu");
1353 rb->lcd_putsxy(3, 16, SOKOBAN_UNDO_NAME " Undo");
1354 rb->lcd_putsxy(3, 26, SOKOBAN_REDO_NAME " Redo");
1355 rb->lcd_putsxy(3, 36, SOKOBAN_PAUSE_NAME " Pause");
1356 rb->lcd_putsxy(3, 46, SOKOBAN_LEVEL_REPEAT_NAME " Restart Level");
1357 #endif
1359 FOR_NB_SCREENS(i)
1360 rb->screens[i]->update();
1362 /* Display until keypress */
1363 do {
1364 rb->sleep(HZ/20);
1365 button = rb->button_get(false);
1366 } while (!button || button & BUTTON_REL ||
1367 button & BUTTON_REPEAT);
1369 menu_quit = false;
1370 break;
1372 case 4: /* Load default levelset */
1373 init_boards();
1374 if (!read_levels(true))
1375 return 5; /* Quit */
1376 load_level();
1377 break;
1379 case 5: /* Quit */
1380 break;
1382 case 6: /* Save & quit */
1383 save(SOKOBAN_SAVE_FILE, false);
1384 rb->reload_directory();
1387 } while (!menu_quit);
1389 /* Restore font */
1390 rb->lcd_setfont(SOKOBAN_FONT);
1392 FOR_NB_SCREENS(i) {
1393 rb->screens[i]->clear_display();
1394 rb->screens[i]->update();
1397 return selection;
1400 static bool sokoban_loop(void)
1402 bool moved;
1403 int i = 0, button = 0, lastbutton = 0;
1404 short r = 0, c = 0;
1405 int w, h;
1406 char *loc;
1408 while (true) {
1409 moved = false;
1411 r = current_info.player.row;
1412 c = current_info.player.col;
1414 button = rb->button_get(true);
1416 switch(button)
1418 #ifdef SOKOBAN_RC_MENU
1419 case SOKOBAN_RC_MENU:
1420 #endif
1421 case SOKOBAN_MENU:
1422 switch (sokoban_menu()) {
1423 case 5: /* Quit */
1424 case 6: /* Save & quit */
1425 return PLUGIN_OK;
1427 update_screen();
1428 break;
1430 case SOKOBAN_UNDO:
1431 #ifdef SOKOBAN_UNDO_PRE
1432 if (lastbutton != SOKOBAN_UNDO_PRE)
1433 break;
1434 #else /* repeat can't work here for Ondio, iPod, et al */
1435 case SOKOBAN_UNDO | BUTTON_REPEAT:
1436 #endif
1437 undo();
1438 rb->lcd_clear_display();
1439 update_screen();
1440 break;
1442 #ifdef SOKOBAN_REDO
1443 case SOKOBAN_REDO:
1444 case SOKOBAN_REDO | BUTTON_REPEAT:
1445 moved = redo();
1446 rb->lcd_clear_display();
1447 update_screen();
1448 break;
1449 #endif
1451 #ifdef SOKOBAN_LEVEL_UP
1452 case SOKOBAN_LEVEL_UP:
1453 case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
1454 /* next level */
1455 init_undo();
1456 if (current_info.level.index + 1 < current_info.max_level)
1457 current_info.level.index++;
1459 draw_level();
1460 break;
1461 #endif
1463 #ifdef SOKOBAN_LEVEL_DOWN
1464 case SOKOBAN_LEVEL_DOWN:
1465 case SOKOBAN_LEVEL_DOWN | BUTTON_REPEAT:
1466 /* previous level */
1467 init_undo();
1468 if (current_info.level.index > 0)
1469 current_info.level.index--;
1471 draw_level();
1472 break;
1473 #endif
1475 #ifdef SOKOBAN_LEVEL_REPEAT
1476 case SOKOBAN_LEVEL_REPEAT:
1477 case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
1478 /* same level */
1479 init_undo();
1480 draw_level();
1481 break;
1482 #endif
1484 case SOKOBAN_LEFT:
1485 case SOKOBAN_LEFT | BUTTON_REPEAT:
1486 moved = move(SOKOBAN_MOVE_LEFT, false);
1487 break;
1489 case SOKOBAN_RIGHT:
1490 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1491 moved = move(SOKOBAN_MOVE_RIGHT, false);
1492 break;
1494 case SOKOBAN_UP:
1495 case SOKOBAN_UP | BUTTON_REPEAT:
1496 moved = move(SOKOBAN_MOVE_UP, false);
1497 break;
1499 case SOKOBAN_DOWN:
1500 case SOKOBAN_DOWN | BUTTON_REPEAT:
1501 moved = move(SOKOBAN_MOVE_DOWN, false);
1502 break;
1504 default:
1505 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
1506 return PLUGIN_USB_CONNECTED;
1507 break;
1510 lastbutton = button;
1512 if (moved) {
1513 rb->lcd_clear_display();
1514 update_screen();
1517 /* We have completed this level */
1518 if (current_info.level.boxes_to_go == 0) {
1520 if (moved) {
1521 rb->lcd_clear_display();
1523 /* Show level complete message & stats */
1524 rb->snprintf(buf, sizeof(buf), "Level %d Complete!",
1525 current_info.level.index + 1);
1526 rb->lcd_getstringsize(buf, &w, &h);
1527 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h*3, buf);
1529 rb->snprintf(buf, sizeof(buf), "%4d Moves ",
1530 current_info.level.moves);
1531 rb->lcd_getstringsize(buf, &w, &h);
1532 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h, buf);
1534 rb->snprintf(buf, sizeof(buf), "%4d Pushes",
1535 current_info.level.pushes);
1536 rb->lcd_getstringsize(buf, &w, &h);
1537 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2, buf);
1539 if (undo_info.count < MAX_UNDOS) {
1540 rb->snprintf(buf, sizeof(buf), "%s: Save solution",
1541 BUTTON_SAVE_NAME);
1542 rb->lcd_getstringsize(buf, &w, &h);
1543 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + h*2, buf);
1546 rb->lcd_update();
1547 rb->sleep(HZ/4);
1548 rb->button_clear_queue();
1550 /* Display for 4 seconds or until new keypress */
1551 for (i = 0; i < 80; i++) {
1552 rb->sleep(HZ/20);
1553 button = rb->button_get(false);
1554 if (button && !(button & BUTTON_REL) &&
1555 !(button & BUTTON_REPEAT))
1556 break;
1559 if (button == BUTTON_SAVE) {
1560 if (undo_info.count < MAX_UNDOS) {
1561 /* Set filename to current levelset plus level number
1562 * and .sok extension. Use SAVE_FOLDER if using the
1563 * default levelset, since it's in a hidden folder. */
1564 if (rb->strcmp(buffered_boards.filename,
1565 SOKOBAN_LEVELS_FILE) == 0) {
1566 rb->snprintf(buf, sizeof(buf),
1567 "%s/sokoban.%d.sok",
1568 SOKOBAN_SAVE_FOLDER,
1569 current_info.level.index + 1);
1570 } else {
1571 if ((loc = rb->strrchr(buffered_boards.filename,
1572 '.')) != NULL)
1573 *loc = '\0';
1574 rb->snprintf(buf, sizeof(buf), "%s.%d.sok",
1575 buffered_boards.filename,
1576 current_info.level.index + 1);
1577 if (loc != NULL)
1578 *loc = '.';
1581 if (!rb->kbd_input(buf, MAX_PATH))
1582 save(buf, true);
1583 } else
1584 rb->splash(HZ*2, "Solution too long to save");
1586 rb->lcd_setfont(SOKOBAN_FONT); /* Restore font */
1590 FOR_NB_SCREENS(i) {
1591 rb->screens[i]->clear_display();
1592 rb->screens[i]->update();
1595 current_info.level.index++;
1597 /* clear undo stats */
1598 init_undo();
1600 if (current_info.level.index >= current_info.max_level) {
1601 /* Show levelset complete message */
1602 rb->snprintf(buf, sizeof(buf), "You WIN!!");
1603 rb->lcd_getstringsize(buf, &w, &h);
1604 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, buf);
1606 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1607 /* Display for 4 seconds or until keypress */
1608 for (i = 0; i < 80; i++) {
1609 rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
1610 rb->lcd_update();
1611 rb->sleep(HZ/10);
1613 button = rb->button_get(false);
1614 if (button && !(button & BUTTON_REL))
1615 break;
1617 rb->lcd_set_drawmode(DRMODE_SOLID);
1619 /* Reset to first level & show quit menu */
1620 current_info.level.index = 0;
1622 switch (sokoban_menu()) {
1623 case 5: /* Quit */
1624 case 6: /* Save & quit */
1625 return PLUGIN_OK;
1629 load_level();
1630 update_screen();
1633 } /* end while */
1635 return PLUGIN_OK;
1639 enum plugin_status plugin_start(const void* parameter)
1641 int w, h;
1643 (void)(parameter);
1645 rb->lcd_setfont(SOKOBAN_FONT);
1647 rb->lcd_clear_display();
1648 rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
1649 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, SOKOBAN_TITLE);
1650 rb->lcd_update();
1651 rb->sleep(HZ); /* Show title for 1 second */
1653 init_boards();
1655 if (parameter == NULL) {
1656 /* Attempt to resume saved progress, otherwise start at beginning */
1657 if (!load(SOKOBAN_SAVE_FILE, true)) {
1658 init_boards();
1659 if (!read_levels(true))
1660 return PLUGIN_OK;
1661 load_level();
1664 } else {
1665 /* The plugin is being used to open a file */
1666 if (load((char*) parameter, false)) {
1667 /* If we loaded & played a solution, quit */
1668 if (current_info.level.boxes_to_go == 0)
1669 return PLUGIN_OK;
1670 } else
1671 return PLUGIN_OK;
1674 rb->lcd_clear_display();
1675 update_screen();
1677 return sokoban_loop();