+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.Resources()[c.resource].density;
+ if (sim.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 {
+ if (sim.Resources()[res].state == world::Resource::SOLID) {
+ // 30% of solids stays in body
+ AddMass(res, amount * 0.3 * composition.Compatibility(res));
+ } else {
+ // 10% of fluids stays in body
+ AddMass(res, amount * 0.1 * composition.Compatibility(res));
+ }
+ math::GaloisLFSR &random = sim.Assets().random;
+ if (random.UNorm() < AdaptChance()) {
+ // change color to be slightly more like resource
+ glm::dvec3 color(rgb2hsl(sim.Resources()[res].base_color));
+ // solids affect base color, others highlight
+ int p = sim.Resources()[res].state == world::Resource::SOLID ? 0 : 1;
+ int q = random.UInt(3); // hue, sat, or val
+ int r = random.UInt(2); // mean or deviation
+ math::Distribution *d = nullptr;
+ double ref = 0.0;
+ if (p == 0) {
+ if (q == 0) {
+ d = &genome.base_hue;
+ ref = color.x;
+ } else if (q == 1) {
+ d = &genome.base_saturation;
+ ref = color.y;
+ } else {
+ d = &genome.base_lightness;
+ ref = color.z;
+ }
+ } else {
+ if (q == 0) {
+ d = &genome.highlight_hue;
+ ref = color.x;
+ } else if (q == 1) {
+ d = &genome.highlight_saturation;
+ ref = color.y;
+ } else {
+ d = &genome.highlight_lightness;
+ ref = color.z;
+ }
+ }
+ if (r == 0) {
+ double diff = ref - d->Mean();
+ if (q == 0) {
+ if (diff < -0.5) {
+ diff += 1.0;
+ } else if (diff > 0.5) {
+ diff -= 1.0;
+ }
+ // move 0-15% of distance
+ d->Mean(std::fmod(d->Mean() + diff * random.UNorm() * 0.15, 1.0));
+ } else {
+ d->Mean(glm::clamp(d->Mean() + diff * random.UNorm() * 0.15, 0.0, 1.0));
+ }
+ } else {
+ // scale by ±15%, enforce bounds
+ d->StandardDeviation(glm::clamp(d->StandardDeviation() * (1.0 + random.SNorm() * 0.15), 0.0001, 0.5));
+ }
+ }
+ if (sim.Resources()[res].state == world::Resource::LIQUID && random.UNorm() < AdaptChance()) {
+ // change texture randomly
+ // TODO: make change depending on surroundings and/or resource
+ int p = random.UInt(2); // back or side
+ int q = random.UInt(2); // mean or deviation
+ math::Distribution &d = p ? genome.skin_side : genome.skin_back;
+ if (q == 0) {
+ // move ± one standard deviation
+ d.Mean(d.Mean() + (random.SNorm() * d.StandardDeviation()));
+ } else {
+ // scale by ±10%, enforce bounds
+ d.StandardDeviation(glm::clamp(d.StandardDeviation() * (1.0 + random.SNorm() * 0.1), 0.0001, 0.5));
+ }
+ }
+}
+
+void Creature::DoWork(double amount) noexcept {
+ stats.Exhaustion().Add(amount / (Stamina() + 1.0));
+ // burn resources proportional to composition
+ // factor = 1/total * 1/efficiency * amount * -1
+ double factor = -amount / (composition.TotalMass() * EnergyEfficiency());
+ // make a copy to total remains constant and
+ // no entries disappear during iteration
+ Composition comp(composition);
+ for (auto &cmp : comp) {
+ double value = cmp.value * factor * sim.Resources()[cmp.resource].inverse_energy;
+ AddMass(cmp.resource, value);
+ }
+ // doing work improves strength a little
+ properties.Strength() += amount * 0.0001;
+}
+
+void Creature::Hurt(double amount) noexcept {
+ stats.Damage().Add(amount);
+ if (stats.Damage().Full()) {
+ Die();
+ }
+}
+
+void Creature::Die() noexcept {
+ if (Dead()) return;
+
+ if (stats.Damage().Full()) {
+ std::ostream &log = sim.Log() << name << " ";
+ if (stats.Exhaustion().Full()) {
+ log << "died of exhaustion";
+ } else if (stats.Breath().Full()) {
+ log << "suffocated";
+ } else if (stats.Thirst().Full()) {
+ log << "died of thirst";
+ } else if (stats.Hunger().Full()) {
+ log << "starved to death";
+ } else {
+ log << "succumed to wounds";
+ }
+ log << " at an age of " << ui::TimeString(Age())
+ << " (" << ui::PercentageString(Age() / properties.Lifetime())
+ << " of life expectancy of " << ui::TimeString(properties.Lifetime())
+ << ")" << std::endl;
+ }
+
+ sim.SetDead(this);
+ death = sim.Time();
+ steering.Off();
+ if (on_death) {
+ on_death(*this);
+ }
+ Remove();
+}
+
+bool Creature::Dead() const noexcept {
+ return death > birth;
+}
+
+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 Dead() ? death - birth : 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);
+ // guarantee at least 1%
+ return std::max(0.01, 1.0 - (3.0 * t * t) + (2.0 * t * t * t));
+}
+
+double Creature::EnergyEfficiency() const noexcept {
+ return 0.25 * AgeFactor(0.05);
+}
+
+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::StrengthFactor() const noexcept {
+ double str = Strength();
+ return str / (str + 1.0);
+}
+
+double Creature::Stamina() const noexcept {
+ return properties.Stamina() * ExhaustionFactor() * AgeFactor(0.25);
+}
+
+double Creature::StaminaFactor() const noexcept {
+ double stm = Stamina();
+ return stm / (stm + 1.0);
+}
+
+double Creature::Dexerty() const noexcept {
+ return properties.Dexerty() * ExhaustionFactor() * AgeFactor(0.25);
+}
+
+double Creature::DexertyFactor() const noexcept {
+ double dex = Dexerty();
+ return dex / (dex + 1.0);
+}
+
+double Creature::Intelligence() const noexcept {
+ return properties.Intelligence() * FatigueFactor() * AgeFactor(0.25);
+}
+
+double Creature::IntelligenceFactor() const noexcept {
+ double intl = Intelligence();
+ return intl / (intl + 1.0);
+}
+
+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::Adaptability() const noexcept {
+ return properties.Adaptability();
+}
+
+double Creature::OffspringMass() const noexcept {
+ return properties.OffspringMass();
+}
+
+double Creature::PerceptionRange() const noexcept {
+ return perception_range;
+}
+
+double Creature::PerceptionOmniRange() const noexcept {
+ return perception_omni_range;
+}
+
+double Creature::PerceptionField() const noexcept {
+ return perception_field;
+}
+
+bool Creature::PerceptionTest(const glm::dvec3 &p) const noexcept {
+ const glm::dvec3 diff(p - situation.Position());
+ double ldiff = glm::length2(diff);
+ if (ldiff < perception_omni_range_squared) return true;
+ if (ldiff > perception_range_squared) return false;
+ return glm::dot(diff / std::sqrt(ldiff), situation.Heading()) > perception_field;
+}
+
+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);
+}
+
+double Creature::AdaptChance() const noexcept {
+ return GetProperties().Adaptability() * (1.0 / 120.0);