From c78e004ae379eb86233570f154910842c06b32f6 Mon Sep 17 00:00:00 2001 From: tobi Date: Fri, 26 Apr 2024 12:48:07 +0200 Subject: [PATCH] Apply clippy fixes --- src/bin/ddnet_bridge.rs | 18 +++++++++--------- src/config.rs | 2 +- src/editor.rs | 4 ++-- src/generator.rs | 10 +++++----- src/grid_render.rs | 4 ++-- src/map.rs | 19 ++++++++----------- src/position.rs | 3 +-- src/random.rs | 4 ++-- src/twmap_export.rs | 14 +++++++------- src/walker.rs | 8 ++++---- 10 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/bin/ddnet_bridge.rs b/src/bin/ddnet_bridge.rs index 59491e6..386843a 100644 --- a/src/bin/ddnet_bridge.rs +++ b/src/bin/ddnet_bridge.rs @@ -1,6 +1,6 @@ use clap::Parser; use core::net::{IpAddr, Ipv4Addr, SocketAddr}; -use gores_mapgen_rust::random::{Random, Seed}; +use gores_mapgen_rust::random::Seed; use gores_mapgen_rust::{config::GenerationConfig, generator::Generator}; use std::collections::HashMap; @@ -45,7 +45,7 @@ struct BridgeArgs { #[derive(Debug)] struct Vote { - player_name: String, + _player_name: String, vote_name: String, vote_reason: String, } @@ -96,7 +96,7 @@ impl Econ { if vote.vote_name.starts_with("generate") { let seed = if vote.vote_reason == "No reason given" { Seed::random() - } else if let Some(seed_u64) = vote.vote_reason.parse::().ok() { + } else if let Ok(seed_u64) = vote.vote_reason.parse::() { Seed::from_u64(seed_u64) } else { Seed::from_string(&vote.vote_reason) @@ -107,7 +107,7 @@ impl Econ { let vote_type = vote_parts.next().expect("should have exactly two parts"); assert_eq!(vote_type, "generate"); let vote_preset = vote_parts.next().expect("should have exactly two parts"); - assert!(vote_parts.next() == None, "should have exactly two parts"); + assert!(vote_parts.next().is_none(), "should have exactly two parts"); // get config based on preset name let config = configs.get(vote_preset).expect("preset does not exist!"); @@ -126,7 +126,7 @@ fn generate_and_change_map( println!("[GEN] Starting Map Generation!"); econ.send_rcon_cmd(format!("say [GEN] Generating Map, seed={:?}", &seed)); let map_path = args.maps.canonicalize().unwrap().join("random_map.map"); - match Generator::generate_map(30_000, &seed, config) { + match Generator::generate_map(30_000, seed, config) { Ok(map) => { println!("[GEN] Finished Map Generation!"); map.export(&map_path); @@ -138,7 +138,7 @@ fn generate_and_change_map( Err(err) => { println!("[GEN] Generation Error: {:?}", err); econ.send_rcon_cmd(format!("say [GEN] Failed due to: {:}", err)); - econ.send_rcon_cmd(format!("say just try again :)")); + econ.send_rcon_cmd("say just try again :)".to_string()); } } } @@ -165,7 +165,7 @@ fn start_bridge(args: &BridgeArgs) { println!("[GEN] Generating initial map"); auth = true; generate_and_change_map( - &args, + args, &Seed::from_u64(42), &GenerationConfig::default(), &mut econ, @@ -185,7 +185,7 @@ fn start_bridge(args: &BridgeArgs) { match message { "Vote passed" => { println!("[VOTE]: Success"); - econ.handle_vote(pending_vote.as_ref().unwrap(), &args, &configs); + econ.handle_vote(pending_vote.as_ref().unwrap(), args, &configs); } "Vote failed" => { pending_vote = None; @@ -203,7 +203,7 @@ fn start_bridge(args: &BridgeArgs) { ); pending_vote = Some(Vote { - player_name, + _player_name: player_name, vote_name, vote_reason, }); diff --git a/src/config.rs b/src/config.rs index 5a9435b..b44a6a1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -80,7 +80,7 @@ impl GenerationConfig { for file_name in GenerationConfigStorage::iter() { let file = GenerationConfigStorage::get(&file_name).unwrap(); let data = std::str::from_utf8(&file.data).unwrap(); - let config: GenerationConfig = serde_json::from_str(&data).unwrap(); + let config: GenerationConfig = serde_json::from_str(data).unwrap(); configs.insert(config.name.clone(), config); } diff --git a/src/editor.rs b/src/editor.rs index b45ab0b..0a5eb16 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,14 +1,14 @@ use std::collections::HashMap; use std::{env, isize}; -use egui::{ComboBox, InnerResponse, RichText}; +use egui::{InnerResponse, RichText}; use tinyfiledialogs; const STEPS_PER_FRAME: usize = 50; use crate::random::Seed; use crate::{ - config::GenerationConfig, generator::Generator, map::Map, position::Position, random::Random, + config::GenerationConfig, generator::Generator, map::Map, position::Position, }; use egui::{epaint::Shadow, CollapsingHeader, Color32, Frame, Label, Margin, Ui}; diff --git a/src/generator.rs b/src/generator.rs index 5a8cdcd..d8d0d73 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -7,8 +7,8 @@ use crate::{ walker::CuteWalker, }; -use dt::{dt, dt_bool, dt_int}; -use ndarray::{arr2, s, Array, Array2, Ix2, IxDyn}; +use dt::{dt_bool}; +use ndarray::{Array2, Ix2}; pub struct Generator { pub walker: CuteWalker, @@ -37,7 +37,7 @@ impl Generator { if !self.walker.finished { // randomly mutate kernel - self.walker.mutate_kernel(&config, &mut self.rnd); + self.walker.mutate_kernel(config, &mut self.rnd); // perform one step self.walker @@ -137,13 +137,13 @@ impl Generator { seed: &Seed, config: &GenerationConfig, ) -> Result { - let mut gen = Generator::new(&config, seed.clone()); + let mut gen = Generator::new(config, seed.clone()); for _ in 0..max_steps { if gen.walker.finished { break; } - gen.step(&config)?; + gen.step(config)?; } gen.post_processing(config); diff --git a/src/grid_render.rs b/src/grid_render.rs index c5d61db..f611bc8 100644 --- a/src/grid_render.rs +++ b/src/grid_render.rs @@ -53,7 +53,7 @@ pub fn draw_chunked_grid( for x in x_start..x_end { for y in y_start..y_end { let value = &grid[[x, y]]; - draw_rectangle(x as f32, y as f32, 1.0, 1.0, blocktype_to_color(&value)); + draw_rectangle(x as f32, y as f32, 1.0, 1.0, blocktype_to_color(value)); } } } else { @@ -118,7 +118,7 @@ pub fn draw_walker_kernel(walker: &CuteWalker, kernel_type: KernelType) { } } -pub fn draw_waypoints(waypoints: &Vec) { +pub fn draw_waypoints(waypoints: &[Position]) { for pos in waypoints.iter() { draw_circle(pos.x as f32 + 0.5, pos.y as f32 + 0.5, 1.0, colors::RED) } diff --git a/src/map.rs b/src/map.rs index ff852fd..70d5968 100644 --- a/src/map.rs +++ b/src/map.rs @@ -31,10 +31,7 @@ impl BlockType { } pub fn is_hookable(&self) -> bool { - match self { - BlockType::Hookable | BlockType::Platform => true, - _ => false, - } + matches!(self, BlockType::Hookable | BlockType::Platform) } } @@ -196,7 +193,7 @@ impl Map { } pub fn export(&self, path: &PathBuf) { - TwExport::export(&self, &path) + TwExport::export(self, path) } pub fn pos_in_bounds(&self, pos: &Position) -> bool { @@ -210,7 +207,7 @@ impl Map { bot_right: &Position, value: &BlockType, ) -> Result { - if !self.pos_in_bounds(&top_left) || !self.pos_in_bounds(&bot_right) { + if !self.pos_in_bounds(top_left) || !self.pos_in_bounds(bot_right) { return Err("checking area out of bounds"); } @@ -227,7 +224,7 @@ impl Map { bot_right: &Position, value: &BlockType, ) -> Result { - if !self.pos_in_bounds(&top_left) || !self.pos_in_bounds(&bot_right) { + if !self.pos_in_bounds(top_left) || !self.pos_in_bounds(bot_right) { return Err("checking area out of bounds"); } let area = self @@ -279,9 +276,9 @@ impl Map { let top_right = Position::new(bot_right.x, top_left.y); let bot_left = Position::new(top_left.x, bot_right.y); - self.set_area(&top_left, &top_right, value, overide); - self.set_area(&top_right, &bot_right, value, overide); - self.set_area(&top_left, &bot_left, value, overide); - self.set_area(&bot_left, &bot_right, value, overide); + self.set_area(top_left, &top_right, value, overide); + self.set_area(&top_right, bot_right, value, overide); + self.set_area(top_left, &bot_left, value, overide); + self.set_area(&bot_left, bot_right, value, overide); } } diff --git a/src/position.rs b/src/position.rs index f28b3b2..6f30aa8 100644 --- a/src/position.rs +++ b/src/position.rs @@ -1,4 +1,3 @@ -use rand_distr::num_traits::CheckedSub; use serde::{Deserialize, Serialize}; use crate::map::Map; @@ -57,7 +56,7 @@ impl Position { shift: &ShiftDirection, map: &Map, ) -> Result<(), &'static str> { - if !self.is_shift_valid(&shift, map) { + if !self.is_shift_valid(shift, map) { return Err("invalid shift"); } diff --git a/src/random.rs b/src/random.rs index 688fb4a..698a92e 100644 --- a/src/random.rs +++ b/src/random.rs @@ -26,7 +26,7 @@ impl Seed { pub fn from_string(seed_str: &String) -> Seed { Seed { - seed_u64: Seed::str_to_u64(&seed_str), + seed_u64: Seed::str_to_u64(seed_str), seed_str: seed_str.to_owned(), } } @@ -112,7 +112,7 @@ impl Random { self.gen.next_u64(); } - pub fn pick_element<'a, T>(&'a mut self, values: &'a Vec) -> &T { + pub fn pick_element<'a, T>(&'a mut self, values: &'a [T]) -> &T { &values[self.in_range_exclusive(0, values.len())] } diff --git a/src/twmap_export.rs b/src/twmap_export.rs index a729a64..dec4b6c 100644 --- a/src/twmap_export.rs +++ b/src/twmap_export.rs @@ -17,7 +17,7 @@ impl AutoMapperConfigs { .expect("automapper rule config not found"); let data = std::str::from_utf8(&file.data).unwrap(); - Automapper::parse(name, &data).expect("failed to parse .rules file") + Automapper::parse(name, data).expect("failed to parse .rules file") } } @@ -51,13 +51,13 @@ impl TwExport { assert_eq!(layer.name, layer_name); let image_name = tw_map.images[layer.image.unwrap() as usize].name(); - let automapper_config = TwExport::get_automapper_config(image_name.clone(), &layer); + let automapper_config = TwExport::get_automapper_config(image_name.clone(), layer); let tiles = layer.tiles_mut().unwrap_mut(); *tiles = Array2::::default((map.width, map.height)); for ((x, y), value) in map.grid.indexed_iter() { - if block_type_in_layer(&value) { + if block_type_in_layer(value) { tiles[[y, x]] = Tile::new(1, TileFlags::empty()) } } @@ -72,10 +72,10 @@ impl TwExport { let mut tw_map = TwMap::parse_file("automap_test.map").expect("parsing failed"); tw_map.load().expect("loading failed"); - TwExport::process_layer(&mut tw_map, &map, &0, "Freeze", |t| { - (*t == BlockType::Freeze) || BlockType::is_hookable(&t) + TwExport::process_layer(&mut tw_map, map, &0, "Freeze", |t| { + (*t == BlockType::Freeze) || BlockType::is_hookable(t) }); - TwExport::process_layer(&mut tw_map, &map, &1, "Hookable", BlockType::is_hookable); + TwExport::process_layer(&mut tw_map, map, &1, "Hookable", BlockType::is_hookable); // get game layer let game_layer = tw_map @@ -96,6 +96,6 @@ impl TwExport { // save map println!("exporting map to {:?}", &path); - tw_map.save_file(&path).expect("failed to write map file"); + tw_map.save_file(path).expect("failed to write map file"); } } diff --git a/src/walker.rs b/src/walker.rs index 8e0b6be..df1f2ec 100644 --- a/src/walker.rs +++ b/src/walker.rs @@ -121,7 +121,7 @@ impl CuteWalker { let mut sampled_shift = &rnd.sample_move(&shifts); // with a certain probabiliy re-use last direction instead - if rnd.with_probability(config.momentum_prob) && !self.last_direction.is_none() { + if rnd.with_probability(config.momentum_prob) && self.last_direction.is_some() { sampled_shift = self.last_direction.as_ref().unwrap(); } @@ -167,15 +167,15 @@ impl CuteWalker { } if rnd.with_probability(config.inner_rad_mut_prob) { - inner_circ = *rnd.pick_element(&vec![0.0, 0.1, 0.2, 0.6, 0.8]); // TODO: also, this is - // terrible + inner_circ = *rnd.pick_element(&[0.0, 0.1, 0.2, 0.6, 0.8]); // TODO: also, this is + // terrible modified = true; } else { rnd.skip(); } if rnd.with_probability(config.outer_rad_mut_prob) { - outer_circ = *rnd.pick_element(&vec![0.0, 0.1, 0.2, 0.6, 0.8]); + outer_circ = *rnd.pick_element(&[0.0, 0.1, 0.2, 0.6, 0.8]); modified = true; } else { rnd.skip();