1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-22 21:43:02 +01:00

Begin implementing chess

This commit is contained in:
jfreegman 2020-12-19 15:40:00 -05:00
parent a623976a0e
commit 60bdcf0ba5
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
12 changed files with 1752 additions and 34 deletions

1
.gitignore vendored
View File

@ -17,3 +17,4 @@ build/toxic
build/*.o build/*.o
build/*.d build/*.d
apidoc/python/build apidoc/python/build
*.vim

View File

@ -13,9 +13,9 @@ LDFLAGS ?=
LDFLAGS += ${USER_LDFLAGS} LDFLAGS += ${USER_LDFLAGS}
OBJ = autocomplete.o avatars.o bootstrap.o chat.o chat_commands.o configdir.o curl_util.o execute.o OBJ = autocomplete.o avatars.o bootstrap.o chat.o chat_commands.o configdir.o curl_util.o execute.o
OBJ += file_transfers.o friendlist.o game_base.o game_centipede.o game_util.o game_snake.o global_commands.o conference_commands.o OBJ += file_transfers.o friendlist.o game_base.o game_centipede.o game_chess.o game_util.o game_snake.o
OBJ += conference.o help.o input.o line_info.o log.o message_queue.o misc_tools.o name_lookup.o notify.o OBJ += global_commands.o conference_commands.o conference.o help.o input.o line_info.o log.o message_queue.o
OBJ += prompt.o qr_code.o settings.o term_mplex.o toxic.o toxic_strings.o windows.o OBJ += misc_tools.o name_lookup.o notify.o prompt.o qr_code.o settings.o term_mplex.o toxic.o toxic_strings.o windows.o
# Check if debug build is enabled # Check if debug build is enabled
RELEASE := $(shell if [ -z "$(ENABLE_RELEASE)" ] || [ "$(ENABLE_RELEASE)" = "0" ] ; then echo disabled ; else echo enabled ; fi) RELEASE := $(shell if [ -z "$(ENABLE_RELEASE)" ] || [ "$(ENABLE_RELEASE)" = "0" ] ; then echo disabled ; else echo enabled ; fi)

View File

@ -27,6 +27,7 @@
#include "game_centipede.h" #include "game_centipede.h"
#include "game_base.h" #include "game_base.h"
#include "game_chess.h"
#include "game_snake.h" #include "game_snake.h"
#include "line_info.h" #include "line_info.h"
#include "misc_tools.h" #include "misc_tools.h"
@ -44,7 +45,6 @@
#define GAME_MAX_UPDATE_INTERVAL 50 #define GAME_MAX_UPDATE_INTERVAL 50
#define GAME_BORDER_COLOUR BAR_TEXT
/* Determines if window is large enough for a respective window type */ /* Determines if window is large enough for a respective window type */
#define WINDOW_SIZE_LARGE_SQUARE_VALID(max_x, max_y)((((max_y) - 4) >= (GAME_MAX_SQUARE_Y))\ #define WINDOW_SIZE_LARGE_SQUARE_VALID(max_x, max_y)((((max_y) - 4) >= (GAME_MAX_SQUARE_Y))\
@ -69,6 +69,7 @@ struct GameList {
static struct GameList game_list[] = { static struct GameList game_list[] = {
{ "centipede", GT_Centipede }, { "centipede", GT_Centipede },
{ "chess", GT_Chess },
{ "snake", GT_Snake }, { "snake", GT_Snake },
{ NULL, GT_Invalid }, { NULL, GT_Invalid },
}; };
@ -120,7 +121,7 @@ void game_list_print(ToxWindow *self)
const char *name = NULL; const char *name = NULL;
for (size_t i = 0; (name = game_list[i].name); ++i) { for (size_t i = 0; (name = game_list[i].name); ++i) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%d: %s", i + 1, name); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%zu: %s", i + 1, name);
} }
} }
@ -178,6 +179,11 @@ static int game_initialize_type(GameData *game)
break; break;
} }
case GT_Chess: {
ret = chess_initialize(game);
break;
}
default: { default: {
break; break;
} }
@ -495,18 +501,23 @@ static void game_draw_status(const GameData *game, const int max_x, const int ma
int y = ((max_y - game->game_max_y) / 2) - 1; int y = ((max_y - game->game_max_y) / 2) - 1;
wattron(win, A_BOLD); wattron(win, A_BOLD);
mvwprintw(win, y, x, "Score: %zu", game->score);
if (game->show_score) {
mvwprintw(win, y, x, "Score: %ld", game->score);
}
if (game->show_high_score) {
mvwprintw(win, y + game->game_max_y + 2, x, "High Score: %zu", game->high_score); mvwprintw(win, y + game->game_max_y + 2, x, "High Score: %zu", game->high_score);
}
x = ((max_x / 2) + (game->game_max_x / 2)) - 7; x = ((max_x / 2) + (game->game_max_x / 2)) - 7;
if (game->level > 0) { if (game->show_level) {
mvwprintw(win, y, x, "Level: %zu", game->level); mvwprintw(win, y, x, "Level: %zu", game->level);
} }
if (game->lives >= 0) { if (game->show_lives) {
mvwprintw(win, y + game->game_max_y + 2, x, "Lives: %zu", game->lives); mvwprintw(win, y + game->game_max_y + 2, x, "Lives: %d", game->lives);
} }
wattroff(win, A_BOLD); wattroff(win, A_BOLD);
@ -617,11 +628,11 @@ void game_onDraw(ToxWindow *self, Tox *m)
{ {
UNUSED_VAR(m); // Note: This function is not thread safe if we ever need to use `m` UNUSED_VAR(m); // Note: This function is not thread safe if we ever need to use `m`
curs_set(0);
game_draw_help_bar(self->window); game_draw_help_bar(self->window);
draw_window_bar(self); draw_window_bar(self);
curs_set(0);
GameData *game = self->game; GameData *game = self->game;
int max_x; int max_x;
@ -663,8 +674,6 @@ void game_onDraw(ToxWindow *self, Tox *m)
} }
game_draw_messages(game, true); game_draw_messages(game, true);
wnoutrefresh(self->window);
} }
bool game_onKey(ToxWindow *self, Tox *m, wint_t key, bool is_printable) bool game_onKey(ToxWindow *self, Tox *m, wint_t key, bool is_printable)
@ -830,6 +839,26 @@ int game_x_left_bound(const GameData *game)
return ((max_x - game->game_max_x) / 2) + 1; return ((max_x - game->game_max_x) / 2) + 1;
} }
void game_show_score(GameData *game, bool show_score)
{
game->show_score = show_score;
}
void game_show_high_score(GameData *game, bool show_high_score)
{
game->show_high_score = show_high_score;
}
void game_show_lives(GameData *game, bool show_lives)
{
game->show_lives = show_lives;
}
void game_show_level(GameData *game, bool show_level)
{
game->show_level = show_level;
}
void game_update_score(GameData *game, long int points) void game_update_score(GameData *game, long int points)
{ {
game->score += points; game->score += points;
@ -851,7 +880,6 @@ void game_increment_level(GameData *game)
void game_update_lives(GameData *game, int lives) void game_update_lives(GameData *game, int lives)
{ {
fprintf(stderr, "%d\n", lives);
game->lives += lives; game->lives += lives;
} }
@ -912,3 +940,4 @@ void game_set_cb_on_pause(GameData *game, cb_game_pause *func, void *cb_data)
game->cb_game_pause = func; game->cb_game_pause = func;
game->cb_game_pause_data = cb_data; game->cb_game_pause_data = cb_data;
} }

