/*
	-----------------------------------------------

			[Licensing Info]
	
	Copyright (C) 2011  Catastrophe
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	-----------------------------------------------
	
*/



#include <amxmodx>
#include <zombieplague>
#include <hamsandwich>


new iDamage[33]
new bool:is_in_dd[33]
new g_MsgSync
new cvar_reduce_time, cvar_reduce_amount, cvar_dd_duration

public plugin_init()
{
       register_plugin("[ZP] Addon: Damage Bar","1.0","Catastrophe")
 
       RegisterHam(Ham_TakeDamage, "player", "fw_damage") 
       RegisterHam(Ham_Killed, "player", "fw_killed") 

       register_event("HLTV", "event_newround", "a", "1=0", "2=0")         
       
       cvar_reduce_time = register_cvar("zp_db_reduce_time","20") 
       cvar_reduce_amount = register_cvar("zp_db_reduce_amount","10")
       cvar_dd_duration = register_cvar("zp_dd_duration", "30.0")


       g_MsgSync = CreateHudSyncObj() 
}

public client_putinserver(id)
{

       iDamage[id] = 0
       is_in_dd[id] = false
       set_task(1.0, "checkhudmessage", id+672)       
       set_task(get_pcvar_float(cvar_reduce_time), "reddam", id+673)
       
}


public client_disconnect(id)
{
             
       iDamage[id] = 0
       is_in_dd[id] = false
       remove_task(id+672)

}

public event_newround()
{

    for( new id = 1; id <get_maxplayers(); id++)
       {

       iDamage[id] = 0
       is_in_dd[id] = false

       }

}

public zp_user_humanized_post(id, survivor)
{

      iDamage[id] = 0
      is_in_dd[id] = false

}

public zp_user_infected_post(id, infector, nemesis)
{

      iDamage[id] = 0
      is_in_dd[id] = false

}

public fw_damage(victim, inflictor, attacker, Float:damage, damage_bits)
{


    if( is_user_alive(victim)  && !zp_get_user_zombie(attacker) && is_in_dd[attacker])
    {


    SetHamParamFloat(4, damage * 2)

    }        

}

public fw_killed(victim, attacker, shoudgib)
{

    if( !zp_get_user_zombie(attacker) && iDamage[attacker] < 100 && !is_in_dd[attacker])
    {

    iDamage[attacker] += 10
    checkdd(attacker)
  

    }

}

public checkhudmessage(id)
{
    id -= 672; 
       
    if(!zp_get_user_zombie(id) && is_user_alive(id) && is_in_dd[id])
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + 100 %%]^n[Double damage Enabled]");   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 90)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[|||||||||-]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 80)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[||||||||--]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 70)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[|||||||---]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 60)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[||||||----]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 50)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[|||||-----]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 40)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[||||------]",  iDamage[id]);   
    
    }
 
    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 30)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[|||-------]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 20)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[||--------]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 10)
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[|---------]",  iDamage[id]);   
    
    }

    else if(!zp_get_user_zombie(id) && is_user_alive(id) && iDamage[id] == 0 && !is_in_dd[id])
    {


      set_hudmessage(255, 255, 0, -1.0, 0.85, 0, 6.0, 1.1, 0.0, 0.0, -1);  
      ShowSyncHudMsg(id, g_MsgSync, "[Damage Status: + %d %%]^n[----------]",  iDamage[id]);   
    
    }
 
 
    set_task(0.1, "checkhudmessage", id+672); 
 


}

public reddam(id)
{ 
    id -= 673;  

    if(get_pcvar_num(cvar_reduce_time) > 0 && iDamage[id] > 0)
    {  
    iDamage[id] -= get_pcvar_num(cvar_reduce_amount) 
    }
  
    set_task(get_pcvar_float(cvar_reduce_time), "reddam", id+673)

}

public checkdd(id)
{

       if(iDamage[id] == 100 && !is_in_dd[id])
       {

       iDamage[id] = 0 
      
       is_in_dd[id] = true
       set_task(get_pcvar_float(cvar_dd_duration),"removedd", id)
 
       }
  

}

public removedd(id)
{

      if( !is_in_dd[id])
          return;

      is_in_dd[id] = false

      set_hudmessage(180, 0, 0, -1.0, -1.0, 0, 6.0, 5.0)
      show_hudmessage(id, "Double damage Disabled !")

}

stock zp_colored_print(target, const message[], any:...)
{
	static buffer[512], msg_SayText = 0
	if( !msg_SayText ) msg_SayText = get_user_msgid("SayText")
	
	// Send to everyone
	if (!target)
	{
		static player, maxplayers, argscount
		maxplayers = get_maxplayers()
		argscount = numargs()
		
		for (player = 1; player <= maxplayers; player++)
		{
			// Not connected
			if (!is_user_connected(player))
				continue;
			
			// Remember changed arguments
			static arg_index, changed_args[20], changedcount // [20] = max LANG_PLAYER occurencies	
			changedcount = 0
			
			// Replace LANG_PLAYER with player id
			for (arg_index = 2; arg_index < argscount; arg_index++)
			{
				if (getarg(arg_index) == LANG_PLAYER && arg_index + 1 < argscount)
				{
					// Check if next param string is a registered language translation
					static lang_key[64], arg_subindex
					arg_subindex = 0
					while ((lang_key[arg_subindex] = getarg(arg_index + 1, arg_subindex++))) { /* keep looping */ }
					if (GetLangTransKey(lang_key) != TransKey_Bad)
					{
						setarg(arg_index, 0, player)
						changed_args[changedcount++] = arg_index
						arg_index++ // skip next argument since we know it's a translation key
					}
				}
			}
			
			// Format message for player (+add ZP prefix)
			vformat(buffer, charsmax(buffer), message, 3)
			format(buffer, charsmax(buffer), "%s%s", ZP_PREFIX, buffer)
			
			// Send it
			message_begin(MSG_ONE_UNRELIABLE, msg_SayText, _, player)
			write_byte(player)
			write_string(buffer)
			message_end()
			
			// Replace back player id's with LANG_PLAYER
			for (arg_index = 0; arg_index < changedcount; arg_index++)
				setarg(changed_args[arg_index], 0, LANG_PLAYER)
		}
	}
	// Send to specific target
	else
	{
		// Format message for player (+add ZP prefix)
		vformat(buffer, charsmax(buffer), message, 3)
		format(buffer, charsmax(buffer), "%s%s", ZP_PREFIX, buffer)
		
		// Send it
		message_begin(MSG_ONE, msg_SayText, _, target)
		write_byte(target)
		write_string(buffer)
		message_end()
	}
}
































