#include <a_samp>

stock J_SetPlayerCheckpoint(playerid, Float:x, Float:y, Float:z, Float:radi)
{
	DisablePlayerCheckpoint(playerid);
	SetPlayerCheckpoint(playerid, x, y, z, radi);
	return true;
}

stock J_AddStaticVehicleEx(model,Float:x,Float:y,Float:z,Float:a,color_1,color_2,spawntime = 300,interior = 0,world = 0)
{
	new carid = AddStaticVehicleEx(model, x, y, z, a, color_1, color_2, spawntime);
	VehicleInfo[carid][vModel] = model;
	VehicleInfo[carid][vPos_X] = x;
	VehicleInfo[carid][vPos_Y] = y;
	VehicleInfo[carid][vPos_Z] = z;
	VehicleInfo[carid][vPos_A] = a;
	OCInfo[carid][ocPos_X][0] = x;
	OCInfo[carid][ocPos_Y][0] = y;
	OCInfo[carid][ocPos_Z][0] = z;
	OCInfo[carid][ocPos_X][1] = x;
	OCInfo[carid][ocPos_Y][1] = y;
	OCInfo[carid][ocPos_Z][1] = z;
	OCInfo[carid][ocPos_A] = a;
	VehicleInfo[carid][vColor_1] = color_1;
	VehicleInfo[carid][vColor_2] = color_2;
	VehicleInfo[carid][vSpawnTime] = spawntime;
	LinkVehicleToInterior(carid, interior);
	SetVehicleVirtualWorld(carid, world);
	VehicleInfo[carid][vTuning] = GetVehicleTuningState( model );
    switch(model)
	{
	    case 430, 446, 452, 453, 454, 472, 473, 484, 493: VehicleState[carid] = VEHICLE_STATE_BOAT;
		case 417, 425, 447, 460, 469, 476, 487, 488, 497, 511, 512, 513, 519, 520, 548, 553,
		563, 577, 592, 593:  VehicleState[carid] = VEHICLE_STATE_PLANE;
		case 448, 581, 522, 461, 521, 523, 463, 468, 471, 586: VehicleState[carid] = VEHICLE_STATE_BIKE;
		case 509, 481, 510: VehicleState[carid] = VEHICLE_STATE_VELIK;
		case 462: VehicleState[carid] = VEHICLE_STATE_MOPED;
		default: VehicleState[carid] = VEHICLE_STATE_CAR;
	}
	return carid;
}

stock J_SetPlayerSpecialAction(playerid, action)
{
    SetPlayerSpecialAction(playerid, action);
    ApplyAnim[playerid] = true;
    return true;
}

stock J_SetPlayerColor(playerid,color,bool:update = true)
{
	if(!GetPlayerInterior(playerid))
	{
	    if(PlayerInfo[playerid][pColor] != 0xFFFFFF00) DestroyPlayerMapDraws(playerid);
	    if(color != 0xFFFFFF00) CreatePlayerMapDraws(playerid);
    	if(update) UpdateHPBar(playerid);
    }
    PlayerInfo[playerid][pColor] = color;
    return true;
}

stock J_SetPlayerFacingAngle(playerid, Float:angle, bool:camera = false)
{
    SetPlayerFacingAngle(playerid, angle);
    if(camera) SetCameraBehindPlayerEx(playerid);
    return true;
}

stock J_ClearAnimations(playerid)
{
	new Float:X,Float:Y,Float:Z;
	GetPlayerPos(playerid,X,Y,Z);
	J_SetPlayerPos(playerid,X,Y,Z);
	ClearAnimations(playerid);
	return true;
}

stock J_GetPlayerDrunkLevel(playerid)
{
	return PlayerDrunkLevel[playerid];
}

