summaryrefslogtreecommitdiff
path: root/src/proc_gen/herringbone.rs
blob: 0a67b0cb2183afd0e8dfc7c344699dc045af1664 (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
31
32
33
34
35
use fxhash::hash32;
use glam::IVec2;

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

pub fn split_position(pixel: IVec2, short: i32) -> (IVec2, IVec2, TileOrientation) {
    let grid = pixel.div_euclid(IVec2::splat(short));
    let local = pixel.rem_euclid(IVec2::splat(short));

    match (grid.x - grid.y).rem_euclid(4) {
        0 => (grid, local, TileOrientation::Horizontal),
        1 => (
            grid - IVec2::X,
            local + IVec2::new(short, 0),
            TileOrientation::Horizontal,
        ),
        3 => (grid, local, TileOrientation::Vertical),
        2 => (
            grid - IVec2::Y,
            local + IVec2::new(0, short),
            TileOrientation::Vertical,
        ),
        _ => unreachable!(),
    }
}

// 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)
}