summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-04-23 15:54:47 -0400
committerAndrew Opalach <andrew@akon.city> 2019-04-23 15:54:47 -0400
commit10835bc09f0979a6bf1c8b1ead2658f48b28b76f (patch)
treee6960d29717ddecd45c88fa7ec510e644eecfda8 /core/src
parent458ab270a8b7246a1a617e34c02aa74deca92ba6 (diff)
downloadcetris-10835bc09f0979a6bf1c8b1ead2658f48b28b76f.tar.gz
cetris-10835bc09f0979a6bf1c8b1ead2658f48b28b76f.tar.bz2
cetris-10835bc09f0979a6bf1c8b1ead2658f48b28b76f.zip
Revert "convert core to ansii c"
This reverts commit 101fe6381be215c7109624099f3e8e3aec115df0.
Diffstat (limited to 'core/src')
-rw-r--r--core/src/cetris.c41
-rw-r--r--core/src/input.c5
-rw-r--r--core/src/matrix.c65
-rw-r--r--core/src/test.c6
-rw-r--r--core/src/types.c8
5 files changed, 53 insertions, 72 deletions
diff --git a/core/src/cetris.c b/core/src/cetris.c
index aa0d105..4e404d6 100644
--- a/core/src/cetris.c
+++ b/core/src/cetris.c
@@ -1,4 +1,4 @@
-//#include <time.h>
+#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
@@ -32,7 +32,7 @@ void init_game(cetris_game* g) {
/* check for config errors */
assert(CETRIS_NEXT_PIECE_DELAY >= CETRIS_LINE_CLEAR_DELAY);
- //srand(time(NULL));
+ srand(time(NULL));
#ifdef BUILD_TESTS
apply_test_board(g, TSPIN_NO_LINES);
@@ -49,8 +49,7 @@ void init_game(cetris_game* g) {
}
void init_piece_queue(cetris_game* g) {
- u8 i;
- for (i = 0; i < 7; i++) {
+ for (u8 i = 0; i < 7; i++) {
g->piece_queue[i].t = i;
g->piece_queue[i].c = i + 1;
memcpy(g->piece_queue[i].m, default_matrices[i], sizeof(piece_matrix));
@@ -58,13 +57,12 @@ void init_piece_queue(cetris_game* g) {
g->piece_queue[i].lock_tick = 0;
g->piece_queue[i].locked = false;
g->piece_queue[i].ghost_y = 0;
- g->piece_queue[i].pos = new_vec2(CETRIS_INITIAL_X, CETRIS_INITIAL_Y - CETRIS_INITIAL_Y_OFFSET);
+ g->piece_queue[i].pos = (vec2){CETRIS_INITIAL_X, CETRIS_INITIAL_Y - CETRIS_INITIAL_Y_OFFSET};
}
}
void shuffle_queue(cetris_game* g) {
- u8 i;
- for (i = 0; i < 7; i++) {
+ for (u8 i = 0; i < 7; i++) {
tetrimino t = g->piece_queue[i];
u8 rand_index = rand() % 7;
g->piece_queue[i] = g->piece_queue[rand_index];
@@ -73,7 +71,6 @@ void shuffle_queue(cetris_game* g) {
}
void update_game_tick(cetris_game* g) {
- u8 did_move;
if (g->game_over) return;
g->tick++;
@@ -84,11 +81,11 @@ void update_game_tick(cetris_game* g) {
if (g->next_piece_tick) return;
- did_move = 0;
+ bool did_move = false;
if (g->tick >= g->next_drop_tick || !g->next_drop_tick) {
if (g->next_drop_tick) {
move_current(g, DOWN);
- did_move = 1;
+ did_move = true;
}
if (g->level <= 20) {
@@ -103,14 +100,14 @@ void update_game_tick(cetris_game* g) {
g->current.pos.y++;
if (check_matrix(g, &g->current.m) <= 0) {
lock_current(g);
- did_move = 1;
+ did_move = true;
}
g->current.pos.y--;
g->current.lock_tick = 0;
}
if (handle_inputs(g)) {
- did_move = 1;
+ did_move = true;
}
if (did_move) update_board(g);
@@ -135,19 +132,18 @@ void next_piece(cetris_game* g) {
}
void lock_current(cetris_game* g) {
- u8 x, y;
- for (x = 0; x < CETRIS_BOARD_X; x++) {
- for (y = 0; y < CETRIS_BOARD_Y; y++) {
+ g->current.locked = true;
+ for (u8 x = 0; x < CETRIS_BOARD_X; x++) {
+ for (u8 y = 0; y < CETRIS_BOARD_Y; y++) {
if (g->board[x][y].occupied) g->board[x][y].constant = 1;
}
}
- g->current.locked = true;
update_board(g);
}
void make_ghosts(cetris_game* g) {
u8 orig_y = g->current.pos.y;
- while (1) {
+ while (true) {
g->current.pos.y++;
if (check_matrix(g, &g->current.m) <= 0) {
g->current.ghost_y = g->current.pos.y - 1;
@@ -160,11 +156,10 @@ void make_ghosts(cetris_game* g) {
void update_board(cetris_game* g) {
if (g->game_over) return;
- u8 x, y, s;
u8 lines_cleared = 0;
- for (y = 0; y < CETRIS_BOARD_Y; y++) {
+ for (u8 y = 0; y < CETRIS_BOARD_Y; y++) {
bool clear_line = true;
- for (x = 0; x < CETRIS_BOARD_X; x++) {
+ for (u8 x = 0; x < CETRIS_BOARD_X; x++) {
if (!g->board[x][y].constant) {
memset(&g->board[x][y], 0, sizeof(slot));
}
@@ -173,10 +168,10 @@ void update_board(cetris_game* g) {
clear_line = false;
}
}
- /* remove tick only tracked on first block of line */
+ // remove tick only tracked on first block of line
if (g->board[0][y].remove_tick && g->board[0][y].remove_tick <= g->tick) {
- for (s = 0; s < y; s++) {
- for (x = 0; x < CETRIS_BOARD_X; x++) {
+ for (i8 s = y - 1; s >= 0; s--) {
+ for (u8 x = 0; x < CETRIS_BOARD_X; x++) {
g->board[x][s + 1] = g->board[x][s];
}
}
diff --git a/core/src/input.c b/core/src/input.c
index c21b587..65dead1 100644
--- a/core/src/input.c
+++ b/core/src/input.c
@@ -29,9 +29,8 @@ bool handle_inputs(cetris_game* g) {
}
if (g->held_moves[LEFT]) {
- if (g->prev_das_move == LEFT || g->das_repeat == 0) {
- g->das_repeat++;
- } else g->das_repeat = 0;
+ if (g->prev_das_move == LEFT || g->das_repeat == 0) g->das_repeat++;
+ else g->das_repeat = 0;
move_current(g, LEFT);
g->prev_das_move = LEFT;
}
diff --git a/core/src/matrix.c b/core/src/matrix.c
index 2c9daee..caae107 100644
--- a/core/src/matrix.c
+++ b/core/src/matrix.c
@@ -6,27 +6,27 @@
/* SRS WALL KICK VALUES */
-/* https://tetris.wiki/SRS */
+// https://tetris.wiki/SRS
static const vec2 srs_wall_kicks[8][5] = {
- { {0, 0}, {-1, 0}, {-1,1}, {0,-2}, {-1,-2} }, /* 0->R */
- { {0, 0}, {1, 0}, {1,-1}, {0,2}, {1,2} }, /* R->0 */
- { {0, 0}, {1, 0}, {1,-1}, {0,2}, {1,2} }, /* R->2 */
- { {0, 0}, {-1, 0}, {-1,1}, {0,-2}, {-1,-2} }, /* 2->R */
- { {0, 0}, {1, 0}, {1,1}, {0,-2}, {1,-2} }, /* 2->L */
- { {0, 0}, {-1, 0}, {-1,-1}, {0,2}, {-1,2} }, /* L->2 */
- { {0, 0}, {-1, 0}, {-1,-1}, {0,2}, {-1,2} }, /* L->0 */
- { {0, 0}, {1, 0}, {1,1}, {0,-2}, {1,-2} } /* 0->L */
+ { {0, 0}, {-1, 0}, {-1,1}, {0,-2}, {-1,-2} }, // 0->R
+ { {0, 0}, {1, 0}, {1,-1}, {0,2}, {1,2} }, // R->0
+ { {0, 0}, {1, 0}, {1,-1}, {0,2}, {1,2} }, // R->2
+ { {0, 0}, {-1, 0}, {-1,1}, {0,-2}, {-1,-2} }, // 2->R
+ { {0, 0}, {1, 0}, {1,1}, {0,-2}, {1,-2} }, // 2->L
+ { {0, 0}, {-1, 0}, {-1,-1}, {0,2}, {-1,2} }, // L->2
+ { {0, 0}, {-1, 0}, {-1,-1}, {0,2}, {-1,2} }, // L->0
+ { {0, 0}, {1, 0}, {1,1}, {0,-2}, {1,-2} } // 0->L
};
static const vec2 srs_wall_kicks_i[8][5] = {
- { {0, 0}, {-2, 0}, {1, 0}, {-2,-1}, {1,2} }, /* 0->R */
- { {0, 0}, {2, 0}, {-1, 0}, {2,1}, {-1,-2} }, /* R->0 */
- { {0, 0}, {-1, 0}, {2, 0}, {-1,2}, {2,-1} }, /* R->2 */
- { {0, 0}, {1, 0}, {-2, 0}, {1,-2}, {-2,1} }, /* 2->R */
- { {0, 0}, {2, 0}, {-1, 0}, {2,1}, {-1,-2} }, /* 2->L */
- { {0, 0}, {-2, 0}, {1, 0}, {-2,-1}, {1,2} }, /* L->2 */
- { {0, 0}, {1, 0}, {-2, 0}, {1,-2}, {-2,1} }, /* L->0 */
- { {0, 0}, {-1, 0}, {2, 0}, {-1,2}, {2,-1} } /* 0->L */
+ { {0, 0}, {-2, 0}, {1, 0}, {-2,-1}, {1,2} }, // 0->R
+ { {0, 0}, {2, 0}, {-1, 0}, {2,1}, {-1,-2} }, // R->0
+ { {0, 0}, {-1, 0}, {2, 0}, {-1,2}, {2,-1} }, // R->2
+ { {0, 0}, {1, 0}, {-2, 0}, {1,-2}, {-2,1} }, // 2->R
+ { {0, 0}, {2, 0}, {-1, 0}, {2,1}, {-1,-2} }, // 2->L
+ { {0, 0}, {-2, 0}, {1, 0}, {-2,-1}, {1,2} }, // L->2
+ { {0, 0}, {1, 0}, {-2, 0}, {1,-2}, {-2,1} }, // L->0
+ { {0, 0}, {-1, 0}, {2, 0}, {-1,2}, {2,-1} } // 0->L
};
/* DEFAULT MATRIX FOR EACH POSSIBLE TETRIMINO */
@@ -85,7 +85,7 @@ const piece_matrix default_matrices[7] = {
/* MATRIX MODIFICATION */
const vec2 basic_movements[5] = {
- {0, 0}, {0, 1}, {0, 1}, {1, 0}, {-1, 0} /* NONE, DOWN, USER_DOWN, RIGHT, LEFT */
+ {0, 0}, {0, 1}, {0, 1}, {1, 0}, {-1, 0} // NONE, DOWN, USER_DOWN, RIGHT, LEFT
};
void move_current(cetris_game* g, input_t move) {
@@ -109,10 +109,9 @@ void move_current(cetris_game* g, input_t move) {
}
i8 check_matrix(cetris_game* g, piece_matrix* m) {
- i8 x, y;
- for (y = 0; y < 4; y++) {
- for (x = 0; x < 4; x++) {
- vec2 r = new_vec2(x + g->current.pos.x, y + g->current.pos.y);
+ for (i8 y = 0; y < 4; y++) {
+ for (i8 x = 0; x < 4; x++) {
+ vec2 r = (vec2){x + g->current.pos.x, y + g->current.pos.y};
if (r.y < 0) continue;
if ((*m)[y][x]) {
if (r.x >= CETRIS_BOARD_X || r.x < 0) return 0;
@@ -126,10 +125,9 @@ i8 check_matrix(cetris_game* g, piece_matrix* m) {
}
void set_matrix(cetris_game* g, piece_matrix* m) {
- i8 x, y;
- for (y = 0; y < 4; y++) {
- for (x = 0; x < 4; x++) {
- vec2 r = new_vec2(x + g->current.pos.x, y + g->current.pos.y);
+ for (i8 y = 0; y < 4; y++) {
+ for (i8 x = 0; x < 4; x++) {
+ vec2 r = (vec2){x + g->current.pos.x, y + g->current.pos.y};
if ((*m)[y][x]) {
if (r.y >= 0) {
g->board[r.x][r.y].occupied = true;
@@ -160,8 +158,7 @@ void hard_drop(cetris_game* g) {
}
}
- /* 2 score for each hard-drop'd cell */
- g->score += 2 * drop_count;
+ g->score += 2 * drop_count; // 2 score for each hard-drop'd cell
update_board(g);
lock_current(g);
@@ -170,9 +167,7 @@ void hard_drop(cetris_game* g) {
void rotate_matrix(cetris_game* g, bool clockwise) {
if (g->game_over || g->next_piece_tick) return;
if (g->current.t == O) return;
-
- u8 x, y, i;
-
+
rstate next = 0;
u8 wall_kick = 0;
switch (g->current.r) {
@@ -213,8 +208,8 @@ void rotate_matrix(cetris_game* g, bool clockwise) {
piece_matrix m;
memset(m, 0, sizeof(piece_matrix));
- for (x = 0; x < 4; x++) {
- for (y = 0; y < 4; y++) {
+ for (u8 x = 0; x < 4; x++) {
+ for (u8 y = 0; y < 4; y++) {
if (g->current.m[y][x]) {
u8 new_x = (clockwise) ? 1 - (y - 2) : 1 + (y - 2);
u8 new_y = (clockwise) ? 2 + (x - 1) : 2 - (x - 1);
@@ -235,7 +230,7 @@ void rotate_matrix(cetris_game* g, bool clockwise) {
vec2 kick;
bool set_current = false;
bool did_kick = false;
- for (i = 0; i < 5; i++) {
+ for (u8 i = 0; i < 5; i++) {
if (g->current.t == I) {
kick = srs_wall_kicks_i[wall_kick][i];
} else {
@@ -260,7 +255,7 @@ void rotate_matrix(cetris_game* g, bool clockwise) {
/* check for tspin */
if (g->current.t == T) {
bool did_tspin = true;
- for (i = 1; i < 5; i++) {
+ for (int i = 1; i < 5; i++) {
g->current.pos.x += basic_movements[i].x;
g->current.pos.y += basic_movements[i].y;
diff --git a/core/src/test.c b/core/src/test.c
index 403823f..bad50d8 100644
--- a/core/src/test.c
+++ b/core/src/test.c
@@ -48,7 +48,7 @@ u8 tspin_no_lines_board[20][10] = {
};
void apply_test_board(cetris_game* g, test t) {
- u8 x, y, (*board)[20][10];
+ u8 (*board)[20][10];
switch (t) {
case TSPIN:
board = &tspin_board;
@@ -59,8 +59,8 @@ void apply_test_board(cetris_game* g, test t) {
default:
return;
}
- for (y = 0; y < CETRIS_BOARD_Y; y++) {
- for (x = 0; x < 10; x++) {
+ for (u8 y = 0; y < CETRIS_BOARD_Y; y++) {
+ for (u8 x = 0; x < 10; x++) {
if ((*board)[y][x]) {
g->board[x][y].occupied = 1;
g->board[x][y].constant = 1;
diff --git a/core/src/types.c b/core/src/types.c
deleted file mode 100644
index 381a14a..0000000
--- a/core/src/types.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include "types.h"
-
-vec2 new_vec2(i8 x, i8 y) {
- vec2 v;
- v.x = x;
- v.y = y;
- return v;
-}