class LookAt(TargetAction): actionName = "look" expr = (pyparsing.Literal("look") + pyparsing.Optional(pyparsing.White() + pyparsing.Literal("at")) + pyparsing.White() + pyparsing.restOfLine.setResultsName("target")) targetInterface = iimaginary.IVisible def targetNotAvailable(self, player, exc): return "You don't see that." def targetRadius(self, player): return 3 def do(self, player, line, target): if player.thing is not target: evt = events.Success(actor=player.thing, target=target, actorMessage=target.visualize(), targetMessage=(player.thing, " looks at you.")) else: evt = events.Success(actor=player.thing, actorMessage=target.visualize()) evt.broadcast()
class PutIn(ToolAction): toolInterface = iimaginary.IThing targetInterface = iimaginary.IContainer def cantFind_target(self, player, targetName): return "That doesn't work." expr = (pyparsing.Literal("put") + pyparsing.White() + targetString("tool") + pyparsing.Optional(pyparsing.White() + pyparsing.Literal("in")) + pyparsing.White() + targetString("target")) def do(self, player, line, tool, target): ctool = iimaginary.IContainer(tool, None) targetObject = target.thing if ctool is not None and (ctool.contains(targetObject) or ctool is target): raise eimaginary.ActionFailure( events.ThatDoesntWork( actor=player.thing, target=targetObject, tool=tool, actorMessage="A thing cannot contain itself in euclidean space.")) dnf = language.Noun(targetObject).definiteNounPhrase() evt = events.Success( actor=player.thing, target=targetObject, tool=tool, actorMessage=("You put ", language.Noun(tool).definiteNounPhrase(), " in ", dnf, "."), targetMessage=language.Sentence([player.thing, " puts ", " tool in you."]), toolMessage=language.Sentence([player.thing, " puts you in ", targetObject, "."]), otherMessage=language.Sentence([player.thing, " puts ", tool, " in ", targetObject, "."])) evt.broadcast() try: tool.moveTo(target) except eimaginary.DoesntFit: # <allexpro> dash: put me in a tent and give it to moshez! raise eimaginary.ActionFailure( events.ThatDoesntWork( actor=player.thing, target=targetObject, tool=tool, actorMessage=language.Sentence([ language.Noun(tool).definiteNounPhrase(), u" does not fit in ", dnf, u"."]))) except eimaginary.Closed: raise eimaginary.ActionFailure( events.ThatDoesntWork( actor=player.thing, target=targetObject, tool=tool, actorMessage=language.Sentence([dnf, " is closed."])))
class LookAt(TargetAction): actionName = "look" expr = (pyparsing.Literal("look") + pyparsing.Optional(pyparsing.White() + pyparsing.Literal("at")) + pyparsing.White() + pyparsing.restOfLine.setResultsName("target")) targetInterface = iimaginary.IVisible def resolve_target(self, player, targetName): """ Resolve the target to look at by looking for a named, visible object in a proximity of 3 meters from the player. @param player: The player doing the looking. @type player: L{IThing} @param targetName: The name of the object we are looking for. @type targetName: C{unicode} @return: A list of the results of C{visualizeWithContents}. @rtype: C{list} of L{IConcept} @raise eimaginary.ActionFailure: with an appropriate message if the target cannot be resolved for an identifiable reason. See L{imaginary.objects.Thing.obtainOrReportWhyNot} for a description of how such reasons may be identified. """ return visualizations(player, lambda path: isKnownTo(player, path, targetName)) def cantFind_target(self, player, name): return "You don't see that." def targetRadius(self, player): return 3 def do(self, player, line, target): if player.thing is not target: evt = events.Success( actor=player.thing, # sometimes 'target' is a thing you're looking at, and # therefore a DescriptionWithContents, and therefore has a # 'target' attribute; other times it's some random # ExpressString instance and you are not actually broadcasting # *to* anywhere. target=getattr(target, "target", None), actorMessage=target, targetMessage=(player.thing, " looks at you.")) else: evt = events.Success( actor=player.thing, actorMessage=target) evt.broadcast()
class TakeFrom(ToolAction): actionName = "take" expr = ((pyparsing.Literal("get") ^ pyparsing.Literal("take")) + pyparsing.White() + targetString("target") + pyparsing.Optional(pyparsing.White() + pyparsing.Literal("from")) + pyparsing.White() + targetString("tool")) def targetNotAvailable(self, player, exc): return "Nothing like that around here." toolNotAvailable = targetNotAvailable def do(self, player, line, target, tool): # XXX Make sure target is in tool targetTaken(player.thing, target, tool).broadcast() try: target.moveTo(player.thing) except eimaginary.DoesntFit: raise tooHeavy(player.thing, target)
class Go(NoTargetAction): expr = (pyparsing.Optional(pyparsing.Literal("go") + pyparsing.White()) + DIRECTION_LITERAL) def do(self, player, line, direction): try: exit = iimaginary.IContainer( player.thing.location).getExitNamed(direction) except KeyError: raise eimaginary.ActionFailure( events.ThatDoesntWork(actor=player.thing, actorMessage=u"You can't go that way.")) dest = exit.toLocation location = player.thing.location evt = events.Success(location=location, actor=player.thing, otherMessage=(player.thing, " leaves ", direction, ".")) evt.broadcast() if exit.sibling is not None: arriveDirection = exit.sibling.name else: arriveDirection = object.OPPOSITE_DIRECTIONS[exit.name] try: player.thing.moveTo( dest, arrivalEventFactory=lambda player: events.MovementArrivalEvent( thing=player, origin=None, direction=arriveDirection)) except eimaginary.DoesntFit: raise eimaginary.ActionFailure( events.ThatDoesntWork(actor=player.thing, actorMessage=language.ExpressString( u"There's no room for you there."))) # XXX A convention for programmatically invoked actions? # None as the line? LookAround().do(player, "look")