RaFaeLs

RaFaeL's Streaming Streamer

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