Jochemd

Jochemd - R7 Dynamic Radio in Vehicles

Jan 7th, 2012
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 7.83 KB | None | 0 0
  1. //  ---------------------------------------------------------------------
  2. //  -------------------- Dynamic Radio Stations.inc ---------------------
  3. //  ------------------------- (c) Jochemd 2012 --------------------------
  4. //  ---------------------------------------------------------------------
  5.  
  6. //------------------------------------------- Includes -------------------------------------------
  7. #include "YSI\y_hooks"
  8.  
  9. //------------------------------------------- Macro's -------------------------------------------
  10. #define USE_TEXTDRAW_SET 1
  11. #define MAX_RADIO_STATIONS 20
  12. #define TEXTDRAW_DISAPPEAR_TIME 2000
  13. #define NAME_SIZE 30
  14. #define LINK_SIZE 70
  15.  
  16. // Defining ENABLE_DEFAULT_STATIONS will allow players to listen to the default SA radio when they set their channel to ID 0 (Radio Off).
  17. //#define ENABLE_DEFAULT_STATIONS
  18.  
  19. #define GetVehicleRadioStation(%1) v_VehicleInfo[%1][RadioPlaying]
  20. #define GetStationName(%1) r_RadioInfo[%1][r_Name]
  21.  
  22. //------------------------------------------- Enumerations -------------------------------------------
  23. enum r_info
  24. {
  25.     r_Name[NAME_SIZE],
  26.     r_Link[LINK_SIZE]
  27. };
  28.  
  29. enum v_info
  30. {
  31.     RadioPlaying
  32. };
  33.  
  34. enum p_info
  35. {
  36.     Text:r_Draw
  37. };
  38.  
  39. //------------------------------------------- Declarations -------------------------------------------
  40. new r_RadioInfo[MAX_RADIO_STATIONS][r_info];
  41. new v_VehicleInfo[MAX_VEHICLES][v_info];
  42. new p_PlayerInfo[MAX_PLAYERS][p_info];
  43. new RadioCount = 0;
  44.  
  45. //------------------------------------------- Forwards -------------------------------------------
  46. forward HideRadioTextdraw(playerid, vehicleid);
  47.  
  48. //------------------------------------------- Stocks -------------------------------------------
  49. stock IsPlayerInSameVehicle(playerid, asplayerid)
  50. {
  51.     if(GetPlayerVehicleID(playerid) == GetPlayerVehicleID(asplayerid)) return 1;
  52.     else return 0;
  53. }
  54.  
  55. stock InitializeScript()
  56. {
  57.     for(new i = 0; i < MAX_PLAYERS; i ++)
  58.     {
  59.         #if USE_TEXTDRAW_SET == 0
  60.             p_PlayerInfo[i][r_Draw] = TextDrawCreate(309.000000, 9.000000, "Radio");
  61.             TextDrawAlignment(p_PlayerInfo[i][r_Draw], 2);
  62.             TextDrawBackgroundColor(p_PlayerInfo[i][r_Draw], 255);
  63.             TextDrawFont(p_PlayerInfo[i][r_Draw], 1);
  64.             TextDrawLetterSize(p_PlayerInfo[i][r_Draw], 0.500000, 1.500000);
  65.             TextDrawColor(p_PlayerInfo[i][r_Draw], -1);
  66.             TextDrawSetOutline(p_PlayerInfo[i][r_Draw], 1);
  67.             TextDrawSetProportional(p_PlayerInfo[i][r_Draw], 1);
  68.         #elseif USE_TEXTDRAW_SET == 1
  69.             p_PlayerInfo[i][r_Draw] = TextDrawCreate(309.000000, 20.000000, "Radio");
  70.             TextDrawAlignment(p_PlayerInfo[i][r_Draw], 2);
  71.             TextDrawBackgroundColor(p_PlayerInfo[i][r_Draw], 255);
  72.             TextDrawFont(p_PlayerInfo[i][r_Draw], 2);
  73.             TextDrawLetterSize(p_PlayerInfo[i][r_Draw], 0.519999, 2.000000);
  74.             TextDrawColor(p_PlayerInfo[i][r_Draw], -1384447745);
  75.             TextDrawSetOutline(p_PlayerInfo[i][r_Draw], 1);
  76.             TextDrawSetProportional(p_PlayerInfo[i][r_Draw], 1);
  77.         #elseif USE_TEXTDRAW_SET == 2
  78.             p_PlayerInfo[i][r_Draw] = TextDrawCreate(95.000000, 304.000000, "Listening to: ~n~~w~Radio");
  79.             TextDrawAlignment(p_PlayerInfo[i][r_Draw], 2);
  80.             TextDrawBackgroundColor(p_PlayerInfo[i][r_Draw], 255);
  81.             TextDrawFont(p_PlayerInfo[i][r_Draw], 1);
  82.             TextDrawLetterSize(p_PlayerInfo[i][r_Draw], 0.400000, 1.899999);
  83.             TextDrawColor(p_PlayerInfo[i][r_Draw], 1002045439);
  84.             TextDrawSetOutline(p_PlayerInfo[i][r_Draw], 1);
  85.             TextDrawSetProportional(p_PlayerInfo[i][r_Draw], 1);
  86.         #endif
  87.     }
  88.     AddRadioStation("Radio Off","Off");
  89.     return 1;
  90. }
  91.  
  92. stock AddRadioStation(name[NAME_SIZE], link[LINK_SIZE])
  93. {
  94.     if(RadioCount == MAX_RADIO_STATIONS - 1)
  95.     {
  96.         printf("Error: Please increase MAX_RADIO_STATIONS, you are trying to create more than %d radio stations!",MAX_RADIO_STATIONS);
  97.         return -1;
  98.     }
  99.     else
  100.     {
  101.         format(r_RadioInfo[RadioCount][r_Name],NAME_SIZE,"%s",name);
  102.         format(r_RadioInfo[RadioCount][r_Link],LINK_SIZE,"%s",link);
  103.         if(RadioCount) printf("Dynamic Radio System: Created a radio station: \"ID %d\", Name: '%s'",RadioCount,name);
  104.         RadioCount ++;
  105.         return RadioCount -1;
  106.     }
  107. }
  108.  
  109. stock SetRadioStation(vehicleid, stationid)
  110. {
  111.     new timer = -1;
  112.     v_VehicleInfo[vehicleid][RadioPlaying] = stationid;
  113.     for(new i = 0; i < MAX_PLAYERS; i ++)
  114.     {
  115.         if(IsPlayerConnected(i))
  116.         {
  117.             if(GetPlayerVehicleID(i) == vehicleid)
  118.             {
  119.                 timer = GetPVarInt(i,"ID_HideRadioTextdraw");
  120.                 if(timer != -1)
  121.                 {
  122.                     TextDrawHideForPlayer(i,p_PlayerInfo[i][r_Draw]);
  123.                     KillTimer(timer);
  124.                 }
  125.                 #if USE_TEXTDRAW_SET == 2
  126.                     new string[50];
  127.                     format(string,sizeof(string),"Listening to: ~n~~w~%s",r_RadioInfo[stationid][r_Name]);
  128.                     TextDrawSetString(p_PlayerInfo[i][r_Draw],string);
  129.                 #else
  130.                     TextDrawSetString(p_PlayerInfo[i][r_Draw],r_RadioInfo[stationid][r_Name]);
  131.                 #endif
  132.                 TextDrawShowForPlayer(i,p_PlayerInfo[i][r_Draw]);
  133.                 SetPVarInt(i,"ID_HideRadioTextdraw",SetTimerEx("HideRadioTextdraw",TEXTDRAW_DISAPPEAR_TIME,false,"i",i));
  134.                 #if defined ENABLE_DEFAULT_STATIONS
  135.                     if(stationid) PlayAudioStreamForPlayer(i,r_RadioInfo[stationid][r_Link]);
  136.                     else StopAudioStreamForPlayer(i);
  137.                 #else
  138.                     PlayAudioStreamForPlayer(i,r_RadioInfo[stationid][r_Link]);
  139.                 #endif
  140.             }
  141.         }
  142.     }
  143.     return 1;
  144. }
  145.  
  146. stock GetRadioStationsString()
  147. {
  148.     new string[NAME_SIZE * MAX_RADIO_STATIONS],str[NAME_SIZE + 20];
  149.     for(new i = 0; i < MAX_RADIO_STATIONS; i ++)
  150.     {
  151.         if(!i) format(str,sizeof(str),"{FFFF00}%s{FFFFFF}",r_RadioInfo[i][r_Name]);
  152.         else format(str,sizeof(str),"\n%s",r_RadioInfo[i][r_Name]);
  153.         strcat(string,str,sizeof(string));
  154.     }
  155.     return string;
  156. }
  157.  
  158. //------------------------------------------- Functions -------------------------------------------
  159. Hook:D_OnGameModeInit() InitializeScript();
  160. Hook:D_OnFilterScriptInit() InitializeScript();
  161.  
  162. Hook:D_OnPlayerConnect(playerid)
  163. {
  164.     SetPVarInt(playerid,"ID_HideRadioTextdraw",-1);
  165.     SetPVarInt(playerid,"ID_OldVehicle",-1);
  166.     return 1;
  167. }
  168.  
  169. Hook:D_OnPlayerStateChange(playerid, newstate, oldstate)
  170. {
  171.     if(newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER)
  172.     {
  173.         new vehicleid = GetPlayerVehicleID(playerid),
  174.             playingid = v_VehicleInfo[vehicleid][RadioPlaying];
  175.         #if USE_TEXTDRAW_SET == 2
  176.             new string[50];
  177.             format(string,sizeof(string),"Listening to: ~n~~w~%s",r_RadioInfo[playingid][r_Name]);
  178.             TextDrawSetString(p_PlayerInfo[playerid][r_Draw],string);
  179.         #else
  180.             TextDrawSetString(p_PlayerInfo[playerid][r_Draw],r_RadioInfo[playingid][r_Name]);
  181.         #endif
  182.         TextDrawShowForPlayer(playerid,p_PlayerInfo[playerid][r_Draw]);
  183.         SetPVarInt(playerid,"ID_HideRadioTextdraw",SetTimerEx("HideRadioTextdraw",TEXTDRAW_DISAPPEAR_TIME,false,"ii",playerid,vehicleid));
  184.         PlayAudioStreamForPlayer(playerid,r_RadioInfo[playingid][r_Link]);
  185.         SetPVarInt(playerid,"ID_OldVehicle",vehicleid);
  186.         return 1;
  187.     }
  188.     if(oldstate == PLAYER_STATE_DRIVER || oldstate == PLAYER_STATE_PASSENGER)
  189.     {
  190.     //new vehicleid = GetPVarInt(playerid,"ID_OldVehicle"),
  191.         new timer = GetPVarInt(playerid,"ID_HideRadioTextdraw");
  192.         if(timer != -1)
  193.         {
  194.             TextDrawHideForPlayer(playerid, p_PlayerInfo[playerid][r_Draw]);
  195.             KillTimer(timer);
  196.         }
  197.         StopAudioStreamForPlayer(playerid);
  198.     }
  199.     return 1;
  200. }
  201.  
  202. Hook:D_OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  203. {
  204.     if(IsPlayerInAnyVehicle(playerid))
  205.     {
  206.         if(newkeys & KEY_YES)
  207.         {
  208.             new vehicleid = GetPlayerVehicleID(playerid),newstation = v_VehicleInfo[vehicleid][RadioPlaying] + 1;
  209.             if(newstation == RadioCount) newstation = 0;
  210.             SetRadioStation(vehicleid,newstation);
  211.             return 1;
  212.         }
  213.         else if(newkeys & KEY_NO)
  214.         {
  215.             new vehicleid = GetPlayerVehicleID(playerid),newstation = v_VehicleInfo[vehicleid][RadioPlaying] - 1;
  216.             if(newstation == -1) newstation = RadioCount - 1;
  217.             SetRadioStation(vehicleid,newstation);
  218.             return 1;
  219.         }
  220.     }
  221.     return 1;
  222. }
  223.  
  224. public HideRadioTextdraw(playerid)
  225. {
  226.     TextDrawHideForPlayer(playerid,p_PlayerInfo[playerid][r_Draw]);
  227.     SetPVarInt(playerid,"ID_HideRadioTextdraw",-1);
  228.     return 1;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment