Add graphviz diagram generator for crafting recipes.

This commit is contained in:
Avi Weinstock 2021-06-08 14:42:51 -04:00
parent 08862a1ebd
commit b5b7de9532
3 changed files with 66 additions and 0 deletions

1
Cargo.lock generated
View File

@ -5796,6 +5796,7 @@ dependencies = [
"num-derive",
"num-traits",
"ordered-float 2.5.1",
"petgraph 0.5.1",
"rand 0.8.3",
"rayon",
"ron",

View File

@ -9,6 +9,7 @@ no-assets = []
tracy = ["common-base/tracy"]
simd = ["vek/platform_intrinsics"]
bin_csv = ["ron", "csv", "structopt"]
bin_graphviz = ["petgraph"]
default = ["simd"]
@ -51,6 +52,8 @@ ron = { version = "0.6", default-features = false, optional = true }
# csv export
csv = { version = "1.1.3", optional = true }
structopt = { version = "0.3.13", optional = true }
# graphviz exporters
petgraph = { version = "0.5.1", optional = true }
# Data structures
hashbrown = { version = "0.11", features = ["rayon", "serde", "nightly"] }
@ -88,3 +91,7 @@ required-features = ["bin_csv"]
[[bin]]
name = "csv_import"
required-features = ["bin_csv"]
[[bin]]
name = "recipe_graphviz"
required-features = ["bin_graphviz"]

View File

@ -0,0 +1,58 @@
use hashbrown::HashMap;
use petgraph::{
dot::{Config, Dot},
Graph,
};
use std::{fs::File, io::Write};
use veloren_common::{
assets::AssetExt,
comp::item::ItemDesc,
recipe::{RecipeBook, RecipeInput},
};
fn main() {
let recipes = RecipeBook::load_expect_cloned("common.recipe_book");
let mut graph = Graph::new();
let mut nodes = HashMap::new();
let mut add_node = |graph: &mut Graph<_, _>, node: &str| {
nodes
.entry(node.to_owned())
.or_insert_with(|| graph.add_node(node.to_owned()))
.clone()
};
for (_, recipe) in recipes.iter() {
let output = recipe.output.0.item_definition_id();
let inputs = recipe
.inputs
.iter()
.map(|(i, _)| i)
.filter_map(|input| {
if let RecipeInput::Item(item) = input {
Some(item.item_definition_id())
} else {
None
}
})
.collect::<Vec<_>>();
let out_node = add_node(&mut graph, output);
for input in inputs.iter() {
let in_node = add_node(&mut graph, input);
graph.add_edge(in_node, out_node, ());
}
}
let mut f = File::create("recipe_graph.dot").unwrap();
writeln!(f, "digraph {{").unwrap();
writeln!(f, "rankdir = \"LR\"").unwrap();
writeln!(
f,
"{:#?}",
Dot::with_attr_getters(
&graph,
&[Config::EdgeNoLabel, Config::GraphContentOnly],
&|_, _| "".to_owned(),
&|_, _| { "constraint=false".to_owned() }
)
)
.unwrap();
writeln!(f, "}}").unwrap();
}