/****************************************************************************
 * amx_ejl_smokin_and_drinkin.sma   version 1.2     Date: 3/19/2003
 *  Author: Eric Lidman      ejlmozart@hotmail.com
 *  Alias: Ludwig van        Upgrade: http://lidmanmusic.com/cs/plugins.html           
 *
 *  A fun plugin for punishing players or just having a good time by making
 *  them appear to be smoking and making them act drunk.
 *
 *  PODBOT: If using my podbots controller, podbots use this plugin too.
 *
 *  ADMIN COMMANDS:
 *
 *    amx_drunkmode <player> <time> : causes player to smoke and drink for time
 *    amx_drunkmode_all <time> : causes all players to smoke and drink for time
 *    amx_drinks <on|off> : Turns player drinking ability or off
 *    amx_drinks_d : sets the current drinking enabled mode as default 
 *
 *  PUBLIC COMMANDS:
 *
 *     say  /drink    :causes a player to get drunk for 60 seconds 
 *
 ***************************************************************************/

#include <amxmod>
#include <amxmisc>
new vault_value[128]

#define DRINKIN_TIME 60 // time in seconds for a player to get himself drunk

new smoke
new moves[4][] = {"+moveleft","+moveright","+back","+forward"}
new dcounter[33]
new moved[33]
new is_drunk[33]
new all_drunk
new wbottle
new bool:drinkin_on = true
new bool:dod_running

public admin_smokin(id,level,cid){
	if (!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED
	new name[32],tname[32]
	get_user_name(id,name,31)
	new arg2[8],arg[32],ccmd[32],drtime
	read_argv(0,ccmd,31)
	if(equali(ccmd[14],"a",1)){
		read_argv(1,arg2,31)	
	}else{
		read_argv(1,arg,31)
		read_argv(2,arg2,7)
	}
	drtime = strtonum(arg2) * 2
	if(drtime < 10 || drtime > 360)
		drtime = 10
	new idargs[2]
	idargs[1] = drtime
	if(equali(ccmd[14],"a",1)){
		if(all_drunk == 1){
			console_print(id,"[AMX] Sorry, everyone's already too drunk")
			return PLUGIN_HANDLED
		}
		all_drunk = 1
		dcounter[0] = 0	
		set_task(0.5,"smokin_a",0,idargs,2,"a",drtime)
		switch(get_cvar_num("amx_show_activity"))	{
			case 2:	client_print(0,print_chat,"ADMIN %s: Has made us all into smoking drunkards for %d seconds",name,drtime/2)
			case 1:	client_print(0,print_chat,"ADMIN: Has made us all into smoking drunkards for %d seconds",drtime/2)
		}		
	}else{
		new player = cmd_target(id,arg,1)
		if (!player) return PLUGIN_HANDLED
		get_user_name(player,tname,31)
		if(is_drunk[player] == 1){
			console_print(id,"[AMX] Sorry, %s is already too drunk",tname)
			return PLUGIN_HANDLED
		}
		idargs[0] = player
		is_drunk[player] = 1
		dcounter[player] = 0	
		set_task(0.5,"smokin_1",0,idargs,2,"a",drtime)

		switch(get_cvar_num("amx_show_activity"))	{
			case 2:	client_print(0,print_chat,"ADMIN %s: made %s a smoking drunkard for %d seconds",name,tname,drtime/2)
			case 1:	client_print(0,print_chat,"ADMIN: made %s a smoking drunkard for %d seconds",tname,drtime/2)
		}
	}
	new authid[16]
	get_user_authid(id,authid,15)
	log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" smokin-drinkin ^"%s^"",
		name,get_user_userid(id),authid,tname)
	return PLUGIN_HANDLED
}

