]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/creature.cpp
little better seek ai
[blobs.git] / src / creature / creature.cpp
index 3c4f544f2166bbf2e7f5a045988fa6442d283898..043aed7353cd66d4c2d6fbfff8eaf8e830f25031 100644 (file)
@@ -1,27 +1,37 @@
 #include "Creature.hpp"
 #include "Situation.hpp"
+#include "Steering.hpp"
 
+#include "Goal.hpp"
 #include "InhaleNeed.hpp"
 #include "IngestNeed.hpp"
 #include "Need.hpp"
 #include "../app/Assets.hpp"
 #include "../world/Body.hpp"
 #include "../world/Planet.hpp"
+#include "../world/Simulation.hpp"
 #include "../world/TileType.hpp"
 
+#include <algorithm>
 #include <glm/gtx/transform.hpp>
 
 #include <iostream>
+#include <glm/gtx/io.hpp>
 
 
 namespace blobs {
 namespace creature {
 
-Creature::Creature()
-: name()
+Creature::Creature(world::Simulation &sim)
+: sim(sim)
+, name()
+, size(1.0)
 , health(1.0)
 , needs()
+, goals()
 , situation()
+, steering()
+, vel(0.0)
 , vao() {
 }
 
@@ -32,23 +42,66 @@ void Creature::Hurt(double dt) noexcept {
        health = std::max(0.0, health - dt);
 }
 
+void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
+       std::cout << "new goal: " << g->Describe() << std::endl;
+       g->Enable();
+       goals.emplace_back(std::move(g));
+}
+
+namespace {
+
+bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
+       return b->Urgency() < a->Urgency();
+}
+
+}
+
 void Creature::Tick(double dt) {
-       // update needs
+       // TODO: better integration method
+       glm::dvec3 acc(steering.Acceleration(*this));
+       situation.Move(vel * dt);
+       vel += acc * dt;
+
        for (auto &need : needs) {
                need->Tick(dt);
        }
+       for (auto &goal : goals) {
+               goal->Tick(dt);
+       }
        // do background stuff
        for (auto &need : needs) {
                need->ApplyEffect(*this, dt);
        }
+       if (goals.empty()) {
+               return;
+       }
+       // if active goal can be interrupted, check priorities
+       if (goals.size() > 1 && goals[0]->Interruptible()) {
+               Goal *old_top = &*goals[0];
+               std::sort(goals.begin(), goals.end(), GoalCompare);
+               Goal *new_top = &*goals[0];
+               if (new_top != old_top) {
+                       std::cout << "changing goal from " << old_top->Describe()
+                               << " to " << new_top->Describe() << std::endl;
+               }
+       }
+       goals[0]->Action();
+       for (auto goal = goals.begin(); goal != goals.end();) {
+               if ((*goal)->Complete()) {
+                       std::cout << "complete goal: " << (*goal)->Describe() << std::endl;
+                       goals.erase(goal);
+               } else {
+                       ++goal;
+               }
+       }
 }
 
 glm::dmat4 Creature::LocalTransform() noexcept {
        // TODO: surface transform
-       constexpr double half_height = 0.25;
+       const double half_size = size * 0.5;
        const glm::dvec3 &pos = situation.Position();
-       return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + situation.GetPlanet().Radius() + half_height))
-               * glm::scale(glm::dvec3(half_height, half_height, half_height));
+       return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
+               * glm::scale(glm::dvec3(half_size, half_size, half_size));
 }
 
 void Creature::BuildVAO() {
@@ -141,7 +194,8 @@ void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
 
 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
        p.AddCreature(&c);
-       c.GetSituation().SetPlanetSurface(p, 0, glm::dvec3(0.0, 0.0, 0.0));
+       c.GetSituation().SetPlanetSurface(p, 0, p.TileCenter(0, p.SideLength() / 2, p.SideLength() / 2));
+       c.Size(0.5);
 
        // probe surrounding area for common resources
        int start = p.SideLength() / 2 - 2;
@@ -149,7 +203,7 @@ void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
        std::map<int, double> yields;
        for (int y = start; y < end; ++y) {
                for (int x = start; x < end; ++x) {
-                       const world::TileType &t = assets.data.tiles[p.TileAt(0, x, y).type];
+                       const world::TileType &t = assets.data.tile_types[p.TileAt(0, x, y).type];
                        for (auto yield : t.resources) {
                                yields[yield.resource] += yield.ubiquity;
                        }
@@ -171,9 +225,9 @@ void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
 
        if (p.HasAtmosphere()) {
                std::cout << "require breathing " << assets.data.resources[p.Atmosphere()].label << std::endl;
-               std::unique_ptr<Need> need(new InhaleNeed(p.Atmosphere(), 0.25, 0.1));
+               std::unique_ptr<Need> need(new InhaleNeed(p.Atmosphere(), 0.5, 0.1));
                need->name = assets.data.resources[p.Atmosphere()].label;
-               need->gain = 0.25;
+               need->gain = 0.2;
                need->inconvenient = 0.4;
                need->critical = 0.95;
                c.AddNeed(std::move(need));
@@ -182,16 +236,16 @@ void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
                std::cout << "require drinking " << assets.data.resources[liquid].label << std::endl;
                std::unique_ptr<Need> need(new IngestNeed(liquid, 0.2, 0.01));
                need->name = assets.data.resources[liquid].label;
-               need->gain = 0.0001;
+               need->gain = 0.02;
                need->inconvenient = 0.6;
                need->critical = 0.95;
                c.AddNeed(std::move(need));
        }
        if (solid > -1) {
                std::cout << "require eating " << assets.data.resources[solid].label << std::endl;
-               std::unique_ptr<Need> need(new IngestNeed(solid, 0.03, 0.001));
+               std::unique_ptr<Need> need(new IngestNeed(solid, 0.1, 0.001));
                need->name = assets.data.resources[solid].label;
-               need->gain = 0.00001;
+               need->gain = 0.017;
                need->inconvenient = 0.6;
                need->critical = 0.95;
                c.AddNeed(std::move(need));
@@ -212,6 +266,34 @@ bool Situation::OnPlanet() const noexcept {
        return type == PLANET_SURFACE;
 }
 
+bool Situation::OnSurface() const noexcept {
+       return type == PLANET_SURFACE;
+}
+
+world::Tile &Situation::GetTile() const noexcept {
+       double side_length = planet->SideLength();
+       double offset = side_length * 0.5;
+       double x = std::max(0.0, std::min(side_length, position.x + offset));
+       double y = std::max(0.0, std::min(side_length, position.y + offset));
+       return planet->TileAt(surface, int(x), int(y));
+}
+
+const world::TileType &Situation::GetTileType() const noexcept {
+       return planet->GetSimulation().TileTypes()[GetTile().type];
+}
+
+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;
@@ -219,5 +301,63 @@ void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &po
        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;
+       }
+}
+
 }
 }