/* This plugin enables friendly fire votes and auto-responses. 
 * Idea for friendly fire vote from Kleen13.
 */
 
/* $Id: plugin_ff.sma,v 1.1 2001/18/04 yensid $ */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
/* Change this to whatever ratio of players need to vote for a setting to change it. */
#define FF_VOTE_RATIO 50
 
/* Change to 1 to have a Friendly Fire vote start 160 seconds in to a map */
#define AUTO_FF_VOTE 0
 
#define ACCESS_VOTE_FF 1
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
 
public FFVote() {
	new strDummy[10];
	vote("Friendly Fire on?", "Yes", "No", "HandleFFVote",strDummy);
}
 
public HandleFFVote(WinningOption,HLData,VoteCount,UserCount) {
	new Text[MAX_TEXT_LENGTH];
	new strNumber[MAX_NUMBER_LENGTH];
	new Ratio = FF_VOTE_RATIO;
	new strData[MAX_DATA_LENGTH];
	convert_string(HLData, strData,MAX_DATA_LENGTH);
 
	if (VoteCount >= Ratio*UserCount/100) {	
		if (WinningOption==1) {
			if(getvar("mp_friendlyfire")==1) {
				centersay("Vote over.  Friendly fire will remain enabled.",18,249,244,0);
			} else {
				centersay("Vote successful. Friendly fire changed to enabled.^nSo it is now a really BAD time to shoot your teammates!!!",18,249,244,0);
				exec("mp_friendlyfire 1");
			}
		} else {
			if(getvar("mp_friendlyfire")==1) {
				centersay("OK Ok...You got your way!^nFriendly Fire Is OFF...^nShoot whomever you please!",18,63,187,239);
				exec("mp_friendlyfire 0");
			} else {
				centersay("Vote over.  Friendly fire will remain disabled.",18,63,187,239);
			}
		}
	} else {
		numtostr(Ratio*UserCount/100,strNumber);
		if(getvar("mp_friendlyfire")==1) {
			snprintf(Text, MAX_TEXT_LENGTH, "Vote succeeded, but not enough votes for change (needed %s)^nFriendly fire will remain enabled.", strNumber);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "Vote succeeded, but not enough votes for change (needed %s)^nFriendly fire will remain disabled.", strNumber);
		}
		centersay(Text,18,63,187,239);
	}
}
 
public admin_vote_ff(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
 
	if (vote_allowed()!=1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
 
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
	say_command(User,Command,Data);		
	FFVote();
	return PLUGIN_HANDLED;
}
 
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);
	new Match = FALSE;
	if (strcasestr(Data, "ff on")!=-1) {
		Match = TRUE;
	} else if (strcasestr(Data, "ff is on")!=-1) {
		Match = TRUE;
	} else if (strcasestr(Data, "friendly fire on")!=-1) {
		Match = TRUE;
	}
	if (Match==TRUE) {
		if (getvar("mp_friendlyfire") == 1){ 
			centersay ("Friendly fire is ON! Watch your fire! ^n Your teammates are counting on you to actually aim!",7,0,255,0); 
		} else {
			centersay ("Friendly fire is OFF. ^n Go ahead, shoot your partner. ",7,0,255,0);
		}
	}
	if (strcasestr(Data, "vote_ff")!=-1) {
		FFVote();
	}
	return PLUGIN_CONTINUE;
}
 
 
public plugin_init() {
	plugin_registerinfo("Friendly Fire Plugin","Enables friendly fire votes and auto-responses.",STRING_VERSION);
	plugin_registercmd("admin_vote_ff","admin_vote_ff",ACCESS_VOTE_FF,"admin_vote_ff : Starts a vote to enable/disable friendly fire.");
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
	plugin_registerhelp("say",ACCESS_ALL,"say ff on, ff is on, friendly fire on: Will give friendly fire status.");
 
#if AUTO_FF_VOTE==1	
	if (vote_allowed()!=1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
	set_timer("FFVote",160,1);
#endif
 
	return PLUGIN_CONTINUE;
}