def unlock(self, actor, item=None): if not self.locked: raise ActionRefused("It is not locked.") if item: if self.check_key(item): key = item else: raise ActionRefused("You can't use that to unlock it.") else: key = self.search_key(actor) if not key: raise ActionRefused( "You don't seem to have the key necessary to unlock it.") self.locked = False actor.tell("You unlock the %s with %s." % (self.name, Lang.a(key.title))) actor.tell_others("{Title} unlocked the %s with %s." % (self.name, Lang.a(key.title))) if self.linked_door: self.linked_door.door.locked = False
def lock(self, actor, item=None): if self.locked: raise ActionRefused("It is already locked.") if item: if self.check_key(item): key = item else: raise ActionRefused("That can not be used to lock it.") else: key = self.search_key(actor) if not key: raise ActionRefused( "You don't seem to have the key necessary to lock it.") self.locked = True actor.tell("You lock the %s with %s." % (self.name, Lang.a(key.title))) actor.tell_others("{Title} locked the %s with %s." % (self.name, Lang.a(key.title))) if self.linked_door: self.linked_door.door.locked = True
def func(player, parsed, ctx): if len(parsed.obj_order) != 2: raise ParseError("What would you like to show and to whom?") shown = parsed.obj_order[0] if shown not in player: raise ActionRefused("You do not have <item>%s</item> to show." % Lang.a(shown.title)) target = parsed.obj_order[1] player.tell("You reveal the <item>%s</item> to <creature>%s</creature>." % (shown.title, target.title)) room_msg = "%s shows something to %s." % (Lang.capital(player.title), target.title) target_msg = "%s reveals the %s to you." % (Lang.capital(player.title), Lang.a(shown.title)) player.location.tell(room_msg, exclude_creature=player, specific_target_msg=target_msg, specific_targets=[target])
def give_all(player, items, target_name, target=None): if not target: target = player.location.search_creature(target_name) if not target: raise ActionRefused("%s isn't here with you." % target_name) if target is player: raise ActionRefused("Giving something to yourself doesn't make much sense.") # Actually try to give the items... items = list(items) refused = [] for item in items: try: item.move(target, player) except ActionRefused as x: refused.append((item, str(x))) # Let the player know why giving a particular item failed for item, message in refused: player.tell(message) items.remove(item) if items: items_str = Lang.join(Lang.a(item.title) for item in items) player_str = Lang.capital(player.title) room_msg = "<player>%s</player> gives <item>%s</item> to <creature>%s</creature>." % (player_str, items_str, target.title) target_msg = "<player>%s</player> gives you <item>%s</item>." % (player_str, items_str) player.location.tell(room_msg, exclude_creature=player, specific_targets=[target], specific_target_msg=target_msg) player.tell("You give <creature>%s</creature> <item>%s</item>." % (target.title, items_str)) else: player.tell("You weren't able to give <creature>%s</creature> anything." % target.title)
def func(player, parsed, ctx): if not parsed.args: raise ParseError( "What would you like to drop? You can also 'drop all' or 'drop everything'." ) def drop(items, container): items = list(items) refused = [] for item in items: try: item.move(player.location, player, verb="drop") if container is not player and container in player: Drop.notify_item_removal(player, item, container) except ActionRefused as x: refused.append((item, str(x))) for item, message in refused: items.remove(item) player.tell(message) if items: strItems = Lang.join(Lang.a(item.title) for item in items) player.tell("You discard <item>%s</item>." % strItems) player.tell_others("{Title} drops %s." % strItems) else: player.tell("Nothing was dropped.") arg = parsed.args[0] # drop all items? if arg == "all" or arg == "everything": drop(player.inventory, player) else: # drop a single item from inventory if parsed.obj_order: item = parsed.obj_order[0] if item in player: drop([item], player) else: raise ActionRefused("You can't seem to drop that!") # drop a container from inventory else: item, container = player.locate_item(arg, include_location=False) if item: if container is not player: Actions.print_object_location(player, item, container) drop([item], container) else: raise ActionRefused("You don't have <item>%s</item>." % Lang.a(arg))
def func(player, parsed, ctx): if len(parsed.args) not in (1, 2) or parsed.unrecognized: raise ParseError("What are you trying to %s?" % Lang.capital(parsed.verb)) if parsed.obj_order: if isinstance(parsed.obj_order[0], Creature): raise ActionRefused( "You can't do that to other living creatures.") obj_name = parsed.args[0] # Is the player using something to try to manipulate the object? with_item_name = None with_item = None if len(parsed.args) == 2: with_item_name = parsed.args[1] what = player.search_item(obj_name, include_inventory=True, include_location=True, include_containers=False) # Are we dealing with an exit? if not what: if obj_name in player.location.exits: what = player.location.exits[obj_name] # Are we dealing with an item? if what: # If so, are they using an item to accomplish the manipulation? if with_item_name: with_item = player.search_item(with_item_name, include_inventory=True, include_location=False, include_containers=False) if not with_item: raise ActionRefused( "You don't seem to have <item>%s</item>." % Lang.a(with_item_name)) getattr(what, parsed.verb)(player, with_item) else: raise ActionRefused("You don't see that here.")
def take_all(player, items, container, where_str=None): # No items were specified if not items: return 0 # Is player taking something from a container? if where_str: player_msg = "You take <item>{items}</item> from the <item>%s</item>" % where_str room_msg = "<player>{{Title}}</player> takes <item>{items}</item> from the <item>%s</item>" % where_str # If not, they must be taking it from the room else: player_msg = "You take <item>{items}</item>" room_msg = "<player>{{Title}}</player> takes <item>{items}</item>" items = list(items) refused = [] # Try to move items one by one. If there are special rules against taking them the item should # raise an ActionRefused exception. for item in items: try: item.move(player, player, verb="take") except ActionRefused as x: refused.append((item, str(x))) # Tell player if any items refused to budge for item, message in refused: player.tell(message) items.remove(item) # Tell player about any items that were moved into their inventory # and tell nearby players and NPCs abou it too! if items: items_str = Lang.join(Lang.a(item.title) for item in items) player.tell(player_msg.format(items=items_str)) player.tell_others(room_msg.format(items=items_str)) return len(items) else: return 0
def drop(items, container): items = list(items) refused = [] for item in items: try: item.move(player.location, player, verb="drop") if container is not player and container in player: Drop.notify_item_removal(player, item, container) except ActionRefused as x: refused.append((item, str(x))) for item, message in refused: items.remove(item) player.tell(message) if items: strItems = Lang.join(Lang.a(item.title) for item in items) player.tell("You discard <item>%s</item>." % strItems) player.tell_others("{Title} drops %s." % strItems) else: player.tell("Nothing was dropped.")
def func(player, parsed, ctx): if len(parsed.args) < 2: raise ParseError( "You need to tell me what to put and where you'd like to put it." ) # If player specified all they want to put their entire inventory into the container if parsed.args[0] == "all": # Does the player have anything in their inventory if player.inventory_size == 0: raise ActionRefused("You don't seem to be carrying anything.") if len(parsed.args) != 2: raise ParseError( "You need to tell me what to put and where you'd like to put it." ) what = list(player.inventory) where = parsed.obj_order[ -1] # The last item represents the "where" elif parsed.unrecognized: raise ActionRefused("I don't see %s here." % Lang.join(parsed.unrecognized)) else: what = parsed.obj_order[:-1] where = parsed.obj_order[-1] if isinstance(where, Creature): raise ActionRefused( "You can't do that but you might be able to give it to them..." ) inventory_items = [] refused = [] word_before = parsed.obj_info[where].previous_word or "in" if word_before != "in" and word_before != "into": raise ActionRefused( "You can only put an item 'in' or 'into' a container of some sort." ) for item in what: if item is where: player.tell("You can't put something inside of itself.") continue try: # Are they using an item they are already carrying? if item in player: item.move(where, player) inventory_items.append(item) # If the item is in the room then we'll take it first and then put it into the container # TODO: We need to handle in a linguistic stylish way the situation where one part of this two-step operation fails elif item in player.location: item.move(player, player) item.move(where, player) player.tell("You take %s and put it in the %s." % (item.title, where.name)) player.tell_others( "{Title} takes %s and puts it in the %s." % (item.title, where.name)) except ActionRefused as x: refused.append((item, str(x))) # The item refused to move at some point so inform the player for item, message in refused: player.tell(message) if inventory_items: items_msg = Lang.join( Lang.a(item.title) for item in inventory_items) player.tell_others("{Title} puts %s in the %s." % (items_msg, where.name)) player.tell( "You put <item>{items}</item> in the <item>{where}</item>.". format(items=items_msg, where=where.name))
def func(player, parsed, ctx): # If we're examining a creature let's get some info for it creature = None if parsed.obj_info and isinstance(parsed.obj_order[0], Creature): creature = parsed.obj_order[0] name = creature.name # If we're not examining a creature we'll get item info if not creature: # Handle the case where nothing to examine was specified if not parsed.args: raise ParseError("Who or what would you like to examine?") Actions.parse_is_are(parsed.args) name = parsed.args[0] creature = player.location.search_creature(name) # If we're trying to examine a creature we'll figure out # which creature we're dealing with if creature: # Player is trying to examine themselves. Sheesh. if creature is player: player.tell("You are <creature>%s</creature>." % Lang.capital(creature.title)) return if creature.name.lower() != name.lower() and name.lower( ) in creature.aliases: player.tell("(By %s you probably meant %s.)" % (name, creature.name)) # If the creature has a description show the player otherwise we'll just share their title if creature.description: player.tell(creature.description) else: player.tell("This is <creature>%s</creature>." % creature.title) # If there is extended info about the creature we'll provide that as well if name in creature.extra_desc: player.tell(creature.extra_desc[name]) if name in player.location.extra_desc: player.tell(player.location.extra_desc[name]) return # Is the player trying to examine an item? item, container = player.locate_item(name) if item: if item.name.lower() != name.lower() and name.lower( ) in item.aliases: player.tell("Maybe you meant %s?" % item.name) # If there is an extended description we'll share that. if name in item.extra_desc: player.tell(item.extra_desc[name]) # Otherwise we'll just share what basic info we have else: if item in player: player.tell("You're carrying <item>%s</item>." % Lang.a(item.title)) elif container and container in player: Actions.print_object_location(player, item, container) else: player.tell("You see <item>%s</item>." % Lang.a(item.title)) if item.description: player.tell(item.description) try: inventory = item.inventory except ActionRefused: pass else: if inventory: msg = "It contains " for item in inventory: msg += "<item>%s</item>, " % item.title msg = msg[:-2] player.tell(msg) else: player.tell("It's empty.") # Player is examining an exit? elif name in player.location.exits: player.tell( "<exit>" + player.location.exits[name].description + "</exit> represents a way you can travel to a different place." ) # If nothing else we'll do a search for any extended descriptive info associated with # locations and items else: text = player.search_extradesc(name) if text: player.tell(text) else: raise ActionRefused("%s doesn't appear to be here." % name)
def sys_destroy(self, actor, ctx): raise ActionRefused( Lang.a(self.__class__.__name__) + " can not be destroyed")
def sys_clone(self, actor): raise ActionRefused( Lang.a(self.__class__.__name__) + " can not be cloned")
def look(self, exclude_creature=None, short=False): paragraphs = ["<location>[" + self.name + "]</location>"] # Construct the short version of a location description. if short: if self.exits and context.config.show_exits_in_look: paragraphs.append("<exit>Exits</exit>: " + ", ".join(sorted(set(self.exits.keys())))) if self.items: item_names = sorted(item.name for item in self.items) paragraphs.append("<item>You see</item>: " + Lang.join(item_names)) if self.creatures: creature_names = sorted(creature.name for creature in self.creatures if creature != exclude_creature) if creature_names: paragraphs.append("<creature>Present</creature>: " + Lang.join(creature_names)) return paragraphs # # Construct the long version of a location description. # if self.description: paragraphs.append(self.description) if self.exits and context.config.show_exits_in_look: # Keep track of exits we've already described exits_seen = set() exit_paragraph = [] for exit_name in sorted(self.exits): exit = self.exits[exit_name] if exit not in exits_seen: exits_seen.add(exit) exit_paragraph.append(exit.short_description) paragraphs.append(" ".join(exit_paragraph)) items_and_creatures = [] # We'll preferentially use item short descriptions if provided and resort to the item's title if necessary items_with_short_descr = [item for item in self.items if item.short_description] items_without_short_descr = [item for item in self.items if not item.short_description] uniq_descriptions = set() # If there's a short description for an item use it preferentially if items_with_short_descr: for item in items_with_short_descr: uniq_descriptions.add(item.short_description) # Add unique item description to our running list of things to describe items_and_creatures.extend(uniq_descriptions) # If there's no short description for an item we'll just use the item's title if items_without_short_descr: titles = sorted([Lang.a(item.title) for item in items_without_short_descr]) items_and_creatures.append("You see " + Lang.join(titles) + ".") # We'll preferentially use creature short descriptions if provided and resort to the title if necessary creatures_with_short_descr = [creature for creature in self.creatures if creature != exclude_creature and creature.short_description] creatures_without_short_descr = [creature for creature in self.creatures if creature != exclude_creature and not creature.short_description] # If there's a short descriptions for a creature use it preferentially if creatures_without_short_descr: titles = sorted(creature.title for creature in creatures_without_short_descr) if titles: titles_str = Lang.join(titles) if len(titles) > 1: titles_str += " are here." else: titles_str += " is here." items_and_creatures.append(Lang.capital(titles_str)) uniq_descriptions = set() # If there's no short description for a creature we'll just use the item's title if creatures_with_short_descr: for creature in creatures_with_short_descr: uniq_descriptions.add(creature.short_description) # Add unique creature descriptions to our running list of things to describe items_and_creatures.extend(uniq_descriptions) if items_and_creatures: paragraphs.append(" ".join(items_and_creatures)) return paragraphs