factorio_blueprint/src/blueprint/belt.rs

103 lines
3.1 KiB
Rust

use crate::{belt_finding::common::PathField, prelude::PositionType};
use super::{Blueprint, BlueprintEntity, BlueprintPosition};
pub enum Beltspeed {
Normal,
Fast,
Express,
Turbo,
}
impl Beltspeed {
pub 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()
}
pub 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 string_splitter(&self) -> String {
match self {
Beltspeed::Normal => "splitter",
Beltspeed::Fast => "fast-splitter",
Beltspeed::Express => "express-splitter",
Beltspeed::Turbo => "turbo-splitter",
}
.to_owned()
}
pub fn halve(&self) -> Self {
match self {
Beltspeed::Normal => Beltspeed::Normal,
Beltspeed::Fast => Beltspeed::Normal,
Beltspeed::Express => Beltspeed::Fast,
Beltspeed::Turbo => Beltspeed::Fast,
}
}
}
pub fn convert_belt_to_blueprint(
belt: &PathField,
speed: &Beltspeed,
nextfree: &mut u32,
) -> impl Iterator<Item = BlueprintEntity> {
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))
}