1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/*
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 crate::diagram_svg::node_color;
use anyhow::{Result, bail};
use hurrycurry_protocol::{
Gamedata, Message,
book::{Diagram, DiagramNode, NodeStyle},
};
use std::{
fmt::Write,
io::Write as W2,
process::{Command, Stdio},
};
pub fn diagram_dot_svg(data: &Gamedata, diagram: &Diagram) -> Result<String> {
let mut child = Command::new("dot")
.arg("-Tsvg")
.arg("-Knop2")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
let dot = diagram_dot(data, diagram, true)?;
child.stdin.as_mut().unwrap().write_all(dot.as_bytes())?;
let output = child.wait_with_output()?;
if !output.status.success() {
bail!("dot failed");
}
Ok(String::from_utf8(output.stdout)?)
}
pub fn diagram_dot(data: &Gamedata, diagram: &Diagram, use_position: bool) -> Result<String> {
let mut out = String::new();
writeln!(out, "digraph {{")?;
for (i, n) in diagram.nodes.iter().enumerate() {
let mut attrs = Vec::new();
if use_position {
attrs.push(format!("pos=\"{},{}!\"", n.position.x, n.position.y));
}
node_style(&mut attrs, &n);
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} [{}]", attrs.join(" "))?;
}
for edge in &diagram.edges {
writeln!(out, "k{} -> k{}", edge.src, edge.dst)?;
}
writeln!(out, "}}")?;
Ok(out)
}
fn node_style(attrs: &mut Vec<String>, node: &DiagramNode) {
let shape = match node.style {
NodeStyle::FinalProduct => "circle",
NodeStyle::IntermediateProduct => "circle",
NodeStyle::ProcessActive => "box",
NodeStyle::ProcessPassive => "box",
NodeStyle::ProcessInstant => "box",
};
let color = node_color(node);
attrs.push(format!("shape={shape}"));
attrs.push("style=filled".to_owned());
attrs.push(format!("fillcolor=\"{color}\""));
attrs.push(format!("color=\"{color}\""));
}
|