diff options
author | Katharina Fey <kookie@spacekookie.de> | 2021-02-06 19:40:53 +0100 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2021-02-06 19:42:04 +0100 |
commit | cf9392a33bb99ae581f818d3ddb8be1231521a02 (patch) | |
tree | 8295d8a4ed199c3263eadd8f1a508b98567a44f7 /games/rstnode/rst-core/src/config | |
parent | 56d96b2f22bf6a61ff992b000215dc3a2c2448ad (diff) |
rstnode: restructure project into workspace and sub-crates
Diffstat (limited to 'games/rstnode/rst-core/src/config')
-rw-r--r-- | games/rstnode/rst-core/src/config/io.rs | 45 | ||||
-rw-r--r-- | games/rstnode/rst-core/src/config/mod.rs | 50 |
2 files changed, 95 insertions, 0 deletions
diff --git a/games/rstnode/rst-core/src/config/io.rs b/games/rstnode/rst-core/src/config/io.rs new file mode 100644 index 000000000000..07aa7e7b3d3f --- /dev/null +++ b/games/rstnode/rst-core/src/config/io.rs @@ -0,0 +1,45 @@ +//! I/O utilities to load configurations from disk + +use super::MapCfg; +use serde_yaml::from_str; +use std::{ + fs::File, + io::{self, Read}, + path::Path, +}; + +/// Encode error state while loading a map configuration +pub enum Error { + Io(String), + Parsing(String), +} + +impl From<io::Error> for Error { + fn from(e: io::Error) -> Self { + Self::Io(e.to_string()) + } +} + +impl From<serde_yaml::Error> for Error { + fn from(e: serde_yaml::Error) -> Self { + Self::Parsing(e.to_string()) + } +} + +/// Load a map YAML configuration from disk +/// +/// This file format is structured according to the configuration +/// types in [config](crate::config). An example configuration is +/// provided below. +/// +/// ```yaml +/// nodes: +/// - +/// ``` +pub fn load_map<'p>(p: impl Into<&'p Path>) -> Result<MapCfg, Error> { + let mut f = File::open(p.into())?; + let mut s = String::new(); + f.read_to_string(&mut s)?; + + Ok(from_str(s.as_str())?) +} diff --git a/games/rstnode/rst-core/src/config/mod.rs b/games/rstnode/rst-core/src/config/mod.rs new file mode 100644 index 000000000000..447c0fcbcc35 --- /dev/null +++ b/games/rstnode/rst-core/src/config/mod.rs @@ -0,0 +1,50 @@ +//! The file formats backing maps and other configs +//! +//! When creating maps to share with other clients, or to load into a +//! server, these types can be constructed with the YML description +//! format, outlined in [load_map](config::io::load_map) + +mod io; +pub use io::*; + +use crate::data::{LinkId, NodeId}; +use serde::{Deserialize, Serialize}; + +/// A config tree that describes a map +#[derive(Serialize, Deserialize)] +pub struct MapCfg { + /// The set of nodes + pub nodes: Vec<NodeCfg>, + /// Links connecting nodes + pub links: Vec<LinkCfg>, + /// Default spawn points (player count) + pub spawns: Vec<SpawnCfg>, +} + +/// A single node on a map, with an x and y coordinate +#[derive(Serialize, Deserialize)] +pub struct NodeCfg { + /// Node ID + pub id: NodeId, + /// Render/world position + pub x: f64, + pub y: f64, +} + +/// A link between two nodes +#[derive(Serialize, Deserialize)] +pub struct LinkCfg { + /// The link ID + id: LinkId, + /// List of connectioned nodes + con: (NodeId, NodeId), +} + +/// Special configuration for a node to act as a player spawn point +#[derive(Serialize, Deserialize)] +pub struct SpawnCfg { + /// The node of the spawn point + pub n_id: NodeId, + /// At what number of players is this spawn available? + pub max_players: usize, +} |