]> git.localhorst.tv Git - blobs.git/commitdiff
track where a creature has been
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Nov 2017 16:38:52 +0000 (17:38 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Nov 2017 16:38:52 +0000 (17:38 +0100)
src/creature/Creature.hpp
src/creature/Memory.hpp [new file with mode: 0644]
src/creature/creature.cpp

index 6c918270e540594bc18d0e19bd0240f80af50c80..7c06cc81d876a1c1464552c4c38e1807db4bf221 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "Genome.hpp"
 #include "Goal.hpp"
+#include "Memory.hpp"
 #include "Need.hpp"
 #include "Situation.hpp"
 #include "Steering.hpp"
@@ -76,6 +77,9 @@ public:
        void Remove() noexcept { removable = true; }
        bool Removable() const noexcept { return removable; }
 
+       Memory &GetMemory() noexcept { return memory; }
+       const Memory &GetMemory() const noexcept { return memory; }
+
        void AddNeed(std::unique_ptr<Need> &&n) { needs.emplace_back(std::move(n)); }
        const std::vector<std::unique_ptr<Need>> &Needs() const noexcept { return needs; }
 
@@ -115,6 +119,8 @@ private:
        Callback on_death;
        bool removable;
 
+       Memory memory;
+
        std::vector<std::unique_ptr<Need>> needs;
        std::vector<std::unique_ptr<Goal>> goals;
 
diff --git a/src/creature/Memory.hpp b/src/creature/Memory.hpp
new file mode 100644 (file)
index 0000000..a6910cc
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef BLOBS_CREATURE_MEMORY_HPP_
+#define BLOBS_CREATURE_MEMORY_HPP_
+
+#include "../math/glm.hpp"
+
+#include <map>
+
+
+namespace blobs {
+namespace world {
+       class Planet;
+}
+namespace creature {
+
+class Creature;
+
+class Memory {
+
+public:
+       struct Location {
+               world::Planet *planet;
+               int surface;
+               glm::ivec2 coords;
+       };
+
+public:
+       explicit Memory(Creature &);
+       ~Memory();
+
+public:
+       void Tick(double dt);
+
+private:
+       /// track time spent on a tile
+       void TrackStay(const Location &, double t);
+
+private:
+       Creature &c;
+
+       struct Stay {
+               double first_been;
+               Location first_loc;
+               double last_been;
+               Location last_loc;
+               double time_spent;
+       };
+       std::map<int, Stay> known_types;
+
+};
+
+}
+}
+
+#endif
index c83f11cea3168c388195da964b20bf118a8c5809..4345bd9837b668bdd96a2cc612514c22a25ae686 100644 (file)
@@ -1,5 +1,6 @@
 #include "Creature.hpp"
 #include "Genome.hpp"
+#include "Memory.hpp"
 #include "Situation.hpp"
 #include "Steering.hpp"
 
@@ -35,6 +36,7 @@ Creature::Creature(world::Simulation &sim)
 , health(1.0)
 , on_death()
 , removable(false)
+, memory(*this)
 , needs()
 , goals()
 , situation()
@@ -112,6 +114,7 @@ void Creature::Tick(double dt) {
                << name << " died of old age" << std::endl;
        }
 
+       memory.Tick(dt);
        for (auto &need : needs) {
                need->Tick(dt);
        }
@@ -391,6 +394,39 @@ 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
+               });
+       }
+}
+
+
 Situation::Situation()
 : planet(nullptr)
 , position(0.0)