Skip to content

Commit

Permalink
Use static rng instead of passing as parameters (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhvy committed Jul 25, 2022
1 parent 2ddc9ff commit fbced0f
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 98 deletions.
88 changes: 81 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions crates/model/src/direction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use rng::Rng;

#[derive(Clone, Copy, PartialEq)]
pub(crate) enum Direction {
Up,
Expand All @@ -9,11 +7,11 @@ pub(crate) enum Direction {
}

impl Direction {
pub(crate) fn maybe_turn(&mut self, rng: &mut Rng, turn_chance: f32) {
let will_turn = rng.gen_bool(turn_chance);
pub(crate) fn maybe_turn(&mut self, turn_chance: f32) {
let will_turn = rng::gen_bool(turn_chance);

if will_turn {
let random_turn_dir = TurnDirection::gen(rng);
let random_turn_dir = TurnDirection::gen();
self.turn(random_turn_dir);
}
}
Expand Down Expand Up @@ -50,8 +48,8 @@ enum TurnDirection {
}

impl TurnDirection {
fn gen(rng: &mut Rng) -> Self {
if rng.gen_bool(0.5) {
fn gen() -> Self {
if rng::gen_bool(0.5) {
Self::Left
} else {
Self::Right
Expand All @@ -60,8 +58,8 @@ impl TurnDirection {
}

impl Direction {
pub(crate) fn gen(rng: &mut Rng) -> Self {
match rng.gen_range(0..4) {
pub(crate) fn gen() -> Self {
match rng::gen_range(0..4) {
0 => Direction::Up,
1 => Direction::Down,
2 => Direction::Left,
Expand Down
33 changes: 13 additions & 20 deletions crates/model/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub use kind::{Kind, KindSet};

use crate::direction::Direction;
use crate::position::{InScreenBounds, Position};
use rng::Rng;

pub struct Pipe {
dir: HistoryKeeper<Direction>,
Expand All @@ -18,15 +17,9 @@ pub struct Pipe {
}

impl Pipe {
pub fn new(
size: (u16, u16),
rng: &mut Rng,
color_mode: ColorMode,
palette: Palette,
kind: Kind,
) -> Self {
let color = color::gen_random_color(rng, color_mode, palette);
let (dir, pos) = Self::gen_rand_dir_and_pos(size, rng);
pub fn new(size: (u16, u16), color_mode: ColorMode, palette: Palette, kind: Kind) -> Self {
let color = color::gen_random_color(color_mode, palette);
let (dir, pos) = Self::gen_rand_dir_and_pos(size);

Self {
dir: HistoryKeeper::new(dir),
Expand All @@ -36,8 +29,8 @@ impl Pipe {
}
}

pub fn dup(&self, size: (u16, u16), rng: &mut Rng) -> Self {
let (dir, pos) = Self::gen_rand_dir_and_pos(size, rng);
pub fn dup(&self, size: (u16, u16)) -> Self {
let (dir, pos) = Self::gen_rand_dir_and_pos(size);

Self {
dir: HistoryKeeper::new(dir),
Expand All @@ -47,14 +40,14 @@ impl Pipe {
}
}

pub fn tick(&mut self, size: (u16, u16), rng: &mut Rng, turn_chance: f32) -> InScreenBounds {
pub fn tick(&mut self, size: (u16, u16), turn_chance: f32) -> InScreenBounds {
let InScreenBounds(in_screen_bounds) = self.pos.move_in(self.dir.current(), size);

if !in_screen_bounds {
return InScreenBounds(false);
}

self.dir.update(|dir| dir.maybe_turn(rng, turn_chance));
self.dir.update(|dir| dir.maybe_turn(turn_chance));

InScreenBounds(true)
}
Expand Down Expand Up @@ -84,24 +77,24 @@ impl Pipe {
}
}

fn gen_rand_dir_and_pos((columns, rows): (u16, u16), rng: &mut Rng) -> (Direction, Position) {
let dir = Direction::gen(rng);
fn gen_rand_dir_and_pos((columns, rows): (u16, u16)) -> (Direction, Position) {
let dir = Direction::gen();
let pos = match dir {
Direction::Up => Position {
x: rng.gen_range_16(0..columns),
x: rng::gen_range_16(0..columns),
y: rows - 1,
},
Direction::Down => Position {
x: rng.gen_range_16(0..columns),
x: rng::gen_range_16(0..columns),
y: 0,
},
Direction::Left => Position {
x: columns - 1,
y: rng.gen_range_16(0..rows),
y: rng::gen_range_16(0..rows),
},
Direction::Right => Position {
x: 0,
y: rng.gen_range_16(0..rows),
y: rng::gen_range_16(0..rows),
},
};

Expand Down
21 changes: 8 additions & 13 deletions crates/model/src/pipe/color.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
use rng::Rng;
use std::ops::Range;
use std::str::FromStr;
use tincture::ColorSpace;

pub(super) fn gen_random_color(
rng: &mut Rng,
color_mode: ColorMode,
palette: Palette,
) -> Option<terminal::Color> {
pub(super) fn gen_random_color(color_mode: ColorMode, palette: Palette) -> Option<terminal::Color> {
match color_mode {
ColorMode::Ansi => Some(gen_random_ansi_color(rng)),
ColorMode::Rgb => Some(gen_random_rgb_color(rng, palette)),
ColorMode::Ansi => Some(gen_random_ansi_color()),
ColorMode::Rgb => Some(gen_random_rgb_color(palette)),
ColorMode::None => None,
}
}

fn gen_random_ansi_color(rng: &mut Rng) -> terminal::Color {
let num = rng.gen_range(0..12);
fn gen_random_ansi_color() -> terminal::Color {
let num = rng::gen_range(0..12);

match num {
0 => terminal::Color::Red,
Expand All @@ -35,9 +30,9 @@ fn gen_random_ansi_color(rng: &mut Rng) -> terminal::Color {
}
}

fn gen_random_rgb_color(rng: &mut Rng, palette: Palette) -> terminal::Color {
let hue = rng.gen_range_float(palette.get_hue_range());
let lightness = rng.gen_range_float(palette.get_lightness_range());
fn gen_random_rgb_color(palette: Palette) -> terminal::Color {
let hue = rng::gen_range_float(palette.get_hue_range());
let lightness = rng::gen_range_float(palette.get_lightness_range());

let oklch = tincture::Oklch {
l: lightness,
Expand Down
5 changes: 2 additions & 3 deletions crates/model/src/pipe/kind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rng::Rng;
use std::num::NonZeroUsize;
use std::str::FromStr;

Expand Down Expand Up @@ -130,8 +129,8 @@ impl KindSet {
Self(vec![kind])
}

pub fn choose_random(&self, rng: &mut Rng) -> Kind {
let idx = rng.gen_range(0..self.0.len() as u32);
pub fn choose_random(&self) -> Kind {
let idx = rng::gen_range(0..self.0.len() as u32);
self.0[idx as usize]
}

Expand Down
19 changes: 5 additions & 14 deletions crates/pipes-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ pub use config::Config;

use model::pipe::{KindSet, Pipe};
use model::position::InScreenBounds;
use rng::Rng;
use std::thread;
use terminal::{Backend, Event, Terminal};

pub struct App<B: Backend> {
terminal: Terminal<B>,
rng: Rng,
config: Config,
kinds: KindSet,
}
Expand All @@ -21,11 +19,8 @@ impl<B: Backend> App<B> {
let largest_custom_width = kinds.custom_widths().max();
let terminal = Terminal::new(backend, kinds.chars(), largest_custom_width)?;

let rng = Rng::new()?;

Ok(Self {
terminal,
rng,
config,
kinds,
})
Expand Down Expand Up @@ -91,15 +86,12 @@ impl<B: Backend> App<B> {
}

fn tick_pipe(&mut self, pipe: &mut Pipe) {
let InScreenBounds(stayed_onscreen) = pipe.tick(
self.terminal.size(),
&mut self.rng,
self.config.turn_chance(),
);
let InScreenBounds(stayed_onscreen) =
pipe.tick(self.terminal.size(), self.config.turn_chance());

if !stayed_onscreen {
*pipe = if self.config.inherit_style() {
pipe.dup(self.terminal.size(), &mut self.rng)
pipe.dup(self.terminal.size())
} else {
self.create_pipe()
};
Expand All @@ -113,7 +105,7 @@ impl<B: Backend> App<B> {
self.terminal.set_text_color(color)?;
}

self.terminal.print(if self.rng.gen_bool(0.99999) {
self.terminal.print(if rng::gen_bool(0.99999) {
pipe.to_char()
} else {
'🦀'
Expand All @@ -129,11 +121,10 @@ impl<B: Backend> App<B> {
}

fn create_pipe(&mut self) -> Pipe {
let kind = self.kinds.choose_random(&mut self.rng);
let kind = self.kinds.choose_random();

Pipe::new(
self.terminal.size(),
&mut self.rng,
self.config.color_mode(),
self.config.palette(),
kind,
Expand Down
Loading

0 comments on commit fbced0f

Please sign in to comment.