From b24120707724a054f4bb5e3f77345b428b10fc5f Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Thu, 18 Jun 2026 21:46:47 +0300 Subject: [PATCH] Smooth works --- src/main.rs | 2 +- src/plate_automation.rs | 35 ++++++++++++++++++++++++----------- src/seed_automation.rs | 2 +- src/subplate_automation.rs | 34 +++++++++++++++++++++++++++++++--- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 47b83eb..6929268 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use rand_chacha::ChaCha8Rng; use rand_chacha::rand_core::SeedableRng; use seed_automation::SeedAutomation; -const RECTANGLE_SIDE: f32 = 750.0; +const RECTANGLE_SIDE: f32 = 1250.0; fn main() { App::new() diff --git a/src/plate_automation.rs b/src/plate_automation.rs index 9493154..64105ed 100644 --- a/src/plate_automation.rs +++ b/src/plate_automation.rs @@ -1,3 +1,5 @@ +use rayon::iter::IndexedParallelIterator; +use std::time::Instant; use crate::SeededRng; use crate::basic::{IntoImage, Table}; use bevy::asset::{Assets, RenderAssetUsages}; @@ -7,8 +9,10 @@ use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, Res use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; use rand::RngExt; use rand_chacha::ChaCha8Rng; +use rayon::prelude::IntoParallelIterator; use crate::seed_automation::SeedAutomation; use crate::subplate_automation::SubPlateAutomation; +use rayon::iter::ParallelIterator; pub fn update_automation_view( query: Query<(&Sprite, &PlateAutomation)>, @@ -54,10 +58,11 @@ pub fn update_automation( } if keys.just_pressed(KeyCode::AltLeft) { - automation.world.grow() + automation.world.grow(); + println!("Map size {}", automation.world.side) } - if keys.just_pressed(KeyCode::Tab) { + if keys.just_pressed(KeyCode::Enter) { println!("Switching to SubPlateAutomation"); let mut new_table = Table::::new(Color::BLACK, automation.world.side); automation.world.convert_copy(&mut new_table, |value| { if value { Color::WHITE } else { Color::BLACK } }); @@ -74,22 +79,30 @@ pub struct PlateAutomation { impl PlateAutomation { fn next(&mut self, rng: &mut ChaCha8Rng) { - for index in 0..self.world.side * self.world.side { + let time = Instant::now(); + let range = 0..self.world.side * self.world.side; + let randoms = range.clone().map(|x| rng.random::()).collect::>(); + let updated = range.into_par_iter().zip(randoms).map(|(index, random)| { if *self.world.get(index) { - continue; + return true; } let around = self.world.around_line(index).iter().filter(|v| ***v).count(); if around == 0 { - continue; - } - let chance = (around as f32 * 0.2).powi(2); - if rng.random::() > chance { - continue; + return false; } - self.world.set(index, true); - } + let chance = (around as f32 * 0.2).powi(2); + if random > chance { + return false; + } + + return true; + }).collect::>(); + + self.world.data = updated; + + println!("Time elapsed: {:?} ms", time.elapsed().as_millis()); } } diff --git a/src/seed_automation.rs b/src/seed_automation.rs index 62dfa3f..5305c8f 100644 --- a/src/seed_automation.rs +++ b/src/seed_automation.rs @@ -52,7 +52,7 @@ pub fn update_automation( let random = &mut seeded_rng.0; automation.next(random) } - if keys.just_pressed(KeyCode::Tab){ + if keys.just_pressed(KeyCode::Enter){ println!("Switching to PlateAutomation"); commands.entity(entity).remove::().insert(PlateAutomation{ world: automation.world.clone() diff --git a/src/subplate_automation.rs b/src/subplate_automation.rs index 8d09aa0..b3a9536 100644 --- a/src/subplate_automation.rs +++ b/src/subplate_automation.rs @@ -1,3 +1,5 @@ +use rayon::iter::ParallelIterator; +use std::time::Instant; use crate::SeededRng; use crate::basic::{IntoImage, Table}; use bevy::asset::{Assets, RenderAssetUsages}; @@ -5,8 +7,10 @@ use bevy::image::{BevyDefault, Image}; use bevy::input::ButtonInput; use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite}; use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; +use bevy::tasks::futures_lite::StreamExt; use rand::{ RngExt}; use rand_chacha::ChaCha8Rng; +use rayon::iter::IntoParallelIterator; pub fn update_automation_view( query: Query<(&Sprite, &SubPlateAutomation)>, @@ -45,7 +49,7 @@ pub fn update_automation( return; } - let (mut automation, entity) = query.iter_mut().next().unwrap(); + let (mut automation, _entity) = query.iter_mut().next().unwrap(); if keys.just_pressed(KeyCode::Space) || keys.all_pressed([KeyCode::Space, KeyCode::ShiftLeft]) { let random = &mut seeded_rng.0; @@ -58,7 +62,8 @@ pub fn update_automation( automation.seed(random); } - if keys.just_pressed(KeyCode::Tab) { + if keys.just_pressed(KeyCode::AltRight) { + automation.smooth(); } } @@ -69,6 +74,8 @@ pub struct SubPlateAutomation { impl SubPlateAutomation { fn next(&mut self, rng: &mut ChaCha8Rng) { + let time = Instant::now(); + for index in 0..self.world.side * self.world.side { let current_color = *self.world.get(index); if current_color == Color::BLACK || current_color != Color::WHITE { @@ -86,9 +93,30 @@ impl SubPlateAutomation { } let color = around[rng.random_range(0..around.len())]; - self.world.set(index, color); } + + println!("Time elapsed: {:?} ms", time.elapsed().as_millis()); + + } + + fn smooth(&mut self) { + let range = 0..self.world.side * self.world.side; + let update = range.into_iter().map(|index| { + let around = self.world.around_line(index); + let mut counts: Vec<(Color, u8)> = vec![]; + for p in around { + for count_index in 0..counts.len() { + if counts[count_index].0 == *p { + counts[count_index].1 += 1; + continue + } + } + counts.push((p.clone(), 1)); + } + counts.iter().max_by_key(|(_, c)| {c}).unwrap().0 + }).collect::>(); + self.world.data = update; } fn seed(&mut self, rng: &mut ChaCha8Rng){