Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Dynamic Radio Filterscript
- By Alternative112
- DO NOT re-release without asking me!
- Notes:
- -The max station name in your .txt should be no higher than 40! It will make it format better in the dialog menu.
- */
- #include <a_samp>
- #include <zcmd>
- #include <sscanf2>
- #define MAX_GENRE_SIZE 20 //All your genres in the .txt should be lower in length than this.
- #define MAX_URL_SIZE 80
- #define MAX_STATIONS 20 //Change this to the amount of stations you have in your txt! I wish I could make this dynamic but PAWN needs to know how large arrays are prior to compilation
- #define Public:%0(%1) forward %0(%1);\
- public %0(%1)
- #define strequal(%0,%1)\
- !strcmp(%0, %1, true)
- new customStream[MAX_PLAYERS][64]; //This is the URL of the custom stream that the player added himself.
- new currStreamUrl[MAX_PLAYERS][64]; //What the player is currently playing - used so we can restart the stream correctly with restartRadio(playerid)
- new bool:isListening[MAX_PLAYERS char];
- enum
- info
- {
- stationName[40],
- url[MAX_URL_SIZE],
- genre[MAX_GENRE_SIZE],
- };
- new stationInfo[MAX_STATIONS][info];
- Public:playTimedStream(playerid, stream[], time, x, y, z, distance, usePos) {
- if (isListening{playerid} && IsPlayerConnected(playerid)) {
- StopAudioStreamForPlayer(playerid);
- PlayAudioStreamForPlayer(playerid, stream, x, y, z, distance, usePos);
- isListening{playerid} = false;
- if (time != 9999) {
- SetTimerEx("restartRadio", time, false, "i", playerid);
- }
- }
- return 1;
- }
- Public:restartRadio(playerid) {
- if (!isListening{playerid} && IsPlayerConnected(playerid)) {
- PlayAudioStreamForPlayer(playerid, currStreamUrl[playerid], 0.0, 0.0, 0.0);
- isListening{playerid} = true;
- }
- return 1;
- }
- stock ShowMainRadioMenu(playerid) {
- new str[500];
- for (new i = 0; i < MAX_STATIONS; i++) {
- if (strlen(stationInfo[i][genre]) > 0) {
- for (new j = 0; j < MAX_STATIONS; j++) {
- if (strlen(stationInfo[j][genre]) > 0) {
- if (strfind(str, stationInfo[j][genre], true) == -1) {
- format(str, sizeof(str), "%s\n{00FF00}>> {FFFFFF}%s", str, stationInfo[j][genre]);
- }
- }
- }
- }
- }
- format(str, sizeof(str), "Input a URL\nPlay Custom Stream: %s\nTurn Off Radio\n%s", customStream[playerid], str);
- ShowPlayerDialog(playerid, 700, DIALOG_STYLE_LIST, "Select a Category / Option!", str, "Open", "Cancel");
- return 1;
- }
- stock loadStations() {
- if (!fexist("radioStations.txt")) {
- print("radioStations.txt does not exist! Creating it.");
- new File:fhandle = fopen("radioStations.txt", io_write);
- fwrite(fhandle, "");
- fclose(fhandle);
- }
- new File:radioTxt = fopen("radioStations.txt", io_read);
- new string[126];
- new i = 0;
- while (fread(radioTxt, string)) {
- printf("String read is %s", string);
- new formatStr[20];
- format(formatStr, sizeof(formatStr), "p<,>s[40]s[%i]s[%i]", MAX_URL_SIZE, MAX_GENRE_SIZE);
- if (sscanf(string, formatStr, stationInfo[i][stationName], stationInfo[i][url], stationInfo[i][genre])) {
- print("There was an error loading that station...");
- }
- else {
- format(stationInfo[i][genre], strlen(stationInfo[i][genre]), "%s", stationInfo[i][genre]); //Get rid of /r/n!
- printf("Loaded station %s with URL %s [Genre: %s]", stationInfo[i][stationName], stationInfo[i][url], stationInfo[i][genre]);
- }
- i++;
- }
- fclose(radioTxt);
- print("----------Results---------");
- for (new j = 0; j < MAX_STATIONS; j++) {
- printf("%s, %s, %s", stationInfo[j][stationName], stationInfo[j][url], stationInfo[j][genre]);
- }
- return 1;
- }
- public OnFilterScriptInit() {
- loadStations();
- return 1;
- }
- public OnFilterScriptExit() {
- for (new i = 0; i < GetMaxPlayers(); i++) {
- if (isListening{i}) {
- StopAudioStreamForPlayer(i);
- SendClientMessage(i, 0xCC0000FF, "Your radio died.");
- }
- }
- return 1;
- }
- public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
- switch(dialogid) {
- case 700: { //Main Menu
- if(response)
- {
- switch(listitem) {
- case 0: { //Input custom station
- ShowPlayerDialog(playerid, 702, DIALOG_STYLE_INPUT, "Input URL", "Type your URL here!", "Play", "Back");
- }
- case 1: { //Play custom station
- if (strlen(customStream[playerid]) > 5 && strlen(customStream[playerid])) {
- format(currStreamUrl[playerid], MAX_URL_SIZE, customStream[playerid]);
- PlayAudioStreamForPlayer(playerid, customStream[playerid]);
- isListening{playerid} = true;
- }
- ShowMainRadioMenu(playerid);
- }
- case 2: { //Turn off radio
- StopAudioStreamForPlayer(playerid);
- format(currStreamUrl[playerid], MAX_URL_SIZE, "");
- SendClientMessage(playerid, 0xFFFF00FF, "The radio has been turned off.");
- isListening{playerid} = false;
- }
- default: { //Station listing
- new menuTxt[(MAX_STATIONS * MAX_URL_SIZE) + 24];
- format(menuTxt, sizeof(menuTxt), "{CC0000}<< {FFFFFF}Back\n");
- strdel(inputtext, 0, 3); //Strip the ">> "
- for (new j = 0; j < MAX_STATIONS; j++) { //Then display each radio station in that genre
- if (strequal(stationInfo[j][genre], inputtext) && strlen(stationInfo[j][genre]) > 0) {
- format(menuTxt, sizeof(menuTxt), "%s\n%s", menuTxt, stationInfo[j][stationName]);
- }
- }
- ShowPlayerDialog(playerid, 701, DIALOG_STYLE_LIST, "Select a Station!", menuTxt, "Play!", "Back");
- }
- }
- }
- }
- case 701: { //Menu 2
- if (listitem == 0) { //Back button
- ShowMainRadioMenu(playerid);
- }
- else {
- for (new i = 0; i < MAX_STATIONS; i++) {
- if (strequal(stationInfo[i][stationName], inputtext)) {
- if (strlen(inputtext) < MAX_URL_SIZE && strlen(inputtext) > 5) {
- format(currStreamUrl[playerid], MAX_URL_SIZE, stationInfo[i][url]);
- PlayAudioStreamForPlayer(playerid, stationInfo[i][url]);
- isListening{playerid} = true;
- }
- else {
- SendClientMessage(playerid, 0xCC0000FF, "Oops, this station is broken....");
- }
- return 1;
- }
- }
- }
- }
- case 702: { //Custom URL
- if (response) {
- if (strlen(inputtext) < MAX_URL_SIZE && strlen(inputtext) > 5) {
- format(customStream[playerid], MAX_URL_SIZE, inputtext);
- format(currStreamUrl[playerid], MAX_URL_SIZE, inputtext);
- PlayAudioStreamForPlayer(playerid, inputtext);
- isListening{playerid} = true;
- }
- else {
- ShowMainRadioMenu(playerid);
- new str[52];
- format(str, sizeof(str), "Invalid URL, must be between 5 and %i characters.", MAX_URL_SIZE);
- SendClientMessage(playerid, 0xCC0000FF, str);
- }
- }
- else {
- ShowMainRadioMenu(playerid);
- }
- }
- }
- return 1;
- }
- CMD:radio(playerid, params[]) {
- ShowMainRadioMenu(playerid);
- return 1;
- }
- CMD:reloadstations(playerid, params[]) {
- if (IsPlayerAdmin(playerid)) {
- //Clear the array out before loading
- for (new i = 0; i < MAX_STATIONS; i++) {
- format(stationInfo[i][stationName], strlen(stationInfo[i][stationName]), "");
- format(stationInfo[i][url], strlen(stationInfo[i][url]), "");
- format(stationInfo[i][genre], strlen(stationInfo[i][genre]), "");
- }
- loadStations();
- SendClientMessage(playerid, 0xFFFF00FF, "Stations reloaded. If something went wrong, check the logs!");
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment