1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "lib/playback_control.h"
25 #ifdef HAVE_LCD_BITMAP
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
[];
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)
46 #define ROWS (LCD_HEIGHT/MAGNIFY)
47 #define COLS ((LCD_WIDTH-40)/MAGNIFY)
48 #elif (LCD_HEIGHT >= 249) && (LCD_WIDTH >= 280)
50 #define ROWS ((LCD_HEIGHT-25)/MAGNIFY)
51 #define COLS (LCD_WIDTH/MAGNIFY)
52 #elif (LCD_HEIGHT >= 144) && (LCD_WIDTH >= 220)
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 */
58 #define ROWS ((LCD_HEIGHT-25)/MAGNIFY)
59 #define COLS ((LCD_WIDTH+4)/MAGNIFY)
60 #elif (LCD_HEIGHT >= 96) && (LCD_WIDTH >= 160)
62 #define ROWS (LCD_HEIGHT/MAGNIFY)
63 #define COLS ((LCD_WIDTH-40)/MAGNIFY)
64 #elif (LCD_HEIGHT >= 121) && (LCD_WIDTH >= 120)
66 #define ROWS ((LCD_HEIGHT-25)/MAGNIFY)
67 #define COLS (LCD_WIDTH/MAGNIFY)
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)
79 #define MAX_LEVEL_DATA 0x20000
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
89 #define MAX_UNDOS (PLUGIN_BUFFER_SIZE - MAX_LEVEL_DATA - 0x3000)
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"
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 */
231 int index
; /* Level index (level number - 1) */
232 int moves
; /* Moves & pushes for the stats */
234 short boxes_to_go
; /* Number of unplaced boxes remaining in level */
235 short height
; /* Height & width for centering level display */
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
];
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 */
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 */
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)
279 undo_info
.current
= 0;
283 static void get_delta(char direction
, short *d_r
, short *d_c
)
286 case SOKOBAN_PUSH_LEFT
:
287 case SOKOBAN_MOVE_LEFT
:
291 case SOKOBAN_PUSH_RIGHT
:
292 case SOKOBAN_MOVE_RIGHT
:
296 case SOKOBAN_PUSH_UP
:
297 case SOKOBAN_MOVE_UP
:
301 case SOKOBAN_PUSH_DOWN
:
302 case SOKOBAN_MOVE_DOWN
:
308 static bool undo(void)
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
)
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;
328 undo
= undo_info
.history
[undo_info
.current
];
330 if (undo
< SOKOBAN_MOVE_MIN
)
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
= ¤t_info
.board
[r
][c
];
340 space_next
= ¤t_info
.board
[r
+ d_r
][c
+ d_c
];
341 space_prev
= ¤t_info
.board
[r
- d_r
][c
- d_c
];
343 /* Update board info */
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
--;
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
--;
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))
381 undo_info
.current
= 0;
383 if (undo_info
.count
< MAX_UNDOS
)
387 static bool move(char direction
, bool redo
)
390 short d_r
= 0, d_c
= 0; /* delta row & delta col */
391 char *space_cur
, *space_next
, *space_beyond
;
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
)
404 /* Give the 3 spaces we're going to use better names */
405 space_cur
= ¤t_info
.board
[r
][c
];
406 space_next
= ¤t_info
.board
[r
+ d_r
][c
+ d_c
];
407 space_beyond
= ¤t_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
;
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 */
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
!= ' ')
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
++;
438 /* Check for invalid move */
439 if (*space_next
!= '.' && *space_next
!= ' ')
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 */
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
)) {
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 */
473 static bool redo(void)
475 /* If no moves have been undone, quit */
476 if (undo_info
.current
== undo_info
.max
)
479 return move(undo_info
.history
[(undo_info
.current
< MAX_UNDOS
?
480 undo_info
.current
: 0)], true);
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;
500 static bool read_levels(bool initialize
)
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
&&
515 buffered_boards
.start
= current_info
.level
.index
-
516 buffered_boards
.prebuffered_boards
;
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
);
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')
531 else if (len
> 1 && buf
[len
- 1] == '\0')
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 */
540 level_count
- buffered_boards
.start
< MAX_LEVELS
) {
541 buffered_boards
.index
[level_count
- buffered_boards
.start
]
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;
554 /* If newline & level is tall enough or is RLE */
555 } else if (buf
[0] == '\0' && (row
> 2 || lastlen
> 22)) {
557 if (level_count
>= buffered_boards
.start
) {
559 if (i
< MAX_LEVEL_DATA
)
560 buffered_boards
.end
= level_count
;
561 else if (!initialize
)
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
;
578 current_info
.max_level
= level_count
;
579 buffered_boards
.prebuffered_boards
= buffered_boards
.end
/2;
587 static void load_level(void)
592 int index
= current_info
.level
.index
- buffered_boards
.start
;
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
) {
599 if (current_info
.level
.index
> buffered_boards
.prebuffered_boards
)
600 index
= buffered_boards
.prebuffered_boards
;
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;
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
] == '|') {
623 /* Update max width of level & go to next row */
624 if (c
> current_info
.level
.width
)
625 current_info
.level
.width
= c
;
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 */
642 else if (level
[i
] == '-' || level
[i
] == '_')
650 current_info
.level
.boxes_to_go
+= n
;
652 /* Put RLE character n times */
654 current_info
.board
[r
][c
++] = level
[i
];
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
;
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';
690 static void update_screen(void)
695 #if LCD_DEPTH < 2 || ((LCD_HEIGHT < 96 || LCD_WIDTH < 152) && \
696 (LCD_HEIGHT < 121 || LCD_WIDTH < 120))
698 int max
= MAGNIFY
- 1;
700 int ldelta
= (middle
+ 1)/2;
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
);
723 #if LCD_WIDTH - (COLS*MAGNIFY) > 40
724 #define STAT_X (LCD_WIDTH - 40)
726 #define STAT_X COLS*MAGNIFY
729 #define STAT_Y (LCD_HEIGHT - 70)/2
731 #define STAT_Y (LCD_HEIGHT - 47)/2
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);
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);
756 /* load the board to the screen */
757 for (rows
= 0; rows
< ROWS
; rows
++) {
758 for (cols
= 0; cols
< COLS
; cols
++) {
760 (BOARD_WIDTH
- current_info
.level
.width
*MAGNIFY
)/2;
762 (BOARD_HEIGHT
- current_info
.level
.height
*MAGNIFY
)/2;
764 switch(current_info
.board
[rows
][cols
]) {
765 case 'X': /* blank space outside of level */
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
);
776 rb
->lcd_bitmap_part(sokoban_tiles
, 0, 1*MAGNIFY
, MAGNIFY
,
777 c
, r
, MAGNIFY
, MAGNIFY
);
781 rb
->lcd_bitmap_part(sokoban_tiles
, 0, 2*MAGNIFY
, MAGNIFY
,
782 c
, r
, MAGNIFY
, MAGNIFY
);
785 case '*': /* box on goal */
786 rb
->lcd_bitmap_part(sokoban_tiles
, 0, 3*MAGNIFY
, MAGNIFY
,
787 c
, r
, MAGNIFY
, MAGNIFY
);
791 rb
->lcd_bitmap_part(sokoban_tiles
, 0, 4*MAGNIFY
, MAGNIFY
,
792 c
, r
, MAGNIFY
, MAGNIFY
);
795 case '@': /* player */
796 rb
->lcd_bitmap_part(sokoban_tiles
, 0, 5*MAGNIFY
, MAGNIFY
,
797 c
, r
, MAGNIFY
, MAGNIFY
);
800 case '+': /* player on goal */
801 rb
->lcd_bitmap_part(sokoban_tiles
, 0, 6*MAGNIFY
, MAGNIFY
,
802 c
, r
, MAGNIFY
, MAGNIFY
);
806 for (i
= c
; i
< c
+ MAGNIFY
; i
++)
807 for (j
= r
; j
< r
+ MAGNIFY
; j
++)
809 rb
->lcd_drawpixel(i
, j
);
813 rb
->lcd_drawrect(c
, r
, MAGNIFY
, MAGNIFY
);
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);
823 rb
->lcd_drawrect(c
+ MAGNIFY
/2 - 1, r
+ MAGNIFY
/2 - 1,
824 MAGNIFY
/2, MAGNIFY
/2);
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
,
832 rb
->lcd_drawline(c
+ max
- middle
, r
, c
+ max
- middle
,
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
);
844 /* print out the screen */
848 static void draw_level(void)
851 rb
->lcd_clear_display();
855 static bool save(char *filename
, bool solution
)
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
)))
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
);
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
);
898 static bool load(char *filename
, bool silent
)
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) {
911 rb
->splash(HZ
*2, "Unable to open %s", filename
);
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) {
922 rb
->strncpy(buffered_boards
.filename
, filename
, MAX_PATH
);
923 if (!read_levels(true))
926 current_info
.level
.index
= 0;
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) {
934 rb
->splash(HZ
*2, "File is not a Sokoban level file");
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')
950 else if (len
> 1 && undo_info
.history
[len
- 1] == '\0')
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;
964 for (n
= 0, i
++; buf
[i
] >= '0' && buf
[i
] <= '9' && i
< 21; i
++)
965 n
= n
*10 + buf
[i
] - '0';
970 if (current_info
.level
.index
< 0) {
972 rb
->splash(HZ
*2, "Error loading level");
975 if (!read_levels(true))
977 if (current_info
.level
.index
>= current_info
.max_level
) {
979 rb
->splash(HZ
*2, "Error loading level");
986 rb
->lcd_clear_display();
988 rb
->sleep(2*delay
[speed
]);
990 /* Replay solution until menu button is pressed */
994 if (!move(undo_info
.history
[i
], true)) {
998 rb
->lcd_clear_display();
1004 rb
->sleep(delay
[speed
]);
1006 while ((button
= rb
->button_get(false)) || paused
) {
1009 /* Pretend the level is complete so we'll quit */
1010 current_info
.level
.boxes_to_go
= 0;
1014 /* Toggle pause state */
1019 case BUTTON_LEFT
| BUTTON_REPEAT
:
1020 /* Go back one move */
1024 rb
->lcd_clear_display();
1030 case BUTTON_RIGHT
| BUTTON_REPEAT
:
1031 /* Go forward one move */
1035 rb
->lcd_clear_display();
1041 case SOKOBAN_UP
| BUTTON_REPEAT
:
1043 if (speed
< sizeof(delay
)/sizeof(int) - 1)
1048 case SOKOBAN_DOWN
| BUTTON_REPEAT
:
1059 /* If level is complete, wait for keypress before quitting */
1060 if (current_info
.level
.boxes_to_go
== 0)
1061 rb
->button_get(true);
1064 /* Advance to current undo */
1065 for (i
= 0; i
< n
; i
++) {
1066 if (!move(undo_info
.history
[i
], true)) {
1072 rb
->button_clear_queue();
1073 rb
->lcd_clear_display();
1076 undo_info
.current
= n
;
1082 static int sokoban_menu(void)
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");
1098 selection
= rb
->do_menu(&menu
, &start_selected
);
1100 switch (selection
) {
1101 case 0: /* Resume */
1104 case 1: /* Select level */
1105 current_info
.level
.index
++;
1106 rb
->set_int("Select Level", "", UNIT_INT
,
1107 ¤t_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
) {
1117 case 2: /* Audio playback control */
1118 playback_control(rb
);
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");
1187 rb
->screens
[i
]->update();
1189 /* Display until keypress */
1192 button
= rb
->button_get(false);
1193 } while (!button
|| button
& BUTTON_REL
||
1194 button
& BUTTON_REPEAT
);
1199 case 4: /* Load default levelset */
1201 if (!read_levels(true))
1202 return 5; /* Quit */
1209 case 6: /* Save & quit */
1210 save(SOKOBAN_SAVE_FILE
, false);
1211 rb
->reload_directory();
1214 } while (!menu_quit
);
1217 rb
->lcd_setfont(SOKOBAN_FONT
);
1220 rb
->screens
[i
]->clear_display();
1221 rb
->screens
[i
]->update();
1227 static bool sokoban_loop(void)
1230 int i
= 0, button
= 0, lastbutton
= 0;
1238 r
= current_info
.player
.row
;
1239 c
= current_info
.player
.col
;
1241 button
= rb
->button_get(true);
1245 #ifdef SOKOBAN_RC_MENU
1246 case SOKOBAN_RC_MENU
:
1249 switch (sokoban_menu()) {
1251 case 6: /* Save & quit */
1258 #ifdef SOKOBAN_UNDO_PRE
1259 if (lastbutton
!= SOKOBAN_UNDO_PRE
)
1261 #else /* repeat can't work here for Ondio, iPod, et al */
1262 case SOKOBAN_UNDO
| BUTTON_REPEAT
:
1265 rb
->lcd_clear_display();
1271 case SOKOBAN_REDO
| BUTTON_REPEAT
:
1273 rb
->lcd_clear_display();
1278 #ifdef SOKOBAN_LEVEL_UP
1279 case SOKOBAN_LEVEL_UP
:
1280 case SOKOBAN_LEVEL_UP
| BUTTON_REPEAT
:
1283 if (current_info
.level
.index
+ 1 < current_info
.max_level
)
1284 current_info
.level
.index
++;
1290 #ifdef SOKOBAN_LEVEL_DOWN
1291 case SOKOBAN_LEVEL_DOWN
:
1292 case SOKOBAN_LEVEL_DOWN
| BUTTON_REPEAT
:
1293 /* previous level */
1295 if (current_info
.level
.index
> 0)
1296 current_info
.level
.index
--;
1302 #ifdef SOKOBAN_LEVEL_REPEAT
1303 case SOKOBAN_LEVEL_REPEAT
:
1304 case SOKOBAN_LEVEL_REPEAT
| BUTTON_REPEAT
:
1312 case BUTTON_LEFT
| BUTTON_REPEAT
:
1313 moved
= move(SOKOBAN_MOVE_LEFT
, false);
1317 case BUTTON_RIGHT
| BUTTON_REPEAT
:
1318 moved
= move(SOKOBAN_MOVE_RIGHT
, false);
1322 case SOKOBAN_UP
| BUTTON_REPEAT
:
1323 moved
= move(SOKOBAN_MOVE_UP
, false);
1327 case SOKOBAN_DOWN
| BUTTON_REPEAT
:
1328 moved
= move(SOKOBAN_MOVE_DOWN
, false);
1332 if (rb
->default_event_handler(button
) == SYS_USB_CONNECTED
)
1333 return PLUGIN_USB_CONNECTED
;
1337 lastbutton
= button
;
1340 rb
->lcd_clear_display();
1344 /* We have completed this level */
1345 if (current_info
.level
.boxes_to_go
== 0) {
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",
1369 rb
->lcd_getstringsize(buf
, &w
, &h
);
1370 rb
->lcd_putsxy(LCD_WIDTH
/2 - w
/2, LCD_HEIGHT
/2 + h
*2, buf
);
1375 rb
->button_clear_queue();
1377 /* Display for 4 seconds or until new keypress */
1378 for (i
= 0; i
< 80; i
++) {
1380 button
= rb
->button_get(false);
1381 if (button
&& !(button
& BUTTON_REL
) &&
1382 !(button
& BUTTON_REPEAT
))
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);
1398 if ((loc
= rb
->strrchr(buffered_boards
.filename
,
1401 rb
->snprintf(buf
, sizeof(buf
), "%s.%d.sok",
1402 buffered_boards
.filename
,
1403 current_info
.level
.index
+ 1);
1408 if (!rb
->kbd_input(buf
, MAX_PATH
))
1411 rb
->splash(HZ
*2, "Solution too long to save");
1413 rb
->lcd_setfont(SOKOBAN_FONT
); /* Restore font */
1418 rb
->screens
[i
]->clear_display();
1419 rb
->screens
[i
]->update();
1422 current_info
.level
.index
++;
1424 /* clear undo stats */
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
);
1440 button
= rb
->button_get(false);
1441 if (button
&& !(button
& BUTTON_REL
))
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()) {
1451 case 6: /* Save & quit */
1466 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
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
);
1479 rb
->sleep(HZ
); /* Show title for 1 second */
1483 if (parameter
== NULL
) {
1484 /* Attempt to resume saved progress, otherwise start at beginning */
1485 if (!load(SOKOBAN_SAVE_FILE
, true)) {
1487 if (!read_levels(true))
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)
1502 rb
->lcd_clear_display();
1505 return sokoban_loop();