unbanished_game/work/stock/
search.rs1use super::*;
4
5#[allow(clippy::too_many_arguments)]
10pub fn find_storage(
11 sites: &[StorageSite],
12 storages: &[Storage],
13 materials: &[RawMaterial],
14 category: u32,
15 cell: i32,
16 home: i32,
17 map: &mut impl StorageSearchMap,
18 rng: &mut Random,
19) -> Option<StorageMatch> {
20 let (_, found) = scan_storages(
21 sites,
22 storages,
23 materials,
24 category,
25 cell,
26 Some(home),
27 i32::MAX,
28 map,
29 rng,
30 );
31 found
32}
33
34#[allow(clippy::too_many_arguments)]
40pub fn scan_storages(
41 sites: &[StorageSite],
42 storages: &[Storage],
43 materials: &[RawMaterial],
44 category: u32,
45 cell: i32,
46 home: Option<i32>,
47 best: i32,
48 map: &mut impl StorageSearchMap,
49 rng: &mut Random,
50) -> (i32, Option<StorageMatch>) {
51 let mut best = best;
52 let mut found = None;
53 let mut covered = false;
54 for (site, s) in sites.iter().enumerate().filter(|(_, s)| s.radius.is_some()) {
55 let entrance = map.entrance(s.entity, rng);
56 let in_radius = home.is_some_and(|h| h >= 0 && map.rank(entrance, h) <= s.radius.unwrap());
57 if map.rank(entrance, cell) < best {
58 if let Some(material) = storages[s.storage].find_item(materials, category, rng) {
59 covered |= in_radius;
60 if let Some((rank, target)) = map.market_position(s.entity, cell, rng) {
61 if rank >= 0 && rank < best {
62 best = rank;
63 found = Some(StorageMatch {
64 site,
65 storage: s.storage,
66 material,
67 target,
68 });
69 }
70 }
71 }
72 }
73 }
74 if covered && found.is_some() {
75 return (best, found);
76 }
77 for (site, s) in sites.iter().enumerate().filter(|(_, s)| s.radius.is_none()) {
78 let entrance = map.entrance(s.entity, rng);
79 if map.rank(entrance, cell) < best {
80 let inventory = &storages[s.storage];
81 if let Some(material) = inventory.find_item(materials, category, rng) {
82 let flags = if s.location_flags == 0 {
83 inventory.accepts
84 } else {
85 s.location_flags
86 };
87 if materials[material].flags & flags != 0 {
88 if let Some((rank, target)) = map.storage_position(s.entity, cell) {
89 if rank >= 0 && rank < best {
90 best = rank;
91 found = Some(StorageMatch {
92 site,
93 storage: s.storage,
94 material,
95 target,
96 });
97 }
98 }
99 }
100 }
101 }
102 }
103 (best, found)
104}
105
106#[allow(clippy::too_many_arguments)]
114pub fn find_material_storage(
115 sites: &[StorageSite],
116 storages: &[Storage],
117 materials: &[RawMaterial],
118 material: usize,
119 count: i32,
120 put: bool,
121 cell: i32,
122 exclude: Option<usize>,
123 map: &mut impl StorageSearchMap,
124 rng: &mut Random,
125) -> Option<StorageMatch> {
126 let mut best = i32::MAX;
127 let mut found = None;
128 for market in [false, true] {
129 for (site, s) in sites.iter().enumerate() {
130 if s.radius.is_some() != market || Some(s.entity) == exclude {
131 continue;
132 }
133 let inventory = &storages[s.storage];
134 if !market {
135 let flags = if s.location_flags == 0 {
136 inventory.accepts
137 } else {
138 s.location_flags
139 };
140 if materials[material].flags & flags == 0 {
141 continue;
142 }
143 }
144 let ok = if put {
145 inventory.fits(materials, material, count)
146 } else {
147 inventory.available(material).wrapping_sub(count) >= 0
148 };
149 if !ok {
150 continue;
151 }
152 let entrance = map.entrance(s.entity, rng);
153 let rank = map.rank(entrance, cell);
154 if rank < best {
155 let point = if market {
156 map.market_position(s.entity, cell, rng)
157 } else {
158 map.storage_position(s.entity, cell)
159 };
160 if let Some((r, target)) = point {
161 if r >= 0 && r < best {
162 best = rank;
163 found = Some(StorageMatch {
164 site,
165 storage: s.storage,
166 material,
167 target,
168 });
169 }
170 }
171 }
172 }
173 }
174 found
175}