1 #include "BlobBackgroundTask.hpp"
3 #include "IdleGoal.hpp"
4 #include "IngestGoal.hpp"
5 #include "LocateResourceGoal.hpp"
7 #include "Creature.hpp"
8 #include "../app/Assets.hpp"
9 #include "../world/Planet.hpp"
10 #include "../world/Resource.hpp"
11 #include "../world/Simulation.hpp"
12 #include "../world/TileType.hpp"
17 #include <glm/gtx/io.hpp>
23 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
26 , drink_subtask(nullptr)
27 , eat_subtask(nullptr) {
30 BlobBackgroundTask::~BlobBackgroundTask() {
33 std::string BlobBackgroundTask::Describe() const {
34 return "being a blob";
37 void BlobBackgroundTask::Tick(double dt) {
39 // TODO: derive breathing ability
40 double amount = GetCreature().GetStats().Breath().gain * -(1.5 + 0.5 * GetCreature().ExhaustionFactor());
41 GetCreature().GetStats().Breath().Add(amount * dt);
42 if (GetCreature().GetStats().Breath().Empty()) {
48 void BlobBackgroundTask::Action() {
54 void BlobBackgroundTask::CheckStats() {
55 Creature::Stats &stats = GetCreature().GetStats();
57 if (!breathing && stats.Breath().Bad()) {
61 if (!drink_subtask && stats.Thirst().Bad()) {
62 drink_subtask = new IngestGoal(GetCreature(), stats.Thirst());
63 for (const auto &cmp : GetCreature().GetComposition()) {
64 if (Assets().data.resources[cmp.resource].state == world::Resource::LIQUID) {
65 drink_subtask->Accept(cmp.resource, 1.0);
68 drink_subtask->OnComplete([&](Goal &) { drink_subtask = nullptr; });
69 GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
72 if (!eat_subtask && stats.Hunger().Bad()) {
73 eat_subtask = new IngestGoal(GetCreature(), stats.Hunger());
74 for (const auto &cmp : GetCreature().GetComposition()) {
75 if (Assets().data.resources[cmp.resource].state == world::Resource::SOLID) {
76 eat_subtask->Accept(cmp.resource, 1.0);
79 eat_subtask->OnComplete([&](Goal &) { eat_subtask = nullptr; });
80 GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
83 // when in bad shape, don't make much effort
84 if (stats.Damage().Bad() || stats.Exhaustion().Bad() || stats.Fatigue().Critical()) {
85 GetCreature().GetSteering().DontSeparate();
87 GetCreature().GetSteering().ResumeSeparate();
91 void BlobBackgroundTask::CheckSplit() {
92 if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
93 && GetCreature().OffspringChance() > Assets().random.UNorm()) {
94 std::cout << "[" << int(GetCreature().GetSimulation().Time())
95 << "s] " << GetCreature().Name() << " split" << std::endl;
101 void BlobBackgroundTask::CheckMutate() {
102 // check for random property mutation
103 if (GetCreature().MutateChance() > Assets().random.UNorm()) {
104 double amount = 1.0 + (Assets().random.SNorm() * 0.05);
105 math::Distribution &d = GetCreature().GetGenome().properties.props[(int(Assets().random.UNorm() * 8.0) % 8)];
106 if (Assets().random.UNorm() < 0.5) {
107 d.Mean(d.Mean() * amount);
109 d.StandardDeviation(d.StandardDeviation() * amount);
116 std::string summarize(const Composition &comp, const app::Assets &assets) {
119 for (const auto &c : comp) {
125 s << assets.data.resources[c.resource].label;
132 IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat)
136 , locate_subtask(nullptr)
143 IngestGoal::~IngestGoal() {
146 void IngestGoal::Accept(int resource, double value) {
147 accept.Add(resource, value);
150 std::string IngestGoal::Describe() const {
151 if (resource == -1) {
152 return "ingest " + summarize(accept, Assets());
154 const world::Resource &r = Assets().data.resources[resource];
155 if (r.state == world::Resource::SOLID) {
156 return "eat " + r.label;
158 return "drink " + r.label;
163 void IngestGoal::Enable() {
166 void IngestGoal::Tick(double dt) {
168 if (locate_subtask) {
169 locate_subtask->Urgency(Urgency() + 0.1);
172 if (OnSuitableTile() && !GetSituation().Moving()) {
173 // TODO: determine satisfaction factor
174 GetCreature().Ingest(resource, yield * dt);
175 stat.Add(-1.0 * yield * dt);
180 // left tile somehow, some idiot probably pushed us off
187 void IngestGoal::Action() {
192 if (OnSuitableTile()) {
193 if (GetSituation().Moving()) {
194 // break with maximum force
195 GetSteering().Haste(1.0);
196 GetSteering().Halt();
199 Interruptible(false);
203 locate_subtask = new LocateResourceGoal(GetCreature());
204 for (const auto &c : accept) {
205 locate_subtask->Accept(c.resource, c.value);
207 locate_subtask->Urgency(Urgency() + 0.1);
208 locate_subtask->OnComplete([&](Goal &){ locate_subtask = nullptr; });
209 GetCreature().AddGoal(std::unique_ptr<Goal>(locate_subtask));
213 bool IngestGoal::OnSuitableTile() {
214 if (!GetSituation().OnTile()) {
217 const world::TileType &t = GetSituation().GetTileType();
218 auto found = t.FindBestResource(accept);
219 if (found != t.resources.end()) {
220 resource = found->resource;
221 yield = found->ubiquity;
230 Goal::Goal(Creature &c)
234 , interruptible(true)
238 Goal::~Goal() noexcept {
241 Situation &Goal::GetSituation() noexcept {
242 return c.GetSituation();
245 const Situation &Goal::GetSituation() const noexcept {
246 return c.GetSituation();
249 Steering &Goal::GetSteering() noexcept {
250 return c.GetSteering();
253 const Steering &Goal::GetSteering() const noexcept {
254 return c.GetSteering();
257 app::Assets &Goal::Assets() noexcept {
258 return c.GetSimulation().Assets();
261 const app::Assets &Goal::Assets() const noexcept {
262 return c.GetSimulation().Assets();
265 void Goal::SetComplete() noexcept {
274 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
282 IdleGoal::IdleGoal(Creature &c)
288 IdleGoal::~IdleGoal() {
291 std::string IdleGoal::Describe() const {
295 void IdleGoal::Enable() {
298 void IdleGoal::Tick(double dt) {
301 void IdleGoal::Action() {
305 LocateResourceGoal::LocateResourceGoal(Creature &c)
316 LocateResourceGoal::~LocateResourceGoal() noexcept {
319 void LocateResourceGoal::Accept(int resource, double value) {
320 accept.Add(resource, value);
323 std::string LocateResourceGoal::Describe() const {
324 return "locate " + summarize(accept, Assets());
327 void LocateResourceGoal::Enable() {
331 void LocateResourceGoal::Tick(double dt) {
335 void LocateResourceGoal::Action() {
336 if (reevaluate < 0.0) {
343 double dist = glm::length2(GetSituation().Position() - target_pos);
347 GetSteering().GoTo(target_pos);
350 } else if (OnTargetTile()) {
351 GetSteering().Halt();
352 if (!GetSituation().Moving()) {
356 GetSteering().GoTo(target_pos);
358 GetSteering().Haste(Urgency());
361 void LocateResourceGoal::LocateResource() {
362 if (GetSituation().OnTile()) {
363 const world::TileType &t = GetSituation().GetTileType();
364 auto yield = t.FindBestResource(accept);
365 if (yield != t.resources.cend()) {
367 GetSteering().Halt();
370 target_pos = GetSituation().Position();
371 target_srf = GetSituation().Surface();
372 target_tile = GetSituation().GetPlanet().SurfacePosition(target_srf, target_pos);
374 // go find somewhere else
382 void LocateResourceGoal::SearchVicinity() {
383 const world::Planet &planet = GetSituation().GetPlanet();
384 int srf = GetSituation().Surface();
385 const glm::dvec3 &pos = GetSituation().Position();
387 glm::ivec2 loc = planet.SurfacePosition(srf, pos);
388 glm::ivec2 seek_radius(2);
389 glm::ivec2 begin(glm::max(glm::ivec2(0), loc - seek_radius));
390 glm::ivec2 end(glm::min(glm::ivec2(planet.SideLength()), loc + seek_radius + glm::ivec2(1)));
392 double rating[end.y - begin.y][end.x - begin.x];
393 std::memset(rating, 0, sizeof(double) * (end.y - begin.y) * (end.x - begin.x));
395 // find close and rich field
396 for (int y = begin.y; y < end.y; ++y) {
397 for (int x = begin.x; x < end.x; ++x) {
398 const world::TileType &type = planet.TypeAt(srf, x, y);
399 auto yield = type.FindBestResource(accept);
400 if (yield != type.resources.cend()) {
401 // TODO: subtract minimum yield
402 rating[y - begin.y][x - begin.x] = yield->ubiquity * accept.Get(yield->resource);
403 double dist = std::max(0.125, 0.25 * glm::length(planet.TileCenter(srf, x, y) - pos));
404 rating[y - begin.y][x - begin.x] /= dist;
409 // demote crowded tiles
410 for (auto &c : planet.Creatures()) {
411 if (&*c == &GetCreature()) continue;
412 if (c->GetSituation().Surface() != srf) continue;
413 glm::ivec2 coords(c->GetSituation().SurfacePosition());
414 if (coords.x < begin.x || coords.x >= end.x) continue;
415 if (coords.y < begin.y || coords.y >= end.y) continue;
416 rating[coords.y - begin.y][coords.x - begin.x] *= 0.9;
419 glm::ivec2 best_pos(0);
420 double best_rating = -1.0;
422 for (int y = begin.y; y < end.y; ++y) {
423 for (int x = begin.x; x < end.x; ++x) {
424 if (rating[y - begin.y][x - begin.x] > best_rating) {
425 best_pos = glm::ivec2(x, y);
426 best_rating = rating[y - begin.y][x - begin.x];
434 target_pos = planet.TileCenter(srf, best_pos.x, best_pos.y);
436 target_tile = best_pos;
437 GetSteering().GoTo(target_pos);
438 } else if (!searching) {
441 target_pos = GetSituation().Position();
442 target_pos[(srf + 0) % 3] += Assets().random.SNorm();
443 target_pos[(srf + 1) % 3] += Assets().random.SNorm();
444 // bias towards current direction
445 target_pos += glm::normalize(GetSituation().Velocity()) * 0.5;
446 target_pos = clamp(target_pos, -planet.Radius(), planet.Radius());
447 GetSteering().GoTo(target_pos);
451 bool LocateResourceGoal::OnTargetTile() const noexcept {
452 const Situation &s = GetSituation();
454 && s.Surface() == target_srf
456 && s.SurfacePosition() == target_tile;