From 74f01cf89587fa0b0a85798b1c972eb9547eaa42 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Mon, 22 Jun 2026 11:37:35 +0300 Subject: [PATCH] Hex table --- src/basic.rs | 10 ++-- src/hex_table.rs | 31 ++++++++++ src/main.rs | 26 ++++++++- src/plate_automation.rs | 7 ++- src/seed_automation.rs | 4 +- src/subplate_automation.rs | 113 +++++++++++++++++++++++++++++++------ 6 files changed, 162 insertions(+), 29 deletions(-) create mode 100644 src/hex_table.rs diff --git a/src/basic.rs b/src/basic.rs index 0176ce8..9444126 100644 --- a/src/basic.rs +++ b/src/basic.rs @@ -111,7 +111,7 @@ impl IntoImage for Table { data[idx] = value; data[idx + 1] = value; data[idx + 2] = value; - data[idx + 3] = 255; + data[idx + 3] = value; }); data } @@ -125,10 +125,10 @@ impl IntoImage for Table { .for_each(|(color, idx)| { let idx = idx * 4; let color = color.to_linear(); - data[idx] = (color.red * 255.0) as u8; - data[idx + 1] = (color.green * 255.0) as u8; - data[idx + 2] = (color.blue * 255.0) as u8; - data[idx + 3] = 255; + data[idx] = (color.red * 256.0) as u8; + data[idx + 1] = (color.green * 256.0) as u8; + data[idx + 2] = (color.blue * 256.0) as u8; + data[idx + 3] = (color.alpha * 256.0) as u8; }); data } diff --git a/src/hex_table.rs b/src/hex_table.rs new file mode 100644 index 0000000..3ead781 --- /dev/null +++ b/src/hex_table.rs @@ -0,0 +1,31 @@ +pub struct HexTable { + len: usize, +} + +const VERTICAL_OFFSET: f32 = 0.866025; +impl HexTable { + pub fn new(len: usize) -> Self{ + Self{ + len + } + } + + pub fn calculate(&self) -> Vec<(f32, f32)>{ + let mut vec = Vec::with_capacity(self.len * self.len); + let mut index = 0; + loop { + let x = index % self.len; + let y = index / self.len; + let y_point = y as f32 * VERTICAL_OFFSET; + + if y_point as usize > self.len { + break; + } + let mut x_point = x as f32; + if y % 2 == 1 { x_point += 0.5; } + vec.push((x_point, y_point)); + index += 1; + } + vec + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6929268..ab90b52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,11 @@ mod basic; mod seed_automation; mod plate_automation; mod subplate_automation; +mod hex_table; use crate::basic::{IntoImage, Table}; use bevy::asset::io::embedded::GetAssetServer; +use bevy::image::TextureFormatPixelInfo; use bevy::input::keyboard::keyboard_input_system; use bevy::prelude::*; use bevy::window::{ExitCondition, WindowResolution}; @@ -12,9 +14,12 @@ use rand::{Rng, RngExt}; use rand_chacha::ChaCha8Rng; use rand_chacha::rand_core::SeedableRng; use seed_automation::SeedAutomation; +use crate::subplate_automation::HexMatrixView; const RECTANGLE_SIDE: f32 = 1250.0; + + fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { @@ -33,25 +38,44 @@ fn main() { .add_systems(Update, plate_automation::update_automation_view) .add_systems(Update, subplate_automation::update_automation) .add_systems(Update, subplate_automation::update_automation_view) + .add_systems(Update, subplate_automation::setup_hex_matrix) .run(); } fn setup(mut commands: Commands, mut images: ResMut>) { commands.spawn(Camera2d); + + let handle = images.add(Image::default()); let mut shape = Sprite::from_image(handle); shape.custom_size = Some(Vec2::new(RECTANGLE_SIDE, RECTANGLE_SIDE)); let automation = SeedAutomation { - world: Table::new(false, 16), + world: Table::new(false, 64), }; commands.spawn((shape, automation)); let seeded_rng = ChaCha8Rng::seed_from_u64(rand::random()); commands.insert_resource(SeededRng(seeded_rng)); + + let mut hex_image = Image::default(); + hex_image.data = Some(vec![ + 0; + hex_image + .texture_descriptor + .format + .pixel_size() + .unwrap_or(0) + ]); + let handle = images.add(hex_image); + + let mut shape = Sprite::from_image(handle); + shape.custom_size = Some(Vec2::new(RECTANGLE_SIDE, RECTANGLE_SIDE)); + + commands.spawn((shape, HexMatrixView, Transform::from_xyz(0.0, 0.0, 1.0))); } #[derive(Resource)] diff --git a/src/plate_automation.rs b/src/plate_automation.rs index 64105ed..e0f1f84 100644 --- a/src/plate_automation.rs +++ b/src/plate_automation.rs @@ -11,7 +11,7 @@ use rand::RngExt; use rand_chacha::ChaCha8Rng; use rayon::prelude::IntoParallelIterator; use crate::seed_automation::SeedAutomation; -use crate::subplate_automation::SubPlateAutomation; +use crate::subplate_automation::{HexMatrixRequest, SubPlateAutomation}; use rayon::iter::ParallelIterator; pub fn update_automation_view( @@ -64,11 +64,12 @@ pub fn update_automation( 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 } }); + let mut new_table = Table::::new(Color::NONE, automation.world.side); + automation.world.convert_copy(&mut new_table, |value| { if value { Color::WHITE } else { Color::NONE} }); commands.entity(entity).remove::().insert(SubPlateAutomation{ world: new_table }); + commands.spawn((HexMatrixRequest)); } } diff --git a/src/seed_automation.rs b/src/seed_automation.rs index 54cafac..b80d3ae 100644 --- a/src/seed_automation.rs +++ b/src/seed_automation.rs @@ -69,8 +69,8 @@ impl SeedAutomation { fn next(&mut self, rng: &mut ChaCha8Rng) { let len = (self.world.side * self.world.side) as f32; loop { - let x = rng.random_range(2..14); - let y = rng.random_range(2..14); + let x = rng.random_range(16..48); + let y = rng.random_range(16..48); if *self.world.get_dim(x, y) { continue; diff --git a/src/subplate_automation.rs b/src/subplate_automation.rs index b3a9536..d2c307a 100644 --- a/src/subplate_automation.rs +++ b/src/subplate_automation.rs @@ -1,16 +1,18 @@ -use rayon::iter::ParallelIterator; -use std::time::Instant; -use crate::SeededRng; use crate::basic::{IntoImage, Table}; +use crate::hex_table::HexTable; +use crate::{RECTANGLE_SIDE, SeededRng}; use bevy::asset::{Assets, RenderAssetUsages}; use bevy::image::{BevyDefault, Image}; use bevy::input::ButtonInput; -use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite}; +use bevy::math::Vec2; +use bevy::prelude::{Color, Commands, Component, Entity, KeyCode, Query, Res, ResMut, Sprite, With}; 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 rayon::iter::IntoParallelIterator; +use rayon::iter::ParallelIterator; +use std::time::Instant; pub fn update_automation_view( query: Query<(&Sprite, &SubPlateAutomation)>, @@ -43,7 +45,6 @@ pub fn update_automation( mut seeded_rng: ResMut, keys: Res>, commands: Commands, - ) { if query.is_empty() { return; @@ -78,15 +79,21 @@ impl SubPlateAutomation { 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 { + if current_color == Color::NONE || current_color != Color::WHITE { continue; } - if rng.random::() < 0.7 { + if rng.random::() < 0.8 { continue; } - let around = self.world.around_line(index).iter().filter(|v| ***v != Color::WHITE && ***v != Color::BLACK).map(|color| (*color).clone()).collect::>(); + let around = self + .world + .around_line(index) + .iter() + .filter(|v| ***v != Color::WHITE && ***v != Color::NONE) + .map(|color| (*color).clone()) + .collect::>(); if around.len() == 0 { continue; @@ -97,38 +104,108 @@ impl SubPlateAutomation { } 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| { + for index in range { 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 + continue; } } counts.push((p.clone(), 1)); } - counts.iter().max_by_key(|(_, c)| {c}).unwrap().0 - }).collect::>(); - self.world.data = update; + self.world + .set(index, counts.iter().max_by_key(|(_, c)| c).unwrap().0); + } } - fn seed(&mut self, rng: &mut ChaCha8Rng){ + fn seed(&mut self, rng: &mut ChaCha8Rng) { let len = (self.world.side * self.world.side) as f32; loop { let index = (rng.random::() * len) as usize; - if *self.world.get(index) == Color::BLACK { + if *self.world.get(index) == Color::NONE { continue; } - self.world.set(index, Color::hsv(rng.random::() * 360.0, 1.0, 1.0)); + self.world + .set(index, Color::hsv(rng.random::() * 360.0, 1.0, 1.0)); break; } } } + +pub fn setup_hex_matrix( + mut request_query: Query<(&HexMatrixRequest, Entity)>, + mut view_query: Query<&Sprite, With>, + mut automation_query: Query<&SubPlateAutomation>, + mut commands: Commands, + mut images: ResMut>, +) { + 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; + + let generator = HexTable::new(resolution); + let mut table = Table::new(false, automation.world.side); + generator.calculate().iter().for_each(|(x, y)| { + let x = (x * 4.0) as usize; + let y = (y * 4.0) as usize; + + if x + y * table.side >= table.data.len() { + return; + } + + if *automation.world.get_dim(x, y) == Color::NONE { + 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{ + points: table + },)); +} + +#[derive(Component)] +pub struct HexMatrixRequest; + +#[derive(Component)] +pub struct HexMatrixView; + +#[derive(Component)] +pub struct HexMatrixBuild{ + points: Table +}