1 #include "AttackGoal.hpp"
2 #include "BlobBackgroundTask.hpp"
4 #include "IdleGoal.hpp"
5 #include "IngestGoal.hpp"
6 #include "LocateResourceGoal.hpp"
7 #include "LookAroundGoal.hpp"
8 #include "StrollGoal.hpp"
10 #include "Creature.hpp"
11 #include "../app/Assets.hpp"
12 #include "../math/const.hpp"
13 #include "../ui/string.hpp"
14 #include "../world/Planet.hpp"
15 #include "../world/Resource.hpp"
16 #include "../world/Simulation.hpp"
17 #include "../world/TileType.hpp"
22 #include <glm/gtx/io.hpp>
23 #include <glm/gtx/rotate_vector.hpp>
29 AttackGoal::AttackGoal(Creature &self, Creature &target)
37 AttackGoal::~AttackGoal() {
40 std::string AttackGoal::Describe() const {
41 return "attack " + target.Name();
44 void AttackGoal::Tick(double dt) {
48 void AttackGoal::Action() {
49 if (target.Dead() || !GetCreature().PerceptionTest(target.GetSituation().Position())) {
53 const glm::dvec3 diff(GetSituation().Position() - target.GetSituation().Position());
54 const double hit_range = GetCreature().Size() * 0.5 * GetCreature().DexertyFactor();
55 const double hit_dist = hit_range + (0.5 * GetCreature().Size()) + 0.5 * (target.Size());
56 if (GetStats().Damage().Critical()) {
58 GetSteering().Pass(diff * 5.0);
59 GetSteering().DontSeparate();
60 GetSteering().Haste(1.0);
61 } else if (glm::length2(diff) > hit_dist * hit_dist) {
62 // full throttle chase
63 GetSteering().Pass(target.GetSituation().Position());
64 GetSteering().DontSeparate();
65 GetSteering().Haste(1.0);
69 GetSteering().DontSeparate();
70 GetSteering().Haste(1.0);
71 if (cooldown <= 0.0) {
72 constexpr double impulse = 0.05;
73 const double force = GetCreature().Strength();
76 * (GetCreature().GetComposition().TotalDensity() / target.GetComposition().TotalDensity())
77 * (GetCreature().Mass() / target.Mass())
79 GetCreature().DoWork(force * impulse * glm::length(diff));
81 target.GetSituation().Accelerate(glm::normalize(diff) * force * -impulse);
82 damage_dealt += damage;
83 if (damage_dealt >= damage_target || target.Dead()) {
86 GetCreature().GetSimulation().Log() << GetCreature().Name()
87 << " killed " << target.Name() << std::endl;
90 cooldown = 1.0 + (4.0 * (1.0 - GetCreature().DexertyFactor()));
95 void AttackGoal::OnBackground() {
96 // abort if something more important comes up
101 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
104 , drink_subtask(nullptr)
105 , eat_subtask(nullptr) {
108 BlobBackgroundTask::~BlobBackgroundTask() {
111 std::string BlobBackgroundTask::Describe() const {
112 return "being a blob";
115 void BlobBackgroundTask::Tick(double dt) {
117 // TODO: derive breathing ability
118 int gas = Assets().data.resources["air"].id;
119 // TODO: check if in compatible atmosphere
120 double amount = GetStats().Breath().gain * -(1.0 + GetCreature().ExhaustionFactor());
121 GetStats().Breath().Add(amount * dt);
122 // maintain ~1% gas composition
123 double gas_amount = GetCreature().GetComposition().Get(gas);
124 if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.01) {
125 double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
126 GetCreature().Ingest(gas, add);
128 if (GetStats().Breath().Empty()) {
134 void BlobBackgroundTask::Action() {
140 void BlobBackgroundTask::CheckStats() {
141 Creature::Stats &stats = GetStats();
143 if (!breathing && stats.Breath().Bad()) {
147 if (!drink_subtask && stats.Thirst().Bad()) {
148 drink_subtask = new IngestGoal(GetCreature(), stats.Thirst());
149 for (const auto &cmp : GetCreature().GetComposition()) {
150 if (Assets().data.resources[cmp.resource].state == world::Resource::LIQUID) {
151 double value = cmp.value / GetCreature().GetComposition().TotalMass();
152 drink_subtask->Accept(cmp.resource, value);
153 for (const auto &compat : Assets().data.resources[cmp.resource].compatibility) {
154 if (Assets().data.resources[compat.first].state == world::Resource::LIQUID) {
155 drink_subtask->Accept(compat.first, value * compat.second);
160 drink_subtask->WhenComplete([&](Goal &) { drink_subtask = nullptr; });
161 GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
164 if (!eat_subtask && stats.Hunger().Bad()) {
165 eat_subtask = new IngestGoal(GetCreature(), stats.Hunger());
166 for (const auto &cmp : GetCreature().GetComposition()) {
167 if (Assets().data.resources[cmp.resource].state == world::Resource::SOLID) {
168 double value = cmp.value / GetCreature().GetComposition().TotalMass();
169 eat_subtask->Accept(cmp.resource, value);
170 for (const auto &compat : Assets().data.resources[cmp.resource].compatibility) {
171 if (Assets().data.resources[compat.first].state == world::Resource::SOLID) {
172 eat_subtask->Accept(compat.first, value * compat.second);
177 eat_subtask->WhenComplete([&](Goal &) { eat_subtask = nullptr; });
178 GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
182 void BlobBackgroundTask::CheckSplit() {
183 if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
184 && GetCreature().OffspringChance() > Random().UNorm()) {
185 GetCreature().GetSimulation().Log() << GetCreature().Name() << " split" << std::endl;
186 Split(GetCreature());
191 void BlobBackgroundTask::CheckMutate() {
192 // check for random property mutation
193 if (GetCreature().MutateChance() > Random().UNorm()) {
194 double amount = 1.0 + (Random().SNorm() * 0.05);
195 math::Distribution &d = GetCreature().GetGenome().properties.props[Random().UInt(9)];
196 if (Random().UNorm() < 0.5) {
197 d.Mean(d.Mean() * amount);
199 d.StandardDeviation(d.StandardDeviation() * amount);
205 Goal::Goal(Creature &c)
211 , interruptible(true)
215 Goal::~Goal() noexcept {
218 app::Assets &Goal::Assets() noexcept {
219 return c.GetSimulation().Assets();
222 const app::Assets &Goal::Assets() const noexcept {
223 return c.GetSimulation().Assets();
226 math::GaloisLFSR &Goal::Random() noexcept {
227 return Assets().random;
230 void Goal::SetComplete() {
240 void Goal::SetForeground() {
243 on_foreground(*this);
247 void Goal::SetBackground() {
250 on_background(*this);
254 void Goal::WhenComplete(std::function<void(Goal &)> cb) noexcept {
261 void Goal::WhenForeground(std::function<void(Goal &)> cb) noexcept {
265 void Goal::WhenBackground(std::function<void(Goal &)> cb) noexcept {
270 IdleGoal::IdleGoal(Creature &c)
276 IdleGoal::~IdleGoal() {
279 std::string IdleGoal::Describe() const {
283 void IdleGoal::Action() {
284 // when in bad shape, don't make much effort
285 if (GetStats().Damage().Bad() || GetStats().Exhaustion().Bad() || GetStats().Fatigue().Critical()) {
286 GetSteering().DontSeparate();
288 GetSteering().ResumeSeparate();
291 // use boredom as chance per 15s
292 if (Random().UNorm() < GetStats().Boredom().value * (1.0 / 900.0)) {
297 void IdleGoal::PickActivity() {
298 int n = Random().UInt(2);
300 GetCreature().AddGoal(std::unique_ptr<Goal>(new StrollGoal(GetCreature())));
302 GetCreature().AddGoal(std::unique_ptr<Goal>(new LookAroundGoal(GetCreature())));
309 std::string summarize(const Composition &comp, const app::Assets &assets) {
312 for (const auto &c : comp) {
318 s << assets.data.resources[c.resource].label;
325 IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat)
328 , accept(Assets().data.resources)
329 , locate_subtask(nullptr)
336 IngestGoal::~IngestGoal() {
339 void IngestGoal::Accept(int resource, double value) {
340 accept.Add(resource, value);
343 std::string IngestGoal::Describe() const {
344 if (resource == -1) {
345 return "ingest " + summarize(accept, Assets());
347 const world::Resource &r = Assets().data.resources[resource];
348 if (r.state == world::Resource::SOLID) {
349 return "eat " + r.label;
351 return "drink " + r.label;
356 void IngestGoal::Enable() {
359 void IngestGoal::Tick(double dt) {
361 if (locate_subtask) {
362 locate_subtask->Urgency(Urgency() + 0.1);
365 if (OnSuitableTile() && !GetSituation().Moving()) {
366 GetCreature().Ingest(resource, yield * dt);
367 stat.Add(-1.0 * yield * GetCreature().GetComposition().Compatibility(resource) * dt);
372 // left tile somehow, some idiot probably pushed us off
379 void IngestGoal::Action() {
384 if (OnSuitableTile()) {
385 if (GetSituation().Moving()) {
386 // break with maximum force
387 GetSteering().Haste(1.0);
388 GetSteering().Halt();
391 // TODO: somehow this still gets interrupted
392 Interruptible(false);
396 locate_subtask = new LocateResourceGoal(GetCreature());
397 for (const auto &c : accept) {
398 locate_subtask->Accept(c.resource, c.value);
400 locate_subtask->SetMinimum(stat.gain * -1.1);
401 locate_subtask->Urgency(Urgency() + 0.1);
402 locate_subtask->WhenComplete([&](Goal &){ locate_subtask = nullptr; });
403 GetCreature().AddGoal(std::unique_ptr<Goal>(locate_subtask));
407 bool IngestGoal::OnSuitableTile() {
408 if (!GetSituation().OnGround()) {
411 const world::TileType &t = GetSituation().GetTileType();
412 auto found = t.FindBestResource(accept);
413 if (found != t.resources.end()) {
414 resource = found->resource;
415 yield = found->ubiquity;
424 LocateResourceGoal::LocateResourceGoal(Creature &c)
426 , accept(Assets().data.resources)
434 LocateResourceGoal::~LocateResourceGoal() noexcept {
437 void LocateResourceGoal::Accept(int resource, double value) {
438 accept.Add(resource, value);
441 std::string LocateResourceGoal::Describe() const {
442 return "locate " + summarize(accept, Assets());
445 void LocateResourceGoal::Enable() {
449 void LocateResourceGoal::Tick(double dt) {
453 void LocateResourceGoal::Action() {
454 if (reevaluate < 0.0) {
465 GetSteering().GoTo(target_pos);
468 } else if (OnTarget()) {
469 GetSteering().Halt();
470 if (!GetSituation().Moving()) {
474 GetSteering().GoTo(target_pos);
476 GetSteering().Haste(Urgency());
479 void LocateResourceGoal::LocateResource() {
480 if (GetSituation().OnSurface()) {
481 const world::TileType &t = GetSituation().GetTileType();
482 auto yield = t.FindBestResource(accept);
483 if (yield != t.resources.cend()) {
485 GetSteering().Halt();
488 target_pos = GetSituation().Position();
490 // go find somewhere else
506 void LocateResourceGoal::SearchVicinity() {
507 const world::Planet &planet = GetSituation().GetPlanet();
508 const glm::dvec3 &pos = GetSituation().Position();
509 const glm::dvec3 normal(planet.NormalAt(pos));
510 const glm::dvec3 step_x(glm::normalize(glm::cross(normal, glm::dvec3(normal.z, normal.x, normal.y))) * (GetCreature().PerceptionOmniRange() * 0.7));
511 const glm::dvec3 step_y(glm::normalize(glm::cross(step_x, normal)) * (GetCreature().PerceptionOmniRange() * 0.7));
513 const int search_radius = int(GetCreature().PerceptionRange() / (GetCreature().PerceptionOmniRange() * 0.7));
514 double rating[2 * search_radius + 1][2 * search_radius + 1];
515 std::memset(rating, '\0', (2 * search_radius + 1) * (2 * search_radius + 1) * sizeof(double));
517 // find close and rich field
518 for (int y = -search_radius; y < search_radius + 1; ++y) {
519 for (int x = -search_radius; x < search_radius + 1; ++x) {
520 const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
521 if (!GetCreature().PerceptionTest(tpos)) continue;
522 const world::TileType &type = planet.TileTypeAt(tpos);
523 auto yield = type.FindBestResource(accept);
524 if (yield != type.resources.cend()) {
525 rating[y + search_radius][x + search_radius] = yield->ubiquity * accept.Get(yield->resource);
527 double dist = std::max(0.125, 0.25 * glm::length2(tpos - pos));
528 rating[y + search_radius][x + search_radius] /= dist;
534 for (auto &c : planet.Creatures()) {
535 if (&*c == &GetCreature()) continue;
536 for (int y = -search_radius; y < search_radius + 1; ++y) {
537 for (int x = -search_radius; x < search_radius + 1; ++x) {
538 const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
539 if (glm::length2(tpos - c->GetSituation().Position()) < 1.0) {
540 rating[y + search_radius][x + search_radius] *= 0.8;
546 glm::ivec2 best_pos(0);
547 double best_rating = -1.0;
549 for (int y = -search_radius; y < search_radius + 1; ++y) {
550 for (int x = -search_radius; x < search_radius + 1; ++x) {
551 if (rating[y + search_radius][x + search_radius] > best_rating) {
552 best_pos = glm::ivec2(x, y);
553 best_rating = rating[y + search_radius][x + search_radius];
558 if (best_rating > minimum) {
561 target_pos = glm::normalize(pos + (double(best_pos.x) * step_x) + (double(best_pos.y) * step_y)) * planet.Radius();
562 GetSteering().GoTo(target_pos);
566 void LocateResourceGoal::Remember() {
568 if (GetCreature().GetMemory().RememberLocation(accept, pos)) {
572 GetSteering().GoTo(target_pos);
576 void LocateResourceGoal::RandomWalk() {
581 const world::Planet &planet = GetSituation().GetPlanet();
582 const glm::dvec3 &pos = GetSituation().Position();
583 const glm::dvec3 normal(planet.NormalAt(pos));
584 const glm::dvec3 step_x(glm::normalize(glm::cross(normal, glm::dvec3(normal.z, normal.x, normal.y))));
585 const glm::dvec3 step_y(glm::normalize(glm::cross(step_x, normal)));
589 target_pos = GetSituation().Position();
590 target_pos += Random().SNorm() * 3.0 * step_x;
591 target_pos += Random().SNorm() * 3.0 * step_y;
592 // bias towards current heading
593 target_pos += GetSituation().Heading() * 1.5;
594 target_pos = glm::normalize(target_pos) * planet.Radius();
595 GetSteering().GoTo(target_pos);
598 bool LocateResourceGoal::OnTarget() const noexcept {
599 const Situation &s = GetSituation();
600 return s.OnGround() && glm::length2(s.Position() - target_pos) < 0.0001;
604 LookAroundGoal::LookAroundGoal(Creature &c)
609 LookAroundGoal::~LookAroundGoal() {
612 std::string LookAroundGoal::Describe() const {
613 return "look around";
616 void LookAroundGoal::Enable() {
617 GetSteering().Halt();
620 void LookAroundGoal::Tick(double dt) {
624 void LookAroundGoal::Action() {
627 timer = 1.0 + (Random().UNorm() * 4.0);
631 void LookAroundGoal::OnBackground() {
635 void LookAroundGoal::PickDirection() noexcept {
636 double r = Random().SNorm();
637 r *= std::abs(r) * 0.5 * PI;
638 GetCreature().HeadingTarget(glm::rotate(GetSituation().Heading(), r, GetSituation().SurfaceNormal()));
642 StrollGoal::StrollGoal(Creature &c)
644 , last(GetSituation().Position())
648 StrollGoal::~StrollGoal() {
651 std::string StrollGoal::Describe() const {
652 return "take a walk";
655 void StrollGoal::Enable() {
656 last = GetSituation().Position();
657 GetSteering().Haste(0.0);
661 void StrollGoal::Action() {
662 if (glm::length2(next - GetSituation().Position()) < 0.0001) {
667 void StrollGoal::OnBackground() {
671 void StrollGoal::PickTarget() noexcept {
673 next += GetSituation().Heading() * 1.5;
674 const glm::dvec3 normal(GetSituation().GetPlanet().NormalAt(GetSituation().Position()));
675 glm::dvec3 rand_x(GetSituation().Heading());
676 if (std::abs(glm::dot(normal, rand_x)) > 0.999) {
677 rand_x = glm::dvec3(normal.z, normal.x, normal.y);
679 glm::dvec3 rand_y = glm::cross(normal, rand_x);
680 next += ((rand_x * Random().SNorm()) + (rand_y * Random().SNorm())) * 1.5;
681 next = glm::normalize(next) * GetSituation().GetPlanet().Radius();
682 GetSteering().GoTo(next);