2026-06-28 13:57:55 +03:00
|
|
|
use crate::table::{IntoImage, Table};
|
2026-06-22 11:37:35 +03:00
|
|
|
use crate::hex_table::HexTable;
|
2026-06-27 10:37:45 +03:00
|
|
|
use crate::SeededRng;
|
2026-06-18 20:51:09 +03:00
|
|
|
use bevy::asset::{Assets, RenderAssetUsages};
|
|
|
|
|
use bevy::image::{BevyDefault, Image};
|
|
|
|
|
use bevy::input::ButtonInput;
|
2026-06-27 10:37:45 +03:00
|
|
|
use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite, Text, With};
|
2026-06-18 20:51:09 +03:00
|
|
|
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat};
|
2026-06-18 21:46:47 +03:00
|
|
|
use bevy::tasks::futures_lite::StreamExt;
|
2026-06-22 11:37:35 +03:00
|
|
|
use rand::RngExt;
|
2026-06-18 20:51:09 +03:00
|
|
|
use rand_chacha::ChaCha8Rng;
|
2026-06-27 10:37:45 +03:00
|
|
|
use std::collections::HashMap;
|
2026-06-22 11:37:35 +03:00
|
|
|
use std::time::Instant;
|
2026-06-18 20:51:09 +03:00
|
|
|
|
|
|
|
|
pub fn update_automation_view(
|
|
|
|
|
query: Query<(&Sprite, &SubPlateAutomation)>,
|
|
|
|
|
mut images: ResMut<Assets<Image>>,
|
|
|
|
|
) {
|
|
|
|
|
if query.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update_automation(
|
|
|
|
|
mut query: Query<(&mut SubPlateAutomation, Entity)>,
|
2026-06-27 10:37:45 +03:00
|
|
|
mut hex_query: Query<&HexMatrixBuild>,
|
2026-06-18 20:51:09 +03:00
|
|
|
mut seeded_rng: ResMut<SeededRng>,
|
|
|
|
|
keys: Res<ButtonInput<KeyCode>>,
|
2026-06-27 10:37:45 +03:00
|
|
|
mut commands: Commands,
|
2026-06-18 20:51:09 +03:00
|
|
|
) {
|
|
|
|
|
if query.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 21:46:47 +03:00
|
|
|
let (mut automation, _entity) = query.iter_mut().next().unwrap();
|
2026-06-18 20:51:09 +03:00
|
|
|
if keys.just_pressed(KeyCode::Space) || keys.all_pressed([KeyCode::Space, KeyCode::ShiftLeft]) {
|
|
|
|
|
let random = &mut seeded_rng.0;
|
|
|
|
|
|
|
|
|
|
automation.next(random)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if keys.just_pressed(KeyCode::AltLeft) {
|
|
|
|
|
let random = &mut seeded_rng.0;
|
|
|
|
|
|
2026-06-27 10:37:45 +03:00
|
|
|
for _ in 0..16 {
|
|
|
|
|
automation.seed(random);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if keys.just_pressed(KeyCode::AltRight) {
|
|
|
|
|
// automation.smooth();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if keys.just_pressed(KeyCode::KeyS) {
|
|
|
|
|
automation.calculate_subplates(&mut commands);
|
2026-06-18 20:51:09 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-27 10:37:45 +03:00
|
|
|
if keys.just_pressed(KeyCode::KeyF) {
|
|
|
|
|
automation.calculate_front_lines(&mut commands, hex_query);
|
2026-06-18 20:51:09 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct SubPlateAutomation {
|
2026-06-27 10:37:45 +03:00
|
|
|
pub(crate) world: Table<u8>,
|
2026-06-18 20:51:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SubPlateAutomation {
|
|
|
|
|
fn next(&mut self, rng: &mut ChaCha8Rng) {
|
2026-06-18 21:46:47 +03:00
|
|
|
let time = Instant::now();
|
|
|
|
|
|
2026-06-18 20:51:09 +03:00
|
|
|
for index in 0..self.world.side * self.world.side {
|
|
|
|
|
let current_color = *self.world.get(index);
|
2026-06-27 10:37:45 +03:00
|
|
|
if current_color == 0 || current_color != u8::MAX {
|
2026-06-18 20:51:09 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 11:37:35 +03:00
|
|
|
if rng.random::<f32>() < 0.8 {
|
2026-06-18 20:51:09 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 11:37:35 +03:00
|
|
|
let around = self
|
|
|
|
|
.world
|
|
|
|
|
.around_line(index)
|
|
|
|
|
.iter()
|
2026-06-27 10:37:45 +03:00
|
|
|
.filter(|v| ***v != u8::MAX && ***v != 0)
|
2026-06-22 11:37:35 +03:00
|
|
|
.map(|color| (*color).clone())
|
|
|
|
|
.collect::<Vec<_>>();
|
2026-06-18 20:51:09 +03:00
|
|
|
|
|
|
|
|
if around.len() == 0 {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let color = around[rng.random_range(0..around.len())];
|
|
|
|
|
|
|
|
|
|
self.world.set(index, color);
|
|
|
|
|
}
|
2026-06-18 21:46:47 +03:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn smooth(&mut self) {
|
|
|
|
|
let range = 0..self.world.side * self.world.side;
|
2026-06-22 11:37:35 +03:00
|
|
|
for index in range {
|
2026-06-18 21:46:47 +03:00
|
|
|
let around = self.world.around_line(index);
|
2026-06-27 10:37:45 +03:00
|
|
|
let mut counts: Vec<(u8, u8)> = vec![];
|
2026-06-18 21:46:47 +03:00
|
|
|
for p in around {
|
|
|
|
|
for count_index in 0..counts.len() {
|
|
|
|
|
if counts[count_index].0 == *p {
|
|
|
|
|
counts[count_index].1 += 1;
|
2026-06-22 11:37:35 +03:00
|
|
|
continue;
|
2026-06-18 21:46:47 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
counts.push((p.clone(), 1));
|
|
|
|
|
}
|
2026-06-22 11:37:35 +03:00
|
|
|
self.world
|
|
|
|
|
.set(index, counts.iter().max_by_key(|(_, c)| c).unwrap().0);
|
|
|
|
|
}
|
2026-06-18 20:51:09 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-22 11:37:35 +03:00
|
|
|
fn seed(&mut self, rng: &mut ChaCha8Rng) {
|
2026-06-18 20:51:09 +03:00
|
|
|
let len = (self.world.side * self.world.side) as f32;
|
|
|
|
|
loop {
|
|
|
|
|
let index = (rng.random::<f32>() * len) as usize;
|
2026-06-27 10:37:45 +03:00
|
|
|
if *self.world.get(index) == 0 {
|
2026-06-18 20:51:09 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 11:37:35 +03:00
|
|
|
self.world
|
2026-06-27 10:37:45 +03:00
|
|
|
.set(index, rng.random::<u8>());
|
2026-06-18 20:51:09 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-27 10:37:45 +03:00
|
|
|
|
|
|
|
|
fn calculate_front_lines(&self, commands: &mut Commands, hex_query: Query<&HexMatrixBuild>) {
|
|
|
|
|
|
|
|
|
|
let generator = &hex_query.iter().next().unwrap().hex_matrix;
|
|
|
|
|
let all_points = generator.calculate();
|
|
|
|
|
|
|
|
|
|
let own_points = all_points.iter().map(|(x, y)| {
|
|
|
|
|
let x_table = *x as usize;
|
|
|
|
|
let y_table = *y as usize;
|
|
|
|
|
|
|
|
|
|
(*self.world.get_dim(x_table, y_table), x, y)
|
|
|
|
|
}).filter(|(v, _, _)| {
|
|
|
|
|
*v != 0
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
commands.spawn((HexMatrixRedrawRequest));
|
|
|
|
|
}
|
|
|
|
|
fn calculate_subplates(&self, commands: &mut Commands) {
|
|
|
|
|
let mut list : HashMap<u8, Vec<usize>> = HashMap::new();
|
|
|
|
|
self.world.iter().zip(0..self.world.data.len()).for_each(|(world, index)| {
|
|
|
|
|
if list.contains_key(world) {
|
|
|
|
|
list.get_mut(world).unwrap().push(index);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
list.insert(world.clone(), vec![index]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
println!("{:?}", list.iter().map(|(c, v)| (c.clone(), v.len())).collect::<Vec<_>>());
|
|
|
|
|
}
|
2026-06-18 20:51:09 +03:00
|
|
|
}
|
2026-06-22 11:37:35 +03:00
|
|
|
|
|
|
|
|
pub fn setup_hex_matrix(
|
|
|
|
|
mut request_query: Query<(&HexMatrixRequest, Entity)>,
|
|
|
|
|
mut view_query: Query<&Sprite, With<HexMatrixView>>,
|
|
|
|
|
mut automation_query: Query<&SubPlateAutomation>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
mut images: ResMut<Assets<Image>>,
|
|
|
|
|
) {
|
|
|
|
|
if request_query.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let entity = request_query.iter().next().unwrap().1;
|
|
|
|
|
let automation = automation_query.iter_mut().next().unwrap();
|
|
|
|
|
commands.entity(entity).despawn();
|
|
|
|
|
|
|
|
|
|
println!("Seed hex matrix");
|
|
|
|
|
|
|
|
|
|
let resolution = automation.world.side / 4;
|
|
|
|
|
|
2026-06-28 13:57:55 +03:00
|
|
|
let generator = HexTable::new(resolution, true, 4.0);
|
2026-06-22 11:37:35 +03:00
|
|
|
let mut table = Table::new(false, automation.world.side);
|
|
|
|
|
generator.calculate().iter().for_each(|(x, y)| {
|
2026-06-27 10:37:45 +03:00
|
|
|
let x = *x as usize;
|
|
|
|
|
let y = *y as usize;
|
2026-06-22 11:37:35 +03:00
|
|
|
|
2026-06-27 10:37:45 +03:00
|
|
|
if *automation.world.get_dim(x, y) == 0 {
|
2026-06-22 11:37:35 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table.set_dim(x, y, true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let data = table.get_image_data();
|
|
|
|
|
let image = Image::new(
|
|
|
|
|
Extent3d {
|
|
|
|
|
width: table.side as u32,
|
|
|
|
|
height: table.side as u32,
|
|
|
|
|
depth_or_array_layers: 1,
|
|
|
|
|
},
|
|
|
|
|
TextureDimension::D2,
|
|
|
|
|
data,
|
|
|
|
|
TextureFormat::bevy_default(),
|
|
|
|
|
RenderAssetUsages::default(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let sprite = view_query.iter().next().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
images.remove(sprite.image.id());
|
|
|
|
|
images.insert(sprite.image.id(), image).unwrap();
|
|
|
|
|
commands.spawn((HexMatrixBuild{
|
2026-06-27 10:37:45 +03:00
|
|
|
points: table,
|
|
|
|
|
hex_matrix: generator,
|
|
|
|
|
}));
|
2026-06-22 11:37:35 +03:00
|
|
|
}
|
|
|
|
|
|
2026-06-27 10:37:45 +03:00
|
|
|
|
|
|
|
|
pub fn update_hex_matrix_view(
|
|
|
|
|
mut data_query: Query<&HexMatrixBuild>,
|
|
|
|
|
mut request_query: Query<Entity, With<HexMatrixRedrawRequest>>,
|
|
|
|
|
mut view_query: Query<&Sprite, With<HexMatrixView>>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
mut images: ResMut<Assets<Image>>,
|
|
|
|
|
){
|
|
|
|
|
if request_query.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let req = request_query.iter_mut().next().unwrap();
|
|
|
|
|
commands.entity(req).despawn();
|
|
|
|
|
println!("Update hex matrix view");
|
|
|
|
|
let table = data_query.iter_mut().next().unwrap();
|
|
|
|
|
let data = table.points.get_image_data();
|
|
|
|
|
|
|
|
|
|
let image = Image::new(
|
|
|
|
|
Extent3d {
|
|
|
|
|
width: table.points.side as u32,
|
|
|
|
|
height: table.points.side as u32,
|
|
|
|
|
depth_or_array_layers: 1,
|
|
|
|
|
},
|
|
|
|
|
TextureDimension::D2,
|
|
|
|
|
data,
|
|
|
|
|
TextureFormat::bevy_default(),
|
|
|
|
|
RenderAssetUsages::default(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let sprite = view_query.iter().next().unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
images.remove(sprite.image.id());
|
|
|
|
|
images.insert(sprite.image.id(), image).unwrap();
|
|
|
|
|
}
|
2026-06-22 11:37:35 +03:00
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct HexMatrixRequest;
|
|
|
|
|
|
2026-06-27 10:37:45 +03:00
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct HexMatrixRedrawRequest;
|
|
|
|
|
|
2026-06-22 11:37:35 +03:00
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct HexMatrixView;
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
pub struct HexMatrixBuild{
|
2026-06-27 10:37:45 +03:00
|
|
|
points: Table<bool>,
|
2026-06-28 13:57:55 +03:00
|
|
|
hex_matrix: HexTable<bool>
|
2026-06-27 10:37:45 +03:00
|
|
|
}
|