+void Creature::AddMass(int res, double amount) {
+ composition.Add(res, amount);
+ double nonsolid = 0.0;
+ double volume = 0.0;
+ for (const auto &c : composition) {
+ volume += c.value / sim.Resources()[c.resource].density;
+ if (sim.Resources()[c.resource].state != world::Resource::SOLID) {
+ nonsolid += c.value;
+ }
+ }
+ Mass(composition.TotalMass());
+ Size(std::cbrt(volume));
+ highlight_color.a = nonsolid / composition.TotalMass();
+}
+
+void Creature::HighlightColor(const glm::dvec3 &c) noexcept {
+ highlight_color = glm::dvec4(c, highlight_color.a);
+}
+
+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(res));
+ } else {
+ // 10% of fluids stays in body
+ AddMass(res, amount * 0.1 * composition.Compatibility(res));
+ }
+ math::GaloisLFSR &random = sim.Assets().random;
+ if (random.UNorm() < AdaptChance()) {
+ // change color to be slightly more like resource
+ glm::dvec3 color(rgb2hsl(sim.Resources()[res].base_color));
+ // solids affect base color, others highlight
+ int p = sim.Resources()[res].state == world::Resource::SOLID ? 0 : 1;
+ int q = random.UInt(3); // hue, sat, or val
+ int r = random.UInt(2); // mean or deviation
+ math::Distribution *d = nullptr;
+ double ref = 0.0;
+ if (p == 0) {
+ if (q == 0) {
+ d = &genome.base_hue;
+ ref = color.x;
+ } else if (q == 1) {
+ d = &genome.base_saturation;
+ ref = color.y;
+ } else {
+ d = &genome.base_lightness;
+ ref = color.z;
+ }
+ } else {
+ if (q == 0) {
+ d = &genome.highlight_hue;
+ ref = color.x;
+ } else if (q == 1) {
+ d = &genome.highlight_saturation;
+ ref = color.y;
+ } else {
+ d = &genome.highlight_lightness;
+ ref = color.z;
+ }
+ }
+ if (r == 0) {
+ double diff = ref - d->Mean();
+ if (q == 0) {
+ if (diff < -0.5) {
+ diff += 1.0;
+ } else if (diff > 0.5) {
+ diff -= 1.0;
+ }
+ // move 0-15% of distance
+ d->Mean(std::fmod(d->Mean() + diff * random.UNorm() * 0.15, 1.0));
+ } else {
+ d->Mean(glm::clamp(d->Mean() + diff * random.UNorm() * 0.15, 0.0, 1.0));
+ }
+ } else {
+ // scale by ±15%, enforce bounds
+ d->StandardDeviation(glm::clamp(d->StandardDeviation() * (1.0 + random.SNorm() * 0.15), 0.0001, 0.5));
+ }
+ }
+ if (sim.Resources()[res].state == world::Resource::LIQUID && random.UNorm() < AdaptChance()) {
+ // change texture randomly
+ // TODO: make change depending on surroundings and/or resource
+ int p = random.UInt(2); // back or side
+ int q = random.UInt(2); // mean or deviation
+ math::Distribution &d = p ? genome.skin_side : genome.skin_back;
+ if (q == 0) {
+ // move ± one standard deviation
+ d.Mean(d.Mean() + (random.SNorm() * d.StandardDeviation()));
+ } else {
+ // scale by ±10%, enforce bounds
+ d.StandardDeviation(glm::clamp(d.StandardDeviation() * (1.0 + random.SNorm() * 0.1), 0.0001, 0.5));
+ }
+ }
+}
+
+void Creature::DoWork(double amount) noexcept {
+ stats.Exhaustion().Add(amount / (Stamina() + 1.0));
+ // burn resources proportional to composition
+ // factor = 1/total * 1/efficiency * amount * -1
+ double factor = -amount / (composition.TotalMass() * EnergyEfficiency());
+ // make a copy to total remains constant and
+ // no entries disappear during iteration
+ Composition comp(composition);
+ for (auto &cmp : comp) {
+ double value = cmp.value * factor * sim.Resources()[cmp.resource].inverse_energy;
+ AddMass(cmp.resource, value);
+ }
+ // doing work improves strength a little
+ properties.Strength() += amount * 0.0001;