Add multistation generation

This commit is contained in:
hal8174 2025-01-24 23:25:43 +01:00
parent 05f4edf83a
commit ce76626f79
7 changed files with 451 additions and 10 deletions

View file

@ -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()));
}

View file

@ -1,5 +1,6 @@
pub mod assembly;
pub mod balancer;
pub mod binary_merger;
pub mod multistation;
pub mod station;
pub mod train;

View file

@ -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
}

View file

@ -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
}