Change position format for blueprint

This commit is contained in:
hal8174 2025-01-22 23:04:13 +01:00
parent e969ba848b
commit fec7dd70db
6 changed files with 115 additions and 79 deletions

View file

@ -79,7 +79,7 @@ fn balancer_path() -> Vec<PathField> {
pub fn generate_4_lane_balancer2() -> Blueprint {
let mut b = abstraction::Blueprint::new();
let splitter_pos = [(0, 0), (2, 0), (1, 1), (1, 4), (0, 7), (2, 7)];
let splitter_pos = [(2, 1), (6, 1), (4, 3), (4, 9), (2, 15), (6, 15)];
for (x, y) in splitter_pos {
b.add_entity(Entity::new_splitter(
@ -91,7 +91,7 @@ pub fn generate_4_lane_balancer2() -> Blueprint {
b.add_path(&balancer_path(), Beltspeed::Normal);
b.transform(Transformation::new(Direction::Right, Position::new(0, 0)));
// b.transform(Transformation::new(Direction::Right, Position::new(0, 0)));
b.to_blueprint()
}

View file

@ -5,6 +5,8 @@ use factorio_core::beltoptions::{Beltspeed, Belttype};
#[derive(Parser)]
struct Args {
#[arg(short, long)]
json: bool,
#[command(subcommand)]
command: Command,
}
@ -13,12 +15,13 @@ struct Args {
enum Command {
Book,
Single {
#[arg(long)]
load: bool,
locomotives: usize,
length: usize,
outputs: usize,
beltspeed: Beltspeed,
#[arg(default_value = "full")]
#[arg(short, long, default_value = "full")]
belttype: Belttype,
},
}
@ -109,6 +112,9 @@ fn main() {
.build(),
);
if args.json {
println!("{}", serde_json::to_string_pretty(&b).unwrap());
}
println!("{}", encode(&serde_json::to_string(&b).unwrap()));
}
Command::Single {
@ -124,7 +130,9 @@ fn main() {
.to_blueprint(),
);
// println!("{}", serde_json::to_string_pretty(&b).unwrap());
if args.json {
println!("{}", serde_json::to_string_pretty(&b).unwrap());
}
println!("{}", encode(&serde_json::to_string(&b).unwrap()));
}

View file

@ -25,10 +25,11 @@ pub fn merger(
for j in 0..depth {
b.add_entity(Entity::new_belt(
stubspeed,
Position::new(
(intervall * (i + o * section_size)) as PositionType,
-(j as PositionType),
),
Position::new(1, 1)
+ 2 * Position::new(
(intervall * (i + o * section_size)) as PositionType,
-(j as PositionType),
),
match reverse {
true => Direction::Down,
false => Direction::Up,
@ -38,10 +39,11 @@ pub fn merger(
b.add_entity(Entity::new_belt(
stubspeed,
Position::new(
(intervall * (i + o * section_size)) as PositionType,
-(depth as PositionType),
),
Position::new(1, 1)
+ 2 * Position::new(
(intervall * (i + o * section_size)) as PositionType,
-(depth as PositionType),
),
match reverse {
true => Direction::Down,
false => Direction::Left,
@ -59,9 +61,10 @@ pub fn merger(
b.add_entity(Entity::new_splitter(
mergespeed,
Position::new(
(intervall * (j * p + o * section_size)) as PositionType
- i as PositionType,
0 - i as PositionType - depth as PositionType + !reverse as PositionType,
2 * ((intervall * (j * p + o * section_size)) as PositionType
- i as PositionType)
+ 1,
2 * (1 - i as PositionType - depth as PositionType),
),
match reverse {
true => Direction::Right,
@ -72,11 +75,12 @@ pub fn merger(
for l in 0..(7 * p / 2) {
b.add_entity(Entity::new_belt(
mergespeed.halve(),
Position::new(
(intervall * (j * p + o * section_size) + l + 1) as PositionType
- i as PositionType,
0 - i as PositionType - depth as PositionType,
),
Position::new(1, 1)
+ 2 * Position::new(
(intervall * (j * p + o * section_size) + l + 1) as PositionType
- i as PositionType,
0 - i as PositionType - depth as PositionType,
),
match reverse {
true => Direction::Right,
false => Direction::Left,
@ -91,10 +95,11 @@ pub fn merger(
for l in 0..(7 * o * section_size) {
b.add_entity(Entity::new_belt(
beltspeed,
Position::new(
l as PositionType - section_size.ilog2() as PositionType,
-(step as PositionType),
),
Position::new(1, 1)
+ 2 * Position::new(
l as PositionType - section_size.ilog2() as PositionType,
-(step as PositionType),
),
match reverse {
true => Direction::Right,
false => Direction::Left,

View file

@ -12,28 +12,28 @@ pub fn unloader(beltspeed: Beltspeed, belttype: Belttype) -> (Blueprint, Positio
if beltspeed == Beltspeed::Normal {
b.add_entity(Entity::new_belt(
Beltspeed::Fast,
Position::new(3, -2),
Position::new(1, 1) + 2 * Position::new(3, -2),
Direction::Up,
));
if belttype.contains_left() {
b.add_entity(Entity::new_unknown(
"steel-chest",
Position::new(5, -2),
Position::new(1, 1) + 2 * Position::new(5, -2),
Direction::Up,
Position::new(1, 1),
Position::new(2, 2),
));
b.add_entity(Entity::new_inserter(
InserterType::Bulk,
None,
Position::new(5, -1),
Position::new(1, 1) + 2 * Position::new(5, -1),
Direction::Down,
));
b.add_entity(Entity::new_inserter(
InserterType::Fast,
None,
Position::new(4, -2),
Position::new(1, 1) + 2 * Position::new(4, -2),
Direction::Right,
));
}
@ -41,21 +41,21 @@ pub fn unloader(beltspeed: Beltspeed, belttype: Belttype) -> (Blueprint, Positio
if belttype.contains_right() {
b.add_entity(Entity::new_unknown(
"steel-chest",
Position::new(1, -2),
Position::new(1, 1) + 2 * Position::new(1, -2),
Direction::Up,
Position::new(1, 1),
Position::new(2, 2),
));
b.add_entity(Entity::new_inserter(
InserterType::Bulk,
None,
Position::new(1, -1),
Position::new(1, 1) + 2 * Position::new(1, -1),
Direction::Down,
));
b.add_entity(Entity::new_inserter(
InserterType::Fast,
None,
Position::new(2, -2),
Position::new(1, 1) + 2 * Position::new(2, -2),
Direction::Left,
));
}
@ -71,31 +71,35 @@ pub fn unloader(beltspeed: Beltspeed, belttype: Belttype) -> (Blueprint, Positio
b.add_entity(Entity::new_belt(
beltspeed,
Position::new(3, -4),
Position::new(1, 1) + 2 * Position::new(3, -4),
Direction::Up,
));
let mut s = |x, beltdir| {
b.add_entity(Entity::new_belt(beltspeed, Position::new(x, -4), beltdir));
b.add_entity(Entity::new_belt(
beltspeed,
Position::new(1, 1) + 2 * Position::new(x, -4),
beltdir,
));
b.add_entity(
Entity::new_inserter(
belt_inserter,
stack_size,
Position::new(x, -3),
Position::new(1, 1) + 2 * Position::new(x, -3),
Direction::Down,
)
.quality(quality),
);
b.add_entity(Entity::new_unknown(
"steel-chest",
Position::new(x, -2),
Position::new(1, 1) + 2 * Position::new(x, -2),
Direction::Up,
Position::new(1, 1),
Position::new(2, 2),
));
b.add_entity(Entity::new_inserter(
InserterType::Bulk,
None,
Position::new(x, -1),
Position::new(1, 1) + 2 * Position::new(x, -1),
Direction::Down,
));
};
@ -126,36 +130,45 @@ pub fn one_loader(beltspeed: Beltspeed) -> (Blueprint, PositionType) {
b.add_entity(Entity::new_splitter(
beltspeed,
Position::new(4, -5),
Position::new(8, -9),
Direction::Down,
));
b.add_entity(Entity::new_belt(
beltspeed,
Position::new(3, -4),
Position::new(1, 1) + 2 * Position::new(3, -4),
Direction::Left,
));
b.add_entity(Entity::new_belt(
beltspeed,
Position::new(4, -4),
Position::new(1, 1) + 2 * Position::new(4, -4),
Direction::Right,
));
let mut s = |x, beltdir| {
b.add_entity(Entity::new_belt(beltspeed, Position::new(x, -4), beltdir));
b.add_entity(Entity::new_belt(
beltspeed,
Position::new(1, 1) + 2 * Position::new(x, -4),
beltdir,
));
b.add_entity(
Entity::new_inserter(belt_inserter, None, Position::new(x, -3), Direction::Up)
.quality(quality),
Entity::new_inserter(
belt_inserter,
None,
Position::new(1, 1) + 2 * Position::new(x, -3),
Direction::Up,
)
.quality(quality),
);
b.add_entity(Entity::new_unknown(
"steel-chest",
Position::new(x, -2),
Position::new(1, 1) + 2 * Position::new(x, -2),
Direction::Up,
Position::new(1, 1),
Position::new(2, 2),
));
b.add_entity(Entity::new_inserter(
InserterType::Bulk,
None,
Position::new(x, -1),
Position::new(1, 1) + 2 * Position::new(x, -1),
Direction::Up,
));
};
@ -188,7 +201,7 @@ pub fn basic_station(
for l in 1..=(length + locomotives) {
poles.push(blueprint.add_entity(Entity::new_electric_pole(
ElectricPoleType::Medium,
Position::new(7 * l as PositionType, -2),
Position::new(1, 1) + 2 * Position::new(7 * l as PositionType, -2),
)));
}
@ -216,7 +229,7 @@ pub fn basic_station(
unloader.transform(Transformation::new(
Direction::Up,
Position::new((7 * l + global_x_offset) as PositionType, 0),
2 * Position::new((7 * l + global_x_offset) as PositionType, 0),
));
blueprint.add_blueprint(unloader);
@ -225,18 +238,18 @@ pub fn basic_station(
// train stop
blueprint.add_entity(Entity::new_unknown(
"train-stop",
Position::new(0, -2),
Position::new(2, -2),
Direction::Left,
Position::new(2, 2),
Position::new(4, 4),
));
// rails
for l in 0..((length * 7 + global_x_offset + 1) / 2) {
blueprint.add_entity(Entity::new_unknown(
"straight-rail",
Position::new(2 * l as PositionType, 0),
Direction::Right,
Position::new(2, 2),
Position::new(2 + 4 * l as PositionType, 2),
Direction::Left,
Position::new(4, 4),
));
}
@ -246,20 +259,12 @@ pub fn basic_station(
m.transform(Transformation::new(
Direction::Up,
Position::new(global_x_offset as PositionType + 3, output_y),
2 * Position::new(global_x_offset as PositionType + 3, output_y),
));
blueprint.add_blueprint(m);
// e.extend(merger(
// load,
// beltspeed,
// global_x_offset as f64 + 3.5,
// output_y,
// e.len() as u32,
// 7,
// outputs,
// length,
// ));
// blueprint.transform(Transformation::new(Direction::Right, Position::new(0, 0)));
blueprint
}

View file

@ -56,8 +56,8 @@ impl ElectricPoleType {
fn size(&self) -> Position {
match self {
ElectricPoleType::Small | ElectricPoleType::Medium => Position::new(1, 1),
ElectricPoleType::Big | ElectricPoleType::Substation => Position::new(2, 2),
ElectricPoleType::Small | ElectricPoleType::Medium => Position::new(2, 2),
ElectricPoleType::Big | ElectricPoleType::Substation => Position::new(4, 4),
}
}
}
@ -213,14 +213,14 @@ impl Entity {
pub fn size(&self) -> Position {
match &self.entity {
EntityType::Splitter(_) => Position::new(2, 1),
EntityType::Splitter(_) => Position::new(4, 2),
EntityType::Unknown {
name: _,
size,
misc: _,
} => *size,
EntityType::ElectricPole(electric_pole_type) => electric_pole_type.size(),
_ => Position::new(1, 1),
_ => Position::new(2, 2),
}
}
@ -268,19 +268,23 @@ impl Blueprint {
for &p in path {
match p {
PathField::Belt { pos, dir } => {
self.add_entity(Entity::new_belt(beltspeed, pos, dir));
self.add_entity(Entity::new_belt(
beltspeed,
2 * pos + Position::new(1, 1),
dir,
));
}
PathField::Underground { pos, dir, len } => {
self.add_entity(Entity::new_underground_belt(
beltspeed,
UndergroundType::Input,
pos,
2 * pos + Position::new(1, 1),
dir,
));
self.add_entity(Entity::new_underground_belt(
beltspeed,
UndergroundType::Output,
pos.in_direction(&dir, len as PositionType),
2 * pos.in_direction(&dir, len as PositionType) + Position::new(1, 1),
dir,
));
}
@ -304,15 +308,10 @@ impl Blueprint {
.iter()
.enumerate()
.map(|(i, (_, e))| {
let size = (e.size() - Position::new(1, 1))
.transform(Transformation::new(e.direction, Position::new(0, 0)));
BlueprintEntity::builder(
e.get_name(),
i as u32 + 1,
BlueprintPosition::new(
e.position.x as f64 + 0.5 * size.x as f64 + 0.5,
e.position.y as f64 + 0.5 * size.y as f64 + 0.5,
),
BlueprintPosition::new(0.5 * e.position.x as f64, 0.5 * e.position.y as f64),
)
.direction(match e.direction {
Direction::Up => 0,

View file

@ -72,3 +72,22 @@ impl From<(i32, i32)> for Position {
Position::new(value.0, value.1)
}
}
impl std::ops::Mul<i32> for Position {
type Output = Position;
fn mul(mut self, rhs: i32) -> Self::Output {
self.x *= rhs;
self.y *= rhs;
self
}
}
impl std::ops::Mul<Position> for i32 {
type Output = Position;
fn mul(self, mut rhs: Position) -> Self::Output {
rhs.x *= self;
rhs.y *= self;
rhs
}
}