From 1842a21f7187f32982f9f19623affbc48b4f4573 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Sun, 8 Mar 2020 00:26:09 +0100
Subject: I gotta figure out

---
 Makefile              |  1 +
 inc/cub3d.h           | 10 ++++++++--
 inc/cub3d_structs.h   | 13 +++++++++++-
 src/ft_darken_rgb.c   | 16 ++++++++-------
 src/ft_draw_verline.c | 14 +++++++++++++
 src/ft_floor_cast.c   | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/ft_init_lists.c   |  1 +
 src/ft_init_map.c     |  2 ++
 src/ft_raycasting.c   |  1 +
 src/ft_warp_level.c   |  1 +
 10 files changed, 104 insertions(+), 10 deletions(-)
 create mode 100644 src/ft_floor_cast.c

diff --git a/Makefile b/Makefile
index e943d9d..59de80d 100644
--- a/Makefile
+++ b/Makefile
@@ -75,6 +75,7 @@ SRCS_NAME	+= ft_save_to_bmp.c
 SRCS_NAME	+= ft_treat_args.c
 SRCS_NAME	+= ft_init_funptr.c
 SRCS_NAME	+= ft_init_bmp.c
+SRCS_NAME	+= ft_floor_cast.c
 #--------------------------------------------------------------------------------------------------#
 SRCS		= $(addprefix ${SRCS_DIR},${SRCS_NAME})
 #--------------------------------------------------------------------------------------------------#
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 063b06f..4151daa 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -115,6 +115,14 @@ uint8_t			ft_check_map_arg(int argc, const char *argv[]);
 uint8_t			ft_use_args(int argc, const char *argv[],
 							char *const envp[], t_cub *clist);
 
+/*
+** ====== RAYCAST ======
+*/
+
+void			ft_castray(t_cub *cl);
+void			ft_floor_cast(t_cub *cl);
+void			ft_detect(t_cub *cl);
+
 /*
 ** ====== OTHER ======
 */
