rust/landgen/src/wavefront_collapse/generator.rs
branchtransitional_engine
changeset 15922 da6b67f13c12
parent 15920 168f44ef9b67
child 15923 d46ad15c6dec
equal deleted inserted replaced
15921:5f00829c55ec 15922:da6b67f13c12
     6 use std::collections::HashSet;
     6 use std::collections::HashSet;
     7 use std::fs::File;
     7 use std::fs::File;
     8 use std::io::{BufReader, Result};
     8 use std::io::{BufReader, Result};
     9 use std::path::Path;
     9 use std::path::Path;
    10 
    10 
       
    11 #[derive(Clone)]
       
    12 pub struct EdgeDescription {
       
    13     pub name: String,
       
    14     pub reversed: Option<bool>,
       
    15     pub symmetrical: Option<bool>,
       
    16 }
       
    17 
       
    18 #[derive(Clone)]
       
    19 pub struct EdgesDescription {
       
    20     pub top: EdgeDescription,
       
    21     pub right: EdgeDescription,
       
    22     pub bottom: EdgeDescription,
       
    23     pub left: EdgeDescription,
       
    24 }
       
    25 
       
    26 #[derive(Clone)]
       
    27 pub struct TileDescription {
       
    28     pub name: String,
       
    29     pub edges: EdgesDescription,
       
    30     pub can_flip: bool,
       
    31     pub can_mirror: bool,
       
    32     pub can_rotate90: bool,
       
    33     pub can_rotate180: bool,
       
    34     pub can_rotate270: bool,
       
    35 }
       
    36 
       
    37 pub struct TemplateDescription {
       
    38     pub size: Size,
       
    39     pub tiles: Vec<TileDescription>,
       
    40 }
       
    41 
    11 pub struct WavefrontCollapseLandGenerator {
    42 pub struct WavefrontCollapseLandGenerator {
    12     pub size: Size,
    43     pub template: TemplateDescription,
    13 }
    44 }
    14 
    45 
    15 impl WavefrontCollapseLandGenerator {
    46 impl WavefrontCollapseLandGenerator {
    16     pub fn new(size: &Size) -> Self {
    47     pub fn new(template: TemplateDescription) -> Self {
    17         Self { size: *size }
    48         Self { template }
    18     }
    49     }
    19 
    50 
    20     fn load_image_tiles<T: Copy + PartialEq + Default>(
    51     fn load_image_tiles<T: Copy + PartialEq + Default>(
    21         parameters: &LandGenerationParameters<T>,
    52         parameters: &LandGenerationParameters<T>,
    22         path: &Path,
    53         path: &Path,
   138 
   169 
   139         let wfc_size = if let Some(first_tile) = tiles.first() {
   170         let wfc_size = if let Some(first_tile) = tiles.first() {
   140             let tile_size = first_tile.size();
   171             let tile_size = first_tile.size();
   141 
   172 
   142             Size::new(
   173             Size::new(
   143                 self.size.width / tile_size.width,
   174                 self.template.size.width / tile_size.width,
   144                 self.size.height / tile_size.height,
   175                 self.template.size.height / tile_size.height,
   145             )
   176             )
   146         } else {
   177         } else {
   147             Size::new(1, 1)
   178             Size::new(1, 1)
   148         };
   179         };
   149 
   180 
   150         wfc.generate_map(&wfc_size, |_| {}, random_numbers);
   181         wfc.generate_map(&wfc_size, |_| {}, random_numbers);
   151 
   182 
   152         let grid = wfc.grid();
   183         let mut result = land2d::Land2D::new(&self.template.size, parameters.zero);
   153 
       
   154         for r in 0..grid.height() {
       
   155             for c in 0..grid.width() {
       
   156                 print!("{:?} ", grid.get(r, c));
       
   157             }
       
   158 
       
   159             println!();
       
   160         }
       
   161 
       
   162         let mut result = land2d::Land2D::new(&self.size, parameters.zero);
       
   163 
   184 
   164         for row in 0..wfc_size.height {
   185         for row in 0..wfc_size.height {
   165             for column in 0..wfc_size.width {
   186             for column in 0..wfc_size.width {
   166                 if let Some(Tile::Numbered(tile_index)) = wfc.grid().get(row, column) {
   187                 if let Some(Tile::Numbered(tile_index)) = wfc.grid().get(row, column) {
   167                     let tile = &tiles[*tile_index];
   188                     let tile = &tiles[*tile_index];
   192     use crate::{LandGenerationParameters, LandGenerator};
   213     use crate::{LandGenerationParameters, LandGenerator};
   193     use integral_geometry::Size;
   214     use integral_geometry::Size;
   194     use std::fs::File;
   215     use std::fs::File;
   195     use std::io::BufWriter;
   216     use std::io::BufWriter;
   196     use std::path::Path;
   217     use std::path::Path;
   197     use vec2d::Vec2D;
       
   198 
   218 
   199     #[test]
   219     #[test]
   200     fn test_generation() {
   220     fn test_generation() {
   201         let wfc_gen = WavefrontCollapseLandGenerator::new(&Size::new(2048, 1024));
   221         let wfc_gen = WavefrontCollapseLandGenerator::new(&Size::new(2048, 1024));
   202         let landgen_params = LandGenerationParameters::new(0u32, 0xff000000u32, 0, true, true);
   222         let landgen_params = LandGenerationParameters::new(0u32, 0xff000000u32, 0, true, true);