1 #include "BlobBackgroundTask.hpp"
3 #include "IdleGoal.hpp"
4 #include "LocateResourceGoal.hpp"
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"
15 #include <glm/gtx/io.hpp>
21 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
25 BlobBackgroundTask::~BlobBackgroundTask() {
28 std::string BlobBackgroundTask::Describe() const {
29 return "being a blob";
32 void BlobBackgroundTask::Tick(double dt) {
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();
41 std::cout << "[" << int(GetCreature().GetSimulation().Time())
42 << "s] " << GetCreature().Name() << " split" << std::endl;
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];
59 } else if (r < 0.75) {
72 d = &props.intelligence;
74 d = &props.mutability;
77 if (Assets().random.UNorm() < 0.5) {
78 d->Mean(d->Mean() * amount);
80 d->StandardDeviation(d->StandardDeviation() * amount);
86 Goal::Goal(Creature &c)
94 Goal::~Goal() noexcept {
97 Situation &Goal::GetSituation() noexcept {
98 return c.GetSituation();
101 const Situation &Goal::GetSituation() const noexcept {
102 return c.GetSituation();
105 Steering &Goal::GetSteering() noexcept {
106 return c.GetSteering();
109 const Steering &Goal::GetSteering() const noexcept {
110 return c.GetSteering();
113 app::Assets &Goal::Assets() noexcept {
114 return c.GetSimulation().Assets();
117 const app::Assets &Goal::Assets() const noexcept {
118 return c.GetSimulation().Assets();
121 void Goal::SetComplete() noexcept {
130 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
138 IdleGoal::IdleGoal(Creature &c)
144 IdleGoal::~IdleGoal() {
147 std::string IdleGoal::Describe() const {
151 void IdleGoal::Enable() {
154 void IdleGoal::Tick(double dt) {
157 void IdleGoal::Action() {
161 LocateResourceGoal::LocateResourceGoal(Creature &c, int res)
172 LocateResourceGoal::~LocateResourceGoal() noexcept {
175 std::string LocateResourceGoal::Describe() const {
176 return "locate " + GetCreature().GetSimulation().Resources()[res].name;
179 void LocateResourceGoal::Enable() {
183 void LocateResourceGoal::Tick(double dt) {
187 void LocateResourceGoal::Action() {
188 if (reevaluate < 0.0) {
195 double dist = glm::length2(GetSituation().Position() - target_pos);
199 GetSteering().GoTo(target_pos);
202 } else if (OnTargetTile()) {
203 GetSteering().Halt();
204 if (!GetSituation().Moving()) {
208 GetSteering().GoTo(target_pos);
210 GetSteering().Haste(Urgency());
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()) {
219 GetSteering().Halt();
222 target_pos = GetSituation().Position();
223 target_srf = GetSituation().Surface();
224 target_tile = GetSituation().GetPlanet().SurfacePosition(target_srf, target_pos);
226 // go find somewhere else
234 void LocateResourceGoal::SearchVicinity() {
235 const world::Planet &planet = GetSituation().GetPlanet();
236 int srf = GetSituation().Surface();
237 const glm::dvec3 &pos = GetSituation().Position();
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)));
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));
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;
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;
271 glm::ivec2 best_pos(0);
272 double best_rating = -1.0;
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];
286 target_pos = planet.TileCenter(srf, best_pos.x, best_pos.y);
288 target_tile = best_pos;
289 GetSteering().GoTo(target_pos);
290 } else if (!searching) {
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);
303 bool LocateResourceGoal::OnTargetTile() const noexcept {
304 const Situation &s = GetSituation();
306 && s.Surface() == target_srf
308 && s.SurfacePosition() == target_tile;