#include "evo/world/environment_units.hpp" #include #include #include #include namespace evo::world { std::expected measure_habitat(HabitatBox box) noexcept { const std::array lengths{box.width.value, box.height.value, box.depth.value}; const std::array origins{box.x.value, box.y.value, box.z.value}; for (std::size_t i = 0; i < lengths.size(); ++i) { if (lengths[i] == 0 || lengths[i] > habitat_axis_limit_um) return std::unexpected{EnvironmentUnitError::invalid_extent}; if (origins[i] > habitat_axis_limit_um - lengths[i]) return std::unexpected{EnvironmentUnitError::outside_voxel}; } // Validated factors <= 1e6: products <= 1e12 and 1e18 fit u64. const auto area = box.width.value * box.height.value; return HabitatMeasure{{area}, {area * box.depth.value}}; } std::expected mass_concentration(accounting::Femtograms mass, CubicMicrometres volume) noexcept { if (volume.value == 0 || volume.value > habitat_volume_limit_um3) return std::unexpected{EnvironmentUnitError::invalid_volume}; const auto divisor = std::gcd(mass.value, volume.value); return MassConcentration{mass.value / divisor, volume.value / divisor}; } std::expected water_saturation(accounting::Femtograms water, accounting::Femtograms capacity) noexcept { if (capacity.value == 0) return std::unexpected{EnvironmentUnitError::zero_capacity}; if (water.value > capacity.value) return std::unexpected{EnvironmentUnitError::excess_water}; const auto divisor = std::gcd(water.value, capacity.value); return WaterSaturation{water.value / divisor, capacity.value / divisor}; } std::expected integrate_energy(EnergyRate rate, std::uint64_t ticks, std::uint64_t remainder) noexcept { if (rate.ticks_denominator == 0) return std::unexpected{EnvironmentUnitError::invalid_rate}; if (remainder >= rate.ticks_denominator) return std::unexpected{EnvironmentUnitError::invalid_remainder}; const auto available = std::numeric_limits::max() - remainder; if (ticks != 0 && rate.picojoules_numerator > available / ticks) return std::unexpected{EnvironmentUnitError::overflow}; const auto numerator = rate.picojoules_numerator * ticks + remainder; return EnergyIntegral{{numerator / rate.ticks_denominator}, numerator % rate.ticks_denominator}; } } // namespace evo::world