Add Layout trait
This commit is contained in:
parent
00eda50872
commit
295490858b
12 changed files with 407 additions and 41 deletions
44
factorio-layout/src/valid_layout.rs
Normal file
44
factorio-layout/src/valid_layout.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use factorio_core::prelude::{Position, PositionType};
|
||||
use factorio_pathfinding::Pathfinder;
|
||||
use rand::Rng;
|
||||
|
||||
use crate::{
|
||||
LayoutResult, Layouter,
|
||||
misc::{initally_set_blocks, path_input_from_blocks_positions},
|
||||
};
|
||||
|
||||
pub struct ValidLayout {
|
||||
pub max_tries: usize,
|
||||
pub retries: usize,
|
||||
pub start_size: Position,
|
||||
pub growth: Position,
|
||||
}
|
||||
|
||||
impl Layouter for ValidLayout {
|
||||
fn layout<R: Rng, P: Pathfinder>(
|
||||
&self,
|
||||
input: &crate::LayoutInput,
|
||||
pathfinder: &P,
|
||||
rng: &mut R,
|
||||
) -> Option<LayoutResult> {
|
||||
for i in 0..self.max_tries {
|
||||
let size = self.start_size + i as PositionType * self.growth;
|
||||
|
||||
if let Some(blocks) = initally_set_blocks(input, size, self.retries, rng) {
|
||||
let (connections, map) = path_input_from_blocks_positions(input, size, &blocks);
|
||||
|
||||
if let Some(paths) = pathfinder.find_paths(factorio_pathfinding::PathInput {
|
||||
connections: &connections,
|
||||
map,
|
||||
}) {
|
||||
return Some(LayoutResult {
|
||||
positions: blocks,
|
||||
path_result: paths,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue