/*  This plugin enables you to change the variable laserbeams_on
It is used by the MetaMod Laserbeam DLL  Code by SR71Goku with the help of code from
plugin_democracy.sma,v 1.0 4/22/01 by BenTheMeek & KrezerJ */
 
/* $Id: plugin_laserbeam.sma,v 1.01 12/17/01 by SR71Goku $ */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_LASERS 8192
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.0";
 
/* Change to 1 to have a lasers vote start 450 seconds in to a map */
#define AUTO_LA_VOTE 0
 
 
 
/* Change to 0 to disable admin_laserbeam */
#define ENABLE_LA 1
/* Change to 0 to disable laser voting */
#define ENABLE_LA_VOTE 1
/* Change to 0 to disable laser status autoresponse */
#define ENABLE_LA_STATUS 1
 
 
/* Change this to whatever ratio of players need to vote pistols only to change it. */
#define LA_VOTE_RATIO 51
 
/* Here we create the variables that will allow us to give the current state of lasers. Dont Change */
new LaserYes=1;
 
/* Change this to the number secconds a NON-admin will have to wait to Re-Vote somthing. Like to */
/* vote_pistols again you wait ?? secconds. This stops Vote-Spams. Thanks to Wraith for the idea */
new RVfreq=180;
 
#if ENABLE_LA_VOTE==1
 
/* Change this to 1 to disable public use of vote_lasers by default */
new LAVoteOff=0;
 
#endif
 
 
/**************************************/
/***** END OF SWITCHES FOR ADMINS *****/
/**************************************/
 
#if ENABLE_LA_VOTE==1
new vote_LAAllowed=1;
@LAVoteTimer() {vote_LAAllowed=1;}
#endif
 
 
public LaserON() {
	exec("lasersight_on 1");
	LaserYes=1; 
}
public LaserOFF() {
	exec("lasersight_on 0");
	LaserYes=0;
}
 
public LAVote() {
	new strDummy[10];
	vote("Lasers on?", "Yes", "No", "HandleLAVote",strDummy);
}
 
public HandleLAVote(WinningOption,HLData,VoteCount,UserCount) {
	new Text[MAX_TEXT_LENGTH];
	new strNumber[MAX_NUMBER_LENGTH];
	new Ratio = LA_VOTE_RATIO;
	new strData[MAX_DATA_LENGTH];
	new i;
	new maxplayers = maxplayercount();
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Team;
	new WONID;
	convert_string(HLData, strData,MAX_DATA_LENGTH);
 
	if (VoteCount >= Ratio*UserCount/100) {	
		if (WinningOption==1) {
			if (LaserYes==0) { 
				snprintf(Text, MAX_TEXT_LENGTH, "Vote successful. Laserbeams turned on!", strNumber);
				LaserON();
			} else {
				snprintf(Text, MAX_TEXT_LENGTH, "Vote over.  Laserbeams stay on.", strNumber);
				LaserON();  	/* I know this isn't needed but I've had a few problems with it not turning on*/
			}
		} else {
			if (LaserYes==0) { 
				snprintf(Text, MAX_TEXT_LENGTH, "Vote completed.  Laserbeams will reamain off.", strNumber);
				LaserOFF();	/* I have had the same problem as above, this is just a safe guard*/		
			} else {
				snprintf(Text, MAX_TEXT_LENGTH, "Vote over.  Laserbeams have been removed.", strNumber);
				LaserOFF();
			}
		}
	} else {
		numtostr(Ratio*UserCount/100,strNumber);
		if (LaserYes==0) { 
			snprintf(Text, MAX_TEXT_LENGTH, "Laserbeams on vote succeeded, but not enough votes for change (needed %s)^nLaserbeams will reamian off.", strNumber);
		} else {
			snprintf(Text, MAX_TEXT_LENGTH, "Laserbeams off vote succeeded, but not enough votes for change (needed %s)^nLasers will remain on.", strNumber);
		}
	}
	centersay(Text,18,63,187,239);
	for(i=1; i<=maxplayers; i++) {
		strinit(Target);
		if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
			messageex(Target,Text,print_console);
		}
	}
}
#if ENABLE_LA_VOTE==1
public admin_vote_lasers(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);
	say_command(User,Command,Data);
 
	if (streq(Data, "off")==1) {
		selfmessage( "Public laserbeam voting has been disabled");
		LAVoteOff=1;
		return PLUGIN_HANDLED;
	}
	if (streq(Data, "on")==1) {
		selfmessage( "Public laserbeam voting has been enabled");
		LAVoteOff=0;
		return PLUGIN_HANDLED;
	}
 
	if (vote_allowed()!=1) {
		selfmessage( "Vote not allowed at this time.");
		return PLUGIN_HANDLED;
	}
 
	vote_LAAllowed=0;
	set_timer("@LAVoteTimer",RVfreq,1);
	LAVote();
 
	return PLUGIN_HANDLED;
}
#endif
 
