Skip to content

Commit

Permalink
Add kernel size fade in
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed Jun 2, 2024
1 parent 3b04eb0 commit 49f045d
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 105 deletions.
10 changes: 7 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,13 @@ pub struct GenerationConfig {

/// TODO:
pub pulse_straight_delay: usize,

/// TODO:
pub pulse_corner_delay: usize,
pub pulse_max_kernel_size: usize,

/// TODO:
pub pulse_max_kernel_size: usize,
pub fade_steps: usize,
pub fade_max_size: usize,
pub fade_min_size: usize,
}

impl GenerationConfig {
Expand Down Expand Up @@ -197,6 +198,9 @@ impl Default for GenerationConfig {
pulse_corner_delay: 5,
pulse_straight_delay: 10,
pulse_max_kernel_size: 4,
fade_steps: 60,
fade_max_size: 6,
fade_min_size: 3,
}
}
}
Expand Down
98 changes: 90 additions & 8 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::{
config::{GenerationConfig, MapConfig},
debug::DebugLayer,
kernel::Kernel,
map::{BlockType, Map},
map::{BlockType, Map, Overwrite},
position::Position,
post_processing as post,
random::{Random, Seed},
walker::CuteWalker,
};

use macroquad::color::colors;
use macroquad::{color::colors, miniquad::conf};

pub fn print_time(timer: &Timer, message: &str) {
println!("{}: {:?}", message, timer.elapsed());
Expand All @@ -30,6 +30,75 @@ pub struct Generator {
spawn: Position,
}

pub fn generate_room(
map: &mut Map,
pos: &Position,
room_size: usize,
platform_margin: usize,
zone_type: Option<&BlockType>,
) -> Result<(), &'static str> {
let room_size: i32 = room_size as i32;
let platform_margin: i32 = platform_margin as i32;

if !map.pos_in_bounds(&pos.shifted_by(room_size + 2, room_size + 1).unwrap())
|| !map.pos_in_bounds(&pos.shifted_by(room_size + 1, room_size + 1).unwrap())
{
return Err("generate room out of bounds");
}

// carve room
map.set_area_border(
&pos.shifted_by(-room_size, -room_size)?,
&pos.shifted_by(room_size, room_size)?,
&BlockType::Empty,
&Overwrite::Force,
);

// only reserve - 1 so that when this is used for platforms
map.set_area(
&pos.shifted_by(-room_size + 1, -room_size + 1)?,
&pos.shifted_by(room_size - 1, room_size - 1)?,
&BlockType::EmptyReserved,
&Overwrite::Force,
);

// set spawns
if zone_type == Some(&BlockType::Start) {
map.set_area(
&pos.shifted_by(-(room_size - platform_margin), room_size - 1)?,
&pos.shifted_by(room_size - platform_margin, room_size - 1)?,
&BlockType::Spawn,
&Overwrite::Force,
);
}

if let Some(zone_type) = zone_type {
map.set_area_border(
&pos.shifted_by(-room_size - 1, -room_size - 1)?,
&pos.shifted_by(room_size + 1, room_size + 1)?,
zone_type,
&Overwrite::ReplaceNonSolidForce,
);

map.set_area(
&pos.shifted_by(-(room_size - platform_margin), room_size + 1)?,
&pos.shifted_by(room_size - platform_margin, room_size + 1)?,
&BlockType::Platform,
&Overwrite::Force,
);
} else {
// set center platform
map.set_area(
&pos.shifted_by(-(room_size - platform_margin), room_size - 3)?,
&pos.shifted_by(room_size - platform_margin, room_size - 3)?,
&BlockType::Platform,
&Overwrite::Force,
);
}

Ok(())
}

