/***************************************************************************************
 * plugin_logd_randomtimer.sma
 *      Oct  30, 2002,  9:50am -- Fixed typo which prevented it from working with AM
 *                                 2.50.50 (changed a TAB to a SPACE, thanks to Reofspa)
 *      Sept 16, 2002,  4:30pm -- Now announces to CT's that the timer is randomized
 *      June 16, 2002, 12:30am -- Fixed bug with enable/disable and quiet commands
 *                                 (thanks to "neuro" !!)
 *      May  26, 2002, 11:30pm -- Added admin_random_max and admin_random_min
 *      May  26, 2002,  2:15pm -- Fixed changing of the mp_c4timer cvar
 *      Feb  26, 2002,  1:30pm -- Fixed the Users array for proper dimensions (oops)
 *      Feb  25, 2002, 10:00pm -- Displays random fake cvar changes to hide the real one
 *      Feb  10, 2002,  4:00pm -- Clears the sonsole to hide the cvar change somewhat
 *      Jan  22, 2002,  9:30pm -- Automatically disables plugin on non-defuse maps
 *      Jan  20, 2002,  7:00pm -- Added ability to enable/disable timer announcement
 *      Jan  20, 2002, 12:40pm -- Original Version
 *
 * This plugin will set the c4 timer (Counter-Strike only) to a random number
 * from 15 to 90 seconds.  When a terrorist plants the bomb, he announces to his
 * team how much time is left (in seconds) before the bomb explodes, and CT's are warned
 * that the timer has been randomized. Admins will be able to enable/disable this plugin
 * with the command "admin_randomtimer <on/off>"
 *
 * Functions included:
 *  admin_randomtimer <1|on,0|off>
 *  admin_random_quiet <1|on,0|off>
 *  admin_random_max <MaxTimer>
 *  admin_random_min <MinTimer>
 * 
 * 
 * Bill Bateman aka "HunteR"
 * http://thepit.shacknet.nu
 * huntercc@hotmail.com
 ***************************************************************************************/
 
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_CONSOLE 131072
#define ACCESS_TIMER 576
#define DEFAULT_TIMER 45  // default value of the c4 timer
#define TEAM_T 1
#define TEAM_CT	2
 
new STRING_VERSION[MAX_DATA_LENGTH] = "1.00.5, RandomTimer v1.5.1";
new RandomEnabled = 1;                       // enables/disables the randomizer
new RandTimer = 0;
new MinTimer = 15;
new MaxTimer = 90;
new AnnounceEnabled = 1;                     // enables/disables announcement of timer length
new Users[MAX_PLAYERS][MAX_NAME_LENGTH];     // used for hiding the variable change in users' consoles
new OriginalTimer;                           // value of mp_c4timer that is initially used (usually from server.cfg)
 
 
public plugin_init() {
	plugin_registerinfo("Bomb Timer Randomizer","Randomizes the c4 timer for each round in a de_ map.",STRING_VERSION);
	plugin_registercmd("admin_randomtimer", "admin_randomtimer", ACCESS_TIMER, "admin_randomtimer <1|on,0|off>: Enables/disables the c4 randomizer; 1 = Enable");
	plugin_registercmd("admin_random_quiet", "admin_random_quiet", ACCESS_TIMER, "admin_random_quiet <1|on,0|off>: Disables/enables announcement of timer length; 1 = Disable");
	plugin_registercmd("admin_random_max", "admin_random_max", ACCESS_TIMER, "admin_random_max <MaxTimer>: Sets upper limit of timer to MaxTimer; takes place next round");
	plugin_registercmd("admin_random_min", "admin_random_min", ACCESS_TIMER, "admin_random_min <MinTimer>: Sets lower limit of timer to MinTimer; takes place next round");
 
	plugin_registercmd("logd_timerAnnounce", "logd_timerAnnounce", ACCESS_CONSOLE);
	exec( "logd_reg 60 admin_command logd_timerAnnounce" );
 
	new CurMapname[MAX_DATA_LENGTH];          // name of current map
	currentmap(CurMapname, MAX_DATA_LENGTH);  // get current mapname
	if ( strncasecmp(CurMapname,"de_",3) != 0 ) RandomEnabled = 0;  // disable plugin if not a de_ map
 
	// initialize the RandTimer variable before it's actually randomized
	RandTimer = getvar("mp_c4timer");	
	// save the inital value so we can restore it when plugin is disabled
	OriginalTimer = RandTimer;
 
	return PLUGIN_CONTINUE;
}
 
public admin_randomtimer(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	convert_string(HLData, Data, MAX_DATA_LENGTH);   //  <-- thanks to Neuro for making me realize I forgot this line
 
	if ( check_param(Data) == 1 ) {
		RandomEnabled = 1;
		selfmessage("[ADMIN] The bomb will now have a random timer each round.");
	} else {
		RandomEnabled = 0;
		snprintf(Text, MAX_TEXT_LENGTH, "mp_c4timer %i", OriginalTimer);
		exec(Text);
		selfmessage("[ADMIN] Random c4 timer has been disabled; timer set to original value.");
		log("RandomTimer >> Random c4 timer has been disabled; timer set to original value.");
	}
 
	return PLUGIN_HANDLED;
}
 
