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 vt102(self, observer): heshe = language.Noun(self.thing).heShe() L = _orderTopClothingByGlobalSlotList(self.garments) if L is None: return language.Sentence([heshe, u' is naked.']).vt102(observer) return language.Sentence([ heshe, u' is wearing ', language.ItemizedList( [language.Noun(g.thing).nounPhrase() for g in L]), u'.' ]).vt102(observer)
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 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 name(self): """ Implement L{iimaginary.IExit.name} to return a descriptive name for the outward exit of this specific container. """ return 'out of ', language.Noun( self.container.thing).definiteNounPhrase()
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 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 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 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 vt102(self, observer): players = self.original.connected return [[T.bold, self.header], u'\n', [[language.Noun(p).shortName().vt102(observer), u'\n'] for p in players], [T.bold, self.footer % {'playerCount': len(players)}], u'\n']
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 vt102(self, observer): return [[ T.bold, T.fg.yellow, language.Noun( self.original.thing).definiteNounPhrase().plaintext(observer) ], u" is ", [T.bold, T.fg.red, self.original._condition(), u"."]]
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."])))
def cantFind_target(self, player, targetName): # XXX Hoist this up to TargetAction and apply it generally. things = _getIt(player.thing, targetName, iimaginary.IThing, self.targetRadius(player)) for thing in things: return (language.Noun(thing).nounPhrase().plaintext(player), " cannot be restored.") return "Who's that?"
def vt102(self, observer): return [ [T.fg.yellow, "Inventory:\n"], [ T.fg.green, [(language.Noun(o).shortName().vt102(observer), '\n') for o in iimaginary.IContainer(self.original).getContents()] ] ]
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 setUp(self): self.thing = FakeThing( name=u'fake thing', description=u"Fake Thing Description", gender="!@>", proper=False, powerupsFor=lambda iface: [], ) self.male = FakeThing( name=u"billy", gender=language.Gender.MALE, ) self.female = FakeThing( name=u"janey", gender=language.Gender.FEMALE, ) self.observer = FakeThing() self.noun = language.Noun(self.thing) self.malenoun = language.Noun(self.male) self.femalenoun = language.Noun(self.female)
def movementImminent(self, movee, destination): """ Something is trying to move. Don't allow it if I'm currently worn. """ if self.wearer is not None and movee is self.thing: raise ActionFailure( ThatDoesntWork( # XXX I don't actually know who is performing the action # :-(. actor=self.wearer.thing, actorMessage=[ "You can't move ", language.Noun(self.thing).definiteNounPhrase(), " without removing it first." ]))
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 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, "."))
def moveTo(self, where, arrivalEventFactory=None): """ Implement L{iimaginary.IThing.moveTo} to change the C{location} of this L{Thing} to a new L{Thing}, broadcasting an L{events.DepartureEvent} to note this object's departure from its current C{location}. Before moving it, invoke each L{IMovementRestriction} powerup on this L{Thing} to allow them to prevent this movement. """ whereContainer = iimaginary.IContainer(where, None) if (whereContainer is iimaginary.IContainer(self.location, None)): # Early out if I'm being moved to the same location that I was # already in. return if whereContainer is None: whereThing = None else: whereThing = whereContainer.thing if whereThing is not None and whereThing.location is self: # XXX should be checked against _all_ locations of whereThing, not # just the proximate one. # XXX actor= here is wrong, who knows who is moving this thing. raise eimaginary.ActionFailure( events.ThatDoesntWork(actor=self, actorMessage=[ language.Noun( where.thing).definiteNounPhrase( ).capitalizeConcept(), " won't fit inside itself." ])) oldLocation = self.location for restriction in self.powerupsFor(iimaginary.IMovementRestriction): restriction.movementImminent(self, where) if oldLocation is not None: events.DepartureEvent(oldLocation, self).broadcast() if where is not None: where = iimaginary.IContainer(where) if oldLocation is not None and not self.portable: raise eimaginary.CannotMove(self, where) where.add(self) if arrivalEventFactory is not None: arrivalEventFactory(self).broadcast() if oldLocation is not None: iimaginary.IContainer(oldLocation).remove(self)
def do(self, player, line, target): if target.location is not player.thing: raise eimaginary.ActionFailure( events.ThatDoesntMakeSense( actor=player.thing, actorMessage="You can't drop that.")) try: target.moveTo( player.thing.location, arrivalEventFactory=lambda target: events.ArrivalEvent( actor=player.thing, actorMessage=("You drop ", language.Noun(target). definiteNounPhrase(), "."), target=target, targetMessage=(player.thing, " drops you."), otherMessage=(player.thing, " drops ", target, "."))) except eimaginary.DoesntFit: raise insufficientSpace(player.thing)
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)
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 targetNotAvailable(self, player, exc): for thing in player.search(self.targetRadius(player), iimaginary.IThing, exc.partValue): return (language.Noun(thing).nounPhrase().plaintext(player), " cannot be restored.") return "Who's that?"
def conceptualize(self): return language.ExpressList( [u'the exit to ', language.Noun(self.toLocation).nounPhrase()])
def _exitAsConcept(exit): return language.ExpressList( [u'the exit to ', language.Noun(exit.toLocation).nounPhrase()])
def visualizeWithContents(self, paths): """ Visualize this L{Thing} via L{Description.fromVisualization}. """ return Description.fromVisualization(self, paths) def isViewOf(self, thing): """ Implement L{IVisible.isViewOf} to return C{True} if its argument is C{self}. In other words, this L{Thing} is only a view of itself. """ return (thing is self) components.registerAdapter(lambda thing: language.Noun(thing).nounPhrase(), Thing, iimaginary.IConcept) def _eventuallyContains(containerThing, containeeThing): """ Does a container, or any containers within it (or any containers within any of those, etc etc) contain some object? @param containeeThing: The L{Thing} which may be contained. @param containerThing: The L{Thing} which may have a L{Container} that contains C{containeeThing}. @return: L{True} if the containee is contained by the container.