overmap, refactor offsets

This commit is contained in:
Llywelwyn 2023-08-26 22:46:04 +01:00
parent 9e294a1680
commit 746de971f0
14 changed files with 322 additions and 95 deletions

View file

@ -8,7 +8,7 @@ pub use interval_spawning_system::try_spawn_interval;
pub mod dungeon;
pub use dungeon::{ level_transition, MasterDungeonMap };
pub mod themes;
use crate::data::visuals::MAX_COLOUR_OFFSET_PERCENT;
use crate::data::visuals::MAX_COLOUR_OFFSET;
// FIXME: If the map size gets too small, entities stop being rendered starting from the right.
// i.e. on a map size of 40*40, only entities to the left of the player are rendered.
@ -16,6 +16,7 @@ use crate::data::visuals::MAX_COLOUR_OFFSET_PERCENT;
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct Map {
pub overmap: bool,
pub tiles: Vec<TileType>,
pub width: i32,
pub height: i32,
@ -23,7 +24,7 @@ pub struct Map {
pub visible_tiles: Vec<bool>,
pub lit_tiles: Vec<bool>,
pub telepath_tiles: Vec<bool>,
pub colour_offset: Vec<(f32, f32, f32)>,
pub colour_offset: Vec<(i32, i32, i32)>,
pub additional_fg_offset: rltk::RGB,
pub id: i32,
pub name: String,
@ -37,10 +38,11 @@ impl Map {
(y as usize) * (self.width as usize) + (x as usize)
}
pub fn new<S: ToString>(new_id: i32, width: i32, height: i32, difficulty: i32, name: S) -> Map {
pub fn new<S: ToString>(overmap: bool, new_id: i32, width: i32, height: i32, difficulty: i32, name: S) -> Map {
let map_tile_count = (width * height) as usize;
crate::spatial::set_size(map_tile_count);
let mut map = Map {
overmap: overmap,
tiles: vec![TileType::Wall; map_tile_count],
width: width,
height: height,
@ -48,11 +50,11 @@ impl Map {
visible_tiles: vec![false; map_tile_count],
lit_tiles: vec![true; map_tile_count], // NYI: Light sources. Once those exist, we can set this to false.
telepath_tiles: vec![false; map_tile_count],
colour_offset: vec![(1.0, 1.0, 1.0); map_tile_count],
colour_offset: vec![(0, 0, 0); map_tile_count],
additional_fg_offset: rltk::RGB::from_u8(
MAX_COLOUR_OFFSET_PERCENT as u8,
MAX_COLOUR_OFFSET_PERCENT as u8,
MAX_COLOUR_OFFSET_PERCENT as u8
MAX_COLOUR_OFFSET as u8,
MAX_COLOUR_OFFSET as u8,
MAX_COLOUR_OFFSET as u8
),
id: new_id,
name: name.to_string(),
@ -61,16 +63,13 @@ impl Map {
view_blocked: HashSet::new(),
};
const TWICE_OFFSET: i32 = MAX_COLOUR_OFFSET_PERCENT * 2;
const TWICE_OFFSET: i32 = MAX_COLOUR_OFFSET * 2;
let mut rng = rltk::RandomNumberGenerator::new();
for idx in 0..map.colour_offset.len() {
let red_roll: f32 =
((rng.roll_dice(1, TWICE_OFFSET - 1) + 1 - MAX_COLOUR_OFFSET_PERCENT) as f32) / 100f32 + 1.0;
let green_roll: f32 =
((rng.roll_dice(1, TWICE_OFFSET - 1) + 1 - MAX_COLOUR_OFFSET_PERCENT) as f32) / 100f32 + 1.0;
let blue_roll: f32 =
((rng.roll_dice(1, TWICE_OFFSET - 1) + 1 - MAX_COLOUR_OFFSET_PERCENT) as f32) / 100f32 + 1.0;
let red_roll: i32 = rng.roll_dice(1, TWICE_OFFSET) - MAX_COLOUR_OFFSET;
let blue_roll: i32 = rng.roll_dice(1, TWICE_OFFSET) - MAX_COLOUR_OFFSET;
let green_roll: i32 = rng.roll_dice(1, TWICE_OFFSET) - MAX_COLOUR_OFFSET;
map.colour_offset[idx] = (red_roll, green_roll, blue_roll);
}

View file

@ -11,8 +11,8 @@ pub fn get_tile_renderables_for_id(
other_pos: Option<Point>,
debug: Option<bool>
) -> (rltk::FontCharType, RGB, RGB) {
let (glyph, mut fg, mut bg) = match map.id {
2 => get_forest_theme_renderables(idx, map, debug),
let (glyph, mut fg, mut bg, offset_mod) = match map.id {
3 => get_forest_theme_renderables(idx, map, debug),
_ => get_default_theme_renderables(idx, map, debug),
};
@ -24,7 +24,7 @@ pub fn get_tile_renderables_for_id(
}
fg = fg.add(map.additional_fg_offset);
(fg, bg) = apply_colour_offset(fg, bg, map, idx);
(fg, bg) = apply_colour_offset(fg, bg, map, idx, offset_mod);
if CONFIG.visuals.with_scanlines && WITH_SCANLINES_BRIGHTEN_AMOUNT > 0.0 {
(fg, bg) = brighten_by(fg, bg, WITH_SCANLINES_BRIGHTEN_AMOUNT);
}
@ -60,12 +60,13 @@ pub fn get_tile_renderables_for_id(
}
#[rustfmt::skip]
pub fn get_default_theme_renderables(idx: usize, map: &Map, debug: Option<bool>) -> (rltk::FontCharType, RGB, RGB) {
pub fn get_default_theme_renderables(idx: usize, map: &Map, debug: Option<bool>) -> (rltk::FontCharType, RGB, RGB, (f32, f32, f32)) {
let glyph: rltk::FontCharType;
#[allow(unused_assignments)]
let mut fg: RGB = RGB::new();
#[allow(unused_assignments)]
let mut bg: RGB = RGB::new();
let mut offset_mod: (f32, f32, f32) = (0.5, 0.2, 0.0);
match map.tiles[idx] {
TileType::Floor => { glyph = rltk::to_cp437(FLOOR_GLYPH); fg = RGB::named(FLOOR_COLOUR); bg = RGB::named(DEFAULT_BG_COLOUR); }
@ -84,26 +85,28 @@ pub fn get_default_theme_renderables(idx: usize, map: &Map, debug: Option<bool>)
TileType::ShallowWater => { glyph = rltk::to_cp437(SHALLOW_WATER_GLYPH); bg = RGB::named(SHALLOW_WATER_COLOUR); }
TileType::DeepWater => { glyph = rltk::to_cp437(DEEP_WATER_GLYPH); bg = RGB::named(DEEP_WATER_COLOUR); }
TileType::Bars => { glyph = rltk::to_cp437(BARS_GLYPH); fg = RGB::named(BARS_COLOUR); bg = RGB::named(FLOOR_COLOUR); }
TileType::ImpassableMountain => { glyph = rltk::to_cp437(IMPASSABLE_MOUNTAIN_GLYPH); bg = RGB::named(IMPASSABLE_MOUNTAIN_COLOUR); fg = RGB::named((20, 20, 20)) }
}
return (glyph, fg, bg);
return (glyph, fg, bg, offset_mod);
}
#[rustfmt::skip]
fn get_forest_theme_renderables(idx:usize, map: &Map, debug: Option<bool>) -> (rltk::FontCharType, RGB, RGB) {
fn get_forest_theme_renderables(idx:usize, map: &Map, debug: Option<bool>) -> (rltk::FontCharType, RGB, RGB, (f32, f32, f32)) {
let glyph;
#[allow(unused_assignments)]
let mut fg = RGB::new();
#[allow(unused_assignments)]
let mut bg = RGB::new();
let mut offset_mod: (f32, f32, f32) = (1.0, 1.0, 1.0);
match map.tiles[idx] {
TileType::Wall => { glyph = rltk::to_cp437(FOREST_WALL_GLYPH); fg = RGB::named(FOREST_WALL_COLOUR); bg = RGB::named(GRASS_COLOUR) }
TileType::Road => { glyph = rltk::to_cp437(ROAD_GLYPH); bg = RGB::named(ROAD_COLOUR); }
TileType::ShallowWater => { glyph = rltk::to_cp437(SHALLOW_WATER_GLYPH); bg = RGB::named(SHALLOW_WATER_COLOUR); }
_ => { (glyph, fg, _) = get_default_theme_renderables(idx, map, debug); bg = RGB::named(GRASS_COLOUR) }
_ => { (glyph, fg, _, offset_mod) = get_default_theme_renderables(idx, map, debug); bg = RGB::named(GRASS_COLOUR) }
}
(glyph, fg, bg)
(glyph, fg, bg, offset_mod)
}
fn is_revealed_and_wall(map: &Map, x: i32, y: i32, debug: Option<bool>) -> bool {
@ -248,10 +251,17 @@ fn wall_glyph(map: &Map, x: i32, y: i32, debug: Option<bool>) -> rltk::FontCharT
}
}
fn apply_colour_offset(mut fg: RGB, mut bg: RGB, map: &Map, idx: usize) -> (RGB, RGB) {
let offsets = map.colour_offset[idx];
fg = multiply_by_float(fg.add(map.additional_fg_offset), offsets);
bg = multiply_by_float(bg, offsets);
fn apply_colour_offset(mut fg: RGB, mut bg: RGB, map: &Map, idx: usize, offset_mod: (f32, f32, f32)) -> (RGB, RGB) {
let mut offsets = map.colour_offset[idx];
let mut additional_fg_offset = map.additional_fg_offset;
offsets.0 = ((offsets.0 as f32) * offset_mod.0) as i32;
offsets.1 = ((offsets.1 as f32) * offset_mod.1) as i32;
offsets.2 = ((offsets.2 as f32) * offset_mod.2) as i32;
additional_fg_offset.r *= offset_mod.0;
additional_fg_offset.g *= offset_mod.1;
additional_fg_offset.b *= offset_mod.2;
fg = add_i32_offsets(fg.add(additional_fg_offset), offsets);
bg = add_i32_offsets(bg, offsets);
return (fg, bg);
}
@ -262,6 +272,14 @@ fn apply_bloodstain_if_necessary(mut bg: RGB, map: &Map, idx: usize) -> RGB {
return bg;
}
pub fn add_i32_offsets(rgb: RGB, offsets: (i32, i32, i32)) -> RGB {
let r = rgb.r + (offsets.0 as f32) / 255.0;
let g = rgb.g + (offsets.1 as f32) / 255.0;
let b = rgb.b + (offsets.2 as f32) / 255.0;
return rltk::RGB::from_f32(r, g, b);
}
pub fn multiply_by_float(rgb: rltk::RGB, offsets: (f32, f32, f32)) -> RGB {
let r = rgb.r * offsets.0;
let g = rgb.g * offsets.1;

View file

@ -3,6 +3,7 @@ use serde::{ Deserialize, Serialize };
#[derive(PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
pub enum TileType {
// Walls (opaque)
ImpassableMountain,
Wall,
// Impassable (transparent)
DeepWater,
@ -44,6 +45,7 @@ pub fn tile_walkable(tt: TileType) -> bool {
pub fn tile_opaque(tt: TileType) -> bool {
match tt {
TileType::ImpassableMountain => true,
TileType::Wall => true,
_ => false,
}