]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/need.cpp
fix AI
[blobs.git] / src / creature / need.cpp
index 88b44197c8de80af4f6a0afcb297915c75053a33..576655a97185ec8740cde7fe6385d1128485509e 100644 (file)
@@ -3,7 +3,9 @@
 #include "IngestNeed.hpp"
 
 #include "Creature.hpp"
+#include "LocateResourceGoal.hpp"
 #include "../world/Planet.hpp"
+#include "../world/TileType.hpp"
 
 
 namespace blobs {
@@ -26,9 +28,11 @@ void Need::Decrease(double delta) noexcept {
 
 
 IngestNeed::IngestNeed(int resource, double speed, double damage)
-: resource(resource)
+: locate_goal(nullptr)
+, resource(resource)
 , speed(speed)
-, damage(damage) {
+, damage(damage)
+, ingesting(false) {
 }
 
 IngestNeed::~IngestNeed() {
@@ -36,11 +40,48 @@ IngestNeed::~IngestNeed() {
 
 void IngestNeed::ApplyEffect(Creature &c, double dt) {
        if (!IsSatisfied()) {
-               // TODO: find resource and start ingest task
+               ingesting = true;
+       }
+       if (ingesting) {
+               if (c.GetSituation().OnSurface()) {
+                       const world::TileType &t = c.GetSituation().GetTileType();
+                       bool found = false;
+                       for (auto &yield : t.resources) {
+                               if (yield.resource == resource) {
+                                       found = true;
+                                       // TODO: check if not busy with something else
+                                       double amount = std::min(yield.ubiquity, speed) * dt;
+                                       c.Ingest(resource, amount * growth * dt);
+                                       Decrease(amount);
+                                       if (value == 0.0) {
+                                               ingesting = false;
+                                               if (locate_goal) {
+                                                       // abort
+                                                       locate_goal->Complete();
+                                               }
+                                       }
+                                       break;
+                               }
+                       }
+                       if (!found && !locate_goal) {
+                               locate_goal = new LocateResourceGoal(c, resource);
+                               locate_goal->OnComplete([&](Goal &g){ OnLocateComplete(g); });
+                               c.AddGoal(std::unique_ptr<Goal>(locate_goal));
+                       }
+               }
        }
        if (IsCritical()) {
                c.Hurt(damage * dt);
        }
+       if (locate_goal) {
+               locate_goal->Urgency(value);
+       }
+}
+
+void IngestNeed::OnLocateComplete(Goal &g) {
+       if (&g == locate_goal) {
+               locate_goal = nullptr;
+       }
 }
 
 
@@ -55,7 +96,7 @@ InhaleNeed::~InhaleNeed() {
 }
 
 void InhaleNeed::ApplyEffect(Creature &c, double dt) {
-       if (!IsSatisfied() && !inhaling) {
+       if (!IsSatisfied()) {
                inhaling = true;
        }
        if (inhaling) {