Explorar o código

primitive wip

Johann Woelper %!s(int64=6) %!d(string=hai) anos
pai
achega
9fabeabd84
Modificáronse 4 ficheiros con 159 adicións e 22 borrados
  1. 74 0
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. BIN=BIN
      landscape.blend
  4. 83 21
      src/main.rs

+ 74 - 0
Cargo.lock

@@ -66,6 +66,14 @@ name = "byteorder"
 version = "1.3.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "c2-chacha"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "cc"
 version = "1.0.46"
@@ -154,6 +162,16 @@ name = "fuchsia-cprng"
 version = "0.1.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "getrandom"
+version = "0.1.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)",
+ "wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "itoa"
 version = "0.4.4"
@@ -199,6 +217,11 @@ dependencies = [
  "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "ppv-lite86"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "proc-macro2"
 version = "1.0.5"
@@ -215,6 +238,27 @@ dependencies = [
  "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "rand"
+version = "0.7.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "rand_core"
 version = "0.3.1"
@@ -228,6 +272,22 @@ name = "rand_core"
 version = "0.4.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "rand_core"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "rand_os"
 version = "0.1.3"
@@ -365,11 +425,17 @@ name = "unicode-xid"
 version = "0.2.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "wasi"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "wfc"
 version = "0.1.0"
 dependencies = [
  "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -405,6 +471,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
 "checksum blake2b_simd 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "5850aeee1552f495dd0250014cf64b82b7c8879a89d83b33bbdace2cc4f63182"
 "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
+"checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb"
 "checksum cc 1.0.46 (registry+https://github.com/rust-lang/crates.io-index)" = "0213d356d3c4ea2c18c40b037c3be23cd639825c18f25ee670ac7813beeef99c"
 "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
 "checksum chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e8493056968583b0193c1bb04d6f7684586f3726992d6c573261941a895dbd68"
@@ -416,6 +483,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum failure 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f8273f13c977665c5db7eb2b99ae520952fe5ac831ae4cd09d80c4c7042b5ed9"
 "checksum failure_derive 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0bc225b78e0391e4b8683440bf2e63c2deeeb2ce5189eab46e2b68c6d3725d08"
 "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
+"checksum getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e7db7ca94ed4cd01190ceee0d8a8052f08a247aa1b469a7f68c6a3b71afcf407"
 "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
 "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
 "checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8"
@@ -423,10 +491,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
 "checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09"
 "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
+"checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
 "checksum proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90cf5f418035b98e655e9cdb225047638296b862b42411c4e45bb88d700f7fc0"
 "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
+"checksum rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae1b169243eaf61759b8475a998f0a385e42042370f3a7dbaf35246eacc8412"
+"checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853"
 "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
 "checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
+"checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
+"checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
 "checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
 "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
 "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
@@ -443,6 +516,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 "checksum term 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c0863a3345e70f61d613eab32ee046ccd1bcc5f9105fe402c61fcd0c13eeb8b5"
 "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
 "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
+"checksum wasi 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b89c3ce4ce14bdc6fb6beaf9ec7928ca331de5df7e5ea278375642a2f478570d"
 "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
 "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 2 - 1
Cargo.toml

@@ -12,4 +12,5 @@ serde = "1.0"
 serde_derive = "1.0"
 serde_json = "1.0"
 log = "*"
-simplelog = "*"
+simplelog = "*"
+rand = "*"

BIN=BIN
landscape.blend


+ 83 - 21
src/main.rs

@@ -5,15 +5,17 @@ extern crate serde_json;
 use std::io::BufWriter;
 use std::io::BufReader;
 use std::fs::File;
-use std::ops::{Add, Sub, Div};
+// use std::ops::{Add, Sub, Div};
+use std::ops::{Add, Mul};
 use std::collections::HashMap;
-// use nalgebra::Vector3;
 #[macro_use] extern crate log;
 extern crate simplelog;
 use simplelog::*;
+use std::fmt;
+use rand::seq::SliceRandom; 
 
 
-#[derive(Clone, Debug, Serialize, Deserialize, Copy, Eq, PartialEq, Hash)]
+#[derive(Clone, Serialize, Deserialize, Copy, Eq, PartialEq, Hash)]
 struct V3 {
     x: i32,
     y: i32,
@@ -50,6 +52,14 @@ impl V3 {
 }
 
 
+
+
+impl fmt::Debug for V3 {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "V[{},{},{}]", self.x, self.y, self.z)
+    }
+}
+
 impl Add<V3> for  V3 {
     type Output = V3;
     fn add(self, other: V3) -> V3 {
@@ -72,6 +82,17 @@ impl Add<i32> for  V3 {
     }
 }
 
+impl Mul<i32> for  V3 {
+    type Output = V3;
+    fn mul(self, other: i32) -> V3 {
+        V3 {
+            x: self.x * other,
+            y: self.y * other,
+            z: self.z * other,
+        }
+    }
+}
+
 impl<'a, 'b> Add<&'b V3> for &'a V3 {
     type Output = V3;
 
@@ -84,6 +105,18 @@ impl<'a, 'b> Add<&'b V3> for &'a V3 {
     }
 }
 
+impl<'a> Mul<i32> for &'a V3 {
+    type Output = V3;
+
+    fn mul(self, other: i32) -> V3 {
+        V3 {
+            x: self.x * other,
+            y: self.y * other,
+            z: self.z * other,
+        }
+    }
+}
+
 // impl Sub<V3> for  V3 {
 //     type Output = V3;
 //     fn sub(self, other: V3) -> V3 {
@@ -121,9 +154,12 @@ fn unitized_grid(vol: &HashMap<V3, String>, divisor: i32) -> HashMap<V3, String>
 
 
 fn guess_gridsize(vol: &HashMap<V3, String>) -> i32{
-    let mut x_positions: Vec<i32> = vol.iter().map(|(k,v)| k.x).collect();
+    let mut x_positions: Vec<i32> = vol.iter().map(|(k, _v)| k.x).collect();
+    let mut y_positions: Vec<i32> = vol.iter().map(|(k, _v)| k.y).collect();
     x_positions.sort();
     x_positions.dedup();
+    y_positions.sort();
+    y_positions.dedup();
     let mut dist_count: HashMap<i32, i32> = HashMap::new();
     let mut last: Option<i32> = None;
     for px in x_positions {
@@ -187,14 +223,14 @@ fn load_export(path: &'static str) -> HashMap<V3, String> {
 }
 
 
-fn get_adjacent_tiles(grid: &HashMap<V3, String>, training_data: &HashMap<String, HashMap<V3, Vec<String>>>) -> HashMap<V3, String> {
-    let mut adjacents: HashMap<V3, String> = HashMap::new();
+// fn get_adjacent_tiles(grid: &HashMap<V3, String>, training_data: &HashMap<String, HashMap<V3, Vec<String>>>) -> HashMap<V3, String> {
+//     let mut adjacents: HashMap<V3, String> = HashMap::new();
 
-    for (pos, tile) in grid {
+//     for (pos, tile) in grid {
 
-    }
-    adjacents
-}
+//     }
+//     adjacents
+// }
 
 fn get_unsolved_neighbors(cell: &V3, grid: &HashMap<V3, String>) -> Vec<V3> {
     let mut neighbors = vec![];
@@ -209,21 +245,35 @@ fn get_unsolved_neighbors(cell: &V3, grid: &HashMap<V3, String>) -> Vec<V3> {
 
 
 
-fn wfc_solver(training_data: HashMap<String, HashMap<V3, Vec<String>>>) -> HashMap<V3, String> {
-    let mut new_grid: HashMap<V3, String> = HashMap::new();
+fn wfc_solver(training_data: &HashMap<String, HashMap<V3, Vec<String>>>, base_map: HashMap<V3, String>) -> HashMap<V3, String> {
+    let mut new_grid = base_map;
     
-    // init start conditions
-    new_grid.insert(V3::new(10, 0, 0), String::from("grass"));
 
-    for (pos, name) in &new_grid {
-        for unsolved_neighbor in get_unsolved_neighbors(pos, &new_grid) {
-            println!("Solving {:?} (next to {})", unsolved_neighbor, name);
-            let trained_data_for_this_tile = training_data.get(name).unwrap();
+
+    for (pos, name) in new_grid.clone() {
+        for unsolved_neighbor in get_unsolved_neighbors(&pos, &new_grid) {
+            for tile_neighbor in &unsolved_neighbor.offsets() {
+                if let Some(tn) = new_grid.get(&(&unsolved_neighbor+tile_neighbor)) {
+                    if let Some(constraints) = training_data.get(tn).unwrap().get(tile_neighbor) {
+                        // println!("\t{} points to me, accepts {:?}", tn, constraints);
+                        println!("Solving {:?} - looking into neighbors", unsolved_neighbor);
+                        let cs = constraints.choose(&mut rand::thread_rng()).unwrap();
+                        println!("\tchose {:?}", cs);
+                        new_grid.insert(unsolved_neighbor, cs.to_string());
+
+                    }
+                }
+
+            }
+
+            // println!("Training data: {:?}", trained_data_for_this_tile);
+
         }
 
     }
 
 
+    println!("grid: {:?}", new_grid);
 
     new_grid
 }
@@ -255,13 +305,25 @@ fn main() {
     //     }
     // }
 
-    let new_grid: HashMap<V3, String> = HashMap::new();
-
-    wfc_solver(training_data);
+    // let new_grid: HashMap<V3, String> = HashMap::new();
+    let mut base_map: HashMap<V3, String> = HashMap::new();
+    // init start conditions
+    base_map.insert(V3::new(10, 0, 0), String::from("grass"));
+    
+    base_map = wfc_solver(&training_data, base_map);
+    base_map = wfc_solver(&training_data, base_map);
+    base_map = wfc_solver(&training_data, base_map);
 
     // dbg!(&vol);
     // let mut adj_map: HashMap<String, Adjacency> = HashMap::new();
 
+    let mut export = vec![];
+    for (p, n) in &base_map {
+        export.push((p*gridsize,n));
+    }
+    let writer = BufWriter::new(File::create("map.json").unwrap());
+    serde_json::to_writer_pretty(writer, &export).unwrap();
+
 }