Преглед на файлове

Started Chunk a 32x32 (1024) grid/area

Chunks will be 1024 u64s

Currently Chunk.fill is broken, as it doesn't properly obtain corner1 to corner2 but not everything between
david преди 1 месец
родител
ревизия
521b5e4a34
променени са 4 файла, в които са добавени 150 реда и са изтрити 9 реда
  1. 132 0
      src/chunk.rs
  2. 3 0
      src/lib.rs
  3. 7 1
      src/main.rs
  4. 8 8
      src/tile_index.rs

+ 132 - 0
src/chunk.rs

@@ -0,0 +1,132 @@
+use std::{cmp::Ordering::Greater, fmt::{Debug, Display}, ops::{Index, IndexMut}};
+use point::Point;
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
+pub struct Chunk {
+    tiles: Vec<u64>
+}
+
+impl Index<Point<u64>> for Chunk {
+    type Output = u64;
+    fn index(&self, index: Point<u64>) -> &Self::Output {
+        let idx = index.distance_value();
+        if idx >= 32*32 {
+            panic!("index out of bounds");
+        }
+        &self.tiles[idx as usize]
+    }
+}
+
+impl IndexMut<Point<u64>> for Chunk {
+    fn index_mut(&mut self, index: Point<u64>) -> &mut Self::Output {
+        let idx = index.distance_value();
+        if idx >= 32*32 {
+            panic!("index out of bounds");
+        }
+        &mut self.tiles[idx as usize]
+    }
+}
+
+impl Index<&Point<u64>> for Chunk {
+    type Output = u64;
+    fn index(&self, index: &Point<u64>) -> &Self::Output {
+        let idx = index.distance_value();
+        if idx >= 32*32 {
+            panic!("index out of bounds");
+        }
+        &self.tiles[idx as usize]
+    }
+}
+
+impl IndexMut<&Point<u64>> for Chunk {
+    fn index_mut(&mut self, index: &Point<u64>) -> &mut Self::Output {
+        let idx = index.distance_value();
+        if idx >= 32*32 {
+            panic!("index out of bounds");
+        }
+        &mut self.tiles[idx as usize]
+    }
+}
+
+impl Chunk {
+    pub fn new() -> Self {
+        let mut s = Self { tiles: Vec::with_capacity(1024) };
+        for _ in 0..1024 {
+            s.tiles.push(0);
+        }
+        s
+    }
+    pub fn new_filled(value: u64) -> Self {
+        let mut s = Self { tiles: Vec::with_capacity(1024) };
+        for _ in 0..1024 {
+            s.tiles.push(value);
+        }
+        s
+    }
+    pub fn get(&self, pos: &Point<u64>) -> Option<&u64> {
+        let idx = pos.distance_value();
+        if idx >= 32*32 {
+            return None;
+        }
+        self.tiles.get(idx as usize)
+    }
+    pub fn get_mut(&mut self, pos: &Point<u64>) -> Option<&mut u64> {
+        let idx = pos.distance_value();
+        if idx >= 32*32 {
+            return None;
+        }
+        self.tiles.get_mut(idx as usize)
+    }
+    pub fn set(&mut self, pos: &Point<u64>, value: u64) -> bool {
+        let idx = pos.distance_value();
+        if idx >= 32*32 {
+            return false;
+        }
+        self.tiles[idx as usize] = value;
+        true
+    }
+    pub fn fill(&mut self, pos1: &Point<u64>, pos2: &Point<u64>, value: u64) -> bool {
+        let (corn1, corn2) = match pos1.partial_cmp(pos2).unwrap() {
+            Greater => {
+                (pos2.distance_value(), pos1.distance_value())
+            },
+            _ => {
+                (pos1.distance_value(), pos2.distance_value())
+            }
+        };
+        if corn1 >= 32*32 || corn2 >= 32*32 {
+            return false;
+        }
+        for idx in corn1..corn2 {
+            self.tiles[idx as usize] = value;
+        }
+        true
+    }
+    pub fn fill_all(&mut self, value: u64) {
+        self.tiles.clear();
+        self.tiles.extend_from_slice(&[value; 1024]);
+    }
+    pub fn clear(&mut self) {
+        self.tiles.clear();
+    }
+}
+
+impl Default for Chunk {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl Display for Chunk {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        for y in 0..32u64 {
+            for x in 0..32u64 {
+                let idx = (x + y) as usize;
+                f.write_str(format!("{}", self.tiles[idx]).as_str())?;
+            }
+            f.write_str("\r\n")?;
+        }
+        Ok(())
+    }
+}

+ 3 - 0
src/lib.rs

@@ -4,3 +4,6 @@ pub use tile::Tile;
 
 mod tile_index;
 pub use tile_index::TileIndex;
+
+mod chunk;
+pub use chunk::Chunk;

+ 7 - 1
src/main.rs

@@ -8,6 +8,7 @@ use server::AppServer;
 
 use std::path::PathBuf;
 
+use point::Point;
 use rmmo::*;
 
 fn main() -> Result<(), anyhow::Error> {
@@ -17,7 +18,7 @@ fn main() -> Result<(), anyhow::Error> {
         Ok(index) => {
             println!("Located {} tiles", index.len());
             let mut idx: usize = 0;
-            for t in index {
+            for t in index.clone() {
                 if let Some(desc) = &t.description {
                     println!("{:3}] \"{:25}\" = '{:1}' {:25} \"{}\"", idx, t.name, t.symbol, t.color, desc);
                 } else {
@@ -25,6 +26,11 @@ fn main() -> Result<(), anyhow::Error> {
                 }
                 idx += 1;
             }
+            let mut c = Chunk::new();
+            if let Some(floor) = index.lookup_index("ground-stone") {
+                c.fill(&Point::from((3, 3)), &Point::from((4, 4)), floor);
+            }
+            println!("{}", c);
         }
         Err(err) => {
             if err.to_string() != "The system cannot find the file specified. (os error 2)".to_string() {

+ 8 - 8
src/tile_index.rs

@@ -26,8 +26,8 @@ impl TileIndex {
     pub fn len(&self) -> usize {
         self.tiles.len()
     }
-    pub fn lookup_index(&self, name: &str) -> Option<usize> {
-        let mut idx: usize = 0;
+    pub fn lookup_index(&self, name: &str) -> Option<u64> {
+        let mut idx: u64 = 0;
         for t in self.tiles.iter() {
             if t.name == name.to_string() {
                 return Some(idx);
@@ -44,16 +44,16 @@ impl TileIndex {
         }
         None
     }
-    pub fn get_index(&self, index: usize) -> Option<&Tile> {
-        if index >= self.tiles.len() {
+    pub fn get_index(&self, index: u64) -> Option<&Tile> {
+        if index >= self.tiles.len() as u64 {
             return None;
         }
-        Some(&self.tiles[index])
+        Some(&self.tiles[index as usize])
     }
     pub fn set(&mut self, t: Tile) -> usize {
         if let Some(idx) = self.lookup_index(&t.name) {
-            self.tiles[idx] = t;
-            return idx;
+            self.tiles[idx as usize] = t;
+            return idx as usize;
         }
         self.tiles.push(t);
         self.tiles.len()
@@ -75,7 +75,7 @@ impl Default for TileIndex {
 
 pub struct TileIndexIter {
     tile_index: TileIndex,
-    index: usize,
+    index: u64,
 }
 
 impl TileIndexIter {