| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <glib.h> |
| 9 |
#include <string.h> |
| 10 |
#include <time.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <sys/time.h> |
| 14 |
|
| 15 |
#include <gtk/gtk.h> |
| 16 |
#include "theme.h" |
| 17 |
#include "prefs.h" |
| 18 |
#include "gfx.h" |
| 19 |
#include "mainmenu.h" |
| 20 |
|
| 21 |
typedef struct _GtkbGameRules GtkbGameRules; |
| 22 |
|
| 23 |
struct _GtkbGameRules { |
| 24 |
gint width; |
| 25 |
gint height; |
| 26 |
gint colors; |
| 27 |
gint next; |
| 28 |
gint destroy; |
| 29 |
}; |
| 30 |
|
| 31 |
GtkbGameRules _rules = {9, 9, 7, 3, 5}; |
| 32 |
|
| 33 |
GtkbGameRules _classic_rules = {9, 9, 7, 3, 5}; |
| 34 |
|
| 35 |
gint _save_fmt_len = 10; |
| 36 |
gchar _save_fmt[] = "%02u%02u%02u%02u%02u"; |
| 37 |
|
| 38 |
|
| 39 |
gint *_board = NULL; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
gint *_board_undo = NULL; |
| 44 |
|
| 45 |
gint *_board_destroys = NULL; |
| 46 |
|
| 47 |
|
| 48 |
gint *_next_colors = NULL; |
| 49 |
|
| 50 |
gint *_next_colors_undo = NULL; |
| 51 |
|
| 52 |
gint _timer_limit = -1; |
| 53 |
time_t _timer_start_time = -1; |
| 54 |
|
| 55 |
|
| 56 |
gint _score = 0; |
| 57 |
|
| 58 |
gint _hi_score = 0; |
| 59 |
|
| 60 |
|
| 61 |
gint _score_undo = -1; |
| 62 |
gint _hi_score_undo = -1; |
| 63 |
|
| 64 |
gint _actions_locked = 0; |
| 65 |
|
| 66 |
gint is_actions_locked(void) { |
| 67 |
return _actions_locked; |
| 68 |
} |
| 69 |
|
| 70 |
void lock_actions(gint lock) { |
| 71 |
_actions_locked = lock; |
| 72 |
menu_set_sensitive_all(1 - lock); |
| 73 |
} |
| 74 |
|
| 75 |
gint rules_get_width(void) { |
| 76 |
return _rules.width; |
| 77 |
} |
| 78 |
|
| 79 |
gint rules_get_height(void) { |
| 80 |
return _rules.height; |
| 81 |
} |
| 82 |
|
| 83 |
gint rules_get_colors(void) { |
| 84 |
return _rules.colors; |
| 85 |
} |
| 86 |
|
| 87 |
gint rules_get_next(void) { |
| 88 |
return _rules.next; |
| 89 |
} |
| 90 |
|
| 91 |
gint rules_get_destroy(void) { |
| 92 |
return _rules.destroy; |
| 93 |
} |
| 94 |
|
| 95 |
gint rules_get_classic_width(void) { |
| 96 |
return _classic_rules.width; |
| 97 |
} |
| 98 |
|
| 99 |
gint rules_get_classic_height(void) { |
| 100 |
return _classic_rules.height; |
| 101 |
} |
| 102 |
|
| 103 |
gint rules_get_classic_colors(void) { |
| 104 |
return _classic_rules.colors; |
| 105 |
} |
| 106 |
|
| 107 |
gint rules_get_classic_next(void) { |
| 108 |
return _classic_rules.next; |
| 109 |
} |
| 110 |
|
| 111 |
gint rules_get_classic_destroy(void) { |
| 112 |
return _classic_rules.destroy; |
| 113 |
} |
| 114 |
|
| 115 |
void rules_set_width(gint width) { |
| 116 |
_rules.width = width; |
| 117 |
} |
| 118 |
|
| 119 |
void rules_set_height(gint height) { |
| 120 |
_rules.height = height; |
| 121 |
} |
| 122 |
|
| 123 |
void rules_set_colors(gint colors) { |
| 124 |
_rules.colors = colors; |
| 125 |
} |
| 126 |
|
| 127 |
void rules_set_next(gint next) { |
| 128 |
_rules.next = next; |
| 129 |
} |
| 130 |
|
| 131 |
void rules_set_destroy(gint destroy) { |
| 132 |
_rules.destroy = destroy; |
| 133 |
} |
| 134 |
|
| 135 |
void rules_set(gint width, gint height, gint colors, gint next, gint destroy) { |
| 136 |
_rules.width = width; |
| 137 |
_rules.height = height; |
| 138 |
_rules.colors = colors; |
| 139 |
_rules.next = next; |
| 140 |
_rules.destroy = destroy; |
| 141 |
} |
| 142 |
|
| 143 |
gboolean _rules_check_rules(GtkbGameRules r) { |
| 144 |
if ((r.width < 4 || r.width > 99) |
| 145 |
|| (r.height < 4 || r.height > 99) |
| 146 |
|| (r.colors < 3 || r.colors > 99) |
| 147 |
|| (r.next < 2 || r.next > 99) |
| 148 |
|| (r.destroy < 3 || r.destroy > 99)) |
| 149 |
{ |
| 150 |
return 0; |
| 151 |
} |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
gchar *_rules_to_str(GtkbGameRules r) { |
| 156 |
if (!_rules_check_rules(r)) { |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
return g_strdup_printf(_save_fmt, r.width, r.height, r.colors, r.next, r.destroy); |
| 160 |
} |
| 161 |
|
| 162 |
gchar *rules_get_classic_as_str() { |
| 163 |
return _rules_to_str(_classic_rules); |
| 164 |
} |
| 165 |
|
| 166 |
gchar *rules_get_as_str() { |
| 167 |
return _rules_to_str(_rules); |
| 168 |
} |
| 169 |
|
| 170 |
gint rules_get_str_len(void) { |
| 171 |
return _save_fmt_len; |
| 172 |
} |
| 173 |
|
| 174 |
gboolean rules_check_str(gchar *rstr) { |
| 175 |
GtkbGameRules r; |
| 176 |
|
| 177 |
if (strlen(rstr) != (size_t) _save_fmt_len || |
| 178 |
strstr(rstr, " ") || |
| 179 |
strstr(rstr, "\t") || |
| 180 |
strstr(rstr, "\r") || |
| 181 |
strstr(rstr, "\n") || |
| 182 |
sscanf(rstr, _save_fmt, &r.width, &r.height, &r.colors, &r.next, &r.destroy) != 5 || |
| 183 |
!_rules_check_rules(r)) |
| 184 |
{ |
| 185 |
return 0; |
| 186 |
} |
| 187 |
return 1; |
| 188 |
} |
| 189 |
|
| 190 |
gboolean rules_get_from_str(gchar *s, gint *width, gint *height, gint *colors, gint *next, gint *destroy) { |
| 191 |
if (!rules_check_str(s)) { |
| 192 |
return 0; |
| 193 |
} |
| 194 |
sscanf(s, _save_fmt, width, height, colors, next, destroy); |
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
gchar *rules_conv_3_0_to_str(gchar *w, gchar *h, gchar *c, gchar *n, gchar *d) { |
| 199 |
GtkbGameRules r; |
| 200 |
|
| 201 |
if (!w[0] || !h[0] || !c[0] || !n[0] || !d[0]) { |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
r.width = strtol(w, NULL, 10); |
| 205 |
r.height = strtol(h, NULL, 10); |
| 206 |
r.colors = strtol(c, NULL, 10); |
| 207 |
r.next = strtol(n, NULL, 10); |
| 208 |
r.destroy = strtol(d, NULL, 10); |
| 209 |
|
| 210 |
return _rules_to_str(r); |
| 211 |
} |
| 212 |
|
| 213 |
gboolean rules_is_current_str(gchar *r) { |
| 214 |
gboolean rval = 0; |
| 215 |
gchar *cur = _rules_to_str(_rules); |
| 216 |
|
| 217 |
if (strcmp(r, cur) == 0) { |
| 218 |
rval = 1; |
| 219 |
} |
| 220 |
g_free(cur); |
| 221 |
|
| 222 |
return rval; |
| 223 |
} |
| 224 |
|
| 225 |
gint game_get_score(void) { |
| 226 |
return _score; |
| 227 |
} |
| 228 |
|
| 229 |
void game_set_score(gint score) { |
| 230 |
_score = score; |
| 231 |
} |
| 232 |
|
| 233 |
gint game_get_hi_score(void) { |
| 234 |
return _hi_score; |
| 235 |
} |
| 236 |
|
| 237 |
void game_set_hi_score(gint score) { |
| 238 |
_hi_score = score; |
| 239 |
} |
| 240 |
|
| 241 |
void game_save_state_for_undo(void) { |
| 242 |
memcpy(_board_undo, _board, sizeof(gint) * _rules.width * _rules.height); |
| 243 |
memcpy(_next_colors_undo, _next_colors, sizeof(gint) * _rules.next); |
| 244 |
_score_undo = _score; |
| 245 |
_hi_score_undo = _hi_score; |
| 246 |
} |
| 247 |
|
| 248 |
void game_restore_state_from_undo(void) { |
| 249 |
if (_score_undo == -1 || _hi_score_undo == -1) { |
| 250 |
|
| 251 |
return; |
| 252 |
} |
| 253 |
memcpy(_board, _board_undo, sizeof(gint) * _rules.width * _rules.height); |
| 254 |
memcpy(_next_colors, _next_colors_undo, sizeof(gint) * _rules.next); |
| 255 |
_score = _score_undo; |
| 256 |
_hi_score = _hi_score_undo; |
| 257 |
} |
| 258 |
|
| 259 |
void game_init_game(gint *balls, gint *nextballs) |
| 260 |
{ |
| 261 |
if (_board) { |
| 262 |
g_free(_board); |
| 263 |
} |
| 264 |
if (_board_undo) { |
| 265 |
g_free(_board_undo); |
| 266 |
} |
| 267 |
if (_board_destroys) { |
| 268 |
g_free(_board_destroys); |
| 269 |
} |
| 270 |
if (_next_colors) { |
| 271 |
g_free(_next_colors); |
| 272 |
} |
| 273 |
if (_next_colors_undo) { |
| 274 |
g_free(_next_colors_undo); |
| 275 |
} |
| 276 |
_board = g_malloc0(sizeof(gint) * _rules.width * _rules.height); |
| 277 |
_board_destroys = g_malloc0(sizeof(gint) * _rules.width * _rules.height); |
| 278 |
if (balls) { |
| 279 |
memcpy(_board, balls, sizeof(gint) * _rules.width * _rules.height); |
| 280 |
} |
| 281 |
_board_undo = g_malloc0(sizeof(gint) * _rules.width * _rules.height); |
| 282 |
_next_colors = g_malloc0(sizeof(gint) * _rules.next); |
| 283 |
if (nextballs) { |
| 284 |
memcpy(_next_colors, nextballs, sizeof(gint) * _rules.next); |
| 285 |
} |
| 286 |
_next_colors_undo = g_malloc0(sizeof(gint) * _rules.next); |
| 287 |
_score = 0; |
| 288 |
_score_undo = -1; |
| 289 |
_hi_score_undo = -1; |
| 290 |
} |
| 291 |
|
| 292 |
gint game_count_free_cells(void) { |
| 293 |
gint i, counter = 0; |
| 294 |
gint *bp = _board; |
| 295 |
|
| 296 |
for (i = 0; i < _rules.width * _rules.height; i++) { |
| 297 |
if (*bp++ == 0) |
| 298 |
counter++; |
| 299 |
} |
| 300 |
return counter; |
| 301 |
} |
| 302 |
|
| 303 |
gint *game_get_board_as_int_arr(void) { |
| 304 |
gint *b; |
| 305 |
|
| 306 |
b = g_malloc(sizeof(gint) * _rules.width * _rules.height); |
| 307 |
memcpy(b, _board, sizeof(gint) * _rules.width * _rules.height); |
| 308 |
return b; |
| 309 |
} |
| 310 |
|
| 311 |
gint *game_get_next_as_int_arr(void) { |
| 312 |
gint *n; |
| 313 |
|
| 314 |
n = g_malloc(sizeof(gint) * _rules.next); |
| 315 |
memcpy(n, _next_colors, sizeof(gint) * _rules.next); |
| 316 |
return n; |
| 317 |
} |
| 318 |
|
| 319 |
gint board_get_at_node(gint node) { |
| 320 |
if (node >= _rules.width * _rules.height) { |
| 321 |
return 0; |
| 322 |
} |
| 323 |
return _board[node]; |
| 324 |
} |
| 325 |
|
| 326 |
gint board_get_at_xy(gint x, gint y) { |
| 327 |
if (x >= _rules.width || y > _rules.height) { |
| 328 |
return 0; |
| 329 |
} |
| 330 |
return _board[y * _rules.width + x]; |
| 331 |
} |
| 332 |
|
| 333 |
gint board_get_destroy_at_xy(gint x, gint y) { |
| 334 |
if (x >= _rules.width || y > _rules.height) { |
| 335 |
return 0; |
| 336 |
} |
| 337 |
return _board_destroys[y * _rules.width + x]; |
| 338 |
} |
| 339 |
|
| 340 |
void board_set_at_node(gint node, gint col) { |
| 341 |
if (node >= _rules.width * _rules.height) { |
| 342 |
return; |
| 343 |
} |
| 344 |
_board[node] = col; |
| 345 |
} |
| 346 |
|
| 347 |
void board_set_at_xy(gint x, gint y, gint col) { |
| 348 |
if (x >= _rules.width || y > _rules.height) { |
| 349 |
return; |
| 350 |
} |
| 351 |
_board[y * _rules.width + x] = col; |
| 352 |
} |
| 353 |
|
| 354 |
gint next_get(gint num) { |
| 355 |
if (num >= _rules.next) { |
| 356 |
return 0; |
| 357 |
} |
| 358 |
return _next_colors[num]; |
| 359 |
} |
| 360 |
|
| 361 |
void next_set(gint num, gint col) { |
| 362 |
if (num >= _rules.next) { |
| 363 |
return; |
| 364 |
} |
| 365 |
_next_colors[num] = col; |
| 366 |
} |
| 367 |
|
| 368 |
void timer_start(void) { |
| 369 |
_timer_start_time = time(NULL); |
| 370 |
} |
| 371 |
|
| 372 |
gboolean timer_is_running(void) { |
| 373 |
if (_timer_start_time == -1 || _timer_limit <= 0) { |
| 374 |
return 0; |
| 375 |
} |
| 376 |
return 1; |
| 377 |
} |
| 378 |
|
| 379 |
gboolean timer_is_expired(void) { |
| 380 |
time_t nowt = time(NULL); |
| 381 |
|
| 382 |
if (nowt - _timer_start_time >= _timer_limit) { |
| 383 |
return 1; |
| 384 |
} |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
gint timer_get_remaining(void) { |
| 389 |
time_t nowt = time (NULL); |
| 390 |
|
| 391 |
return _timer_limit + _timer_start_time - nowt; |
| 392 |
} |
| 393 |
|
| 394 |
void timer_set_limit(gint limit) { |
| 395 |
_timer_limit =limit; |
| 396 |
} |
| 397 |
|
| 398 |
gint timer_get_limit(void) { |
| 399 |
return _timer_limit; |
| 400 |
} |
| 401 |
|
| 402 |
struct gtkb_animarray { |
| 403 |
gint color, x, y, phase, time; |
| 404 |
}; |
| 405 |
|
| 406 |
int animsort(const void *a, const void *b) |
| 407 |
{ |
| 408 |
if (((const struct gtkb_animarray *)a)->time == ((const struct gtkb_animarray *)b)->time) { |
| 409 |
return 0; |
| 410 |
} |
| 411 |
if (((const struct gtkb_animarray *)a)->time > ((const struct gtkb_animarray *)b)->time) { |
| 412 |
return 1; |
| 413 |
} |
| 414 |
return -1; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
gint game_destroy_lines(gboolean count_score) |
| 419 |
{ |
| 420 |
gint x, y, i, j; |
| 421 |
gint *del, have_del = 0; |
| 422 |
|
| 423 |
if (rules_get_width() < rules_get_destroy() && |
| 424 |
rules_get_height() < rules_get_destroy()) { |
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
del = g_malloc0(rules_get_width() * rules_get_height() * sizeof(gint)); |
| 429 |
|
| 430 |
for (y = 0; y < rules_get_height(); y++) |
| 431 |
{ |
| 432 |
for (x = 0; x < rules_get_width(); x++) |
| 433 |
{ |
| 434 |
if (board_get_at_xy(x, y) != 0) { |
| 435 |
|
| 436 |
if (rules_get_width() - x >= rules_get_destroy()) { |
| 437 |
for (i = 1; |
| 438 |
i < rules_get_width() - x && |
| 439 |
board_get_at_xy(x + i, y) == board_get_at_xy(x, y); |
| 440 |
i++); |
| 441 |
if (i >= rules_get_destroy()) { |
| 442 |
have_del = 1; |
| 443 |
for (j = 0; j < i; j ++) { |
| 444 |
del[y * rules_get_width() + x + j] = 1; |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
if (rules_get_height() - y >= rules_get_destroy()) { |
| 450 |
for (i = 1; |
| 451 |
i < rules_get_height() - y && |
| 452 |
board_get_at_xy(x, y + i) == board_get_at_xy(x, y); |
| 453 |
i++); |
| 454 |
if (i >= rules_get_destroy()) { |
| 455 |
have_del = 1; |
| 456 |
for (j = 0; j < i; j ++) { |
| 457 |
del[(y + j) * rules_get_width() + x] = 1; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
if (rules_get_width() - x >= rules_get_destroy() && |
| 463 |
rules_get_height() - y >= rules_get_destroy()) { |
| 464 |
for (i = 1; |
| 465 |
i < rules_get_width() - x && |
| 466 |
i < rules_get_height() - y && |
| 467 |
board_get_at_xy(x + i, y + i) == board_get_at_xy(x, y); |
| 468 |
i++); |
| 469 |
if (i >= rules_get_destroy()) { |
| 470 |
have_del = 1; |
| 471 |
for (j = 0; j < i; j ++) { |
| 472 |
del[(y + j) * rules_get_width() + x + j] = 1; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
if (x + 1 >= rules_get_destroy() && |
| 478 |
rules_get_height() - y >= rules_get_destroy()) { |
| 479 |
for (i = 1; |
| 480 |
i <= x && |
| 481 |
i < rules_get_height() - y && |
| 482 |
board_get_at_xy(x - i, y + i) == board_get_at_xy(x, y); |
| 483 |
i++); |
| 484 |
if (i >= rules_get_destroy()) { |
| 485 |
have_del = 1; |
| 486 |
for (j = 0; j < i; j ++) { |
| 487 |
del[(y + j) * rules_get_width() + x - j] = 1; |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
i = 0; |
| 496 |
if (have_del) |
| 497 |
{ |
| 498 |
if (pref_get_show_destroy()) |
| 499 |
{ |
| 500 |
gint animcadres = 0, animpos = 0, animtime; |
| 501 |
struct gtkb_animarray *animarray; |
| 502 |
struct timeval tvs, tve; |
| 503 |
|
| 504 |
animarray = g_new0(struct gtkb_animarray, rules_get_width() * rules_get_height() * gtkbTheme->maxdestphases); |
| 505 |
for (y = 0; y < rules_get_height(); y++) |
| 506 |
{ |
| 507 |
for (x = 0; x < rules_get_width(); x++) |
| 508 |
{ |
| 509 |
if (del[y * rules_get_width() + x] == 1) { |
| 510 |
gint color = board_get_at_xy(x, y); |
| 511 |
|
| 512 |
animcadres += gtkbTheme->balls[color - 1].destroyphases + 1; |
| 513 |
for (j = 0, animtime = 0; j <= gtkbTheme->balls[color - 1].destroyphases; j++) { |
| 514 |
if (j != gtkbTheme->balls[color - 1].destroyphases) { |
| 515 |
animarray[animpos].color = color; |
| 516 |
animarray[animpos].phase = j; |
| 517 |
animtime += gtkbTheme->balls[color - 1].destroydelays[j]; |
| 518 |
} else { |
| 519 |
animarray[animpos].color = 0; |
| 520 |
animarray[animpos].phase = 0; |
| 521 |
} |
| 522 |
animarray[animpos].x = x; |
| 523 |
animarray[animpos].y = y; |
| 524 |
animarray[animpos].time = animtime; |
| 525 |
animpos++; |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
qsort(animarray, animcadres, sizeof(struct gtkb_animarray), animsort); |
| 531 |
lock_actions(1); |
| 532 |
draw_board(); |
| 533 |
for (animtime = 0, i = 0; i < animcadres;) |
| 534 |
{ |
| 535 |
gettimeofday(&tvs, NULL); |
| 536 |
gint isav = i; |
| 537 |
for (; animtime == animarray[i].time && i < animcadres; i++) { |
| 538 |
draw_ball(animarray[i].color, animarray[i].x, animarray[i].y, 0, animarray[i].phase + 1); |
| 539 |
_board_destroys[animarray[i].y * rules_get_width() + animarray[i].x] = animarray[i].phase + 1; |
| 540 |
} |
| 541 |
do { |
| 542 |
gtk_main_iteration_do(0); |
| 543 |
gettimeofday(&tve, NULL); |
| 544 |
} while((tve.tv_sec - tvs.tv_sec) * 1000000 + tve.tv_usec - tvs.tv_usec < (animarray[i].time - animtime) * 1000); |
| 545 |
animtime = animarray[isav].time; |
| 546 |
} |
| 547 |
g_free(animarray); |
| 548 |
memset(_board_destroys, 0, rules_get_width() * rules_get_height() * sizeof(gint)); |
| 549 |
lock_actions(0); |
| 550 |
} |
| 551 |
|
| 552 |
for (i = 0, y = 0; y < rules_get_height(); y++) { |
| 553 |
for (x = 0; x < rules_get_width(); x++) { |
| 554 |
if (del[y * rules_get_width() + x] == 1) { |
| 555 |
i++; |
| 556 |
board_set_at_xy(x, y, 0); |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
g_free(del); |
| 563 |
return i; |
| 564 |
} |