Finish subfactory interface

This commit is contained in:
hal8174 2025-04-17 18:50:52 +02:00
parent 82394cc342
commit ff8795cea5
11 changed files with 537 additions and 310 deletions

View file

@ -1,23 +1,28 @@
subfactories: subfactories:
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: military-science-pack recipe: military-science-pack
machines: 40 num_machines: 40
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: piercing-rounds-magazine recipe: piercing-rounds-magazine
machines: 12 num_machines: 12
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: firearm-magazine recipe: firearm-magazine
machines: 4 num_machines: 4
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: grenade recipe: grenade
machines: 32 num_machines: 32
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: stone-wall recipe: stone-wall
machines: 4 num_machines: 4
machine: assembling-machine-3 machine: assembling-machine-3
- !ExternalConnection - !ExternalConnection
factory_connections: factory_connections:

View file

@ -1,43 +1,53 @@
subfactories: subfactories:
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: chemical-science-pack recipe: chemical-science-pack
machines: 96 num_machines: 96
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: engine-unit recipe: engine-unit
machines: 80 num_machines: 80
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: iron-gear-wheel recipe: iron-gear-wheel
machines: 4 num_machines: 4
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: pipe recipe: pipe
machines: 8 num_machines: 8
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: advanced-circuit recipe: advanced-circuit
machines: 72 num_machines: 72
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: electronic-circuit recipe: electronic-circuit
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: electronic-circuit recipe: electronic-circuit
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 10 num_machines: 10
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 10 num_machines: 10
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 10 num_machines: 10
machine: assembling-machine-3 machine: assembling-machine-3
- !ExternalConnection - !ExternalConnection
- !Splitter - !Splitter

View file

@ -1,56 +1,68 @@
subfactories: subfactories:
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: chemical-science-pack recipe: chemical-science-pack
machines: 96 num_machines: 96
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: engine-unit recipe: engine-unit
machines: 80 num_machines: 80
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: iron-gear-wheel recipe: iron-gear-wheel
machines: 4 num_machines: 4
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: pipe recipe: pipe
machines: 8 num_machines: 8
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: advanced-circuit recipe: advanced-circuit
machines: 72 num_machines: 72
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: electronic-circuit recipe: electronic-circuit
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: electronic-circuit recipe: electronic-circuit
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 10 num_machines: 10
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 10 num_machines: 10
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 10 num_machines: 10
machine: assembling-machine-3 machine: assembling-machine-3
- !ExternalConnection - !ExternalConnection
- !Distributor - !Distributor
- !Distributor - !Distributor
- !Distributor - !Distributor
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-plate recipe: copper-plate
machines: 64 num_machines: 64
machine: electric-furnace machine: electric-furnace
- !Distributor - !Distributor
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-plate recipe: copper-plate
machines: 48 num_machines: 48
machine: electric-furnace machine: electric-furnace
factory_connections: factory_connections:
- item: chemical-science-pack - item: chemical-science-pack

View file

@ -1,24 +1,29 @@
subfactories: subfactories:
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: iron-gear-wheel recipe: iron-gear-wheel
machines: 2 num_machines: 2
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 9 num_machines: 9
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: electronic-circuit recipe: electronic-circuit
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !Splitter - !Splitter
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: inserter recipe: inserter
machines: 2 num_machines: 2
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: fast-inserter recipe: fast-inserter
machines: 2 num_machines: 2
machine: assembling-machine-3 machine: assembling-machine-3
- !SideLoader - !SideLoader
- !ExternalConnection - !ExternalConnection

View file

@ -1,27 +1,33 @@
subfactories: subfactories:
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: logistic-science-pack recipe: logistic-science-pack
machines: 48 num_machines: 48
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: inserter recipe: inserter
machines: 4 num_machines: 4
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: electronic-circuit recipe: electronic-circuit
machines: 4 num_machines: 4
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: transport-belt recipe: transport-belt
machines: 2 num_machines: 2
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: iron-gear-wheel recipe: iron-gear-wheel
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !ExternalConnection - !ExternalConnection
- !Splitter - !Splitter

View file

