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,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;