aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_select_bad_boy_action.c
blob: 5a4f5be5f6cd3286554ec7ce974e8c98c23d8003 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_select_bad_boy_actions.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 <libft.h>
#include <cub3d.h>
#include <stdlib.h>
#include <math.h>

/*
** bad_boy.act[] - index summary
** -----------------------------
** 0: wait
** 1: walk
** 2: fire
**
** rand() rules
** ------------
** I. player is not in sight
** rand(0-3) | [0-2] wait | [3] walk
** II. player is in sight
** rand(0-7) | [0-1] wait | [2] walk | [3-7] fire
*/

static double
	ft_get_dist(t_sprite sprite, t_cub *cl)
{
	return (sqrt(pow(cl->plist.pos_x - sprite.s_pos_x, 2) +
			pow(cl->plist.pos_y - sprite.s_pos_y, 2)));
}

static int8_t
	ft_set_r_i(void)
{
	int8_t	r;

	if (FT_OS == 2)
		r = random() % 4;
	else
		r = rand() % 4;
	if (r == 3)
		return (1);
	else
		return (0);
	return (0);
}

static int8_t
	ft_set_r_ii(void)
{
	int8_t	r;

	if (FT_OS == 2)
		r = random() % 8;
	else
		r = rand() % 8;
	if (r <= 1)
		return (0);
	else if (r == 2)
		return (1);
	else
		return (2);
	return (0);
}

static void
	ft_bb_act(int8_t r, int8_t i, t_cub *cl)
{
	cl->bad_boy[i].sleep = 1;
	cl->bad_boy[i].act[r](&cl->bad_boy[i],
		&cl->sprites[13][i], &cl->mlist);
}

int8_t
	ft_can_it_shoot(int8_t id, double d, t_cub *cl)
{
	uint8_t		hit;
	t_win		*wl;
	t_player	*pl;
	double		teta;
	double		a2;

	wl = &cl->wlist;
	pl = &cl->plist;
	a2 = 0;
	if (cl->sprites[13][id].s_pos_y > cl->plist.pos_y)
	{
		while (a2 < cl->sprites[13][id].s_pos_y - cl->plist.pos_y)
			a2 += 0.005;
	}
	else
	{
		while (a2 < cl->plist.pos_y - cl->sprites[13][id].s_pos_y)
			a2 += 0.005;
	}
	if (a2 != d)
	{
		teta = acos(a2 / d);
		cl->rlist.y_ray_dir = 1 * cos(teta);
		cl->rlist.x_ray_dir = 1 * sin(teta);
	}
	else
	{
		teta = 0.0;
		cl->rlist.y_ray_dir = 1;
		cl->rlist.x_ray_dir = 1 * sin(teta);
	}
	if (cl->plist.pos_x > cl->sprites[13][id].s_pos_x)
		cl->rlist.x_ray_dir = -cl->rlist.x_ray_dir;
	hit = 0;
	cl->rlist.y_ray_pos = cl->plist.pos_y;
	cl->rlist.x_ray_pos = cl->plist.pos_x;
	cl->rlist.sqy = (uint64_t)cl->rlist.y_ray_pos;
	cl->rlist.sqx = (uint64_t)cl->rlist.x_ray_pos;
	ft_detection_init_x(cl);
	ft_detection_init_y(cl);
	while (hit == 0 &&
		(cl->rlist.sqy != (uint64_t)cl->sprites[13][id].s_pos_y ||
		cl->rlist.sqx != (uint64_t)cl->sprites[13][id].s_pos_x))
	{
		if (cl->rlist.x_side_dist < cl->rlist.y_side_dist)
		{
			cl->rlist.x_side_dist += cl->rlist.x_delta_dist;
			cl->rlist.sqy += cl->mlist.x_step;
			cl->rlist.side = 0;
		}
		else
		{
			cl->rlist.y_side_dist += cl->rlist.y_delta_dist;
			cl->rlist.sqx += cl->mlist.y_step;
			cl->rlist.side = 1;
		}
		if (cl->mlist.map[cl->rlist.sqy][cl->rlist.sqx] == '1')
  	 		hit = 1;
	}
	return ((hit == 1) ? (0) : (1));
}

void
	ft_select_action(int8_t i, double d, t_cub *cl)
{
	int8_t	can_shoot;
	int8_t	r;

	if (cl->bad_boy[i].life <= 0)
		cl->bad_boy[i].does = 3;
	else
		can_shoot = ft_can_it_shoot(i, d, cl);
	if (cl->bad_boy[i].sleep == 0 && cl->bad_boy[i].life > 0 &&
		(d <= FT_ENMY_SIGHT_RANGE &&
		can_shoot == 1))
	{
		r = ft_set_r_ii();
		ft_bb_act(r, i, cl);
	}
	else if (cl->bad_boy[i].sleep == 0 && cl->bad_boy[i].life > 0 &&
		d > FT_ENMY_SIGHT_RANGE)
	{
		r = ft_set_r_i();
		ft_bb_act(r, i, cl);
	}
}

void
	ft_select_bad_boy_action(t_cub *cl)
{
	int8_t	i;
	double	d;

	i = 0;
	while (i < cl->mlist.sprite_nbr[13])
	{
		if ((d = ft_get_dist(cl->sprites[13][i], cl))
			<= 4 * FT_ENMY_SIGHT_RANGE && cl->bad_boy[i].sleep == 0)
		{
			ft_select_action(i, d, cl);
		}
		i++;
	}
}