Track transfers, reservations and item owners through colony work
This commit is contained in:
1 parent
4f42488d54
commit
6c6f4e7857
4 files changed
+972
-72
No files matched your search
+177
-72
@@ -58,33 +58,33 @@ std::uint16_t initial_hunger(AgentId id) {
|
||||
}
|
||||
|
||||
struct Recipe {
|
||||
std::array<ItemStack, 2> ingredients;
|
||||
std::array<ItemAmount, 2> ingredients;
|
||||
ItemType output;
|
||||
std::uint32_t ticks;
|
||||
};
|
||||
|
||||
const Recipe& recipe_for(RecipeId id) {
|
||||
static constexpr Recipe saw{{ItemStack{ItemType::Wood, 1}, {}}, ItemType::Plank, 12};
|
||||
static constexpr Recipe cut{{ItemStack{ItemType::Stone, 1}, {}}, ItemType::StoneBlock, 16};
|
||||
static constexpr Recipe cabin{{ItemStack{ItemType::Plank, 3}, ItemStack{ItemType::StoneBlock, 2}},
|
||||
static constexpr Recipe saw{{ItemAmount{ItemType::Wood, 1}, {}}, ItemType::Plank, 12};
|
||||
static constexpr Recipe cut{{ItemAmount{ItemType::Stone, 1}, {}}, ItemType::StoneBlock, 16};
|
||||
static constexpr Recipe cabin{{ItemAmount{ItemType::Plank, 3}, ItemAmount{ItemType::StoneBlock, 2}},
|
||||
ItemType::None, 28};
|
||||
static constexpr Recipe bed{{ItemStack{ItemType::Plank, 1}, {}}, ItemType::None, 14};
|
||||
static constexpr Recipe weave{{ItemStack{ItemType::Flax, 1}, {}}, ItemType::Cloth, 12};
|
||||
static constexpr Recipe floor{{ItemStack{ItemType::Wood, 1}, {}}, ItemType::None, 6};
|
||||
static constexpr Recipe wall{{ItemStack{ItemType::Wood, 1}, {}}, ItemType::None, 9};
|
||||
static constexpr Recipe door{{ItemStack{ItemType::Wood, 2}, {}}, ItemType::None, 12};
|
||||
static constexpr Recipe cabinet{{ItemStack{ItemType::Wood, 1}, {}}, ItemType::None, 10};
|
||||
static constexpr Recipe mining_rack{{ItemStack{ItemType::Wood, 1}, ItemStack{ItemType::Stone, 1}},
|
||||
static constexpr Recipe bed{{ItemAmount{ItemType::Plank, 1}, {}}, ItemType::None, 14};
|
||||
static constexpr Recipe weave{{ItemAmount{ItemType::Flax, 1}, {}}, ItemType::Cloth, 12};
|
||||
static constexpr Recipe floor{{ItemAmount{ItemType::Wood, 1}, {}}, ItemType::None, 6};
|
||||
static constexpr Recipe wall{{ItemAmount{ItemType::Wood, 1}, {}}, ItemType::None, 9};
|
||||
static constexpr Recipe door{{ItemAmount{ItemType::Wood, 2}, {}}, ItemType::None, 12};
|
||||
static constexpr Recipe cabinet{{ItemAmount{ItemType::Wood, 1}, {}}, ItemType::None, 10};
|
||||
static constexpr Recipe mining_rack{{ItemAmount{ItemType::Wood, 1}, ItemAmount{ItemType::Stone, 1}},
|
||||
ItemType::None, 10};
|
||||
static constexpr Recipe loom{{ItemStack{ItemType::Wood, 2}, {}}, ItemType::None, 12};
|
||||
static constexpr Recipe install_door{{ItemStack{ItemType::WoodenDoor, 1}, {}}, ItemType::None, 8};
|
||||
static constexpr Recipe craft_door{{ItemStack{ItemType::Wood, 2}, {}}, ItemType::WoodenDoor, 14};
|
||||
static constexpr Recipe loom{{ItemAmount{ItemType::Wood, 2}, {}}, ItemType::None, 12};
|
||||
static constexpr Recipe install_door{{ItemAmount{ItemType::WoodenDoor, 1}, {}}, ItemType::None, 8};
|
||||
static constexpr Recipe craft_door{{ItemAmount{ItemType::Wood, 2}, {}}, ItemType::WoodenDoor, 14};
|
||||
static constexpr Recipe craft_cabinet{cabinet.ingredients, ItemType::MedicineCabinet, 14};
|
||||
static constexpr Recipe craft_rack{mining_rack.ingredients, ItemType::MiningRack, 14};
|
||||
static constexpr Recipe craft_loom{loom.ingredients, ItemType::Loom, 16};
|
||||
static constexpr Recipe install_cabinet{{ItemStack{ItemType::MedicineCabinet, 1}, {}}, ItemType::None, 8};
|
||||
static constexpr Recipe install_rack{{ItemStack{ItemType::MiningRack, 1}, {}}, ItemType::None, 8};
|
||||
static constexpr Recipe install_loom{{ItemStack{ItemType::Loom, 1}, {}}, ItemType::None, 8};
|
||||
static constexpr Recipe install_cabinet{{ItemAmount{ItemType::MedicineCabinet, 1}, {}}, ItemType::None, 8};
|
||||
static constexpr Recipe install_rack{{ItemAmount{ItemType::MiningRack, 1}, {}}, ItemType::None, 8};
|
||||
static constexpr Recipe install_loom{{ItemAmount{ItemType::Loom, 1}, {}}, ItemType::None, 8};
|
||||
switch (id) {
|
||||
case RecipeId::SawPlank: return saw;
|
||||
case RecipeId::CutStoneBlock: return cut;
|
||||
@@ -276,6 +276,7 @@ void Inventory::remove(ItemType type, std::uint32_t count) noexcept {
|
||||
const auto index = static_cast<std::size_t>(type);
|
||||
assert(index != 0 && index <= counts_.size() && count <= counts_[index - 1]);
|
||||
counts_[index - 1] -= count;
|
||||
if (counts_[index - 1] == 0) stack_ids_[index - 1] = 0;
|
||||
}
|
||||
|
||||
ItemType Inventory::first_item() const noexcept {
|
||||
@@ -497,8 +498,20 @@ void World::stock_storage(std::uint64_t id, ItemType type, std::uint32_t count)
|
||||
}
|
||||
if (count == 0) return;
|
||||
auto stack = stack_position(*storage, type);
|
||||
if (stack != storage->items.end() && stack->type == type) stack->count += count;
|
||||
else storage->items.insert(stack, ItemStack{type, count});
|
||||
auto& balance = item_balances_[static_cast<std::size_t>(type) - 1];
|
||||
const auto grant = Void::Numeric::checked_add(balance.grant, std::uint64_t{count});
|
||||
if (!grant) throw std::overflow_error("stock grant balance exhausted");
|
||||
if (stack != storage->items.end() && stack->type == type) {
|
||||
const auto amount = Void::Numeric::checked_add(stack->count, count);
|
||||
if (!amount) throw std::overflow_error("stock quantity exhausted");
|
||||
stack->count = *amount;
|
||||
} else {
|
||||
const auto next = Void::Numeric::checked_add(next_stack_id_, std::uint64_t{1});
|
||||
if (next_stack_id_ == 0 || !next) throw std::overflow_error("stack identities exhausted");
|
||||
storage->items.insert(stack, ItemStack{type, count, 0, next_stack_id_});
|
||||
next_stack_id_ = *next;
|
||||
}
|
||||
balance.grant = *grant;
|
||||
}
|
||||
|
||||
void World::enable_hunger() noexcept {
|
||||
@@ -565,12 +578,12 @@ bool World::can_place_room(const RoomPlan& plan) const noexcept {
|
||||
});
|
||||
}
|
||||
|
||||
std::array<ItemStack, 2> World::construction_materials(RecipeId recipe, BedBase base) const {
|
||||
std::array<ItemAmount, 2> World::construction_materials(RecipeId recipe, BedBase base) const {
|
||||
if (recipe == RecipeId::BuildBed && staged_beds_) {
|
||||
return {ItemStack{base == BedBase::Stone ? ItemType::Stone : ItemType::Wood, 1},
|
||||
ItemStack{ItemType::Cloth, 2}};
|
||||
return {ItemAmount{base == BedBase::Stone ? ItemType::Stone : ItemType::Wood, 1},
|
||||
ItemAmount{ItemType::Cloth, 2}};
|
||||
}
|
||||
return is_construction(recipe) ? recipe_for(recipe).ingredients : std::array<ItemStack, 2>{};
|
||||
return is_construction(recipe) ? recipe_for(recipe).ingredients : std::array<ItemAmount, 2>{};
|
||||
}
|
||||
|
||||
RecipeId World::room_construction_recipe(RecipeId part) const noexcept {
|
||||
@@ -584,8 +597,8 @@ RecipeId World::room_construction_recipe(RecipeId part) const noexcept {
|
||||
return part;
|
||||
}
|
||||
|
||||
std::array<ItemStack, 2> World::carpentry_materials(RecipeId recipe) const {
|
||||
return is_carpentry_recipe(recipe) ? recipe_for(recipe).ingredients : std::array<ItemStack, 2>{};
|
||||
std::array<ItemAmount, 2> World::carpentry_materials(RecipeId recipe) const {
|
||||
return is_carpentry_recipe(recipe) ? recipe_for(recipe).ingredients : std::array<ItemAmount, 2>{};
|
||||
}
|
||||
|
||||
std::size_t World::missing_carpentry_orders(ItemType product, std::size_t new_installations,
|
||||
@@ -746,6 +759,14 @@ CommandStatus World::edit_object(GridPosition position, const GridPosition* dest
|
||||
// Demand is evaluated against the post-removal inventory, without
|
||||
// losing stock or issuing any order until the entire batch fits.
|
||||
if (!reserve_production(total)) return CommandStatus::Busy;
|
||||
auto balances = item_balances_;
|
||||
for (const auto& stack : storage->items) {
|
||||
auto& sink = balances[static_cast<std::size_t>(stack.type) - 1].demolition;
|
||||
const auto amount = Void::Numeric::checked_add(sink, std::uint64_t{stack.count});
|
||||
if (!amount) return CommandStatus::Busy;
|
||||
sink = *amount;
|
||||
}
|
||||
item_balances_ = balances;
|
||||
storages_.erase(storage);
|
||||
for (std::size_t i = 0; i < replacements.size(); ++i) {
|
||||
for (std::size_t j = 0; j < replacements[i]; ++j) {
|
||||
@@ -1305,6 +1326,7 @@ void World::assign_resource_tasks(ResourceFocus focus) {
|
||||
}
|
||||
|
||||
if (node.needs_tending()) {
|
||||
if (!admit_custody(agent, TransferKind::Tending, 0, node.id, 0, {})) break;
|
||||
++resource_nodes_[node_index].reserved_workers;
|
||||
--untended_sites;
|
||||
agent.resource_node_id = node.id;
|
||||
@@ -1341,8 +1363,9 @@ void World::assign_resource_tasks(ResourceFocus focus) {
|
||||
continue;
|
||||
}
|
||||
|
||||
++resource_nodes_[node_index].reserved_workers;
|
||||
Storage& storage = storages_[storage_index];
|
||||
if (!admit_custody(agent, TransferKind::Harvest, 0, node.id, storage.id, {})) break;
|
||||
++resource_nodes_[node_index].reserved_workers;
|
||||
++storage.reserved_slots;
|
||||
if (!has_space(storage)) {
|
||||
if (storage.kind == StorageKind::Pantry) --open_pantry_count;
|
||||
@@ -1369,7 +1392,7 @@ void World::assign_production_tasks(bool weaving) {
|
||||
})) return;
|
||||
for (std::size_t index = 0; index < production_orders_.size(); ++index) {
|
||||
ProductionOrder& order = production_orders_[index];
|
||||
if (order.state != JobState::Available || (order.recipe == RecipeId::WeaveCloth) != weaving) continue;
|
||||
if (order.suspended || order.state != JobState::Available || (order.recipe == RecipeId::WeaveCloth) != weaving) continue;
|
||||
|
||||
const bool site_busy = std::any_of(production_orders_.begin(), production_orders_.end(),
|
||||
[&order](const ProductionOrder& other) {
|
||||
@@ -1387,7 +1410,7 @@ void World::assign_production_tasks(bool weaving) {
|
||||
std::vector<Access> storage_access(deliver_ingredients || weaving ? storages_.size() : 0, Access::Unknown);
|
||||
std::vector<MaterialReservation> materials;
|
||||
bool supplied = true;
|
||||
for (const ItemStack& ingredient : recipe.ingredients) {
|
||||
for (const ItemAmount& ingredient : recipe.ingredients) {
|
||||
std::uint32_t remaining = ingredient.count;
|
||||
for (std::size_t storage_index = 0; storage_index < storages_.size() && remaining > 0;
|
||||
++storage_index) {
|
||||
@@ -1438,10 +1461,13 @@ void World::assign_production_tasks(bool weaving) {
|
||||
if (agent.state != AgentState::Idle || !eligible) continue;
|
||||
auto path = assignment_path(agent.position, destination);
|
||||
if (!path.reachable) continue;
|
||||
std::vector<GridPosition> haul_route;
|
||||
if (carpentry) {
|
||||
agent.haul_route = assignment_path(order.position, storage_by_id(materials.front().storage_id).position).positions;
|
||||
haul_route = assignment_path(order.position, storage_by_id(materials.front().storage_id).position).positions;
|
||||
}
|
||||
|
||||
const auto output_storage = recipe.output == ItemType::None ? 0 : materials.front().storage_id;
|
||||
if (!admit_custody(agent, TransferKind::Production, order.id, 0, output_storage, materials)) break;
|
||||
if (carpentry) agent.haul_route = std::move(haul_route);
|
||||
for (const auto& material : materials) {
|
||||
stack_position(storage_by_id(material.storage_id), material.type)->reserved += material.count;
|
||||
}
|
||||
@@ -1459,19 +1485,6 @@ void World::assign_production_tasks(bool weaving) {
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
void take_reserved_material(Storage& storage, const MaterialReservation& material) {
|
||||
auto stack = stack_position(storage, material.type);
|
||||
assert(stack != storage.items.end() && stack->type == material.type);
|
||||
assert(stack->reserved >= material.count && stack->count >= stack->reserved);
|
||||
stack->reserved -= material.count;
|
||||
stack->count -= material.count;
|
||||
if (stack->count == 0) storage.items.erase(stack);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void World::assign_meals() {
|
||||
for (Agent& agent : agents_) {
|
||||
if (agent.hunger < max_hunger) ++agent.hunger;
|
||||
@@ -1497,6 +1510,8 @@ void World::assign_meals() {
|
||||
auto path = assignment_path(agent.position, storage.position);
|
||||
if (!path.reachable) continue;
|
||||
|
||||
const std::vector meal{MaterialReservation{storage.id, ItemType::Berries, 1}};
|
||||
if (!admit_custody(agent, TransferKind::Meal, 0, 0, 0, meal)) break;
|
||||
auto stack = stack_position(storage, ItemType::Berries);
|
||||
++stack->reserved;
|
||||
agent.storage_id = storage.id;
|
||||
@@ -1512,9 +1527,10 @@ void World::assign_meals() {
|
||||
void World::finish_meal_step(Agent& agent) {
|
||||
if (agent.state == AgentState::GoingToEat) {
|
||||
if (agent.route_cursor != agent.route.size()) return;
|
||||
const std::array meal{MaterialReservation{agent.storage_id, ItemType::Berries, 1}};
|
||||
if (!withdraw_reserved_materials(storage_by_id(agent.storage_id), agent.inventory, meal)) {
|
||||
throw std::logic_error("invalid reserved meal pickup");
|
||||
const std::vector meal{MaterialReservation{agent.storage_id, ItemType::Berries, 1}};
|
||||
if (!pickup_custody(agent, storage_by_id(agent.storage_id), meal, false)) {
|
||||
if (agent.execution->blocked == CustodyStatus::Invalid) throw std::logic_error("invalid reserved meal pickup");
|
||||
return;
|
||||
}
|
||||
agent.storage_id = 0;
|
||||
agent.route.clear();
|
||||
@@ -1523,8 +1539,19 @@ void World::finish_meal_step(Agent& agent) {
|
||||
agent.state = AgentState::Eating;
|
||||
return;
|
||||
}
|
||||
if (--agent.work_remaining != 0) return;
|
||||
if (agent.work_remaining > 0) --agent.work_remaining;
|
||||
if (agent.work_remaining != 0) return;
|
||||
auto& sink = item_balances_[static_cast<std::size_t>(ItemType::Berries) - 1].meal;
|
||||
const auto amount = Void::Numeric::checked_add(sink, std::uint64_t{1});
|
||||
const auto meals = Void::Numeric::checked_add(meals_eaten_, std::uint64_t{1});
|
||||
auto receipt = prepare_custody(agent, CustodyOperation::Eat,
|
||||
amount && meals ? CustodyStatus::Accepted : CustodyStatus::Overflow);
|
||||
if (!receipt) return;
|
||||
commit_custody(agent, receipt);
|
||||
if (!amount || !meals) return;
|
||||
sink = *amount;
|
||||
agent.inventory.remove(ItemType::Berries);
|
||||
finish_custody(agent);
|
||||
agent.hunger = 0;
|
||||
agent.state = AgentState::Idle;
|
||||
++meals_eaten_;
|
||||
@@ -1585,9 +1612,7 @@ void World::finish_rest_step(Agent& agent) {
|
||||
void World::advance_construction_delivery(Agent& agent) {
|
||||
ProductionOrder& order = production_by_id(agent.production_id);
|
||||
if (order.collected_stops == order.delivery_stops.size()) {
|
||||
for (const auto& material : order.materials) {
|
||||
agent.inventory.remove(material.type, material.count);
|
||||
}
|
||||
if (!deliver_custody(agent, order.input, {OwnerKind::Production, order.id})) return;
|
||||
order.delivery_stops.clear();
|
||||
order.collected_stops = 0;
|
||||
agent.state = AgentState::Working;
|
||||
@@ -1602,8 +1627,9 @@ void World::advance_construction_delivery(Agent& agent) {
|
||||
auto& stop = order.delivery_stops[order.collected_stops];
|
||||
const bool reserve_output = is_carpentry_recipe(order.recipe) &&
|
||||
stop.storage_id == order.materials.front().storage_id;
|
||||
if (!withdraw_reserved_materials(storage_by_id(stop.storage_id), agent.inventory, order.materials, reserve_output)) {
|
||||
throw std::logic_error("invalid reserved construction pickup");
|
||||
if (!pickup_custody(agent, storage_by_id(stop.storage_id), order.materials, reserve_output)) {
|
||||
if (agent.execution->blocked == CustodyStatus::Invalid) throw std::logic_error("invalid reserved construction pickup");
|
||||
return;
|
||||
}
|
||||
++order.collected_stops;
|
||||
agent.route = std::move(stop.route_to_next);
|
||||
@@ -1614,26 +1640,83 @@ void World::advance_construction_delivery(Agent& agent) {
|
||||
void World::finish_production(Agent& agent) {
|
||||
ProductionOrder& order = production_by_id(agent.production_id);
|
||||
const Recipe recipe = recipe_for(order);
|
||||
assert(!order.materials.empty());
|
||||
if (is_carpentry_recipe(order.recipe)) {
|
||||
agent.inventory.add(recipe.output);
|
||||
auto balances = item_balances_;
|
||||
auto input = order.input;
|
||||
auto inventory = agent.inventory;
|
||||
auto next = next_stack_id_;
|
||||
std::vector<Storage> stores;
|
||||
const bool delivered = delivers_materials(order.recipe);
|
||||
const bool carpentry = is_carpentry_recipe(order.recipe);
|
||||
auto status = CustodyStatus::Accepted;
|
||||
for (const auto& material : order.materials) {
|
||||
auto& balance = balances[static_cast<std::size_t>(material.type) - 1];
|
||||
auto& sink = recipe.output == ItemType::None ? balance.installation : balance.recipe_input;
|
||||
const auto amount = Void::Numeric::checked_add(sink, std::uint64_t{material.count});
|
||||
if (!amount) { status = CustodyStatus::Overflow; break; }
|
||||
sink = *amount;
|
||||
if (delivered) {
|
||||
if (input.item_count(material.type) < material.count) { status = CustodyStatus::Invalid; break; }
|
||||
input.remove(material.type, material.count);
|
||||
} else {
|
||||
auto store = std::find_if(stores.begin(), stores.end(), [&](const auto& s) { return s.id == material.storage_id; });
|
||||
if (store == stores.end()) { stores.push_back(storage_by_id(material.storage_id)); store = std::prev(stores.end()); }
|
||||
const auto stack = stack_position(*store, material.type);
|
||||
if (stack == store->items.end() || stack->type != material.type || stack->reserved < material.count || stack->count < material.count) {
|
||||
status = CustodyStatus::Invalid; break;
|
||||
}
|
||||
stack->count -= material.count;
|
||||
stack->reserved -= material.count;
|
||||
if (stack->count == 0) store->items.erase(stack);
|
||||
}
|
||||
}
|
||||
StackId output_id = 0;
|
||||
if (status == CustodyStatus::Accepted && recipe.output != ItemType::None) {
|
||||
auto& balance = balances[static_cast<std::size_t>(recipe.output) - 1].recipe_output;
|
||||
const auto amount = Void::Numeric::checked_add(balance, std::uint64_t{1});
|
||||
if (!amount) status = CustodyStatus::Overflow;
|
||||
else balance = *amount;
|
||||
if (carpentry) {
|
||||
if (next == 0 || next == std::numeric_limits<StackId>::max()) status = CustodyStatus::Exhausted;
|
||||
else { output_id = next++; inventory.add(recipe.output); inventory.set_stack_id(recipe.output, output_id); }
|
||||
} else {
|
||||
auto& store = stores.front();
|
||||
auto stack = stack_position(store, recipe.output);
|
||||
if (stack != store.items.end() && stack->type == recipe.output) { ++stack->count; output_id = stack->id; }
|
||||
else if (next == 0 || next == std::numeric_limits<StackId>::max()) status = CustodyStatus::Exhausted;
|
||||
else { output_id = next++; store.items.insert(stack, {recipe.output, 1, 0, output_id}); }
|
||||
}
|
||||
}
|
||||
auto receipt = std::const_pointer_cast<CustodyReceipt>(prepare_custody(agent, CustodyOperation::Complete, status));
|
||||
if (!receipt) return;
|
||||
if (status == CustodyStatus::Accepted) {
|
||||
receipt->result.terminal = !carpentry;
|
||||
if (output_id) {
|
||||
receipt->result.effects[0] = {carpentry ? StockOwner{OwnerKind::Resident, agent.id} :
|
||||
StockOwner{OwnerKind::Storage, stores.front().id}, output_id, recipe.output,
|
||||
carpentry ? inventory.item_count(recipe.output) : stores.front().item_count(recipe.output)};
|
||||
receipt->result.effect_count = 1;
|
||||
}
|
||||
}
|
||||
commit_custody(agent, receipt);
|
||||
if (status != CustodyStatus::Accepted) return;
|
||||
for (auto& store : stores) storage_by_id(store.id).items.swap(store.items);
|
||||
item_balances_ = balances;
|
||||
next_stack_id_ = next;
|
||||
order.input = input;
|
||||
agent.inventory = inventory;
|
||||
for (auto& claim : agent.execution->reservations) if (claim.kind != ReservationKind::Capacity) claim = {};
|
||||
if (carpentry) {
|
||||
agent.storage_id = order.materials.front().storage_id;
|
||||
agent.route = std::move(agent.haul_route);
|
||||
agent.haul_route.clear();
|
||||
agent.route_cursor = 0;
|
||||
agent.production_id = 0;
|
||||
agent.execution->production = 0;
|
||||
agent.state = AgentState::Hauling;
|
||||
order.materials.clear();
|
||||
order.state = JobState::Completed;
|
||||
return;
|
||||
}
|
||||
if (recipe.output != ItemType::None) {
|
||||
for (const auto& material : order.materials) {
|
||||
take_reserved_material(storage_by_id(material.storage_id), material);
|
||||
}
|
||||
// Crafting replaces one raw item with one finished item in the same store.
|
||||
storage_by_id(order.materials.front().storage_id).add_item(recipe.output);
|
||||
}
|
||||
order.materials.clear();
|
||||
if (order.recipe == RecipeId::BuildBed &&
|
||||
(order.bed_stage == BedStage::Foundation || order.bed_stage == BedStage::Bedding)) {
|
||||
@@ -1644,13 +1727,12 @@ void World::finish_production(Agent& agent) {
|
||||
order.state = JobState::Available;
|
||||
} else if (order.recipe == RecipeId::WeaveCloth) {
|
||||
order.state = JobState::Available;
|
||||
} else {
|
||||
order.state = JobState::Completed;
|
||||
}
|
||||
} else order.state = JobState::Completed;
|
||||
agent.production_id = 0;
|
||||
agent.state = AgentState::Idle;
|
||||
agent.route.clear();
|
||||
agent.route_cursor = 0;
|
||||
finish_custody(agent);
|
||||
}
|
||||
|
||||
void World::grow_sites() {
|
||||
@@ -1707,7 +1789,7 @@ void World::step(TickProfile* profile) {
|
||||
Agent& agent = agents_[index];
|
||||
if ((agent.state == AgentState::Travelling || agent.state == AgentState::Hauling ||
|
||||
agent.state == AgentState::FetchingMaterials || agent.state == AgentState::GoingToEat ||
|
||||
agent.state == AgentState::GoingToSleep) &&
|
||||
agent.state == AgentState::GoingToSleep || agent.state == AgentState::Returning) &&
|
||||
agent.route_cursor < agent.route.size()) {
|
||||
agent.position = agent.route[agent.route_cursor++];
|
||||
if (agent.state == AgentState::Travelling && agent.route_cursor == agent.route.size()) {
|
||||
@@ -1762,6 +1844,7 @@ void World::step(TickProfile* profile) {
|
||||
}
|
||||
|
||||
for (Agent& agent : agents_) {
|
||||
if (agent.state == AgentState::Returning) { advance_return(agent); continue; }
|
||||
if (agent.state == AgentState::GoingToEat || agent.state == AgentState::Eating) {
|
||||
finish_meal_step(agent);
|
||||
continue;
|
||||
@@ -1783,7 +1866,13 @@ void World::step(TickProfile* profile) {
|
||||
if (agent.work_remaining == 0 && agent.resource_node_id != 0) {
|
||||
ResourceNode& node = resource_node_by_id(agent.resource_node_id);
|
||||
if (agent.storage_id == 0) {
|
||||
node.growth_at = tick_ + growing_time(node.site);
|
||||
const auto growth = Void::Numeric::checked_add(tick_, growing_time(node.site));
|
||||
auto receipt = prepare_custody(agent, CustodyOperation::Tend, growth ? CustodyStatus::Accepted : CustodyStatus::Overflow);
|
||||
if (!receipt) continue;
|
||||
commit_custody(agent, receipt);
|
||||
if (!growth) continue;
|
||||
node.growth_at = *growth;
|
||||
finish_custody(agent);
|
||||
--node.reserved_workers;
|
||||
agent.resource_node_id = 0;
|
||||
agent.state = AgentState::Idle;
|
||||
@@ -1791,9 +1880,26 @@ void World::step(TickProfile* profile) {
|
||||
agent.route_cursor = 0;
|
||||
continue;
|
||||
}
|
||||
if (node.quantity > 0) --node.quantity;
|
||||
if (node.reserved_workers > 0) --node.reserved_workers;
|
||||
auto& balance = item_balances_[static_cast<std::size_t>(node.type) - 1].harvest;
|
||||
const auto amount = Void::Numeric::checked_add(balance, std::uint64_t{1});
|
||||
const auto next = Void::Numeric::checked_add(next_stack_id_, std::uint64_t{1});
|
||||
auto receipt = std::const_pointer_cast<CustodyReceipt>(prepare_custody(agent, CustodyOperation::Harvest,
|
||||
!next ? CustodyStatus::Exhausted : !amount ? CustodyStatus::Overflow : CustodyStatus::Accepted));
|
||||
if (!receipt) continue;
|
||||
if (next && amount) {
|
||||
receipt->result.effects[0] = {{OwnerKind::Resident, agent.id}, next_stack_id_, node.type, 1};
|
||||
receipt->result.effect_count = 1;
|
||||
}
|
||||
commit_custody(agent, receipt);
|
||||
if (!next || !amount) continue;
|
||||
--node.quantity;
|
||||
--node.reserved_workers;
|
||||
balance = *amount;
|
||||
agent.inventory.add(node.type);
|
||||
agent.inventory.set_stack_id(node.type, next_stack_id_);
|
||||
next_stack_id_ = *next;
|
||||
for (auto& claim : agent.execution->reservations) if (claim.kind == ReservationKind::Harvest) claim = {};
|
||||
agent.execution->source = 0;
|
||||
agent.resource_node_id = 0;
|
||||
agent.route = std::move(agent.haul_route);
|
||||
agent.haul_route.clear();
|
||||
@@ -1806,9 +1912,8 @@ void World::step(TickProfile* profile) {
|
||||
agent.storage_id != 0) {
|
||||
Storage& storage = storage_by_id(agent.storage_id);
|
||||
const ItemType cargo = agent.inventory.first_item();
|
||||
if (storage.reserved_slots > 0) --storage.reserved_slots;
|
||||
storage.add_item(cargo);
|
||||
agent.inventory.remove(cargo);
|
||||
if (!deposit_custody(agent, storage, cargo, 1)) continue;
|
||||
finish_custody(agent);
|
||||
agent.storage_id = 0;
|
||||
agent.state = AgentState::Idle;
|
||||
agent.route.clear();
|
||||
|
||||
Reference in new issue
Block a user