stock J_CreateDynamicPickup(model, type, Float:x, Float:y, Float:z, world = -1, interior = -1, playerid = -1, Float:distance = 100.0)
{
    new pickup = CreateDynamicPickup(model, type, x, y, z, world, interior, playerid, distance);
    PickupInfo[pickup][ppModel] = model;
    PickupInfo[pickup][ppType] = type;
    PickupInfo[pickup][ppPosX] = x;
    PickupInfo[pickup][ppPosY] = y;
    PickupInfo[pickup][ppPosZ] = z;
    PickupInfo[pickup][ppWorld] = world;
    PickupInfo[pickup][ppInt] = interior;
    PickupInfo[pickup][ppPlayer] = playerid;
    PickupInfo[pickup][ppDistance] = distance;
    return pickup;
}

stock J_SetPlayerDrunkLevel(playerid,level)
{
	if(SetPlayerDrunkLevel(playerid,level))
	{
	    PlayerDrunkLevel[playerid] = level;
	    return true;
    }
    return false;
}

stock J_SetPlayerScore(playerid,score)
{
    if(SetPlayerScore(playerid,score))
    {
    	PlayerInfo[playerid][pLevel] = score;
    	return true;
    }
    return false;
}

stock J_SetPlayerAmmo(playerid, weaponid, ammo)
{
    new slot = GetWeaponSlot(weaponid), gunid, ammos;
    GetPlayerWeaponData(playerid, slot, gunid, ammos);
    if(gunid == weaponid)
    {
	    SetPlayerAmmo(playerid, weaponid, ammo);
	    AntiCheatOFF[playerid][GUN_HACK][slot] = 2;
	    GunAmmo[playerid][slot] = ammo;
	   	DamageAmmo[playerid][slot] = ammo;
	   	return true;
   	}
   	else
   	{
   	    for(new i; i < 3; i ++)
   	    {
   	        if(PlayerInfo[playerid][pWeapon][i] == weaponid)
   	        {
   	            PlayerInfo[playerid][pAmmo][i] = ammo;
   	        }
   	    }
   	}
   	return false;
}

stock J_ResetPlayerWeapons(playerid)
{
	if(IsNoAFK(playerid))
	{
	    if(ResetPlayerWeapons(playerid))
	    {
		    for(new i;i < 13;i ++)
		    {
	         	AntiCheatOFF[playerid][GUN_HACK][i] = 2;
		        GunID[playerid][i] = 0;
		        GunAmmo[playerid][i] = 0;
		    }
		    return true;
	    }
    }
    else
    {
        PlayerAtAFK[playerid][afkResGuns] = true;
        for(new i;i < 13;i ++)
		{
        	PlayerAtAFK[playerid][afkGunID][i] = 0;
        	PlayerAtAFK[playerid][afkGunAmmo][i] = 0;
        }
    }
    return false;
}

stock J_TogglePlayerControllable(playerid,bool:toggle)
{
	if(TogglePlayerControllable(playerid,toggle))
	{
		AntiCheatOFF[playerid][CONTROLLABLE_HACK] = 2;
		Controllable[playerid] = toggle;
		GetPlayerPos(playerid,PlayerInfo[playerid][pCPos_X],PlayerInfo[playerid][pCPos_Y],PlayerInfo[playerid][pCPos_Z]);
		return true;
	}
	return false;
}

stock J_PutPlayerInVehicle(playerid, vehicleid, seat)
{
	if(IsNoAFK(playerid))
	{
		if(PutPlayerInVehicle(playerid,vehicleid,seat))
		{
			EnterVehicle[playerid] = vehicleid;
			if(	GetPlayerVehicleID(playerid) != vehicleid ||
				GetPlayerVehicleSeat(playerid) != seat)
			{
				KillTimer(PutTimer[playerid]);
				PutTimer[playerid] = SetPlayerTimer(playerid,"PutInVehicle",2000,0);
				return true;
			}
		}
	}
	else
	{
	    PlayerAtAFK[playerid][afkPutCar][0] = vehicleid;
	    PlayerAtAFK[playerid][afkPutCar][1] = seat;
	}
	return false;
}

public RemoveFromVehicle(playerid)
{
	if(IsPlayerInAnyVehicle(playerid))
	{
 		J_TogglePlayerControllable(playerid,true);
  		if(RemovePlayerFromVehicle(playerid))
		{
  			KillTimer(RemoveCarTimer[playerid]);
 			RemoveCarTimer[playerid] = SetTimerEx("UnRemoved", 200, false, "ii", playerid, 0);
  		}
		return true;
	}
	return false;
}


