/***************************************************************************
 * plugin_biggy_random.sma
 * Author: BigWig aka [LAPD]-l3lGGY
 *
 * Date: July 1st, 2002 - Original Version
 * Last Updated: July 2, 2002
 *
 * in a seperate file called phrases.txt. No need to recompile to change says.
 * You can change it at any time you want to, no need to reload the map even.
 * The only two things you can really change in this plugin is the name of the
 * phrase file "phrases.txt" and the interval between messages. It is now set to
 * pick a random number between 30 and 120 seconds and display another say.
 * The 30 seconds set into it is for when it picks the random number 2 and yep,2
 * seconds later it says another say which i didn't like. You can have it random time
 * or a set time between says with "admin_random <on/off>". You can also set the time
 * interval between says with "admin_rantime <interval in secs>" Last but not the least
 * you can turn this whole plugin on or off with "admin_ransay <on/off>". Hope you like it.
 ***************************************************************************/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define MAX_PHRASES 32 
#define ACCESS_RS 4096 
new FILENAME[MAX_DATA_LENGTH] = "phrases.txt"; 
new NUM_PHRASES=0;
new RAN_SAY=1;
new RAN_DOM=1;
new RAN_TIME = 90; 
new RAN_PHRASE=0;
new NAME_PHRASE=1;
new Phrase [MAX_PHRASES][MAX_TEXT_LENGTH];
new NamePhrase [MAX_PHRASES][MAX_TEXT_LENGTH];
new TotalPhrases=0;
new FileStatus=0;
new STRING_VERSION[MAX_DATA_LENGTH] = "2.00";
 
public sayem() {
  new gotLine;
  new lineNumber=1;
  new lineStr [MAX_TEXT_LENGTH];
  new CmdBuffer[MAX_COMMAND_LENGTH];
  new sCounter[MAX_NAME_LENGTH];
  TotalPhrases = 0;
  FileStatus = fileexists(FILENAME);
 
  if ( RAN_SAY == 1) {
    if ( FileStatus > 0) {
      do {
        gotLine = readfile(FILENAME,lineStr, lineNumber, MAX_TEXT_LENGTH);
 
        if (gotLine) {
          lineNumber++;
          if (strncmp(lineStr, "#", 1) == 0) {
          }
          else {
            TotalPhrases++;
          }
        }
        if (TotalPhrases == MAX_PHRASES) {
          return PLUGIN_CONTINUE;
        }
      } while (gotLine);
      NUM_PHRASES=TotalPhrases;
      numtostr(NUM_PHRASES,sCounter);
      RAN_PHRASE = random(NUM_PHRASES);
      if (RAN_PHRASE == 0) {
        RAN_PHRASE++;
        RAN_PHRASE++;
      }
      else {
        RAN_PHRASE++;
      }
      if (RAN_PHRASE == TotalPhrases) {
        RAN_PHRASE--;
      }
      readfile(FILENAME,lineStr, NAME_PHRASE, MAX_TEXT_LENGTH);
      strtok(lineStr,"", NamePhrase[RAN_PHRASE],MAX_TEXT_LENGTH);
      readfile(FILENAME,lineStr, RAN_PHRASE, MAX_TEXT_LENGTH);
      strtok(lineStr,"", Phrase[RAN_PHRASE],MAX_TEXT_LENGTH);
      snprintf(CmdBuffer, MAX_DATA_LENGTH, "say %s %s", NamePhrase , Phrase);
      exec(CmdBuffer);
 
 
      if ( RAN_DOM == 1) {
        new timer=random(RAN_TIME);
        timer=(timer+30);
        set_timer("sayem", timer, 0);
      } else {
        set_timer("sayem",RAN_TIME,0);
      }
 
 
 
 
    }
    else {
      printf ("Cannot find name file^n");
    }
  }
  else {
    return PLUGIN_HANDLED;
  }
  return PLUGIN_CONTINUE;
}
 
 
public admin_random(HLCommand,HLData,HLUserName,UserIndex) {
 
  new User[MAX_NAME_LENGTH];
  new Data[MAX_TEXT_LENGTH];
  new Command[MAX_TEXT_LENGTH];
  new sCounter[MAX_NAME_LENGTH];
 
  convert_string(HLUserName,User,MAX_NAME_LENGTH);
  convert_string(HLData,Data,MAX_NAME_LENGTH);
  convert_string(HLCommand,Command,MAX_NAME_LENGTH);
 
  if (strlen(Data) > 0) {
    if(check_param(Data)==1) {
      RAN_DOM = 1;
    } else {
      RAN_DOM = 0;
    }
 
    numtostr(RAN_DOM,sCounter);
    set_vaultdata("RAN_DOM", sCounter);
  }
 
  if (RAN_DOM == 1) {
    selfmessage("Random Say Times are On");
    sayem();
  } else {
    selfmessage("Says Times are Constant. Random is off.");
  }
 
  say_command(User, Command, Data);
  log_command(User, Command, Data);
 
  return PLUGIN_HANDLED;
}
 
