diff options
| -rw-r--r-- | examples/text-adventure/data.rs | 178 | ||||
| -rw-r--r-- | examples/text-adventure/dsl.rs | 144 | ||||
| -rw-r--r-- | examples/text-adventure/logic.rs | 13 | ||||
| -rw-r--r-- | examples/text-adventure/main.rs | 30 | ||||
| -rw-r--r-- | examples/text-adventure/side_effects.rs | 49 |
5 files changed, 265 insertions, 149 deletions
diff --git a/examples/text-adventure/data.rs b/examples/text-adventure/data.rs new file mode 100644 index 0000000..2c12bb0 --- /dev/null +++ b/examples/text-adventure/data.rs @@ -0,0 +1,178 @@ +#[derive(Clone, Copy)] +pub enum Speaker{ + Partner, + DeliLady, + Cashier, +} + +impl Speaker{ + pub fn text_description(self)->&'static str{ + match self{ + Speaker::Partner => "Your partner", + Speaker::DeliLady => "The lady behind the deli counter", + Speaker::Cashier => "The cashier", + } + } +} + +#[derive(Clone, Copy)] +pub enum Mood{ + Friendly, + Confused, + Happy, + Amused, + Annoyed, + Apologetic, +} + +impl Mood{ + pub fn text_description(self)->&'static str{ + match self{ + Mood::Friendly => "a friendly expression", + Mood::Confused => "a confused expression", + Mood::Happy => "a happy expression", + Mood::Amused => "an amused expression", + Mood::Annoyed => "an annoyed expression", + Mood::Apologetic => "an apologetic expression", + } + } +} + +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Location{ + Entrance, + Deli, + Checkout, + Refrigerators, + Shelves, +} + +impl Location{ + //Used if we want to present the location in text. If we were writing a visual novel, we would offer another function that returns the respective assets. + pub fn get_text_description(self)->&'static str{ + match self { + Location::Entrance => "You are at the entrance area of the super market. Behind you is the parking lot, in front the inviting automated doors of the entrance. Your partner is here with you.", + Location::Deli => "This is the area with the deli counter. There is a lady wearing a hair protector and plastic gloves standing behind the presentation tray.", + Location::Checkout => "You have reached the checkout area of the super market. Stands full of sweets and other stuff that might attract the attention of people waiting to pay dominate this area. There is an employee sitting at one of the counters.", + Location::Refrigerators => "This is the are where fresh products are waiting to be picked up. Refrigerators with milk, cheese and similar stuff are lined along the wall.", + Location::Shelves => "This is the main area of the super market. Here you find several shelves filled with more or less useful stuff, ranging from conserved vegetables to cleaning utensils.", + } + } +} + +#[derive(Copy,Clone,PartialEq,Eq)] +pub enum Item{ + //refrigerators + Milk, + Yoghurt, + Cheese, + //shelves + Pickles, + CatFood, + Beer, + ToiletPaper, + //deli + SausageRoll, + FishSandwich, + //cashier + ChewingGum, + Shots, + Pulp, +} + +impl Item { + fn price(self) -> usize { + match self { + Item::SausageRoll => 300, + Item::Pickles => 250, + Item::Milk => 125, + Item::Yoghurt => 125, + Item::Cheese => 750, + Item::CatFood => 2500, + Item::Beer => 125, + Item::ToiletPaper => 500, + Item::FishSandwich => 300, + Item::ChewingGum => 100, + Item::Shots => 300, + Item::Pulp => 250, + + } + } + pub fn description(self) -> &'static str { + match self { + Item::SausageRoll => "A sausage roll, costing €3.00.", + Item::Pickles => "A glass of pickles, costing €2.50", + Item::Milk => "A bottle of milk, costing €1.25", + Item::Yoghurt => "A cup of yoghurt, costing €1.25", + Item::Cheese => "A block of expensive grey cheese, costing €7.50", + Item::CatFood => "A bag of cat food, costing €25.00", + Item::Beer => "A bottle of beer, for €1.25", + Item::ToiletPaper => "A package of toilet paper, costing €5.00", + Item::FishSandwich => "A fish sandwich, emitting a tasty smell, costing €3.00", + Item::ChewingGum => "A pack of chewing gum, costing €1.00", + Item::Shots => "A shot of a sad excuse for whisky, costing €3.00", + Item::Pulp => "A pulp novel called \"Aliens ate my trashbin\", which should not cost the €2.50 it does", + + } + } +} + +impl Location{ + pub fn items(self) -> Vec<Item> { + match self { + Location::Entrance => Vec::default(), + Location::Deli => vec![], //must talk to deli lady to get the sausage roll. This also means it cannot be returned. + Location::Checkout => vec![Item::ChewingGum, Item::Shots, Item::Pulp], + Location::Refrigerators => vec![Item::Milk, Item::Yoghurt, Item::Cheese], + Location::Shelves => vec![Item::Pickles, Item::CatFood, Item::Beer, Item::ToiletPaper], + } + } +} + +//In a real project I would probably aim to make this Copy as well, especially if it's as small as this. +//I left it as Clone intentionally, to illustrate how one can work around the limitation of it not being Copy. +#[derive(Clone)] +pub struct Inventory { + pub items : Vec<Item>, +} + +impl Default for Inventory{ + fn default() -> Self { + Self { items: Default::default() } + } +} + +impl Inventory{ + pub fn has_item_from_room(&self, room : Location) -> bool { + let items_from_room = room.items(); + self.items.iter().any(|i| items_from_room.contains(i)) + } + pub fn try_add(self, item : Item) -> Result<Self, Self>{ + if self.items.len() < 3 { + let mut items = self.items; + items.push(item); //am I the only one that hates that push doesn't return the updated vec? + Ok(Inventory{items, ..self}) + } else { + Err(self) + } + } + pub fn try_remove(mut self, item : Item) -> Result<Self, Self>{ + let idx = self.items.iter().position(|i| *i == item); + match idx { + Some(idx) => { + self.items.swap_remove(idx); + Ok(self) + }, + None => Err(self), + } + } + pub fn get_money() -> &'static str{ + "€10.00" //It doesn't change. Can as well be a constant. + } + pub fn total_price(&self) -> usize { + self.items.iter().cloned().map(Item::price).sum::<usize>() + } + pub fn can_afford(&self) -> bool { + self.total_price() <= 1000_usize + } +}
\ No newline at end of file diff --git a/examples/text-adventure/dsl.rs b/examples/text-adventure/dsl.rs index 394a5c6..44bc978 100644 --- a/examples/text-adventure/dsl.rs +++ b/examples/text-adventure/dsl.rs @@ -5,32 +5,7 @@ use std::{rc::Rc, borrow::Cow}; use higher_free_macro::free; use std::convert::identity; use higher::Functor; - -#[derive(Clone)] -pub enum Speaker{ - Partner, - DeliLady, - Cashier, -} - -#[derive(Clone)] -pub enum Mood{ - Friendly, - Confused, - Happy, - Amused, - Annoyed, - Apologetic, -} - -#[derive(Clone, Copy, PartialEq, Eq)] -pub enum Location{ - Entrance, - Deli, - Checkout, - Refrigerators, - Shelves, -} +use super::data::*; #[derive(Clone)] pub enum SausageRoll<'a, 's,A>{ @@ -53,122 +28,6 @@ pub enum SausageRoll<'a, 's,A>{ next : A, } } -#[derive(Copy,Clone,PartialEq,Eq)] -pub enum Item{ - //refrigerators - Milk, - Yoghurt, - Cheese, - //shelves - Pickles, - CatFood, - Beer, - ToiletPaper, - //deli - SausageRoll, - FishSandwich, - //cashier - ChewingGum, - Shots, - Pulp, -} - -impl Item { - fn price(self) -> usize { - match self { - Item::SausageRoll => 300, - Item::Pickles => 250, - Item::Milk => 125, - Item::Yoghurt => 125, - Item::Cheese => 750, - Item::CatFood => 2500, - Item::Beer => 125, - Item::ToiletPaper => 500, - Item::FishSandwich => 300, - Item::ChewingGum => 100, - Item::Shots => 300, - Item::Pulp => 250, - - } - } - pub fn description(self) -> &'static str { - match self { - Item::SausageRoll => "A sausage roll, costing €3.00.", - Item::Pickles => "A glass of pickles, costing €2.50", - Item::Milk => "A bottle of milk, costing €1.25", - Item::Yoghurt => "A cup of yoghurt, costing €1.25", - Item::Cheese => "A block of expensive grey cheese, costing €7.50", - Item::CatFood => "A bag of cat food, costing €25.00", - Item::Beer => "A bottle of beer, for €1.25", - Item::ToiletPaper => "A package of toilet paper, costing €5.00", - Item::FishSandwich => "A fish sandwich, emitting a tasty smell, costing €3.00", - Item::ChewingGum => "A pack of chewing gum, costing €1.00", - Item::Shots => "A shot of a sad excuse for whisky, costing €3.00", - Item::Pulp => "A pulp novel called \"Aliens ate my trashbin\", which should not cost the €2.50 it does", - - } - } -} - -impl Location{ - pub fn items(self) -> Vec<Item> { - match self { - Location::Entrance => Vec::default(), - Location::Deli => vec![], //must talk to deli lady to get the sausage roll. This also means it cannot be returned. - Location::Checkout => vec![Item::ChewingGum, Item::Shots, Item::Pulp], - Location::Refrigerators => vec![Item::Milk, Item::Yoghurt, Item::Cheese], - Location::Shelves => vec![Item::Pickles, Item::CatFood, Item::Beer, Item::ToiletPaper], - } - } -} - -//In a real project I would probably aim to make this Copy as well, especially if it's as small as this. -//I left it as Clone intentionally, to illustrate how one can work around the limitation of it not being Copy. -#[derive(Clone)] -pub struct Inventory { - pub items : Vec<Item>, -} - -impl Default for Inventory{ - fn default() -> Self { - Self { items: Default::default() } - } -} - -impl Inventory{ - pub fn has_item_from_room(&self, room : Location) -> bool { - let items_from_room = room.items(); - self.items.iter().any(|i| items_from_room.contains(i)) - } - pub fn try_add(self, item : Item) -> Result<Self, Self>{ - if self.items.len() < 3 { - let mut items = self.items; - items.push(item); //am I the only one that hates that push doesn't return the updated vec? - Ok(Inventory{items, ..self}) - } else { - Err(self) - } - } - pub fn try_remove(mut self, item : Item) -> Result<Self, Self>{ - let idx = self.items.iter().position(|i| *i == item); - match idx { - Some(idx) => { - self.items.swap_remove(idx); - Ok(self) - }, - None => Err(self), - } - } - pub fn get_money() -> &'static str{ - "€10.00" //It doesn't change. Can as well be a constant. - } - pub fn total_price(&self) -> usize { - self.items.iter().cloned().map(Item::price).sum::<usize>() - } - pub fn can_afford(&self) -> bool { - self.total_price() <= 1000_usize - } -} impl<'a,'s, A : 'a> Functor<'a,A> for SausageRoll<'a,'s,A>{ type Target<T> = SausageRoll<'a,'s,T>; @@ -187,7 +46,6 @@ impl<'a,'s, A : 'a> Functor<'a,A> for SausageRoll<'a,'s,A>{ free!(<'a>, pub FreeSausageRoll<'a,'s,A>, SausageRoll<'a, 's, FreeSausageRoll<'a,'s,A>>); - pub fn say_dialogue_line<'a,'s:'a>(speaker : Speaker, text : Cow<'s,str>, mood : Mood) -> FreeSausageRoll<'a, 's, ()>{ FreeSausageRoll::lift_f(SausageRoll::SayDialogueLine { speaker, text, mood, next: () }) } diff --git a/examples/text-adventure/logic.rs b/examples/text-adventure/logic.rs index 5df5f6e..dc3ff45 100644 --- a/examples/text-adventure/logic.rs +++ b/examples/text-adventure/logic.rs @@ -4,6 +4,7 @@ use std::borrow::Cow; use higher::{run, Functor, Pure, Bind}; +use super::data::*; use super::dsl::*; //Haskell has a when function, and it's nice. Sooo, copy that. @@ -77,6 +78,7 @@ fn handle_rooms<'a,'s:'a>(room: Location, inventory : Inventory) -> FreeSausageR run!{ c <= handle_room(room, inventory); if c.0 != Location::Entrance { + //If this were an actual game, we could put a save-point here. At this location the next room to handle is just determined by room and inventory. handle_rooms(c.0, c.1) } else { run!{ @@ -151,7 +153,7 @@ fn handle_shelves<'a, 's: 'a>(inventory : Inventory) -> FreeSausageRoll<'a, 's, let inventory = inventory.clone(); run!{ check_inventory(inventory.clone()); - handle_refrigerators(inventory.clone()) + handle_shelves(inventory.clone()) } } 4 => { run!{ @@ -177,7 +179,7 @@ fn handle_deli<'a,'s:'a>(inventory : Inventory) -> FreeSausageRoll<'a, 's, (Loca let inventory = inventory.clone(); run!{ check_inventory(inventory.clone()); - handle_refrigerators(inventory.clone()) + handle_deli(inventory.clone()) } }, 3 => { @@ -207,7 +209,12 @@ fn handle_checkout<'a,'s:'a>(inventory : Inventory) -> FreeSausageRoll<'a, 's, ( run!{ r <= try_pay(inventory.clone()); match r { - Ok(inventory) => FreeSausageRoll::pure((Location::Entrance, inventory.clone())), + Ok(inventory) => { + run!{ + exposition("You leave the supermarket. Your partner is already waiting outside."); + FreeSausageRoll::pure((Location::Entrance, inventory.clone())) + } + }, Err(inventory) => handle_checkout(inventory.clone()), } } diff --git a/examples/text-adventure/main.rs b/examples/text-adventure/main.rs index 788c3d5..c98441b 100644 --- a/examples/text-adventure/main.rs +++ b/examples/text-adventure/main.rs @@ -1,11 +1,37 @@ //! A small example text adventure, the logic of which is implemented as a Free Monad based eDSL. //! -//! The goal of this game is to buy a sausage roll. +//! The goal of this game is to buy a sausage roll. With pickle. +//! +//! The code of this example contains a few peculiarities, to highlight features of and issues with the current +//! Free Monad code. +//! For instance, it intentionally does not have `Copy` implemented on the player's inventory, to illustrate how +//! one can work around a limitation in the current run!{} macro version. +//! Another thing that is not really that useful in practice is that all strings that are hardcoded are references +//! instead of owned copies. This is just to illustrate that lifetimes are supported too. +//! +//! In a real project, I'd just make all game state (here: inventory) `Copy`, and use owned values wherever possible to make the code +//! more concise. If `Copy` is not an option, I'd probably make a custom version of `run!{}` that allows to clone the +//! game state in a convenient way (see https://github.com/bodil/higher/issues/6). +//! +//! But on to the explanation what is going on: +//! This project has 4 modules: +//! - "data" contains the data. Stuff like item types, item descriptions, rooms, etc. +//! - "dsl" contains the embedded domain specific language. In other words, a Functor and the corresponding Free Monad type (and some helpers) +//! - "logic" describes the game's main logic using the language defined in "dsl" +//! - "side_effects" actually runs the logic. +//! +//! The important part here is that all the stuff that isn't in "side_effects" is independent of the concrete implementation of "side_effects". +//! The current "side_effects" runs a text-adventure, but it could just as well render as a visual-novel, without the need to touch any of the other modules. +mod data; mod dsl; mod logic; mod side_effects; -fn main() { +fn main() -> std::io::Result<()> { + //Let's build the game logic. As a data structure. let game = logic::game(); + + //And now let's do something with it. + side_effects::run(game) }
\ No newline at end of file diff --git a/examples/text-adventure/side_effects.rs b/examples/text-adventure/side_effects.rs index 51f1f4c..ed0f9e1 100644 --- a/examples/text-adventure/side_effects.rs +++ b/examples/text-adventure/side_effects.rs @@ -1 +1,48 @@ -//this module interprets the domain specific language.
\ No newline at end of file +//! this module interprets the domain specific language as a text adventure. + +use crate::dsl::FreeSausageRoll; + +pub fn run<'a, 's:'a>(mut game : FreeSausageRoll<'a, 's, ()>) -> std::io::Result<()>{ + //this function doesn't know who it is, or why it is here. It only knows it must deal. + //Deal with the few commands in the eDSL and nothing more. + + //This would be easier to write recursively. However, in an actual project this might run for quite some time. + //Since we operate on the stack, let's rather be safe than sorry, and use a loop instead of recursion therefore. + while let FreeSausageRoll::Free(command) = game { + game = match *command { + crate::dsl::SausageRoll::SayDialogueLine { speaker, text, mood, next } => { + println!("{} says: \"{}\" with {} on their face.", speaker.text_description(), text, mood.text_description()); + next + }, + crate::dsl::SausageRoll::GivePlayerOptions { options, next } => + { + println!("Your options are:"); + for (id, option) in options.iter().enumerate().map(|(i,o)| (i+1,o)){ + println!("{}: {}", id, option); + } + + let mut input = String::new(); + let mut chosen; + while let None = { + input.clear(); + std::io::stdin().read_line(&mut input)?; + chosen = input.trim().parse().ok().filter(|o : &usize| *o > 0 && *o <= options.len()); + chosen + } { + println!("Invalid choice. Please select one of the options given above."); + } + println!(); + next(chosen.unwrap()-1) + }, + crate::dsl::SausageRoll::PresentLocation { location, next } => { + println!("{}", location.get_text_description()); + next + }, + crate::dsl::SausageRoll::Exposition { text, next } => { + println!("{}", text); + next + }, + }; + }; + std::io::Result::Ok(()) +}
\ No newline at end of file |
