+ c.AddMass(solid, 0.1);
+ color_avg += c.GetSimulation().Resources()[solid].base_color;
+ color_divisor += 1.0;
+ }
+
+ if (color_divisor > 0.001) {
+ color_avg /= color_divisor;
+ }
+ glm::dvec3 hsl = rgb2hsl(color_avg);
+ genome.base_hue = { hsl.x, 0.01 };
+ genome.base_saturation = { hsl.y, 0.01 };
+ genome.base_lightness = { hsl.z, 0.01 };
+ // use opposite color as start highlight
+ genome.highlight_hue = { std::fmod(hsl.x + 0.5, 1.0), 0.01 };
+ genome.highlight_saturation = { 1.0 - hsl.y, 0.01 };
+ genome.highlight_lightness = { 1.0 - hsl.z, 0.01 };
+
+ genome.Configure(c);
+}
+
+void Genome::Configure(Creature &c) const {
+ c.GetGenome() = *this;
+
+ math::GaloisLFSR &random = c.GetSimulation().Assets().random;
+
+ c.GetProperties() = Instantiate(properties, random);
+
+ // TODO: derive stats from properties
+ c.GetStats().Damage().gain = (-1.0 / 100.0);
+ c.GetStats().Breath().gain = (1.0 / 5.0);
+ c.GetStats().Thirst().gain = (1.0 / 60.0);
+ c.GetStats().Hunger().gain = (1.0 / 200.0);
+ c.GetStats().Exhaustion().gain = (-1.0 / 100.0);
+ c.GetStats().Fatigue().gain = (-1.0 / 100.0);
+ c.GetStats().Boredom().gain = (1.0 / 300.0);
+
+ glm::dvec3 base_color(
+ std::fmod(base_hue.FakeNormal(random.SNorm()) + 1.0, 1.0),
+ glm::clamp(base_saturation.FakeNormal(random.SNorm()), 0.0, 1.0),
+ glm::clamp(base_lightness.FakeNormal(random.SNorm()), 0.0, 1.0)
+ );
+ glm::dvec3 highlight_color(
+ std::fmod(highlight_hue.FakeNormal(random.SNorm()) + 1.0, 1.0),
+ glm::clamp(highlight_saturation.FakeNormal(random.SNorm()), 0.0, 1.0),
+ glm::clamp(highlight_lightness.FakeNormal(random.SNorm()), 0.0, 1.0)
+ );
+ c.BaseColor(hsl2rgb(base_color));
+ c.HighlightColor(hsl2rgb(highlight_color));
+ c.SetBackgroundTask(std::unique_ptr<Goal>(new BlobBackgroundTask(c)));
+ c.AddGoal(std::unique_ptr<Goal>(new IdleGoal(c)));
+}
+
+
+void Split(Creature &c) {
+ Creature *a = new Creature(c.GetSimulation());
+ const Situation &s = c.GetSituation();
+ a->AddParent(c);
+ a->Name(c.GetSimulation().Assets().name.Sequential());
+ c.GetGenome().Configure(*a);
+ for (const auto &cmp : c.GetComposition()) {
+ a->AddMass(cmp.resource, cmp.value * 0.5);
+ }
+ s.GetPlanet().AddCreature(a);
+ // TODO: duplicate situation somehow
+ a->GetSituation().SetPlanetSurface(
+ s.GetPlanet(),
+ s.Position() + glm::rotate(s.Heading() * a->Size() * 0.6, PI * 0.5, s.SurfaceNormal()));
+ a->BuildVAO();
+ c.GetSimulation().Log() << a->Name() << " was born" << std::endl;
+
+ Creature *b = new Creature(c.GetSimulation());
+ b->AddParent(c);
+ b->Name(c.GetSimulation().Assets().name.Sequential());
+ c.GetGenome().Configure(*b);
+ for (const auto &cmp : c.GetComposition()) {
+ b->AddMass(cmp.resource, cmp.value * 0.5);
+ }
+ s.GetPlanet().AddCreature(b);
+ b->GetSituation().SetPlanetSurface(
+ s.GetPlanet(),
+ s.Position() + glm::rotate(s.Heading() * b->Size() * 0.6, PI * -0.5, s.SurfaceNormal()));
+ b->BuildVAO();
+ c.GetSimulation().Log() << b->Name() << " was born" << std::endl;
+
+ c.Die();
+}
+
+
+Memory::Memory(Creature &c)
+: c(c) {
+}
+
+Memory::~Memory() {
+}
+
+void Memory::Erase() {
+ known_types.clear();
+ known_creatures.clear();
+}
+
+bool Memory::RememberLocation(const Composition &accept, glm::dvec3 &pos) const noexcept {
+ double best_rating = -1.0;
+ for (const auto &k : known_types) {
+ const world::TileType &t = c.GetSimulation().TileTypes()[k.first];
+ auto entry = t.FindBestResource(accept);
+ if (entry != t.resources.end()) {
+ double rating = entry->ubiquity / std::max(0.125, 0.25 * glm::length2(c.GetSituation().Position() - k.second.first_loc.position));
+ if (rating > best_rating) {
+ best_rating = rating;
+ pos = k.second.first_loc.position;
+ }
+ rating = entry->ubiquity / std::max(0.125, 0.25 * glm::length2(c.GetSituation().Position() - k.second.last_loc.position));
+ if (rating > best_rating) {
+ best_rating = rating;
+ pos = k.second.last_loc.position;
+ }
+ }
+ }
+ if (best_rating > 0.0) {
+ glm::dvec3 error(
+ c.GetSimulation().Assets().random.SNorm(),
+ c.GetSimulation().Assets().random.SNorm(),
+ c.GetSimulation().Assets().random.SNorm());
+ pos += error * (4.0 * (1.0 - c.IntelligenceFactor()));
+ pos = glm::normalize(pos) * c.GetSituation().GetPlanet().Radius();
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void Memory::TrackCollision(Creature &other) {
+ // TODO: find out whose fault it was
+ // TODO: source values from personality
+ Profile &p = known_creatures[&other];
+ p.annoyance += 0.1;
+ const double annoy_fact = p.annoyance / (p.annoyance + 1.0);
+ if (c.GetSimulation().Assets().random.UNorm() > annoy_fact * 0.1 * (1.0 - c.GetStats().Damage().value)) {
+ AttackGoal *g = new AttackGoal(c, other);
+ g->SetDamageTarget(annoy_fact);
+ g->Urgency(annoy_fact);
+ c.AddGoal(std::unique_ptr<Goal>(g));
+ p.annoyance *= 0.5;
+ }
+}
+
+void Memory::Tick(double dt) {
+ Situation &s = c.GetSituation();
+ if (s.OnSurface()) {
+ TrackStay({ &s.GetPlanet(), s.Position() }, dt);
+ }
+ // TODO: forget
+}
+
+void Memory::TrackStay(const Location &l, double t) {
+ const world::TileType &type = l.planet->TileTypeAt(l.position);
+ auto entry = known_types.find(type.id);
+ if (entry != known_types.end()) {
+ if (c.GetSimulation().Time() - entry->second.last_been > c.GetProperties().Lifetime() * 0.1) {
+ // "it's been ages"
+ if (entry->second.time_spent > c.Age() * 0.25) {
+ // the place is very familiar
+ c.GetStats().Boredom().Add(-0.2);
+ } else {
+ // infrequent stays
+ c.GetStats().Boredom().Add(-0.1);
+ }
+ }
+ 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
+ });
+ // completely new place, interesting
+ // TODO: scale by personality trait
+ c.GetStats().Boredom().Add(-0.25);
+ }
+}
+
+
+NameGenerator::NameGenerator()
+: counter(0) {
+}
+
+NameGenerator::~NameGenerator() {
+}
+
+std::string NameGenerator::Sequential() {
+ std::stringstream ss;
+ ss << "Blob " << ++counter;
+ return ss.str();
+}
+
+
+Situation::Situation()
+: planet(nullptr)
+, state(glm::dvec3(0.0), glm::dvec3(0.0))
+, type(LOST) {
+}
+
+Situation::~Situation() {
+}
+
+bool Situation::OnPlanet() const noexcept {
+ return type == PLANET_SURFACE;
+}
+
+bool Situation::OnSurface() const noexcept {
+ return type == PLANET_SURFACE;
+}
+
+bool Situation::OnGround() const noexcept {
+ return OnSurface() && glm::length2(state.pos) < (planet->Radius() + 0.05) * (planet->Radius() + 0.05);
+}
+
+glm::dvec3 Situation::SurfaceNormal() const noexcept {
+ return planet->NormalAt(state.pos);
+}
+
+world::Tile &Situation::GetTile() const noexcept {
+ return planet->TileAt(state.pos);
+}
+
+const world::TileType &Situation::GetTileType() const noexcept {
+ return planet->TileTypeAt(state.pos);
+}
+
+void Situation::Move(const glm::dvec3 &dp) noexcept {
+ state.pos += dp;
+ EnforceConstraints(state);
+}
+
+void Situation::Accelerate(const glm::dvec3 &dv) noexcept {
+ state.vel += dv;
+ EnforceConstraints(state);
+}
+
+void Situation::EnforceConstraints(State &s) const noexcept {
+ if (OnSurface()) {
+ double r = GetPlanet().Radius();
+ if (glm::length2(s.pos) < r * r) {
+ const glm::dvec3 normal(GetPlanet().NormalAt(s.pos));
+ s.pos = normal * r;
+ s.vel -= normal * glm::dot(normal, s.vel);
+ }