@@ -6,6 +6,11 @@ import Text.Printf
6import Graphics.Vty
6import Graphics.Vty
7import Graphics.Vty.Inline
7import Graphics.Vty.Inline
9+import Control.Monad.Trans.State.Strict
11+import Data.Map.Strict (Map)
12+import qualified Data.Map.Strict as Map
9-- Characters are identified by Names
14-- Characters are identified by Names
10type Character = Name
15type Character = Name
11type Name = String
16type Name = String
@@ -28,7 +33,7 @@ class Story repr where
28 -- Aspects are animate objects, inanimate objects or histories.
33 -- Aspects are animate objects, inanimate objects or histories.
29 animate_obj :: Type -> repr Object
34 animate_obj :: Type -> repr Object
30 inanimate_obj :: Type -> repr Object
35 inanimate_obj :: Type -> repr Object
31− history :: Type -> repr History
36+ history :: repr History
33 -- The goal is to acquire, or dispose of an object.
38 -- The goal is to acquire, or dispose of an object.
34 dispose_obj :: repr Object -> repr Goal
39 dispose_obj :: repr Object -> repr Goal
@@ -61,7 +66,7 @@ instance Story X where
61 add_unknown_obj obj as =
66 add_unknown_obj obj as =
62 animate_obj t =
67 animate_obj t =
63 inanimate_obj t =
68 inanimate_obj t =
64− history t =
69+ history =
65 dispose_obj obj =
70 dispose_obj obj =
66 acquire_obj obj =
71 acquire_obj obj =
67 named obj n =
72 named obj n =
@@ -78,7 +83,7 @@ instance Story ShowStory where
78 add_unknown_obj obj as = ShowStory $ printf "Unknown(%s):%s" (unShowStory obj) (unShowStory as)
83 add_unknown_obj obj as = ShowStory $ printf "Unknown(%s):%s" (unShowStory obj) (unShowStory as)
79 animate_obj t = ShowStory $ printf "%s :: AnimateObj" t
84 animate_obj t = ShowStory $ printf "%s :: AnimateObj" t
80 inanimate_obj t = ShowStory $ printf "%s :: InanimateObj" t
85 inanimate_obj t = ShowStory $ printf "%s :: InanimateObj" t
81− history t = ShowStory $ printf "%s :: History" t
86+ history = ShowStory $ printf "History"
82 dispose_obj obj = ShowStory $ printf "Goal(%s)" (unShowStory obj)
87 dispose_obj obj = ShowStory $ printf "Goal(%s)" (unShowStory obj)
83 acquire_obj obj = ShowStory $ printf "Acquire(%s)" (unShowStory obj)
88 acquire_obj obj = ShowStory $ printf "Acquire(%s)" (unShowStory obj)
84 named obj n = ShowStory $ printf "%s+Name(%s)" (unShowStory obj) (show n)
89 named obj n = ShowStory $ printf "%s+Name(%s)" (unShowStory obj) (show n)
@@ -99,7 +104,7 @@ instance Story TellStory where
99 add_unknown_obj _ as = as
104 add_unknown_obj _ as = as
100 animate_obj t = TellStory t
105 animate_obj t = TellStory t
101 inanimate_obj t = TellStory t
106 inanimate_obj t = TellStory t
102− history t = TellStory t
107+ history = TellStory "History"
103 dispose_obj (TellStory obj)
108 dispose_obj (TellStory obj)
104 = TellStory $ "to dispose of the accursed " ++ obj
109 = TellStory $ "to dispose of the accursed " ++ obj
105 acquire_obj (TellStory obj)
110 acquire_obj (TellStory obj)
@@ -107,26 +112,43 @@ instance Story TellStory where
107 named (TellStory obj) name
112 named (TellStory obj) name
108 = TellStory $ obj ++ " with the name " ++ name
113 = TellStory $ obj ++ " with the name " ++ name
110−newtype PictureStory t = PictureStory { compose_to_picture :: Picture }
115+newtype BuildPicture t = BuildPicture {build_picture :: Picture -> State PictureCtx Picture}
112−{-
117+type Sprite = (Int, Int, Image)
118+data PictureCtx = PictureCtx
119+ { next_ID :: Int -- globally monotonic
121+ , known_sprites :: Map Int Sprite
124+get_ID :: State PictureCtx Int
125+get_ID = gets this_ID
127+new_ID :: Picture -> State PictureCtx Picture
130+ let the_ID = next_ID ctx
131+ put $ ctx {next_ID = the_ID + 1, this_ID = the_ID}
134+empty_picture_ctx = PictureCtx 0 1 Map.empty
113−instance Story X where
136+instance Story BuildPicture where
114− story c q =
137+ story _c (BuildPicture q) = BuildPicture q
115− quest g as =
138+ quest _g (BuildPicture as) = BuildPicture as
116− empty_aspects =
139+ empty_aspects = BuildPicture new_ID
117− add_known_history h as =
140+ add_known_history h (BuildPicture as) = BuildPicture as
118− add_known_obj obj as =
141+ add_known_obj obj (BuildPicture as) = BuildPicture as
119− add_unknown_history h as =
142+ add_unknown_history h (BuildPicture as) = BuildPicture as
120− add_unknown_obj obj as =
143+ add_unknown_obj obj (BuildPicture as) = BuildPicture as
121− animate_obj t =
144+ animate_obj t = BuildPicture new_ID
122− inanimate_obj t =
145+ inanimate_obj t = BuildPicture new_ID
123− history t =
146+ history = BuildPicture new_ID
124− dispose_obj obj =
147+ dispose_obj (BuildPicture obj) = BuildPicture obj
125− acquire_obj obj =
148+ acquire_obj (BuildPicture obj) = BuildPicture obj
126− named obj n =
149+ named (BuildPicture obj) n = BuildPicture obj
130 putStrLn $ unTellStory story_0
152 putStrLn $ unTellStory story_0
131 putStrLn $ unShowStory story_0
153 putStrLn $ unShowStory story_0
132− withVty $ flip update (compose_to_picture story_0)
154+ withVty $ flip update $ evalState (build_picture story_0 empty_picture) empty_picture_ctx