]> git.localhorst.tv Git - blobs.git/commitdiff
more variety over age
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Nov 2017 23:03:58 +0000 (00:03 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 27 Nov 2017 23:03:58 +0000 (00:03 +0100)
src/creature/Creature.hpp
src/creature/Genome.hpp
src/creature/creature.cpp
src/world/world.cpp

index e2f9b446ab010600a4541d78449d21f949aea168..f443e48940d2caa20da318c706f196f939a667fd 100644 (file)
@@ -108,6 +108,7 @@ private:
 
        Genome genome;
        Genome::Properties<double> properties;
+       int cur_prop;
 
        double mass;
        double density;
index 057c6609915abc0cece270fbdb197a426215eb41..882a8a28f3b9c5e6acf1671c4a6883c944c9339c 100644 (file)
@@ -2,6 +2,7 @@
 #define BLOBS_CREATURE_GENOME_HPP_
 
 #include "../math/Distribution.hpp"
+#include "../math/GaloisLFSR.hpp"
 
 #include <vector>
 
@@ -17,17 +18,27 @@ class Creature;
 struct Genome {
 
        template<class T>
-       struct Properties {
-               T birth_mass;
-               T fertile_mass;
-               T max_mass;
-
-               T fertile_age;
-               T infertile_age;
-               T death_age;
-
+       struct PropertySet {
+               T age;
+               T mass;
                T fertility;
        };
+       template<class T>
+       struct Properties {
+               PropertySet<T> props[6];
+               PropertySet<T> &Birth() noexcept { return props[0]; }
+               const PropertySet<T> &Birth() const noexcept { return props[0]; }
+               PropertySet<T> &Child() noexcept { return props[1]; }
+               const PropertySet<T> &Child() const noexcept { return props[1]; }
+               PropertySet<T> &Youth() noexcept { return props[2]; }
+               const PropertySet<T> &Youth() const noexcept { return props[2]; }
+               PropertySet<T> &Adult() noexcept { return props[3]; }
+               const PropertySet<T> &Adult() const noexcept { return props[3]; }
+               PropertySet<T> &Elder() noexcept { return props[4]; }
+               const PropertySet<T> &Elder() const noexcept { return props[4]; }
+               PropertySet<T> &Death() noexcept { return props[5]; }
+               const PropertySet<T> &Death() const noexcept { return props[5]; }
+       };
        Properties<math::Distribution> properties;
 
 
@@ -48,6 +59,31 @@ struct Genome {
 
        void Configure(Creature &) const;
 
+       static PropertySet<double> Instantiate(
+               const PropertySet<math::Distribution> &p,
+               math::GaloisLFSR &rand
+       ) noexcept {
+               return {
+                       p.age.FakeNormal(rand.SNorm()),
+                       p.mass.FakeNormal(rand.SNorm()),
+                       p.fertility.FakeNormal(rand.SNorm())
+               };
+       }
+
+       static Properties<double> Instantiate(
+               const Properties<math::Distribution> &p,
+               math::GaloisLFSR &rand
+       ) noexcept {
+               return {
+                       Instantiate(p.props[0], rand),
+                       Instantiate(p.props[1], rand),
+                       Instantiate(p.props[2], rand),
+                       Instantiate(p.props[3], rand),
+                       Instantiate(p.props[4], rand),
+                       Instantiate(p.props[5], rand)
+               };
+       }
+
 };
 
 }
index d870755cfa8ae46607c62d7b4be120c5755b290c..6c9fb8596680e496804b6f3174a855ed6992164c 100644 (file)
@@ -31,6 +31,8 @@ Creature::Creature(world::Simulation &sim)
 : sim(sim)
 , name()
 , genome()
+, properties()
+, cur_prop(0)
 , mass(1.0)
 , density(1.0)
 , size(1.0)
@@ -50,7 +52,7 @@ Creature::~Creature() {
 }
 
 void Creature::Grow(double amount) noexcept {
-       Mass(std::min(properties.max_mass, mass + amount));
+       Mass(std::min(properties.props[cur_prop].mass, mass + amount));
 }
 
 void Creature::Hurt(double dt) noexcept {
@@ -81,13 +83,8 @@ double Creature::Age() const noexcept {
 }
 
 double Creature::Fertility() const noexcept {
-       double age = Age();
-       if (mass < properties.fertile_mass
-               || age < properties.fertile_age
-               || age > properties.infertile_age) {
-               return 0.0;
-       }
-       return properties.fertility / 3600.0;
+       // TODO: lerp based on age?
+       return properties.props[cur_prop].fertility / 3600.0;
 }
 
 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
@@ -105,6 +102,15 @@ bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b)
 }
 
 void Creature::Tick(double dt) {
+       if (cur_prop < 5 && Age() > properties.props[cur_prop + 1].age) {
+               ++cur_prop;
+               if (cur_prop == 5) {
+                       std::cout << "[" << int(sim.Time()) << "s] "
+                               << name << " died of old age" << std::endl;
+                       Die();
+               }
+       }
+
        {
                Situation::State state(situation.GetState());
                Situation::Derivative a(Step(Situation::Derivative(), 0.0));
@@ -120,11 +126,6 @@ void Creature::Tick(double dt) {
                situation.SetState(state);
        }
 
-       if (Age() > properties.death_age) {
-               std::cout << "[" << int(sim.Time()) << "s] "
-               << name << " died of old age" << std::endl;
-       }
-
        memory.Tick(dt);
        for (auto &need : needs) {
                need->Tick(dt);
@@ -296,13 +297,30 @@ void Spawn(Creature &c, world::Planet &p) {
        }
 
        Genome genome;
-       genome.properties.birth_mass = { 0.5, 0.1 };
-       genome.properties.fertile_mass = { 1.0, 0.1 };
-       genome.properties.max_mass = { 1.2, 0.1 };
-       genome.properties.fertile_age = { 60.0, 5.0 };
-       genome.properties.infertile_age = { 700.0, 30.0 };
-       genome.properties.death_age = { 900.0, 90.0 };
-       genome.properties.fertility = { 0.5, 0.01 };
+
+       genome.properties.Birth().age = { 0.0, 0.0 };
+       genome.properties.Birth().mass = { 0.5, 0.05 };
+       genome.properties.Birth().fertility = { 0.0, 0.0 };
+
+       genome.properties.Child().age = { 30.0, 1.0 };
+       genome.properties.Child().mass = { 0.7, 0.05 };
+       genome.properties.Child().fertility = { 0.0, 0.0 };
+
+       genome.properties.Youth().age = { 60.0, 5.0 };
+       genome.properties.Youth().mass = { 0.9, 0.1 };
+       genome.properties.Youth().fertility = { 0.5, 0.03 };
+
+       genome.properties.Adult().age = { 120.0, 10.0 };
+       genome.properties.Adult().mass = { 1.2, 0.1 };
+       genome.properties.Adult().fertility = { 0.4, 0.01 };
+
+       genome.properties.Elder().age = { 360.0, 30.0 };
+       genome.properties.Elder().mass = { 1.0, 0.05 };
+       genome.properties.Elder().fertility = { 0.1, 0.01 };
+
+       genome.properties.Death().age = { 480.0, 60.0 };
+       genome.properties.Death().mass = { 0.9, 0.05 };
+       genome.properties.Death().fertility = { 0.0, 0.0 };
 
        if (p.HasAtmosphere()) {
                genome.composition.push_back({
@@ -341,13 +359,7 @@ void Genome::Configure(Creature &c) const {
 
        math::GaloisLFSR &random = c.GetSimulation().Assets().random;
 
-       c.GetProperties().birth_mass = properties.birth_mass.FakeNormal(random.SNorm());
-       c.GetProperties().fertile_mass = properties.fertile_mass.FakeNormal(random.SNorm());
-       c.GetProperties().max_mass = properties.max_mass.FakeNormal(random.SNorm());
-       c.GetProperties().fertile_age = properties.fertile_age.FakeNormal(random.SNorm());
-       c.GetProperties().infertile_age = properties.infertile_age.FakeNormal(random.SNorm());
-       c.GetProperties().death_age = properties.death_age.FakeNormal(random.SNorm());
-       c.GetProperties().fertility = properties.fertility.FakeNormal(random.SNorm());
+       c.GetProperties() = Instantiate(properties, random);
 
        double mass = 0.0;
        double volume = 0.0;
@@ -377,7 +389,7 @@ void Genome::Configure(Creature &c) const {
                c.AddNeed(std::move(need));
        }
 
-       c.Mass(c.GetProperties().birth_mass);
+       c.Mass(c.GetProperties().props[0].mass);
        c.Density(mass / volume);
        c.GetSteering().MaxAcceleration(1.4);
        c.GetSteering().MaxSpeed(4.4);
index 91f9bc6751fbaedcda6f67fd1378720c87eda680..19f6f4af6603607c3eb709a832defcad0215a26c 100644 (file)
@@ -142,6 +142,7 @@ void Body::Tick(double dt) {
        rotation += dt * AngularMomentum() / Inertia();
        Cache();
        for (creature::Creature *c : Creatures()) {
+               // TODO: this is self modifying, fix it fix it fix it
                c->Tick(dt);
        }
        for (auto c = Creatures().begin(); c != Creatures().end();) {