1 #include "BlobBackgroundTask.hpp"
3 #include "IdleGoal.hpp"
4 #include "IngestGoal.hpp"
5 #include "LocateResourceGoal.hpp"
6 #include "StrollGoal.hpp"
8 #include "Creature.hpp"
9 #include "../app/Assets.hpp"
10 #include "../ui/string.hpp"
11 #include "../world/Planet.hpp"
12 #include "../world/Resource.hpp"
13 #include "../world/Simulation.hpp"
14 #include "../world/TileType.hpp"
19 #include <glm/gtx/io.hpp>
25 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
28 , drink_subtask(nullptr)
29 , eat_subtask(nullptr) {
32 BlobBackgroundTask::~BlobBackgroundTask() {
35 std::string BlobBackgroundTask::Describe() const {
36 return "being a blob";
39 void BlobBackgroundTask::Tick(double dt) {
41 // TODO: derive breathing ability
42 int gas = Assets().data.resources["air"].id;
43 // TODO: check if in compatible atmosphere
44 double amount = GetCreature().GetStats().Breath().gain * -(1.0 + GetCreature().ExhaustionFactor());
45 GetCreature().GetStats().Breath().Add(amount * dt);
46 // maintain ~1% gas composition
47 double gas_amount = GetCreature().GetComposition().Get(gas);
48 if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.01) {
49 double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
50 GetCreature().Ingest(gas, add);
52 if (GetCreature().GetStats().Breath().Empty()) {
58 void BlobBackgroundTask::Action() {
64 void BlobBackgroundTask::CheckStats() {
65 Creature::Stats &stats = GetCreature().GetStats();
67 if (!breathing && stats.Breath().Bad()) {
71 if (!drink_subtask && stats.Thirst().Bad()) {
72 drink_subtask = new IngestGoal(GetCreature(), stats.Thirst());
73 for (const auto &cmp : GetCreature().GetComposition()) {
74 if (Assets().data.resources[cmp.resource].state == world::Resource::LIQUID) {
75 drink_subtask->Accept(cmp.resource, 1.0);
78 drink_subtask->WhenComplete([&](Goal &) { drink_subtask = nullptr; });
79 GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
82 if (!eat_subtask && stats.Hunger().Bad()) {
83 eat_subtask = new IngestGoal(GetCreature(), stats.Hunger());
84 for (const auto &cmp : GetCreature().GetComposition()) {
85 if (Assets().data.resources[cmp.resource].state == world::Resource::SOLID) {
86 eat_subtask->Accept(cmp.resource, 1.0);
89 eat_subtask->WhenComplete([&](Goal &) { eat_subtask = nullptr; });
90 GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
93 // when in bad shape, don't make much effort
94 if (stats.Damage().Bad() || stats.Exhaustion().Bad() || stats.Fatigue().Critical()) {
95 GetCreature().GetSteering().DontSeparate();
97 GetCreature().GetSteering().ResumeSeparate();
101 void BlobBackgroundTask::CheckSplit() {
102 if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
103 && GetCreature().OffspringChance() > Random().UNorm()) {
104 GetCreature().GetSimulation().Log() << GetCreature().Name() << " split" << std::endl;
105 Split(GetCreature());
110 void BlobBackgroundTask::CheckMutate() {
111 // check for random property mutation
112 if (GetCreature().MutateChance() > Random().UNorm()) {
113 double amount = 1.0 + (Random().SNorm() * 0.05);
114 math::Distribution &d = GetCreature().GetGenome().properties.props[Random().UInt(9)];
115 if (Random().UNorm() < 0.5) {
116 d.Mean(d.Mean() * amount);
118 d.StandardDeviation(d.StandardDeviation() * amount);
124 Goal::Goal(Creature &c)
130 , interruptible(true)
134 Goal::~Goal() noexcept {
137 Situation &Goal::GetSituation() noexcept {
138 return c.GetSituation();
141 const Situation &Goal::GetSituation() const noexcept {
142 return c.GetSituation();
145 Steering &Goal::GetSteering() noexcept {
146 return c.GetSteering();
149 const Steering &Goal::GetSteering() const noexcept {
150 return c.GetSteering();
153 app::Assets &Goal::Assets() noexcept {
154 return c.GetSimulation().Assets();
157 const app::Assets &Goal::Assets() const noexcept {
158 return c.GetSimulation().Assets();
161 math::GaloisLFSR &Goal::Random() noexcept {
162 return Assets().random;
165 void Goal::SetComplete() {
175 void Goal::SetForeground() {
178 on_foreground(*this);
182 void Goal::SetBackground() {
185 on_background(*this);
189 void Goal::WhenComplete(std::function<void(Goal &)> cb) noexcept {
196 void Goal::WhenForeground(std::function<void(Goal &)> cb) noexcept {
200 void Goal::WhenBackground(std::function<void(Goal &)> cb) noexcept {
205 IdleGoal::IdleGoal(Creature &c)
211 IdleGoal::~IdleGoal() {
214 std::string IdleGoal::Describe() const {
218 void IdleGoal::Action() {
219 // use boredom as chance per minute
220 if (Random().UNorm() < GetCreature().GetStats().Boredom().value * (1.0 / 3600.0)) {
225 void IdleGoal::PickActivity() {
226 GetCreature().AddGoal(std::unique_ptr<Goal>(new StrollGoal(GetCreature())));
232 std::string summarize(const Composition &comp, const app::Assets &assets) {
235 for (const auto &c : comp) {
241 s << assets.data.resources[c.resource].label;
248 IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat)
252 , locate_subtask(nullptr)
259 IngestGoal::~IngestGoal() {
262 void IngestGoal::Accept(int resource, double value) {
263 accept.Add(resource, value);
266 std::string IngestGoal::Describe() const {
267 if (resource == -1) {
268 return "ingest " + summarize(accept, Assets());
270 const world::Resource &r = Assets().data.resources[resource];
271 if (r.state == world::Resource::SOLID) {
272 return "eat " + r.label;
274 return "drink " + r.label;
279 void IngestGoal::Enable() {
282 void IngestGoal::Tick(double dt) {
284 if (locate_subtask) {
285 locate_subtask->Urgency(Urgency() + 0.1);
288 if (OnSuitableTile() && !GetSituation().Moving()) {
289 // TODO: determine satisfaction factor
290 GetCreature().Ingest(resource, yield * dt);
291 stat.Add(-1.0 * yield * dt);
296 // left tile somehow, some idiot probably pushed us off
303 void IngestGoal::Action() {
308 if (OnSuitableTile()) {
309 if (GetSituation().Moving()) {
310 // break with maximum force
311 GetSteering().Haste(1.0);
312 GetSteering().Halt();
315 // TODO: somehow this still gets interrupted
316 Interruptible(false);
320 locate_subtask = new LocateResourceGoal(GetCreature());
321 for (const auto &c : accept) {
322 locate_subtask->Accept(c.resource, c.value);
324 locate_subtask->Urgency(Urgency() + 0.1);
325 locate_subtask->WhenComplete([&](Goal &){ locate_subtask = nullptr; });
326 GetCreature().AddGoal(std::unique_ptr<Goal>(locate_subtask));
330 bool IngestGoal::OnSuitableTile() {
331 if (!GetSituation().OnSurface()) {
334 const world::TileType &t = GetSituation().GetTileType();
335 auto found = t.FindBestResource(accept);
336 if (found != t.resources.end()) {
337 resource = found->resource;
338 yield = found->ubiquity;
347 LocateResourceGoal::LocateResourceGoal(Creature &c)
356 LocateResourceGoal::~LocateResourceGoal() noexcept {
359 void LocateResourceGoal::Accept(int resource, double value) {
360 accept.Add(resource, value);
363 std::string LocateResourceGoal::Describe() const {
364 return "locate " + summarize(accept, Assets());
367 void LocateResourceGoal::Enable() {
371 void LocateResourceGoal::Tick(double dt) {
375 void LocateResourceGoal::Action() {
376 if (reevaluate < 0.0) {
383 double dist = glm::length2(GetSituation().Position() - target_pos);
388 GetSteering().GoTo(target_pos);
391 } else if (NearTarget()) {
392 GetSteering().Halt();
393 if (!GetSituation().Moving()) {
397 GetSteering().GoTo(target_pos);
399 GetSteering().Haste(Urgency());
402 void LocateResourceGoal::LocateResource() {
403 if (GetSituation().OnSurface()) {
404 const world::TileType &t = GetSituation().GetTileType();
405 auto yield = t.FindBestResource(accept);
406 if (yield != t.resources.cend()) {
408 GetSteering().Halt();
411 target_pos = GetSituation().Position();
413 // go find somewhere else
423 void LocateResourceGoal::SearchVicinity() {
424 const world::Planet &planet = GetSituation().GetPlanet();
425 const glm::dvec3 &pos = GetSituation().Position();
426 const glm::dvec3 normal(planet.NormalAt(pos));
427 const glm::dvec3 step_x(normalize(cross(normal, glm::dvec3(normal.z, normal.x, normal.y))));
428 const glm::dvec3 step_y(normalize(cross(step_x, normal)));
430 constexpr int search_radius = 2;
431 double rating[2 * search_radius + 1][2 * search_radius + 1] = {0};
433 // find close and rich field
434 for (int y = -search_radius; y < search_radius + 1; ++y) {
435 for (int x = -search_radius; x < search_radius + 1; ++x) {
436 const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
437 if (!GetCreature().PerceptionTest(tpos)) continue;
438 const world::TileType &type = planet.TileTypeAt(tpos);
439 auto yield = type.FindBestResource(accept);
440 if (yield != type.resources.cend()) {
441 // TODO: subtract minimum yield
442 rating[y + search_radius][x + search_radius] = yield->ubiquity * accept.Get(yield->resource);
444 double dist = std::max(0.125, 0.25 * glm::length(tpos - pos));
445 rating[y + search_radius][x + search_radius] /= dist;
451 for (auto &c : planet.Creatures()) {
452 if (&*c == &GetCreature()) continue;
453 for (int y = -search_radius; y < search_radius + 1; ++y) {
454 for (int x = -search_radius; x < search_radius + 1; ++x) {
455 const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
456 if (length2(tpos - c->GetSituation().Position()) < 1.0) {
457 rating[y + search_radius][x + search_radius] *= 0.8;
463 glm::ivec2 best_pos(0);
464 double best_rating = -1.0;
466 for (int y = -search_radius; y < search_radius + 1; ++y) {
467 for (int x = -search_radius; x < search_radius + 1; ++x) {
468 if (rating[y + search_radius][x + search_radius] > best_rating) {
469 best_pos = glm::ivec2(x, y);
470 best_rating = rating[y + search_radius][x + search_radius];
475 if (best_rating > 0.0) {
478 target_pos = normalize(pos + (double(best_pos.x) * step_x) + (double(best_pos.y) * step_y)) * planet.Radius();
479 GetSteering().GoTo(target_pos);
480 } else if (!searching) {
483 target_pos = GetSituation().Position();
484 target_pos += Random().SNorm() * step_x;
485 target_pos += Random().SNorm() * step_y;
486 // bias towards current heading
487 target_pos += GetSituation().Heading() * 1.5;
488 target_pos = normalize(target_pos) * planet.Radius();
489 GetSteering().GoTo(target_pos);
493 bool LocateResourceGoal::NearTarget() const noexcept {
494 const Situation &s = GetSituation();
495 return s.OnSurface() && length2(s.Position() - target_pos) < 0.5;
499 StrollGoal::StrollGoal(Creature &c)
501 , last(GetSituation().Position())
505 StrollGoal::~StrollGoal() {
508 std::string StrollGoal::Describe() const {
509 return "take a walk";
512 void StrollGoal::Enable() {
513 last = GetSituation().Position();
514 GetSteering().Haste(0.0);
518 void StrollGoal::Action() {
519 if (length2(next - GetSituation().Position()) < 0.0001) {
524 void StrollGoal::OnBackground() {
528 void StrollGoal::PickTarget() noexcept {
530 next += GetSituation().Heading() * 1.5;
531 const glm::dvec3 normal(GetSituation().GetPlanet().NormalAt(GetSituation().Position()));
532 glm::dvec3 rand_x(GetSituation().Heading());
533 if (std::abs(dot(normal, rand_x)) > 0.999) {
534 rand_x = glm::dvec3(normal.z, normal.x, normal.y);
536 glm::dvec3 rand_y = cross(normal, rand_x);
537 next += ((rand_x * Random().SNorm()) + (rand_y * Random().SNorm())) * 1.5;
538 next = normalize(next) * GetSituation().GetPlanet().Radius();
539 GetSteering().GoTo(next);