#if ENABLE_LA==1
public admin_lasers(HLCommand,HLData,HLUserName,UserIndex) {
	new Command[MAX_COMMAND_LENGTH];
	new Data[MAX_DATA_LENGTH];
	new User[MAX_NAME_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new strNumber[MAX_NUMBER_LENGTH];
	new maxplayers = maxplayercount();
	new SessionID;
	new Target[MAX_NAME_LENGTH];
	new Team;
	new WONID;
	new i;
	convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
	convert_string(HLData,Data,MAX_DATA_LENGTH);
	convert_string(HLUserName,User,MAX_NAME_LENGTH);
 
	if (LaserYes==0) {
		snprintf(Text, MAX_TEXT_LENGTH, "ADMIN has enabled laserbeams", strNumber);
		centersay(Text,10,0,255,0);
		for(i=1; i<=maxplayers; i++) {
			strinit(Target);
			if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
				messageex(Target,Text,print_console);
			}
		}
		LaserON();
	} else {
		snprintf(Text, MAX_TEXT_LENGTH, "ADMIN has disabled laserbeams", strNumber);
		centersay(Text,10,0,255,0);
		for(i=1; i<=maxplayers; i++) {
			strinit(Target);
			if(playerinfo(i,Target,MAX_NAME_LENGTH,SessionID,WONID,Team)==1) {
				messageex(Target,Text,print_console);
			}
		}
		LaserOFF();
	}
	return PLUGIN_HANDLED;
}
#endif
 
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 ENABLE_LA_STATUS==1
	new Match = FALSE;
	if (strcasestr(Data, "laser")!=-1) {
		Match = TRUE;
	}
 
#endif
 
/****     What is Restricted??     ****/
#if ENABLE_LA_STATUS==1
	if (Match==TRUE) {
		if (LaserYes==1) { 
			centersay ("Laserbeams are currently on. ^n To change say vote_lasers. ",9,0,255,0);
		} else { 
			centersay ("Laserbeams are currently off. ^n To change say vote_lasers. ",9,0,255,0);
		}
	}
#endif
 
 
 
#if ENABLE_LA_VOTE==1
	if (strcasestr(Data, "vote_lasers")!=-1) {
		if (LAVoteOff==1) {
			say("Admin has disabled public use of vote_lasers.");
		} else {
			if (vote_LAAllowed != 1){
				say("A vote has just been completed, sorry if you didn't like the results.");
			} else {
				if (vote_allowed()!=1) {
					say("Vote not allowed at this time.");
				} else {
					vote_LAAllowed=0;
					set_timer("@LAVoteTimer",RVfreq,1);
					LAVote();
				}
			}
		}
	}
#endif	
	return PLUGIN_CONTINUE;
}
 
@LAVote() {
	if (vote_allowed()==1) {
		LAVote();
		say("[AutoVoteLasers] Vote Started");
	} else {
		set_timer("@LAVote",30,1);
		say("[AutoVoteLasers] Vote not allowed at this time");
		say("Server will retry in 30 secconds");
	}
}
 
 
public plugin_init() {
	plugin_registerinfo("Laserbeam plugin","vote for laserbeams on or off with autorespond status",STRING_VERSION);
	plugin_registercmd("say","HandleSay",ACCESS_ALL);
#if ENABLE_LA==1
	plugin_registercmd("admin_lasers","admin_lasers",ACCESS_LASERS,"admin_lasers : toggles laserbeams on and off.");
#endif
 
#if ENABLE_LA_VOTE==1
	plugin_registercmd("admin_vote_lasers","admin_vote_lasers",ACCESS_LASERS,"admin_vote_lasers : Starts a vote to enable laserbeams. off/on changes public use of vote_lasers");
	plugin_registerhelp("say",ACCESS_ALL,"say vote_lasers: Will start laserbeams vote and give status.");
#endif
 
#if ENABLE_LA_STATUS==1
	plugin_registerhelp("say",ACCESS_ALL,"say laser: Will give laserbeams status.");
#endif
 
#if AUTO_LA_VOTE==1	
	set_timer("@LAVote",450,1);
#endif
 
	return PLUGIN_CONTINUE;
}