diff options
author | metamuffin <metamuffin@disroot.org> | 2025-10-06 23:03:32 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-10-06 23:03:40 +0200 |
commit | 176e6bc6c4c29bea3be2aceca99743b997c76c97 (patch) | |
tree | 1161e7a966843324756340da4b6452492902fa07 /server/src/data/demands.rs | |
parent | ea86b11b682500160f37b35ea8f06b081cd05036 (diff) | |
download | hurrycurry-176e6bc6c4c29bea3be2aceca99743b997c76c97.tar hurrycurry-176e6bc6c4c29bea3be2aceca99743b997c76c97.tar.bz2 hurrycurry-176e6bc6c4c29bea3be2aceca99743b997c76c97.tar.zst |
Move data code to own crate + general data refactor
Diffstat (limited to 'server/src/data/demands.rs')
-rw-r--r-- | server/src/data/demands.rs | 88 |
1 files changed, 0 insertions, 88 deletions
diff --git a/server/src/data/demands.rs b/server/src/data/demands.rs deleted file mode 100644 index 77e187af..00000000 --- a/server/src/data/demands.rs +++ /dev/null @@ -1,88 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright (C) 2025 Hurry Curry! Contributors - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, version 3 of the License only. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <https://www.gnu.org/licenses/>. - -*/ -use hurrycurry_protocol::{Demand, ItemIndex, Recipe, TileIndex}; -use std::collections::{HashMap, HashSet}; - -pub fn generate_demands( - tiles: &HashSet<TileIndex>, - items: &HashSet<ItemIndex>, - raw_demands: &[(ItemIndex, Option<ItemIndex>, f32)], - recipes: &[Recipe], -) -> Vec<Demand> { - let recipes = recipes - .iter() - .filter(|r| r.tile().map(|t| tiles.contains(&t)).unwrap_or(true)) - .collect::<Vec<_>>(); - - let mut producable = HashMap::new(); - - for i in items { - producable.insert(*i, 0.0); - } - - loop { - let prod_count = producable.len(); - - for r in &recipes { - let output_count = r.outputs().iter().filter(|o| !items.contains(o)).count(); - let Some(ingred_cost) = r - .inputs() - .iter() - .map(|i| producable.get(i).copied()) - .reduce(|a, b| { - if let (Some(a), Some(b)) = (a, b) { - Some(a + b) - } else { - None - } - }) - .unwrap_or(Some(0.)) - else { - continue; - }; - - let base_cost = match r { - Recipe::Passive { speed, .. } => 2. + (1. / speed) * 0.1, - Recipe::Active { speed, .. } => 2. + (1. / speed), - Recipe::Instant { .. } => 1., - }; - - let output_cost = (ingred_cost + base_cost) / output_count as f32; - for o in r.outputs() { - let cost = producable.entry(o).or_insert(f32::INFINITY); - *cost = cost.min(output_cost); - } - } - - if prod_count == producable.len() { - break; - } - } - - raw_demands - .iter() - .filter_map(|(i, o, d)| { - producable.get(i).map(|cost| Demand { - input: *i, - output: *o, - duration: *d, - points: *cost as i64, - }) - }) - .collect() -} |