1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-12-22 12:33:26 +01:00

Some minor fixes for game module

This commit is contained in:
jfreegman 2021-06-28 13:54:25 -04:00
parent 321f694bb8
commit 8dfd009e0e
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
10 changed files with 80 additions and 49 deletions

2
.gitignore vendored
View File

@ -18,3 +18,5 @@ build/*.o
build/*.d
apidoc/python/build
*.vim
*.tox
*.nvim*

View File

@ -151,7 +151,4 @@ texinfo_documents = [
(master_doc, 'toxic_api', 'toxic_api Documentation',
author, 'toxic_api', 'One line description of project.',
'Miscellaneous'),
]
]

View File

@ -771,7 +771,7 @@ void chat_onGameInvite(ToxWindow *self, Tox *m, uint32_t friend_number, const ui
return;
}
if (length < GAME_PACKET_HEADER_SIZE) {
if (length < GAME_PACKET_HEADER_SIZE || length > GAME_MAX_DATA_SIZE) {
return;
}

View File

@ -162,13 +162,17 @@ void game_kill(ToxWindow *self)
{
GameData *game = self->game;
if (game->cb_game_kill) {
game->cb_game_kill(game, game->cb_game_kill_data);
if (game) {
if (game->cb_game_kill) {
game->cb_game_kill(game, game->cb_game_kill_data);
}
delwin(game->window);
free(game->messages);
free(game);
}
delwin(game->window);
free(game->messages);
free(game);
kill_notifs(self->active_box);
del_window(self);
}
@ -746,7 +750,7 @@ bool game_onKey(ToxWindow *self, Tox *m, wint_t key, bool is_printable)
return true;
}
if (!game->is_multiplayer && key == KEY_F(2)) {
if (key == KEY_F(2) && !game->is_multiplayer) {
game_toggle_pause(self->game);
return true;
}
@ -814,6 +818,8 @@ void game_onPacket(ToxWindow *self, Tox *m, uint32_t friendnumber, const uint8_t
}
if (data[0] != GAME_NETWORKING_VERSION) {
fprintf(stderr, "Game packet rejected: wrong networking version (got %d, expected %d)\n", data[0],
GAME_NETWORKING_VERSION);
return;
}
@ -1081,7 +1087,7 @@ void game_set_cb_on_packet(GameData *game, cb_game_on_packet *func, void *cb_dat
/*
* Wraps `packet` in a header comprised of the custom packet type, game type and game id.
*/
static int game_wrap_packet(const GameData *game, uint8_t *packet, size_t size, GamePacketType packet_type)
static int game_packet_wrap(const GameData *game, uint8_t *packet, size_t size, GamePacketType packet_type)
{
if (size < GAME_PACKET_HEADER_SIZE + 1) {
return -1;
@ -1100,7 +1106,7 @@ static int game_wrap_packet(const GameData *game, uint8_t *packet, size_t size,
return 0;
}
int game_send_packet(const GameData *game, const uint8_t *data, size_t length, GamePacketType packet_type)
int game_packet_send(const GameData *game, const uint8_t *data, size_t length, GamePacketType packet_type)
{
if (length > GAME_MAX_DATA_SIZE) {
return -1;
@ -1108,7 +1114,7 @@ int game_send_packet(const GameData *game, const uint8_t *data, size_t length, G
uint8_t packet[GAME_MAX_PACKET_SIZE];
if (game_wrap_packet(game, packet, sizeof(packet), packet_type) == -1) {
if (game_packet_wrap(game, packet, sizeof(packet), packet_type) == -1) {
return -1;
}
@ -1124,5 +1130,5 @@ int game_send_packet(const GameData *game, const uint8_t *data, size_t length, G
return -1;
}
return -0;
return 0;
}

View File

@ -394,7 +394,7 @@ void game_kill(ToxWindow *self);
*
* `packet_type` should be GP_Invite for an invite packet or GP_Data for all other game data.
*/
int game_send_packet(const GameData *game, const uint8_t *data, size_t length, GamePacketType packet_type);
int game_packet_send(const GameData *game, const uint8_t *data, size_t length, GamePacketType packet_type);
#endif // GAME_BASE

View File

@ -42,7 +42,7 @@
#define CENT_MAX_NUM_SEGMENTS 12
/* Get a free life every time we get this many points. Needs to be > the most points we can get in a single shot. */
#define CENT_SCORE_ONE_UP 5000
#define CENT_SCORE_ONE_UP 7000
/* Max number of lives we can have */
#define CENT_MAX_LIVES 6
@ -53,6 +53,9 @@
/* Max speed of an enemy agent */
#define CENT_MAX_ENEMY_AGENT_SPEED 8
/* Determines the overall speed of the game per game_set_update_interval() */
#define CENT_GAME_UPDATE_INTERVAL 14
/* How often a head that reaches the bottom can repdoduce */
#define CENT_REPRODUCE_TIMEOUT 10
@ -64,7 +67,7 @@
#define CENT_BULLET_COLOUR YELLOW
#define CENT_BULLET_ATTR A_BOLD
#define CENT_BULLET_CHAR '|'
#define CENT_BULLET_SPEED 150
#define CENT_BULLET_SPEED 300
#define CENT_BLASTER_ATTR A_BOLD
#define CENT_BLASTER_CHAR 'U'
@ -1730,7 +1733,7 @@ int centipede_initialize(GameData *game)
game_show_lives(game, true);
game_show_high_score(game, true);
game_increment_level(game);
game_set_update_interval(game, 10);
game_set_update_interval(game, CENT_GAME_UPDATE_INTERVAL);
if (cent_init_state(game, state) == -1) {
free(state);

View File

@ -826,9 +826,7 @@ static bool chess_mock_move_valid(ChessState *state, const Player *player, Tile
chess_copy_piece(&to->piece, &from->piece);
from->piece.type = NoPiece;
if (chess_player_in_check(state, player)) {
in_check = true;;
}
in_check = chess_player_in_check(state, player);
from->piece.type = from_piece.type;
chess_copy_piece(&to->piece, &to_piece);
@ -1079,9 +1077,7 @@ static void chess_update_state(ChessState *state, Player *self, Player *other, c
self->in_check = false;
if (chess_player_in_check(state, other)) {
other->in_check = true;
}
other->in_check = chess_player_in_check(state, other);
state->message_length = 0;
state->black_to_move ^= 1;
@ -1813,7 +1809,7 @@ void chess_cb_kill(GameData *game, void *cb_data)
static int chess_handle_opponent_move_packet(const GameData *game, ChessState *state, const uint8_t *data,
size_t length)
{
if (length != CHESS_PACKET_MOVE_SIZE || data == NULL) {
if (length < CHESS_PACKET_MOVE_SIZE || data == NULL) {
return -1;
}
@ -1845,7 +1841,7 @@ static int chess_handle_opponent_move_packet(const GameData *game, ChessState *s
}
if (chess_try_move_opponent(state, from_tile, to_tile) != 0) {
fprintf(stderr, "opponent tried to make an illegal move: %c%d-%c%d\n", from_l, from_n, to_l, to_n);
fprintf(stderr, "Chess opponent tried to make an illegal move: %c%d-%c%d\n", from_l, from_n, to_l, to_n);
return -1;
}
@ -1887,7 +1883,7 @@ static void chess_cb_on_packet(GameData *game, const uint8_t *data, size_t lengt
return;
}
if (!cb_data) {
if (cb_data == NULL) {
return;
}
@ -1928,7 +1924,7 @@ static void chess_cb_on_packet(GameData *game, const uint8_t *data, size_t lengt
default: {
fprintf(stderr, "Got unknown chess packet type: %d\n", type);
break;
return;
}
}
@ -2044,7 +2040,7 @@ static int chess_packet_send_resign(const GameData *game)
uint8_t data[1];
data[0] = CHESS_PACKET_RESIGN;
if (game_send_packet(game, data, 1, GP_Data) == -1) {
if (game_packet_send(game, data, 1, GP_Data) == -1) {
return -1;
}
@ -2060,7 +2056,7 @@ static int chess_packet_send_move(const GameData *game, const Tile *from, const
data[3] = to->chess_coords.L;
data[4] = to->chess_coords.N;
if (game_send_packet(game, data, 5, GP_Data) == -1) {
if (game_packet_send(game, data, 5, GP_Data) == -1) {
return -1;
}
@ -2073,7 +2069,7 @@ static int chess_packet_send_invite(const GameData *game, bool self_is_white)
data[0] = CHESS_PACKET_INIT_SEND_INVITE;
data[1] = self_is_white ? Black : White;
if (game_send_packet(game, data, 2, GP_Invite) == -1) {
if (game_packet_send(game, data, 2, GP_Invite) == -1) {
return -1;
}
@ -2085,7 +2081,7 @@ static int chess_packet_send_accept(const GameData *game)
uint8_t data[1];
data[0] = CHESS_PACKET_INIT_ACCEPT_INVITE;
if (game_send_packet(game, data, 1, GP_Data) == -1) {
if (game_packet_send(game, data, 1, GP_Data) == -1) {
return -1;
}
@ -2160,4 +2156,3 @@ int chess_initialize(GameData *game, const uint8_t *init_data, size_t length)
return 0;
}

View File

@ -104,22 +104,28 @@ void game_util_move_coords(Direction direction, Coords *coords)
{
switch (direction) {
case NORTH: {
--(coords->y);
if (coords->y > 0) {
--(coords->y);
}
break;
}
case SOUTH: {
++(coords->y);
++(coords->y); // Will rollover if you do something stupid
break;
}
case EAST: {
++(coords->x);
++(coords->x); // Will rollover if you do something stupid
break;
}
case WEST: {
--(coords->x);
if (coords->x > 0) {
--(coords->x);
}
break;
}

View File

@ -457,7 +457,12 @@ void del_window(ToxWindow *w)
refresh();
if (num_active_windows > 0) {
if (active_window_index == 2) { // if closing current window would bring us to friend list
set_next_window(-1); // skip back to the home window instead. FIXME: magic numbers
}
set_next_window(-1);
--num_active_windows;
}
}
@ -519,6 +524,12 @@ void on_window_resize(void)
getmaxyx(w->window, y2, x2);
if (y2 <= 0 || x2 <= 0) {
fprintf(stderr, "Failed to resize game window: max_x: %d, max_y: %d\n", x2, y2);
delwin(w->window);
continue;
}
w->window_bar = subwin(w->window, WINDOW_BAR_HEIGHT, COLS, LINES - 2, 0);
w->game->window = subwin(w->window, y2 - CHATBOX_HEIGHT - WINDOW_BAR_HEIGHT, x2, 0, 0);
continue;
@ -549,7 +560,7 @@ void on_window_resize(void)
if (y2 <= 0 || x2 <= 0) {
fprintf(stderr, "Failed to resize window: max_x: %d, max_y: %d\n", x2, y2);
delwin(w->window);
return;
continue;
}
if (w->show_peerlist) {
@ -790,7 +801,6 @@ void draw_active_window(Tox *m)
if (ch == user_settings->key_next_tab || ch == user_settings->key_prev_tab) {
set_next_window(ch);
ch = KEY_F(2);
}
a->onKey(a, m, ch, false);
@ -892,18 +902,30 @@ void kill_all_windows(Tox *m)
continue;
}
if (w->type == WINDOW_TYPE_CHAT) {
kill_chat_window(w, m);
} else if (w->type == WINDOW_TYPE_CONFERENCE) {
free_conference(w, w->num);
}
switch (w->type) {
case WINDOW_TYPE_CHAT: {
kill_chat_window(w, m);
break;
}
case WINDOW_TYPE_CONFERENCE: {
free_conference(w, w->num);
break;
}
#ifdef GAMES
else if (w->type == WINDOW_TYPE_GAME) {
game_kill(w);
}
case WINDOW_TYPE_GAME: {
game_kill(w);
break;
}
#endif // GAMES
default: {
break;
}
}
}
/* TODO: use enum instead of magic indices */

View File

@ -310,5 +310,5 @@ void draw_window_bar(ToxWindow *self);
call at least once per second */
void refresh_inactive_windows(void);
#endif // WINWDOWS_H
#endif // WINDOWS_H