stock J_SetPlayerHealth(playerid,Float:health)
{
	if(health < 0.0) health = 0.0;
	if(IsNoAFK(playerid))
	{
		if(SetPlayerHealth(playerid,health))
		{
			if(health < 1) INVALID_DEATH[playerid] = true;
			AntiCheatOFF[playerid][HEALTH_HACK] = 2;
			PlayerInfo[playerid][pHealth] = health;
			DamageHealth[playerid] = health;
			UpdateHPBar(playerid, health);
			//UpdatePlayerHealth(playerid, health);
			return true;
		}
	}
	else
	{
     	PlayerAtAFK[playerid][afkHealth] = health;
	}
	return false;
}

stock J_SetPlayerArmour(playerid,Float:armour)
{
    if(armour < 0.0) armour = 0.0;
    if(IsNoAFK(playerid))
	{
		if(SetPlayerArmour(playerid,armour))
		{
			AntiCheatOFF[playerid][ARMOUR_HACK] = 2;
			PlayerInfo[playerid][pArmour] = armour;
			DamageArmour[playerid] = armour;
			UpdateHPBar(playerid, -1, armour);
			//UpdatePlayerArmour(playerid, armour);
			return true;
		}
	}
	else
	{
	    PlayerAtAFK[playerid][afkArmour] = armour;
	}
	return false;
}

stock J_SetPlayerDamageHealth(playerid,Float:health)
{
	if(health < 0.0) health = 0.0;
	if(SetPlayerHealth(playerid,health))
	{
		AntiCheatOFF[playerid][HEALTH_HACK] = 2;
		PlayerInfo[playerid][pHealth] = health;
		DamageHealth[playerid] = health;
	//	UpdatePlayerHealth(playerid, health);
		UpdateHPBar(playerid, health);
		return true;
	}
	return false;
}

stock J_SetPlayerDamageArmour(playerid,Float:armour)
{
	if(armour < 0.0) armour = 0.0;
	if(SetPlayerArmour(playerid,armour))
	{
		AntiCheatOFF[playerid][ARMOUR_HACK] = 2;
		PlayerInfo[playerid][pArmour] = armour;
		DamageArmour[playerid] = armour;
		//UpdatePlayerArmour(playerid, health);
		UpdateHPBar(playerid, -1, armour);
		return true;
	}
	return false;
}

stock J_SetVehicleHealth(carid,Float:health)
{
    if(health < 0.0) health = 0.0;
	if(SetVehicleHealth(carid,health))
	{
		AntiCarHealthCheatOFF[carid] = 2;
		VehicleHealth[carid] = health;
		return true;
	}
	return false;
}

stock J_RepairVehicle(carid)
{
	if(RepairVehicle(carid))
	{
    	AntiCarHealthCheatOFF[carid] = 2;
		VehicleHealth[carid] = 1000;
		return true;
	}
	return false;
}

stock J_RefillVehicle(carid)
{
	Fuel[carid] = 100.0;
	return true;
}

stock GivePlayerWeaponEx(playerid, weaponid, ammo)
{
	new slot = GetWeaponSlot(weaponid);
	new gunid[13], ammos[13];
	CheckPlayerWeapons(playerid, gunid, ammos);
	if(weaponid == 24 && gunid[slot] == 23) weaponid = 23;
	else if(weaponid == 23 && gunid[slot] == 24) weaponid = 24;
	GivePlayerWeapon(playerid, weaponid, ammo);
	AntiCheatOFF[playerid][GUN_HACK][slot] = 2;
	GunID[playerid][slot] = weaponid;
	GunAmmo[playerid][slot] += ammo;
	DamageAmmo[playerid][slot] += ammo;
	return true;
}

stock CheckPlayerWeapons(playerid, weapons[13], ammos[13])
{
    for(new i; i < 13; i ++)
	{
		GetPlayerWeaponData(playerid, i, weapons[i], ammos[i]);
	}
	return true;
}

