]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/creature.cpp
simple name generator
[blobs.git] / src / creature / creature.cpp
index c83f11cea3168c388195da964b20bf118a8c5809..f51f52ca4a16427cf75ab870577b8f5e71df45b6 100644 (file)
@@ -1,5 +1,7 @@
 #include "Creature.hpp"
 #include "Genome.hpp"
+#include "Memory.hpp"
+#include "NameGenerator.hpp"
 #include "Situation.hpp"
 #include "Steering.hpp"
 
@@ -15,6 +17,7 @@
 #include "../world/TileType.hpp"
 
 #include <algorithm>
+#include <sstream>
 #include <glm/gtx/transform.hpp>
 
 #include <iostream>
@@ -35,6 +38,7 @@ Creature::Creature(world::Simulation &sim)
 , health(1.0)
 , on_death()
 , removable(false)
+, memory(*this)
 , needs()
 , goals()
 , situation()
@@ -112,6 +116,7 @@ void Creature::Tick(double dt) {
                << name << " died of old age" << std::endl;
        }
 
+       memory.Tick(dt);
        for (auto &need : needs) {
                need->Tick(dt);
        }
@@ -368,7 +373,7 @@ void Split(Creature &c) {
        Creature *a = new Creature(c.GetSimulation());
        const Situation &s = c.GetSituation();
        // TODO: generate names
-       a->Name("Blobby");
+       a->Name(c.GetSimulation().Assets().name.Sequential());
        // TODO: mutate
        c.GetGenome().Configure(*a);
        s.GetPlanet().AddCreature(a);
@@ -379,7 +384,7 @@ void Split(Creature &c) {
        a->BuildVAO();
 
        Creature *b = new Creature(c.GetSimulation());
-       b->Name("Sir Blobalot");
+       b->Name(c.GetSimulation().Assets().name.Sequential());
        c.GetGenome().Configure(*b);
        s.GetPlanet().AddCreature(b);
        b->GetSituation().SetPlanetSurface(
@@ -391,6 +396,53 @@ void Split(Creature &c) {
 }
 
 
+Memory::Memory(Creature &c)
+: c(c) {
+}
+
+Memory::~Memory() {
+}
+
+void Memory::Tick(double dt) {
+       Situation &s = c.GetSituation();
+       if (s.OnSurface()) {
+               TrackStay({ &s.GetPlanet(), s.Surface(), s.SurfacePosition() }, dt);
+       }
+}
+
+void Memory::TrackStay(const Location &l, double t) {
+       const world::TileType &type = l.planet->TypeAt(l.surface, l.coords.x, l.coords.y);
+       auto entry = known_types.find(type.id);
+       if (entry != known_types.end()) {
+               entry->second.last_been = c.GetSimulation().Time();
+               entry->second.last_loc = l;
+               entry->second.time_spent += t;
+       } else {
+               known_types.emplace(type.id, Stay{
+                       c.GetSimulation().Time(),
+                       l,
+                       c.GetSimulation().Time(),
+                       l,
+                       t
+               });
+       }
+}
+
+
+NameGenerator::NameGenerator()
+: counter(0) {
+}
+
+NameGenerator::~NameGenerator() {
+}
+
+std::string NameGenerator::Sequential() {
+       std::stringstream ss;
+       ss << "Blob " << ++counter;
+       return ss.str();
+}
+
+
 Situation::Situation()
 : planet(nullptr)
 , position(0.0)