Add the beginning of item entities

Add a new Item raw type and entity type, with preliminary, basic support
for food. There's a really frustrating toml-rs bug that prevents writing
these nicely as toml right now, so I also added support for mixing JSON
and TOML in a single config dir
This commit is contained in:
Griffin Smith 2019-07-19 21:55:09 -04:00
parent 4e9138aa6f
commit 29c80ac8ba
14 changed files with 274 additions and 35 deletions

8
Cargo.lock generated
View file

@ -454,6 +454,11 @@ name = "maplit"
version = "1.0.1" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "matches"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.2.0" version = "2.2.0"
@ -1218,6 +1223,7 @@ dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"log4rs 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "log4rs 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)",
"maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"nom 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "nom 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proptest 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", "proptest 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1226,6 +1232,7 @@ dependencies = [
"serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1301,6 +1308,7 @@ dependencies = [
"checksum log-mdc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7" "checksum log-mdc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7"
"checksum log4rs 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "100052474df98158c0738a7d3f4249c99978490178b5f9f68cd835ac57adbd1b" "checksum log4rs 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "100052474df98158c0738a7d3f4249c99978490178b5f9f68cd835ac57adbd1b"
"checksum maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08cbb6b4fef96b6d77bfc40ec491b1690c779e77b05cd9f07f787ed376fd4c43" "checksum maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08cbb6b4fef96b6d77bfc40ec491b1690c779e77b05cd9f07f787ed376fd4c43"
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
"checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" "checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39"
"checksum miniz-sys 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9e3ae51cea1576ceba0dde3d484d30e6e5b86dee0b2d412fe3a16a15c98202" "checksum miniz-sys 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9e3ae51cea1576ceba0dde3d484d30e6e5b86dee0b2d412fe3a16a15c98202"
"checksum miniz_oxide 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c468f2369f07d651a5d0bb2c9079f8488a66d5466efe42d0c5c6466edcb7f71e" "checksum miniz_oxide 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c468f2369f07d651a5d0bb2c9079f8488a66d5466efe42d0c5c6466edcb7f71e"

View file

@ -14,6 +14,7 @@ itertools = "*"
lazy_static = "*" lazy_static = "*"
log = "*" log = "*"
log4rs = "*" log4rs = "*"
matches = "0.1.8"
maplit = "^1.0.1" maplit = "^1.0.1"
nom = "^5.0.0" nom = "^5.0.0"
prettytable-rs = "^0.8" prettytable-rs = "^0.8"
@ -23,6 +24,7 @@ rand = {version = "^0.7.0", features = ["small_rng"]}
serde = "^1.0.8" serde = "^1.0.8"
serde_derive = "^1.0.8" serde_derive = "^1.0.8"
serde_json = "*" serde_json = "*"
serde_yaml = "0.8"
termion = "*" termion = "*"
toml = "^0.5.1" toml = "^0.5.1"

View file

@ -35,6 +35,12 @@ impl<'a> color::Color for &'a Color {
} }
} }
impl Default for Color {
fn default() -> Self {
Color::new(color::Reset)
}
}
pub struct ColorVisitor { pub struct ColorVisitor {
marker: PhantomData<fn() -> Color>, marker: PhantomData<fn() -> Color>,
} }

View file

@ -4,7 +4,9 @@ use termion::color;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct EntityChar { pub struct EntityChar {
#[serde(default)]
color: Color, color: Color,
#[serde(rename = "char")] #[serde(rename = "char")]
chr: char, chr: char,
} }

44
src/entities/item.rs Normal file
View file

