]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/creature.cpp
brake hard, not break hard
[blobs.git] / src / creature / creature.cpp
index 7333fdc42978693728e2d2bf599c9e3c159669b1..396385e9ce33253c512eea680c1f9a3ee255da5a 100644 (file)
@@ -266,7 +266,7 @@ void Creature::AddParent(Creature &p) {
 }
 
 double Creature::Age() const noexcept {
-       return sim.Time() - birth;
+       return Dead() ? death - birth : sim.Time() - birth;
 }
 
 double Creature::AgeFactor(double peak) const noexcept {
@@ -294,18 +294,34 @@ double Creature::Strength() const noexcept {
        return properties.Strength() * ExhaustionFactor() * AgeFactor(0.25);
 }
 
+double Creature::StrengthFactor() const noexcept {
+       return Strength() / (Strength() + 1.0);
+}
+
 double Creature::Stamina() const noexcept {
        return properties.Stamina() * ExhaustionFactor() * AgeFactor(0.25);
 }
 
+double Creature::StaminaFactor() const noexcept {
+       return Stamina() / (Stamina() + 1.0);
+}
+
 double Creature::Dexerty() const noexcept {
        return properties.Dexerty() * ExhaustionFactor() * AgeFactor(0.25);
 }
 
+double Creature::DexertyFactor() const noexcept {
+       return Dexerty() / (Dexerty() + 1.0);
+}
+
 double Creature::Intelligence() const noexcept {
        return properties.Intelligence() * FatigueFactor() * AgeFactor(0.25);
 }
 
+double Creature::IntelligenceFactor() const noexcept {
+       return Intelligence() / (Intelligence() + 1.0);
+}
+
 double Creature::Lifetime() const noexcept {
        return properties.Lifetime();
 }
@@ -327,16 +343,16 @@ double Creature::OffspringMass() const noexcept {
 }
 
 double Creature::PerceptionRange() const noexcept {
-       return 3.0 * (Dexerty() / (Dexerty() + 1)) + Size();
+       return 3.0 * DexertyFactor() + Size();
 }
 
 double Creature::PerceptionOmniRange() const noexcept {
-       return 0.5 * (Dexerty() / (Dexerty() + 1)) + Size();
+       return 0.5 * DexertyFactor() + 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));
+       return 0.8 - DexertyFactor();
 }
 
 bool Creature::PerceptionTest(const glm::dvec3 &p) const noexcept {
@@ -740,7 +756,7 @@ void Split(Creature &c) {
        // TODO: duplicate situation somehow
        a->GetSituation().SetPlanetSurface(
                s.GetPlanet(),
-               s.Position() + glm::dvec3(0.0, 0.55 * a->Size(), 0.0));
+               s.Position() + glm::rotate(s.Heading() * a->Size() * 0.6, PI * 0.5, s.SurfaceNormal()));
        a->BuildVAO();
        c.GetSimulation().Log() << a->Name() << " was born" << std::endl;
 
@@ -754,7 +770,7 @@ void Split(Creature &c) {
        s.GetPlanet().AddCreature(b);
        b->GetSituation().SetPlanetSurface(
                s.GetPlanet(),
-               s.Position() - glm::dvec3(0.0, 0.55 * b->Size(), 0.0));
+               s.Position() + glm::rotate(s.Heading() * b->Size() * 0.6, PI * -0.5, s.SurfaceNormal()));
        b->BuildVAO();
        c.GetSimulation().Log() << b->Name() << " was born" << std::endl;
 
@@ -773,11 +789,43 @@ void Memory::Erase() {
        known_types.clear();
 }
 
+bool Memory::RememberLocation(const Composition &accept, glm::dvec3 &pos) const noexcept {
+       double best_rating = -1.0;
+       for (const auto &k : known_types) {
+               const world::TileType &t = c.GetSimulation().TileTypes()[k.first];
+               auto entry = t.FindBestResource(accept);
+               if (entry != t.resources.end()) {
+                       double rating = entry->ubiquity / std::max(0.125, 0.25 * glm::length2(c.GetSituation().Position() - k.second.first_loc.position));
+                       if (rating > best_rating) {
+                               best_rating = rating;
+                               pos = k.second.first_loc.position;
+                       }
+                       rating = entry->ubiquity / std::max(0.125, 0.25 * glm::length2(c.GetSituation().Position() - k.second.last_loc.position));
+                       if (rating > best_rating) {
+                               best_rating = rating;
+                               pos = k.second.last_loc.position;
+                       }
+               }
+       }
+       if (best_rating > 0.0) {
+               glm::dvec3 error(
+                       c.GetSimulation().Assets().random.SNorm(),
+                       c.GetSimulation().Assets().random.SNorm(),
+                       c.GetSimulation().Assets().random.SNorm());
+               pos += error * (2.0 * (1.0 - c.IntelligenceFactor()));
+               pos = glm::normalize(pos) * c.GetSituation().GetPlanet().Radius();
+               return true;
+       } else {
+               return false;
+       }
+}
+
 void Memory::Tick(double dt) {
        Situation &s = c.GetSituation();
        if (s.OnSurface()) {
                TrackStay({ &s.GetPlanet(), s.Position() }, dt);
        }
+       // TODO: forget
 }
 
 void Memory::TrackStay(const Location &l, double t) {
@@ -843,6 +891,10 @@ bool Situation::OnSurface() const noexcept {
        return type == PLANET_SURFACE;
 }
 
+glm::dvec3 Situation::SurfaceNormal() const noexcept {
+       return planet->NormalAt(state.pos);
+}
+
 world::Tile &Situation::GetTile() const noexcept {
        return planet->TileAt(state.pos);
 }
@@ -955,8 +1007,8 @@ glm::dvec3 Steering::Force(const Situation::State &s) const noexcept {
                result += repulse;
        }
        if (halting) {
-               // break twice as hard
-               result += -2.0 * s.vel * force;
+               // brake hard
+               result += -5.0 * s.vel * force;
        }
        if (seeking) {
                glm::dvec3 diff = target - s.pos;