2014-06-29 18:49:07 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Franklin Wei
|
|
|
|
*
|
|
|
|
* Clone of 2048 by Gabriele Cirulli
|
|
|
|
*
|
|
|
|
* Thanks to [Saint], saratoga, and gevaerts for answering all my n00b
|
|
|
|
* questions :)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/* TODO
|
|
|
|
* Sounds!
|
|
|
|
* Better animations!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* includes */
|
|
|
|
|
|
|
|
#include <plugin.h>
|
|
|
|
|
|
|
|
#include "lib/display_text.h"
|
|
|
|
|
|
|
|
#include "lib/helper.h"
|
|
|
|
#include "lib/highscore.h"
|
|
|
|
#include "lib/playback_control.h"
|
|
|
|
#include "lib/pluginlib_actions.h"
|
|
|
|
#include "lib/pluginlib_exit.h"
|
|
|
|
|
|
|
|
#include "pluginbitmaps/_2048_background.h"
|
|
|
|
#include "pluginbitmaps/_2048_tiles.h"
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* some constants */
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
static const int ANIM_SLEEPTIME = (HZ/20);
|
|
|
|
static const int NUM_STARTING_TILES = 2;
|
|
|
|
static const int VERT_SPACING = 4;
|
|
|
|
static const int WHAT_FONT = FONT_UI;
|
|
|
|
static const unsigned int WINNING_TILE = 2048;
|
|
|
|
|
|
|
|
/* must use macros for these */
|
2014-06-29 18:49:07 +00:00
|
|
|
#define GRID_SIZE 4
|
|
|
|
#define HISCORES_FILE PLUGIN_GAMES_DATA_DIR "/2048.score"
|
2015-04-01 22:15:29 +00:00
|
|
|
#define MIN_SPACE (BMPHEIGHT__2048_tiles * 0.134)
|
2014-06-29 18:49:07 +00:00
|
|
|
#define NUM_SCORES 5
|
|
|
|
#define RESUME_FILE PLUGIN_GAMES_DATA_DIR "/2048.save"
|
2015-04-01 22:15:29 +00:00
|
|
|
#define SPACES (GRID_SIZE * GRID_SIZE)
|
2014-06-29 18:49:07 +00:00
|
|
|
|
|
|
|
/* screen-specific configuration */
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
#if (LCD_WIDTH < LCD_HEIGHT) /* tall screens */
|
|
|
|
# define TITLE_X 0
|
|
|
|
# define TITLE_Y 0
|
|
|
|
# define BASE_Y (BMPHEIGHT__2048_tiles*1.5)
|
|
|
|
# define BASE_X (BMPHEIGHT__2048_tiles*.5-MIN_SPACE)
|
|
|
|
# define SCORE_X 0
|
|
|
|
# define SCORE_Y (max_numeral_height)
|
|
|
|
# define BEST_SCORE_X 0
|
|
|
|
# define BEST_SCORE_Y (2*max_numeral_height)
|
|
|
|
#else /* wide or square screens */
|
|
|
|
# define TITLE_X 0
|
|
|
|
# define TITLE_Y 0
|
|
|
|
# define BASE_X (LCD_WIDTH-(GRID_SIZE*BMPHEIGHT__2048_tiles)-(((GRID_SIZE+1)*MIN_SPACE)))
|
|
|
|
# define BASE_Y (BMPHEIGHT__2048_tiles*.5-MIN_SPACE)
|
|
|
|
# define SCORE_X 0
|
|
|
|
# define SCORE_Y (max_numeral_height)
|
|
|
|
# define BEST_SCORE_X 0
|
|
|
|
# define BEST_SCORE_Y (2*max_numeral_height)
|
|
|
|
#endif /* LCD_WIDTH < LCD_HEIGHT */
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* where to draw the background bitmap */
|
|
|
|
static const int BACKGROUND_X = (BASE_X-MIN_SPACE);
|
|
|
|
static const int BACKGROUND_Y = (BASE_Y-MIN_SPACE);
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* key mappings */
|
2014-06-29 18:49:07 +00:00
|
|
|
#define KEY_UP PLA_UP
|
|
|
|
#define KEY_DOWN PLA_DOWN
|
|
|
|
#define KEY_LEFT PLA_LEFT
|
|
|
|
#define KEY_RIGHT PLA_RIGHT
|
|
|
|
#define KEY_EXIT PLA_CANCEL
|
|
|
|
#define KEY_UNDO PLA_SELECT
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* notice how "color" is spelled :P */
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* colors */
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
static const unsigned BACKGROUND = LCD_RGBPACK(0xfa, 0xf8, 0xef);
|
|
|
|
static const unsigned BOARD_BACKGROUND = LCD_RGBPACK(0xbb, 0xad, 0xa0);
|
|
|
|
static const unsigned TEXT_COLOR = LCD_RGBPACK(0x77, 0x6e, 0x65);
|
|
|
|
|
|
|
|
#endif
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* PLA data */
|
2014-06-29 18:49:07 +00:00
|
|
|
static const struct button_mapping *plugin_contexts[] = { pla_main_ctx };
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/*** game data structures ***/
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
struct game_ctx_t {
|
2015-04-01 22:15:29 +00:00
|
|
|
unsigned int grid[GRID_SIZE][GRID_SIZE]; /* 0 = empty */
|
|
|
|
unsigned int score;
|
|
|
|
unsigned int cksum; /* sum of grid, XORed by score */
|
|
|
|
bool already_won; /* has the player gotten 2048 yet? */
|
|
|
|
} game_ctx;
|
|
|
|
|
|
|
|
static struct game_ctx_t *ctx = &game_ctx;
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/*** temporary data ***/
|
2014-06-29 18:49:07 +00:00
|
|
|
|
|
|
|
static bool merged_grid[GRID_SIZE][GRID_SIZE];
|
|
|
|
static int old_grid[GRID_SIZE][GRID_SIZE];
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
static int max_numeral_height = -1;
|
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
#if LCD_DEPTH <= 1
|
|
|
|
static int max_numeral_width;
|
|
|
|
#endif
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
static bool loaded = false; /* has a save been loaded? */
|
|
|
|
|
|
|
|
/* the high score */
|
|
|
|
static unsigned int best_score;
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
static bool abnormal_exit = true;
|
2014-06-29 18:49:07 +00:00
|
|
|
static struct highscore highscores[NUM_SCORES];
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/***************************** UTILITY FUNCTIONS *****************************/
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
static inline int rand_range(int min, int max)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
return rb->rand() % (max-min + 1) + min;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* prepares for exit */
|
2014-06-29 18:49:07 +00:00
|
|
|
static void cleanup(void)
|
|
|
|
{
|
|
|
|
backlight_use_settings();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns 2 or 4 */
|
|
|
|
static inline int rand_2_or_4(void)
|
|
|
|
{
|
|
|
|
/* 1 in 10 chance of a four */
|
2015-04-01 22:15:29 +00:00
|
|
|
if(rb->rand() % 10 == 0)
|
2014-06-29 18:49:07 +00:00
|
|
|
return 4;
|
|
|
|
else
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* displays the help text */
|
2014-06-29 18:49:07 +00:00
|
|
|
static bool do_help(void)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_foreground(LCD_WHITE);
|
|
|
|
rb->lcd_set_background(LCD_BLACK);
|
|
|
|
#endif
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_setfont(FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
static char* help_text[]= {"2048", "", "Aim",
|
|
|
|
"", "Join", "the", "numbers", "to", "get", "to", "the", "2048", "tile!", "", "",
|
|
|
|
"How", "to", "Play", "",
|
|
|
|
"", "Use", "the", "directional", "keys", "to", "move", "the", "tiles.", "When",
|
|
|
|
"two", "tiles", "with", "the", "same", "number", "touch,", "they", "merge", "into", "one!"};
|
|
|
|
|
|
|
|
struct style_text style[] = {
|
|
|
|
{0, TEXT_CENTER | TEXT_UNDERLINE},
|
|
|
|
{2, C_RED},
|
|
|
|
{15, C_RED},
|
|
|
|
{16, C_RED},
|
|
|
|
{17, C_RED},
|
2014-06-29 18:49:07 +00:00
|
|
|
LAST_STYLE_ITEM
|
|
|
|
};
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
return display_text(ARRAYLEN(help_text), help_text, style, NULL, true);
|
|
|
|
}
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/*** tile movement logic ***/
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* this function performs the tile movement */
|
2014-06-29 18:49:07 +00:00
|
|
|
static inline void slide_internal(int startx, int starty,
|
|
|
|
int stopx, int stopy,
|
|
|
|
int dx, int dy,
|
2014-08-19 19:20:19 +00:00
|
|
|
int lookx, int looky,
|
|
|
|
bool update_best)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
unsigned int best_score_old = best_score;
|
|
|
|
|
|
|
|
/* loop over the rows or columns, moving the tiles in the specified direction */
|
|
|
|
for(int y = starty; y != stopy; y += dy)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int x = startx; x != stopx; x += dx)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
if(ctx->grid[x + lookx][y + looky] == ctx->grid[x][y] &&
|
|
|
|
ctx->grid[x][y] &&
|
|
|
|
!merged_grid[x + lookx][y + looky] &&
|
|
|
|
!merged_grid[x][y]) /* merge these two tiles */
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
/* Each merged tile cannot be merged again */
|
2015-04-01 22:15:29 +00:00
|
|
|
merged_grid[x + lookx][y + looky] = true;
|
|
|
|
ctx->grid[x + lookx][y + looky] = 2 * ctx->grid[x][y];
|
|
|
|
ctx->score += ctx->grid[x + lookx][y + looky];
|
|
|
|
ctx->grid[x][y] = 0;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
else if(ctx->grid[x + lookx][y + looky] == 0) /* Empty! */
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
ctx->grid[x + lookx][y + looky] = ctx->grid[x][y];
|
|
|
|
ctx->grid[x][y] = 0;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
if(ctx->score > best_score_old && update_best)
|
|
|
|
best_score = ctx->score;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* these functions move each tile 1 space in the direction specified via calls to slide_internal */
|
|
|
|
|
|
|
|
/* Up
|
|
|
|
0
|
|
|
|
1 ^ ^ ^ ^
|
|
|
|
2 ^ ^ ^ ^
|
|
|
|
3 ^ ^ ^ ^
|
|
|
|
0 1 2 3
|
|
|
|
*/
|
2014-08-19 19:20:19 +00:00
|
|
|
static void up(bool update_best)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
slide_internal(0, 1, /* start values */
|
|
|
|
GRID_SIZE, GRID_SIZE, /* stop values */
|
|
|
|
1, 1, /* delta values */
|
2014-08-19 19:20:19 +00:00
|
|
|
0, -1, /* lookahead values */
|
|
|
|
update_best);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* Down
|
|
|
|
0 v v v v
|
|
|
|
1 v v v v
|
|
|
|
2 v v v v
|
|
|
|
3
|
|
|
|
0 1 2 3
|
|
|
|
*/
|
2014-08-19 19:20:19 +00:00
|
|
|
static void down(bool update_best)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
slide_internal(0, GRID_SIZE-2,
|
|
|
|
GRID_SIZE, -1,
|
|
|
|
1, -1,
|
2014-08-19 19:20:19 +00:00
|
|
|
0, 1,
|
|
|
|
update_best);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* Left
|
|
|
|
0 < < <
|
|
|
|
1 < < <
|
|
|
|
2 < < <
|
|
|
|
3 < < <
|
|
|
|
0 1 2 3
|
|
|
|
*/
|
2014-08-19 19:20:19 +00:00
|
|
|
static void left(bool update_best)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
slide_internal(1, 0,
|
|
|
|
GRID_SIZE, GRID_SIZE,
|
|
|
|
1, 1,
|
2014-08-19 19:20:19 +00:00
|
|
|
-1, 0,
|
|
|
|
update_best);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* Right
|
|
|
|
0 > > >
|
|
|
|
1 > > >
|
|
|
|
2 > > >
|
|
|
|
3 > > >
|
|
|
|
0 1 2 3
|
|
|
|
*/
|
2014-08-19 19:20:19 +00:00
|
|
|
static void right(bool update_best)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
slide_internal(GRID_SIZE-2, 0, /* start */
|
|
|
|
-1, GRID_SIZE, /* stop */
|
|
|
|
-1, 1, /* delta */
|
2014-08-19 19:20:19 +00:00
|
|
|
1, 0, /* lookahead */
|
|
|
|
update_best);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* copies old_grid to ctx->grid */
|
|
|
|
static inline void RESTORE_GRID(void)
|
|
|
|
{
|
|
|
|
memcpy(&ctx->grid, &old_grid, sizeof(ctx->grid));
|
|
|
|
}
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
/* slightly modified base 2 logarithm, returns 1 when given zero, and log2(n) + 1 for anything else */
|
2014-06-29 18:49:07 +00:00
|
|
|
static inline int ilog2(int n)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
if(n == 0)
|
2014-06-29 18:49:07 +00:00
|
|
|
return 1;
|
2015-04-01 22:15:29 +00:00
|
|
|
int log = 0;
|
|
|
|
while(n > 1)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
n >>= 1;
|
2014-06-29 18:49:07 +00:00
|
|
|
++log;
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
return log + 1;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
/* low-depth displays resort to text drawing, see the #else case below */
|
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
#if LCD_DEPTH > 1
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
/* draws game screen + updates LCD */
|
2014-06-29 18:49:07 +00:00
|
|
|
static void draw(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_background(BACKGROUND);
|
|
|
|
#endif
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_clear_display();
|
|
|
|
|
|
|
|
/* draw the background */
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->lcd_bitmap(_2048_background,
|
|
|
|
BACKGROUND_X, BACKGROUND_Y,
|
|
|
|
BMPWIDTH__2048_background, BMPWIDTH__2048_background);
|
2014-06-29 18:49:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
grey_gray_bitmap(_2048_background, BACKGROUND_X, BACKGROUND_Y, BMPWIDTH__2048_background, BMPHEIGHT__2048_background);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* draw the grid */
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int y = 0; y < GRID_SIZE; ++y)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->lcd_bitmap_part(_2048_tiles, /* source */
|
|
|
|
BMPWIDTH__2048_tiles - BMPHEIGHT__2048_tiles * ilog2(ctx->grid[x][y]), 0, /* source upper left corner */
|
|
|
|
STRIDE(SCREEN_MAIN, BMPWIDTH__2048_tiles, BMPHEIGHT__2048_tiles), /* stride */
|
|
|
|
(BMPHEIGHT__2048_tiles + MIN_SPACE) * x + BASE_X, (BMPHEIGHT__2048_tiles + MIN_SPACE) * y + BASE_Y, /* dest upper-left corner */
|
|
|
|
BMPHEIGHT__2048_tiles, BMPHEIGHT__2048_tiles); /* size of the cut section */
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* draw the title */
|
|
|
|
char buf[32];
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_foreground(TEXT_COLOR);
|
|
|
|
#endif
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "%d", WINNING_TILE);
|
|
|
|
|
|
|
|
/* check if the title will overlap the grid */
|
2014-06-29 18:49:07 +00:00
|
|
|
int w, h;
|
|
|
|
rb->lcd_setfont(FONT_UI);
|
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
bool draw_title = true;
|
|
|
|
if(w + TITLE_X >= BACKGROUND_X && h + TITLE_Y >= BACKGROUND_Y)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
/* if it goes into the grid, use the system font, which should be smaller */
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
2014-08-19 19:20:19 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_SYSFIXED);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + TITLE_X >= BACKGROUND_X && h + TITLE_Y >= BACKGROUND_Y)
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
|
|
|
/* title can't fit, don't draw it */
|
2015-04-01 22:15:29 +00:00
|
|
|
draw_title = false;
|
|
|
|
h = 0;
|
2014-08-19 19:20:19 +00:00
|
|
|
}
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
if(draw_title)
|
|
|
|
rb->lcd_putsxy(TITLE_X, TITLE_Y, buf);
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
int score_y = TITLE_Y + h + VERT_SPACING;
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* draw the score */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "Score: %d", ctx->score);
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_foreground(LCD_WHITE);
|
|
|
|
rb->lcd_set_background(BOARD_BACKGROUND);
|
|
|
|
#endif
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_setfont(FONT_UI);
|
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
/* try making the score fit */
|
|
|
|
if(w + SCORE_X >= BACKGROUND_X && h + SCORE_Y >= BACKGROUND_Y)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
/* score overflows */
|
|
|
|
/* first see if it fits with Score: and FONT_SYSFIXED */
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_SYSFIXED);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
/* it fits, go and draw it */
|
|
|
|
goto draw_lbl;
|
|
|
|
|
|
|
|
/* now try with S: and FONT_UI */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "S: %d", ctx->score);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_UI);
|
|
|
|
rb->lcd_setfont(FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
goto draw_lbl;
|
|
|
|
|
|
|
|
/* now try with S: and FONT_SYSFIXED */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "S: %d", ctx->score);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_SYSFIXED);
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
goto draw_lbl;
|
|
|
|
|
|
|
|
/* then try without Score: and FONT_UI */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "%d", ctx->score);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_UI);
|
|
|
|
rb->lcd_setfont(FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
goto draw_lbl;
|
|
|
|
|
|
|
|
/* as a last resort, don't use Score: and use the system font */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "%d", ctx->score);
|
2014-08-19 19:20:19 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_SYSFIXED);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + SCORE_X < BACKGROUND_X)
|
2014-08-19 19:20:19 +00:00
|
|
|
goto draw_lbl;
|
|
|
|
else
|
|
|
|
goto skip_draw_score;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
draw_lbl:
|
2014-08-19 19:20:19 +00:00
|
|
|
rb->lcd_putsxy(SCORE_X, score_y, buf);
|
2015-04-01 22:15:29 +00:00
|
|
|
score_y += h + VERT_SPACING;
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* draw the best score */
|
2014-08-19 19:20:19 +00:00
|
|
|
skip_draw_score:
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "Best: %d", best_score);
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
#ifdef HAVE_LCD_COLOR
|
|
|
|
rb->lcd_set_foreground(LCD_WHITE);
|
|
|
|
rb->lcd_set_background(BOARD_BACKGROUND);
|
|
|
|
#endif
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_setfont(FONT_UI);
|
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + BEST_SCORE_X >= BACKGROUND_X && h + BEST_SCORE_Y >= BACKGROUND_Y)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
/* score overflows */
|
|
|
|
/* first see if it fits with Score: and FONT_SYSFIXED */
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_SYSFIXED);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + BEST_SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
/* it fits, go and draw it */
|
|
|
|
goto draw_best;
|
|
|
|
|
|
|
|
/* now try with S: and FONT_UI */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "B: %d", best_score);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_UI);
|
|
|
|
rb->lcd_setfont(FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + BEST_SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
goto draw_best;
|
|
|
|
|
|
|
|
/* now try with S: and FONT_SYSFIXED */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "B: %d", best_score);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_SYSFIXED);
|
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + BEST_SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
goto draw_best;
|
|
|
|
|
|
|
|
/* then try without Score: and FONT_UI */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "%d", best_score);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_UI);
|
|
|
|
rb->lcd_setfont(FONT_UI);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + BEST_SCORE_X < BACKGROUND_X)
|
2014-06-29 18:49:07 +00:00
|
|
|
goto draw_best;
|
|
|
|
|
|
|
|
/* as a last resort, don't use Score: and use the system font */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, sizeof(buf), "%d", best_score);
|
2014-08-19 19:20:19 +00:00
|
|
|
rb->font_getstringsize(buf, &w, &h, FONT_SYSFIXED);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_setfont(FONT_SYSFIXED);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(w + BEST_SCORE_X < BACKGROUND_X)
|
2014-08-19 19:20:19 +00:00
|
|
|
goto draw_best;
|
|
|
|
else
|
|
|
|
goto skip_draw_best;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
draw_best:
|
2014-08-19 19:20:19 +00:00
|
|
|
rb->lcd_putsxy(BEST_SCORE_X, score_y, buf);
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
skip_draw_best:
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_update();
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
/* revert the font */
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_setfont(WHAT_FONT);
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
#else /* LCD_DEPTH > 1 */
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
/* 1-bit display :( */
|
2015-04-01 22:15:29 +00:00
|
|
|
/* bitmaps are unreadable on these screens, so just resort to text-based drawing */
|
2014-08-19 19:20:19 +00:00
|
|
|
static void draw(void)
|
|
|
|
{
|
|
|
|
rb->lcd_clear_display();
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
/* Draw the grid */
|
|
|
|
/* find the biggest tile */
|
2015-04-01 22:15:29 +00:00
|
|
|
unsigned int biggest_tile = 0;
|
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int y = 0; y < GRID_SIZE; ++y)
|
|
|
|
if(ctx->grid[x][y] > biggest_tile)
|
|
|
|
biggest_tile = ctx->grid[x][y];
|
2014-08-19 19:20:19 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
rb->snprintf(buf, 32, "%d", biggest_tile);
|
|
|
|
|
|
|
|
int biggest_tile_width = rb->strlen(buf) * rb->font_get_width(rb->font_get(WHAT_FONT), '0') + MIN_SPACE;
|
|
|
|
|
|
|
|
for(int y = 0; y < GRID_SIZE; ++y)
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
|
|
|
if(ctx->grid[x][y])
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
if(ctx->grid[x][y] > biggest_tile)
|
|
|
|
biggest_tile = ctx->grid[x][y];
|
|
|
|
rb->snprintf(buf, 32, "%d", ctx->grid[x][y]);
|
|
|
|
rb->lcd_putsxy(biggest_tile_width * x, y * max_numeral_height + max_numeral_height, buf);
|
2014-08-19 19:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
/* Now draw the score, and the game title */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->snprintf(buf, 32, "Score: %d", ctx->score);
|
|
|
|
int buf_width, buf_height;
|
|
|
|
rb->font_getstringsize(buf, &buf_width, &buf_height, WHAT_FONT);
|
|
|
|
|
|
|
|
int score_leftmost = LCD_WIDTH - buf_width - 1;
|
2014-08-19 19:20:19 +00:00
|
|
|
/* Check if there is enough space to display "Score: ", otherwise, only display the score */
|
2015-04-01 22:15:29 +00:00
|
|
|
if(score_leftmost >= 0)
|
|
|
|
rb->lcd_putsxy(score_leftmost, 0, buf);
|
2014-08-19 19:20:19 +00:00
|
|
|
else
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->lcd_putsxy(score_leftmost, 0, buf + rb->strlen("Score: "));
|
|
|
|
|
|
|
|
rb->snprintf(buf, 32, "%d", WINNING_TILE);
|
|
|
|
rb->font_getstringsize(buf, &buf_width, &buf_height, WHAT_FONT);
|
|
|
|
if(buf_width < score_leftmost)
|
|
|
|
rb->lcd_putsxy(0, 0, buf);
|
2014-06-29 18:49:07 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
rb->lcd_update();
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
#endif /* LCD_DEPTH > 1 */
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* place a 2 or 4 in a random empty space */
|
|
|
|
static void place_random(void)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
int xpos[SPACES], ypos[SPACES];
|
|
|
|
int back = 0;
|
2014-06-29 18:49:07 +00:00
|
|
|
/* get the indexes of empty spaces */
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int y = 0; y < GRID_SIZE; ++y)
|
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
if(!ctx->grid[x][y])
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
xpos[back] = x;
|
|
|
|
ypos[back++] = y;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
if(!back)
|
|
|
|
/* no empty spaces */
|
|
|
|
return;
|
|
|
|
|
2015-04-01 22:15:29 +00:00
|
|
|
int idx = rand_range(0, back - 1);
|
|
|
|
ctx->grid[ xpos[idx] ][ ypos[idx] ] = rand_2_or_4();
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* checks for a win or loss */
|
|
|
|
static bool check_gameover(void)
|
|
|
|
{
|
2014-08-19 19:20:19 +00:00
|
|
|
/* first, check for a loss */
|
2015-04-01 22:15:29 +00:00
|
|
|
int oldscore = ctx->score;
|
|
|
|
bool have_legal_move = false;
|
|
|
|
|
|
|
|
memset(&merged_grid, 0, SPACES * sizeof(bool));
|
2014-08-19 19:20:19 +00:00
|
|
|
up(false);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(&old_grid, &ctx->grid, sizeof(ctx->grid)))
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
RESTORE_GRID();
|
|
|
|
ctx->score = oldscore;
|
|
|
|
have_legal_move = true;
|
2014-08-19 19:20:19 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
RESTORE_GRID();
|
|
|
|
|
|
|
|
memset(&merged_grid, 0, SPACES * sizeof(bool));
|
2014-08-19 19:20:19 +00:00
|
|
|
down(false);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(&old_grid, &ctx->grid, sizeof(ctx->grid)))
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
RESTORE_GRID();
|
|
|
|
ctx->score = oldscore;
|
|
|
|
have_legal_move = true;
|
2014-08-19 19:20:19 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
RESTORE_GRID();
|
|
|
|
|
|
|
|
memset(&merged_grid, 0, SPACES * sizeof(bool));
|
2014-08-19 19:20:19 +00:00
|
|
|
left(false);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(&old_grid, &ctx->grid, sizeof(ctx->grid)))
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
RESTORE_GRID();
|
|
|
|
ctx->score = oldscore;
|
|
|
|
have_legal_move = true;
|
2014-08-19 19:20:19 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
RESTORE_GRID();
|
|
|
|
|
|
|
|
memset(&merged_grid, 0, SPACES * sizeof(bool));
|
2014-08-19 19:20:19 +00:00
|
|
|
right(false);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(&old_grid, &ctx->grid, sizeof(ctx->grid)))
|
2014-08-19 19:20:19 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
RESTORE_GRID();
|
|
|
|
ctx->score = oldscore;
|
|
|
|
have_legal_move = true;
|
2014-08-19 19:20:19 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
ctx->score = oldscore;
|
2014-08-19 19:20:19 +00:00
|
|
|
if(!have_legal_move)
|
|
|
|
{
|
|
|
|
/* no more legal moves */
|
2015-04-01 22:15:29 +00:00
|
|
|
draw(); /* Shame the player */
|
2014-08-19 19:20:19 +00:00
|
|
|
rb->splash(HZ*2, "Game Over!");
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
for(int y = 0;y < GRID_SIZE; ++y)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
if(ctx->grid[x][y] == WINNING_TILE && !ctx->already_won)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
/* Let the user see the tile in its full glory... */
|
|
|
|
draw();
|
2015-04-01 22:15:29 +00:00
|
|
|
ctx->already_won = true;
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->splash(HZ*2,"You win!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loads highscores from disk */
|
|
|
|
/* creates an empty structure if the file does not exist */
|
|
|
|
static void load_hs(void)
|
|
|
|
{
|
|
|
|
if(rb->file_exists(HISCORES_FILE))
|
|
|
|
highscore_load(HISCORES_FILE, highscores, NUM_SCORES);
|
|
|
|
else
|
2015-04-01 22:15:29 +00:00
|
|
|
memset(highscores, 0, sizeof(struct highscore) * NUM_SCORES);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the data structures */
|
|
|
|
static void init_game(bool newgame)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
best_score = highscores[0].score;
|
2014-08-19 19:20:19 +00:00
|
|
|
if(loaded && ctx->score > best_score)
|
2015-04-01 22:15:29 +00:00
|
|
|
best_score = ctx->score;
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
if(newgame)
|
|
|
|
{
|
|
|
|
/* initialize the game context */
|
2015-04-01 22:15:29 +00:00
|
|
|
memset(ctx->grid, 0, sizeof(ctx->grid));
|
|
|
|
for(int i = 0; i < NUM_STARTING_TILES; ++i)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
place_random();
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
ctx->score = 0;
|
|
|
|
ctx->already_won = false;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* using the menu resets the font */
|
|
|
|
/* set it again here */
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_setfont(WHAT_FONT);
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* Now calculate font sizes */
|
|
|
|
/* Now get the height of the font */
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->font_getstringsize("0123456789", NULL, &max_numeral_height, WHAT_FONT);
|
|
|
|
max_numeral_height += VERT_SPACING;
|
|
|
|
|
2014-08-19 19:20:19 +00:00
|
|
|
#if LCD_DEPTH <= 1
|
2015-04-01 22:15:29 +00:00
|
|
|
max_numeral_width = rb->font_get_width(rb->font_get(WHAT_FONT), '0');
|
2014-08-19 19:20:19 +00:00
|
|
|
#endif
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
backlight_ignore_timeout();
|
2022-11-24 04:00:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the current game state */
|
|
|
|
static void save_game(void)
|
|
|
|
{
|
|
|
|
rb->splash(0, "Saving...");
|
2015-04-01 22:15:29 +00:00
|
|
|
int fd = rb->open(RESUME_FILE, O_WRONLY|O_CREAT, 0666);
|
|
|
|
if(fd < 0)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
/* calculate checksum */
|
|
|
|
ctx->cksum = 0;
|
|
|
|
|
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
|
|
|
for(int y = 0; y < GRID_SIZE; ++y)
|
|
|
|
ctx->cksum += ctx->grid[x][y];
|
|
|
|
|
|
|
|
ctx->cksum ^= ctx->score;
|
|
|
|
|
|
|
|
rb->write(fd, ctx, sizeof(struct game_ctx_t));
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->close(fd);
|
|
|
|
rb->lcd_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loads a saved game, returns true on success */
|
|
|
|
static bool load_game(void)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
int success = 0;
|
|
|
|
int fd = rb->open(RESUME_FILE, O_RDONLY);
|
|
|
|
if(fd < 0)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
rb->remove(RESUME_FILE);
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
int numread = rb->read(fd, ctx, sizeof(struct game_ctx_t));
|
|
|
|
|
|
|
|
/* verify checksum */
|
|
|
|
unsigned int calc = 0;
|
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
|
|
|
for(int y = 0; y < GRID_SIZE; ++y)
|
|
|
|
calc += ctx->grid[x][y];
|
|
|
|
|
|
|
|
calc ^= ctx->score;
|
|
|
|
|
|
|
|
if(numread == sizeof(struct game_ctx_t) && calc == ctx->cksum)
|
2014-06-29 18:49:07 +00:00
|
|
|
++success;
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->close(fd);
|
|
|
|
rb->remove(RESUME_FILE);
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
return (success > 0);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update the highscores with ctx->score */
|
|
|
|
static void hs_check_update(bool noshow)
|
|
|
|
{
|
|
|
|
/* first, find the biggest tile to show as the level */
|
2015-04-01 22:15:29 +00:00
|
|
|
unsigned int biggest = 0;
|
|
|
|
for(int x = 0; x < GRID_SIZE; ++x)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int y = 0; y < GRID_SIZE; ++y)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
if(ctx->grid[x][y] > biggest)
|
|
|
|
biggest = ctx->grid[x][y];
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
int hs_idx = highscore_update(ctx->score,biggest, "", highscores,NUM_SCORES);
|
2014-06-29 18:49:07 +00:00
|
|
|
if(!noshow)
|
|
|
|
{
|
|
|
|
/* show the scores if there is a new high score */
|
2015-04-01 22:15:29 +00:00
|
|
|
if(hs_idx >= 0)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
rb->splashf(HZ*2, "New High Score: %d", ctx->score);
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->lcd_clear_display();
|
2015-04-01 22:15:29 +00:00
|
|
|
highscore_show(hs_idx, highscores, NUM_SCORES, true);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
highscore_save(HISCORES_FILE, highscores, NUM_SCORES);
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* asks the user if they wish to quit */
|
|
|
|
static bool confirm_quit(void)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
const struct text_message prompt = { (const char*[]) {"Are you sure?", "This will clear your current game."}, 2};
|
|
|
|
enum yesno_res response = rb->gui_syncyesno_run(&prompt, NULL, NULL);
|
|
|
|
if(response == YESNO_NO)
|
2014-06-29 18:49:07 +00:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* show the pause menu */
|
|
|
|
static int do_2048_pause_menu(void)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
int sel = 0;
|
2014-06-29 18:49:07 +00:00
|
|
|
MENUITEM_STRINGLIST(menu,"2048 Menu", NULL,
|
|
|
|
"Resume Game",
|
|
|
|
"Start New Game",
|
|
|
|
"High Scores",
|
|
|
|
"Playback Control",
|
|
|
|
"Help",
|
|
|
|
"Quit without Saving",
|
|
|
|
"Quit");
|
2021-07-24 17:28:28 +00:00
|
|
|
while(1)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
switch(rb->do_menu(&menu, &sel, NULL, false))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
draw();
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
if(!confirm_quit())
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hs_check_update(false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 2:
|
2015-04-01 22:15:29 +00:00
|
|
|
highscore_show(-1, highscores, NUM_SCORES, true);
|
2014-06-29 18:49:07 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
playback_control(NULL);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
do_help();
|
|
|
|
break;
|
|
|
|
case 5: /* quit w/o saving */
|
|
|
|
{
|
|
|
|
if(!confirm_quit())
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 6:
|
|
|
|
return 3;
|
2021-07-24 17:28:28 +00:00
|
|
|
default:
|
|
|
|
break;
|
2014-06-29 18:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exit_handler(void)
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
if(abnormal_exit)
|
|
|
|
save_game();
|
2014-08-18 01:56:08 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(false); /* back to idle */
|
|
|
|
#endif
|
2014-06-29 18:49:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
static bool check_hs;
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
/* main game loop */
|
|
|
|
static enum plugin_status do_game(bool newgame)
|
|
|
|
{
|
|
|
|
init_game(newgame);
|
2015-04-01 22:15:29 +00:00
|
|
|
rb_atexit(exit_handler);
|
|
|
|
int made_move = 0;
|
2014-06-29 18:49:07 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(false); /* Save battery when idling */
|
|
|
|
#endif
|
|
|
|
/* Wait for a button press */
|
2015-04-01 22:15:29 +00:00
|
|
|
int button = pluginlib_getaction(-1, plugin_contexts, ARRAYLEN(plugin_contexts));
|
|
|
|
made_move = 0;
|
|
|
|
|
|
|
|
memset(&merged_grid, 0, SPACES*sizeof(bool));
|
2014-06-29 18:49:07 +00:00
|
|
|
memcpy(&old_grid, &ctx->grid, sizeof(int)*SPACES);
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
unsigned int grid_before_anim_step[GRID_SIZE][GRID_SIZE];
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(true); /* doing work now... */
|
|
|
|
#endif
|
|
|
|
switch(button)
|
|
|
|
{
|
|
|
|
case KEY_UP:
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int i = 0; i < GRID_SIZE - 1; ++i)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
memcpy(grid_before_anim_step, ctx->grid, sizeof(ctx->grid));
|
2014-08-19 19:20:19 +00:00
|
|
|
up(true);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(grid_before_anim_step, ctx->grid, sizeof(ctx->grid)))
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
rb->sleep(ANIM_SLEEPTIME);
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
made_move = 1;
|
2014-06-29 18:49:07 +00:00
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int i = 0; i < GRID_SIZE - 1; ++i)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
memcpy(grid_before_anim_step, ctx->grid, sizeof(ctx->grid));
|
2014-08-19 19:20:19 +00:00
|
|
|
down(true);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(grid_before_anim_step, ctx->grid, sizeof(ctx->grid)))
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
rb->sleep(ANIM_SLEEPTIME);
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
made_move = 1;
|
2014-06-29 18:49:07 +00:00
|
|
|
break;
|
|
|
|
case KEY_LEFT:
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int i = 0; i < GRID_SIZE - 1; ++i)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
memcpy(grid_before_anim_step, ctx->grid, sizeof(ctx->grid));
|
2014-08-19 19:20:19 +00:00
|
|
|
left(true);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(grid_before_anim_step, ctx->grid, sizeof(ctx->grid)))
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
rb->sleep(ANIM_SLEEPTIME);
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
made_move = 1;
|
2014-06-29 18:49:07 +00:00
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
2015-04-01 22:15:29 +00:00
|
|
|
for(int i = 0; i < GRID_SIZE - 1; ++i)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
memcpy(grid_before_anim_step, ctx->grid, sizeof(ctx->grid));
|
2014-08-19 19:20:19 +00:00
|
|
|
right(true);
|
2015-04-01 22:15:29 +00:00
|
|
|
if(memcmp(grid_before_anim_step, ctx->grid, sizeof(ctx->grid)))
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
rb->sleep(ANIM_SLEEPTIME);
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
made_move = 1;
|
2014-06-29 18:49:07 +00:00
|
|
|
break;
|
|
|
|
case KEY_EXIT:
|
|
|
|
switch(do_2048_pause_menu())
|
|
|
|
{
|
|
|
|
case 0: /* resume */
|
|
|
|
break;
|
|
|
|
case 1: /* new game */
|
|
|
|
init_game(true);
|
2015-04-01 22:15:29 +00:00
|
|
|
made_move = 1;
|
2014-06-29 18:49:07 +00:00
|
|
|
continue;
|
|
|
|
case 2: /* quit without saving */
|
2015-04-01 22:15:29 +00:00
|
|
|
check_hs = true;
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->remove(RESUME_FILE);
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
case 3: /* save and quit */
|
2015-04-01 22:15:29 +00:00
|
|
|
check_hs = false;
|
2014-06-29 18:49:07 +00:00
|
|
|
save_game();
|
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
exit_on_usb(button); /* handle poweroff and USB events */
|
2014-06-29 18:49:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
if(made_move)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
/* Check if any tiles moved, then add random */
|
|
|
|
if(memcmp(&old_grid, ctx->grid, sizeof(ctx->grid)))
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
place_random();
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
memcpy(&old_grid, ctx->grid, sizeof(ctx->grid));
|
2014-06-29 18:49:07 +00:00
|
|
|
if(check_gameover())
|
|
|
|
return PLUGIN_OK;
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
|
|
|
rb->cpu_boost(false); /* back to idle */
|
|
|
|
#endif
|
|
|
|
rb->yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decide if this_item should be shown in the main menu */
|
|
|
|
/* used to hide resume option when there is no save */
|
2020-07-19 17:42:04 +00:00
|
|
|
static int mainmenu_cb(int action,
|
|
|
|
const struct menu_item_ex *this_item,
|
|
|
|
struct gui_synclist *this_list)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2020-07-19 17:42:04 +00:00
|
|
|
(void)this_list;
|
2015-04-01 22:15:29 +00:00
|
|
|
int idx = ((intptr_t)this_item);
|
|
|
|
if(action == ACTION_REQUEST_MENUITEM && !loaded && (idx == 0 || idx == 5))
|
2014-06-29 18:49:07 +00:00
|
|
|
return ACTION_EXIT_MENUITEM;
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* show the main menu */
|
|
|
|
static enum plugin_status do_2048_menu(void)
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
int sel = 0;
|
|
|
|
loaded = load_game();
|
|
|
|
MENUITEM_STRINGLIST(menu,
|
|
|
|
"2048 Menu",
|
|
|
|
mainmenu_cb,
|
|
|
|
"Resume Game",
|
|
|
|
"Start New Game",
|
|
|
|
"High Scores",
|
|
|
|
"Playback Control",
|
|
|
|
"Help",
|
|
|
|
"Quit without Saving",
|
|
|
|
"Quit");
|
2021-07-24 17:28:28 +00:00
|
|
|
while(true)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
switch(rb->do_menu(&menu, &sel, NULL, false))
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
case 0: /* Start new game or resume a game */
|
|
|
|
case 1:
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
if(sel == 1 && loaded)
|
2014-06-29 18:49:07 +00:00
|
|
|
{
|
|
|
|
if(!confirm_quit())
|
|
|
|
break;
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
enum plugin_status ret = do_game(sel == 1);
|
2014-06-29 18:49:07 +00:00
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case PLUGIN_OK:
|
|
|
|
{
|
2015-04-01 22:15:29 +00:00
|
|
|
loaded = false;
|
2014-06-29 18:49:07 +00:00
|
|
|
rb->remove(RESUME_FILE);
|
|
|
|
hs_check_update(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PLUGIN_USB_CONNECTED:
|
|
|
|
save_game();
|
|
|
|
/* Don't bother showing the high scores... */
|
|
|
|
return ret;
|
|
|
|
case PLUGIN_ERROR: /* exit without menu */
|
|
|
|
if(check_hs)
|
|
|
|
hs_check_update(false);
|
|
|
|
return PLUGIN_OK;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
2015-04-01 22:15:29 +00:00
|
|
|
highscore_show(-1, highscores, NUM_SCORES, true);
|
2014-06-29 18:49:07 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
playback_control(NULL);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
do_help();
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if(confirm_quit())
|
|
|
|
return PLUGIN_OK;
|
2021-07-24 17:28:28 +00:00
|
|
|
break;
|
2014-06-29 18:49:07 +00:00
|
|
|
case 6:
|
|
|
|
if(loaded)
|
|
|
|
save_game();
|
|
|
|
return PLUGIN_OK;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
/* plugin entry point */
|
2014-06-29 18:49:07 +00:00
|
|
|
enum plugin_status plugin_start(const void* param)
|
|
|
|
{
|
|
|
|
(void)param;
|
|
|
|
rb->srand(*rb->current_tick);
|
|
|
|
load_hs();
|
|
|
|
rb->lcd_setfont(WHAT_FONT);
|
|
|
|
|
|
|
|
/* now start the game menu */
|
2015-04-01 22:15:29 +00:00
|
|
|
enum plugin_status ret = do_2048_menu();
|
|
|
|
|
|
|
|
highscore_save(HISCORES_FILE, highscores, NUM_SCORES);
|
2014-06-29 18:49:07 +00:00
|
|
|
cleanup();
|
2015-04-01 22:15:29 +00:00
|
|
|
|
|
|
|
abnormal_exit = false;
|
|
|
|
|
2014-06-29 18:49:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|