44from tale .base import Location , Exit , Item , Stats , Weapon , Wearable
55from tale .coord import Coord
66from tale .items .basic import Boxlike , Drink , Food , Health , Money , Note
7+ from tale .llm .LivingNpc import LivingNpc
78from tale .npc_defs import StationaryMob , StationaryNpc
89from tale .races import UnarmedAttack
910from tale .story import GameMode , MoneyType , TickMethod , StoryConfig
1314import json
1415import re
1516import sys
17+ import os
18+
1619
1720def load_json (file_path : str ):
21+ """
22+ Loads json from supplied file path
23+ Returns dict
24+ Fails silently and returns an empty dict if file doesn't exist
25+ """
26+ if not os .path .isfile (file_path ):
27+ return {}
28+
1829 with open (file_path ) as f :
1930 return json .load (f , strict = False )
2031
@@ -125,7 +136,7 @@ def load_npcs(json_npcs: [], locations = {}) -> dict:
125136 Inserts into locations if supplied and has location
126137 """
127138 npcs = {}
128- for npc in json_npcs :
139+ for npc in json_npcs : # type dict
129140 npc_type = npc .get ('type' , 'Mob' )
130141 if npc_type == 'ignore' :
131142 continue
@@ -160,12 +171,13 @@ def load_npcs(json_npcs: [], locations = {}) -> dict:
160171 new_npc .stats .level = npc .get ('level' , 1 )
161172 if npc .get ('stats' , None ):
162173 new_npc .stats = load_stats (npc ['stats' ])
163- # else:
164- # module = sys.modules['tale.items.basic']
165- # clazz = getattr(module, npc_type)
166- # new_npc = clazz(name=npc['name'], gender=npc['gender'], race=npc['race'], title=npc['title'], descr=npc['descr'], short_descr=npc['short_descr'], args=npc)
174+
167175 if locations and npc ['location' ]:
168176 _insert (new_npc , locations , npc ['location' ])
177+
178+ if npc .get ('memory' , None ):
179+ new_npc .load_memory (npc ['memory' ])
180+
169181 npcs [name ] = new_npc
170182 return npcs
171183
@@ -325,13 +337,11 @@ def trim_response(message: str):
325337
326338def sanitize_json (result : str ):
327339 """ Removes special chars from json string. Some common, and some 'creative' ones. """
328- # .replace('}}', '}')
329- # .replace('""', '"')
330340 if result is None :
331341 return ''
332342 result = result .replace ('```json' , '' ) #.replace('\\"', '"').replace('"\\n"', '","').replace('\\n', '').replace('}\n{', '},{').replace('}{', '},{').replace('\\r', '').replace('\\t', '').replace('"{', '{').replace('}"', '}').replace('"\\', '"').replace('\\”', '"').replace('" "', '","').replace(':,',':').replace('},]', '}]').replace('},}', '}}')
333343 result = result .split ('```' )[0 ]
334- print ('sanitized json: ' + result )
344+ # print('sanitized json: ' + result)
335345 return result
336346
337347def _convert_name (name : str ):
@@ -561,6 +571,9 @@ def save_npcs(creatures: []) -> dict:
561571 stored_npc ['aliases' ] = list (npc .aliases )
562572 stored_npc ['short_descr' ] = npc .short_description
563573 stored_npc ['descr' ] = npc .description
574+ stored_npc ['personality' ] = npc .personality
575+ stored_npc ['occupation' ] = npc .occupation
576+ stored_npc ['age' ] = npc .age
564577
565578 if isinstance (npc , StationaryMob ):
566579 stored_npc ['type' ] = 'Npc'
@@ -575,6 +588,9 @@ def save_npcs(creatures: []) -> dict:
575588 stored_npc ['short_descr' ] = npc .short_description
576589 stored_npc ['level' ] = npc .stats .level
577590 stored_npc ['stats' ] = save_stats (npc .stats )
591+
592+ if isinstance (npc , LivingNpc ):
593+ stored_npc ['memory' ] = npc .dump_memory ()
578594
579595 npcs [npc .name ] = stored_npc
580596 return npcs
0 commit comments