/*
	Pipe Tobacco by Gasman
	zcmd by Zeex
	sscanf2 by Y_LESS
	progress2 by SouthClaw
*/
#include <a_samp>
#include <zcmd>
#include <sscanf2>
#include <progress2>
new PlayerText:TextPipeTobacco[MAX_PLAYERS][7];
enum pipetobacco
{
	tlSmoking,
	Float:tlProgress,
	PlayerBar:tlBar,
	tlKey,
	tlWrong,
}
new PipeTobacco[MAX_PLAYERS][pipetobacco];
public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print(" PIPE TOBACCO BY GASMAN");
	print("--------------------------------------\n");
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

public OnPlayerConnect(playerid)
{
	LoadPipeTobaccoText(playerid);
	PipeTobacco[playerid][tlBar] = CreatePlayerProgressBar(playerid, 50.000000, 321.000000, 80.500000, 9.199999, -1429936641, 100.0000, 0);
	return 1;
}
public OnPlayerRequestSpawn(playerid)
{
    PreloadAnimThuocLao(playerid);
	return 1;
}
CMD:pipetobacco(playerid,params[])
{
	new type[128];
	if(sscanf(params,"s[128]",type)) return SendClientMessage(playerid,-1,"/pipetobacco [smoke,help,credits]");
    if (strcmp("smoke", type, true, 10) == 0)
	{
	    if(PipeTobacco[playerid][tlSmoking] == 1) return SendClientMessage(playerid,-1,"You already Smoke");
		StartSmoke(playerid);
	}
	else if (strcmp("help", type, true, 10) == 0)
	{
	    ShowPlayerDialog(playerid,69691,0,"Pipe Tobacco Helps","Press Right or Left b Button Mouse\n\
	    When color button is Red\n\
	    If Press Wrong 2 time will Choke\nAfter smoking will Drugged\nRandom Shock Drug\nReceive 100% Heal","Oke","");
	}
	else if (strcmp("credits", type, true, 10) == 0)
	{
	    ShowPlayerDialog(playerid,69691,0,"Credits Pipe Tobacco","Script by Gasman\nSscanf by Y_Less\nZcmd by ZeeX\nProgress2 by SouthClaw","Oke","");
	}
	return 1;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
	if(PipeTobacco[playerid][tlSmoking] == 1)
	{
	    if(newkeys & KEY_FIRE)
	    {
     		if(PipeTobacco[playerid][tlKey] == KEY_FIRE)
     		{
	     		PipeTobacco[playerid][tlProgress] += 10.0;
	     		SetPlayerProgressBarValue(playerid,PipeTobacco[playerid][tlBar],PipeTobacco[playerid][tlProgress]);
	     		RandomTLKey(playerid);
	     		if(PipeTobacco[playerid][tlProgress] >= 100.0)
	     		{
	     		    PipeTobaccoSmoking(playerid);
     			}
     		}
     		else
     		{
	     		PipeTobacco[playerid][tlWrong]++;
	     		new saitext[128];
	     		format(saitext,sizeof(saitext),"Sai %d/2",PipeTobacco[playerid][tlWrong]);
	     		PlayerTextDrawSetString(playerid,TextPipeTobacco[playerid][5],saitext);
	     		PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][5]);
	     		RandomTLKey(playerid);
	     		if(PipeTobacco[playerid][tlWrong] >= 2)
	     		{
					ChokeSmoke(playerid);
	     		}
     		}
	    }
	    else if(newkeys & KEY_HANDBRAKE)
	    {
            if(PipeTobacco[playerid][tlKey] == KEY_HANDBRAKE)
     		{
	     		PipeTobacco[playerid][tlProgress] += 10.0;
	            SetPlayerProgressBarValue(playerid,PipeTobacco[playerid][tlBar],PipeTobacco[playerid][tlProgress]);
	            RandomTLKey(playerid);
	            if(PipeTobacco[playerid][tlProgress] >= 100.0)
	     		{
	     		    PipeTobaccoSmoking(playerid);
     			}
     		}
     		else
     		{
	     		PipeTobacco[playerid][tlWrong]++;
	     		new saitext[128];
	     		format(saitext,sizeof(saitext),"Wrong %d/2",PipeTobacco[playerid][tlWrong]);
	     		PlayerTextDrawSetString(playerid,TextPipeTobacco[playerid][5],saitext);
	     		PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][5]);
	     		RandomTLKey(playerid);
	     		if(PipeTobacco[playerid][tlWrong] >= 2)
	     		{
					ChokeSmoke(playerid);
	     		}
     		}
	    }
	}
	return 1;
}
stock ChokeSmoke(playerid)
{
    PlayAudioStreamForPlayer(playerid,"http://grg-gasman.esy.es/choke.mp3");
	GameTextForPlayer(playerid,"Choke Smoke",3000,6);
    ApplyAnimation(playerid, "FAT", "IDLE_TIRED", 4.0, 0, 0, 0, 0, 3000);
    StopSmoke(playerid);
	return 1;
}
stock PipeTobaccoSmoking(playerid)
{
    PipeTobacco[playerid][tlSmoking] = 2;
    ApplyAnimation(playerid, "BOMBER", "BOM_PLANT_CROUCH_IN", 4.0, 0, 0, 0, 1, 0); // PipeTobaccoSmoking
    RemovePlayerAttachedObject(playerid,5);
	RemovePlayerAttachedObject(playerid,6);
	RemovePlayerAttachedObject(playerid,7);
    SetPlayerAttachedObject(playerid, 9, 1666, 1, 0.3190, 0.3660, 0.1079, 110.9000, 24.5999, 11.4000, 0.6369, 0.6459, 2.9759, 0xFFFFFFFF, 0xFFFFFFFF);
	SetPlayerAttachedObject(playerid, 8, 3044, 2, -0.0089, 0.1399, -0.0150, 177.1999, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0xFFFFFFFF, 0xFFFFFFFF); // CIGAR attached to the Head of Gasman
    PlayAudioStreamForPlayer(playerid,"http://grg-gasman.esy.es/thuoclao.mp3");
    SetTimerEx("PTSmoking",1000,0,"i",playerid);
    PlayerTextDrawSetString(playerid,TextPipeTobacco[playerid][2],"Smoking...");
    PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][2]);
	return 1;
}
forward PTSmoking(playerid);
public PTSmoking(playerid)
{
    PipeTobacco[playerid][tlProgress] -= 20.0;
    SetPlayerProgressBarValue(playerid,PipeTobacco[playerid][tlBar],PipeTobacco[playerid][tlProgress]);
    if(PipeTobacco[playerid][tlProgress] <= 0.0 || GetPlayerProgressBarValue(playerid,PipeTobacco[playerid][tlBar]) <= 0.0)
    {
		ApplyAnimation(playerid, "SUNBATHE", "BATHERDOWN", 4.0, 0, 0, 0, 1, 0);
		SetTimerEx("PTStandUp",5000,0,"i",playerid);
  		StopSmoke(playerid);
  		SetPlayerAttachedObject(playerid, 8, 3044, 2, -0.0089, 0.1399, -0.0150, 177.1999, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0xFFFFFFFF, 0xFFFFFFFF); // CIGAR attached to the Head of Gasman
    }
    else
    {
    	SetTimerEx("PTSmoking",1000,0,"i",playerid);
    }
	return 1;
}
forward PTStandUp(playerid);
public PTStandUp(playerid)
{
	RemovePlayerAttachedObject(playerid,8);
    ApplyAnimation(playerid, "SUNBATHE", "SITNWAIT_OUT_W", 4.0, 0, 0, 0, 0, 0);
    SetTimerEx("DruggesPipe",3000,0,"ii",playerid,1);
	return 1;
}
forward DruggesPipe(playerid,type);
public DruggesPipe(playerid,type)
{
	GameTextForPlayer(playerid,"DRUGGES",1000,6);
	if(type == 1)
	{
		ApplyAnimation(playerid, "CRACK", "BBALBAT_IDLE_01", 4.0, 0, 0, 0, 0, 0);
		SetTimerEx("DruggesPipe",5000,0,"ii",playerid,2);
	}
	else if(type ==2)
	{
	    ApplyAnimation(playerid, "CRACK", "BBALBAT_IDLE_02", 4.0, 0, 0, 0, 0, 0); // phe2
	    SetPlayerHealth(playerid,100);
	    switch(random(3))
	    {
	        case 1:SetTimerEx("ShockSmoke",3000,0,"ii",playerid,1);
	    }
	}
	return 1;
}
forward ShockSmoke(playerid,type);
public ShockSmoke(playerid,type)
{
    GameTextForPlayer(playerid,"SHOCK SMOKE",1000,6);
	if(type == 1)
	{
	    switch(random(2))
	    {
		case 0:ApplyAnimation(playerid, "CRACK", "CRCKDETH3", 4.0, 0, 0, 0, 1, 0); // shock
		case 1:ApplyAnimation(playerid, "CRACK", "CRCKIDLE3", 4.0, 0, 0, 0, 1, 0); // shock2
		}
		SetTimerEx("ShockSmoke",3000,0,"ii",playerid,2);
	}
	else if(type == 2)
	{
	    ApplyAnimation(playerid, "SUNBATHE", "PARKSIT_W_OUT", 4.0, 0, 0, 0, 0, 0); // shockdungday
	}
	return 1;
}
stock StopSmoke(playerid)
{
    PipeTobacco[playerid][tlSmoking] = 0;
    PipeTobacco[playerid][tlProgress] = 0.0;
    PipeTobacco[playerid][tlWrong] = 0;
    for(new i =0;i<7;i++)
	{
		PlayerTextDrawHide(playerid,TextPipeTobacco[playerid][i]);
	}
	HidePlayerProgressBar(playerid,PipeTobacco[playerid][tlBar]);
	RemovePlayerAttachedObject(playerid,5);
	RemovePlayerAttachedObject(playerid,6);
	RemovePlayerAttachedObject(playerid,7);
	RemovePlayerAttachedObject(playerid,8);
	RemovePlayerAttachedObject(playerid,9);
	return 1;
}
stock StartSmoke(playerid)
{
    PipeTobacco[playerid][tlSmoking] = 1;
    PipeTobacco[playerid][tlProgress] = 0.0;
    PipeTobacco[playerid][tlWrong] = 0;
    SetPlayerArmedWeapon(playerid,0);
    ApplyAnimation(playerid, "BUDDY", "BUDDY_CROUCHRELOAD", 4.0, 0, 0, 0, 1, 0); // hut
    SetPlayerAttachedObject(playerid, 5, 1666, 1, 0.2487, 0.4408, 0.1939, 127.0998, 21.3999, 11.7999, 0.6620, 0.7839, 3.5720, 0xFFFFFFFF, 0xFFFFFFFF); // propbeerglass1 attached to the Spine of Gasman
	SetPlayerAttachedObject(playerid, 6, 1942, 1, 0.2558, 0.5037, 0.2409, 34.2000, 0.0000, 112.1998, 0.0637, 0.6539, 0.0599, 0xFFFFFFFF, 0xFFFFFFFF); // kg50 attached to the Spine of Gasman
	SetPlayerAttachedObject(playerid, 7, 1942, 1, 0.1490, 0.6419, 0.3479, 33.5000, 7.4998, 21.7999, 0.0860, 0.4699, 0.0920, 0xFFFFFFFF, 0xFFFFFFFF); // kg50 attached to the Spine of Gasman
	SetPlayerAttachedObject(playerid, 8, 18673, 1, 0.3449, -0.3939, -1.0950, -34.4000, -1.2999, 101.3000, 1.0000, 1.0000, 1.0000, 0xFFFFFFFF, 0xFFFFFFFF); // cigarette_smoke attached to the Spine of Gasman
	SetPlayerAttachedObject(playerid, 9, 1933, 5, 0.0777, 0.0399, -0.0368, 0.0000, 0.0000, 0.0000, 0.1620, 0.0419, 0.5529, 0xFFFFFFFF, 0xFFFFFFFF); // chip_stack16 attached to the Left Hand of Gasman
    SetPlayerProgressBarValue(playerid,PipeTobacco[playerid][tlBar],PipeTobacco[playerid][tlProgress]);
    PlayerTextDrawSetString(playerid,TextPipeTobacco[playerid][2],"Starting...");
    PlayerTextDrawSetString(playerid,TextPipeTobacco[playerid][5],"Wrong 0/2");
	for(new i =0;i<7;i++)
	{
		PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][i]);
	}
	ShowPlayerProgressBar(playerid,PipeTobacco[playerid][tlBar]);
	RandomTLKey(playerid);
	return 1;
}
stock RandomTLKey(playerid)
{
    switch(random(2))
	{
		case 0:
		{
		PipeTobacco[playerid][tlKey] = KEY_FIRE;
		PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][3],-16776961);
		PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][4],-1);
		PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][3]);
		PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][4]);
		}
		case 1:
		{
		PipeTobacco[playerid][tlKey] = KEY_HANDBRAKE;
		PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][4],-16776961);
		PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][3],-1);
		PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][3]);
		PlayerTextDrawShow(playerid,TextPipeTobacco[playerid][4]);
		}
	}
	return 1;
}
stock LoadPipeTobaccoText(playerid)
{
	TextPipeTobacco[playerid][0] = CreatePlayerTextDraw(playerid,45.000000, 264.000000, "Pipe Tobacco");
	PlayerTextDrawBackgroundColor(playerid,TextPipeTobacco[playerid][0], 0);
	PlayerTextDrawFont(playerid,TextPipeTobacco[playerid][0], 0);
	PlayerTextDrawLetterSize(playerid,TextPipeTobacco[playerid][0], 0.500000, 1.000000);
	PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][0], 255);
	PlayerTextDrawSetOutline(playerid,TextPipeTobacco[playerid][0], 0);
	PlayerTextDrawSetProportional(playerid,TextPipeTobacco[playerid][0], 1);
	PlayerTextDrawSetShadow(playerid,TextPipeTobacco[playerid][0], 1);
	PlayerTextDrawSetSelectable(playerid,TextPipeTobacco[playerid][0], 0);

	TextPipeTobacco[playerid][1] = CreatePlayerTextDraw(playerid,49.000000, 277.000000, "_");
	PlayerTextDrawBackgroundColor(playerid,TextPipeTobacco[playerid][1], 255);
	PlayerTextDrawFont(playerid,TextPipeTobacco[playerid][1], 1);
	PlayerTextDrawLetterSize(playerid,TextPipeTobacco[playerid][1], 0.500000, 6.000000);
	PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][1], -1);
	PlayerTextDrawSetOutline(playerid,TextPipeTobacco[playerid][1], 0);
	PlayerTextDrawSetProportional(playerid,TextPipeTobacco[playerid][1], 1);
	PlayerTextDrawSetShadow(playerid,TextPipeTobacco[playerid][1], 1);
	PlayerTextDrawUseBox(playerid,TextPipeTobacco[playerid][1], 1);
	PlayerTextDrawBoxColor(playerid,TextPipeTobacco[playerid][1], 103);
	PlayerTextDrawTextSize(playerid,TextPipeTobacco[playerid][1], 127.000000, 0.000000);
	PlayerTextDrawSetSelectable(playerid,TextPipeTobacco[playerid][1], 0);

	TextPipeTobacco[playerid][2] = CreatePlayerTextDraw(playerid,48.000000, 276.000000, "Starting...");
	PlayerTextDrawBackgroundColor(playerid,TextPipeTobacco[playerid][2], 255);
	PlayerTextDrawFont(playerid,TextPipeTobacco[playerid][2], 0);
	PlayerTextDrawLetterSize(playerid,TextPipeTobacco[playerid][2], 0.419999, 1.100000);
	PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][2], -1);
	PlayerTextDrawSetOutline(playerid,TextPipeTobacco[playerid][2], 0);
	PlayerTextDrawSetProportional(playerid,TextPipeTobacco[playerid][2], 1);
	PlayerTextDrawSetShadow(playerid,TextPipeTobacco[playerid][2], 1);
	PlayerTextDrawSetSelectable(playerid,TextPipeTobacco[playerid][2], 0);

	TextPipeTobacco[playerid][3] = CreatePlayerTextDraw(playerid,51.000000, 292.000000, "LMB");
	PlayerTextDrawBackgroundColor(playerid,TextPipeTobacco[playerid][3], 255);
	PlayerTextDrawFont(playerid,TextPipeTobacco[playerid][3], 2);
	PlayerTextDrawLetterSize(playerid,TextPipeTobacco[playerid][3], 0.419999, 1.100000);
	PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][3], -16776961);
	PlayerTextDrawSetOutline(playerid,TextPipeTobacco[playerid][3], 1);
	PlayerTextDrawSetProportional(playerid,TextPipeTobacco[playerid][3], 1);
	PlayerTextDrawSetSelectable(playerid,TextPipeTobacco[playerid][3], 0);

	TextPipeTobacco[playerid][4] = CreatePlayerTextDraw(playerid,91.000000, 292.000000, "RMB");
	PlayerTextDrawBackgroundColor(playerid,TextPipeTobacco[playerid][4], 255);
	PlayerTextDrawFont(playerid,TextPipeTobacco[playerid][4], 2);
	PlayerTextDrawLetterSize(playerid,TextPipeTobacco[playerid][4], 0.419999, 1.100000);
	PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][4], -1);
	PlayerTextDrawSetOutline(playerid,TextPipeTobacco[playerid][4], 1);
	PlayerTextDrawSetProportional(playerid,TextPipeTobacco[playerid][4], 1);
	PlayerTextDrawSetSelectable(playerid,TextPipeTobacco[playerid][4], 0);

	TextPipeTobacco[playerid][5] = CreatePlayerTextDraw(playerid,49.000000, 306.000000, "Wrong : 0/2");
	PlayerTextDrawBackgroundColor(playerid,TextPipeTobacco[playerid][5], 255);
	PlayerTextDrawFont(playerid,TextPipeTobacco[playerid][5], 0);
	PlayerTextDrawLetterSize(playerid,TextPipeTobacco[playerid][5], 0.390000, 0.899999);
	PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][5], -1);
	PlayerTextDrawSetOutline(playerid,TextPipeTobacco[playerid][5], 0);
	PlayerTextDrawSetProportional(playerid,TextPipeTobacco[playerid][5], 1);
	PlayerTextDrawSetShadow(playerid,TextPipeTobacco[playerid][5], 1);
	PlayerTextDrawSetSelectable(playerid,TextPipeTobacco[playerid][5], 0);

	TextPipeTobacco[playerid][6] = CreatePlayerTextDraw(playerid,47.000000, 332.000000, "by Gasman");
	PlayerTextDrawBackgroundColor(playerid,TextPipeTobacco[playerid][6], 0);
	PlayerTextDrawFont(playerid,TextPipeTobacco[playerid][6], 1);
	PlayerTextDrawLetterSize(playerid,TextPipeTobacco[playerid][6], 0.210000, 0.899999);
	PlayerTextDrawColor(playerid,TextPipeTobacco[playerid][6], 68);
	PlayerTextDrawSetOutline(playerid,TextPipeTobacco[playerid][6], 0);
	PlayerTextDrawSetProportional(playerid,TextPipeTobacco[playerid][6], 1);
	PlayerTextDrawSetShadow(playerid,TextPipeTobacco[playerid][6], 1);
	PlayerTextDrawSetSelectable(playerid,TextPipeTobacco[playerid][6], 0);

	return 1;
}
stock PreloadAnimThuocLao(playerid)
{
    ApplyAnimation(playerid, "BOMBER", "BOM_PLANT_CROUCH_IN", 4.0, 0, 0, 0, 1, 0); // PipeTobaccoSmoking
    ApplyAnimation(playerid, "BUDDY", "BUDDY_CROUCHRELOAD", 4.0, 0, 0, 0, 1, 0); // hut
	ApplyAnimation(playerid, "SUNBATHE", "BATHERDOWN", 4.0, 0, 0, 0, 1, 0); // NamXuongXiKhoi
	ApplyAnimation(playerid, "SUNBATHE", "SITNWAIT_OUT_W", 4.0, 0, 0, 0, 0, 0); // dungday
	ApplyAnimation(playerid, "CRACK", "BBALBAT_IDLE_01", 4.0, 0, 0, 0, 0, 0); // phe1
	ApplyAnimation(playerid, "CRACK", "BBALBAT_IDLE_02", 4.0, 0, 0, 0, 0, 0); // phe2
	ApplyAnimation(playerid, "CRACK", "CRCKDETH3", 4.0, 0, 0, 0, 1, 0); // shock
	ApplyAnimation(playerid, "CRACK", "CRCKIDLE3", 4.0, 0, 0, 0, 1, 0); // shock2
	ApplyAnimation(playerid, "SUNBATHE", "PARKSIT_W_OUT", 4.0, 0, 0, 0, 0, 0); // shockdungday
	ApplyAnimation(playerid, "FAT", "IDLE_TIRED", 4.0, 0, 0, 0, 0, 3000); // ChokeSmoke
	return 1;
}