Example #1
0
    def throw(self, player, item, target_text):
        
        with Observer(player) as ob:
            if not player.is_carrying(item):
                player.notify_observers_reply('not_carrying', template=ReplyHelpers.TEMPLATE_ACTION, action=Actions.get_action_text(Actions.THROW), item_prefix=item.text_prefix, item_text=item.description)
                return self.__build_reply(ob)

        target = None
        if target_text is None:
            target = self.get_next_monster()
        else:
            for m in self.get_monsters():
                if m.is_match(target_text):
                    target = m
                    break
        #Ok probably room item
        if not target:
            target = self.get_room_item_by_text(target_text)
        if not target:
            return ReplyHelpers.render_action_template('no_such_target', item_text=item.description)

        #Either throw at a room item or a monster
        is_monster = isinstance(target, players.Monster)
        is_player = isinstance(target, players.Player) and not is_monster
        is_weapon = isinstance(item, weapons.Weapon)
        is_item = isinstance(target, items.Item) or isinstance(target, RoomItem)

        floor_text = self.__get_room_text()[1]
        with Observer(player) as ob:
            is_hit = player.throw(item, target)

            if is_hit and is_item:
                player.notify_observers_reply('throw_item_hit', template=ReplyHelpers.TEMPLATE_ACTION, item_text=item.description, target_text=target.description, floor_text=floor_text)
            elif is_item:
                player.notify_observers_reply('throw_item_miss', template=ReplyHelpers.TEMPLATE_ACTION, item_text=item.description, target_text=target.description, floor_text=floor_text)

            if is_hit and is_monster and is_weapon:
                #Hit and lodged in monster
                if not GameRules.is_thrown_item_lost():
                    target.pickup(item)
            elif is_hit and is_player:
                #Hit and lodged in player
                pass
            else:
                #Miss or hit and on floor
                if not GameRules.is_thrown_item_lost():
                    self.add_floor_item(item.one_of())

            if is_monster and is_weapon and not target.is_dead:
                target.strike(player)

            return self.__build_reply(ob)
Example #2
0
 def pick_lock(self, item):
     #TODO:
     #if item is None:
     #    return ReplyHelpers.render_room_template('query_picklock') + ' ' + self.build_openable_items()
     with Observer(item) as ob:
         item.unlock_with_pick(self.__player)
         return self.__build_reply(ob)
Example #3
0
 def shoot(self, player, target):
     with Observer(player) as ob:
         player.shoot(target)
         #TODO: This doesn't look right
         if not target.is_dead:
             target.strike(player)
         return self.__build_reply(ob)
Example #4
0
 def open(self, item):
     if isinstance(item, RoomOpenableItem):
         with Observer(item) as ob:
             item.open(self.__player)
             return self.__build_reply(ob)
     else:
         return ReplyHelpers.try_render_room_template(self, item, 'open_cannot')
Example #5
0
 def lock(self, player, item):
     with Observer(item) as ob:
         key = item.get_key()
         if player.is_carrying(key):
             item.lock_with_key(player)
         elif player.can_cast_spell(spells.LockSpell()):
             item.lock_with_spell(spells.LockSpell(), player)
         else:
             return ReplyHelpers.render_room_template('nothing_to_lock_item', item=item.description)
         return self.__build_reply(ob)
Example #6
0
 def unlock(self, player, item):
     with Observer(item) as ob:
         key = item.get_key()
         if player.is_carrying(key):
             item.unlock(player, key=key)
         elif self.__player.can_picklock():
             item.unlock(player)
         elif player.can_cast_spell(spells.UnlockSpell()): #TODO fix
             item.unlock(player, spell=spells.UnlockSpell())
         else:
             return ReplyHelpers.render_room_template('nothing_to_unlock_item', item=item.description)
         return self.__build_reply(ob)
Example #7
0
    def cast(self, player, spell_text, target):
        spell = spells.SpellStats.get_spell_by_text(spell_text)
        if not spell:
            return ReplyHelpers.render_action_template('spell_cannot')

        with Observer(player) as ob:
            if not player.can_cast_spell(spell):
                return ReplyHelpers.render_action_template('spell_cannot_cast',spell_name=spell.description) + self.__build_reply(ob)
        
            result = ''
            player.cast(spell, target)
        
            #You attacked a poor monster
            if isinstance(target, players.Monster):
                target.strike(player)
            result += ' ' + self.__build_reply(ob)
            return result
Example #8
0
 def __list_items_reply(self, items, pickup=False, player=None):
     result = ''
     i = 0
     for item in items:
         i += 1
         if i == 1:
             result += ' ' + ReplyHelpers.render_room_template('found_items',item=item.text_prefix + ' ' + item.description)
         elif i == len(items):
             result += ' and {} {}.'.format(item.text_prefix, item.description)
         else:
             result += ', {}'.format(item.description)
     if pickup:
         with Observer(player) as ob:
             for item in items:
                 player.pickup(item)
         result += self.__build_reply(ob)
     if i==0:
         result += ' ' + ReplyHelpers.render_room_template('found_nothing')
     return result
Example #9
0
 def __check_for_monster(self, player):
     #Has a monster appeared?
     monster = self.get_next_monster()
     result = ''
     if monster is None:
         player._end_battle()
     else:
         #Yikes a monster!
         if not player.is_in_battle:
             player._start_battle()
             with Observer(player) as ob:
                 if GameRules.roll_initiative_check(monster) > GameRules.roll_initiative_check(player):
                     #Monster strikes first
                     result = ReplyHelpers.render_action_template('monster_detects_you',monster_name=monster.class_name)
                     monster.strike(player)
                 else:
                     #Player gets to attack first
                     result = ReplyHelpers.render_action_template('monster_detected',monster_name=monster.class_name)
                 result += ' ' + self.__build_reply(ob)
     return result
Example #10
0
 def drink(self, player, item):
     with Observer(player) as ob:
         player.drink(item)
         return self.__build_reply(ob)
Example #11
0
 def eat(self, player, item):
     if not isinstance(item, items.Food):
         return ReplyHelpers.render_action_template('eat_cannot', item_text=item.description)
     with Observer(player) as ob:
         player.eat(item)
         return self.__build_reply(ob)
Example #12
0
 def pickup(self, item):
     with Observer(item) as ob:
         if self.__player.pickup(item):
             self._floor_items.remove(item)
         return ReplyHelpers.render_room_template('pickup_item', item=item.description) + ' ' + self.__build_reply(ob)
Example #13
0
 def close(self, item):
     with Observer(item) as ob:
         item.close(self.__player)
         return self.__build_reply(ob)
Example #14
0
 def push(self, item):
     with Observer(item) as ob:
         item.push(self.__player)
         return self.__build_reply(ob)