Some bug fixes, build fixes and code formatting transitional_engine
authorunC0Rr
Tue, 14 Feb 2023 08:52:20 +0100
branchtransitional_engine
changeset 15924 9502611bffc1
parent 15923 d46ad15c6dec
child 15925 b0e8cc72bfef
Some bug fixes, build fixes and code formatting
rust/landgen/src/wavefront_collapse/generator.rs
rust/landgen/src/wavefront_collapse/tile_image.rs
rust/landgen/src/wavefront_collapse/wavefront_collapse.rs
rust/lib-hedgewars-engine/src/instance.rs
rust/lib-hedgewars-engine/src/lib.rs
rust/lib-hedgewars-engine/src/render/gear.rs
rust/lib-hedgewars-engine/src/world.rs
rust/mapgen/src/lib.rs
rust/mapgen/src/template/outline.rs
rust/mapgen/src/template/wavefront_collapse.rs
share/hedgewars/Data/wfc_templates.yaml
--- a/rust/landgen/src/wavefront_collapse/generator.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/landgen/src/wavefront_collapse/generator.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -27,18 +27,19 @@
 pub struct TileDescription {
     pub name: String,
     pub edges: EdgesDescription,
-    pub is_negative: bool,
-    pub can_flip: bool,
-    pub can_mirror: bool,
-    pub can_rotate90: bool,
-    pub can_rotate180: bool,
-    pub can_rotate270: bool,
+    pub is_negative: Option<bool>,
+    pub can_flip: Option<bool>,
+    pub can_mirror: Option<bool>,
+    pub can_rotate90: Option<bool>,
+    pub can_rotate180: Option<bool>,
+    pub can_rotate270: Option<bool>,
 }
 
 #[derive(Clone)]
 pub struct TemplateDescription {
     pub size: Size,
     pub tiles: Vec<TileDescription>,
+    pub wrap: bool,
 }
 
 pub struct WavefrontCollapseLandGenerator {
@@ -76,7 +77,7 @@
 
         let mut tiles_image_pixels = tiles_image.as_mut_slice().iter_mut();
 
-        let (zero, basic) = if tile_description.is_negative {
+        let (zero, basic) = if tile_description.is_negative.unwrap_or_default() {
             (parameters.basic(), parameters.zero())
         } else {
             (parameters.zero(), parameters.basic())
@@ -100,11 +101,7 @@
                         *tiles_image_pixels
                             .next()
                             .expect("vec2d size matching image dimensions") =
-                            if value[0] == 0u8 {
-                                zero
-                            } else {
-                                basic
-                            };
+                            if value[0] == 0u8 { zero } else { basic };
                     }
                 }
             }
@@ -142,23 +139,25 @@
 
         result.push(tile.clone());
 
