Make rust code

This commit is contained in:
Astatin3
2024-10-02 11:20:18 -06:00
parent 7f301beade
commit 7258d4b195
3 changed files with 323 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "Rustbg"
version = "0.1.0"
edition = "2021"
[dependencies]
term_size = "0.3.2"
rand = "0.9.0-alpha.2"
ansi-escapes = "0.2.0"
+200
View File
@@ -0,0 +1,200 @@
mod noise;
use term_size;
use std::{thread, time::Duration};
use ansi_escapes;
use rand::random;
// fn noise_to_ascii(value: f64) -> char {
// const ASCII_CHARS: &str = " .-=+*#%@";
// let index = ((value + 1.0) / 2.0 * (ASCII_CHARS.len() - 1) as f64).round() as usize;
// ASCII_CHARS.chars().nth(index).unwrap()
// }
//
// fn main() {
// let width;
// let height;
//
// if let Some((w, h)) = term_size::dimensions() {
// width = w;
// height = h;
// } else {
// println!("Unable to get term size :(");
// return;
// }
//
// let perlin = noise::PerlinNoise::new();
// let scale = 0.1;
//
// for y in 0..height {
// for x in 0..width {
// let value = perlin.noise(x as f64 * scale, y as f64 * scale, 0.0);
// let ascii_char = noise_to_ascii(value);
// print!("{}", ascii_char);
// }
// println!();
// }
// }
const HEXAGON_STRING: [&str; 4] = [
" ┏┛ ┗┓ ",
"━━━┫ ┣━━━",
" ┗┓ ┏┛ ",
" ┣━━━━━━┫ "
];
const SQUARES: [&str; 5] = [
"%───────",
"",
"",
"",
"",
];
const SQUARE_REMOVER: [&str; 5] = [
"........",
". ",
". ",
". ",
". ",
];
const TRIANGLES: [&str; 3] = [
" . ",
". .",
".. ",
];
fn hexagon(x: i16, y: i16) -> String {
let xindex: usize = (x % 16) as usize;
let yindex: usize = (y % 4) as usize;
let c: String = HEXAGON_STRING[yindex].chars().nth(xindex).unwrap().into();
// if c.is_none() {return ".".into();}
if c == " " {return ansi_escapes::CursorMove::X(1).to_string()};
return c;
}
fn squares(x: i16, y: i16) -> String {
let xindex: usize = (x % 8) as usize;
let yindex: usize = (y % 5) as usize;
let c: String = SQUARES[yindex].chars().nth(xindex).unwrap().into();
// if c.is_none() {return ".".into();}
if c == " " {return ansi_escapes::CursorMove::X(1).to_string()};
return c;
}
fn squares_2(x: i16, y: i16) -> String {
let xindex: usize = (x % 8) as usize;
let yindex: usize = (y % 5) as usize;
let c: String = SQUARE_REMOVER[yindex].chars().nth(xindex).unwrap().into();
// if c.is_none() {return ".".into();}
if c == " " {return ansi_escapes::CursorMove::X(1).to_string()};
if c == "." {return "".into()};
return c;
}
fn triangles(x: i16, y: i16) -> String {
let xindex: usize = (x % 3) as usize;
let yindex: usize = (y % 3) as usize;
let c: String = TRIANGLES[yindex].chars().nth(xindex).unwrap().into();
// if c.is_none() {return ".".into();}
if c == " " {return ansi_escapes::CursorMove::X(1).to_string()};
return c;
}
// fn number(value: f64, x: i16, y: i16) -> String {
// const TEXT: &str = " .-*%#";
// let index: usize = ((value + 1.0) / 2.0 * (TEXT.len() - 1) as f64).round() as usize;
// return TEXT.chars().nth(index).unwrap().into();
//
// }
fn noise_to_ascii(value: f64, x: i16, y:i16) -> String {
const options_length: i32 = 6;
let index = ((value + 1.0) / 2.0 * (options_length - 1) as f64).round() as usize;
match index {
0 => return hexagon(x, y),
1 => return hexagon(x, y),
2 => return triangles(x,y),
3 => return squares(x, y),
4 => return squares_2(x, y),
5 => return squares_2(x, y),
_ => {return "e".into() }
}
}
fn abs(value: f64) -> f64 {
if(value < 0.){
return -value;
}
return value;
}
fn sign(value: f64) -> i8 {
if(value > 0.){
return 1;
}else if(value < 0.){
return -1;
}
return 0;
}
fn randInc() -> i8{
let tmp:f64 = (random::<f64>()*2.)-1.;
if abs(tmp) > 0.95 {
return sign(tmp);
}
return 0
}
fn main() {
let width;
let height;
if let Some((w, h)) = term_size::dimensions() {
width = w;
height = h;
} else {
println!("Unable to get term size :(");
return;
}
// for y in 0 ..height {
// for x in 0..width {
// }
// println!();
// }
let perlin = noise::PerlinNoise::new();
let scale = 0.1;
let time_scale = 0.02;
let mut time = 0.0;
let mut xoffset:i16 = 0;
let mut yoffset:i16 = 0;
loop {
// print!("{}", ansi_escapes::ClearScreen);
// print!("{}", ansi_escapes::CursorTo::AbsoluteXY(0, 0));
// xoffset += randInc() as i16;
// yoffset += randInc() as i16;
// println!("{}", ((2. * random::<f64>() - 1.)).round());
for y in 0..height {
print!("{}", ansi_escapes::CursorTo::AbsoluteXY(0, y as u16));
for x in 0..width {
let value = perlin.noise(x as f64 * scale, y as f64 * scale, time);
print!("{}", noise_to_ascii(value, (x as i16 + xoffset) % width as i16, (y as i16 + yoffset) % height as i16));
}
// println!();
}
time += time_scale;
thread::sleep(Duration::from_millis(50));
}
}
+114
View File
@@ -0,0 +1,114 @@
use rand::Rng;
use std::{thread, time::Duration};
const PERMUTATION: [u8; 256] = [
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69,
142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219,
203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175,
74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230,
220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209,
76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198,
173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212,
207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2,
44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110,
79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144,
12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106,
157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67,
29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
];
pub(crate) struct PerlinNoise {
p: [u8; 512],
}
impl PerlinNoise {
pub(crate) fn new() -> Self {
let mut rng = rand::thread_rng();
let mut p = [0; 512];
for i in 0..256 {
p[i] = i as u8;
}
for i in 0..256 {
let j = rng.gen_range(0..256);
p.swap(i, j);
}
for i in 0..256 {
p[i + 256] = p[i];
}
PerlinNoise { p }
}
fn fade(t: f64) -> f64 {
t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}
fn lerp(t: f64, a: f64, b: f64) -> f64 {
a + t * (b - a)
}
fn grad(hash: u8, x: f64, y: f64, z: f64) -> f64 {
let h = hash & 15;
let u = if h < 8 { x } else { y };
let v = if h < 4 {
y
} else if h == 12 || h == 14 {
x
} else {
z
};
let s = if (h & 1) == 0 { 1.0 } else { -1.0 };
let t = if (h & 2) == 0 { 1.0 } else { -1.0 };
s * u + t * v
}
pub fn noise(&self, x: f64, y: f64, z: f64) -> f64 {
let xi = x.floor() as usize & 255;
let yi = y.floor() as usize & 255;
let zi = z.floor() as usize & 255;
let x = x - x.floor();
let y = y - y.floor();
let z = z - z.floor();
let u = Self::fade(x);
let v = Self::fade(y);
let w = Self::fade(z);
let a = self.p[xi] as usize + yi;
let aa = self.p[a] as usize + zi;
let ab = self.p[a + 1] as usize + zi;
let b = self.p[xi + 1] as usize + yi;
let ba = self.p[b] as usize + zi;
let bb = self.p[b + 1] as usize + zi;
Self::lerp(
w,
Self::lerp(
v,
Self::lerp(
u,
Self::grad(self.p[aa], x, y, z),
Self::grad(self.p[ba], x - 1.0, y, z),
),
Self::lerp(
u,
Self::grad(self.p[ab], x, y - 1.0, z),
Self::grad(self.p[bb], x - 1.0, y - 1.0, z),
),
),
Self::lerp(
v,
Self::lerp(
u,
Self::grad(self.p[aa + 1], x, y, z - 1.0),
Self::grad(self.p[ba + 1], x - 1.0, y, z - 1.0),
),
Self::lerp(
u,
Self::grad(self.p[ab + 1], x, y - 1.0, z - 1.0),
Self::grad(self.p[bb + 1], x - 1.0, y - 1.0, z - 1.0),
),
),
)
}
}