Esempio n. 1
0
    def take_damage(self, damage, attacker, weapon=None, skip_hooks=True):
        # TODO: This method should not have been called if the victim is already dead
        assert not self.player.dead

        if not self.player.dead:
            global _global_bypass

            _global_bypass = skip_hooks

            take_damage_info = TakeDamageInfo()
            take_damage_info.attacker = attacker

            global _global_weapon_entity

            if _global_weapon_entity is None:
                _global_weapon_entity = Entity.create('info_target')

            if weapon is None:
                _global_weapon_entity.set_key_value_string('classname', 'point_hurt')
            else:
                _global_weapon_entity.set_key_value_string('classname', f'wcs_{weapon}')

            take_damage_info.weapon = _global_weapon_entity.index
            take_damage_info.inflictor = _global_weapon_entity.index
            take_damage_info.damage = damage
            take_damage_info.type = DamageTypes.GENERIC

            try:
                self.player.on_take_damage(take_damage_info)
            finally:
                _global_bypass = False
Esempio n. 2
0
    def take_damage(self, damage, attacker, weapon=None, skip_hooks=True):
        # TODO: This method should not have been called if the victim is already dead
        assert not self.player.dead

        if not self.player.dead:
            global _global_bypass

            _global_bypass = skip_hooks

            take_damage_info = TakeDamageInfo()
            take_damage_info.attacker = attacker

            if weapon is None:
                index = set_weapon_name('point_hurt', None)
            else:
                index = set_weapon_name(weapon)

            take_damage_info.weapon = index
            take_damage_info.inflictor = index
            take_damage_info.damage = damage
            take_damage_info.type = DamageTypes.GENERIC

            try:
                self.player.on_take_damage(take_damage_info)
            finally:
                _global_bypass = False
Esempio n. 3
0
    def _on_player_victim(self, attacker, victim, **kwargs):
        if attacker.dead or self.level == 0:
            return

        info = TakeDamageInfo()
        info.inflictor = victim.index
        info.damage = self.reflect_damage
        attacker.on_take_damage(info)
Esempio n. 4
0
    def _on_player_victim(self, attacker, victim, **kwargs):
        if attacker.dead or randint(0,
                                    101) > (self.level * 2) or self.level == 0:
            return

        info = TakeDamageInfo()
        info.inflictor = victim.index
        info.damage = self.reflect_damage
        attacker.on_take_damage(info)
        send_wcs_saytext_by_index(
            self._msg_a.format(damage=self.reflect_damage, name=attacker.name),
            victim.index)
Esempio n. 5
0
    def take_damage(
            self, damage, damage_type=DamageTypes.GENERIC, attacker_index=None,
            weapon_index=None, hitgroup=HitGroup.GENERIC, skip_hooks=False,
            **kwargs):
        """Method used to hurt the entity with the given arguments."""
        # Import Entity classes
        # Doing this in the global scope causes cross import errors
        from weapons.entity import Weapon

        # Is the game supported?
        if not hasattr(self, 'on_take_damage'):

            # Raise an error if not supported
            raise NotImplementedError(
                '"take_damage" is not implemented for {0}'.format(GAME_NAME))

        # Store values for later use
        attacker = None
        weapon = None

        # Was an attacker given?
        if attacker_index is not None:

            # Try to get the Entity instance of the attacker
            with suppress(ValueError):
                attacker = Entity(attacker_index)

        # Was a weapon given?
        if weapon_index is not None:

            # Try to get the Weapon instance of the weapon
            with suppress(ValueError):
                weapon = Weapon(weapon_index)

        # Is there a weapon but no attacker?
        if attacker is None and weapon is not None:

            # Try to get the attacker based off of the weapon's owner
            with suppress(ValueError, OverflowError):
                attacker_index = index_from_inthandle(weapon.current_owner)
                attacker = Entity(attacker_index)

        # Is there an attacker but no weapon?
        if attacker is not None and weapon is None:

            # Try to use the attacker's active weapon
            with suppress(AttributeError, ValueError, OverflowError):
                weapon = Weapon(index_from_inthandle(attacker.active_weapon))

        # Try to set the hitgroup
        with suppress(AttributeError):
            self.hitgroup = hitgroup

        # Get a TakeDamageInfo instance
        take_damage_info = TakeDamageInfo()

        # Is there a valid weapon?
        if weapon is not None:

            # Is the weapon a projectile?
            if weapon.classname in _projectile_weapons:

                # Set the inflictor to the weapon's index
                take_damage_info.inflictor = weapon.index

            # Is the weapon not a projectile and the attacker is valid?
            elif attacker_index is not None:

                # Set the inflictor to the attacker's index
                take_damage_info.inflictor = attacker_index

            # Set the weapon to the weapon's index
            take_damage_info.weapon = weapon.index

        # Is the attacker valid?
        if attacker_index is not None:

            # Set the attacker to the attacker's index
            take_damage_info.attacker = attacker_index

        # Set the damage amount
        take_damage_info.damage = damage

        # Set the damage type value
        take_damage_info.type = damage_type

        # Loop through the given keywords
        for item in kwargs:

            # Set the offset's value
            setattr(take_damage_info, item, kwargs[item])

        if skip_hooks:
            self.on_take_damage.skip_hooks(take_damage_info)
        else:
            self.on_take_damage(take_damage_info)
