Advertisement
Southclaw

[SA:MP] Simple Sound Trigger System

Aug 26th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.85 KB | None | 0 0
  1. #define FILTERSCRIPT
  2.  
  3. #include <a_samp>
  4. #include <SIF\SIF>
  5.  
  6. // In the sound-system class/library
  7.  
  8. #define MAX_SOUND_LEVEL 100.0
  9.  
  10. static
  11. Float:  sound_Level[MAX_PLAYERS],
  12. Float:  sound_DropSpeed[MAX_PLAYERS],
  13. bool:   sound_MakingSound[MAX_PLAYERS];
  14.  
  15. // Core functions
  16.  
  17. PlayerMakeSound(playerid, Float:amount, Float:dropspeed)
  18. {
  19.     //printf("%.0f\t%.1f", amount, dropspeed);
  20.     if(!IsPlayerConnected(playerid))
  21.         return 0;
  22.  
  23.     if(!sound_MakingSound[playerid] && sound_Level[playerid] > 0.0)
  24.     {
  25.         sound_Level[playerid] = 0.0;
  26.     }
  27.     else
  28.     {
  29.         if(amount < sound_Level[playerid])
  30.             return -1;
  31.     }
  32.  
  33.     sound_Level[playerid] = amount;
  34.  
  35.     if(dropspeed > sound_DropSpeed[playerid])
  36.         sound_DropSpeed[playerid] = dropspeed;
  37.  
  38.     if(sound_Level[playerid] > MAX_SOUND_LEVEL)
  39.         sound_Level[playerid] = MAX_SOUND_LEVEL;
  40.  
  41.     sound_MakingSound[playerid] = true;
  42.  
  43.     return 1;
  44. }
  45.  
  46. // Internal
  47.  
  48. /*
  49. This function is either called on your Player Update timer (if you have one)
  50. or on a 100ms ptask.
  51.  
  52. Don't confuse a player update timer with OnPlayerUpdate. My mode relies a lot on
  53. process frames, so each player has a 100ms repeating timer running in order to
  54. process various things like this.
  55. */
  56.  
  57. timer UpdatePlayerSoundLevel[100](playerid)
  58. {
  59.     // If the sound level is 0 or below, set it to 0.0 and return.
  60.     // This will just keep happening until the sound level is raised.
  61.  
  62.     if(sound_Level[playerid] <= 0.0)
  63.     {
  64.         sound_Level[playerid] = 0.0;
  65.         sound_DropSpeed[playerid] = 0.0;
  66.         sound_MakingSound[playerid] = false;
  67.  
  68.         return;
  69.     }
  70.  
  71.     // If the sound level is above 0.0, subtract the drop speed value each tick.
  72.  
  73.     sound_Level[playerid] -= sound_DropSpeed[playerid];
  74.  
  75.     // If the drop speed is at 0.0, the sound level is maintained until it is
  76.     // manipulated by other means.
  77.  
  78.     return;
  79. }
  80.  
  81.  
  82. // Interface
  83.  
  84.  
  85. Float:GetPlayerSoundLevel(playerid)
  86. {
  87.     if(!IsPlayerConnected(playerid))
  88.         return 0.0;
  89.  
  90.     if(sound_Level[playerid] < 0.0)
  91.         sound_Level[playerid] = 0.0;
  92.  
  93.     return sound_Level[playerid];
  94. }
  95.  
  96.  
  97. // Outside the sound-system class/library
  98.  
  99. // Checking for different sound triggers
  100. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  101. {
  102.     if(newkeys & KEY_FIRE && newkeys & 128)
  103.     {
  104.         if(22 <= GetPlayerWeapon(playerid) <= 38)
  105.         {
  106.             new weaponstate = GetPlayerWeaponState(playerid);
  107.  
  108.             if((weaponstate != WEAPONSTATE_RELOADING && weaponstate != WEAPONSTATE_NO_BULLETS))
  109.             {
  110.                 // When a player fires any firearm, sound level maxes out
  111.                 // And drops by 1.0 every tick (Very loud and quick)
  112.                 PlayerMakeSound(playerid, 100.0, 5.0);
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. timer AnimationCheck[100](playerid)
  119. {
  120.     new
  121.         Float:x,
  122.         Float:y,
  123.         Float:z;
  124.  
  125.     GetPlayerVelocity(playerid, x, y, z);
  126.  
  127.     if(x + y + z == 0.0)
  128.         return;
  129.  
  130.     new
  131.         animidx = GetPlayerAnimationIndex(playerid),
  132.         k,
  133.         ud,
  134.         lr;
  135.  
  136.     GetPlayerKeys(playerid, k, ud, lr);
  137.  
  138.     printf("%d", animidx);
  139.  
  140.     if(animidx == 1159) // Crouching
  141.     {
  142.         PlayerMakeSound(playerid, 1.0, 0.1);
  143.     }
  144.     else if(animidx == 1195) // Jumping
  145.     {
  146.         PlayerMakeSound(playerid, 50.0, 2.0);
  147.     }
  148.     else if(animidx == 1231 || animidx == 1196) // Running
  149.     {
  150.         if(k & KEY_WALK) // Walking
  151.         {
  152.             PlayerMakeSound(playerid, 5.0, 0.1);
  153.         }
  154.         else if(k & KEY_SPRINT) // Sprinting
  155.         {
  156.             PlayerMakeSound(playerid, 40.0, 1.0);
  157.         }
  158.         else if(k & KEY_JUMP) // Jump
  159.         {
  160.             PlayerMakeSound(playerid, 50.0, 2.0);
  161.         }
  162.         else
  163.         {
  164.             PlayerMakeSound(playerid, 20.0, 1.0);
  165.         }
  166.     }
  167.  
  168.     return;
  169. }
  170.  
  171. // Quick GUI to visualize the sound level
  172. public OnPlayerUpdate(playerid)
  173. {
  174.     new str[128];
  175.     format(str, 128, "Sound: %f", GetPlayerSoundLevel(playerid));
  176.     ShowActionText(playerid, str, 0);
  177. }
  178.  
  179. // Other stuff to make this work as a filterscript
  180. public OnFilterScriptInit()
  181. {
  182.     for(new i; i < MAX_PLAYERS; i++)
  183.     {
  184.         if(IsPlayerConnected(i))
  185.         {
  186.             repeat UpdatePlayerSoundLevel(i);
  187.             repeat AnimationCheck(i);
  188.         }
  189.     }
  190.  
  191.     return 1;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement