]> git.localhorst.tv Git - blobs.git/commitdiff
better heading implementation
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 15 Dec 2017 11:41:16 +0000 (12:41 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 15 Dec 2017 11:41:16 +0000 (12:41 +0100)
well, hopefully better

src/app/states.cpp
src/creature/Creature.hpp
src/creature/Goal.hpp
src/creature/creature.cpp
src/creature/goal.cpp
src/ui/ui.cpp

index 842b2f1e02c42a8630f4489b775fbb3a5190a172..929656f09f80f0dce90f2d5325cfe752eedbefdd 100644 (file)
@@ -187,7 +187,6 @@ void MasterState::OnRender(graphics::Viewport &viewport) {
 
        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()));
index f22f3da2e9cf461d5caabde8468046133dfcc65a..db0aa5212dc92e52014446331912b6ce5e275052 100644 (file)
@@ -3,7 +3,6 @@
 
 #include "Composition.hpp"
 #include "Genome.hpp"
-#include "Goal.hpp"
 #include "Memory.hpp"
 #include "Situation.hpp"
 #include "Steering.hpp"
@@ -11,6 +10,7 @@
 #include "../math/geometry.hpp"
 #include "../math/glm.hpp"
 
+#include <functional>
 #include <memory>
 #include <string>
 #include <vector>
@@ -30,6 +30,8 @@ namespace world {
 }
 namespace creature {
 
+class Goal;
+
 class Creature {
 
 public:
@@ -163,8 +165,8 @@ 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; }
@@ -177,6 +179,8 @@ public:
        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;
 
@@ -224,6 +228,8 @@ private:
 
        Situation situation;
        Steering steering;
+       glm::dvec3 heading_target;
+       bool heading_manual;
 
        // cached because steering makes heavy use of this
        double perception_range;
index 4d3a17f431382d616a8f4828f7e1f9f3ee64529b..1705b15c371e4d92db60a7c485320ec996a71355 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef BLOBS_CREATURE_GOAL_HPP_
 #define BLOBS_CREATURE_GOAL_HPP_
 
+#include "Creature.hpp"
+
 #include <functional>
 #include <string>
 
@@ -14,10 +16,6 @@ namespace math {
 }
 namespace creature {
 
-class Creature;
-class Situation;
-class Steering;
-
 class Goal {
 
 public:
@@ -30,10 +28,12 @@ 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;
index 7ad0657e0d176f2a14412b5eb984255e6f707706..0a8b460c09e8b568af1e138ae7ae962386fc98ca 100644 (file)
@@ -142,6 +142,8 @@ Creature::Creature(world::Simulation &sim)
 , 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)
@@ -224,10 +226,10 @@ void Creature::Ingest(int res, double amount) noexcept {
                                } 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
@@ -430,6 +432,14 @@ void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
        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) {
@@ -470,23 +480,27 @@ void Creature::TickState(double dt) {
        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 {
@@ -702,6 +716,7 @@ void Spawn(Creature &c, world::Planet &p) {
        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;
@@ -820,7 +835,7 @@ void Split(Creature &c) {
        // 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;
 
@@ -834,7 +849,7 @@ void Split(Creature &c) {
        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;
 
index 958ebce86ea9e7dbd20f57a81dc621516f7e0b56..72fc9a814d4eaec47708301b2c36b923c759275a 100644 (file)
@@ -47,24 +47,24 @@ void AttackGoal::Action() {
                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();
@@ -114,15 +114,15 @@ void BlobBackgroundTask::Tick(double dt) {
                // 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;
                }
        }
@@ -135,7 +135,7 @@ void BlobBackgroundTask::Action() {
 }
 
 void BlobBackgroundTask::CheckStats() {
-       Creature::Stats &stats = GetCreature().GetStats();
+       Creature::Stats &stats = GetStats();
 
        if (!breathing && stats.Breath().Bad()) {
                breathing = true;
@@ -174,13 +174,6 @@ void BlobBackgroundTask::CheckStats() {
                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() {
@@ -219,22 +212,6 @@ Goal::Goal(Creature &c)
 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();
 }
@@ -301,8 +278,15 @@ std::string IdleGoal::Describe() const {
 }
 
 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();
        }
 }
@@ -530,7 +514,6 @@ void LocateResourceGoal::SearchVicinity() {
                        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));
index bbe7dbdb15db5ae6572324b73ac1de31dbd3068b..c361df55b06858c9d1271cf9c0fc37aa320c4f02 100644 (file)
@@ -8,6 +8,7 @@
 #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"