game.c: consistent indentation

This commit is contained in:
wdlkmpx 2021-01-14 21:01:44 +08:00
parent d4b2abb5f9
commit c1989eec10

View File

@ -141,18 +141,21 @@ void rules_set(gint width, gint height, gint colors, gint next, gint destroy) {
} }
gboolean _rules_check_rules(GtkbGameRules r) { gboolean _rules_check_rules(GtkbGameRules r) {
if((r.width < 4 || r.width > 99) || if ((r.width < 4 || r.width > 99)
(r.height < 4 || r.height > 99) || || (r.height < 4 || r.height > 99)
(r.colors < 3 || r.colors > 99) || || (r.colors < 3 || r.colors > 99)
(r.next < 2 || r.next > 99) || || (r.next < 2 || r.next > 99)
(r.destroy < 3 || r.destroy > 99)) || (r.destroy < 3 || r.destroy > 99))
{
return 0; return 0;
}
return 1; return 1;
} }
gchar *_rules_to_str(GtkbGameRules r) { gchar *_rules_to_str(GtkbGameRules r) {
if(!_rules_check_rules(r)) if (!_rules_check_rules(r)) {
return NULL; return NULL;
}
return g_strdup_printf(_save_fmt, r.width, r.height, r.colors, r.next, r.destroy); return g_strdup_printf(_save_fmt, r.width, r.height, r.colors, r.next, r.destroy);
} }
@ -178,14 +181,16 @@ gboolean rules_check_str(gchar *rstr) {
strstr(rstr, "\n") || strstr(rstr, "\n") ||
sscanf(rstr, _save_fmt, &r.width, &r.height, &r.colors, &r.next, &r.destroy) != 5 || sscanf(rstr, _save_fmt, &r.width, &r.height, &r.colors, &r.next, &r.destroy) != 5 ||
!_rules_check_rules(r)) !_rules_check_rules(r))
{
return 0; return 0;
}
return 1; return 1;
} }
gboolean rules_get_from_str(gchar *s, gint *width, gint *height, gint *colors, gint *next, gint *destroy) { gboolean rules_get_from_str(gchar *s, gint *width, gint *height, gint *colors, gint *next, gint *destroy) {
if(!rules_check_str(s)) if (!rules_check_str(s)) {
return 0; return 0;
}
sscanf(s, _save_fmt, width, height, colors, next, destroy); sscanf(s, _save_fmt, width, height, colors, next, destroy);
return 1; return 1;
} }
@ -193,9 +198,9 @@ gboolean rules_get_from_str(gchar *s, gint *width, gint *height, gint *colors, g
gchar *rules_conv_3_0_to_str(gchar *w, gchar *h, gchar *c, gchar *n, gchar *d) { gchar *rules_conv_3_0_to_str(gchar *w, gchar *h, gchar *c, gchar *n, gchar *d) {
GtkbGameRules r; GtkbGameRules r;
if(!w[0] || !h[0] || !c[0] || !n[0] || !d[0]) if (!w[0] || !h[0] || !c[0] || !n[0] || !d[0]) {
return NULL; return NULL;
}
r.width = strtol(w, NULL, 10); r.width = strtol(w, NULL, 10);
r.height = strtol(h, NULL, 10); r.height = strtol(h, NULL, 10);
r.colors = strtol(c, NULL, 10); r.colors = strtol(c, NULL, 10);
@ -209,8 +214,9 @@ gboolean rules_is_current_str(gchar *r) {
gboolean rval = 0; gboolean rval = 0;
gchar *cur = _rules_to_str(_rules); gchar *cur = _rules_to_str(_rules);
if(strcmp(r, cur) == 0) if (strcmp(r, cur) == 0) {
rval = 1; rval = 1;
}
g_free(cur); g_free(cur);
return rval; return rval;
@ -240,26 +246,33 @@ void game_save_state_for_undo(void) {
} }
void game_restore_state_from_undo(void) { void game_restore_state_from_undo(void) {
if(_score_undo == -1 || _hi_score_undo == -1) if (_score_undo == -1 || _hi_score_undo == -1) {
/* cannot undo */ /* cannot undo */
return; return;
}
memcpy(_board, _board_undo, sizeof(gint) * _rules.width * _rules.height); memcpy(_board, _board_undo, sizeof(gint) * _rules.width * _rules.height);
memcpy(_next_colors, _next_colors_undo, sizeof(gint) * _rules.next); memcpy(_next_colors, _next_colors_undo, sizeof(gint) * _rules.next);
_score = _score_undo; _score = _score_undo;
_hi_score = _hi_score_undo; _hi_score = _hi_score_undo;
} }
void game_init_game(gint *balls, gint *nextballs) { void game_init_game(gint *balls, gint *nextballs)
if(_board) {
if (_board) {
g_free(_board); g_free(_board);
if(_board_undo) }
if (_board_undo) {
g_free(_board_undo); g_free(_board_undo);
if(_board_destroys) }
if (_board_destroys) {
g_free(_board_destroys); g_free(_board_destroys);
if(_next_colors) }
if (_next_colors) {
g_free(_next_colors); g_free(_next_colors);
if(_next_colors_undo) }
if (_next_colors_undo) {
g_free(_next_colors_undo); g_free(_next_colors_undo);
}
_board = g_malloc0(sizeof(gint) * _rules.width * _rules.height); _board = g_malloc0(sizeof(gint) * _rules.width * _rules.height);
_board_destroys = g_malloc0(sizeof(gint) * _rules.width * _rules.height); _board_destroys = g_malloc0(sizeof(gint) * _rules.width * _rules.height);
if (balls) { if (balls) {
@ -280,10 +293,10 @@ gint game_count_free_cells(void) {
gint i, counter = 0; gint i, counter = 0;
gint *bp = _board; gint *bp = _board;
for(i = 0; i < _rules.width * _rules.height; i++) for (i = 0; i < _rules.width * _rules.height; i++) {
if (*bp++ == 0) if (*bp++ == 0)
counter++; counter++;
}
return counter; return counter;
} }
@ -304,44 +317,51 @@ gint *game_get_next_as_int_arr(void) {
} }
gint board_get_at_node(gint node) { gint board_get_at_node(gint node) {
if(node >= _rules.width * _rules.height) if (node >= _rules.width * _rules.height) {
return 0; return 0;
}
return _board[node]; return _board[node];
} }
gint board_get_at_xy(gint x, gint y) { gint board_get_at_xy(gint x, gint y) {
if(x >= _rules.width || y > _rules.height) if (x >= _rules.width || y > _rules.height) {
return 0; return 0;
}
return _board[y * _rules.width + x]; return _board[y * _rules.width + x];
} }
gint board_get_destroy_at_xy(gint x, gint y) { gint board_get_destroy_at_xy(gint x, gint y) {
if(x >= _rules.width || y > _rules.height) if (x >= _rules.width || y > _rules.height) {
return 0; return 0;
}
return _board_destroys[y * _rules.width + x]; return _board_destroys[y * _rules.width + x];
} }
void board_set_at_node(gint node, gint col) { void board_set_at_node(gint node, gint col) {
if(node >= _rules.width * _rules.height) if (node >= _rules.width * _rules.height) {
return; return;
}
_board[node] = col; _board[node] = col;
} }
void board_set_at_xy(gint x, gint y, gint col) { void board_set_at_xy(gint x, gint y, gint col) {
if(x >= _rules.width || y > _rules.height) if (x >= _rules.width || y > _rules.height) {
return; return;
}
_board[y * _rules.width + x] = col; _board[y * _rules.width + x] = col;
} }
gint next_get(gint num) { gint next_get(gint num) {
if(num >= _rules.next) if (num >= _rules.next) {
return 0; return 0;
}
return _next_colors[num]; return _next_colors[num];
} }
void next_set(gint num, gint col) { void next_set(gint num, gint col) {
if(num >= _rules.next) if (num >= _rules.next) {
return; return;
}
_next_colors[num] = col; _next_colors[num] = col;
} }
@ -350,16 +370,18 @@ void timer_start(void) {
} }
gboolean timer_is_running(void) { gboolean timer_is_running(void) {
if(_timer_start_time == -1 || _timer_limit <= 0) if (_timer_start_time == -1 || _timer_limit <= 0) {
return 0; return 0;
}
return 1; return 1;
} }
gboolean timer_is_expired(void) { gboolean timer_is_expired(void) {
time_t nowt = time(NULL); time_t nowt = time(NULL);
if(nowt - _timer_start_time >= _timer_limit) if (nowt - _timer_start_time >= _timer_limit) {
return 1; return 1;
}
return 0; return 0;
} }
@ -381,13 +403,20 @@ struct gtkb_animarray {
gint color, x, y, phase, time; gint color, x, y, phase, time;
}; };
int animsort(const void *a, const void *b) { int animsort(const void *a, const void *b)
if(((const struct gtkb_animarray *)a)->time == ((const struct gtkb_animarray *)b)->time) return 0; {
if(((const struct gtkb_animarray *)a)->time > ((const struct gtkb_animarray *)b)->time) return 1; if (((const struct gtkb_animarray *)a)->time == ((const struct gtkb_animarray *)b)->time) {
return 0;
}
if (((const struct gtkb_animarray *)a)->time > ((const struct gtkb_animarray *)b)->time) {
return 1;
}
return -1; return -1;
} }
gint game_destroy_lines(gboolean count_score) {
gint game_destroy_lines(gboolean count_score)
{
gint x, y, i, j; gint x, y, i, j;
gint *del, have_del = 0; gint *del, have_del = 0;
@ -398,8 +427,10 @@ gint game_destroy_lines(gboolean count_score) {
del = g_malloc0(rules_get_width() * rules_get_height() * sizeof(gint)); del = g_malloc0(rules_get_width() * rules_get_height() * sizeof(gint));
for(y = 0; y < rules_get_height(); y++) { for (y = 0; y < rules_get_height(); y++)
for(x = 0; x < rules_get_width(); x++) { {
for (x = 0; x < rules_get_width(); x++)
{
if (board_get_at_xy(x, y) != 0) { if (board_get_at_xy(x, y) != 0) {
/* horizontal */ /* horizontal */
if (rules_get_width() - x >= rules_get_destroy()) { if (rules_get_width() - x >= rules_get_destroy()) {
@ -460,16 +491,21 @@ gint game_destroy_lines(gboolean count_score) {
} }
} }
} }
i = 0; i = 0;
if(have_del) { if (have_del)
if(pref_get_show_destroy()) { {
if (pref_get_show_destroy())
{
gint animcadres = 0, animpos = 0, animtime; gint animcadres = 0, animpos = 0, animtime;
struct gtkb_animarray *animarray; struct gtkb_animarray *animarray;
struct timeval tvs, tve; struct timeval tvs, tve;
animarray = g_new0(struct gtkb_animarray, rules_get_width() * rules_get_height() * gtkbTheme->maxdestphases); animarray = g_new0(struct gtkb_animarray, rules_get_width() * rules_get_height() * gtkbTheme->maxdestphases);
for(y = 0; y < rules_get_height(); y++) { for (y = 0; y < rules_get_height(); y++)
for(x = 0; x < rules_get_width(); x++) { {
for (x = 0; x < rules_get_width(); x++)
{
if (del[y * rules_get_width() + x] == 1) { if (del[y * rules_get_width() + x] == 1) {
gint color = board_get_at_xy(x, y); gint color = board_get_at_xy(x, y);
@ -494,7 +530,8 @@ gint game_destroy_lines(gboolean count_score) {
qsort(animarray, animcadres, sizeof(struct gtkb_animarray), animsort); qsort(animarray, animcadres, sizeof(struct gtkb_animarray), animsort);
lock_actions(1); lock_actions(1);
draw_board(); draw_board();
for(animtime = 0, i = 0; i < animcadres;) { for (animtime = 0, i = 0; i < animcadres;)
{
gettimeofday(&tvs, NULL); gettimeofday(&tvs, NULL);
gint isav = i; gint isav = i;
for (; animtime == animarray[i].time && i < animcadres; i++) { for (; animtime == animarray[i].time && i < animcadres; i++) {