Re: [HL2:DM] Headshot Help
Posted: Thu Oct 20, 2016 2:52 am
so is there anything specific, that isnt working for you?
Syntax: Select all
from contextlib import suppress
from engines.precache import Model
from engines.sound import Sound
from entities import TakeDamageInfo
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook
from events import Event
from listeners.tick import Delay
from memory import make_object
from players.constants import HitGroup
from players.entity import Player
from stringtables.downloads import Downloadables
# =============================================================================
# >> CONFIGURATION
# =============================================================================
REMOVE_OVERLAY_TIME = 1.5
HEADSHOT_OVERLAY_VMT = "exae/rocksheadshot1.vmt"
HEADSHOT_OVERLAY_VTF = "exae/rocksheadshot1.vtf"
# Headshot but not dead sound + path
HEADSHOT_SOUND = 'exae/quake/human-hit-4.mp3'
# <weapon> : <headshot_sound>
WEAPON_FILES = {
'weapon_crossbow': 'exae/quake/headshot2.mp3',
'weapon_357' : 'exae/quake/headshot-2.mp3',
'weapon_smg1' : 'exae/quake/wife-headshot.mp3',
'weapon_ar2' : 'exae/quake/boomheadshot.mp3',
'weapon_pistol' : 'exae/quake/headshot-1.mp3',
'weapon_shotgun' : 'exae/quake/headshot2.mp3'
}
DEAD_MODEL_PATH = 'models/headless_body/metrocop.mdl'
DEAD_MODEL_DL_FILES = [
"models/headless_body/metrocop.dx80.vtx",
"models/headless_body/metrocop.dx90.vtx",
"models/headless_body/metrocop.mdl",
"models/headless_body/metrocop.phy",
"models/headless_body/metrocop.sw.vtx",
"models/headless_body/metrocop.vvd",
]
# =============================================================================
# >> END OF CONFIGURATION
# =============================================================================
HEADSHOT_SOUND = Sound(HEADSHOT_SOUND, download=True)
WEAPON_FILES = {x: Sound(y, download=True) for x, y in WEAPON_FILES.items()}
downloads = Downloadables()
for item in (HEADSHOT_OVERLAY_VMT, HEADSHOT_OVERLAY_VTF):
downloads.add('materials/' + item)
for item in DEAD_MODEL_DL_FILES:
downloads.add(item)
DEAD_MODEL = Model(
path=DEAD_MODEL_PATH,
preload=True,
)
_old_models = {}
_damage_weapons = {}
@EntityPreHook(EntityCondition.is_player, 'on_take_damage')
def _pre_take_damage(stack_data):
victim = make_object(Entity, stack_data[0])
if not victim.is_player():
return
victim = Player(victim.index)
info = make_object(TakeDamageInfo, stack_data[1])
# World damage?
if info.attacker == 0:
_damage_weapons[victim.userid] = 'worldspawn'
# Was a projectile used?
elif info.attacker != info.inflictor:
_damage_weapons[victim.userid] = Entity(info.inflictor).classname
# Not a projectile
else:
_damage_weapons[victim.userid] = Player(info.attacker).active_weapon.classname
@Event('player_hurt')
def player_hurt(game_event):
victim = Player.from_userid(game_event['userid'])
attacker_userid = game_event['attacker']
# Suicide?
if attacker_userid in (victim.userid, 0):
return
# Not a headshot?
if victim.hitgroup != HitGroup.HEAD:
return
# Victim still alive?
if game_event['health']:
HEADSHOT_SOUND.index = victim.index
HEADSHOT_SOUND.play(victim.index)
return
weapon_sound = WEAPON_FILES.get(_damage_weapons.get(victim.userid))
if weapon_sound is None:
return
# Play weapon specific headshot sound
weapon_sound.index = victim.index
weapon_sound.play()
# Set the victim's overlay
victim.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, victim.userid)
# Set the attacker's overlay
attacker = Player.from_userid(attacker_userid)
attacker.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, attacker_userid)
# Set the victim's model
_old_models[victim.userid] = victim.model
victim.model = DEAD_MODEL
@Event('player_spawn')
def _reset_model(game_event):
userid = game_event['userid']
old_model = _old_models.pop(userid, None)
if old_model is not None:
Player.from_userid(userid).model = old_model
def _remove_overlay(userid):
with suppress(Exception):
Player.from_userid(userid).client_command('r_screenoverlay 0')
Code: Select all
[SP] Caught an Exception:
Traceback (most recent call last):
File "../addons/source-python/plugins/headshot/headshot.py", line 86, in _pre_take_damage
_damage_weapons[victim.userid] = Player(info.attacker).active_weapon.classname
AttributeError: 'NoneType' object has no attribute 'classname'
Syntax: Select all
@EntityPreHook(EntityCondition.is_player, 'on_take_damage')
def _pre_take_damage(stack_data):
victim = make_object(Entity, stack_data[0])
if not victim.is_player():
return
victim = Player(victim.index)
info = make_object(TakeDamageInfo, stack_data[1])
try:
index = info.weapon
except ValueError:
index = WORLD_ENTITY_INDEX
_damage_weapons[victim.userid] = BaseEntity(index).classname
Syntax: Select all
from contextlib import suppress
from engines.precache import Model
from engines.sound import Sound
from entities import TakeDamageInfo
from entities.constants import WORLD_ENTITY_INDEX
from entities.entity import BaseEntity
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook
from events import Event
from listeners.tick import Delay
from memory import make_object
from players.constants import HitGroup
from players.entity import Player
from stringtables.downloads import Downloadables
# =============================================================================
# >> CONFIGURATION
# =============================================================================
REMOVE_OVERLAY_TIME = 1.5
HEADSHOT_OVERLAY_VMT = "exae/rocksheadshot1.vmt"
HEADSHOT_OVERLAY_VTF = "exae/rocksheadshot1.vtf"
# Headshot but not dead sound + path
HEADSHOT_SOUND = 'exae/quake/human-hit-4.mp3'
# <weapon> : <headshot_sound>
WEAPON_FILES = {
'weapon_crossbow': 'exae/quake/headshot2.mp3',
'weapon_357' : 'exae/quake/headshot-2.mp3',
'weapon_smg1' : 'exae/quake/wife-headshot.mp3',
'weapon_ar2' : 'exae/quake/boomheadshot.mp3',
'weapon_pistol' : 'exae/quake/headshot-1.mp3',
'weapon_shotgun' : 'exae/quake/headshot2.mp3'
}
DEAD_MODEL_PATH = 'models/headless_body/metrocop.mdl'
DEAD_MODEL_DL_FILES = [
"models/headless_body/metrocop.dx80.vtx",
"models/headless_body/metrocop.dx90.vtx",
"models/headless_body/metrocop.mdl",
"models/headless_body/metrocop.phy",
"models/headless_body/metrocop.sw.vtx",
"models/headless_body/metrocop.vvd",
]
# =============================================================================
# >> END OF CONFIGURATION
# =============================================================================
HEADSHOT_SOUND = Sound(HEADSHOT_SOUND, download=True)
WEAPON_FILES = {x: Sound(y, download=True) for x, y in WEAPON_FILES.items()}
downloads = Downloadables()
for item in (HEADSHOT_OVERLAY_VMT, HEADSHOT_OVERLAY_VTF):
downloads.add('materials/' + item)
for item in DEAD_MODEL_DL_FILES:
downloads.add(item)
DEAD_MODEL = Model(
path=DEAD_MODEL_PATH,
preload=True,
)
_old_models = {}
_damage_weapons = {}
@EntityPreHook(EntityCondition.is_player, 'on_take_damage')
def _pre_take_damage(stack_data):
victim = make_object(Entity, stack_data[0])
if not victim.is_player():
return
victim = Player(victim.index)
info = make_object(TakeDamageInfo, stack_data[1])
try:
index = info.weapon
except ValueError:
index = WORLD_ENTITY_INDEX
_damage_weapons[victim.userid] = BaseEntity(index).classname
@Event('player_hurt')
def player_hurt(game_event):
victim = Player.from_userid(game_event['userid'])
attacker_userid = game_event['attacker']
# Suicide?
if attacker_userid in (victim.userid, 0):
return
# Not a headshot?
if victim.hitgroup != HitGroup.HEAD:
return
# Victim still alive?
if game_event['health']:
HEADSHOT_SOUND.index = victim.index
HEADSHOT_SOUND.play(victim.index)
return
weapon_sound = WEAPON_FILES.get(_damage_weapons.get(victim.userid))
if weapon_sound is None:
return
# Play weapon specific headshot sound
weapon_sound.index = victim.index
weapon_sound.play()
# Set the victim's overlay
victim.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT
)
)
victim.delay(REMOVE_OVERLAYTIME, victim.client_command, 'r_screenoverlay 0')
# Set the attacker's overlay
attacker = Player.from_userid(attacker_userid)
attacker.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT
)
)
victim.delay(REMOVE_OVERLAYTIME, attacker.client_command, 'r_screenoverlay 0')
# Set the victim's model
_old_models[victim.userid] = victim.model
victim.model = DEAD_MODEL
@Event('player_spawn')
def _reset_model(game_event):
userid = game_event['userid']
old_model = _old_models.pop(userid, None)
if old_model is not None:
Player.from_userid(userid).model = old_model
Syntax: Select all
from contextlib import suppress
from engines.precache import Model
from engines.sound import Sound
from entities import TakeDamageInfo
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook
from events import Event
from listeners.tick import Delay
from memory import make_object
from players.constants import HitGroup
from players.entity import Player
from stringtables.downloads import Downloadables
# =============================================================================
# >> CONFIGURATION
# =============================================================================
REMOVE_OVERLAY_TIME = 1.5
HEADSHOT_OVERLAY_VMT = "exae/rocksheadshot1.vmt"
HEADSHOT_OVERLAY_VTF = "exae/rocksheadshot1.vtf"
# Headshot but not dead sound + path
HEADSHOT_SOUND = 'exae/quake/human-hit-4.mp3'
# <weapon> : <headshot_sound>
WEAPON_FILES = {
'weapon_crossbow': 'exae/quake/headshot2.mp3',
'weapon_357' : 'exae/quake/headshot-2.mp3',
'weapon_smg1' : 'exae/quake/wife-headshot.mp3',
'weapon_ar2' : 'exae/quake/boomheadshot.mp3',
'weapon_pistol' : 'exae/quake/headshot-1.mp3',
'weapon_shotgun' : 'exae/quake/headshot2.mp3'
}
DEAD_MODEL_PATH = 'models/headless_body/metrocop.mdl'
DEAD_MODEL_DL_FILES = [
"models/headless_body/metrocop.dx80.vtx",
"models/headless_body/metrocop.dx90.vtx",
"models/headless_body/metrocop.mdl",
"models/headless_body/metrocop.phy",
"models/headless_body/metrocop.sw.vtx",
"models/headless_body/metrocop.vvd",
]
# =============================================================================
# >> END OF CONFIGURATION
# =============================================================================
HEADSHOT_SOUND = Sound(HEADSHOT_SOUND, download=True)
WEAPON_FILES = {x: Sound(y, download=True) for x, y in WEAPON_FILES.items()}
downloads = Downloadables()
for item in (HEADSHOT_OVERLAY_VMT, HEADSHOT_OVERLAY_VTF):
downloads.add('materials/' + item)
for item in DEAD_MODEL_DL_FILES:
downloads.add(item)
DEAD_MODEL = Model(
path=DEAD_MODEL_PATH,
preload=True,
)
_old_models = {}
_damage_weapons = {}
@EntityPreHook(EntityCondition.is_player, 'on_take_damage')
def _pre_take_damage(stack_data):
victim = make_object(Entity, stack_data[0])
if not victim.is_player():
return
victim = Player(victim.index)
info = make_object(TakeDamageInfo, stack_data[1])
# World damage?
if info.attacker == 0:
_damage_weapons[victim.userid] = 'worldspawn'
# Was a projectile used?
elif info.attacker != info.inflictor:
_damage_weapons[victim.userid] = Entity(info.inflictor).classname
# Not a projectile
else:
_damage_weapons[victim.userid] = Player(info.attacker).active_weapon.classname
@Event('player_hurt')
def player_hurt(game_event):
victim = Player.from_userid(game_event['userid'])
attacker_userid = game_event['attacker']
# Suicide?
if attacker_userid in (victim.userid, 0):
return
# Not a headshot?
if victim.hitgroup != HitGroup.HEAD:
return
# Victim still alive?
if game_event['health']:
HEADSHOT_SOUND.index = victim.index
HEADSHOT_SOUND.play(victim.index)
return
weapon_sound = WEAPON_FILES.get(_damage_weapons.get(victim.userid))
if weapon_sound is None:
return
# Play weapon specific headshot sound
weapon_sound.index = victim.index
weapon_sound.play()
# Set the victim's overlay
victim.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, victim.userid)
# Set the attacker's overlay
attacker = Player.from_userid(attacker_userid)
attacker.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, attacker_userid)
# Set the victim's model
_old_models[victim.userid] = victim.model
victim.model = DEAD_MODEL
@Event('player_spawn')
def _reset_model(game_event):
userid = game_event['userid']
old_model = _old_models.pop(userid, None)
if old_model is not None:
Player.from_userid(userid).model = old_model
def _remove_overlay(userid):
with suppress(Exception):
Player.from_userid(userid).client_command('r_screenoverlay 0')
Syntax: Select all
from contextlib import suppress
from engines.precache import Model
from engines.sound import Sound
from entities import TakeDamageInfo
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook
from events import Event
from listeners.tick import Delay
from memory import make_object
from players.constants import HitGroup
from players.entity import Player
from stringtables.downloads import Downloadables
# =============================================================================
# >> CONFIGURATION
# =============================================================================
REMOVE_OVERLAY_TIME = 1.5
HEADSHOT_OVERLAY_VMT_ATTACKER = "exae/rocksheadshot1.vmt"
HEADSHOT_OVERLAY_VTF_ATTACKER = "exae/rocksheadshot1.vtf"
HEADSHOT_OVERLAY_VMT_VICTIM = "exae/rocksheadshot1.vmt"
HEADSHOT_OVERLAY_VTF_VICTIM = "exae/rocksheadshot1.vtf"
# Headshot but not dead sound + path
HEADSHOT_SOUND = 'exae/quake/human-hit-4.mp3'
# <weapon> : <headshot_sound>
WEAPON_FILES = {
'weapon_crossbow': 'exae/quake/headshot2.mp3',
'weapon_357' : 'exae/quake/headshot-2.mp3',
'weapon_smg1' : 'exae/quake/wife-headshot.mp3',
'weapon_ar2' : 'exae/quake/boomheadshot.mp3',
'weapon_pistol' : 'exae/quake/headshot-1.mp3',
'weapon_shotgun' : 'exae/quake/headshot2.mp3'
}
DEAD_MODEL_PATH = 'models/headless_body/metrocop.mdl'
DEAD_MODEL_DL_FILES = [
"models/headless_body/metrocop.dx80.vtx",
"models/headless_body/metrocop.dx90.vtx",
"models/headless_body/metrocop.mdl",
"models/headless_body/metrocop.phy",
"models/headless_body/metrocop.sw.vtx",
"models/headless_body/metrocop.vvd",
]
# =============================================================================
# >> END OF CONFIGURATION
# =============================================================================
HEADSHOT_SOUND = Sound(HEADSHOT_SOUND, download=True)
WEAPON_FILES = {x: Sound(y, download=True) for x, y in WEAPON_FILES.items()}
downloads = Downloadables()
for item in (HEADSHOT_OVERLAY_VMT_ATTACKER, HEADSHOT_OVERLAY_VTF_ATTACKER,
HEADSHOT_OVERLAY_VMT_VICTIM, HEADSHOT_OVERLAY_VTF_VICTIM):
downloads.add('materials/' + item)
for item in DEAD_MODEL_DL_FILES:
downloads.add(item)
DEAD_MODEL = Model(
path=DEAD_MODEL_PATH,
preload=True,
)
_old_models = {}
_damage_weapons = {}
@EntityPreHook(EntityCondition.is_player, 'on_take_damage')
def _pre_take_damage(stack_data):
victim = make_object(Entity, stack_data[0])
if not victim.is_player():
return
victim = Player(victim.index)
info = make_object(TakeDamageInfo, stack_data[1])
# World damage?
if info.attacker == 0:
_damage_weapons[victim.userid] = 'worldspawn'
# Was a projectile used?
elif info.attacker != info.inflictor:
_damage_weapons[victim.userid] = Entity(info.inflictor).classname
# Not a projectile
else:
_damage_weapons[victim.userid] = Player(info.attacker).active_weapon.classname
@Event('player_hurt')
def player_hurt(game_event):
victim = Player.from_userid(game_event['userid'])
attacker_userid = game_event['attacker']
# Suicide?
if attacker_userid in (victim.userid, 0):
return
# Not a headshot?
if victim.hitgroup != HitGroup.HEAD:
return
# Victim still alive?
if game_event['health']:
HEADSHOT_SOUND.index = victim.index
HEADSHOT_SOUND.play(victim.index)
return
weapon_sound = WEAPON_FILES.get(_damage_weapons.get(victim.userid))
if weapon_sound is None:
return
attacker = Player.from_userid(attacker_userid)
# Play weapon specific headshot sound
weapon_sound.play(attacker.index)
# Set the victim's overlay
victim.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT_VICTIM
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, [victim.userid])
# Set the attacker's overlay
attacker.client_command(
'r_screenoverlay {overlay}'.format(
overlay=HEADSHOT_OVERLAY_VMT_ATTACKER
)
)
Delay(REMOVE_OVERLAY_TIME, _remove_overlay, [attacker_userid])
# Set the victim's model
_old_models[victim.userid] = victim.model
victim.model = DEAD_MODEL
@Event('player_spawn')
def _reset_model(game_event):
userid = game_event['userid']
old_model = _old_models.pop(userid, None)
if old_model is not None:
Player.from_userid(userid).model = old_model
def _remove_overlay(userid):
with suppress(Exception):
Player.from_userid(userid).client_command('r_screenoverlay 0')
Code: Select all
2017-02-20 19:01:30 - sp - EXCEPTION
[SP] Caught an Exception:
Traceback (most recent call last):
File "../addons/source-python/plugins/headshot/headshot.py", line 98, in _pre_take_damage
_damage_weapons[victim.userid] = Player(info.attacker).active_weapon.classname
File "../addons/source-python/packages/source-python/entities/_base.py", line 116, in __getattr__
raise AttributeError('Attribute "{0}" not found'.format(attr))
AttributeError: Attribute "active_weapon" not found
Syntax: Select all
2019-05-26 22:12:09 - sp - EXCEPTION
[SP] Caught an Exception:
Traceback (most recent call last):
File "..\addons\source-python\plugins\explosive\explosive.py", line 21, in _pre_take_damage
weapon_index = info.weapon
ValueError: Attacker is not a player.
2019-05-26 22:12:09 - sp - EXCEPTION
[SP] Caught an Exception:
Traceback (most recent call last):
File "..\addons\source-python\plugins\headshot\headshot.py", line 97, in _pre_take_damage
_damage_weapons[victim.userid] = Player(info.attacker).active_weapon.classname
File "..\addons\source-python\packages\source-python\players\_base.py", line 88, in __init__
super().__init__(index)
File "..\addons\source-python\packages\source-python\entities\_base.py", line 94, in __init__
super().__init__(index)
ValueError: Index '692' is not a valid player.
2019-05-26 22:12:09 - sp - EXCEPTION
[SP] Caught an Exception:
Traceback (most recent call last):
File "..\addons\source-python\plugins\blood\blood.py", line 22, in _pre_take_damage
weapon_index = info.weapon
ValueError: Attacker is not a player.