-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHunter.cpp
More file actions
159 lines (127 loc) · 4.7 KB
/
Hunter.cpp
File metadata and controls
159 lines (127 loc) · 4.7 KB
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
//****************************************************************************
//
// Hunter
//
//****************************************************************************
// ===========================================================================
// Libraries
// ===========================================================================
#include <math.h>
// ===========================================================================
// Project Files
// ===========================================================================
#include "Hunter.h"
#include "DefVal.h"
//############################################################################
// #
// Class Hunter #
// #
//############################################################################
// ===========================================================================
// Definition of static attributes
// ===========================================================================
// ===========================================================================
// Constructors
// ===========================================================================
Hunter::Hunter(double x, double y) : HUNTING_SPEED(DefVal::HUNTING_SPEED),
Agent(x,y,DefVal::HUNTER_HITBOX,DefVal::HUNTER_RADIUS,DefVal::HUNTER_COLOR)
{
}
Hunter::Hunter(double x, double y, unsigned int c) : HUNTING_SPEED(DefVal::HUNTING_SPEED),
Agent(x,y,DefVal::HUNTER_HITBOX,DefVal::HUNTER_RADIUS,c)
{
}
// ===========================================================================
// Destructor
// ===========================================================================
Hunter::~Hunter(void)
{
}
// ===========================================================================
// Public Methods
// ===========================================================================
void Hunter::move(Border* borders, unsigned int nb_b, Agent** tab, int index, unsigned int nb_a, Obstacle* obs, unsigned int nb_o)
{
// If a hunter has to die
if(hunters_killed)
{
state = 2;
hunters_killed--;
}
// Wind
speed_from_borders(borders, nb_b);
// Isotropic motion
ndx += DefVal::HUNTER_DRUNKENNESS * pow(-1,round((float)rand()/RAND_MAX))*((float)rand()/RAND_MAX);
ndy += DefVal::HUNTER_DRUNKENNESS * pow(-1,round((float)rand()/RAND_MAX))*((float)rand()/RAND_MAX);
int i;
// Minimum distance between a hunter and its prey
double min_dist = DefVal::WINDOW_HEIGHT + DefVal::WINDOW_WIDTH;
// Hunt
for(i = 0; i < nb_a; i++)
{
double* dv;
if(i != index)
{
dv = tab[i]->speed_for_hunters(x,y,r);
if( (dv[0]!=0) || (dv[1]!= 0) ){
if(dv[2] < min_dist)
ndx = dv[0];
ndy = dv[1];
min_dist = dv[2];
// If hunter just ate
if(dv[2] == -1 )
{
state = - DefVal::WAITING_STEPS * DefVal::TIME_STEP;
preys_eaten += 1;;
}
}
}
}
// Obstacles
for(i = 0; i < nb_o; i++)
if( ( (obs[i].x - x)*(obs[i].x - x) + (obs[i].y - y)*(obs[i].y - y) ) < (hitbox+obs[i].r)*(hitbox+obs[i].r))
{
// V3
ndx += - DefVal::GAMMA3o * (obs[i].x - x);
ndy += - DefVal::GAMMA3o * (obs[i].y - y);
}
// Max speed
if (ndx*ndx > (DefVal::MAX_HUNT_SPEED)*(DefVal::MAX_HUNT_SPEED))
ndx /= (abs(ndx)/DefVal::MAX_HUNT_SPEED);
if (dy*dy > (DefVal::MAX_HUNT_SPEED)*(DefVal::MAX_HUNT_SPEED))
ndy /= (abs(ndy)/DefVal::MAX_HUNT_SPEED);
}
double* Hunter::speed_for_preys(double x2, double y2, double dx2, double dy2, double r2)
{
// Returned induced speed
double* res = new double [3];
res[0] = 0;
res[1] = 0;
res[2] = 2;
// if hunter is alive
if(state != 2)
{
double dis = sqrt((x-x2)*(x-x2) + (y - y2)*(y-y2));
if( dis < r2)
{
res[0] = - ((x - x2) * DefVal::GAMMA4) / dis;
res[1] = - ((y - y2) * DefVal::GAMMA4) / dis;
}
}
return res;
}
double* Hunter::speed_for_hunters(double x2, double y2, double r2)
{
// Returned induced speed
double* res = new double [3];
res[0] = 0;
res[1] = 0;
res[2] = 0;
return res;
}
// ===========================================================================
// Protected Methods
// ===========================================================================
// ===========================================================================
// Non inline accessors
// ===========================================================================