+ std::unique_ptr<Need> need;
+ if (c.GetSimulation().Resources()[comp.resource].state == world::Resource::SOLID) {
+ need.reset(new IngestNeed(comp.resource, intake, penalty));
+ need->gain = intake * 0.05;
+ } else if (c.GetSimulation().Resources()[comp.resource].state == world::Resource::LIQUID) {
+ need.reset(new IngestNeed(comp.resource, intake, penalty));
+ need->gain = intake * 0.1;
+ } else {
+ need.reset(new InhaleNeed(comp.resource, intake, penalty));
+ need->gain = intake * 0.5;
+ }
+ need->name = c.GetSimulation().Resources()[comp.resource].label;
+ need->growth = comp.growth.FakeNormal(random.SNorm());
+ need->inconvenient = 0.5;
+ need->critical = 0.95;
+ c.AddNeed(std::move(need));
+ }
+
+ c.Mass(c.GetProperties().birth_mass);
+ c.Density(mass / volume);
+ c.GetSteering().MaxAcceleration(1.4);
+ c.GetSteering().MaxSpeed(4.4);
+ c.AddGoal(std::unique_ptr<Goal>(new IdleGoal(c)));
+}
+
+
+void Split(Creature &c) {
+ Creature *a = new Creature(c.GetSimulation());
+ const Situation &s = c.GetSituation();
+ // TODO: generate names
+ a->Name("Blobby");
+ // TODO: mutate
+ c.GetGenome().Configure(*a);
+ s.GetPlanet().AddCreature(a);
+ // TODO: duplicate situation somehow
+ a->GetSituation().SetPlanetSurface(
+ s.GetPlanet(), s.Surface(),
+ s.Position() + glm::dvec3(0.0, a->Size() * 0.51, 0.0));
+ a->BuildVAO();
+
+ Creature *b = new Creature(c.GetSimulation());
+ b->Name("Sir Blobalot");
+ c.GetGenome().Configure(*b);
+ s.GetPlanet().AddCreature(b);
+ b->GetSituation().SetPlanetSurface(
+ s.GetPlanet(), s.Surface(),
+ s.Position() + glm::dvec3(0.0, b->Size() * -0.51, 0.0));
+ b->BuildVAO();
+
+ c.Die();
+}
+
+
+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)
+, surface(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::OnTile() const noexcept {
+ glm::ivec2 t(planet->SurfacePosition(surface, position));
+ return type == PLANET_SURFACE
+ && t.x >= 0 && t.x < planet->SideLength()
+ && t.y >= 0 && t.y < planet->SideLength();
+}
+
+glm::ivec2 Situation::SurfacePosition() const noexcept {
+ return planet->SurfacePosition(surface, position);
+}
+
+world::Tile &Situation::GetTile() const noexcept {
+ glm::ivec2 t(planet->SurfacePosition(surface, position));
+ return planet->TileAt(surface, t.x, t.y);
+}
+
+const world::TileType &Situation::GetTileType() const noexcept {
+ glm::ivec2 t(planet->SurfacePosition(surface, position));
+ return planet->TypeAt(surface, t.x, t.y);
+}
+
+void Situation::Move(const glm::dvec3 &dp) noexcept {
+ position += dp;
+ if (OnSurface()) {
+ // enforce ground constraint
+ if (Surface() < 3) {
+ position[(Surface() + 2) % 3] = std::max(0.0, position[(Surface() + 2) % 3]);
+ } else {
+ position[(Surface() + 2) % 3] = std::min(0.0, position[(Surface() + 2) % 3]);
+ }
+ }
+}
+
+void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
+ type = PLANET_SURFACE;
+ planet = &p;
+ surface = srf;
+ position = pos;
+}
+
+
+Steering::Steering()
+: seek_target(0.0)
+, max_accel(1.0)
+, max_speed(1.0)
+, halting(false)
+, seeking(false) {
+}
+
+Steering::~Steering() {
+}
+
+void Steering::Halt() noexcept {
+ halting = true;
+ seeking = false;
+}
+
+void Steering::GoTo(const glm::dvec3 &t) noexcept {
+ seek_target = t;
+ halting = false;
+ seeking = true;
+}
+
+glm::dvec3 Steering::Acceleration(Creature &c) const noexcept {
+ glm::dvec3 acc(0.0);
+ if (halting) {
+ SumForce(acc, c.Velocity() * -max_accel);
+ }
+ if (seeking) {
+ glm::dvec3 diff = seek_target - c.GetSituation().Position();
+ if (!allzero(diff)) {
+ SumForce(acc, ((normalize(diff) * max_speed) - c.Velocity()) * max_accel);
+ }
+ }
+ return acc;
+}
+
+bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
+ if (allzero(in) || anynan(in)) {
+ return false;
+ }
+ double cur = allzero(out) ? 0.0 : length(out);
+ double rem = max_accel - cur;
+ if (rem < 0.0) {
+ return true;
+ }
+ double add = length(in);
+ if (add > rem) {
+ // this method is off if in and out are in different
+ // directions, but gives okayish results
+ out += in * (1.0 / add);
+ return true;
+ } else {
+ out += in;
+ return false;
+ }