Smooth works

This commit is contained in:
2026-06-18 21:46:47 +03:00
parent 0ab895f79e
commit b241207077
4 changed files with 57 additions and 16 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ use rand_chacha::ChaCha8Rng;
use rand_chacha::rand_core::SeedableRng; use rand_chacha::rand_core::SeedableRng;
use seed_automation::SeedAutomation; use seed_automation::SeedAutomation;
const RECTANGLE_SIDE: f32 = 750.0; const RECTANGLE_SIDE: f32 = 1250.0;
fn main() { fn main() {
App::new() App::new()
+24 -11
View File
@@ -1,3 +1,5 @@
use rayon::iter::IndexedParallelIterator;
use std::time::Instant;
use crate::SeededRng; use crate::SeededRng;
use crate::basic::{IntoImage, Table}; use crate::basic::{IntoImage, Table};
use bevy::asset::{Assets, RenderAssetUsages}; 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 bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use rand::RngExt; use rand::RngExt;
use rand_chacha::ChaCha8Rng; use rand_chacha::ChaCha8Rng;
use rayon::prelude::IntoParallelIterator;
use crate::seed_automation::SeedAutomation; use crate::seed_automation::SeedAutomation;
use crate::subplate_automation::SubPlateAutomation; use crate::subplate_automation::SubPlateAutomation;
use rayon::iter::ParallelIterator;
pub fn update_automation_view( pub fn update_automation_view(
query: Query<(&Sprite, &PlateAutomation)>, query: Query<(&Sprite, &PlateAutomation)>,
@@ -54,10 +58,11 @@ pub fn update_automation(
} }
if keys.just_pressed(KeyCode::AltLeft) { 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"); println!("Switching to SubPlateAutomation");
let mut new_table = Table::<Color>::new(Color::BLACK, automation.world.side); let mut new_table = Table::<Color>::new(Color::BLACK, automation.world.side);
automation.world.convert_copy(&mut new_table, |value| { if value { Color::WHITE } else { Color::BLACK } }); automation.world.convert_copy(&mut new_table, |value| { if value { Color::WHITE } else { Color::BLACK } });
@@ -74,22 +79,30 @@ pub struct PlateAutomation {
impl PlateAutomation { impl PlateAutomation {
fn next(&mut self, rng: &mut ChaCha8Rng) { 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::<f32>()).collect::<Vec<f32>>();
let updated = range.into_par_iter().zip(randoms).map(|(index, random)| {
if *self.world.get(index) { if *self.world.get(index) {
continue; return true;
} }
let around = self.world.around_line(index).iter().filter(|v| ***v).count(); let around = self.world.around_line(index).iter().filter(|v| ***v).count();
if around == 0 { if around == 0 {
continue; return false;
}
let chance = (around as f32 * 0.2).powi(2);
if rng.random::<f32>() > chance {
continue;
} }
self.world.set(index, true); let chance = (around as f32 * 0.2).powi(2);
} if random > chance {
return false;
}
return true;
}).collect::<Vec<bool>>();
self.world.data = updated;
println!("Time elapsed: {:?} ms", time.elapsed().as_millis());
} }
} }
+1 -1
View File
@@ -52,7 +52,7 @@ pub fn update_automation(
let random = &mut seeded_rng.0; let random = &mut seeded_rng.0;
automation.next(random) automation.next(random)
} }
if keys.just_pressed(KeyCode::Tab){ if keys.just_pressed(KeyCode::Enter){
println!("Switching to PlateAutomation"); println!("Switching to PlateAutomation");
commands.entity(entity).remove::<SeedAutomation>().insert(PlateAutomation{ commands.entity(entity).remove::<SeedAutomation>().insert(PlateAutomation{
world: automation.world.clone() world: automation.world.clone()
+31 -3
View File
@@ -1,3 +1,5 @@
use rayon::iter::ParallelIterator;
use std::time::Instant;
use crate::SeededRng; use crate::SeededRng;
use crate::basic::{IntoImage, Table}; use crate::basic::{IntoImage, Table};
use bevy::asset::{Assets, RenderAssetUsages}; use bevy::asset::{Assets, RenderAssetUsages};
@@ -5,8 +7,10 @@ use bevy::image::{BevyDefault, Image};
use bevy::input::ButtonInput; use bevy::input::ButtonInput;
use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite}; use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite};
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat}; use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use bevy::tasks::futures_lite::StreamExt;
use rand::{ RngExt}; use rand::{ RngExt};
use rand_chacha::ChaCha8Rng; use rand_chacha::ChaCha8Rng;
use rayon::iter::IntoParallelIterator;
pub fn update_automation_view( pub fn update_automation_view(
query: Query<(&Sprite, &SubPlateAutomation)>, query: Query<(&Sprite, &SubPlateAutomation)>,
@@ -45,7 +49,7 @@ pub fn update_automation(
return; 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]) { if keys.just_pressed(KeyCode::Space) || keys.all_pressed([KeyCode::Space, KeyCode::ShiftLeft]) {
let random = &mut seeded_rng.0; let random = &mut seeded_rng.0;
@@ -58,7 +62,8 @@ pub fn update_automation(
automation.seed(random); 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 { impl SubPlateAutomation {
fn next(&mut self, rng: &mut ChaCha8Rng) { fn next(&mut self, rng: &mut ChaCha8Rng) {
let time = Instant::now();
for index in 0..self.world.side * self.world.side { for index in 0..self.world.side * self.world.side {
let current_color = *self.world.get(index); let current_color = *self.world.get(index);
if current_color == Color::BLACK || current_color != Color::WHITE { if current_color == Color::BLACK || current_color != Color::WHITE {
@@ -86,9 +93,30 @@ impl SubPlateAutomation {
} }
let color = around[rng.random_range(0..around.len())]; let color = around[rng.random_range(0..around.len())];
self.world.set(index, color); 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::<Vec<Color>>();
self.world.data = update;
} }
fn seed(&mut self, rng: &mut ChaCha8Rng){ fn seed(&mut self, rng: &mut ChaCha8Rng){