X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fcreature%2Fcreature.cpp;h=911d9d024f4ce7c58d4b69e4960ec42575e67ba4;hb=fe2ad02c75b369e2a055c38b8ee08a43f5f6c367;hp=1a88dba1ab7d76831972bdaa3b0fab4d3e7b0973;hpb=05c42da80ec11cd73cc8a1b6ff21ac817b02a6e6;p=blobs.git diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 1a88dba..911d9d0 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -209,28 +209,31 @@ void Creature::DoWork(double amount) noexcept { 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 { - if (Dead()) return; sim.SetDead(this); death = sim.Time(); @@ -321,6 +324,28 @@ 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); } @@ -415,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; @@ -697,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); @@ -712,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(); } @@ -927,6 +949,7 @@ 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);