Skip to content

Commit

Permalink
Update platform generation
Browse files Browse the repository at this point in the history
- cleanup code
- fix chunked rendering issues
- add ui for platform distances
- migrate configs
  • Loading branch information
iMilchshake committed Apr 19, 2024
1 parent 429762b commit dfca940
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 31 deletions.
4 changes: 4 additions & 0 deletions configs/cringe4teero.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
12,
3
],
"platform_distance_bounds": [
500,
750
],
"waypoints": [
{
"x": 250,
Expand Down
6 changes: 5 additions & 1 deletion configs/cringe4teero2.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
8,
4
],
"platform_distance_bounds": [
500,
750
],
"waypoints": [
{
"x": 250,
Expand All @@ -41,4 +45,4 @@
"y": 50
}
]
}
}
48 changes: 48 additions & 0 deletions configs/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "default",
"description": null,
"inner_size_bounds": [
3,
3
],
"outer_size_bounds": [
1,
5
],
"inner_rad_mut_prob": 0.25,
"inner_size_mut_prob": 0.5,
"outer_rad_mut_prob": 0.25,
"outer_size_mut_prob": 0.5,
"step_weights": [
20,
11,
10,
9
],
"platform_distance_bounds": [
500,
750
],
"waypoints": [
{
"x": 250,
"y": 250
},
{
"x": 250,
"y": 150
},
{
"x": 50,
"y": 150
},
{
"x": 50,
"y": 50
},
{
"x": 250,
"y": 50
}
]
}
4 changes: 4 additions & 0 deletions configs/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
7,
10
],
"platform_distance_bounds": [
500,
750
],
"waypoints": [
{
"x": 250,
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Default for GenerationConfig {
Position::new(250, 50),
],
step_weights: vec![20, 11, 10, 9],
platform_distance_bounds: (1, 200),
platform_distance_bounds: (500, 750),
}
}
}
8 changes: 8 additions & 0 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ impl Editor {
true,
);

field_edit_widget(
ui,
&mut self.config.platform_distance_bounds,
edit_range_usize,
"platform distances",
true,
);

// only show these in setup mode
ui.add_visible_ui(self.is_setup(), |ui| {
vec_edit_widget(
Expand Down
15 changes: 6 additions & 9 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ impl Generator {
self.walker
.probabilistic_step(&mut self.map, &mut self.rnd)?;

// TODO: this is terrible
if self.walker.steps_since_platform > config.platform_distance_bounds.1 {
self.walker.check_platform(&mut self.map, true);
} else if self.walker.steps_since_platform > config.platform_distance_bounds.0 {
// self.walker.check_platform(&mut self.map, false);
self.walker.steps_since_platform += 1;
} else {
self.walker.steps_since_platform += 1;
}
// handle platforms
self.walker.check_platform(
&mut self.map,
config.platform_distance_bounds.0,
config.platform_distance_bounds.1,
);
}

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion src/grid_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ pub fn draw_chunked_grid(
}
}
} else {
let mut color = blocktype_to_color(&BlockType::Hookable); // assumed that initial value is hookable
color.a = 0.95;
draw_rectangle(
(x_chunk * chunk_size) as f32,
(y_chunk * chunk_size) as f32,
chunk_size as f32,
chunk_size as f32,
blocktype_to_color(&BlockType::Hookable), // assumed that initial value is hookable
color,
);
}
}
Expand Down
36 changes: 22 additions & 14 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{position::Position, walker::CuteWalker};
use ndarray::{s, Array2};
use rand_distr::num_traits::ToPrimitive;
use std::path::PathBuf;
use twmap::{GameLayer, GameTile, TileFlags, TilemapLayer, TwMap};