public smokin_1(id[]){
	new cmd[16],dfov,a,b
	dcounter[id[0]] += 1
	if(is_user_alive(id[0])){
		new vec[3]
		get_user_origin(id[0],vec)
		new y1,x1
		x1 = random_num(-40,40)
		y1 = random_num(-40,40)
		//Smoke    
		message_begin( MSG_BROADCAST,SVC_TEMPENTITY) 
		write_byte( 5 ) // 5
		write_coord(vec[0]+x1) 
		write_coord(vec[1]+y1) 
		write_coord(vec[2]+30) 
		write_short( smoke ) 
		write_byte( 30 )  // 10
		write_byte( 10 )  // 10
		message_end()
		dfov = random_num(10,120)
		format(cmd,15,"default_fov %d",dfov)
		client_cmd(id[0],cmd)
		if(moved[id[0]]){
			client_cmd(id[0],"-moveleft;-moveright;-forward;-back")
			moved[id[0]] = 0
		}
		b = random_num(0,9)
		if(b == 1){
			a = random_num(0,3)				
			client_cmd(id[0],moves[a])
			moved[id[0]] = 1
			new aimvec[3] 
			new velocityvec[3] 
			new length 	 
			new speed = 500 
			get_user_origin(id[0],aimvec,2) 
			velocityvec[0]=aimvec[0]-vec[0] 
			velocityvec[1]=aimvec[1]-vec[1] 
			velocityvec[2]=aimvec[2]-vec[2] 
			length=sqrt(velocityvec[0]*velocityvec[0]+velocityvec[1]*velocityvec[1]+velocityvec[2]*velocityvec[2]) 
			velocityvec[0]=velocityvec[0]*speed/length 
			velocityvec[1]=velocityvec[1]*speed/length 
			velocityvec[2]=velocityvec[2]*speed/length 
			// TE_MODEL from HL-SDK common/const.h 
			message_begin(MSG_BROADCAST,SVC_TEMPENTITY) 
			write_byte(106) 
			write_coord(vec[0]) 
			write_coord(vec[1]) 
			write_coord(vec[2]+20) 
			write_coord(velocityvec[0]) 
			write_coord(velocityvec[1]) 
			write_coord(velocityvec[2]+100) 
			write_angle (0) 
			write_short (wbottle) 
			write_byte (2) 
			write_byte (255) 
			message_end() 
		}
	}
	if(dcounter[id[0]] >= id[1]){
		if(moved[id[0]]){
			client_cmd(id[0],"-moveleft;-moveright;-forward;-back")
		}
		if(is_drunk[id[0]] == 1){

			new tname[32]
			get_user_name(id[0],tname,31)	
			client_print(0,print_chat,"[AMX] %s has sobered up",tname)
		}
		is_drunk[id[0]] = 0
		client_cmd(id[0],"default_fov 90")
	}
	return PLUGIN_CONTINUE
}

public smokin_a(id[]){
	new cmd[16],dfov,a,b
	dcounter[0] += 1
	new iMaxPlayers = get_maxplayers()+1
	for (new i = 1; i <= iMaxPlayers; i++) {
		if(is_user_alive(i)){
			new vec[3]
			get_user_origin(i,vec)
			new y1,x1
			x1 = random_num(-40,40)
			y1 = random_num(-40,40)
			//Smoke    
			message_begin( MSG_BROADCAST,SVC_TEMPENTITY) 
			write_byte( 5 ) // 5
			write_coord(vec[0]+x1) 
			write_coord(vec[1]+y1) 
			write_coord(vec[2]+30) 
			write_short( smoke ) 
			write_byte( 30 )  // 10
			write_byte( 10 )  // 10
			message_end()

			dfov = random_num(10,120)
			format(cmd,15,"default_fov %d",dfov)
			client_cmd(i,cmd)
			if(moved[i]){
				client_cmd(i,"-moveleft;-moveright;-forward;-back")
				moved[i] = 0
			}
			b = random_num(0,9)
			if(b == 1){
				a = random_num(0,3)				
				client_cmd(i,moves[a])
				moved[i] = 1

				new aimvec[3] 
				new velocityvec[3] 
				new length 	 
				new speed = 500 
				get_user_origin(i,aimvec,2) 
				velocityvec[0]=aimvec[0]-vec[0] 
				velocityvec[1]=aimvec[1]-vec[1] 
				velocityvec[2]=aimvec[2]-vec[2] 
				length=sqrt(velocityvec[0]*velocityvec[0]+velocityvec[1]*velocityvec[1]+velocityvec[2]*velocityvec[2]) 
				velocityvec[0]=velocityvec[0]*speed/length 
				velocityvec[1]=velocityvec[1]*speed/length 
				velocityvec[2]=velocityvec[2]*speed/length 

				// TE_MODEL from HL-SDK common/const.h 
				message_begin(MSG_BROADCAST,SVC_TEMPENTITY) 
				write_byte(106) 
				write_coord(vec[0]) 
				write_coord(vec[1]) 
				write_coord(vec[2]+20) 
				write_coord(velocityvec[0]) 
				write_coord(velocityvec[1]) 
				write_coord(velocityvec[2]+100) 
				write_angle (0) 
				write_short (wbottle) 
				write_byte (2) 
				write_byte (255) 
				message_end() 
			}
		}
	}
	if(dcounter[0] >= id[1]){
		for (new i = 1; i <= iMaxPlayers; i++) {
			if(moved[i])
				client_cmd(i,"-moveleft;-moveright;-forward;-back")
		}
		if(all_drunk == 1){
			client_print(0,print_chat,"[AMX] Ok, were all sobered up now.")
		}
		all_drunk = 0	
		client_cmd(0,"default_fov 90")
	}
	return PLUGIN_CONTINUE
}

