This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Milieu.cpp
executable file
·278 lines (243 loc) · 7.29 KB
/
Milieu.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "Milieu.h"
#include <cstdlib>
#include <ctime>
#include <random>
#include <fstream>
#include <string>
#include <memory>
#include "Control.h"
const T Milieu::white[] = { (T)255, (T)255, (T)255 };
/*********** Constructeurs et destructeurs **********/
Milieu::Milieu( int _width, int _height ) : UImg( _width, _height, 1, 3 ),
width(_width), height(_height)
{
readConfig();
cout << "const Milieu" << endl;
std::srand( time(NULL) );
}
Milieu::~Milieu( void )
{
cout << "dest Milieu" << endl;
}
/*********** Initialisation **********/
// Fonction : readConfig
// Entrée : rien
// Sortie : rien
// Action : lit le fichier de configuration et initialise les paramètres de la simulation
void Milieu::readConfig(void)
{
ifstream fichier("./config.txt", ios::in);
if (fichier)
{
std::string ligne;
std::string champ;
double valeur;
for (int i =0; i < 10; i++)
{
getline(fichier, ligne);
std::size_t pos = ligne.find("=");
champ = ligne.substr(0,pos);
valeur = stod(ligne.substr(pos+1));
if (champ == "Pourcentage Kamikaze") {
pourcentageKamikaze = valeur;
}
if (champ == "Pourcentage Grégaire") {
pourcentageGregaire = valeur;
}
if (champ == "Pourcentage Peureuse") {
pourcentagePeureuse = valeur;
}
if (champ == "Pourcentage Personnalité Multiple") {
pourcentagePersoMulti = valeur;
}
if (champ == "Pourcentage Prevoyante") {
pourcentagePrevoyante = valeur;
}
if (champ == "Probabilité de naissance spontanée") {
probaNaissance = valeur;
}
}
}
else
{
cerr << "Impossible d'ouvrir le fichier de configuration !" << endl;
}
fichier.close();
}
/*********** Actions **********/
// Fonction : step
// Entrée : rien
// Sortie : rien
// Action : une étape de simulation
void Milieu::step( void ){
phaseDetection();
phaseAction();
phaseEnvironnement();
naissanceAlea();
}
bool del(std::shared_ptr<Bestiole> b){
if(b->isDeletedSoon())
{
b= NULL;
return true;
}
return false;
}
/* Fonction : phaseEnvironnement
* Entrée : rien
* Sortie : rien
* Action : vérifie les âges des bestioles, les collisions
* élimine les bestioles qui doivent mourir
*/
void Milieu::phaseEnvironnement( void ){
// Gestion de l'âge
// Si la bestiole atteint son âge maximal, elle meurt
for ( std::list<std::shared_ptr<Bestiole>>::iterator it = listeBestioles.begin() ;
it != listeBestioles.end() ; it++ ){
(*it)->vieillit();
if((*it)->getAnneesRestantes()<=0)
{
(*it)->deleteBestiole();
}
}
//Gestion des collisions
std::vector<std::shared_ptr<Bestiole>> collisions;
for ( std::list<std::shared_ptr<Bestiole>>::iterator it1 = listeBestioles.begin() ;
it1 != listeBestioles.end() ; it1++ )
{
for ( std::list<std::shared_ptr<Bestiole>>::iterator it2 = listeBestioles.begin() ;
it2 != it1 ; it2++ )
{
if ((*it2)->checkCollision((*it1)))
{
collisions.push_back(*it1);
collisions.push_back(*it2);
}
}
}
for ( std::vector<std::shared_ptr<Bestiole>>::iterator it1 = collisions.begin() ;
it1 != collisions.end() ; it1++ ){
if((**it1).collision())
{
(*it1)->deleteBestiole();
}
else
{
(**it1).rebondit();
}
}
// Changement de personalité des personalités multiples
// for ( std::list<std::shared_ptr<Bestiole>>::iterator it = listeBestioles.begin() ;
// it != listeBestioles.end() ; it++ ){
// if((*it)->isPersoMult())
// {
// if(std::rand()%60==0)
// {
// BestioleFactory factory;
// int type = randomPerso();
// std::shared_ptr<Bestiole> best = factory.creationBestiole(true, type, false, false, false, false, false);
// best->cloneFromBestiole(*it);
// (*it)->deleteBestiole();
// addMember(best);
// }
// }
// }
listeBestioles.erase(std::remove_if(listeBestioles.begin(), listeBestioles.end(), del), listeBestioles.end());
}
/* Fonction : phaseDetection
* Entrée : rien
* Sortie : rien
* Action : vérifie les bestioles qui se voient entre elles
* change leurs attributs selon leur comportement
*/
void Milieu::phaseDetection ( void ){
std::vector<std::shared_ptr<Bestiole>> bestiolesAlentours;
for ( std::list<std::shared_ptr<Bestiole>>::iterator it1 = listeBestioles.begin() ; it1 != listeBestioles.end() ; it1++ )
{
for ( std::list<std::shared_ptr<Bestiole>>::iterator it2 = listeBestioles.begin() ; it2 != listeBestioles.end() ; it2++ )
{
if ((*it1)->getIdentite() != (*it2)->getIdentite())
{
if ( (*it1)->jeTeVois(**it2) )
{
bestiolesAlentours.push_back(*it2);
}
(*it1)->update(bestiolesAlentours);
}
}
bestiolesAlentours.clear();
}
}
/* Fonction : phaseAction
* Entrée : rien
* Sortie : rien
* Action : Déplace les bestioles
*/
void Milieu::phaseAction( void )
{
cimg_forXY( *this, x, y ) fillC( x, y, 0, white[0], white[1], white[2] );
for ( std::list<std::shared_ptr<Bestiole>>::iterator it = listeBestioles.begin() ; it != listeBestioles.end() ; ++it )
{
(*it)->action( *this );
(*it)->draw( *this );
}
}
/*********** Autres méthodes **********/
/* Fonction : randomPerso
* Entrée : rien
* Sortie : un entier
* Action : tire un type aléatoir de bestiole en respectant la config
*/
int Milieu::randomPerso(){
double tirageAlea = static_cast<double>( std::rand()/RAND_MAX);
int type = 1;
if (tirageAlea < pourcentageGregaire)
{
type = 2;
}
else if (tirageAlea < pourcentageGregaire + pourcentagePeureuse)
{
type = 4;
}
else if (tirageAlea < pourcentageGregaire + pourcentagePeureuse + pourcentageKamikaze)
{
type = 1;
}
else
{
type = 3;
}
return type;
}
/* Fonction : naissanceAlea
* Entrée : rien
* Sortie : rien
* Action : fait naître une bestiole aléatoirement
*/
void Milieu::naissanceAlea( void ){
BestioleFactory factory;
double tirageAlea = ((double) std::rand())/ (double) RAND_MAX;
if (tirageAlea < probaNaissance)
{
addPersoAlea();
}
}
/* Fonction : addPersoAlea
* Entrée : rien
* Sortie : rien
* Action : crée et ajoute une bestiole aléatoire
*/
void Milieu::addPersoAlea( void ){
BestioleFactory factory;
bool persoMult,aOreille,aYeux,aCamouflage,aCarapace,aNageoires;
double tirageAlea = ((double) std::rand())/ (double) RAND_MAX;
persoMult = tirageAlea < pourcentagePersoMulti;
aYeux = std::rand()%2;
aOreille = std::rand()%2;
aCamouflage = std::rand()%2;
aCarapace = std::rand()%2;
aNageoires = std::rand()%2;
int persoAlea = randomPerso();
std::shared_ptr<Bestiole> best = factory.creationBestiole(persoMult, persoAlea, aOreille, aYeux, aCamouflage, aCarapace, aNageoires);
addMember(best);
}