]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/need.cpp
eating and drinking
[blobs.git] / src / creature / need.cpp
index 8249631038a639925697d9fb4a535d5135a9e1c0..60e7cb816b3a0b8cd375df295da00e80723dbc3a 100644 (file)
@@ -3,6 +3,7 @@
 #include "IngestNeed.hpp"
 
 #include "Creature.hpp"
+#include "LocateResourceGoal.hpp"
 #include "../world/Planet.hpp"
 #include "../world/TileType.hpp"
 
@@ -30,7 +31,8 @@ IngestNeed::IngestNeed(int resource, double speed, double damage)
 : resource(resource)
 , speed(speed)
 , damage(damage)
-, ingesting(false) {
+, ingesting(false)
+, locating(false) {
 }
 
 IngestNeed::~IngestNeed() {
@@ -40,16 +42,26 @@ 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) {
+                                       found = true;
+                                       // TODO: check if not busy with something else
                                        Decrease(std::min(yield.ubiquity, speed) * dt);
+                                       if (value == 0.0) {
+                                               ingesting = false;
+                                               locating = false;
+                                       }
                                        break;
                                }
                        }
+                       if (!found && !locating) {
+                               c.AddGoal(std::unique_ptr<Goal>(new LocateResourceGoal(resource)));
+                               locating = true;
+                       }
                }
        }
        if (IsCritical()) {