Skip to main content

unbanished_game/work/stock/
search.rs

1//! Finding a storage: one with room, scanning them all, and one holding a material.
2
3use super::*;
4
5/// Strict rank improvements, radius-list precedence, then storage. The covered-home flag is sticky
6/// after eligible material selection, even if its slot fails.
7///
8/// Original: `FUN_140093200`.
9#[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/// The markets (radius list), then the storages, each taking a strictly better rank than `best`.
35/// With `home`, a market whose radius covers the home ends the search after the markets; without
36/// it, every storage is weighed. Returns the best rank and its match.
37///
38/// Original: `FUN_140093200`; the scan without a home is `FUN_140093840`.
39#[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/// The storage nearest `cell` (entrance path rank) that holds `count` of `material` (`put` false)
107/// or can take them (`put` true). Ordinary storages, then markets; `exclude` is skipped. A
108/// StorageLocation must accept the material's flags. On a strict entrance-rank improvement the
109/// target point (slot scan, or a market use point) must exist with its own rank below the best, and
110/// the best becomes the entrance rank.
111///
112/// Original: `FUN_140092a60`.
113#[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}