#include "IngestNeed.hpp"
#include "Creature.hpp"
+#include "LocateResourceGoal.hpp"
#include "../world/Planet.hpp"
#include "../world/TileType.hpp"
IngestNeed::IngestNeed(int resource, double speed, double damage)
-: resource(resource)
+: locate_goal(nullptr)
+, resource(resource)
, speed(speed)
, damage(damage)
, ingesting(false) {
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;
+ }
}