2014-12-04 21:27:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2015-02-21 02:36:15 +00:00
|
|
|
#include "engine.h"
|
2015-02-21 22:29:15 +00:00
|
|
|
#include "highscore.h"
|
2014-12-04 21:27:25 +00:00
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Utilize block counter to improve some of the functions so they can run
|
|
|
|
* quicker */
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* This function will move all blocks in the given game the given direction.
|
|
|
|
* The callback function is called after each single move. It can be used to
|
|
|
|
* animate the movement of the board. */
|
|
|
|
static void gravitate(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state *s, struct gamestate *g))
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
#define swap_if_space(xoff, yoff)\
|
|
|
|
do {\
|
|
|
|
if (g->grid[x][y] == 0 && g->grid[x+xoff][y+yoff] != 0) {\
|
|
|
|
g->grid[x][y] = g->grid[x+xoff][y+yoff];\
|
|
|
|
g->grid[x+xoff][y+yoff] = 0;\
|
|
|
|
done = 0;\
|
|
|
|
g->moved = 1;\
|
|
|
|
}\
|
|
|
|
} while (0)
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
int x, y;
|
2014-12-04 21:27:25 +00:00
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
if (d == dir_left) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (x = 0; x < g->opts->grid_width - 1; ++x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
swap_if_space(1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_right) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (x = g->opts->grid_width - 1; x > 0; --x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
swap_if_space(-1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_down) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (y = g->opts->grid_height - 1; y > 0; --y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
swap_if_space(0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_up) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (y = 0; y < g->opts->grid_height - 1; ++y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
swap_if_space(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fatal("Invalid direction passed to gravitate()");
|
|
|
|
/* Not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef swap_if_space
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* The merge function will combine adjacent blocks with the same value for
|
|
|
|
* the given direction. Note, left and right merges will merge in a different
|
|
|
|
* order, so they are not identical in all cases.
|
|
|
|
*
|
|
|
|
* Consider 2 2 2 0
|
|
|
|
*
|
|
|
|
* Right merging: 4 0 2 0
|
|
|
|
* Left merging: 2 0 4 0
|
|
|
|
*/
|
|
|
|
static void merge(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state *s, struct gamestate *g))
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
#define merge_if_equal(xoff, yoff)\
|
|
|
|
do {\
|
|
|
|
if (g->grid[x][y] && (g->grid[x][y] == g->grid[x+xoff][y+yoff])) {\
|
|
|
|
g->grid[x][y] += g->grid[x+xoff][y+yoff];\
|
|
|
|
g->grid[x+xoff][y+yoff] = 0;\
|
2014-12-07 09:09:22 +00:00
|
|
|
g->blocks_in_play -= 1;\
|
2014-12-05 23:02:12 +00:00
|
|
|
g->score_last += g->grid[x][y];\
|
2014-12-04 21:27:25 +00:00
|
|
|
g->score += g->grid[x][y];\
|
|
|
|
g->moved = 1;\
|
|
|
|
}\
|
|
|
|
} while (0)
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
int x, y;
|
2014-12-05 23:02:12 +00:00
|
|
|
g->score_last = 0;
|
2015-02-21 02:36:15 +00:00
|
|
|
|
2014-12-04 21:27:25 +00:00
|
|
|
if (d == dir_left) {
|
|
|
|
for (x = 0; x < g->opts->grid_width - 1; ++x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
merge_if_equal(1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_right) {
|
|
|
|
for (x = g->opts->grid_width - 1; x > 0; --x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
merge_if_equal(-1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_down) {
|
|
|
|
for (y = g->opts->grid_height - 1; y > 0; --y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
merge_if_equal(0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_up) {
|
|
|
|
for (y = 0; y < g->opts->grid_height - 1; ++y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
merge_if_equal(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fatal("Invalid direction passed to merge()");
|
|
|
|
/* Not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
|
|
|
|
#undef merge_if_equal
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Scan the current board and determine if an end outcome has been reached.
|
|
|
|
* -1 indicates a lose condition, 1 indicates a win condition, 0 indicates
|
|
|
|
* end has not yet been reached. */
|
|
|
|
int gamestate_end_condition(struct gamestate *g)
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
2014-12-06 00:43:00 +00:00
|
|
|
int ret = -1;
|
2015-02-21 02:36:15 +00:00
|
|
|
//size_t blocks_counted = 0;
|
2015-02-22 03:38:53 +00:00
|
|
|
int x, y;
|
2015-02-21 02:36:15 +00:00
|
|
|
|
2015-02-22 03:38:53 +00:00
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
2014-12-06 00:43:00 +00:00
|
|
|
if (g->grid[x][y] >= g->opts->goal)
|
2015-02-21 02:36:15 +00:00
|
|
|
return 1;
|
2014-12-04 21:27:25 +00:00
|
|
|
if (!g->grid[x][y] || ((x + 1 < g->opts->grid_width) &&
|
|
|
|
(g->grid[x][y] == g->grid[x+1][y]))
|
2015-02-21 02:36:15 +00:00
|
|
|
|| ((y + 1 < g->opts->grid_height) &&
|
2014-12-04 21:27:25 +00:00
|
|
|
(g->grid[x][y] == g->grid[x][y+1])))
|
2014-12-06 00:43:00 +00:00
|
|
|
ret = 0;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:43:00 +00:00
|
|
|
return ret;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Place a random block into the current grid */
|
|
|
|
void gamestate_new_block(struct gamestate *g)
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
|
|
|
/* pick random square, if it is full, then move forward until we find
|
|
|
|
* an empty square. This is biased */
|
|
|
|
|
|
|
|
static int seeded = 0;
|
|
|
|
if (!seeded) {
|
|
|
|
seeded = 1;
|
|
|
|
srand(time(NULL));
|
|
|
|
}
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Fix up this random number generator */
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Method:
|
2014-12-07 09:09:22 +00:00
|
|
|
* - Find a non-biased index between 0 and blocks_play, n
|
|
|
|
* - Find the nth 0 element in the array
|
|
|
|
* - insert a random value there
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Error here */
|
2015-02-21 02:36:15 +00:00
|
|
|
#ifdef SKIP
|
2014-12-07 09:09:22 +00:00
|
|
|
size_t block_position = (size_t)rand() % (
|
|
|
|
g->opts->grid_width * g->opts->grid_height - g->blocks_in_play);
|
|
|
|
|
|
|
|
size_t i, ps;
|
|
|
|
for (i = 0, ps = 0; ps < block_position; ++i) {
|
|
|
|
if (!g->grid[i / g->opts->grid_width][i % g->opts->grid_height]) ps++;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
g->grid[i / g->opts->grid_width][i % g->opts->grid_height]
|
|
|
|
= (rand() & 3) ? g->opts->spawn_value : g->opts->spawn_value * 2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Use rudimentary for now */
|
|
|
|
int x, y;
|
|
|
|
while (g->grid[x = rand() % g->opts->grid_width][y = rand() % g->opts->grid_height]);
|
2014-12-04 21:27:25 +00:00
|
|
|
g->grid[x][y] = (rand() & 3) ? g->opts->spawn_value : g->opts->spawn_value * 2;
|
2014-12-07 09:09:22 +00:00
|
|
|
g->blocks_in_play += 1;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* This returns the number of digits in the base10 rep of n. The ceiling is
|
|
|
|
* taken so this will be one greater than required */
|
2015-02-21 02:36:15 +00:00
|
|
|
static int digits_ceiling(unsigned int n)
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
|
|
|
int l = 0;
|
|
|
|
while (n) n /= 10, ++l;
|
2014-12-06 20:55:18 +00:00
|
|
|
return l + 1;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Return NULL if we couldn't allocate space for the gamestate. The opt
|
|
|
|
* argument can be passed directly via gameoptions_default i.e
|
|
|
|
* *o = gamestate_init(gameoptions_default) is valid, as the delete function
|
|
|
|
* will find the pointer to the gameoptions and delete the data accordingly. */
|
2014-12-04 21:27:25 +00:00
|
|
|
struct gamestate* gamestate_init(struct gameoptions *opt)
|
|
|
|
{
|
|
|
|
if (!opt) return NULL;
|
|
|
|
|
|
|
|
struct gamestate *g = malloc(sizeof(struct gamestate));
|
|
|
|
if (!g) goto gamestate_alloc_fail;
|
2014-12-07 09:09:22 +00:00
|
|
|
g->gridsize = opt->grid_width * opt->grid_height;
|
2014-12-04 21:27:25 +00:00
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
g->grid_data_ptr = calloc(g->gridsize, sizeof(long));
|
|
|
|
if (!g->grid_data_ptr) goto grid_data_alloc_fail;
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
g->grid = malloc(opt->grid_height * sizeof(long*));
|
2014-12-04 21:27:25 +00:00
|
|
|
if (!g->grid) goto grid_alloc_fail;
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Switch to two allocation version */
|
2015-02-22 03:38:53 +00:00
|
|
|
int i;
|
2014-12-07 09:09:22 +00:00
|
|
|
long **iterator = g->grid;
|
2015-02-22 03:38:53 +00:00
|
|
|
for (i = 0; i < g->gridsize; i += opt->grid_width)
|
2015-02-21 02:36:15 +00:00
|
|
|
*iterator++ = &g->grid_data_ptr[i];
|
2014-12-07 09:09:22 +00:00
|
|
|
|
2014-12-04 21:27:25 +00:00
|
|
|
g->moved = 0;
|
|
|
|
g->score = 0;
|
|
|
|
g->score_high = 0;
|
|
|
|
g->score_last = 0;
|
2015-02-21 02:36:15 +00:00
|
|
|
g->print_width = digits_ceiling(opt->goal);
|
2014-12-07 09:09:22 +00:00
|
|
|
g->blocks_in_play = 0;
|
2014-12-04 21:27:25 +00:00
|
|
|
g->opts = opt;
|
|
|
|
|
2015-02-21 22:29:15 +00:00
|
|
|
highscore_load(g);
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Initial 3 random blocks */
|
2015-02-21 02:36:15 +00:00
|
|
|
gamestate_new_block(g);
|
|
|
|
gamestate_new_block(g);
|
|
|
|
gamestate_new_block(g);
|
2014-12-04 21:27:25 +00:00
|
|
|
return g;
|
|
|
|
|
|
|
|
grid_alloc_fail:
|
2015-02-21 02:36:15 +00:00
|
|
|
free(g->grid_data_ptr);
|
|
|
|
grid_data_alloc_fail:
|
2014-12-04 21:27:25 +00:00
|
|
|
free(g);
|
|
|
|
gamestate_alloc_fail:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* A tick is a gravitate, merge then gravitate all in the same direction.
|
|
|
|
* the moved variable is set to 0 initially and if the gravitate of merge
|
|
|
|
* functions modify it, we can determine which action to take. */
|
|
|
|
int gamestate_tick(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state*, struct gamestate*))
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Reset move. Altered by gravitate and merge if we do move */
|
2014-12-04 21:27:25 +00:00
|
|
|
g->moved = 0;
|
2015-02-21 02:36:15 +00:00
|
|
|
gravitate(s, g, d, callback);
|
|
|
|
merge(s, g, d, callback);
|
|
|
|
gravitate(s, g, d, callback);
|
2014-12-04 21:27:25 +00:00
|
|
|
return g->moved;
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Free all data associated with the gamestate */
|
2014-12-04 21:27:25 +00:00
|
|
|
void gamestate_clear(struct gamestate *g)
|
|
|
|
{
|
2015-02-21 22:29:15 +00:00
|
|
|
highscore_save(g);
|
2015-02-21 02:36:15 +00:00
|
|
|
gameoptions_destroy(g->opts);
|
|
|
|
free(g->grid_data_ptr); /* Free grid data */
|
2014-12-07 09:09:22 +00:00
|
|
|
free(g->grid); /* Free pointers to data slots */
|
2014-12-04 21:27:25 +00:00
|
|
|
free(g);
|
|
|
|
}
|