2022-12-31 13:03:24 +00:00
|
|
|
use std::{rc::Rc, collections::HashMap};
|
2022-12-31 07:54:18 +00:00
|
|
|
use rand::prelude::*;
|
|
|
|
|
|
|
|
use crate::generators::TownNameGenerator;
|
|
|
|
|
2022-12-30 20:30:12 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2022-12-30 20:13:35 +00:00
|
|
|
pub enum Terrain {
|
2022-12-30 20:30:12 +00:00
|
|
|
Void,
|
2022-12-30 20:13:35 +00:00
|
|
|
DeepOcean,
|
|
|
|
Ocean,
|
|
|
|
Beach,
|
|
|
|
Flats,
|
|
|
|
Hills,
|
|
|
|
Mountains,
|
|
|
|
HighMountains,
|
|
|
|
}
|
|
|
|
|
2022-12-30 20:30:12 +00:00
|
|
|
pub struct Town {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2022-12-31 13:03:24 +00:00
|
|
|
pub struct Tavern {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2022-12-30 20:30:12 +00:00
|
|
|
pub enum Structure {
|
2022-12-31 07:54:18 +00:00
|
|
|
Town(Rc<Town>),
|
2022-12-31 13:03:24 +00:00
|
|
|
Tavern(Rc<Tavern>),
|
2022-12-30 20:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct WorldCell {
|
|
|
|
pub terrain: Terrain,
|
|
|
|
}
|
|
|
|
|
2022-12-30 20:13:35 +00:00
|
|
|
pub struct World {
|
2022-12-30 20:30:12 +00:00
|
|
|
pub map: Vec<Vec<WorldCell>>,
|
2022-12-30 20:13:35 +00:00
|
|
|
pub size: usize,
|
2022-12-31 13:03:24 +00:00
|
|
|
pub structures: HashMap<[usize; 2], Structure>
|
2022-12-30 20:13:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 20:30:12 +00:00
|
|
|
impl WorldCell {
|
|
|
|
pub fn new(terrain: Terrain) -> WorldCell {
|
|
|
|
WorldCell {
|
|
|
|
terrain: terrain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 20:13:35 +00:00
|
|
|
impl World {
|
|
|
|
pub fn new(size: usize) -> World {
|
|
|
|
let mut map = Vec::new();
|
|
|
|
for _ in 0..size {
|
|
|
|
let mut row = Vec::new();
|
|
|
|
for _ in 0..size {
|
2022-12-30 20:30:12 +00:00
|
|
|
row.push(WorldCell::new(Terrain::Void));
|
2022-12-30 20:13:35 +00:00
|
|
|
}
|
|
|
|
map.push(row);
|
|
|
|
}
|
|
|
|
World {
|
|
|
|
map: map,
|
|
|
|
size: size,
|
2022-12-31 13:03:24 +00:00
|
|
|
structures: HashMap::new(),
|
2022-12-30 20:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-30 20:30:12 +00:00
|
|
|
|
|
|
|
pub fn add_structure(&mut self, x: usize, y: usize, structure: Structure) {
|
2022-12-31 13:03:24 +00:00
|
|
|
self.structures.insert([x, y], structure);
|
2022-12-30 20:30:12 +00:00
|
|
|
}
|
2022-12-31 08:38:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
pub fn get_town_location(&self, town: &Rc<Town>) -> [usize; 2] {
|
2022-12-31 13:03:24 +00:00
|
|
|
for (location, structure) in self.structures.iter() {
|
|
|
|
match structure {
|
|
|
|
Structure::Town(t) => {
|
|
|
|
if Rc::ptr_eq(t, town) {
|
|
|
|
return *location;
|
2022-12-31 08:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-31 13:03:24 +00:00
|
|
|
_ => {}
|
2022-12-31 08:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("Town not found");
|
|
|
|
}
|
2022-12-31 13:03:24 +00:00
|
|
|
|
|
|
|
pub fn get_tavern_location(&self, tavern: &Rc<Tavern>) -> [usize; 2] {
|
|
|
|
for (location, structure) in self.structures.iter() {
|
|
|
|
match structure {
|
|
|
|
Structure::Tavern(t) => {
|
|
|
|
if Rc::ptr_eq(t, tavern) {
|
|
|
|
return *location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic!("Tavern not found");
|
|
|
|
}
|
|
|
|
|
2022-12-30 20:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Terrain {
|
|
|
|
pub fn from_height(height: f64) -> Terrain {
|
|
|
|
if height < -2000.0 {
|
|
|
|
Terrain::DeepOcean
|
|
|
|
} else if height < 0.0 {
|
|
|
|
Terrain::Ocean
|
|
|
|
} else if height < 50.0 {
|
|
|
|
Terrain::Beach
|
|
|
|
} else if height < 1000.0 {
|
|
|
|
Terrain::Flats
|
|
|
|
} else if height < 2000.0 {
|
|
|
|
Terrain::Hills
|
|
|
|
} else if height < 3500.0 {
|
|
|
|
Terrain::Mountains
|
|
|
|
} else {
|
|
|
|
Terrain::HighMountains
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_color(self) -> [u8; 3] {
|
|
|
|
match self {
|
2022-12-30 20:30:12 +00:00
|
|
|
Terrain::Void => [0, 0, 0],
|
2022-12-30 20:13:35 +00:00
|
|
|
Terrain::DeepOcean => [20, 60, 255],
|
|
|
|
Terrain::Ocean => [20, 120, 255],
|
|
|
|
Terrain::Beach => [255, 255, 100],
|
|
|
|
Terrain::Flats => [0, 204, 0],
|
|
|
|
Terrain::Hills => [102, 153, 0],
|
|
|
|
Terrain::Mountains => [153, 153, 102],
|
|
|
|
Terrain::HighMountains => [230, 230, 230],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-30 20:30:12 +00:00
|
|
|
|
|
|
|
impl Town {
|
|
|
|
pub fn new() -> Town {
|
|
|
|
Town {
|
2022-12-31 07:54:18 +00:00
|
|
|
name: TownNameGenerator::name(),
|
2022-12-30 20:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-31 13:03:24 +00:00
|
|
|
|
|
|
|
impl Tavern {
|
|
|
|
pub fn new() -> Tavern {
|
|
|
|
Tavern {
|
|
|
|
name: TownNameGenerator::name(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|