Weaponlib

From EventScripts Community Encyclopedia


Module: weaponlib

Module Overview

EventScripts version: 2.0.0.250a or later

Table of Contents

Contents


Description

Provides weapon information commonly needed for each weapon and eliminates weapon name ambiguity (meaning you no longer need to remember whether a function returns famas or weapon_famas).

Weapons returned by weaponlib are instances of weaponlib.WeaponManager.Weapon.

Quick Example

Querying weapon data and putting it to practical use:

""" Example of weapon attribute methods and string handling """
>>> import weaponlib
 
>>> glock = weaponlib.getWeapon('glock')
 
# Each method works for all attributes
>>> glock.get('clip')
7
>>> glock['clip']
7
>>> glock.clip
7
 
# Instances return weapon name when coerced to a string
>>> str(glock)
'weapon_glock'
### Show victims their killer's name, weapon, and weapon index ###
import weaponlib
 
# This should work on any game
def player_death(event_var):
   weapon   = weaponlib.getWeapon(event_var['weapon'])
   attacker = int(event_var['attacker'])
   # Ensure the weapon is recognized and the attacker is a valid userid
   if weapon and attacker:
      # Refil the attacker's ammo to max for the weapon used to kill the victim
      prop    = weapon.prop
      maxammo = weapon.maxammo
      # Ensure the prop and maxammo attributes returned usable values
      if prop and maxammo:
         es.setplayerprop(attacker, prop, maxammo)
 
      # Find the attacker's weapon index
      index  = 0
      handle = es.getplayerhandle(attacker)
      for x in weapon.indexlist:
         # When the attacker's handle matches the index handle we have found the attacker's weapon index
         if es.getindexprop(x, 'CBaseEntity.m_hOwnerEntity') == handle:
            index = x
            break
 
      es.tell(userid, 'You were killed by ' + event_var['es_attackername'] + ' with the ' + weapon + ' (ent index %s)' % index)
### Advanced example ###
import es
import weaponlib
 
 
def round_start(event_var):
   # Remove all idle primary weapons
   for index in weaponlib.getIndexList('#primary'):
      # If the weapon has no owner it is removed
      if es.getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == -1:
         es.server.cmd('es_xremove %s' % index)
 
 
def item_pickup(event_var):
   userid = int(event_var['userid'])
   handle = es.getplayerhandle(userid)
 
   # Remove idle weapons of the type just picked up
   for index in weaponlib.getIndexList(event_var['item']):
      if es.getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == -1:
         es.server.cmd('es_xremove %s' % index)
 
   # Gather a list of the player's weapons
   myweapons = []
   for weapon in weaponlib.getWeaponList('#all'):
      for index in weapon.indexlist:
         if es.getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == handle:
            myweapons.append(weapon)
            break
 
   # Show the player his or her weapons sorted by slot
   if myweapons:
      sorted_weapons = sorted(myweapons, key=lambda x: x.slot)
      es.tell(userid, 'Current weapons: ' + ', '.join(map(str, sorted_weapons)))
 
   else:
      es.tell(userid, 'You have no weapons')
 
 
def player_spawn(event_var):
   userid = int(event_var['userid'])
   handle = es.getplayerhandle(userid)
 
   usp = weaponlib.getWeapon('usp')
   # Loop through all usps to find the one belonging to the player
   for index in usp.indexlist:
      if es.getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == handle:
 
         # Remove the player's usp
         es.server.cmd('es_xremove %s' % index)
 
         # Set the player's usp ammo to 0
         es.setplayerprop(userid, usp.prop, 0)
 
         # Stop looping
         break
 
   glock = weaponlib.getWeapon('glock')
   # Loop through all glocks to find the one belonging to the player
   for index in glock.indexlist:
      if es.getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == handle:
         # Make the player drop the glock
         es.sexec(userid, 'use weapon_glock')
         es.sexec(userid, 'drop')
 
         # Set the player's glock ammo to max
         es.setplayerprop(userid, glock.prop, glock.maxammo)
 
         # Stop looping
         break
 
   # Loop though each primary weapon and set the player's ammo to the number of bullets in one clip
   for weapon in weaponlib.getWeaponList('#primary'):
      for index in weapon.indexlist:
         if es.getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == handle:
            es.setplayerprop(userid, weapon.prop, weapon.clip)
            return

Module Content

Classes

Methods

