factorio_blueprint/factorio-blueprint/src/belt.rs
2025-01-18 22:47:48 +01:00

57 lines
1.9 KiB
Rust

use factorio_core::{beltoptions::Beltspeed, pathfield::PathField, prelude::*};
use super::{BlueprintEntity, BlueprintPosition};
pub fn convert_belt_to_blueprint(
belt: &PathField,
speed: &Beltspeed,
nextfree: &mut u32,
) -> impl Iterator<Item = BlueprintEntity> + use<> {
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<Item = BlueprintEntity> + use<'p, 's, 'n> {
path.iter()
.flat_map(|b| convert_belt_to_blueprint(b, speed, nextfree))
}