11import json
2+ import typing
23from tale import parse_utils
34from tale .base import Item , Living , Location
45from tale .coord import Coord
@@ -10,10 +11,7 @@ class DynamicStory(StoryBase):
1011
1112 def __init__ (self ) -> None :
1213 self ._zones = dict () # type: dict[str, Zone]
13- self ._world = dict () # type: dict[str, any]
14- self ._world ["creatures" ] = dict ()
15- self ._world ["items" ] = dict ()
16- self ._world ["locations" ] = dict () # type: dict[Coord, Location]
14+ self ._world = WorldInfo ()
1715
1816 def get_zone (self , name : str ) -> Zone :
1917 """ Find a zone by name."""
@@ -48,7 +46,9 @@ def add_location(self, location: Location, zone: str = '') -> bool:
4846 """ Add a location to the story.
4947 If zone is specified, add to that zone, otherwise add to first zone.
5048 """
51- self ._world ["locations" ][location .world_location ] = location
49+ self ._world ._locations [location .name ] = location
50+ coord = location .world_location
51+ self ._world ._grid [coord .as_tuple ()] = location
5252 if zone :
5353 return self ._zones [zone ].add_location (location )
5454 for zone in self ._zones :
@@ -68,44 +68,77 @@ def zone_info(self, zone_name: str = '', location: str = '') -> dict():
6868 return zone .get_info ()
6969
7070 def get_npc (self , npc : str ) -> Living :
71- return self ._world [ " creatures" ] [npc ]
71+ return self ._world . creatures [npc ]
7272
7373 def get_item (self , item : str ) -> Item :
74- return self ._world ["items" ][item ]
74+ return self ._world .items [item ]
75+
76+ @property
77+ def locations (self ) -> dict :
78+ return self ._world ._locations
79+
80+ @property
81+ def world (self ) -> 'WorldInfo' :
82+ return self ._world
7583
7684
7785 def neighbors_for_location (self , location : Location ) -> dict :
7886 """ Return a dict of neighboring locations for a given location."""
7987 neighbors = dict () # type: dict[str, Location]
8088 for dir in ['north' , 'east' , 'south' , 'west' , 'up' , 'down' ]:
81- neighbors [dir ] = self ._world [ "locations" ][ Coord (location .world_location .add (parse_utils .coordinates_from_direction (dir )))]
89+ neighbors [dir ] = self ._world . _grid [ Coord (location .world_location .add (parse_utils .coordinates_from_direction (dir ))). as_tuple ( )]
8290 return neighbors
8391
84- @property
85- def world_creatures (self ) -> dict :
86- return self ._world ["creatures" ]
87-
88- @world_creatures .setter
89- def world_creatures (self , value : dict ):
90- self ._world ["creatures" ] = value
91-
92- @property
93- def world_items (self ) -> dict :
94- return self ._world ["items" ]
95-
96- @world_items .setter
97- def world_items (self , value : dict ):
98- self ._world ["items" ] = value
99-
10092 def save (self ) -> None :
10193 """ Save the story to disk."""
10294 story = dict ()
95+ story ["story" ] = dict ()
96+ story ["story" ]["name" ] = self .config .name
97+
10398 story ["zones" ] = dict ()
104- story ["world" ] = self ._world
99+ story ["world" ] = self ._world . to_json ()
105100 for zone in self ._zones .values ():
106101 story ["zones" ][zone .name ] = zone .get_info ()
102+ story ["zones" ][zone .name ]["name" ] = zone .name
103+ story ["zones" ][zone .name ]["locations" ] = parse_utils .save_locations (zone .locations .values ())
104+ print (story )
105+ with open ('world.json' , "w" ) as fp :
106+ json .dump (story , fp , indent = 4 )
107107
108- with open (self . config . name , "w" ) as fp :
109- json .dump (story , fp )
108+ with open ('story_config.json' , "w" ) as fp :
109+ json .dump (parse_utils . save_story_config ( self . config ) , fp , indent = 4 )
110110
111111
112+ class WorldInfo ():
113+
114+ def __init__ (self ) -> None :
115+ self ._creatures = dict ()
116+ self ._items = dict ()
117+ self ._locations = dict () # type: dict[str, Location]
118+ self ._grid = dict () # type: dict[Coord, Location]
119+
120+ @property
121+ def creatures (self ) -> dict :
122+ return self ._creatures
123+
124+ @creatures .setter
125+ def creatures (self , value : dict ):
126+ self ._creatures = value
127+
128+ @property
129+ def items (self ) -> dict :
130+ return self ._items
131+
132+ @items .setter
133+ def items (self , value : dict ):
134+ self ._items = value
135+
136+ def get_npc (self , npc : str ) -> Living :
137+ return self ._creatures [npc ]
138+
139+ def get_item (self , item : str ) -> Item :
140+ return self ._items [item ]
141+
142+ def to_json (self ) -> dict :
143+ return dict (creatures = parse_utils .save_creatures (self ._creatures .values ()),
144+ items = parse_utils .save_items (self ._items .values ()))
0 commit comments