-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prey.cc
158 lines (150 loc) · 4.34 KB
/
Prey.cc
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
#include "GL/freeglut.h"
#include "GL/gl.h"
#include <cmath>
#include <vector>
#include <map>
#include <iostream>
#include <random>
#include "Tools.h"
#include "Prey.h"
#include "Predator.h"
void Prey::update(const std::vector<Prey*> &preys,
const std::vector<Predator*> &predators)
{
std::pair<double, double> e;
std::pair<double, double> s = separation(preys);
vx += s.first * 5;
vy += s.second * 5;
if (!escapePredator(predators, e))
{
std::pair<double, double> c = cohesion(preys);
std::pair<double, double> a = alignment(preys);
std::pair<double, double> f = foodMove();
vx += f.first * 10 + c.first * .5 + a.first;
vy += f.second * 10 + c.second * .5 + a.second;
}
else
{
vx += e.first * 1000;
vy += e.second * 1000;
}
if (vx == 0 && vy == 0)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> d(-1, 1);
vx = d(gen);
vy = d(gen);
}
updatePosition_NormalizeSpeed();
eatFood();
}
bool Prey::escapePredator(const std::vector<Predator*> &predators,
std::pair<double, double> &newDirection) const
{
newDirection.first = 0;
newDirection.second = 0;
bool danger = false;
for (const Predator* p : predators)
{
if (squaredTorusDistance(*p) < std::pow(vision, 2))
{
auto pos = p->getPosition();
newDirection.first += x - pos.first;
newDirection.second += y - pos.second;
danger = true;
}
}
return danger;
}
void Prey::eatFood()
{
int xgrid = (x + 1) / 2 * food->n_food_sites;
int ygrid = (y + 1) / 2 * food->n_food_sites;
const int food_radius = 3;
for (int i = -food_radius; i <= food_radius && foodStock < FOOD_CAPACITY; i++)
{
for (int j = -food_radius; j <= food_radius && foodStock < FOOD_CAPACITY; j++)
{
int x = positive_modulo(xgrid + i, food->n_food_sites);
int y = positive_modulo(ygrid + j, food->n_food_sites);
if (food->getCurrent(x, y) > 0)
{
foodStock += food->getCurrent(x, y);
food->consume(x, y);
}
}
}
}
std::pair<double, double> Prey::cohesion(const std::vector<Prey*> &preys) const
{
if (preys.size() == 1)
{
return std::make_pair(0, 0);
}
double sx = 0, sy = 0;
for (const Prey* b : preys)
{
if (b != this)
continue;
sx += b->x;
sy += b->y;
}
sx /= preys.size() - 1;
sy /= preys.size() - 1;
return std::make_pair(x < 0 ? sx - x : x - sx, y < 0 ? sy - y : y - sy);
}
std::pair<double, double> Prey::alignment(const std::vector<Prey*> &preys) const
{
if (preys.size() == 1)
{
return std::make_pair(0, 0);
}
double sx = 0, sy = 0;
for (const Prey* b : preys)
{
if (b != this)
continue;
sx += b->vx;
sy += b->vy;
}
sx /= preys.size() - 1;
sy /= preys.size() - 1;
return std::make_pair((sx - vx) / 8, (sy - vy) / 8);
}
std::pair<double, double> Prey::foodMove() const
{
if (foodStock >= FOOD_CAPACITY)
return std::make_pair(0, 0);
int xgrid = (x + 1) / 2 * food->n_food_sites;
int ygrid = (y + 1) / 2 * food->n_food_sites;
int maxF = -1, dirX = 0, dirY = 0, minD = 0;
for (int i = -vision_int; i <= vision_int; i++)
{
for (int j = -vision_int; j <= vision_int; j++)
{
int x = positive_modulo(xgrid + i, food->n_food_sites);
int y = positive_modulo(ygrid + j, food->n_food_sites);
int d = squaredTorusDistance(food->n_food_sites, xgrid, ygrid, x, y);
if (food->getCurrent(x, y) > maxF || (food->getCurrent(x, y) == maxF && d < minD))
{
minD = d;
maxF = food->getCurrent(x, y);
dirX = i < 0 ? -1 : (i > 0 ? 1 : 0);
dirY = j < 0 ? -1 : (j > 0 ? 1 : 0);
}
}
}
if ((dirX == 0 && dirY == 0) || maxF <= 0)
{
return std::make_pair(0, 0);
}
else
{
double norm = sqrt((dirX * dirX) + (dirY * dirY));
double dx = (double) dirX / norm;
double dy = (double) dirY / norm;
const double food_fact = .07;
return std::make_pair(dx*food_fact, dy * food_fact);
}
}