From: Daniel Karbach Date: Sat, 9 Dec 2017 22:33:01 +0000 (+0100) Subject: scale compatibility to same state resources only X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=00f55d5b55ff993d2516f00f8d635887562983c7;p=blobs.git scale compatibility to same state resources only --- diff --git a/src/creature/Composition.hpp b/src/creature/Composition.hpp index da66992..969407f 100644 --- a/src/creature/Composition.hpp +++ b/src/creature/Composition.hpp @@ -2,6 +2,7 @@ #define BLOBLS_CREATURE_COMPOSITION_HPP_ #include "../world/Set.hpp" +#include "../world/Resource.hpp" #include @@ -23,7 +24,7 @@ public: }; public: - Composition(); + explicit Composition(const world::Set &); ~Composition(); Composition(const Composition &) = default; @@ -37,8 +38,10 @@ public: bool Has(int res) const noexcept; double Get(int res) const noexcept; double Proportion(int res) const noexcept; - double Compatibility(const world::Set &, int res) const noexcept; + double StateProportion(int res) const noexcept; + double Compatibility(int res) const noexcept; double TotalMass() const noexcept { return total_mass; } + double StateMass(world::Resource::State s) const noexcept { return state_mass[s]; } public: std::vector::size_type size() const noexcept { return components.size(); } @@ -50,8 +53,10 @@ public: std::vector::const_iterator cend() noexcept { return components.cend(); } private: + const world::Set &resources; std::vector components; double total_mass; + double state_mass[4]; }; diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 536144b..72d0b3f 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -29,9 +29,11 @@ namespace blobs { namespace creature { -Composition::Composition() -: components() -, total_mass(0.0) { +Composition::Composition(const world::Set &resources) +: resources(resources) +, components() +, total_mass(0.0) +, state_mass{0.0} { } Composition::~Composition() { @@ -49,6 +51,7 @@ void Composition::Add(int res, double amount) { if (c->resource == res) { c->value += amount; if (c->value <= 0.0) { + amount += c->value; components.erase(c); } found = true; @@ -59,6 +62,7 @@ void Composition::Add(int res, double amount) { components.emplace_back(res, amount); } std::sort(components.begin(), components.end(), CompositionCompare); + state_mass[resources[res].state] += amount; total_mass += amount; } @@ -84,14 +88,18 @@ double Composition::Proportion(int res) const noexcept { return Get(res) / TotalMass(); } -double Composition::Compatibility(const world::Set &resources, int res) const noexcept { +double Composition::StateProportion(int res) const noexcept { + return Get(res) / StateMass(resources[res].state); +} + +double Composition::Compatibility(int res) const noexcept { if (Has(res)) { - return Proportion(res); + return StateProportion(res); } double max_compat = -1.0; double min_compat = 1.0; for (const auto &c : components) { - double prop = c.value / TotalMass(); + double prop = c.value / StateMass(resources[res].state); for (const auto &compat : resources[c.resource].compatibility) { double value = compat.second * prop; if (value > max_compat) { @@ -115,7 +123,7 @@ Creature::Creature(world::Simulation &sim) , name() , genome() , properties() -, composition() +, composition(sim.Resources()) , base_color(1.0) , highlight_color(0.0, 0.0, 0.0, 1.0) , mass(1.0) @@ -145,8 +153,8 @@ void Creature::AddMass(int res, double amount) { double nonsolid = 0.0; double volume = 0.0; for (const auto &c : composition) { - volume += c.value / sim.Assets().data.resources[c.resource].density; - if (sim.Assets().data.resources[c.resource].state != world::Resource::SOLID) { + volume += c.value / sim.Resources()[c.resource].density; + if (sim.Resources()[c.resource].state != world::Resource::SOLID) { nonsolid += c.value; } } @@ -162,10 +170,10 @@ void Creature::HighlightColor(const glm::dvec3 &c) noexcept { void Creature::Ingest(int res, double amount) noexcept { if (sim.Resources()[res].state == world::Resource::SOLID) { // 30% of solids stays in body - AddMass(res, amount * 0.3 * composition.Compatibility(sim.Resources(), res)); + AddMass(res, amount * 0.3 * composition.Compatibility(res)); } else { - // 10% of fluids stays in body - AddMass(res, amount * 0.1 * composition.Compatibility(sim.Resources(), res)); + // 5% of fluids stays in body + AddMass(res, amount * 0.05 * composition.Compatibility(res)); } math::GaloisLFSR &random = sim.Assets().random; if (random.UNorm() < AdaptChance()) { diff --git a/src/creature/goal.cpp b/src/creature/goal.cpp index 55fa6c9..65de6c0 100644 --- a/src/creature/goal.cpp +++ b/src/creature/goal.cpp @@ -260,7 +260,7 @@ std::string summarize(const Composition &comp, const app::Assets &assets) { IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat) : Goal(c) , stat(stat) -, accept() +, accept(Assets().data.resources) , locate_subtask(nullptr) , ingesting(false) , resource(-1) @@ -298,7 +298,7 @@ void IngestGoal::Tick(double dt) { } if (ingesting) { if (OnSuitableTile() && !GetSituation().Moving()) { - GetCreature().Ingest(resource, yield * GetCreature().GetComposition().Compatibility(Assets().data.resources, resource) * dt); + GetCreature().Ingest(resource, yield * GetCreature().GetComposition().Compatibility(resource) * dt); stat.Add(-1.0 * yield * dt); if (stat.Empty()) { SetComplete(); @@ -358,7 +358,7 @@ bool IngestGoal::OnSuitableTile() { LocateResourceGoal::LocateResourceGoal(Creature &c) : Goal(c) -, accept() +, accept(Assets().data.resources) , found(false) , target_pos(0.0) , searching(false) diff --git a/src/world/Resource.hpp b/src/world/Resource.hpp index 17df324..3798d70 100644 --- a/src/world/Resource.hpp +++ b/src/world/Resource.hpp @@ -29,7 +29,7 @@ struct Resource { }; // the resource's natural state // TODO: something about temperature and pressure and stuff - int state = SOLID; + State state = SOLID; glm::dvec3 base_color = glm::dvec3(1.0);