def do(self, player, line, target): if target is player: raise eimaginary.ActionFailure( events.ThatDoesntMakeSense(u"Hit yourself? Stupid.", actor=player.thing)) cost = random.randrange(1, 5) if player.stamina < cost: raise eimaginary.ActionFailure( events.ThatDoesntWork(u"You're too tired!", actor=player.thing)) damage = random.randrange(1, 5) player.stamina.decrease(cost) thp = target.hitpoints.decrease(damage) events.Success( actor=player.thing, target=target.thing, targetMessage=language.Sentence([player.thing, " hits you for ", damage, " hitpoints."]), actorMessage=language.Sentence(["You hit ", language.Noun(target.thing).definiteNounPhrase(), " for ", damage, " hitpoints."]), otherMessage=language.Sentence([player.thing, " hits ", target.thing, "."])).broadcast() if thp <= 0: xp = target.experience / 2 + 1 player.gainExperience(xp) # I LOVE IT targetIsDead = [target.thing, " is dead!", "\n"] events.Success( actor=player.thing, target=target.thing, actorMessage=["\n", targetIsDead, "You gain ", xp, " experience"], targetMessage=["You are dead!"], otherMessage=targetIsDead).broadcast() target.thing.destroy()
def set_GENDER(self, player, line, target, value): """ Attempt to change the gender of a thing. @param target: The thing to change the gender of. @param value: A string naming a gender on L{language.Gender}. """ try: target.gender = getattr(language.Gender, value.upper()) except AttributeError: gender = {language.Gender.MALE: "male", language.Gender.FEMALE: "female", language.Gender.NEUTER: "neuter"}.get(target.gender) raise eimaginary.ActionFailure(events.ThatDoesntMakeSense( actor=player.thing, actorMessage=("Only male, female, and neuter are valid " "genders. You remain ", gender, "."))) else: if player.thing is target: # XXX Why can't I do something with Noun to collapse these # cases? event = events.Success( actor=player.thing, actorMessage=(u"You set your gender to ", value, ".")) else: event = events.Success( actor=player.thing, target=target, actorMessage=("You set ", language.Noun(target).hisHer(), " gender to ", value, "."), targetMessage=(player.thing, " set your gender to ", value, ".")) event.broadcast()
def coinAdded(self, coin): """ Called when a coin is added to this thing. @type coin: C{ICoin} provider """ self._currencyCounter += 1 if self._currencyCounter >= 5 and self.getContents(): self._currencyCounter = 0 try: obj = iter(self.getContents()).next() except StopIteration: evt = events.Success( actor=self.thing, target=obj, otherMessage=language.Sentence([self.thing, " thumps loudly."])) else: evt = events.Success( actor=self.thing, target=obj, otherMessage=language.Sentence([ language.Noun(self.thing).definiteNounPhrase(), " thumps loudly and spits out ", obj, " onto the ground."])) state = self.closed self.closed = False try: obj.moveTo(self.thing.location) finally: self.closed = state evt.broadcast()
def do(self, player, line, direction): for exit in iimaginary.IContainer(player.thing.location).getExits(): if exit.name == direction: if exit.sibling is not None: evt = events.Success(location=exit.toLocation, otherMessage=language.Sentence([ exit.sibling, " crumbles and disappears." ])) evt.broadcast() evt = events.Success(actor=player.thing, actorMessage="It's gone.", otherMessage=language.Sentence([ language.Noun( player.thing).nounPhrase(), " destroyed ", exit, "." ])) evt.broadcast() exit.destroy() return raise eimaginary.ActionFailure( events.ThatDoesntMakeSense( actor=player.thing, actorMessage="There isn't an exit in that direction."))
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()
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()
def do(self, player, line, target): from imaginary import garments try: player.putOn(target) except garments.TooBulky as e: raise eimaginary.ActionFailure( events.ThatDoesntWork( actor=player.thing, target=target.thing, actorMessage=language.Sentence([ language.Noun( e.wornGarment.thing).definiteNounPhrase(), u" you are already wearing is too bulky for you to do" u" that." ]), otherMessage=language.Sentence([ player.thing, u" wrestles with basic personal problems." ]))) events.Success(actor=player.thing, target=target.thing, actorMessage=(u"You put on ", language.Noun( target.thing).definiteNounPhrase(), "."), otherMessage=language.Sentence( [player.thing, " puts on ", target.thing, "."])).broadcast()
def targetTaken(player, target, container=None): if container is None: return events.Success(actor=player, target=target, actorMessage=("You take ", target, "."), targetMessage=(player, " takes you."), otherMessage=(player, " takes ", target, ".")) idop = language.Noun(container).definiteNounPhrase() return events.Success( actor=player, target=target, tool=container, actorMessage=("You take ", target, " from ", idop, "."), targetMessage=(player, " takes you from ", idop, "."), toolMessage=(player, " takes ", target, " from you."), otherMessage=(player, " takes ", target, " from ", idop, "."))
class Remove(TargetAction): expr = ((pyparsing.Literal("remove") | pyparsing.Literal("take off")) + pyparsing.White() + targetString("target")) targetInterface = iimaginary.IClothing actorInterface = iimaginary.IClothingWearer def do(self, player, line, target): from imaginary import garments try: player.takeOff(target) except garments.InaccessibleGarment, e: raise eimaginary.ActionFailure( events.ThatDoesntWork( actor=player.thing, target=target.thing, actorMessage=(u"You cannot take off ", language.Noun( target.thing).definiteNounPhrase(), u" because you are wearing ", e.obscuringGarment.thing, u"."), otherMessage=language.Sentence([ player.thing, u" gets a dumb look on ", language.Noun(player.thing).hisHer(), u" face." ]))) evt = events.Success( actor=player.thing, target=target.thing, actorMessage=(u"You take off ", language.Noun(target.thing).definiteNounPhrase(), u"."), otherMessage=language.Sentence( [player.thing, u" takes off ", target.thing, u"."])) evt.broadcast()
def do(self, player, line, target, name): evt = events.Success( actor=player.thing, actorMessage=("You change ", target, "'s name."), otherMessage=language.Sentence([player.thing, " changes ", target, "'s name to ", name, "."])) evt.broadcast() target.name = name
def do(self, player, line, target, description): target.description = description evt = events.Success( actor=player.thing, actorMessage=("You change ", target, "'s description."), otherMessage=(player.thing, " changes ", target, "'s description.")) evt.broadcast()
def set_PROPER(self, player, line, target, value): """ Attempt to change the name of a thing from a proper noun to a common noun or the other way around. @param target: The thing to change. @param value: The string C{"true"} or C{"false"}. """ if value == "true": target.proper = True phrase = '" a proper noun.' elif value == "false": target.proper = False phrase = '" a common noun.' else: raise eimaginary.ActionFailure( events.ThatDoesntMakeSense( actor=player.thing, actorMessage=("Only true and false are valid settings " "for proper."))) events.Success( actor=player.thing, actorMessage=('You make the name of "', language.Noun(target).shortName(), phrase)).broadcast()
def gainExperience(self, amount): experience = self.experience + amount level = int(math.log(experience) / math.log(2)) evt = None if level > self.level: evt = events.Success( actor=self.thing, actorMessage=("You gain ", level - self.level, " levels!\n")) elif level < self.level: evt = events.Success( actor=self.thing, actorMessage=("You lose ", self.level - level, " levels!\n")) self.level = level self.experience = experience if evt is not None: self.send(evt)
def do(self, player, line): """ Tell the player the thing types which exist. """ events.Success(actor=player.thing, actorMessage=[(t, "\n") for t in listThingTypes()]).broadcast()
def do(self, player, line, target): target.hitpoints.current = target.hitpoints.max target.stamina.current = target.stamina.max if player is target: evt = events.Success( actor=player.thing, actorMessage="You have fully restored yourself.") evt.broadcast() else: evt = events.Success( actor=player.thing, actorMessage=("You have restored ", target.thing, " to full health."), target=target.thing, targetMessage=(player.thing, " has restored you to full health."), otherMessage=(player.thing, " has restored ", target.thing, " to full health.")) evt.broadcast()
def do(self, player, line, name): srch = player.thing.search(2, iimaginary.IVisible, name) evt = events.Success( actor=player.thing, actorMessage=language.ExpressList( list(iterutils.interlace('\n', (o.visualize() for o in srch))))) evt.broadcast()
def do(self, player, line): for visible in player.thing.findProviders(iimaginary.IVisible, 1): if player.thing.location is visible.thing: concept = visible.visualize() break else: concept = u"You are floating in an empty, formless void." events.Success(actor=player.thing, actorMessage=concept).broadcast()
def send(self, *event): if len(event) != 1 or isinstance(event[0], (str, tuple)): event = events.Success( actor=self.thing, actorMessage=event) else: event = event[0] self.prepare(event)()
def test_intelligenceReceivesEvent(self): """ Enduring intelligences should receive events. """ evt = events.Success(location=self.location, otherMessage=u"Hello, how are you?") self.actor.send(evt) self.assertEquals(self.intelligence.concepts, [evt])
def do(self, player, line): from imaginary import garments equipment = list( player.store.query( objects.Thing, AND(garments.Garment.thing == objects.Thing.storeID, garments.Garment.wearer == player), sort=objects.Thing.name.ascending)) if equipment: evt = events.Success(actor=player.thing, actorMessage=[ u"You are wearing ", language.ItemizedList(equipment), u"." ]) else: evt = events.Success(actor=player.thing, actorMessage=language.ExpressString( u"You aren't wearing any equipment.")) evt.broadcast()
def do(self, player, line): ultimateLocation = player.thing.location while ultimateLocation.location is not None: ultimateLocation = ultimateLocation.location targets = visualizations( player.thing, lambda viewTarget: viewTarget.targetAs(IThing) is ultimateLocation) if targets: target = targets[0] else: target = u"You are floating in an empty, formless void." events.Success(actor=player.thing, actorMessage=target).broadcast()
def creationSuccess(player, creation): """ Create and return an event describing that an object was successfully created. """ phrase = language.Noun(creation).nounPhrase() return events.Success( actor=player, target=creation, actorMessage=language.Sentence(["You create ", phrase, "."]), targetMessage=language.Sentence([player, " creates you."]), otherMessage=language.Sentence([player, " creates ", phrase, "."]))
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."])))
def do(self, player, line, target): dnf = language.Noun(target.thing).definiteNounPhrase() if target.closed: raise eimaginary.ActionFailure(events.ThatDoesntWork( actor=player.thing, target=target.thing, actorMessage=language.Sentence([dnf, " is already closed."]))) target.closed = True evt = events.Success( actor=player.thing, target=target.thing, actorMessage=("You close ", dnf, "."), targetMessage=language.Sentence([player.thing, " closes you."]), otherMessage=language.Sentence([player.thing, " closes ", target.thing, "."])) evt.broadcast()
def do(self, player, line, direction, name): direction = expandDirection(direction) if iimaginary.IContainer(player.thing.location).getExitNamed(direction, None) is not None: raise eimaginary.ActionFailure(events.ThatDoesntMakeSense( actor=player.thing, actorMessage="There is already an exit in that direction.")) room = objects.Thing(store=player.store, name=name) objects.Container.createFor(room, capacity=1000) objects.Exit.link(player.thing.location, room, direction) evt = events.Success( actor=player.thing, actorMessage="You create an exit.", otherMessage=language.Sentence([player.thing, " created an exit to the ", direction, "."])) evt.broadcast()
def do(self, player, line, direction): location = player.location evt = events.Success( location=location, actor=player, otherMessage=(player, " leaves ", direction.name, ".")) evt.broadcast() try: direction.traverse(player) except eimaginary.DoesntFit: raise eimaginary.ActionFailure(events.ThatDoesntWork( actor=player, actorMessage=language.ExpressString( u"There's no room for you there."))) # This is subtly incorrect: see http://divmod.org/trac/ticket/2917 lookAroundActor = iimaginary.IActor(player) LookAround().do(lookAroundActor, "look")
def responseReceived(self, responder, romajiResponse): """ Called when some speech is observed. """ me = self._actor().thing if self.vetteChallengeResponse(romajiResponse): self._currentChallenge = None verb = u"salute" else: verb = u"bite" evt = events.Success( actor=me, target=responder, actorMessage=language.Sentence(["You ", verb, " ", responder, "."]), targetMessage=language.Sentence( [language.Noun(me).shortName(), " ", verb, "s you!"]), otherMessage=language.Sentence( [me, " ", verb, "s ", responder, "."])) # F**k the reactor, F**k scheduling, why does responseReceived # need to be concerned with these stupid scheduling details # when all it wants to do is respond basically-immediately. self._callLater(0, evt.broadcast)
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")
class Wear(TargetAction): expr = (pyparsing.Literal("wear") + pyparsing.White() + targetString("target")) targetInterface = iimaginary.IClothing actorInterface = iimaginary.IClothingWearer def do(self, player, line, target): from imaginary import garments try: player.putOn(target) except garments.TooBulky, e: raise eimaginary.ActionFailure( events.ThatDoesntWork( actor=player.thing, target=target.thing, actorMessage=language.Sentence([ language.Noun( e.wornGarment.thing).definiteNounPhrase(), u" you are already wearing is too bulky for you to do" u" that." ]), otherMessage=language.Sentence([ player.thing, u" wrestles with basic personal problems." ]))) evt = events.Success( actor=player.thing, target=target.thing, actorMessage=(u"You put on ", language.Noun(target.thing).definiteNounPhrase(), "."), otherMessage=language.Sentence( [player.thing, " puts on ", target.thing, "."])) evt.broadcast()
def do(self, player, line, candelas): """ Attempt to change the illumination of the player's surroundings. @param player: a manipulator that can change the illumination of its room. @type player: L{IManipulator} @param line: the text being parsed @type line: L{str} @param candelas: the number of candelas to change the ambient illumination to. @type candelas: L{str} """ candelas = int(candelas) oldCandelas = player.setIllumination(candelas) otherMessage = None if oldCandelas == candelas: actorMessage = u"You do it. Swell." elif candelas == 0: actorMessage = ( u"Your environs fade to black due to Ineffable Spooky Magic.") otherMessage = actorMessage elif oldCandelas == 0: actorMessage = u"Your environs are suddenly alight." otherMessage = actorMessage elif candelas < oldCandelas: actorMessage = u"Your environs seem slightly dimmer." otherMessage = actorMessage elif candelas > oldCandelas: actorMessage = u"Your environs seem slightly brighter." otherMessage = actorMessage events.Success(actor=player.thing, actorMessage=actorMessage, otherMessage=otherMessage).broadcast()