public handle_botsayplugin(){
	new sarg2[192]
	read_args(sarg2,191)
	new arg1[8]
	read_argv(2,arg1,7)
	new id = strtonum(arg1)
	if(is_user_bot(id)){
		new OnlySay[128]
		new len = strlen(sarg2)
		new start = contain(sarg2,":    ")
		copy(OnlySay,len-start+5,sarg2[start+5])
		if(equal(OnlySay,"/drink")){
			if(drinkin_on == false)
				return PLUGIN_CONTINUE
			if(is_drunk[id] == 1)
				return PLUGIN_CONTINUE
			new idargs[2]
			idargs[0] = id
			idargs[1] = DRINKIN_TIME * 2
			is_drunk[id] = 1
			dcounter[id] = 0	
			set_task(0.5,"smokin_1",0,idargs,2,"a",DRINKIN_TIME * 2)
			client_cmd(0,"play gnyso/L2_drink")
		}
	}
	return PLUGIN_CONTINUE
}

public make_me_drunk(id){
	if(drinkin_on == false){
		client_print(id, print_chat, "[AMX] Sorry, drinking has been disabled by admin")
		return PLUGIN_CONTINUE
	}
	if(is_drunk[id] == 1){
		client_print(id,print_chat,"[AMX] Sorry, I am already too drunk")
		return PLUGIN_HANDLED
	}
	new idargs[2]
	idargs[0] = id
	idargs[1] = DRINKIN_TIME * 2
	is_drunk[id] = 1
	dcounter[id] = 0	
	set_task(0.5,"smokin_1",0,idargs,2,"a",DRINKIN_TIME * 2)
	client_print(id,print_chat,"[AMX] Ok, you are drunk for 60 seconds")
	client_cmd(0,"play gnyso/L2_drink")
	return PLUGIN_CONTINUE
}

public sqrt(num) { 
	new div = num 
	new result = 1 
	while (div > result) { // end when div == result, or just below 
		div = (div + result) / 2 // take mean value as new divisor 
		result = num / div 
	} 
	return div 
} 

public admin_drinks(id,level,cid){
	if (!cmd_access(id,level,cid,1))
		return PLUGIN_HANDLED

	if (read_argc() < 2){
		new onoff[4]
		if(drinkin_on == true){
			copy(onoff, 4, "ON")
		}else{
			copy(onoff, 4, "OFF")
		}
		client_print(id,print_console,"[AMX] Usage: amx_drinking < on | off >     Currently: %s", onoff)
		return PLUGIN_HANDLED
	}

	new arg[10]
	read_argv(1,arg,10)

	if ( (equali(arg,"on", 2)) || (equal(arg,"1", 1)) ){
		drinkin_on = true
		console_print(id,"[AMX] Drinking mode is now ON")
		client_print(0,print_chat,"[AMX] Admin has turned Drinking mode ON")
	}else{
		drinkin_on = false
		console_print(id,"[AMX] Drinking mode is now OFF")
		client_print(0,print_chat,"[AMX] Admin has turned Drinking mode OFF")
	}

	new authid[16],name[32]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)

	log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" drinkmod_mode %s",name,get_user_userid(id),authid,arg)

	return PLUGIN_HANDLED
}

