norn

nRadio v0.1

May 18th, 2011
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 10.00 KB | None | 0 0
  1. /*
  2.       __________             .___.__
  3.   ____\______   \_____     __| _/|__| ____
  4.  /    \|       _/\__  \   / __ | |  |/  _ \
  5. |   |  \    |   \ / __ \_/ /_/ | |  (  <_> )
  6. |___|  /____|_  /(____  /\____ | |__|\____/ v0.1
  7.      \/       \/      \/      \/
  8.  
  9.     Credits:
  10.     Incognito (http://forum.sa-mp.com/member.php?u=925) (Audio Plugin)
  11.     Norn (Scott Davey) (http://forum.sa-mp.com/member.php?u=33812)
  12.  
  13. Useful Links:
  14. http://www.internet-radio.org.uk    (Internet Radio List)
  15. http://forum.sa-mp.com/showthread.php?t=82162 (Audio Plugin)
  16. */
  17.  
  18. #include <a_samp>
  19. #include <audio>
  20.  
  21. #define NRMAX_RADIOS  500
  22. #define NRMAX_CATEGORIES  30
  23. #define NRINVALID_RADIO_ID    -1
  24. #define NRMAX_CATEGORY_LENGTH 75
  25. #define NRMAX_RADIO_LENGTH    75
  26. #define NRMAX_STREAM_URL_LENGTH   128
  27. #define NRCATEGORIES_DIALOG_ID    1553
  28. #define NRRADIO_DIALOG_ID 1552
  29.  
  30. enum nRadioItem
  31. {
  32.     nrCategoryID,
  33.     nrTitle[NRMAX_RADIO_LENGTH],
  34.     nrStreamURL[NRMAX_STREAM_URL_LENGTH]
  35. }
  36. new RadioItem[NRMAX_RADIOS][nRadioItem], nCategories[NRMAX_RADIOS][NRMAX_CATEGORY_LENGTH], nrCATEGORY_COUNT = 0, nrRADIO_COUNT = 0, nrDIALOG_STRING[1028], nrTextString[128];
  37.  
  38. forward RadioAdd(categoryid, name[NRMAX_RADIO_LENGTH], streamurl[NRMAX_STREAM_URL_LENGTH]);
  39. forward RadioAddCategory(name[NRMAX_CATEGORY_LENGTH]);
  40. forward RadioInitDialog(playerid);
  41. forward RadioStop(playerid);
  42. forward RadioPlay(playerid, radioid);
  43. forward DeleteCategory(categoryid);
  44. forward ModifyCategory(categoryid, name[NRMAX_CATEGORY_LENGTH]);
  45. forward ModifyRadio(radioid, name[NRMAX_RADIO_LENGTH], streamurl[NRMAX_STREAM_URL_LENGTH]);
  46. forward DeleteRadio(radioid);
  47. forward RadioCountFromCategory(categoryid);
  48. forward CategoryIDFromDialogOrder(listitem);
  49. forward RadioCategoryBrowse(playerid, listitem);
  50. forward RadioIDFromDialogOrder(playerid, listitem);
  51. forward nRadio_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
  52. forward RadioSetVolume(playerid, volume);
  53.  
  54. public RadioAddCategory(name[NRMAX_CATEGORY_LENGTH])
  55. {
  56.     new CATEGORY_ID = NRINVALID_RADIO_ID, bool:breakloop = false;
  57.     if(strlen(name) >= 1 && strlen(name) <= NRMAX_CATEGORY_LENGTH) {
  58.         if(nrCATEGORY_COUNT < NRMAX_CATEGORIES) {
  59.             for(new i=0;i<NRMAX_CATEGORIES;i++) {
  60.                 if(!breakloop) {
  61.                     if(!strlen(nCategories[i])) {
  62.                         format(nCategories[i], NRMAX_CATEGORY_LENGTH, name);
  63.                         CATEGORY_ID = i;
  64.                         printf("[nRadio:] Category ID %d (%s) created.", CATEGORY_ID, nCategories[i]);
  65.                         nrCATEGORY_COUNT++;
  66.                         breakloop = true;
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     return CATEGORY_ID;
  73. }
  74.  
  75.  
  76. public RadioAdd(categoryid, name[NRMAX_RADIO_LENGTH], streamurl[NRMAX_STREAM_URL_LENGTH])
  77. {
  78.     new bool:breakloop = false, RADIO_ID = -1;
  79.     if(strlen(name) >= 1 && strlen(name) <= NRMAX_RADIO_LENGTH) {
  80.         if(nrRADIO_COUNT < NRMAX_RADIOS) {
  81.             for(new i=0;i<NRMAX_RADIOS;i++) {
  82.                 if(!breakloop) {
  83.                     if(!strlen(RadioItem[i][nrTitle])) {
  84.                         RadioItem[i][nrCategoryID] = categoryid;
  85.                         format(RadioItem[i][nrTitle], NRMAX_RADIO_LENGTH, name);
  86.                         format(RadioItem[i][nrStreamURL], NRMAX_STREAM_URL_LENGTH, streamurl);
  87.                         printf("[nRadio:] Radio ID %d (%s) created and attached to %s.", i, RadioItem[i][nrTitle], nCategories[categoryid]);
  88.                         breakloop = true;
  89.                         nrRADIO_COUNT++;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95.     return RADIO_ID;
  96. }
  97.  
  98.  
  99. public RadioInitDialog(playerid)
  100. {
  101.     format(nrDIALOG_STRING, sizeof(nrDIALOG_STRING), "");
  102.     for(new i=0;i<nrCATEGORY_COUNT;i++) {
  103.         if(!i) {
  104.             if(strlen(nCategories[i]) >= 1)
  105.                 format(nrDIALOG_STRING, sizeof(nrDIALOG_STRING), nCategories[i]);
  106.         }
  107.         else {
  108.             if(strlen(nCategories[i]) >= 1)
  109.                 format(nrDIALOG_STRING, sizeof(nrDIALOG_STRING), "%s\n%s", nrDIALOG_STRING, nCategories[i]);
  110.         }
  111.     }
  112.     if(strlen(nrDIALOG_STRING) >= 1) { ShowPlayerDialog(playerid, NRCATEGORIES_DIALOG_ID, DIALOG_STYLE_LIST, "Radio - Browse Categories", nrDIALOG_STRING, "Browse", "Cancel"); }
  113.     return true;
  114. }
  115.  
  116.  
  117. stock RadioCategoryNameFromID(categoryid)
  118. {
  119.     new category_name[NRMAX_CATEGORY_LENGTH];
  120.     format(category_name, sizeof(category_name), nCategories[categoryid]);
  121.     return category_name;
  122. }
  123.  
  124.  
  125. stock RadioNameFromID(radioid)
  126. {
  127.     new radio_name[NRMAX_RADIO_LENGTH];
  128.     format(radio_name, sizeof(radio_name), RadioItem[radioid][nrTitle]);
  129.     return radio_name;
  130. }
  131.  
  132.  
  133. public RadioStop(playerid)
  134. {
  135.     if(GetPVarInt(playerid, "radioStream") != 0){Audio_Stop(playerid, GetPVarInt(playerid, "radioStream"));DeletePVar(playerid, "radioStream");}
  136.     return true;
  137. }
  138.  
  139.  
  140. public RadioSetVolume(playerid, volume)
  141. {
  142.     SetPVarInt(playerid, "radioVolume", volume);
  143.     Audio_SetVolume(playerid, GetPVarInt(playerid, "radioStream"), GetPVarInt(playerid, "radioVolume"));
  144.     return true;
  145. }
  146.  
  147.  
  148. public RadioPlay(playerid, radioid)
  149. {
  150.     RadioStop(playerid);
  151.     if(!GetPVarInt(playerid, "radioVolume")) { SetPVarInt(playerid, "radioVolume", 100); }
  152.     SetPVarInt(playerid, "radioStream", Audio_PlayStreamed(playerid, RadioItem[radioid][nrStreamURL],false,false,false));
  153.     Audio_SetVolume(playerid, GetPVarInt(playerid, "radioStream"), GetPVarInt(playerid, "radioVolume"));
  154.     return true;
  155. }
  156.  
  157.  
  158. public DeleteCategory(categoryid)
  159. {
  160.     format(nCategories[categoryid], NRMAX_CATEGORY_LENGTH, "");
  161.     for(new i=0;i<nrRADIO_COUNT;i++) {
  162.         if(RadioItem[i][nrCategoryID] == categoryid && strlen(RadioItem[i][nrTitle]) >= 1) {
  163.                                                   // Deleting all the radios attached to the category so the slot can be used.
  164.             format(RadioItem[i][nrTitle], NRMAX_RADIO_LENGTH, "");
  165.             nrRADIO_COUNT--;
  166.         }
  167.     }
  168.     nrCATEGORY_COUNT--;
  169.     return true;
  170. }
  171.  
  172.  
  173. public ModifyCategory(categoryid, name[NRMAX_CATEGORY_LENGTH])
  174. {
  175.     format(nCategories[categoryid], NRMAX_RADIO_LENGTH, name);
  176.     return true;
  177. }
  178.  
  179.  
  180. public ModifyRadio(radioid, name[NRMAX_RADIO_LENGTH], streamurl[NRMAX_STREAM_URL_LENGTH])
  181. {
  182.     format(RadioItem[radioid][nrTitle], NRMAX_RADIO_LENGTH, name);
  183.     format(RadioItem[radioid][nrStreamURL], NRMAX_RADIO_LENGTH, streamurl);
  184.     return true;
  185. }
  186.  
  187.  
  188. public DeleteRadio(radioid)
  189. {
  190.     format(RadioItem[radioid][nrTitle], NRMAX_RADIO_LENGTH, "");
  191.     nrRADIO_COUNT--;
  192.     return true;
  193. }
  194.  
  195.  
  196. public RadioCountFromCategory(categoryid)
  197. {
  198.     new count = 0;
  199.     for(new i=0;i<nrRADIO_COUNT;i++) {
  200.         if(RadioItem[i][nrCategoryID] == categoryid && strlen(RadioItem[i][nrTitle]) >= 1) {
  201.             count++;
  202.         }
  203.     }
  204.     return count;
  205. }
  206.  
  207.  
  208. public CategoryIDFromDialogOrder(listitem)
  209. {
  210.     new current_array_id, bool:breakloop = false, id = -1;
  211.     for(new i=0;i<nrCATEGORY_COUNT;i++) {
  212.         if(strlen(nCategories[i]) >= 1) {
  213.             if(!breakloop) {
  214.                 if(current_array_id == listitem) {
  215.                     id = i;
  216.                     breakloop = true;
  217.                 }
  218.                 current_array_id++;
  219.             }
  220.         }
  221.     }
  222.     return id;
  223. }
  224.  
  225.  
  226. public RadioIDFromDialogOrder(playerid, listitem)
  227. {
  228.     new categoryid = GetPVarInt(playerid, "radioCategory"), current_array_id, bool:breakloop = false, id = -1;
  229.     for(new i=0;i<nrRADIO_COUNT;i++) {
  230.         if(RadioItem[i][nrCategoryID] == categoryid && strlen(RadioItem[i][nrTitle]) >= 1) {
  231.             if(!breakloop) {
  232.                 if(current_array_id == listitem) {
  233.                     id = i;
  234.                     breakloop = true;
  235.                 }
  236.                 current_array_id++;
  237.             }
  238.         }
  239.     }
  240.     return id;
  241. }
  242.  
  243.  
  244. public RadioCategoryBrowse(playerid, listitem)
  245. {
  246.     format(nrDIALOG_STRING, sizeof(nrDIALOG_STRING), "");
  247.     new categoryid = CategoryIDFromDialogOrder(listitem);
  248.     SetPVarInt(playerid, "radioCategory", categoryid);
  249.     if(RadioCountFromCategory(categoryid) >= 1) {
  250.         for(new i=0;i<nrRADIO_COUNT;i++) {
  251.             if(RadioItem[i][nrCategoryID] == categoryid) {
  252.                 if(!i) {
  253.                     if(strlen(RadioItem[i][nrTitle]) >= 1)
  254.                         format(nrDIALOG_STRING, sizeof(nrDIALOG_STRING), RadioItem[i][nrTitle]);
  255.                 }
  256.                 else {
  257.                     if(strlen(RadioItem[i][nrTitle]) >= 1)
  258.                         format(nrDIALOG_STRING, sizeof(nrDIALOG_STRING), "%s\n%s", nrDIALOG_STRING, RadioItem[i][nrTitle]);
  259.                 }
  260.             }
  261.         }
  262.         new dialog_title[100];
  263.         format(dialog_title, sizeof(dialog_title), "Radio - %s", nCategories[categoryid]);
  264.         if(strlen(nrDIALOG_STRING) >= 1) { ShowPlayerDialog(playerid, NRRADIO_DIALOG_ID, DIALOG_STYLE_LIST, dialog_title, nrDIALOG_STRING, "Stream", "Go Back"); }
  265.     }
  266.     else {
  267.         format(nrTextString, sizeof(nrTextString), "[nRadio:] No stations exist in category ' %s '.", RadioCategoryNameFromID(categoryid));
  268.         SendClientMessage(playerid, 0xFF0000FF, nrTextString);
  269.         RadioInitDialog(playerid);
  270.     }
  271.     return true;
  272. }
  273.  
  274.  
  275. public nRadio_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  276. {
  277.     switch(dialogid) {
  278.         case NRRADIO_DIALOG_ID:
  279.         {
  280.             if(response) {
  281.                 new radioid = RadioIDFromDialogOrder(playerid, listitem);
  282.                 RadioPlay(playerid, radioid);
  283.                 format(nrTextString,sizeof(nrTextString),"[nRadio:] Stream for %s started.", RadioNameFromID(radioid));
  284.                 SendClientMessage(playerid, 0xADD8E6FF, nrTextString);
  285.             }
  286.             else {
  287.                 RadioInitDialog(playerid);
  288.             }
  289.         }
  290.         case NRCATEGORIES_DIALOG_ID:
  291.         {
  292.             if(response) {
  293.                 RadioCategoryBrowse(playerid, listitem);
  294.             }
  295.         }
  296.     }
  297.     return 1;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment