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 int gas = Assets().data.resources["air"].id;
41 // TODO: check if in compatible atmosphere
42 double amount = GetCreature().GetStats().Breath().gain * -(1.5 + 0.5 * GetCreature().ExhaustionFactor());
43 GetCreature().GetStats().Breath().Add(amount * dt);
44 // maintain ~2.5% gas composition
45 double gas_amount = GetCreature().GetComposition().Get(gas);
46 if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.025) {
47 double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
48 GetCreature().Ingest(gas, add);
50 if (GetCreature().GetStats().Breath().Empty()) {
56 void BlobBackgroundTask::Action() {
62 void BlobBackgroundTask::CheckStats() {
63 Creature::Stats &stats = GetCreature().GetStats();
65 if (!breathing && stats.Breath().Bad()) {
69 if (!drink_subtask && stats.Thirst().Bad()) {
70 drink_subtask = new IngestGoal(GetCreature(), stats.Thirst());
71 for (const auto &cmp : GetCreature().GetComposition()) {
72 if (Assets().data.resources[cmp.resource].state == world::Resource::LIQUID) {
73 drink_subtask->Accept(cmp.resource, 1.0);
76 drink_subtask->OnComplete([&](Goal &) { drink_subtask = nullptr; });
77 GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
80 if (!eat_subtask && stats.Hunger().Bad()) {
81 eat_subtask = new IngestGoal(GetCreature(), stats.Hunger());
82 for (const auto &cmp : GetCreature().GetComposition()) {
83 if (Assets().data.resources[cmp.resource].state == world::Resource::SOLID) {
84 eat_subtask->Accept(cmp.resource, 1.0);
87 eat_subtask->OnComplete([&](Goal &) { eat_subtask = nullptr; });
88 GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
91 // when in bad shape, don't make much effort
92 if (stats.Damage().Bad() || stats.Exhaustion().Bad() || stats.Fatigue().Critical()) {
93 GetCreature().GetSteering().DontSeparate();
95 GetCreature().GetSteering().ResumeSeparate();
99 void BlobBackgroundTask::CheckSplit() {
100 if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
101 && GetCreature().OffspringChance() > Assets().random.UNorm()) {
102 std::cout << "[" << int(GetCreature().GetSimulation().Time())
103 << "s] " << GetCreature().Name() << " split" << std::endl;
104 Split(GetCreature());
109 void BlobBackgroundTask::CheckMutate() {
110 // check for random property mutation
111 if (GetCreature().MutateChance() > Assets().random.UNorm()) {
112 double amount = 1.0 + (Assets().random.SNorm() * 0.05);
113 math::Distribution &d = GetCreature().GetGenome().properties.props[(int(Assets().random.UNorm() * 8.0) % 8)];
114 if (Assets().random.UNorm() < 0.5) {
115 d.Mean(d.Mean() * amount);
117 d.StandardDeviation(d.StandardDeviation() * amount);
124 std::string summarize(const Composition &comp, const app::Assets &assets) {
127 for (const auto &c : comp) {
133 s << assets.data.resources[c.resource].label;
140 IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat)
144 , locate_subtask(nullptr)
151 IngestGoal::~IngestGoal() {
154 void IngestGoal::Accept(int resource, double value) {
155 accept.Add(resource, value);
158 std::string IngestGoal::Describe() const {
159 if (resource == -1) {
160 return "ingest " + summarize(accept, Assets());
162 const world::Resource &r = Assets().data.resources[resource];
163 if (r.state == world::Resource::SOLID) {
164 return "eat " + r.label;
166 return "drink " + r.label;
171 void IngestGoal::Enable() {
174 void IngestGoal::Tick(double dt) {
176 if (locate_subtask) {
177 locate_subtask->Urgency(Urgency() + 0.1);
180 if (OnSuitableTile() && !GetSituation().Moving()) {
181 // TODO: determine satisfaction factor
182 GetCreature().Ingest(resource, yield * dt);
183 stat.Add(-1.0 * yield * dt);
188 // left tile somehow, some idiot probably pushed us off
195 void IngestGoal::Action() {
200 if (OnSuitableTile()) {
201 if (GetSituation().Moving()) {
202 // break with maximum force
203 GetSteering().Haste(1.0);
204 GetSteering().Halt();
207 Interruptible(false);
211 locate_subtask = new LocateResourceGoal(GetCreature());
212 for (const auto &c : accept) {
213 locate_subtask->Accept(c.resource, c.value);
215 locate_subtask->Urgency(Urgency() + 0.1);
216 locate_subtask->OnComplete([&](Goal &){ locate_subtask = nullptr; });
217 GetCreature().AddGoal(std::unique_ptr<Goal>(locate_subtask));
221 bool IngestGoal::OnSuitableTile() {
222 if (!GetSituation().OnTile()) {
225 const world::TileType &t = GetSituation().GetTileType();
226 auto found = t.FindBestResource(accept);
227 if (found != t.resources.end()) {
228 resource = found->resource;
229 yield = found->ubiquity;
238 Goal::Goal(Creature &c)
242 , interruptible(true)
246 Goal::~Goal() noexcept {
249 Situation &Goal::GetSituation() noexcept {
250 return c.GetSituation();
253 const Situation &Goal::GetSituation() const noexcept {
254 return c.GetSituation();
257 Steering &Goal::GetSteering() noexcept {
258 return c.GetSteering();
261 const Steering &Goal::GetSteering() const noexcept {
262 return c.GetSteering();
265 app::Assets &Goal::Assets() noexcept {
266 return c.GetSimulation().Assets();
269 const app::Assets &Goal::Assets() const noexcept {
270 return c.GetSimulation().Assets();
273 void Goal::SetComplete() noexcept {
282 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
290 IdleGoal::IdleGoal(Creature &c)
296 IdleGoal::~IdleGoal() {
299 std::string IdleGoal::Describe() const {
303 void IdleGoal::Enable() {
306 void IdleGoal::Tick(double dt) {
309 void IdleGoal::Action() {
313 LocateResourceGoal::LocateResourceGoal(Creature &c)
324 LocateResourceGoal::~LocateResourceGoal() noexcept {
327 void LocateResourceGoal::Accept(int resource, double value) {
328 accept.Add(resource, value);
331 std::string LocateResourceGoal::Describe() const {
332 return "locate " + summarize(accept, Assets());
335 void LocateResourceGoal::Enable() {
339 void LocateResourceGoal::Tick(double dt) {
343 void LocateResourceGoal::Action() {
344 if (reevaluate < 0.0) {
351 double dist = glm::length2(GetSituation().Position() - target_pos);
355 GetSteering().GoTo(target_pos);
358 } else if (OnTargetTile()) {
359 GetSteering().Halt();
360 if (!GetSituation().Moving()) {
364 GetSteering().GoTo(target_pos);
366 GetSteering().Haste(Urgency());
369 void LocateResourceGoal::LocateResource() {
370 if (GetSituation().OnTile()) {
371 const world::TileType &t = GetSituation().GetTileType();
372 auto yield = t.FindBestResource(accept);
373 if (yield != t.resources.cend()) {
375 GetSteering().Halt();
378 target_pos = GetSituation().Position();
379 target_srf = GetSituation().Surface();
380 target_tile = GetSituation().GetPlanet().SurfacePosition(target_srf, target_pos);
382 // go find somewhere else
390 void LocateResourceGoal::SearchVicinity() {
391 const world::Planet &planet = GetSituation().GetPlanet();
392 int srf = GetSituation().Surface();
393 const glm::dvec3 &pos = GetSituation().Position();
395 glm::ivec2 loc = planet.SurfacePosition(srf, pos);
396 glm::ivec2 seek_radius(2);
397 glm::ivec2 begin(glm::max(glm::ivec2(0), loc - seek_radius));
398 glm::ivec2 end(glm::min(glm::ivec2(planet.SideLength()), loc + seek_radius + glm::ivec2(1)));
400 double rating[end.y - begin.y][end.x - begin.x];
401 std::memset(rating, 0, sizeof(double) * (end.y - begin.y) * (end.x - begin.x));
403 // find close and rich field
404 for (int y = begin.y; y < end.y; ++y) {
405 for (int x = begin.x; x < end.x; ++x) {
406 const world::TileType &type = planet.TypeAt(srf, x, y);
407 auto yield = type.FindBestResource(accept);
408 if (yield != type.resources.cend()) {
409 // TODO: subtract minimum yield
410 rating[y - begin.y][x - begin.x] = yield->ubiquity * accept.Get(yield->resource);
411 double dist = std::max(0.125, 0.25 * glm::length(planet.TileCenter(srf, x, y) - pos));
412 rating[y - begin.y][x - begin.x] /= dist;
417 // demote crowded tiles
418 for (auto &c : planet.Creatures()) {
419 if (&*c == &GetCreature()) continue;
420 if (c->GetSituation().Surface() != srf) continue;
421 glm::ivec2 coords(c->GetSituation().SurfacePosition());
422 if (coords.x < begin.x || coords.x >= end.x) continue;
423 if (coords.y < begin.y || coords.y >= end.y) continue;
424 rating[coords.y - begin.y][coords.x - begin.x] *= 0.9;
427 glm::ivec2 best_pos(0);
428 double best_rating = -1.0;
430 for (int y = begin.y; y < end.y; ++y) {
431 for (int x = begin.x; x < end.x; ++x) {
432 if (rating[y - begin.y][x - begin.x] > best_rating) {
433 best_pos = glm::ivec2(x, y);
434 best_rating = rating[y - begin.y][x - begin.x];
442 target_pos = planet.TileCenter(srf, best_pos.x, best_pos.y);
444 target_tile = best_pos;
445 GetSteering().GoTo(target_pos);
446 } else if (!searching) {
449 target_pos = GetSituation().Position();
450 target_pos[(srf + 0) % 3] += Assets().random.SNorm();
451 target_pos[(srf + 1) % 3] += Assets().random.SNorm();
452 // bias towards current direction
453 target_pos += glm::normalize(GetSituation().Velocity()) * 0.5;
454 target_pos = clamp(target_pos, -planet.Radius(), planet.Radius());
455 GetSteering().GoTo(target_pos);
459 bool LocateResourceGoal::OnTargetTile() const noexcept {
460 const Situation &s = GetSituation();
462 && s.Surface() == target_srf
464 && s.SurfacePosition() == target_tile;