public admin_random_quiet(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	convert_string(HLData, Data, MAX_DATA_LENGTH);   //  <-- thanks to Neuro for making me realize I forgot this line
 
	if ( check_param(Data) == 0 ) {
		AnnounceEnabled = 1;
		selfmessage("[ADMIN] Timer length will be announced to all terrorists only when bomb is planted.");
	} else {
		AnnounceEnabled = 0;
		selfmessage("[ADMIN] Timer length will not be announced to anyone.");
	}
 
	return PLUGIN_HANDLED;
}
 
public admin_random_max(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	convert_string(HLData, Data, MAX_DATA_LENGTH);
 
	MaxTimer = strtonum(Data);
	selfmessage("[ADMIN] Upper limit of timer has now been changed.");
 
	return PLUGIN_HANDLED;
}
 
public admin_random_min(HLCommand,HLData,HLUserName,UserIndex) {
	new Data[MAX_DATA_LENGTH];
	convert_string(HLData, Data, MAX_DATA_LENGTH);
 
	MinTimer = strtonum(Data);
	selfmessage("[ADMIN] Lower limit of timer has now been changed.");
 
	return PLUGIN_HANDLED;
}
 
public logd_timerAnnounce(HLCommand,HLData,HLUserName,UserIndex) {
	if ( !RandomEnabled || !AnnounceEnabled ) 
		return PLUGIN_HANDLED;  // don't announce if not enabled
 
	new Data[MAX_DATA_LENGTH];
	new Text[MAX_TEXT_LENGTH];
	new sCommand[MAX_DATA_LENGTH];
	new sID[MAX_DATA_LENGTH];
 
	convert_string(HLData, Data, MAX_DATA_LENGTH);
	strbreak(Data, sID, Data, MAX_DATA_LENGTH);
 
	if(strcmp(Data,	"Planted_The_Bomb") != 0)
		return PLUGIN_HANDLED;
 
	new Name[MAX_NAME_LENGTH];
	new iID	= strtonum( sID	);
 
	if ( iID == 0 )
	{
		log("RandomTimer >> ERROR: iID returned 0");
		return PLUGIN_HANDLED;
	}
 
	if( !playerinfo(iID,Name,MAX_NAME_LENGTH) )
	{
		snprintf(Text, MAX_TEXT_LENGTH, "[RANDOMTIMER] ID ^"%i^" and Name ^"%s^" did not check out.", iID, Name);
		log(Text);
		return PLUGIN_FAILURE;
	}
 
	snprintf(sCommand, MAX_DATA_LENGTH, "say_team Team, bomb is set for %i seconds!!", RandTimer);
	execclient(Name,sCommand);
 
	say("CT's: bomb timer has been randomized! Find and defuse before it detonates!!");
 
	set_timer("randomizer", 1, 0);
 
	return PLUGIN_HANDLED;
}
 
/*
 5 / 26 / 02
 Originally this would change the cvar at beginning of each round,
 but I discovered later that this did not actually work... so now
 it changes the cvar right after the bomb is planted, and right after
 the bomb-planter announces the timer length
*/
public randomizer() {
	new sCommand[MAX_DATA_LENGTH];
	new TimerRange = MaxTimer - MinTimer;
 
	RandTimer = random(TimerRange);  // choose a value from 0 to (TimerRange - 1)
	RandTimer += MinTimer;  // re-adjust timer so it is within our min/max range
	snprintf(sCommand, MAX_DATA_LENGTH, "mp_c4timer %i", RandTimer);
	exec(sCommand);
	fool_the_user();
	// send random number of fake messages before we change the cvar, then send more after we change it:
	set_timer("fool_the_user", 1, 0);  // timer is necessary because the cvar change takes a small amount of time
 
	return PLUGIN_HANDLED;
}
 
public fool_the_user()
{
	new i,j;
	new maxFake = 3; // max number of fake messages
	new Text[MAX_TEXT_LENGTH];
	new fakeCvar;
 
	new fakeNum = random(maxFake)+1;  // number of fake messages to actually display
	for ( i=1; i<=fakeNum; i++ )
	{
		fakeCvar = random(MaxTimer - MinTimer) + MinTimer;  // make sure we're in the min/max range
		for ( j=1; j<=maxplayercount(); j++ )
		{
			snprintf(Text, MAX_TEXT_LENGTH, "^"mp_c4timer^" changed to ^"%i^"", fakeCvar);
			if ( check_user(Users[j]) ) messageex(Users[j], Text, print_console);
		}
	}
}
 
public plugin_connect(HLUserName, HLIP, UserIndex)
{
	convert_string(HLUserName, Users[UserIndex], MAX_NAME_LENGTH);
 
	return PLUGIN_CONTINUE;
}
 
public plugin_disconnect(HLUserName, UserIndex)
{
	strinit(Users[UserIndex]);
 
	return PLUGIN_CONTINUE;
}