@@ -122,8 +130,6 @@ uint8_t			ft_use_args(int argc, const char *argv[],
 void			ft_set_minimap_scale(t_cub *clist);
 void			ft_enable_music(t_cub *cl);
 void			ft_music_fork(char **mcmd_words, char *const envp[]);
-void			ft_detect(t_cub *cl);
-void			ft_castray(t_cub *cl);
 int8_t			ft_save_to_bmp(t_cub *cl);
 int				ft_error(uint8_t retval, const char *errmsg, t_cub *clist);
 uint8_t			ft_free_words(char **words);
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h
index d392f0c..ecbbaf3 100644
--- a/inc/cub3d_structs.h
+++ b/inc/cub3d_structs.h
@@ -16,7 +16,7 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <stdint.h>
-#include <pthread.h>
+#include <sys/types.h>
 
 typedef struct			s_win
 {
@@ -108,6 +108,7 @@ typedef struct			s_player
 {
 	float				pos_x;
 	float				pos_y;
+	float				pos_z;
 	float				start_x;
 	float				start_y;
 	float				dir_x;
@@ -125,10 +126,18 @@ typedef struct			s_ray
 	float				y_ray_pos;
 	float				x_ray_dir;
 	float				y_ray_dir;
+	float				x_ray_dir_bis;
+	float				y_ray_dir_bis;
 	float				x_side_dist;
 	float				y_side_dist;
 	float				x_delta_dist;
 	float				y_delta_dist;
+	float				floor_x;
+	float				floor_y;
+	int16_t				ceil_x;
+	int16_t				ceil_y;
+	float				row_dist;
+	float				ceil_dist;
 	int16_t				wall_t;
 	int16_t				wall_b;
 	uint8_t				side;
@@ -156,6 +165,8 @@ typedef struct			s_map
 	char				**mcmd_words;
 	int8_t				x_step;
 	int8_t				y_step;
+	int8_t				x_floor_step;
+	int8_t				y_floor_step;
 	size_t				map_w;
 	size_t				map_h;
 	size_t				mapl_len;
diff --git a/src/ft_darken_rgb.c b/src/ft_darken_rgb.c
index 564dfbd..7f5e12a 100644
--- a/src/ft_darken_rgb.c
+++ b/src/ft_darken_rgb.c
@@ -12,21 +12,23 @@
 
 #include <cub3d.h>
 #include <stdint.h>
-#include <stdio.h>
 
 uint32_t
 	ft_darken_ceil(t_rgb rgb, int32_t y, t_cub *cl)
 {
 	t_rgb		darker;
+	float		dist;
 	float		calc;
 
 	darker = rgb;
-	calc = ((float)(y - (75000.0 / cl->wlist.y_size))
-			/ (cl->wlist.y_size / 2));
-	calc = (calc < 0) ? (0) : (calc);
-	darker.r *= 1 - calc;
-	darker.g *= 1 - calc;
-	darker.b *= 1 - calc;
+	(void)y;
+	dist = (cl->rlist.ceil_dist > 0) ? (cl->rlist.ceil_dist) : (0.0001);
+	calc = dist * 0.4;
+	calc = (calc >= 255) ? (255) : (calc);
+	calc = (calc < 1) ? (1) : (calc);
+	darker.r /= calc;
+	darker.g /= calc;
+	darker.b /= calc;
 	return (ft_rgb_to_hex(darker));
 }
 
diff --git a/src/ft_draw_verline.c b/src/ft_draw_verline.c
index 18f7cfe..6fa0d1e 100644
--- a/src/ft_draw_verline.c
+++ b/src/ft_draw_verline.c
@@ -40,6 +40,19 @@ static void
 	}
 }
 
+#include <stdio.h>
+static void
+	ft_calc_ceil_dist(int32_t y, t_ray *rl, t_cub *cl)
+{
+	int16_t	p;
+
+	p = y - cl->wlist.y_size / 2;
+	cl->plist.pos_z = 0.5 * cl->wlist.y_size;
+	rl->ceil_dist = cl->plist.pos_z / p;
+	rl->ceil_dist = (rl->ceil_dist > 0) ? (rl->ceil_dist) : (-rl->ceil_dist);
+	printf("%f\n", rl->ceil_dist);
+}
+
 int8_t
 	ft_draw_verline(t_cub *cl, int32_t x, int32_t y, int32_t y2)
 {
@@ -48,6 +61,7 @@ int8_t
 
 	(y < 0) ? (y = 0) : 0;
 	(y2 < 0) ? (y2 = 0) : 0;
+	ft_calc_ceil_dist(y, &cl->rlist, cl);
 	ft_draw_ceil(cl, y, x);
 	(cl->rlist.line_h <= 0) ? (cl->rlist.line_h = 1) : 0;
 	while (y < y2)
diff --git a/src/ft_floor_cast.c b/src/ft_floor_cast.c
new file mode 100644
index 0000000..c82163a
--- /dev/null
+++ b/src/ft_floor_cast.c
@@ -0,0 +1,55 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_raycasting.c                                    :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2020/02/14 17:22:57 by rbousset          #+#    #+#             */
+/*   Updated: 2020/02/14 17:23:42 by rbousset         ###   ########lyon.fr   */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <cub3d.h>
+#include <stdint.h>
+
+static void
+	ft_initray(uint16_t y, t_ray *rl, t_player *pl, t_cub *cl)
+{
+	int16_t	p;
+
+	rl->x_ray_dir = pl->dir_x - pl->plane_x;
+	rl->y_ray_dir = pl->dir_y - pl->plane_y;
+	rl->x_ray_dir_bis = pl->dir_x + pl->plane_x;
+	rl->y_ray_dir_bis = pl->dir_y + pl->plane_y;
+	p = y - cl->wlist.y_size / 2;
+	pl->pos_z = 0.5 * cl->wlist.y_size;
+	rl->row_dist = pl->pos_z / p;
+	cl->mlist.x_floor_step = rl->row_dist *
+		(rl->x_ray_dir_bis - rl->x_ray_dir) / cl->wlist.x_size;
+	cl->mlist.y_floor_step = rl->row_dist *
+		(rl->y_ray_dir_bis - rl->y_ray_dir) / cl->wlist.x_size;
+	rl->floor_x = pl->pos_x + rl->row_dist * rl->x_ray_dir;
+	rl->floor_y = pl->pos_y + rl->row_dist * rl->y_ray_dir;
+}
+
+void
+	ft_floor_cast(t_cub *cl)
+{
+	uint16_t	y;
+	uint16_t	x;
+
+	y = 0;
+	while (y < cl->wlist.y_size)
+	{
+		ft_initray(y, &cl->rlist, &cl->plist, cl);
+		x = 1;
+		while (x < cl->wlist.x_size)
+		{
+			cl->rlist.ceil_x = (int16_t)cl->rlist.floor_x;
+			cl->rlist.ceil_y = (int16_t)cl->rlist.floor_y;
+			x++;
+		}
+		y++;
+	}
+}
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index 5bd4d06..e4eb72d 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -35,6 +35,7 @@ static t_player
 
 	plist.pos_x = 0;
 	plist.pos_y = 0;
+	plist.pos_z = 0;
 	plist.start_x = 0;
 	plist.start_y = 0;
 	plist.cam_x = 0;
diff --git a/src/ft_init_map.c b/src/ft_init_map.c
index 1c12189..965bf8e 100644
--- a/src/ft_init_map.c
+++ b/src/ft_init_map.c
@@ -49,6 +49,8 @@ int8_t
 	mlist->mapl_len = 0;
 	mlist->x_step = 0;
 	mlist->y_step = 0;
+	mlist->x_floor_step = 0;
+	mlist->y_floor_step = 0;
 	mlist->line_chk = 0;
 	mlist->map_start = 0;
 	mlist->isspawn = 0;
diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c
index 50bdbf9..5bb3649 100644
--- a/src/ft_raycasting.c
+++ b/src/ft_raycasting.c
@@ -112,6 +112,7 @@ void
 
 	i = 0;
 	wl = &cl->wlist;
+	/* ft_floor_cast(cl); */
 	while (i < wl->x_size)
 	{
 		ft_initray(cl, i);
diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c
index 37789a5..09fbc7c 100644
--- a/src/ft_warp_level.c
+++ b/src/ft_warp_level.c
@@ -45,6 +45,7 @@ static void
 
 	cl->plist.pos_x = 0;
 	cl->plist.pos_y = 0;
+	cl->plist.pos_z = 0;
 	cl->plist.start_x = 0;
 	cl->plist.start_y = 0;
 	cl->plist.cam_x = 0;
-- 
cgit v1.2.3