int num_lights = 0;
for (auto sun : sim.Suns()) {
- // TODO: source sun's light color and strength
glm::vec3 pos(cam.View() * cam.Model(*sun)[3]);
assets.shaders.planet_surface.Activate();
assets.shaders.planet_surface.SetLight(num_lights, pos, glm::vec3(sun->Color()), float(sun->Luminosity()));
#include "Composition.hpp"
#include "Genome.hpp"
-#include "Goal.hpp"
#include "Memory.hpp"
#include "Situation.hpp"
#include "Steering.hpp"
#include "../math/geometry.hpp"
#include "../math/glm.hpp"
+#include <functional>
#include <memory>
#include <string>
#include <vector>
}
namespace creature {
+class Goal;
+
class Creature {
public:
const Memory &GetMemory() const noexcept { return memory; }
/// constantly active goal. every creature in the simulation is required to have one
- void SetBackgroundTask(std::unique_ptr<Goal> &&g) { bg_task = std::move(g); }
- Goal &BackgroundTask() { return *bg_task; }
+ void SetBackgroundTask(std::unique_ptr<Goal> &&g);
+ Goal &BackgroundTask();
void AddGoal(std::unique_ptr<Goal> &&);
const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
Steering &GetSteering() noexcept { return steering; }
const Steering &GetSteering() const noexcept { return steering; }
+ void HeadingTarget(const glm::dvec3 &t) noexcept { heading_target = t; heading_manual = true; }
+
math::AABB CollisionBounds() const noexcept;
glm::dmat4 CollisionTransform() const noexcept;
Situation situation;
Steering steering;
+ glm::dvec3 heading_target;
+ bool heading_manual;
// cached because steering makes heavy use of this
double perception_range;
#ifndef BLOBS_CREATURE_GOAL_HPP_
#define BLOBS_CREATURE_GOAL_HPP_
+#include "Creature.hpp"
+
#include <functional>
#include <string>
}
namespace creature {
-class Creature;
-class Situation;
-class Steering;
-
class Goal {
public:
public:
Creature &GetCreature() noexcept { return c; }
const Creature &GetCreature() const noexcept { return c; }
- Situation &GetSituation() noexcept;
- const Situation &GetSituation() const noexcept;
- Steering &GetSteering() noexcept;
- const Steering &GetSteering() const noexcept;
+ Creature::Stats &GetStats() noexcept { return c.GetStats(); }
+ const Creature::Stats &GetStats() const noexcept { return c.GetStats(); }
+ Situation &GetSituation() noexcept { return c.GetSituation(); }
+ const Situation &GetSituation() const noexcept { return c.GetSituation(); }
+ Steering &GetSteering() noexcept { return c.GetSteering(); }
+ const Steering &GetSteering() const noexcept { return c.GetSteering(); }
app::Assets &Assets() noexcept;
const app::Assets &Assets() const noexcept;
math::GaloisLFSR &Random() noexcept;
, goals()
, situation()
, steering(*this)
+, heading_target(0.0, 0.0, -1.0)
+, heading_manual(false)
, perception_range(1.0)
, perception_range_squared(1.0)
, perception_omni_range(1.0)
} else if (diff > 0.5) {
diff -= 1.0;
}
- // move ±15% of distance
- d->Mean(std::fmod(d->Mean() + diff * random.SNorm() * 0.15, 1.0));
+ // move 0-15% of distance
+ d->Mean(std::fmod(d->Mean() + diff * random.UNorm() * 0.15, 1.0));
} else {
- d->Mean(glm::clamp(d->Mean() + diff * random.SNorm() * 0.15, 0.0, 1.0));
+ d->Mean(glm::clamp(d->Mean() + diff * random.UNorm() * 0.15, 0.0, 1.0));
}
} else {
// scale by ±15%, enforce bounds
goals.emplace_back(std::move(g));
}
+void Creature::SetBackgroundTask(std::unique_ptr<Goal> &&g) {
+ bg_task = std::move(g);
+}
+
+Goal &Creature::BackgroundTask() {
+ return *bg_task;
+}
+
namespace {
bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
state.pos += f.vel * dt;
state.vel += f.acc * dt;
situation.EnforceConstraints(state);
- if (glm::length2(state.vel) > 0.000001) {
- glm::dvec3 nvel(glm::normalize(state.vel));
- double ang = glm::angle(nvel, state.dir);
- double turn_rate = PI * 0.75 * dt;
- if (ang < turn_rate) {
- state.dir = glm::normalize(state.vel);
- } else if (std::abs(ang - PI) < 0.001) {
- state.dir = glm::rotate(state.dir, turn_rate, situation.GetPlanet().NormalAt(state.pos));
- } else {
- state.dir = glm::rotate(state.dir, turn_rate, glm::normalize(glm::cross(state.dir, nvel)));
+
+ if (!heading_manual && glm::length2(state.vel) > 0.000001) {
+ const glm::dvec3 normal(situation.GetPlanet().NormalAt(state.pos));
+ const glm::dvec3 tangent(state.vel - (normal * glm::dot(state.vel, normal)));
+ if (glm::length2(tangent) > 0.000001) {
+ heading_target = glm::normalize(tangent);
}
}
+ double ang = glm::angle(heading_target, state.dir);
+ double turn_rate = PI * 0.75 * dt;
+ if (ang < turn_rate) {
+ state.dir = heading_target;
+ heading_manual = false;
+ } else {
+ state.dir = glm::rotate(state.dir, turn_rate, glm::normalize(glm::cross(state.dir, heading_target)));
+ }
+
situation.SetState(state);
// work is force times distance
- // exclude gravity for no apparent reason
- // actually, this should solely be based on steering force
- DoWork(glm::length(f.acc - situation.GetPlanet().GravityAt(state.pos)) * Mass() * glm::length(f.vel) * dt);
+ // keep 10% of gravity as a kind of background burn
+ DoWork(glm::length(f.acc - (0.9 * situation.GetPlanet().GravityAt(state.pos))) * Mass() * glm::length(f.vel) * dt);
}
Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt) const noexcept {
p.AddCreature(&c);
c.GetSituation().SetPlanetSurface(p, glm::dvec3(0.0, 0.0, p.Radius()));
c.GetSituation().Heading(glm::dvec3(1.0, 0.0, 0.0));
+ c.HeadingTarget(glm::dvec3(1.0, 0.0, 0.0));
// probe surrounding area for common resources
int start = p.SideLength() / 2 - 2;
// TODO: duplicate situation somehow
a->GetSituation().SetPlanetSurface(
s.GetPlanet(),
- s.Position() + glm::rotate(s.Heading() * a->Size() * 0.6, PI * 0.5, s.SurfaceNormal()));
+ s.Position() + glm::rotate(s.Heading() * a->Size() * 0.86, PI * 0.5, s.SurfaceNormal()));
a->BuildVAO();
c.GetSimulation().Log() << a->Name() << " was born" << std::endl;
s.GetPlanet().AddCreature(b);
b->GetSituation().SetPlanetSurface(
s.GetPlanet(),
- s.Position() + glm::rotate(s.Heading() * b->Size() * 0.6, PI * -0.5, s.SurfaceNormal()));
+ s.Position() + glm::rotate(s.Heading() * b->Size() * 0.86, PI * -0.5, s.SurfaceNormal()));
b->BuildVAO();
c.GetSimulation().Log() << b->Name() << " was born" << std::endl;
SetComplete();
return;
}
- const glm::dvec3 diff(GetCreature().GetSituation().Position() - target.GetSituation().Position());
+ const glm::dvec3 diff(GetSituation().Position() - target.GetSituation().Position());
const double hit_range = GetCreature().Size() * 0.5 * GetCreature().DexertyFactor();
const double hit_dist = hit_range + (0.5 * GetCreature().Size()) + 0.5 * (target.Size());
- if (GetCreature().GetStats().Damage().Critical()) {
+ if (GetStats().Damage().Critical()) {
// flee
- GetCreature().GetSteering().Pass(diff * 5.0);
- GetCreature().GetSteering().DontSeparate();
- GetCreature().GetSteering().Haste(1.0);
+ GetSteering().Pass(diff * 5.0);
+ GetSteering().DontSeparate();
+ GetSteering().Haste(1.0);
} else if (glm::length2(diff) > hit_dist * hit_dist) {
// full throttle chase
- GetCreature().GetSteering().Pass(target.GetSituation().Position());
- GetCreature().GetSteering().DontSeparate();
- GetCreature().GetSteering().Haste(1.0);
+ GetSteering().Pass(target.GetSituation().Position());
+ GetSteering().DontSeparate();
+ GetSteering().Haste(1.0);
} else {
// attack
- GetCreature().GetSteering().Halt();
- GetCreature().GetSteering().DontSeparate();
- GetCreature().GetSteering().Haste(1.0);
+ GetSteering().Halt();
+ GetSteering().DontSeparate();
+ GetSteering().Haste(1.0);
if (cooldown <= 0.0) {
constexpr double impulse = 0.05;
const double force = GetCreature().Strength();
// TODO: derive breathing ability
int gas = Assets().data.resources["air"].id;
// TODO: check if in compatible atmosphere
- double amount = GetCreature().GetStats().Breath().gain * -(1.0 + GetCreature().ExhaustionFactor());
- GetCreature().GetStats().Breath().Add(amount * dt);
+ double amount = GetStats().Breath().gain * -(1.0 + GetCreature().ExhaustionFactor());
+ GetStats().Breath().Add(amount * dt);
// maintain ~1% gas composition
double gas_amount = GetCreature().GetComposition().Get(gas);
if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.01) {
double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
GetCreature().Ingest(gas, add);
}
- if (GetCreature().GetStats().Breath().Empty()) {
+ if (GetStats().Breath().Empty()) {
breathing = false;
}
}
}
void BlobBackgroundTask::CheckStats() {
- Creature::Stats &stats = GetCreature().GetStats();
+ Creature::Stats &stats = GetStats();
if (!breathing && stats.Breath().Bad()) {
breathing = true;
eat_subtask->WhenComplete([&](Goal &) { eat_subtask = nullptr; });
GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
}
-
- // when in bad shape, don't make much effort
- if (stats.Damage().Bad() || stats.Exhaustion().Bad() || stats.Fatigue().Critical()) {
- GetCreature().GetSteering().DontSeparate();
- } else {
- GetCreature().GetSteering().ResumeSeparate();
- }
}
void BlobBackgroundTask::CheckSplit() {
Goal::~Goal() noexcept {
}
-Situation &Goal::GetSituation() noexcept {
- return c.GetSituation();
-}
-
-const Situation &Goal::GetSituation() const noexcept {
- return c.GetSituation();
-}
-
-Steering &Goal::GetSteering() noexcept {
- return c.GetSteering();
-}
-
-const Steering &Goal::GetSteering() const noexcept {
- return c.GetSteering();
-}
-
app::Assets &Goal::Assets() noexcept {
return c.GetSimulation().Assets();
}
}
void IdleGoal::Action() {
+ // when in bad shape, don't make much effort
+ if (GetStats().Damage().Bad() || GetStats().Exhaustion().Bad() || GetStats().Fatigue().Critical()) {
+ GetSteering().DontSeparate();
+ } else {
+ GetSteering().ResumeSeparate();
+ }
+
// use boredom as chance per minute
- if (Random().UNorm() < GetCreature().GetStats().Boredom().value * (1.0 / 3600.0)) {
+ if (Random().UNorm() < GetStats().Boredom().value * (1.0 / 3600.0)) {
PickActivity();
}
}
const world::TileType &type = planet.TileTypeAt(tpos);
auto yield = type.FindBestResource(accept);
if (yield != type.resources.cend()) {
- // TODO: subtract minimum yield
rating[y + search_radius][x + search_radius] = yield->ubiquity * accept.Get(yield->resource);
// penalize distance
double dist = std::max(0.125, 0.25 * glm::length2(tpos - pos));
#include "Meter.hpp"
#include "../app/Assets.hpp"
#include "../creature/Creature.hpp"
+#include "../creature/Goal.hpp"
#include "../graphics/Viewport.hpp"
#include "../math/const.hpp"
#include "../world/Body.hpp"