impl Generator {
/// derive a initial generator state based on a GenerationConfig
pub fn new(gen_config: &GenerationConfig, map_config: &MapConfig, seed: Seed) -> Generator {
Expand Down Expand Up @@ -71,7 +140,16 @@ impl Generator {
config.validate()?;

// randomly mutate kernel
self.walker.mutate_kernel(config, &mut self.rnd);
if self.walker.steps > config.fade_steps {
self.walker.mutate_kernel(config, &mut self.rnd);
} else {
self.walker.set_fade_kernel(
self.walker.steps,
config.fade_min_size,
config.fade_max_size,
config.fade_steps,
);
}

// perform one step
self.walker
Expand All @@ -95,12 +173,16 @@ impl Generator {
self.debug_layers.get_mut("edge_bugs").unwrap().grid = edge_bugs;
print_time(&timer, "fix edge bugs");

self.map
.generate_room(&self.spawn, 4, 3, Some(&BlockType::Start))
generate_room(&mut self.map, &self.spawn, 6, 3, Some(&BlockType::Start))
.expect("start room generation failed");
self.map
.generate_room(&self.walker.pos.clone(), 4, 3, Some(&BlockType::Finish))
.expect("start finish room generation");
generate_room(
&mut self.map,
&self.walker.pos.clone(),
4,
3,
Some(&BlockType::Finish),
)
.expect("start finish room generation");
print_time(&timer, "place rooms");

if config.min_freeze_size > 0 {
Expand Down
102 changes: 21 additions & 81 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ impl BlockType {
}

pub enum Overwrite {
/// will replace EVERYTHING
/// Replace EVERYTHING
Force,

/// Replace Hookable+Freeze
ReplaceSolidFreeze,

/// Replace Hookable
ReplaceSolidOnly,

/// Replace Empty
ReplaceEmptyOnly,

/// Replace Freeze+Empty
ReplaceNonSolid,

/// Replace Freeze+Empty+EmptyReserved
ReplaceNonSolidForce,
}

impl Overwrite {
Expand All @@ -59,6 +69,10 @@ impl Overwrite {
Overwrite::ReplaceSolidOnly => matches!(&btype, BlockType::Hookable),
Overwrite::ReplaceEmptyOnly => matches!(&btype, BlockType::Empty),
Overwrite::ReplaceNonSolid => matches!(&btype, BlockType::Freeze | BlockType::Empty),
Overwrite::ReplaceNonSolidForce => matches!(
&btype,
BlockType::Freeze | BlockType::Empty | BlockType::EmptyReserved
),
}
}
}
Expand Down Expand Up @@ -106,11 +120,11 @@ impl Map {
}
}

pub fn update(
pub fn apply_kernel(
&mut self,
walker: &CuteWalker,
kernel: &Kernel,
kernel_type: KernelType,
block_type: BlockType,
) -> Result<(), &'static str> {
let offset: usize = kernel.size / 2; // offset of kernel wrt. position (top/left)
let extend: usize = kernel.size - offset; // how much kernel extends position (bot/right)
Expand All @@ -121,7 +135,7 @@ impl Map {
let exceeds_lower_bound = (walker.pos.y + extend) > self.height;

if exceeds_left_bound || exceeds_upper_bound || exceeds_right_bound || exceeds_lower_bound {
return Err("kernel out of bounds");
return Err("Kernel out of bounds");
}

let root_pos = Position::new(walker.pos.x - offset, walker.pos.y - offset);
Expand All @@ -130,17 +144,9 @@ impl Map {
if *kernel_active {
let current_type = &self.grid[absolute_pos.as_index()];

let new_type = match (&kernel_type, current_type) {
// inner kernel removes everything
(KernelType::Inner, BlockType::Hookable) => Some(BlockType::Empty),
(KernelType::Inner, BlockType::Freeze) => Some(BlockType::Empty),

// outer kernel will turn hookables to freeze
(KernelType::Outer, BlockType::Hookable) => Some(BlockType::Freeze),
(KernelType::Outer, BlockType::Freeze) => Some(BlockType::Freeze),

// ignore everything else
(_, _) => None,
let new_type = match current_type {
BlockType::Hookable | BlockType::Freeze => Some(block_type.clone()),
_ => None,
};

if let Some(new_type) = new_type {
Expand All @@ -155,72 +161,6 @@ impl Map {
Ok(())
}

pub fn generate_room(
&mut self,
pos: &Position,
room_size: usize,
platform_margin: usize,
zone_type: Option<&BlockType>,
) -> Result<(), &'static str> {
if pos.x < (room_size + 1)
|| pos.y < (room_size + 1)
|| pos.x > self.width - (room_size + 1)
|| pos.y > self.height - (room_size + 1)
{
return Err("generate room out of bounds");
}

// TODO: i feel like this is utterly stupid
let room_size: i32 = room_size.to_i32().unwrap();
let platform_margin: i32 = platform_margin.to_i32().unwrap();

// carve room
self.set_area_border(
&pos.shifted_by(-room_size, -room_size)?,
&pos.shifted_by(room_size, room_size)?,
&BlockType::Empty,
&Overwrite::Force,
);

let inner_room_size = room_size - 1;
assert!(inner_room_size > 0);
self.set_area(
&pos.shifted_by(-inner_room_size, -inner_room_size)?,
&pos.shifted_by(inner_room_size, inner_room_size)?,
&BlockType::EmptyReserved,
&Overwrite::Force,
);

// set platform
self.set_area(
&pos.shifted_by(-(room_size - platform_margin), room_size - 3)?,
&pos.shifted_by(room_size - platform_margin, room_size - 3)?,
&BlockType::Platform,
&Overwrite::Force,
);

// set spawns
if zone_type == Some(&BlockType::Start) {
self.set_area(
&pos.shifted_by(-(room_size - platform_margin), room_size - 4)?,
&pos.shifted_by(room_size - platform_margin, room_size - 4)?,
&BlockType::Spawn,
&Overwrite::Force,
);
}
// set start/finish line
if let Some(zone_type) = zone_type {
self.set_area_border(
&pos.shifted_by(-room_size - 1, -room_size - 1)?,
&pos.shifted_by(room_size + 1, room_size + 1)?,
zone_type,
&Overwrite::ReplaceNonSolid,
);
}

Ok(())
}

fn pos_to_chunk_pos(&self, pos: Position) -> Position {
Position::new(pos.x / self.chunk_size, pos.y / self.chunk_size)
}
Expand Down
4 changes: 2 additions & 2 deletions src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fn blocktype_to_color(value: &BlockType) -> Color {
BlockType::EmptyReserved => Color::new(0.3, 0.0, 0.0, 0.1),
BlockType::Finish => Color::new(1.0, 0.1, 0.1, 0.8),
BlockType::Start => Color::new(0.1, 1.0, 0.1, 0.8),
BlockType::Spawn => Color::new(0.5, 0.5, 0.0, 0.8),
BlockType::Platform => Color::new(0.2, 0.2, 0.7, 0.8),
BlockType::Platform => Color::new(0.5, 0.5, 0.0, 0.8),
BlockType::Spawn => Color::new(0.2, 0.2, 0.7, 0.8),
}
}

Expand Down
Loading

0 comments on commit 49f045d

Please sign in to comment.