aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_can_it_shoot.c
blob: 0029c39a6b53312e094da3208a545d2e8872190b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_can_it_shoot.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:51 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:28:51 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <cub3d.h>
#include <stdlib.h>
#include <math.h>

static double
	ft_get_a_big(int8_t id, double d, t_cub *cl)
{
	double	a_big;

	a_big = 0;
	if (cl->sprites[13][id].s_pos_y > cl->plist.pos_y)
	{
		while (a_big < cl->sprites[13][id].s_pos_y - cl->plist.pos_y)
			a_big += 0.001;
	}
	else if (cl->sprites[13][id].s_pos_y < cl->plist.pos_y)
	{
		while (a_big < cl->plist.pos_y - cl->sprites[13][id].s_pos_y)
			a_big += 0.001;
	}
	else
		a_big = d;
	return (a_big);
}

static double
	ft_get_b_big(int8_t id, t_cub *cl)
{
	double	b_big;

	b_big = 0;
	if (cl->sprites[13][id].s_pos_x > cl->plist.pos_x)
	{
		while (b_big < cl->sprites[13][id].s_pos_x - cl->plist.pos_x)
			b_big += 0.0001;
	}
	else if (cl->sprites[13][id].s_pos_x < cl->plist.pos_x)
	{
		while (b_big < cl->plist.pos_x - cl->sprites[13][id].s_pos_x)
			b_big += 0.0001;
	}
	return (b_big);
}

static void
	ft_init_ray_vars(int8_t id, double d, t_cub *cl)
{
	double		a_big;
	double		b_big;
	double		teta;

	a_big = ft_get_a_big(id, d, cl);
	b_big = ft_get_b_big(id, cl);
	if (a_big > b_big)
		teta = acos(a_big / d);
	else
		teta = asin(b_big / d);
	cl->rlist.y_ray_dir = 1 * cos(teta);
	cl->rlist.x_ray_dir = 1 * sin(teta);
	if (cl->plist.pos_y > cl->sprites[13][id].s_pos_y)
		cl->rlist.y_ray_dir = -cl->rlist.y_ray_dir;
	if (cl->plist.pos_x > cl->sprites[13][id].s_pos_x)
		cl->rlist.x_ray_dir = -cl->rlist.x_ray_dir;
	cl->rlist.y_ray_pos = cl->plist.pos_y;
	cl->rlist.x_ray_pos = cl->plist.pos_x;
	ft_detection_init_x(cl);
	ft_detection_init_y(cl);
}

static int8_t
	ft_shoot_cast_loop(int8_t id, double sqy, double sqx, t_cub *cl)
{
	uint8_t		hit;

	hit = 0;
	while (hit == 0)
	{
		if (cl->rlist.x_side_dist < cl->rlist.y_side_dist)
		{
			cl->rlist.x_side_dist += cl->rlist.x_delta_dist;
			sqy += cl->mlist.x_step * 0.01;
		}
		else
		{
			cl->rlist.y_side_dist += cl->rlist.y_delta_dist;
			sqx += cl->mlist.y_step * 0.01;
		}
		if ((sqy >= cl->sprites[13][id].s_pos_y - 0.15 &&
			sqy <= cl->sprites[13][id].s_pos_y + 0.15) &&
			(sqx >= cl->sprites[13][id].s_pos_x - 0.15 &&
			sqx <= cl->sprites[13][id].s_pos_x + 0.15))
			break ;
		if (cl->mlist.map[(uint64_t)(sqy + 0.5)][(uint64_t)(sqx + 0.5)] == '1')
			hit = 1;
	}
	return ((hit == 1) ? (0) : (1));
}

int8_t
	ft_can_it_shoot(int8_t id, double d, t_cub *cl)
{
	double		sqx;
	double		sqy;

	ft_init_ray_vars(id, d, cl);
	sqy = cl->rlist.y_ray_pos;
	sqx = cl->rlist.x_ray_pos;
	return (ft_shoot_cast_loop(id, sqy, sqx, cl));
}