@ -0,0 +1,44 @@
use crate::display;
use crate::entities::raws::{raw, EntityRaw, ItemType};
use crate::entities::EntityID;
use crate::types::Position;
use std::io::{self, Write};
#[derive(Debug, Clone)]
pub struct Item {
pub id: Option<EntityID>,
pub typ: &'static ItemType<'static>,
pub position: Position,
}
impl Item {
pub fn new_from_raw(name: &'static str, position: Position) -> Self {
match raw(name) {
EntityRaw::Item(typ) => Self::new_with_type(typ, position),
_ => panic!("Invalid raw type for {:?}, expected Item", name),
}
}
pub fn new_with_type(
typ: &'static ItemType<'static>,
position: Position,
) -> Self {
Item {
id: None,
typ,
position,
}
}
pub fn is_edible(&self) -> bool {
self.typ.is_edible()
}
}
entity!(Item);
impl display::Draw for Item {
fn do_draw(&self, out: &mut Write) -> io::Result<()> {
write!(out, "{}", self.typ.chr)
}
}

View file

@ -3,12 +3,15 @@ pub mod entity;
pub mod character; pub mod character;
pub mod creature; pub mod creature;
pub mod entity_char; pub mod entity_char;
pub mod item;
pub mod raw_types;
pub mod raws; pub mod raws;
pub use character::Character; pub use character::Character;
pub use creature::Creature; pub use creature::Creature;
pub use entity::{Entity, Identified}; pub use entity::{Entity, Identified};
pub use entity_char::EntityChar; pub use entity_char::EntityChar;
pub use item::Item;
pub use raws::raw; pub use raws::raw;
pub type EntityID = u32; pub type EntityID = u32;

104
src/entities/raw_types.rs Normal file
View file

@ -0,0 +1,104 @@
use crate::entities::entity_char::EntityChar;
use crate::messages::Message;
use crate::types::Speed;
#[derive(Debug, Deserialize)]
pub struct CreatureType<'a> {
/// The name of the creature. Used in raw lookups.
pub name: &'a str,
/// A description of the entity, used by the "look" command
pub description: &'a str,
#[serde(rename = "char")]
pub chr: EntityChar,
pub max_hitpoints: u16,
pub speed: Speed,
pub friendly: bool,
}
#[derive(Debug, Deserialize)]
pub struct EdibleItem<'a> {
#[serde(borrow)]
pub eat_message: Option<Message<'a>>,
/// The number of hitpoints that eating this item heals
pub hitpoints_healed: u16,
}
#[derive(Debug, Deserialize)]
pub struct ItemType<'a> {
pub name: &'a str,
/// A description of the item, used by the "look" command
pub description: &'a str,
pub edible_item: Option<EdibleItem<'a>>,
#[serde(rename = "char")]
pub chr: EntityChar,
}
#[cfg(test)]
mod item_type_tests {
use super::*;
#[test]
fn test_deserialize_item_type() {
let result = serde_json::from_str(
r#"{
"Item": {
"name": "noodles",
"description": "You know exactly what kind of noodles",
"char": { "char": "n" },
"edible_item": {
"eat_message": "You slurp up the noodles",
"hitpoints_healed": 2
}
}
}"#,
)
.unwrap();
assert_matches!(result, EntityRaw::Item(_));
if let EntityRaw::Item(item) = result {
assert_eq!(item.name, "noodles");
}
let toml_result = toml::from_str(
r#"[Item]
name = "noodles"
description = "You know exactly what kind of noodles"
char = { char = "🍜" }
edible_item = { eat_message = "You slurp up the noodles", hitpoints_healed = 2 }
"#,
)
.unwrap();
assert_matches!(toml_result, EntityRaw::Item(_));
if let EntityRaw::Item(item) = toml_result {
assert_eq!(item.name, "noodles");
}
}
}
impl<'a> ItemType<'a> {
pub fn is_edible(&self) -> bool {
self.edible_item.is_some()
}
}
#[derive(Debug, Deserialize)]
pub enum EntityRaw<'a> {
Creature(#[serde(borrow)] CreatureType<'a>),
Item(#[serde(borrow)] ItemType<'a>),
}
impl<'a> EntityRaw<'a> {
pub fn name(&self) -> &'a str {
use EntityRaw::*;
match self {
Creature(typ) => typ.name,
Item(typ) => typ.name,
}
}
}

View file

