RaFaeLs

RaFaeL's Streaming Streamer

Oct 9th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.12 KB | None | 0 0
  1. /*
  2.     * By RaFaeL zilberman (c) 2012
  3.     * This include have writed for Sa:Mp - San andress multi player!
  4.     *
  5. */
  6.  
  7. #if defined _stsreamer_included
  8.   #endinput
  9. #endif
  10.  
  11.  
  12. //==========================================================================================
  13.  
  14. #define STREAM_RATE        100        // Time to update the streamer - in milseconds like 1000 (1sec) or 60000(1min)
  15. #define MAX_STREAMS        90         // Maximum global streaming
  16.  
  17. //==========================================================================================
  18.  
  19. /****************** Natives *******************/
  20. /*
  21. native PlayDynamicStream(url[], playerid = INVALID_PLAYER_ID, Float:posX = 0.0, Float:posY = 0.0, Float:posZ = 0.0, Float:distance = 50.0);
  22. native StopDynamicStream(playerid = INVALID_PLAYER_ID);
  23. natice CountDynamicStreams();
  24.  
  25. native IsAnyStreamForPlayer(playerid);
  26. native IsStreamForPlayer(playerid, streamid);
  27. native GetPlayerStreaming(playerid);
  28. native TooglePlayerStreaming(playerid, bool:status);
  29. native PlayerToogleStatus(playerid);
  30. */
  31. forward OnDynamicStreamIn(playerid, streamid);
  32. forward OnDynamicStreamOut(playerid, streamid);
  33.  
  34. //==========================================================================================
  35. enum StreamInfo {
  36.     surl[128],
  37.     sUser,
  38.     bool:uPosXYZ,
  39.     Float:sposX,
  40.     Float:sposY,
  41.     Float:sposZ,
  42.     Float:sdistance
  43. };
  44. new STService;
  45. new Streams[MAX_STREAMS][StreamInfo];
  46. new bool:UsedSTSlot[MAX_STREAMS];
  47. new STService_actual[MAX_PLAYERS];
  48. new bool:UserDisabled[MAX_PLAYERS];
  49.  
  50. forward STSERVICE_BGWokrer();
  51. //==========================================================================================
  52.  
  53.  
  54. //==========================================================================================
  55. stock PlayDynamicStream(url[], playerid = INVALID_PLAYER_ID, Float:posX = 0.0, Float:posY = 0.0, Float:posZ = 0.0, Float:distance = 50.0) {
  56.     if(STService == 0) STService = SetTimer("STSERVICE_BGWokrer", STREAM_RATE, true);
  57.    
  58.     new streamid = 1;
  59.     while(UsedSTSlot[streamid] == true) streamid++;
  60.    
  61.     UsedSTSlot[streamid] = true;
  62.     format(Streams[streamid][surl], 128, url);
  63.     Streams[streamid][uPosXYZ] = (posX==0.0&&posY==0.0&&posZ==0.0)? (false):(true);
  64.     Streams[streamid][sposX] = posX;
  65.     Streams[streamid][sposY] = posY;
  66.     Streams[streamid][sposZ] = posZ;
  67.     Streams[streamid][sdistance] = (Streams[streamid][uPosXYZ])? (distance):(1000000.0);
  68.     Streams[streamid][sUser] = (playerid != INVALID_PLAYER_ID)? (playerid):(INVALID_PLAYER_ID);
  69.     return streamid;
  70. }
  71. stock StopDynamicStream(streamid) {
  72.     if(streamid == 0 || !UsedSTSlot[streamid]) return false;
  73.     UsedSTSlot[streamid] = false;
  74.    
  75.     for(new i,j=GetMaxPlayers(); i<j; i++) if(STService_actual[i] == streamid) {
  76.         if(STService_actual[i] > 0) CallLocalFunction("OnDynamicStreamOut", "ii", i, STService_actual[i]);
  77.         StopAudioStreamForPlayer(i);
  78.         STService_actual[i] = 0;
  79.     }
  80.     return 1;
  81. }
  82. stock CountDynamicStreams() {
  83.     new count;
  84.     for(new streamid, j=MAX_STREAMS; streamid<j; streamid++) if(UsedSTSlot[streamid] == true) count++;
  85.    
  86.     return count;
  87. }
  88.  
  89. stock IsAnyStreamForPlayer(playerid) {
  90.     return (STService_actual[playerid] == 0)? (false):(true);
  91. }
  92. stock IsStreamForPlayer(playerid, streamid) {
  93.     return (IsAnyStreamForPlayer(playerid) && STService_actual[playerid] == streamid)? (true):(false);
  94. }
  95. stock GetPlayerStreaming(playerid) {
  96.     return (!IsAnyStreamForPlayer(playerid))? (0):(STService_actual[playerid]);
  97. }
  98. stock TooglePlayerStreaming(playerid, bool:status) {
  99.     UserDisabled[playerid] = (status)? (false):(true);
  100.     if(STService_actual[playerid] > 0) StopAudioStreamForPlayer(playerid);
  101.     STService_actual[playerid] = 0;
  102.     return 1;
  103. }
  104. stock PlayerToogleStatus(playerid) {
  105.     return (UserDisabled[playerid])? (false):(true);
  106. }
  107. //==========================================================================================
  108.  
  109.  
  110. //==========================================================================================
  111. public STSERVICE_BGWokrer() {
  112.     for(new i,j=GetMaxPlayers(); i<j; i++) if(!UserDisabled[i]) {
  113.         new Float:prevdist = 100000.000;
  114.         new prevst, prevstx;
  115.         for(new streamid=1; streamid < MAX_STREAMS; streamid++) if(UsedSTSlot[streamid]) {
  116.             new Float:dist = STSERVICE_getdist(i,Streams[streamid][sposX],Streams[streamid][sposY],Streams[streamid][sposZ]);
  117.             if(dist < prevdist && dist < Streams[streamid][sdistance]) {
  118.                 prevdist = dist;
  119.                 prevst = streamid;
  120.             }
  121.             if(!Streams[streamid][uPosXYZ]) {
  122.                 prevstx = streamid;
  123.             }
  124.         }
  125.         new streamid = (prevst > 0)? (prevst):(prevstx);
  126.         if(streamid > 0) {
  127.             if(STService_actual[i] == streamid) return 1;
  128.            
  129.             if(Streams[streamid][sUser] != INVALID_PLAYER_ID && Streams[streamid][sUser] == i) {
  130.                 StopAudioStreamForPlayer(i);
  131.                 if(STService_actual[i] > 0) CallLocalFunction("OnDynamicStreamOut", "ii", i, STService_actual[i]);
  132.                 STService_actual[i] = streamid;
  133.                 CallLocalFunction("OnDynamicStreamIn", "ii", i, streamid);
  134.                 PlayAudioStreamForPlayer(i, Streams[streamid][surl], Streams[streamid][sposX],Streams[streamid][sposY],Streams[streamid][sposZ], Streams[streamid][sdistance], (Streams[streamid][uPosXYZ])? (1):(0));
  135.             } else if(Streams[streamid][sUser] == INVALID_PLAYER_ID) {
  136.                 StopAudioStreamForPlayer(i);
  137.                 if(STService_actual[i] > 0) CallLocalFunction("OnDynamicStreamOut", "ii", i, STService_actual[i]);
  138.                 STService_actual[i] = streamid;
  139.                 CallLocalFunction("OnDynamicStreamIn", "ii", i, streamid);
  140.                 PlayAudioStreamForPlayer(i, Streams[streamid][surl], Streams[streamid][sposX],Streams[streamid][sposY],Streams[streamid][sposZ], Streams[streamid][sdistance], (Streams[streamid][uPosXYZ])? (1):(0));
  141.             }
  142.         } else {
  143.             if(STService_actual[i] != 0){
  144.                 STService_actual[i] = 0;
  145.                 StopAudioStreamForPlayer(i);
  146.             }
  147.         }
  148.     }
  149.     return 1;
  150. }
  151. stock STSERVICE_getdist(playerid,Float:x2,Float:y2,Float:z2) {
  152.     new Float:x1,Float:y1,Float:z1, Float:tmpdis;
  153.     GetPlayerPos(playerid,x1,y1,z1);
  154.     tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
  155.     return floatround(tmpdis);
  156. }
  157. //==========================================================================================
Add Comment
Please, Sign In to add comment