Expand Down Expand Up @@ -131,47 +132,47 @@ impl Map {

pub fn generate_room(&mut self, pos: &Position, margin: usize, zone_type: Option<&BlockType>) {
// TODO: ensure valid position?
// TODO: use new shift method here

let margin: i32 = margin.to_i32().unwrap();

// carve room
self.set_area(
&Position::new(pos.x - margin, pos.y - margin),
&Position::new(pos.x + margin, pos.y + margin),
&pos.shifted_by(-margin, -margin),
&pos.shifted_by(margin, margin),
&BlockType::Empty,
true,
);

// set platform
self.set_area(
&Position::new(pos.x - (margin - 2), pos.y),
&Position::new(pos.x + (margin - 2), pos.y),
&pos.shifted_by(-(margin - 2), 1),
&pos.shifted_by(margin - 2, 1),
&BlockType::Platform,
true,
);

// set spawns
if zone_type == Some(&BlockType::Start) {
self.set_area(
&Position::new(pos.x - (margin - 2), pos.y - 1),
&Position::new(pos.x + (margin - 2), pos.y - 1),
&pos.shifted_by(-(margin - 2), 0),
&pos.shifted_by(margin - 2, 0),
&BlockType::Spawn,
true,
);
}

// set start/finish line
if let Some(zone_type) = zone_type {
self.set_area_border(
&Position::new(pos.x - margin - 1, pos.y - margin - 1),
&Position::new(pos.x + margin + 1, pos.y + margin + 1),
&pos.shifted_by(-margin - 1, -margin - 1),
&pos.shifted_by(margin + 1, margin + 1),
zone_type,
false,
);
}
}

fn pos_to_chunk_pos(&self, pos: Position) -> Position {
Position::new(pos.x / CHUNK_SIZE, pos.y / CHUNK_SIZE)
Position::new(pos.x / self.chunk_size, pos.y / self.chunk_size)
}

pub fn export(&self, path: &PathBuf) {
Expand Down Expand Up @@ -246,15 +247,22 @@ impl Map {
return;
}

let chunk_size = self.chunk_size;

let mut view = self
.grid
.slice_mut(s![top_left.x..bot_right.x + 1, top_left.y..bot_right.y + 1]);
view.map_inplace(|current_value| {
.slice_mut(s![top_left.x..=bot_right.x, top_left.y..=bot_right.y]);

for ((x, y), current_value) in view.indexed_iter_mut() {
if overide || *current_value == BlockType::Empty || *current_value == BlockType::Freeze
{
*current_value = value.clone();

let chunk_pos =
Position::new((top_left.x + x) / chunk_size, (top_left.y + y) / chunk_size);
self.chunk_edited[chunk_pos.as_index()] = true;
}
});
}
}

/// sets the outline of an area define by two positions
Expand Down
18 changes: 13 additions & 5 deletions src/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,25 @@ impl CuteWalker {

/// will try to place a platform at the walkers position.
/// If force is true it will enforce a platform.
pub fn check_platform(&mut self, map: &mut Map, force: bool) {
pub fn check_platform(&mut self, map: &mut Map, min_distance: usize, max_distance: usize) {
self.steps_since_platform += 1;

// Case 1: min distance is not reached -> skip
if self.steps_since_platform < min_distance {
return;
}

let walker_pos = self.pos.clone();

if force {
map.generate_room(&walker_pos, 5, None);
// Case 2: max distance has been exceeded -> force platform using a room
if self.steps_since_platform > max_distance {
// TODO: for now this is hardcoded so that platform is shifted down by 7 blocks.
map.generate_room(&walker_pos.shifted_by(0, 7), 4, None);
self.steps_since_platform = 0;
return;
}

// Case 3: min distance has been exceeded -> Try to place platform, but only if possible
let area_empty = map.check_area_all(
&walker_pos.shifted_by(-2, -3),
&walker_pos.shifted_by(2, 1),
Expand All @@ -82,8 +92,6 @@ impl CuteWalker {
);
self.steps_since_platform = 0;
}

self.steps_since_platform += 1;
}

pub fn probabilistic_step(
Expand Down

0 comments on commit dfca940

Please sign in to comment.