mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
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:
commit
875b26a93a
@ -6,7 +6,7 @@ code-quality:
|
||||
script:
|
||||
- ln -s /dockercache/target target
|
||||
- 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
|
||||
|
||||
security:
|
||||
|
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -5796,6 +5796,7 @@ dependencies = [
|
||||
"num-derive",
|
||||
"num-traits",
|
||||
"ordered-float 2.5.1",
|
||||
"petgraph 0.5.1",
|
||||
"rand 0.8.3",
|
||||
"rayon",
|
||||
"ron",
|
||||
|
@ -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"]
|
||||
|
59
common/src/bin/recipe_graphviz.rs
Normal file
59
common/src/bin/recipe_graphviz.rs
Normal 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user