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 factory_graph: FactoryGraph = serde_yaml::from_reader(text).unwrap();
dbg!(&factory_graph);
// dbg!(&factory_graph);
let l = ValidLayout {
max_tries: 4,
@ -36,8 +36,8 @@ fn main() {
let l = GeneticAlgorithm {
mutation_retries: 20,
population_size: 40,
population_keep: 5,
population_new: 5,
population_keep: 8,
population_new: 2,
generations: 80,
valid_layout: l,
};

View file

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