diff options
author | metamuffin <metamuffin@disroot.org> | 2025-10-06 13:31:00 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-10-06 13:31:00 +0200 |
commit | 821bacba156738d590a55505e03c8488c5db5db8 (patch) | |
tree | 2eec5b46b6553fd7c577733c2c67861e5fa0d440 | |
parent | 54329141cca73f03f6465933d3b37deb7d77ba58 (diff) | |
download | hurrycurry-821bacba156738d590a55505e03c8488c5db5db8.tar hurrycurry-821bacba156738d590a55505e03c8488c5db5db8.tar.bz2 hurrycurry-821bacba156738d590a55505e03c8488c5db5db8.tar.zst |
Filter out large instant recipe clusters
-rw-r--r-- | server/protocol/src/book.rs | 9 | ||||
-rw-r--r-- | server/tools/src/recipe_diagram.rs | 67 |
2 files changed, 59 insertions, 17 deletions
diff --git a/server/protocol/src/book.rs b/server/protocol/src/book.rs index 73b14c07..7c529f22 100644 --- a/server/protocol/src/book.rs +++ b/server/protocol/src/book.rs @@ -70,3 +70,12 @@ pub struct DiagramEdge { pub dst: usize, pub label: Option<Message>, } + +impl NodeStyle { + pub fn is_procuct(&self) -> bool { + matches!( + self, + NodeStyle::IntermediateProduct | NodeStyle::FinalProduct + ) + } +} diff --git a/server/tools/src/recipe_diagram.rs b/server/tools/src/recipe_diagram.rs index 958e5aa9..8e9c14b6 100644 --- a/server/tools/src/recipe_diagram.rs +++ b/server/tools/src/recipe_diagram.rs @@ -23,7 +23,10 @@ use hurrycurry_protocol::{ glam::Vec2, }; use hurrycurry_server::data::Serverdata; -use std::collections::{BTreeMap, BTreeSet, HashSet}; +use std::{ + cmp::Reverse, + collections::{BTreeMap, BTreeSet, HashSet}, +}; pub(crate) fn recipe_diagram( data: &Gamedata, @@ -94,6 +97,18 @@ pub(crate) fn recipe_diagram( for r in recipes { let index = diag.nodes.len(); let recipe = data.recipe(r.index); + + if matches!(recipe, Recipe::Instant { .. }) && r.outputs.len() == 1 { + for i in r.inputs { + diag.edges.push(DiagramEdge { + src: item_index[&i], + dst: item_index[&r.outputs[0]], + label: None, + }); + } + continue; + } + let (kind, style) = match recipe { Recipe::Passive { .. } => ("Passive", NodeStyle::ProcessPassive), Recipe::Active { .. } => ("Active", NodeStyle::ProcessActive), @@ -125,17 +140,17 @@ pub(crate) fn recipe_diagram( } } - remove_instant_intermediate(&mut diag); + merge_combine_clusters(&mut diag); Ok(diag) } -pub fn remove_instant_intermediate(diag: &mut Diagram) { +fn merge_combine_clusters(diag: &mut Diagram) { let instant_nodes = diag .nodes .iter() .enumerate() - .filter(|(_, n)| matches!(n.style, NodeStyle::ProcessInstant)) + .filter(|(_, n)| matches!(n.style, NodeStyle::IntermediateProduct)) .map(|(i, _)| i) .collect::<Vec<_>>(); @@ -155,20 +170,38 @@ pub fn remove_instant_intermediate(diag: &mut Diagram) { .map(|(i, e)| (i, e.dst)) .collect::<Vec<_>>(); - if inputs.len() == 1 && outputs.len() == 1 { - // order remove because index changes - if inputs[0].0 > outputs[0].0 { - diag.edges.remove(inputs[0].0); - diag.edges.remove(outputs[0].0); - } else { - diag.edges.remove(outputs[0].0); - diag.edges.remove(inputs[0].0); + if outputs + .iter() + .all(|&(_, i)| diag.nodes[i].style.is_procuct()) + && inputs + .iter() + .all(|&(_, i)| diag.nodes[i].style.is_procuct()) + { + let mut to_remove = inputs + .iter() + .map(|&(i, _)| i) + .chain(outputs.iter().map(|&(i, _)| i)) + .collect::<Vec<_>>(); + to_remove.sort_by_key(|x| Reverse(*x)); + for i in to_remove { + diag.edges.remove(i); + } + + for &input in &inputs { + for &output in &outputs { + if !diag + .edges + .iter() + .any(|e| e.src == input.1 && e.dst == output.1) + { + diag.edges.push(DiagramEdge { + src: input.1, + dst: output.1, + label: None, + }); + } + } } - diag.edges.push(DiagramEdge { - src: inputs[0].1, - dst: outputs[0].1, - label: None, - }); } } } |