Explorar el Código

Updated main to use TileIndex

It also appears I'm doing it wrong... I need to update TileIndex so it uses a Vec<Tile> rather than the horribly inaccurate HashMap<String, Tile>...

TileIndex isn't keeping order... so every time I load or save TileIndex the order will be different... this is NOT desired.

(Vec will fix this as it keeps ordering, unlike HashMap)
david hace 1 mes
padre
commit
828d236ca0
Se han modificado 5 ficheros con 108 adiciones y 1 borrados
  1. 1 0
      .gitignore
  2. 45 0
      Cargo.lock
  3. 1 0
      Cargo.toml
  4. 22 1
      src/main.rs
  5. 39 0
      src/tile_index.rs

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
 /target
 /data
+*.json

+ 45 - 0
Cargo.lock

@@ -882,6 +882,20 @@ dependencies = [
  "windows-sys",
 ]
 
+[[package]]
+name = "num"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
+dependencies = [
+ "num-bigint",
+ "num-complex",
+ "num-integer",
+ "num-iter",
+ "num-rational",
+ "num-traits",
+]
+
 [[package]]
 name = "num-bigint"
 version = "0.4.6"
@@ -910,6 +924,15 @@ dependencies = [
  "zeroize",
 ]
 
+[[package]]
+name = "num-complex"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
+dependencies = [
+ "num-traits",
+]
+
 [[package]]
 name = "num-integer"
 version = "0.1.46"
@@ -930,6 +953,17 @@ dependencies = [
  "num-traits",
 ]
 
+[[package]]
+name = "num-rational"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
+dependencies = [
+ "num-bigint",
+ "num-integer",
+ "num-traits",
+]
+
 [[package]]
 name = "num-traits"
 version = "0.2.19"
@@ -1126,6 +1160,16 @@ dependencies = [
  "spki",
 ]
 
+[[package]]
+name = "point"
+version = "0.1.2"
+source = "git+https://git.red-green.com/david/point#8541bcd4bd981e275d7d6bceddc69e9080737e9c"
+dependencies = [
+ "anyhow",
+ "num",
+ "serde",
+]
+
 [[package]]
 name = "poly1305"
 version = "0.8.0"
@@ -1265,6 +1309,7 @@ dependencies = [
  "crossterm",
  "delegate",
  "futures",
+ "point",
  "ratatui",
  "russh",
  "russh-keys",

+ 1 - 0
Cargo.toml

@@ -10,6 +10,7 @@ color-eyre = "0.6.3"
 crossterm = "0.28.1"
 delegate = "0.13.0"
 futures = "0.3.31"
+point = { git = "https://git.red-green.com/david/point", version = "0.1.2", features = ["serde"] }
 ratatui = { version = "0.28.1", features = ["unstable-widget-ref"] }
 russh = "0.45.0"
 russh-keys = "0.45.0"

+ 22 - 1
src/main.rs

@@ -6,6 +6,27 @@ mod server;
 use server::AppServer;
 */
 
-fn main() {
+use std::{fs, path::PathBuf};
+
+use rmmo::*;
+
+fn main() -> Result<(), anyhow::Error> {
     println!("Hello, World!");
+    if let Ok(content) = fs::read_to_string(PathBuf::from("tiles.json")) {
+        let index = serde_json::from_str::<TileIndex>(&content)?;
+        println!("Located {} tiles", index.tiles.len());
+        for (k, t) in index.tiles.iter() {
+            if let Some(desc) = &t.description {
+                println!("\"{}\" = '{}' {} \"{}\"", k, t.symbol, t.color, desc);
+            } else {
+                println!("\"{}\" = '{}' {}", k, t.symbol, t.color);
+            }
+        }
+    } else {
+        let index = TileIndex::new();
+        let content = serde_json::to_string_pretty(&index)?;
+        fs::write(PathBuf::from("tiles.json"), content)?;
+        println!("Created {} tiles", index.tiles.len());
+    }
+    Ok(())
 }

+ 39 - 0
src/tile_index.rs

@@ -66,4 +66,43 @@ impl TileIndex {
     pub fn set(&mut self, name: &str, t: Tile) {
         self.tiles.insert(name.to_string(), t);
     }
+
+    pub fn new() -> Self {
+        let mut s: Self = Self { tiles: HashMap::with_capacity(24) };
+        // s.set("", Tile::with_description("", "", ""));
+        // s.set("", Tile::new("", ""));
+        s.set("void", Tile::with_description("", "", "You can fly over the void, or fall into it"));
+        s.set("player", Tile::with_description("@", "lime", "You"));
+        s.set("player-ally", Tile::with_description("@", "green", "Another Player, an ally to you"));
+        s.set("player-neutral", Tile::with_description("@", "silver", "Another Player, neutral to you"));
+        s.set("player-enemy", Tile::with_description("@", "red", "Another Player, an enemy to you"));
+        s.set("ground-stone", Tile::with_description(".", "gray", "A stone path, you can walk over it"));
+        s.set("ground-grass", Tile::with_description(".", "green", "A grassy path, you can walk over it"));
+        s.set("ground-sand", Tile::with_description(".", "yellow", "A sandy path, you can walk over it"));
+        s.set("wall-v-stone", Tile::with_description("|", "gray", "A stone wall"));
+        s.set("wall-h-stone", Tile::with_description("-", "gray", "A stone wall"));
+        s.set("wall-v-wood", Tile::with_description("|", "brown", "A wooden wall, flammable"));
+        s.set("wall-h-wood", Tile::with_description("-", "brown", "A wooden wall, flammable"));
+        s.set("door-wood-closed", Tile::with_description("x", "brown", "A wooden door, flammable"));
+        s.set("door-wood-open", Tile::with_description("+", "brown", "A wooden door, flammable, you can walk thru it"));
+        s.set("door-stone-closed", Tile::with_description("x", "gray", "A stone door"));
+        s.set("door-stone-open", Tile::with_description("+", "gray", "A stone door, you can walk thru it"));
+        s.set("door-golden-closed", Tile::with_description("x", "gold", "A golden door, find a gold key to open"));
+        s.set("door-golden-open", Tile::with_description("+", "gold", "A golden door, you can walk thru it"));
+        s.set("gold", Tile::with_description("$", "yellow", "Gold, a common currency"));
+        s.set("gem-blue", Tile::with_description("*", "cyan", "Sapphire, a rare currency"));
+        s.set("gem-red", Tile::with_description("*", "bright red", "Ruby, a rare currency"));
+        s.set("gem-green", Tile::with_description("*", "green", "Emerald, a rare currency"));
+        s.set("gem-purple", Tile::with_description("*", "magenta", "Amethyst, a rare currency"));
+        s.set("gem-white", Tile::with_description("*", "bright white", "Quartz, a rare currency"));
+        s
+    }
+}
+
+impl Default for TileIndex {
+    fn default() -> Self {
+        let mut s = Self { tiles: HashMap::with_capacity(1) };
+        s.set("void", Tile::with_description("", "", "You can fly over the void, or fall into it"));
+        s
+    }
 }