Add factory generator

This commit is contained in:
hal8174 2025-01-31 23:37:45 +01:00
parent 295490858b
commit 6fbff67e61
14 changed files with 308 additions and 15 deletions

View file

@ -5,7 +5,9 @@ edition = "2024"
[dependencies]
factorio-pathfinding = { path = "../factorio-pathfinding" }
factorio-layout = { path = "../factorio-layout" }
factorio-core = { path = "../factorio-core" }
factorio-blueprint = { path = "../factorio-blueprint" }
serde_json = "1.0.135"
clap = { version = "4.5.26", features = ["derive"] }
rand = { version = "0.8.5", features = ["small_rng"] }

View file

@ -28,7 +28,7 @@ pub fn assembly_line(assembly_machines: usize) -> Blueprint {
for i in 0..assembly_machines {
blueprint.add_entity(Entity::new_production(
"assembling-machine-3",
"iron-gears",
"iron-gear-wheel",
Position::new(3 + 6 * i as PositionType, 9),
Direction::Up,
Position::new(6, 6),

View file

@ -0,0 +1,39 @@
use clap::Parser;
use factorio_blueprint::{BlueprintString, encode};
use factorio_blueprint_generator::factory::generate_factory;
use factorio_core::prelude::*;
use factorio_layout::valid_layout::ValidLayout;
use factorio_pathfinding::belt_finding::ConflictAvoidance;
use rand::{SeedableRng, rngs::SmallRng};
#[derive(Parser)]
struct Args {
#[arg(default_value_t = 0)]
seed: u64,
#[arg(short, long)]
json: bool,
}
fn main() {
let args = Args::parse();
let l = ValidLayout {
max_tries: 10,
retries: 10,
start_size: Position::new(100, 100),
growth: Position::new(5, 5),
};
let p = ConflictAvoidance {
timeout: Some(std::time::Duration::from_millis(20)),
};
let mut rng = SmallRng::seed_from_u64(args.seed);
let b = BlueprintString::Blueprint(generate_factory(&l, &p, &mut rng).to_blueprint());
if args.json {
println!("{}", serde_json::to_string_pretty(&b).unwrap());
}
println!("{}", encode(&serde_json::to_string(&b).unwrap()));
}

View file

@ -0,0 +1,122 @@
use factorio_blueprint::abstraction::Blueprint;
use factorio_core::{beltoptions::Beltspeed, prelude::*};
use factorio_layout::{Connection, Interface, LayoutInput, Layouter, MacroBlock};
use factorio_pathfinding::Pathfinder;
use rand::Rng;
use crate::assembly::assembly_line;
pub fn generate_factory<L: Layouter, P: Pathfinder>(
layouter: &L,
pathfinder: &P,
rng: &mut impl Rng,
) -> Blueprint {
// 2 * gears
// 3 * copper wires
// 2 * green circuits
// 2 * inserter
let input = vec![
Interface {
offset: Position::new(0, 1),
dir: Direction::Right,
},
Interface {
offset: Position::new(0, 7),
dir: Direction::Right,
},
Interface {
offset: Position::new(0, 8),
dir: Direction::Right,
},
];
let output = vec![Interface {
offset: Position::new(0, 0),
dir: Direction::Left,
}];
let blocks = vec![
MacroBlock {
size: Position::new(6, 9),
input: input.clone(),
output: output.clone(),
},
MacroBlock {
size: Position::new(9, 9),
input: input.clone(),
output: output.clone(),
},
MacroBlock {
size: Position::new(6, 9),
input: input.clone(),
output: output.clone(),
},
MacroBlock {
size: Position::new(6, 9),
input: input.clone(),
output: output.clone(),
},
];
let connections = vec![
Connection {
startblock: 0,
startpoint: 0,
endblock: 3,
endpoint: 0,
lanes: 1,
beltspeed: Beltspeed::Fast,
},
Connection {
startblock: 1,
startpoint: 0,
endblock: 2,
endpoint: 0,
lanes: 1,
beltspeed: Beltspeed::Fast,
},
Connection {
startblock: 2,
startpoint: 0,
endblock: 3,
endpoint: 1,
lanes: 1,
beltspeed: Beltspeed::Fast,
},
];
let l = layouter
.layout(
&LayoutInput {
blocks,
connections,
},
pathfinder,
rng,
)
.unwrap();
let blueprints = vec![
assembly_line(2),
assembly_line(3),
assembly_line(2),
assembly_line(2),
];
let mut b = Blueprint::new();
for (block, mut assembly_blueprint) in l.positions.iter().zip(blueprints) {
let offset = match block.dir() {
Direction::Up => Position::new(0, 0),
Direction::Right => Position::new(2, 0),
Direction::Down => Position::new(2, 2),
Direction::Left => Position::new(0, 2),
};
assembly_blueprint.transform(Transformation::new(block.dir(), offset + 2 * block.pos()));
b.add_blueprint(assembly_blueprint);
}
for path in &l.path_result {
b.add_path(&path[1..], Beltspeed::Fast);
}
b
}

View file

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