getWeapon(name)
Returns a weaponlib.WeaponManager.Weapon instance for the name specified
getWeaponList(tag='#all')
Returns a list of weaponlib.WeaponManager.Weapon instances that have the specified tag (only one tag allowed). When a weapon name is used the list return contains only the weapon return from weaponlib.getWeapon(tag).
getWeaponNameList(tag='#all')
Returns the classname (entity name) of weapons returned by weaponlib.getWeaponList(tag)
getIndexList(tag='#all')
Returns a list of all entity indexes of the weapons returned by weaponlib.getWeaponList(tag)
xgetIndexList(tag='#all')
Works the same as weaponlib.getIndexList except it returns a weaponlib.WeaponManager.IndexIter instance that compiles the entity index list while being iterated over. This function is ideal for looping through indexes to find a single index with a specific attribute.
getWeaponIndexList(tag='#all')
Returns a list of tuples containing weapon indexes and corresponding weapon instances (index, instance).

See Also

Notes

  • All exposed instances of weaponlib.WeaponManager have the same methods as the weaponlib module to allow easy access to weapon information for a specific game.
  • weaponlib.currentgame, which holds the weaponlib.WeaponManager instance for the game the server is running, is used internally by the weaponlib methods above. There is little reason to use the instance directly.
  • Available weapon tags are:
    • cstrike: '#all', '#explosive', '#grenade', '#knife', '#machinegun', '#melee', '#objective', '#pistol', '#primary', '#rifle', '#secondary', '#shotgun', '#smg', '#sniper'
    • hl2mp: '#all', '#explosive', '#grenade', '#hand', '#melee', '#pistol', '#rocket', '#shotgun', '#smg', '#sniper', '#tool'
    • dod: '#all', '#bazooka', '#explosive', '#grenade', '#hand', '#machinegun', '#melee', '#pistol', '#primary', '#rifle', '#secondary', '#smg', '#sniper'
    • tf: '#all', '#demoman', '#engineer', '#explosive', '#flame', '#heavy', '#machinegun', '#medic', '#melee', '#pistol', '#primary', '#pyro', '#scout', '#secondary', '#shotgun', '#sniper', '#soldier', '#spy', '#tool'
    • ageofchivalry: '#agatha_knights', '#all', '#axe', '#bow', '#crossbowman', '#crusader', '#dagger', '#explosive', '#flail', '#flamberge', '#guardsman', '#halberd', '#heavy_knight', '#javelineer', '#knight', '#longbowman', '#mace', '#man_at_arms', '#mason_order', '#melee', '#ranged', '#sergeant', '#shield', '#spear', '#sword', '#warhammer'
    • bg2: '#all', '#america', '#britain', '#commander', '#frontiersman', '#infantry', '#jaeger', '#officer', '#pistol', '#rifle', '#soldier', '#sword'
    • dystopia: '#all', '#grenade', '#heavy', '#light', '#medium', '#melee', '#primary', '#secondary'
    • empires: '#all', '#empire', '#engineer', '#explosive', '#explosive,', '#faction', '#grenade', '#grenadier', '#gun', '#machinegun', '#mine', '#pistol', '#primary', '#rifle', '#rifleman', '#rocket', '#scout', '#secondary', '#smg', '#tool'
    • esmod: '#all', '#hacker', '#heavy', '#marine', '#ngm', '#primary', '#secondary', '#tool', '#utf'
    • gesource: '#all', '#explosive', '#grenade', '#knife', '#melee', '#mine', '#pistol', '#primary', '#rifle', '#secondary', '#shotgun', '#smg', '#sniper', '#thrown'
    • hidden: '#all', '#explosive', '#gun', '#hidden', '#iris', '#melee', '#pistol', '#primary', '#secondary'
    • hl2ctf: '#all', '#explosive', '#grenade', '#hand', '#melee', '#pistol', '#rocket', '#shotgun', '#smg', '#sniper', '#tool'
    • pvkii: '#all', '#archer', '#axe', '#beserker', '#bird', '#bow', '#captain', '#explosive', '#gun', '#huscarl', '#knight', '#melee', '#primary', '#ranged', '#secondary', '#skirmisher', '#special', '#sword', '#tertiary'
    • smashball: '#all', '#explosive', '#forward', '#fullback', '#goalie', '#halfback', '#mine', '#primary', '#secondary'
    • so: '#all', '#explosive', '#grenade', '#melee', '#pistol', '#primary', '#rifle', '#rocket', '#secondary', '#shotgun', '#smg', '#sniper', '#survivor', '#tool', '#zombie'
    • sourceforts: '#all', '#engineer', '#explosive', '#grenade', '#hand', '#melee', '#pistol', '#rocket', '#rocketeer', '#scout', '#shotgun', '#smg', '#sniper', '#soldier', '#tool'
    • synergy: '#all', '#explosive', '#grenade', '#hand', '#melee', '#pistol', '#rocket', '#shotgun', '#smg', '#sniper', '#tool'
    • zombie_master: '#all', '#grenade', '#gun', '#melee', '#pistol', '#primary', '#secondary'
    • zps: '#all', '#explosive', '#gun', '#grenade', '#hand', '#melee', '#pistol', '#rifle', '#shotgun', '#tool'
blog comments powered by Disqus