]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/goal.cpp
color mutation
[blobs.git] / src / creature / goal.cpp
index 31983dee92fe6d0650381bd6b1fa90a2edfadd75..a4f33304f3f75af56b86c376b0c653879405006e 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "Creature.hpp"
 #include "../app/Assets.hpp"
+#include "../ui/string.hpp"
 #include "../world/Planet.hpp"
 #include "../world/Resource.hpp"
 #include "../world/Simulation.hpp"
@@ -37,8 +38,16 @@ std::string BlobBackgroundTask::Describe() const {
 void BlobBackgroundTask::Tick(double dt) {
        if (breathing) {
                // TODO: derive breathing ability
-               double amount = GetCreature().GetStats().Breath().gain * -(1.0 + GetCreature().ExhaustionFactor());
+               int gas = Assets().data.resources["air"].id;
+               // TODO: check if in compatible atmosphere
+               double amount = GetCreature().GetStats().Breath().gain * -(1.5 + 0.5 * GetCreature().ExhaustionFactor());
                GetCreature().GetStats().Breath().Add(amount * dt);
+               // maintain ~1% gas composition
+               double gas_amount = GetCreature().GetComposition().Get(gas);
+               if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.01) {
+                       double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
+                       GetCreature().Ingest(gas, add);
+               }
                if (GetCreature().GetStats().Breath().Empty()) {
                        breathing = false;
                }
@@ -79,13 +88,20 @@ void BlobBackgroundTask::CheckStats() {
                eat_subtask->OnComplete([&](Goal &) { eat_subtask = nullptr; });
                GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
        }
+
+       // when in bad shape, don't make much effort
+       if (stats.Damage().Bad() || stats.Exhaustion().Bad() || stats.Fatigue().Critical()) {
+               GetCreature().GetSteering().DontSeparate();
+       } else {
+               GetCreature().GetSteering().ResumeSeparate();
+       }
 }
 
 void BlobBackgroundTask::CheckSplit() {
        if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
                && GetCreature().OffspringChance() > Assets().random.UNorm()) {
-               std::cout << "[" << int(GetCreature().GetSimulation().Time())
-                       << "s] " << GetCreature().Name() << " split" << std::endl;
+               std::cout << "[" << ui::TimeString(GetCreature().GetSimulation().Time())
+                       << "] " << GetCreature().Name() << " split" << std::endl;
                Split(GetCreature());
                return;
        }
@@ -95,7 +111,7 @@ void BlobBackgroundTask::CheckMutate() {
        // check for random property mutation
        if (GetCreature().MutateChance() > Assets().random.UNorm()) {
                double amount = 1.0 + (Assets().random.SNorm() * 0.05);
-               math::Distribution &d = GetCreature().GetGenome().properties.props[(int(Assets().random.UNorm() * 8.0) % 8)];
+               math::Distribution &d = GetCreature().GetGenome().properties.props[Assets().random.UInt(9)];
                if (Assets().random.UNorm() < 0.5) {
                        d.Mean(d.Mean() * amount);
                } else {
@@ -204,7 +220,7 @@ void IngestGoal::Action() {
 }
 
 bool IngestGoal::OnSuitableTile() {
-       if (!GetSituation().OnSurface()) {
+       if (!GetSituation().OnTile()) {
                return false;
        }
        const world::TileType &t = GetSituation().GetTileType();
@@ -352,7 +368,7 @@ void LocateResourceGoal::Action() {
 }
 
 void LocateResourceGoal::LocateResource() {
-       if (GetSituation().OnSurface()) {
+       if (GetSituation().OnTile()) {
                const world::TileType &t = GetSituation().GetTileType();
                auto yield = t.FindBestResource(accept);
                if (yield != t.resources.cend()) {
@@ -382,15 +398,6 @@ void LocateResourceGoal::SearchVicinity() {
        glm::ivec2 begin(glm::max(glm::ivec2(0), loc - seek_radius));
        glm::ivec2 end(glm::min(glm::ivec2(planet.SideLength()), loc + seek_radius + glm::ivec2(1)));
 
-       // this happens when location is way off the planet
-       // that's a bug in Situation, actually, but I'm working aound that here
-       if (end.x <= begin.x) {
-               end.x = begin.x + 2;
-       }
-       if (end.y <= begin.y) {
-               end.y = begin.y + 2;
-       }
-
        double rating[end.y - begin.y][end.x - begin.x];
        std::memset(rating, 0, sizeof(double) * (end.y - begin.y) * (end.x - begin.x));