From 24b989b9f2c99ba6f933ea3bd7da6be58ecd1530 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Sat, 11 Jan 2025 22:08:40 +0100 Subject: [PATCH] Improve belt blueprint generation --- src/blueprint/balancer.rs | 199 ++++++++++++++++---------------------- src/blueprint/belt.rs | 84 ++++++++++++++++ src/blueprint/mod.rs | 1 + 3 files changed, 166 insertions(+), 118 deletions(-) create mode 100644 src/blueprint/belt.rs diff --git a/src/blueprint/balancer.rs b/src/blueprint/balancer.rs index ed173fa..1b7098f 100644 --- a/src/blueprint/balancer.rs +++ b/src/blueprint/balancer.rs @@ -1,137 +1,100 @@ -use super::{Blueprint, BlueprintEntity, BlueprintPosition}; +use crate::{belt_finding::common::PathField, prelude::*}; + +use super::{ + belt::{convert_to_blueprint, Beltspeed}, + Blueprint, BlueprintEntity, BlueprintPosition, +}; pub fn generate_4_lane_balancer() -> Blueprint { - let e = vec![ + let mut e = vec![ BlueprintEntity::builder("splitter".to_owned(), 1, BlueprintPosition::new(1.0, 0.5)) .build(), BlueprintEntity::builder("splitter".to_owned(), 2, BlueprintPosition::new(3.0, 0.5)) .build(), BlueprintEntity::builder("splitter".to_owned(), 3, BlueprintPosition::new(2.0, 1.5)) .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 4, - BlueprintPosition::new(0.5, 1.5), - ) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 5, - BlueprintPosition::new(3.5, 1.5), - ) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 6, - BlueprintPosition::new(0.5, 2.5), - ) - .build(), - BlueprintEntity::builder( - "underground-belt".to_owned(), - 4, - BlueprintPosition::new(1.5, 2.5), - ) - .underground_type("output".to_owned()) - .build(), - BlueprintEntity::builder( - "underground-belt".to_owned(), - 5, - BlueprintPosition::new(2.5, 2.5), - ) - .underground_type("output".to_owned()) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 6, - BlueprintPosition::new(3.5, 2.5), - ) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 7, - BlueprintPosition::new(0.5, 3.5), - ) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 8, - BlueprintPosition::new(1.5, 3.5), - ) - .direction(12) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 9, - BlueprintPosition::new(2.5, 3.5), - ) - .direction(4) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 10, - BlueprintPosition::new(3.5, 3.5), - ) - .build(), BlueprintEntity::builder("splitter".to_owned(), 11, BlueprintPosition::new(2.0, 4.5)) .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 12, - BlueprintPosition::new(0.5, 5.5), - ) - .direction(4) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 13, - BlueprintPosition::new(1.5, 5.5), - ) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 14, - BlueprintPosition::new(2.5, 5.5), - ) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 15, - BlueprintPosition::new(3.5, 5.5), - ) - .direction(12) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 16, - BlueprintPosition::new(0.5, 6.5), - ) - .build(), - BlueprintEntity::builder( - "underground-belt".to_owned(), - 17, - BlueprintPosition::new(1.5, 6.5), - ) - .underground_type("input".to_owned()) - .build(), - BlueprintEntity::builder( - "underground-belt".to_owned(), - 18, - BlueprintPosition::new(2.5, 6.5), - ) - .underground_type("input".to_owned()) - .build(), - BlueprintEntity::builder( - "transport-belt".to_owned(), - 19, - BlueprintPosition::new(3.5, 6.5), - ) - .build(), BlueprintEntity::builder("splitter".to_owned(), 20, BlueprintPosition::new(1.0, 7.5)) .build(), BlueprintEntity::builder("splitter".to_owned(), 21, BlueprintPosition::new(3.0, 7.5)) .build(), ]; + let mut nextfree = e.len() as u32; + e.extend(convert_to_blueprint( + &[ + PathField::Belt { + pos: Position::new(0, 1), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(3, 1), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(0, 2), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(3, 2), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(0, 3), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(1, 3), + dir: Direction::Left, + }, + PathField::Belt { + pos: Position::new(2, 3), + dir: Direction::Right, + }, + PathField::Belt { + pos: Position::new(3, 3), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(0, 5), + dir: Direction::Right, + }, + PathField::Belt { + pos: Position::new(1, 5), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(2, 5), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(3, 5), + dir: Direction::Left, + }, + PathField::Belt { + pos: Position::new(0, 6), + dir: Direction::Up, + }, + PathField::Belt { + pos: Position::new(3, 6), + dir: Direction::Up, + }, + PathField::Underground { + pos: Position::new(1, 6), + dir: Direction::Up, + len: 4, + }, + PathField::Underground { + pos: Position::new(2, 6), + dir: Direction::Up, + len: 4, + }, + ], + &Beltspeed::Normal, + &mut nextfree, + )); + Blueprint::builder() .label("balancer".to_string()) .entities(e) diff --git a/src/blueprint/belt.rs b/src/blueprint/belt.rs new file mode 100644 index 0000000..2e2e66f --- /dev/null +++ b/src/blueprint/belt.rs @@ -0,0 +1,84 @@ +use crate::{belt_finding::common::PathField, prelude::PositionType}; + +use super::{Blueprint, BlueprintEntity, BlueprintPosition}; + +pub enum Beltspeed { + Normal, + Fast, + Express, + Turbo, +} + +impl Beltspeed { + fn string(&self) -> String { + match self { + Beltspeed::Normal => "transport-belt", + Beltspeed::Fast => "fast-transport-belt", + Beltspeed::Express => "express-transport-belt", + Beltspeed::Turbo => "turbo-transport-belt", + } + .to_owned() + } + + fn string_underground(&self) -> String { + match self { + Beltspeed::Normal => "underground-belt", + Beltspeed::Fast => "fast-underground-belt", + Beltspeed::Express => "express-underground-belt", + Beltspeed::Turbo => "turbo-underground-belt", + } + .to_owned() + } +} + +pub fn convert_belt_to_blueprint( + belt: &PathField, + speed: &Beltspeed, + nextfree: &mut u32, +) -> impl Iterator { + match belt { + PathField::Belt { pos, dir } => { + *nextfree += 1; + vec![BlueprintEntity::builder( + speed.string(), + *nextfree - 1, + BlueprintPosition::new(pos.x as f64 + 0.5, pos.y as f64 + 0.5), + ) + .direction(dir.get_index() * 4) + .build()] + .into_iter() + } + PathField::Underground { pos, dir, len } => { + *nextfree += 2; + let endpos = pos.in_direction(dir, *len as PositionType); + vec![ + BlueprintEntity::builder( + speed.string_underground(), + *nextfree - 2, + BlueprintPosition::new(pos.x as f64 + 0.5, pos.y as f64 + 0.5), + ) + .underground_type("input".to_string()) + .direction(dir.get_index() * 4) + .build(), + BlueprintEntity::builder( + speed.string_underground(), + *nextfree - 1, + BlueprintPosition::new(endpos.x as f64 + 0.5, endpos.y as f64 + 0.5), + ) + .underground_type("output".to_string()) + .direction(dir.get_index() * 4) + .build(), + ] + .into_iter() + } + } +} + +pub fn convert_to_blueprint<'p, 's, 'n>( + path: &'p [PathField], + speed: &'s Beltspeed, + nextfree: &'n mut u32, +) -> impl Iterator + use<'p, 's, 'n> { + path.iter() + .flat_map(|b| convert_belt_to_blueprint(b, speed, nextfree)) +} diff --git a/src/blueprint/mod.rs b/src/blueprint/mod.rs index d27b748..446efdf 100644 --- a/src/blueprint/mod.rs +++ b/src/blueprint/mod.rs @@ -9,6 +9,7 @@ pub mod structs; pub use structs::*; pub mod balancer; +pub mod belt; pub mod train; pub fn decode(s: &str) -> String {