-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.cpp
More file actions
163 lines (131 loc) · 4.01 KB
/
Environment.cpp
File metadata and controls
163 lines (131 loc) · 4.01 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
160
161
162
163
#include "Environment.h"
#include "behavior/Careful.h"
#include "behavior/Fearful.h"
#include "behavior/SuicideBomber.h"
#include "behavior/Gregarious.h"
#include "behavior/MultipleBehavior.h"
#include <cstdlib>
#include <ctime>
const T Environment::white[] = { (T)255, (T)255, (T)255 };
Environment::Environment(int w, int l): UImg( w, l, 1, 3 ),width(w),length(l){
std::srand( time(NULL) );
cout << "Construct the Environment" << endl;
for(int i=0; i<INITIAL_NUMBER; i++){
this->addCritter();
}
int n_careful = int(INITIAL_NUMBER*PERCENTAGE_CAREFUL/100);
int n_fearful = int(INITIAL_NUMBER*PERCENTAGE_FEARFUL/100);
int n_gregarious = int(INITIAL_NUMBER*PERCENTAGE_GREGARIOUS/100);
int n_kamikaze = int(INITIAL_NUMBER*PERCENTAGE_SUICIDE/100);
for(int i=0; i<n_careful ; i++){
listCritters[i]->setBehavior(new Careful());
}
for(int i=n_careful; i<n_careful+n_fearful; i++){
listCritters[i]->setBehavior(new Fearful());
}
for(int i=n_careful+n_fearful; i<n_careful+n_fearful+n_gregarious; i++){
listCritters[i]->setBehavior(new Gregarious());
}
for(int i=n_careful+n_fearful+n_gregarious; i<n_careful+n_fearful+n_gregarious+n_kamikaze; i++){
listCritters[i]->setBehavior(new SuicideBomber());
}
for(int i = n_careful+n_fearful+n_gregarious+n_kamikaze; i<INITIAL_NUMBER; i++){
listCritters[i]->setBehavior(new MultipleBehavior());
}
}
Environment::~Environment(){
cout << "Destruct the Environment" << endl;
}
void Environment::step(){
std::vector<std::shared_ptr<Critter>> clonedCritters;
for ( std::vector<std::shared_ptr<Critter>>::iterator it = listCritters.begin() ; it != listCritters.end() ; ++it ){
bool col = false;
if ((*it)->getAgeLimit()==0){
(*it)->dead();
}
else{
(*it)->decrementAge();}
for( std::vector<std::shared_ptr<Critter>>::iterator neighbor= listCritters.begin() ; neighbor != listCritters.end() ; ++neighbor ){
if(it==neighbor){
continue;
}
if((*it)->collision((*neighbor).get())){
(*it)->behaviorAfterCollision();
col = true;
}
}
std::vector<std::shared_ptr<Critter>> listNeighbors = getNeighbors((*it).get());
if(!col && !(*it)->getIsDead()){
(*it)->specialBehavior(listNeighbors);
}
//
}
removeDeadCritters();
cloning();
int n = std::rand()%NMAX;
double pro = static_cast<double>( std::rand() )/RAND_MAX;
if(pro<P_BIRTH){
for(int i=0; i<n; i++){
addCritter();
}
}
cimg_forXY( *this, x, y ) fillC( x, y, 0, white[0], white[1], white[2] );
for ( std::vector<std::shared_ptr<Critter>>::iterator it = listCritters.begin() ; it != listCritters.end() ; ++it ){
(*it)->draw( *this );
(*it)->action( *this );
}
}
void Environment::cloning(){
if(listCritters.size()<30){
int size = listCritters.size();
for(int i=0; i<size; i++){
if(listCritters[i]->cloning(P_CLONAGE)){
addCritter(listCritters[i]->clone());
}
}
}
}
void Environment::addCritter(){
Critter* c = new Critter(width, length);
addCritter(c);
}
void Environment::addCritter(Critter* crit){
std::shared_ptr<Critter> critter;
critter.reset(crit);
listCritters.push_back(critter);
}
std::vector<std::shared_ptr<Critter>> Environment::getNeighbors(Critter* c){
std::vector<std::shared_ptr<Critter>> list;
for ( std::vector<std::shared_ptr<Critter>>::iterator it = listCritters.begin() ; it != listCritters.end() ; ++it ){
if(c->getIdentity() != (*it)->getIdentity() && c->detection((*it).get())){
list.push_back((*it));
}
}
return list;
}
int Environment::getWidth(){
return width;
}
int Environment::getLength(){
return length;
}
void Environment::removeDeadCritters(){
std::vector<std::shared_ptr<Critter>>::iterator it = listCritters.begin();
while(it != listCritters.end()){
if((*it)->getIsDead()){
listCritters.erase(it);
}else{
it++;
}
}
}
Critter* Environment::getCritterById(int id){
std::vector<std::shared_ptr<Critter>>::iterator it = listCritters.begin();
while(it != listCritters.end()){
if((*it)->getIdentity() == id){
return (*it).get();
}
it++;
}
return nullptr;
}