Esempio n. 6
0
    def take_damage(self,
                    damage,
                    damage_type=DamageTypes.GENERIC,
                    attacker_index=None,
                    weapon_index=None,
                    hitgroup=HitGroup.GENERIC,
                    skip_hooks=False,
                    **kwargs):
        """Deal damage to the entity.

        :param int damage:
            Amount of damage to deal.
        :param DamageTypes damage_type:
            Type of the dealed damage.
        :param int attacker_index:
            If not None, the index will be used as the attacker.
        :param int weapon_index:
            If not None, the index will be used as the weapon. This method
            also tries to retrieve the attacker from the weapon, if
            ``attacker_index`` wasn't set.
        :param HitGroup hitgroup:
            The hitgroup where the damage should be applied.
        :param bool skip_hooks:
            If True, the damage will be dealed directly by skipping any
            registered hooks.
        """
        # Import Entity classes
        # Doing this in the global scope causes cross import errors
        from weapons.entity import Weapon

        # Is the game supported?
        if not hasattr(self, 'on_take_damage'):

            # Raise an error if not supported
            raise NotImplementedError(
                '"take_damage" is not implemented for {0}'.format(GAME_NAME))

        # Store values for later use
        attacker = None
        weapon = None

        # Was an attacker given?
        if attacker_index is not None:

            # Try to get the Entity instance of the attacker
            with suppress(ValueError):
                attacker = Entity(attacker_index)

        # Was a weapon given?
        if weapon_index is not None:

            # Try to get the Weapon instance of the weapon
            with suppress(ValueError):
                weapon = Weapon(weapon_index)

        # Is there a weapon but no attacker?
        if attacker is None and weapon is not None:

            # Try to get the attacker based off of the weapon's owner
            with suppress(ValueError, OverflowError):
                attacker_index = index_from_inthandle(weapon.owner_handle)
                attacker = Entity(attacker_index)

        # Is there an attacker but no weapon?
        if attacker is not None and weapon is None:

            # Try to use the attacker's active weapon
            with suppress(AttributeError):
                weapon = attacker.active_weapon

        # Try to set the hitgroup
        with suppress(AttributeError):
            self.hitgroup = hitgroup

        # Get a TakeDamageInfo instance
        take_damage_info = TakeDamageInfo()

        # Is there a valid weapon?
        if weapon is not None:

            # Is the weapon a projectile?
            if weapon.classname in _projectile_weapons:

                # Set the inflictor to the weapon's index
                take_damage_info.inflictor = weapon.index

            # Is the weapon not a projectile and the attacker is valid?
            elif attacker_index is not None:

                # Set the inflictor to the attacker's index
                take_damage_info.inflictor = attacker_index

            # Set the weapon to the weapon's index
            take_damage_info.weapon = weapon.index

        # Is the attacker valid?
        if attacker_index is not None:

            # Set the attacker to the attacker's index
            take_damage_info.attacker = attacker_index

        # Set the damage amount
        take_damage_info.damage = damage

        # Set the damage type value
        take_damage_info.type = damage_type

        # Loop through the given keywords
        for item in kwargs:

            # Set the offset's value
            setattr(take_damage_info, item, kwargs[item])

        if skip_hooks:
            self.on_take_damage.skip_hooks(take_damage_info)
        else:
            self.on_take_damage(take_damage_info)
    def take_damage(
            self, damage, damage_type=DamageTypes.GENERIC, attacker_index=None,
            weapon_index=None, hitgroup=HitGroup.GENERIC, skip_hooks=False,
            **kwargs):
        """Deal damage to the entity.

        :param int damage:
            Amount of damage to deal.
        :param DamageTypes damage_type:
            Type of the dealed damage.
        :param int attacker_index:
            If not None, the index will be used as the attacker.
        :param int weapon_index:
            If not None, the index will be used as the weapon. This method
            also tries to retrieve the attacker from the weapon, if
            ``attacker_index`` wasn't set.
        :param HitGroup hitgroup:
            The hitgroup where the damage should be applied.
        :param bool skip_hooks:
            If True, the damage will be dealed directly by skipping any
            registered hooks.
        """
        # Import Entity classes
        # Doing this in the global scope causes cross import errors
        from weapons.entity import Weapon

        # Is the game supported?
        if not hasattr(self, 'on_take_damage'):

            # Raise an error if not supported
            raise NotImplementedError(
                '"take_damage" is not implemented for {0}'.format(GAME_NAME))

        # Store values for later use
        attacker = None
        weapon = None

        # Was an attacker given?
        if attacker_index is not None:

            # Try to get the Entity instance of the attacker
            with suppress(ValueError):
                attacker = Entity(attacker_index)

        # Was a weapon given?
        if weapon_index is not None:

            # Try to get the Weapon instance of the weapon
            with suppress(ValueError):
                weapon = Weapon(weapon_index)

        # Is there a weapon but no attacker?
        if attacker is None and weapon is not None:

            # Try to get the attacker based off of the weapon's owner
            with suppress(ValueError, OverflowError):
                attacker_index = index_from_inthandle(weapon.owner_handle)
                attacker = Entity(attacker_index)

        # Is there an attacker but no weapon?
        if attacker is not None and weapon is None:

            # Try to use the attacker's active weapon
            with suppress(AttributeError):
                weapon = attacker.active_weapon

        # Try to set the hitgroup
        with suppress(AttributeError):
            self.hitgroup = hitgroup

        # Get a TakeDamageInfo instance
        take_damage_info = TakeDamageInfo()

        # Is there a valid weapon?
        if weapon is not None:

            # Is the weapon a projectile?
            if weapon.classname in _projectile_weapons:

                # Set the inflictor to the weapon's index
                take_damage_info.inflictor = weapon.index

            # Is the weapon not a projectile and the attacker is valid?
            elif attacker_index is not None:

                # Set the inflictor to the attacker's index
                take_damage_info.inflictor = attacker_index

            # Set the weapon to the weapon's index
            take_damage_info.weapon = weapon.index

        # Is the attacker valid?
        if attacker_index is not None:

            # Set the attacker to the attacker's index
            take_damage_info.attacker = attacker_index

        # Set the damage amount
        take_damage_info.damage = damage

        # Set the damage type value
        take_damage_info.type = damage_type

        # Loop through the given keywords
        for item in kwargs:

            # Set the offset's value
            setattr(take_damage_info, item, kwargs[item])

        if skip_hooks:
            self.on_take_damage.skip_hooks(take_damage_info)
        else:
            self.on_take_damage(take_damage_info)
