Add Layout trait
This commit is contained in:
parent
00eda50872
commit
295490858b
12 changed files with 407 additions and 41 deletions
119
factorio-layout/src/misc.rs
Normal file
119
factorio-layout/src/misc.rs
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
use crate::LayoutInput;
|
||||
use factorio_core::prelude::*;
|
||||
use factorio_pathfinding::{Connection, PathInput, examples::HashMapMap};
|
||||
use rand::Rng;
|
||||
|
||||
pub fn initally_set_blocks(
|
||||
input: &LayoutInput,
|
||||
size: Position,
|
||||
retries: usize,
|
||||
rng: &mut impl Rng,
|
||||
) -> Option<Vec<Block>> {
|
||||
let mut blocks = Vec::new();
|
||||
|
||||
let mut aabbs = Vec::new();
|
||||
|
||||
if place_block(input, size, retries, &mut blocks, &mut aabbs, rng) {
|
||||
Some(blocks)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn place_block(
|
||||
input: &LayoutInput,
|
||||
size: Position,
|
||||
retries: usize,
|
||||
blocks: &mut Vec<Block>,
|
||||
aabbs: &mut Vec<AABB>,
|
||||
rng: &mut impl Rng,
|
||||
) -> bool {
|
||||
if input.blocks.len() == blocks.len() {
|
||||
return true;
|
||||
}
|
||||
|
||||
let b = &input.blocks[blocks.len()];
|
||||
for _ in 0..retries {
|
||||
let dir = rng.r#gen::<Direction>();
|
||||
|
||||
let pos = match dir {
|
||||
Direction::Up => Position::new(
|
||||
rng.gen_range(0..=(size.x - b.size.x)),
|
||||
rng.gen_range(0..=(size.y - b.size.y)),
|
||||
),
|
||||
Direction::Right => Position::new(
|
||||
rng.gen_range((b.size.y - 1)..size.x),
|
||||
rng.gen_range(0..=(size.y - b.size.x)),
|
||||
),
|
||||
Direction::Down => Position::new(
|
||||
rng.gen_range((b.size.x - 1)..size.x),
|
||||
rng.gen_range((b.size.y - 1)..size.y),
|
||||
),
|
||||
Direction::Left => Position::new(
|
||||
rng.gen_range(0..=(size.x - b.size.y)),
|
||||
rng.gen_range((b.size.x - 1)..size.y),
|
||||
),
|
||||
};
|
||||
|
||||
let current = Block::new(pos, dir, b.size);
|
||||
let current_aabb = current.get_aabb();
|
||||
|
||||
if aabbs.iter().all(|&b| !AABB::collision(b, current_aabb)) {
|
||||
blocks.push(current);
|
||||
aabbs.push(current_aabb);
|
||||
|
||||
if place_block(input, size, retries, blocks, aabbs, rng) {
|
||||
return true;
|
||||
}
|
||||
|
||||
blocks.pop();
|
||||
aabbs.pop();
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn path_input_from_blocks_positions(
|
||||
input: &LayoutInput,
|
||||
size: Position,
|
||||
block_positions: &[Block],
|
||||
) -> (Vec<Connection>, HashMapMap) {
|
||||
let mut map = HashMapMap::new(size);
|
||||
|
||||
let mut connections = Vec::new();
|
||||
|
||||
for b in block_positions {
|
||||
let aabb = b.get_aabb();
|
||||
|
||||
map.set_blocked_range(aabb.min(), aabb.max(), true);
|
||||
}
|
||||
|
||||
for c in &input.connections {
|
||||
let start_transform = block_positions[c.startblock].block_to_world();
|
||||
let start_pos = input.blocks[c.startblock].output[c.startpoint]
|
||||
.offset
|
||||
.transform(start_transform);
|
||||
let start_dir = input.blocks[c.startblock].output[c.startpoint]
|
||||
.dir
|
||||
.transform(start_transform);
|
||||
let end_transform = block_positions[c.endblock].block_to_world();
|
||||
let end_pos = input.blocks[c.endblock].input[c.endpoint]
|
||||
.offset
|
||||
.transform(end_transform);
|
||||
let end_dir = input.blocks[c.endblock].input[c.endpoint]
|
||||
.dir
|
||||
.transform(end_transform);
|
||||
|
||||
connections.push(Connection {
|
||||
start_pos,
|
||||
start_dir,
|
||||
end_pos,
|
||||
end_dir,
|
||||
beltspeed: c.beltspeed,
|
||||
lanes: c.lanes,
|
||||
});
|
||||
}
|
||||
|
||||
(connections, map)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue