diff options
Diffstat (limited to 'server/tools')
| -rw-r--r-- | server/tools/src/diagram_dot.rs | 67 | ||||
| -rw-r--r-- | server/tools/src/recipe_diagram.rs | 28 | 
2 files changed, 61 insertions, 34 deletions
| diff --git a/server/tools/src/diagram_dot.rs b/server/tools/src/diagram_dot.rs index e78f831f..2c953987 100644 --- a/server/tools/src/diagram_dot.rs +++ b/server/tools/src/diagram_dot.rs @@ -17,7 +17,10 @@  */  use anyhow::Result; -use hurrycurry_protocol::{Gamedata, Message, book::Diagram}; +use hurrycurry_protocol::{ +    Gamedata, Message, +    book::{Diagram, NodeStyle}, +};  use std::fmt::Write;  pub fn diagram_dot(data: &Gamedata, diagram: &Diagram) -> Result<String> { @@ -25,20 +28,37 @@ pub fn diagram_dot(data: &Gamedata, diagram: &Diagram) -> Result<String> {      writeln!(out, "digraph {{")?;      for (i, n) in diagram.nodes.iter().enumerate() {          let mut attrs = Vec::new(); -        if let Some(color) = &n.color { -            attrs.push(format!("style=filled")); -            attrs.push(format!("fillcolor={color:?}")); -            attrs.push(format!("color={color:?}")); -        } -        if let Some(shape) = &n.shape { -            attrs.push(format!("shape={shape}")); + +        node_style(&mut attrs, &n.style); +        match &n.label { +            Message::Text(text) => { +                attrs.push(format!("label=\"{text}\"")); +            } +            Message::Item(item_index) => { +                attrs.push(format!( +                    "image=\"/tmp/items/{}.png\"", +                    data.item_name(*item_index) +                )); +                attrs.push("imagescale=true".to_owned()); +                attrs.push("width=1".to_owned()); +                attrs.push("height=1".to_owned()); +                attrs.push("fixedsize=true".to_owned()); +                attrs.push("label=\"\"".to_owned()); +            } +            Message::Tile(tile_index) => { +                attrs.push(format!( +                    "image=\"/tmp/tiles/{}.png\"", +                    data.tile_name(*tile_index) +                )); +                attrs.push("imagescale=true".to_owned()); +                attrs.push("width=1".to_owned()); +                attrs.push("height=1".to_owned()); +                attrs.push("fixedsize=true".to_owned()); +                attrs.push("label=\"\"".to_owned()); +            } +            _ => unimplemented!(),          } -        writeln!( -            out, -            "k{i} [label=\"{}\" {}]", -            message_str(data, &n.label), -            attrs.join(" ") -        )?; +        writeln!(out, "k{i} [{}]", attrs.join(" "))?;      }      for edge in &diagram.edges {          writeln!(out, "k{} -> k{}", edge.src, edge.dst)?; @@ -47,11 +67,16 @@ pub fn diagram_dot(data: &Gamedata, diagram: &Diagram) -> Result<String> {      Ok(out)  } -fn message_str(data: &Gamedata, message: &Message) -> String { -    match message { -        Message::Translation { .. } => todo!(), -        Message::Text(x) => x.to_owned(), -        Message::Item(item_index) => data.item_name(*item_index).to_string(), -        Message::Tile(tile_index) => data.tile_name(*tile_index).to_string(), -    } +fn node_style(attrs: &mut Vec<String>, style: &NodeStyle) { +    let (shape, color) = match style { +        NodeStyle::FinalProduct => ("circle", "#555"), +        NodeStyle::IntermediateProduct => ("circle", "#333"), +        NodeStyle::ProcessActive => ("box", "#47c42b"), +        NodeStyle::ProcessPassive => ("box", "#c4a32b"), +        NodeStyle::ProcessInstant => ("box", "#5452d8"), +    }; +    attrs.push(format!("shape={shape}")); +    attrs.push("style=filled".to_owned()); +    attrs.push(format!("fillcolor=\"{color}\"")); +    attrs.push(format!("color=\"{color}\""));  } diff --git a/server/tools/src/recipe_diagram.rs b/server/tools/src/recipe_diagram.rs index cdc6a942..5a119494 100644 --- a/server/tools/src/recipe_diagram.rs +++ b/server/tools/src/recipe_diagram.rs @@ -19,7 +19,7 @@  use anyhow::Result;  use hurrycurry_protocol::{      Gamedata, ItemIndex, Message, Recipe, RecipeIndex, -    book::{Diagram, DiagramEdge, DiagramNode}, +    book::{Diagram, DiagramEdge, DiagramNode, NodeStyle},      glam::Vec2,  };  use hurrycurry_server::data::Serverdata; @@ -84,27 +84,29 @@ pub(crate) fn recipe_diagram(          diag.nodes.push(DiagramNode {              label: Message::Item(i),              position: Vec2::ZERO, -            color: None, -            shape: None, +            style: if target_items.contains(data.item_name(i)) { +                NodeStyle::FinalProduct +            } else { +                NodeStyle::IntermediateProduct +            },          });      }      for r in recipes {          let index = diag.nodes.len();          let recipe = data.recipe(r.index); -        let (kind, color) = match recipe { -            Recipe::Passive { .. } => ("Passive", "#c4a32b"), -            Recipe::Active { .. } => ("Active", "#47c42b"), -            Recipe::Instant { .. } => ("Instant", "#5452d8"), +        let (kind, style) = match recipe { +            Recipe::Passive { .. } => ("Passive", NodeStyle::ProcessPassive), +            Recipe::Active { .. } => ("Active", NodeStyle::ProcessActive), +            Recipe::Instant { .. } => ("Instant", NodeStyle::ProcessInstant),          };          diag.nodes.push(DiagramNode {              position: Vec2::ZERO, -            label: Message::Text(if let Some(tile) = recipe.tile() { -                format!("{kind} on {}", data.tile_name(tile)) +            label: if let Some(tile) = recipe.tile() { +                Message::Tile(tile)              } else { -                format!("{kind}") -            }), -            color: Some(color.to_string()), -            shape: Some("box".to_string()), +                Message::Text(format!("{kind}")) +            }, +            style,          });          for i in r.inputs { | 
