Skip to content

Commit

Permalink
sista::Field filled fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
FLAK-ZOSO committed Nov 16, 2023
1 parent 71e38c9 commit d7357c6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
59 changes: 42 additions & 17 deletions organism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ void Organism::breed(Organism* other) {
// Now we have to place the children in the field
for (Organism* child : children) {
sista::Coordinates new_coordinates = coordinates;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();

bool found = false;
for (int _ = 0; _ < 25; _++) {
int i = rand() % 5 + 1;
int j = rand() % 5 + 1;
Expand All @@ -220,22 +220,23 @@ void Organism::breed(Organism* other) {
#if DEBUG
debug << "\t" << child << " is placed at delta {" << i << ", " << j << "}" << std::endl;
#endif
goto found;
found = true;
break;
}
}
goto not_found;
found:
child->coordinates = new_coordinates;
field->addPawn((sista::Pawn*)child);
continue;
not_found:
#if DEBUG
debug << "\t" << child << " couldn't find a place to be born" << std::endl;
#endif
organisms.erase(std::find(organisms.begin(), organisms.end(), child));
children.erase(std::find(children.begin(), children.end(), child));
delete child;
return; // There's no space for other children
if (found) {
child->coordinates = new_coordinates;
field->addPawn((sista::Pawn*)child);
continue;
} else {
#if DEBUG
debug << "\t" << child << " couldn't find a place to be born" << std::endl;
#endif
organisms.erase(std::remove(organisms.begin(), organisms.end(), child), organisms.end());
children.erase(std::remove(children.begin(), children.end(), child), children.end());
// delete child;
return; // There's no space for other children
}
}
}

Expand Down Expand Up @@ -327,7 +328,7 @@ void Organism::eat(Food* food) {
health += food->energy;
health = std::min(dna->genes.at(Gene::STRENGTH)->value*10, health);
field->removePawn(food);
Food::foods.erase(std::find(Food::foods.begin(), Food::foods.end(), food));
Food::foods.erase(std::remove(Food::foods.begin(), Food::foods.end(), food), Food::foods.end());
delete food;
}

Expand All @@ -339,23 +340,47 @@ void Organism::breathe() {
}
if (breath >= Breath::AEROBIC) {
if (globals::oxygen < Organism::organisms.size() * 5) {
#if DEBUG
debug << "\t" << this << " is taking oxygen but the level is too low so it's taking damage" << std::endl;
#endif
health--;
}
if (breath > globals::oxygen) {
#if DEBUG
debug << "\t" << this << " is taking " << breath << " oxygen but there are only " << globals::oxygen << " in the atmosphere" << std::endl;
#endif
health -= (breath - globals::oxygen) * 10; // 10 damage per missing oxygen
#if DEBUG
debug << "\t" << this << " has taken " << (breath - globals::oxygen) * 10 << " damage" << std::endl;
#endif
}
} else if (breath <= Breath::PHOTOAUTOTROPH) {
if (globals::carbon_dioxide < Organism::organisms.size() * 3) {
if (globals::carbon_dioxide < Organism::organisms.size() * 5) {
#if DEBUG
debug << "\t" << this << " is taking carbon dioxide but the level is too low so it's taking damage" << std::endl;
#endif
health--;
}
if (std::abs(breath) > std::abs(globals::carbon_dioxide)) {
#if DEBUG
debug << "\t" << this << " is taking " << std::abs(breath) << " carbon dioxide but there are only " << globals::carbon_dioxide << " in the atmosphere" << std::endl;
#endif
health -= std::abs(globals::carbon_dioxide + breath) * 8; // 8 damage per missing carbon dioxide
#if DEBUG
debug << "\t" << this << " has taken " << std::abs(globals::carbon_dioxide + breath) * 8 << " damage" << std::endl;
#endif
}
}
globals::oxygen -= breath;
globals::carbon_dioxide += breath;
globals::oxygen = std::max(globals::oxygen, 0);
globals::carbon_dioxide = std::max(globals::carbon_dioxide, 0);
if (health <= 0) {
#if DEBUG
debug << "\t" << this << " died because it couldn't breathe" << std::endl;
#endif
dead_organisms.push_back(this);
}
}


Expand Down
Binary file modified starklag
Binary file not shown.
13 changes: 7 additions & 6 deletions starklag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ int main(int argc, char* argv[]) {
(short unsigned)(random_engine() % 30),
(short unsigned)(random_engine() % 50)
);
ANSI::ForegroundColor foreground_color = (ANSI::ForegroundColor)(random_engine() % 8 + 30);
ANSI::BackgroundColor background_color = (ANSI::BackgroundColor)(random_engine() % 8 + 40);
ANSI::ForegroundColor foreground_color = (ANSI::ForegroundColor)((random_engine() + rand()) % 8 + 30);
ANSI::BackgroundColor background_color = (ANSI::BackgroundColor)((random_engine() + rand()) % 8 + 40);
if ((int)foreground_color == (int)background_color - 10) {
foreground_color = ANSI::ForegroundColor::F_WHITE;
background_color = ANSI::BackgroundColor::B_BLACK;
Expand All @@ -116,8 +116,8 @@ int main(int argc, char* argv[]) {
field_.addPawn(pawn_);
}
// Set default atmosphere
globals::oxygen = Organism::organisms.size() * 20;
globals::carbon_dioxide = Organism::organisms.size() * 20;
globals::oxygen = Organism::organisms.size() * 100;
globals::carbon_dioxide = Organism::organisms.size() * 100;

// Create the food
for (int i = 0; i < 40; i++) {
Expand Down Expand Up @@ -243,6 +243,7 @@ int main(int argc, char* argv[]) {
// Clean the dead organisms
for (Organism* organism : Organism::dead_organisms) {
Organism::organisms.erase(std::remove(Organism::organisms.begin(), Organism::organisms.end(), organism), Organism::organisms.end());
field->removePawn(organism->getCoordinates());
}
Organism::dead_organisms.clear(); // I hope this doesn't cause a memory leak
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
Expand Down Expand Up @@ -344,7 +345,8 @@ void saveOrganisms() {
void loadOrganisms() {
// Load atmosphere
std::ifstream atmosphere("atmosphere-stats.txt");
while (atmosphere >> globals::oxygen >> globals::carbon_dioxide) {}
char comma;
while (atmosphere >> globals::oxygen >> comma >> globals::carbon_dioxide) {}
atmosphere.close();
// Load scenario from file
std::ifstream sklg_("organisms_set.sklg");
Expand Down Expand Up @@ -387,7 +389,6 @@ void loadOrganisms() {
organism->id = id;
Organism::id_counter = id;
sista::Pawn* pawn_ = (sista::Pawn*)(Entity*)organism;
// Organism::organisms.push_back(organism); // WTF? How could it work without this line in v0.8.1.air?
field->addPawn(pawn_);
}
}
Expand Down

0 comments on commit d7357c6

Please sign in to comment.