From 41a528a49c4687490454a9ca638e364aeccbdaeb Mon Sep 17 00:00:00 2001 From: hal8174 Date: Fri, 28 Feb 2025 21:15:54 +0100 Subject: [PATCH] Use stationary interfaces for external connections --- factorio-blueprint-generator/src/factory.rs | 228 ++++++++++++++------ factorio-layout/src/lib.rs | 4 +- factorio-layout/src/misc.rs | 8 +- 3 files changed, 171 insertions(+), 69 deletions(-) diff --git a/factorio-blueprint-generator/src/factory.rs b/factorio-blueprint-generator/src/factory.rs index df14f03..b9b797e 100644 --- a/factorio-blueprint-generator/src/factory.rs +++ b/factorio-blueprint-generator/src/factory.rs @@ -38,6 +38,16 @@ pub struct FactoryConnection { pub to: usize, } +struct IntermediateConnection { + input: HashMap, + output: HashMap, +} + +struct IntermediateConnectionEntry { + block: Option, + interface: usize, +} + pub fn generate_factory( layouter: &L, pathfinder: &P, @@ -49,6 +59,9 @@ pub fn generate_factory c1.amount + c2.amount; - intermediate_connections.push(( - HashMap::from([ - (connection_id0, (block_index, if c0_bigger { 1 } else { 0 })), - (connection_id1, (block_index + 1, 0)), - (connection_id2, (block_index + 1, 1)), + 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, + }, + ), ]), - intermediate_output_connection, - )); + output: intermediate_output_connection, + }); let beltspeed = Beltspeed::from_items_per_second(2.0 * f64::max(c1.amount, c2.amount)); @@ -144,23 +180,41 @@ pub fn generate_factory { let step = 4; - blocks.push(MacroBlock { - size: Position::new(1, step * (inputs + outputs) as PositionType), - input: (0..*inputs) - .map(|y| Interface { - offset: Position::new(0, step * y as PositionType), - dir: Direction::Left, + let static_input_offset = static_input.len(); + let static_output_offset = static_output.len(); + static_input.extend((0..*inputs).map(|y| Interface { + offset: Position::new( + 0, + step * (y + static_input_offset + static_output_offset) as PositionType, + ), + dir: Direction::Left, + })); + static_output.extend((0..*outputs).map(|y| Interface { + offset: Position::new( + 0, + step * (y + static_input_offset + static_output_offset + inputs) + as PositionType, + ), + dir: Direction::Right, + })); + intermediate_connections.push(IntermediateConnection { + input: input_connections + .iter() + .enumerate() + .map(|(port, &(connection_id, _))| { + ( + connection_id, + IntermediateConnectionEntry { + block: None, + interface: static_input_offset + port, + }, + ) }) - .collect(), - output: (0..*outputs) - .map(|y| Interface { - offset: Position::new(0, step * (inputs + y) as PositionType), - dir: Direction::Right, + .collect::>(), + output: output_connections + .iter() + .enumerate() + .map(|(port, &(connection_id, _))| { + ( + connection_id, + IntermediateConnectionEntry { + block: None, + interface: static_output_offset + port, + }, + ) }) - .collect(), + .collect::>(), }); - - blueprints.push(Blueprint::new()); - intermediate_connections.push(( - input_connections - .iter() - .enumerate() - .map(|(port, &(connection_id, _))| (connection_id, (block_index, port))) - .collect::>(), - output_connections - .iter() - .enumerate() - .map(|(port, &(connection_id, _))| (connection_id, (block_index, port))) - .collect::>(), - )); } Building::SideLoader => { let mut blueprint = Blueprint::new(); @@ -263,18 +333,34 @@ pub fn generate_factory>(), - output_connections + output: output_connections .iter() .enumerate() - .map(|(port, &(connection_id, _))| (connection_id, (block_index, port))) + .map(|(port, &(connection_id, _))| { + ( + connection_id, + IntermediateConnectionEntry { + block: Some(block_index), + interface: port, + }, + ) + }) .collect::>(), - )); + }); } Building::Splitter => { let mut blueprint = Blueprint::new(); @@ -315,18 +401,34 @@ pub fn generate_factory>(), - output_connections + output: output_connections .iter() .enumerate() - .map(|(port, &(connection_id, _))| (connection_id, (block_index, port))) + .map(|(port, &(connection_id, _))| { + ( + connection_id, + IntermediateConnectionEntry { + block: Some(block_index), + interface: port, + }, + ) + }) .collect::>(), - )); + }); } } } @@ -335,10 +437,10 @@ pub fn generate_factory, - pub input: Vec, - pub output: Vec, + pub static_input: Vec, + pub static_output: Vec, pub connections: Vec, } diff --git a/factorio-layout/src/misc.rs b/factorio-layout/src/misc.rs index 81fcad9..dd00b45 100644 --- a/factorio-layout/src/misc.rs +++ b/factorio-layout/src/misc.rs @@ -121,8 +121,8 @@ pub fn path_input_from_blocks_positions( .dir .transform(start_transform); } else { - start_pos = input.output[c.startpoint].offset; - start_dir = input.output[c.startpoint].dir; + start_pos = input.static_output[c.startpoint].offset; + start_dir = input.static_output[c.startpoint].dir; } let end_pos; let end_dir; @@ -135,8 +135,8 @@ pub fn path_input_from_blocks_positions( .dir .transform(end_transform); } else { - end_pos = input.input[c.endpoint].offset; - end_dir = input.input[c.endpoint].dir; + end_pos = input.static_input[c.endpoint].offset; + end_dir = input.static_input[c.endpoint].dir; } connections.push(Connection {