Advertisement
KirillMysnik

Untitled

Oct 26th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. from contextlib import suppress
  2.  
  3. from engines.precache import Model
  4. from engines.sound import Sound
  5. from entities import TakeDamageInfo
  6. from entities.entity import Entity
  7. from entities.hooks import EntityCondition, EntityPreHook
  8. from events import Event
  9. from listeners.tick import Delay
  10. from memory import make_object
  11. from players.constants import HitGroup
  12. from players.entity import Player
  13. from stringtables.downloads import Downloadables
  14.  
  15.  
  16. # =============================================================================
  17. # >> CONFIGURATION
  18. # =============================================================================
  19. REMOVE_OVERLAY_TIME = 1.5
  20. HEADSHOT_OVERLAY_VMT = "exae/rocksheadshot1.vmt"
  21. HEADSHOT_OVERLAY_VTF = "exae/rocksheadshot1.vtf"
  22.  
  23. # Headshot but not dead sound + path
  24. HEADSHOT_SOUND = 'exae/quake/human-hit-4.mp3'
  25.  
  26. # <weapon> : <headshot_sound>
  27. WEAPON_FILES = {    
  28.     'weapon_crossbow': 'exae/quake/headshot2.mp3',
  29.     'weapon_357' : 'exae/quake/headshot-2.mp3',
  30.     'weapon_smg1' : 'exae/quake/wife-headshot.mp3',
  31.     'weapon_ar2' : 'exae/quake/boomheadshot.mp3',
  32.     'weapon_pistol' : 'exae/quake/headshot-1.mp3',
  33.     'weapon_shotgun' : 'exae/quake/headshot2.mp3'
  34. }
  35.  
  36. DEAD_MODEL_PATH = 'models/headless_body/metrocop.mdl'
  37. DEAD_MODEL_DL_FILES = [
  38.     "models/headless_body/metrocop.dx80.vtx",
  39.     "models/headless_body/metrocop.dx90.vtx",
  40.     "models/headless_body/metrocop.mdl",
  41.     "models/headless_body/metrocop.phy",
  42.     "models/headless_body/metrocop.sw.vtx",
  43.     "models/headless_body/metrocop.vvd",
  44. ]
  45. # =============================================================================
  46. # >> END OF CONFIGURATION
  47. # =============================================================================
  48.  
  49.  
  50. HEADSHOT_SOUND = Sound(HEADSHOT_SOUND, download=True)
  51. WEAPON_FILES = {x: Sound(y, download=True) for x, y in WEAPON_FILES.items()}
  52. downloads = Downloadables()
  53. for item in (HEADSHOT_OVERLAY_VMT, HEADSHOT_OVERLAY_VTF):
  54.     downloads.add('materials/' + item)
  55. for item in DEAD_MODEL_DL_FILES:
  56.     downloads.add(item)
  57.  
  58. DEAD_MODEL = Model(
  59.     path=DEAD_MODEL_PATH,
  60.     preload=True,
  61. )
  62.  
  63. _old_models = {}
  64. _damage_weapons = {}
  65.  
  66.  
  67. @EntityPreHook(EntityCondition.is_player, 'on_take_damage')
  68. def _pre_take_damage(stack_data):
  69.     victim = make_object(Entity, stack_data[0])
  70.     if not victim.is_player():
  71.         return
  72.  
  73.     victim = Player(victim.index)
  74.     info = make_object(TakeDamageInfo, stack_data[1])
  75.  
  76.     # World damage?
  77.     if info.attacker == 0:
  78.         _damage_weapons[victim.userid] = 'worldspawn'
  79.  
  80.     # Was a projectile used?
  81.     elif info.attacker != info.inflictor:
  82.         _damage_weapons[victim.userid] = Entity(info.inflictor).classname
  83.  
  84.     # Not a projectile
  85.     else:
  86.         _damage_weapons[victim.userid] = Player(info.attacker).active_weapon.classname
  87.  
  88.  
  89. @Event('player_hurt')
  90. def player_hurt(game_event):
  91.     victim = Player.from_userid(game_event['userid'])
  92.     attacker_userid = game_event['attacker']
  93.  
  94.     # Suicide?
  95.     if attacker_userid in (victim.userid, 0):
  96.         return
  97.  
  98.     # Not a headshot?
  99.     if victim.hitgroup != HitGroup.HEAD:
  100.         return
  101.  
  102.     # Victim still alive?
  103.     if game_event['health']:
  104.         HEADSHOT_SOUND.index = victim.index
  105.         HEADSHOT_SOUND.play(victim.index)
  106.         return
  107.  
  108.     weapon_sound = WEAPON_FILES.get(_damage_weapons.get(victim.userid))
  109.     if weapon_sound is None:
  110.         return
  111.  
  112.     # Play weapon specific headshot sound
  113.     weapon_sound.index = victim.index
  114.     weapon_sound.play()
  115.  
  116.     # Set the victim's overlay
  117.     victim.client_command(
  118.         'r_screenoverlay {overlay}'.format(
  119.             overlay=HEADSHOT_OVERLAY_VMT
  120.         )
  121.     )
  122.     Delay(REMOVE_OVERLAY_TIME, _remove_overlay, victim.userid)
  123.  
  124.     # Set the attacker's overlay
  125.     attacker = Player.from_userid(attacker_userid)
  126.     attacker.client_command(
  127.         'r_screenoverlay {overlay}'.format(
  128.             overlay=HEADSHOT_OVERLAY_VMT
  129.         )
  130.     )
  131.     Delay(REMOVE_OVERLAY_TIME, _remove_overlay, attacker_userid)
  132.  
  133.     # Set the victim's model
  134.     _old_models[victim.userid] = victim.model
  135.     victim.model = DEAD_MODEL
  136.  
  137.  
  138. @Event('player_spawn')
  139. def _reset_model(game_event):
  140.     userid = game_event['userid']
  141.     old_model = _old_models.pop(userid, None)
  142.     if old_model is not None:
  143.         Player.from_userid(userid).model = old_model
  144.  
  145.  
  146. def _remove_overlay(userid):
  147.     with suppress(Exception):
  148.         Player.from_userid(userid).client_command('r_screenoverlay 0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement