+void Creature::AddMass(int res, double amount) {
+ composition.Add(res, amount);
+ double nonsolid = 0.0;
+ double volume = 0.0;
+ for (const auto &c : composition) {
+ volume += c.value / sim.Assets().data.resources[c.resource].density;
+ if (sim.Assets().data.resources[c.resource].state != world::Resource::SOLID) {
+ nonsolid += c.value;
+ }
+ }
+ Mass(composition.TotalMass());
+ Size(std::cbrt(volume));
+ highlight_color.a = nonsolid / composition.TotalMass();
+}
+
+void Creature::HighlightColor(const glm::dvec3 &c) noexcept {
+ highlight_color = glm::dvec4(c, highlight_color.a);
+}
+
+void Creature::Ingest(int res, double amount) noexcept {
+ // TODO: check foreign materials
+ if (sim.Resources()[res].state == world::Resource::SOLID) {
+ // 15% of solids stays in body
+ AddMass(res, amount * 0.15);
+ } else {
+ // 10% of fluids stays in body
+ AddMass(res, amount * 0.05);
+ }
+}
+
+void Creature::Hurt(double amount) noexcept {
+ stats.Damage().Add(amount);
+ if (stats.Damage().Full()) {
+ std::cout << "[" << ui::TimeString(sim.Time()) << "] " << name << " ";
+ if (stats.Exhaustion().Full()) {
+ std::cout << "died of exhaustion";
+ } else if (stats.Breath().Full()) {
+ std::cout << "suffocated";
+ } else if (stats.Thirst().Full()) {
+ std::cout << "died of thirst";
+ } else if (stats.Hunger().Full()) {
+ std::cout << "starved to death";
+ } else {
+ std::cout << "succumed to wounds";
+ }
+ std::cout << " at an age of " << ui::TimeString(Age())
+ << " (" << ui::PercentageString(Age() / properties.Lifetime())
+ << "% of life expectancy of " << ui::TimeString(properties.Lifetime())
+ << ")" << std::endl;
+ Die();
+ }
+}
+
+void Creature::Die() noexcept {
+ sim.SetDead(this);
+ death = sim.Time();
+ steering.Halt();
+ if (on_death) {
+ on_death(*this);
+ }
+ Remove();
+}
+
+void Creature::Remove() noexcept {
+ removable = true;
+}
+
+void Creature::Removed() noexcept {
+ bg_task.reset();
+ goals.clear();
+ memory.Erase();
+ KillVAO();
+}
+
+void Creature::AddParent(Creature &p) {
+ parents.push_back(&p);
+}
+
+double Creature::Age() const noexcept {
+ return sim.Time() - birth;
+}
+
+double Creature::AgeFactor(double peak) const noexcept {
+ // shifted inverse hermite, y = 1 - (3t² - 2t³) with t = normalized age - peak
+ // goes negative below -0.5 and starts to rise again above 1.0
+ double t = glm::clamp((Age() / properties.Lifetime()) - peak, -0.5, 1.0);
+ return 1.0 - (3.0 * t * t) + (2.0 * t * t * t);
+}
+
+double Creature::ExhaustionFactor() const noexcept {
+ return 1.0 - (glm::smoothstep(0.5, 1.0, stats.Exhaustion().value) * 0.5);
+}
+
+double Creature::FatigueFactor() const noexcept {
+ return 1.0 - (glm::smoothstep(0.5, 1.0, stats.Fatigue().value) * 0.5);
+}
+
+double Creature::Strength() const noexcept {
+ // TODO: replace all age factors with actual growth and decay
+ return properties.Strength() * ExhaustionFactor() * AgeFactor(0.25);
+}
+
+double Creature::Stamina() const noexcept {
+ return properties.Stamina() * ExhaustionFactor() * AgeFactor(0.25);
+}
+
+double Creature::Dexerty() const noexcept {
+ return properties.Dexerty() * ExhaustionFactor() * AgeFactor(0.25);
+}
+
+double Creature::Intelligence() const noexcept {
+ return properties.Intelligence() * FatigueFactor() * AgeFactor(0.25);
+}
+
+double Creature::Lifetime() const noexcept {
+ return properties.Lifetime();
+}
+
+double Creature::Fertility() const noexcept {
+ return properties.Fertility() * AgeFactor(0.25);
+}
+
+double Creature::Mutability() const noexcept {
+ return properties.Mutability();
+}
+
+double Creature::OffspringMass() const noexcept {
+ return properties.OffspringMass();
+}
+
+double Creature::OffspringChance() const noexcept {
+ return AgeFactor(0.25) * properties.Fertility() * (1.0 / 3600.0);
+}
+
+double Creature::MutateChance() const noexcept {
+ return GetProperties().Mutability() * (1.0 / 3600.0);
+}
+
+void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
+ g->Enable();
+ goals.emplace_back(std::move(g));
+}
+
+namespace {
+
+bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
+ return b->Urgency() < a->Urgency();
+}
+
+}