Save a few bytes by changing unit selection strategy
[kugel-rb.git] / apps / plugins / sokoban.c
blobe031f6064aed50c7868120214bdb087816f0e5ca
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 * All files in this archive are subject to the GNU General Public License.
16 * See the file COPYING in the source tree root for full license agreement.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "plugin.h"
23 #include "lib/playback_control.h"
25 #ifdef HAVE_LCD_BITMAP
27 PLUGIN_HEADER
29 #if LCD_DEPTH >= 2 && ((LCD_HEIGHT >= 96 && LCD_WIDTH >= 152) || \
30 (LCD_HEIGHT >= 121 && LCD_WIDTH >= 120))
31 extern const fb_data sokoban_tiles[];
32 #endif
34 #define SOKOBAN_TITLE "Sokoban"
36 #define SOKOBAN_LEVELS_FILE PLUGIN_GAMES_DIR "/sokoban.levels"
37 #define SOKOBAN_SAVE_FILE PLUGIN_GAMES_DIR "/sokoban.save"
38 #define SOKOBAN_SAVE_FOLDER "/games"
40 /* Magnify is the number of pixels for each block.
41 * Set dynamically so all targets can support levels
42 * that fill their entire screen, less the stat box.
43 * 16 rows & 20 cols minimum */
44 #if (LCD_HEIGHT >= 224) && (LCD_WIDTH >= 320)
45 #define MAGNIFY 14
46 #define ROWS (LCD_HEIGHT/MAGNIFY)
47 #define COLS ((LCD_WIDTH-40)/MAGNIFY)
48 #elif (LCD_HEIGHT >= 249) && (LCD_WIDTH >= 280)
49 #define MAGNIFY 14
50 #define ROWS ((LCD_HEIGHT-25)/MAGNIFY)
51 #define COLS (LCD_WIDTH/MAGNIFY)
52 #elif (LCD_HEIGHT >= 144) && (LCD_WIDTH >= 220)
53 #define MAGNIFY 9
54 #define ROWS (LCD_HEIGHT/MAGNIFY)
55 #define COLS ((LCD_WIDTH-40)/MAGNIFY)
56 #elif (LCD_HEIGHT >= 169) && (LCD_WIDTH+4 >= 180) /* plus 4 for sansa */
57 #define MAGNIFY 9
58 #define ROWS ((LCD_HEIGHT-25)/MAGNIFY)
59 #define COLS ((LCD_WIDTH+4)/MAGNIFY)
60 #elif (LCD_HEIGHT >= 96) && (LCD_WIDTH >= 160)
61 #define MAGNIFY 6
62 #define ROWS (LCD_HEIGHT/MAGNIFY)
63 #define COLS ((LCD_WIDTH-40)/MAGNIFY)
64 #elif (LCD_HEIGHT >= 121) && (LCD_WIDTH >= 120)
65 #define MAGNIFY 6
66 #define ROWS ((LCD_HEIGHT-25)/MAGNIFY)
67 #define COLS (LCD_WIDTH/MAGNIFY)
68 #else
69 #define MAGNIFY 4
70 #define ROWS 16
71 #define COLS 20
72 #endif
74 /* Use either all but 16k of the plugin buffer for level data
75 * or 128k, which ever is less */
76 #if PLUGIN_BUFFER_SIZE - 0x4000 < 0x20000
77 #define MAX_LEVEL_DATA (PLUGIN_BUFFER_SIZE - 0x4000)
78 #else
79 #define MAX_LEVEL_DATA 0x20000
80 #endif
82 /* Number of levels for which to allocate buffer indexes */
83 #define MAX_LEVELS MAX_LEVEL_DATA/70
85 /* Use 4k plus remaining plugin buffer (-12k for prog) for undo, up to 64k */
86 #if PLUGIN_BUFFER_SIZE - MAX_LEVEL_DATA - 0x3000 > 0x10000
87 #define MAX_UNDOS 0x10000
88 #else
89 #define MAX_UNDOS (PLUGIN_BUFFER_SIZE - MAX_LEVEL_DATA - 0x3000)
90 #endif
92 /* Move/push definitions for undo */
93 #define SOKOBAN_PUSH_LEFT 'L'
94 #define SOKOBAN_PUSH_RIGHT 'R'
95 #define SOKOBAN_PUSH_UP 'U'
96 #define SOKOBAN_PUSH_DOWN 'D'
97 #define SOKOBAN_MOVE_LEFT 'l'
98 #define SOKOBAN_MOVE_RIGHT 'r'
99 #define SOKOBAN_MOVE_UP 'u'
100 #define SOKOBAN_MOVE_DOWN 'd'
102 #define SOKOBAN_MOVE_DIFF (SOKOBAN_MOVE_LEFT-SOKOBAN_PUSH_LEFT)
103 #define SOKOBAN_MOVE_MIN SOKOBAN_MOVE_DOWN
105 /* variable button definitions */
106 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
107 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
108 #define SOKOBAN_UP BUTTON_UP
109 #define SOKOBAN_DOWN BUTTON_DOWN
110 #define SOKOBAN_MENU BUTTON_OFF
111 #define SOKOBAN_UNDO BUTTON_ON
112 #define SOKOBAN_REDO BUTTON_PLAY
113 #define SOKOBAN_LEVEL_DOWN BUTTON_F1
114 #define SOKOBAN_LEVEL_REPEAT BUTTON_F2
115 #define SOKOBAN_LEVEL_UP BUTTON_F3
116 #define SOKOBAN_PAUSE BUTTON_PLAY
117 #define BUTTON_SAVE BUTTON_ON
118 #define BUTTON_SAVE_NAME "ON"
120 #elif CONFIG_KEYPAD == ONDIO_PAD
121 #define SOKOBAN_UP BUTTON_UP
122 #define SOKOBAN_DOWN BUTTON_DOWN
123 #define SOKOBAN_MENU BUTTON_OFF
124 #define SOKOBAN_UNDO_PRE BUTTON_MENU
125 #define SOKOBAN_UNDO (BUTTON_MENU | BUTTON_REL)
126 #define SOKOBAN_REDO (BUTTON_MENU | BUTTON_DOWN)
127 #define SOKOBAN_LEVEL_DOWN (BUTTON_MENU | BUTTON_LEFT)
128 #define SOKOBAN_LEVEL_REPEAT (BUTTON_MENU | BUTTON_UP)
129 #define SOKOBAN_LEVEL_UP (BUTTON_MENU | BUTTON_RIGHT)
130 #define SOKOBAN_PAUSE BUTTON_MENU
131 #define BUTTON_SAVE BUTTON_MENU
132 #define BUTTON_SAVE_NAME "MENU"
134 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
135 (CONFIG_KEYPAD == IRIVER_H300_PAD)
136 #define SOKOBAN_UP BUTTON_UP
137 #define SOKOBAN_DOWN BUTTON_DOWN
138 #define SOKOBAN_MENU BUTTON_OFF
139 #define SOKOBAN_UNDO BUTTON_REC
140 #define SOKOBAN_REDO BUTTON_MODE
141 #define SOKOBAN_LEVEL_DOWN (BUTTON_ON | BUTTON_DOWN)
142 #define SOKOBAN_LEVEL_REPEAT BUTTON_ON
143 #define SOKOBAN_LEVEL_UP (BUTTON_ON | BUTTON_UP)
144 #define SOKOBAN_PAUSE BUTTON_ON
145 #define BUTTON_SAVE BUTTON_MODE
146 #define BUTTON_SAVE_NAME "MODE"
148 #define SOKOBAN_RC_MENU BUTTON_RC_STOP
150 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
151 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
152 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
153 #define SOKOBAN_UP BUTTON_MENU
154 #define SOKOBAN_DOWN BUTTON_PLAY
155 #define SOKOBAN_MENU (BUTTON_SELECT | BUTTON_MENU)
156 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
157 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
158 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_PLAY)
159 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_LEFT)
160 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_RIGHT)
161 #define SOKOBAN_PAUSE BUTTON_SELECT
162 #define BUTTON_SAVE BUTTON_SELECT
163 #define BUTTON_SAVE_NAME "SELECT"
165 /* FIXME: if/when simultaneous button presses work for X5/M5,
166 * add level up/down */
167 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
168 #define SOKOBAN_UP BUTTON_UP
169 #define SOKOBAN_DOWN BUTTON_DOWN
170 #define SOKOBAN_MENU BUTTON_POWER
171 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
172 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
173 #define SOKOBAN_LEVEL_REPEAT BUTTON_REC
174 #define SOKOBAN_REDO BUTTON_PLAY
175 #define SOKOBAN_PAUSE BUTTON_PLAY
176 #define BUTTON_SAVE BUTTON_SELECT
177 #define BUTTON_SAVE_NAME "SELECT"
179 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
180 #define SOKOBAN_UP BUTTON_SCROLL_UP
181 #define SOKOBAN_DOWN BUTTON_SCROLL_DOWN
182 #define SOKOBAN_MENU BUTTON_POWER
183 #define SOKOBAN_UNDO_PRE BUTTON_REW
184 #define SOKOBAN_UNDO (BUTTON_REW | BUTTON_REL)
185 #define SOKOBAN_REDO BUTTON_FF
186 #define SOKOBAN_LEVEL_DOWN (BUTTON_PLAY | BUTTON_SCROLL_DOWN)
187 #define SOKOBAN_LEVEL_REPEAT (BUTTON_PLAY | BUTTON_RIGHT)
188 #define SOKOBAN_LEVEL_UP (BUTTON_PLAY | BUTTON_SCROLL_UP)
189 #define SOKOBAN_PAUSE BUTTON_PLAY
190 #define BUTTON_SAVE BUTTON_PLAY
191 #define BUTTON_SAVE_NAME "PLAY"
193 #elif CONFIG_KEYPAD == GIGABEAT_PAD
194 #define SOKOBAN_UP BUTTON_UP
195 #define SOKOBAN_DOWN BUTTON_DOWN
196 #define SOKOBAN_MENU BUTTON_POWER
197 #define SOKOBAN_UNDO BUTTON_SELECT
198 #define SOKOBAN_REDO BUTTON_A
199 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
200 #define SOKOBAN_LEVEL_REPEAT BUTTON_MENU
201 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
202 #define SOKOBAN_PAUSE BUTTON_SELECT
203 #define BUTTON_SAVE BUTTON_SELECT
204 #define BUTTON_SAVE_NAME "SELECT"
206 #elif CONFIG_KEYPAD == SANSA_E200_PAD
207 #define SOKOBAN_UP BUTTON_UP
208 #define SOKOBAN_DOWN BUTTON_DOWN
209 #define SOKOBAN_MENU BUTTON_POWER
210 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
211 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
212 #define SOKOBAN_REDO BUTTON_REC
213 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
214 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
215 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
216 #define SOKOBAN_PAUSE BUTTON_SELECT
217 #define BUTTON_SAVE BUTTON_SELECT
218 #define BUTTON_SAVE_NAME "SELECT"
220 #endif
222 #define SOKOBAN_FONT FONT_SYSFIXED
225 /* The Location, Undo and LevelInfo structs are OO-flavored.
226 * (oooh!-flavored as Schnueff puts it.) It makes more you have to know,
227 * but the overall data layout becomes more manageable. */
229 /* Level data & stats */
230 struct LevelInfo {
231 int index; /* Level index (level number - 1) */
232 int moves; /* Moves & pushes for the stats */
233 int pushes;
234 short boxes_to_go; /* Number of unplaced boxes remaining in level */
235 short height; /* Height & width for centering level display */
236 short width;
239 struct Location {
240 short row;
241 short col;
244 /* Our full undo history */
245 static struct UndoInfo {
246 int count; /* How many undos have been done */
247 int current; /* Which history is the current undo */
248 int max; /* Which history is the max redoable */
249 char history[MAX_UNDOS];
250 } undo_info;
252 /* Our playing board */
253 static struct BoardInfo {
254 char board[ROWS][COLS]; /* The current board data */
255 struct LevelInfo level; /* Level data & stats */
256 struct Location player; /* Where the player is */
257 int max_level; /* The number of levels we have */
258 } current_info;
260 static struct BufferedBoards {
261 char filename[MAX_PATH]; /* Filename of the levelset we're using */
262 char data[MAX_LEVEL_DATA]; /* Buffered level data */
263 int index[MAX_LEVELS + 1]; /* Where each buffered board begins & ends */
264 int start; /* Index of first buffered board */
265 int end; /* Index of last buffered board */
266 short prebuffered_boards; /* Number of boards before current to store */
267 } buffered_boards;
270 static struct plugin_api* rb;
271 MEM_FUNCTION_WRAPPERS(rb);
273 static char buf[ROWS*(COLS + 1)]; /* Enough for a whole board or a filename */
276 static void init_undo(void)
278 undo_info.count = 0;
279 undo_info.current = 0;
280 undo_info.max = 0;
283 static void get_delta(char direction, short *d_r, short *d_c)
285 switch (direction) {
286 case SOKOBAN_PUSH_LEFT:
287 case SOKOBAN_MOVE_LEFT:
288 *d_r = 0;
289 *d_c = -1;
290 break;
291 case SOKOBAN_PUSH_RIGHT:
292 case SOKOBAN_MOVE_RIGHT:
293 *d_r = 0;
294 *d_c = 1;
295 break;
296 case SOKOBAN_PUSH_UP:
297 case SOKOBAN_MOVE_UP:
298 *d_r = -1;
299 *d_c = 0;
300 break;
301 case SOKOBAN_PUSH_DOWN:
302 case SOKOBAN_MOVE_DOWN:
303 *d_r = 1;
304 *d_c = 0;
308 static bool undo(void)
310 char undo;
311 short r, c;
312 short d_r = 0, d_c = 0; /* delta row & delta col */
313 char *space_cur, *space_next, *space_prev;
314 bool undo_push = false;
316 /* If no more undos or we've wrapped all the way around, quit */
317 if (undo_info.count == 0 || undo_info.current - 1 == undo_info.max)
318 return false;
320 /* Move to previous undo in the list */
321 if (undo_info.current == 0 && undo_info.count > 1)
322 undo_info.current = MAX_UNDOS - 1;
323 else
324 undo_info.current--;
326 undo_info.count--;
328 undo = undo_info.history[undo_info.current];
330 if (undo < SOKOBAN_MOVE_MIN)
331 undo_push = true;
333 get_delta(undo, &d_r, &d_c);
335 r = current_info.player.row;
336 c = current_info.player.col;
338 /* Give the 3 spaces we're going to use better names */
339 space_cur = &current_info.board[r][c];
340 space_next = &current_info.board[r + d_r][c + d_c];
341 space_prev = &current_info.board[r - d_r][c - d_c];
343 /* Update board info */
344 if (undo_push) {
345 /* Moving box from goal to floor */
346 if (*space_next == '*' && *space_cur == '@')
347 current_info.level.boxes_to_go++;
348 /* Moving box from floor to goal */
349 else if (*space_next == '$' && *space_cur == '+')
350 current_info.level.boxes_to_go--;
352 /* Move box off of next space... */
353 *space_next = (*space_next == '*' ? '.' : ' ');
354 /* ...and on to current space */
355 *space_cur = (*space_cur == '+' ? '*' : '$');
357 current_info.level.pushes--;
358 } else
359 /* Just move player off of current space */
360 *space_cur = (*space_cur == '+' ? '.' : ' ');
361 /* Move player back to previous space */
362 *space_prev = (*space_prev == '.' ? '+' : '@');
364 /* Update position */
365 current_info.player.row -= d_r;
366 current_info.player.col -= d_c;
368 current_info.level.moves--;
370 return true;
373 static void add_undo(char undo)
375 undo_info.history[undo_info.current] = undo;
377 /* Wrap around if MAX_UNDOS exceeded */
378 if (undo_info.current < (MAX_UNDOS - 1))
379 undo_info.current++;
380 else
381 undo_info.current = 0;
383 if (undo_info.count < MAX_UNDOS)
384 undo_info.count++;
387 static bool move(char direction, bool redo)
389 short r, c;
390 short d_r = 0, d_c = 0; /* delta row & delta col */
391 char *space_cur, *space_next, *space_beyond;
392 bool push = false;
394 get_delta(direction, &d_r, &d_c);
396 r = current_info.player.row;
397 c = current_info.player.col;
399 /* Check for out-of-bounds */
400 if (r + 2*d_r < 0 || r + 2*d_r >= ROWS ||
401 c + 2*d_c < 0 || c + 2*d_c >= COLS)
402 return false;
404 /* Give the 3 spaces we're going to use better names */
405 space_cur = &current_info.board[r][c];
406 space_next = &current_info.board[r + d_r][c + d_c];
407 space_beyond = &current_info.board[r + 2*d_r][c + 2*d_c];
409 if (*space_next == '$' || *space_next == '*') {
410 /* Change direction from move to push for undo */
411 if (direction >= SOKOBAN_MOVE_MIN)
412 direction -= SOKOBAN_MOVE_DIFF;
413 push = true;
415 else if (direction < SOKOBAN_MOVE_MIN)
416 /* Change back to move if redo/solution playback push is invalid */
417 direction += SOKOBAN_MOVE_DIFF;
419 /* Update board info */
420 if (push) {
421 /* Moving box from goal to floor */
422 if (*space_next == '*' && *space_beyond == ' ')
423 current_info.level.boxes_to_go++;
424 /* Moving box from floor to goal */
425 else if (*space_next == '$' && *space_beyond == '.')
426 current_info.level.boxes_to_go--;
427 /* Check for invalid move */
428 else if (*space_beyond != '.' && *space_beyond != ' ')
429 return false;
431 /* Move player onto next space */
432 *space_next = (*space_next == '*' ? '+' : '@');
433 /* Move box onto space beyond next */
434 *space_beyond = (*space_beyond == '.' ? '*' : '$');
436 current_info.level.pushes++;
437 } else {
438 /* Check for invalid move */
439 if (*space_next != '.' && *space_next != ' ')
440 return false;
442 /* Move player onto next space */
443 *space_next = (*space_next == '.' ? '+' : '@');
445 /* Move player off of current space */
446 *space_cur = (*space_cur == '+' ? '.' : ' ');
448 /* Update position */
449 current_info.player.row += d_r;
450 current_info.player.col += d_c;
452 current_info.level.moves++;
454 /* Update undo_info.max to current on every normal move,
455 * except if it's the same as a redo. */
456 /* normal move and either */
457 if (!redo &&
458 /* moves have been undone... */
459 ((undo_info.max != undo_info.current &&
460 /* ...and the current move is NOT the same as the one in history */
461 undo_info.history[undo_info.current] != direction) ||
462 /* or moves have not been undone */
463 undo_info.max == undo_info.current)) {
464 add_undo(direction);
465 undo_info.max = undo_info.current;
466 } else /* redo move or move was same as redo */
467 add_undo(direction); /* add_undo to update current */
469 return true;
472 #ifdef SOKOBAN_REDO
473 static bool redo(void)
475 /* If no moves have been undone, quit */
476 if (undo_info.current == undo_info.max)
477 return false;
479 return move(undo_info.history[(undo_info.current < MAX_UNDOS ?
480 undo_info.current : 0)], true);
482 #endif
484 static void init_boards(void)
486 rb->strncpy(buffered_boards.filename, SOKOBAN_LEVELS_FILE, MAX_PATH);
488 current_info.level.index = 0;
489 current_info.player.row = 0;
490 current_info.player.col = 0;
491 current_info.max_level = 0;
493 buffered_boards.start = 0;
494 buffered_boards.end = 0;
495 buffered_boards.prebuffered_boards = 0;
497 init_undo();
500 static bool read_levels(bool initialize)
502 int fd = 0;
503 short len;
504 short lastlen = 0;
505 short row = 0;
506 int level_count = 0;
508 int i = 0;
509 int level_len = 0;
510 bool index_set = false;
512 /* Get the index of the first level to buffer */
513 if (current_info.level.index > buffered_boards.prebuffered_boards &&
514 !initialize)
515 buffered_boards.start = current_info.level.index -
516 buffered_boards.prebuffered_boards;
517 else
518 buffered_boards.start = 0;
520 if ((fd = rb->open(buffered_boards.filename, O_RDONLY)) < 0) {
521 rb->splash(HZ*2, "Unable to open %s", buffered_boards.filename);
522 return false;
525 do {
526 len = rb->read_line(fd, buf, sizeof(buf));
528 /* Correct len when trailing \r's or \n's are counted */
529 if (len > 2 && buf[len - 2] == '\0')
530 len -= 2;
531 else if (len > 1 && buf[len - 1] == '\0')
532 len--;
534 /* Skip short lines & lines with non-level data */
535 if (len >= 3 && ((buf[0] >= '1' && buf[0] <= '9') || buf[0] == '#' ||
536 buf[0] == ' ' || buf[0] == '-' || buf[0] == '_')) {
537 if (level_count >= buffered_boards.start) {
538 /* Set the index of this level */
539 if (!index_set &&
540 level_count - buffered_boards.start < MAX_LEVELS) {
541 buffered_boards.index[level_count - buffered_boards.start]
542 = i;
543 index_set = true;
545 /* Copy buffer to board data */
546 if (i + level_len + len < MAX_LEVEL_DATA) {
547 rb->memcpy(&buffered_boards.data[i + level_len], buf, len);
548 buffered_boards.data[i + level_len + len] = '\n';
551 level_len += len + 1;
552 row++;
554 /* If newline & level is tall enough or is RLE */
555 } else if (buf[0] == '\0' && (row > 2 || lastlen > 22)) {
556 level_count++;
557 if (level_count >= buffered_boards.start) {
558 i += level_len;
559 if (i < MAX_LEVEL_DATA)
560 buffered_boards.end = level_count;
561 else if (!initialize)
562 break;
564 row = 0;
565 level_len = 0;
566 index_set = false;
568 } else if (len > 22)
569 len = 1;
571 } while ((lastlen = len));
573 /* Set the index of the end of the last level */
574 if (level_count - buffered_boards.start < MAX_LEVELS)
575 buffered_boards.index[level_count - buffered_boards.start] = i;
577 if (initialize) {
578 current_info.max_level = level_count;
579 buffered_boards.prebuffered_boards = buffered_boards.end/2;
582 rb->close(fd);
584 return true;
587 static void load_level(void)
589 int c, r;
590 int i, n;
591 int level_size;
592 int index = current_info.level.index - buffered_boards.start;
593 char *level;
595 /* Get the buffered board index of the current level */
596 if (current_info.level.index < buffered_boards.start ||
597 current_info.level.index >= buffered_boards.end) {
598 read_levels(false);
599 if (current_info.level.index > buffered_boards.prebuffered_boards)
600 index = buffered_boards.prebuffered_boards;
601 else
602 index = current_info.level.index;
604 level = &buffered_boards.data[buffered_boards.index[index]];
606 /* Reset level info */
607 current_info.level.moves = 0;
608 current_info.level.pushes = 0;
609 current_info.level.boxes_to_go = 0;
610 current_info.level.width = 0;
612 /* Clear board */
613 for (r = 0; r < ROWS; r++)
614 for (c = 0; c < COLS; c++)
615 current_info.board[r][c] = 'X';
617 level_size = buffered_boards.index[index + 1] -
618 buffered_boards.index[index];
620 for (r = 0, c = 0, n = 1, i = 0; i < level_size; i++) {
621 if (level[i] == '\n' || level[i] == '|') {
622 if (c > 3) {
623 /* Update max width of level & go to next row */
624 if (c > current_info.level.width)
625 current_info.level.width = c;
626 c = 0;
627 r++;
628 if (r >= ROWS)
629 break;
631 } else if (c < COLS) {
632 /* Read RLE character's length into n */
633 if (level[i] >= '0' && level[i] <= '9') {
634 n = level[i++] - '0';
635 if (level[i] >= '0' && level[i] <= '9')
636 n = n*10 + level[i++] - '0';
639 /* Cleanup & replace */
640 if (level[i] == '%')
641 level[i] = '*';
642 else if (level[i] == '-' || level[i] == '_')
643 level[i] = ' ';
645 if (n > 1) {
646 if (c + n >= COLS)
647 n = COLS - c;
649 if (level[i] == '.')
650 current_info.level.boxes_to_go += n;
652 /* Put RLE character n times */
653 while (n--)
654 current_info.board[r][c++] = level[i];
655 n = 1;
657 } else {
658 if (level[i] == '.' || level[i] == '+')
659 current_info.level.boxes_to_go++;
661 if (level[i] == '@' ||level[i] == '+') {
662 current_info.player.row = r;
663 current_info.player.col = c;
666 current_info.board[r][c++] = level[i];
671 current_info.level.height = r;
673 #if LCD_DEPTH > 2
674 /* Fill in blank space outside level on color targets */
675 for (r = 0; r < ROWS; r++)
676 for (c = 0; current_info.board[r][c] == ' ' && c < COLS; c++)
677 current_info.board[r][c] = 'X';
679 for (c = 0; c < COLS; c++) {
680 for (r = 0; (current_info.board[r][c] == ' ' ||
681 current_info.board[r][c] == 'X') && r < ROWS; r++)
682 current_info.board[r][c] = 'X';
683 for (r = ROWS - 1; (current_info.board[r][c] == ' ' ||
684 current_info.board[r][c] == 'X') && r >= 0; r--)
685 current_info.board[r][c] = 'X';
687 #endif
690 static void update_screen(void)
692 int c, r;
693 int rows, cols;
695 #if LCD_DEPTH < 2 || ((LCD_HEIGHT < 96 || LCD_WIDTH < 152) && \
696 (LCD_HEIGHT < 121 || LCD_WIDTH < 120))
697 int i, j;
698 int max = MAGNIFY - 1;
699 int middle = max/2;
700 int ldelta = (middle + 1)/2;
701 #endif
703 #if LCD_WIDTH - (COLS*MAGNIFY) < 32
704 #define STAT_HEIGHT 25
705 #define STAT_X (LCD_WIDTH - 120)/2
706 #define STAT_Y (LCD_HEIGHT - STAT_HEIGHT)
707 #define BOARD_WIDTH LCD_WIDTH
708 #define BOARD_HEIGHT (LCD_HEIGHT - STAT_HEIGHT)
709 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 4, "Level");
710 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
711 rb->lcd_putsxy(STAT_X + 7, STAT_Y + 14, buf);
712 rb->lcd_putsxy(STAT_X + 41, STAT_Y + 4, "Moves");
713 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
714 rb->lcd_putsxy(STAT_X + 44, STAT_Y + 14, buf);
715 rb->lcd_putsxy(STAT_X + 79, STAT_Y + 4, "Pushes");
716 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
717 rb->lcd_putsxy(STAT_X + 82, STAT_Y + 14, buf);
719 rb->lcd_drawrect(STAT_X, STAT_Y, 38, STAT_HEIGHT);
720 rb->lcd_drawrect(STAT_X + 37, STAT_Y, 39, STAT_HEIGHT);
721 rb->lcd_drawrect(STAT_X + 75, STAT_Y, 45, STAT_HEIGHT);
722 #else
723 #if LCD_WIDTH - (COLS*MAGNIFY) > 40
724 #define STAT_X (LCD_WIDTH - 40)
725 #else
726 #define STAT_X COLS*MAGNIFY
727 #endif
728 #if LCD_HEIGHT >= 70
729 #define STAT_Y (LCD_HEIGHT - 70)/2
730 #else
731 #define STAT_Y (LCD_HEIGHT - 47)/2
732 #endif
733 #define STAT_WIDTH (LCD_WIDTH - STAT_X)
734 #define BOARD_WIDTH (LCD_WIDTH - STAT_WIDTH)
735 #define BOARD_HEIGHT LCD_HEIGHT
736 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 3, "Level");
737 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
738 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 13, buf);
739 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 26, "Moves");
740 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
741 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 36, buf);
743 rb->lcd_drawrect(STAT_X, STAT_Y + 0, STAT_WIDTH, 24);
744 rb->lcd_drawrect(STAT_X, STAT_Y + 23, STAT_WIDTH, 24);
746 #if LCD_HEIGHT >= 70
747 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 49, "Pushes");
748 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
749 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 59, buf);
751 rb->lcd_drawrect(STAT_X, STAT_Y + 46, STAT_WIDTH, 24);
752 #endif
754 #endif
756 /* load the board to the screen */
757 for (rows = 0; rows < ROWS; rows++) {
758 for (cols = 0; cols < COLS; cols++) {
759 c = cols*MAGNIFY +
760 (BOARD_WIDTH - current_info.level.width*MAGNIFY)/2;
761 r = rows*MAGNIFY +
762 (BOARD_HEIGHT - current_info.level.height*MAGNIFY)/2;
764 switch(current_info.board[rows][cols]) {
765 case 'X': /* blank space outside of level */
766 break;
768 #if LCD_DEPTH >= 2 && ((LCD_HEIGHT >= 96 && LCD_WIDTH >= 152) || \
769 (LCD_HEIGHT >= 121 && LCD_WIDTH >= 120))
770 case ' ': /* floor */
771 rb->lcd_bitmap_part(sokoban_tiles, 0, 0*MAGNIFY, MAGNIFY,
772 c, r, MAGNIFY, MAGNIFY);
773 break;
775 case '#': /* wall */
776 rb->lcd_bitmap_part(sokoban_tiles, 0, 1*MAGNIFY, MAGNIFY,
777 c, r, MAGNIFY, MAGNIFY);
778 break;
780 case '$': /* box */
781 rb->lcd_bitmap_part(sokoban_tiles, 0, 2*MAGNIFY, MAGNIFY,
782 c, r, MAGNIFY, MAGNIFY);
783 break;
785 case '*': /* box on goal */
786 rb->lcd_bitmap_part(sokoban_tiles, 0, 3*MAGNIFY, MAGNIFY,
787 c, r, MAGNIFY, MAGNIFY);
788 break;
790 case '.': /* goal */
791 rb->lcd_bitmap_part(sokoban_tiles, 0, 4*MAGNIFY, MAGNIFY,
792 c, r, MAGNIFY, MAGNIFY);
793 break;
795 case '@': /* player */
796 rb->lcd_bitmap_part(sokoban_tiles, 0, 5*MAGNIFY, MAGNIFY,
797 c, r, MAGNIFY, MAGNIFY);
798 break;
800 case '+': /* player on goal */
801 rb->lcd_bitmap_part(sokoban_tiles, 0, 6*MAGNIFY, MAGNIFY,
802 c, r, MAGNIFY, MAGNIFY);
803 break;
804 #else
805 case '#': /* wall */
806 for (i = c; i < c + MAGNIFY; i++)
807 for (j = r; j < r + MAGNIFY; j++)
808 if ((i ^ j) & 1)
809 rb->lcd_drawpixel(i, j);
810 break;
812 case '$': /* box */
813 rb->lcd_drawrect(c, r, MAGNIFY, MAGNIFY);
814 break;
816 case '*': /* box on goal */
817 rb->lcd_drawrect(c, r, MAGNIFY, MAGNIFY);
818 rb->lcd_drawrect(c + MAGNIFY/2 - 1, r + MAGNIFY/2 - 1,
819 MAGNIFY/2, MAGNIFY/2);
820 break;
822 case '.': /* goal */
823 rb->lcd_drawrect(c + MAGNIFY/2 - 1, r + MAGNIFY/2 - 1,
824 MAGNIFY/2, MAGNIFY/2);
825 break;
827 case '@': /* player */
828 case '+': /* player on goal */
829 rb->lcd_drawline(c, r + middle, c + max, r + middle);
830 rb->lcd_drawline(c + middle, r, c + middle,
831 r + max - ldelta);
832 rb->lcd_drawline(c + max - middle, r, c + max - middle,
833 r + max - ldelta);
834 rb->lcd_drawline(c + middle, r + max - ldelta,
835 c + middle - ldelta, r + max);
836 rb->lcd_drawline(c + max - middle, r + max - ldelta,
837 c + max - middle + ldelta, r + max);
838 break;
839 #endif
844 /* print out the screen */
845 rb->lcd_update();
848 static void draw_level(void)
850 load_level();
851 rb->lcd_clear_display();
852 update_screen();
855 static bool save(char *filename, bool solution)
857 int fd;
858 char *loc;
859 DIR *dir;
860 char dirname[MAX_PATH];
862 rb->splash(0, "Saving...");
864 /* Create dir if it doesn't exist */
865 if ((loc = rb->strrchr(filename, '/')) != NULL) {
866 rb->strncpy(dirname, filename, loc - filename);
867 dirname[loc - filename] = '\0';
868 if(!(dir = rb->opendir(dirname)))
869 rb->mkdir(dirname);
870 else
871 rb->closedir(dir);
874 if (filename[0] == '\0' ||
875 (fd = rb->open(filename, O_WRONLY|O_CREAT|O_TRUNC)) < 0) {
876 rb->splash(HZ*2, "Unable to open %s", filename);
877 return false;
880 /* Sokoban: S/P for solution/progress : level number : current undo */
881 rb->snprintf(buf, sizeof(buf), "Sokoban:%c:%d:%d\n", (solution ? 'S' : 'P'),
882 current_info.level.index + 1, undo_info.current);
883 rb->write(fd, buf, rb->strlen(buf));
885 /* Filename of levelset */
886 rb->write(fd, buffered_boards.filename,
887 rb->strlen(buffered_boards.filename));
888 rb->write(fd, "\n", 1);
890 /* Full undo history */
891 rb->write(fd, undo_info.history, undo_info.max);
893 rb->close(fd);
895 return true;
898 static bool load(char *filename, bool silent)
900 int fd;
901 int i, n;
902 int len;
903 int button;
904 bool play_solution;
905 bool paused = false;
906 unsigned short speed = 2;
907 int delay[] = {HZ/2, HZ/3, HZ/4, HZ/6, HZ/8, HZ/12, HZ/16, HZ/25};
909 if (filename[0] == '\0' || (fd = rb->open(filename, O_RDONLY)) < 0) {
910 if (!silent)
911 rb->splash(HZ*2, "Unable to open %s", filename);
912 return false;
915 /* Read header, level number, & current undo */
916 rb->read_line(fd, buf, sizeof(buf));
918 /* If we're opening a level file, not a solution/progress file */
919 if (rb->strncmp(buf, "Sokoban", 7) != 0) {
920 rb->close(fd);
922 rb->strncpy(buffered_boards.filename, filename, MAX_PATH);
923 if (!read_levels(true))
924 return false;
926 current_info.level.index = 0;
927 load_level();
929 /* If there aren't any boxes to go or the player position wasn't set,
930 * the file probably wasn't a Sokoban level file */
931 if (current_info.level.boxes_to_go == 0 ||
932 current_info.player.row == 0 || current_info.player.col == 0) {
933 if (!silent)
934 rb->splash(HZ*2, "File is not a Sokoban level file");
935 return false;
938 } else {
940 /* Read filename of levelset */
941 rb->read_line(fd, buffered_boards.filename,
942 sizeof(buffered_boards.filename));
944 /* Read full undo history */
945 len = rb->read_line(fd, undo_info.history, MAX_UNDOS);
947 /* Correct len when trailing \r's or \n's are counted */
948 if (len > 2 && undo_info.history[len - 2] == '\0')
949 len -= 2;
950 else if (len > 1 && undo_info.history[len - 1] == '\0')
951 len--;
953 rb->close(fd);
955 /* Check to see if we're going to play a solution or resume progress */
956 play_solution = (buf[8] == 'S');
958 /* Get level number */
959 for (n = 0, i = 10; buf[i] >= '0' && buf[i] <= '9' && i < 15; i++)
960 n = n*10 + buf[i] - '0';
961 current_info.level.index = n - 1;
963 /* Get undo index */
964 for (n = 0, i++; buf[i] >= '0' && buf[i] <= '9' && i < 21; i++)
965 n = n*10 + buf[i] - '0';
966 if (n > len)
967 n = len;
968 undo_info.max = len;
970 if (current_info.level.index < 0) {
971 if (!silent)
972 rb->splash(HZ*2, "Error loading level");
973 return false;
975 if (!read_levels(true))
976 return false;
977 if (current_info.level.index >= current_info.max_level) {
978 if (!silent)
979 rb->splash(HZ*2, "Error loading level");
980 return false;
983 load_level();
985 if (play_solution) {
986 rb->lcd_clear_display();
987 update_screen();
988 rb->sleep(2*delay[speed]);
990 /* Replay solution until menu button is pressed */
991 i = 0;
992 while (true) {
993 if (i < len) {
994 if (!move(undo_info.history[i], true)) {
995 n = i;
996 break;
998 rb->lcd_clear_display();
999 update_screen();
1000 i++;
1001 } else
1002 paused = true;
1004 rb->sleep(delay[speed]);
1006 while ((button = rb->button_get(false)) || paused) {
1007 switch (button) {
1008 case SOKOBAN_MENU:
1009 /* Pretend the level is complete so we'll quit */
1010 current_info.level.boxes_to_go = 0;
1011 return true;
1013 case SOKOBAN_PAUSE:
1014 /* Toggle pause state */
1015 paused = !paused;
1016 break;
1018 case BUTTON_LEFT:
1019 case BUTTON_LEFT | BUTTON_REPEAT:
1020 /* Go back one move */
1021 if (paused) {
1022 if (undo())
1023 i--;
1024 rb->lcd_clear_display();
1025 update_screen();
1027 break;
1029 case BUTTON_RIGHT:
1030 case BUTTON_RIGHT | BUTTON_REPEAT:
1031 /* Go forward one move */
1032 if (paused) {
1033 if (redo())
1034 i++;
1035 rb->lcd_clear_display();
1036 update_screen();
1038 break;
1040 case SOKOBAN_UP:
1041 case SOKOBAN_UP | BUTTON_REPEAT:
1042 /* Speed up */
1043 if (speed < sizeof(delay)/sizeof(int) - 1)
1044 speed++;
1045 break;
1047 case SOKOBAN_DOWN:
1048 case SOKOBAN_DOWN | BUTTON_REPEAT:
1049 /* Slow down */
1050 if (speed > 0)
1051 speed--;
1054 if (paused)
1055 rb->sleep(HZ/33);
1059 /* If level is complete, wait for keypress before quitting */
1060 if (current_info.level.boxes_to_go == 0)
1061 rb->button_get(true);
1063 } else {
1064 /* Advance to current undo */
1065 for (i = 0; i < n; i++) {
1066 if (!move(undo_info.history[i], true)) {
1067 n = i;
1068 break;
1072 rb->button_clear_queue();
1073 rb->lcd_clear_display();
1076 undo_info.current = n;
1079 return true;
1082 static int sokoban_menu(void)
1084 int button;
1085 int selection = 0;
1086 int i;
1087 bool menu_quit;
1088 int start_selected = 0;
1089 int prev_level = current_info.level.index;
1091 MENUITEM_STRINGLIST(menu, "Sokoban Menu", NULL,
1092 "Resume", "Select Level", "Audio Playback", "Keys",
1093 "Load Default Level Set", "Quit Without Saving",
1094 "Save Progress & Quit");
1096 do {
1097 menu_quit = true;
1098 selection = rb->do_menu(&menu, &start_selected);
1100 switch (selection) {
1101 case 0: /* Resume */
1102 break;
1104 case 1: /* Select level */
1105 current_info.level.index++;
1106 rb->set_int("Select Level", "", UNIT_INT,
1107 &current_info.level.index, NULL, 1, 1,
1108 current_info.max_level, NULL);
1109 current_info.level.index--;
1110 if (prev_level != current_info.level.index) {
1111 init_undo();
1112 draw_level();
1113 } else
1114 menu_quit = false;
1115 break;
1117 case 2: /* Audio playback control */
1118 playback_control(rb);
1119 menu_quit = false;
1120 break;
1122 case 3: /* Keys */
1123 FOR_NB_SCREENS(i)
1124 rb->screens[i]->clear_display();
1125 rb->lcd_setfont(SOKOBAN_FONT);
1127 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
1128 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
1129 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1130 rb->lcd_putsxy(3, 16, "[ON] Undo");
1131 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1132 rb->lcd_putsxy(3, 36, "[F1] Down a Level");
1133 rb->lcd_putsxy(3, 46, "[F2] Restart Level");
1134 rb->lcd_putsxy(3, 56, "[F3] Up a Level");
1135 #elif CONFIG_KEYPAD == ONDIO_PAD
1136 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1137 rb->lcd_putsxy(3, 16, "[MODE] Undo");
1138 rb->lcd_putsxy(3, 26, "[MODE+DOWN] Redo");
1139 rb->lcd_putsxy(3, 36, "[MODE+LEFT] Previous Level");
1140 rb->lcd_putsxy(3, 46, "[MODE+UP] Restart Level");
1141 rb->lcd_putsxy(3, 56, "[MODE+RIGHT] Up Level");
1142 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
1143 (CONFIG_KEYPAD == IRIVER_H300_PAD)
1144 rb->lcd_putsxy(3, 6, "[STOP] Menu");
1145 rb->lcd_putsxy(3, 16, "[REC] Undo");
1146 rb->lcd_putsxy(3, 26, "[MODE] Redo");
1147 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1148 rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
1149 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1150 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1151 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1152 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1153 rb->lcd_putsxy(3, 6, "[SELECT+MENU] Menu");
1154 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1155 rb->lcd_putsxy(3, 26, "[SELECT+PLAY] Redo");
1156 rb->lcd_putsxy(3, 36, "[SELECT+LEFT] Previous Level");
1157 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Next Level");
1158 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
1159 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1160 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1161 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1162 rb->lcd_putsxy(3, 36, "[REC] Restart Level");
1163 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1164 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1165 rb->lcd_putsxy(3, 16, "[REW] Undo");
1166 rb->lcd_putsxy(3, 26, "[FF] Redo");
1167 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1168 rb->lcd_putsxy(3, 46, "[PLAY+RIGHT] Restart Level");
1169 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1170 #elif CONFIG_KEYPAD == GIGABEAT_PAD
1171 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1172 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1173 rb->lcd_putsxy(3, 26, "[A] Redo");
1174 rb->lcd_putsxy(3, 36, "[VOL-] Previous Level");
1175 rb->lcd_putsxy(3, 46, "[MENU] Restart Level");
1176 rb->lcd_putsxy(3, 56, "[VOL+] Next Level");
1177 #elif CONFIG_KEYPAD == SANSA_E200_PAD
1178 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1179 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1180 rb->lcd_putsxy(3, 26, "[REC] Redo");
1181 rb->lcd_putsxy(3, 36, "[SELECT+DOWN] Previous Level");
1182 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Restart Level");
1183 rb->lcd_putsxy(3, 56, "[SELECT+UP] Next Level");
1184 #endif
1186 FOR_NB_SCREENS(i)
1187 rb->screens[i]->update();
1189 /* Display until keypress */
1190 do {
1191 rb->sleep(HZ/20);
1192 button = rb->button_get(false);
1193 } while (!button || button & BUTTON_REL ||
1194 button & BUTTON_REPEAT);
1196 menu_quit = false;
1197 break;
1199 case 4: /* Load default levelset */
1200 init_boards();
1201 if (!read_levels(true))
1202 return 5; /* Quit */
1203 load_level();
1204 break;
1206 case 5: /* Quit */
1207 break;
1209 case 6: /* Save & quit */
1210 save(SOKOBAN_SAVE_FILE, false);
1211 rb->reload_directory();
1214 } while (!menu_quit);
1216 /* Restore font */
1217 rb->lcd_setfont(SOKOBAN_FONT);
1219 FOR_NB_SCREENS(i) {
1220 rb->screens[i]->clear_display();
1221 rb->screens[i]->update();
1224 return selection;
1227 static bool sokoban_loop(void)
1229 bool moved;
1230 int i = 0, button = 0, lastbutton = 0;
1231 short r = 0, c = 0;
1232 int w, h;
1233 char *loc;
1235 while (true) {
1236 moved = false;
1238 r = current_info.player.row;
1239 c = current_info.player.col;
1241 button = rb->button_get(true);
1243 switch(button)
1245 #ifdef SOKOBAN_RC_MENU
1246 case SOKOBAN_RC_MENU:
1247 #endif
1248 case SOKOBAN_MENU:
1249 switch (sokoban_menu()) {
1250 case 5: /* Quit */
1251 case 6: /* Save & quit */
1252 return PLUGIN_OK;
1254 update_screen();
1255 break;
1257 case SOKOBAN_UNDO:
1258 #ifdef SOKOBAN_UNDO_PRE
1259 if (lastbutton != SOKOBAN_UNDO_PRE)
1260 break;
1261 #else /* repeat can't work here for Ondio, iPod, et al */
1262 case SOKOBAN_UNDO | BUTTON_REPEAT:
1263 #endif
1264 undo();
1265 rb->lcd_clear_display();
1266 update_screen();
1267 break;
1269 #ifdef SOKOBAN_REDO
1270 case SOKOBAN_REDO:
1271 case SOKOBAN_REDO | BUTTON_REPEAT:
1272 moved = redo();
1273 rb->lcd_clear_display();
1274 update_screen();
1275 break;
1276 #endif
1278 #ifdef SOKOBAN_LEVEL_UP
1279 case SOKOBAN_LEVEL_UP:
1280 case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
1281 /* next level */
1282 init_undo();
1283 if (current_info.level.index + 1 < current_info.max_level)
1284 current_info.level.index++;
1286 draw_level();
1287 break;
1288 #endif
1290 #ifdef SOKOBAN_LEVEL_DOWN
1291 case SOKOBAN_LEVEL_DOWN:
1292 case SOKOBAN_LEVEL_DOWN | BUTTON_REPEAT:
1293 /* previous level */
1294 init_undo();
1295 if (current_info.level.index > 0)
1296 current_info.level.index--;
1298 draw_level();
1299 break;
1300 #endif
1302 #ifdef SOKOBAN_LEVEL_REPEAT
1303 case SOKOBAN_LEVEL_REPEAT:
1304 case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
1305 /* same level */
1306 init_undo();
1307 draw_level();
1308 break;
1309 #endif
1311 case BUTTON_LEFT:
1312 case BUTTON_LEFT | BUTTON_REPEAT:
1313 moved = move(SOKOBAN_MOVE_LEFT, false);
1314 break;
1316 case BUTTON_RIGHT:
1317 case BUTTON_RIGHT | BUTTON_REPEAT:
1318 moved = move(SOKOBAN_MOVE_RIGHT, false);
1319 break;
1321 case SOKOBAN_UP:
1322 case SOKOBAN_UP | BUTTON_REPEAT:
1323 moved = move(SOKOBAN_MOVE_UP, false);
1324 break;
1326 case SOKOBAN_DOWN:
1327 case SOKOBAN_DOWN | BUTTON_REPEAT:
1328 moved = move(SOKOBAN_MOVE_DOWN, false);
1329 break;
1331 default:
1332 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
1333 return PLUGIN_USB_CONNECTED;
1334 break;
1337 lastbutton = button;
1339 if (moved) {
1340 rb->lcd_clear_display();
1341 update_screen();
1344 /* We have completed this level */
1345 if (current_info.level.boxes_to_go == 0) {
1347 if (moved) {
1348 rb->lcd_clear_display();
1350 /* Show level complete message & stats */
1351 rb->snprintf(buf, sizeof(buf), "Level %d Complete!",
1352 current_info.level.index + 1);
1353 rb->lcd_getstringsize(buf, &w, &h);
1354 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h*3, buf);
1356 rb->snprintf(buf, sizeof(buf), "%4d Moves ",
1357 current_info.level.moves);
1358 rb->lcd_getstringsize(buf, &w, &h);
1359 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h, buf);
1361 rb->snprintf(buf, sizeof(buf), "%4d Pushes",
1362 current_info.level.pushes);
1363 rb->lcd_getstringsize(buf, &w, &h);
1364 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2, buf);
1366 if (undo_info.count < MAX_UNDOS) {
1367 rb->snprintf(buf, sizeof(buf), "%s: Save solution",
1368 BUTTON_SAVE_NAME);
1369 rb->lcd_getstringsize(buf, &w, &h);
1370 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + h*2, buf);
1373 rb->lcd_update();
1374 rb->sleep(HZ/4);
1375 rb->button_clear_queue();
1377 /* Display for 4 seconds or until new keypress */
1378 for (i = 0; i < 80; i++) {
1379 rb->sleep(HZ/20);
1380 button = rb->button_get(false);
1381 if (button && !(button & BUTTON_REL) &&
1382 !(button & BUTTON_REPEAT))
1383 break;
1386 if (button == BUTTON_SAVE) {
1387 if (undo_info.count < MAX_UNDOS) {
1388 /* Set filename to current levelset plus level number
1389 * and .sok extension. Use SAVE_FOLDER if using the
1390 * default levelset, since it's in a hidden folder. */
1391 if (rb->strcmp(buffered_boards.filename,
1392 SOKOBAN_LEVELS_FILE) == 0) {
1393 rb->snprintf(buf, sizeof(buf),
1394 "%s/sokoban.%d.sok",
1395 SOKOBAN_SAVE_FOLDER,
1396 current_info.level.index + 1);
1397 } else {
1398 if ((loc = rb->strrchr(buffered_boards.filename,
1399 '.')) != NULL)
1400 *loc = '\0';
1401 rb->snprintf(buf, sizeof(buf), "%s.%d.sok",
1402 buffered_boards.filename,
1403 current_info.level.index + 1);
1404 if (loc != NULL)
1405 *loc = '.';
1408 if (!rb->kbd_input(buf, MAX_PATH))
1409 save(buf, true);
1410 } else
1411 rb->splash(HZ*2, "Solution too long to save");
1413 rb->lcd_setfont(SOKOBAN_FONT); /* Restore font */
1417 FOR_NB_SCREENS(i) {
1418 rb->screens[i]->clear_display();
1419 rb->screens[i]->update();
1422 current_info.level.index++;
1424 /* clear undo stats */
1425 init_undo();
1427 if (current_info.level.index >= current_info.max_level) {
1428 /* Show levelset complete message */
1429 rb->snprintf(buf, sizeof(buf), "You WIN!!");
1430 rb->lcd_getstringsize(buf, &w, &h);
1431 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, buf);
1433 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1434 /* Display for 4 seconds or until keypress */
1435 for (i = 0; i < 80; i++) {
1436 rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
1437 rb->lcd_update();
1438 rb->sleep(HZ/10);
1440 button = rb->button_get(false);
1441 if (button && !(button & BUTTON_REL))
1442 break;
1444 rb->lcd_set_drawmode(DRMODE_SOLID);
1446 /* Reset to first level & show quit menu */
1447 current_info.level.index = 0;
1449 switch (sokoban_menu()) {
1450 case 5: /* Quit */
1451 case 6: /* Save & quit */
1452 return PLUGIN_OK;
1456 load_level();
1457 update_screen();
1460 } /* end while */
1462 return PLUGIN_OK;
1466 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
1468 int w, h;
1470 (void)(parameter);
1471 rb = api;
1473 rb->lcd_setfont(SOKOBAN_FONT);
1475 rb->lcd_clear_display();
1476 rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
1477 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, SOKOBAN_TITLE);
1478 rb->lcd_update();
1479 rb->sleep(HZ); /* Show title for 1 second */
1481 init_boards();
1483 if (parameter == NULL) {
1484 /* Attempt to resume saved progress, otherwise start at beginning */
1485 if (!load(SOKOBAN_SAVE_FILE, true)) {
1486 init_boards();
1487 if (!read_levels(true))
1488 return PLUGIN_OK;
1489 load_level();
1492 } else {
1493 /* The plugin is being used to open a file */
1494 if (load((char*) parameter, false)) {
1495 /* If we loaded & played a solution, quit */
1496 if (current_info.level.boxes_to_go == 0)
1497 return PLUGIN_OK;
1498 } else
1499 return PLUGIN_OK;
1502 rb->lcd_clear_display();
1503 update_screen();
1505 return sokoban_loop();
1508 #endif