
/***************************************************************************
 *  amx_ejl_matrix.sma
 *   version 1.2         Date: 1/3/2002
 *   Author: Eric Lidman     ejlmozart@hotmail.com
 *   Alias: Ludwig van       Upgrade: http://lidmanmusic.com/cs/plugins.html
 *          
 *  A partial conversion to AMX of adminmod matrix plugin by "Shady Milmman".
 *  Matrix style jumping and voting for it can only be enabled by admin.
 *  
 *  Commands:
 *   amx_matrix      -- toggles on and off matrix jumping
 *   amx_votematrix  -- starts a vote for matrix style jumping
 *   stopvote        -- stops a vote in progress
 *
 *  To do a matrix jump, jump, and move back -- you will shoot forward in 
 *   a way simialr to the way they jump in the movie, The Matrix.
 *
 ***************************************************************************/


#include <amxmod>

new vault_value[128]
new matrix_cvar[16]

new votenumber = 0
new votecontroller[33][2]
new option[4]
new Float:vote_ratio
new totalvoters = 0
new bool:bVoteToStop = false
new st_vote

public MXVote(id) {

	new Float:voting = get_cvar_float("amx_last_voting")
	if (voting > get_gametime()){ 
		console_print(id,"There is already one voting...") 
		return PLUGIN_HANDLED 
	} 
	if (voting && voting + get_cvar_float("amx_vote_delay") > get_gametime()) { 
		console_print(id,"Voting not allowed at this time") 
		return PLUGIN_HANDLED 
	}

	new inprogress[32]
	get_cvar_string("amx_vote_inprogress",inprogress,32)
	if(equal(inprogress, "0",1)){
	new menu_msg[256]
	new keys
	format(menu_msg,256,"\yDo you want Matrix style jumping? \w^n^n1.  Hand me the red pill!^n2.  No thanks")
	keys = (1<<0)|(1<<1)

	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><>^" vote matrix",
		name,get_user_userid(id),authid)
	new Float:vote_time = get_cvar_float("amx_vote_time") + 2.0 
	set_cvar_float("amx_last_voting",  get_gametime() + vote_time )
	vote_ratio = get_cvar_float("amx_matrixvote_ratio")
	for(new a = 0; a < 33; a++){
		votecontroller[a][1] = 0
	}
	votenumber++
	set_cvar_string("amx_vote_inprogress","1")
	ejl_vault("WRITE","CURRENT_VOTE","MATRIX")
	bVoteToStop = true

	totalvoters = 0
	new Team[33]
	new maxpl = get_maxplayers()+1
	for(new k = 1; k < maxpl; k++){
		get_user_team(k,Team,33)
		if(!((equali(Team,"UNAS",4)) || (equali(Team,"SPEC",4)) || (get_user_time(k)== 0) )){
			show_menu(k,keys,menu_msg,floatround(vote_time))
			totalvoters++
		}
	}
	st_vote = 0	
	set_task(vote_time,"HandleMXVote")
	client_print(id,print_console,"[AMX] Voting has started...")
	for(new j = 0; j < 4;++j) option[j] = 0
	}else{
	client_print(id,print_console,"[AMX] Voting not allowed yet.")	
	}
	return PLUGIN_HANDLED
}

public vote_count(id,key){
	ejl_vault("READ","CURRENT_VOTE","")
	if(equal(vault_value,"MATRIX")){
	new name[32]
	get_user_name(id,name,32)
	if(votenumber != votecontroller[id][0]){
		votecontroller[id][0] = votenumber
		if (get_cvar_float("amx_vote_answers")) {
			client_print(0,print_chat,"[AMX] %s voted for option # %d",name,key+1)
		}
		option[key] += 1
	}else{
		votecontroller[id][1] +=1
	}
	}else{
		return PLUGIN_CONTINUE
	}
	return PLUGIN_HANDLED
}

