summaryrefslogtreecommitdiff
path: root/apps/plugins/puzzles/src/midend.c
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2018-12-21 22:13:13 -0500
committerFranklin Wei <git@fwei.tk>2018-12-21 22:13:33 -0500
commitb3356e3aff34a4ab94778e7f6a8db43f9135296c (patch)
tree9119f850f2138db65da93461505b0a9b08e56d32 /apps/plugins/puzzles/src/midend.c
parentf08d218e676f9ee8b5c26e755088671baf3a70b7 (diff)
downloadrockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.zip
rockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.tar.gz
rockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.tar.bz2
rockbox-b3356e3aff34a4ab94778e7f6a8db43f9135296c.tar.xz
puzzles: resync with upstream
This brings the code to upstream commit 3ece3d6 (I've made my own Rockbox- specific changes on top of that). Changes include using C99 `bool' throughout, and minor logic fixes for some puzzles. Change-Id: Ie823e73ae49a8ee1de411d6d406df2ba835af541
Diffstat (limited to 'apps/plugins/puzzles/src/midend.c')
-rw-r--r--apps/plugins/puzzles/src/midend.c149
1 files changed, 76 insertions, 73 deletions
diff --git a/apps/plugins/puzzles/src/midend.c b/apps/plugins/puzzles/src/midend.c
index a42e516..036c856 100644
--- a/apps/plugins/puzzles/src/midend.c
+++ b/apps/plugins/puzzles/src/midend.c
@@ -69,7 +69,7 @@ struct midend {
struct midend_state_entry *states;
struct midend_serialise_buf newgame_undo, newgame_redo;
- int newgame_can_store_undo;
+ bool newgame_can_store_undo;
game_params *params, *curparams;
game_drawstate *drawstate;
@@ -80,7 +80,7 @@ struct midend {
float flash_time, flash_pos;
int dir;
- int timing;
+ bool timing;
float elapsed;
char *laststatus;
@@ -122,7 +122,7 @@ struct deserialise_data {
* Forward reference.
*/
static const char *midend_deserialise_internal(
- midend *me, int (*read)(void *ctx, void *buf, int len), void *rctx,
+ midend *me, bool (*read)(void *ctx, void *buf, int len), void *rctx,
const char *(*check)(void *ctx, midend *, const struct deserialise_data *),
void *cctx);
@@ -167,7 +167,7 @@ midend *midend_new(frontend *fe, const game *ourgame,
me->newgame_undo.size = me->newgame_undo.len = 0;
me->newgame_redo.buf = NULL;
me->newgame_redo.size = me->newgame_redo.len = 0;
- me->newgame_can_store_undo = FALSE;
+ me->newgame_can_store_undo = false;
me->params = ourgame->default_params();
me->game_id_change_notify_function = NULL;
me->game_id_change_notify_ctx = NULL;
@@ -202,7 +202,7 @@ midend *midend_new(frontend *fe, const game *ourgame,
me->ui = NULL;
me->pressed_mouse_button = 0;
me->laststatus = NULL;
- me->timing = FALSE;
+ me->timing = false;
me->elapsed = 0.0F;
me->tilesize = me->winwidth = me->winheight = 0;
if (drapi)
@@ -297,7 +297,7 @@ static void midend_size_new_drawstate(midend *me)
}
}
-void midend_size(midend *me, int *x, int *y, int user_size)
+void midend_size(midend *me, int *x, int *y, bool user_size)
{
int min, max;
int rx, ry;
@@ -315,7 +315,7 @@ void midend_size(midend *me, int *x, int *y, int user_size)
/*
* Find the tile size that best fits within the given space. If
- * `user_size' is TRUE, we must actually find the _largest_ such
+ * `user_size' is true, we must actually find the _largest_ such
* tile size, in order to get as close to the user's explicit
* request as possible; otherwise, we bound above at the game's
* preferred tile size, so that the game gets what it wants
@@ -542,15 +542,15 @@ void midend_new_game(midend *me)
if (me->game_id_change_notify_function)
me->game_id_change_notify_function(me->game_id_change_notify_ctx);
- me->newgame_can_store_undo = TRUE;
+ me->newgame_can_store_undo = true;
}
-int midend_can_undo(midend *me)
+bool midend_can_undo(midend *me)
{
return (me->statepos > 1 || me->newgame_undo.len);
}
-int midend_can_redo(midend *me)
+bool midend_can_redo(midend *me)
{
return (me->statepos < me->nstates || me->newgame_redo.len);
}
@@ -560,18 +560,20 @@ struct newgame_undo_deserialise_read_ctx {
int len, pos;
};
-static int newgame_undo_deserialise_read(void *ctx, void *buf, int len)
+static bool newgame_undo_deserialise_read(void *ctx, void *buf, int len)
{
struct newgame_undo_deserialise_read_ctx *const rctx = ctx;
- int use = min(len, rctx->len - rctx->pos);
- memcpy(buf, rctx->ser->buf + rctx->pos, use);
- rctx->pos += use;
- return use;
+ if (len > rctx->len - rctx->pos)
+ return false;
+
+ memcpy(buf, rctx->ser->buf + rctx->pos, len);
+ rctx->pos += len;
+ return true;
}
struct newgame_undo_deserialise_check_ctx {
- int refused;
+ bool refused;
};
static const char *newgame_undo_deserialise_check(
@@ -604,19 +606,19 @@ static const char *newgame_undo_deserialise_check(
* We check both params and cparams, to be as safe as possible.
*/
- old = me->ourgame->encode_params(me->params, TRUE);
- new = me->ourgame->encode_params(data->params, TRUE);
+ old = me->ourgame->encode_params(me->params, true);
+ new = me->ourgame->encode_params(data->params, true);
if (strcmp(old, new)) {
/* Set a flag to distinguish this deserialise failure
* from one due to faulty decoding */
- ctx->refused = TRUE;
+ ctx->refused = true;
return "Undoing this new-game operation would change params";
}
- old = me->ourgame->encode_params(me->curparams, TRUE);
- new = me->ourgame->encode_params(data->cparams, TRUE);
+ old = me->ourgame->encode_params(me->curparams, true);
+ new = me->ourgame->encode_params(data->cparams, true);
if (strcmp(old, new)) {
- ctx->refused = TRUE;
+ ctx->refused = true;
return "Undoing this new-game operation would change params";
}
@@ -626,7 +628,7 @@ static const char *newgame_undo_deserialise_check(
return NULL;
}
-static int midend_undo(midend *me)
+static bool midend_undo(midend *me)
{
const char *deserialise_error;
@@ -637,7 +639,7 @@ static int midend_undo(midend *me)
me->states[me->statepos-2].state);
me->statepos--;
me->dir = -1;
- return 1;
+ return true;
} else if (me->newgame_undo.len) {
struct newgame_undo_deserialise_read_ctx rctx;
struct newgame_undo_deserialise_check_ctx cctx;
@@ -655,7 +657,7 @@ static int midend_undo(midend *me)
rctx.ser = &me->newgame_undo;
rctx.len = me->newgame_undo.len; /* copy for reentrancy safety */
rctx.pos = 0;
- cctx.refused = FALSE;
+ cctx.refused = false;
deserialise_error = midend_deserialise_internal(
me, newgame_undo_deserialise_read, &rctx,
newgame_undo_deserialise_check, &cctx);
@@ -667,7 +669,7 @@ static int midend_undo(midend *me)
* function, which we ignore.)
*/
sfree(serbuf.buf);
- return 0;
+ return false;
} else {
/*
* There should never be any _other_ deserialisation
@@ -693,13 +695,13 @@ static int midend_undo(midend *me)
newgame_serialise_write(&me->newgame_redo, serbuf.buf, serbuf.len);
sfree(serbuf.buf);
- return 1;
+ return true;
}
} else
- return 0;
+ return false;
}
-static int midend_redo(midend *me)
+static bool midend_redo(midend *me)
{
const char *deserialise_error;
@@ -710,7 +712,7 @@ static int midend_redo(midend *me)
me->states[me->statepos].state);
me->statepos++;
me->dir = +1;
- return 1;
+ return true;
} else if (me->newgame_redo.len) {
struct newgame_undo_deserialise_read_ctx rctx;
struct newgame_undo_deserialise_check_ctx cctx;
@@ -728,7 +730,7 @@ static int midend_redo(midend *me)
rctx.ser = &me->newgame_redo;
rctx.len = me->newgame_redo.len; /* copy for reentrancy safety */
rctx.pos = 0;
- cctx.refused = FALSE;
+ cctx.refused = false;
deserialise_error = midend_deserialise_internal(
me, newgame_undo_deserialise_read, &rctx,
newgame_undo_deserialise_check, &cctx);
@@ -740,7 +742,7 @@ static int midend_redo(midend *me)
* function, which we ignore.)
*/
sfree(serbuf.buf);
- return 0;
+ return false;
} else {
/*
* There should never be any _other_ deserialisation
@@ -766,10 +768,10 @@ static int midend_redo(midend *me)
newgame_serialise_write(&me->newgame_undo, serbuf.buf, serbuf.len);
sfree(serbuf.buf);
- return 1;
+ return true;
}
} else
- return 0;
+ return false;
}
static void midend_finish_move(midend *me)
@@ -850,11 +852,12 @@ void midend_restart_game(midend *me)
midend_set_timer(me);
}
-static int midend_really_process_key(midend *me, int x, int y, int button)
+static bool midend_really_process_key(midend *me, int x, int y, int button)
{
game_state *oldstate =
me->ourgame->dup_game(me->states[me->statepos - 1].state);
- int type = MOVE, gottype = FALSE, ret = 1;
+ int type = MOVE;
+ bool gottype = false, ret = true;
float anim_time;
game_state *s;
char *movestr = NULL;
@@ -876,7 +879,7 @@ static int midend_really_process_key(midend *me, int x, int y, int button)
button == UI_UNDO) {
midend_stop_anim(me);
type = me->states[me->statepos-1].movetype;
- gottype = TRUE;
+ gottype = true;
if (!midend_undo(me))
goto done;
} else if (button == 'r' || button == 'R' ||
@@ -891,7 +894,7 @@ static int midend_really_process_key(midend *me, int x, int y, int button)
goto done;
} else if (button == 'q' || button == 'Q' || button == '\x11' ||
button == UI_QUIT) {
- ret = 0;
+ ret = false;
goto done;
} else
goto done;
@@ -965,9 +968,9 @@ static int midend_really_process_key(midend *me, int x, int y, int button)
return ret;
}
-int midend_process_key(midend *me, int x, int y, int button)
+bool midend_process_key(midend *me, int x, int y, int button)
{
- int ret = 1;
+ bool ret = true;
/*
* Harmonise mouse drag and release messages.
@@ -1159,7 +1162,7 @@ void midend_freeze_timer(midend *me, float tprop)
void midend_timer(midend *me, float tplus)
{
- int need_redraw = (me->anim_time > 0 || me->flash_time > 0);
+ bool need_redraw = (me->anim_time > 0 || me->flash_time > 0);
me->anim_pos += tplus;
if (me->anim_pos >= me->anim_time ||
@@ -1281,7 +1284,7 @@ game_params *preset_menu_lookup_by_id(struct preset_menu *menu, int id)
}
static char *preset_menu_add_from_user_env(
- midend *me, struct preset_menu *menu, char *p, int top_level)
+ midend *me, struct preset_menu *menu, char *p, bool top_level)
{
while (*p) {
char *name, *val;
@@ -1302,7 +1305,7 @@ static char *preset_menu_add_from_user_env(
if (*name) {
struct preset_menu *submenu =
preset_menu_add_submenu(menu, dupstr(name));
- p = preset_menu_add_from_user_env(me, submenu, p, FALSE);
+ p = preset_menu_add_from_user_env(me, submenu, p, false);
} else {
/*
* If we get a 'close submenu' indication at the top
@@ -1318,7 +1321,7 @@ static char *preset_menu_add_from_user_env(
preset = me->ourgame->default_params();
me->ourgame->decode_params(preset, val);
- if (me->ourgame->validate_params(preset, TRUE)) {
+ if (me->ourgame->validate_params(preset, true)) {
/* Drop this one from the list. */
me->ourgame->free_params(preset);
continue;
@@ -1349,7 +1352,7 @@ static void preset_menu_encode_params(midend *me, struct preset_menu *menu)
for (i = 0; i < menu->n_entries; i++) {
if (menu->entries[i].params) {
me->encoded_presets[menu->entries[i].id] =
- me->ourgame->encode_params(menu->entries[i].params, TRUE);
+ me->ourgame->encode_params(menu->entries[i].params, true);
} else {
preset_menu_encode_params(me, menu->entries[i].submenu);
}
@@ -1404,7 +1407,7 @@ struct preset_menu *midend_get_presets(midend *me, int *id_limit)
if ((e = getenv(buf)) != NULL) {
e = dupstr(e);
- preset_menu_add_from_user_env(me, me->preset_menu, e, TRUE);
+ preset_menu_add_from_user_env(me, me->preset_menu, e, true);
sfree(e);
}
}
@@ -1428,7 +1431,7 @@ struct preset_menu *midend_get_presets(midend *me, int *id_limit)
int midend_which_preset(midend *me)
{
- char *encoding = me->ourgame->encode_params(me->params, TRUE);
+ char *encoding = me->ourgame->encode_params(me->params, true);
int i, ret;
ret = -1;
@@ -1443,7 +1446,7 @@ int midend_which_preset(midend *me)
return ret;
}
-int midend_wants_statusbar(midend *me)
+bool midend_wants_statusbar(midend *me)
{
return me->ourgame->wants_statusbar;
}
@@ -1535,7 +1538,7 @@ static const char *midend_game_id_int(midend *me, const char *id, int defmode)
char *par = NULL;
const char *desc, *seed;
game_params *newcurparams, *newparams, *oldparams1, *oldparams2;
- int free_params;
+ bool free_params;
seed = strchr(id, '#');
desc = strchr(id, ':');
@@ -1650,18 +1653,18 @@ static const char *midend_game_id_int(midend *me, const char *id, int defmode)
newparams = me->ourgame->dup_params(me->params);
- tmpstr = me->ourgame->encode_params(newcurparams, FALSE);
+ tmpstr = me->ourgame->encode_params(newcurparams, false);
me->ourgame->decode_params(newparams, tmpstr);
sfree(tmpstr);
} else {
newparams = me->ourgame->dup_params(newcurparams);
}
- free_params = TRUE;
+ free_params = true;
} else {
newcurparams = me->curparams;
newparams = me->params;
- free_params = FALSE;
+ free_params = false;
}
if (desc) {
@@ -1708,7 +1711,7 @@ static const char *midend_game_id_int(midend *me, const char *id, int defmode)
sfree(par);
- me->newgame_can_store_undo = FALSE;
+ me->newgame_can_store_undo = false;
return NULL;
}
@@ -1722,7 +1725,7 @@ char *midend_get_game_id(midend *me)
{
char *parstr, *ret;
- parstr = me->ourgame->encode_params(me->curparams, FALSE);
+ parstr = me->ourgame->encode_params(me->curparams, false);
assert(parstr);
assert(me->desc);
ret = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
@@ -1738,7 +1741,7 @@ char *midend_get_random_seed(midend *me)
if (!me->seedstr)
return NULL;
- parstr = me->ourgame->encode_params(me->curparams, TRUE);
+ parstr = me->ourgame->encode_params(me->curparams, true);
assert(parstr);
ret = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
sprintf(ret, "%s#%s", parstr, me->seedstr);
@@ -1754,7 +1757,7 @@ const char *midend_set_config(midend *me, int which, config_item *cfg)
switch (which) {
case CFG_SETTINGS:
params = me->ourgame->custom_params(cfg);
- error = me->ourgame->validate_params(params, TRUE);
+ error = me->ourgame->validate_params(params, true);
if (error) {
me->ourgame->free_params(params);
@@ -1777,12 +1780,12 @@ const char *midend_set_config(midend *me, int which, config_item *cfg)
return NULL;
}
-int midend_can_format_as_text_now(midend *me)
+bool midend_can_format_as_text_now(midend *me)
{
if (me->ourgame->can_format_as_text_ever)
return me->ourgame->can_format_as_text_now(me->params);
else
- return FALSE;
+ return false;
}
char *midend_text_format(midend *me)
@@ -1947,7 +1950,7 @@ void midend_serialise(midend *me,
* The current long-term parameters structure, in full.
*/
if (me->params) {
- char *s = me->ourgame->encode_params(me->params, TRUE);
+ char *s = me->ourgame->encode_params(me->params, true);
wr("PARAMS", s);
sfree(s);
}
@@ -1956,7 +1959,7 @@ void midend_serialise(midend *me,
* The current short-term parameters structure, in full.
*/
if (me->curparams) {
- char *s = me->ourgame->encode_params(me->curparams, TRUE);
+ char *s = me->ourgame->encode_params(me->curparams, true);
wr("CPARAMS", s);
sfree(s);
}
@@ -1985,7 +1988,7 @@ void midend_serialise(midend *me,
len = strlen(me->aux_info);
s1 = snewn(len, unsigned char);
memcpy(s1, me->aux_info, len);
- obfuscate_bitmap(s1, len*8, FALSE);
+ obfuscate_bitmap(s1, len*8, false);
s2 = bin2hex(s1, len);
wr("AUXINFO", s2);
@@ -2058,13 +2061,13 @@ void midend_serialise(midend *me,
* success, or an error message.
*/
static const char *midend_deserialise_internal(
- midend *me, int (*read)(void *ctx, void *buf, int len), void *rctx,
+ midend *me, bool (*read)(void *ctx, void *buf, int len), void *rctx,
const char *(*check)(void *ctx, midend *, const struct deserialise_data *),
void *cctx)
{
struct deserialise_data data;
int gotstates = 0;
- int started = FALSE;
+ bool started = false;
int i;
char *val = NULL;
@@ -2144,7 +2147,7 @@ static const char *midend_deserialise_internal(
}
/* Now most errors are this one, unless otherwise specified */
ret = "Saved data ended unexpectedly";
- started = TRUE;
+ started = true;
} else {
if (!strcmp(key, "VERSION")) {
if (strcmp(val, SERIALISE_VERSION)) {
@@ -2181,7 +2184,7 @@ static const char *midend_deserialise_internal(
unsigned char *tmp;
int len = strlen(val) / 2; /* length in bytes */
tmp = hex2bin(val, len);
- obfuscate_bitmap(tmp, len*8, TRUE);
+ obfuscate_bitmap(tmp, len*8, true);
sfree(data.auxinfo);
data.auxinfo = snewn(len + 1, char);
@@ -2236,17 +2239,17 @@ static const char *midend_deserialise_internal(
data.params = me->ourgame->default_params();
me->ourgame->decode_params(data.params, data.parstr);
- if (me->ourgame->validate_params(data.params, TRUE)) {
+ if (me->ourgame->validate_params(data.params, true)) {
ret = "Long-term parameters in save file are invalid";
goto cleanup;
}
data.cparams = me->ourgame->default_params();
me->ourgame->decode_params(data.cparams, data.cparstr);
- if (me->ourgame->validate_params(data.cparams, FALSE)) {
+ if (me->ourgame->validate_params(data.cparams, false)) {
ret = "Short-term parameters in save file are invalid";
goto cleanup;
}
- if (data.seed && me->ourgame->validate_params(data.cparams, TRUE)) {
+ if (data.seed && me->ourgame->validate_params(data.cparams, true)) {
/*
* The seed's no use with this version, but we can perfectly
* well use the rest of the data.
@@ -2426,7 +2429,7 @@ static const char *midend_deserialise_internal(
}
const char *midend_deserialise(
- midend *me, int (*read)(void *ctx, void *buf, int len), void *rctx)
+ midend *me, bool (*read)(void *ctx, void *buf, int len), void *rctx)
{
return midend_deserialise_internal(me, read, rctx, NULL, NULL);
}
@@ -2439,11 +2442,11 @@ const char *midend_deserialise(
* failure.
*/
const char *identify_game(char **name,
- int (*read)(void *ctx, void *buf, int len),
+ bool (*read)(void *ctx, void *buf, int len),
void *rctx)
{
int nstates = 0, statepos = -1, gotstates = 0;
- int started = FALSE;
+ bool started = false;
char *val = NULL;
/* Initially all errors give the same report */
@@ -2513,7 +2516,7 @@ const char *identify_game(char **name,
}
/* Now most errors are this one, unless otherwise specified */
ret = "Saved data ended unexpectedly";
- started = TRUE;
+ started = true;
} else {
if (!strcmp(key, "VERSION")) {
if (strcmp(val, SERIALISE_VERSION)) {
@@ -2537,7 +2540,7 @@ const char *identify_game(char **name,
return ret;
}
-const char *midend_print_puzzle(midend *me, document *doc, int with_soln)
+const char *midend_print_puzzle(midend *me, document *doc, bool with_soln)
{
game_state *soln = NULL;