eyal282

[CS:GO] Crossbow Like Backwards

Jan 13th, 2021 (edited)
706
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sourcemod>
  2. #include <sdkhooks>
  3. #include <sdktools>
  4. #include <cstrike>
  5. #include <smlib>
  6.  
  7. #pragma newdecls required
  8.  
  9. #define PLUGIN_VERSION "1.0"
  10.  
  11. public Plugin myinfo =
  12. {
  13.     name = "[CS:GO] Crossbow like Backwards",
  14.     author = "Eyal282",
  15.     description = "Crossbow gun like in the TTT server of backwards'",
  16.     version = PLUGIN_VERSION,
  17.     url = "N/A"
  18. }
  19.  
  20. #define EF_NODRAW 32
  21.  
  22. bool bHoldingCrossbow[MAXPLAYERS + 1];
  23.  
  24. public void OnMapStart()
  25. {
  26.     LoadDirOfModels("materials/models/weapons/eminem/advanced_crossbow");
  27.     LoadDirOfModels("models/weapons/eminem/advanced_crossbow");
  28.     LoadDirOfModels("sound/weapons/eminem/advanced_crossbow");
  29.    
  30.     PrecacheModel("models/weapons/w_eq_fraggrenade_dropped.mdl", true);
  31.     PrecacheModel("models/weapons/eminem/advanced_crossbow/w_crossbow_bolt_dropped.mdl", true)
  32.    
  33.     PrecacheGeneric("particles/backwards_ttt_blood.pcf", true)
  34.     PrecacheParticleSystem("backwards_ttt_blood");
  35. }
  36. public void OnPluginStart()
  37. {
  38.     HookEvent("player_spawn", Event_PlayerSpawn, EventHookMode_Post);
  39.    
  40.     for (int i = 1; i <= MaxClients;i++)
  41.     {
  42.         if(!IsClientInGame(i))
  43.             continue;
  44.            
  45.         Func_OnClientPutInServer(i);
  46.     }
  47. }
  48.  
  49. public void OnClientPutInServer(int client)
  50. {
  51.     Func_OnClientPutInServer(client);
  52. }
  53.  
  54. public void Func_OnClientPutInServer(int client)
  55. {
  56.     SDKHook(client, SDKHook_WeaponCanUse, OnWeaponCanUse);
  57.     SDKHook(client, SDKHook_WeaponSwitchPost, OnWeaponSwitchPost);
  58.     SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
  59. }
  60.  
  61. public Action OnWeaponCanUse(int client, int weapon)
  62. {
  63.     if(weapon == -1)
  64.         return Plugin_Continue;
  65.        
  66.     else if(!IsEntityAxe(weapon))
  67.         return Plugin_Continue;
  68.        
  69.     GivePlayerAxe(client);
  70.     AcceptEntityInput(weapon, "Kill");
  71.     return Plugin_Handled;
  72. }
  73. public void OnWeaponSwitchPost(int client, int weapon)
  74. {
  75.     bHoldingCrossbow[client] = false;
  76.    
  77.     if(weapon == -1)
  78.         return;
  79.    
  80.     else if(!IsEntityAxe(weapon))
  81.         return;
  82.        
  83.     bHoldingCrossbow[client] = true;
  84. }
  85.  
  86. public Action OnTakeDamage(int victim, int& attacker, int& inflictor, float& damage, int& damagetype)
  87. {
  88.     if(bHoldingCrossbow[attacker])
  89.     {
  90.         damage = 0.0;
  91.         return Plugin_Stop;
  92.     }
  93.    
  94.     return Plugin_Continue;
  95. }
  96. public Action OnPlayerRunCmd(int client, int& buttons, int& impulse, float vel[3], float angles[3], int& weapon, int& subtype, int& cmdnum, int& tickcount, int& seed, int mouse[2])
  97. {
  98.     if(bHoldingCrossbow[client])
  99.     {
  100.         buttons &= ~IN_ATTACK2;
  101.        
  102.         if(!(buttons & IN_ATTACK))
  103.             return Plugin_Continue;
  104.            
  105.         else if(GetEntPropFloat(client, Prop_Send, "m_flNextAttack") > GetGameTime())
  106.             return Plugin_Continue;
  107.        
  108.         int bullet = CreateEntityByName("smokegrenade_projectile");
  109.            
  110.         //SetEntProp(bullet, Prop_Send, "m_usSolidFlags", 12); //FSOLID_NOT_SOLID|FSOLID_TRIGGER
  111.         SetEntProp(bullet, Prop_Data, "m_nSolidType", 6); // SOLID_VPHYSICS
  112.         //SetEntProp(bullet, Prop_Send, "m_CollisionGroup", 1); //COLLISION_GROUP_DEBRIS
  113.        
  114.         //SetEntityMoveType(bullet, MOVETYPE_FLY);
  115.        
  116.         // A random model to create a physics object.
  117.         //SetEntityModel(bullet, "models/weapons/w_eq_fraggrenade_dropped.mdl");
  118.        
  119.         float speed = 1024.0;
  120.        
  121.         float fOrigin[3], fAngles[3], fFwd[3];
  122.        
  123.         GetClientEyePosition(client, fOrigin);
  124.         GetClientEyeAngles(client, fAngles);
  125.        
  126.         GetAngleVectors(fAngles, fFwd, NULL_VECTOR, NULL_VECTOR);
  127.        
  128.         //fAngles = fFwd;
  129.         NormalizeVector(fFwd, fFwd);
  130.         ScaleVector(fFwd, speed);
  131.        
  132.         // While the bullet is a smokegrenade model, make it invisible.
  133.         SetEntProp(bullet, Prop_Send, "m_fEffects", GetEntProp(bullet, Prop_Send, "m_fEffects") | EF_NODRAW);
  134.        
  135.         DispatchSpawn(bullet);
  136.         ActivateEntity(bullet);
  137.        
  138.         AcceptEntityInput(bullet, "EnableMotion");
  139.        
  140.         TeleportEntity(bullet, fOrigin, fAngles, fFwd);
  141.        
  142.         //SetEntityMoveType(bullet, MOVETYPE_VPHYSICS);
  143.        
  144.         DataPack DP = new DataPack();
  145.         DP.WriteCell(bullet);
  146.         DP.WriteFloat(fFwd[2]);
  147.        
  148.         RequestFrame(Frame_NoGravity, DP);
  149.        
  150.         SetEntPropFloat(client, Prop_Send, "m_flNextAttack", GetGameTime() + 1.0);
  151.        
  152.         // After the entity is created, give the real model.
  153.        
  154.         SetEntPropEnt(bullet, Prop_Send, "m_hOwnerEntity", client);
  155.         SDKHook(bullet, SDKHook_StartTouchPost, OnStartTouch);
  156.         RequestFrame(Frame_GiveModel, bullet);
  157.  
  158.         buttons &= ~IN_ATTACK;
  159.        
  160.         return Plugin_Changed;
  161.     }
  162.    
  163.     return Plugin_Continue;
  164. }
  165.  
  166. public void OnStartTouch(int bullet, int toucher)
  167. {
  168.     int owner = GetEntPropEnt(bullet, Prop_Send, "m_hOwnerEntity");
  169.    
  170.     if(owner == -1)
  171.         return;
  172.        
  173.     else if(IsPlayer(toucher))
  174.     {
  175.         if(toucher == owner)
  176.             return;
  177.            
  178.         OnPlayerHitByCrossbow(toucher, owner);
  179.     }
  180.    
  181.     else
  182.     {
  183.         // If you hit glass or a JailBreak vent, break it!
  184.         SDKHooks_TakeDamage(toucher, bullet, owner, 128.0, DMG_BULLET);
  185.     }
  186.    
  187.     AcceptEntityInput(bullet, "Kill");
  188. }
  189.  
  190. public void OnPlayerHitByCrossbow(int victim, int attacker)
  191. {
  192.     CreateParticle(victim, "backwards_ttt_blood");
  193.    
  194.     //TE_SetupBloodSprite(fOrigin, { 90.0, 0.0, 0.0 }, { 255, 0, 0, 255 }, 25, bloodModel, bloodPuddleModel);
  195.     //TE_SendToAll();
  196.    
  197.     PrintToChatAll("B");
  198. }
  199.  
  200. void CreateParticle(int victim, char[] szName)
  201. {
  202.     int iEntity;
  203.     iEntity = CreateEntityByName("info_particle_system");
  204.  
  205.     if (IsValidEdict(iEntity) && (victim > 0))
  206.     {
  207.         if (IsPlayerAlive(victim))
  208.         {
  209.             // Get players current position
  210.             float vPosition[3];
  211.             GetEntPropVector(victim, Prop_Send, "m_vecOrigin", vPosition);
  212.            
  213.             // Move particle to player
  214.             TeleportEntity(iEntity, vPosition, NULL_VECTOR, NULL_VECTOR);
  215.            
  216.             // Set entity name
  217.             DispatchKeyValue(iEntity, "targetname", "particle");
  218.            
  219.             // Get player entity name
  220.             char szParentName[64];
  221.             GetEntPropString(victim, Prop_Data, "m_iName", szParentName, sizeof(szParentName));
  222.            
  223.             // Set the effect name
  224.             DispatchKeyValue(iEntity, "effect_name", szName);
  225.            
  226.             // Spawn the particle
  227.             DispatchSpawn(iEntity);
  228.            
  229.             // Target the particle
  230.             SetVariantString("!activator");
  231.            
  232.             // Set particle parent name
  233.             DispatchKeyValue(iEntity, "parentname", szParentName);
  234.            
  235.             // Set client to parent of particle
  236.             AcceptEntityInput(iEntity, "SetParent", victim, iEntity, 0);
  237.            
  238.             // Activate the entity (starts animation)
  239.             ActivateEntity(iEntity);
  240.             AcceptEntityInput(iEntity, "Start");
  241.            
  242.             // Attach to parent model
  243.             SetEntPropEnt(iEntity, Prop_Send, "m_hOwnerEntity", victim);
  244.            
  245.             SetFlags(iEntity);
  246.             SDKHook(iEntity, SDKHook_SetTransmit, OnSetTransmit);
  247.             //g_unClientAura[iClient] = EntIndexToEntRef(iEntity);
  248.         }
  249.     }
  250. }
  251.  
  252. public void SetFlags(int iEdict)
  253. {
  254.     if (GetEdictFlags(iEdict) & FL_EDICT_ALWAYS)
  255.     {
  256.         SetEdictFlags(iEdict, (GetEdictFlags(iEdict) ^ FL_EDICT_ALWAYS));
  257.     }
  258. }
  259.  
  260. public Action OnSetTransmit(int iEnt, int iClient)
  261. {
  262.     int iOwner = GetEntPropEnt(iEnt, Prop_Send, "m_hOwnerEntity");
  263.     SetFlags(iEnt);
  264.     if(iOwner && IsClientInGame(iOwner))
  265.     {
  266.         return Plugin_Continue;
  267.        
  268.     }
  269.    
  270.     return Plugin_Continue;
  271. }
  272.  
  273. public void Frame_NoGravity(DataPack DP)
  274. {
  275.     ResetPack(DP);
  276.    
  277.     int bullet = DP.ReadCell();
  278.    
  279.     if(!IsValidEntity(bullet))
  280.     {
  281.         delete DP;
  282.         return;
  283.     }
  284.     float Velocity[3];
  285.    
  286.     GetEntPropVector(bullet, Prop_Data, "m_vecVelocity", Velocity);
  287.    
  288.     Velocity[2] = DP.ReadFloat();
  289.    
  290.     TeleportEntity(bullet, NULL_VECTOR, NULL_VECTOR, Velocity);
  291.    
  292.     RequestFrame(Frame_NoGravity, DP);
  293. }
  294. public void Frame_GiveModel(int bullet)
  295. {
  296.     SetEntityModel(bullet, "models/weapons/eminem/advanced_crossbow/w_crossbow_bolt_dropped.mdl");
  297.    
  298.     SetEntProp(bullet, Prop_Send, "m_fEffects", GetEntProp(bullet, Prop_Send, "m_fEffects") & ~EF_NODRAW);
  299.     //new Float:Origin[3];
  300. }
  301.  
  302. public Action Event_PlayerSpawn(Handle hEvent, const char[] Name, bool dontBroadcast)
  303. {
  304.    
  305.     int client = GetClientOfUserId(GetEventInt(hEvent, "userid"));
  306.    
  307.     GivePlayerAxe(client);
  308.    
  309.     int weapon = GetEntPropEnt(client, Prop_Send, "m_hActiveWeapon");
  310.    
  311.     bHoldingCrossbow[client] = false;
  312.    
  313.     if(weapon == -1)
  314.         return;
  315.    
  316.     else if(!IsEntityAxe(weapon))
  317.         return;
  318.        
  319.     bHoldingCrossbow[client] = true;
  320. }
  321.  
  322. stock void LoadDirOfModels(char[] dirofmodels)
  323. {
  324.     char path[256];
  325.     FileType type;
  326.     char FileAfter[256];
  327.     Handle dir = OpenDirectory(dirofmodels, false, "GAME");
  328.    
  329.     if (!dir)
  330.     {
  331.         return;
  332.     }
  333.     while (ReadDirEntry(dir, path, 256, type))
  334.     {
  335.         if (type == FileType_File)
  336.         {
  337.             FormatEx(FileAfter, 256, "%s/%s", dirofmodels, path);
  338.             AddFileToDownloadsTable(FileAfter);
  339.         }
  340.     }
  341.     CloseHandle(dir);
  342.     dir = INVALID_HANDLE;
  343.     return;
  344. }
  345.  
  346. stock void GivePlayerAxe(int client)
  347. {
  348.     int entity = CreateEntityByName("weapon_axe");
  349.    
  350.     EquipPlayerWeapon(client, entity);
  351. }
  352.  
  353.  
  354. stock bool IsPlayer(int entity)
  355. {
  356.     if(entity < 1)
  357.         return false;
  358.        
  359.     else if(entity > MaxClients)
  360.         return false;
  361.        
  362.     return true;
  363. }
  364.  
  365. stock bool IsEntityAxe(int entity)
  366. {
  367.    
  368.     return GetEntProp(entity, Prop_Send, "m_iItemDefinitionIndex") == CS_WeaponIDToItemDefIndex(CSWeapon_AXE);
  369. }
RAW Paste Data