Add Visualize to LayoutResult

This commit is contained in:
hal8174 2025-02-04 23:27:37 +01:00
parent ec869d4c18
commit c472a28591
5 changed files with 73 additions and 35 deletions

View file

@ -1,11 +1,11 @@
use factorio_blueprint::abstraction::{Blueprint, Entity};
use factorio_core::{beltoptions::Beltspeed, prelude::*};
use factorio_core::{beltoptions::Beltspeed, prelude::*, visualize::Visualize};
use factorio_layout::{Connection, Interface, LayoutInput, Layouter, MacroBlock};
use factorio_pathfinding::Pathfinder;
use rand::Rng;
use serde::{Deserialize, Serialize};
use crate::assembly::{assembly_line, assembly_line_2_input};
use crate::assembly::assembly_line_2_input;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FactoryGraph {
@ -167,6 +167,8 @@ pub fn generate_factory<L: Layouter, P: Pathfinder>(
};
let l = layouter.layout(&layout_input, pathfinder, rng).unwrap();
l.print_visualization();
let mut b = Blueprint::new();
for (block, mut assembly_blueprint) in l.positions.iter().zip(blueprints) {
let offset = match block.dir() {

View file

@ -1,7 +1,7 @@
mod image;
mod print;
use crate::prelude::*;
use crate::{pathfield::PathField, prelude::*};
use ::image::RgbaImage;
pub use image::image_grid;
use std::collections::HashMap;
@ -124,6 +124,25 @@ impl Visualization {
}
}
pub fn add_path(&mut self, path: &[PathField], fg: Option<Color>, bg: Option<Color>) {
for p in path {
match p {
PathField::Belt { pos, dir } => {
self.add_symbol(*pos, Symbol::Arrow(*dir), fg, bg);
}
PathField::Underground { pos, dir, len } => {
self.add_symbol(*pos, Symbol::ArrowEnter(*dir), fg, None);
self.add_symbol(
pos.in_direction(dir, *len as i32),
Symbol::ArrowExit(*dir),
fg,
bg,
);
}
}
}
}
pub fn overwrite_background(&mut self, pos: Position, bg: Option<Color>) {
if let Some(s) = self.symbols.get_mut(&pos) {
s.2 = bg;

View file

@ -1,4 +1,9 @@
use factorio_core::{beltoptions::Beltspeed, pathfield::PathField, prelude::*};
use factorio_core::{
beltoptions::Beltspeed,
pathfield::PathField,
prelude::*,
visualize::{self, Visualization, Visualize},
};
use factorio_pathfinding::Pathfinder;
use rand::Rng;
use serde::{Deserialize, Serialize};
@ -53,3 +58,36 @@ pub trait Layouter {
rng: &mut R,
) -> Option<LayoutResult>;
}
impl Visualize for LayoutResult {
fn visualize(&self) -> factorio_core::visualize::Visualization {
let mut s = Visualization::new(self.size);
for (i, block) in self.positions.iter().enumerate() {
let aabb = block.get_aabb();
for x in aabb.min().x..=aabb.max().x {
for y in aabb.min().y..=aabb.max().y {
s.add_symbol(
Position::new(x, y),
visualize::Symbol::Block,
Some(factorio_core::visualize::Color::index(i)),
None,
);
}
}
}
for (i, path) in self.path_result.iter().enumerate() {
s.add_path(
path,
Some(factorio_core::visualize::Color::index(
self.positions.len() + i,
)),
None,
);
}
s
}
}

View file

@ -1,6 +1,6 @@
use crate::{LayoutInput, LayoutResult};
use factorio_core::prelude::*;
use factorio_pathfinding::{Connection, PathInput, Pathfinder, examples::HashMapMap};
use factorio_pathfinding::{Connection, examples::HashMapMap};
use rand::{Rng, seq::SliceRandom};
pub fn initally_set_blocks(
@ -137,7 +137,7 @@ pub fn mutate<R: Rng>(
#[allow(clippy::type_complexity)]
let r: &[(
&dyn Fn(&LayoutInput, &LayoutResult, &mut Vec<Block>, &mut R) -> bool,
&dyn Fn(&LayoutInput, &LayoutResult, &mut [Block], &mut R) -> bool,
_,
)] = &[
(&mutate_replace::<R>, 30),
@ -158,7 +158,7 @@ pub fn mutate<R: Rng>(
fn mutate_replace<R: Rng>(
input: &LayoutInput,
output: &LayoutResult,
blocks: &mut Vec<Block>,
blocks: &mut [Block],
rng: &mut R,
) -> bool {
let _ = input;
@ -205,7 +205,7 @@ fn mutate_replace<R: Rng>(
fn mutate_flip<R: Rng>(
input: &LayoutInput,
output: &LayoutResult,
blocks: &mut Vec<Block>,
blocks: &mut [Block],
rng: &mut R,
) -> bool {
let _ = output;
@ -229,7 +229,7 @@ fn mutate_flip<R: Rng>(
fn mutate_jiggle<R: Rng>(
input: &LayoutInput,
output: &LayoutResult,
blocks: &mut Vec<Block>,
blocks: &mut [Block],
rng: &mut R,
) -> bool {
let _ = input;

View file

@ -610,32 +610,11 @@ impl Visualize for Bruteforce {
}
for (i, problem) in self.problems.iter().enumerate() {
for p in &problem.path {
match p {
PathField::Belt { pos, dir } => {
v.add_symbol(
*pos,
factorio_core::visualize::Symbol::Arrow(*dir),
v.add_path(
&problem.path,
Some(factorio_core::visualize::Color::index(i)),
None,
);
}
PathField::Underground { pos, dir, len } => {
v.add_symbol(
*pos,
factorio_core::visualize::Symbol::ArrowEnter(*dir),
Some(factorio_core::visualize::Color::index(i)),
None,
);
v.add_symbol(
pos.in_direction(dir, *len as i32),
factorio_core::visualize::Symbol::ArrowExit(*dir),
Some(factorio_core::visualize::Color::index(i)),
None,
);
}
}
}
v.add_symbol(
problem.end_pos,
factorio_core::visualize::Symbol::Char(match problem.finished {