def remove_weapon(index): """Safely remove a weapon entity.""" with contextlib.suppress(ValueError): weapon = Weapon(index) if weapon.owner is None: weapon.remove()
def remove_weapon(weapon_index): """Remove a weapon entity from the server if it is still valid.""" with suppress(ValueError): weapon = Weapon(weapon_index) if weapon.owner is None: weapon.remove()
def _pre_on_take_damage(args): """ Hooked to a function that is fired any time an entity takes damage. """ player_index = index_from_pointer(args[0]) info = make_object(TakeDamageInfo, args[1]) defender = Player(player_index) attacker = None if not info.attacker else Player(info.attacker) eargs = { 'attacker': attacker, 'defender': defender, 'info': info, 'weapon': Weapon(index_from_inthandle(attacker.active_weapon)).class_name if attacker and attacker.active_weapon != -1 else '' } if not player_index == info.attacker: defender.hero.execute_skills('player_pre_defend', **eargs) ''' Added exception to check whether world caused damage. ''' if attacker: attacker.hero.execute_skills('player_pre_attack', **eargs)
def _drop_command(command, index): """Throw melee weapon on 'drop'.""" player = Player(index) class_name = getattr(player.active_weapon, 'classname', None) if class_name not in _melee_weapons: return True if not _available_count[player.userid]: TextMsg(MESSAGE_STRINGS['Empty']).send(index) return _available_count[player.userid] -= 1 TextMsg(MESSAGE_STRINGS['Remaining']).send( index, current=_available_count[player.userid], total=int(total_max), ) start_location = player.eye_location velocity = player.view_coordinates - start_location weapon = Weapon.create(class_name) weapon.spawn() weapon.teleport(origin=start_location, velocity=velocity * 200) _throwers[weapon.index] = player.userid delay = int(remove_delay) weapon.delay(delay, weapon.remove)
def restore_weapons(self): self.strip() for weapon_dict in self.saved_weapons: weapon = Weapon.create(weapon_dict['classname']) weapon.teleport(self.player.origin, None, None) weapon.spawn() weapon.clip = weapon_dict['clip'] if weapon_dict['ammo'] is not None: weapon.ammo = weapon_dict['ammo']
def weapons(self, classname=None, is_filters=None, not_filters=None): """Iterate over the player's weapons for the given arguments. :return: A generator of :class:`weapons.entity.Weapon` objects :rtype: generator """ # Loop through all the players weapons for the given arguments for index in self.weapon_indexes(classname, is_filters, not_filters): # Yield the current weapon yield Weapon(index)
def get_active_weapon(self): """Return the player's active weapon. :return: None if the player does not have an active weapon. :rtype: Weapon """ try: index = index_from_inthandle(self.active_weapon_handle) except (ValueError, OverflowError): return None return Weapon(index)
def max_ammo(self, weapon_classnames): for index in self.player.weapon_indexes(): weapon_classname = edict_from_index(index).classname if weapon_classname not in weapon_classnames: continue if weapon_classname in PROJECTILE_CLASSNAMES: continue weapon_class = weapon_manager[weapon_classname] if weapon_class.maxammo > 0: Weapon(index).ammo = weapon_class.maxammo
def restore_weapons(self): self.strip() for weapon_dict in self.saved_weapons: weapon = Weapon.create(weapon_dict['classname']) weapon.teleport(self.player.origin, None, None) weapon.spawn() if weapon_dict['clip'] is not None: weapon.clip = weapon_dict['clip'] if weapon_dict['ammo'] is not None: weapon.ammo = weapon_dict['ammo']
def on_player_restriction_added(self, player, weapon): """Notify the handler if the player is carrying the weapon. :param Player player: The player that just had a restriction added. :param str weapon: The weapon that was just restricted. """ # Does the player own the weapon type? weapon_index = self._get_player_weapon_index(player, weapon) if weapon_index is INVALID_ENTITY_INDEX: return # Notify the player is carrying the weapon self.on_player_carrying_restricted_weapon(player, Weapon(weapon_index))
def iterator(): """Iterate over all :class:`weapons.entity.Weapon` objects.""" # Import the Weapon class. # This is done here to avoid circular imports from weapons.entity import Weapon # Loop through all entities on the server for edict in EntityGenerator(): # Is the entity a weapon? if edict.classname in weapon_manager: # Yield the Weapon instance for the current edict yield Weapon(index_from_edict(edict))
def _pre_bump_weapon(args): """ Hooked to a function that is fired any time a weapon is requested to be picked up in game. """ player_index = index_from_pointer(args[0]) weapon_index = index_from_pointer(args[1]) weapon = Weapon(weapon_index) player = Player(player_index) eargs = {'weapon': weapon, 'player': player} if weapon.classname in player.restrictions: player.hero.execute_skills('weapon_pickup_fail', **eargs) return False else: player.hero.execute_skills('weapon_pickup', **eargs)
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): """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)
def _pre_bump_weapon(args): """Prevent the weapon bump if the weapon is restricted.""" player = EasyPlayer(index_from_pointer(args[0])) weapon = Weapon(index_from_pointer(args[1])) if weapon.classname in player.restrictions: return False