def move(command): # Find the direction part of the command direction = "" for word in command: if word in directionCommands: direction = word break # Get the direction's shortcode for shortcode in directionMap.keys(): if direction in directionMap[shortcode]: directionShortcode = shortcode break # Check to see if there is a room in that direction from the player's room destination = "" for connection in room.list_connections( infoutil.fetch('state', 'player', '0')['location']): if directionShortcode in connection[2]: destination = connection[0] direction = connection[3] break if destination == "": return u"There is no room in that direction." # Fail state else: # Move the player infoutil.update_state('player', '0', 'location', destination) return u"You move " + direction + "..." + room.describe(destination)
def list_connections(roomID): # Returns (roomID, room name, direction shortcode, direction longcode) for connections in room connections = [] for connection in infoutil.fetch('info', 'room', roomID)['connections']: connections.append( (connection['room'], infoutil.name('room', connection['room']), connection['direction'], directionLongcodes[connection['direction']])) return connections
def look(command): # Find the thing to look at in the command lookTarget = "" for word in command: if word not in viewCommands + [u"at"]: lookTarget = word break if lookTarget == "": lookTarget = u"room" # Look inventory, inventory, i, look inv, etc. inventoryList = [] if lookTarget in invCommands: for itemID in list_inventory(): inventoryList.append( item.name(itemID)[0].upper() + item.name(itemID)[1:]) return u"You have:\n" + '\n'.join(inventoryList) # Look room if lookTarget == u"room": return room.describe( infoutil.fetch('state', 'player', '0')['location']) # Look [object] else: # check if [object] is an npc for npci in room.list_npcs( infoutil.fetch('state', 'player', '0')['location']): if lookTarget.lower() == npci[1]: return npc.describe(npci[0]) # check if [object] is an item # Get the names of held items invItems = [] for invItem in list_inventory(): invItems.append((invItem, infoutil.name('item', invItem))) # Look for the item in the command in a list of items in the room and held items for itemi in room.list_inventory( infoutil.fetch('state', 'player', '0')['location']) + invItems: if lookTarget.lower() == itemi[1] or lookTarget in item.synonyms( itemi[0]): return item.describe(itemi[0]) # if the item/npc isn't found return u"You can\'t see that here!"
def interact(command): # todo: find a good way to integrate this with item commands interactionTarget = "" for word in command: if word not in invManagementCommands + usageCommands + [ u"up", u"down" ]: interactionTarget = word break # Take/get [object] if command[0] in [u"take", u"get"]: for itemi in room.list_inventory( infoutil.fetch('state', 'player', '0')['location']): if interactionTarget.lower( ) == itemi[1] or interactionTarget in item.synonyms(itemi[0]): infoutil.remove_item( 'room', infoutil.fetch('state', 'player', '0')['location'], itemi[0]) # Remove the item from the room's inventory infoutil.add_item( 'player', '0', itemi[0]) # And add the item to the player's inventory return u"You take the %s." % interactionTarget # If the item isn't found return u"There is no %s within arm\'s reach." # Drop [object], put (down) [object], etc. elif command[0] in [u"drop", u"put", u"place"]: for itemi in list_inventory(): if interactionTarget.lower() == infoutil.name( 'item', itemi) or interactionTarget in item.synonyms(itemi): infoutil.remove_item( 'player', '0', itemi) # Remove the item from the player's inventory infoutil.add_item( 'room', infoutil.fetch('state', 'player', '0')['location'], itemi) # And add the item to the room's inventory return u"You drop the %s." % interactionTarget # If the item isn't found return u"There is no %s within arm\'s reach." % interactionTarget
def describe(roomID): ## Create and return a description of the roomID in argument # Get the room's name and description name = infoutil.name('room', roomID).upper() description = infoutil.fetch('info', 'room', roomID)['description'] # Get inventory, NPCs, and connections for the room inventory = list_inventory(roomID) npcs = list_npcs(roomID) connections = list_connections(roomID) # Format the room information as a string inventoryentence = u"" for count, item in enumerate(inventory): inventoryentence += item[1] if len(inventory) - 2 == count: inventoryentence += u", and " elif len(inventory) - 1 != count: inventoryentence += u", " elif len(inventory) - 1 == count: inventoryentence += u" are here." npcSentence = u"" for count, npc in enumerate(npcs): npcSentence += npc[1] if len(npcs) - 2 == count: npcSentence += u", and " elif len(npcs) - 1 != count: npcSentence += u", " if len(npcs) > 1 and len(npcs) - 1 == count: npcSentence += u" are here." elif len(npcs) == 1 and len(npcs) - 1 == count: npcSentence += u" is here." connectionSentence = u"" if len(connections) > 1: connectionSentence += u"There is " for count, connection in enumerate(connections): connectionSentence += u"a " + connection[1] + u" to the " + connection[ 3] if len(connections) - 2 == count: connectionSentence += u", and " elif len(connections) - 1 != count: connectionSentence += u", " elif len(connections) - 1 == count: connectionSentence += u"." directionList = u"You can go " for count, connection in enumerate(connections): directionList += connection[3] if len(connections) - 2 == count: directionList += u", and " elif len(connections) - 1 != count: directionList += u", " elif len(connections) - 1 == count: directionList += u"." passageList = [ description, inventoryentence, npcSentence, connectionSentence ] passageStr = u"" for sentence in passageList: if sentence != u"": passageStr += sentence[0].upper() + sentence[1:] + u" " return u"\n" + name + u"\n\n" + passageStr + u"\n\n" + directionList
def list_inventory(roomID): # Returns (itemID, item name) for item in room inventory = [] for itemID in infoutil.fetch('state', 'room', roomID)['inventory']: inventory.append((itemID, infoutil.name('item', itemID))) return inventory
import ioutil import infoutil import player import room import item import npc while True: ## Main game loop # Get the player's command command = raw_input("\n" + infoutil.name( 'room', infoutil.fetch('state', 'player', '0')['location']).upper() + " :: ").lower().split(' ') print player.do(command)
def list_inventory(): return infoutil.fetch('state', 'player', '0')['inventory']
def commands(itemID): return infoutil.fetch('info', 'item', itemID)['actions'] # todo: get started on item commands def state_value(itemID, state): pass
def synonyms(itemID): return infoutil.fetch('info', 'item', itemID)['synonyms'].split(', ') def commands(itemID): return infoutil.fetch('info', 'item', itemID)['actions']
def describe(itemID): return infoutil.fetch('info', 'item', itemID)['description'] def synonyms(itemID): return infoutil.fetch('info', 'item', itemID)['synonyms'].split(', ')
def name(itemID): return infoutil.fetch('info', 'item', itemID)['name'] def describe(itemID): return infoutil.fetch('info', 'item', itemID)['description']
def describe(npcID): return infoutil.fetch('info', 'npc', npcID)['description']
def name(npcID): return infoutil.fetch('info', 'npc', npcID)['name']