View File

@ -31,6 +31,8 @@
#include "game_util.h" #include "game_util.h"
#include "windows.h" #include "windows.h"
#define GAME_BORDER_COLOUR BAR_TEXT
/* Max size of a default size square game window */ /* Max size of a default size square game window */
#define GAME_MAX_SQUARE_Y 26 #define GAME_MAX_SQUARE_Y 26
#define GAME_MAX_SQUARE_X (GAME_MAX_SQUARE_Y * 2) #define GAME_MAX_SQUARE_X (GAME_MAX_SQUARE_Y * 2)
@ -44,7 +46,7 @@
#define GAME_MAX_RECT_X (GAME_MAX_RECT_Y * 4) #define GAME_MAX_RECT_X (GAME_MAX_RECT_Y * 4)
/* Max size of a small rectangle game window */ /* Max size of a small rectangle game window */
#define GAME_MAX_RECT_Y_SMALL 12 #define GAME_MAX_RECT_Y_SMALL 14
#define GAME_MAX_RECT_X_SMALL (GAME_MAX_RECT_Y_SMALL * 4) #define GAME_MAX_RECT_X_SMALL (GAME_MAX_RECT_Y_SMALL * 4)
/* Maximum length of a game message set with game_set_message() */ /* Maximum length of a game message set with game_set_message() */
@ -60,13 +62,13 @@ typedef void cb_game_key_press(GameData *game, int key, void *cb_data);
typedef enum GameWindowShape { typedef enum GameWindowShape {
GW_ShapeSquare = 0, GW_ShapeSquare = 0u,
GW_ShapeRectangle, GW_ShapeRectangle,
GW_ShapeInvalid, GW_ShapeInvalid,
} GameWindowShape; } GameWindowShape;
typedef enum GameStatus { typedef enum GameStatus {
GS_None = 0, GS_None = 0u,
GS_Paused, GS_Paused,
GS_Running, GS_Running,
GS_Finished, GS_Finished,
@ -74,8 +76,9 @@ typedef enum GameStatus {
} GameStatus; } GameStatus;
typedef enum GameType { typedef enum GameType {
GT_Snake = 0, GT_Centipede = 0u,
GT_Centipede, GT_Chess,
GT_Snake,
GT_Invalid, GT_Invalid,
} GameType; } GameType;
@ -103,6 +106,11 @@ struct GameData {
GameStatus status; GameStatus status;
GameType type; GameType type;
bool show_lives;
bool show_score;
bool show_high_score;
bool show_level;
GameMessage *messages; GameMessage *messages;
size_t messages_size; size_t messages_size;
@ -196,7 +204,7 @@ bool game_coordinates_in_bounds(const GameData *game, int x, int y);
void game_random_coords(const GameData *game, Coords *coords); void game_random_coords(const GameData *game, Coords *coords);
/* /*
*Gets the current max dimensions of the game window. * Gets the current max dimensions of the game window.
*/ */
void game_max_x_y(const GameData *game, int *x, int *y); void game_max_x_y(const GameData *game, int *x, int *y);
@ -208,6 +216,14 @@ int game_y_top_bound(const GameData *game);
int game_x_right_bound(const GameData *game); int game_x_right_bound(const GameData *game);
int game_x_left_bound(const GameData *game); int game_x_left_bound(const GameData *game);
/*
* Toggle whether the respective game info is shown around the game window.
*/
void game_show_score(GameData *game, bool show_score);
void game_show_high_score(GameData *game, bool show_high_score);
void game_show_lives(GameData *game, bool show_lives);
void game_show_level(GameData *game, bool show_level);
/* /*
* Updates game score. * Updates game score.
*/ */

View File

@ -53,7 +53,7 @@
/* Max speed of an enemy agent */ /* Max speed of an enemy agent */
#define CENT_MAX_ENEMY_AGENT_SPEED 8 #define CENT_MAX_ENEMY_AGENT_SPEED 8
/* How often a head that reachest he bottom can repdoduce */ /* How often a head that reaches the bottom can repdoduce */
#define CENT_REPRODUCE_TIMEOUT 10 #define CENT_REPRODUCE_TIMEOUT 10
#define CENT_CENTIPEDE_DEFAULT_SPEED 5 #define CENT_CENTIPEDE_DEFAULT_SPEED 5
@ -194,7 +194,7 @@ typedef struct CentState {
#define CENT_LEVEL_COLOURS_SIZE 19 #define CENT_LEVEL_COLOURS_SIZE 19
const static int cent_level_colours[] = { static const int cent_level_colours[] = {
RED, RED,
CYAN, CYAN,
MAGENTA, MAGENTA,
@ -487,6 +487,8 @@ static int cent_birth_centipede(const GameData *game, CentState *state, size_t l
new_head->display_char = CENT_CENTIPEDE_HEAD_CHAR; new_head->display_char = CENT_CENTIPEDE_HEAD_CHAR;
new_head->prev = NULL; new_head->prev = NULL;
centipedes->heads[head_idx] = new_head;
Segment *prev = new_head; Segment *prev = new_head;
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
@ -510,8 +512,6 @@ static int cent_birth_centipede(const GameData *game, CentState *state, size_t l
prev = new_seg; prev = new_seg;
} }
centipedes->heads[head_idx] = new_head;
return 0; return 0;
} }
@ -1077,8 +1077,7 @@ static void cent_set_head_direction(CentState *state, Segment *head, int y_botto
} }
/* /*
* If a head has reached the bottom it reproduces (spawns an additional head) every time it * If a head has reached the bottom it reproduces (spawns an additional head) on a timer.
* makes two round-trips across the screen.
*/ */
static void cent_do_reproduce(const GameData *game, CentState *state, Segment *head, int x_right, int x_left, static void cent_do_reproduce(const GameData *game, CentState *state, Segment *head, int x_right, int x_left,
int y_bottom) int y_bottom)
@ -1510,6 +1509,10 @@ void cent_cb_update_game_state(GameData *game, void *cb_data)
CentState *state = (CentState *)cb_data; CentState *state = (CentState *)cb_data;
if (state->game_over) {
return;
}
TIME_MS cur_time = get_time_millis(); TIME_MS cur_time = get_time_millis();
cent_blaster_collision_check(game, state); cent_blaster_collision_check(game, state);
@ -1720,6 +1723,10 @@ int centipede_initialize(GameData *game)
return -1; return -1;
} }
game_show_level(game, true);
game_show_score(game, true);
game_show_lives(game, true);
game_show_high_score(game, true);
game_increment_level(game); game_increment_level(game);
game_set_update_interval(game, 10); game_set_update_interval(game, 10);

