From 838022c799cecd0e93826565e537a0667bded024 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 28 Nov 2017 00:03:58 +0100 Subject: [PATCH] more variety over age --- src/creature/Creature.hpp | 1 + src/creature/Genome.hpp | 54 +++++++++++++++++++++++++------ src/creature/creature.cpp | 68 +++++++++++++++++++++++---------------- src/world/world.cpp | 1 + 4 files changed, 87 insertions(+), 37 deletions(-) diff --git a/src/creature/Creature.hpp b/src/creature/Creature.hpp index e2f9b44..f443e48 100644 --- a/src/creature/Creature.hpp +++ b/src/creature/Creature.hpp @@ -108,6 +108,7 @@ private: Genome genome; Genome::Properties properties; + int cur_prop; double mass; double density; diff --git a/src/creature/Genome.hpp b/src/creature/Genome.hpp index 057c660..882a8a2 100644 --- a/src/creature/Genome.hpp +++ b/src/creature/Genome.hpp @@ -2,6 +2,7 @@ #define BLOBS_CREATURE_GENOME_HPP_ #include "../math/Distribution.hpp" +#include "../math/GaloisLFSR.hpp" #include @@ -17,17 +18,27 @@ class Creature; struct Genome { template - 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 + struct Properties { + PropertySet props[6]; + PropertySet &Birth() noexcept { return props[0]; } + const PropertySet &Birth() const noexcept { return props[0]; } + PropertySet &Child() noexcept { return props[1]; } + const PropertySet &Child() const noexcept { return props[1]; } + PropertySet &Youth() noexcept { return props[2]; } + const PropertySet &Youth() const noexcept { return props[2]; } + PropertySet &Adult() noexcept { return props[3]; } + const PropertySet &Adult() const noexcept { return props[3]; } + PropertySet &Elder() noexcept { return props[4]; } + const PropertySet &Elder() const noexcept { return props[4]; } + PropertySet &Death() noexcept { return props[5]; } + const PropertySet &Death() const noexcept { return props[5]; } + }; Properties properties; @@ -48,6 +59,31 @@ struct Genome { void Configure(Creature &) const; + static PropertySet Instantiate( + const PropertySet &p, + math::GaloisLFSR &rand + ) noexcept { + return { + p.age.FakeNormal(rand.SNorm()), + p.mass.FakeNormal(rand.SNorm()), + p.fertility.FakeNormal(rand.SNorm()) + }; + } + + static Properties Instantiate( + const Properties &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) + }; + } + }; } diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index d870755..6c9fb85 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -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 &&g) { @@ -105,6 +102,15 @@ bool GoalCompare(const std::unique_ptr &a, const std::unique_ptr &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); diff --git a/src/world/world.cpp b/src/world/world.cpp index 91f9bc6..19f6f4a 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -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();) { -- 2.39.2