/* This plugin contains speech functions and commands
 * The idea for the speech commands, and the bulk of the code
 * for speaktimeleft came from [WHO]Them.
 */
 
/* $Id: plugin_speech.sma,v 1.2 2001/07/05 yensid $ */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_SPEAK 64
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
new iLastSpeak = 0;
 
number_array(num, return_array[]) {
	new counter = 0;
	new isnegative = 1;
	if(num < 0)
		isnegative = -1;
	num = num * isnegative;
	for(counter = 0; counter < 50; counter = counter + 1) return_array[counter] = '^0';
 
	for(counter = 0; num != 0; counter=counter+1) {
		for(return_array[counter] = 0; num % 10 != 0; num = num -1) {
			return_array[counter] = return_array[counter] + 1;
		}
		num = num / 10;
	}
 
	if(isnegative == -1)
		return_array[counter] = -1;
}
 
public SpeakTimeleft() {
        new i;
	new maxplayers = maxplayercount();
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Team;
	new WONID;
	new words[13][30];
	new secondsleft[50];
	new outputstring[300];
	new time1=timeleft();
	new time2 = 0;
	new minutesleft[50];
	time2 = time1 % 60;
	time1 = time1 / 60;
	number_array(time2, secondsleft);
	number_array(time1, minutesleft);
	if(secondsleft[0] > 0 || secondsleft[1] > 0) {
		strncpy(words[12], "seconds ", strlen("seconds "),MAX_DATA_LENGTH);
	}
	if(secondsleft[1] == 1) {
		if(secondsleft[0] == 0) {
			strncpy(words[10], "ten ", strlen("ten "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 1) {
			strncpy(words[10], "eleven ", strlen("eleven "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 2) {
			strncpy(words[10], "twelve ", strlen("twelve "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 3) {
			strncpy(words[10], "thirteen ", strlen("thriteen "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 4) {
			strncpy(words[10], "fourteen ", strlen("fourteen "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 5) {
			strncpy(words[10], "fifteen ", strlen("fifteen "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 6) {
			strncpy(words[10], "sixteen ", strlen("sixteen "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 7) {
			strncpy(words[10], "seventeen ", strlen("seventeen "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 8) {
			strncpy(words[10], "eightteen ", strlen("eightteen "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 9) {
			strncpy(words[10], "nineteen ", strlen("nineteen "),MAX_DATA_LENGTH);
		}
	}
	else {
		if(secondsleft[1] == 2) {
			strncpy(words[10], "twenty ", strlen("twenty "),MAX_DATA_LENGTH);
		}
		if(secondsleft[1] == 3) {
			strncpy(words[10], "thirty ", strlen("thirty "),MAX_DATA_LENGTH);
		}
		if(secondsleft[1] == 4) {
			strncpy(words[10], "fourty ", strlen("fourty "),MAX_DATA_LENGTH);
		}
		if(secondsleft[1] == 5) {
			strncpy(words[10], "fifty ", strlen("fifty "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 1) {
			strncpy(words[11], "one ", strlen("one "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 2) {
			strncpy(words[11], "two ", strlen("two "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 3) {
			strncpy(words[11], "three ", strlen("three "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 4) {
			strncpy(words[11], "four ", strlen("four "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 5) {
			strncpy(words[11], "five ", strlen("five "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 6) {
			strncpy(words[11], "six ", strlen("six "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 7) {
			strncpy(words[11], "seven ", strlen("seven "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 8) {
			strncpy(words[11], "eight ", strlen("eight "),MAX_DATA_LENGTH);
		}
		if(secondsleft[0] == 9) {
			strncpy(words[11], "nine ", strlen("nine "),MAX_DATA_LENGTH);
		}
	}
	if(minutesleft[0] > 0 || minutesleft[1] > 0) strncpy(words[9], "minutes ", strlen("minutes "),MAX_DATA_LENGTH);
	if(minutesleft[1] == 1) {
		if(minutesleft[0] == 0) {
			strncpy(words[7], "ten ", strlen("ten "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 1) {
			strncpy(words[7], "eleven ", strlen("eleven "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 2) {
			strncpy(words[7], "twelve ", strlen("twelve "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 3) {
			strncpy(words[7], "thirteen ", strlen("thriteen "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 4) {
			strncpy(words[7], "fourteen ", strlen("fourteen "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 5) {
			strncpy(words[7], "fifteen ", strlen("fifteen "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 6) {
			strncpy(words[7], "sixteen ", strlen("sixteen "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 7) {
			strncpy(words[7], "seventeen ", strlen("seventeen "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 8) {
			strncpy(words[7], "eightteen ", strlen("eightteen "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 9) {
			strncpy(words[7], "nineteen ", strlen("nineteen "),MAX_DATA_LENGTH);
		}
	}
	else {
		if(minutesleft[1] == 2) {
			strncpy(words[7], "twenty ", strlen("twenty "),MAX_DATA_LENGTH);
		}
		if(minutesleft[1] == 3) {
			strncpy(words[7], "thirty ", strlen("thirty "),MAX_DATA_LENGTH);
		}
		if(minutesleft[1] == 4) {
			strncpy(words[7], "fourty ", strlen("fourty "),MAX_DATA_LENGTH);
		}
		if(minutesleft[1] == 5) {
			strncpy(words[7], "fifty ", strlen("fifty "),MAX_DATA_LENGTH);
		}
		if(minutesleft[1] == 6) {
			strncpy(words[7], "sixty ", strlen("sixty "),MAX_DATA_LENGTH);
		}
		if(minutesleft[1] == 7) {
			strncpy(words[7], "seventy ", strlen("seventy "),MAX_DATA_LENGTH);
		}
		if(minutesleft[1] == 8) {
			strncpy(words[7], "eighty ", strlen("eighty "),MAX_DATA_LENGTH);
		}
		if(minutesleft[1] == 9) {
			strncpy(words[7], "ninety ", strlen("ninety "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 1) {
			strncpy(words[8], "one ", strlen("one "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 2) {
			strncpy(words[8], "two ", strlen("two "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 3) {
			strncpy(words[8], "three ", strlen("three "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 4) {
			strncpy(words[8], "four ", strlen("four "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 5) {
			strncpy(words[8], "five ", strlen("five "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 6) {
			strncpy(words[8], "six ", strlen("six "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 7) {
			strncpy(words[8], "seven ", strlen("seven "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 8) {
			strncpy(words[8], "eight ", strlen("eight "),MAX_DATA_LENGTH);
		}
		if(minutesleft[0] == 9) {
			strncpy(words[8], "nine ", strlen("nine "),MAX_DATA_LENGTH);
		}
	}
        snprintf(outputstring, MAX_DATA_LENGTH, "speak ^"fvox/%s%s%s%s%s%s remaining^"", words[7], words[8], words[9], words[10], words[11], words[12]); 
	for(i=1; i<=maxplayers; i++) {
		strinit(Target);
		if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
			execclient(Target,outputstring);
		}
	}
}
 
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strstripquotes(Data);
	if (streq(Data, "timeleft")==1) {
		if (systemtime() < iLastSpeak) {
			return PLUGIN_CONTINUE;
  		} else {
			SpeakTimeleft();
			iLastSpeak = systemtime();
			iLastSpeak += 60;
		}
	}
	return PLUGIN_CONTINUE;
}
 
public admin_speak(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Target[MAX_NAME_LENGTH];
	new Speak[MAX_DATA_LENGTH];
	new Cmd[MAX_DATA_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	strbreak(Data, Target, Cmd, MAX_DATA_LENGTH);
	if (Cmd[0] == NULL_CHAR) {
		selfmessage( "Unparsable format: no command found.");
		return PLUGIN_HANDLED;
	}
 
	if(check_user(Target)==0) {
		selfmessage( "Unrecognized player: ");
		selfmessage( Target);
		return PLUGIN_HANDLED;
	}
	strsubst(Cmd," ( ","(",MAX_DATA_LENGTH);
	strsubst(Cmd," )",")",MAX_DATA_LENGTH);
        snprintf(Speak, MAX_DATA_LENGTH, "speak ^"%s^"", Cmd); 
 
	if (execclient(Target,Speak)==0) {
		selfmessage( "Failed.");
	} else {
		selfmessage( "Succeeded.");
	}
	log_command(User,Command,Data);
	return PLUGIN_HANDLED;
}
 
public admin_speakall(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
        new Speak[MAX_DATA_LENGTH];
        new i;
	new maxplayers = maxplayercount();
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Team;
	new WONID;
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH); 
	strsubst(Data," ( ","(",MAX_DATA_LENGTH);
	strsubst(Data," )",")",MAX_DATA_LENGTH);
        snprintf(Speak, MAX_DATA_LENGTH, "speak ^"%s^"", Data); 
	for(i=1; i<=maxplayers; i++) {
		strinit(Target);
		if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
			execclient(Target,Speak);
		}
	}
	log_command(User,Command,Data);
	return PLUGIN_HANDLED; 
}
 
public admin_speakteam(HLCommand,HLData,HLUserName,UserIndex) {
	new Cmd[MAX_DATA_LENGTH];
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new i;
	new maxplayers = maxplayercount();
	new SessionID;
	new ExecTeam = 0;
	new strTeam[MAX_TEXT_LENGTH];
	new Target[MAX_NAME_LENGTH];
	new Team;
	new User[MAX_NAME_LENGTH];
	new WONID;
        new Speak[MAX_DATA_LENGTH];
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	strbreak(Data, strTeam, Cmd, MAX_DATA_LENGTH);
	if (strlen(Cmd) == 0) {
		selfmessage( "Unparsable format: no command found.");
		return PLUGIN_HANDLED;
	}
	ExecTeam = strtonum(strTeam);
        snprintf(Speak, MAX_DATA_LENGTH, "speak ^"%s^"", Cmd);
	if(ExecTeam > 0) {
		for(i=1; i<=maxplayers; i++) {
			strinit(Target);
			if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
				if(Team==ExecTeam) {
					execclient(Target,Speak);
				}
			}
		}
	} else {
		selfmessage("The team to exec must be a number.");
	}
	log_command(User,Command,Data);
	return PLUGIN_HANDLED;
}
 
public plugin_init() {
	plugin_registerinfo("Admin Speech Plugin","Commands for speaking on clients systems.",STRING_VERSION);
 
	plugin_registercmd("admin_speak","admin_speak",ACCESS_SPEAK,"admin_speak <target> <msg>: Speak words on system of target.");
	plugin_registercmd("admin_speakall","admin_speakall",ACCESS_SPEAK,"admin_speakall <msg>: Speak words on system of everyone.");
	plugin_registercmd("admin_speakteam","admin_speakteam",ACCESS_SPEAK,"admin_speakteam <team> <msg>: Speak words on systems of everyone on team.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say timeleft: Will speak the time left.");
 
	return PLUGIN_CONTINUE;	
}