Guest User

Untitled

a guest
Aug 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.87 KB | None | 0 0
  1. #pragma semicolon 1
  2.  
  3. #include <sourcemod>
  4. #include <sdktools>
  5.  
  6. #define PLUGIN_VERSION "1.0.0.3"
  7.  
  8. #define MAX_PLAYERS 64
  9.  
  10.  
  11. // globals
  12. new gObj[MAXPLAYERS+1];         // how many tripmines player has this spawn
  13. new Float:gThrow[MAXPLAYERS+1]; // throw charge state
  14. new Handle:gTimer;      
  15. new String:gSound[256];
  16.  
  17. // convars
  18. new Handle:cvSpeed = INVALID_HANDLE;
  19. new Handle:cvDistance = INVALID_HANDLE;
  20. new Handle:cvTeamRestrict = INVALID_HANDLE;
  21. new Handle:cvSound = INVALID_HANDLE;
  22. new Handle:cvGround = INVALID_HANDLE;
  23. new Handle:cvThrowTime = INVALID_HANDLE;
  24. new Handle:cvThrowSpeed = INVALID_HANDLE;
  25. new Handle:cvMaxDistance = INVALID_HANDLE;
  26. new Handle:cvSteal = INVALID_HANDLE;
  27. new Handle:cvDropOnJump = INVALID_HANDLE;
  28.  
  29. public OnPluginStart()
  30. {
  31.     // events
  32.     HookEvent("player_death", PlayerDeath);
  33.     HookEvent("player_spawn",PlayerSpawn);
  34.    
  35.     // convars
  36.     CreateConVar("sm_grabber_version", PLUGIN_VERSION, "Grabber:SM Version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
  37.     cvSpeed = CreateConVar("sm_grabber_speed", "10.0");
  38.     cvDistance = CreateConVar("sm_grabber_distance", "64.0");
  39.     cvTeamRestrict = CreateConVar("sm_grabber_team_restrict", "0", "team restriction (0=all use, 2 or 3 to restrict that team");
  40.     cvSound = CreateConVar("sm_grabber_sound", "weapons/physcannon/hold_loop.wav", "sound to play, change takes effect on map change");
  41.     cvGround = CreateConVar("sm_grabber_groundmode", "0", "ground mode (soccer) 0=off 1=on");
  42.     cvThrowTime = CreateConVar("sm_grabber_throwtime", "2.0", "time to charge up to full throw speed");
  43.     cvThrowSpeed = CreateConVar("sm_grabber_throwspeed", "1000.0", "speed at which an object is thrown");
  44.     cvMaxDistance = CreateConVar("sm_grabber_maxdistance", "512.0", "maximum distance from which you can grab an object");
  45.     cvSteal = CreateConVar("sm_grabber_steal", "1", "can objects be 'stolen' from other players (0=no 1=yes)");
  46.     cvDropOnJump = CreateConVar("sm_grabber_droponjump", "1", "drop objects when jumping (prevents player from flying around level on large objects) (0=no 1=yes)");
  47.    
  48.     // commands
  49.     RegConsoleCmd("+grab", Command_Grab);
  50.     RegConsoleCmd("-grab", Command_UnGrab2);
  51.     RegConsoleCmd("+throw", Command_Throw);
  52.     RegConsoleCmd("-throw", Command_UnThrow);
  53. }
  54.  
  55. public OnEventShutdown(){
  56.     UnhookEvent("player_death", PlayerDeath);
  57.     UnhookEvent("player_spawn",PlayerSpawn);
  58. }
  59.  
  60. public OnMapStart()
  61. {
  62.     // reset object list
  63.     new i;
  64.     for (i=0; i<=MAX_PLAYERS; i++)
  65.     {
  66.         gObj[i]=-1;
  67.         gThrow[i]=0.0;
  68.     }
  69.    
  70.     // start timer
  71.     gTimer = CreateTimer(0.1, UpdateObjects, INVALID_HANDLE, TIMER_REPEAT);
  72.    
  73.     // precache sounds
  74.     GetConVarString(cvSound, gSound, sizeof(gSound));
  75.     PrecacheSound(gSound, true);
  76. }
  77.  
  78. public OnMapEnd()
  79. {
  80.     CloseHandle(gTimer);
  81. }
  82.  
  83. // When a new client is put in the server we reset their status
  84. public OnClientPutInServer(client){
  85.     if(client && !IsFakeClient(client))
  86.     {
  87.         gThrow[client]=0.0;
  88.         gObj[client] = -1;
  89.     }
  90. }
  91.  
  92. public OnClientDisconnect(client)
  93. {
  94.     if (gObj[client]>0)
  95.         Command_UnGrab(client, 0);
  96. }
  97.  
  98. public Action:PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast)
  99. {
  100.     new client;
  101.     client = GetClientOfUserId(GetEventInt(event, "userid"));
  102.     // reset object held
  103.     gThrow[client]=0.0;
  104.     gObj[client] = -1;
  105.     StopSound(client, SNDCHAN_AUTO, gSound);
  106.     return Plugin_Continue;
  107. }
  108.  
  109. public Action:PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast){
  110.     new client;
  111.     client = GetClientOfUserId(GetEventInt(event, "userid"));
  112.     // reset object held
  113.     gThrow[client]=0.0;
  114.     gObj[client] = -1;
  115.     StopSound(client, SNDCHAN_AUTO, gSound);
  116.     return Plugin_Continue;
  117. }
  118.  
  119. public Action:Command_Grab(client, args)
  120. {  
  121.    
  122.     // if an object is being held, go to UnGrab
  123.     if (gObj[client]>0)
  124.         return Command_UnGrab(client, args);
  125.    
  126.     // make sure client is not spectating
  127.     if (!IsPlayerAlive(client))
  128.         return Plugin_Handled;
  129.    
  130.     // check team restrictions
  131.     new restrict = GetConVarInt(cvTeamRestrict);
  132.     if (restrict>0)
  133.     {
  134.         if (restrict==GetClientTeam(client))
  135.         {
  136.             return Plugin_Handled;
  137.         }
  138.     }
  139.    
  140.     // find entity
  141.     new ent = TraceToEntity(client);
  142.     if (ent==-1)
  143.         return Plugin_Handled;
  144.    
  145.     // only grab physics entities
  146.     new String:edictname[128];
  147.     GetEdictClassname(ent, edictname, 128);
  148.     if (strncmp("prop_", edictname, 5, false)==0)
  149.     {
  150.         // check if another player is holding it
  151.         new j;
  152.         for (j=1; j<=MAX_PLAYERS; j++)
  153.         {
  154.             if (gObj[j]==ent)
  155.             {
  156.                 if (GetConVarInt(cvSteal)==1)
  157.                 {
  158.                     // steal from other player
  159.                     Command_UnGrab(j, args);
  160.                 }
  161.                 else
  162.                 {
  163.                     // already being held - stealing not allowed
  164.                     return Plugin_Handled;
  165.                 }
  166.             }
  167.         }
  168.        
  169.         // grab entity
  170.         gObj[client] = ent;
  171.         gThrow[client] = 0.0;
  172.         if (GetConVarInt(cvGround)!=1)
  173.         {
  174.             EmitSoundToAll(gSound, client);   // no sound in ground mode
  175.         }
  176.     }
  177.    
  178.     return Plugin_Handled;
  179. }
  180.  
  181. public Action:Command_UnGrab(client, args)
  182. {  
  183.     // make sure client is not spectating
  184.     if (!IsPlayerAlive(client))
  185.         return Plugin_Handled;
  186.    
  187.     if (GetConVarInt(cvGround)!=1)
  188.     {
  189.         StopSound(client, SNDCHAN_AUTO, gSound);  // no sound in ground mode
  190.     }
  191.    
  192.     if (gThrow[client]>0.0)
  193.         PrintHintText(client, "");
  194.    
  195.     gThrow[client] = 0.0;
  196.     gObj[client] = -1;
  197.    
  198.     return Plugin_Handled;
  199. }
  200.  
  201. public Action:Command_Throw(client, args)
  202. {
  203.     // make sure client is not spectating
  204.     if (!IsPlayerAlive(client))
  205.         return Plugin_Handled;
  206.     // has an object?
  207.     if (gObj[client]<1)
  208.         return Plugin_Handled;
  209.    
  210.     // start throw timer
  211.     gThrow[client] = GetEngineTime();
  212.    
  213.     return Plugin_Handled;
  214. }
  215.  
  216. public Action:Command_UnGrab2(client, args)
  217. {
  218.     // changed so Commmand_Ungrab is called from Command_Grab if an object is already held
  219.     // so we need to handle -grab with this function
  220.     return Plugin_Handled;
  221. }
  222.  
  223. public Action:Command_UnThrow(client, args)
  224. {
  225.     // make sure client is not spectating
  226.     if (!IsPlayerAlive(client))
  227.         return Plugin_Handled;
  228.     // has an object?
  229.     if (gObj[client]<1)
  230.         return Plugin_Handled;
  231.    
  232.    
  233.     // throw object
  234.     new Float:throwtime = GetConVarFloat(cvThrowTime);
  235.     new Float:throwspeed = GetConVarFloat(cvThrowSpeed);
  236.     new Float:time = GetEngineTime();
  237.     new Float:percent;
  238.    
  239.     time=time-gThrow[client];
  240.     if (time>throwtime)
  241.     {
  242.         percent = 1.0;
  243.     }
  244.     else
  245.     {
  246.         percent = time/throwtime;
  247.     }
  248.     throwspeed*=percent;
  249.    
  250.     new Float:start[3];
  251.     GetClientEyePosition(client, start);
  252.     new Float:angle[3];
  253.     new Float:speed[3];
  254.     GetClientEyeAngles(client, angle);
  255.     GetAngleVectors(angle, speed, NULL_VECTOR, NULL_VECTOR);
  256.     speed[0]*=throwspeed; speed[1]*=throwspeed; speed[2]*=throwspeed;
  257.    
  258.     TeleportEntity(gObj[client], NULL_VECTOR, NULL_VECTOR, speed);
  259.    
  260.     // cleanup
  261.     Command_UnGrab(client, args);
  262.     PrintHintText(client, "");  
  263.    
  264.     return Plugin_Handled;
  265. }
  266.  
  267.  
  268. public Action:UpdateObjects(Handle:timer)
  269.  
  270. {
  271.     new Float:vecDir[3], Float:vecPos[3], Float:vecVel[3];      // vectors
  272.     new Float:viewang[3];                                       // angles
  273.     new i;
  274.     new Float:speed = GetConVarFloat(cvSpeed);
  275.     new Float:distance = GetConVarFloat(cvDistance);
  276.     new groundmode = GetConVarInt(cvGround);
  277.     new Float:throwtime = GetConVarFloat(cvThrowTime);
  278.     new Float:time = GetEngineTime();
  279.     new bool:DropInJump = false;
  280.     for (i=0; i<=MAX_PLAYERS; i++)
  281.     {
  282.         if (gObj[i]>0)
  283.         {
  284.             if (GetConVarBool(cvDropOnJump))
  285.             {
  286.                 DropInJump = GetClientButtons(i) & IN_JUMP ? true : false;
  287.             }
  288.             if (IsValidEdict(gObj[i]) && IsValidEntity(gObj[i]) && !DropInJump )
  289.             {
  290.                 // get client info
  291.                 GetClientEyeAngles(i, viewang);
  292.                 GetAngleVectors(viewang, vecDir, NULL_VECTOR, NULL_VECTOR);
  293.                 if (groundmode==1)
  294.                 {
  295.                     GetClientAbsOrigin(i, vecPos);
  296.                 }
  297.                 else
  298.                 {
  299.                     GetClientEyePosition(i, vecPos);
  300.                 }
  301.                
  302.                 // update object
  303.                 vecPos[0]+=vecDir[0]*distance;
  304.                 vecPos[1]+=vecDir[1]*distance;
  305.                 if (groundmode!=1)
  306.                 {
  307.                     vecPos[2]+=vecDir[2]*distance;    // don't change up/down in ground mode
  308.                 }
  309.                
  310.                 GetEntPropVector(gObj[i], Prop_Send, "m_vecOrigin", vecDir);
  311.                
  312.                 SubtractVectors(vecPos, vecDir, vecVel);
  313.                
  314.                 ScaleVector(vecVel, speed);
  315.                 if (groundmode==1)
  316.                 {
  317.                     vecVel[2]=0.0;
  318.                 }
  319.                 TeleportEntity(gObj[i], NULL_VECTOR, NULL_VECTOR, vecVel);
  320.                
  321.                 // update throw time
  322.                 if (gThrow[i]>0.0)
  323.                 {
  324.                     ShowBar(i, time-gThrow[i], throwtime);
  325.                 }
  326.                
  327.             }
  328.             else
  329.             {
  330.                 gObj[i]=-1;
  331.             }
  332.            
  333.         }
  334.     }
  335.    
  336.     return Plugin_Continue;
  337. }
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353. public TraceToEntity(client)
  354. {
  355.     new Float:vecClientEyePos[3], Float:vecClientEyeAng[3];
  356.     GetClientEyePosition(client, vecClientEyePos); // Get the position of the player's eyes
  357.     GetClientEyeAngles(client, vecClientEyeAng); // Get the angle the player is looking    
  358.    
  359.     //Check for colliding entities
  360.     TR_TraceRayFilter(vecClientEyePos, vecClientEyeAng, MASK_SOLID, RayType_Infinite, TraceRayDontHitSelf, client);
  361.    
  362.     if (TR_DidHit(INVALID_HANDLE))
  363.     {
  364.         new TRIndex = TR_GetEntityIndex(INVALID_HANDLE);
  365.        
  366.         // check max distance
  367.         new Float:pos[3];
  368.         GetEntPropVector(TRIndex, Prop_Send, "m_vecOrigin", pos);
  369.         if (GetVectorDistance(vecClientEyePos, pos)>GetConVarFloat(cvMaxDistance))
  370.         {
  371.             return -1;
  372.         }
  373.         else
  374.         {
  375.             return TRIndex;
  376.         }
  377.     }
  378.    
  379.     return -1;
  380. }
  381.  
  382. public bool:TraceRayDontHitSelf(entity, mask, any:data)
  383. {
  384.     if(entity == data) // Check if the TraceRay hit the itself.
  385.     {
  386.         return false; // Don't let the entity be hit
  387.     }
  388.     return true; // It didn't hit itself
  389. }
  390.  
  391.  
  392. // show a progres bar via hint text
  393. ShowBar(client, Float:curTime, Float:totTime)
  394. {
  395.     new i;
  396.     new String:output[128];
  397.    
  398.     if (curTime>totTime)
  399.     {
  400.         Format(output, sizeof(output), "[XXXXXXXXXXXXXXXXXXXX]");
  401.     }
  402.     else
  403.     {
  404.         new Float:fl = 1.0;
  405.         output[0]='[';
  406.         for (i=1;i<21;i++)
  407.         {
  408.             if (fl/20.0 > curTime/totTime)
  409.             {
  410.                 output[i]='-';
  411.             }
  412.             else
  413.             {
  414.                 output[i]='X';
  415.             }
  416.             fl+=1.0;
  417.         }
  418.         output[21]=']';
  419.         output[22]='\0';
  420.     }
  421.    
  422.     PrintHintText(client, output);
  423. }
Add Comment
Please, Sign In to add comment