#include <amxmodx>
#include <amxmisc>
#include <fun>
#include <cstrike>
#include <jailbreak>

#include <hamsandwich>
#include <fakemeta>
#include <engine>

#define PLUGIN "[JB] ZombiDay"
#define VERSION "1.0.6"
#define AUTHOR "Cypis"

native cs_set_player_model(id, newmodel[]);
new id_zabawa;

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	id_zabawa = jail_register_game("ZombiDay");
}

public plugin_precache()
{
	precache_model("models/player/zombi_cypis/zombi_cypis.mdl");
	precache_model("models/jb_cypis/v_zombi.mdl");
	
	precache_sound("ambience/the_horror2.wav");
}

public OnRemoveData(day)
{
	if(day == id_zabawa)
	{
		set_lights("#OFF");	
	
		ZombiRegister(false);
		CreateFog(0, .clear=true);
	}
}

public OnDayStartPre(day, szInfo[256], szInfo2[512], setting[10], gTimeRound)
{	
	static szTime[12];
	if(day == id_zabawa)
	{
		format_time(szTime, 11, "%M:%S", gTimeRound-60);
		formatex(szInfo2, 511, "Zasady:^n%s - Straznicy maja czas na zkampienie sie^n%s - Wiezniowie zaczynaja szukac^nKto zabije straznika dostaje FreeDaya w nastepnej rundzie", szTime, szTime);
		szInfo = "Dzisiaj jest ZombiDay";
		
		jail_set_prisoners_micro(true, true);
		jail_set_ct_hit_tt(true);
		jail_set_tt_hit_ct(true);

		CreateFog(0, random_num(100,150), 1, 1, 0.006);
		
		for(new i=1; i<=32; i++)
		{
			if(!is_user_connected(i) || !is_user_alive(i) || cs_get_user_team(i) != CS_TEAM_T)
				continue;
				
			UstawZombi(i);
		}
		
		setting[0] = 2;
		setting[1] = 1;
		setting[2] = 1;
		setting[4] = 1;
		setting[6] = 1;
		setting[7] = 2;
	}
}

public OnDayStartPost(day)
{
	if(day == id_zabawa)
	{
		ZombiRegister(true);
		jail_set_game_hud(60, "Rozpoczecie zabawy za");
	}
}

public OnGameHudEnd(day)
{
	if(day == id_zabawa)
	{
		set_lights("a");
		jail_open_cele();
			
		jail_set_ct_hit_tt(false);
		jail_set_tt_hit_ct(false);
			
		emit_sound(0, CHAN_AUTO, "ambience/the_horror2.wav", 1.0, ATTN_NORM, 0, PITCH_NORM);
	}
}

////////zombi////////
new HamHook:hSpawn, HamHook:hItemDeploy, HamHook:hKilled;
new fmTouch, fmPlayerPreThink;

public ZombiRegister(bool:wartosc)
{
	if(wartosc)
	{
		if(!hSpawn)
			hSpawn = RegisterHam(Ham_Spawn, "player", "fwSpawn", 1);
		else
			EnableHamForward(hSpawn);
			
		if(!hItemDeploy)
			hItemDeploy = RegisterHam(Ham_Item_Deploy, "weapon_knife", "fwWeaponKnife", 1);
		else
			EnableHamForward(hItemDeploy);
				
		if(!hKilled)
			hKilled = RegisterHam(Ham_Killed, "player", "fwKilled", 1);
		else
			EnableHamForward(hKilled);
				
		if(!fmTouch)
			fmTouch = register_forward(FM_Touch, "fwTouch");
				
		if(!fmPlayerPreThink)
			fmPlayerPreThink = register_forward(FM_PlayerPreThink, "fwPlayerPreThink");
	}
	else
	{
		if(hSpawn)
			DisableHamForward(hSpawn);
		
		if(hItemDeploy)
			DisableHamForward(hItemDeploy);
			
		if(hKilled)
			DisableHamForward(hKilled);
			
		if(fmTouch)
		{
			unregister_forward(FM_Touch, fmTouch);
			fmTouch = 0;
		}
		
		if(fmPlayerPreThink)
		{
			unregister_forward(FM_PlayerPreThink, fmPlayerPreThink);
			fmPlayerPreThink = 0;
		}
	}
}