@ -1,19 +1,23 @@
subfactories: subfactories:
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: iron-gear-wheel recipe: iron-gear-wheel
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: copper-cable recipe: copper-cable
machines: 9 num_machines: 9
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: electronic-circuit recipe: electronic-circuit
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !SubFactory - !SubFactory
subfactory: !DoubleAssemblingLine
recipe: inserter recipe: inserter
machines: 6 num_machines: 6
machine: assembling-machine-3 machine: assembling-machine-3
- !ExternalConnection - !ExternalConnection
factory_connections: factory_connections:

View file

@ -10,7 +10,7 @@ struct Args {
json: bool, json: bool,
assembly_machines: usize, assembly_machines: usize,
machine: String, machine: String,
recipe: String, recipe: Option<String>,
output_belt: Beltspeed, output_belt: Beltspeed,
input_belt: Vec<Beltspeed>, input_belt: Vec<Beltspeed>,
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
multistation::{StationSpec, multistation}, multistation::{StationSpec, multistation},
subfactory::assembling_line::assembly_line_2_input, subfactory::{BeltConnection, SubFactory, assembling_line::assembly_line_2_input},
}; };
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};
@ -18,11 +18,7 @@ pub struct FactoryGraph {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Building { pub enum Building {
SubFactory { SubFactory { subfactory: SubFactory },
recipe: String,
machines: usize,
machine: String,
},
ExternalConnection, ExternalConnection,
Splitter, Splitter,
SideLoader, SideLoader,
@ -65,7 +61,7 @@ pub fn generate_factory<L: Layouter, P: Pathfinder + Sync, R: Rng + SeedableRng
let mut station_spec = Vec::new(); let mut station_spec = 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 let input_connections = factory_graph
.factory_connections .factory_connections
.iter() .iter()
.enumerate() .enumerate()
@ -82,84 +78,115 @@ pub fn generate_factory<L: Layouter, P: Pathfinder + Sync, R: Rng + SeedableRng
let block_index = blocks.len(); let block_index = blocks.len();
match b { match b {
Building::SubFactory { Building::SubFactory { subfactory } => {
recipe, let inputs = input_connections
machines, .iter()
machine, .map(|(_, f)| (f.item.as_str(), f.amount))
} => { .fold(HashMap::new(), |mut h, (s, a)| {
// assert_eq!(machine, "assembly-machine-3"); match h.entry(s) {
assert_eq!(output_connections.len(), 1); std::collections::hash_map::Entry::Occupied(mut occupied_entry) => {
assert!(input_connections.len() <= 3); *occupied_entry.get_mut() += a;
let output_connection = output_connections.first().unwrap(); }
let intermediate_output_connection = HashMap::from([( std::collections::hash_map::Entry::Vacant(vacant_entry) => {
output_connection.0, vacant_entry.insert(a);
}
}
h
})
.into_iter()
.collect::<Vec<_>>();
let outputs = output_connections
.iter()
.map(|(_, f)| (f.item.as_str(), f.amount))
.fold(HashMap::new(), |mut h, (s, a)| {
match h.entry(s) {
std::collections::hash_map::Entry::Occupied(mut occupied_entry) => {
*occupied_entry.get_mut() += a;
}
std::collections::hash_map::Entry::Vacant(vacant_entry) => {
vacant_entry.insert(a);
}
}
h
})
.into_iter()
.collect::<Vec<_>>();
let (b, m, input_belts, output_belts) =
subfactory.get_subfactory(&inputs, &outputs);
assert!(output_belts.len() == 1);
assert!(output_connections.len() == 1);
assert!(output_belts[0] == BeltConnection::Full(outputs[0]));
blueprints.push(b);
blocks.push(m);
let mut intermediate_input_connections = HashMap::new();
for (interface, input_belt) in input_belts.iter().enumerate() {
match input_belt {
BeltConnection::Full((s, _a)) => {
let input_connection = input_connections
.iter()
.find(|(_, f)| f.item == *s)
.unwrap();
intermediate_input_connections.insert(
input_connection.0,
IntermediateConnectionEntry { IntermediateConnectionEntry {
block: Some(block_index), block: Some(block_index),
interface: 0, interface,
}, },
)]); );
}
input_connections.sort_by(|a, b| b.1.amount.total_cmp(&a.1.amount));
let (input_belt1, input_belt2, addition_blocks) = if let Some(
&[
(connection_id0, c0),
(connection_id1, c1),
(connection_id2, c2),
],
) =
input_connections.first_chunk()
{
let c0_bigger = c0.amount > c1.amount + c2.amount;
intermediate_connections.push(IntermediateConnection {
input: HashMap::from([
(
connection_id0,
IntermediateConnectionEntry {
block: Some(block_index),
interface: if c0_bigger { 1 } else { 0 },
},
),
(
connection_id1,
IntermediateConnectionEntry {
block: Some(block_index + 1),
interface: 0,
},
),
(
connection_id2,
IntermediateConnectionEntry {
block: Some(block_index + 1),
interface: 1,
},
),
]),
output: intermediate_output_connection,
});
BeltConnection::Split { left, right } => {
let beltspeed = let beltspeed =
Beltspeed::from_items_per_second(2.0 * f64::max(c1.amount, c2.amount)); Beltspeed::from_items_per_second(2.0 * f64::max(left.1, right.1));
connections.push(Connection { connections.push(Connection {
startblock: Some(block_index + 1), startblock: Some(blocks.len()),
startpoint: 0, startpoint: 0,
endblock: Some(block_index), endblock: Some(block_index),
endpoint: if c0_bigger { 0 } else { 1 }, endpoint: interface,
lanes: 1, lanes: 1,
beltspeed, beltspeed,
}); });
let mut b = Blueprint::new();
let input_connection_left = input_connections
.iter()
.find(|(_, f)| f.item == *left.0)
.unwrap();
intermediate_input_connections.insert(
input_connection_left.0,
IntermediateConnectionEntry {
block: Some(blocks.len()),
interface: 0,
},
);
let input_connection_right = input_connections
.iter()
.find(|(_, f)| f.item == *right.0)
.unwrap();
intermediate_input_connections.insert(
input_connection_right.0,
IntermediateConnectionEntry {
block: Some(blocks.len()),
interface: 1,
},
);
let mut b = Blueprint::new();
b.add_entity(Entity::new_belt( b.add_entity(Entity::new_belt(
beltspeed, beltspeed,
Position::new(1, 1), Position::new(1, 1),
Direction::Up, Direction::Up,
)); ));
(
Beltspeed::from_items_per_second(c0.amount), blueprints.push(b);
Some(beltspeed), blocks.push(MacroBlock {
vec![(
MacroBlock {
size: Position::new(1, 1), size: Position::new(1, 1),
input: vec![ input: vec![
Interface { Interface {
@ -175,87 +202,191 @@ pub fn generate_factory<L: Layouter, P: Pathfinder + Sync, R: Rng + SeedableRng
offset: Position::new(0, 0), offset: Position::new(0, 0),
dir: Direction::Up, dir: Direction::Up,
}], }],
},
b,
)],
)
} else if let Some(&[(connection_id0, c0), (connection_id1, c1)]) =
input_connections.first_chunk()
{
intermediate_connections.push(IntermediateConnection {
input: HashMap::from([
(
connection_id0,
IntermediateConnectionEntry {
block: Some(block_index),
interface: 0,
},
),
(
connection_id1,
IntermediateConnectionEntry {
block: Some(block_index),
interface: 1,
},
),
]),
output: intermediate_output_connection,
}); });
( }
Beltspeed::from_items_per_second(c0.amount), }
Some(Beltspeed::from_items_per_second(c1.amount)), }
vec![],
)
} else if let Some(&[(connection_id, c)]) = input_connections.first_chunk() {
intermediate_connections.push(IntermediateConnection { intermediate_connections.push(IntermediateConnection {
input: HashMap::from([( input: intermediate_input_connections,
connection_id, output: HashMap::from([(
output_connections[0].0,
IntermediateConnectionEntry { IntermediateConnectionEntry {
block: Some(block_index), block: Some(block_index),
interface: 0, interface: 0,
}, },
)]), )]),
output: intermediate_output_connection,
});
(Beltspeed::from_items_per_second(c.amount), None, vec![])
} else {
unreachable!()
};
let (b, size, y_output, y_inputs) = assembly_line_2_input(
*machines,
machine,
recipe,
input_belt1,
input_belt2,
Beltspeed::from_items_per_second(
output_connections
.first()
.map(|&(_, c)| c.amount)
.unwrap_or(1.0),
),
);
blueprints.push(b);
blocks.push(MacroBlock {
size,
input: y_inputs
.iter()
.map(|&y| Interface {
offset: Position::new(0, y),
dir: Direction::Right,
})
.collect(),
output: vec![Interface {
offset: Position::new(0, y_output),
dir: Direction::Left,
}],
}); });
for (mb, b) in addition_blocks { // // assert_eq!(machine, "assembly-machine-3");
blueprints.push(b); // assert_eq!(output_connections.len(), 1);
blocks.push(mb); // assert!(input_connections.len() <= 3);
} // let output_connection = output_connections.first().unwrap();
// let intermediate_output_connection = HashMap::from([(
// output_connection.0,
// IntermediateConnectionEntry {
// block: Some(block_index),
// interface: 0,
// },
// )]);
// input_connections.sort_by(|a, b| b.1.amount.total_cmp(&a.1.amount));
// let (input_belt1, input_belt2, addition_blocks) = if let Some(
// &[
// (connection_id0, c0),
// (connection_id1, c1),
// (connection_id2, c2),
// ],
// ) =
// input_connections.first_chunk()
// {
// let c0_bigger = c0.amount > c1.amount + c2.amount;
// intermediate_connections.push(IntermediateConnection {
// input: HashMap::from([
// (
// connection_id0,
// IntermediateConnectionEntry {
// block: Some(block_index),
// interface: if c0_bigger { 1 } else { 0 },
// },
// ),
// (
// connection_id1,
// IntermediateConnectionEntry {
// block: Some(block_index + 1),
// interface: 0,
// },
// ),
// (
// connection_id2,
// IntermediateConnectionEntry {
// block: Some(block_index + 1),
// interface: 1,
// },
// ),
// ]),
// output: intermediate_output_connection,
// });
// let beltspeed =
// Beltspeed::from_items_per_second(2.0 * f64::max(c1.amount, c2.amount));
// connections.push(Connection {
// startblock: Some(block_index + 1),
// startpoint: 0,
// endblock: Some(block_index),
// endpoint: if c0_bigger { 0 } else { 1 },
// lanes: 1,
// beltspeed,
// });
// let mut b = Blueprint::new();
// b.add_entity(Entity::new_belt(
// beltspeed,
// Position::new(1, 1),
// Direction::Up,
// ));
// (
// Beltspeed::from_items_per_second(c0.amount),
// Some(beltspeed),
// vec![(
// MacroBlock {
// size: Position::new(1, 1),
// input: vec![
// Interface {
// offset: Position::new(0, 0),
// dir: Direction::Right,
// },
// Interface {
// offset: Position::new(0, 0),
// dir: Direction::Left,
// },
// ],
// output: vec![Interface {
// offset: Position::new(0, 0),
// dir: Direction::Up,
// }],
// },
// b,
// )],
// )
// } else if let Some(&[(connection_id0, c0), (connection_id1, c1)]) =
// input_connections.first_chunk()
// {
// intermediate_connections.push(IntermediateConnection {
// input: HashMap::from([
// (
// connection_id0,
// IntermediateConnectionEntry {
// block: Some(block_index),
// interface: 0,
// },
// ),
// (
// connection_id1,
// IntermediateConnectionEntry {
// block: Some(block_index),
// interface: 1,
// },
// ),
// ]),
// output: intermediate_output_connection,
// });
// (
// Beltspeed::from_items_per_second(c0.amount),
// Some(Beltspeed::from_items_per_second(c1.amount)),
// vec![],
// )
// } else if let Some(&[(connection_id, c)]) = input_connections.first_chunk() {
// intermediate_connections.push(IntermediateConnection {
// input: HashMap::from([(
// connection_id,
// IntermediateConnectionEntry {
// block: Some(block_index),
// interface: 0,
// },
// )]),
// output: intermediate_output_connection,
// });
// (Beltspeed::from_items_per_second(c.amount), None, vec![])
// } else {
// unreachable!()
// };
// let (b, size, y_output, y_inputs) = assembly_line_2_input(
// *machines,
// machine,
// Some(recipe),
// input_belt1,
// input_belt2,
// Beltspeed::from_items_per_second(
// output_connections
// .first()
// .map(|&(_, c)| c.amount)
// .unwrap_or(1.0),
// ),
// );
// blueprints.push(b);
// blocks.push(MacroBlock {
// size,
// input: y_inputs
// .iter()
// .map(|&y| Interface {
// offset: Position::new(0, y),
// dir: Direction::Right,
// })
// .collect(),
// output: vec![Interface {
// offset: Position::new(0, y_output),
// dir: Direction::Left,
// }],
// });
// for (mb, b) in addition_blocks {
// blueprints.push(b);
// blocks.push(mb);
// }
} }
Building::ExternalConnection => { Building::ExternalConnection => {
// let step = 1; // let step = 1;

View file

@ -1,9 +1,15 @@
use assembling_line::assembly_line_2_input;
use factorio_blueprint::abstraction::Blueprint; use factorio_blueprint::abstraction::Blueprint;
use factorio_core::beltoptions::{Beltspeed, Belttype}; use factorio_core::{
use factorio_layout::MacroBlock; beltoptions::{Beltspeed, Belttype},
prelude::{Direction, Position},
};
use factorio_layout::{Interface, MacroBlock};
use serde::{Deserialize, Serialize};
pub mod assembling_line; pub mod assembling_line;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum SubFactory { pub enum SubFactory {
AssemblingLine { AssemblingLine {
num_machines: usize, num_machines: usize,
@ -17,21 +23,25 @@ pub enum SubFactory {
}, },
} }
enum BeltConnection { #[derive(Debug, Clone, PartialEq)]
Full(String), pub enum BeltConnection<'a> {
Split { left: String, right: String }, Full((&'a str, f64)),
Split {
left: (&'a str, f64),
right: (&'a str, f64),
},
} }
impl SubFactory { impl SubFactory {
fn get_subfactory( pub fn get_subfactory<'a>(
&self, &self,
inputs: &[(&str, f64)], inputs: &[(&'a str, f64)],
outputs: &[(&str, f64)], outputs: &[(&'a str, f64)],
) -> ( ) -> (
Blueprint, Blueprint,
MacroBlock, MacroBlock,
Vec<BeltConnection>, Vec<BeltConnection<'a>>,
Vec<BeltConnection>, Vec<BeltConnection<'a>>,
) { ) {
match self { match self {
SubFactory::AssemblingLine { SubFactory::AssemblingLine {
@ -61,13 +71,13 @@ impl SubFactory {
{ {
( (
BeltConnection::Split { BeltConnection::Split {
left: input1.to_owned(), left: (input1, amount1),
right: input2.to_owned(), right: (input2, amount2),
}, },
Beltspeed::from_items_per_second(2.0 * f64::max(amount1, amount2)), Beltspeed::from_items_per_second(2.0 * f64::max(amount1, amount2)),
BeltConnection::Split { BeltConnection::Split {
left: input3.to_owned(), left: (input3, amount3),
right: input4.to_owned(), right: (input4, amount4),
}, },
Beltspeed::from_items_per_second(2.0 * f64::max(amount3, amount4)), Beltspeed::from_items_per_second(2.0 * f64::max(amount3, amount4)),
) )
@ -75,11 +85,11 @@ impl SubFactory {
inputs.first_chunk() inputs.first_chunk()
{ {
( (
BeltConnection::Full(input1.to_owned()), BeltConnection::Full((input1, amount1)),
Beltspeed::from_items_per_second(amount1), Beltspeed::from_items_per_second(amount1),
BeltConnection::Split { BeltConnection::Split {
left: input2.to_owned(), left: (input2, amount2),
right: input3.to_owned(), right: (input3, amount3),
}, },
Beltspeed::from_items_per_second(2.0 * f64::max(amount2, amount3)), Beltspeed::from_items_per_second(2.0 * f64::max(amount2, amount3)),
) )
@ -87,27 +97,71 @@ impl SubFactory {
inputs.first_chunk() inputs.first_chunk()
{ {
( (
BeltConnection::Full(input1.to_owned()), BeltConnection::Full((input1, amount1)),
Beltspeed::from_items_per_second(amount1), Beltspeed::from_items_per_second(amount1),
BeltConnection::Full(input2.to_owned()), BeltConnection::Full((input2, amount2)),
Beltspeed::from_items_per_second(amount2), Beltspeed::from_items_per_second(amount2),
) )
} else { } else {
unreachable!() unreachable!()
}; };
let (b, size, y_output, y_inputs) = assembly_line_2_input(
*num_machines,
machine,
recipe.as_ref(),
belttype1,
Some(belttype2),
Beltspeed::from_items_per_second(outputs[0].1),
);
( (
todo!(), b,
todo!(), MacroBlock {
size,
input: y_inputs
.iter()
.map(|&y| Interface {
offset: Position::new(0, y),
dir: Direction::Right,
})
.collect(),
output: vec![Interface {
offset: Position::new(0, y_output),
dir: Direction::Left,
}],
},
vec![input1, input2], vec![input1, input2],
vec![BeltConnection::Full(outputs[0].0.to_owned())], vec![BeltConnection::Full(outputs[0])],
) )
} else { } else {
let (b, size, y_output, y_inputs) = assembly_line_2_input(
*num_machines,
machine,
recipe.as_ref(),
Beltspeed::from_items_per_second(inputs[0].1),
None,
Beltspeed::from_items_per_second(outputs[0].1),
);
( (
todo!(), b,
todo!(), MacroBlock {
vec![BeltConnection::Full(inputs[0].0.to_owned())], size,
vec![BeltConnection::Full(outputs[0].0.to_owned())], input: y_inputs
.iter()
.map(|&y| Interface {
offset: Position::new(0, y),
dir: Direction::Right,
})
.collect(),
output: vec![Interface {
offset: Position::new(0, y_output),
dir: Direction::Left,
}],
},
vec![BeltConnection::Full(inputs[0])],
vec![BeltConnection::Full(outputs[0])],
) )
} }
} }

View file

@ -10,7 +10,7 @@ AAABBBCCC
pub fn assembly_line( pub fn assembly_line(
assembly_machines: usize, assembly_machines: usize,
machine: impl AsRef<str>, machine: impl AsRef<str>,
recipe: impl AsRef<str>, recipe: Option<impl AsRef<str>>,
) -> Blueprint { ) -> Blueprint {
let mut blueprint = Blueprint::new(); let mut blueprint = Blueprint::new();
let mut last = None; let mut last = None;
@ -228,7 +228,7 @@ pub fn assembly_line(
pub fn assembly_line_2_input( pub fn assembly_line_2_input(
assembly_machines: usize, assembly_machines: usize,
machine: impl AsRef<str>, machine: impl AsRef<str>,
recipe: impl AsRef<str>, recipe: Option<impl AsRef<str>>,
input_belt1: Beltspeed, input_belt1: Beltspeed,
input_belt2: Option<Beltspeed>, input_belt2: Option<Beltspeed>,
output_belt: Beltspeed, output_belt: Beltspeed,

View file

@ -164,7 +164,7 @@ pub enum EntityType {
}, },
Production { Production {
name: String, name: String,
recipe: String, recipe: Option<String>,
size: Position, size: Position,
}, },
Rail { Rail {
@ -276,7 +276,7 @@ impl Entity {
pub fn new_production( pub fn new_production(
name: impl AsRef<str>, name: impl AsRef<str>,
recipe: impl AsRef<str>, recipe: Option<impl AsRef<str>>,
position: Position, position: Position,
direction: Direction, direction: Direction,
size: Position, size: Position,
@ -284,7 +284,7 @@ impl Entity {
Self::new( Self::new(
EntityType::Production { EntityType::Production {
name: name.as_ref().to_owned(), name: name.as_ref().to_owned(),
recipe: recipe.as_ref().to_owned(), recipe: recipe.map(|s| s.as_ref().to_owned()),
size, size,
}, },
position, position,
@ -384,7 +384,7 @@ impl Entity {
name: _, name: _,
recipe, recipe,
size: _, size: _,
} => Some(recipe.clone()), } => recipe.clone(),
_ => None, _ => None,
} }
} }