1631
src/game_chess.c Normal file

File diff suppressed because it is too large Load Diff

30
src/game_chess.h Normal file
View File

@ -0,0 +1,30 @@
/* game_chess.h
*
*
* Copyright (C) 2020 Toxic All Rights Reserved.
*
* This file is part of Toxic.
*
* Toxic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toxic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef GAME_CHESS
#define GAME_CHESS
#include "game_base.h"
int chess_initialize(GameData *game);
#endif // GAME_CHESS

View File

@ -886,7 +886,10 @@ int snake_initialize(GameData *game)
state->last_powerup_time = get_unix_time(); state->last_powerup_time = get_unix_time();
game_update_lives(game, -1); game_show_level(game, true);
game_show_score(game, true);
game_show_high_score(game, true);
game_increment_level(game); game_increment_level(game);
game_set_update_interval(game, SNAKE_DEFAULT_UPDATE_INTERVAL); game_set_update_interval(game, SNAKE_DEFAULT_UPDATE_INTERVAL);
game_random_coords(game, &state->food); game_random_coords(game, &state->food);

View File

@ -32,7 +32,7 @@ typedef struct Coords {
// don't change these // don't change these
typedef enum Direction { typedef enum Direction {
NORTH = 0, NORTH = 0u,
SOUTH = 1, SOUTH = 1,
EAST = 3, EAST = 3,
WEST = 4, WEST = 4,
@ -60,7 +60,7 @@ typedef time_t TIME_S;
/* /*
* Return true if dir is a valid Direction. * Return true if dir is a valid Direction.
*/ */
#define GAME_UTIL_DIRECTION_VALID(dir)(((dir) >= 0) && ((dir) < (INVALID_DIRECTION))) #define GAME_UTIL_DIRECTION_VALID(dir)((dir) < (INVALID_DIRECTION))
/* /*
* Returns cardinal direction mapped to `key`. * Returns cardinal direction mapped to `key`.

View File

@ -376,10 +376,10 @@ static void init_term(void)
init_pair(YELLOW, COLOR_YELLOW, bg_color); init_pair(YELLOW, COLOR_YELLOW, bg_color);
init_pair(MAGENTA, COLOR_MAGENTA, bg_color); init_pair(MAGENTA, COLOR_MAGENTA, bg_color);
init_pair(BLACK, COLOR_BLACK, COLOR_BLACK); init_pair(BLACK, COLOR_BLACK, COLOR_BLACK);
init_pair(BLUE_BLACK, COLOR_BLUE, COLOR_BLACK);
init_pair(WHITE_BLUE, COLOR_WHITE, COLOR_BLUE); init_pair(WHITE_BLUE, COLOR_WHITE, COLOR_BLUE);
init_pair(BLACK_WHITE, COLOR_BLACK, COLOR_WHITE); init_pair(BLACK_WHITE, COLOR_BLACK, COLOR_WHITE);
init_pair(WHITE_BLACK, COLOR_WHITE, COLOR_BLACK); init_pair(WHITE_BLACK, COLOR_WHITE, COLOR_BLACK);
init_pair(WHITE_GREEN, COLOR_WHITE, COLOR_GREEN);
init_pair(BLACK_BG, COLOR_BLACK, bar_bg_color); init_pair(BLACK_BG, COLOR_BLACK, bar_bg_color);
init_pair(PURPLE_BG, COLOR_MAGENTA, bar_bg_color); init_pair(PURPLE_BG, COLOR_MAGENTA, bar_bg_color);
init_pair(BAR_TEXT, bar_fg_color, bar_bg_color); init_pair(BAR_TEXT, bar_fg_color, bar_bg_color);

View File

@ -53,10 +53,10 @@ typedef enum {
YELLOW, YELLOW,
MAGENTA, MAGENTA,
BLACK, BLACK,
BLUE_BLACK,
BLACK_WHITE, BLACK_WHITE,
WHITE_BLACK, WHITE_BLACK,
WHITE_BLUE, WHITE_BLUE,
WHITE_GREEN,
BAR_TEXT, BAR_TEXT,
STATUS_ONLINE, STATUS_ONLINE,
BAR_ACCENT, BAR_ACCENT,
@ -290,4 +290,5 @@ void draw_window_bar(ToxWindow *self);
call at least once per second */ call at least once per second */
void refresh_inactive_windows(void); void refresh_inactive_windows(void);
#endif /* WINDOWS_H */ #endif // WINWDOWS_H