-        if tile_description.can_flip {
+        if tile_description.can_flip.unwrap_or_default() {
             result.push(tile.flipped());
         }
-        if tile_description.can_mirror {
+        if tile_description.can_mirror.unwrap_or_default() {
             result.push(tile.mirrored());
         }
-        if tile_description.can_flip && tile_description.can_mirror {
+        if tile_description.can_flip.unwrap_or_default()
+            && tile_description.can_mirror.unwrap_or_default()
+        {
             result.push(tile.mirrored().flipped());
         }
 
-        if tile_description.can_rotate90 {
+        if tile_description.can_rotate90.unwrap_or_default() {
             result.push(tile.rotated90());
         }
-        if tile_description.can_rotate180 {
+        if tile_description.can_rotate180.unwrap_or_default() {
             result.push(tile.rotated180());
         }
-        if tile_description.can_rotate270 {
+        if tile_description.can_rotate270.unwrap_or_default() {
             result.push(tile.rotated270());
         }
 
@@ -229,7 +228,7 @@
             });
         }
 
-        let mut wfc = WavefrontCollapse::default();
+        let mut wfc = WavefrontCollapse::new(self.template.wrap);
         wfc.set_rules(rules);
 
         let wfc_size = if let Some(first_tile) = tiles.first() {
@@ -246,6 +245,8 @@
         wfc.generate_map(&wfc_size, |_| {}, random_numbers);
 
         let mut result = land2d::Land2D::new(&self.template.size, parameters.zero);
+        let offset_y = result.height() - result.play_height();
+        let offset_x = (result.width() - result.play_width()) / 2;
 
         for row in 0..wfc_size.height {
             for column in 0..wfc_size.width {
@@ -255,8 +256,8 @@
                     for tile_row in 0..tile.size().height {
                         for tile_column in 0..tile.size().width {
                             result.map(
-                                (row * tile.size().height + tile_row) as i32,
-                                (column * tile.size().width + tile_column) as i32,
+                                (row * tile.size().height + tile_row + offset_y) as i32,
+                                (column * tile.size().width + tile_column + offset_x) as i32,
                                 |p| {
                                     *p =
                                         *tile.get(tile_row, tile_column).unwrap_or(&parameters.zero)
@@ -271,44 +272,3 @@
         result
     }
 }
-
-#[cfg(test)]
-mod tests {
-    use super::WavefrontCollapseLandGenerator;
-    use crate::{LandGenerationParameters, LandGenerator};
-    use integral_geometry::Size;
-    use std::fs::File;
-    use std::io::BufWriter;
-    use std::path::Path;
-
-    #[test]
-    fn test_generation() {
-        let wfc_gen = WavefrontCollapseLandGenerator::new(&Size::new(2048, 1024));
-        let landgen_params = LandGenerationParameters::new(0u32, 0xff000000u32, 0, true, true);
-        let land = wfc_gen.generate_land(
-            &landgen_params,
-            &mut [0u32, 1u32, 3u32, 5u32, 7u32, 11u32].into_iter().cycle(),
-        );
-
-        let path = Path::new(r"output.png");
-        let file = File::create(path).unwrap();
-        let ref mut w = BufWriter::new(file);
-
-        let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
-        encoder.set_color(png::ColorType::Rgba);
-        encoder.set_depth(png::BitDepth::Eight);
-        encoder.set_source_gamma(png::ScaledFloat::from_scaled(45455)); // 1.0 / 2.2, scaled by 100000
-        encoder.set_source_gamma(png::ScaledFloat::new(1.0 / 2.2)); // 1.0 / 2.2, unscaled, but rounded
-        let source_chromaticities = png::SourceChromaticities::new(
-            // Using unscaled instantiation here
-            (0.31270, 0.32900),
-            (0.64000, 0.33000),
-            (0.30000, 0.60000),
-            (0.15000, 0.06000),
-        );
-        encoder.set_source_chromaticities(source_chromaticities);
-        let mut writer = encoder.write_header().unwrap();
-
-        writer.write_image_data(land.raw_pixel_bytes()).unwrap(); // Save
-    }
-}
--- a/rust/landgen/src/wavefront_collapse/tile_image.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/landgen/src/wavefront_collapse/tile_image.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -11,6 +11,7 @@
 }
 
 impl<I: PartialEq + Clone> Edge<I> {
+    #[inline]
     pub fn new(id: I, symmetrical: bool) -> Self {
         Self {
             id,
@@ -19,6 +20,7 @@
         }
     }
 
+    #[inline]
     pub fn reversed(&self) -> Self {
         Self {
             id: self.id.clone(),
@@ -27,6 +29,7 @@
         }
     }
 
+    #[inline]
     pub fn is_compatible(&self, other: &Self) -> bool {
         self.id == other.id && ((self.reverse != other.reverse) || self.symmetrical)
     }
@@ -115,22 +118,27 @@
         }
     }
 
+    #[inline]
     pub fn right_edge(&self) -> &Edge<I> {
         &self.right
     }
 
+    #[inline]
     pub fn bottom_edge(&self) -> &Edge<I> {
         &self.bottom
     }
 
+    #[inline]
     pub fn left_edge(&self) -> &Edge<I> {
         &self.left
     }
 
+    #[inline]
     pub fn top_edge(&self) -> &Edge<I> {
         &self.top
     }
 
+    #[inline]
     pub fn size(&self) -> Size {
         match self.transform {
             Transform::Rotate0(_) => self.image.size(),
@@ -138,6 +146,7 @@
         }
     }
 
+    #[inline]
     pub fn get(&self, row: usize, column: usize) -> Option<&T> {
         match self.transform {
             Transform::Rotate0(_) => {
--- a/rust/landgen/src/wavefront_collapse/wavefront_collapse.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/landgen/src/wavefront_collapse/wavefront_collapse.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -27,6 +27,7 @@
 pub struct WavefrontCollapse {
     rules: Vec<CollapseRule>,
     grid: Vec2D<Tile>,
+    wrap: bool,
 }
 
 impl Default for WavefrontCollapse {
@@ -34,11 +35,20 @@
         Self {
             rules: Vec::new(),
             grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
+            wrap: false,
         }
     }
 }
 
 impl WavefrontCollapse {
+    pub fn new(wrap: bool) -> Self {
+        Self {
+            rules: Vec::new(),
+            grid: Vec2D::new(&Size::new(1, 1), Tile::Empty),
+            wrap,
+        }
+    }
+
     pub fn generate_map<I: Iterator<Item = u32>, F: FnOnce(&mut Vec2D<Tile>)>(
         &mut self,
         map_size: &Size,
@@ -57,6 +67,20 @@
     }
 
     fn get_tile(&self, y: usize, x: usize) -> Tile {
+        let x = if self.wrap {
+            if x == usize::MAX {
+                self.grid.width() - 1
+            } else {
+                if x == self.grid.width() {
+                    0
+                } else {
+                    x
+                }
+            }
+        } else {
+            x
+        };
+        
         self.grid.get(y, x).copied().unwrap_or_default()
     }
 
--- a/rust/lib-hedgewars-engine/src/instance.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/lib-hedgewars-engine/src/instance.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -5,7 +5,7 @@
 use hedgewars_engine_messages::queue::*;
 
 use integral_geometry::{Point, Rect, Size};
-use landgen::outline_template::OutlineTemplate;
+use landgen::outline_template_based::outline_template::OutlineTemplate;
 
 use std::path::Path;
 
--- a/rust/lib-hedgewars-engine/src/lib.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/lib-hedgewars-engine/src/lib.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -5,7 +5,7 @@
 mod world;
 
 use std::{
-    ffi::{CString, CStr},
+    ffi::{CStr, CString},
     io::{Read, Write},
     mem::replace,
     os::raw::{c_char, c_void},
--- a/rust/lib-hedgewars-engine/src/render/gear.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/lib-hedgewars-engine/src/render/gear.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -71,22 +71,10 @@
 }
 
 const SPRITE_LOAD_LIST: &[(SpriteId, &str)] = &[
-    (
-        SpriteId::Mine,
-        "Graphics/MineOn.png",
-    ),
-    (
-        SpriteId::Grenade,
-        "Graphics/Bomb.png",
-    ),
-    (
-        SpriteId::Cheese,
-        "Graphics/cheese.png",
-    ),
-    (
-        SpriteId::Cleaver,
-        "Graphics/cleaver.png",
-    ),
+    (SpriteId::Mine, "Graphics/MineOn.png"),
+    (SpriteId::Grenade, "Graphics/Bomb.png"),
+    (SpriteId::Cheese, "Graphics/cheese.png"),
+    (SpriteId::Cleaver, "Graphics/cleaver.png"),
 ];
 
 const MAX_SPRITES: usize = SpriteId::MaxSprite as usize + 1;
--- a/rust/lib-hedgewars-engine/src/world.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/lib-hedgewars-engine/src/world.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -7,8 +7,9 @@
 use integral_geometry::{Point, Rect, Size};
 use land2d::Land2D;
 use landgen::{
-    outline_template::OutlineTemplate, template_based::TemplatedLandGenerator,
-    LandGenerationParameters, LandGenerator,
+    outline_template_based::outline_template::OutlineTemplate,
+    outline_template_based::template_based::TemplatedLandGenerator, LandGenerationParameters,
+    LandGenerator,
 };
 use lfprng::LaggedFibonacciPRNG;
 use std::path::{Path, PathBuf};
@@ -64,9 +65,14 @@
         if let Some(ref state) = self.game_state {
             self.camera.position = state.land.play_box().center();
 
+            let parameters = LandGenerationParameters::new(0u32, 0x8000u32, 0, false, false);
             let theme =
                 Theme::load(self.data_path.join(Path::new("Themes/Cheese/")).as_path()).unwrap();
-            let texture = MapGenerator::new().make_texture(&state.land, &theme);
+            let texture = MapGenerator::<OutlineTemplate>::new().make_texture(
+                &state.land,
+                &parameters,
+                &theme,
+            );
             if let Some(ref mut renderer) = self.map_renderer {
                 renderer.init(&texture);
             }
--- a/rust/mapgen/src/lib.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/mapgen/src/lib.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -17,7 +17,6 @@
 };
 use rand::{seq::SliceRandom, Rng};
 
-
 use std::{borrow::Borrow, collections::hash_map::HashMap};
 use vec2d::Vec2D;
 
@@ -249,7 +248,8 @@
 
 #[cfg(test)]
 mod tests {
-    use crate::{MapGenerator, TemplateType};
+    use crate::{MapGenerator, TemplateType, OutlineTemplate};
+    use rand::thread_rng;
 
     #[test]
     fn simple_load() {
@@ -283,14 +283,14 @@
     test: [0]
 "#;
 
-        let mut generator = MapGenerator::new();
+        let mut generator = MapGenerator::<OutlineTemplate>::new();
         generator.import_yaml_templates(&text);
 
         assert!(generator
             .templates
             .contains_key(&TemplateType("test".to_string())));
 
-        let template = generator.get_template("test").unwrap();
+        let template = generator.get_template("test", &mut thread_rng()).unwrap();
 
         assert_eq!(template.islands[0].len(), 7);
     }
--- a/rust/mapgen/src/template/outline.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/mapgen/src/template/outline.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -1,11 +1,9 @@
 use integral_geometry::{Point, Rect, Size};
 
-use landgen::{
-    outline_template_based::outline_template::OutlineTemplate,
-};
+use landgen::outline_template_based::outline_template::OutlineTemplate;
 use serde_derive::Deserialize;
 
-use std::{collections::hash_map::HashMap};
+use std::collections::hash_map::HashMap;
 
 #[derive(Deserialize)]
 pub struct PointDesc {
--- a/rust/mapgen/src/template/wavefront_collapse.rs	Mon Feb 13 17:02:08 2023 +0100
+++ b/rust/mapgen/src/template/wavefront_collapse.rs	Tue Feb 14 08:52:20 2023 +0100
@@ -1,9 +1,9 @@
-use integral_geometry::{Size};
+use integral_geometry::Size;
 
-use landgen::{wavefront_collapse::generator::*};
+use landgen::wavefront_collapse::generator::*;
 use serde_derive::Deserialize;
 
-use std::{collections::hash_map::HashMap};
+use std::collections::hash_map::HashMap;
 
 #[derive(Deserialize)]
 #[serde(remote = "EdgeDescription")]
@@ -32,12 +32,12 @@
     pub name: String,
     #[serde(with = "EdgesDesc")]
     pub edges: EdgesDescription,
-    pub is_negative: bool,
-    pub can_flip: bool,
-    pub can_mirror: bool,
-    pub can_rotate90: bool,
-    pub can_rotate180: bool,
-    pub can_rotate270: bool,
+    pub is_negative: Option<bool>,
+    pub can_flip: Option<bool>,
+    pub can_mirror: Option<bool>,
+    pub can_rotate90: Option<bool>,
+    pub can_rotate180: Option<bool>,
+    pub can_rotate270: Option<bool>,
 }
 
 #[derive(Deserialize)]
@@ -51,6 +51,7 @@
     pub is_negative: bool,
     pub put_girders: bool,
     pub max_hedgehogs: u8,
+    pub wrap: bool,
     pub tiles: Vec<TileDescriptionHelper>,
 }
 
@@ -69,6 +70,7 @@
                 .iter()
                 .map(|TileDescriptionHelper(t)| t.clone())
                 .collect(),
+            wrap: desc.wrap,
         }
     }
 }
--- a/share/hedgewars/Data/wfc_templates.yaml	Mon Feb 13 17:02:08 2023 +0100
+++ b/share/hedgewars/Data/wfc_templates.yaml	Tue Feb 14 08:52:20 2023 +0100
@@ -4,11 +4,12 @@
   # 00
   -
     width: 3960
-    height: 1980
+    height: 1920
     can_invert: false
     is_negative: false
     put_girders: true
     max_hedgehogs: 40
+    wrap: true
     tiles:
       - name: "120_bar.png"
         edges: