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);
} }
@ -171,21 +174,23 @@ gint rules_get_str_len(void) {
gboolean rules_check_str(gchar *rstr) { gboolean rules_check_str(gchar *rstr) {
GtkbGameRules r; GtkbGameRules r;
if(strlen(rstr) != _save_fmt_len || if (strlen(rstr) != _save_fmt_len ||
strstr(rstr, " ") || strstr(rstr, " ") ||
strstr(rstr, "\t") || strstr(rstr, "\t") ||
strstr(rstr, "\r") || strstr(rstr, "\r") ||
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,34 +246,41 @@ 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) {
memcpy(_board, balls, sizeof(gint) * _rules.width * _rules.height); memcpy(_board, balls, sizeof(gint) * _rules.width * _rules.height);
} }
_board_undo = g_malloc0(sizeof(gint) * _rules.width * _rules.height); _board_undo = g_malloc0(sizeof(gint) * _rules.width * _rules.height);
_next_colors = g_malloc0(sizeof(gint) * _rules.next); _next_colors = g_malloc0(sizeof(gint) * _rules.next);
if(nextballs) { if (nextballs) {
memcpy(_next_colors, nextballs, sizeof(gint) * _rules.next); memcpy(_next_colors, nextballs, sizeof(gint) * _rules.next);
} }
_next_colors_undo = g_malloc0(sizeof(gint) * _rules.next); _next_colors_undo = g_malloc0(sizeof(gint) * _rules.next);
@ -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,21 +370,23 @@ 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;
} }
gint timer_get_remaining(void) { gint timer_get_remaining(void) {
time_t nowt = time(NULL); time_t nowt = time (NULL);
return _timer_limit + _timer_start_time - nowt; return _timer_limit + _timer_start_time - nowt;
} }
@ -381,78 +403,87 @@ 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;
if(rules_get_width() < rules_get_destroy() && if (rules_get_width() < rules_get_destroy() &&
rules_get_height() < rules_get_destroy()) { /* destroy is impossible */ rules_get_height() < rules_get_destroy()) { /* destroy is impossible */
return 0; return 0;
} }
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++) { {
if(board_get_at_xy(x, y) != 0) { for (x = 0; x < rules_get_width(); x++)
{
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()) {
for(i = 1; for (i = 1;
i < rules_get_width() - x && i < rules_get_width() - x &&
board_get_at_xy(x + i, y) == board_get_at_xy(x, y); board_get_at_xy(x + i, y) == board_get_at_xy(x, y);
i++); i++);
if(i >= rules_get_destroy()) { if (i >= rules_get_destroy()) {
have_del = 1; have_del = 1;
for(j = 0; j < i; j ++) { for (j = 0; j < i; j ++) {
del[y * rules_get_width() + x + j] = 1; del[y * rules_get_width() + x + j] = 1;
} }
} }
} }
/* vertical */ /* vertical */
if(rules_get_height() - y >= rules_get_destroy()) { if (rules_get_height() - y >= rules_get_destroy()) {
for(i = 1; for (i = 1;
i < rules_get_height() - y && i < rules_get_height() - y &&
board_get_at_xy(x, y + i) == board_get_at_xy(x, y); board_get_at_xy(x, y + i) == board_get_at_xy(x, y);
i++); i++);
if(i >= rules_get_destroy()) { if (i >= rules_get_destroy()) {
have_del = 1; have_del = 1;
for(j = 0; j < i; j ++) { for (j = 0; j < i; j ++) {
del[(y + j) * rules_get_width() + x] = 1; del[(y + j) * rules_get_width() + x] = 1;
} }
} }
} }
/* diagonal left -> right */ /* diagonal left -> right */
if(rules_get_width() - x >= rules_get_destroy() && if (rules_get_width() - x >= rules_get_destroy() &&
rules_get_height() - y >= rules_get_destroy()) { rules_get_height() - y >= rules_get_destroy()) {
for(i = 1; for (i = 1;
i < rules_get_width() - x && i < rules_get_width() - x &&
i < rules_get_height() - y && i < rules_get_height() - y &&
board_get_at_xy(x + i, y + i) == board_get_at_xy(x, y); board_get_at_xy(x + i, y + i) == board_get_at_xy(x, y);
i++); i++);
if(i >= rules_get_destroy()) { if (i >= rules_get_destroy()) {
have_del = 1; have_del = 1;
for(j = 0; j < i; j ++) { for (j = 0; j < i; j ++) {
del[(y + j) * rules_get_width() + x + j] = 1; del[(y + j) * rules_get_width() + x + j] = 1;
} }
} }
} }
/* diagonal right -> left */ /* diagonal right -> left */
if(x + 1 >= rules_get_destroy() && if (x + 1 >= rules_get_destroy() &&
rules_get_height() - y >= rules_get_destroy()) { rules_get_height() - y >= rules_get_destroy()) {
for(i = 1; for (i = 1;
i <= x && i <= x &&
i < rules_get_height() - y && i < rules_get_height() - y &&
board_get_at_xy(x - i, y + i) == board_get_at_xy(x, y); board_get_at_xy(x - i, y + i) == board_get_at_xy(x, y);
i++); i++);
if(i >= rules_get_destroy()) { if (i >= rules_get_destroy()) {
have_del = 1; have_del = 1;
for(j = 0; j < i; j ++) { for (j = 0; j < i; j ++) {
del[(y + j) * rules_get_width() + x - j] = 1; del[(y + j) * rules_get_width() + x - j] = 1;
} }
} }
@ -460,22 +491,27 @@ 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++) { {
if(del[y * rules_get_width() + x] == 1) { for (x = 0; x < rules_get_width(); x++)
{
if (del[y * rules_get_width() + x] == 1) {
gint color = board_get_at_xy(x, y); gint color = board_get_at_xy(x, y);
animcadres += gtkbTheme->balls[color - 1].destroyphases + 1; animcadres += gtkbTheme->balls[color - 1].destroyphases + 1;
for(j = 0, animtime = 0; j <= gtkbTheme->balls[color - 1].destroyphases; j++) { for (j = 0, animtime = 0; j <= gtkbTheme->balls[color - 1].destroyphases; j++) {
if(j != gtkbTheme->balls[color - 1].destroyphases) { if (j != gtkbTheme->balls[color - 1].destroyphases) {
animarray[animpos].color = color; animarray[animpos].color = color;
animarray[animpos].phase = j; animarray[animpos].phase = j;
animtime += gtkbTheme->balls[color - 1].destroydelays[j]; animtime += gtkbTheme->balls[color - 1].destroydelays[j];
@ -494,10 +530,11 @@ 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++) {
draw_ball(animarray[i].color, animarray[i].x, animarray[i].y, 0, animarray[i].phase + 1); draw_ball(animarray[i].color, animarray[i].x, animarray[i].y, 0, animarray[i].phase + 1);
_board_destroys[animarray[i].y * rules_get_width() + animarray[i].x] = animarray[i].phase + 1; _board_destroys[animarray[i].y * rules_get_width() + animarray[i].x] = animarray[i].phase + 1;
} }
@ -512,9 +549,9 @@ gint game_destroy_lines(gboolean count_score) {
lock_actions(0); lock_actions(0);
} }
for(i = 0, y = 0; y < rules_get_height(); y++) { for (i = 0, 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) {
i++; i++;
board_set_at_xy(x, y, 0); board_set_at_xy(x, y, 0);
} }