Advertisement
FlacoBey

Untitled

Jul 1st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.47 KB | None | 0 0
  1. #pragma semicolon 1
  2. #pragma newdecls required
  3.  
  4. #include <sourcemod>
  5. #include <sdktools>
  6. #include <sdkhooks>
  7.  
  8. // Plugin Variables
  9. int g_iLightIndex[MAXPLAYERS+1], g_iPlayerEnum[MAXPLAYERS+1], g_iWeaponIndex[MAXPLAYERS+1], g_iFlashlight_Offset;
  10. float g_fTime[MAXPLAYERS+1];
  11.  
  12. enum ()
  13. {
  14.     ENUM_INCAPPED   = (1 << 0),
  15.     ENUM_INSTART    = (1 << 1),
  16.     ENUM_BLOCKED    = (1 << 2),
  17.     ENUM_POUNCED    = (1 << 3),
  18.     ENUM_ONLEDGE    = (1 << 4),
  19.     ENUM_INREVIVE   = (1 << 5),
  20.     ENUM_DISTANCE   = (1 << 6),
  21.     ENUM_BLOCK      = (1 << 7)
  22. }
  23.  
  24. public void OnPluginStart()
  25. {
  26.     g_iFlashlight_Offset = FindSendPropInfo("CTerrorPlayer", "m_fEffects");
  27.     HookEvents();
  28. }
  29.  
  30. public void OnPluginEnd()
  31. {
  32.     UnhookEvents();
  33.     for( int i = 1; i <= MaxClients; i++ )
  34.         DeleteLight(i);
  35. }
  36.  
  37. int GetColor(char[] sTemp)
  38. {
  39.     char sColors[3][4];
  40.     ExplodeString(sTemp, " ", sColors, 3, 4);
  41.  
  42.     int color;
  43.     color = StringToInt(sColors[0]);
  44.     color += 256 * StringToInt(sColors[1]);
  45.     color += 65536 * StringToInt(sColors[2]);
  46.     return color;
  47. }
  48.  
  49. // ====================================================================================================
  50. //                  EVENTS
  51. // ====================================================================================================
  52. void HookEvents()
  53. {
  54.     HookEvent("round_start",            Event_RoundStart,   EventHookMode_PostNoCopy);
  55.     HookEvent("player_ledge_grab",      Event_LedgeGrab);
  56.     HookEvent("player_spawn",           Event_PlayerSpawn);
  57.     HookEvent("revive_begin",           Event_ReviveStart);
  58.     HookEvent("revive_end",             Event_ReviveEnd);
  59.     HookEvent("revive_success",         Event_ReviveSuccess);
  60.     HookEvent("player_death",           Event_Unblock);
  61.     HookEvent("lunge_pounce",           Event_BlockHunter);
  62.     HookEvent("pounce_end",             Event_BlockEndHunt);
  63.     HookEvent("tongue_grab",            Event_BlockStart);
  64.     HookEvent("tongue_release",         Event_BlockEnd);
  65.     HookEvent("charger_pummel_start",   Event_BlockStart);
  66.     HookEvent("charger_carry_start",    Event_BlockStart);
  67.     HookEvent("charger_carry_end",      Event_BlockEnd);
  68.     HookEvent("charger_pummel_end",     Event_BlockEnd);
  69.     HookEvent("jockey_ride",            Event_BlockStart);
  70.     HookEvent("jockey_ride_end",        Event_BlockEnd);
  71. }
  72.  
  73. void UnhookEvents()
  74. {
  75.     UnhookEvent("round_start",          Event_RoundStart,   EventHookMode_PostNoCopy);
  76.     UnhookEvent("player_ledge_grab",    Event_LedgeGrab);
  77.     UnhookEvent("player_spawn",         Event_PlayerSpawn);
  78.     UnhookEvent("revive_begin",         Event_ReviveStart);
  79.     UnhookEvent("revive_end",           Event_ReviveEnd);
  80.     UnhookEvent("revive_success",       Event_ReviveSuccess);
  81.     UnhookEvent("player_death",         Event_Unblock);
  82.     UnhookEvent("lunge_pounce",         Event_BlockHunter);
  83.     UnhookEvent("pounce_end",           Event_BlockEndHunt);
  84.     UnhookEvent("tongue_grab",          Event_BlockStart);
  85.     UnhookEvent("tongue_release",       Event_BlockEnd);
  86.     UnhookEvent("charger_pummel_start",     Event_BlockStart);
  87.     UnhookEvent("charger_carry_start",      Event_BlockStart);
  88.     UnhookEvent("charger_carry_end",        Event_BlockEnd);
  89.     UnhookEvent("charger_pummel_end",       Event_BlockEnd);
  90.     UnhookEvent("jockey_ride",              Event_BlockStart);
  91.     UnhookEvent("jockey_ride_end",          Event_BlockEnd);
  92. }
  93.  
  94. public void Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
  95. {
  96.     for( int i = 1; i < MAXPLAYERS; i++ )
  97.         g_iPlayerEnum[i] = 0;
  98. }
  99.  
  100. public void Event_BlockUserEnd(Event event, const char[] name, bool dontBroadcast)
  101. {
  102.     int client = GetClientOfUserId(event.GetInt("victim"));
  103.     if( client > 0 )
  104.         g_iPlayerEnum[client] &= ~ENUM_BLOCKED;
  105. }
  106.  
  107. public void Event_BlockStart(Event event, const char[] name, bool dontBroadcast)
  108. {
  109.     int client = GetClientOfUserId(event.GetInt("victim"));
  110.     if( client > 0 )
  111.         g_iPlayerEnum[client] |= ENUM_BLOCKED;
  112. }
  113.  
  114. public void Event_BlockEnd(Event event, const char[] name, bool dontBroadcast)
  115. {
  116.     int client = GetClientOfUserId(event.GetInt("victim"));
  117.     if( client > 0 )
  118.         g_iPlayerEnum[client] &= ~ENUM_BLOCKED;
  119. }
  120.  
  121. public void Event_BlockHunter(Event event, const char[] name, bool dontBroadcast)
  122. {
  123.     int client = GetClientOfUserId(event.GetInt("victim"));
  124.     if( client > 0 )
  125.         g_iPlayerEnum[client] |= ENUM_POUNCED;
  126. }
  127.  
  128. public void Event_BlockEndHunt(Event event, const char[] name, bool dontBroadcast)
  129. {
  130.     int client = GetClientOfUserId(event.GetInt("victim"));
  131.     if( client > 0 )
  132.     {
  133.         g_fTime[client] = GetGameTime() - 1.0;
  134.         g_iPlayerEnum[client] &= ~ENUM_POUNCED;
  135.     }
  136. }
  137.  
  138. public void Event_LedgeGrab(Event event, const char[] name, bool dontBroadcast)
  139. {
  140.     int client = GetClientOfUserId(event.GetInt("userid"));
  141.     if( client > 0 )
  142.         g_iPlayerEnum[client] |= ENUM_ONLEDGE;
  143. }
  144.  
  145. public void Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
  146. {
  147.     int client = GetClientOfUserId(event.GetInt("userid"));
  148.     if( client > 0 )
  149.     {
  150.         g_iPlayerEnum[client] = 0;
  151.     }
  152. }
  153.  
  154. public void Event_ReviveStart(Event event, const char[] name, bool dontBroadcast)
  155. {
  156.     int client = GetClientOfUserId(event.GetInt("subject"));
  157.     if( client > 0 )
  158.         g_iPlayerEnum[client] |= ENUM_INREVIVE;
  159.  
  160.     client = GetClientOfUserId(event.GetInt("userid"));
  161.     if( client > 0 )
  162.         g_iPlayerEnum[client] |= ENUM_INREVIVE;
  163. }
  164.  
  165. public void Event_ReviveEnd(Event event, const char[] name, bool dontBroadcast)
  166. {
  167.     int client = GetClientOfUserId(event.GetInt("subject"));
  168.     if( client > 0 )
  169.         g_iPlayerEnum[client] &= ~ENUM_INREVIVE;
  170.  
  171.     client = GetClientOfUserId(event.GetInt("userid"));
  172.     if( client > 0 )
  173.         g_iPlayerEnum[client] &= ~ENUM_INREVIVE;
  174. }
  175.  
  176. public void Event_ReviveSuccess(Event event, const char[] name, bool dontBroadcast)
  177. {
  178.     int client = GetClientOfUserId(event.GetInt("subject"));
  179.     if( client > 0 )
  180.     {
  181.         g_iPlayerEnum[client] &= ~ENUM_INREVIVE;
  182.         g_iPlayerEnum[client] &= ~ENUM_ONLEDGE;
  183.     }
  184.  
  185.     client = GetClientOfUserId(event.GetInt("userid"));
  186.     if( client > 0 )
  187.         g_iPlayerEnum[client] &= ~ENUM_INREVIVE;
  188. }
  189.  
  190. public void Event_Unblock(Event event, const char[] name, bool dontBroadcast)
  191. {
  192.     int client = GetClientOfUserId(event.GetInt("userid"));
  193.     if( client > 0)
  194.         g_iPlayerEnum[client] = 0;
  195. }
  196.  
  197. // ====================================================================================================
  198. //                  DYNAMIC LIGHT ON/OFF
  199. // ====================================================================================================
  200. public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3], int &weapon)
  201. {
  202.    
  203.     int entity = g_iLightIndex[client];
  204.  
  205.     if( GetClientTeam(client) != 2 || !IsPlayerAlive(client) )
  206.     {
  207.         if( IsValidEntRef(entity) == true )
  208.             DeleteLight(client);
  209.  
  210.         return;
  211.     }
  212.  
  213.     if( IsValidEntRef(entity) == false )
  214.     {
  215.         entity = CreateLight(client);
  216.     }
  217.        
  218.     int index = g_iWeaponIndex[client];
  219.     int active = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
  220.  
  221.     if( index != active )
  222.     {
  223.         g_iWeaponIndex[client] = active;
  224.  
  225.         if( active == -1  )
  226.         {
  227.             g_iPlayerEnum[client] |= ENUM_BLOCK;
  228.         }
  229.         else
  230.         {
  231.             char sTemp[32];
  232.             GetClientWeapon(client, sTemp, sizeof(sTemp));
  233.  
  234.             if( strcmp(sTemp, "weapon_melee") == 0 ||
  235.                 strcmp(sTemp, "weapon_chainsaw") == 0 ||
  236.                 strcmp(sTemp, "weapon_vomitjar") == 0 ||
  237.                 strcmp(sTemp, "weapon_pipe_bomb") == 0 ||
  238.                 strcmp(sTemp, "weapon_defibrillator") == 0 ||
  239.                 strcmp(sTemp, "weapon_first_aid_kit") == 0 ||
  240.                 strcmp(sTemp, "weapon_upgradepack_explosive") == 0 ||
  241.                 strcmp(sTemp, "weapon_upgradepack_incendiary") == 0 ||
  242.                 strcmp(sTemp, "weapon_first_aid_kit") == 0 ||
  243.                 strcmp(sTemp, "weapon_pain_pills") == 0 ||
  244.                 strcmp(sTemp, "weapon_adrenaline") == 0 ||
  245.                 strcmp(sTemp, "cola_bottles") == 0 ||
  246.                 strcmp(sTemp, "weapon_fireworkcrate") == 0 ||
  247.                 strcmp(sTemp, "weapon_gascan") == 0 ||
  248.                 strcmp(sTemp, "weapon_gnome") == 0 ||
  249.                 strcmp(sTemp, "weapon_oxygentank") == 0 ||
  250.                 strcmp(sTemp, "weapon_propanetank") == 0
  251.             )
  252.                 g_iPlayerEnum[client] |= ENUM_BLOCK;
  253.             else
  254.                 g_iPlayerEnum[client] &= ~ENUM_BLOCK;
  255.         }
  256.     }
  257.  
  258.     int playerenum = g_iPlayerEnum[client];
  259.     if(playerenum == 0 && IsValidEntity(entity))
  260.     {
  261.         if(GetFlashlightState(client) == 4)
  262.         {
  263.             AcceptEntityInput(entity, "TurnOn");
  264.             TeleportDynamicLight(client, entity);
  265.         }
  266.         else
  267.             AcceptEntityInput(entity, "TurnOff");
  268.  
  269.     }
  270. }
  271.  
  272. static int GetFlashlightState(int client)
  273. {
  274.     return GetEntData(client, g_iFlashlight_Offset);
  275. }
  276.  
  277. void DeleteLight(int client)
  278. {
  279.     int entity = g_iLightIndex[client];
  280.     g_iLightIndex[client] = 0;
  281.  
  282.     if(IsValidEntRef(entity) )
  283.     {
  284.         AcceptEntityInput(entity, "Kill");
  285.     }
  286. }
  287.  
  288. int CreateLight(int client)
  289. {
  290.     int entity = g_iLightIndex[client];
  291.     if( IsValidEntRef(entity) )
  292.         return 0;
  293.  
  294.     entity = CreateEntityByName("light_dynamic");
  295.     if( entity == -1)
  296.     {
  297.         LogError("Failed to create 'light_dynamic'");
  298.         return 0;
  299.     }
  300.    
  301.     char sColor[12];
  302.     Format(sColor, sizeof sColor, "200 225 255");
  303.    
  304.     DispatchKeyValue(entity, "brightness", "1");
  305.     DispatchKeyValueFloat(entity, "spotlight_radius", 32.0);
  306.     DispatchKeyValueFloat(entity, "distance", 125.0);
  307.     DispatchKeyValue(entity, "style", "0");
  308.     SetEntProp(entity, Prop_Send, "m_clrRender", GetColor(sColor));
  309.     DispatchSpawn(entity);
  310.     AcceptEntityInput(entity, "TurnOn");
  311.  
  312.     g_iLightIndex[client] = EntIndexToEntRef(entity);
  313.     return entity;
  314. }
  315.  
  316. void TeleportDynamicLight(int client, int entity)
  317. {
  318.     float vLoc[3], vPos[3], vAng[3];
  319.  
  320.     GetClientEyeAngles(client, vAng);
  321.     GetClientEyePosition(client, vLoc);
  322.  
  323.     Handle trace;
  324.     float vDir[3], vEnd[3];
  325.     GetAngleVectors(vAng, vDir, NULL_VECTOR, NULL_VECTOR);
  326.     vEnd = vLoc;
  327.     vEnd[0] += vDir[0] * 5000;
  328.     vEnd[1] += vDir[1] * 5000;
  329.     vEnd[2] += vDir[2] * 5000;
  330.     trace = TR_TraceHullFilterEx(vLoc, vEnd, view_as<float>({ -10.0, -10.0, -10.0 }), view_as<float>({ 10.0, 10.0, 10.0 }), MASK_SHOT, TraceFilter, client);
  331.  
  332.     if( TR_DidHit(trace) )
  333.     {
  334.         TR_GetEndPosition(vPos, trace);
  335.         float fDist = GetVectorDistance(vLoc, vPos);
  336.        
  337.         if(fDist <= 60.0)
  338.         {
  339.             AcceptEntityInput(entity, "TurnOff");
  340.         }
  341.         else if( fDist <= 500.0 + 50 )
  342.         {
  343.             GetAngleVectors(vAng, vAng, NULL_VECTOR, NULL_VECTOR);
  344.             vPos[0] -= vAng[0] * 50;
  345.             vPos[1] -= vAng[1] * 50;
  346.             vPos[2] -= vAng[2] * 50;
  347.             TeleportEntity(entity, vPos, NULL_VECTOR, NULL_VECTOR);
  348.         }
  349.         else
  350.         {
  351.             GetAngleVectors(vAng, vAng, NULL_VECTOR, NULL_VECTOR);
  352.             vPos[0] = vLoc[0] + (vAng[0] * 500.0);
  353.             vPos[1] = vLoc[1] + (vAng[1] * 500.0);
  354.             vPos[2] = vLoc[2] + (vAng[2] * 500.0);
  355.             TeleportEntity(entity, vPos, NULL_VECTOR, NULL_VECTOR);
  356.         }
  357.     }
  358.  
  359.     delete trace;
  360. }
  361.  
  362. public bool TraceFilter(int entity, int contentsMask, any client)
  363. {
  364.     if( entity == client )
  365.         return false;
  366.     return true;
  367. }
  368.  
  369. bool IsValidEntRef(int entity)
  370. {
  371.     if( entity && EntRefToEntIndex(entity) != INVALID_ENT_REFERENCE )
  372.         return true;
  373.     return false;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement