-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.c
128 lines (117 loc) · 3.08 KB
/
play.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* play.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rvan-der <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/21 19:21:08 by rvan-der #+# #+# */
/* Updated: 2017/02/27 16:42:50 by rvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "filler.h"
t_coord *get_valid_pos(t_skin target, t_plateau p)
{
t_coord *ret;
t_args ag;
int i;
ret = NULL;
set_args(target, p, &ag);
while ((*(ag.c) += 1) != ag.limc && \
!is_ennemi((p.map)[(ag.pnt).y][(ag.pnt).x], p.pl))
{
*(ag.l) -= ag.dl;
i = 0;
while ((*(ag.l) += ag.dl) != ag.liml && p.pos != NULL && \
!is_ennemi((p.map)[(ag.pnt).y][(ag.pnt).x], p.pl))
{
if (is_inlist((ag.pnt).x, (ag.pnt).y, p.pos))
crdlist_pushback(&ret, new_crdlist((ag.pnt).x, (ag.pnt).y));
i += ag.dl;
}
*(ag.l) -= i;
}
return (ret);
}
int ctr_occupied(t_plateau p)
{
int i;
int lim;
lim = MAX((p.size).x, (p.size).y) / 10;
i = -1;
while (++i <= lim)
{
if ((p.map)[(p.ctr).y + i][(p.ctr).x] != '.' ||\
(p.map)[(p.ctr).y - i][(p.ctr).x] != '.' ||\
(p.map)[(p.ctr).y][(p.ctr).x + i] != '.' ||\
(p.map)[(p.ctr).y][(p.ctr).x - i] != '.')
return (1);
}
return (0);
}
t_skin get_target(t_plateau p, t_ennemi e)
{
t_skin center;
if (e.skin == NULL)
return (fill_area(p));
if (e.mark == far && !ctr_occupied(p) && \
dmin_to_coord(p.ctr, p.pos) > dmin_to_skin(p.ctr, e.skin))
{
center.crd = p.ctr;
return (center);
}
if (e.mark == far)
return (counter_ennemi(e));
if (e.mark == ocp)
return (fill_area(p));
return (surround_ennemi(p, e));
}
t_coord find_closest(t_coord *pnt, t_coord ref)
{
t_coord *tmp;
t_coord ret;
double min;
double d;
ret.x = -1000;
ret.y = -1000;
if (pnt != NULL)
{
tmp = pnt;
min = distance_2d(ref, *tmp);
ret = *tmp;
tmp = tmp->next;
while (tmp != NULL)
{
d = distance_2d(ref, *tmp);
if (d < min)
{
min = d;
ret = *tmp;
}
tmp = tmp->next;
}
}
return (ret);
}
t_coord play(t_plateau p, t_ennemi e)
{
t_coord ret;
t_skin target;
t_coord *valid;
ret.x = -3;
ret.y = -3;
valid = NULL;
if ((p.pce).crd != NULL && p.pos != NULL)
{
target = get_target(p, e);
if (e.mark != far && !is_inlist((target.crd).x, (target.crd).y, p.pos))
valid = get_valid_pos(target, p);
ret = find_closest((valid ? valid : p.pos), target.crd);
ret.x -= ((p.pce).offset).x;
ret.y -= ((p.pce).offset).y;
delete_crdlist(&valid);
}
if (e.skin != NULL)
((e.skin)->prev)->next = NULL;
return (ret);
}