stock IsASecondWeapon(weaponid)
{
	switch(weaponid)
	{
	    case 0, 1, 44, 45, 46: return true;
	    default: return false;
	}
	return false;
}

stock ChangeDeagle(&weaponid, gunid)
{
    if(weaponid == 24 && gunid == 23) weaponid = 23;
	else if(weaponid == 23 && gunid == 24) weaponid = 24;
	return true;
}

stock PutPlayerWeapon(playerid, weaponid, ammo)
{
    if(IsASecondWeapon(weaponid) || !ammo) return -2;
	for(new i; i < 3; i ++)
	{
	    new gun = PlayerInfo[playerid][pWeapon][i];
	    new amount = PlayerInfo[playerid][pAmmo][i];
	    if(gun && gun != weaponid) continue;
	    if(gun == weaponid && (ammo + amount) > MaxAmmo[weaponid]) return -1;
	    PlayerInfo[playerid][pWeapon][i] = weaponid;
	    PlayerInfo[playerid][pAmmo][i] += ammo;
	    PutGun(playerid,PlayerInfo[playerid][pWeapon][i],i+7);
	    RemovePlayerWeaponEx(playerid, weaponid);
	    return 1;
	}
	return 0;
}

stock J_GivePlayerWeapon(playerid, weaponid, ammo)
{
	new slot = GetWeaponSlot(weaponid);
	new gunid[13], ammos[13];
	CheckPlayerWeapons(playerid, gunid, ammos);
	ChangeDeagle(weaponid, gunid[slot]);
	for(new i; i < 3; i ++)
	{
		if(PlayerInfo[playerid][pWeapon][i] == weaponid)
		{
		    if( (ammo + ammos[slot]) > MaxAmmo[weaponid] )
		    {
		        PlayerInfo[playerid][pAmmo][i] = MaxAmmo[weaponid];
		        return 3;
		    }
		    else PlayerInfo[playerid][pAmmo][i] += ammo;
		    return 4;
		}
	}
	new current = CurrentWeapon[playerid];
	if(!current || current == weaponid)
	{
	    GivePlayerGunEx(playerid, weaponid, ammo);
		return 1;
	}
	else if(!IsASecondWeapon(current))
	{
	    new put = PutPlayerWeapon(playerid, current, ammo);
		if(put == 1)
		{
			GivePlayerGunEx(playerid, weaponid, ammo);
			return 2;
		}
		else if( put != (-2) )
		{
		    new dgun = 500;
		    for(new i;i < 500;i ++)
		 	{
				if(DGunInfo[i][gunID] == 0)
				{
					dgun = i;
			    	break;
			 	}
			}
			if(dgun < 500)
			{
			    new s = GetWeaponSlot(current);
			    new model = GetWeaponModel(current);
			    AntiGunCheat(playerid);
			    DGunInfo[dgun][gunID] = current;
			    DGunInfo[dgun][gunAmmo] = ammos[s];
			    GetPlayerPos(playerid,DGunInfo[dgun][gunPos][0],DGunInfo[dgun][gunPos][1],DGunInfo[dgun][gunPos][2]);
				DGunInfo[dgun][gunObject] = CreateDynamicObject(model,DGunInfo[dgun][gunPos][0],DGunInfo[dgun][gunPos][1],DGunInfo[dgun][gunPos][2]-1, 93.7, 120.0, 120.0);
				RemovePlayerWeaponEx(playerid, current);
				GivePlayerGunEx(playerid, weaponid, ammo);
				GunReset[playerid] = 3;
			}
		}
	}
	return 0;
}

