mirror of
https://github.com/edg-l/ddnet-map-gen.git
synced 2024-09-19 01:02:23 +00:00
improvements
This commit is contained in:
parent
8125833ccd
commit
56388ff9d1
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -3,4 +3,4 @@
|
|||
server/
|
||||
TODO.md
|
||||
|
||||
test.map
|
||||
*.map
|
17
src/cli.rs
17
src/cli.rs
|
@ -19,8 +19,11 @@ struct Cli {
|
|||
/// The seed used when generating a map. By default a random one.
|
||||
#[arg(short, long)]
|
||||
seed: Option<String>,
|
||||
/// The mapres directory.
|
||||
#[arg(short, long, default_value = "mapres")]
|
||||
mapres: PathBuf,
|
||||
/// The output map file.
|
||||
#[arg(short, long)]
|
||||
#[arg(short, long, default_value = "generated.map")]
|
||||
output: PathBuf,
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
|
@ -37,8 +40,8 @@ enum Commands {
|
|||
impl Commands {
|
||||
pub fn print(&self) {
|
||||
let name = match self {
|
||||
Self::Fly => "Maze",
|
||||
Self::Maze => "Fly",
|
||||
Self::Fly => "Fly",
|
||||
Self::Maze => "Maze",
|
||||
};
|
||||
println!("Selected map generator: {}", name.purple().bold());
|
||||
}
|
||||
|
@ -66,7 +69,11 @@ pub fn run_cli() -> Result<()> {
|
|||
cli.command.print();
|
||||
|
||||
match cli.command {
|
||||
Commands::Maze => MazeGenerator::save_file(&mut rng, cli.width, cli.height, &cli.output),
|
||||
Commands::Fly => FlyGenerator::save_file(&mut rng, cli.width, cli.height, &cli.output),
|
||||
Commands::Maze => {
|
||||
MazeGenerator::save_file(&mut rng, &cli.mapres, cli.width, cli.height, &cli.output)
|
||||
}
|
||||
Commands::Fly => {
|
||||
FlyGenerator::save_file(&mut rng, &cli.mapres, cli.width, cli.height, &cli.output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,13 @@ use rand::Rng;
|
|||
pub struct FlyGenerator;
|
||||
|
||||
impl MapGenerator for FlyGenerator {
|
||||
fn generate<R: Rng + ?Sized>(rng: &mut R, width: usize, height: usize) -> Result<TwMap> {
|
||||
let mut map = create_initial_map()?;
|
||||
fn generate<R: Rng + ?Sized>(
|
||||
rng: &mut R,
|
||||
mapres: &Path,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Result<TwMap> {
|
||||
let mut map = create_initial_map(mapres)?;
|
||||
|
||||
let mut tiles = Array2::from_shape_simple_fn((height, width), || {
|
||||
GameTile::new(TILE_EMPTY, TileFlags::empty())
|
||||
|
@ -50,7 +55,7 @@ impl MapGenerator for FlyGenerator {
|
|||
direction_steps = rng.gen_range(1..=10);
|
||||
direction = rng.gen_range(-1..=1);
|
||||
}
|
||||
|
||||
|
||||
let width_change: i64 = rng.gen_range(-1..=1);
|
||||
center += direction;
|
||||
fly_width += width_change;
|
||||
|
|
|
@ -6,7 +6,12 @@ use ndarray::Array2;
|
|||
pub struct MazeGenerator;
|
||||
|
||||
impl MapGenerator for MazeGenerator {
|
||||
fn generate<R: Rng + ?Sized>(rng: &mut R, width: usize, height: usize) -> Result<TwMap> {
|
||||
fn generate<R: Rng + ?Sized>(
|
||||
rng: &mut R,
|
||||
mapres: &Path,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Result<TwMap> {
|
||||
// Must be odd.
|
||||
let width = {
|
||||
if width % 2 == 0 {
|
||||
|
@ -23,17 +28,16 @@ impl MapGenerator for MazeGenerator {
|
|||
}
|
||||
};
|
||||
|
||||
let mut map = create_initial_map()?;
|
||||
let mut map = create_initial_map(mapres)?;
|
||||
let maze = Maze::new(width, height).unwrap().generate(rng);
|
||||
|
||||
let hookable_tiles =
|
||||
Array2::from_shape_fn((height, width), |(y, x)| {
|
||||
let mut t = 0;
|
||||
if maze[x][y] == 1 {
|
||||
t = 9;
|
||||
}
|
||||
Tile::new(t, TileFlags::empty())
|
||||
});
|
||||
let hookable_tiles = Array2::from_shape_fn((height, width), |(y, x)| {
|
||||
let mut t = 0;
|
||||
if maze[x][y] == 1 {
|
||||
t = 9;
|
||||
}
|
||||
Tile::new(t, TileFlags::empty())
|
||||
});
|
||||
|
||||
let mut tiles = Array2::from_shape_fn((width, height), |(y, x)| {
|
||||
GameTile::new(maze[x][y], TileFlags::empty())
|
||||
|
|
|
@ -19,43 +19,53 @@ pub const TILE_FINISH: u8 = 34;
|
|||
pub const TILE_SPAWN: u8 = 192;
|
||||
|
||||
pub trait MapGenerator {
|
||||
fn generate<R: Rng + ?Sized>(rng: &mut R, width: usize, height: usize) -> Result<TwMap>;
|
||||
fn generate<R: Rng + ?Sized>(
|
||||
rng: &mut R,
|
||||
mapres: &Path,
|
||||
width: usize,
|
||||
height: usize,
|
||||
) -> Result<TwMap>;
|
||||
|
||||
fn save_file<R: Rng + ?Sized>(
|
||||
rng: &mut R,
|
||||
mapres: &Path,
|
||||
width: usize,
|
||||
height: usize,
|
||||
path: &Path,
|
||||
) -> Result<()> {
|
||||
let mut map = Self::generate(rng, width, height)?;
|
||||
let mut map = Self::generate(rng, mapres, width, height)?;
|
||||
map.save_file(path)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_initial_map() -> Result<TwMap> {
|
||||
pub fn create_initial_map(mapres: &Path) -> Result<TwMap> {
|
||||
let mut map = TwMap::empty(Version::DDNet06);
|
||||
map.info.author = "github.com/edg-l/ddnet-map-gen".to_string();
|
||||
map.info.credits = "github.com/edg-l/ddnet-map-gen".to_string();
|
||||
//map.info.version =
|
||||
map.images.push(Image::External(ExternalImage {
|
||||
name: "generic_unhookable".to_string(),
|
||||
size: Point::new_same(1024),
|
||||
}));
|
||||
map.images.push(Image::Embedded(EmbeddedImage::from_file(
|
||||
"mapres/basic_freeze.png",
|
||||
mapres.join("basic_freeze.png"),
|
||||
)?));
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
|
||||
// Creates the sky quad from the editor.
|
||||
pub fn quads_sky() -> Group {
|
||||
let mut quads_group = Group::default();
|
||||
let mut quads_layer = QuadsLayer::default();
|
||||
quads_group.parallax.x = 0;
|
||||
quads_group.parallax.y = 0;
|
||||
|
||||
let mut quad = Quad::new(Default::default(), Point::new(I17F15::from_num(50), I17F15::from_num(30))).unwrap();
|
||||
|
||||
let mut quad = Quad::new(
|
||||
Default::default(),
|
||||
Point::new(I17F15::from_num(50), I17F15::from_num(30)),
|
||||
)
|
||||
.unwrap();
|
||||
quad.colors = [
|
||||
Color {
|
||||
r: 94,
|
||||
|
|
Loading…
Reference in a new issue