-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentSystem.cpp
104 lines (88 loc) · 3.23 KB
/
AgentSystem.cpp
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
#include "AgentSystem.h"
AgentSystem::AgentSystem(int width, int height, size_t numAgents) : m_numAgents(numAgents),
m_spawnPos(SpawnPosition::CENTER) {
this->init(width, height);
}
void AgentSystem::init(int width, int height) {
this->agents.resize(this->m_numAgents);
this->speciesSpecs = {DEFAULT_SPECIES_SPEC, DEFAULT_SPECIES_SPEC, DEFAULT_SPECIES_SPEC};
std::mt19937 random;
std::uniform_real_distribution<float> posX(0, (float) width);
std::uniform_real_distribution<float> posY(0, (float) height);
std::uniform_real_distribution<float> angle(0, TWO_PI);
std::uniform_int_distribution<int> species(0, this->currentNumSpecies - 1);
switch (this->m_spawnPos) {
case CIRCLE: {
const float radius = std::min(width, height) / 2.0f;
float x, y;
for (size_t i = 0; i < this->m_numAgents; i++) {
do {
x = posX(random);
y = posY(random);
} while (powf(x - width * 0.5f, 2) + powf(y - height * 0.5f, 2) > (radius * radius));
int index = species(random);
agents.at(i) = {x, y, angle(random), index};
}
break;
}
case CENTER: {
float centerX = width / 2.0f;
float centerY = height / 2.0f;
for (size_t i = 0; i < this->m_numAgents; i++) {
int index = species(random);
agents.at(i) = {centerX, centerY, angle(random), index};
}
break;
}
case EDGES: {
for (size_t i = 0; i < this->m_numAgents; i++) {
float x, y;
int index = species(random);
std::uniform_int_distribution<int> perimeterLength(0, 2 * width + 2 * height);
int point = perimeterLength(random);
if (point < (width + height)) {
if (point < width) {
x = point;
y = 0;
} else {
x = width;
y = point - width;
}
} else {
point -= (width + height);
if (point < width) {
x = width - point;
y = height;
} else {
x = 0;
y = height - (point - width);
}
}
agents.at(i) = {x, y, angle(random), index};
}
break;
}
case RANDOM: {
for (size_t i = 0; i < this->m_numAgents; i++) {
int index = species(random);
agents.at(i) = {posX(random), posY(random), angle(random), index};
}
break;
}
default: {
break;
}
}
}
int AgentSystem::getNumAgents() const {
return this->m_numAgents;
}
int AgentSystem::signum(int val) {
return (val > 0) ? 1 : ((val < 0) ? -1 : 0);
}
SpawnPosition AgentSystem::getSpawnPos() {
return this->m_spawnPos;
}
void AgentSystem::setSpawnPos(SpawnPosition spawnPos) {
this->m_spawnPos = spawnPos;
}