fix: prevent crash due to out-of-bounds when game lags

This commit is contained in:
Khaïs COLIN 2025-08-06 11:24:15 +02:00
parent 1d6d97ef25
commit a18c115ef1
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
7 changed files with 90 additions and 26 deletions

View file

@ -6,79 +6,80 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/05 12:53:06 by kcolin #+# #+# */
/* Updated: 2025/08/05 12:53:06 by kcolin ### ########.fr */
/* Updated: 2025/08/06 11:46:04 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "angle.h"
#include "../consts.h"
#include "../map/collision.h"
void move_player_forward(t_cub3d_data *data)
{
char **map;
t_mapdata *map;
double next_x;
double next_y;
float movspeed;
movspeed = MOVEMENT_SPEED * data->delta;
map = data->map->map;
map = data->map;
next_x = data->player.x + data->player.dir_x * movspeed;
next_y = data->player.y + data->player.dir_y * movspeed;
if (map[(int)data->player.y][(int)next_x] == '0')
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
data->player.x += data->player.dir_x * movspeed;
if (map[(int)next_y][(int)data->player.x] == '0')
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
data->player.y += data->player.dir_y * movspeed;
}
void move_player_backward(t_cub3d_data *data)
{
char **map;
t_mapdata *map;
double next_x;
double next_y;
float movspeed;
movspeed = MOVEMENT_SPEED * data->delta;
map = data->map->map;
map = data->map;
next_x = data->player.x - data->player.dir_x * movspeed;
next_y = data->player.y - data->player.dir_y * movspeed;
if (map[(int)data->player.y][(int)next_x] == '0')
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
data->player.x -= data->player.dir_x * movspeed;
if (map[(int)next_y][(int)data->player.x] == '0')
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
data->player.y -= data->player.dir_y * movspeed;
}
void move_player_strafe_left(t_cub3d_data *data)
{
char **map;
t_mapdata *map;
double next_x;
double next_y;
float movspeed;
movspeed = MOVEMENT_SPEED * data->delta;
map = data->map->map;
map = data->map;
next_x = data->player.x - data->player.plane_x * movspeed;
next_y = data->player.y - data->player.plane_y * movspeed;
if (map[(int)data->player.y][(int)next_x] == '0')
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
data->player.x -= data->player.plane_x * movspeed;
if (map[(int)next_y][(int)data->player.x] == '0')
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
data->player.y -= data->player.plane_y * movspeed;
}
void move_player_strafe_right(t_cub3d_data *data)
{
char **map;
t_mapdata *map;
double next_x;
double next_y;
float movspeed;
movspeed = MOVEMENT_SPEED * data->delta;
map = data->map->map;
map = data->map;
next_x = data->player.x + data->player.plane_x * movspeed;
next_y = data->player.y + data->player.plane_y * movspeed;
if (map[(int)data->player.y][(int)next_x] == '0')
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
data->player.x += data->player.plane_x * movspeed;
if (map[(int)next_y][(int)data->player.x] == '0')
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
data->player.y += data->player.plane_y * movspeed;
}