]> git.localhorst.tv Git - blobs.git/blob - src/creature/goal.cpp
old developers
[blobs.git] / src / creature / goal.cpp
1 #include "BlobBackgroundTask.hpp"
2 #include "Goal.hpp"
3 #include "IdleGoal.hpp"
4 #include "LocateResourceGoal.hpp"
5
6 #include "Creature.hpp"
7 #include "../app/Assets.hpp"
8 #include "../world/Planet.hpp"
9 #include "../world/Resource.hpp"
10 #include "../world/Simulation.hpp"
11 #include "../world/TileType.hpp"
12
13 #include <cstring>
14 #include <iostream>
15 #include <glm/gtx/io.hpp>
16
17
18 namespace blobs {
19 namespace creature {
20
21 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
22 : Goal(c) {
23 }
24
25 BlobBackgroundTask::~BlobBackgroundTask() {
26 }
27
28 std::string BlobBackgroundTask::Describe() const {
29         return "being a blob";
30 }
31
32 void BlobBackgroundTask::Tick(double dt) {
33 }
34
35 void BlobBackgroundTask::Action() {
36         // check if eligible to split
37         if (GetCreature().Mass() > GetCreature().GetProperties().Birth().mass * 1.8) {
38                 double fert = GetCreature().Fertility();
39                 double rand = Assets().random.UNorm();
40                 if (fert > rand) {
41                         std::cout << "[" << int(GetCreature().GetSimulation().Time())
42                                 << "s] " << GetCreature().Name() << " split" << std::endl;
43                         Split(GetCreature());
44                         return;
45                 }
46         }
47         // check for random property mutation
48         if (GetCreature().Mutability() > Assets().random.UNorm()) {
49                 double amount = 1.0 + (Assets().random.SNorm() * 0.05);
50                 auto &props = GetCreature().GetGenome().properties;
51                 double r = Assets().random.UNorm();
52                 math::Distribution *d = nullptr;
53                 if (Assets().random.UNorm() < 0.5) {
54                         auto &set = props.props[(int(Assets().random.UNorm() * 4.0) % 4) + 1];
55                         if (r < 0.25) {
56                                 d = &set.age;
57                         } else if (r < 0.5) {
58                                 d = &set.mass;
59                         } else if (r < 0.75) {
60                                 d = &set.fertility;
61                         } else {
62                                 d = &set.highlight;
63                         }
64                 } else {
65                         if (r < 0.2) {
66                                 d = &props.strength;
67                         } else if (r < 0.4) {
68                                 d = &props.stamina;
69                         } else if (r < 0.6) {
70                                 d = &props.dexerty;
71                         } else if (r < 0.8) {
72                                 d = &props.intelligence;
73                         } else {
74                                 d = &props.mutability;
75                         }
76                 }
77                 if (Assets().random.UNorm() < 0.5) {
78                         d->Mean(d->Mean() * amount);
79                 } else {
80                         d->StandardDeviation(d->StandardDeviation() * amount);
81                 }
82         }
83 }
84
85
86 Goal::Goal(Creature &c)
87 : c(c)
88 , on_complete()
89 , urgency(0.0)
90 , interruptible(true)
91 , complete(false) {
92 }
93
94 Goal::~Goal() noexcept {
95 }
96
97 Situation &Goal::GetSituation() noexcept {
98         return c.GetSituation();
99 }
100
101 const Situation &Goal::GetSituation() const noexcept {
102         return c.GetSituation();
103 }
104
105 Steering &Goal::GetSteering() noexcept {
106         return c.GetSteering();
107 }
108
109 const Steering &Goal::GetSteering() const noexcept {
110         return c.GetSteering();
111 }
112
113 app::Assets &Goal::Assets() noexcept {
114         return c.GetSimulation().Assets();
115 }
116
117 const app::Assets &Goal::Assets() const noexcept {
118         return c.GetSimulation().Assets();
119 }
120
121 void Goal::SetComplete() noexcept {
122         if (!complete) {
123                 complete = true;
124                 if (on_complete) {
125                         on_complete(*this);
126                 }
127         }
128 }
129
130 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
131         on_complete = cb;
132         if (complete) {
133                 on_complete(*this);
134         }
135 }
136
137
138 IdleGoal::IdleGoal(Creature &c)
139 : Goal(c) {
140         Urgency(-1.0);
141         Interruptible(true);
142 }
143
144 IdleGoal::~IdleGoal() {
145 }
146
147 std::string IdleGoal::Describe() const {
148         return "idle";
149 }
150
151 void IdleGoal::Enable() {
152 }
153
154 void IdleGoal::Tick(double dt) {
155 }
156
157 void IdleGoal::Action() {
158 }
159
160
161 LocateResourceGoal::LocateResourceGoal(Creature &c, int res)
162 : Goal(c)
163 , res(res)
164 , found(false)
165 , target_pos(0.0)
166 , target_srf(0)
167 , target_tile(0)
168 , searching(false)
169 , reevaluate(0.0) {
170 }
171
172 LocateResourceGoal::~LocateResourceGoal() noexcept {
173 }
174
175 std::string LocateResourceGoal::Describe() const {
176         return "locate " + GetCreature().GetSimulation().Resources()[res].name;
177 }
178
179 void LocateResourceGoal::Enable() {
180
181 }
182
183 void LocateResourceGoal::Tick(double dt) {
184         reevaluate -= dt;
185 }
186
187 void LocateResourceGoal::Action() {
188         if (reevaluate < 0.0) {
189                 LocateResource();
190                 reevaluate = 3.0;
191         } else if (!found) {
192                 if (!searching) {
193                         LocateResource();
194                 } else {
195                         double dist = glm::length2(GetSituation().Position() - target_pos);
196                         if (dist < 0.0001) {
197                                 LocateResource();
198                         } else {
199                                 GetSteering().GoTo(target_pos);
200                         }
201                 }
202         } else if (OnTargetTile()) {
203                 GetSteering().Halt();
204                 if (!GetSituation().Moving()) {
205                         SetComplete();
206                 }
207         } else {
208                 GetSteering().GoTo(target_pos);
209         }
210         GetSteering().Haste(Urgency());
211 }
212
213 void LocateResourceGoal::LocateResource() {
214         if (GetSituation().OnSurface()) {
215                 const world::TileType &t = GetSituation().GetTileType();
216                 auto yield = t.FindResource(res);
217                 if (yield != t.resources.cend()) {
218                         // hoooray
219                         GetSteering().Halt();
220                         found = true;
221                         searching = false;
222                         target_pos = GetSituation().Position();
223                         target_srf = GetSituation().Surface();
224                         target_tile = GetSituation().GetPlanet().SurfacePosition(target_srf, target_pos);
225                 } else {
226                         // go find somewhere else
227                         SearchVicinity();
228                 }
229         } else {
230                 // well, what now?
231         }
232 }
233
234 void LocateResourceGoal::SearchVicinity() {
235         const world::Planet &planet = GetSituation().GetPlanet();
236         int srf = GetSituation().Surface();
237         const glm::dvec3 &pos = GetSituation().Position();
238
239         glm::ivec2 loc = planet.SurfacePosition(srf, pos);
240         glm::ivec2 seek_radius(2);
241         glm::ivec2 begin(glm::max(glm::ivec2(0), loc - seek_radius));
242         glm::ivec2 end(glm::min(glm::ivec2(planet.SideLength()), loc + seek_radius + glm::ivec2(1)));
243
244         double rating[end.y - begin.y][end.x - begin.x];
245         std::memset(rating, 0, sizeof(double) * (end.y - begin.y) * (end.x - begin.x));
246
247         // find close and rich field
248         for (int y = begin.y; y < end.y; ++y) {
249                 for (int x = begin.x; x < end.x; ++x) {
250                         const world::TileType &type = planet.TypeAt(srf, x, y);
251                         auto yield = type.FindResource(res);
252                         if (yield != type.resources.cend()) {
253                                 // TODO: subtract minimum yield
254                                 rating[y - begin.y][x - begin.x] = yield->ubiquity;
255                                 double dist = std::max(0.125, 0.25 * glm::length(planet.TileCenter(srf, x, y) - pos));
256                                 rating[y - begin.y][x - begin.x] /= dist;
257                         }
258                 }
259         }
260
261         // demote crowded tiles
262         for (auto &c : planet.Creatures()) {
263                 if (&*c == &GetCreature()) continue;
264                 if (c->GetSituation().Surface() != srf) continue;
265                 glm::ivec2 coords(c->GetSituation().SurfacePosition());
266                 if (coords.x < begin.x || coords.x >= end.x) continue;
267                 if (coords.y < begin.y || coords.y >= end.y) continue;
268                 rating[coords.y - begin.y][coords.x - begin.x] *= 0.9;
269         }
270
271         glm::ivec2 best_pos(0);
272         double best_rating = -1.0;
273
274         for (int y = begin.y; y < end.y; ++y) {
275                 for (int x = begin.x; x < end.x; ++x) {
276                         if (rating[y - begin.y][x - begin.x] > best_rating) {
277                                 best_pos = glm::ivec2(x, y);
278                                 best_rating = rating[y - begin.y][x - begin.x];
279                         }
280                 }
281         }
282
283         if (best_rating) {
284                 found = true;
285                 searching = false;
286                 target_pos = planet.TileCenter(srf, best_pos.x, best_pos.y);
287                 target_srf = srf;
288                 target_tile = best_pos;
289                 GetSteering().GoTo(target_pos);
290         } else if (!searching) {
291                 found = false;
292                 searching = true;
293                 target_pos = GetSituation().Position();
294                 target_pos[(srf + 0) % 3] += Assets().random.SNorm();
295                 target_pos[(srf + 1) % 3] += Assets().random.SNorm();
296                 // bias towards current direction
297                 target_pos += glm::normalize(GetSituation().Velocity()) * 0.5;
298                 target_pos = clamp(target_pos, -planet.Radius(), planet.Radius());
299                 GetSteering().GoTo(target_pos);
300         }
301 }
302
303 bool LocateResourceGoal::OnTargetTile() const noexcept {
304         const Situation &s = GetSituation();
305         return s.OnSurface()
306                 && s.Surface() == target_srf
307                 && s.OnTile()
308                 && s.SurfacePosition() == target_tile;
309 }
310
311 }
312 }