From: Daniel Karbach Date: Fri, 15 Dec 2017 11:41:16 +0000 (+0100) Subject: better heading implementation X-Git-Url: http://git.localhorst.tv/?p=blobs.git;a=commitdiff_plain;h=ae4f59520574caf5054d4a19cd76fa86c4a97264 better heading implementation well, hopefully better --- diff --git a/src/app/states.cpp b/src/app/states.cpp index 842b2f1..929656f 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -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())); diff --git a/src/creature/Creature.hpp b/src/creature/Creature.hpp index f22f3da..db0aa52 100644 --- a/src/creature/Creature.hpp +++ b/src/creature/Creature.hpp @@ -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 #include #include #include @@ -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 &&g) { bg_task = std::move(g); } - Goal &BackgroundTask() { return *bg_task; } + void SetBackgroundTask(std::unique_ptr &&g); + Goal &BackgroundTask(); void AddGoal(std::unique_ptr &&); const std::vector> &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; diff --git a/src/creature/Goal.hpp b/src/creature/Goal.hpp index 4d3a17f..1705b15 100644 --- a/src/creature/Goal.hpp +++ b/src/creature/Goal.hpp @@ -1,6 +1,8 @@ #ifndef BLOBS_CREATURE_GOAL_HPP_ #define BLOBS_CREATURE_GOAL_HPP_ +#include "Creature.hpp" + #include #include @@ -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; diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 7ad0657..0a8b460 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -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 &&g) { goals.emplace_back(std::move(g)); } +void Creature::SetBackgroundTask(std::unique_ptr &&g) { + bg_task = std::move(g); +} + +Goal &Creature::BackgroundTask() { + return *bg_task; +} + namespace { bool GoalCompare(const std::unique_ptr &a, const std::unique_ptr &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; diff --git a/src/creature/goal.cpp b/src/creature/goal.cpp index 958ebce..72fc9a8 100644 --- a/src/creature/goal.cpp +++ b/src/creature/goal.cpp @@ -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(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)); diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index bbe7dbd..c361df5 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -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"