/*
				[ZP] Extra Server Addon : Buy Message
						
						by Fry!


	Description :

			With this plugin you can buy Hud message, when you press 'Y' and type '/buymsg - (your text)' in chat, it will be showed as Hud message for all players.
			Of course it will cost to you some ammo packs. If you don't want to buy HUD messages then type your text as usually.

			
	Cvars :
			
			zp_buymsg_cost "2"	- How much this message will cost of your ammo packs.
			zp_buymsg_dead_cost "5" - How much this message will cost to dead players.
			zp_buymsg_dead <1|0>	- Turn on or turn off if dead players can buy these messages too, default is 1.
			zp_buymsg_anonym <1|0>	- Turn on or turn off then all players will be shown with names, default is 1.
			zp_buymsg_time "8"	- How long this message will be shown on screen.
			zp_buymsg_red "255"	- Ammount of RED (0 - 255)
			zp_buymsg_blue "100"	- Ammount of BLUE (0 - 255)
			zp_buymsg_green "255"	- Ammount of GREEN (0 - 255)

	
	Credits :

			OVERLOAD - for his Buy$Message plugin.


	Changelog :

			16/11/2008 - v1.0 - First release
			22/06/2009 - v1.0.4 - Removed toggle cvar, changed plugin name, fixed chat you couldn't say nothing public without buying HUD msg, optimized code.
*/

#include <amxmodx>
#include <zombieplague>

#define PLUGIN 	"[ZP] Extra Server Addon : Buy Message"
#define VERSION "1.0.4"
#define AUTHOR	"Fry!"

new sender_name[20], chat_message[192], chat_message_final[192]
new g_buymsg_cost, g_buymsg_dead_cost, g_buymsg_dead, g_buymsg_anonym, g_buymsg_time, 
g_buymsg_red, g_buymsg_blue, g_buymsg_green

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_cvar("zp_extra_server_addon_buymsg",VERSION,FCVAR_SERVER|FCVAR_EXTDLL|FCVAR_UNLOGGED|FCVAR_SPONLY)
	
	g_buymsg_cost = register_cvar("zp_buymsg_cost", "2")
	g_buymsg_dead_cost = register_cvar("zp_buymsg_dead_cost", "5")
	g_buymsg_dead = register_cvar("zp_buymsg_dead", "1")
	g_buymsg_anonym = register_cvar("zp_buymsg_anonym", "1")    
	g_buymsg_time = register_cvar("zp_buymsg_time", "8")
	g_buymsg_red = register_cvar("zp_buymsg_red", "255")
	g_buymsg_blue = register_cvar("zp_buymsg_blue", "100")
	g_buymsg_green = register_cvar("zp_buymsg_green", "255")  
	
	register_clcmd("say", "buymsg")
}


public buymsg(id)
{
	read_argv(id, chat_message, 191)
	
	if(equali(chat_message, "/buymsg -", 8))
	{
		if(task_exists(2, 0) == 1)
		{
			client_print(id, print_chat, "[ZP - BuyMessage] Please Wait a Moment, There is Already a Message Being Displayed.") 
			return PLUGIN_CONTINUE
		}
	}
	
	if(!equali(chat_message, "/buymsg -", 8))
		return PLUGIN_CONTINUE
	
	if (is_user_alive(id))
	{
		if (zp_get_user_ammo_packs(id) < get_pcvar_num(g_buymsg_cost))
		{
			client_print(id, print_chat,"[ZP - BuyMessage] Not enough ammo packs. You need at least %i ammo packs.", get_pcvar_num(g_buymsg_cost)) 
			return PLUGIN_CONTINUE
		}
     
		zp_set_user_ammo_packs(id, (zp_get_user_ammo_packs(id) - get_pcvar_num(g_buymsg_cost)))
		client_print(id, print_chat,"[ZP - BuyMessage] Bought One HUD Message for %i ammo packs.", get_pcvar_num(g_buymsg_cost)) 
		buymsg_hud(id)
		
		return PLUGIN_CONTINUE
	}
    
	if (!is_user_alive(id))
	{
		if (get_pcvar_num(g_buymsg_dead) == 0)
		{
			client_print(id, print_chat,"[ZP - BuyMessage] 'Dead Speak' is Disabled.") 
			return PLUGIN_CONTINUE
		}
      
		if (zp_get_user_ammo_packs(id) < get_pcvar_num(g_buymsg_dead_cost))
		{
			client_print(id, print_chat,"[ZP - BuyMessage] Not enough ammo packs. You need at least %i ammo packs, when you are Dead.", get_pcvar_num(g_buymsg_dead_cost)) 
			return PLUGIN_CONTINUE
		}
      
		zp_set_user_ammo_packs(id, (zp_get_user_ammo_packs(id) - get_pcvar_num(g_buymsg_dead_cost)))
		client_print(id, print_chat,"[ZP - BuyMessage] Bought one HUD message for %i ammo packs.", get_pcvar_num(g_buymsg_dead_cost)) 
		buymsg_hud(id)
	}
	return PLUGIN_CONTINUE
}

public buymsg_hud(id)
{
	chat_message_final = chat_message
	if (get_pcvar_num(g_buymsg_anonym) == 0)
	{
		set_task(1.0,"buymsg_hud_anonymous",2,"",0,"a", get_pcvar_num(g_buymsg_time))
		return PLUGIN_CONTINUE
	}
  
	get_user_name(id, sender_name, 20)
	set_task(1.0,"buymsg_hud_normal",2,"",0,"a", get_pcvar_num(g_buymsg_time))
  
	return PLUGIN_CONTINUE
}

public buymsg_hud_normal()
{
	set_hudmessage(get_pcvar_num(g_buymsg_red), get_pcvar_num(g_buymsg_green), get_pcvar_num(g_buymsg_blue), 0.01, 0.50, 2, 0.01, 1.1, 0.0, 0.0, 170)
	show_hudmessage(0, "%s: %s", sender_name, chat_message_final)
	
	return PLUGIN_CONTINUE
}

public buymsg_hud_anonymous()
{	
	set_hudmessage(get_pcvar_num(g_buymsg_red), get_pcvar_num(g_buymsg_green), get_pcvar_num(g_buymsg_blue), 0.01, 0.50, 2, 0.01, 1.1, 0.0, 0.0, 170)
	show_hudmessage(0, "Anonymous: %s", chat_message_final)
	
	return PLUGIN_CONTINUE
}
