Allow spawn rate option to work as intended

This commit is contained in:
Tiehuis 2015-02-23 10:12:01 +13:00
parent a75c200842
commit 375cdeb7da
2 changed files with 12 additions and 4 deletions

View File

@ -183,6 +183,9 @@ void gamestate_new_block(struct gamestate *g)
srand(time(NULL)); srand(time(NULL));
} }
/* Exit early if there are no spaces to place a block */
if (g->blocks_in_play == g->gridsize) return;
/* Fix up this random number generator */ /* Fix up this random number generator */
/* Method: /* Method:
* - Find a non-biased index between 0 and blocks_play, n * - Find a non-biased index between 0 and blocks_play, n
@ -252,6 +255,11 @@ struct gamestate* gamestate_init(struct gameoptions *opt)
g->blocks_in_play = 0; g->blocks_in_play = 0;
g->opts = opt; g->opts = opt;
/* Clamp spawn rate to maximum to avoid possible excessive calculation
* int generation of blocks */
if (g->opts->spawn_rate > g->gridsize)
g->opts->spawn_rate = g->gridsize;
highscore_load(g); highscore_load(g);
/* Initial 3 random blocks */ /* Initial 3 random blocks */

View File

@ -47,18 +47,18 @@ get_new_key:;
/* Game will only end if 0 moves available */ /* Game will only end if 0 moves available */
if (game_running) { if (game_running) {
/* Maybe change this behaviour so if we don't move, we still generate a block */ gamestate_tick(s, g, direction, g->opts->animate ? draw_then_sleep : NULL);
if (gamestate_tick(s, g, direction, g->opts->animate ? draw_then_sleep : NULL))
int spawned;
for (spawned = 0; spawned < g->opts->spawn_rate; spawned++)
gamestate_new_block(g); gamestate_new_block(g);
if (gamestate_end_condition(g)) { if (gamestate_end_condition(g)) {
game_running = false; game_running = false;
goto game_end;
} }
} }
} }
game_end:
gfx_destroy(s); gfx_destroy(s);
gamestate_clear(g); gamestate_clear(g);
return 0; return 0;