public amx_drinks_d(id,level,cid){
	if (!cmd_access(id,level,cid,1))
		return PLUGIN_HANDLED

	if(drinkin_on == false){
		ejl_vault("WRITE","DRINKIN","off")
		console_print(id,"[AMX] Drinking mode OFF   is now the default.")
	}else{
		ejl_vault("WRITE","DRINKIN","on")
		console_print(id,"[AMX] Drinking mode ON    is now the default.")
	}

	new authid[16],name[32]
	get_user_authid(id,authid,16)
	get_user_name(id,name,32)

	log_to_file("addons/amx/admin.log","^"%s<%d><%s><>^" drinkmod_d",name,get_user_userid(id),authid)

	return PLUGIN_HANDLED
}

public plugin_precache(){
	new mod_name[32]
	get_modname(mod_name,31)
	dod_running = equal(mod_name,"dod") ? true : false
	if(dod_running)
		smoke = precache_model("sprites/smoke.spr")
	else
		smoke = precache_model("sprites/steam1.spr")
	if(file_exists("models/tuckerman_pale_ale_v1.mdl")==1){
		wbottle = precache_model("models/tuckerman_pale_ale_v1.mdl")
	}
	else if(file_exists("models/winebottle.mdl")==1){
		wbottle = precache_model("models/winebottle.mdl")
	}else{
		wbottle = precache_model("models/can.mdl")		
	}
	return PLUGIN_CONTINUE 
} 

public plugin_init(){
	register_plugin("Smokin and Drinkin","1.2","EJL")
	register_clcmd("say /drink","make_me_drunk")
	register_clcmd("say drink","make_me_drunk")
	register_concmd("amx_drinks", "admin_drinks",ADMIN_LEVEL_H, "<on|off>: Turns player drinking ability or off.")
	register_concmd("amx_drinks_d","amx_drinks_d",ADMIN_RCON,": sets the current drinkind enabled mode as default")
	register_concmd("amx_drunkmode_all","admin_smokin",ADMIN_LEVEL_H," <time> : cause all players to go into a drunken state for time")
	register_concmd("amx_drunkmode","admin_smokin",ADMIN_LEVEL_H," <target> <time> : causes player to get drunk for time")
	register_srvcmd("botsay","handle_botsayplugin")
	register_cvar("amx_luds_drunkmode","v1.1",FCVAR_SERVER )

	ejl_vault("READ","DRINKIN","")
	if(equali(vault_value,"off"))
		drinkin_on = false
	else
		drinkin_on = true

	return PLUGIN_CONTINUE
}

public ejl_vault(rw[],key[],value[]){
	new data[192]
	new stxtsize = 0
	new line = 0 
	new skip = 0
	new vkey[64]
	new vvalue[128]
	new vaultwrite[192]
	if(equal(rw,"READ")){
		if(file_exists("addons/amx/ejl_vault.ini") == 1){
			copy(vault_value,128,"")
			while((line=read_file("addons/amx/ejl_vault.ini",line,data,192,stxtsize))!=0){
				parse(data,vkey,64,vvalue,128)
				if(equal(vkey,key)){
					copy(vault_value,128,vvalue)
				}
			}
		}else{
			write_file("addons/amx/ejl_vault.ini", "**** Plugins use to store values -- immune to crashes and map changes ****", 0)
		}
	}
	else if(equal(rw,"WRITE")){
		if(file_exists("addons/amx/ejl_vault.ini") == 1){		
			format(vaultwrite,192,"%s %s",key,value)
			while((line=read_file("addons/amx/ejl_vault.ini",line,data,192,stxtsize))!=0){
				parse(data,vkey,64,vvalue,128)
				if(skip == 0){
					if( (equal(data,"")) || (equal(vkey,key)) ){
						skip = 1
						write_file("addons/amx/ejl_vault.ini",vaultwrite,line-1)
					}
				}
				else if(equal(vkey,key)){
					write_file("addons/amx/ejl_vault.ini","",line-1)
				}
			}
			if(skip == 0){
				write_file("addons/amx/ejl_vault.ini",vaultwrite,-1)
			}
		}
	}
	return PLUGIN_CONTINUE
}


