Skip to content

Commit

Permalink
Adjust organism breathing and breeding probabilities based on air con…
Browse files Browse the repository at this point in the history
…ditions
  • Loading branch information
FLAK-ZOSO committed Nov 10, 2023
1 parent cba39be commit 356ff79
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
25 changes: 21 additions & 4 deletions organism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ void Organism::meet(Entity* other) {
}
}

bool areAirConditionsGood(Organism* organism) {
int breath = organism->dna->genes.at(Gene::BREATH)->value;
if (breath == Breath::ANAEROBIC) {
return false; // Anaerobic organisms don't need oxygen nor carbon dioxide
}
return globals::oxygen > globals::carbon_dioxide == breath > Breath::ANAEROBIC;
}
void Organism::meet(Organism* other) {
#if DEBUG
debug << this << " is trying to meet with " << other << std::endl;
Expand All @@ -134,8 +141,12 @@ void Organism::meet(Organism* other) {

if (attack_probability(random_engine)) {
this->attack(other);
} else if (breeding_probability(random_engine)) {
this->breed(other);
} else {
if (areAirConditionsGood(this)) {
this->breed(other); // Full probability of breeding when the atmosphere is perfect
} else if (breeding_probability(random_engine)) {
this->breed(other);
}
}
}

Expand Down Expand Up @@ -333,10 +344,16 @@ void Organism::breathe() {
return; // Anaerobic organisms don't need oxygen nor carbon dioxide
}
if (breath >= Breath::AEROBIC) {
if (globals::oxygen < Organism::organisms.size() * 5) {
health--;
}
if (breath > globals::oxygen) {
health -= (breath - globals::oxygen) * 10; // 10 damage per missing oxygen
}
} else if (breath <= Breath::PHOTOAUTOTROPH) {
if (globals::carbon_dioxide < Organism::organisms.size()) {
health--;
}
if (breath < - globals::carbon_dioxide) {
health += (globals::carbon_dioxide + breath) * 5; // 5 damage per missing carbon dioxide
}
Expand Down Expand Up @@ -391,7 +408,7 @@ bool Organism::breedable(const Organism* other) const {
too_different_alleles++;
}
}
if (too_different_alleles >= 3) {
if (too_different_alleles >= 4) {
#if DEBUG
debug << "\t" << this << " can't breed with " << other << " because they have " << too_different_alleles << " too different alleles" << std::endl;
#endif
Expand All @@ -400,5 +417,5 @@ bool Organism::breedable(const Organism* other) const {
debug << "\t" << this << " can breed with " << other << " because they have only " << too_different_alleles << " too different alleles" << std::endl;
#endif
}
return too_different_alleles < 3;
return too_different_alleles < 4;
}
Binary file modified starklag
Binary file not shown.
12 changes: 8 additions & 4 deletions starklag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ int main(int argc, char* argv[]) {
sista::clearScreen();
field->print(border);
Organism::dead_organisms.clear();
globals::oxygen = Organism::organisms.size() * 2;
globals::carbon_dioxide = Organism::organisms.size() * 2;
globals::oxygen = Organism::organisms.size() * 20;
globals::carbon_dioxide = Organism::organisms.size() * 20;

bool paused = false;
bool quit = false;
Expand Down Expand Up @@ -177,8 +177,6 @@ int main(int argc, char* argv[]) {
} else if (free_spaces == 1 && organism->stats.age > 50) {
organism->health -= 5;
}
// The organism has to breathe
organism->breathe();
// Check of death and aging
if (organism->left <= 0 || organism->health <= 0) {
#if DEBUG
Expand All @@ -193,6 +191,8 @@ int main(int argc, char* argv[]) {
organism->left--;
if (organism->health < organism->dna->genes.at(Gene::STRENGTH)->value*5)
organism->left--; // If the organism is weak, it will die faster
// The organism has to breathe
organism->breathe();
organism->move();
}
// All the organisms may meet
Expand Down Expand Up @@ -297,6 +297,10 @@ int main(int argc, char* argv[]) {
ANSI::reset();
field->print(border);
#endif
if (!Organism::organisms.size()) {
std::cout << "All the organisms are dead." << std::endl;
break;
}
}
for (Organism* organism : Organism::organisms) {
delete organism;
Expand Down

0 comments on commit 356ff79

Please sign in to comment.