Permalink
Browse files

Bring back random tree colour generation

Wasm can't use thread_rng so we're creating a new "one-off" rng for
things that shouldn't affect the replay.
  • Loading branch information...
tomassedovic committed Dec 22, 2017
1 parent 6707b53 commit 8184ab306f0373b9a136750482e25e7bb740d27a
Showing with 16 additions and 12 deletions.
  1. +12 −4 src/generators/forrest.rs
  2. +1 −6 src/level.rs
  3. +1 −1 src/state.rs
  4. +2 −1 src/world.rs
@@ -1,5 +1,6 @@
use generators::GeneratedWorld;

use color;
use formula;
use item::{self, Item};
use level::{Tile, TileKind};
@@ -12,7 +13,7 @@ use rand::distributions::{IndependentSample, Weighted, WeightedChoice};
// TODO: Instead of `map_size`, use a Rectangle with the world
// positions here. We want to expose the non-world coordinates in as
// few places as possible.
fn generate_map<R: Rng>(rng: &mut R, map_size: Point, player_pos: Point) -> Vec<(Point, Tile)> {
fn generate_map<R: Rng, G: Rng>(rng: &mut R, one_off_rng: &mut G, map_size: Point, player_pos: Point) -> Vec<(Point, Tile)> {
let mut weights = [
Weighted {
weight: 610,
@@ -37,7 +38,14 @@ fn generate_map<R: Rng>(rng: &mut R, map_size: Point, player_pos: Point) -> Vec<
true => TileKind::Empty,
false => opts.ind_sample(rng),
};
result.push((Point::new(x, y), Tile::new(kind)));

let mut tile = Tile::new(kind);
if tile.kind == TileKind::Tree {
let options = [color::tree_1, color::tree_2, color::tree_3];
tile.fg_color = *one_off_rng.choose(&options).unwrap();
}

result.push((Point::new(x, y), tile));
}
}
result
@@ -202,8 +210,8 @@ fn generate_items<R: Rng>(rng: &mut R, map: &[(Point, Tile)]) -> Vec<(Point, Ite
}


pub fn generate<R: Rng>(rng: &mut R, size: Point, player: Point) -> GeneratedWorld {
let map = generate_map(rng, size, player);
pub fn generate<R: Rng, G: Rng>(rng: &mut R, one_off_rng: &mut G, size: Point, player: Point) -> GeneratedWorld {
let map = generate_map(rng, one_off_rng, size, player);
let monsters = generate_monsters(rng, &map);
let items = generate_items(rng, &map);
(map, monsters, items)
@@ -4,7 +4,6 @@ use graphics::Render;
use item::Item;
use point;

use rand::{self, Rng};
use std::collections::HashMap;
use std::time::Duration;

@@ -46,11 +45,7 @@ impl Tile {
pub fn new(kind: TileKind) -> Tile {
let color = match kind {
TileKind::Empty => color::empty_tile,
TileKind::Tree => {
let options = [color::tree_1, color::tree_2, color::tree_3];
//*rand::thread_rng().choose(&options).unwrap()
color::tree_1
}
TileKind::Tree => color::tree_1,
};
Tile {
kind,
@@ -5,7 +5,7 @@ use monster;
use player::Player;
use point::Point;
use util;
use rand::{self, IsaacRng, SeedableRng};
use rand::{IsaacRng, SeedableRng};

use stats::Stats;
use std::collections::VecDeque;
@@ -37,8 +37,9 @@ impl Chunk {
monsters: vec![],
};

let mut one_off_rng = chunk.rng.clone();
let generated_data =
generators::forrest::generate(&mut chunk.rng, chunk.level.size(), player_position);
generators::forrest::generate(&mut chunk.rng, &mut one_off_rng, chunk.level.size(), player_position);

chunk.populate(generated_data);

0 comments on commit 8184ab3

Please sign in to comment.