public HandleMXVote() {

	get_cvar_string("sv_airaccelerate",matrix_cvar,16)
	set_hudmessage(63,187,239, -1.0, 0.70, 2, 0.02, 10.0, 0.01, 0.1, 4)

	set_cvar_string("amx_vote_inprogress","0")
	ejl_vault("WRITE","CURRENT_VOTE","NULL")
	bVoteToStop = false
	for(new a = 0; a < 33; a++){
		votecontroller[a][1] = 0
	}

	if(st_vote == 1){
		st_vote = 0
		client_print(0,print_chat,"[AMX]  Voting sucessfully stopped.")
		return PLUGIN_HANDLED
	}

	new best = 0
	new best_count = 0
	for(new a = 0; a < 4; ++a) {
		if (option[a] > best_count){
			best_count = option[a]
			best = a
		}
	}
	new inum = totalvoters
	new Float:result_v = inum ? (float(option[best]) / float(inum)) : 0.0
	if (result_v >= vote_ratio) {
		if(best == 0){
			if(strtonum(matrix_cvar) == -15) {
 				show_hudmessage(0,"Voting Results: Matrix will stay !!!^n^n%d votes to keep Matrix style jumping enabled^n%d votes to disable Matrix style jumping^n^n%d Total eligible voters",
				option[0],option[1],totalvoters)
			} else {
 				show_hudmessage(0,"Voting Results: Welcome to the Matrix !!!^n^n%d votes to enable Matrix style jumping^n%d votes to keep normal jumping on^n^n%d Total eligible voters",
				option[0],option[1],totalvoters)
				set_cvar_string("sv_airaccelerate", "-15")
			}
			log_to_file("addons/amx/admin.log","World triggered ^"voting_success^" (needed ^"%.2f^") (ratio ^"%.2f^") (result ^"matrix^")",
				vote_ratio,result_v)
		}else {
			if( (strtonum(matrix_cvar) ==15) || (strtonum(matrix_cvar) ==10)  ){
 				show_hudmessage(0,"Voting Results: Normal jumping will remain^n^n%d votes to enable Matrix style jumping^n%d votes to keep normal jumping on^n^n%d Total eligible voters",
				option[0],option[1],totalvoters)
			} else {
				show_hudmessage(0,"Voting Results: Welcome back Neo.^n^n%d votes to keep Matrix style jumping enabled^n%d votes to turn jumping back to normal^n^n%d Total eligible voters",
				option[0],option[1],totalvoters)
				set_cvar_string("sv_airaccelerate", "15")
			}
			log_to_file("addons/amx/admin.log","World triggered ^"voting_failure^" (needed ^"%.2f^") (ratio ^"%.2f^")",
				vote_ratio,result_v)
		}
	}else{
		client_print(0,print_chat,"[AMX] Voting for matrix failed... No clear majority.")
	}
	return PLUGIN_HANDLED
} 

public client_connect(id){
	votecontroller[id][1] = 0
}

public client_disconnect(id){
	votecontroller[id][1] = 0
}

public stopvote(id){

	if (!(get_user_flags(id)&ADMIN_MAP)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	if(bVoteToStop == true){
		new name[32]
		get_user_name(id,name,32)
		st_vote = 1
		client_print(id,print_console,"[AMX]  ADMIN !!! Ignore the unknown command message -- its ok")
		client_print(0,print_chat,"[AMX]  %s has disabled the vote in progress.",name)
	}
	return PLUGIN_CONTINUE
}

public admin_vote_matrix(id) {
	if (!(get_user_flags(id)&ADMIN_VOTE)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}
	MXVote(id)
	return PLUGIN_HANDLED
}

public HandleSay(id) {
	new Speech[192]
	read_args(Speech,192)
	remove_quotes(Speech)
	if (containi(Speech, "matrix")!=-1) {
		get_cvar_string("sv_airaccelerate",matrix_cvar,16)
		set_hudmessage(0,255,0, -1.0, 0.25, 2, 0.02, 7.0, 0.01, 0.1, 2)
		if (strtonum(matrix_cvar) == -15){
			show_hudmessage(0,"Matrix jumping is on!")
		} else {
			show_hudmessage(0,"Boring normal jumping is on now.")
		}
	}
	return PLUGIN_CONTINUE
}

public admin_matrix(id) {

	if (!(get_user_flags(id)&ADMIN_VOTE)){
		client_print(id,print_console,"[AMX] You have no access to that command")
		return PLUGIN_HANDLED
	}

	if (read_argc() < 2){
		new onoff[4]
		get_cvar_string("sv_airaccelerate",matrix_cvar,16)
		if(strtonum(matrix_cvar) == -15){
			copy(onoff, 4, "ON")
		}else{
			copy(onoff, 4, "OFF")
		}
		client_print(id,print_console,"[AMX] Usage: amx_matrix < on | off >     Currently: %s", onoff)
		return PLUGIN_HANDLED
	}

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

	if ( (equal(arg,"on", 2)) || (equal(arg,"1", 1)) ){
		set_cvar_string("sv_airaccelerate", "-15")
		client_print(id,print_console,"[AMX] MATRIX JUMPING mode is now ON")
		client_print(0,print_chat,"[AMX] Admin has turned MATRIX JUMPING mode ON")
	}else{
		set_cvar_string("sv_airaccelerate", "15")
		client_print(id,print_console,"[AMX] MATRIX JUMPING mode is now OFF")
		client_print(0,print_chat,"[AMX] Admin has turned MATRIX JUMPING 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><>^" matrix_mode %s",
		name,get_user_userid(id),authid,arg)

	return PLUGIN_HANDLED
}

public plugin_init() {
	register_plugin("Matrix Plugin",".7","EJL")
	register_clcmd("amx_votematrix","admin_vote_matrix",ADMIN_VOTE,"amx_votematrix : Starts a vote to enable matrix style jumping.")
	register_clcmd("amx_matrix","admin_matrix",ADMIN_VOTE,"amx_matrix <#> 1 turns matrix on, 0 turns matrix jumping off.")
	register_clcmd("stopvote","stopvote")
	register_clcmd("say","HandleSay")
	register_cvar("amx_last_voting","0")
	set_cvar_float("amx_last_voting",0.0)

	register_menucmd(register_menuid("Do you want Matrix style jumping? "),(1<<0)|(1<<1)|(1<<2)|(1<<3),"vote_count")
	register_cvar("amx_matrixvote_ratio","0.70")
	register_cvar("amx_vote_inprogress","0")

	ejl_vault("WRITE","CURRENT_VOTE","NULL")
	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
}


