Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CPP Ecosystem Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RIVOAL Samuel
CPP Ecosystem Project
Merge requests
!23
Aymeric
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Aymeric
Aymeric
into
master
Overview
0
Commits
4
Pipelines
0
Changes
1
Merged
BERTHOUMIEU Aymeric
requested to merge
Aymeric
into
master
4 years ago
Overview
0
Commits
4
Pipelines
0
Changes
1
0
0
Merge request reports
Viewing commit
831633fe
Prev
Next
Show latest version
1 file
+
86
−
87
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
831633fe
clt alt l
· 831633fe
Aymeric berthoumieu
authored
4 years ago
src/KamikazeBehaviour.cpp
+
86
−
87
View file @ 831633fe
Edit in single-file editor
Open in Web IDE
Show full file
@@ -11,31 +11,31 @@
std
::
string
KamikazeBehaviour
::
NAME
=
"b_Kamikaze"
;
std
::
string
KamikazeBehaviour
::
getBehaviourName
(){
return
NAME
;
std
::
string
KamikazeBehaviour
::
getBehaviourName
()
{
return
NAME
;
}
const
T
KamikazeBehaviour
::
color
[
3
]
=
{
230
,
0
,
0
};
const
T
*
KamikazeBehaviour
::
getColor
()
{
const
T
*
KamikazeBehaviour
::
getColor
()
{
return
color
;
}
KamikazeBehaviour
*
KamikazeBehaviour
::
kamikazebehaviour
=
nullptr
;
KamikazeBehaviour
*
KamikazeBehaviour
::
kamikazebehaviour
=
nullptr
;
KamikazeBehaviour
*
KamikazeBehaviour
::
getBehaviourInstance
(){
if
(
kamikazebehaviour
==
nullptr
){
kamikazebehaviour
=
new
KamikazeBehaviour
();
KamikazeBehaviour
*
KamikazeBehaviour
::
getBehaviourInstance
()
{
if
(
kamikazebehaviour
==
nullptr
)
{
kamikazebehaviour
=
new
KamikazeBehaviour
();
}
return
kamikazebehaviour
;
}
void
KamikazeBehaviour
::
getRidOfInstance
(
void
){
delete
kamikazebehaviour
;
cout
<<
" LA DESTRUCTION DE LA KAMIKAZE EFFECTIVEMENT LIEU !"
<<
endl
;
void
KamikazeBehaviour
::
getRidOfInstance
(
void
)
{
delete
kamikazebehaviour
;
cout
<<
" LA DESTRUCTION DE LA KAMIKAZE EFFECTIVEMENT LIEU !"
<<
endl
;
}
KamikazeBehaviour
::~
KamikazeBehaviour
()
{
@@ -44,92 +44,91 @@ KamikazeBehaviour::~KamikazeBehaviour() {
// Méthode permettant de récupérer la bestiole la plus proche
// de la bestiole courante
std
::
vector
<
Animal
*>
KamikazeBehaviour
::
nearestNeighbors
(
Animal
*
pet
,
Environment
&
myEnvironment
){
double
dist
;
Animal
*
closest
;
std
::
vector
<
Animal
*>
closestPets
;
// On récupère le vecteurs des voisins détectés dans l'environnement
std
::
vector
<
Animal
*
>
pets
=
myEnvironment
.
detectedNeighbors
(
pet
);
auto
cord
=
pet
->
getCoordinates
();
int
x
=
std
::
get
<
0
>
(
cord
);
int
y
=
std
::
get
<
1
>
(
cord
);
double
best_distance
=
std
::
numeric_limits
<
double
>::
infinity
();
int
neighbor
=
0
;
// On ne garde que la bestiole la plus proche
for
(
std
::
vector
<
Animal
*>::
iterator
it
=
pets
.
begin
()
;
it
!=
pets
.
end
()
;
++
it
){
auto
neighbor_cord
=
(
*
it
)
->
getCoordinates
();
int
neighbor_x
=
std
::
get
<
0
>
(
neighbor_cord
);
int
neighbor_y
=
std
::
get
<
1
>
(
neighbor_cord
);
++
neighbor
;
dist
=
std
::
sqrt
(
(
x
-
neighbor_x
)
*
(
x
-
neighbor_x
)
+
(
y
-
neighbor_y
)
*
(
y
-
neighbor_y
)
);
if
(
dist
<=
best_distance
){
std
::
vector
<
Animal
*>
KamikazeBehaviour
::
nearestNeighbors
(
Animal
*
pet
,
Environment
&
myEnvironment
)
{
double
dist
;
Animal
*
closest
;
std
::
vector
<
Animal
*
>
closestPets
;
// On récupère le vecteurs des voisins détectés dans l'environnement
std
::
vector
<
Animal
*
>
pets
=
myEnvironment
.
detectedNeighbors
(
pet
);
auto
cord
=
pet
->
getCoordinates
();
int
x
=
std
::
get
<
0
>
(
cord
);
int
y
=
std
::
get
<
1
>
(
cord
);
double
best_distance
=
std
::
numeric_limits
<
double
>::
infinity
();
int
neighbor
=
0
;
// On ne garde que la bestiole la plus proche
for
(
std
::
vector
<
Animal
*>::
iterator
it
=
pets
.
begin
();
it
!=
pets
.
end
();
++
it
)
{
auto
neighbor_cord
=
(
*
it
)
->
getCoordinates
();
int
neighbor_x
=
std
::
get
<
0
>
(
neighbor_cord
);
int
neighbor_y
=
std
::
get
<
1
>
(
neighbor_cord
);
++
neighbor
;
dist
=
std
::
sqrt
((
x
-
neighbor_x
)
*
(
x
-
neighbor_x
)
+
(
y
-
neighbor_y
)
*
(
y
-
neighbor_y
));
if
(
dist
<=
best_distance
)
{
// Pour la Kamikaze, on ne garde que la bestiole la plus proche
closest
=
*
it
;
}
}
if
(
neighbor
!=
0
)
{
// Pour la Kamikaze, on ne garde que le voisin le plus proche
// que l'on récupère dans le vecteur de sortie
closestPets
.
push_back
(
closest
);
}
return
closestPets
;
}
}
if
(
neighbor
!=
0
)
{
// Pour la Kamikaze, on ne garde que le voisin le plus proche
// que l'on récupère dans le vecteur de sortie
closestPets
.
push_back
(
closest
);
}
return
closestPets
;
}
void
KamikazeBehaviour
::
move
(
int
xLim
,
int
yLim
,
Animal
*
pet
,
Environment
&
myEnvironment
)
{
auto
cord
=
pet
->
getCoordinates
();
auto
cumul
=
pet
->
getCumul
();
auto
orient_speed
=
pet
->
getOrientationSpeed
();
int
x
=
std
::
get
<
0
>
(
cord
);
int
y
=
std
::
get
<
1
>
(
cord
);
double
cumulX
=
std
::
get
<
0
>
(
cumul
);
double
cumulY
=
std
::
get
<
1
>
(
cumul
);
double
orientation
=
std
::
get
<
0
>
(
orient_speed
);
double
speed
=
std
::
get
<
1
>
(
orient_speed
);
// On récupère les coordonnées de la bestiole la plus proche
// de la bestiole courante
double
nearestPet_x
;
double
nearestPet_y
;
std
::
vector
<
Animal
*>
closestPets
=
this
->
nearestNeighbors
(
pet
,
myEnvironment
);
// On vérifie qu'il y a au moins une bestiole identifiée
// comme étant la plus proche
if
(
!
closestPets
.
empty
()){
cout
<<
"At least one neighbor for Kamikaze ! "
<<
endl
;
// Lorsqu'une Kamikaze ne détecte aucun bestiole autour d'elle
// la variable has_reset_orientation permettra de vérifier que
// la bestiole a relancé son mouvement avec une orientation
// aléatoire
has_reset_orientation
=
0
;
auto
nearestPet_cord
=
closestPets
.
back
()
->
getCoordinates
();
nearestPet_x
=
std
::
get
<
0
>
(
nearestPet_cord
);
nearestPet_y
=
std
::
get
<
1
>
(
nearestPet_cord
);
// La nouvelle orientation de la kamikaze sera fonction
// de ses cordonnées et celles de son plus proche voisin
double
abs_diff
=
nearestPet_x
-
x
;
double
ord_diff
=
nearestPet_y
-
y
;
double
hypothenuse
=
sqrt
(
pow
(
abs_diff
,
2
)
+
pow
(
ord_diff
,
2
));
if
(
hypothenuse
!=
0
){
// La nouvelle orientation de la kamikaze sera fonction
// de ses cordonnées et celles de son plus proche voisin
orientation
=
acos
(
abs_diff
/
hypothenuse
)
;
}
else
{
void
KamikazeBehaviour
::
move
(
int
xLim
,
int
yLim
,
Animal
*
pet
,
Environment
&
myEnvironment
)
{
auto
cord
=
pet
->
getCoordinates
();
auto
cumul
=
pet
->
getCumul
();
auto
orient_speed
=
pet
->
getOrientationSpeed
();
int
x
=
std
::
get
<
0
>
(
cord
);
int
y
=
std
::
get
<
1
>
(
cord
);
double
cumulX
=
std
::
get
<
0
>
(
cumul
);
double
cumulY
=
std
::
get
<
1
>
(
cumul
);
double
orientation
=
std
::
get
<
0
>
(
orient_speed
);
double
speed
=
std
::
get
<
1
>
(
orient_speed
);
// On récupère les coordonnées de la bestiole la plus proche
// de la bestiole courante
double
nearestPet_x
;
double
nearestPet_y
;
std
::
vector
<
Animal
*
>
closestPets
=
this
->
nearestNeighbors
(
pet
,
myEnvironment
);
// On vérifie qu'il y a au moins une bestiole identifiée
// comme étant la plus proche
if
(
!
closestPets
.
empty
())
{
cout
<<
"At least one neighbor for Kamikaze ! "
<<
endl
;
// Lorsqu'une Kamikaze ne détecte aucun bestiole autour d'elle
// la variable has_reset_orientation permettra de vérifier que
// la bestiole a relancé son mouvement avec une orientation
// aléatoire
has_reset_orientation
=
0
;
auto
nearestPet_cord
=
closestPets
.
back
()
->
getCoordinates
();
nearestPet_x
=
std
::
get
<
0
>
(
nearestPet_cord
);
nearestPet_y
=
std
::
get
<
1
>
(
nearestPet_cord
);
// La nouvelle orientation de la kamikaze sera fonction
// de ses cordonnées et celles de son plus proche voisin
double
abs_diff
=
nearestPet_x
-
x
;
double
ord_diff
=
nearestPet_y
-
y
;
double
hypothenuse
=
sqrt
(
pow
(
abs_diff
,
2
)
+
pow
(
ord_diff
,
2
));
if
(
hypothenuse
!=
0
)
{
// La nouvelle orientation de la kamikaze sera fonction
// de ses cordonnées et celles de son plus proche voisin
orientation
=
acos
(
abs_diff
/
hypothenuse
);
}
else
{
// Si la kamikaze ne détecte rien, on lui attribue une orientation
// aléatoire (si cela n'a pas été déjà fait)
cout
<<
"No neighbor for Kamikaze "
<<
endl
;
if
(
!
has_reset_orientation
){
cout
<<
"Reset Orientation for Kamikaze "
<<
orientation
<<
endl
;
has_reset_orientation
=
1
;
orientation
=
static_cast
<
double
>
(
rand
()
)
/
RAND_MAX
*
2.
*
M_PI
;
cout
<<
"No neighbor for Kamikaze "
<<
endl
;
if
(
!
has_reset_orientation
)
{
cout
<<
"Reset Orientation for Kamikaze "
<<
orientation
<<
endl
;
has_reset_orientation
=
1
;
orientation
=
static_cast
<
double
>
(
rand
()
)
/
RAND_MAX
*
2.
*
M_PI
;
}
}
// On définit les nouveaux paramètres de mouvement de la bestiole
MoveUtils
::
setMoveParameters
(
pet
,
x
,
y
,
xLim
,
yLim
,
orientation
,
speed
,
cumulX
,
cumulY
);
}
}
}
Loading