rust/mapgen/src/lib.rs
changeset 14160 c24a76f131d6
parent 14156 74ca70cb753d
child 14164 1749961647b9
equal deleted inserted replaced
14159:0aeea29ef890 14160:c24a76f131d6
     3 use std::{
     3 use std::{
     4     collections::hash_map::HashMap,
     4     collections::hash_map::HashMap,
     5     borrow::Borrow,
     5     borrow::Borrow,
     6     mem::replace
     6     mem::replace
     7 };
     7 };
     8 use serde::{Deserialize};
       
     9 use serde_derive::{Deserialize};
     8 use serde_derive::{Deserialize};
    10 use serde_yaml;
     9 use serde_yaml;
    11 use integral_geometry::{Point, Size, Rect};
    10 use integral_geometry::{Point, Size, Rect};
    12 use landgen::{
    11 use landgen::{
    13     outline_template::OutlineTemplate
    12     outline_template::OutlineTemplate
    14 };
    13 };
    15 use rand::{thread_rng, Rng};
    14 use rand::{thread_rng, Rng};
    16 use land2d::Land2D;
    15 use land2d::Land2D;
       
    16 use vec2d::Vec2D;
    17 use self::theme::Theme;
    17 use self::theme::Theme;
    18 
    18 
    19 #[derive(Deserialize)]
    19 #[derive(Deserialize)]
    20 struct PointDesc {
    20 struct PointDesc {
    21     x: u32,
    21     x: u32,
   104 
   104 
   105     pub fn get_template(&self, template_type: &str) -> Option<&OutlineTemplate> {
   105     pub fn get_template(&self, template_type: &str) -> Option<&OutlineTemplate> {
   106         self.templates.get(template_type).and_then(|t| thread_rng().choose(t))
   106         self.templates.get(template_type).and_then(|t| thread_rng().choose(t))
   107     }
   107     }
   108 
   108 
   109     pub fn make_texture(&self, land: &Land2D<u32>, theme: &Theme) {
   109     pub fn make_texture(&self, land: &Land2D<u32>, theme: &Theme) -> Vec2D<u32> {
   110 
   110         let mut texture = Vec2D::new(land.size(), 0);
   111     }
   111         if let Some(land_sprite) = theme.land_texture() {
       
   112             for (row_index, (land_row, tex_row)) in land.rows()
       
   113                 .zip(texture.rows_mut())
       
   114                 .enumerate()
       
   115             {
       
   116                 let sprite_row = land_sprite.get_row(row_index % land_sprite.height());
       
   117                 let mut x_offset = 0;
       
   118                 while sprite_row.len() < land.width() - x_offset {
       
   119                     let copy_range = x_offset..x_offset + sprite_row.len();
       
   120                     tex_row_copy(
       
   121                         &land_row[copy_range.clone()],
       
   122                         &mut tex_row[copy_range],
       
   123                         sprite_row
       
   124                     );
       
   125 
       
   126                     x_offset += land_sprite.width()
       
   127                 }
       
   128 
       
   129                 if x_offset < land.width() {
       
   130                     let final_range = x_offset..land.width() - 1;
       
   131                     tex_row_copy(
       
   132                         &land_row[final_range.clone()],
       
   133                         &mut tex_row[final_range],
       
   134                         &sprite_row[..land.width() - x_offset]
       
   135                     );
       
   136                 }
       
   137             }
       
   138         }
       
   139         texture
       
   140     }
       
   141 }
       
   142 
       
   143 fn tex_row_copy(land_row: &[u32], tex_row: &mut [u32], sprite_row: &[u32]) {
       
   144     for ((land_v, tex_v), sprite_v) in
       
   145         land_row.iter().zip(tex_row.iter_mut()).zip(sprite_row)
       
   146         {
       
   147             *tex_v = if *land_v == 0 {
       
   148                 *sprite_v
       
   149             } else {
       
   150                 0
       
   151             }
       
   152         }
   112 }
   153 }
   113 
   154 
   114 #[cfg(test)]
   155 #[cfg(test)]
   115 mod tests {
   156 mod tests {
   116     use crate::{
   157     use crate::{