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"
14 #include <glm/gtx/io.hpp>
20 BlobBackgroundTask::BlobBackgroundTask(Creature &c)
24 BlobBackgroundTask::~BlobBackgroundTask() {
27 std::string BlobBackgroundTask::Describe() const {
28 return "being a blob";
31 void BlobBackgroundTask::Tick(double dt) {
34 void BlobBackgroundTask::Action() {
35 // check if eligible to split
36 if (GetCreature().Mass() > GetCreature().GetProperties().Birth().mass * 1.8) {
37 double fert = GetCreature().Fertility();
38 double rand = Assets().random.UNorm();
40 std::cout << "[" << int(GetCreature().GetSimulation().Time())
41 << "s] " << GetCreature().Name() << " split" << std::endl;
46 // check for random property mutation
47 if (GetCreature().Mutability() > Assets().random.UNorm()) {
48 double amount = 1.0 + (Assets().random.SNorm() * 0.05);
49 auto &props = GetCreature().GetGenome().properties;
50 double r = Assets().random.UNorm();
51 math::Distribution *d = nullptr;
52 if (Assets().random.UNorm() < 0.5) {
53 auto &set = props.props[(int(Assets().random.UNorm() * 4.0) % 4) + 1];
58 } else if (r < 0.75) {
71 d = &props.intelligence;
73 d = &props.mutability;
76 if (Assets().random.UNorm() < 0.5) {
77 d->Mean(d->Mean() * amount);
79 d->StandardDeviation(d->StandardDeviation() * amount);
85 Goal::Goal(Creature &c)
93 Goal::~Goal() noexcept {
96 Situation &Goal::GetSituation() noexcept {
97 return c.GetSituation();
100 const Situation &Goal::GetSituation() const noexcept {
101 return c.GetSituation();
104 Steering &Goal::GetSteering() noexcept {
105 return c.GetSteering();
108 const Steering &Goal::GetSteering() const noexcept {
109 return c.GetSteering();
112 app::Assets &Goal::Assets() noexcept {
113 return c.GetSimulation().Assets();
116 const app::Assets &Goal::Assets() const noexcept {
117 return c.GetSimulation().Assets();
120 void Goal::SetComplete() noexcept {
129 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
137 IdleGoal::IdleGoal(Creature &c)
143 IdleGoal::~IdleGoal() {
146 std::string IdleGoal::Describe() const {
150 void IdleGoal::Enable() {
153 void IdleGoal::Tick(double dt) {
156 void IdleGoal::Action() {
160 LocateResourceGoal::LocateResourceGoal(Creature &c, int res)
171 LocateResourceGoal::~LocateResourceGoal() noexcept {
174 std::string LocateResourceGoal::Describe() const {
175 return "locate " + GetCreature().GetSimulation().Resources()[res].name;
178 void LocateResourceGoal::Enable() {
182 void LocateResourceGoal::Tick(double dt) {
186 void LocateResourceGoal::Action() {
187 if (reevaluate < 0.0) {
194 double dist = glm::length2(GetSituation().Position() - target_pos);
198 GetSteering().GoTo(target_pos);
201 } else if (OnTargetTile()) {
202 GetSteering().Halt();
203 if (!GetSituation().Moving()) {
207 GetSteering().GoTo(target_pos);
209 GetSteering().Haste(Urgency());
212 void LocateResourceGoal::LocateResource() {
213 if (GetSituation().OnSurface()) {
214 const world::TileType &t = GetSituation().GetTileType();
215 auto yield = t.FindResource(res);
216 if (yield != t.resources.cend()) {
218 GetSteering().Halt();
221 target_pos = GetSituation().Position();
222 target_srf = GetSituation().Surface();
223 target_tile = GetSituation().GetPlanet().SurfacePosition(target_srf, target_pos);
225 // go find somewhere else
233 void LocateResourceGoal::SearchVicinity() {
234 const world::Planet &planet = GetSituation().GetPlanet();
235 int srf = GetSituation().Surface();
236 const glm::dvec3 &pos = GetSituation().Position();
238 glm::ivec2 loc = planet.SurfacePosition(srf, pos);
239 glm::ivec2 seek_radius(2);
240 glm::ivec2 begin(glm::max(glm::ivec2(0), loc - seek_radius));
241 glm::ivec2 end(glm::min(glm::ivec2(planet.SideLength()), loc + seek_radius + glm::ivec2(1)));
243 double rating[end.y - begin.y][end.x - begin.x];
244 std::memset(rating, 0, sizeof(double) * (end.y - begin.y) * (end.x - begin.x))
246 // find close and rich field
247 for (int y = begin.y; y < end.y; ++y) {
248 for (int x = begin.x; x < end.x; ++x) {
249 const world::TileType &type = planet.TypeAt(srf, x, y);
250 auto yield = type.FindResource(res);
251 if (yield != type.resources.cend()) {
252 // TODO: subtract minimum yield
253 rating[y - begin.y][x - begin.x] = yield->ubiquity;
254 double dist = std::max(0.125, 0.25 * glm::length(planet.TileCenter(srf, x, y) - pos));
255 rating[y - begin.y][x - begin.x] /= dist;
260 // demote crowded tiles
261 for (auto &c : planet.Creatures()) {
262 if (&*c == &GetCreature()) continue;
263 if (c->GetSituation().Surface() != srf) continue;
264 glm::ivec2 coords(c->GetSituation().SurfacePosition());
265 if (coords.x < begin.x || coords.x >= end.x) continue;
266 if (coords.y < begin.y || coords.y >= end.y) continue;
267 rating[coords.y - begin.y][coords.x - begin.x] *= 0.9;
270 glm::ivec2 best_pos(0);
271 double best_rating = -1.0;
273 for (int y = begin.y; y < end.y; ++y) {
274 for (int x = begin.x; x < end.x; ++x) {
275 if (rating[y - begin.y][x - begin.x] > best_rating) {
276 best_pos = glm::ivec2(x, y);
277 best_rating = rating[y - begin.y][x - begin.x];
285 target_pos = planet.TileCenter(srf, best_pos.x, best_pos.y);
287 target_tile = best_pos;
288 GetSteering().GoTo(target_pos);
289 } else if (!searching) {
292 target_pos = GetSituation().Position();
293 target_pos[(srf + 0) % 3] += Assets().random.SNorm();
294 target_pos[(srf + 1) % 3] += Assets().random.SNorm();
295 // bias towards current direction
296 target_pos += glm::normalize(GetSituation().Velocity()) * 0.5;
297 target_pos = clamp(target_pos, -planet.Radius(), planet.Radius());
298 GetSteering().GoTo(target_pos);
302 bool LocateResourceGoal::OnTargetTile() const noexcept {
303 const Situation &s = GetSituation();
305 && s.Surface() == target_srf
307 && s.SurfacePosition() == target_tile;