]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/creature.cpp
only show death message when damage is max
[blobs.git] / src / creature / creature.cpp
index fb6e9f6540b2872152bc390d07623cf779cfe26b..911d9d024f4ce7c58d4b69e4960ec42575e67ba4 100644 (file)
@@ -45,14 +45,17 @@ bool CompositionCompare(const Composition::Component &a, const Composition::Comp
 
 void Composition::Add(int res, double amount) {
        bool found = false;
-       for (auto &c : components) {
-               if (c.resource == res) {
-                       c.value += amount;
+       for (auto c = components.begin(); c != components.end(); ++c) {
+               if (c->resource == res) {
+                       c->value += amount;
+                       if (c->value <= 0.0) {
+                               components.erase(c);
+                       }
                        found = true;
                        break;
                }
        }
-       if (!found) {
+       if (!found && amount > 0.0) {
                components.emplace_back(res, amount);
        }
        std::sort(components.begin(), components.end(), CompositionCompare);
@@ -89,7 +92,7 @@ Creature::Creature(world::Simulation &sim)
 , mass(1.0)
 , size(1.0)
 , birth(sim.Time())
-, death(0.0)
+, death(-1.0)
 , on_death()
 , removable(false)
 , parents()
@@ -136,41 +139,115 @@ void Creature::Ingest(int res, double amount) noexcept {
                // 10% of fluids stays in body
                AddMass(res, amount * 0.05);
        }
+       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
+               double p = sim.Resources()[res].state == world::Resource::SOLID ? 0 : 1;
+               double q = random.UInt(3); // hue, sat, or val
+               double 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 ±15% of distance
+                               d->Mean(std::fmod(d->Mean() + diff * random.SNorm() * 0.15, 1.0));
+                       } else {
+                               d->Mean(glm::clamp(d->Mean() + diff * random.SNorm() * 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));
+               }
+       }
+}
+
+void Creature::DoWork(double amount) noexcept {
+       stats.Exhaustion().Add(amount / Stamina());
+       // 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);
+       }
 }
 
 void Creature::Hurt(double amount) noexcept {
        stats.Damage().Add(amount);
        if (stats.Damage().Full()) {
-               std::cout << "[" << ui::TimeString(sim.Time()) << "] " << name << " ";
+               Die();
+       }
+}
+
+void Creature::Die() noexcept {
+       if (Dead()) return;
+
+       if (stats.Damage().Full()) {
+               std::ostream &log = sim.Log() << name << " ";
                if (stats.Exhaustion().Full()) {
-                       std::cout << "died of exhaustion";
+                       log << "died of exhaustion";
                } else if (stats.Breath().Full()) {
-                       std::cout << "suffocated";
+                       log << "suffocated";
                } else if (stats.Thirst().Full()) {
-                       std::cout << "died of thirst";
+                       log << "died of thirst";
                } else if (stats.Hunger().Full()) {
-                       std::cout << "starved to death";
+                       log << "starved to death";
                } else {
-                       std::cout << "succumed to wounds";
+                       log << "succumed to wounds";
                }
-               std::cout << " at an age of " << ui::TimeString(Age())
+               log << " at an age of " << ui::TimeString(Age())
                        << " (" << ui::PercentageString(Age() / properties.Lifetime())
-                       << "% of life expectancy of " << ui::TimeString(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();
+       steering.Off();
        if (on_death) {
                on_death(*this);
        }
        Remove();
 }
 
+bool Creature::Dead() const noexcept {
+       return death > birth;
+}
+
 void Creature::Remove() noexcept {
        removable = true;
 }
@@ -194,7 +271,12 @@ 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);
+       // 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 {
@@ -234,10 +316,36 @@ 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 3.0 * (Dexerty() / (Dexerty() + 1)) + Size();
+}
+
+double Creature::PerceptionOmniRange() const noexcept {
+       return 0.5 * (Dexerty() / (Dexerty() + 1)) + Size();
+}
+
+double Creature::PerceptionField() const noexcept {
+       // this is the cosine of half the angle, so 1.0 is none, -1.0 is perfect
+       return 0.8 - (Dexerty() / (Dexerty() + 1));
+}
+
+bool Creature::PerceptionTest(const glm::dvec3 &p) const noexcept {
+       const glm::dvec3 diff(p - situation.Position());
+       double omni_range = PerceptionOmniRange();
+       if (length2(diff) < omni_range * omni_range) return true;
+       double range = PerceptionRange();
+       if (length2(diff) > range * range) return false;
+       return dot(normalize(diff), situation.Heading()) > PerceptionField();
+}
+
 double Creature::OffspringChance() const noexcept {
        return AgeFactor(0.25) * properties.Fertility() * (1.0 / 3600.0);
 }
@@ -246,6 +354,10 @@ double Creature::MutateChance() const noexcept {
        return GetProperties().Mutability() * (1.0 / 3600.0);
 }
 
+double Creature::AdaptChance() const noexcept {
+       return GetProperties().Adaptability() * (1.0 / 120.0);
+}
+
 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
        g->Enable();
        goals.emplace_back(std::move(g));
@@ -293,7 +405,8 @@ void Creature::TickState(double dt) {
                }
        }
        situation.SetState(state);
