cygrind_utils/
util.rs

1use flo_curves::{bezier, BezierCurveFactory, Coord2, Geo, Line};
2use rand::Rng;
3
4use crate::parser::Pattern;
5
6/// Ported method for extracting colour information from height from [the official editor](https://gitlab.com/PITR_DEV/ultrakill.pattern-editor/-/blob/master/src/helpers/ColorHelper.js#L5)
7pub fn evaluate_height(height: i32) -> f64 {
8    let emulated_x = ((height + 10) as f64 / 30.0).clamp(0.0, f64::MAX);
9    let col_curve = bezier::Curve::from_points(
10        Coord2(0.0, 0.0),
11        (Coord2(0.0, 255.0), Coord2(0.6, 0.0)),
12        Coord2(1.0, 255.0),
13    );
14    let line = ColLine::from_points(Coord2(emulated_x, 0.0), Coord2(emulated_x, 255.0));
15
16    bezier::curve_intersects_line(&col_curve, &line)[0].0 * 255.0
17}
18
19/// Generate a pattern with random heights in the range -10..=20, and random prefabs for each cell
20pub fn random_pattern() -> Pattern {
21    let mut rng = rand::thread_rng();
22    let mut pattern = Pattern::new();
23
24    for row in pattern.0.iter_mut() {
25        for cell in row {
26            cell.set_height(rng.gen_range(-10..=20));
27            cell.set_prefab(rand::random())
28        }
29    }
30
31    pattern
32}
33
34#[doc(hidden)]
35pub struct ColLine(Coord2, Coord2);
36
37impl Geo for ColLine {
38    type Point = Coord2;
39}
40
41impl Line for ColLine {
42    fn from_points(p1: Self::Point, p2: Self::Point) -> Self {
43        ColLine(p1, p2)
44    }
45
46    fn points(&self) -> (Self::Point, Self::Point) {
47        (self.0, self.1)
48    }
49}