#include "evo/server/lifecycle/cellular_light.hpp" #include namespace evo::server { namespace { bool multiply(std::uint64_t& value, std::uint64_t factor) noexcept { if (factor != 0 && value > std::numeric_limits::max() / factor) return false; value *= factor; return true; } } // namespace std::optional cellular_light_capture(const biology::Genome& genome, std::uint32_t extent_um, const CellularLight& light, bool moving) noexcept { if (!genome.carries(biology::Capability::photosynthesis) || light.numerator == 0 || light.denominator == 0) return 0; const auto efficiency = genome.parameter(biology::Capability::photosynthesis); if (!efficiency) return std::nullopt; // Exact checked products before the single rounding; overflow is refused. std::uint64_t energy = extent_um; if (!multiply(energy, extent_um) || !multiply(energy, light.numerator) || !multiply(energy, *efficiency) || (moving && !multiply(energy, light.moving_per_thousand))) return std::nullopt; std::uint64_t divisor = light.denominator; if (!multiply(divisor, 1000) || (moving && !multiply(divisor, 1000))) return std::nullopt; return energy / divisor; } } // namespace evo::server