Refactor into different crates
This commit is contained in:
parent
94473c64e0
commit
dfdeae5638
82 changed files with 624 additions and 647 deletions
11
factorio-blueprint-generator/Cargo.toml
Normal file
11
factorio-blueprint-generator/Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "factorio-blueprint-generator"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
factorio-pathfinding = { path = "../factorio-pathfinding" }
|
||||
factorio-core = { path = "../factorio-core" }
|
||||
factorio-blueprint = { path = "../factorio-blueprint" }
|
||||
serde_json = "1.0.135"
|
||||
clap = { version = "4.5.26", features = ["derive"] }
|
||||
100
factorio-blueprint-generator/src/balancer.rs
Normal file
100
factorio-blueprint-generator/src/balancer.rs
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
use factorio_blueprint::{
|
||||
Blueprint, BlueprintEntity, BlueprintPosition, belt::convert_to_blueprint, misc::Beltspeed,
|
||||
};
|
||||
use factorio_core::{pathfield::PathField, prelude::*};
|
||||
|
||||
pub fn generate_4_lane_balancer() -> Blueprint {
|
||||
let mut e = vec![
|
||||
BlueprintEntity::builder("splitter".to_owned(), 1, BlueprintPosition::new(1.0, 0.5))
|
||||
.build(),
|
||||
BlueprintEntity::builder("splitter".to_owned(), 2, BlueprintPosition::new(3.0, 0.5))
|
||||
.build(),
|
||||
BlueprintEntity::builder("splitter".to_owned(), 3, BlueprintPosition::new(2.0, 1.5))
|
||||
.build(),
|
||||
BlueprintEntity::builder("splitter".to_owned(), 11, BlueprintPosition::new(2.0, 4.5))
|
||||
.build(),
|
||||
BlueprintEntity::builder("splitter".to_owned(), 20, BlueprintPosition::new(1.0, 7.5))
|
||||
.build(),
|
||||
BlueprintEntity::builder("splitter".to_owned(), 21, BlueprintPosition::new(3.0, 7.5))
|
||||
.build(),
|
||||
];
|
||||
|
||||
let mut nextfree = e.len() as u32;
|
||||
e.extend(convert_to_blueprint(
|
||||
&[
|
||||
PathField::Belt {
|
||||
pos: Position::new(0, 1),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(3, 1),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(0, 2),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(3, 2),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(0, 3),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(1, 3),
|
||||
dir: Direction::Left,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(2, 3),
|
||||
dir: Direction::Right,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(3, 3),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(0, 5),
|
||||
dir: Direction::Right,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(1, 5),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(2, 5),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(3, 5),
|
||||
dir: Direction::Left,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(0, 6),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Belt {
|
||||
pos: Position::new(3, 6),
|
||||
dir: Direction::Up,
|
||||
},
|
||||
PathField::Underground {
|
||||
pos: Position::new(1, 6),
|
||||
dir: Direction::Up,
|
||||
len: 4,
|
||||
},
|
||||
PathField::Underground {
|
||||
pos: Position::new(2, 6),
|
||||
dir: Direction::Up,
|
||||
len: 4,
|
||||
},
|
||||
],
|
||||
&Beltspeed::Normal,
|
||||
&mut nextfree,
|
||||
));
|
||||
|
||||
Blueprint::builder()
|
||||
.label("balancer".to_string())
|
||||
.entities(e)
|
||||
.build()
|
||||
}
|
||||
10
factorio-blueprint-generator/src/bin/balancer_blueprint.rs
Normal file
10
factorio-blueprint-generator/src/bin/balancer_blueprint.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use factorio_blueprint::{BlueprintString, encode};
|
||||
use factorio_blueprint_generator::balancer::generate_4_lane_balancer;
|
||||
|
||||
fn main() {
|
||||
let b = BlueprintString::Blueprint(generate_4_lane_balancer());
|
||||
|
||||
println!("{}", serde_json::to_string_pretty(&b).unwrap());
|
||||
|
||||
println!("{}", encode(&serde_json::to_string(&b).unwrap()));
|
||||
}
|
||||
129
factorio-blueprint-generator/src/bin/station.rs
Normal file
129
factorio-blueprint-generator/src/bin/station.rs
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
use factorio_blueprint::{
|
||||
BlueprintBook, BlueprintBookEntry, BlueprintString, encode,
|
||||
misc::{Beltspeed, Belttype},
|
||||
};
|
||||
use factorio_blueprint_generator::station::basic_unload_station;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Args {
|
||||
#[command(subcommand)]
|
||||
command: Command,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Command {
|
||||
Book,
|
||||
Single {
|
||||
locomotives: usize,
|
||||
length: usize,
|
||||
outputs: usize,
|
||||
beltspeed: Beltspeed,
|
||||
#[arg(default_value = "full")]
|
||||
belttype: Belttype,
|
||||
},
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
match args.command {
|
||||
Command::Book => {
|
||||
let mut b = Vec::new();
|
||||
|
||||
let layouts = [
|
||||
(1, 1),
|
||||
(1, 2),
|
||||
(1, 4),
|
||||
(2, 4),
|
||||
(1, 8),
|
||||
(2, 8),
|
||||
(3, 8),
|
||||
(4, 8),
|
||||
];
|
||||
|
||||
for (i, (locomotives, cargo)) in layouts.into_iter().enumerate() {
|
||||
let mut inner_b = Vec::new();
|
||||
|
||||
let mut j = 0;
|
||||
for beltspeed in [
|
||||
Beltspeed::Normal,
|
||||
Beltspeed::Fast,
|
||||
Beltspeed::Express,
|
||||
Beltspeed::Turbo,
|
||||
] {
|
||||
let belttypes: &[_] = match beltspeed {
|
||||
Beltspeed::Normal => &[Belttype::Full, Belttype::Left, Belttype::Right],
|
||||
_ => &[Belttype::Full],
|
||||
};
|
||||
for &belttype in belttypes {
|
||||
let mut inner_inner_b = Vec::new();
|
||||
|
||||
for (l, o) in (0..=(cargo as u32).ilog2()).enumerate() {
|
||||
let o = 1 << o;
|
||||
|
||||
let blueprint =
|
||||
basic_unload_station(locomotives, cargo, o, beltspeed, belttype);
|
||||
|
||||
inner_inner_b.push(BlueprintBookEntry::new(
|
||||
BlueprintString::Blueprint(blueprint),
|
||||
l as u32,
|
||||
));
|
||||
}
|
||||
|
||||
inner_b.push(BlueprintBookEntry::new(
|
||||
BlueprintString::BlueprintBook(
|
||||
BlueprintBook::builder()
|
||||
.blueprints(inner_inner_b)
|
||||
.active_index(0)
|
||||
.label(format!("{:?}-{:?}", beltspeed, belttype))
|
||||
.build(),
|
||||
),
|
||||
j,
|
||||
));
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
|
||||
b.push(BlueprintBookEntry::new(
|
||||
BlueprintString::BlueprintBook(
|
||||
BlueprintBook::builder()
|
||||
.blueprints(inner_b)
|
||||
.active_index(0)
|
||||
.label(format!("{locomotives}-{cargo}"))
|
||||
.build(),
|
||||
),
|
||||
i as u32,
|
||||
));
|
||||
}
|
||||
|
||||
let b = BlueprintString::BlueprintBook(
|
||||
BlueprintBook::builder()
|
||||
.blueprints(b)
|
||||
.active_index(0)
|
||||
.build(),
|
||||
);
|
||||
|
||||
println!("{}", encode(&serde_json::to_string(&b).unwrap()));
|
||||
}
|
||||
Command::Single {
|
||||
locomotives,
|
||||
length,
|
||||
outputs,
|
||||
beltspeed,
|
||||
belttype,
|
||||
} => {
|
||||
let b = BlueprintString::Blueprint(basic_unload_station(
|
||||
locomotives,
|
||||
length,
|
||||
outputs,
|
||||
beltspeed,
|
||||
belttype,
|
||||
));
|
||||
|
||||
// println!("{}", serde_json::to_string_pretty(&b).unwrap());
|
||||
|
||||
println!("{}", encode(&serde_json::to_string(&b).unwrap()));
|
||||
}
|
||||
}
|
||||
}
|
||||
41
factorio-blueprint-generator/src/bin/train_blueprint.rs
Normal file
41
factorio-blueprint-generator/src/bin/train_blueprint.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use factorio_blueprint::{BlueprintBook, BlueprintBookEntry, BlueprintString, encode};
|
||||
use factorio_blueprint_generator::train::generate_train;
|
||||
|
||||
fn main() {
|
||||
let layouts = [
|
||||
(1, 1),
|
||||
(1, 2),
|
||||
(1, 4),
|
||||
(2, 4),
|
||||
(1, 8),
|
||||
(2, 8),
|
||||
(3, 8),
|
||||
(4, 8),
|
||||
];
|
||||
|
||||
let mut b = Vec::new();
|
||||
|
||||
for (i, (locomotives, wagons)) in layouts.into_iter().enumerate() {
|
||||
b.push(BlueprintBookEntry::new(
|
||||
BlueprintString::Blueprint(generate_train(locomotives, wagons, true, false)),
|
||||
i as u32 * 2,
|
||||
));
|
||||
b.push(BlueprintBookEntry::new(
|
||||
BlueprintString::Blueprint(generate_train(locomotives, wagons, true, true)),
|
||||
i as u32 * 2 + 1,
|
||||
));
|
||||
}
|
||||
|
||||
let b = BlueprintString::BlueprintBook(
|
||||
BlueprintBook::builder()
|
||||
.blueprints(b)
|
||||
.active_index(0)
|
||||
.build(),
|
||||
);
|
||||
|
||||
// let b = BlueprintString::Blueprint(generate_train(1, 2, false, false));
|
||||
|
||||
println!("{}", serde_json::to_string_pretty(&b).unwrap());
|
||||
|
||||
println!("{}", encode(&serde_json::to_string(&b).unwrap()));
|
||||
}
|
||||
3
factorio-blueprint-generator/src/lib.rs
Normal file
3
factorio-blueprint-generator/src/lib.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod balancer;
|
||||
pub mod station;
|
||||
pub mod train;
|
||||
535
factorio-blueprint-generator/src/station.rs
Normal file
535
factorio-blueprint-generator/src/station.rs
Normal file
|
|
@ -0,0 +1,535 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use factorio_blueprint::{
|
||||
Blueprint, BlueprintEntity, BlueprintPosition,
|
||||
misc::{Beltspeed, Belttype},
|
||||
};
|
||||
|
||||
pub fn station_unload(
|
||||
length: usize,
|
||||
locomotives: usize,
|
||||
unloader: &Vec<BlueprintEntity>,
|
||||
beltspeed: Beltspeed,
|
||||
outputs: usize,
|
||||
output_x: f64,
|
||||
output_y: f64,
|
||||
) -> Blueprint {
|
||||
let section_size = length / outputs;
|
||||
assert!(length % outputs == 0);
|
||||
assert!(section_size.is_power_of_two());
|
||||
assert!(outputs <= length);
|
||||
|
||||
let mut e = Vec::new();
|
||||
|
||||
let global_x_offset = locomotives * 7;
|
||||
|
||||
// electric poles
|
||||
for l in 1..=(length + locomotives) {
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
"medium-electric-pole".to_owned(),
|
||||
l as u32 - 1,
|
||||
BlueprintPosition::new((7 * l) as f64 + 0.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
// unloader
|
||||
let offset = e.len();
|
||||
for l in 0..length {
|
||||
e.extend(unloader.iter().cloned().map(|mut e| {
|
||||
e.position.x += (7 * l + global_x_offset) as f64;
|
||||
e.entity_number += (offset + unloader.len() * l) as u32;
|
||||
e
|
||||
}));
|
||||
}
|
||||
|
||||
// train stop
|
||||
let offset = e.len();
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
"train-stop".to_owned(),
|
||||
offset as u32,
|
||||
BlueprintPosition::new(1.0, -1.0),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
);
|
||||
|
||||
// rails
|
||||
for l in 0..((length * 7 + global_x_offset + 1) / 2) {
|
||||
let offset = e.len() as u32;
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
"straight-rail".to_owned(),
|
||||
offset,
|
||||
BlueprintPosition::new(2.0 * l as f64 + 1.0, 1.0),
|
||||
)
|
||||
.direction(4)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
// output and merging
|
||||
for o in 0..outputs {
|
||||
// stubs
|
||||
let stubspeed = beltspeed.halvings(section_size.ilog2() as usize);
|
||||
for i in 0..section_size {
|
||||
let depth = o + i.count_ones() as usize;
|
||||
|
||||
for j in 0..depth {
|
||||
let offset = e.len() as u32;
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
stubspeed.string(),
|
||||
offset,
|
||||
BlueprintPosition::new(
|
||||
(7 * (i + o * section_size) + global_x_offset) as f64 + output_x,
|
||||
output_y - j as f64,
|
||||
),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
let offset = e.len() as u32;
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
stubspeed.string(),
|
||||
offset,
|
||||
BlueprintPosition::new(
|
||||
(7 * (i + o * section_size) + global_x_offset) as f64 + output_x,
|
||||
output_y - depth as f64,
|
||||
),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
// merger
|
||||
for i in 1..(section_size.ilog2() as usize + 1) {
|
||||
let p = 1 << i;
|
||||
let mergespeed = beltspeed.halvings((section_size.ilog2() as usize) - i);
|
||||
for j in 0..(section_size / p) {
|
||||
let depth = o + j.count_ones() as usize;
|
||||
let offset = e.len() as u32;
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
mergespeed.string_splitter(),
|
||||
offset,
|
||||
BlueprintPosition::new(
|
||||
(7 * (j * p + o * section_size) + global_x_offset) as f64 - i as f64
|
||||
+ output_x,
|
||||
output_y - i as f64 + 0.5 - depth as f64,
|
||||
),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
);
|
||||
e.extend((0..(7 * p / 2)).map(|l| {
|
||||
BlueprintEntity::builder(
|
||||
mergespeed.halve().string(),
|
||||
offset + 1 + l as u32,
|
||||
BlueprintPosition::new(
|
||||
(7 * (j * p + o * section_size) + global_x_offset) as f64 - i as f64
|
||||
+ output_x
|
||||
+ l as f64
|
||||
+ 1.0,
|
||||
output_y - i as f64 - depth as f64,
|
||||
),
|
||||
)
|
||||
.direction(12)
|
||||
.build()
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
// connect
|
||||
let offset = e.len() as u32;
|
||||
let step = o + section_size.ilog2() as usize;
|
||||
e.extend((0..(7 * o * section_size)).map(|l| {
|
||||
BlueprintEntity::builder(
|
||||
beltspeed.string(),
|
||||
offset + l as u32,
|
||||
BlueprintPosition::new(
|
||||
(l + global_x_offset) as f64 + output_x - section_size.ilog2() as f64,
|
||||
output_y - step as f64,
|
||||
),
|
||||
)
|
||||
.direction(12)
|
||||
.build()
|
||||
}));
|
||||
}
|
||||
|
||||
Blueprint::builder()
|
||||
.label("station".to_owned())
|
||||
.entities(e)
|
||||
.wires(
|
||||
(0..((length + locomotives - 1) as u32))
|
||||
.map(|i| [i, 5, i + 1, 5])
|
||||
.collect(),
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
pub fn unloader(beltspeed: Beltspeed, belttype: Belttype) -> (Vec<BlueprintEntity>, f64) {
|
||||
match beltspeed {
|
||||
Beltspeed::Normal => {
|
||||
let mut e = vec![
|
||||
BlueprintEntity::builder(
|
||||
"fast-transport-belt".to_owned(),
|
||||
0,
|
||||
BlueprintPosition::new(3.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
];
|
||||
|
||||
if belttype.contains_left() {
|
||||
let offset = e.len() as u32;
|
||||
e.extend_from_slice(&[
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset,
|
||||
BlueprintPosition::new(5.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 1,
|
||||
BlueprintPosition::new(5.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-inserter".to_owned(),
|
||||
offset + 2,
|
||||
BlueprintPosition::new(4.5, -1.5),
|
||||
)
|
||||
.direction(4)
|
||||
.build(),
|
||||
]);
|
||||
}
|
||||
|
||||
if belttype.contains_right() {
|
||||
let offset = e.len() as u32;
|
||||
e.extend_from_slice(&[
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset,
|
||||
BlueprintPosition::new(1.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 1,
|
||||
BlueprintPosition::new(1.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-inserter".to_owned(),
|
||||
offset + 2,
|
||||
BlueprintPosition::new(2.5, -1.5),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
]);
|
||||
}
|
||||
|
||||
(e, -2.5)
|
||||
}
|
||||
Beltspeed::Fast => {
|
||||
let mut e = vec![
|
||||
BlueprintEntity::builder(
|
||||
"fast-transport-belt".to_owned(),
|
||||
0,
|
||||
BlueprintPosition::new(3.5, -3.5),
|
||||
)
|
||||
.build(),
|
||||
];
|
||||
|
||||
if belttype.contains_left() {
|
||||
let offset = e.len() as u32;
|
||||
e.extend_from_slice(&[
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset,
|
||||
BlueprintPosition::new(1.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 1,
|
||||
BlueprintPosition::new(1.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-inserter".to_owned(),
|
||||
offset + 2,
|
||||
BlueprintPosition::new(1.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-transport-belt".to_owned(),
|
||||
offset + 3,
|
||||
BlueprintPosition::new(1.5, -3.5),
|
||||
)
|
||||
.direction(4)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset + 4,
|
||||
BlueprintPosition::new(2.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 5,
|
||||
BlueprintPosition::new(2.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-inserter".to_owned(),
|
||||
offset + 6,
|
||||
BlueprintPosition::new(2.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-transport-belt".to_owned(),
|
||||
offset + 7,
|
||||
BlueprintPosition::new(2.5, -3.5),
|
||||
)
|
||||
.direction(4)
|
||||
.build(),
|
||||
]);
|
||||
}
|
||||
|
||||
if belttype.contains_right() {
|
||||
let offset = e.len() as u32;
|
||||
e.extend_from_slice(&[
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset,
|
||||
BlueprintPosition::new(4.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 1,
|
||||
BlueprintPosition::new(4.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-inserter".to_owned(),
|
||||
offset + 2,
|
||||
BlueprintPosition::new(4.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-transport-belt".to_owned(),
|
||||
offset + 3,
|
||||
BlueprintPosition::new(4.5, -3.5),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset + 4,
|
||||
BlueprintPosition::new(5.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 5,
|
||||
BlueprintPosition::new(5.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-inserter".to_owned(),
|
||||
offset + 6,
|
||||
BlueprintPosition::new(5.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"fast-transport-belt".to_owned(),
|
||||
offset + 7,
|
||||
BlueprintPosition::new(5.5, -3.5),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
]);
|
||||
}
|
||||
|
||||
(e, -4.5)
|
||||
}
|
||||
Beltspeed::Express => {
|
||||
let mut e = vec![
|
||||
BlueprintEntity::builder(
|
||||
"express-transport-belt".to_owned(),
|
||||
0,
|
||||
BlueprintPosition::new(3.5, -3.5),
|
||||
)
|
||||
.build(),
|
||||
];
|
||||
|
||||
if belttype.contains_left() {
|
||||
let offset = e.len() as u32;
|
||||
e.extend_from_slice(&[
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset,
|
||||
BlueprintPosition::new(1.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 1,
|
||||
BlueprintPosition::new(1.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 2,
|
||||
BlueprintPosition::new(1.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.override_stack_size(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"express-transport-belt".to_owned(),
|
||||
offset + 3,
|
||||
BlueprintPosition::new(1.5, -3.5),
|
||||
)
|
||||
.direction(4)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset + 4,
|
||||
BlueprintPosition::new(2.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 5,
|
||||
BlueprintPosition::new(2.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 6,
|
||||
BlueprintPosition::new(2.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.override_stack_size(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"express-transport-belt".to_owned(),
|
||||
offset + 7,
|
||||
BlueprintPosition::new(2.5, -3.5),
|
||||
)
|
||||
.direction(4)
|
||||
.build(),
|
||||
]);
|
||||
}
|
||||
|
||||
if belttype.contains_right() {
|
||||
let offset = e.len() as u32;
|
||||
e.extend_from_slice(&[
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset,
|
||||
BlueprintPosition::new(4.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 1,
|
||||
BlueprintPosition::new(4.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 2,
|
||||
BlueprintPosition::new(4.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.override_stack_size(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"express-transport-belt".to_owned(),
|
||||
offset + 3,
|
||||
BlueprintPosition::new(4.5, -3.5),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"steel-chest".to_owned(),
|
||||
offset + 4,
|
||||
BlueprintPosition::new(5.5, -1.5),
|
||||
)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 5,
|
||||
BlueprintPosition::new(5.5, -0.5),
|
||||
)
|
||||
.direction(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"bulk-inserter".to_owned(),
|
||||
offset + 6,
|
||||
BlueprintPosition::new(5.5, -2.5),
|
||||
)
|
||||
.direction(8)
|
||||
.override_stack_size(8)
|
||||
.build(),
|
||||
BlueprintEntity::builder(
|
||||
"express-transport-belt".to_owned(),
|
||||
offset + 7,
|
||||
BlueprintPosition::new(5.5, -3.5),
|
||||
)
|
||||
.direction(12)
|
||||
.build(),
|
||||
]);
|
||||
}
|
||||
|
||||
(e, -4.5)
|
||||
}
|
||||
Beltspeed::Turbo => (Vec::new(), -4.5),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn basic_unload_station(
|
||||
locomotives: usize,
|
||||
length: usize,
|
||||
outputs: usize,
|
||||
beltspeed: Beltspeed,
|
||||
belttype: Belttype,
|
||||
) -> Blueprint {
|
||||
let (unloader, output_y) = unloader(
|
||||
beltspeed.halvings((length / outputs).ilog2() as usize),
|
||||
belttype,
|
||||
);
|
||||
station_unload(
|
||||
length,
|
||||
locomotives,
|
||||
&unloader,
|
||||
beltspeed,
|
||||
outputs,
|
||||
3.5,
|
||||
output_y,
|
||||
)
|
||||
}
|
||||
208
factorio-blueprint-generator/src/train.rs
Normal file
208
factorio-blueprint-generator/src/train.rs
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
use factorio_blueprint::{
|
||||
Blueprint, BlueprintEntity, BlueprintInventoryLocation, BlueprintItemID, BlueprintItemRequest,
|
||||
BlueprintPosition, BlueprintStockConnection,
|
||||
};
|
||||
|
||||
pub fn generate_train(locomotives: u32, wagons: u32, rails: bool, fluid: bool) -> Blueprint {
|
||||
let mut e = Vec::new();
|
||||
|
||||
for i in 0..locomotives {
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
"locomotive".to_owned(),
|
||||
i + 1,
|
||||
BlueprintPosition::new(1.0, 3.5 + 7.0 * i as f64),
|
||||
)
|
||||
.items(vec![BlueprintItemRequest::new(
|
||||
BlueprintItemID::builder("rocket-fuel".to_owned()).build(),
|
||||
vec![BlueprintInventoryLocation::new(5, 1, 0)],
|
||||
)])
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
for i in 0..wagons {
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
if fluid {
|
||||
"fluid-wagon".to_owned()
|
||||
} else {
|
||||
"cargo-wagon".to_owned()
|
||||
},
|
||||
locomotives + i + 1,
|
||||
BlueprintPosition::new(1.0, 3.5 + 7.0 * (locomotives + i) as f64),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
if rails {
|
||||
for i in 0..((locomotives + wagons) * 7).div_ceil(2) {
|
||||
e.push(
|
||||
BlueprintEntity::builder(
|
||||
"straight-rail".to_owned(),
|
||||
locomotives + wagons + i + 1,
|
||||
BlueprintPosition::new(1.0, 1.0 + 2.0 * i as f64),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mut stock_connections = Vec::new();
|
||||
|
||||
if locomotives + wagons > 1 {
|
||||
stock_connections.push(BlueprintStockConnection::builder(1).back(2).build());
|
||||
for i in 2..=(locomotives + wagons - 1) {
|
||||
stock_connections.push(
|
||||
BlueprintStockConnection::builder(i)
|
||||
.front(i - 1)
|
||||
.back(i + 1)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
stock_connections.push(
|
||||
BlueprintStockConnection::builder(locomotives + wagons)
|
||||
.front(locomotives + wagons - 1)
|
||||
.build(),
|
||||
);
|
||||
}
|
||||
|
||||
let cargotype = if fluid {
|
||||
"[virtual-signal=signal-L]"
|
||||
} else {
|
||||
"[virtual-signal=signal-C]"
|
||||
};
|
||||
|
||||
let mut fuel_conditions = Vec::new();
|
||||
|
||||
fuel_conditions.push(serde_json::json!({
|
||||
"compare_type": "and",
|
||||
"station": format!("[virtual-signal=signal-F][virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}]"),
|
||||
"type": "specific_destination_not_full"
|
||||
}));
|
||||
|
||||
for (fuel, min) in [
|
||||
("coal", 50),
|
||||
("solid-fuel", 50),
|
||||
("rocket-fuel", 20),
|
||||
("nuclear-fuel", 1),
|
||||
] {
|
||||
fuel_conditions.push(serde_json::json!({
|
||||
"compare_type": "and",
|
||||
"type": "fuel_item_count_any",
|
||||
"condition": {
|
||||
"comparator": "≤",
|
||||
"constant": min,
|
||||
"first_signal": {
|
||||
"name": fuel,
|
||||
},
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
let schedule = serde_json::json!({
|
||||
"locomotives": [1],
|
||||
"schedule": {
|
||||
"group": format!("{cargotype}[virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}]"),
|
||||
"interrupts": [
|
||||
{
|
||||
"name": format!("[virtual-signal=signal-F][virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}]"),
|
||||
"conditions": fuel_conditions,
|
||||
"inside_interrupt": true,
|
||||
"targets": [
|
||||
{
|
||||
"station": format!("[virtual-signal=signal-F][virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}]"),
|
||||
"wait_conditions": [
|
||||
{
|
||||
"compare_type": "and",
|
||||
"ticks": 120,
|
||||
"type": "inactivity"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": format!("{cargotype}[virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}][virtual-signal=signal-L]"),
|
||||
"conditions": [
|
||||
{
|
||||
"compare_type": "and",
|
||||
"station": format!("{cargotype}[virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}][virtual-signal=signal-L]"),
|
||||
"type": "specific_destination_not_full"
|
||||
},
|
||||
{
|
||||
"compare_type": "and",
|
||||
"type": "empty"
|
||||
}
|
||||
],
|
||||
"inside_interrupt": false,
|
||||
"targets": [
|
||||
{
|
||||
"station": format!("{cargotype}[virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}][virtual-signal=signal-L]"),
|
||||
"wait_conditions": [
|
||||
{
|
||||
"compare_type": "and",
|
||||
"type": "full"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": format!("{cargotype}[virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}][virtual-signal=signal-U]"),
|
||||
"conditions": [
|
||||
{
|
||||
"type": "item_count",
|
||||
"compare_type": "and",
|
||||
"condition": {
|
||||
"comparator": "≠",
|
||||
"constant": 0,
|
||||
"first_signal": {
|
||||
"name": "signal-item-parameter",
|
||||
"type": "virtual",
|
||||
}
|
||||
},
|
||||
}
|
||||
],
|
||||
"targets": [
|
||||
{
|
||||
"station": format!("{cargotype}[virtual-signal=signal-{locomotives}][virtual-signal=signal-{wagons}][virtual-signal=signal-U][virtual-signal=signal-item-parameter]"),
|
||||
"wait_conditions": [
|
||||
{
|
||||
"type": "item_count",
|
||||
"compare_type": "and",
|
||||
"condition": {
|
||||
"comparator": "=",
|
||||
"constant": 0,
|
||||
"first_signal": {
|
||||
"name": "signal-item-parameter",
|
||||
"type": "virtual"
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"records": [
|
||||
{
|
||||
"station": format!("[virtual-signal=signal-D][virtual-signal=signal-{}]", locomotives + wagons),
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
Blueprint::builder()
|
||||
.label("train".to_string())
|
||||
.entities(e)
|
||||
.stock_connections(stock_connections)
|
||||
.schedules(vec![schedule])
|
||||
.snap_to_grid(BlueprintPosition::new(
|
||||
4.0,
|
||||
2.0 * ((locomotives + wagons) * 7).div_ceil(2) as f64,
|
||||
))
|
||||
.build()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue