aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_select_bad_boy_action.c
blob: 5f4cc3aef8cdce022d3397364f45b8b1a875f547 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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-5 walk | 6-7
*/

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 (r);
}

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 && r <= 5)
		return (1);
	else
		return (2);
	return (r);
}

void
	ft_select_bad_boy_action(t_cub *cl)
{
	int8_t	i;
	int8_t	r;

	i = 0;
	while (i < cl->mlist.sprite_nbr[13])
	{
		if (cl->bad_boy[i].sleep == 0 && cl->bad_boy[i].life > 0 &&
			(ft_get_dist(cl->sprites[13][i], cl) > FT_ENMY_SIGHT_RANGE
			|| cl->rlist.wall_dist_tab[cl->wlist.x_size / 2] >
			ft_get_dist(cl->sprites[13][0], cl)))
		{
			r = ft_set_r_i();
		}
		else if (cl->bad_boy[i].sleep == 0 && cl->bad_boy[i].life > 0 &&
			(ft_get_dist(cl->sprites[13][i], cl) <= FT_ENMY_SIGHT_RANGE
			|| cl->rlist.wall_dist_tab[cl->wlist.x_size / 2] <=
			ft_get_dist(cl->sprites[13][0], cl)))
		{
			r = ft_set_r_ii();
		}
		else if (cl->bad_boy[i].life <= 0)
			/* something */
		cl->bad_boy[i].act[r](&cl->bad_boy[i],
			&cl->sprites[13][i], &cl->mlist);
		cl->bad_boy[i].sleep = 1;
		i++;
	}
}