
/* AMX Mod script.
*
* (c) Copyright 2002, OLO
* This file is provided as is (no warranties).
*
*/

#include <amxmod>

/* commands:
*  check timeleft (available for all clients)
*  say timeleft
*
*  set time displaying
*  amx_time_display < flags time > ...
*  flags:
*  "a" - display text
*  "b" - use voice
*  "c" - don't add "remaining" (only in voice)
*  "d" - don't add "hours/minutes/seconds" (only in voice)
*  "e" - show/speak if current time is less than this set
*  example:
*  amx_time_display "ab 600" "ab 300" "ab 180" "ab 60" "bcde 11"
*/

new time_set[32][2]

public delayed_answer(text[]) {
	client_print(0,print_chat,text)
}

public say_thetime()
{
	new ctime[64], arg[96]
	get_time("%m/%d/%Y - %H:%M:%S",ctime,64)
	new len = format(arg,96,"[AMX] The time:   %s",ctime)
	set_task(0.1,"delayed_answer",0,arg,len + 1)
	return PLUGIN_CONTINUE
}

public say_timeleft(id)
{
	new  tmlf = get_timeleft()
	if (tmlf < 0) {
		set_task(0.1,"delayed_answer",0,"[AMX] No Time Limit",32)
	}
	else {
		new arg[128]
		new len = format(arg,128,"[AMX] Time Remaining:  %d:%02d", (tmlf / 60) , (tmlf % 60) )
		set_task(0.1,"delayed_answer",0,arg,len + 1)
	}
	return PLUGIN_CONTINUE
}

set_time_text(text[],len,tmlf)
{
	new secs = tmlf % 60
	new mins = tmlf / 60
	if (secs == 0)
		format(text,len,"%d minute%s", mins , (mins > 1) ? "s" : "" )
	else if (mins == 0)
		format(text,len,"%d second%s",  secs,(secs > 1) ? "s" : ""   )	
	else
		format(text,len,"%d minute%s %d second%s", mins , (mins > 1) ? "s" : "" ,secs ,(secs > 1) ? "s" : ""  )
}

set_time_voice(text[],len,flags,tmlf)
{
	new temp[7][32]
	for(new i = 0;i < 7;i++) temp[i][0] = 0 /* clear strings at the begining */
	new secs = tmlf % 60
	new mins = tmlf / 60
	if (secs > 0){
		num_to_word(secs,temp[4],32)
		if (!(flags & 8)) copy(temp[5],32, "seconds ") /* there is no "second" in voice */
	}
	if (mins > 59){
		new hours = mins / 60
		num_to_word(hours,temp[0],32)
		if (!(flags & 8)) copy(temp[1],32, "hours " )
		mins = mins % 60
	}
	if (mins > 0) {
		num_to_word(mins ,temp[2],32)
		if (!(flags & 8)) copy(temp[3],32,  "minutes " )
	}
	if (!(flags & 4)) copy(temp[6],32,"remaining")
	format(text,len,"spk ^"vox/%s%s%s%s%s%s%s^"", temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6] )
}

find_display_format(time)
{
	for(new i = 0;time_set[i][0];++i){
		if (time_set[i][1] & 16){
			if (time_set[i][0] > time)
				return i
		}
		else if (time_set[i][0] == time)
			return i
	}
	return -1
}

public set_displaying()
{
	new arg[32], flags[32], num[32]
	new argc = read_argc() - 1
	new i = 0
	while (i < argc){
		read_argv(i+1,arg,32)
		parse(arg,flags,32,num,32)
		time_set[i][0] = str_to_num(num)
		time_set[i][1] = read_flags(flags)
		i++;
	}
	time_set[i][0] = 0
	return PLUGIN_CONTINUE
}

public time_remaining()
{
	new tmlf = get_timeleft()
	if (tmlf > 0){
		new tm_set = find_display_format(tmlf)
		if ( tm_set != -1){
			new flags = time_set[tm_set][1]
			new arg[128]
			if (flags & 1){
				set_time_text(arg,128,tmlf)
				if (flags & 16)
					set_hudmessage(255, 255, 255, -1.0, 0.85, 0, 0.0, 1.1, 0.1, 0.5, 1)
				else
					set_hudmessage(255, 255, 255, -1.0, 0.85, 0, 0.0, 3.0, 0.0, 0.5, 1)
				show_hudmessage(0,arg)
			}
			if (flags & 2){
				set_time_voice(arg,128,flags,tmlf)
				client_cmd(0,arg)
			}
		}
	}
}

public plugin_init() 
{
	register_plugin("Time Left","0.5","default")
	register_srvcmd("amx_time_display","set_displaying")
	register_clcmd("say timeleft","say_timeleft")
	register_clcmd("say thetime","say_thetime")
	set_task(1.0,"time_remaining",0,"",0,"b")
	return PLUGIN_CONTINUE
}