public admin_ransay(HLCommand,HLData,HLUserName,UserIndex) {
 
  new User[MAX_NAME_LENGTH];
  new Data[MAX_TEXT_LENGTH];
  new Command[MAX_TEXT_LENGTH];
  new sCounter[MAX_NAME_LENGTH];
 
  convert_string(HLUserName,User,MAX_NAME_LENGTH);
  convert_string(HLData,Data,MAX_NAME_LENGTH);
  convert_string(HLCommand,Command,MAX_NAME_LENGTH);
 
  if (strlen(Data) > 0) {
    if(check_param(Data)==1) {
      RAN_SAY = 1;
    } else {
      RAN_SAY = 0;
    }
 
    numtostr(RAN_SAY,sCounter);
    set_vaultdata("RAN_SAY", sCounter);
  }
 
  if (RAN_SAY == 1) {
    selfmessage("Random Server Says are On");
    sayem();
  } else {
    selfmessage("Random Server Says are Off");
  }
 
  say_command(User, Command, Data);
  log_command(User, Command, Data);
 
  return PLUGIN_HANDLED;
}
 
public admin_rantime(HLCommand,HLData,HLUserName,UserIndex) {
  new Command[MAX_COMMAND_LENGTH];
  new Data[MAX_DATA_LENGTH];
  new User[MAX_NAME_LENGTH];
  new sCounter[MAX_NAME_LENGTH];
  new Text[MAX_TEXT_LENGTH];
 
  convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
  convert_string(HLData,Data,MAX_DATA_LENGTH);
  convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
  new temp_tol=0;
 
  if (strlen(Data)==0) {
    snprintf(Text,MAX_TEXT_LENGTH,"Time intervals set at: %i secs(+30 if set random)",RAN_TIME);
    selfmessage(Text);
  } else{
    temp_tol=strtonum(Data);
    RAN_TIME=temp_tol;
    numtostr(RAN_TIME,sCounter);
    set_vaultdata("RAN_TIME", sCounter);
    snprintf(Text,MAX_TEXT_LENGTH,"Time intervals changed to %i secs(+30 if set random)",RAN_TIME);
    selfmessage(Text);
  }
  say_command(User, Command, Data);
  log_command(User, Command, Data);
 
  return PLUGIN_HANDLED;
}
 
public plugin_init() {
  plugin_registerinfo("Biggy's Random Phrase Plugin", "Random Server Says", STRING_VERSION);
  plugin_registercmd("admin_ransay","admin_ransay",ACCESS_RS,"admin_ransay <on/off>: Sets/Displays Random Say status.");
  plugin_registercmd("admin_rantime","admin_rantime",ACCESS_RS,"admin_rantime <time in secs>: Sets/Displays Random Say Time Intervals.");
  plugin_registercmd("admin_random","admin_random",ACCESS_RS,"admin_random <on/off>: Sets/Displays Random Times Status.");
 
  new VaultData[MAX_DATA_LENGTH];
 
  if(get_vaultdata("RAN_DOM", VaultData, MAX_DATA_LENGTH) != 0) {
    RAN_DOM = strtonum(VaultData);
  } else {
    RAN_DOM = 1;
    set_vaultdata("RAN_DOM", "1");
  }
 
  if(get_vaultdata("RAN_SAY", VaultData, MAX_DATA_LENGTH) != 0) {
    RAN_SAY = strtonum(VaultData);
  } else {
    RAN_SAY = 1;
    set_vaultdata("RAN_SAY", "1");
  }
 
 
  if(get_vaultdata("TIME", VaultData, MAX_DATA_LENGTH) != 0) {
    RAN_TIME = strtonum(VaultData);
  } else {
    RAN_TIME = 90;
    set_vaultdata("RAN_TIME", "90");
  }
 
  set_timer("sayem", 60, 0);
  return PLUGIN_CONTINUE;
}