Esempio n. 8
0
    def take_damage(
            self, damage, damage_type=DamageTypes.GENERIC, attacker_index=None,
            weapon_index=None, hitgroup=HitGroup.GENERIC, skip_hooks=False,
            **kwargs):
        """Method used to hurt the entity with the given arguments."""
        # Import Entity classes
        # Doing this in the global scope causes cross import errors
        from weapons.entity import Weapon

        # Is the game supported?
        if not hasattr(self, 'on_take_damage'):

            # Raise an error if not supported
            raise NotImplementedError(
                '"take_damage" is not implemented for {0}'.format(GAME_NAME))

        # Store values for later use
        attacker = None
        weapon = None

        # Was an attacker given?
        if attacker_index is not None:

            # Try to get the Entity instance of the attacker
            with suppress(ValueError):
                attacker = Entity(attacker_index)

        # Was a weapon given?
        if weapon_index is not None:

            # Try to get the Weapon instance of the weapon
            with suppress(ValueError):
                weapon = Weapon(weapon_index)

        # Is there a weapon but no attacker?
        if attacker is None and weapon is not None:

            # Try to get the attacker based off of the weapon's owner
            with suppress(ValueError, OverflowError):
                attacker_index = index_from_inthandle(weapon.current_owner)
                attacker = Entity(attacker_index)

        # Is there an attacker but no weapon?
        if attacker is not None and weapon is None:

            # Try to use the attacker's active weapon
            with suppress(AttributeError, ValueError, OverflowError):
                weapon = Weapon(index_from_inthandle(attacker.active_weapon))

        # Try to set the hitgroup
        with suppress(AttributeError):
            self.hitgroup = hitgroup

        # Get a TakeDamageInfo instance
        take_damage_info = TakeDamageInfo()

        # Is there a valid weapon?
        if weapon is not None:

            # Is the weapon a projectile?
            if weapon.classname in _projectile_weapons:

                # Set the inflictor to the weapon's index
                take_damage_info.inflictor = weapon.index

            # Is the weapon not a projectile and the attacker is valid?
            elif attacker_index is not None:

                # Set the inflictor to the attacker's index
                take_damage_info.inflictor = attacker_index

            # Set the weapon to the weapon's index
            take_damage_info.weapon = weapon.index

        # Is the attacker valid?
        if attacker_index is not None:

            # Set the attacker to the attacker's index
            take_damage_info.attacker = attacker_index

        # Set the damage amount
        take_damage_info.damage = damage

        # Set the damage type value
        take_damage_info.type = damage_type

        # Loop through the given keywords
        for item in kwargs:

            # Set the offset's value
            setattr(take_damage_info, item, kwargs[item])

        if skip_hooks:
            self.on_take_damage.skip_hooks(take_damage_info)
        else:
            self.on_take_damage(take_damage_info)