Skip to content

Commit

Permalink
Add CLI prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
iMilchshake committed Jan 2, 2025
1 parent b6c2ee0 commit 2f73771
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
24 changes: 22 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::{crate_version, Parser};

#[derive(Parser, Debug)]
#[command(name = "Random Gores Map Generator")]
#[command(name = "Random Gores Map Generator - Editor")]
#[command(version = crate_version!())]
#[command(about = "Visual editor for generating maps and customizing the generators presets", long_about = None)]
pub struct Args {
pub struct EditorArgs {
/// select initial generation config
pub gen_config: Option<String>,

Expand Down Expand Up @@ -35,3 +35,23 @@ pub struct Args {
#[arg(short = 'e', long, value_delimiter = ',', num_args = 1..)]
pub enable_layers: Option<Vec<String>>,
}

#[derive(Parser, Debug)]
#[command(name = "Random Gores Map Generator - CLI")]
#[command(version = crate_version!())]
#[command(about = "CLI for generating maps using generators presets", long_about = None)]
pub struct CLIArgs {
/// select initial generation config
pub gen_config_name: String,

/// select initial map config
pub map_config_name: String,

/// enable fixed seed
#[arg(short = 's', long = "seed")]
pub fixed_seed: Option<u64>,

/// The maximum amount of generation steps before generation stops
#[arg(long, default_value = "200000")]
pub max_gen_steps: usize,
}
58 changes: 58 additions & 0 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use clap::Parser;
use gores_mapgen::{
args::CLIArgs,
config::{GenerationConfig, MapConfig, ThemeConfig},
generator::Generator,
random::{Random, Seed},
};
use log::{info, warn};
use simple_logger::SimpleLogger;
use std::panic::{self};

fn main() {
let args = CLIArgs::parse();
SimpleLogger::new().init().unwrap();

let all_map_configs = MapConfig::get_all_configs();
let all_gen_configs = GenerationConfig::get_all_configs();

let map_config = all_map_configs
.iter()
.find(|c| c.name == args.map_config_name)
.unwrap_or_else(|| panic!("map config '{}' not found", args.map_config_name));

let gen_config = all_gen_configs
.iter()
.find(|c| c.name == args.gen_config_name)
.unwrap_or_else(|| panic!("gen config '{}' not found", args.map_config_name));

let seed = args.fixed_seed.unwrap_or(Random::get_u64_from_entropy());

// disable panic hook so they no longer get printed
// panic::set_hook(Box::new(|_info| {}));

let generation_result = panic::catch_unwind(|| {
Generator::generate_map(
args.max_gen_steps,
&Seed::from_u64(seed),
gen_config,
map_config,
&ThemeConfig::default(),
)
});

match generation_result {
// map was generated successfully
Ok(Ok(_map)) => {
info!("generation success!");
}
// no panic, but map generation failed
Ok(Err(generation_error)) => {
warn!("generation failed: {}", generation_error)
}
// map generation panic
Err(panic_info) => {
warn!("generation panicked: {:?}", panic_info)
}
}
}
4 changes: 2 additions & 2 deletions src/bin/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use clap::Parser;
use gores_mapgen::{
args::Args,
args::EditorArgs,
config::{GenerationConfig, MapConfig, ThemeConfig},
editor::*,
fps_control::*,
Expand Down Expand Up @@ -33,7 +33,7 @@ fn window_conf() -> Conf {
#[macroquad::main(window_conf)]
async fn main() {
// initialization
let args = Args::parse();
let args = EditorArgs::parse();
SimpleLogger::new().init().unwrap();
let mut editor = Editor::new(
GenerationConfig::get_initial_gen_config(),
Expand Down
4 changes: 2 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{path::PathBuf, str::FromStr};
const STEPS_PER_FRAME: usize = 50;

use crate::{
args::Args,
args::EditorArgs,
config::{GenerationConfig, MapConfig, ThemeConfig},
debug::DebugLayers,
generator::Generator,
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Editor {
gen_config: GenerationConfig,
map_config: MapConfig,
thm_config: ThemeConfig,
args: &Args,
args: &EditorArgs,
) -> Editor {
let init_gen_configs: Vec<GenerationConfig> = GenerationConfig::get_all_configs();
let init_map_configs: Vec<MapConfig> = MapConfig::get_all_configs();
Expand Down

0 comments on commit 2f73771

Please sign in to comment.