Files
tect_gen/src/plate_automation.rs
T

109 lines
3.4 KiB
Rust
Raw Normal View History

2026-06-18 21:46:47 +03:00
use rayon::iter::IndexedParallelIterator;
use std::time::Instant;
2026-06-18 12:04:53 +03:00
use crate::SeededRng;
use crate::basic::{IntoImage, Table};
use bevy::asset::{Assets, RenderAssetUsages};
use bevy::image::{BevyDefault, Image};
use bevy::input::ButtonInput;
2026-06-18 20:51:09 +03:00
use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite};
2026-06-18 12:04:53 +03:00
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
use rand::RngExt;
use rand_chacha::ChaCha8Rng;
2026-06-18 21:46:47 +03:00
use rayon::prelude::IntoParallelIterator;
2026-06-18 20:51:09 +03:00
use crate::seed_automation::SeedAutomation;
2026-06-22 11:37:35 +03:00
use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation};
2026-06-18 21:46:47 +03:00
use rayon::iter::ParallelIterator;
2026-06-18 12:04:53 +03:00
2026-06-18 20:51:09 +03:00
pub fn update_automation_view(
2026-06-18 12:04:53 +03:00
query: Query<(&Sprite, &PlateAutomation)>,
mut images: ResMut<Assets<Image>>,
) {
2026-06-18 12:38:59 +03:00
if query.is_empty() {
return;
}
2026-06-18 12:04:53 +03:00
let (sprite, automation) = query.iter().next().unwrap();
let size = automation.world.side as u32;
let data = automation.world.get_image_data();
let image = Image::new(
Extent3d {
width: size,
height: size,
depth_or_array_layers: 1,
},
TextureDimension::D2,
data,
TextureFormat::bevy_default(),
RenderAssetUsages::default(),
);
images.remove(sprite.image.id());
images.insert(sprite.image.id(), image).unwrap();
}
2026-06-18 20:51:09 +03:00
pub fn update_automation(
mut query: Query<(&mut PlateAutomation, Entity)>,
2026-06-18 12:04:53 +03:00
mut seeded_rng: ResMut<SeededRng>,
2026-06-18 12:38:59 +03:00
keys: Res<ButtonInput<KeyCode>>,
2026-06-18 20:51:09 +03:00
mut commands: Commands,
2026-06-18 12:04:53 +03:00
) {
2026-06-18 12:38:59 +03:00
if query.is_empty() {
return;
}
2026-06-18 20:51:09 +03:00
let (mut automation, entity) = query.iter_mut().next().unwrap();
if keys.just_pressed(KeyCode::Space) || keys.all_pressed([KeyCode::Space, KeyCode::ShiftLeft]) {
2026-06-18 12:04:53 +03:00
let random = &mut seeded_rng.0;
automation.next(random)
}
if keys.just_pressed(KeyCode::AltLeft) {
2026-06-18 21:46:47 +03:00
automation.world.grow();
println!("Map size {}", automation.world.side)
2026-06-18 12:04:53 +03:00
}
2026-06-18 20:51:09 +03:00
2026-06-18 21:46:47 +03:00
if keys.just_pressed(KeyCode::Enter) {
2026-06-18 20:51:09 +03:00
println!("Switching to SubPlateAutomation");
2026-06-22 11:37:35 +03:00
let mut new_table = Table::<Color>::new(Color::NONE, automation.world.side);
automation.world.convert_copy(&mut new_table, |value| { if value { Color::WHITE } else { Color::NONE} });
2026-06-18 20:51:09 +03:00
commands.entity(entity).remove::<PlateAutomation>().insert(SubPlateAutomation{
world: new_table
});
2026-06-22 11:37:35 +03:00
commands.spawn((HexMatrixRequest));
2026-06-18 20:51:09 +03:00
}
2026-06-18 12:04:53 +03:00
}
#[derive(Component)]
pub struct PlateAutomation {
pub(crate) world: Table<bool>,
}
impl PlateAutomation {
fn next(&mut self, rng: &mut ChaCha8Rng) {
2026-06-18 21:46:47 +03:00
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)| {
2026-06-18 20:51:09 +03:00
if *self.world.get(index) {
2026-06-18 21:46:47 +03:00
return true;
2026-06-18 20:51:09 +03:00
}
let around = self.world.around_line(index).iter().filter(|v| ***v).count();
if around == 0 {
2026-06-18 21:46:47 +03:00
return false;
2026-06-18 20:51:09 +03:00
}
2026-06-18 21:46:47 +03:00
2026-06-18 20:51:09 +03:00
let chance = (around as f32 * 0.2).powi(2);
2026-06-18 21:46:47 +03:00
if random > chance {
return false;
2026-06-18 20:51:09 +03:00
}
2026-06-18 21:46:47 +03:00
return true;
}).collect::<Vec<bool>>();
self.world.data = updated;
println!("Time elapsed: {:?} ms", time.elapsed().as_millis());
2026-06-18 12:04:53 +03:00
}
}