1 #include "AttackGoal.hpp"
2 #include "BlobBackgroundTask.hpp"
4 #include "IdleGoal.hpp"
5 #include "IngestGoal.hpp"
6 #include "LocateResourceGoal.hpp"
7 #include "StrollGoal.hpp"
9 #include "Creature.hpp"
10 #include "../app/Assets.hpp"
11 #include "../ui/string.hpp"
12 #include "../world/Planet.hpp"
13 #include "../world/Resource.hpp"
14 #include "../world/Simulation.hpp"
15 #include "../world/TileType.hpp"
20 #include <glm/gtx/io.hpp>
26 AttackGoal::AttackGoal(Creature &self, Creature &target)
34 AttackGoal::~AttackGoal() {
37 std::string AttackGoal::Describe() const {
38 return "attack " + target.Name();
41 void AttackGoal::Tick(double dt) {
45 void AttackGoal::Action() {
46 if (target.Dead() || !GetCreature().PerceptionTest(target.GetSituation().Position())) {
50 const glm::dvec3 diff(GetCreature().GetSituation().Position() - target.GetSituation().Position());
51 const double hit_range = GetCreature().Size() * 0.5 * GetCreature().DexertyFactor();
52 const double hit_dist = hit_range + (0.5 * GetCreature().Size()) + 0.5 * (target.Size());
53 if (GetCreature().GetStats().Damage().Critical()) {
55 GetCreature().GetSteering().Pass(diff * 5.0);
56 GetCreature().GetSteering().DontSeparate();
57 GetCreature().GetSteering().Haste(1.0);
58 } else if (glm::length2(diff) > hit_dist * hit_dist) {
59 // full throttle chase
60 GetCreature().GetSteering().Pass(target.GetSituation().Position());
61 GetCreature().GetSteering().DontSeparate();
62 GetCreature().GetSteering().Haste(1.0);
65 GetCreature().GetSteering().Halt();
66 GetCreature().GetSteering().DontSeparate();
67 GetCreature().GetSteering().Haste(1.0);
68 if (cooldown <= 0.0) {
69 constexpr double impulse = 0.05;
70 const double force = GetCreature().Strength();
73 * (GetCreature().GetComposition().TotalDensity() / target.GetComposition().TotalDensity())
74 * (GetCreature().Mass() / target.Mass())
76 GetCreature().DoWork(force * impulse * glm::length(diff));
78 target.GetSituation().Accelerate(glm::normalize(diff) * force * -impulse);
79 damage_dealt += damage;
80 if (damage_dealt >= damage_target || target.Dead()) {
83 GetCreature().GetSimulation().Log() << GetCreature().Name()
84 << " killed " << target.Name() << std::endl;
87 cooldown = 1.0 + (4.0 * (1.0 - GetCreature().DexertyFactor()));
92 void AttackGoal::OnBackground() {
93 // abort if something more important comes up
98 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
101 , drink_subtask(nullptr)
102 , eat_subtask(nullptr) {
105 BlobBackgroundTask::~BlobBackgroundTask() {
108 std::string BlobBackgroundTask::Describe() const {
109 return "being a blob";
112 void BlobBackgroundTask::Tick(double dt) {
114 // TODO: derive breathing ability
115 int gas = Assets().data.resources["air"].id;
116 // TODO: check if in compatible atmosphere
117 double amount = GetCreature().GetStats().Breath().gain * -(1.0 + GetCreature().ExhaustionFactor());
118 GetCreature().GetStats().Breath().Add(amount * dt);
119 // maintain ~1% gas composition
120 double gas_amount = GetCreature().GetComposition().Get(gas);
121 if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.01) {
122 double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
123 GetCreature().Ingest(gas, add);
125 if (GetCreature().GetStats().Breath().Empty()) {
131 void BlobBackgroundTask::Action() {
137 void BlobBackgroundTask::CheckStats() {
138 Creature::Stats &stats = GetCreature().GetStats();
140 if (!breathing && stats.Breath().Bad()) {
144 if (!drink_subtask && stats.Thirst().Bad()) {
145 drink_subtask = new IngestGoal(GetCreature(), stats.Thirst());
146 for (const auto &cmp : GetCreature().GetComposition()) {
147 if (Assets().data.resources[cmp.resource].state == world::Resource::LIQUID) {
148 double value = cmp.value / GetCreature().GetComposition().TotalMass();
149 drink_subtask->Accept(cmp.resource, value);
150 for (const auto &compat : Assets().data.resources[cmp.resource].compatibility) {
151 if (Assets().data.resources[compat.first].state == world::Resource::LIQUID) {
152 drink_subtask->Accept(compat.first, value * compat.second);
157 drink_subtask->WhenComplete([&](Goal &) { drink_subtask = nullptr; });
158 GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
161 if (!eat_subtask && stats.Hunger().Bad()) {
162 eat_subtask = new IngestGoal(GetCreature(), stats.Hunger());
163 for (const auto &cmp : GetCreature().GetComposition()) {
164 if (Assets().data.resources[cmp.resource].state == world::Resource::SOLID) {
165 double value = cmp.value / GetCreature().GetComposition().TotalMass();
166 eat_subtask->Accept(cmp.resource, value);
167 for (const auto &compat : Assets().data.resources[cmp.resource].compatibility) {
168 if (Assets().data.resources[compat.first].state == world::Resource::SOLID) {
169 eat_subtask->Accept(compat.first, value * compat.second);
174 eat_subtask->WhenComplete([&](Goal &) { eat_subtask = nullptr; });
175 GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
178 // when in bad shape, don't make much effort
179 if (stats.Damage().Bad() || stats.Exhaustion().Bad() || stats.Fatigue().Critical()) {
180 GetCreature().GetSteering().DontSeparate();
182 GetCreature().GetSteering().ResumeSeparate();
186 void BlobBackgroundTask::CheckSplit() {
187 if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
188 && GetCreature().OffspringChance() > Random().UNorm()) {
189 GetCreature().GetSimulation().Log() << GetCreature().Name() << " split" << std::endl;
190 Split(GetCreature());
195 void BlobBackgroundTask::CheckMutate() {
196 // check for random property mutation
197 if (GetCreature().MutateChance() > Random().UNorm()) {
198 double amount = 1.0 + (Random().SNorm() * 0.05);
199 math::Distribution &d = GetCreature().GetGenome().properties.props[Random().UInt(9)];
200 if (Random().UNorm() < 0.5) {
201 d.Mean(d.Mean() * amount);
203 d.StandardDeviation(d.StandardDeviation() * amount);
209 Goal::Goal(Creature &c)
215 , interruptible(true)
219 Goal::~Goal() noexcept {
222 Situation &Goal::GetSituation() noexcept {
223 return c.GetSituation();
226 const Situation &Goal::GetSituation() const noexcept {
227 return c.GetSituation();
230 Steering &Goal::GetSteering() noexcept {
231 return c.GetSteering();
234 const Steering &Goal::GetSteering() const noexcept {
235 return c.GetSteering();
238 app::Assets &Goal::Assets() noexcept {
239 return c.GetSimulation().Assets();
242 const app::Assets &Goal::Assets() const noexcept {
243 return c.GetSimulation().Assets();
246 math::GaloisLFSR &Goal::Random() noexcept {
247 return Assets().random;
250 void Goal::SetComplete() {
260 void Goal::SetForeground() {
263 on_foreground(*this);
267 void Goal::SetBackground() {
270 on_background(*this);
274 void Goal::WhenComplete(std::function<void(Goal &)> cb) noexcept {
281 void Goal::WhenForeground(std::function<void(Goal &)> cb) noexcept {
285 void Goal::WhenBackground(std::function<void(Goal &)> cb) noexcept {
290 IdleGoal::IdleGoal(Creature &c)
296 IdleGoal::~IdleGoal() {
299 std::string IdleGoal::Describe() const {
303 void IdleGoal::Action() {
304 // use boredom as chance per minute
305 if (Random().UNorm() < GetCreature().GetStats().Boredom().value * (1.0 / 3600.0)) {
310 void IdleGoal::PickActivity() {
311 GetCreature().AddGoal(std::unique_ptr<Goal>(new StrollGoal(GetCreature())));
317 std::string summarize(const Composition &comp, const app::Assets &assets) {
320 for (const auto &c : comp) {
326 s << assets.data.resources[c.resource].label;
333 IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat)
336 , accept(Assets().data.resources)
337 , locate_subtask(nullptr)
344 IngestGoal::~IngestGoal() {
347 void IngestGoal::Accept(int resource, double value) {
348 accept.Add(resource, value);
351 std::string IngestGoal::Describe() const {
352 if (resource == -1) {
353 return "ingest " + summarize(accept, Assets());
355 const world::Resource &r = Assets().data.resources[resource];
356 if (r.state == world::Resource::SOLID) {
357 return "eat " + r.label;
359 return "drink " + r.label;
364 void IngestGoal::Enable() {
367 void IngestGoal::Tick(double dt) {
369 if (locate_subtask) {
370 locate_subtask->Urgency(Urgency() + 0.1);
373 if (OnSuitableTile() && !GetSituation().Moving()) {
374 GetCreature().Ingest(resource, yield * GetCreature().GetComposition().Compatibility(resource) * dt);
375 stat.Add(-1.0 * yield * dt);
380 // left tile somehow, some idiot probably pushed us off
387 void IngestGoal::Action() {
392 if (OnSuitableTile()) {
393 if (GetSituation().Moving()) {
394 // break with maximum force
395 GetSteering().Haste(1.0);
396 GetSteering().Halt();
399 // TODO: somehow this still gets interrupted
400 Interruptible(false);
404 locate_subtask = new LocateResourceGoal(GetCreature());
405 for (const auto &c : accept) {
406 locate_subtask->Accept(c.resource, c.value);
408 locate_subtask->SetMinimum(stat.gain * -1.1);
409 locate_subtask->Urgency(Urgency() + 0.1);
410 locate_subtask->WhenComplete([&](Goal &){ locate_subtask = nullptr; });
411 GetCreature().AddGoal(std::unique_ptr<Goal>(locate_subtask));
415 bool IngestGoal::OnSuitableTile() {
416 if (!GetSituation().OnGround()) {
419 const world::TileType &t = GetSituation().GetTileType();
420 auto found = t.FindBestResource(accept);
421 if (found != t.resources.end()) {
422 resource = found->resource;
423 yield = found->ubiquity;
432 LocateResourceGoal::LocateResourceGoal(Creature &c)
434 , accept(Assets().data.resources)
442 LocateResourceGoal::~LocateResourceGoal() noexcept {
445 void LocateResourceGoal::Accept(int resource, double value) {
446 accept.Add(resource, value);
449 std::string LocateResourceGoal::Describe() const {
450 return "locate " + summarize(accept, Assets());
453 void LocateResourceGoal::Enable() {
457 void LocateResourceGoal::Tick(double dt) {
461 void LocateResourceGoal::Action() {
462 if (reevaluate < 0.0) {
473 GetSteering().GoTo(target_pos);
476 } else if (OnTarget()) {
477 GetSteering().Halt();
478 if (!GetSituation().Moving()) {
482 GetSteering().GoTo(target_pos);
484 GetSteering().Haste(Urgency());
487 void LocateResourceGoal::LocateResource() {
488 if (GetSituation().OnSurface()) {
489 const world::TileType &t = GetSituation().GetTileType();
490 auto yield = t.FindBestResource(accept);
491 if (yield != t.resources.cend()) {
493 GetSteering().Halt();
496 target_pos = GetSituation().Position();
498 // go find somewhere else
514 void LocateResourceGoal::SearchVicinity() {
515 const world::Planet &planet = GetSituation().GetPlanet();
516 const glm::dvec3 &pos = GetSituation().Position();
517 const glm::dvec3 normal(planet.NormalAt(pos));
518 const glm::dvec3 step_x(glm::normalize(glm::cross(normal, glm::dvec3(normal.z, normal.x, normal.y))) * (GetCreature().PerceptionOmniRange() * 0.7));
519 const glm::dvec3 step_y(glm::normalize(glm::cross(step_x, normal)) * (GetCreature().PerceptionOmniRange() * 0.7));
521 const int search_radius = int(GetCreature().PerceptionRange() / (GetCreature().PerceptionOmniRange() * 0.7));
522 double rating[2 * search_radius + 1][2 * search_radius + 1];
523 std::memset(rating, '\0', (2 * search_radius + 1) * (2 * search_radius + 1) * sizeof(double));
525 // find close and rich field
526 for (int y = -search_radius; y < search_radius + 1; ++y) {
527 for (int x = -search_radius; x < search_radius + 1; ++x) {
528 const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
529 if (!GetCreature().PerceptionTest(tpos)) continue;
530 const world::TileType &type = planet.TileTypeAt(tpos);
531 auto yield = type.FindBestResource(accept);
532 if (yield != type.resources.cend()) {
533 // TODO: subtract minimum yield
534 rating[y + search_radius][x + search_radius] = yield->ubiquity * accept.Get(yield->resource);
536 double dist = std::max(0.125, 0.25 * glm::length2(tpos - pos));
537 rating[y + search_radius][x + search_radius] /= dist;
543 for (auto &c : planet.Creatures()) {
544 if (&*c == &GetCreature()) continue;
545 for (int y = -search_radius; y < search_radius + 1; ++y) {
546 for (int x = -search_radius; x < search_radius + 1; ++x) {
547 const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
548 if (glm::length2(tpos - c->GetSituation().Position()) < 1.0) {
549 rating[y + search_radius][x + search_radius] *= 0.8;
555 glm::ivec2 best_pos(0);
556 double best_rating = -1.0;
558 for (int y = -search_radius; y < search_radius + 1; ++y) {
559 for (int x = -search_radius; x < search_radius + 1; ++x) {
560 if (rating[y + search_radius][x + search_radius] > best_rating) {
561 best_pos = glm::ivec2(x, y);
562 best_rating = rating[y + search_radius][x + search_radius];
567 if (best_rating > minimum) {
570 target_pos = glm::normalize(pos + (double(best_pos.x) * step_x) + (double(best_pos.y) * step_y)) * planet.Radius();
571 GetSteering().GoTo(target_pos);
575 void LocateResourceGoal::Remember() {
577 if (GetCreature().GetMemory().RememberLocation(accept, pos)) {
581 GetSteering().GoTo(target_pos);
585 void LocateResourceGoal::RandomWalk() {
590 const world::Planet &planet = GetSituation().GetPlanet();
591 const glm::dvec3 &pos = GetSituation().Position();
592 const glm::dvec3 normal(planet.NormalAt(pos));
593 const glm::dvec3 step_x(glm::normalize(glm::cross(normal, glm::dvec3(normal.z, normal.x, normal.y))));
594 const glm::dvec3 step_y(glm::normalize(glm::cross(step_x, normal)));
598 target_pos = GetSituation().Position();
599 target_pos += Random().SNorm() * 3.0 * step_x;
600 target_pos += Random().SNorm() * 3.0 * step_y;
601 // bias towards current heading
602 target_pos += GetSituation().Heading() * 1.5;
603 target_pos = glm::normalize(target_pos) * planet.Radius();
604 GetSteering().GoTo(target_pos);
607 bool LocateResourceGoal::OnTarget() const noexcept {
608 const Situation &s = GetSituation();
609 return s.OnGround() && glm::length2(s.Position() - target_pos) < 0.0001;
613 StrollGoal::StrollGoal(Creature &c)
615 , last(GetSituation().Position())
619 StrollGoal::~StrollGoal() {
622 std::string StrollGoal::Describe() const {
623 return "take a walk";
626 void StrollGoal::Enable() {
627 last = GetSituation().Position();
628 GetSteering().Haste(0.0);
632 void StrollGoal::Action() {
633 if (glm::length2(next - GetSituation().Position()) < 0.0001) {
638 void StrollGoal::OnBackground() {
642 void StrollGoal::PickTarget() noexcept {
644 next += GetSituation().Heading() * 1.5;
645 const glm::dvec3 normal(GetSituation().GetPlanet().NormalAt(GetSituation().Position()));
646 glm::dvec3 rand_x(GetSituation().Heading());
647 if (std::abs(glm::dot(normal, rand_x)) > 0.999) {
648 rand_x = glm::dvec3(normal.z, normal.x, normal.y);
650 glm::dvec3 rand_y = glm::cross(normal, rand_x);
651 next += ((rand_x * Random().SNorm()) + (rand_y * Random().SNorm())) * 1.5;
652 next = glm::normalize(next) * GetSituation().GetPlanet().Radius();
653 GetSteering().GoTo(next);