From ce76626f797d18e84c88b1afe45cdcce21f625eb Mon Sep 17 00:00:00 2001 From: hal8174 Date: Fri, 24 Jan 2025 23:25:43 +0100 Subject: [PATCH] Add multistation generation --- .../src/bin/multistation.rs | 54 +++ factorio-blueprint-generator/src/lib.rs | 1 + .../src/multistation.rs | 325 ++++++++++++++++++ factorio-blueprint-generator/src/station.rs | 4 +- factorio-blueprint/blueprints/rail-curve.bp | 2 +- factorio-blueprint/blueprints/stacker.bp | 1 + factorio-blueprint/src/abstraction.rs | 74 +++- 7 files changed, 451 insertions(+), 10 deletions(-) create mode 100644 factorio-blueprint-generator/src/bin/multistation.rs create mode 100644 factorio-blueprint-generator/src/multistation.rs create mode 100644 factorio-blueprint/blueprints/stacker.bp diff --git a/factorio-blueprint-generator/src/bin/multistation.rs b/factorio-blueprint-generator/src/bin/multistation.rs new file mode 100644 index 0000000..8dd895e --- /dev/null +++ b/factorio-blueprint-generator/src/bin/multistation.rs @@ -0,0 +1,54 @@ +use clap::Parser; +use factorio_blueprint::{BlueprintString, encode}; +use factorio_blueprint_generator::multistation::{StationSpec, multistation}; +use factorio_core::beltoptions::Beltspeed; + +#[derive(Parser)] +struct Args { + #[arg(short, long)] + json: bool, +} + +fn main() { + let args = Args::parse(); + + let stations: Vec<_> = (0..8) + .map(|_| StationSpec { + locomotives: 1, + wagons: 2, + load: false, + beltspeed: Beltspeed::Normal, + lanes: 1, + }) + .collect(); + + let b = BlueprintString::Blueprint( + multistation( + &stations, + // &[ + // StationSpec { + // locomotives: 1, + // wagons: 1, + // load: false, + // beltspeed: Beltspeed::Normal, + // lanes: 1, + // }, + // StationSpec { + // locomotives: 1, + // wagons: 1, + // load: false, + // beltspeed: Beltspeed::Express, + // lanes: 1, + // }, + // ], + 16, + ) + .to_blueprint(), + ); + + if args.json { + println!("{}", serde_json::to_string_pretty(&b).unwrap()); + } + + println!("{}", encode(&serde_json::to_string(&b).unwrap())); +} diff --git a/factorio-blueprint-generator/src/lib.rs b/factorio-blueprint-generator/src/lib.rs index d024449..0c2db0a 100644 --- a/factorio-blueprint-generator/src/lib.rs +++ b/factorio-blueprint-generator/src/lib.rs @@ -1,5 +1,6 @@ pub mod assembly; pub mod balancer; pub mod binary_merger; +pub mod multistation; pub mod station; pub mod train; diff --git a/factorio-blueprint-generator/src/multistation.rs b/factorio-blueprint-generator/src/multistation.rs new file mode 100644 index 0000000..c54d80e --- /dev/null +++ b/factorio-blueprint-generator/src/multistation.rs @@ -0,0 +1,325 @@ +use factorio_blueprint::abstraction::{Blueprint, Entity, EntityType, RailType}; +use factorio_core::{beltoptions::Beltspeed, prelude::*}; + +use crate::station::basic_station; + +pub struct StationSpec { + pub locomotives: usize, + pub wagons: usize, + pub load: bool, + pub beltspeed: Beltspeed, + pub lanes: usize, +} + +pub fn multistation(stations: &[StationSpec], stacker_size: usize) -> Blueprint { + let longest_train = stations + .iter() + .map(|s| s.locomotives + s.wagons) + .max() + .unwrap(); + + let mut blueprint = Blueprint::new(); + + // stacker + let stacker_length = (longest_train * 5).div_ceil(2); + blueprint.add_entity(Entity::new_rail( + RailType::ChainSignal, + Position::new(23, 1), + 0, + )); + for i in 0..stacker_size { + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new(26, 4 + 8 * i as PositionType), + 10, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new(22, 14 + 8 * i as PositionType), + 10, + )); + + blueprint.add_entity(Entity::new_rail( + RailType::RailSignal, + Position::new(17, 15 + 8 * i as PositionType), + 2, + )); + + for j in 0..stacker_length { + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new( + 16 - 4 * j as PositionType, + 20 + 4 * j as PositionType + 8 * i as PositionType, + ), + 2, + )); + } + + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new( + 14 - 4 * stacker_length as PositionType, + 22 + 4 * stacker_length as PositionType + 8 * i as PositionType, + ), + 2, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new( + 10 - 4 * stacker_length as PositionType, + 32 + 4 * stacker_length as PositionType + 8 * i as PositionType, + ), + 2, + )); + blueprint.add_entity(Entity::new_rail( + RailType::ChainSignal, + Position::new( + 15 - 4 * stacker_length as PositionType, + 17 + 4 * stacker_length as PositionType + 8 * i as PositionType, + ), + 2, + )); + + if i != 0 { + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new(26, 2 + 8 * (i - 1) as PositionType), + 0, + )); + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new(26, 6 + 8 * (i - 1) as PositionType), + 0, + )); + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new( + 10 - 4 * stacker_length as PositionType, + 30 + 4 * stacker_length as PositionType + 8 * i as PositionType, + ), + 0, + )); + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new( + 10 - 4 * stacker_length as PositionType, + 34 + 4 * stacker_length as PositionType + 8 * i as PositionType, + ), + 0, + )); + } + } + + let total_stacker_height = (26 + 4 * stacker_length + 8 * stacker_size) as PositionType; + + let inrail_x = 10 - 4 * stacker_length as PositionType; + let outrail_x = inrail_x + 52 + 4 * (7 * longest_train).div_ceil(2) as PositionType; + let mut previous_station_heights = 0; + // station + for (i, station) in stations.iter().enumerate() { + // in turn + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new( + inrail_x, + total_stacker_height + 4 + previous_station_heights, + ), + 8, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new( + inrail_x + 4, + total_stacker_height + 14 + previous_station_heights, + ), + 8, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new( + inrail_x + 12, + total_stacker_height + 22 + previous_station_heights, + ), + 14, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new( + inrail_x + 22, + total_stacker_height + 26 + previous_station_heights, + ), + 14, + )); + blueprint.add_entity(Entity::new_rail( + RailType::RailSignal, + Position::new( + inrail_x + 3, + total_stacker_height + 17 + previous_station_heights, + ), + 15, + )); + + // out turn + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new( + outrail_x, + total_stacker_height + 4 + previous_station_heights, + ), + 10, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new( + outrail_x - 4, + total_stacker_height + 14 + previous_station_heights, + ), + 10, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new( + outrail_x - 12, + total_stacker_height + 22 + previous_station_heights, + ), + 4, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new( + outrail_x - 22, + total_stacker_height + 26 + previous_station_heights, + ), + 4, + )); + blueprint.add_entity(Entity::new_rail( + RailType::RailSignal, + Position::new( + outrail_x - 3, + total_stacker_height + 17 + previous_station_heights, + ), + 9, + )); + + for j in 0..dbg!( + (7 * longest_train).div_ceil(2) + - (7 * (station.locomotives + station.wagons)).div_ceil(2) + ) { + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new( + inrail_x + 26 + 4 * j as PositionType, + 28 + total_stacker_height + previous_station_heights, + ), + 4, + )); + } + + let mut b = basic_station( + station.load, + station.locomotives, + station.wagons, + station.lanes, + station.beltspeed, + factorio_core::beltoptions::Belttype::Full, + ); + + let station_height = PositionType::max(16, 16); + assert!(station_height % 4 == 0); + + b.transform(Transformation::new( + Direction::Down, + Position::new( + inrail_x + 26 + 4 * (7 * longest_train).div_ceil(2) as PositionType, + 30 + total_stacker_height + previous_station_heights, + ), + )); + + blueprint.add_blueprint(b); + + // rail connection + if i != stations.len() - 1 { + for j in 0..(station_height / 4) { + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new( + inrail_x, + total_stacker_height + 2 + previous_station_heights + 4 * j as PositionType, + ), + 0, + )); + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new( + outrail_x, + total_stacker_height + 2 + previous_station_heights + 4 * j as PositionType, + ), + 0, + )); + } + + blueprint.add_entity(Entity::new_rail( + RailType::ChainSignal, + Position::new( + inrail_x - 3, + total_stacker_height + 13 + previous_station_heights, + ), + 0, + )); + blueprint.add_entity(Entity::new_rail( + RailType::RailSignal, + Position::new( + outrail_x + 3, + total_stacker_height + 13 + previous_station_heights, + ), + 8, + )); + } + + previous_station_heights += station_height; + } + + // output rails + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new(outrail_x, total_stacker_height - 4), + 0, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new(outrail_x - 4, total_stacker_height - 14), + 0, + )); + let out_diagonal_length = (outrail_x - 54) / 4; + for i in 0..out_diagonal_length { + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new( + outrail_x - 10 - 4 * i as PositionType, + total_stacker_height - 20 - 4 * i as PositionType, + ), + 6, + )); + } + let remaining_offset = total_stacker_height - 34 - out_diagonal_length * 4; + blueprint.add_entity(Entity::new_rail( + RailType::CurvedB, + Position::new(42, remaining_offset + 14), + 8, + )); + blueprint.add_entity(Entity::new_rail( + RailType::CurvedA, + Position::new(38, remaining_offset + 4), + 8, + )); + for i in 0..(remaining_offset / 4) { + blueprint.add_entity(Entity::new_rail( + RailType::Straight, + Position::new(38, 2 + 4 * i as PositionType), + 0, + )); + } + + blueprint +} diff --git a/factorio-blueprint-generator/src/station.rs b/factorio-blueprint-generator/src/station.rs index c16b105..700d82d 100644 --- a/factorio-blueprint-generator/src/station.rs +++ b/factorio-blueprint-generator/src/station.rs @@ -244,7 +244,7 @@ pub fn basic_station( )); // rails - for l in 0..((length * 7 + global_x_offset + 1) / 2) { + for l in 0..(length * 7 + global_x_offset).div_ceil(2) { blueprint.add_entity(Entity::new_unknown( "straight-rail", Position::new(2 + 4 * l as PositionType, 2), @@ -264,7 +264,5 @@ pub fn basic_station( blueprint.add_blueprint(m); - // blueprint.transform(Transformation::new(Direction::Right, Position::new(0, 0))); - blueprint } diff --git a/factorio-blueprint/blueprints/rail-curve.bp b/factorio-blueprint/blueprints/rail-curve.bp index f46987f..044c2b3 100644 --- a/factorio-blueprint/blueprints/rail-curve.bp +++ b/factorio-blueprint/blueprints/rail-curve.bp @@ -1 +1 @@ -0eNqN0UsKgzAQBuC7zDqCUaMmVyml+BjKgMYSo1Qkd2/USmnrwuU8/m8xM0PZDPgwpC2oGajqdA/qMkNPd100S08XLYICU1ADjgHpGp+guLsyQG3JEm6JtZhuemhLNH6B7clqMCPWwQIEBTB4dL1PdXrBvSRyyWACFfAw9X5NBqttzEPH/tzo0C2P3Gx3+Qk3Pu/GmyuzbzY5UJPTV8jytyp+VX9osth64/MrBiOafl0QaSQTKYVIRCRj7twLWaaXjg== +0eNqN0csKgzAQBdB/mXWERhNr8iulFB9DGdAo8UFF8u+NWilFoS4nmXsWdybIyh4bS6YDPQHltWlB3yZo6WnScn4zaYWgwaZUgmNApsAXaO7uDNB01BGuiWUYH6avMrR+gW3JvLcDFsEMBCkwaOrWp2oz416KQ8VgBB1wkXi/IIv5+u3HHRsestkBG/GNjf6z0XlWfthI/bJcHLjidAvisrnXneurpg4rr3yvxWBA2y4b0lcolJJS8kTEwrk3ocSX7Q== diff --git a/factorio-blueprint/blueprints/stacker.bp b/factorio-blueprint/blueprints/stacker.bp new file mode 100644 index 0000000..cd4a839 --- /dev/null +++ b/factorio-blueprint/blueprints/stacker.bp @@ -0,0 +1 @@ +0eNqN1tluwjAQBdB/8bOD4j3Or1RVFcCilsCgLKgI5d9rAgOtGjX3McscTXIzdq5svR/CqY2pZ/WVxc0xdax+u7Iu7lKzv51LzSGwmrVN3LORs5i24YvVYnznLKQ+9jHcK6aDy0caDuvQ5hs4VXZ9rt199sVEcHY6drnqmG54llxpOLuwupBOjCP/A8kntBnac9hOTNH8B9kqN7qNbdjcL4tyxlX856MVjweeUcvV05Ur81uWM7CebXg9RyuCFdCwwd9oSW653K5FWeurB2uqZdbhrCXWLrMVzmpi9TLrcVYSK5dZUeIuZWaAzAQ8Xbai0DQQmpC4S6lpIDWhcJdi00BsQuMu5aaR3AzuUm4ayQ0fNke5KSQ3fNoc5aaQ3PBxc5SbQnLz2OJrrX8uvkogi6/EJ87RF6GAL0IKdFXPPT9c6QEX3t6sdeQCb1jiE/dy8/6bd/XYh0Mue/0YcHYObTdVGCu99t4YIypt9Th+A9zvq+s= diff --git a/factorio-blueprint/src/abstraction.rs b/factorio-blueprint/src/abstraction.rs index 44e90bb..341e87b 100644 --- a/factorio-blueprint/src/abstraction.rs +++ b/factorio-blueprint/src/abstraction.rs @@ -1,5 +1,6 @@ use factorio_core::{ beltoptions::Beltspeed, + direction, pathfield::PathField, prelude::{Direction, Position, PositionType, Transformable, Transformation}, }; @@ -73,6 +74,32 @@ pub enum Quality { Legendary, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum RailType { + Straight, + CurvedA, + CurvedB, + RailSignal, + ChainSignal, +} + +impl RailType { + fn string(&self) -> String { + match self { + RailType::Straight => "straight-rail", + RailType::CurvedA => "curved-rail-a", + RailType::CurvedB => "curved-rail-b", + RailType::RailSignal => "rail-signal", + RailType::ChainSignal => "rail-chain-signal", + } + .to_string() + } + + fn size(&self) -> Position { + todo!() + } +} + pub struct Entity { entity: EntityType, position: Position, @@ -94,6 +121,10 @@ pub enum EntityType { recipe: String, size: Position, }, + Rail { + rail_type: RailType, + direction: u8, + }, Unknown { name: String, size: Position, @@ -174,6 +205,17 @@ impl Entity { ) } + pub fn new_rail(rail_type: RailType, position: Position, direction: u8) -> Self { + Self::new( + EntityType::Rail { + rail_type, + direction, + }, + position, + Direction::Up, + ) + } + pub fn new_unknown( name: impl AsRef, position: Position, @@ -218,6 +260,10 @@ impl Entity { recipe: _, size: _, } => name.clone(), + EntityType::Rail { + rail_type, + direction: _, + } => rail_type.string(), } } @@ -266,6 +312,10 @@ impl Entity { size, } => *size, EntityType::ElectricPole(electric_pole_type) => electric_pole_type.size(), + EntityType::Rail { + rail_type, + direction: _, + } => rail_type.size(), _ => Position::new(2, 2), } } @@ -343,8 +393,14 @@ impl Blueprint { } pub fn add_blueprint(&mut self, other: Self) { + let previous_entities = self.entities.len(); self.entities.extend(other.entities); - self.keys.extend(other.keys); + self.keys.extend( + other + .keys + .into_iter() + .map(|(k, v)| (k, v + previous_entities)), + ); self.wires.extend(other.wires); } @@ -359,11 +415,17 @@ impl Blueprint { i as u32 + 1, BlueprintPosition::new(0.5 * e.position.x as f64, 0.5 * e.position.y as f64), ) - .direction(match e.direction { - Direction::Up => 0, - Direction::Right => 4, - Direction::Down => 8, - Direction::Left => 12, + .direction(match &e.entity { + &EntityType::Rail { + rail_type: _, + direction, + } => direction, + _ => match e.direction { + Direction::Up => 0, + Direction::Right => 4, + Direction::Down => 8, + Direction::Left => 12, + }, }) .maybe_underground_type(e.get_maybe_underground_type_string()) .maybe_override_stack_size(e.get_maybe_override_stack_size())