public fwKilled(id, attacker, shouldgib)
{	
	if(!is_user_connected(id) || !is_user_connected(attacker))
		return HAM_IGNORED;

	if(get_user_team(id) != 2 || get_user_team(attacker) != 1) 
		return HAM_IGNORED;
	
	jail_set_prisoner_free(attacker, false, true);
	client_print(attacker, 3, "[ZOMBI] Dostaniesz FreeDay'a w nastepnej rundzie!");
	return HAM_IGNORED;
}

new Float:g_wallorigin[33][3];
public fwTouch(id, world)
{
	if(!is_user_alive(id) || get_user_team(id) != 1)
		return;
        
	new classname[32];
	pev(world, pev_classname, classname, 31);
	if(equal(classname, "worldspawn") || equal(classname, "func_wall") || equal(classname, "func_breakable"))
		pev(id, pev_origin, g_wallorigin[id]);
}

public fwPlayerPreThink(id)
{
	if(!is_user_alive(id) || get_user_team(id) != 1)
		return;
		
	static button; 
	static flags; 
	button = get_user_button(id);
	flags = get_entity_flags(id);
	if(button & IN_DUCK && flags & FL_ONGROUND)
	{
		static Float:velocity[3];
		VelocityByAim(id, 800, velocity);
		velocity[2] = 210.0;
		set_pev(id, pev_velocity, velocity);
	}
	if(button & IN_USE)
	{
		static Float:origin[3];
		pev(id, pev_origin, origin);
		
		if(get_distance_f(origin, g_wallorigin[id]) > 25.0)     
			return;
	
		if(flags & FL_ONGROUND) 
			return;
	
		if(button & IN_FORWARD)
		{
			static Float:velocity[3];
			velocity_by_aim(id, 240, velocity);
			set_pev(id, pev_velocity, velocity);
		}
		else if(button & IN_BACK)
		{
			static Float:velocity[3];
			velocity_by_aim(id, -240, velocity);
			set_pev(id, pev_velocity, velocity);
		}
	} 
}

public fwWeaponKnife(ent)
{
	new id = pev(ent, pev_owner); //pev_owner daje opóźnienie i można tego użyć
	if(get_user_team(id) == 1)
		set_pev(id, pev_viewmodel2, "models/jb_cypis/v_zombi.mdl");
}

public fwSpawn(id)
{
	if(!is_user_alive(id) || get_user_team(id) != 1)
		return;
		
	UstawZombi(id);
}

UstawZombi(id)
{
	CreateFOV(id);
	
	jail_set_user_speed(id, 200.0);
	set_user_health(id, 1000);
	
	cs_set_user_nvg(id, 1);
	cs_set_player_model(id, "zombi_cypis");
	
	if(get_user_weapon(id) == CSW_KNIFE)
		set_pev(id, pev_viewmodel2, "models/jb_cypis/v_zombi.mdl");
}

stock CreateFOV(const index = 0, const number = 170)
{
	static msgFOV;
	if(msgFOV || (msgFOV = get_user_msgid("SetFOV")))
	{
		message_begin(index? MSG_ONE_UNRELIABLE: MSG_BROADCAST, msgFOV, .player = index);
		write_byte(number);
		message_end();
	}
}

stock CreateFog( const index = 0, const red = 127, const green = 127, const blue = 127, const Float:density_f = 0.001, bool:clear = false )
{
	static msgFog;
	if(msgFog || (msgFog = get_user_msgid("Fog")))
	{
		new density = _:floatclamp(density_f, 0.0001, 0.25) * _:!clear;

		message_begin(index? MSG_ONE_UNRELIABLE: MSG_BROADCAST, msgFog, .player = index);
		write_byte(clamp(red, 0, 255));
		write_byte(clamp(green, 0, 255));
		write_byte(clamp(blue, 0, 255));
		write_long(_:density);
		message_end();
	}
}
////////zombi////////