-       stats.Exhaustion().Add(length(f.acc) * Mass() / Stamina() * 0.5 * dt);
+       // work is force times distance
+       DoWork(length(f.acc) * Mass() * length(f.vel) * dt);
 }
 
 Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt) const noexcept {
@@ -327,7 +440,6 @@ void Creature::TickStats(double dt) {
        for (auto &s : stats.stat) {
                s.Add(s.gain * dt);
        }
-       stats.Breath().Add(stats.Breath().gain * stats.Exhaustion().value * dt);
        // TODO: damage values depending on properties
        if (stats.Breath().Full()) {
                constexpr double dps = 1.0 / 4.0;
@@ -523,7 +635,8 @@ void Spawn(Creature &c, world::Planet &p) {
        genome.properties.Intelligence() = { 1.0, 0.1 };
        genome.properties.Lifetime() = { 480.0, 60.0 };
        genome.properties.Fertility() = { 0.5, 0.03 };
-       genome.properties.Mutability() = { 1.0, 0.1 };
+       genome.properties.Mutability() = { 0.9, 0.1 };
+       genome.properties.Adaptability() = { 0.9, 0.1 };
        genome.properties.OffspringMass() = { 0.3, 0.02 };
 
        glm::dvec3 color_avg(0.0);
@@ -552,6 +665,10 @@ void Spawn(Creature &c, world::Planet &p) {
        genome.base_hue = { hsl.x, 0.01 };
        genome.base_saturation = { hsl.y, 0.01 };
        genome.base_lightness = { hsl.z, 0.01 };
+       // use opposite color as start highlight
+       genome.highlight_hue = { std::fmod(hsl.x + 0.5, 1.0), 0.01 };
+       genome.highlight_saturation = { 1.0 - hsl.y, 0.01 };
+       genome.highlight_lightness = { 1.0 - hsl.z, 0.01 };
 
        genome.Configure(c);
 }
@@ -578,9 +695,9 @@ void Genome::Configure(Creature &c) const {
                glm::clamp(base_lightness.FakeNormal(random.SNorm()), 0.0, 1.0)
        );
        glm::dvec3 highlight_color(
-               std::fmod(base_color.x + 0.5, 1.0),
-               1.0 - base_color.y,
-               1.0 - base_color.z
+               std::fmod(highlight_hue.FakeNormal(random.SNorm()) + 1.0, 1.0),
+               glm::clamp(highlight_saturation.FakeNormal(random.SNorm()), 0.0, 1.0),
+               glm::clamp(highlight_lightness.FakeNormal(random.SNorm()), 0.0, 1.0)
        );
        c.BaseColor(hsl2rgb(base_color));
        c.HighlightColor(hsl2rgb(highlight_color));
@@ -604,8 +721,7 @@ void Split(Creature &c) {
                s.GetPlanet(), s.Surface(),
                s.Position() + glm::dvec3(0.0, 0.55 * a->Size(), 0.0));
        a->BuildVAO();
-       std::cout << "[" << ui::TimeString(c.GetSimulation().Time()) << "] "
-               << a->Name() << " was born" << std::endl;
+       c.GetSimulation().Log() << a->Name() << " was born" << std::endl;
 
        Creature *b = new Creature(c.GetSimulation());
        b->AddParent(c);
@@ -619,8 +735,7 @@ void Split(Creature &c) {
                s.GetPlanet(), s.Surface(),
                s.Position() - glm::dvec3(0.0, 0.55 * b->Size(), 0.0));
        b->BuildVAO();
-       std::cout << "[" << ui::TimeString(c.GetSimulation().Time()) << "] "
-               << b->Name() << " was born" << std::endl;
+       c.GetSimulation().Log() << b->Name() << " was born" << std::endl;
 
        c.Die();
 }
@@ -709,9 +824,9 @@ bool Situation::OnSurface() const noexcept {
 }
 
 bool Situation::OnTile() const noexcept {
+       if (type != PLANET_SURFACE) return false;
        glm::ivec2 t(planet->SurfacePosition(surface, state.pos));
-       return type == PLANET_SURFACE
-               && t.x >= 0 && t.x < planet->SideLength()
+       return t.x >= 0 && t.x < planet->SideLength()
                && t.y >= 0 && t.y < planet->SideLength();
 }
 
@@ -781,6 +896,13 @@ Steering::Steering(const Creature &c)
 Steering::~Steering() {
 }
 
+void Steering::Off() noexcept {
+       separating = false;
+       halting = false;
+       seeking = false;
+       arriving = false;
+}
+
 void Steering::Separate(double min_distance, double max_lookaround) noexcept {
        separating = true;
        min_dist = min_distance;
@@ -827,51 +949,35 @@ glm::dvec3 Steering::Force(const Situation::State &s) const noexcept {
                        if (&*other == &c) continue;
                        glm::dvec3 diff = s.Position() - other->GetSituation().Position();
                        if (length2(diff) > max_look * max_look) continue;
+                       if (!c.PerceptionTest(other->GetSituation().Position())) continue;
                        double sep = length(diff) - other->Size() * 0.707 - c.Size() * 0.707;
                        if (sep < min_dist) {
                                repulse += normalize(diff) * (1.0 - sep / min_dist);
                        }
                }
-               SumForce(result, repulse, force);
+               result += repulse;
        }
        if (halting) {
-               SumForce(result, s.vel * -force, force);
+               // break twice as hard
+               result += -2.0 * s.vel * force;
        }
        if (seeking) {
                glm::dvec3 diff = target - s.pos;
                if (!allzero(diff)) {
-                       SumForce(result, TargetVelocity(s, (normalize(diff) * speed), force), force);
+                       result += TargetVelocity(s, (normalize(diff) * speed), force);
                }
        }
        if (arriving) {
                glm::dvec3 diff = target - s.pos;
                double dist = length(diff);
                if (!allzero(diff) && dist > std::numeric_limits<double>::epsilon()) {
-                       SumForce(result, TargetVelocity(s, diff * std::min(dist * force, speed) / dist, force), force);
+                       result += TargetVelocity(s, diff * std::min(dist * force, speed) / dist, force);
                }
        }
-       return result;
-}
-
-bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in, double max) const noexcept {
-       if (allzero(in) || anynan(in)) {
-               return false;
-       }
-       double cur = allzero(out) ? 0.0 : length(out);
-       double rem = max - cur;
-       if (rem < 0.0) {
-               return true;
-       }
-       double add = length(in);
-       if (add > rem) {
-               // this method is off if in and out are in different
-               // directions, but gives okayish results
-               out += in * (1.0 / add);
-               return true;
-       } else {
-               out += in;
-               return false;
+       if (length2(result) > max_force * max_force) {
+               result = normalize(result) * max_force;
        }
+       return result;
 }
 
 glm::dvec3 Steering::TargetVelocity(const Situation::State &s, const glm::dvec3 &vel, double acc) const noexcept {