improvements

This commit is contained in:
Edgar 2022-11-02 11:08:23 +01:00
parent 8125833ccd
commit 56388ff9d1
No known key found for this signature in database
5 changed files with 52 additions and 26 deletions

2
.gitignore vendored
View file

@ -3,4 +3,4 @@
server/ server/
TODO.md TODO.md
test.map *.map

View file

@ -19,8 +19,11 @@ struct Cli {
/// The seed used when generating a map. By default a random one. /// The seed used when generating a map. By default a random one.
#[arg(short, long)] #[arg(short, long)]
seed: Option<String>, seed: Option<String>,
/// The mapres directory.
#[arg(short, long, default_value = "mapres")]
mapres: PathBuf,
/// The output map file. /// The output map file.
#[arg(short, long)] #[arg(short, long, default_value = "generated.map")]
output: PathBuf, output: PathBuf,
#[command(subcommand)] #[command(subcommand)]
command: Commands, command: Commands,
@ -37,8 +40,8 @@ enum Commands {
impl Commands { impl Commands {
pub fn print(&self) { pub fn print(&self) {
let name = match self { let name = match self {
Self::Fly => "Maze", Self::Fly => "Fly",
Self::Maze => "Fly", Self::Maze => "Maze",
}; };
println!("Selected map generator: {}", name.purple().bold()); println!("Selected map generator: {}", name.purple().bold());
} }
@ -66,7 +69,11 @@ pub fn run_cli() -> Result<()> {
cli.command.print(); cli.command.print();
match cli.command { match cli.command {
Commands::Maze => MazeGenerator::save_file(&mut rng, cli.width, cli.height, &cli.output), Commands::Maze => {
Commands::Fly => FlyGenerator::save_file(&mut rng, cli.width, cli.height, &cli.output), 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)
}
} }
} }

View file

@ -6,8 +6,13 @@ use rand::Rng;
pub struct FlyGenerator; pub struct FlyGenerator;
impl MapGenerator for FlyGenerator { impl MapGenerator for FlyGenerator {
fn generate<R: Rng + ?Sized>(rng: &mut R, width: usize, height: usize) -> Result<TwMap> { fn generate<R: Rng + ?Sized>(
let mut map = create_initial_map()?; 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), || { let mut tiles = Array2::from_shape_simple_fn((height, width), || {
GameTile::new(TILE_EMPTY, TileFlags::empty()) GameTile::new(TILE_EMPTY, TileFlags::empty())

View file

@ -6,7 +6,12 @@ use ndarray::Array2;
pub struct MazeGenerator; pub struct MazeGenerator;
impl MapGenerator for 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. // Must be odd.
let width = { let width = {
if width % 2 == 0 { 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 maze = Maze::new(width, height).unwrap().generate(rng);
let hookable_tiles = let hookable_tiles = Array2::from_shape_fn((height, width), |(y, x)| {
Array2::from_shape_fn((height, width), |(y, x)| { let mut t = 0;
let mut t = 0; if maze[x][y] == 1 {
if maze[x][y] == 1 { t = 9;
t = 9; }
} Tile::new(t, TileFlags::empty())
Tile::new(t, TileFlags::empty()) });
});
let mut tiles = Array2::from_shape_fn((width, height), |(y, x)| { let mut tiles = Array2::from_shape_fn((width, height), |(y, x)| {
GameTile::new(maze[x][y], TileFlags::empty()) GameTile::new(maze[x][y], TileFlags::empty())

View file

@ -19,35 +19,41 @@ pub const TILE_FINISH: u8 = 34;
pub const TILE_SPAWN: u8 = 192; pub const TILE_SPAWN: u8 = 192;
pub trait MapGenerator { 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>( fn save_file<R: Rng + ?Sized>(
rng: &mut R, rng: &mut R,
mapres: &Path,
width: usize, width: usize,
height: usize, height: usize,
path: &Path, path: &Path,
) -> Result<()> { ) -> Result<()> {
let mut map = Self::generate(rng, width, height)?; let mut map = Self::generate(rng, mapres, width, height)?;
map.save_file(path)?; map.save_file(path)?;
Ok(()) Ok(())
} }
} }
pub fn create_initial_map() -> Result<TwMap> { pub fn create_initial_map(mapres: &Path) -> Result<TwMap> {
let mut map = TwMap::empty(Version::DDNet06); let mut map = TwMap::empty(Version::DDNet06);
map.info.author = "github.com/edg-l/ddnet-map-gen".to_string(); 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.credits = "github.com/edg-l/ddnet-map-gen".to_string();
//map.info.version =
map.images.push(Image::External(ExternalImage { map.images.push(Image::External(ExternalImage {
name: "generic_unhookable".to_string(), name: "generic_unhookable".to_string(),
size: Point::new_same(1024), size: Point::new_same(1024),
})); }));
map.images.push(Image::Embedded(EmbeddedImage::from_file( map.images.push(Image::Embedded(EmbeddedImage::from_file(
"mapres/basic_freeze.png", mapres.join("basic_freeze.png"),
)?)); )?));
Ok(map) Ok(map)
} }
// Creates the sky quad from the editor. // Creates the sky quad from the editor.
pub fn quads_sky() -> Group { pub fn quads_sky() -> Group {
let mut quads_group = Group::default(); let mut quads_group = Group::default();
@ -55,7 +61,11 @@ pub fn quads_sky() -> Group {
quads_group.parallax.x = 0; quads_group.parallax.x = 0;
quads_group.parallax.y = 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 = [ quad.colors = [
Color { Color {
r: 94, r: 94,