Look up material stacks by tile in build searches

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

author
Marijn Besseling <njirambem@gmail.com> · 2026-10-24 21:35 UTC
commit
5b0b7f3e37dc6b9a3242234e4de528db3b642f24
parent
588a85c456
tree
browse at this commit

1 file changed +9 -13

src/jobs/mod.rs +9 -13

@@ -622,23 +622,19 @@ impl BuildContext<'_> {
622 622 }
623 623
624 624 fn material_source(&self, from: UVec2, (kind, amount): (ItemKind, u32)) -> Option<UVec2> {
625 - let free = |pos: UVec2| self.reservations.is_free(Target::Pickup(pos));
626 - let available: u32 = self
625 + let stacks: Vec<(u32, UVec2)> = self
627 626 .items
628 627 .iter()
629 - .filter(|(item, pos)| item.kind == kind && free(pos.0))
630 - .map(|(item, _)| item.count)
631 - .sum();
632 - if available < amount {
628 + .filter(|(item, pos)| {
629 + item.kind == kind && self.reservations.is_free(Target::Pickup(pos.0))
630 + })
631 + .map(|(item, pos)| (item.count, pos.0))
632 + .collect();
633 + if stacks.iter().map(|(count, _)| count).sum::<u32>() < amount {
633 634 return None;
634 635 }
635 - nearest_tile(self.map, from, SEARCH_TILES, |p| {
636 - free(p)
637 - && self
638 - .items
639 - .iter()
640 - .any(|(item, pos)| pos.0 == p && item.kind == kind)
641 - })
636 + let tiles: HashSet<UVec2> = stacks.into_iter().map(|(_, tile)| tile).collect();
637 + nearest_of(self.map, from, SEARCH_TILES, &tiles)
642 638 }
643 639
644 640 fn build_task(&self, from: UVec2, site: UVec2, kind: BuildKind) -> Option<Task> {