Skip to content
Snippets Groups Projects
Commit efd207ee authored by fuzzy_bunny's avatar fuzzy_bunny
Browse files

added all the behaviours, but there is a problem un the deallocation of behaviours...

parent bd3c0210
Branches
No related tags found
1 merge request!19Master
......@@ -2,6 +2,7 @@
*.o
*.csv
*.swp
*.old
main
......
......@@ -15,8 +15,8 @@ int main(){
int windowWidth = 1200; //640
int windowHeight = 800; //480
int delay = 30;
int startingNbPets = 30;
map<string, float> animalsDistribution = {{KamikazeBehaviour::getBehaviourName(), 30}, {FearfulBehaviour::getBehaviourName(), 30}, {GregariousBehaviour::getBehaviourName(), 20}, {"multiple", 20}};
int startingNbPets = 70;
map<string, float> animalsDistribution = {{KamikazeBehaviour::getBehaviourName(), 10}, {FearfulBehaviour::getBehaviourName(), 30}, {GregariousBehaviour::getBehaviourName(), 40}, {"multiple", 20}};
Aquarium ecosystem(windowWidth, windowHeight, delay, startingNbPets, animalsDistribution);
// for (int i = 1; i <= 20; ++i)
......
......@@ -9,6 +9,7 @@
#include <cmath>
#include <stdlib.h>
const double Animal::AFF_SIZE = 8.;
const double Animal::MAX_SPEED = 10.;
const double Animal::LIMIT_VIEW = 30.;
......@@ -30,17 +31,9 @@ Animal::Animal() {
orientation = static_cast<double>( rand() )/RAND_MAX*2.*M_PI;
speed = static_cast<double>( rand() )/RAND_MAX*MAX_SPEED;
isMultiple = 1;
//behaviour = new KamikazeBehaviour();
behaviour = FearfulBehaviour::getBehaviourInstance();
color = new T[ 3 ];
color[ 0 ] = static_cast<int>( static_cast<double>( rand() )/RAND_MAX*230. );
color[ 1 ] = static_cast<int>( static_cast<double>( rand() )/RAND_MAX*230. );
color[ 2 ] = static_cast<int>( static_cast<double>( rand() )/RAND_MAX*230. );}
behaviour = FearfulBehaviour::getBehaviourInstance();
}
Animal::Animal( const Animal & a ){
identity = ++next;
......@@ -56,13 +49,10 @@ Animal::Animal( const Animal & a ){
orientation = a.orientation;
speed = a.speed;
isMultiple = 1;
//behaviour = new KamikazeBehaviour();
behaviour = FearfulBehaviour::getBehaviourInstance();
color = new T[ 3 ];
memcpy( color, a.color, 3*sizeof(T) );}
memcpy( color, a.color, 3*sizeof(T) );
behaviour = FearfulBehaviour::getBehaviourInstance();
}
Animal::~Animal( void ){
......@@ -73,6 +63,7 @@ Animal::~Animal( void ){
cout << "dest Pet" << endl;
}
Animal& Animal::operator=(Animal&& p) noexcept
{
// Guard self assignment
......@@ -115,13 +106,13 @@ Animal& Animal::operator=(const Animal& p) noexcept
return *this;
}
void Animal::initCoords( int xLim, int yLim ){
x = rand() % xLim;
y = rand() % yLim;}
void Animal::move( int xLim, int yLim, Environment& myEnvironment){
behaviour->move(xLim,yLim,*this, myEnvironment);}
......@@ -133,6 +124,7 @@ void Animal::action( Environment & myEnvironment ){
}
}
void Animal::draw( UImg & support ){
double xt = x + cos( orientation )*AFF_SIZE/2.1;
double yt = y - sin( orientation )*AFF_SIZE/2.1;
......@@ -151,11 +143,13 @@ bool Animal::isDetecting( const Animal & a ) const{
dist = std::sqrt( (x-a.x)*(x-a.x) + (y-a.y)*(y-a.y) );
return ( dist <= LIMIT_VIEW );}
void Animal::decrement() {
// decrement the life of the animal
--life;
}
void Animal::onCollision(){
double proba = ((double) rand() / (RAND_MAX));
if (proba < this->getProbabilityOfFatalCollision()) {
......@@ -164,63 +158,65 @@ void Animal::onCollision(){
}
}
int Animal::getLife() const {
return life;
}
int Animal::getIdentity() const {
return this->identity;
}
double Animal::getProbabilityOfFatalCollision() const {
return probabilityOfFatalCollision;
}
std::tuple<int, int> Animal::getCoordinates(){
return std::make_tuple(this->x,this->y);
}
std::tuple<double, double> Animal::getCumul(){
return std::make_tuple(this->cumulX,this->cumulY);
}
std::tuple<double, double> Animal::getOrientationSpeed(){
return std::make_tuple(this->orientation,this->speed);
}
void Animal::setCoordinates(int new_x,int new_y){
this->x = new_x;
this->y = new_y;
}
std::tuple<double, double> Animal::getCumul(){
return std::make_tuple(this->cumulX,this->cumulY);
}
void Animal::setCumul(double new_cumul_x,double new_cumul_y){
this->cumulX = new_cumul_x;
this->cumulY = new_cumul_y;
}
std::tuple<double, double> Animal::getOrientationSpeed(){
return std::make_tuple(this->orientation,this->speed);
}
void Animal::setOrientationSpeed(double new_orientation,double new_speed){
this->orientation = new_orientation;
this->speed = new_speed;
}
BehaviourStrategy* choose_behaviour() {
BehaviourStrategy* behaviour;
int which_behaviour;
which_behaviour = rand() % 3 + 1;
if ( which_behaviour == 1 ){
// TODO
//behaviour = GregariousBehaviour::getBehaviourInstance();
behaviour = FearfulBehaviour::getBehaviourInstance();
behaviour = GregariousBehaviour::getBehaviourInstance();
}
if ( which_behaviour == 2 ){
behaviour = FearfulBehaviour::getBehaviourInstance();
}
if ( which_behaviour == 3 ){
// TODO
//behaviour = KamikazeBehaviour::getBehaviourInstance();
behaviour = FearfulBehaviour::getBehaviourInstance();
behaviour = KamikazeBehaviour::getBehaviourInstance();
}
return behaviour;
}
......@@ -250,29 +246,22 @@ void Animal::setColor(const T c[3]) {
void Animal::setBehaviour(string behaviourName) {
if (behaviourName == GregariousBehaviour::getBehaviourName()) {
this->setColor(GregariousBehaviour::getColor());
this->behaviour = GregariousBehaviour::getBehaviourInstance();
}
else {
if (behaviourName == FearfulBehaviour::getBehaviourName()) {
this->setColor(FearfulBehaviour::getColor());
this->behaviour = FearfulBehaviour::getBehaviourInstance();
// TODO TODO TODO
// if (behaviourName == GregariousBehaviour::getBehaviourName()) {
// this->setColor(GregariousBehaviour::getColor());
// // TODO
// //this->behaviour = GregariousBehaviour::getBehaviourInstance();
// this->behaviour = FearfulBehaviour::getBehaviourInstance();
// }
// else {
// if (behaviourName == FearfulBehaviour::getBehaviourName()) {
// this->setColor(FearfulBehaviour::getColor());
// this->behaviour = FearfulBehaviour::getBehaviourInstance();
// }
// else {
// if (behaviourName == KamikazeBehaviour::getBehaviourName()) {
// this->setColor(KamikazeBehaviour::getColor());
// // TODO
// //this->behaviour = KamikazeBehaviour::getBehaviourInstance();
// this->behaviour = FearfulBehaviour::getBehaviourInstance();
// }
// }
// }
}
else {
if (behaviourName == KamikazeBehaviour::getBehaviourName()) {
this->setColor(KamikazeBehaviour::getColor());
this->behaviour = KamikazeBehaviour::getBehaviourInstance();
}
}
}
}
......
......@@ -17,6 +17,7 @@ Aquarium::Aquarium( int width, int height, int _delay, int startingNbOfAnimals,
cout << "const Aquarium" << endl;
// TODO : fixer la liste des availableCaptorsAndAccessories
Statistics* statistics = new Statistics({Fin::getName(), Eyes::getName()});
// TODO : fixer la liste des availableCaptorsAndAccessories
......
#include "GregariousBehaviour.h"
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
std::string GregariousBehaviour::NAME = "Gregarious";
GregariousBehaviour* GregariousBehaviour::gregariousbehaviour= nullptr;
std::string GregariousBehaviour::NAME = "b_Gregarious";
std::string GregariousBehaviour::getBehaviourName(){
return NAME;
}
void GregariousBehaviour::getRidOfInstance(void){
delete gregariousbehaviour;
cout << " LA DESTRUCTION DE LA GREGAIRE EFFECTIVEMENT LIEU !" <<endl;
const T GregariousBehaviour::color[3] = {0, 230, 0};
const T* GregariousBehaviour::getColor() {
return color;
}
GregariousBehaviour* GregariousBehaviour::gregariousbehaviour= nullptr;
GregariousBehaviour* GregariousBehaviour::getBehaviourInstance(){
if (gregariousbehaviour == nullptr ){
gregariousbehaviour = new GregariousBehaviour();
......@@ -25,12 +29,21 @@ GregariousBehaviour* GregariousBehaviour::getBehaviourInstance(){
return gregariousbehaviour;
}
std::vector<Animal> GregariousBehaviour::nearestNeighbors(Animal& pet, Environment& myEnvironment){
void GregariousBehaviour::getRidOfInstance(void){
delete gregariousbehaviour;
cout << " LA DESTRUCTION DE LA GREGAIRE EFFECTIVEMENT LIEU !" <<endl;
}
GregariousBehaviour::~GregariousBehaviour() {
delete[] &color;
}
std::vector<Animal> GregariousBehaviour::nearestNeighbors(Animal& pet, Environment& myEnvironment){
std::vector<Animal> pets = myEnvironment.detectedNeighbors(pet);
return pets;}
void GregariousBehaviour::move(int xLim, int yLim, Animal& pet, Environment& myEnvironment) {
......
#ifndef GREGARIOUS_BEHAVIOUR_H
#define GREGARIOUS_BEHAVIOUR_H
#include "BehaviourStrategy.h"
#include <string>
#include <UImg.h>
class GregariousBehaviour: public BehaviourStrategy{
private:
static std::string NAME ;
static const T color[3];
static GregariousBehaviour* gregariousbehaviour;
const int LIMIT_SURROUNDING = 1;
GregariousBehaviour(){};
public:
~GregariousBehaviour(){}
~GregariousBehaviour();
static GregariousBehaviour* getBehaviourInstance();
static void getRidOfInstance();
static std::string getBehaviourName();
static const T* getColor();
std::vector<Animal> nearestNeighbors(Animal& pet, Environment& myEnvironment) override;
void move(int xLim, int yLim, Animal& pet, Environment& myEnvironment) override;
};
#endif
#include "KamikazeBehaviour.h"
#include <cstdlib>
#include <cmath>
#include <iostream>
......@@ -8,17 +9,20 @@
std::string KamikazeBehaviour::NAME = "Kamikaze";
KamikazeBehaviour* KamikazeBehaviour::kamikazebehaviour= nullptr;
std::string KamikazeBehaviour::getBehaviourName(){
return NAME;
}
void KamikazeBehaviour::getRidOfInstance(void){
delete kamikazebehaviour;
cout << " LA DESTRUCTION DE LA KAMIKAZE EFFECTIVEMENT LIEU !" <<endl;
const T KamikazeBehaviour::color[3] = {230, 0, 0};
const T* KamikazeBehaviour::getColor() {
return color;
}
KamikazeBehaviour* KamikazeBehaviour::kamikazebehaviour= nullptr;
KamikazeBehaviour* KamikazeBehaviour::getBehaviourInstance(){
if (kamikazebehaviour == nullptr ){
kamikazebehaviour = new KamikazeBehaviour();
......@@ -26,6 +30,17 @@ KamikazeBehaviour* KamikazeBehaviour::getBehaviourInstance(){
return kamikazebehaviour;
}
void KamikazeBehaviour::getRidOfInstance(void){
delete kamikazebehaviour;
cout << " LA DESTRUCTION DE LA KAMIKAZE EFFECTIVEMENT LIEU !" <<endl;
}
KamikazeBehaviour::~KamikazeBehaviour() {
delete[] &color;
}
std::vector<Animal> KamikazeBehaviour::nearestNeighbors(Animal& pet, Environment& myEnvironment){
double dist;
......
#ifndef KAMIKAZE_BEHAVIOUR_H
#define KAMIKAZE_BEHAVIOUR_H
#include "BehaviourStrategy.h"
#include <string>
class KamikazeBehaviour: public BehaviourStrategy{
private:
KamikazeBehaviour(){};
static std::string NAME ;
static const T color[3];
static KamikazeBehaviour* kamikazebehaviour;
bool has_reset_orientation = 0;
KamikazeBehaviour(){};
public:
~KamikazeBehaviour(){};
~KamikazeBehaviour();
static KamikazeBehaviour* getBehaviourInstance();
static void getRidOfInstance();
static std::string getBehaviourName();
static const T* getColor() ;
std::vector<Animal> nearestNeighbors(Animal& pet, Environment& myEnvironment) override;
void move(int xLim, int yLim, Animal& pet, Environment& myEnvironment) override;
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment