]> git.localhorst.tv Git - blobs.git/blob - src/creature/goal.cpp
4124876851ba160e433442c0c756b33027f1a6cb
[blobs.git] / src / creature / goal.cpp
1 #include "BlobBackgroundTask.hpp"
2 #include "Goal.hpp"
3 #include "IdleGoal.hpp"
4 #include "IngestGoal.hpp"
5 #include "LocateResourceGoal.hpp"
6
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"
14
15 #include <cstring>
16 #include <iostream>
17 #include <sstream>
18 #include <glm/gtx/io.hpp>
19
20
21 namespace blobs {
22 namespace creature {
23
24 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
25 : Goal(c)
26 , breathing(false)
27 , drink_subtask(nullptr)
28 , eat_subtask(nullptr) {
29 }
30
31 BlobBackgroundTask::~BlobBackgroundTask() {
32 }
33
34 std::string BlobBackgroundTask::Describe() const {
35         return "being a blob";
36 }
37
38 void BlobBackgroundTask::Tick(double dt) {
39         if (breathing) {
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.0 + 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);
50                 }
51                 if (GetCreature().GetStats().Breath().Empty()) {
52                         breathing = false;
53                 }
54         }
55 }
56
57 void BlobBackgroundTask::Action() {
58         CheckStats();
59         CheckSplit();
60         CheckMutate();
61 }
62
63 void BlobBackgroundTask::CheckStats() {
64         Creature::Stats &stats = GetCreature().GetStats();
65
66         if (!breathing && stats.Breath().Bad()) {
67                 breathing = true;
68         }
69
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);
75                         }
76                 }
77                 drink_subtask->OnComplete([&](Goal &) { drink_subtask = nullptr; });
78                 GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
79         }
80
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);
86                         }
87                 }
88                 eat_subtask->OnComplete([&](Goal &) { eat_subtask = nullptr; });
89                 GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
90         }
91
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();
95         } else {
96                 GetCreature().GetSteering().ResumeSeparate();
97         }
98 }
99
100 void BlobBackgroundTask::CheckSplit() {
101         if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
102                 && GetCreature().OffspringChance() > Assets().random.UNorm()) {
103                 GetCreature().GetSimulation().Log() << GetCreature().Name() << " split" << std::endl;
104                 Split(GetCreature());
105                 return;
106         }
107 }
108
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[Assets().random.UInt(9)];
114                 if (Assets().random.UNorm() < 0.5) {
115                         d.Mean(d.Mean() * amount);
116                 } else {
117                         d.StandardDeviation(d.StandardDeviation() * amount);
118                 }
119         }
120 }
121
122 namespace {
123
124 std::string summarize(const Composition &comp, const app::Assets &assets) {
125         std::stringstream s;
126         bool first = true;
127         for (const auto &c : comp) {
128                 if (first) {
129                         first = false;
130                 } else {
131                         s << " or ";
132                 }
133                 s << assets.data.resources[c.resource].label;
134         }
135         return s.str();
136 }
137
138 }
139
140 IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat)
141 : Goal(c)
142 , stat(stat)
143 , accept()
144 , locate_subtask(nullptr)
145 , ingesting(false)
146 , resource(-1)
147 , yield(0.0) {
148         Urgency(stat.value);
149 }
150
151 IngestGoal::~IngestGoal() {
152 }
153
154 void IngestGoal::Accept(int resource, double value) {
155         accept.Add(resource, value);
156 }
157
158 std::string IngestGoal::Describe() const {
159         if (resource == -1) {
160                 return "ingest " + summarize(accept, Assets());
161         } else {
162                 const world::Resource &r = Assets().data.resources[resource];
163                 if (r.state == world::Resource::SOLID) {
164                         return "eat " + r.label;
165                 } else {
166                         return "drink " + r.label;
167                 }
168         }
169 }
170
171 void IngestGoal::Enable() {
172 }
173
174 void IngestGoal::Tick(double dt) {
175         Urgency(stat.value);
176         if (locate_subtask) {
177                 locate_subtask->Urgency(Urgency() + 0.1);
178         }
179         if (ingesting) {
180                 if (OnSuitableTile() && !GetSituation().Moving()) {
181                         // TODO: determine satisfaction factor
182                         GetCreature().Ingest(resource, yield * dt);
183                         stat.Add(-1.0 * yield * dt);
184                         if (stat.Empty()) {
185                                 SetComplete();
186                         }
187                 } else {
188                         // left tile somehow, some idiot probably pushed us off
189                         ingesting = false;
190                         Interruptible(true);
191                 }
192         }
193 }
194
195 void IngestGoal::Action() {
196         if (ingesting) {
197                 // all good
198                 return;
199         }
200         if (OnSuitableTile()) {
201                 if (GetSituation().Moving()) {
202                         // break with maximum force
203                         GetSteering().Haste(1.0);
204                         GetSteering().Halt();
205                 } else {
206                         // finally
207                         Interruptible(false);
208                         ingesting = true;
209                 }
210         } else {
211                 locate_subtask = new LocateResourceGoal(GetCreature());
212                 for (const auto &c : accept) {
213                         locate_subtask->Accept(c.resource, c.value);
214                 }
215                 locate_subtask->Urgency(Urgency() + 0.1);
216                 locate_subtask->OnComplete([&](Goal &){ locate_subtask = nullptr; });
217                 GetCreature().AddGoal(std::unique_ptr<Goal>(locate_subtask));
218         }
219 }
220
221 bool IngestGoal::OnSuitableTile() {
222         if (!GetSituation().OnSurface()) {
223                 return false;
224         }
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;
230                 return true;
231         } else {
232                 resource = -1;
233                 return false;
234         }
235 }
236
237
238 Goal::Goal(Creature &c)
239 : c(c)
240 , on_complete()
241 , urgency(0.0)
242 , interruptible(true)
243 , complete(false) {
244 }
245
246 Goal::~Goal() noexcept {
247 }
248
249 Situation &Goal::GetSituation() noexcept {
250         return c.GetSituation();
251 }
252
253 const Situation &Goal::GetSituation() const noexcept {
254         return c.GetSituation();
255 }
256
257 Steering &Goal::GetSteering() noexcept {
258         return c.GetSteering();
259 }
260
261 const Steering &Goal::GetSteering() const noexcept {
262         return c.GetSteering();
263 }
264
265 app::Assets &Goal::Assets() noexcept {
266         return c.GetSimulation().Assets();
267 }
268
269 const app::Assets &Goal::Assets() const noexcept {
270         return c.GetSimulation().Assets();
271 }
272
273 void Goal::SetComplete() noexcept {
274         if (!complete) {
275                 complete = true;
276                 if (on_complete) {
277                         on_complete(*this);
278                 }
279         }
280 }
281
282 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
283         on_complete = cb;
284         if (complete) {
285                 on_complete(*this);
286         }
287 }
288
289
290 IdleGoal::IdleGoal(Creature &c)
291 : Goal(c) {
292         Urgency(-1.0);
293         Interruptible(true);
294 }
295
296 IdleGoal::~IdleGoal() {
297 }
298
299 std::string IdleGoal::Describe() const {
300         return "idle";
301 }
302
303 void IdleGoal::Enable() {
304 }
305
306 void IdleGoal::Tick(double dt) {
307 }
308
309 void IdleGoal::Action() {
310 }
311
312
313 LocateResourceGoal::LocateResourceGoal(Creature &c)
314 : Goal(c)
315 , accept()
316 , found(false)
317 , target_pos(0.0)
318 , searching(false)
319 , reevaluate(0.0) {
320 }
321
322 LocateResourceGoal::~LocateResourceGoal() noexcept {
323 }
324
325 void LocateResourceGoal::Accept(int resource, double value) {
326         accept.Add(resource, value);
327 }
328
329 std::string LocateResourceGoal::Describe() const {
330         return "locate " + summarize(accept, Assets());
331 }
332
333 void LocateResourceGoal::Enable() {
334
335 }
336
337 void LocateResourceGoal::Tick(double dt) {
338         reevaluate -= dt;
339 }
340
341 void LocateResourceGoal::Action() {
342         if (reevaluate < 0.0) {
343                 LocateResource();
344                 reevaluate = 3.0;
345         } else if (!found) {
346                 if (!searching) {
347                         LocateResource();
348                 } else {
349                         double dist = glm::length2(GetSituation().Position() - target_pos);
350                         if (dist < 0.0001) {
351                                 LocateResource();
352                         } else {
353                                 GetSteering().GoTo(target_pos);
354                         }
355                 }
356         } else if (NearTarget()) {
357                 GetSteering().Halt();
358                 if (!GetSituation().Moving()) {
359                         SetComplete();
360                 }
361         } else {
362                 GetSteering().GoTo(target_pos);
363         }
364         GetSteering().Haste(Urgency());
365 }
366
367 void LocateResourceGoal::LocateResource() {
368         if (GetSituation().OnSurface()) {
369                 const world::TileType &t = GetSituation().GetTileType();
370                 auto yield = t.FindBestResource(accept);
371                 if (yield != t.resources.cend()) {
372                         // hoooray
373                         GetSteering().Halt();
374                         found = true;
375                         searching = false;
376                         target_pos = GetSituation().Position();
377                 } else {
378                         // go find somewhere else
379                         SearchVicinity();
380                 }
381         } else {
382                 // well, what now?
383                 found = false;
384                 searching = false;
385         }
386 }
387
388 void LocateResourceGoal::SearchVicinity() {
389         const world::Planet &planet = GetSituation().GetPlanet();
390         const glm::dvec3 &pos = GetSituation().Position();
391         const glm::dvec3 normal(planet.NormalAt(pos));
392         const glm::dvec3 step_x(normalize(cross(normal, glm::dvec3(normal.z, normal.x, normal.y))));
393         const glm::dvec3 step_y(normalize(cross(step_x, normal)));
394
395         constexpr int search_radius = 2;
396         double rating[2 * search_radius + 1][2 * search_radius + 1] = {0};
397
398         // find close and rich field
399         for (int y = -search_radius; y < search_radius + 1; ++y) {
400                 for (int x = -search_radius; x < search_radius + 1; ++x) {
401                         const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
402                         if (!GetCreature().PerceptionTest(tpos)) continue;
403                         const world::TileType &type = planet.TileTypeAt(tpos);
404                         auto yield = type.FindBestResource(accept);
405                         if (yield != type.resources.cend()) {
406                                 // TODO: subtract minimum yield
407                                 rating[y + search_radius][x + search_radius] = yield->ubiquity * accept.Get(yield->resource);
408                                 // penalize distance
409                                 double dist = std::max(0.125, 0.25 * glm::length(tpos - pos));
410                                 rating[y + search_radius][x + search_radius] /= dist;
411                         }
412                 }
413         }
414
415         // penalize crowding
416         for (auto &c : planet.Creatures()) {
417                 if (&*c == &GetCreature()) continue;
418                 for (int y = -search_radius; y < search_radius + 1; ++y) {
419                         for (int x = -search_radius; x < search_radius + 1; ++x) {
420                                 const glm::dvec3 tpos(pos + (double(x) * step_x) + (double(y) * step_y));
421                                 if (length2(tpos - c->GetSituation().Position()) < 1.0) {
422                                         rating[y + search_radius][x + search_radius] *= 0.8;
423                                 }
424                         }
425                 }
426         }
427
428         glm::ivec2 best_pos(0);
429         double best_rating = -1.0;
430
431         for (int y = -search_radius; y < search_radius + 1; ++y) {
432                 for (int x = -search_radius; x < search_radius + 1; ++x) {
433                         if (rating[y + search_radius][x + search_radius] > best_rating) {
434                                 best_pos = glm::ivec2(x, y);
435                                 best_rating = rating[y + search_radius][x + search_radius];
436                         }
437                 }
438         }
439
440         if (best_rating) {
441                 found = true;
442                 searching = false;
443                 target_pos = normalize(pos + (double(best_pos.x) * step_x) + (double(best_pos.y) * step_y)) * planet.Radius();
444                 GetSteering().GoTo(target_pos);
445         } else if (!searching) {
446                 found = false;
447                 searching = true;
448                 target_pos = GetSituation().Position();
449                 target_pos += Assets().random.SNorm() * step_x;
450                 target_pos += Assets().random.SNorm() * step_y;
451                 // bias towards current heading
452                 target_pos += GetSituation().Heading() * 0.5;
453                 target_pos = normalize(target_pos) * planet.Radius();
454                 GetSteering().GoTo(target_pos);
455         }
456 }
457
458 bool LocateResourceGoal::NearTarget() const noexcept {
459         const Situation &s = GetSituation();
460         return s.OnSurface() && length2(s.Position() - target_pos) < 0.5;
461 }
462
463 }
464 }