#include "settlement/core/inventory_transaction.hpp" #include #include #include #include namespace settlement { // Replace the conditional in-memory batch when Void/Library #8 supplies this // contract: https://git.faumaray.com/Void/Library/issues/8 std::expected withdraw_reserved_materials( Storage& source, Inventory& destination, std::span materials, bool reserve_output_slot) noexcept { using Error = QuantityTransferError; if (source.id == 0 || source.kind > StorageKind::Pantry) return std::unexpected(Error::InvalidStorage); std::array amounts{}; bool selected = false; std::uint64_t withdrawn = 0; for (const auto& material : materials) { if (material.storage_id != source.id) continue; const auto type = static_cast(material.type); if (type == 0 || type > item_type_count || material.count == 0) { return std::unexpected(Error::InvalidMaterial); } const auto amount = Void::Numeric::checked_add(amounts[type - 1], material.count); const auto total = Void::Numeric::checked_add(withdrawn, std::uint64_t{material.count}); if (!amount || !total) return std::unexpected(Error::Overflow); amounts[type - 1] = *amount; withdrawn = *total; selected = true; } if (!selected) return std::unexpected(Error::MissingSelection); // Validate the entire source before indexing or changing any stack. Its // sorted unique representation bounds all subsequent work by item_type_count. auto previous = ItemType::None; std::uint64_t total_items = 0; std::array present{}; for (const auto& stack : source.items) { const auto type = static_cast(stack.type); if (type == 0 || type > item_type_count || stack.type <= previous || !source.accepts(stack.type) || stack.count == 0 || stack.reserved > stack.count) { return std::unexpected(Error::InvalidStock); } previous = stack.type; present[type - 1] = true; const auto total = Void::Numeric::checked_add(total_items, std::uint64_t{stack.count}); if (!total) return std::unexpected(Error::Overflow); total_items = *total; if (amounts[type - 1] > stack.reserved || amounts[type - 1] > stack.count) { return std::unexpected(Error::InsufficientStock); } } for (std::size_t i = 0; i < amounts.size(); ++i) { if (amounts[i] == 0) continue; if (!present[i]) return std::unexpected(Error::InsufficientStock); if (!Void::Numeric::checked_add(destination.item_count(static_cast(i + 1)), amounts[i])) { return std::unexpected(Error::Overflow); } } const auto occupied = Void::Numeric::checked_add(total_items, std::uint64_t{source.reserved_slots}); if (!occupied || source.capacity == 0 || *occupied > source.capacity) return std::unexpected(Error::Capacity); const auto slots = Void::Numeric::checked_add(source.reserved_slots, std::uint32_t{reserve_output_slot}); if (!slots) return std::unexpected(Error::Overflow); const auto remaining = Void::Numeric::checked_sub(total_items, withdrawn); if (!remaining) return std::unexpected(Error::InsufficientStock); const auto final_occupied = Void::Numeric::checked_add(*remaining, std::uint64_t{*slots}); if (!final_occupied || *final_occupied > source.capacity) return std::unexpected(Error::Capacity); // All operations after this point are nonthrowing. No stack iterator survives // compaction, which removes every depleted stack while retaining source order. static_assert(std::is_nothrow_move_assignable_v && std::is_nothrow_destructible_v); for (auto& stack : source.items) { const auto amount = amounts[static_cast(stack.type) - 1]; stack.count -= amount; stack.reserved -= amount; if (amount != 0) destination.add(stack.type, amount); } std::erase_if(source.items, [](const ItemStack& stack) noexcept { return stack.count == 0; }); source.reserved_slots = *slots; return {}; } } // namespace settlement