]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/creature.cpp
remove outdated TODOs
[blobs.git] / src / creature / creature.cpp
index 57c1f53f720ca6804647fdf42fa4a1bea4423602..12924b94b9239651e212392868958d0d041f3915 100644 (file)
@@ -5,12 +5,14 @@
 #include "Situation.hpp"
 #include "Steering.hpp"
 
+#include "BlobBackgroundTask.hpp"
 #include "Goal.hpp"
 #include "IdleGoal.hpp"
 #include "InhaleNeed.hpp"
 #include "IngestNeed.hpp"
 #include "Need.hpp"
 #include "../app/Assets.hpp"
+#include "../math/const.hpp"
 #include "../world/Body.hpp"
 #include "../world/Planet.hpp"
 #include "../world/Simulation.hpp"
@@ -44,6 +46,7 @@ Creature::Creature(world::Simulation &sim)
 , on_death()
 , removable(false)
 , memory(*this)
+, bg_task()
 , needs()
 , goals()
 , situation()
@@ -127,7 +130,11 @@ double Creature::AgeLerp(double from, double to) const noexcept {
 }
 
 double Creature::Fertility() const noexcept {
-       return AgeLerp(CurProps().fertility, NextProps().fertility) / 3600.0;
+       return AgeLerp(CurProps().fertility, NextProps().fertility) * (1.0 / 3600.0);
+}
+
+double Creature::Mutability() const noexcept {
+       return GetProperties().mutability * (1.0 / 3600.0);
 }
 
 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
@@ -164,17 +171,28 @@ void Creature::Tick(double dt) {
                Situation::Derivative d(Step(c, dt));
                Situation::Derivative f(
                        (1.0 / 6.0) * (a.vel + 2.0 * (b.vel + c.vel) + d.vel),
-                       (1.0 / 6.0) * (a.acc + 2.0 * (b.acc + c.acc) + d.acc),
-                       (1.0 / 6.0) * (a.turn + 2.0 * (b.turn + c.turn) + d.turn)
+                       (1.0 / 6.0) * (a.acc + 2.0 * (b.acc + c.acc) + d.acc)
                );
                state.pos += f.vel * dt;
                state.vel += f.acc * dt;
-               constexpr double turn_speed = 10.0;
-               // TODO: this is crap
-               state.dir = glm::normalize(state.dir + f.turn * turn_speed * dt);
+               if (length2(state.vel) > 0.000001) {
+                       glm::dvec3 nvel(normalize(state.vel));
+                       double ang = angle(nvel, state.dir);
+                       double turn_rate = PI * dt;
+                       if (ang < turn_rate) {
+                               state.dir = normalize(state.vel);
+                       } else if (std::abs(ang - PI) < 0.001) {
+                               state.dir = rotate(state.dir, turn_rate, world::Planet::SurfaceNormal(situation.Surface()));
+                       } else {
+                               state.dir = rotate(state.dir, turn_rate, normalize(cross(state.dir, nvel)));
+                       }
+               }
+
                situation.SetState(state);
        }
 
+       bg_task->Tick(dt);
+       bg_task->Action();
        memory.Tick(dt);
        for (auto &need : needs) {
                need->Tick(dt);
@@ -207,21 +225,19 @@ Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt)
        Situation::State s = situation.GetState();
        s.pos += ds.vel * dt;
        s.vel += ds.acc * dt;
-       s.dir = normalize(s.dir + ds.turn * dt);
        return {
                s.vel,
-               steering.Acceleration(s),
-               allzero(s.vel) ? glm::dvec3(0.0) : normalize(s.vel) - s.dir
+               steering.Acceleration(s)
        };
 }
 
 glm::dmat4 Creature::LocalTransform() noexcept {
-       // TODO: surface transform
        const double half_size = size * 0.5;
        const glm::dvec3 &pos = situation.Position();
+       const glm::dmat3 srf(world::Planet::SurfaceOrientation(situation.Surface()));
        return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
-               * glm::dmat4(world::Planet::SurfaceOrientation(situation.Surface()))
-               * glm::rotate(glm::orientedAngle(glm::dvec3(0.0, 0.0, -1.0), situation.Heading(), glm::dvec3(0.0, 1.0, 0.0)), glm::dvec3(0.0, 1.0, 0.0))
+               * glm::rotate(glm::orientedAngle(-srf[2], situation.Heading(), srf[1]), srf[1])
+               * glm::dmat4(srf)
                * glm::scale(glm::dvec3(half_size, half_size, half_size));
 }
 
@@ -316,6 +332,7 @@ void Creature::Draw(graphics::Viewport &viewport) {
 void Spawn(Creature &c, world::Planet &p) {
        p.AddCreature(&c);
        c.GetSituation().SetPlanetSurface(p, 0, p.TileCenter(0, p.SideLength() / 2, p.SideLength() / 2));
+       c.GetSituation().Heading(-world::Planet::SurfaceOrientation(0)[2]);
 
        // probe surrounding area for common resources
        int start = p.SideLength() / 2 - 2;
@@ -485,6 +502,7 @@ void Genome::Configure(Creature &c) const {
        c.Density(mass / volume);
        c.GetSteering().MaxAcceleration(1.4 * std::atan(c.GetProperties().strength));
        c.GetSteering().MaxSpeed(4.4 * std::atan(c.GetProperties().dexerty));
+       c.SetBackgroundTask(std::unique_ptr<Goal>(new BlobBackgroundTask(c)));
        c.AddGoal(std::unique_ptr<Goal>(new IdleGoal(c)));
 }
 
@@ -493,7 +511,6 @@ void Split(Creature &c) {
        Creature *a = new Creature(c.GetSimulation());
        const Situation &s = c.GetSituation();
        a->Name(c.GetSimulation().Assets().name.Sequential());
-       // TODO: mutate
        c.GetGenome().Configure(*a);
        s.GetPlanet().AddCreature(a);
        // TODO: duplicate situation somehow