Merge branch 'aweinstock/recipe-graphviz' into 'master'

Add graphviz diagram generator for crafting recipes.

See merge request veloren/veloren!2401
This commit is contained in:
Marcel 2021-06-09 09:14:13 +00:00
commit 875b26a93a
4 changed files with 68 additions and 1 deletions

View File

@ -6,7 +6,7 @@ code-quality:
script: script:
- ln -s /dockercache/target target - ln -s /dockercache/target target
- rm -r target/debug/incremental/* || echo "all good" # TMP FIX FOR 2021-03-22-nightly - rm -r target/debug/incremental/* || echo "all good" # TMP FIX FOR 2021-03-22-nightly
- cargo clippy --all-targets --locked --features="bin_csv,bin_bot,asset_tweak" -- -D warnings - cargo clippy --all-targets --locked --features="bin_csv,bin_graphviz,bin_bot,asset_tweak" -- -D warnings
- cargo fmt --all -- --check - cargo fmt --all -- --check
security: security:

1
Cargo.lock generated
View File

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

View File

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

View File

@ -0,0 +1,59 @@
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()))
};
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, ());
}
}
// you can render the dot file as a png with `dot -Tpng recipe_graph.dot >
// recipe_graph.png` or interactively view it with `xdot recipe_graph.dot`
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();
}