summaryrefslogtreecommitdiff
path: root/src/proc_gen/herringbone.rs
blob: e8569c893dad924ab5172a624b835888e1a46729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use fxhash::hash32;
use glam::IVec2;

use crate::proc_gen::tileset_loader::TileOrientation;

fn tile_starting_at(grid: IVec2) -> Option<TileOrientation> {
    match (grid.x - grid.y).rem_euclid(4) {
        // 1 is the right half of the horizontal tile starting at 0,
        // 2 is the bottom half of the vertical tile starting at 3
        0 => Some(TileOrientation::Horizontal),
        3 => Some(TileOrientation::Vertical),
        _ => None,
    }
}

pub fn tiles_overlapping(min: IVec2, max: IVec2) -> impl Iterator<Item = (IVec2, TileOrientation)> {
    ((min.y - 1)..max.y)
        .flat_map(move |y| ((min.x - 1)..max.x).map(move |x| IVec2::new(x, y)))
        .filter_map(|grid| tile_starting_at(grid).map(|orientation| (grid, orientation)))
}

// better "%" for hashes since it prefers the high bits
#[inline]
fn reduce(h: u32, n: usize) -> usize {
    (((h as u128) * (n as u128)) >> 32) as usize
}

pub fn variant_index(grid: IVec2, variants: usize) -> usize {
    reduce(hash32(&grid), variants)
}