@ -1,37 +1,8 @@
use crate::entities::entity_char::EntityChar; pub use crate::entities::raw_types::{CreatureType, EntityRaw, ItemType};
use crate::types::Speed;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Deserialize)]
pub struct CreatureType<'a> {
/// The name of the creature. Used in raw lookups.
pub name: &'a str,
/// A description of the entity, used by the "look" command
pub description: &'a str,
#[serde(rename = "char")]
pub chr: EntityChar,
pub max_hitpoints: u16,
pub speed: Speed,
pub friendly: bool,
}
#[derive(Debug, Deserialize)]
pub enum EntityRaw<'a> {
Creature(#[serde(borrow)] CreatureType<'a>),
}
impl<'a> EntityRaw<'a> {
pub fn name(&self) -> &'a str {
match self {
EntityRaw::Creature(typ) => typ.name,
}
}
}
static_cfg! { static_cfg! {
static ref RAWS: Vec<EntityRaw<'static>> = toml_dir("src/entities/raws"); static ref RAWS: Vec<EntityRaw<'static>> = cfg_dir("src/entities/raws");
} }
lazy_static! { lazy_static! {
@ -54,3 +25,14 @@ pub fn raw(name: &'static str) -> &'static EntityRaw<'static> {
.map(|e| *e) .map(|e| *e)
.expect(format!("Raw not found: {}", name).as_str()) .expect(format!("Raw not found: {}", name).as_str())
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_raws() {
RAWS_BY_NAME.keys();
assert_eq!(raw("noodles").name(), "noodles");
}
}

View file

@ -0,0 +1,14 @@
{
"Item": {
"name": "noodles",
"char": {
"char": "🍜"
},
"description": "You know exactly what kind of noodles",
"edible_item": {
"eat_message": "You slurp up the noodles",
"hitpoints_healed": 2
},
"display_name": "big bowl o' noodles"
}
}

View file

@ -1,5 +1,7 @@
use crate::display::{self, Viewport}; use crate::display::{self, Viewport};
use crate::entities::{Character, Creature, Entity, EntityID, Identified}; use crate::entities::{
Character, Creature, Entity, EntityID, Identified, Item,
};
use crate::messages::message; use crate::messages::message;
use crate::settings::Settings; use crate::settings::Settings;
use crate::types::command::Command; use crate::types::command::Command;
@ -80,6 +82,9 @@ impl<'a> Game<'a> {
"gormlak", "gormlak",
pos(10, 0), pos(10, 0),
))); )));
entities
.insert(Box::new(Item::new_from_raw("noodles", pos(0, 10))));
} }
Game { Game {

View file

@ -7,6 +7,7 @@ extern crate serde;
extern crate toml; extern crate toml;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate serde_json;
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
#[macro_use] #[macro_use]
@ -23,6 +24,8 @@ extern crate backtrace;
extern crate include_dir; extern crate include_dir;
#[macro_use] #[macro_use]
extern crate nom; extern crate nom;
#[macro_use]
extern crate matches;
#[macro_use] #[macro_use]
mod util; mod util;

View file

@ -6,7 +6,7 @@ use std::collections::HashMap;
#[derive(Deserialize, Debug, PartialEq, Eq)] #[derive(Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)] #[serde(untagged)]
enum Message<'a> { pub enum Message<'a> {
#[serde(borrow)] #[serde(borrow)]
Single(Template<'a>), Single(Template<'a>),
Choice(Vec<Template<'a>>), Choice(Vec<Template<'a>>),
@ -123,6 +123,13 @@ static_cfg! {
static ref MESSAGES: NestedMap<'static> = toml_file("messages.toml"); static ref MESSAGES: NestedMap<'static> = toml_file("messages.toml");
} }
pub fn get<R: Rng + ?Sized>(
name: &'static str,
rng: &mut R,
) -> Option<&'static Template<'static>> {
MESSAGES.lookup(name).and_then(|msg| msg.resolve(rng))
}
/// Look up and format a game message based on the given (dot-separated) name, /// Look up and format a game message based on the given (dot-separated) name,
/// with the given random generator used to select from choice-based messages /// with the given random generator used to select from choice-based messages
pub fn message<'a, R: Rng + ?Sized>( pub fn message<'a, R: Rng + ?Sized>(
@ -130,7 +137,7 @@ pub fn message<'a, R: Rng + ?Sized>(
rng: &mut R, rng: &mut R,
params: &TemplateParams<'a>, params: &TemplateParams<'a>,
) -> String { ) -> String {
match MESSAGES.lookup(name).and_then(|msg| msg.resolve(rng)) { match get(name, rng) {
Some(msg) => msg.format(params).unwrap_or_else(|e| { Some(msg) => msg.format(params).unwrap_or_else(|e| {
error!("Error formatting template: {}", e); error!("Error formatting template: {}", e);
"Template Error".to_string() "Template Error".to_string()

View file

@ -9,3 +9,6 @@ killed = [
"The {{creature.name}} kicks it.", "The {{creature.name}} kicks it.",
"The {{creature.name}} beefs it." "The {{creature.name}} beefs it."
] ]
[defaults.item]
eat = "You eat the {{item.name}}"

View file

@ -14,6 +14,9 @@ macro_rules! __static_cfg_include {
(json_dir, $filename:expr) => { (json_dir, $filename:expr) => {
include_dir!($filename) include_dir!($filename)
}; };
(cfg_dir, $filename:expr) => {
include_dir!($filename)
};
} }
macro_rules! __static_cfg_type { macro_rules! __static_cfg_type {
@ -21,6 +24,7 @@ macro_rules! __static_cfg_type {
(json_file) => (&'static str); (json_file) => (&'static str);
(toml_dir) => (include_dir::Dir<'static>); (toml_dir) => (include_dir::Dir<'static>);
(json_dir) => (include_dir::Dir<'static>); (json_dir) => (include_dir::Dir<'static>);
(cfg_dir) => (include_dir::Dir<'static>);
} }
macro_rules! __static_cfg_parse { macro_rules! __static_cfg_parse {
@ -39,6 +43,10 @@ macro_rules! __static_cfg_parse {
(json_dir, $e:expr) => { (json_dir, $e:expr) => {
crate::util::static_cfg::parse_json_dir($e) crate::util::static_cfg::parse_json_dir($e)
}; };
(cfg_dir, $e:expr) => {
crate::util::static_cfg::parse_cfg_dir($e);
};
} }
macro_rules! __static_cfg_inner { macro_rules! __static_cfg_inner {
@ -70,13 +78,61 @@ macro_rules! static_cfg {
() => () () => ()
} }
pub fn parse_cfg_dir<'a, T>(d: Dir<'a>) -> Vec<T>
where
T: de::Deserialize<'a>,
{
d.files()
.iter()
.filter_map(|f| {
let path = f.path();
let contents = f.contents_utf8().unwrap();
match path.extension().and_then(|e| e.to_str()) {
Some("toml") => {
Some(toml::from_str(contents).unwrap_or_else(|e| {
panic!(
"Error parsing TOML file {}: {}",
path.display(),
e
)
}))
}
Some("json") => {
Some(serde_json::from_str(contents).unwrap_or_else(|e| {
panic!(
"Error parsing JSON file {}: {}",
path.display(),
e
)
}))
}
// > YAML currently does not support zero-copy deserialization
// Some("yaml") => {
// Some(serde_yaml::from_str(contents).unwrap_or_else(|e| {
// panic!(
// "Error parsing YAML file {}: {}",
// path.display(),
// e
// )
// }))
// }
_ => None,
}
})
.collect()
}
pub fn parse_toml_dir<'a, T>(d: Dir<'a>) -> Vec<T> pub fn parse_toml_dir<'a, T>(d: Dir<'a>) -> Vec<T>
where where
T: de::Deserialize<'a>, T: de::Deserialize<'a>,
{ {
d.files() d.files()
.iter() .iter()
.map(|f| toml::from_str(f.contents_utf8().unwrap()).unwrap()) .map(|f| {
toml::from_str(f.contents_utf8().unwrap()).unwrap_or_else(|e| {
panic!("Error parsing TOML file {}: {}", f.path, e)
})
})
.collect() .collect()
} }