Browse Source

Added worldgen and debug facilities

master
Graham Northup 3 years ago
parent
commit
511915410c
Signed by untrusted user: grissess GPG Key ID: 5D000E6F539376FB
  1. 2
      src/world/gen.rs
  2. 40
      src/world/mod.rs
  3. 34
      src/world/render.rs

2
src/world/gen.rs

@ -188,6 +188,7 @@ mod test
}
}
/*
#[test]
fn show_rooms()
{
@ -207,5 +208,6 @@ mod test
println!("");
}
}
*/
}

40
src/world/mod.rs

@ -1,7 +1,7 @@
pub mod render;
pub mod gen;
use rogue_util::{coord::*, grid::region::{Region, RegionConfig}};
use rogue_util::{coord::*, grid::{Grid, region::{Region, RegionConfig}}};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Content {
@ -37,7 +37,13 @@ pub struct World {
impl World {
pub fn new(seed: u64) -> World {
let start_region = RegionConfig::default().build().expect("Failed to make World");
let start_region = RegionConfig::default()
.with_grid_gen(Some(Box::new(move |_, r, o, d| {
let mut g = Grid::from_default(o, d).expect("Failed to generate Grid");
gen::carve(seed, &mut g, r);
g
})))
.build().expect("Failed to make World");
// TODO: Worldgen on start_region
World {
seed,
@ -54,3 +60,33 @@ impl World {
&mut self.regions[self.cur_region]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn room_align() -> std::io::Result<()> {
let mut w = World::new(0);
{
let r = w.region_mut();
let gs = r.grid_size();
r.get_or_create(V2i(0, 0));
r.get_or_create(V2i(0, gs.1));
r.get_or_create(V2i(gs.0, 0));
r.get_or_create(gs);
r.get_or_create(V2i(0, 0) - gs);
r.get_or_create(V2i(0, -gs.1));
r.get_or_create(V2i(-gs.0, 0));
}
let (width, height) = termion::terminal_size()?;
let rs = render::RenderState::new(V2i(width as isize, height as isize));
let mut out = std::io::stdout();
rs.render(w.region(), &mut render::InvertRenderer, &mut out)?;
rs.draw_unpop(w.region(), &mut out)?;
rs.draw_grid(w.region(), &mut out)
}
}

34
src/world/render.rs

@ -10,6 +10,7 @@ pub struct Neighbors<'a, T: Debug> {
ul: &'a T, u: &'a T, ur: &'a T,
l: &'a T, this: &'a T, r: &'a T,
dl: &'a T, d: &'a T, dr: &'a T,
this_exists: bool,
}
impl<'a, T: Debug + Default> Neighbors<'a, T> {
@ -24,6 +25,7 @@ impl<'a, T: Debug + Default> Neighbors<'a, T> {
dl: r.get(p + V2i(-1, 1)).unwrap_or(def),
d: r.get(p + V2i(0, 1)).unwrap_or(def),
dr: r.get(p + V2i(1, 1)).unwrap_or(def),
this_exists: r.get(p).is_some(),
}
}
}
@ -151,6 +153,38 @@ impl RenderState {
Ok(())
}
pub fn go_to_pt<W: Write>(&self, pt: V2i, out: &mut W) -> io::Result<()> {
let spt = pt - self.screen_rect().origin() + V2i(1, 1);
write!(out, "{}", termion::cursor::Goto(spt.0 as u16, spt.1 as u16))
}
pub fn draw_grid<W: Write>(&self, region: &Region<Tile>, out: &mut W) -> io::Result<()> {
let screen = self.screen_rect();
let gs = region.grid_size();
for pt in &screen {
if pt.0.rem_euclid(gs.0) == 0 || pt.1.rem_euclid(gs.1) == 0 {
self.go_to_pt(pt, out)?;
write!(out, "+")?;
}
}
Ok(())
}
pub fn draw_unpop<W: Write>(&self, region: &Region<Tile>, out: &mut W) -> io::Result<()> {
let screen = self.screen_rect();
for pt in &screen {
if !region.is_populated_tile(pt) {
self.go_to_pt(pt, out)?;
write!(out, "?")?;
}
}
Ok(())
}
}
#[cfg(test)]

Loading…
Cancel
Save