Fix station type for multistation

This commit is contained in:
hal8174 2025-05-11 16:57:30 +02:00
parent d26449195b
commit 0f6c96f737
3 changed files with 90 additions and 36 deletions

View file

@ -75,12 +75,16 @@ fn main() {
s.contains('s'), s.contains('s'),
) )
} }
"e" => StationSpec::new_empty( "e" => {
let (left, right) = s.split_once('-').expect("parsing space");
StationSpec::new_empty(
locomotives.parse().expect("parsing locomotive count"), locomotives.parse().expect("parsing locomotive count"),
wagons.parse().expect("parsing wagon count"), wagons.parse().expect("parsing wagon count"),
format!("test"), format!("test"),
s.parse().expect("Unable to parse space"), left.parse().expect("Unable to parse left space"),
), right.parse().expect("Unable to parse right space"),
)
}
_ => panic!("unknown station type"), _ => panic!("unknown station type"),
} }
}) })

View file

@ -7,7 +7,7 @@ use factorio_blueprint::abstraction::{
}; };
use factorio_core::{beltoptions::Beltspeed, prelude::*}; use factorio_core::{beltoptions::Beltspeed, prelude::*};
fn calculate_station_height( pub fn calculate_station_height(
output_height: PositionType, output_height: PositionType,
lanes: usize, lanes: usize,
beltspeed: Beltspeed, beltspeed: Beltspeed,
@ -189,10 +189,10 @@ pub fn multistation(
blueprint.add_entity(Entity::new_rail( blueprint.add_entity(Entity::new_rail(
RailType::RailSignal, RailType::RailSignal,
Position::new( Position::new(
inrail_x + 3, inrail_x + 5,
total_stacker_height + 17 + previous_station_heights, total_stacker_height + 21 + previous_station_heights,
), ),
15, 14,
)); ));
// out turn // out turn
@ -231,10 +231,10 @@ pub fn multistation(
blueprint.add_entity(Entity::new_rail( blueprint.add_entity(Entity::new_rail(
RailType::RailSignal, RailType::RailSignal,
Position::new( Position::new(
outrail_x - 3, outrail_x - 5,
total_stacker_height + 17 + previous_station_heights, total_stacker_height + 21 + previous_station_heights,
), ),
9, 10,
)); ));
for j in 0..((7 * longest_train).div_ceil(2) for j in 0..((7 * longest_train).div_ceil(2)
@ -266,16 +266,25 @@ pub fn multistation(
blueprint.add_blueprint(b); blueprint.add_blueprint(b);
let station_height = match &station_spec.station_type { let mut station_height = 4
crate::station::StationType::Empty(station_type_empty) => 4 + station_type_empty.space, + station_spec.get_space_right()
+ (stations.get(i + 1).map(|s| s.get_space_left()).unwrap_or(0));
// ensure station_height is divisible by 4
station_height = (station_height + 3) & !0b11;
match &station_spec.station_type {
crate::station::StationType::Empty(_station_type_empty) => (),
crate::station::StationType::Belt(station_type_belt) => { crate::station::StationType::Belt(station_type_belt) => {
// belt output // belt output
let station_height = calculate_station_height( let old_station_height = calculate_station_height(
output_height, output_height,
station_type_belt.lanes, station_type_belt.lanes,
station_type_belt.beltspeed, station_type_belt.beltspeed,
); );
station_height = station_height.max(old_station_height);
output_heights output_heights
.push(30 + total_stacker_height + previous_station_heights + output_height); .push(30 + total_stacker_height + previous_station_heights + output_height);
@ -566,7 +575,6 @@ pub fn multistation(
)); ));
} }
} }
station_height
} }
}; };
@ -591,6 +599,7 @@ pub fn multistation(
)); ));
} }
if station_height > 8 {
blueprint.add_entity(Entity::new_rail( blueprint.add_entity(Entity::new_rail(
RailType::ChainSignal, RailType::ChainSignal,
Position::new( Position::new(
@ -608,6 +617,7 @@ pub fn multistation(
8, 8,
)); ));
} }
}
previous_station_heights += station_height; previous_station_heights += station_height;
} }

View file

@ -29,7 +29,8 @@ pub enum StationType {
#[derive(Debug)] #[derive(Debug)]
pub struct StationTypeEmpty { pub struct StationTypeEmpty {
pub space: PositionType, pub space_left: PositionType,
pub space_right: PositionType,
} }
#[derive(Debug)] #[derive(Debug)]
@ -101,15 +102,54 @@ impl StationSpec {
locomotives: usize, locomotives: usize,
wagons: usize, wagons: usize,
station_name: String, station_name: String,
space: PositionType, space_left: PositionType,
space_right: PositionType,
) -> Self { ) -> Self {
Self::new( Self::new(
locomotives, locomotives,
wagons, wagons,
station_name, station_name,
StationType::Empty(StationTypeEmpty { space }), StationType::Empty(StationTypeEmpty {
space_left,
space_right,
}),
) )
} }
pub fn get_space_left(&self) -> PositionType {
match &self.station_type {
StationType::Empty(station_type_empty) => station_type_empty.space_left,
StationType::Belt(_station_type_belt) => 0,
}
}
pub fn get_space_right(&self) -> PositionType {
match &self.station_type {
StationType::Empty(station_type_empty) => station_type_empty.space_right,
StationType::Belt(station_type_belt) => {
let mut r = 2 * station_type_belt.lanes as PositionType;
r += 2
* (self.train_information.wagons / station_type_belt.lanes).ilog2()
as PositionType;
r += if station_type_belt.load {
10
} else {
if station_type_belt.beltspeed.halvings(
(self.train_information.wagons / station_type_belt.lanes).ilog2() as usize,
) == Beltspeed::Normal
{
4
} else {
8
}
};
r
}
}
}
} }
impl Default for StationBasicSpec { impl Default for StationBasicSpec {