diff --git a/factorio-blueprint-generator/src/bin/generate_factory.rs b/factorio-blueprint-generator/src/bin/generate_factory.rs index 587952a..9778b86 100644 --- a/factorio-blueprint-generator/src/bin/generate_factory.rs +++ b/factorio-blueprint-generator/src/bin/generate_factory.rs @@ -1,12 +1,11 @@ -use std::path::PathBuf; - use clap::Parser; use factorio_blueprint::{BlueprintString, encode}; use factorio_blueprint_generator::factory::{FactoryGraph, generate_factory}; -use factorio_core::prelude::*; +use factorio_core::{prelude::*, visualize::Visualize}; use factorio_layout::{genetic_algorithm_v1::GeneticAlgorithm, valid_layout::ValidLayout}; use factorio_pathfinding::belt_finding::ConflictAvoidance; use rand::{SeedableRng, rngs::SmallRng}; +use std::path::PathBuf; #[derive(Parser)] struct Args { @@ -25,7 +24,7 @@ fn main() { // .with_env_filter(EnvFilter::from_default_env()) // .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) // .init(); - tracing_perfetto::install_perfetto_subscriber(); + // tracing_perfetto::install_perfetto_subscriber(); let text = std::fs::File::open(&args.path).unwrap(); let factory_graph: FactoryGraph = serde_yaml::from_reader(text).unwrap(); @@ -41,10 +40,10 @@ fn main() { let l = GeneticAlgorithm { mutation_retries: 20, - population_size: 4, - population_keep: 1, - population_new: 1, - generations: 5, + population_size: 10, + population_keep: 2, + population_new: 2, + generations: 4, valid_layout: l, }; let p = ConflictAvoidance { @@ -53,9 +52,11 @@ fn main() { let mut rng = SmallRng::seed_from_u64(args.seed); - let b = BlueprintString::Blueprint( - generate_factory(&l, &p, factory_graph, &mut rng).to_blueprint(), - ); + let b = generate_factory(&l, &p, factory_graph, &mut rng); + + b.print_visualization(); + + let b = BlueprintString::Blueprint(b.to_blueprint()); if args.json { println!("{}", serde_json::to_string_pretty(&b).unwrap()); diff --git a/factorio-blueprint/src/abstraction/power_connection.rs b/factorio-blueprint/src/abstraction/power_connection.rs index 280d425..e57e024 100644 --- a/factorio-blueprint/src/abstraction/power_connection.rs +++ b/factorio-blueprint/src/abstraction/power_connection.rs @@ -8,14 +8,35 @@ use super::*; #[derive(Debug)] struct PowerGraph { - nodes: HashMap>, + nodes: HashMap, f64)>, +} + +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] +enum NodeType { + In, + Out, } impl WheightedGraph for PowerGraph { - type Node = Position; + type Node = (Position, NodeType); fn edge(&self, node: &Self::Node, num: usize) -> Option<(Self::Node, f64)> { - self.nodes.get(node).and_then(|v| v.get(num).cloned()) + match node.1 { + NodeType::In => { + if num == 0 { + self.nodes + .get(&node.0) + .map(|v| ((node.0, NodeType::Out), v.1)) + } else { + None + } + } + NodeType::Out => self + .nodes + .get(&node.0) + .and_then(|v| v.0.get(num).cloned()) + .map(|v| ((v, NodeType::In), 0.0)), + } } } @@ -59,7 +80,7 @@ impl Blueprint { for y_source in 0..blocked.height { for x_source in 0..blocked.width { - if blocked.get(x_source, y_source).is_some() { + if let &Some(w) = blocked.get(x_source, y_source) { let pos = Position::new(x_source as PositionType, y_source as PositionType); let mut edges = Vec::new(); for (xx, yy) in (-9..=9) @@ -77,11 +98,11 @@ impl Blueprint { }) { if let &Some(w) = blocked.get(xx, yy) { - edges.push((Position::new(xx as PositionType, yy as PositionType), w)); + edges.push(Position::new(xx as PositionType, yy as PositionType)); } } if !edges.is_empty() { - graph.nodes.insert(pos, edges); + graph.nodes.insert(pos, (edges, w)); } } } @@ -89,7 +110,12 @@ impl Blueprint { let pole_positions = power_poles .iter() - .map(|(_, e)| (e.position - Position::new(1, 1)) / 2 + offset) + .map(|(_, e)| { + ( + (e.position - Position::new(1, 1)) / 2 + offset, + NodeType::Out, + ) + }) .collect::>(); if let Some(res) = @@ -101,7 +127,11 @@ impl Blueprint { .collect::>(); for path in res { - for &p in &path { + let path_iter = path.iter().filter_map(|(p, n)| match n { + NodeType::In => None, + NodeType::Out => Some(p), + }); + for &p in path_iter.clone() { match power_pole_map.entry(p) { std::collections::hash_map::Entry::Occupied(_occupied_entry) => (), std::collections::hash_map::Entry::Vacant(vacant_entry) => { @@ -113,7 +143,7 @@ impl Blueprint { } } } - for (p, n) in path.iter().zip(path[1..].iter()) { + for (p, n) in path_iter.clone().zip(path_iter.skip(1)) { self.add_wire(power_pole_map[n], 5, power_pole_map[p], 5); } }