-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCarnivore.cpp
More file actions
64 lines (58 loc) · 2.42 KB
/
Carnivore.cpp
File metadata and controls
64 lines (58 loc) · 2.42 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
#include "Carnivore.h"
#include "Meat.h"
Carnivore::Carnivore(double x, double y, int maxSpeed, double damage, double energy, unsigned int maxMating, unsigned int generationNumber, World *world):
Animal(x,y,maxSpeed,damage,energy,maxMating,generationNumber,world)
{
}
Carnivore::Carnivore(double x, double y, int maxSpeed, double damage, double energy, unsigned int maxMating, unsigned int generationNumber, World *world, bool sex):
Animal(x,y,maxSpeed,damage,energy,maxMating,generationNumber,world, sex)
{
}
Carnivore::Carnivore(double x, double y, int maxSpeed, double damage, double energy, unsigned int maxMating, unsigned int generationNumber, World *world, NeuralNetwork * brain, int mating):
Animal(x,y,maxSpeed,damage,energy,maxMating,generationNumber,world, brain, mating)
{
}
void Carnivore::tryToEat(std::shared_ptr<Entity> food)
{
//Carenivore are... carnivore... so they try to eat meat !
if(std::shared_ptr<Meat> meat = std::dynamic_pointer_cast<Meat>(food))
{
if(m_hunger > 0)
{
int quantity = std::min(config::EAT_MAX_MEAT_QUANTITY,(unsigned)m_hunger);
World::mutexMeat.lock();
double eatenQuantity = meat->eat(quantity);
World::mutexMeat.unlock();
m_hunger -= eatenQuantity;
//m_radius += config::FATNESS_CARNIVORE * eatenQuantity;
}
//heal himself
if(m_health < config::MAX_HEALTH && m_thirst < config::MAX_THIRST*3/4)
{
m_health += std::min(config::EAT_MAX_HEALING_VALUE,(unsigned)(config::MAX_HEALTH-m_health));
}
}
}
bool Carnivore::tryToMate(std::shared_ptr<Entity> carnivoreEntity)
{
if(std::shared_ptr<Carnivore> carnivoreToMate = std::dynamic_pointer_cast<Carnivore>(carnivoreEntity))
{
// this Animal is the female
if(m_female && !carnivoreToMate->isFemale())
{
//both carnivore are ready to mate
if(m_mating >= m_maxMating && carnivoreToMate->getMating() >= carnivoreToMate->getMaxMating())
{
//this carnivore (female) is neither hungry nor thirsty
if(m_thirst < (config::MAX_THIRST*3/4) && m_hunger < (config::MAX_HUNGER*3/4))
{
World::mutexMateList.lock();
m_world->updateMateList(this, carnivoreToMate);
World::mutexMateList.unlock();
return true;
}
}
}
}
return false;
}