-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfox.cpp
More file actions
174 lines (166 loc) · 6.15 KB
/
fox.cpp
File metadata and controls
174 lines (166 loc) · 6.15 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
164
165
166
167
168
169
170
171
172
173
174
#include "fox.h"
#include "rabbit.h"
Fox::Fox(Vector2 pos, Enviroment * enviroment,float mutateProbability) : Animal(pos,enviroment,mutateProbability)
{
eType = EntityType::Fox;
this->gens.speed *= 2;
this->gens.senseRadius *= 1.5;
this->gens.energyEfficiency /= 1.5;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> randBool(0,1);
if(randBool(mt)){
this->sex = Gender::Male;
}
else{
this->sex = Gender::Female;
}
}
void Fox::update(){
std::lock_guard<std::mutex> thisLock(accessEntity);
if(this->hunger > 200){
//Starving to death
int index = nextEnviroment->getEntityIndexByID(this->id);
this->dead = true;
if(index != -1){
nextEnviroment->removeEntity(index);
}
}
if(this->infertilityDuration > 0){
infertilityDuration--;
}
else if(this->infertilityDuration == 0){
fertility = true;
}
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<float> dist(-100,100);
Vector2 direction(dist(mt),dist(mt));
direction.normalize();
std::vector<Fox*> nearbyFoxes;
std::vector<Rabbit*> nearbyRabbits;
calcMostNeed();
EntityType entityTypeToInteract = EntityType::Rabbit;
for(const std::shared_ptr<Entity> entity : currentEnviroment->Entitys){
if(!(*entity == *this )){
//std::lock_guard lockE(entity->accessEntity);
float dis = Vector2::distance(this->pos, entity->pos);
if(dis <= gens.senseRadius){
if (entity->eType == EntityType::Rabbit && entityTypeToInteract == EntityType::Rabbit ) {
nearbyRabbits.push_back(dynamic_cast<Rabbit*>(entity.get()));
entityTypeToInteract = EntityType::Rabbit;
}
else if(entity->eType == EntityType::Fox && mostNeed == MostNeed::Reproduce){
nearbyFoxes.push_back(dynamic_cast<Fox*>(entity.get()));
if(nearbyFoxes.back()->sex != this->sex && this->fertility == true){
entityTypeToInteract = EntityType::Fox;
}
}
//Water needed
}
}
}
if(entityTypeToInteract == EntityType::Fox){
Fox * nearestFox;
float minDistance = gens.senseRadius*2;
bool mateFound = false;
for(Fox * fox : nearbyFoxes){
//std::lock_guard lockFox(fox->accessEntity);
if(fox->sex != this->sex && fox->fertility == true && this->fertility == true){
mateFound = true;
float distance = Vector2::distance(this->pos,fox->pos);
if(distance < 2 && this->sex == Gender::Female){
Reproduce(fox);
mateFound = false;
break;
}
else if(distance < 2){
mateFound = false;
this->urgeToReproduce -= 50;
this->fertility = false;
this->infertilityDuration = 500;
this->hunger += 20;
}
else if(distance<minDistance){
minDistance = distance;
nearestFox = fox;
}
}
}
if(mateFound){
//std::lock_guard l(nearestFox->accessEntity);
direction = nearestFox->pos - this->pos;
direction.normalize();
}
}
else if(entityTypeToInteract == EntityType::Rabbit && this->hunger > 20){
Rabbit * nearestRabbit;
float minDistance = gens.senseRadius*2;
bool nearRabbitNotEaten = false;
for(Rabbit * rabbit : nearbyRabbits){
//std::lock_guard lockRabbit(rabbit->accessEntity);
float distance = Vector2::distance(this->pos,rabbit->pos);
if(distance<3){
this->eat(*rabbit);
break;
}
else if(distance<minDistance){
nearRabbitNotEaten = true;
minDistance = distance;
nearestRabbit = rabbit;
}
}
if(nearRabbitNotEaten){
//std::lock_guard lockRabbit(nearestRabbit->accessEntity);
direction = nearestRabbit->pos - this->pos;
direction.normalize();
}
}
hunger += this->gens.speed * this->gens.energyEfficiency;
urgeToReproduce += 0.1f;
if(entityTypeToInteract == EntityType::Fox){
activity = Activity::SearchingForMate;
}
else if(entityTypeToInteract == EntityType::Rabbit){
activity = Activity::Hunting;
}
this->pos = this->pos + (direction*gens.speed);
//Until better solution
if(this->pos.x>currentEnviroment->maxCoordinates.x){
this->pos.x = currentEnviroment->maxCoordinates.x;
}
else if(this->pos.x<currentEnviroment->minCoordinates.x){
this->pos.x = currentEnviroment->minCoordinates.x;
}
if(this->pos.y>currentEnviroment->maxCoordinates.y){
this->pos.y = currentEnviroment->maxCoordinates.y;
}
else if(this->pos.y<currentEnviroment->minCoordinates.y){
this->pos.y = currentEnviroment->minCoordinates.y;
}
//Update of Water
}
void Fox::Reproduce(const Animal * father){
//std::lock_guard thisLock(accessEntity);
this->fertility = false;
this->infertilityDuration = 500;
this->urgeToReproduce -= 50;
this->hunger+=20;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(minChildren,maxChildren);
int numChildren = dist(mt);
std::vector<std::shared_ptr<Fox>> ans(numChildren,std::make_shared<Fox>(Vector2(0,0),
currentEnviroment,this->mutateProbability));
for (auto an:ans){
an->generation += this->generation;
an->pos = this->pos;
an->eType = this->eType;
an->infertilityDuration = 300;
an->hunger = this->hunger;
an->maxChildren = this->maxChildren;
an->minChildren = this->minChildren;
an->gens = CalcNewGens(father);
nextEnviroment->addEntity(an);
}
}