def refreshCommandList(self): """ Updates cmdMap to contain all commands player can type """ # locate the rooms and objects configs loader = AssetLoader() area = loader.getConfig(Globals.AreasConfigPath)[self.areaId] items = loader.getConfig(Globals.ItemsConfigPath) rooms = loader.getConfig(area['roomsConfig']) objects = loader.getConfig(area['objectsConfig']) # locate the relevant scripts cmdMap = {} roomScript = loader.getScript(rooms[self.roomId]['script']) objectNames = [] objectScripts = [] for obj in rooms[self.roomId]['objects']: objectNames.append(objects[obj]['name']) objectScripts.append(loader.getScript(objects[obj]['script'])) # fill out the mapping, starting with 'go to' cmdMap['go to'] = {} for neighbor in rooms[self.roomId]['neighbors']: neighborName = rooms[neighbor]['name'] neighborScript = loader.getScript(rooms[neighbor]['script']) neighborReaction = None for action in neighborScript: if action[0] == 'go to': neighborReaction = action[1] cmdMap['go to'][neighborName] = neighborReaction # go through actions to take on room for action in roomScript: # ignore 'go to' current room (not possible) if action[0] == 'go to': continue cmdMap[action[0]] = action[1] # prefill 'use <item>' cmdMap['use'] = {} inventory = self.inventory for item in inventory: if int(inventory[item]) == 0: continue itemName = items[item]['name'] cmdMap['use'][itemName] = None # go through actions to take on objects for i in range(len(objectScripts)): objScript = objectScripts[i] objName = objectNames[i] for action in objScript: # "use .. on" needs special treatment for inventory items verbWords = action[0].split() if verbWords[0] == 'use' and verbWords[-1] == 'on': itemWord = ' '.join(verbWords[1:-1]) itemKey = loader.reverseItemLookup(itemWord) targetPhrase = 'on ' + objName if itemKey == '': raise Exception( 'script item name', itemWord, 'not found in items configuration file') if itemKey in inventory and int( inventory[itemKey]) > 0: if cmdMap['use'][itemWord] is None: cmdMap['use'][itemWord] = {} cmdMap['use'][itemWord][targetPhrase] = action[1] # all other verbs are straightforward else: if action[0] not in cmdMap: cmdMap[action[0]] = {} cmdMap[action[0]][objName] = action[1] self.cmdMap = cmdMap
def refreshCommandList(self): """ Updates GameState cmdMap to contain all commands player can type """ # Helper method to see if rooms or objects are visible in game def isVisible(self, obj): # if parameter is part of config possibly containing showIf if isinstance(obj, dict): if 'showIf' in obj: conditionVal = obj['showIf'] return self.evaluateConditionTree(conditionVal) # if parameter is tuple of (verb, reaction, condition) if isinstance(obj, tuple): if obj[0].startswith('_'): # verbs starting with underscore are not publicly visible return False if obj[2] is not None: # evaluate the condition return self.evaluateCondition(obj[2].split('_')) return True # visible by default # locate the rooms and objects configs loader = AssetLoader() gs = GameState() area = loader.getConfig(Globals.AreasConfigPath)[gs.areaId] items = loader.getConfig(Globals.ItemsConfigPath) rooms = loader.getConfig(area['roomsConfig']) objects = loader.getConfig(area['objectsConfig']) # locate the relevant scripts cmdMap = {} roomScript = loader.getScript(rooms[gs.roomId]['script']) objectNames = [] objectScripts = [] for obj in rooms[gs.roomId]['objects']: if not isVisible(self, objects[obj]): continue # do not show the object if conditions not met objectNames.append(objects[obj]['name']) objectScripts.append(loader.getScript(objects[obj]['script'])) # fill out the mapping, starting with 'go to' cmdMap['go to'] = {} for neighbor in rooms[gs.roomId]['neighbors']: if not isVisible(self, rooms[neighbor]): continue # do not show the room if conditions not met neighborName = rooms[neighbor]['name'] neighborScript = loader.getScript(rooms[neighbor]['script']) neighborReaction = None for action in neighborScript: if not isVisible(self, action): continue if action[0] == 'go to': neighborReaction = action[1] if neighborReaction: cmdMap['go to'][neighborName] = neighborReaction # go through actions to take on room for action in roomScript: # ignore 'go to' current room (not possible) if action[0] == 'go to' or not isVisible(self, action): continue cmdMap[action[0]] = action[1] # prefill 'use <item>', 'give <item>', 'show <item>' itemActionPhrases = [('use', 'on'), ('give', 'to'), ('show', 'to')] # go through actions to take on objects for i in range(len(objectScripts)): objScript = objectScripts[i] objName = objectNames[i] for action in objScript: if not isVisible(self, action): continue verbWords = action[0].split() # "use .. on", "give ... to", "show ... to" needs special treatment for inventory items for prefix, suffix in itemActionPhrases: if verbWords[0] == prefix and verbWords[-1] == suffix: itemWord = ' '.join(verbWords[1:-1]) itemKey = loader.reverseItemLookup(itemWord) targetPhrase = suffix + ' ' + objName if itemKey == '': raise Exception('script item name', itemWord ,'not found in items configuration file') if itemKey in gs.inventory and int(gs.inventory[itemKey]) > 0: if prefix not in cmdMap: cmdMap[prefix] = {} if itemWord not in cmdMap[prefix]: cmdMap[prefix][itemWord] = {} cmdMap[prefix][itemWord][targetPhrase] = action[1] break # all other verbs are straightforward else: if action[0] not in cmdMap: cmdMap[action[0]] = {} cmdMap[action[0]][objName] = action[1] gs.cmdMap = cmdMap