2007-06-28 21:13:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Matthias Wientapper
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the implementation of a maze generation algorithm.
|
|
|
|
* The generated mazes are "perfect", i.e. there is one and only
|
|
|
|
* one path from any point in the maze to any other point.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The implemented algorithm is called "Depth-First search", the
|
|
|
|
* solving is done by a dead-end filler routine.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
#include "pluginlib_actions.h"
|
|
|
|
|
|
|
|
PLUGIN_HEADER
|
|
|
|
|
2007-06-29 19:30:35 +00:00
|
|
|
#if (CONFIG_KEYPAD == IPOD_4G_PAD) || \
|
2007-07-27 09:57:27 +00:00
|
|
|
(CONFIG_KEYPAD == IPOD_3G_PAD) || \
|
|
|
|
(CONFIG_KEYPAD == IPOD_1G2G_PAD)
|
2007-06-29 19:30:35 +00:00
|
|
|
# undef __PLUGINLIB_ACTIONS_H__
|
|
|
|
# define MAZE_NEW (BUTTON_SELECT | BUTTON_REPEAT)
|
|
|
|
# define MAZE_NEW_PRE BUTTON_SELECT
|
|
|
|
# define MAZE_QUIT (BUTTON_SELECT | BUTTON_MENU)
|
|
|
|
# define MAZE_SOLVE (BUTTON_SELECT | BUTTON_PLAY)
|
|
|
|
# define MAZE_RIGHT BUTTON_RIGHT
|
|
|
|
# define MAZE_LEFT BUTTON_LEFT
|
|
|
|
# define MAZE_UP BUTTON_MENU
|
|
|
|
# define MAZE_DOWN BUTTON_PLAY
|
|
|
|
# define MAZE_RRIGHT (BUTTON_RIGHT | BUTTON_REPEAT)
|
|
|
|
# define MAZE_RLEFT (BUTTON_LEFT | BUTTON_REPEAT)
|
|
|
|
# define MAZE_RUP (BUTTON_MENU | BUTTON_REPEAT)
|
|
|
|
# define MAZE_RDOWN (BUTTON_PLAY | BUTTON_REPEAT)
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-06-29 19:30:35 +00:00
|
|
|
#else
|
|
|
|
# define MAZE_NEW PLA_START
|
|
|
|
# define MAZE_QUIT PLA_QUIT
|
|
|
|
# define MAZE_SOLVE PLA_FIRE
|
|
|
|
# define MAZE_RIGHT PLA_RIGHT
|
|
|
|
# define MAZE_LEFT PLA_LEFT
|
|
|
|
# define MAZE_UP PLA_UP
|
|
|
|
# define MAZE_DOWN PLA_DOWN
|
|
|
|
# define MAZE_RRIGHT PLA_RIGHT_REPEAT
|
|
|
|
# define MAZE_RLEFT PLA_LEFT_REPEAT
|
|
|
|
# define MAZE_RUP PLA_UP_REPEAT
|
|
|
|
# define MAZE_RDOWN PLA_DOWN_REPEAT
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-06-29 19:30:35 +00:00
|
|
|
#endif
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
/* propertie bits of the cell */
|
|
|
|
#define WALL_N 0x00000001
|
|
|
|
#define WALL_E 0x00000002
|
|
|
|
#define WALL_S 0x00000004
|
|
|
|
#define WALL_W 0x00000008
|
|
|
|
#define WALL_ALL 0x0000000F
|
|
|
|
|
|
|
|
#define BORDER_N 0x00000010
|
|
|
|
#define BORDER_E 0x00000020
|
|
|
|
#define BORDER_S 0x00000040
|
|
|
|
#define BORDER_W 0x00000080
|
|
|
|
#define PATH 0x00000100
|
|
|
|
|
|
|
|
static struct plugin_api* rb;
|
2007-06-29 19:30:35 +00:00
|
|
|
|
|
|
|
#ifdef __PLUGINLIB_ACTIONS_H__
|
2007-06-28 21:13:04 +00:00
|
|
|
const struct button_mapping *plugin_contexts[]
|
|
|
|
= {generic_directions, generic_actions};
|
2007-06-29 19:30:35 +00:00
|
|
|
#endif
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
#if ( LCD_WIDTH == 112 )
|
|
|
|
#define MAZE_WIDTH 16
|
|
|
|
#define MAZE_HEIGHT 12
|
|
|
|
#else
|
|
|
|
#define MAZE_WIDTH 32
|
|
|
|
#define MAZE_HEIGHT 24
|
|
|
|
#endif
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
struct coord_stack{
|
|
|
|
int data[MAZE_WIDTH*MAZE_HEIGHT];
|
|
|
|
int stp;
|
|
|
|
};
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void coord_stack_init(struct coord_stack* stack){
|
|
|
|
stack->stp=0;
|
|
|
|
}
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void coord_stack_push(struct coord_stack* stack, int x, int y){
|
|
|
|
stack->data[stack->stp] = ((x<<8)|y);
|
|
|
|
stack->stp++;
|
|
|
|
}
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void coord_stack_get(struct coord_stack* stack, int index, int* x, int* y){
|
|
|
|
*y = stack->data[index];
|
|
|
|
*y &= 0xff;
|
|
|
|
*x = (stack->data[index])>>8;
|
|
|
|
}
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void coord_stack_pop(struct coord_stack* stack, int* x, int* y){
|
|
|
|
stack->stp--;
|
|
|
|
coord_stack_get(stack, stack->stp, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
int coord_stack_count(struct coord_stack* stack){
|
|
|
|
return(stack->stp);
|
|
|
|
}
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
struct maze{
|
|
|
|
unsigned short maze[MAZE_WIDTH][MAZE_HEIGHT];
|
|
|
|
int solved;
|
|
|
|
int player_x;
|
|
|
|
int player_y;
|
|
|
|
};
|
|
|
|
|
|
|
|
void maze_init(struct maze* maze){
|
|
|
|
int x, y;
|
|
|
|
rb->memset(maze->maze, 0, sizeof(maze->maze));
|
|
|
|
maze->solved = false;
|
|
|
|
maze->player_x = 0;
|
|
|
|
maze->player_y = 0;
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
for(y=0; y<MAZE_HEIGHT; y++){
|
|
|
|
for(x=0; x<MAZE_WIDTH; x++){
|
|
|
|
|
|
|
|
/* all walls are up */
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y] |= WALL_ALL | PATH;
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
/* setup borders */
|
|
|
|
if(x == 0)
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y]|= BORDER_W;
|
2007-06-28 21:13:04 +00:00
|
|
|
if(y == 0)
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y]|= BORDER_N;
|
2007-06-28 21:13:04 +00:00
|
|
|
if(x == MAZE_WIDTH-1)
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y]|= BORDER_E;
|
2007-06-28 21:13:04 +00:00
|
|
|
if(y == MAZE_HEIGHT-1)
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y]|= BORDER_S;
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void maze_draw(struct maze* maze, struct screen* display){
|
2007-06-28 21:13:04 +00:00
|
|
|
int x, y;
|
|
|
|
int wx, wy;
|
2007-07-26 11:25:24 +00:00
|
|
|
int point_width, point_height, point_offset_x, point_offset_y;
|
2007-06-28 21:13:04 +00:00
|
|
|
unsigned short cell;
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
wx = (int) display->width / MAZE_WIDTH;
|
|
|
|
wy = (int) display->height / MAZE_HEIGHT;
|
|
|
|
|
|
|
|
if(wx>3){
|
|
|
|
point_width=wx-3;
|
|
|
|
point_offset_x=2;
|
|
|
|
}else{
|
|
|
|
point_width=1;
|
|
|
|
point_offset_x=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wy>3){
|
|
|
|
point_height=wy-3;
|
|
|
|
point_offset_y=2;
|
|
|
|
}else{
|
|
|
|
point_height=1;
|
|
|
|
point_offset_y=1;
|
|
|
|
}
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
display->clear_display();
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
for(y=0; y<MAZE_HEIGHT; y++){
|
|
|
|
for(x=0; x<MAZE_WIDTH; x++){
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
cell = maze->maze[x][y];
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
/* draw walls */
|
|
|
|
if(cell & WALL_N)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx, y*wy, x*wx+wx, y*wy);
|
2007-06-28 21:13:04 +00:00
|
|
|
if(cell & WALL_E)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx+wx, y*wy, x*wx+wx, y*wy+wy);
|
2007-06-28 21:13:04 +00:00
|
|
|
if(cell & WALL_S)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx, y*wy+wy, x*wx+wx, y*wy+wy);
|
2007-06-28 21:13:04 +00:00
|
|
|
if(cell & WALL_W)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx, y*wy, x*wx, y*wy+wy);
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
if(cell & BORDER_N)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx, y*wy, x*wx+wx, y*wy);
|
2007-06-28 21:13:04 +00:00
|
|
|
if(cell & BORDER_E)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx+wx, y*wy, x*wx+wx, y*wy+wy);
|
2007-06-28 21:13:04 +00:00
|
|
|
if(cell & BORDER_S)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx, y*wy+wy, x*wx+wx, y*wy+wy);
|
2007-06-28 21:13:04 +00:00
|
|
|
if(cell & BORDER_W)
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(x*wx, y*wy, x*wx, y*wy+wy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(maze->solved){
|
2007-07-26 11:59:36 +00:00
|
|
|
#if LCD_DEPTH >= 16
|
|
|
|
if(display->depth>=16)
|
|
|
|
display->set_foreground(LCD_RGBPACK(127,127,127));
|
|
|
|
#endif
|
|
|
|
#if LCD_DEPTH >= 2
|
|
|
|
if(display->depth==2)
|
|
|
|
display->set_foreground(1);
|
|
|
|
#endif
|
2007-07-26 11:25:24 +00:00
|
|
|
for(y=0; y<MAZE_HEIGHT; y++){
|
|
|
|
for(x=0; x<MAZE_WIDTH; x++){
|
|
|
|
cell = maze->maze[x][y];
|
|
|
|
if(cell & PATH)
|
|
|
|
display->fillrect(x*wx+point_offset_x,
|
|
|
|
y*wy+point_offset_y,
|
|
|
|
point_width, point_height);
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-26 11:59:36 +00:00
|
|
|
#if LCD_DEPTH >= 16
|
|
|
|
if(display->depth>=16)
|
|
|
|
display->set_foreground(LCD_RGBPACK(0,0,0));
|
2007-07-26 11:25:24 +00:00
|
|
|
#endif
|
2007-07-26 11:59:36 +00:00
|
|
|
#if LCD_DEPTH >= 2
|
|
|
|
if(display->depth==2)
|
|
|
|
display->set_foreground(0);
|
|
|
|
#endif
|
|
|
|
}
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
/* mark start and end */
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline(0, 0, wx, wy);
|
|
|
|
display->drawline(0, wy, wx, 0);
|
|
|
|
display->drawline((MAZE_WIDTH-1)*wx,(MAZE_HEIGHT-1)*wy,
|
2007-06-28 21:13:04 +00:00
|
|
|
(MAZE_WIDTH-1)*wx+wx, (MAZE_HEIGHT-1)*wy+wy);
|
2007-07-26 11:25:24 +00:00
|
|
|
display->drawline((MAZE_WIDTH-1)*wx,(MAZE_HEIGHT-1)*wy+wy,
|
2007-06-28 21:13:04 +00:00
|
|
|
(MAZE_WIDTH-1)*wx+wx, (MAZE_HEIGHT-1)*wy);
|
|
|
|
|
|
|
|
|
|
|
|
/* draw current position */
|
2007-07-26 11:25:24 +00:00
|
|
|
display->fillrect(maze->player_x*wx+point_offset_x,
|
|
|
|
maze->player_y*wy+point_offset_y,
|
|
|
|
point_width, point_height);
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
display->update();
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
int maze_pick_random_neighbour_cell_with_walls(struct maze* maze,
|
|
|
|
int x, int y, int *pnx, int *pny){
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
int ncount = 0;
|
2007-07-26 11:25:24 +00:00
|
|
|
struct coord_stack neighbours;
|
|
|
|
unsigned short current_cell=maze->maze[x][y];
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
coord_stack_init(&neighbours);
|
2007-06-28 21:13:04 +00:00
|
|
|
/* look for neighbour cells with walls */
|
|
|
|
|
|
|
|
/* north */
|
2007-07-26 11:25:24 +00:00
|
|
|
if(!(current_cell & BORDER_N)){
|
|
|
|
if((maze->maze[x][y-1] & WALL_ALL) == WALL_ALL){
|
|
|
|
coord_stack_push(&neighbours, x, y-1);
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* west */
|
2007-07-26 11:25:24 +00:00
|
|
|
if(!(current_cell & BORDER_W)){
|
|
|
|
if((maze->maze[x-1][y] & WALL_ALL) == WALL_ALL){
|
|
|
|
coord_stack_push(&neighbours, x-1, y);
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* east */
|
2007-07-26 11:25:24 +00:00
|
|
|
if(!(current_cell & BORDER_E)){
|
|
|
|
if((maze->maze[x+1][y] & WALL_ALL) == WALL_ALL){
|
|
|
|
coord_stack_push(&neighbours, x+1, y);
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* south */
|
2007-07-26 11:25:24 +00:00
|
|
|
if(!(current_cell & BORDER_S)){
|
|
|
|
if((maze->maze[x][y+1] & WALL_ALL) == WALL_ALL){
|
|
|
|
coord_stack_push(&neighbours, x, y+1);
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* randomly select neighbour cell with walls */
|
2007-07-26 11:25:24 +00:00
|
|
|
ncount=coord_stack_count(&neighbours);
|
|
|
|
if(ncount!=0)
|
|
|
|
coord_stack_get(&neighbours, rb->rand()%ncount, pnx, pny);
|
2007-06-28 21:13:04 +00:00
|
|
|
return ncount;
|
|
|
|
}
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
/* Removes the wall between the cell (x,y) and the cell (nx,ny) */
|
|
|
|
void maze_remove_wall(struct maze* maze, int x, int y, int nx, int ny){
|
2007-06-28 21:13:04 +00:00
|
|
|
/* where is our neighbour? */
|
|
|
|
|
|
|
|
/* north or south */
|
2007-07-26 11:25:24 +00:00
|
|
|
if(x==nx){
|
|
|
|
if(y<ny){
|
2007-06-28 21:13:04 +00:00
|
|
|
/*south*/
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y] &= ~WALL_S;
|
|
|
|
maze->maze[nx][ny] &= ~WALL_N;
|
2007-06-28 21:13:04 +00:00
|
|
|
} else {
|
|
|
|
/*north*/
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y] &= ~WALL_N;
|
|
|
|
maze->maze[nx][ny] &= ~WALL_S;
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* east or west */
|
2007-07-26 11:25:24 +00:00
|
|
|
if(y==ny){
|
|
|
|
if(x<nx){
|
2007-06-28 21:13:04 +00:00
|
|
|
/* east */
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y] &= ~WALL_E;
|
|
|
|
maze->maze[nx][ny] &= ~WALL_W;
|
2007-06-28 21:13:04 +00:00
|
|
|
} else {
|
|
|
|
/*west*/
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y] &= ~WALL_W;
|
|
|
|
maze->maze[nx][ny] &= ~WALL_E;
|
2007-06-28 21:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void maze_generate(struct maze* maze){
|
2007-06-28 21:13:04 +00:00
|
|
|
int total_cells = MAZE_WIDTH * MAZE_HEIGHT;
|
|
|
|
int visited_cells;
|
2007-07-26 11:25:24 +00:00
|
|
|
int available_neighbours;
|
2007-06-28 21:13:04 +00:00
|
|
|
int x, y;
|
|
|
|
int nx = 0;
|
|
|
|
int ny = 0;
|
2007-07-26 11:25:24 +00:00
|
|
|
struct coord_stack done_cells;
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
coord_stack_init(&done_cells);
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
x = rb->rand()%MAZE_WIDTH;
|
|
|
|
y = rb->rand()%MAZE_HEIGHT;
|
|
|
|
|
|
|
|
visited_cells = 1;
|
|
|
|
while (visited_cells < total_cells){
|
2007-07-26 11:25:24 +00:00
|
|
|
available_neighbours =
|
|
|
|
maze_pick_random_neighbour_cell_with_walls(maze, x, y, &nx, &ny);
|
|
|
|
if(available_neighbours == 0){
|
2007-06-28 21:13:04 +00:00
|
|
|
/* pop from stack */
|
2007-07-26 11:25:24 +00:00
|
|
|
coord_stack_pop(&done_cells, &x, &y);
|
2007-06-28 21:13:04 +00:00
|
|
|
} else {
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_remove_wall(maze, x, y, nx, ny);
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
/* save position on the stack */
|
2007-07-26 11:25:24 +00:00
|
|
|
coord_stack_push(&done_cells, x, y);
|
|
|
|
|
2007-06-28 21:13:04 +00:00
|
|
|
/* current cell = neighbour cell */
|
|
|
|
x=nx;
|
|
|
|
y=ny;
|
|
|
|
|
|
|
|
visited_cells++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void maze_solve(struct maze* maze){
|
2007-06-28 21:13:04 +00:00
|
|
|
int x, y;
|
|
|
|
unsigned short cell;
|
|
|
|
unsigned short w;
|
|
|
|
int dead_ends = 1;
|
2007-07-26 11:25:24 +00:00
|
|
|
unsigned short solved_maze[MAZE_WIDTH][MAZE_HEIGHT];
|
2007-06-28 21:13:04 +00:00
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->solved = ~(maze->solved);
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
/* copy maze for solving */
|
2007-07-26 11:25:24 +00:00
|
|
|
rb->memcpy(solved_maze, maze->maze, (MAZE_HEIGHT*MAZE_WIDTH*sizeof(maze->maze[0][0])));
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* remove some borders and walls on start and end point */
|
|
|
|
solved_maze[0][0] &= ~(WALL_N|BORDER_N);
|
|
|
|
solved_maze[MAZE_WIDTH-1][MAZE_HEIGHT-1] &= ~(WALL_S|BORDER_S);
|
|
|
|
|
|
|
|
while(dead_ends){
|
|
|
|
dead_ends = 0;
|
|
|
|
/* scan for dead ends */
|
|
|
|
for(y=0; y<MAZE_HEIGHT; y++){
|
|
|
|
rb->yield();
|
|
|
|
for(x=0; x<MAZE_WIDTH; x++){
|
|
|
|
cell = solved_maze[x][y];
|
|
|
|
w = ~cell & 0x000f;
|
|
|
|
if(w == WALL_N ||
|
|
|
|
w == WALL_E ||
|
|
|
|
w == WALL_S ||
|
|
|
|
w == WALL_W){
|
|
|
|
/* found dead end, clear path bit and fill it up */
|
2007-07-26 11:25:24 +00:00
|
|
|
maze->maze[x][y] &= ~PATH;
|
2007-06-28 21:13:04 +00:00
|
|
|
solved_maze[x][y] |= WALL_ALL;
|
|
|
|
/* don't forget the neighbours */
|
|
|
|
if(!(cell & BORDER_N))
|
|
|
|
solved_maze[x][y-1]|=WALL_S;
|
|
|
|
if(!(cell & BORDER_E))
|
|
|
|
solved_maze[x+1][y]|=WALL_W;
|
|
|
|
if(!(cell & BORDER_S))
|
|
|
|
solved_maze[x][y+1]|=WALL_N;
|
|
|
|
if(!(cell & BORDER_W))
|
|
|
|
solved_maze[x-1][y]|=WALL_E;
|
|
|
|
dead_ends++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-26 11:25:24 +00:00
|
|
|
void maze_move_player_down(struct maze* maze){
|
|
|
|
unsigned short cell=maze->maze[maze->player_x][maze->player_y];
|
|
|
|
if( !(cell & WALL_S) &&
|
|
|
|
!(cell & BORDER_S)){
|
|
|
|
maze->player_y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void maze_move_player_up(struct maze* maze){
|
|
|
|
unsigned short cell=maze->maze[maze->player_x][maze->player_y];
|
|
|
|
if( !(cell & WALL_N) &&
|
|
|
|
!(cell & BORDER_N)){
|
|
|
|
maze->player_y--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void maze_move_player_left(struct maze* maze){
|
|
|
|
unsigned short cell=maze->maze[maze->player_x][maze->player_y];
|
|
|
|
if( !(cell & WALL_W) &&
|
|
|
|
!(cell & BORDER_W)){
|
|
|
|
maze->player_x--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void maze_move_player_right(struct maze* maze){
|
|
|
|
unsigned short cell=maze->maze[maze->player_x][maze->player_y];
|
|
|
|
if( !(cell & WALL_E) &&
|
|
|
|
!(cell & BORDER_E)){
|
|
|
|
maze->player_x++;
|
|
|
|
}
|
|
|
|
}
|
2007-06-28 21:13:04 +00:00
|
|
|
/**********************************/
|
|
|
|
/* this is the plugin entry point */
|
|
|
|
/**********************************/
|
2007-07-26 11:25:24 +00:00
|
|
|
enum plugin_status plugin_start(struct plugin_api* api, void* parameter){
|
2007-06-29 19:30:35 +00:00
|
|
|
int button, lastbutton = BUTTON_NONE;
|
2007-06-28 21:13:04 +00:00
|
|
|
int quit = 0;
|
2007-07-26 11:25:24 +00:00
|
|
|
int i;
|
|
|
|
struct maze maze;
|
2007-06-28 21:13:04 +00:00
|
|
|
(void)parameter;
|
|
|
|
rb = api;
|
|
|
|
|
|
|
|
rb->backlight_set_timeout(1);
|
2007-07-26 11:59:36 +00:00
|
|
|
|
2007-06-28 21:13:04 +00:00
|
|
|
#if LCD_DEPTH > 1
|
|
|
|
rb->lcd_set_backdrop(NULL);
|
2007-07-26 11:59:36 +00:00
|
|
|
rb->lcd_set_background(LCD_DEFAULT_BG);
|
|
|
|
#if LCD_DEPTH >= 16
|
|
|
|
rb->lcd_set_foreground( LCD_RGBPACK( 0, 0, 0));
|
|
|
|
#elif LCD_DEPTH == 2
|
|
|
|
rb->lcd_set_foreground(0);
|
|
|
|
#endif
|
2007-06-28 21:13:04 +00:00
|
|
|
#endif
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_init(&maze);
|
|
|
|
maze_generate(&maze);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
maze_draw(&maze, rb->screens[i]);
|
2007-06-28 21:13:04 +00:00
|
|
|
|
|
|
|
while(!quit) {
|
2007-06-29 19:30:35 +00:00
|
|
|
#ifdef __PLUGINLIB_ACTIONS_H__
|
2007-06-28 21:13:04 +00:00
|
|
|
button = pluginlib_getaction(rb, TIMEOUT_BLOCK, plugin_contexts, 2);
|
2007-06-29 19:30:35 +00:00
|
|
|
#else
|
|
|
|
button = rb->button_get(true);
|
|
|
|
#endif
|
2007-06-28 21:13:04 +00:00
|
|
|
switch(button) {
|
|
|
|
case MAZE_NEW:
|
2007-06-29 19:30:35 +00:00
|
|
|
#ifdef MAZE_NEW_PRE
|
|
|
|
if(lastbutton != MAZE_NEW_PRE)
|
|
|
|
break;
|
|
|
|
#endif
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_init(&maze);
|
|
|
|
maze_generate(&maze);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
maze_draw(&maze, rb->screens[i]);
|
2007-06-28 21:13:04 +00:00
|
|
|
break;
|
|
|
|
case MAZE_SOLVE:
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_solve(&maze);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
maze_draw(&maze, rb->screens[i]);
|
2007-06-28 21:13:04 +00:00
|
|
|
break;
|
|
|
|
case MAZE_RIGHT:
|
|
|
|
case MAZE_RRIGHT:
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_move_player_right(&maze);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
maze_draw(&maze, rb->screens[i]);
|
2007-06-28 21:13:04 +00:00
|
|
|
break;
|
|
|
|
case MAZE_LEFT:
|
|
|
|
case MAZE_RLEFT:
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_move_player_left(&maze);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
maze_draw(&maze, rb->screens[i]);
|
2007-06-28 21:13:04 +00:00
|
|
|
break;
|
|
|
|
case MAZE_UP:
|
|
|
|
case MAZE_RUP:
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_move_player_up(&maze);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
maze_draw(&maze, rb->screens[i]);
|
2007-06-28 21:13:04 +00:00
|
|
|
break;
|
|
|
|
case MAZE_DOWN:
|
|
|
|
case MAZE_RDOWN:
|
2007-07-26 11:25:24 +00:00
|
|
|
maze_move_player_down(&maze);
|
|
|
|
FOR_NB_SCREENS(i)
|
|
|
|
maze_draw(&maze, rb->screens[i]);
|
2007-06-28 21:13:04 +00:00
|
|
|
break;
|
|
|
|
case MAZE_QUIT:
|
|
|
|
/* quit plugin */
|
|
|
|
quit=true;
|
|
|
|
return PLUGIN_OK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (rb->default_event_handler(button) == SYS_USB_CONNECTED) {
|
|
|
|
return PLUGIN_USB_CONNECTED;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-06-29 19:30:35 +00:00
|
|
|
if( button != BUTTON_NONE )
|
|
|
|
lastbutton = button;
|
2007-06-28 21:13:04 +00:00
|
|
|
rb->yield();
|
|
|
|
}
|
|
|
|
rb->backlight_set_timeout(rb->global_settings->backlight_timeout);
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|