stock GivePlayerGunEx(playerid, weaponid, ammo)
{
    new slot = GetWeaponSlot(weaponid);
    if(IsNoAFK(playerid))
	{
	    new gunid, ammos;
	    AntiCheatOFF[playerid][GUN_HACK][slot] = 2;
	    GunID[playerid][slot] = weaponid;
		if(!IsASecondWeapon(weaponid))
		{
	    	CurrentWeapon[playerid] = weaponid;
	    }
	    GetPlayerWeaponData(playerid, slot, gunid, ammos);
	    if( (ammo + ammos) > MaxAmmo[weaponid] )
	    {
	        SetPlayerAmmo(playerid, weaponid, MaxAmmo[weaponid]);
			GunAmmo[playerid][slot] = MaxAmmo[weaponid];
			DamageAmmo[playerid][slot] = MaxAmmo[weaponid];
			return 2;
	    }
	    else
	    {
			GivePlayerWeapon(playerid, weaponid, ammo);
			GunAmmo[playerid][slot] += ammo;
			DamageAmmo[playerid][slot] += ammo;
			return 1;
		}
	}
	else
	{
		if(PlayerAtAFK[playerid][afkGunID][slot] == weaponid)
		{
	 		PlayerAtAFK[playerid][afkGunAmmo][slot] += ammo;
		}
		else
		{
	 		PlayerAtAFK[playerid][afkGunAmmo][slot] = ammo;
			PlayerAtAFK[playerid][afkGunID][slot] = weaponid;
		}
	}
	return -2;
}

public ChangePlayerPos(playerid, Float:X, Float:Y, Float:Z)
{
	SetPlayerPos(playerid, X, Y, Z);
	return true;
}

stock J_SetPlayerPos(playerid, Float:X, Float:Y, Float:Z, bool:freeze = true)
{
	if(IsNoAFK(playerid))
	{
	    GetPlayerPos(playerid, PlayerInfo[playerid][pOld_X], PlayerInfo[playerid][pOld_Y], PlayerInfo[playerid][pOld_Z]);
		if(SetPlayerPos(playerid,X,Y,Z))
		{
		    ChangeView(playerid, 1, 1);
		    Streamer_UpdateEx(playerid, X, Y, Z);
		    AntiCheatOFF[playerid][TELEPORT_HACK] = 2;
		    ReturnCordsEx(playerid, X, Y, Z);
			if(freeze)
			{
				TogglePlayerControllable(playerid, false);
				SetPlayerTimer(playerid, "UnFreeze", GetPlayerPing(playerid) * 2, false);
			}
	    }
    }
    else
    {
        PlayerAtAFK[playerid][afkPos][0] = X;
		PlayerAtAFK[playerid][afkPos][1] = Y;
		PlayerAtAFK[playerid][afkPos][2] = Z;
		PlayerAtAFK[playerid][afkRemovecar] = false;
		PlayerAtAFK[playerid][afkPutCar][0] = INVALID_VEHICLE_ID;
		PlayerAtAFK[playerid][afkPutCar][1] = -1;
  	}
	return true;
}

stock J_SetVehiclePos(vehicleid,Float:X,Float:Y,Float:Z)
{
    GetVehiclePos(vehicleid, OCInfo[vehicleid][ocPos_X][1], OCInfo[vehicleid][ocPos_Y][1], OCInfo[vehicleid][ocPos_Z][1]);
	if(SetVehiclePos(vehicleid,X,Y,Z))
	{
	    for(new i;i < GetMaxPlayersEx();i ++)
		{
		    if(!IsPlayerConnected(i)) continue;
		    if(IsPlayerInVehicle(i,vehicleid))
		    {
		        ChangeView(i, GetPlayerState(i), GetPlayerState(i));
		        AntiCheatOFF[i][TELEPORT_HACK] = 2;
		        ReturnCordsEx(i, X, Y, Z);
		        Streamer_UpdateEx(i, X, Y, Z);
		    }
		}
		OCInfo[vehicleid][ocPos_X][0] = X;
		OCInfo[vehicleid][ocPos_Y][0] = Y;
		OCInfo[vehicleid][ocPos_Z][0] = Z;
	    return true;
    }
    return false;
}

stock J_TogglePlayerSpectating(playerid,bool:toggle)
{
    Spectating[playerid] = toggle;
    if(!toggle)
	{
	    SettingSpawn(playerid);
		SetPlayerSpawn(playerid);
    	AntiCheatsOFF(playerid);
		OnSpawn[playerid] = true;
	}
	TogglePlayerSpectating(playerid,toggle);
    return true;
}

