Smooth works
This commit is contained in:
+1
-1
@@ -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()
|
||||
|
||||
+24
-11
@@ -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::<Color>::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::<f32>()).collect::<Vec<f32>>();
|
||||
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::<f32>() > 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::<Vec<bool>>();
|
||||
|
||||
self.world.data = updated;
|
||||
|
||||
println!("Time elapsed: {:?} ms", time.elapsed().as_millis());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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::<SeedAutomation>().insert(PlateAutomation{
|
||||
world: automation.world.clone()
|
||||
|
||||
@@ -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::<Vec<Color>>();
|
||||
self.world.data = update;
|
||||
}
|
||||
|
||||
fn seed(&mut self, rng: &mut ChaCha8Rng){
|
||||
|
||||
Reference in New Issue
Block a user