]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/need.cpp
fix AI
[blobs.git] / src / creature / need.cpp
index 8249631038a639925697d9fb4a535d5135a9e1c0..576655a97185ec8740cde7fe6385d1128485509e 100644 (file)
@@ -3,6 +3,7 @@
 #include "IngestNeed.hpp"
 
 #include "Creature.hpp"
+#include "LocateResourceGoal.hpp"
 #include "../world/Planet.hpp"
 #include "../world/TileType.hpp"
 
@@ -27,7 +28,8 @@ 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)
 , ingesting(false) {
@@ -40,21 +42,46 @@ void IngestNeed::ApplyEffect(Creature &c, double dt) {
        if (!IsSatisfied()) {
                ingesting = true;
        }
-       if (!IsSatisfied()) {
-               // TODO: find resource and start ingest task
+       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) {
-                                       Decrease(std::min(yield.ubiquity, speed) * dt);
+                                       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;
+       }
 }