public J_RemovePlayerFromVehicle(playerid)
{
    if(IsPlayerNPC(playerid)) return true;
	if(IsPlayerInAnyVehicle(playerid))
	{
	    new carid = GetPlayerVehicleID(playerid);
	    new State = GetPlayerState(playerid);
	    if(IsNoAFK(playerid))
	    {
	        new speed = GetPlayerVehicleSpeed(playerid);
			if( speed < 35 || ( ( IsABike(carid) || IsAMoped(carid) ) && State == 2 && GetVehicleSeat(carid, 1) != INVALID_PLAYER_ID) )
			{
			    if( TogglePlayerControllable(playerid, false) )
			    {
			    	return SetPlayerTimer(playerid,"RemoveFromVehicle",500 + GetPlayerPing(playerid),0);
				}
			}
			else
			{
			    if(RemovePlayerFromVehicle(playerid))
				{
				    KillTimer(RemoveCarTimer[playerid]);
			    	RemoveCarTimer[playerid] = SetTimerEx("UnRemoved", 200, false, "ii", playerid, 0);
			    }
				return true;
			}
		}
		else
		{
		    PlayerAtAFK[playerid][afkRemovecar] = true;
		}
		return true;
	}
	return false;
}


stock J_CreateMapIcon(Float:X, Float:Y, Float:Z, style)
{
	CreateDynamicMapIcon(X, Y, Z, style, 0, -1, -1, -1, 1500.0);
	return true;
}

stock J_Hash(string[])
{
	new str[100];
	if(EvenNumber(string))
	{
		for(new i = 0;i < strlen(string);i ++)
		{
		    str[i] = string[i+1];
		    str[i+1] = string[i];
		    i ++;
		}
	}
	else
	{
	    new unstr = (strlen(string)-1);
	    for(new i = 0;i < unstr;i ++)
		{
		    str[i] = string[i+1];
		    str[i+1] = string[i];
		    i ++;
		}
		str[unstr] = string[unstr];
	}
	return str;
}

stock J_UnHash(string[])
{
	new str[100];
	for(new i = 0;i < strlen(string);i ++)
	{
	    new s = (strlen(string)-1);
	    str[i] = string[s-i] - Hash0x[i];
	}
	return str;
}

stock J_DestroyPickup(&pickup)
{
	if(pickup != -1)
	{
	    DestroyPickup(pickup);
        pickup = -1;
	    return true;
	}
	return false;
}

stock J_DestroyObject(&object)
{
	if(object != -1)
	{
	    DestroyObject(object);
	    object = -1;
	    return true;
    }
    return false;
}

stock J_Kick(playerid,reason[] = "None")
{
	new string[150];
	if(IsPlayerLogged[playerid])
	{
		J_TogglePlayerControllable(playerid,false);
	}
	format(string,sizeof(string),"%s[%d] был кикнут с причиной: %s",PN(playerid),playerid,reason);
	WriteLog("KickLog.log",string);
	SetPlayerTimer(playerid,"PlKick",1,false);
	return true;
}

stock J_SendClientMessage(playerid,bool:all,color,text[],transfer = 0)
{
	if(all) SendClientMessageToAllEx(color,text,transfer);
	else SendClientMessageEx(playerid,color,text,transfer);
	return true;
}

stock J_ShowPlayerDialogEx(playerid,dialogid,style,name[],string[],key_1[] = "Ок",key_2[] = "")
{
	return ShowPlayerDialogEx(playerid,dialogid,style,name,string,key_1,key_2);
}

stock J_SetWeather(weather)
{
	/*if(weather == 8) Weather = 7;
	else if(weather == 16) Weather = 15;
	else Weather = weather;*/
	SetWeather(weather);
/*	switch(Weather)
	{
	    case 5, 7, 8, 12, 15, 16:
		{
		    if(!Snow) StartSnow();
	    }
	    default:
	    {
	        if(Snow) StopSnow();
	    }
	}*/
	return true;
}

stock J_GetSquareDistance(Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2)
{
	x1-=x2;
	y1-=y2;
	z1-=z2;
	x1*=x1;
	y1*=y1;
	z1*=z1;
	return floatround(x1+y1+z1);
}
