+void Creature::AddMass(int res, double amount) {
+ composition.Add(res, amount);
+ double mass = 0.0;
+ double nonsolid = 0.0;
+ double volume = 0.0;
+ for (const auto &c : composition) {
+ mass += c.value;
+ 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(mass);
+ Size(std::cbrt(volume));
+ highlight_color.a = nonsolid / mass;
+}
+
+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
+ // 10% stays in body
+ AddMass(res, amount * 0.1);
+}
+
+void Creature::Hurt(double amount) noexcept {
+ stats.Damage().Add(amount);
+ if (stats.Damage().Full()) {
+ std::cout << "[" << int(sim.Time()) << "s] " << 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 ";
+ {
+ int age = int(Age());
+ if (age >= 3600) {
+ std::cout << (age / 3600) << "h ";
+ age %= 3600;
+ }
+ if (age >= 60) {
+ std::cout << (age / 60) << "m ";
+ age %= 60;
+ }
+ std::cout << age << 's';
+ }
+ std::cout << " (" << int(Age() / properties.Lifetime() * 100)
+ << "% of life expectancy of ";
+ {
+ int lt = int(properties.Lifetime());
+ if (lt >= 3600) {
+ std::cout << (lt / 3600) << "h ";
+ lt %= 3600;
+ }
+ if (lt >= 60) {
+ std::cout << (lt / 60) << "m ";
+ lt %= 60;
+ }
+ std::cout << lt << 's';
+ }
+ std::cout << ")" << std::endl;
+ Die();
+ }
+}
+
+void Creature::Die() noexcept {
+ goals.clear();
+ steering.Halt();
+ if (on_death) {
+ on_death(*this);
+ }
+ Remove();
+}
+
+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);