#include <amxmodx>

new bool:showhud
new bool:dollo
new Float:linestart
new e_time

new sync,sync2
new onlyts
public plugin_init() {
	register_plugin("Bomb Timer Line","1.1","Sh!nE*")
	
	onlyts = register_cvar("btl_only_for_ts","1")
	
	register_event("HLTV","round_start","a","1=0","2=0")
	register_event("SendAudio","bomb_plantdef","a","2=%!MRAD_BOMBPL","2=%!MRAD_BOMBDEF")
	register_logevent("bomb_exploded",6,"2=triggered", "1=TERRORIST","3=Target_Bombed")
	
	e_time = get_cvar_pointer("mp_c4timer")
	
	sync = CreateHudSyncObj()
	sync2 = CreateHudSyncObj()
}

public client_putinserver(id) set_task(1.0,"check_team",id+12345,_,_,"b")
public client_disconnect(id) {remove_task(id+1111);remove_task(id+2222);remove_task(id+3333);remove_task(id+4444);}

public check_team(taskid) {
	new id = taskid - 12345
	new team = get_user_team(id)
	if(team != 0) {
		if((get_pcvar_num(onlyts) && team==1) || (!get_pcvar_num(onlyts))) {set_task(0.1,"showbomb",id+1111);set_task(0.1,"showline",id+2222);remove_task(taskid);}
	}
}

public round_start() {
	dollo=false
	showhud=false
	new players[32],num
	get_players(players,num)
	
	remove_task(12345)
	for(new i=0;i < num;i++) {remove_task(players[i]+1111);remove_task(players[i]+2222);remove_task(players[i]+3333);remove_task(players[i]+4444);}
	linestart = ((0.50 - (get_pcvar_num(e_time) * 0.01)) + ((get_pcvar_num(e_time) * 0.01 ) / 90))
}

public show_hud() {
	new players[32],num
	if(get_pcvar_num(onlyts))
		get_players(players,num,"e","TERRORIST")
	else
		get_players(players,num)
	
	showhud=true
	set_task(0.1,"add_line",12345)
	for(new i=0;i < num;i++) {set_task(0.1,"showbomb",players[i]+1111);set_task(0.1,"showline",players[i]+2222);}
}

public add_line() {
	if(showhud) {
		linestart += 0.001
		set_task(0.1,"add_line",12345)
	}
}

public remove_hud() {showhud=false;dollo=true;}

public showbomb(taskid) {
	if(showhud) {
		set_hudmessage(255,0,0,0.50,0.93,0,0.0,0.3,0.0,0.0)
		ShowSyncHudMsg(taskid-1111,sync,"|<<< C4")
		set_task(0.1,"showbomb",taskid)
	}
}

public showline(taskid) {
	if(showhud) {
		set_hudmessage(0,255,0,linestart,0.93,0,0.0,0.3,0.0,0.0)
		ShowSyncHudMsg(taskid-2222,sync2,">|")
		set_task(0.1,"showline",taskid)
	}
}

public bomb_plantdef(id) {
	new data[13]
	read_data(2,data,12)
	switch(data[11]) {
		case 'P':show_hud()
		case 'D':
		{
			set_task(3.0,"dollo_set")
			new players[32],num
			if(get_pcvar_num(onlyts))
				get_players(players,num,"e","TERRORIST")
			else
				get_players(players,num)
			
			remove_hud()
			for(new i=0;i < num;i++)
				set_task(0.1,"showdefused",players[i]+4444)
		}
	}
}

public bomb_exploded() {
	new players[32],num
	if(get_pcvar_num(onlyts))
		get_players(players,num,"e","TERRORIST")
	else
		get_players(players,num)
	
	remove_hud()
	for(new i=0;i < num;i++)
		set_task(0.1,"showexploded",players[i]+3333)
	
	set_task(3.0,"dollo_set")
}

public dollo_set() dollo=false

public showdefused(taskid) {
	if(dollo) {
		set_hudmessage(0,0,255,0.50,0.93,0,0.0,0.3,0.0,0.0)
		ShowSyncHudMsg(taskid-4444,sync,"[DEFUSED]")
		set_task(0.1,"showdefused",taskid)
	}
}

public showexploded(taskid) {
	if(dollo) {
		set_hudmessage(255,0,0,0.50,0.93,0,0.0,0.3,0.0,0.0)
		ShowSyncHudMsg(taskid-3333,sync,"[BOOM]")
		set_task(0.1,"showexploded",taskid)
	}
}

