Set correct beltspeed for input and output of factory block

This commit is contained in:
hal8174 2025-02-08 23:22:24 +01:00
parent e6e5001334
commit 84d00238ab
2 changed files with 61 additions and 27 deletions

View file

@ -24,7 +24,7 @@ fn main() {
let text = std::fs::File::open(&args.path).unwrap(); let text = std::fs::File::open(&args.path).unwrap();
let factory_graph: FactoryGraph = serde_yaml::from_reader(text).unwrap(); let factory_graph: FactoryGraph = serde_yaml::from_reader(text).unwrap();
dbg!(&factory_graph); // dbg!(&factory_graph);
let l = ValidLayout { let l = ValidLayout {
max_tries: 4, max_tries: 4,
@ -36,8 +36,8 @@ fn main() {
let l = GeneticAlgorithm { let l = GeneticAlgorithm {
mutation_retries: 20, mutation_retries: 20,
population_size: 40, population_size: 40,
population_keep: 5, population_keep: 8,
population_new: 5, population_new: 2,
generations: 80, generations: 80,
valid_layout: l, valid_layout: l,
}; };

View file

@ -1,3 +1,5 @@
use std::collections::HashMap;
use factorio_blueprint::abstraction::{Blueprint, Entity}; use factorio_blueprint::abstraction::{Blueprint, Entity};
use factorio_core::{beltoptions::Beltspeed, prelude::*, visualize::Visualize}; use factorio_core::{beltoptions::Beltspeed, prelude::*, visualize::Visualize};
use factorio_layout::{Connection, Interface, LayoutInput, Layouter, MacroBlock}; use factorio_layout::{Connection, Interface, LayoutInput, Layouter, MacroBlock};
@ -45,9 +47,23 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
let mut blocks = Vec::new(); let mut blocks = Vec::new();
let mut blueprints = Vec::new(); let mut blueprints = Vec::new();
let mut connections = Vec::new(); let mut connections = Vec::new();
let mut used_connections = Vec::new(); let mut intermediate_connections = Vec::new();
for (i, b) in factory_graph.subfactories.iter().enumerate() { for (i, b) in factory_graph.subfactories.iter().enumerate() {
let mut input_connections = factory_graph
.factory_connections
.iter()
.enumerate()
.filter(|&(_, c)| c.to == i)
.collect::<Vec<_>>();
let output_connections = factory_graph
.factory_connections
.iter()
.enumerate()
.filter(|&(_, c)| c.from == i)
.collect::<Vec<_>>();
match b { match b {
Building::SubFactory { Building::SubFactory {
recipe, recipe,
@ -55,23 +71,28 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
machine, machine,
} => { } => {
assert!(machine == "assembly-machine-3"); assert!(machine == "assembly-machine-3");
input_connections.sort_by(|a, b| b.1.amount.total_cmp(&a.1.amount));
let input_count = factory_graph
.factory_connections
.iter()
.filter(|&c| c.to == i)
.count();
let (b, size, y_output, y_inputs) = assembly_line_2_input( let (b, size, y_output, y_inputs) = assembly_line_2_input(
*machines, *machines,
recipe, recipe,
Beltspeed::Express, Beltspeed::from_items_per_second(
if input_count == 2 { input_connections
Some(Beltspeed::Express) .first()
.map(|&(_, c)| c.amount)
.unwrap_or(1.0),
),
if let Some(&(_, c)) = input_connections.get(1) {
Some(Beltspeed::from_items_per_second(c.amount))
} else { } else {
None None
}, },
Beltspeed::Express, Beltspeed::from_items_per_second(
output_connections
.first()
.map(|&(_, c)| c.amount)
.unwrap_or(1.0),
),
); );
blueprints.push(b); blueprints.push(b);
@ -89,8 +110,6 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
dir: Direction::Left, dir: Direction::Left,
}], }],
}); });
used_connections.push((0, 0));
} }
Building::ExternalConnection { inputs, outputs } => { Building::ExternalConnection { inputs, outputs } => {
blocks.push(MacroBlock { blocks.push(MacroBlock {
@ -110,12 +129,16 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
}); });
blueprints.push(Blueprint::new()); blueprints.push(Blueprint::new());
used_connections.push((0, 0));
} }
Building::SideLoader => { Building::SideLoader => {
let mut blueprint = Blueprint::new(); let mut blueprint = Blueprint::new();
blueprint.add_entity(Entity::new_belt( blueprint.add_entity(Entity::new_belt(
Beltspeed::Fast, Beltspeed::from_items_per_second(
output_connections
.first()
.map(|&(_, c)| c.amount)
.unwrap_or(1.0),
),
Position::new(1, 1), Position::new(1, 1),
Direction::Up, Direction::Up,
)); ));
@ -138,12 +161,13 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
}], }],
}); });
blueprints.push(blueprint); blueprints.push(blueprint);
used_connections.push((0, 0));
} }
Building::Splitter => { Building::Splitter => {
let mut blueprint = Blueprint::new(); let mut blueprint = Blueprint::new();
blueprint.add_entity(Entity::new_splitter( blueprint.add_entity(Entity::new_splitter(
Beltspeed::Fast, Beltspeed::from_items_per_second(
input_connections.iter().map(|&(_, c)| c.amount).sum(),
),
Position::new(2, 1), Position::new(2, 1),
Direction::Up, Direction::Up,
)); ));
@ -172,23 +196,33 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
], ],
}); });
blueprints.push(blueprint); blueprints.push(blueprint);
used_connections.push((0, 0));
} }
} }
let input_connection_map = input_connections
.iter()
.enumerate()
.map(|(port, &(connection_id, _))| (connection_id, port))
.collect::<HashMap<_, _>>();
let output_connection_map = output_connections
.iter()
.enumerate()
.map(|(port, &(connection_id, _))| (connection_id, port))
.collect::<HashMap<_, _>>();
intermediate_connections.push((input_connection_map, output_connection_map));
} }
for c in &factory_graph.factory_connections { for (i, c) in factory_graph.factory_connections.iter().enumerate() {
connections.push(Connection { connections.push(Connection {
startblock: c.from, startblock: c.from,
startpoint: used_connections[c.from].0, startpoint: intermediate_connections[c.from].1[&i],
endblock: c.to, endblock: c.to,
endpoint: used_connections[c.to].1, endpoint: intermediate_connections[c.to].0[&i],
lanes: 1, lanes: 1,
beltspeed: Beltspeed::from_items_per_second(c.amount), beltspeed: Beltspeed::from_items_per_second(c.amount),
}); });
used_connections[c.from].0 += 1;
used_connections[c.to].1 += 1;
} }
// dbg!(&blocks); // dbg!(&blocks);