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 "../ui/String.hpp"
10 #include "../world/Planet.hpp"
11 #include "../world/Resource.hpp"
12 #include "../world/Simulation.hpp"
13 #include "../world/TileType.hpp"
18 #include <glm/gtx/io.hpp>
24 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
27 , drink_subtask(nullptr)
28 , eat_subtask(nullptr) {
31 BlobBackgroundTask::~BlobBackgroundTask() {
34 std::string BlobBackgroundTask::Describe() const {
35 return "being a blob";
38 void BlobBackgroundTask::Tick(double dt) {
40 // TODO: derive breathing ability
41 int gas = Assets().data.resources["air"].id;
42 // TODO: check if in compatible atmosphere
43 double amount = GetCreature().GetStats().Breath().gain * -(1.5 + 0.5 * GetCreature().ExhaustionFactor());
44 GetCreature().GetStats().Breath().Add(amount * dt);
45 // maintain ~1% gas composition
46 double gas_amount = GetCreature().GetComposition().Get(gas);
47 if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.01) {
48 double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
49 GetCreature().Ingest(gas, add);
51 if (GetCreature().GetStats().Breath().Empty()) {
57 void BlobBackgroundTask::Action() {
63 void BlobBackgroundTask::CheckStats() {
64 Creature::Stats &stats = GetCreature().GetStats();
66 if (!breathing && stats.Breath().Bad()) {
70 if (!drink_subtask && stats.Thirst().Bad()) {
71 drink_subtask = new IngestGoal(GetCreature(), stats.Thirst());
72 for (const auto &cmp : GetCreature().GetComposition()) {
73 if (Assets().data.resources[cmp.resource].state == world::Resource::LIQUID) {
74 drink_subtask->Accept(cmp.resource, 1.0);
77 drink_subtask->OnComplete([&](Goal &) { drink_subtask = nullptr; });
78 GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
81 if (!eat_subtask && stats.Hunger().Bad()) {
82 eat_subtask = new IngestGoal(GetCreature(), stats.Hunger());
83 for (const auto &cmp : GetCreature().GetComposition()) {
84 if (Assets().data.resources[cmp.resource].state == world::Resource::SOLID) {
85 eat_subtask->Accept(cmp.resource, 1.0);
88 eat_subtask->OnComplete([&](Goal &) { eat_subtask = nullptr; });
89 GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
92 // when in bad shape, don't make much effort
93 if (stats.Damage().Bad() || stats.Exhaustion().Bad() || stats.Fatigue().Critical()) {
94 GetCreature().GetSteering().DontSeparate();
96 GetCreature().GetSteering().ResumeSeparate();
100 void BlobBackgroundTask::CheckSplit() {
101 if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
102 && GetCreature().OffspringChance() > Assets().random.UNorm()) {
103 std::cout << "[" << ui::TimeString(GetCreature().GetSimulation().Time())
104 << "] " << GetCreature().Name() << " split" << std::endl;
105 Split(GetCreature());
110 void BlobBackgroundTask::CheckMutate() {
111 // check for random property mutation
112 if (GetCreature().MutateChance() > Assets().random.UNorm()) {
113 double amount = 1.0 + (Assets().random.SNorm() * 0.05);
114 math::Distribution &d = GetCreature().GetGenome().properties.props[(int(Assets().random.UNorm() * 8.0) % 8)];
115 if (Assets().random.UNorm() < 0.5) {
116 d.Mean(d.Mean() * amount);
118 d.StandardDeviation(d.StandardDeviation() * amount);
125 std::string summarize(const Composition &comp, const app::Assets &assets) {
128 for (const auto &c : comp) {
134 s << assets.data.resources[c.resource].label;
141 IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat)
145 , locate_subtask(nullptr)
152 IngestGoal::~IngestGoal() {
155 void IngestGoal::Accept(int resource, double value) {
156 accept.Add(resource, value);
159 std::string IngestGoal::Describe() const {
160 if (resource == -1) {
161 return "ingest " + summarize(accept, Assets());
163 const world::Resource &r = Assets().data.resources[resource];
164 if (r.state == world::Resource::SOLID) {
165 return "eat " + r.label;
167 return "drink " + r.label;
172 void IngestGoal::Enable() {
175 void IngestGoal::Tick(double dt) {
177 if (locate_subtask) {
178 locate_subtask->Urgency(Urgency() + 0.1);
181 if (OnSuitableTile() && !GetSituation().Moving()) {
182 // TODO: determine satisfaction factor
183 GetCreature().Ingest(resource, yield * dt);
184 stat.Add(-1.0 * yield * dt);
189 // left tile somehow, some idiot probably pushed us off
196 void IngestGoal::Action() {
201 if (OnSuitableTile()) {
202 if (GetSituation().Moving()) {
203 // break with maximum force
204 GetSteering().Haste(1.0);
205 GetSteering().Halt();
208 Interruptible(false);
212 locate_subtask = new LocateResourceGoal(GetCreature());
213 for (const auto &c : accept) {
214 locate_subtask->Accept(c.resource, c.value);
216 locate_subtask->Urgency(Urgency() + 0.1);
217 locate_subtask->OnComplete([&](Goal &){ locate_subtask = nullptr; });
218 GetCreature().AddGoal(std::unique_ptr<Goal>(locate_subtask));
222 bool IngestGoal::OnSuitableTile() {
223 if (!GetSituation().OnTile()) {
226 const world::TileType &t = GetSituation().GetTileType();
227 auto found = t.FindBestResource(accept);
228 if (found != t.resources.end()) {
229 resource = found->resource;
230 yield = found->ubiquity;
239 Goal::Goal(Creature &c)
243 , interruptible(true)
247 Goal::~Goal() noexcept {
250 Situation &Goal::GetSituation() noexcept {
251 return c.GetSituation();
254 const Situation &Goal::GetSituation() const noexcept {
255 return c.GetSituation();
258 Steering &Goal::GetSteering() noexcept {
259 return c.GetSteering();
262 const Steering &Goal::GetSteering() const noexcept {
263 return c.GetSteering();
266 app::Assets &Goal::Assets() noexcept {
267 return c.GetSimulation().Assets();
270 const app::Assets &Goal::Assets() const noexcept {
271 return c.GetSimulation().Assets();
274 void Goal::SetComplete() noexcept {
283 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
291 IdleGoal::IdleGoal(Creature &c)
297 IdleGoal::~IdleGoal() {
300 std::string IdleGoal::Describe() const {
304 void IdleGoal::Enable() {
307 void IdleGoal::Tick(double dt) {
310 void IdleGoal::Action() {
314 LocateResourceGoal::LocateResourceGoal(Creature &c)
325 LocateResourceGoal::~LocateResourceGoal() noexcept {
328 void LocateResourceGoal::Accept(int resource, double value) {
329 accept.Add(resource, value);
332 std::string LocateResourceGoal::Describe() const {
333 return "locate " + summarize(accept, Assets());
336 void LocateResourceGoal::Enable() {
340 void LocateResourceGoal::Tick(double dt) {
344 void LocateResourceGoal::Action() {
345 if (reevaluate < 0.0) {
352 double dist = glm::length2(GetSituation().Position() - target_pos);
356 GetSteering().GoTo(target_pos);
359 } else if (OnTargetTile()) {
360 GetSteering().Halt();
361 if (!GetSituation().Moving()) {
365 GetSteering().GoTo(target_pos);
367 GetSteering().Haste(Urgency());
370 void LocateResourceGoal::LocateResource() {
371 if (GetSituation().OnTile()) {
372 const world::TileType &t = GetSituation().GetTileType();
373 auto yield = t.FindBestResource(accept);
374 if (yield != t.resources.cend()) {
376 GetSteering().Halt();
379 target_pos = GetSituation().Position();
380 target_srf = GetSituation().Surface();
381 target_tile = GetSituation().GetPlanet().SurfacePosition(target_srf, target_pos);
383 // go find somewhere else
391 void LocateResourceGoal::SearchVicinity() {
392 const world::Planet &planet = GetSituation().GetPlanet();
393 int srf = GetSituation().Surface();
394 const glm::dvec3 &pos = GetSituation().Position();
396 glm::ivec2 loc = planet.SurfacePosition(srf, pos);
397 glm::ivec2 seek_radius(2);
398 glm::ivec2 begin(glm::max(glm::ivec2(0), loc - seek_radius));
399 glm::ivec2 end(glm::min(glm::ivec2(planet.SideLength()), loc + seek_radius + glm::ivec2(1)));
401 double rating[end.y - begin.y][end.x - begin.x];
402 std::memset(rating, 0, sizeof(double) * (end.y - begin.y) * (end.x - begin.x));
404 // find close and rich field
405 for (int y = begin.y; y < end.y; ++y) {
406 for (int x = begin.x; x < end.x; ++x) {
407 const world::TileType &type = planet.TypeAt(srf, x, y);
408 auto yield = type.FindBestResource(accept);
409 if (yield != type.resources.cend()) {
410 // TODO: subtract minimum yield
411 rating[y - begin.y][x - begin.x] = yield->ubiquity * accept.Get(yield->resource);
412 double dist = std::max(0.125, 0.25 * glm::length(planet.TileCenter(srf, x, y) - pos));
413 rating[y - begin.y][x - begin.x] /= dist;
418 // demote crowded tiles
419 for (auto &c : planet.Creatures()) {
420 if (&*c == &GetCreature()) continue;
421 if (c->GetSituation().Surface() != srf) continue;
422 glm::ivec2 coords(c->GetSituation().SurfacePosition());
423 if (coords.x < begin.x || coords.x >= end.x) continue;
424 if (coords.y < begin.y || coords.y >= end.y) continue;
425 rating[coords.y - begin.y][coords.x - begin.x] *= 0.9;
428 glm::ivec2 best_pos(0);
429 double best_rating = -1.0;
431 for (int y = begin.y; y < end.y; ++y) {
432 for (int x = begin.x; x < end.x; ++x) {
433 if (rating[y - begin.y][x - begin.x] > best_rating) {
434 best_pos = glm::ivec2(x, y);
435 best_rating = rating[y - begin.y][x - begin.x];
443 target_pos = planet.TileCenter(srf, best_pos.x, best_pos.y);
445 target_tile = best_pos;
446 GetSteering().GoTo(target_pos);
447 } else if (!searching) {
450 target_pos = GetSituation().Position();
451 target_pos[(srf + 0) % 3] += Assets().random.SNorm();
452 target_pos[(srf + 1) % 3] += Assets().random.SNorm();
453 // bias towards current direction
454 target_pos += glm::normalize(GetSituation().Velocity()) * 0.5;
455 target_pos = clamp(target_pos, -planet.Radius(), planet.Radius());
456 GetSteering().GoTo(target_pos);
460 bool LocateResourceGoal::OnTargetTile() const noexcept {
461 const Situation &s = GetSituation();
463 && s.Surface() == target_srf
465 && s.SurfacePosition() == target_tile;