2003-06-29 16:33:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Eric Linenberg
|
|
|
|
* February 2003: Robert Hak performs a cleanup/rewrite/feature addition.
|
|
|
|
* Eric smiles. Bjorn cries. Linus say 'huh?'.
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
|
2006-01-15 18:20:18 +00:00
|
|
|
PLUGIN_HEADER
|
|
|
|
|
2006-06-10 23:08:29 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
2006-06-10 20:39:33 +00:00
|
|
|
extern const fb_data sokoban_tiles[];
|
|
|
|
#endif
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
#define SOKOBAN_TITLE "Sokoban"
|
|
|
|
|
2005-06-09 06:50:02 +00:00
|
|
|
#define LEVELS_FILE PLUGIN_DIR "/sokoban.levels"
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
#define ROWS 16
|
|
|
|
#define COLS 20
|
2006-01-19 07:42:57 +00:00
|
|
|
#define SOKOBAN_LEVEL_SIZE (ROWS*COLS)
|
2007-02-13 06:48:10 +00:00
|
|
|
#define MAX_BUFFERED_BOARDS 500
|
|
|
|
/* Use either all but 12k of the plugin buffer for board data
|
|
|
|
or just enough for MAX_BUFFERED_BOARDS, which ever is less */
|
|
|
|
#if (PLUGIN_BUFFER_SIZE - 0x3000)/SOKOBAN_LEVEL_SIZE < MAX_BUFFERED_BOARDS
|
|
|
|
#define NUM_BUFFERED_BOARDS (PLUGIN_BUFFER_SIZE - 0x3000)/SOKOBAN_LEVEL_SIZE
|
|
|
|
#else
|
|
|
|
#define NUM_BUFFERED_BOARDS MAX_BUFFERED_BOARDS
|
|
|
|
#endif
|
|
|
|
/* Use 4k plus remaining plugin buffer (-8k for prog) for undo, up to 32k */
|
|
|
|
#if PLUGIN_BUFFER_SIZE - NUM_BUFFERED_BOARDS*SOKOBAN_LEVEL_SIZE - 0x2000 > \
|
|
|
|
0x7FFF
|
|
|
|
#define MAX_UNDOS 0x7FFF
|
|
|
|
#else
|
|
|
|
#define MAX_UNDOS PLUGIN_BUFFER_SIZE - \
|
|
|
|
NUM_BUFFERED_BOARDS*SOKOBAN_LEVEL_SIZE - 0x2000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Move/push definitions for undo */
|
|
|
|
enum {
|
|
|
|
SOKOBAN_PUSH_LEFT,
|
|
|
|
SOKOBAN_PUSH_RIGHT,
|
|
|
|
SOKOBAN_PUSH_UP,
|
|
|
|
SOKOBAN_PUSH_DOWN,
|
|
|
|
SOKOBAN_MOVE_LEFT,
|
|
|
|
SOKOBAN_MOVE_RIGHT,
|
|
|
|
SOKOBAN_MOVE_UP,
|
|
|
|
SOKOBAN_MOVE_DOWN
|
|
|
|
};
|
|
|
|
#define SOKOBAN_MOVE_DIFF (SOKOBAN_MOVE_LEFT-SOKOBAN_PUSH_LEFT)
|
|
|
|
#define SOKOBAN_MOVE_MIN SOKOBAN_MOVE_LEFT
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
/* variable button definitions */
|
|
|
|
#if CONFIG_KEYPAD == RECORDER_PAD
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SOKOBAN_UP BUTTON_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_DOWN
|
2004-10-18 21:45:00 +00:00
|
|
|
#define SOKOBAN_QUIT BUTTON_OFF
|
|
|
|
#define SOKOBAN_UNDO BUTTON_ON
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_REDO BUTTON_PLAY
|
2004-10-18 21:45:00 +00:00
|
|
|
#define SOKOBAN_LEVEL_UP BUTTON_F3
|
|
|
|
#define SOKOBAN_LEVEL_DOWN BUTTON_F1
|
|
|
|
#define SOKOBAN_LEVEL_REPEAT BUTTON_F2
|
|
|
|
|
2007-01-14 13:48:09 +00:00
|
|
|
#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
|
|
|
|
#define SOKOBAN_UP BUTTON_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_DOWN
|
|
|
|
#define SOKOBAN_QUIT BUTTON_OFF
|
|
|
|
#define SOKOBAN_UNDO BUTTON_ON
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_REDO BUTTON_PLAY
|
2007-01-14 13:48:09 +00:00
|
|
|
#define SOKOBAN_LEVEL_UP BUTTON_F3
|
|
|
|
#define SOKOBAN_LEVEL_DOWN BUTTON_F1
|
|
|
|
#define SOKOBAN_LEVEL_REPEAT BUTTON_F2
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
#elif CONFIG_KEYPAD == ONDIO_PAD
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SOKOBAN_UP BUTTON_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_DOWN
|
2004-10-18 21:45:00 +00:00
|
|
|
#define SOKOBAN_QUIT BUTTON_OFF
|
|
|
|
#define SOKOBAN_UNDO_PRE BUTTON_MENU
|
|
|
|
#define SOKOBAN_UNDO (BUTTON_MENU | BUTTON_REL)
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_REDO (BUTTON_MENU | BUTTON_DOWN)
|
2004-10-18 21:45:00 +00:00
|
|
|
#define SOKOBAN_LEVEL_UP (BUTTON_MENU | BUTTON_RIGHT)
|
|
|
|
#define SOKOBAN_LEVEL_DOWN (BUTTON_MENU | BUTTON_LEFT)
|
|
|
|
#define SOKOBAN_LEVEL_REPEAT (BUTTON_MENU | BUTTON_UP)
|
|
|
|
|
2005-06-29 12:47:24 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IRIVER_H300_PAD)
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SOKOBAN_UP BUTTON_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_DOWN
|
2005-02-04 12:41:09 +00:00
|
|
|
#define SOKOBAN_QUIT BUTTON_OFF
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_UNDO BUTTON_REC
|
|
|
|
#define SOKOBAN_REDO BUTTON_MODE
|
|
|
|
#define SOKOBAN_LEVEL_UP (BUTTON_ON | BUTTON_UP)
|
|
|
|
#define SOKOBAN_LEVEL_DOWN (BUTTON_ON | BUTTON_DOWN)
|
|
|
|
#define SOKOBAN_LEVEL_REPEAT BUTTON_ON
|
2005-12-14 01:31:37 +00:00
|
|
|
|
2006-06-30 16:43:47 +00:00
|
|
|
#define SOKOBAN_RC_QUIT BUTTON_RC_STOP
|
|
|
|
|
2006-02-24 20:54:09 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IPOD_3G_PAD)
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SOKOBAN_UP BUTTON_MENU
|
|
|
|
#define SOKOBAN_DOWN BUTTON_PLAY
|
|
|
|
#define SOKOBAN_QUIT (BUTTON_SELECT | BUTTON_MENU)
|
|
|
|
#define SOKOBAN_UNDO_PRE BUTTON_SELECT
|
|
|
|
#define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_REDO (BUTTON_SELECT | BUTTON_PLAY)
|
2005-12-14 01:31:37 +00:00
|
|
|
#define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_RIGHT)
|
|
|
|
#define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_LEFT)
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
/* fixme: if/when simultaneous button presses work for X5,
|
|
|
|
add redo & level repeat */
|
2007-03-16 23:02:39 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
|
2006-01-18 11:09:06 +00:00
|
|
|
#define SOKOBAN_UP BUTTON_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_DOWN
|
|
|
|
#define SOKOBAN_QUIT BUTTON_POWER
|
2006-02-24 15:06:25 +00:00
|
|
|
#define SOKOBAN_UNDO_PRE BUTTON_SELECT
|
|
|
|
#define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_LEVEL_UP BUTTON_PLAY
|
|
|
|
#define SOKOBAN_LEVEL_DOWN BUTTON_REC
|
2006-01-18 11:09:06 +00:00
|
|
|
|
2006-02-24 15:42:52 +00:00
|
|
|
#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
|
|
|
|
#define SOKOBAN_UP BUTTON_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_DOWN
|
|
|
|
#define SOKOBAN_QUIT BUTTON_A
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_UNDO BUTTON_SELECT
|
|
|
|
#define SOKOBAN_REDO BUTTON_POWER
|
|
|
|
#define SOKOBAN_LEVEL_UP (BUTTON_MENU | BUTTON_UP)
|
|
|
|
#define SOKOBAN_LEVEL_DOWN (BUTTON_MENU | BUTTON_DOWN)
|
|
|
|
#define SOKOBAN_LEVEL_REPEAT BUTTON_MENU
|
2006-02-24 15:42:52 +00:00
|
|
|
|
2006-10-26 13:38:09 +00:00
|
|
|
#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
|
|
|
|
#define SOKOBAN_UP BUTTON_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_DOWN
|
|
|
|
#define SOKOBAN_QUIT BUTTON_POWER
|
|
|
|
#define SOKOBAN_UNDO_PRE BUTTON_SELECT
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_UNDO (BUTTON_SELECT | BUTTON_REL)
|
|
|
|
#define SOKOBAN_REDO BUTTON_REC
|
|
|
|
#define SOKOBAN_LEVEL_UP (BUTTON_SELECT | BUTTON_UP)
|
|
|
|
#define SOKOBAN_LEVEL_DOWN (BUTTON_SELECT | BUTTON_DOWN)
|
|
|
|
#define SOKOBAN_LEVEL_REPEAT (BUTTON_SELECT | BUTTON_RIGHT)
|
2006-10-26 13:38:09 +00:00
|
|
|
|
2006-08-03 20:17:25 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
|
|
|
|
#define SOKOBAN_UP BUTTON_SCROLL_UP
|
|
|
|
#define SOKOBAN_DOWN BUTTON_SCROLL_DOWN
|
|
|
|
#define SOKOBAN_QUIT BUTTON_POWER
|
|
|
|
#define SOKOBAN_UNDO_PRE BUTTON_REW
|
|
|
|
#define SOKOBAN_UNDO (BUTTON_REW | BUTTON_REL)
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_REDO BUTTON_FF
|
2006-08-03 20:17:25 +00:00
|
|
|
#define SOKOBAN_LEVEL_UP (BUTTON_PLAY | BUTTON_SCROLL_UP)
|
|
|
|
#define SOKOBAN_LEVEL_DOWN (BUTTON_PLAY | BUTTON_SCROLL_DOWN)
|
2007-02-13 06:48:10 +00:00
|
|
|
#define SOKOBAN_LEVEL_REPEAT (BUTTON_PLAY | BUTTON_RIGHT)
|
2006-08-03 20:17:25 +00:00
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
#endif
|
|
|
|
|
2005-11-16 21:09:23 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
2007-02-13 06:48:10 +00:00
|
|
|
/* Background color. Default Rockbox light blue. */
|
|
|
|
#define BG_COLOR LCD_RGBPACK(181, 199, 231)
|
2006-01-19 06:25:59 +00:00
|
|
|
|
2006-06-10 23:17:56 +00:00
|
|
|
#elif LCD_DEPTH >= 2
|
2005-11-16 21:09:23 +00:00
|
|
|
#define MEDIUM_GRAY LCD_BRIGHTNESS(127)
|
2005-07-19 16:48:02 +00:00
|
|
|
#endif
|
|
|
|
|
2006-05-09 05:36:46 +00:00
|
|
|
/* The Location, Undo and LevelInfo structs are OO-flavored.
|
|
|
|
* (oooh!-flavored as Schnueff puts it.) It makes more you have to know,
|
2003-06-29 16:33:04 +00:00
|
|
|
* but the overall data layout becomes more manageable. */
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
/* Level data & stats */
|
2003-06-29 16:33:04 +00:00
|
|
|
struct LevelInfo {
|
|
|
|
short level;
|
|
|
|
short moves;
|
2007-02-13 06:48:10 +00:00
|
|
|
short pushes;
|
2003-06-29 16:33:04 +00:00
|
|
|
short boxes_to_go;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Location {
|
|
|
|
short row;
|
|
|
|
short col;
|
|
|
|
};
|
|
|
|
|
2006-01-19 06:25:59 +00:00
|
|
|
struct Board {
|
|
|
|
char spaces[ROWS][COLS];
|
|
|
|
};
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
/* Our full undo history */
|
|
|
|
static struct UndoInfo {
|
2007-02-13 06:48:10 +00:00
|
|
|
short count; /* How many undos are left */
|
2003-06-29 16:33:04 +00:00
|
|
|
short current; /* Which history is the current undo */
|
2007-02-13 06:48:10 +00:00
|
|
|
short max; /* Which history is the max redoable */
|
|
|
|
char history[MAX_UNDOS];
|
2003-06-29 16:33:04 +00:00
|
|
|
} undo_info;
|
|
|
|
|
|
|
|
/* Our playing board */
|
|
|
|
static struct BoardInfo {
|
|
|
|
char board[ROWS][COLS];
|
|
|
|
struct LevelInfo level;
|
|
|
|
struct Location player;
|
|
|
|
int max_level; /* How many levels do we have? */
|
|
|
|
int loaded_level; /* Which level is in memory */
|
|
|
|
} current_info;
|
|
|
|
|
2006-01-19 06:25:59 +00:00
|
|
|
static struct BufferedBoards {
|
2007-02-13 06:48:10 +00:00
|
|
|
struct Board levels[NUM_BUFFERED_BOARDS];
|
2006-01-19 06:25:59 +00:00
|
|
|
int low;
|
|
|
|
} buffered_boards;
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
static struct plugin_api* rb;
|
|
|
|
|
|
|
|
static void init_undo(void)
|
|
|
|
{
|
|
|
|
undo_info.count = 0;
|
2007-02-13 06:48:10 +00:00
|
|
|
undo_info.current = -1;
|
|
|
|
undo_info.max = -1;
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
static void get_delta(char direction, short *d_r, short *d_c)
|
2003-06-29 16:33:04 +00:00
|
|
|
{
|
2007-02-13 06:48:10 +00:00
|
|
|
switch (direction) {
|
|
|
|
case SOKOBAN_PUSH_LEFT:
|
|
|
|
case SOKOBAN_MOVE_LEFT:
|
|
|
|
*d_r = 0;
|
|
|
|
*d_c = -1;
|
|
|
|
break;
|
|
|
|
case SOKOBAN_PUSH_RIGHT:
|
|
|
|
case SOKOBAN_MOVE_RIGHT:
|
|
|
|
*d_r = 0;
|
|
|
|
*d_c = 1;
|
|
|
|
break;
|
|
|
|
case SOKOBAN_PUSH_UP:
|
|
|
|
case SOKOBAN_MOVE_UP:
|
|
|
|
*d_r = -1;
|
|
|
|
*d_c = 0;
|
|
|
|
break;
|
|
|
|
case SOKOBAN_PUSH_DOWN:
|
|
|
|
case SOKOBAN_MOVE_DOWN:
|
|
|
|
*d_r = 1;
|
|
|
|
*d_c = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
static void undo(void)
|
2003-06-29 16:33:04 +00:00
|
|
|
{
|
2007-02-13 06:48:10 +00:00
|
|
|
char undo;
|
|
|
|
short r, c;
|
|
|
|
short d_r = 0, d_c = 0; /* delta row & delta col */
|
|
|
|
char *space_cur, *space_next, *space_prev;
|
|
|
|
bool undo_push = false;
|
|
|
|
|
|
|
|
/* If no more undos or we've wrapped all the way around, quit */
|
|
|
|
if (undo_info.count == 0 || undo_info.current-1 == undo_info.max)
|
2003-06-29 16:33:04 +00:00
|
|
|
return;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
undo = undo_info.history[undo_info.current];
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
if (undo < SOKOBAN_MOVE_MIN)
|
|
|
|
undo_push = true;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
get_delta(undo, &d_r, &d_c);
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
r = current_info.player.row;
|
|
|
|
c = current_info.player.col;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
/* Give the 3 spaces we're going to use better names */
|
|
|
|
space_cur = ¤t_info.board[r][c];
|
|
|
|
space_next = ¤t_info.board[r + d_r][c + d_c];
|
|
|
|
space_prev = ¤t_info.board[r - d_r][c - d_c];
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
/* Update board info */
|
|
|
|
if (undo_push) {
|
|
|
|
/* Moving box from goal to blank */
|
|
|
|
if (*space_next == '%' && *space_cur == '@')
|
|
|
|
current_info.level.boxes_to_go++;
|
|
|
|
/* Moving box from blank to goal */
|
|
|
|
else if (*space_next == '$' && *space_cur == '+')
|
|
|
|
current_info.level.boxes_to_go--;
|
|
|
|
|
|
|
|
/* Move box off of next space... */
|
|
|
|
*space_next = (*space_next == '%' ? '.' : ' ');
|
|
|
|
/* ...and on to current space */
|
|
|
|
*space_cur = (*space_cur == '+' ? '%' : '$');
|
|
|
|
|
|
|
|
current_info.level.pushes--;
|
|
|
|
} else
|
|
|
|
/* Just move player off of current space */
|
|
|
|
*space_cur = (*space_cur == '+' ? '.' : ' ');
|
|
|
|
/* Move player back to previous space */
|
|
|
|
*space_prev = (*space_prev == '.' ? '+' : '@');
|
|
|
|
|
|
|
|
/* Update position */
|
|
|
|
current_info.player.row -= d_r;
|
|
|
|
current_info.player.col -= d_c;
|
|
|
|
|
|
|
|
current_info.level.moves--;
|
|
|
|
|
|
|
|
/* Move to previous undo in the list */
|
|
|
|
if (undo_info.current == 0 && undo_info.count > 1)
|
|
|
|
undo_info.current = MAX_UNDOS - 1;
|
|
|
|
else
|
|
|
|
undo_info.current--;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
undo_info.count--;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
static void add_undo(char undo)
|
|
|
|
{
|
|
|
|
/* Wrap around if MAX_UNDOS exceeded */
|
|
|
|
if (undo_info.current < (MAX_UNDOS - 1))
|
|
|
|
undo_info.current++;
|
|
|
|
else
|
|
|
|
undo_info.current = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
undo_info.history[undo_info.current] = undo;
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
if (undo_info.count < MAX_UNDOS)
|
|
|
|
undo_info.count++;
|
|
|
|
}
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
static bool move(char direction, bool redo)
|
|
|
|
{
|
|
|
|
short r, c;
|
|
|
|
short d_r = 0, d_c = 0; /* delta row & delta col */
|
|
|
|
char *space_cur, *space_next, *space_beyond;
|
|
|
|
bool push = false;
|
|
|
|
|
|
|
|
get_delta(direction, &d_r, &d_c);
|
|
|
|
|
|
|
|
r = current_info.player.row;
|
|
|
|
c = current_info.player.col;
|
|
|
|
|
|
|
|
/* Check for out-of-bounds */
|
|
|
|
if (r + 2*d_r < 0 || r + 2*d_r >= ROWS ||
|
|
|
|
c + 2*d_c < 0 || c + 2*d_c >= COLS)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Give the 3 spaces we're going to use better names */
|
|
|
|
space_cur = ¤t_info.board[r][c];
|
|
|
|
space_next = ¤t_info.board[r + d_r][c + d_c];
|
|
|
|
space_beyond = ¤t_info.board[r + 2*d_r][c + 2*d_c];
|
|
|
|
|
|
|
|
if (*space_next == '$' || *space_next == '%') {
|
|
|
|
/* Change direction from move to push for undo */
|
|
|
|
if (direction >= SOKOBAN_MOVE_MIN)
|
|
|
|
direction -= SOKOBAN_MOVE_DIFF;
|
|
|
|
push = true;
|
|
|
|
}
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
/* Update board info */
|
|
|
|
if (push) {
|
|
|
|
/* Moving box from goal to blank */
|
|
|
|
if (*space_next == '%' && *space_beyond == ' ')
|
|
|
|
current_info.level.boxes_to_go++;
|
|
|
|
/* Moving box from blank to goal */
|
|
|
|
else if (*space_next == '$' && *space_beyond == '.')
|
|
|
|
current_info.level.boxes_to_go--;
|
|
|
|
/* Check for illegal move */
|
|
|
|
else if (*space_beyond != '.' && *space_beyond != ' ')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Move player onto next space */
|
|
|
|
*space_next = (*space_next == '%' ? '+' : '@');
|
|
|
|
/* Move box onto space beyond next */
|
|
|
|
*space_beyond = (*space_beyond == '.' ? '%' : '$');
|
|
|
|
|
|
|
|
current_info.level.pushes++;
|
|
|
|
} else {
|
|
|
|
/* Check for illegal move */
|
|
|
|
if (*space_next == '#' || *space_next == 'X')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Move player onto next space */
|
|
|
|
*space_next = (*space_next == '.' ? '+' : '@');
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
2007-02-13 06:48:10 +00:00
|
|
|
/* Move player off of current space */
|
|
|
|
*space_cur = (*space_cur == '+' ? '.' : ' ');
|
|
|
|
|
|
|
|
/* Update position */
|
|
|
|
current_info.player.row += d_r;
|
|
|
|
current_info.player.col += d_c;
|
|
|
|
|
|
|
|
current_info.level.moves++;
|
|
|
|
|
|
|
|
/* Update undo_info.max to current on every normal move,
|
|
|
|
except if it's the same as a redo. */
|
|
|
|
/* normal move */
|
|
|
|
if (!redo &&
|
|
|
|
/* moves have been undone */
|
|
|
|
((undo_info.max != undo_info.current &&
|
|
|
|
/* and the current move is NOT the same as the one in history */
|
|
|
|
undo_info.history[undo_info.current+1] != direction) ||
|
|
|
|
/* or moves have not been undone */
|
|
|
|
undo_info.max == undo_info.current)) {
|
|
|
|
add_undo(direction);
|
|
|
|
undo_info.max = undo_info.current;
|
|
|
|
} else /* redo move or move was same as redo */
|
|
|
|
add_undo(direction); /* (just to update current) */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
#ifdef SOKOBAN_REDO
|
|
|
|
static bool redo(void)
|
|
|
|
{
|
|
|
|
/* If no moves have been undone, quit */
|
|
|
|
if (undo_info.current == undo_info.max)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return move(undo_info.history[(undo_info.current+1 < MAX_UNDOS ?
|
|
|
|
undo_info.current+1 : 0)], true);
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
2007-02-13 06:48:10 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
static void init_boards(void)
|
|
|
|
{
|
|
|
|
current_info.level.level = 0;
|
|
|
|
current_info.level.moves = 0;
|
2007-02-13 06:48:10 +00:00
|
|
|
current_info.level.pushes = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
current_info.level.boxes_to_go = 0;
|
|
|
|
current_info.player.row = 0;
|
|
|
|
current_info.player.col = 0;
|
|
|
|
current_info.max_level = 0;
|
|
|
|
current_info.loaded_level = 0;
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2006-01-19 06:25:59 +00:00
|
|
|
buffered_boards.low = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
init_undo();
|
|
|
|
}
|
|
|
|
|
2006-05-09 05:36:46 +00:00
|
|
|
static int read_levels(int initialize_count)
|
2003-06-29 16:33:04 +00:00
|
|
|
{
|
|
|
|
int fd = 0;
|
2006-01-19 06:25:59 +00:00
|
|
|
int len;
|
2003-06-29 16:33:04 +00:00
|
|
|
int lastlen = 0;
|
2006-01-19 06:25:59 +00:00
|
|
|
int row = 0;
|
|
|
|
int level_count = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
char buffer[COLS + 3]; /* COLS plus CR/LF and \0 */
|
2006-01-19 06:25:59 +00:00
|
|
|
int endpoint = current_info.level.level-1;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2006-01-19 06:25:59 +00:00
|
|
|
if (endpoint < buffered_boards.low)
|
2007-02-13 06:48:10 +00:00
|
|
|
endpoint = current_info.level.level - NUM_BUFFERED_BOARDS;
|
2006-01-19 06:25:59 +00:00
|
|
|
|
|
|
|
if (endpoint < 0) endpoint = 0;
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2006-01-19 06:25:59 +00:00
|
|
|
buffered_boards.low = endpoint;
|
2007-02-13 06:48:10 +00:00
|
|
|
endpoint += NUM_BUFFERED_BOARDS;
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
if ((fd = rb->open(LEVELS_FILE, O_RDONLY)) < 0) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Unable to open %s", LEVELS_FILE);
|
2003-06-29 16:33:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2006-01-19 06:25:59 +00:00
|
|
|
do {
|
|
|
|
len = rb->read_line(fd, buffer, sizeof(buffer));
|
|
|
|
if (len >= 3) {
|
|
|
|
/* This finds lines that are more than 1 or 2 characters
|
|
|
|
* shorter than they should be. Due to the possibility of
|
|
|
|
* a mixed unix and dos CR/LF file format, I'm not going to
|
|
|
|
* do a precise check */
|
|
|
|
if (len < COLS) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Error in levels file: short line");
|
2003-06-29 16:33:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-01-19 06:25:59 +00:00
|
|
|
if (level_count >= buffered_boards.low && level_count < endpoint) {
|
|
|
|
int index = level_count - buffered_boards.low;
|
|
|
|
rb->memcpy(
|
2007-02-13 06:48:10 +00:00
|
|
|
buffered_boards.levels[index].spaces[row], buffer, COLS);
|
2006-01-19 06:25:59 +00:00
|
|
|
}
|
|
|
|
row++;
|
|
|
|
} else if (len) {
|
|
|
|
if (lastlen < 3) {
|
|
|
|
/* Two short lines in a row means new level */
|
|
|
|
level_count++;
|
|
|
|
if (level_count >= endpoint && !initialize_count) break;
|
|
|
|
if (level_count && row != ROWS) {
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Error in levels file: short board");
|
2006-01-19 06:25:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
row = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-19 06:25:59 +00:00
|
|
|
} while ((lastlen=len));
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
rb->close(fd);
|
2006-01-19 06:25:59 +00:00
|
|
|
if (initialize_count) {
|
|
|
|
/* Plus one because there aren't trailing short lines in the file */
|
|
|
|
current_info.max_level = level_count + 1;
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
/* return non-zero on error */
|
2006-01-19 06:25:59 +00:00
|
|
|
static void load_level(void)
|
2003-06-29 16:33:04 +00:00
|
|
|
{
|
2006-01-19 06:25:59 +00:00
|
|
|
int c = 0;
|
|
|
|
int r = 0;
|
|
|
|
int index = current_info.level.level - buffered_boards.low - 1;
|
|
|
|
struct Board *level;
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
if (index < 0 || index >= NUM_BUFFERED_BOARDS) {
|
2006-01-19 06:25:59 +00:00
|
|
|
read_levels(false);
|
2007-02-13 06:48:10 +00:00
|
|
|
index = index < 0 ? NUM_BUFFERED_BOARDS-1 : 0;
|
2006-01-19 06:25:59 +00:00
|
|
|
}
|
|
|
|
level = &buffered_boards.levels[index];
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
current_info.level.boxes_to_go = 0;
|
|
|
|
current_info.level.moves = 0;
|
2007-02-13 06:48:10 +00:00
|
|
|
current_info.level.pushes = 0;
|
2006-01-19 06:25:59 +00:00
|
|
|
current_info.loaded_level = current_info.level.level;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
for (r = 0; r < ROWS; r++) {
|
2006-01-19 06:25:59 +00:00
|
|
|
for (c = 0; c < COLS; c++) {
|
|
|
|
current_info.board[r][c] = level->spaces[r][c];
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
if (current_info.board[r][c] == '.' ||
|
|
|
|
current_info.board[r][c] == '+')
|
2003-06-29 16:33:04 +00:00
|
|
|
current_info.level.boxes_to_go++;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
if (current_info.board[r][c] == '@' ||
|
|
|
|
current_info.board[r][c] == '+') {
|
2003-06-29 16:33:04 +00:00
|
|
|
current_info.player.row = r;
|
|
|
|
current_info.player.col = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-09 05:36:46 +00:00
|
|
|
static void update_screen(void)
|
2003-06-29 16:33:04 +00:00
|
|
|
{
|
2005-03-02 06:50:06 +00:00
|
|
|
int b = 0, c = 0;
|
|
|
|
int rows = 0, cols = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
char s[25];
|
2006-01-19 06:25:59 +00:00
|
|
|
|
2006-06-10 20:39:33 +00:00
|
|
|
/* magnify is the number of pixels for each block */
|
2007-02-13 06:48:10 +00:00
|
|
|
#if (LCD_HEIGHT >= 224) && (LCD_WIDTH >= 312) || \
|
|
|
|
(LCD_HEIGHT >= 249) && (LCD_WIDTH >= 280) /* ipod 5g */
|
|
|
|
#define MAGNIFY 14
|
|
|
|
#elif (LCD_HEIGHT >= 144) && (LCD_WIDTH >= 212) || \
|
|
|
|
(LCD_HEIGHT >= 169) && (LCD_WIDTH >= 180-4) /* h3x0, ipod color/photo */
|
|
|
|
#define MAGNIFY 9
|
|
|
|
#elif (LCD_HEIGHT >= 96) && (LCD_WIDTH >= 152) || \
|
|
|
|
(LCD_HEIGHT >= 121) && (LCD_WIDTH >= 120) /* h1x0, ipod nano/mini */
|
|
|
|
#define MAGNIFY 6
|
2006-06-10 20:39:33 +00:00
|
|
|
#else /* other */
|
2007-02-13 06:48:10 +00:00
|
|
|
#define MAGNIFY 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if LCD_DEPTH < 2
|
|
|
|
int i, j;
|
|
|
|
int max = MAGNIFY - 1;
|
|
|
|
int middle = max / 2;
|
|
|
|
int ldelta = (middle + 1) / 2;
|
2005-02-28 07:12:37 +00:00
|
|
|
#endif
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
/* load the board to the screen */
|
2007-02-13 06:48:10 +00:00
|
|
|
for (rows=0; rows < ROWS; rows++) {
|
|
|
|
for (cols = 0; cols < COLS; cols++) {
|
|
|
|
c = cols * MAGNIFY;
|
|
|
|
b = rows * MAGNIFY;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
switch(current_info.board[rows][cols]) {
|
|
|
|
case 'X': /* black space */
|
|
|
|
break;
|
2005-03-02 06:50:06 +00:00
|
|
|
|
|
|
|
case '#': /* this is a wall */
|
2006-06-10 23:17:56 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_bitmap_part(sokoban_tiles, 0, 1*MAGNIFY, MAGNIFY,
|
|
|
|
c, b, MAGNIFY, MAGNIFY);
|
2005-07-19 16:48:02 +00:00
|
|
|
#else
|
2007-02-13 06:48:10 +00:00
|
|
|
for (i = c; i < c + MAGNIFY; i++)
|
|
|
|
for (j = b; j < b + MAGNIFY; j++)
|
|
|
|
if ((i ^ j) & 1)
|
|
|
|
rb->lcd_drawpixel(i, j);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '$': /* this is a box */
|
|
|
|
#if LCD_DEPTH >= 2
|
|
|
|
rb->lcd_bitmap_part(sokoban_tiles, 0, 2*MAGNIFY, MAGNIFY,
|
|
|
|
c, b, MAGNIFY, MAGNIFY);
|
|
|
|
#else
|
|
|
|
/* Free boxes are not filled in */
|
|
|
|
rb->lcd_drawrect(c, b, MAGNIFY, MAGNIFY);
|
2005-07-19 16:48:02 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
case '*':
|
|
|
|
case '%': /* this is a box on a goal */
|
|
|
|
|
2006-06-10 23:17:56 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_bitmap_part(sokoban_tiles, 0, 3*MAGNIFY, MAGNIFY,
|
|
|
|
c, b, MAGNIFY, MAGNIFY );
|
2006-01-19 06:25:59 +00:00
|
|
|
#else
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_drawrect(c, b, MAGNIFY, MAGNIFY);
|
|
|
|
rb->lcd_drawrect(c+(MAGNIFY/2)-1, b+(MAGNIFY/2)-1, MAGNIFY/2,
|
|
|
|
MAGNIFY/2);
|
2006-01-19 06:25:59 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
case '.': /* this is a goal */
|
2006-06-10 23:17:56 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_bitmap_part(sokoban_tiles, 0, 4*MAGNIFY, MAGNIFY,
|
|
|
|
c, b, MAGNIFY, MAGNIFY);
|
2006-06-10 20:39:33 +00:00
|
|
|
#else
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_drawrect(c+(MAGNIFY/2)-1, b+(MAGNIFY/2)-1, MAGNIFY/2,
|
|
|
|
MAGNIFY/2);
|
2006-06-10 20:39:33 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '@': /* this is you */
|
2006-06-10 23:17:56 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_bitmap_part(sokoban_tiles, 0, 5*MAGNIFY, MAGNIFY,
|
|
|
|
c, b, MAGNIFY, MAGNIFY);
|
2006-06-10 20:39:33 +00:00
|
|
|
#else
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_drawline(c, b+middle, c+max, b+middle);
|
|
|
|
rb->lcd_drawline(c+middle, b, c+middle, b+max-ldelta);
|
|
|
|
rb->lcd_drawline(c+max-middle, b,
|
|
|
|
c+max-middle, b+max-ldelta);
|
|
|
|
rb->lcd_drawline(c+middle, b+max-ldelta,
|
|
|
|
c+middle-ldelta, b+max);
|
|
|
|
rb->lcd_drawline(c+max-middle, b+max-ldelta,
|
|
|
|
c+max-middle+ldelta, b+max);
|
2006-06-10 20:39:33 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
case '+': /* this is you on drugs, erm, on a goal */
|
2006-06-10 23:17:56 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_bitmap_part(sokoban_tiles, 0, 6*MAGNIFY, MAGNIFY,
|
|
|
|
c, b, MAGNIFY, MAGNIFY );
|
2006-01-19 06:25:59 +00:00
|
|
|
#else
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_drawline(c, b+middle, c+max, b+middle);
|
|
|
|
rb->lcd_drawline(c+middle, b, c+middle, b+max-ldelta);
|
|
|
|
rb->lcd_drawline(c+max-middle, b, c+max-middle, b+max-ldelta);
|
|
|
|
rb->lcd_drawline(c+middle, b+max-ldelta, c+middle-ldelta,
|
|
|
|
b+max);
|
|
|
|
rb->lcd_drawline(c+max-middle, b+max-ldelta,
|
|
|
|
c+max-middle+ldelta, b+max);
|
|
|
|
rb->lcd_drawline(c+middle-1, b+middle+1, c+max-middle+1,
|
|
|
|
b+middle+1);
|
2006-01-19 06:25:59 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
2006-06-10 20:39:33 +00:00
|
|
|
|
2006-06-10 23:17:56 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
2006-06-10 20:39:33 +00:00
|
|
|
default:
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_bitmap_part(sokoban_tiles, 0, 0*MAGNIFY, MAGNIFY,
|
|
|
|
c, b, MAGNIFY, MAGNIFY );
|
2006-06-10 20:39:33 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
#if LCD_WIDTH-(COLS*MAGNIFY) < 32
|
|
|
|
#define STAT_SIZE 25
|
|
|
|
#define STAT_POS LCD_HEIGHT-STAT_SIZE
|
|
|
|
#define STAT_CENTER (LCD_WIDTH-120)/2
|
|
|
|
|
|
|
|
rb->lcd_putsxy(4+STAT_CENTER, STAT_POS+4, "Level");
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->snprintf(s, sizeof(s), "%d", current_info.level.level);
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_putsxy(7+STAT_CENTER, STAT_POS+14, s);
|
|
|
|
rb->lcd_putsxy(41+STAT_CENTER, STAT_POS+4, "Moves");
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->snprintf(s, sizeof(s), "%d", current_info.level.moves);
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_putsxy(44+STAT_CENTER, STAT_POS+14, s);
|
|
|
|
rb->lcd_putsxy(79+STAT_CENTER, STAT_POS+4, "Pushes");
|
|
|
|
rb->snprintf(s, sizeof(s), "%d", current_info.level.pushes);
|
|
|
|
rb->lcd_putsxy(82+STAT_CENTER, STAT_POS+14, s);
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_drawrect(STAT_CENTER, STAT_POS, 38, STAT_SIZE);
|
|
|
|
rb->lcd_drawrect(37+STAT_CENTER, STAT_POS, 39, STAT_SIZE);
|
|
|
|
rb->lcd_drawrect(75+STAT_CENTER, STAT_POS, 45, STAT_SIZE);
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define STAT_POS COLS*MAGNIFY
|
|
|
|
#define STAT_SIZE LCD_WIDTH-STAT_POS
|
|
|
|
|
|
|
|
rb->lcd_putsxy(STAT_POS+1, 3, "Level");
|
|
|
|
rb->snprintf(s, sizeof(s), "%d", current_info.level.level);
|
|
|
|
rb->lcd_putsxy(STAT_POS+4, 13, s);
|
|
|
|
rb->lcd_putsxy(STAT_POS+1, 26, "Moves");
|
|
|
|
rb->snprintf(s, sizeof(s), "%d", current_info.level.moves);
|
|
|
|
rb->lcd_putsxy(STAT_POS+4, 36, s);
|
|
|
|
|
|
|
|
rb->lcd_drawrect(STAT_POS, 0, STAT_SIZE, 24);
|
|
|
|
rb->lcd_drawrect(STAT_POS, 23, STAT_SIZE, 24);
|
|
|
|
|
|
|
|
#if LCD_HEIGHT >= 70
|
|
|
|
rb->lcd_putsxy(STAT_POS+1, 49, "Pushes");
|
|
|
|
rb->snprintf(s, sizeof(s), "%d", current_info.level.pushes);
|
|
|
|
rb->lcd_putsxy(STAT_POS+4, 59, s);
|
|
|
|
|
|
|
|
rb->lcd_drawrect(STAT_POS, 46, STAT_SIZE, 24);
|
|
|
|
#endif
|
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
/* print out the screen */
|
|
|
|
rb->lcd_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_level(void)
|
|
|
|
{
|
|
|
|
load_level();
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
update_screen();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sokoban_loop(void)
|
|
|
|
{
|
|
|
|
bool moved = true;
|
2004-10-18 21:45:00 +00:00
|
|
|
int i = 0, button = 0, lastbutton = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
short r = 0, c = 0;
|
2007-02-13 06:48:10 +00:00
|
|
|
int w, h;
|
|
|
|
char s[25];
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
current_info.level.level = 1;
|
|
|
|
|
|
|
|
load_level();
|
2006-05-09 05:36:46 +00:00
|
|
|
update_screen();
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
moved = true;
|
|
|
|
|
|
|
|
r = current_info.player.row;
|
|
|
|
c = current_info.player.col;
|
|
|
|
|
|
|
|
button = rb->button_get(true);
|
|
|
|
|
2006-05-09 05:36:46 +00:00
|
|
|
switch(button)
|
2003-06-29 16:33:04 +00:00
|
|
|
{
|
2006-06-30 16:43:47 +00:00
|
|
|
#ifdef SOKOBAN_RC_QUIT
|
|
|
|
case SOKOBAN_RC_QUIT:
|
|
|
|
#endif
|
2005-12-14 01:31:37 +00:00
|
|
|
case SOKOBAN_QUIT:
|
2003-06-29 16:33:04 +00:00
|
|
|
/* get out of here */
|
2006-05-09 05:36:46 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR /* reset background color */
|
|
|
|
rb->lcd_set_background(rb->global_settings->bg_color);
|
|
|
|
#endif
|
2004-10-18 21:45:00 +00:00
|
|
|
return PLUGIN_OK;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
case SOKOBAN_UNDO:
|
|
|
|
#ifdef SOKOBAN_UNDO_PRE
|
|
|
|
if (lastbutton != SOKOBAN_UNDO_PRE)
|
|
|
|
break;
|
2007-02-13 06:48:10 +00:00
|
|
|
#else /* repeat can't work here for Ondio et al */
|
2004-10-18 21:45:00 +00:00
|
|
|
case SOKOBAN_UNDO | BUTTON_REPEAT:
|
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
undo();
|
|
|
|
rb->lcd_clear_display();
|
2004-10-18 21:45:00 +00:00
|
|
|
update_screen();
|
2003-06-29 16:33:04 +00:00
|
|
|
moved = false;
|
|
|
|
break;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
#ifdef SOKOBAN_REDO
|
|
|
|
case SOKOBAN_REDO:
|
|
|
|
case SOKOBAN_REDO | BUTTON_REPEAT:
|
|
|
|
moved = redo();
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
update_screen();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
case SOKOBAN_LEVEL_UP:
|
|
|
|
case SOKOBAN_LEVEL_UP | BUTTON_REPEAT:
|
2007-02-13 06:48:10 +00:00
|
|
|
/* next level */
|
2003-06-29 16:33:04 +00:00
|
|
|
init_undo();
|
2007-02-13 06:48:10 +00:00
|
|
|
if (current_info.level.level < current_info.max_level)
|
|
|
|
current_info.level.level++;
|
|
|
|
|
|
|
|
draw_level();
|
|
|
|
moved = false;
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
case SOKOBAN_LEVEL_DOWN:
|
|
|
|
case SOKOBAN_LEVEL_DOWN | BUTTON_REPEAT:
|
2003-06-29 16:33:04 +00:00
|
|
|
/* previous level */
|
|
|
|
init_undo();
|
|
|
|
if (current_info.level.level > 1)
|
|
|
|
current_info.level.level--;
|
|
|
|
|
|
|
|
draw_level();
|
|
|
|
moved = false;
|
|
|
|
break;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
#ifdef SOKOBAN_LEVEL_REPEAT
|
2004-10-18 21:45:00 +00:00
|
|
|
case SOKOBAN_LEVEL_REPEAT:
|
|
|
|
case SOKOBAN_LEVEL_REPEAT | BUTTON_REPEAT:
|
2003-06-29 16:33:04 +00:00
|
|
|
/* same level */
|
|
|
|
init_undo();
|
|
|
|
draw_level();
|
|
|
|
moved = false;
|
|
|
|
break;
|
2007-02-13 06:48:10 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
case BUTTON_LEFT:
|
2007-02-13 06:48:10 +00:00
|
|
|
case BUTTON_LEFT | BUTTON_REPEAT:
|
|
|
|
moved = move(SOKOBAN_MOVE_LEFT, false);
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
case BUTTON_RIGHT:
|
|
|
|
case BUTTON_RIGHT | BUTTON_REPEAT:
|
|
|
|
moved = move(SOKOBAN_MOVE_RIGHT, false);
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
2005-12-14 01:31:37 +00:00
|
|
|
case SOKOBAN_UP:
|
2007-02-13 06:48:10 +00:00
|
|
|
case SOKOBAN_UP | BUTTON_REPEAT:
|
|
|
|
moved = move(SOKOBAN_MOVE_UP, false);
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
2005-12-14 01:31:37 +00:00
|
|
|
case SOKOBAN_DOWN:
|
2007-02-13 06:48:10 +00:00
|
|
|
case SOKOBAN_DOWN | BUTTON_REPEAT:
|
|
|
|
moved = move(SOKOBAN_MOVE_DOWN, false);
|
2003-06-29 16:33:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-10-18 21:45:00 +00:00
|
|
|
if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
|
|
|
|
return PLUGIN_USB_CONNECTED;
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
moved = false;
|
|
|
|
break;
|
|
|
|
}
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2004-10-18 21:45:00 +00:00
|
|
|
if (button != BUTTON_NONE)
|
|
|
|
lastbutton = button;
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
if (moved) {
|
|
|
|
rb->lcd_clear_display();
|
2004-10-18 21:45:00 +00:00
|
|
|
update_screen();
|
2003-06-29 16:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We have completed this level */
|
|
|
|
if (current_info.level.boxes_to_go == 0) {
|
2007-02-13 06:48:10 +00:00
|
|
|
|
|
|
|
if (moved) {
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
/* Center level completed message */
|
|
|
|
rb->snprintf(s, sizeof(s), "Level %d Complete!",
|
|
|
|
current_info.level.level);
|
|
|
|
rb->lcd_getstringsize(s, &w, &h);
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - 16 , s);
|
|
|
|
rb->snprintf(s, sizeof(s), "%4d Moves ",
|
|
|
|
current_info.level.moves);
|
|
|
|
rb->lcd_getstringsize(s, &w, &h);
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + 0 , s);
|
|
|
|
rb->snprintf(s, sizeof(s), "%4d Pushes",
|
|
|
|
current_info.level.pushes);
|
|
|
|
rb->lcd_getstringsize(s, &w, &h);
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 + 8 , s);
|
|
|
|
rb->lcd_update();
|
|
|
|
rb->button_get(false);
|
|
|
|
|
|
|
|
rb->sleep(HZ/2);
|
|
|
|
for (i = 0; i < 30; i++) {
|
|
|
|
rb->sleep(HZ/20);
|
|
|
|
button = rb->button_get(false);
|
|
|
|
if (button && ((button & BUTTON_REL) != BUTTON_REL))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
current_info.level.level++;
|
|
|
|
|
|
|
|
/* clear undo stats */
|
|
|
|
init_undo();
|
|
|
|
|
|
|
|
rb->lcd_clear_display();
|
2005-03-02 06:50:06 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
if (current_info.level.level > current_info.max_level) {
|
2006-01-19 06:25:59 +00:00
|
|
|
/* Center "You WIN!!" on all screen sizes */
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->snprintf(s, sizeof(s), "You WIN!!");
|
|
|
|
rb->lcd_getstringsize(s, &w, &h);
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, s);
|
2003-06-29 16:33:04 +00:00
|
|
|
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
|
2007-02-13 06:48:10 +00:00
|
|
|
/* Display for 10 seconds or until keypress */
|
|
|
|
for (i = 0; i < 200; i++) {
|
2006-01-19 06:25:59 +00:00
|
|
|
rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2006-01-19 07:42:57 +00:00
|
|
|
rb->sleep(HZ/20);
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
button = rb->button_get(false);
|
|
|
|
if (button && ((button & BUTTON_REL) != BUTTON_REL))
|
|
|
|
break;
|
|
|
|
}
|
2005-06-24 22:33:21 +00:00
|
|
|
rb->lcd_set_drawmode(DRMODE_SOLID);
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
load_level();
|
|
|
|
update_screen();
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* end while */
|
|
|
|
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
|
|
|
|
{
|
|
|
|
int w, h;
|
2007-02-13 06:48:10 +00:00
|
|
|
int i;
|
|
|
|
int button = 0;
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
(void)(parameter);
|
|
|
|
rb = api;
|
2006-05-09 05:36:46 +00:00
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
|
|
|
|
2006-05-09 05:36:46 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_background(BG_COLOR);
|
|
|
|
#endif
|
|
|
|
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_getstringsize(SOKOBAN_TITLE, &w, &h);
|
|
|
|
rb->lcd_putsxy(LCD_WIDTH/2 - w/2, LCD_HEIGHT/2 - h/2, SOKOBAN_TITLE);
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_update();
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->sleep(HZ);
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
rb->lcd_clear_display();
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
#if (CONFIG_KEYPAD == RECORDER_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == ARCHOS_AV300_PAD)
|
2006-01-19 06:25:59 +00:00
|
|
|
rb->lcd_putsxy(3, 6, "[OFF] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[ON] Undo");
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_putsxy(3, 26, "[PLAY] Redo");
|
|
|
|
rb->lcd_putsxy(3, 36, "[F1] Down a Level");
|
|
|
|
rb->lcd_putsxy(3, 46, "[F2] Restart Level");
|
|
|
|
rb->lcd_putsxy(3, 56, "[F3] Up a Level");
|
2004-10-18 21:45:00 +00:00
|
|
|
#elif CONFIG_KEYPAD == ONDIO_PAD
|
2006-01-19 06:25:59 +00:00
|
|
|
rb->lcd_putsxy(3, 6, "[OFF] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[MODE] Undo");
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_putsxy(3, 26, "[MODE+DOWN] Redo");
|
|
|
|
rb->lcd_putsxy(3, 36, "[MODE+LEFT] Down a Level");
|
|
|
|
rb->lcd_putsxy(3, 46, "[MODE+UP] Restart Level");
|
|
|
|
rb->lcd_putsxy(3, 56, "[MODE+RIGHT] Up Level");
|
2005-07-19 16:48:02 +00:00
|
|
|
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IRIVER_H300_PAD)
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_putsxy(3, 6, "[STOP] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[REC] Undo");
|
|
|
|
rb->lcd_putsxy(3, 26, "[MODE] Redo");
|
|
|
|
rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Down a Level");
|
|
|
|
rb->lcd_putsxy(3, 46, "[PLAY] Restart Level");
|
|
|
|
rb->lcd_putsxy(3, 56, "[PLAY+UP] Up a Level");
|
|
|
|
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IPOD_3G_PAD)
|
|
|
|
rb->lcd_putsxy(3, 6, "[SELECT+MENU] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[SELECT] Undo");
|
|
|
|
rb->lcd_putsxy(3, 26, "[SELECT+PLAY] Redo");
|
|
|
|
rb->lcd_putsxy(3, 36, "[SELECT+LEFT] Down a Level");
|
|
|
|
rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Up a Level");
|
2007-03-16 23:02:39 +00:00
|
|
|
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_putsxy(3, 6, "[POWER] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[SELECT] Undo");
|
2006-01-19 06:25:59 +00:00
|
|
|
rb->lcd_putsxy(3, 26, "[REC] Down a Level");
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->lcd_putsxy(3, 36, "[PLAY] Up Level");
|
|
|
|
#elif CONFIG_KEYPAD == GIGABEAT_PAD
|
|
|
|
rb->lcd_putsxy(3, 6, "[A] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[SELECT] Undo");
|
|
|
|
rb->lcd_putsxy(3, 26, "[POWER] Redo");
|
|
|
|
rb->lcd_putsxy(3, 36, "[MENU+DOWN] Down a Level");
|
|
|
|
rb->lcd_putsxy(3, 46, "[MENU] Restart Level");
|
|
|
|
rb->lcd_putsxy(3, 56, "[MENU+UP] Up Level");
|
|
|
|
#elif CONFIG_KEYPAD == SANSA_E200_PAD
|
|
|
|
rb->lcd_putsxy(3, 6, "[POWER] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[SELECT] Undo");
|
|
|
|
rb->lcd_putsxy(3, 26, "[REC] Redo");
|
|
|
|
rb->lcd_putsxy(3, 36, "[SELECT+DOWN] Down a Level");
|
|
|
|
rb->lcd_putsxy(3, 46, "[SELECT+RIGHT] Restart Level");
|
|
|
|
rb->lcd_putsxy(3, 56, "[SELECT+UP] Up Level");
|
|
|
|
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
|
|
|
|
rb->lcd_putsxy(3, 6, "[POWER] Quit");
|
|
|
|
rb->lcd_putsxy(3, 16, "[REW] Undo");
|
|
|
|
rb->lcd_putsxy(3, 26, "[FF] Redo");
|
|
|
|
rb->lcd_putsxy(3, 36, "[PLAY+DOWN] Down a Level");
|
|
|
|
rb->lcd_putsxy(3, 46, "[PLAY+RIGHT] Restart Level");
|
|
|
|
rb->lcd_putsxy(3, 56, "[PLAY+UP] Up Level");
|
2004-10-18 21:45:00 +00:00
|
|
|
#endif
|
2003-06-29 16:33:04 +00:00
|
|
|
|
|
|
|
rb->lcd_update();
|
2007-02-13 06:48:10 +00:00
|
|
|
rb->button_get(false);
|
|
|
|
/* Display for 3 seconds or until keypress */
|
|
|
|
for (i = 0; i < 60; i++) {
|
|
|
|
rb->sleep(HZ/20);
|
|
|
|
button = rb->button_get(false);
|
|
|
|
if (button && ((button & BUTTON_REL) != BUTTON_REL))
|
|
|
|
break;
|
|
|
|
}
|
2003-06-29 16:33:04 +00:00
|
|
|
rb->lcd_clear_display();
|
|
|
|
|
|
|
|
init_boards();
|
|
|
|
|
2007-02-13 06:48:10 +00:00
|
|
|
if (read_levels(1) != 0)
|
2003-06-29 16:33:04 +00:00
|
|
|
return PLUGIN_OK;
|
|
|
|
|
|
|
|
return sokoban_loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|