Don't rebuild the dependency file on 'make reconf'. Build type and target won't chang...
[kugel-rb.git] / apps / plugins / sokoban.c
blobe8ccdaca1094777676081a68bba270386c6a4717
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Eric Linenberg
11 * February 2003: Robert Hak performs a cleanup/rewrite/feature addition.
12 * Eric smiles. Bjorn cries. Linus say 'huh?'.
13 * March 2007: Sean Morrisey performs a major rewrite/feature addition.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "plugin.h"
25 #include "lib/playback_control.h"
27 PLUGIN_HEADER
29 #define SOKOBAN_TITLE "Sokoban"
31 #define SOKOBAN_LEVELS_FILE PLUGIN_GAMES_DIR "/sokoban.levels"
32 #define SOKOBAN_SAVE_FILE PLUGIN_GAMES_DIR "/sokoban.save"
33 #define SOKOBAN_SAVE_FOLDER "/games"
35 #include "pluginbitmaps/sokoban_tiles.h"
36 #define SOKOBAN_TILESIZE BMPWIDTH_sokoban_tiles
38 /* If tilesize is 0 (which it is during dependency generation) gcc will abort
39 (div by 0) and this plugin won't get any dependencies
41 #if SOKOBAN_TILESIZE < 1
42 #define SOKOBAN_TILESIZE 10
43 #endif
45 /* SOKOBAN_TILESIZE is the number of pixels for each block.
46 * Set dynamically so all targets can support levels
47 * that fill their entire screen, less the stat box.
48 * 16 rows & 20 cols minimum */
49 #if LCD_WIDTH > LCD_HEIGHT /* horizontal layout*/
50 #define ROWS (LCD_HEIGHT/SOKOBAN_TILESIZE)
51 #if (LCD_WIDTH+4) >= (20*SOKOBAN_TILESIZE+40) /* wide or narrow stats box */
52 #define COLS ((LCD_WIDTH-40)/SOKOBAN_TILESIZE)
53 #else
54 #define COLS ((LCD_WIDTH-32)/SOKOBAN_TILESIZE)
55 #endif
56 #else /* vertical layout*/
57 #define ROWS ((LCD_HEIGHT-25)/SOKOBAN_TILESIZE)
58 #define COLS (LCD_WIDTH/SOKOBAN_TILESIZE)
59 #endif
61 /* Use either all but 16k of the plugin buffer for level data
62 * or 128k, which ever is less */
63 #if PLUGIN_BUFFER_SIZE - 0x4000 < 0x20000
64 #define MAX_LEVEL_DATA (PLUGIN_BUFFER_SIZE - 0x4000)
65 #else
66 #define MAX_LEVEL_DATA 0x20000
67 #endif
69 /* Number of levels for which to allocate buffer indexes */
70 #define MAX_LEVELS MAX_LEVEL_DATA/70
72 /* Use 4k plus remaining plugin buffer (-12k for prog) for undo, up to 64k */
73 #if PLUGIN_BUFFER_SIZE - MAX_LEVEL_DATA - 0x3000 > 0x10000
74 #define MAX_UNDOS 0x10000
75 #else
76 #define MAX_UNDOS (PLUGIN_BUFFER_SIZE - MAX_LEVEL_DATA - 0x3000)
77 #endif
79 /* Move/push definitions for undo */
80 #define SOKOBAN_PUSH_LEFT 'L'
81 #define SOKOBAN_PUSH_RIGHT 'R'
82 #define SOKOBAN_PUSH_UP 'U'
83 #define SOKOBAN_PUSH_DOWN 'D'
84 #define SOKOBAN_MOVE_LEFT 'l'
85 #define SOKOBAN_MOVE_RIGHT 'r'
86 #define SOKOBAN_MOVE_UP 'u'
87 #define SOKOBAN_MOVE_DOWN 'd'
89 #define SOKOBAN_MOVE_DIFF (SOKOBAN_MOVE_LEFT-SOKOBAN_PUSH_LEFT)
90 #define SOKOBAN_MOVE_MIN SOKOBAN_MOVE_DOWN
92 /* variable button definitions */
93 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
94 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
95 #define SOKOBAN_LEFT BUTTON_LEFT
96 #define SOKOBAN_RIGHT BUTTON_RIGHT
97 #define SOKOBAN_UP BUTTON_UP
98 #define SOKOBAN_DOWN BUTTON_DOWN
99 #define SOKOBAN_MENU BUTTON_OFF
100 #define SOKOBAN_UNDO BUTTON_ON
101 #define SOKOBAN_REDO BUTTON_PLAY
102 #define SOKOBAN_LEVEL_DOWN BUTTON_F1
103 #define SOKOBAN_LEVEL_REPEAT BUTTON_F2
104 #define SOKOBAN_LEVEL_UP BUTTON_F3
105 #define SOKOBAN_PAUSE BUTTON_PLAY
106 #define BUTTON_SAVE BUTTON_ON
107 #define BUTTON_SAVE_NAME "ON"
109 #elif CONFIG_KEYPAD == ONDIO_PAD
110 #define SOKOBAN_LEFT BUTTON_LEFT
111 #define SOKOBAN_RIGHT BUTTON_RIGHT
112 #define SOKOBAN_UP BUTTON_UP
113 #define SOKOBAN_DOWN BUTTON_DOWN
114 #define SOKOBAN_MENU BUTTON_OFF
115 #define SOKOBAN_UNDO_PRE BUTTON_MENU
116 #define SOKOBAN_UNDO (BUTTON_MENU | BUTTON_REL)
117 #define SOKOBAN_REDO (BUTTON_MENU | BUTTON_DOWN)
118 #define SOKOBAN_LEVEL_DOWN (BUTTON_MENU | BUTTON_LEFT)
119 #define SOKOBAN_LEVEL_REPEAT (BUTTON_MENU | BUTTON_UP)
120 #define SOKOBAN_LEVEL_UP (BUTTON_MENU | BUTTON_RIGHT)
121 #define SOKOBAN_PAUSE BUTTON_MENU
122 #define BUTTON_SAVE BUTTON_MENU
123 #define BUTTON_SAVE_NAME "MENU"
125 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
126 (CONFIG_KEYPAD == IRIVER_H300_PAD)
127 #define SOKOBAN_LEFT BUTTON_LEFT
128 #define SOKOBAN_RIGHT BUTTON_RIGHT
129 #define SOKOBAN_UP BUTTON_UP
130 #define SOKOBAN_DOWN BUTTON_DOWN
131 #define SOKOBAN_MENU BUTTON_OFF
132 #define SOKOBAN_UNDO BUTTON_REC
133 #define SOKOBAN_REDO BUTTON_MODE
134 #define SOKOBAN_LEVEL_DOWN (BUTTON_ON | BUTTON_DOWN)
135 #define SOKOBAN_LEVEL_REPEAT BUTTON_ON
136 #define SOKOBAN_LEVEL_UP (BUTTON_ON | BUTTON_UP)
137 #define SOKOBAN_PAUSE BUTTON_ON
138 #define BUTTON_SAVE BUTTON_MODE
139 #define BUTTON_SAVE_NAME "MODE"
141 #define SOKOBAN_RC_MENU BUTTON_RC_STOP
143 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
144 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
145 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
146 #define SOKOBAN_LEFT BUTTON_LEFT
147 #define SOKOBAN_RIGHT BUTTON_RIGHT
148 #define SOKOBAN_UP BUTTON_MENU
149 #define SOKOBAN_DOWN BUTTON_PLAY
150 #define SOKOBAN_MENU (BUTTON_SELECT | BUTTON_MENU)
151 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
152 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
153 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_PLAY)
154 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_LEFT)
155 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_RIGHT)
156 #define SOKOBAN_PAUSE BUTTON_SELECT
157 #define BUTTON_SAVE BUTTON_SELECT
158 #define BUTTON_SAVE_NAME "SELECT"
160 /* FIXME: if/when simultaneous button presses work for X5/M5,
161 * add level up/down */
162 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
163 #define SOKOBAN_LEFT BUTTON_LEFT
164 #define SOKOBAN_RIGHT BUTTON_RIGHT
165 #define SOKOBAN_UP BUTTON_UP
166 #define SOKOBAN_DOWN BUTTON_DOWN
167 #define SOKOBAN_MENU BUTTON_POWER
168 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
169 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
170 #define SOKOBAN_LEVEL_REPEAT BUTTON_REC
171 #define SOKOBAN_REDO BUTTON_PLAY
172 #define SOKOBAN_PAUSE BUTTON_PLAY
173 #define BUTTON_SAVE BUTTON_SELECT
174 #define BUTTON_SAVE_NAME "SELECT"
176 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
177 #define SOKOBAN_LEFT BUTTON_LEFT
178 #define SOKOBAN_RIGHT BUTTON_RIGHT
179 #define SOKOBAN_UP BUTTON_SCROLL_UP
180 #define SOKOBAN_DOWN BUTTON_SCROLL_DOWN
181 #define SOKOBAN_MENU BUTTON_POWER
182 #define SOKOBAN_UNDO_PRE BUTTON_REW
183 #define SOKOBAN_UNDO (BUTTON_REW | BUTTON_REL)
184 #define SOKOBAN_REDO BUTTON_FF
185 #define SOKOBAN_LEVEL_DOWN (BUTTON_PLAY | BUTTON_SCROLL_DOWN)
186 #define SOKOBAN_LEVEL_REPEAT (BUTTON_PLAY | BUTTON_RIGHT)
187 #define SOKOBAN_LEVEL_UP (BUTTON_PLAY | BUTTON_SCROLL_UP)
188 #define SOKOBAN_PAUSE BUTTON_PLAY
189 #define BUTTON_SAVE BUTTON_PLAY
190 #define BUTTON_SAVE_NAME "PLAY"
192 #elif CONFIG_KEYPAD == GIGABEAT_PAD
193 #define SOKOBAN_LEFT BUTTON_LEFT
194 #define SOKOBAN_RIGHT BUTTON_RIGHT
195 #define SOKOBAN_UP BUTTON_UP
196 #define SOKOBAN_DOWN BUTTON_DOWN
197 #define SOKOBAN_MENU BUTTON_POWER
198 #define SOKOBAN_UNDO BUTTON_SELECT
199 #define SOKOBAN_REDO BUTTON_A
200 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
201 #define SOKOBAN_LEVEL_REPEAT BUTTON_MENU
202 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
203 #define SOKOBAN_PAUSE BUTTON_SELECT
204 #define BUTTON_SAVE BUTTON_SELECT
205 #define BUTTON_SAVE_NAME "SELECT"
207 #elif CONFIG_KEYPAD == SANSA_E200_PAD
208 #define SOKOBAN_LEFT BUTTON_LEFT
209 #define SOKOBAN_RIGHT BUTTON_RIGHT
210 #define SOKOBAN_UP BUTTON_UP
211 #define SOKOBAN_DOWN BUTTON_DOWN
212 #define SOKOBAN_MENU BUTTON_POWER
213 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
214 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
215 #define SOKOBAN_REDO BUTTON_REC
216 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
217 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
218 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
219 #define SOKOBAN_PAUSE BUTTON_SELECT
220 #define BUTTON_SAVE BUTTON_SELECT
221 #define BUTTON_SAVE_NAME "SELECT"
223 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
224 #define SOKOBAN_LEFT BUTTON_LEFT
225 #define SOKOBAN_RIGHT BUTTON_RIGHT
226 #define SOKOBAN_UP BUTTON_UP
227 #define SOKOBAN_DOWN BUTTON_DOWN
228 #define SOKOBAN_MENU BUTTON_POWER
229 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
230 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
231 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_LEFT)
232 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
233 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
234 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
235 #define SOKOBAN_PAUSE BUTTON_SELECT
236 #define BUTTON_SAVE BUTTON_SELECT
237 #define BUTTON_SAVE_NAME "SELECT"
239 #elif CONFIG_KEYPAD == SANSA_C200_PAD
240 #define SOKOBAN_LEFT BUTTON_LEFT
241 #define SOKOBAN_RIGHT BUTTON_RIGHT
242 #define SOKOBAN_UP BUTTON_UP
243 #define SOKOBAN_DOWN BUTTON_DOWN
244 #define SOKOBAN_MENU BUTTON_POWER
245 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
246 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
247 #define SOKOBAN_REDO BUTTON_REC
248 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
249 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
250 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
251 #define SOKOBAN_PAUSE BUTTON_SELECT
252 #define BUTTON_SAVE BUTTON_SELECT
253 #define BUTTON_SAVE_NAME "SELECT"
255 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
256 #define SOKOBAN_LEFT BUTTON_LEFT
257 #define SOKOBAN_RIGHT BUTTON_RIGHT
258 #define SOKOBAN_UP BUTTON_UP
259 #define SOKOBAN_DOWN BUTTON_DOWN
260 #define SOKOBAN_MENU BUTTON_POWER
261 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
262 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
263 #define SOKOBAN_REDO BUTTON_HOME
264 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
265 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
266 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
267 #define SOKOBAN_PAUSE BUTTON_SELECT
268 #define BUTTON_SAVE BUTTON_SELECT
269 #define BUTTON_SAVE_NAME "SELECT"
271 #elif CONFIG_KEYPAD == SANSA_M200_PAD
272 #define SOKOBAN_LEFT BUTTON_LEFT
273 #define SOKOBAN_RIGHT BUTTON_RIGHT
274 #define SOKOBAN_UP BUTTON_UP
275 #define SOKOBAN_DOWN BUTTON_DOWN
276 #define SOKOBAN_MENU BUTTON_POWER
277 #define SOKOBAN_UNDO_PRE BUTTON_SELECT
278 #define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
279 #define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_UP)
280 #define SOKOBAN_LEVEL_DOWN BUTTON_VOL_DOWN
281 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
282 #define SOKOBAN_LEVEL_UP BUTTON_VOL_UP
283 #define SOKOBAN_PAUSE BUTTON_SELECT
284 #define BUTTON_SAVE BUTTON_SELECT
285 #define BUTTON_SAVE_NAME "SELECT"
287 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
288 #define SOKOBAN_LEFT BUTTON_LEFT
289 #define SOKOBAN_RIGHT BUTTON_RIGHT
290 #define SOKOBAN_UP BUTTON_UP
291 #define SOKOBAN_DOWN BUTTON_DOWN
292 #define SOKOBAN_MENU BUTTON_MENU
293 #define SOKOBAN_UNDO BUTTON_VOL_UP
294 #define SOKOBAN_REDO BUTTON_VOL_DOWN
295 #define SOKOBAN_LEVEL_DOWN BUTTON_PREV
296 #define SOKOBAN_LEVEL_REPEAT BUTTON_PLAY
297 #define SOKOBAN_LEVEL_UP BUTTON_NEXT
298 #define SOKOBAN_PAUSE BUTTON_SELECT
299 #define BUTTON_SAVE BUTTON_SELECT
300 #define BUTTON_SAVE_NAME "SELECT"
302 #elif CONFIG_KEYPAD == MROBE100_PAD
303 #define SOKOBAN_LEFT BUTTON_LEFT
304 #define SOKOBAN_RIGHT BUTTON_RIGHT
305 #define SOKOBAN_UP BUTTON_UP
306 #define SOKOBAN_DOWN BUTTON_DOWN
307 #define SOKOBAN_MENU BUTTON_POWER
308 #define SOKOBAN_UNDO BUTTON_SELECT
309 #define SOKOBAN_REDO BUTTON_MENU
310 #define SOKOBAN_LEVEL_DOWN (BUTTON_DISPLAY | BUTTON_DOWN)
311 #define SOKOBAN_LEVEL_REPEAT (BUTTON_DISPLAY | BUTTON_RIGHT)
312 #define SOKOBAN_LEVEL_UP (BUTTON_DISPLAY | BUTTON_UP)
313 #define SOKOBAN_PAUSE BUTTON_SELECT
314 #define BUTTON_SAVE BUTTON_SELECT
315 #define BUTTON_SAVE_NAME "SELECT"
317 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
318 #define SOKOBAN_LEFT BUTTON_RC_REW
319 #define SOKOBAN_RIGHT BUTTON_RC_FF
320 #define SOKOBAN_UP BUTTON_RC_VOL_UP
321 #define SOKOBAN_DOWN BUTTON_RC_VOL_DOWN
322 #define SOKOBAN_MENU BUTTON_RC_REC
323 #define SOKOBAN_UNDO BUTTON_RC_MODE
324 #define SOKOBAN_REDO BUTTON_RC_MENU
325 #define SOKOBAN_PAUSE BUTTON_RC_PLAY
326 #define BUTTON_SAVE BUTTON_RC_PLAY
327 #define BUTTON_SAVE_NAME "PLAY"
329 #define SOKOBAN_RC_MENU BUTTON_REC
331 #elif CONFIG_KEYPAD == COWOND2_PAD
332 #define SOKOBAN_MENU BUTTON_MENU
333 #define SOKOBAN_MENU_NAME "[MENU]"
335 #elif CONFIG_KEYPAD == IAUDIO67_PAD
336 #define SOKOBAN_LEFT BUTTON_LEFT
337 #define SOKOBAN_RIGHT BUTTON_RIGHT
338 #define SOKOBAN_UP BUTTON_STOP
339 #define SOKOBAN_DOWN BUTTON_PLAY
340 #define SOKOBAN_MENU BUTTON_MENU
341 #define SOKOBAN_UNDO BUTTON_VOLDOWN
342 #define SOKOBAN_REDO BUTTON_VOLUP
343 #define SOKOBAN_PAUSE (BUTTON_MENU|BUTTON_LEFT)
344 #define BUTTON_SAVE (BUTTON_MENU|BUTTON_PLAY)
345 #define BUTTON_SAVE_NAME "MENU+PLAY"
347 #define SOKOBAN_RC_MENU (BUTTON_MENU|BUTTON_STOP)
349 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
350 #define SOKOBAN_LEFT BUTTON_LEFT
351 #define SOKOBAN_RIGHT BUTTON_RIGHT
352 #define SOKOBAN_UP BUTTON_UP
353 #define SOKOBAN_DOWN BUTTON_DOWN
354 #define SOKOBAN_MENU BUTTON_MENU
355 #define SOKOBAN_UNDO BUTTON_BACK
356 #define SOKOBAN_REDO (BUTTON_BACK | BUTTON_PLAY)
357 #define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
358 #define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
359 #define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
360 #define SOKOBAN_PAUSE BUTTON_PLAY
361 #define BUTTON_SAVE BUTTON_CUSTOM
362 #define BUTTON_SAVE_NAME "CUSTOM"
364 #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 #else
380 #error No keymap defined!
381 #endif
383 #ifdef HAVE_TOUCHSCREEN
384 #ifndef SOKOBAN_LEFT
385 #define SOKOBAN_LEFT BUTTON_MIDLEFT
386 #endif
387 #ifndef SOKOBAN_RIGHT
388 #define SOKOBAN_RIGHT BUTTON_MIDRIGHT
389 #endif
390 #ifndef SOKOBAN_UP
391 #define SOKOBAN_UP BUTTON_TOPMIDDLE
392 #endif
393 #ifndef SOKOBAN_DOWN
394 #define SOKOBAN_DOWN BUTTON_BOTTOMMIDDLE
395 #endif
396 #ifndef SOKOBAN_MENU
397 #define SOKOBAN_MENU BUTTON_TOPLEFT
398 #define SOKOBAN_MENU_NAME "[TOPLEFT]"
399 #endif
400 #ifndef SOKOBAN_UNDO
401 #define SOKOBAN_UNDO BUTTON_BOTTOMRIGHT
402 #define SOKOBAN_UNDO_NAME "[BOTTOMRIGHT]"
403 #endif
404 #ifndef SOKOBAN_REDO
405 #define SOKOBAN_REDO BUTTON_BOTTOMLEFT
406 #define SOKOBAN_REDO_NAME "[BOTTOMLEFT]"
407 #endif
408 #ifndef SOKOBAN_PAUSE
409 #define SOKOBAN_PAUSE BUTTON_CENTER
410 #define SOKOBAN_PAUSE_NAME "[CENTER]"
411 #endif
412 #ifndef SOKOBAN_LEVEL_REPEAT
413 #define SOKOBAN_LEVEL_REPEAT BUTTON_TOPRIGHT
414 #define SOKOBAN_LEVEL_REPEAT_NAME "[TOPRIGHT]"
415 #endif
416 #ifndef BUTTON_SAVE
417 #define BUTTON_SAVE BUTTON_CENTER
418 #define BUTTON_SAVE_NAME "CENTER"
419 #endif
420 #endif
422 #define SOKOBAN_FONT FONT_SYSFIXED
425 /* The Location, Undo and LevelInfo structs are OO-flavored.
426 * (oooh!-flavored as Schnueff puts it.) It makes more you have to know,
427 * but the overall data layout becomes more manageable. */
429 /* Level data & stats */
430 struct LevelInfo {
431 int index; /* Level index (level number - 1) */
432 int moves; /* Moves & pushes for the stats */
433 int pushes;
434 short boxes_to_go; /* Number of unplaced boxes remaining in level */
435 short height; /* Height & width for centering level display */
436 short width;
439 struct Location {
440 short row;
441 short col;
444 /* Our full undo history */
445 static struct UndoInfo {
446 int count; /* How many undos have been done */
447 int current; /* Which history is the current undo */
448 int max; /* Which history is the max redoable */
449 char history[MAX_UNDOS];
450 } undo_info;
452 /* Our playing board */
453 static struct BoardInfo {
454 char board[ROWS][COLS]; /* The current board data */
455 struct LevelInfo level; /* Level data & stats */
456 struct Location player; /* Where the player is */
457 int max_level; /* The number of levels we have */
458 } current_info;
460 static struct BufferedBoards {
461 char filename[MAX_PATH]; /* Filename of the levelset we're using */
462 char data[MAX_LEVEL_DATA]; /* Buffered level data */
463 int index[MAX_LEVELS + 1]; /* Where each buffered board begins & ends */
464 int start; /* Index of first buffered board */
465 int end; /* Index of last buffered board */
466 short prebuffered_boards; /* Number of boards before current to store */
467 } buffered_boards;
470 static char buf[ROWS*(COLS + 1)]; /* Enough for a whole board or a filename */
473 static void init_undo(void)
475 undo_info.count = 0;
476 undo_info.current = 0;
477 undo_info.max = 0;
480 static void get_delta(char direction, short *d_r, short *d_c)
482 switch (direction) {
483 case SOKOBAN_PUSH_LEFT:
484 case SOKOBAN_MOVE_LEFT:
485 *d_r = 0;
486 *d_c = -1;
487 break;
488 case SOKOBAN_PUSH_RIGHT:
489 case SOKOBAN_MOVE_RIGHT:
490 *d_r = 0;
491 *d_c = 1;
492 break;
493 case SOKOBAN_PUSH_UP:
494 case SOKOBAN_MOVE_UP:
495 *d_r = -1;
496 *d_c = 0;
497 break;
498 case SOKOBAN_PUSH_DOWN:
499 case SOKOBAN_MOVE_DOWN:
500 *d_r = 1;
501 *d_c = 0;
505 static bool undo(void)
507 char undo;
508 short r, c;
509 short d_r = 0, d_c = 0; /* delta row & delta col */
510 char *space_cur, *space_next, *space_prev;
511 bool undo_push = false;
513 /* If no more undos or we've wrapped all the way around, quit */
514 if (undo_info.count == 0 || undo_info.current - 1 == undo_info.max)
515 return false;
517 /* Move to previous undo in the list */
518 if (undo_info.current == 0 && undo_info.count > 1)
519 undo_info.current = MAX_UNDOS - 1;
520 else
521 undo_info.current--;
523 undo_info.count--;
525 undo = undo_info.history[undo_info.current];
527 if (undo < SOKOBAN_MOVE_MIN)
528 undo_push = true;
530 get_delta(undo, &d_r, &d_c);
532 r = current_info.player.row;
533 c = current_info.player.col;
535 /* Give the 3 spaces we're going to use better names */
536 space_cur = &current_info.board[r][c];
537 space_next = &current_info.board[r + d_r][c + d_c];
538 space_prev = &current_info.board[r - d_r][c - d_c];
540 /* Update board info */
541 if (undo_push) {
542 /* Moving box from goal to floor */
543 if (*space_next == '*' && *space_cur == '@')
544 current_info.level.boxes_to_go++;
545 /* Moving box from floor to goal */
546 else if (*space_next == '$' && *space_cur == '+')
547 current_info.level.boxes_to_go--;
549 /* Move box off of next space... */
550 *space_next = (*space_next == '*' ? '.' : ' ');
551 /* ...and on to current space */
552 *space_cur = (*space_cur == '+' ? '*' : '$');
554 current_info.level.pushes--;
555 } else
556 /* Just move player off of current space */
557 *space_cur = (*space_cur == '+' ? '.' : ' ');
558 /* Move player back to previous space */
559 *space_prev = (*space_prev == '.' ? '+' : '@');
561 /* Update position */
562 current_info.player.row -= d_r;
563 current_info.player.col -= d_c;
565 current_info.level.moves--;
567 return true;
570 static void add_undo(char undo)
572 undo_info.history[undo_info.current] = undo;
574 /* Wrap around if MAX_UNDOS exceeded */
575 if (undo_info.current < (MAX_UNDOS - 1))
576 undo_info.current++;
577 else
578 undo_info.current = 0;
580 if (undo_info.count < MAX_UNDOS)
581 undo_info.count++;
584 static bool move(char direction, bool redo)
586 short r, c;
587 short d_r = 0, d_c = 0; /* delta row & delta col */
588 char *space_cur, *space_next, *space_beyond;
589 bool push = false;
591 get_delta(direction, &d_r, &d_c);
593 r = current_info.player.row;
594 c = current_info.player.col;
596 /* Check for out-of-bounds */
597 if (r + 2*d_r < 0 || r + 2*d_r >= ROWS ||
598 c + 2*d_c < 0 || c + 2*d_c >= COLS)
599 return false;
601 /* Give the 3 spaces we're going to use better names */
602 space_cur = &current_info.board[r][c];
603 space_next = &current_info.board[r + d_r][c + d_c];
604 space_beyond = &current_info.board[r + 2*d_r][c + 2*d_c];
606 if (*space_next == '$' || *space_next == '*') {
607 /* Change direction from move to push for undo */
608 if (direction >= SOKOBAN_MOVE_MIN)
609 direction -= SOKOBAN_MOVE_DIFF;
610 push = true;
612 else if (direction < SOKOBAN_MOVE_MIN)
613 /* Change back to move if redo/solution playback push is invalid */
614 direction += SOKOBAN_MOVE_DIFF;
616 /* Update board info */
617 if (push) {
618 /* Moving box from goal to floor */
619 if (*space_next == '*' && *space_beyond == ' ')
620 current_info.level.boxes_to_go++;
621 /* Moving box from floor to goal */
622 else if (*space_next == '$' && *space_beyond == '.')
623 current_info.level.boxes_to_go--;
624 /* Check for invalid move */
625 else if (*space_beyond != '.' && *space_beyond != ' ')
626 return false;
628 /* Move player onto next space */
629 *space_next = (*space_next == '*' ? '+' : '@');
630 /* Move box onto space beyond next */
631 *space_beyond = (*space_beyond == '.' ? '*' : '$');
633 current_info.level.pushes++;
634 } else {
635 /* Check for invalid move */
636 if (*space_next != '.' && *space_next != ' ')
637 return false;
639 /* Move player onto next space */
640 *space_next = (*space_next == '.' ? '+' : '@');
642 /* Move player off of current space */
643 *space_cur = (*space_cur == '+' ? '.' : ' ');
645 /* Update position */
646 current_info.player.row += d_r;
647 current_info.player.col += d_c;
649 current_info.level.moves++;
651 /* Update undo_info.max to current on every normal move,
652 * except if it's the same as a redo. */
653 /* normal move and either */
654 if (!redo &&
655 /* moves have been undone... */
656 ((undo_info.max != undo_info.current &&
657 /* ...and the current move is NOT the same as the one in history */
658 undo_info.history[undo_info.current] != direction) ||
659 /* or moves have not been undone */
660 undo_info.max == undo_info.current)) {
661 add_undo(direction);
662 undo_info.max = undo_info.current;
663 } else /* redo move or move was same as redo */
664 add_undo(direction); /* add_undo to update current */
666 return true;
669 #ifdef SOKOBAN_REDO
670 static bool redo(void)
672 /* If no moves have been undone, quit */
673 if (undo_info.current == undo_info.max)
674 return false;
676 return move(undo_info.history[(undo_info.current < MAX_UNDOS ?
677 undo_info.current : 0)], true);
679 #endif
681 static void init_boards(void)
683 rb->strncpy(buffered_boards.filename, SOKOBAN_LEVELS_FILE, MAX_PATH);
685 current_info.level.index = 0;
686 current_info.player.row = 0;
687 current_info.player.col = 0;
688 current_info.max_level = 0;
690 buffered_boards.start = 0;
691 buffered_boards.end = 0;
692 buffered_boards.prebuffered_boards = 0;
694 init_undo();
697 static bool read_levels(bool initialize)
699 int fd = 0;
700 short len;
701 short lastlen = 0;
702 short row = 0;
703 int level_count = 0;
705 int i = 0;
706 int level_len = 0;
707 bool index_set = false;
709 /* Get the index of the first level to buffer */
710 if (current_info.level.index > buffered_boards.prebuffered_boards &&
711 !initialize)
712 buffered_boards.start = current_info.level.index -
713 buffered_boards.prebuffered_boards;
714 else
715 buffered_boards.start = 0;
717 if ((fd = rb->open(buffered_boards.filename, O_RDONLY)) < 0) {
718 rb->splashf(HZ*2, "Unable to open %s", buffered_boards.filename);
719 return false;
722 do {
723 len = rb->read_line(fd, buf, sizeof(buf));
725 /* Correct len when trailing \r's or \n's are counted */
726 if (len > 2 && buf[len - 2] == '\0')
727 len -= 2;
728 else if (len > 1 && buf[len - 1] == '\0')
729 len--;
731 /* Skip short lines & lines with non-level data */
732 if (len >= 3 && ((buf[0] >= '1' && buf[0] <= '9') || buf[0] == '#' ||
733 buf[0] == ' ' || buf[0] == '-' || buf[0] == '_')) {
734 if (level_count >= buffered_boards.start) {
735 /* Set the index of this level */
736 if (!index_set &&
737 level_count - buffered_boards.start < MAX_LEVELS) {
738 buffered_boards.index[level_count - buffered_boards.start]
739 = i;
740 index_set = true;
742 /* Copy buffer to board data */
743 if (i + level_len + len < MAX_LEVEL_DATA) {
744 rb->memcpy(&buffered_boards.data[i + level_len], buf, len);
745 buffered_boards.data[i + level_len + len] = '\n';
748 level_len += len + 1;
749 row++;
751 /* If newline & level is tall enough or is RLE */
752 } else if (buf[0] == '\0' && (row > 2 || lastlen > 22)) {
753 level_count++;
754 if (level_count >= buffered_boards.start) {
755 i += level_len;
756 if (i < MAX_LEVEL_DATA)
757 buffered_boards.end = level_count;
758 else if (!initialize)
759 break;
761 row = 0;
762 level_len = 0;
763 index_set = false;
765 } else if (len > 22)
766 len = 1;
768 } while ((lastlen = len));
770 /* Set the index of the end of the last level */
771 if (level_count - buffered_boards.start < MAX_LEVELS)
772 buffered_boards.index[level_count - buffered_boards.start] = i;
774 if (initialize) {
775 current_info.max_level = level_count;
776 buffered_boards.prebuffered_boards = buffered_boards.end/2;
779 rb->close(fd);
781 return true;
784 static void load_level(void)
786 int c, r;
787 int i, n;
788 int level_size;
789 int index = current_info.level.index - buffered_boards.start;
790 char *level;
792 /* Get the buffered board index of the current level */
793 if (current_info.level.index < buffered_boards.start ||
794 current_info.level.index >= buffered_boards.end) {
795 read_levels(false);
796 if (current_info.level.index > buffered_boards.prebuffered_boards)
797 index = buffered_boards.prebuffered_boards;
798 else
799 index = current_info.level.index;
801 level = &buffered_boards.data[buffered_boards.index[index]];
803 /* Reset level info */
804 current_info.level.moves = 0;
805 current_info.level.pushes = 0;
806 current_info.level.boxes_to_go = 0;
807 current_info.level.width = 0;
809 /* Clear board */
810 for (r = 0; r < ROWS; r++)
811 for (c = 0; c < COLS; c++)
812 current_info.board[r][c] = 'X';
814 level_size = buffered_boards.index[index + 1] -
815 buffered_boards.index[index];
817 for (r = 0, c = 0, n = 1, i = 0; i < level_size; i++) {
818 if (level[i] == '\n' || level[i] == '|') {
819 if (c > 3) {
820 /* Update max width of level & go to next row */
821 if (c > current_info.level.width)
822 current_info.level.width = c;
823 c = 0;
824 r++;
825 if (r >= ROWS)
826 break;
828 } else if (c < COLS) {
829 /* Read RLE character's length into n */
830 if (level[i] >= '0' && level[i] <= '9') {
831 n = level[i++] - '0';
832 if (level[i] >= '0' && level[i] <= '9')
833 n = n*10 + level[i++] - '0';
836 /* Cleanup & replace */
837 if (level[i] == '%')
838 level[i] = '*';
839 else if (level[i] == '-' || level[i] == '_')
840 level[i] = ' ';
842 if (n > 1) {
843 if (c + n >= COLS)
844 n = COLS - c;
846 if (level[i] == '.')
847 current_info.level.boxes_to_go += n;
849 /* Put RLE character n times */
850 while (n--)
851 current_info.board[r][c++] = level[i];
852 n = 1;
854 } else {
855 if (level[i] == '.' || level[i] == '+')
856 current_info.level.boxes_to_go++;
858 if (level[i] == '@' ||level[i] == '+') {
859 current_info.player.row = r;
860 current_info.player.col = c;
863 current_info.board[r][c++] = level[i];
868 current_info.level.height = r;
870 #if LCD_DEPTH > 2
871 /* Fill in blank space outside level on color targets */
872 for (r = 0; r < ROWS; r++)
873 for (c = 0; current_info.board[r][c] == ' ' && c < COLS; c++)
874 current_info.board[r][c] = 'X';
876 for (c = 0; c < COLS; c++) {
877 for (r = 0; (current_info.board[r][c] == ' ' ||
878 current_info.board[r][c] == 'X') && r < ROWS; r++)
879 current_info.board[r][c] = 'X';
880 for (r = ROWS - 1; (current_info.board[r][c] == ' ' ||
881 current_info.board[r][c] == 'X') && r >= 0; r--)
882 current_info.board[r][c] = 'X';
884 #endif
887 static void update_screen(void)
889 int c, r;
890 int rows, cols;
892 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) < 32
893 #define STAT_HEIGHT 25
894 #define STAT_X (LCD_WIDTH - 120)/2
895 #define STAT_Y (LCD_HEIGHT - STAT_HEIGHT)
896 #define BOARD_WIDTH LCD_WIDTH
897 #define BOARD_HEIGHT (LCD_HEIGHT - STAT_HEIGHT)
898 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 4, "Level");
899 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
900 rb->lcd_putsxy(STAT_X + 7, STAT_Y + 14, buf);
901 rb->lcd_putsxy(STAT_X + 41, STAT_Y + 4, "Moves");
902 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
903 rb->lcd_putsxy(STAT_X + 44, STAT_Y + 14, buf);
904 rb->lcd_putsxy(STAT_X + 79, STAT_Y + 4, "Pushes");
905 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
906 rb->lcd_putsxy(STAT_X + 82, STAT_Y + 14, buf);
908 rb->lcd_drawrect(STAT_X, STAT_Y, 38, STAT_HEIGHT);
909 rb->lcd_drawrect(STAT_X + 37, STAT_Y, 39, STAT_HEIGHT);
910 rb->lcd_drawrect(STAT_X + 75, STAT_Y, 45, STAT_HEIGHT);
911 #else
912 #if LCD_WIDTH - (COLS*SOKOBAN_TILESIZE) > 40
913 #define STAT_X (LCD_WIDTH - 40)
914 #else
915 #define STAT_X COLS*SOKOBAN_TILESIZE
916 #endif
917 #define STAT_Y (LCD_HEIGHT - 64)/2
918 #define STAT_WIDTH (LCD_WIDTH - STAT_X)
919 #define BOARD_WIDTH (LCD_WIDTH - STAT_WIDTH)
920 #define BOARD_HEIGHT LCD_HEIGHT
921 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 2, "Level");
922 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.index + 1);
923 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 12, buf);
924 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 23, "Moves");
925 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.moves);
926 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 33, buf);
927 #if STAT_WIDTH < 38
928 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Push");
929 #else
930 rb->lcd_putsxy(STAT_X + 1, STAT_Y + 44, "Pushes");
931 #endif
932 rb->snprintf(buf, sizeof(buf), "%d", current_info.level.pushes);
933 rb->lcd_putsxy(STAT_X + 4, STAT_Y + 54, buf);
935 rb->lcd_drawrect(STAT_X, STAT_Y + 0, STAT_WIDTH, 64);
936 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 21);
937 rb->lcd_hline(STAT_X, LCD_WIDTH - 1, STAT_Y + 42);
939 #endif
941 /* load the board to the screen */
942 for (rows = 0; rows < ROWS; rows++) {
943 for (cols = 0; cols < COLS; cols++) {
944 c = cols*SOKOBAN_TILESIZE +
945 (BOARD_WIDTH - current_info.level.width*SOKOBAN_TILESIZE)/2;
946 r = rows*SOKOBAN_TILESIZE +
947 (BOARD_HEIGHT - current_info.level.height*SOKOBAN_TILESIZE)/2;
949 switch(current_info.board[rows][cols]) {
950 case 'X': /* blank space outside of level */
951 break;
953 case ' ': /* floor */
954 rb->lcd_bitmap_part(sokoban_tiles, 0, 0*SOKOBAN_TILESIZE,
955 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
956 SOKOBAN_TILESIZE);
957 break;
959 case '#': /* wall */
960 rb->lcd_bitmap_part(sokoban_tiles, 0, 1*SOKOBAN_TILESIZE,
961 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
962 SOKOBAN_TILESIZE);
963 break;
965 case '$': /* box */
966 rb->lcd_bitmap_part(sokoban_tiles, 0, 2*SOKOBAN_TILESIZE,
967 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
968 SOKOBAN_TILESIZE);
969 break;
971 case '*': /* box on goal */
972 rb->lcd_bitmap_part(sokoban_tiles, 0, 3*SOKOBAN_TILESIZE,
973 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
974 SOKOBAN_TILESIZE);
975 break;
977 case '.': /* goal */
978 rb->lcd_bitmap_part(sokoban_tiles, 0, 4*SOKOBAN_TILESIZE,
979 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
980 SOKOBAN_TILESIZE);
981 break;
983 case '@': /* player */
984 rb->lcd_bitmap_part(sokoban_tiles, 0, 5*SOKOBAN_TILESIZE,
985 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
986 SOKOBAN_TILESIZE);
987 break;
989 case '+': /* player on goal */
990 rb->lcd_bitmap_part(sokoban_tiles, 0, 6*SOKOBAN_TILESIZE,
991 SOKOBAN_TILESIZE, c, r, SOKOBAN_TILESIZE,
992 SOKOBAN_TILESIZE);
993 break;
998 /* print out the screen */
999 rb->lcd_update();
1002 static void draw_level(void)
1004 load_level();
1005 rb->lcd_clear_display();
1006 update_screen();
1009 static bool save(char *filename, bool solution)
1011 int fd;
1012 char *loc;
1013 DIR *dir;
1014 char dirname[MAX_PATH];
1016 rb->splash(0, "Saving...");
1018 /* Create dir if it doesn't exist */
1019 if ((loc = rb->strrchr(filename, '/')) != NULL) {
1020 rb->strncpy(dirname, filename, loc - filename);
1021 dirname[loc - filename] = '\0';
1022 if(!(dir = rb->opendir(dirname)))
1023 rb->mkdir(dirname);
1024 else
1025 rb->closedir(dir);
1028 if (filename[0] == '\0' ||
1029 (fd = rb->open(filename, O_WRONLY|O_CREAT|O_TRUNC)) < 0) {
1030 rb->splashf(HZ*2, "Unable to open %s", filename);
1031 return false;
1034 /* Sokoban: S/P for solution/progress : level number : current undo */
1035 rb->snprintf(buf, sizeof(buf), "Sokoban:%c:%d:%d\n", (solution ? 'S' : 'P'),
1036 current_info.level.index + 1, undo_info.current);
1037 rb->write(fd, buf, rb->strlen(buf));
1039 /* Filename of levelset */
1040 rb->write(fd, buffered_boards.filename,
1041 rb->strlen(buffered_boards.filename));
1042 rb->write(fd, "\n", 1);
1044 /* Full undo history */
1045 rb->write(fd, undo_info.history, undo_info.max);
1047 rb->close(fd);
1049 return true;
1052 static bool load(char *filename, bool silent)
1054 int fd;
1055 int i, n;
1056 int len;
1057 int button;
1058 bool play_solution;
1059 bool paused = false;
1060 unsigned short speed = 2;
1061 int delay[] = {HZ/2, HZ/3, HZ/4, HZ/6, HZ/8, HZ/12, HZ/16, HZ/25};
1063 if (filename[0] == '\0' || (fd = rb->open(filename, O_RDONLY)) < 0) {
1064 if (!silent)
1065 rb->splashf(HZ*2, "Unable to open %s", filename);
1066 return false;
1069 /* Read header, level number, & current undo */
1070 rb->read_line(fd, buf, sizeof(buf));
1072 /* If we're opening a level file, not a solution/progress file */
1073 if (rb->strncmp(buf, "Sokoban", 7) != 0) {
1074 rb->close(fd);
1076 rb->strncpy(buffered_boards.filename, filename, MAX_PATH);
1077 if (!read_levels(true))
1078 return false;
1080 current_info.level.index = 0;
1081 load_level();
1083 /* If there aren't any boxes to go or the player position wasn't set,
1084 * the file probably wasn't a Sokoban level file */
1085 if (current_info.level.boxes_to_go == 0 ||
1086 current_info.player.row == 0 || current_info.player.col == 0) {
1087 if (!silent)
1088 rb->splash(HZ*2, "File is not a Sokoban level file");
1089 return false;
1092 } else {
1094 /* Read filename of levelset */
1095 rb->read_line(fd, buffered_boards.filename,
1096 sizeof(buffered_boards.filename));
1098 /* Read full undo history */
1099 len = rb->read_line(fd, undo_info.history, MAX_UNDOS);
1101 /* Correct len when trailing \r's or \n's are counted */
1102 if (len > 2 && undo_info.history[len - 2] == '\0')
1103 len -= 2;
1104 else if (len > 1 && undo_info.history[len - 1] == '\0')
1105 len--;
1107 rb->close(fd);
1109 /* Check to see if we're going to play a solution or resume progress */
1110 play_solution = (buf[8] == 'S');
1112 /* Get level number */
1113 for (n = 0, i = 10; buf[i] >= '0' && buf[i] <= '9' && i < 15; i++)
1114 n = n*10 + buf[i] - '0';
1115 current_info.level.index = n - 1;
1117 /* Get undo index */
1118 for (n = 0, i++; buf[i] >= '0' && buf[i] <= '9' && i < 21; i++)
1119 n = n*10 + buf[i] - '0';
1120 if (n > len)
1121 n = len;
1122 undo_info.max = len;
1124 if (current_info.level.index < 0) {
1125 if (!silent)
1126 rb->splash(HZ*2, "Error loading level");
1127 return false;
1129 if (!read_levels(true))
1130 return false;
1131 if (current_info.level.index >= current_info.max_level) {
1132 if (!silent)
1133 rb->splash(HZ*2, "Error loading level");
1134 return false;
1137 load_level();
1139 if (play_solution) {
1140 rb->lcd_clear_display();
1141 update_screen();
1142 rb->sleep(2*delay[speed]);
1144 /* Replay solution until menu button is pressed */
1145 i = 0;
1146 while (true) {
1147 if (i < len) {
1148 if (!move(undo_info.history[i], true)) {
1149 n = i;
1150 break;
1152 rb->lcd_clear_display();
1153 update_screen();
1154 i++;
1155 } else
1156 paused = true;
1158 rb->sleep(delay[speed]);
1160 while ((button = rb->button_get(false)) || paused) {
1161 switch (button) {
1162 case SOKOBAN_MENU:
1163 /* Pretend the level is complete so we'll quit */
1164 current_info.level.boxes_to_go = 0;
1165 return true;
1167 case SOKOBAN_PAUSE:
1168 /* Toggle pause state */
1169 paused = !paused;
1170 break;
1172 case SOKOBAN_LEFT:
1173 case SOKOBAN_LEFT | BUTTON_REPEAT:
1174 /* Go back one move */
1175 if (paused) {
1176 if (undo())
1177 i--;
1178 rb->lcd_clear_display();
1179 update_screen();
1181 break;
1183 case SOKOBAN_RIGHT:
1184 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1185 /* Go forward one move */
1186 if (paused) {
1187 if (redo())
1188 i++;
1189 rb->lcd_clear_display();
1190 update_screen();
1192 break;
1194 case SOKOBAN_UP:
1195 case SOKOBAN_UP | BUTTON_REPEAT:
1196 /* Speed up */
1197 if (speed < sizeof(delay)/sizeof(int) - 1)
1198 speed++;
1199 break;
1201 case SOKOBAN_DOWN:
1202 case SOKOBAN_DOWN | BUTTON_REPEAT:
1203 /* Slow down */
1204 if (speed > 0)
1205 speed--;
1208 if (paused)
1209 rb->sleep(HZ/33);
1213 /* If level is complete, wait for keypress before quitting */
1214 if (current_info.level.boxes_to_go == 0)
1215 rb->button_get(true);
1217 } else {
1218 /* Advance to current undo */
1219 for (i = 0; i < n; i++) {
1220 if (!move(undo_info.history[i], true)) {
1221 n = i;
1222 break;
1226 rb->button_clear_queue();
1227 rb->lcd_clear_display();
1230 undo_info.current = n;
1233 return true;
1236 static int sokoban_menu(void)
1238 int button;
1239 int selection = 0;
1240 int i;
1241 bool menu_quit;
1242 int start_selected = 0;
1243 int prev_level = current_info.level.index;
1245 MENUITEM_STRINGLIST(menu, "Sokoban Menu", NULL,
1246 "Resume", "Select Level", "Audio Playback", "Keys",
1247 "Load Default Level Set", "Quit Without Saving",
1248 "Save Progress & Quit");
1250 do {
1251 menu_quit = true;
1252 selection = rb->do_menu(&menu, &start_selected, NULL, false);
1254 switch (selection) {
1255 case 0: /* Resume */
1256 break;
1258 case 1: /* Select level */
1259 current_info.level.index++;
1260 rb->set_int("Select Level", "", UNIT_INT,
1261 &current_info.level.index, NULL, 1, 1,
1262 current_info.max_level, NULL);
1263 current_info.level.index--;
1264 if (prev_level != current_info.level.index) {
1265 init_undo();
1266 draw_level();
1267 } else
1268 menu_quit = false;
1269 break;
1271 case 2: /* Audio playback control */
1272 playback_control(NULL);
1273 menu_quit = false;
1274 break;
1276 case 3: /* Keys */
1277 FOR_NB_SCREENS(i)
1278 rb->screens[i]->clear_display();
1279 rb->lcd_setfont(SOKOBAN_FONT);
1281 #if (CONFIG_KEYPAD == RECORDER_PAD) || \
1282 (CONFIG_KEYPAD == ARCHOS_AV300_PAD)
1283 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1284 rb->lcd_putsxy(3, 16, "[ON] Undo");
1285 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1286 rb->lcd_putsxy(3, 36, "[F1] Down a Level");
1287 rb->lcd_putsxy(3, 46, "[F2] Restart Level");
1288 rb->lcd_putsxy(3, 56, "[F3] Up a Level");
1289 #elif CONFIG_KEYPAD == ONDIO_PAD
1290 rb->lcd_putsxy(3, 6, "[OFF] Menu");
1291 rb->lcd_putsxy(3, 16, "[MODE] Undo");
1292 rb->lcd_putsxy(3, 26, "[MODE+DOWN] Redo");
1293 rb->lcd_putsxy(3, 36, "[MODE+LEFT] Previous Level");
1294 rb->lcd_putsxy(3, 46, "[MODE+UP] Restart Level");
1295 rb->lcd_putsxy(3, 56, "[MODE+RIGHT] Up Level");
1296 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
1297 (CONFIG_KEYPAD == IRIVER_H300_PAD)
1298 rb->lcd_putsxy(3, 6, "[STOP] Menu");
1299 rb->lcd_putsxy(3, 16, "[REC] Undo");
1300 rb->lcd_putsxy(3, 26, "[MODE] Redo");
1301 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1302 rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
1303 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1304 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
1305 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
1306 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
1307 rb->lcd_putsxy(3, 6, "[SELECT+MENU] Menu");
1308 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1309 rb->lcd_putsxy(3, 26, "[SELECT+PLAY] Redo");
1310 rb->lcd_putsxy(3, 36, "[SELECT+LEFT] Previous Level");
1311 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Next Level");
1312 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
1313 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1314 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1315 rb->lcd_putsxy(3, 26, "[PLAY] Redo");
1316 rb->lcd_putsxy(3, 36, "[REC] Restart Level");
1317 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
1318 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1319 rb->lcd_putsxy(3, 16, "[REW] Undo");
1320 rb->lcd_putsxy(3, 26, "[FF] Redo");
1321 rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Previous Level");
1322 rb->lcd_putsxy(3, 46, "[PLAY+RIGHT] Restart Level");
1323 rb->lcd_putsxy(3, 56, "[PLAY+UP] Next Level");
1324 #elif CONFIG_KEYPAD == GIGABEAT_PAD
1325 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1326 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1327 rb->lcd_putsxy(3, 26, "[A] Redo");
1328 rb->lcd_putsxy(3, 36, "[VOL-] Previous Level");
1329 rb->lcd_putsxy(3, 46, "[MENU] Restart Level");
1330 rb->lcd_putsxy(3, 56, "[VOL+] Next Level");
1331 #elif CONFIG_KEYPAD == SANSA_E200_PAD
1332 rb->lcd_putsxy(3, 6, "[POWER] Menu");
1333 rb->lcd_putsxy(3, 16, "[SELECT] Undo");
1334 rb->lcd_putsxy(3, 26, "[REC] Redo");
1335 rb->lcd_putsxy(3, 36, "[SELECT+DOWN] Previous Level");
1336 rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Restart Level");
1337 rb->lcd_putsxy(3, 56, "[SELECT+UP] Next Level");
1338 #endif
1340 #ifdef HAVE_TOUCHSCREEN
1341 rb->lcd_putsxy(3, 6, SOKOBAN_MENU_NAME " Menu");
1342 rb->lcd_putsxy(3, 16, SOKOBAN_UNDO_NAME " Undo");
1343 rb->lcd_putsxy(3, 26, SOKOBAN_REDO_NAME " Redo");
1344 rb->lcd_putsxy(3, 36, SOKOBAN_PAUSE_NAME " Pause");
1345 rb->lcd_putsxy(3, 46, SOKOBAN_LEVEL_REPEAT_NAME " Restart Level");
1346 #endif
1348 FOR_NB_SCREENS(i)
1349 rb->screens[i]->update();
1351 /* Display until keypress */
1352 do {
1353 rb->sleep(HZ/20);
1354 button = rb->button_get(false);
1355 } while (!button || button & BUTTON_REL ||
1356 button & BUTTON_REPEAT);
1358 menu_quit = false;
1359 break;
1361 case 4: /* Load default levelset */
1362 init_boards();
1363 if (!read_levels(true))
1364 return 5; /* Quit */
1365 load_level();
1366 break;
1368 case 5: /* Quit */
1369 break;
1371 case 6: /* Save & quit */
1372 save(SOKOBAN_SAVE_FILE, false);
1373 rb->reload_directory();
1376 } while (!menu_quit);
1378 /* Restore font */
1379 rb->lcd_setfont(SOKOBAN_FONT);
1381 FOR_NB_SCREENS(i) {
1382 rb->screens[i]->clear_display();
1383 rb->screens[i]->update();
1386 return selection;
1389 static bool sokoban_loop(void)
1391 bool moved;
1392 int i = 0, button = 0, lastbutton = 0;
1393 short r = 0, c = 0;
1394 int w, h;
1395 char *loc;
1397 while (true) {
1398 moved = false;
1400 r = current_info.player.row;
1401 c = current_info.player.col;
1403 button = rb->button_get(true);
1405 switch(button)
1407 #ifdef SOKOBAN_RC_MENU
1408 case SOKOBAN_RC_MENU:
1409 #endif
1410 case SOKOBAN_MENU:
1411 switch (sokoban_menu()) {
1412 case 5: /* Quit */
1413 case 6: /* Save & quit */
1414 return PLUGIN_OK;
1416 update_screen();
1417 break;
1419 case SOKOBAN_UNDO:
1420 #ifdef SOKOBAN_UNDO_PRE
1421 if (lastbutton != SOKOBAN_UNDO_PRE)
1422 break;
1423 #else /* repeat can't work here for Ondio, iPod, et al */
1424 case SOKOBAN_UNDO | BUTTON_REPEAT:
1425 #endif
1426 undo();
1427 rb->lcd_clear_display();
1428 update_screen();
1429 break;
1431 #ifdef SOKOBAN_REDO
1432 case SOKOBAN_REDO:
1433 case SOKOBAN_REDO | BUTTON_REPEAT:
1434 moved = redo();
1435 rb->lcd_clear_display();
1436 update_screen();
1437 break;
1438 #endif
1440 #ifdef SOKOBAN_LEVEL_UP
1441 case SOKOBAN_LEVEL_UP:
1442 case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
1443 /* next level */
1444 init_undo();
1445 if (current_info.level.index + 1 < current_info.max_level)
1446 current_info.level.index++;
1448 draw_level();
1449 break;
1450 #endif
1452 #ifdef SOKOBAN_LEVEL_DOWN
1453 case SOKOBAN_LEVEL_DOWN:
1454 case SOKOBAN_LEVEL_DOWN | BUTTON_REPEAT:
1455 /* previous level */
1456 init_undo();
1457 if (current_info.level.index > 0)
1458 current_info.level.index--;
1460 draw_level();
1461 break;
1462 #endif
1464 #ifdef SOKOBAN_LEVEL_REPEAT
1465 case SOKOBAN_LEVEL_REPEAT:
1466 case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
1467 /* same level */
1468 init_undo();
1469 draw_level();
1470 break;
1471 #endif
1473 case SOKOBAN_LEFT:
1474 case SOKOBAN_LEFT | BUTTON_REPEAT:
1475 moved = move(SOKOBAN_MOVE_LEFT, false);
1476 break;
1478 case SOKOBAN_RIGHT:
1479 case SOKOBAN_RIGHT | BUTTON_REPEAT:
1480 moved = move(SOKOBAN_MOVE_RIGHT, false);
1481 break;
1483 case SOKOBAN_UP:
1484 case SOKOBAN_UP | BUTTON_REPEAT:
1485 moved = move(SOKOBAN_MOVE_UP, false);
1486 break;
1488 case SOKOBAN_DOWN:
1489 case SOKOBAN_DOWN | BUTTON_REPEAT:
1490 moved = move(SOKOBAN_MOVE_DOWN, false);
1491 break;
1493 default:
1494 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
1495 return PLUGIN_USB_CONNECTED;
1496 break;
1499 lastbutton = button;
1501 if (moved) {
1502 rb->lcd_clear_display();
1503 update_screen();
1506 /* We have completed this level */
1507 if (current_info.level.boxes_to_go == 0) {
1509 if (moved) {
1510 rb->lcd_clear_display();
1512 /* Show level complete message & stats */
1513 rb->snprintf(buf, sizeof(buf), "Level %d Complete!",
1514 current_info.level.index + 1);
1515 rb->lcd_getstringsize(buf, &w, &h);
1516 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h*3, buf);
1518 rb->snprintf(buf, sizeof(buf), "%4d Moves ",
1519 current_info.level.moves);
1520 rb->lcd_getstringsize(buf, &w, &h);
1521 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h, buf);
1523 rb->snprintf(buf, sizeof(buf), "%4d Pushes",
1524 current_info.level.pushes);
1525 rb->lcd_getstringsize(buf, &w, &h);
1526 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2, buf);
1528 if (undo_info.count < MAX_UNDOS) {
1529 rb->snprintf(buf, sizeof(buf), "%s: Save solution",
1530 BUTTON_SAVE_NAME);
1531 rb->lcd_getstringsize(buf, &w, &h);
1532 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + h*2, buf);
1535 rb->lcd_update();
1536 rb->sleep(HZ/4);
1537 rb->button_clear_queue();
1539 /* Display for 4 seconds or until new keypress */
1540 for (i = 0; i < 80; i++) {
1541 rb->sleep(HZ/20);
1542 button = rb->button_get(false);
1543 if (button && !(button & BUTTON_REL) &&
1544 !(button & BUTTON_REPEAT))
1545 break;
1548 if (button == BUTTON_SAVE) {
1549 if (undo_info.count < MAX_UNDOS) {
1550 /* Set filename to current levelset plus level number
1551 * and .sok extension. Use SAVE_FOLDER if using the
1552 * default levelset, since it's in a hidden folder. */
1553 if (rb->strcmp(buffered_boards.filename,
1554 SOKOBAN_LEVELS_FILE) == 0) {
1555 rb->snprintf(buf, sizeof(buf),
1556 "%s/sokoban.%d.sok",
1557 SOKOBAN_SAVE_FOLDER,
1558 current_info.level.index + 1);
1559 } else {
1560 if ((loc = rb->strrchr(buffered_boards.filename,
1561 '.')) != NULL)
1562 *loc = '\0';
1563 rb->snprintf(buf, sizeof(buf), "%s.%d.sok",
1564 buffered_boards.filename,
1565 current_info.level.index + 1);
1566 if (loc != NULL)
1567 *loc = '.';
1570 if (!rb->kbd_input(buf, MAX_PATH))
1571 save(buf, true);
1572 } else
1573 rb->splash(HZ*2, "Solution too long to save");
1575 rb->lcd_setfont(SOKOBAN_FONT); /* Restore font */
1579 FOR_NB_SCREENS(i) {
1580 rb->screens[i]->clear_display();
1581 rb->screens[i]->update();
1584 current_info.level.index++;
1586 /* clear undo stats */
1587 init_undo();
1589 if (current_info.level.index >= current_info.max_level) {
1590 /* Show levelset complete message */
1591 rb->snprintf(buf, sizeof(buf), "You WIN!!");
1592 rb->lcd_getstringsize(buf, &w, &h);
1593 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, buf);
1595 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
1596 /* Display for 4 seconds or until keypress */
1597 for (i = 0; i < 80; i++) {
1598 rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
1599 rb->lcd_update();
1600 rb->sleep(HZ/10);
1602 button = rb->button_get(false);
1603 if (button && !(button & BUTTON_REL))
1604 break;
1606 rb->lcd_set_drawmode(DRMODE_SOLID);
1608 /* Reset to first level & show quit menu */
1609 current_info.level.index = 0;
1611 switch (sokoban_menu()) {
1612 case 5: /* Quit */
1613 case 6: /* Save & quit */
1614 return PLUGIN_OK;
1618 load_level();
1619 update_screen();
1622 } /* end while */
1624 return PLUGIN_OK;
1628 enum plugin_status plugin_start(const void* parameter)
1630 int w, h;
1632 (void)(parameter);
1634 rb->lcd_setfont(SOKOBAN_FONT);
1636 rb->lcd_clear_display();
1637 rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
1638 rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, SOKOBAN_TITLE);
1639 rb->lcd_update();
1640 rb->sleep(HZ); /* Show title for 1 second */
1642 init_boards();
1644 if (parameter == NULL) {
1645 /* Attempt to resume saved progress, otherwise start at beginning */
1646 if (!load(SOKOBAN_SAVE_FILE, true)) {
1647 init_boards();
1648 if (!read_levels(true))
1649 return PLUGIN_OK;
1650 load_level();
1653 } else {
1654 /* The plugin is being used to open a file */
1655 if (load((char*) parameter, false)) {
1656 /* If we loaded & played a solution, quit */
1657 if (current_info.level.boxes_to_go == 0)
1658 return PLUGIN_OK;
1659 } else
1660 return PLUGIN_OK;
1663 rb->lcd_clear_display();
1664 update_screen();
1666 return sokoban_loop();