//------------------------------------------------------------------------------
//                   FilterScript Gone in 60 second by Eragon
//                        SA-MP .::: Eragon Studio :::.
//                         http://eragon-studio.3dn.ru
//------------------------------------------------------------------------------
#include <a_samp>
#include <a_players>
#include <a_vehicles>

#pragma tabsize 0

#define COLOR_RED 0xAA3333AA
#define COLOR_WHITE 0xFFFFFFAA
#define COLOR_YELLOW 0xFFFF00AA
#define MinCarValue 100000
#define MaxCarValue 250000

new Timer1;
new CheckpointStats[MAX_PLAYERS];
new BoomBonus;
new BoomCount = 60;
new BoomTimer2;
new BoomTimer1;
new RandomStatus = 0;
new PlayerIDCar;
new CarValue;
new IsPlayerInCar;
new IsGameStarted;
new Text:ValueText;
new IsTextdrawCreated;
new Float:TempX;
new Float:TempY;
new Float:TempZ;
new Float:FinishTempX;
new Float:FinishTempY;
new Float:FinishTempZ;
new car;
new carmodel[3] = {411,506,541};
new Float:carfinish[3][3] =
{
{541.670,881.188,-36.441},
{-2251.721,2291.147,3.961},
{279.225,1956.099,16.789}
};
new Float:carset[24][4] =
{
{2456.6995,-1717.3616,13.3678,284.1972},
{2370.5315,-2174.2224,13.3278,224.0777},
{2748.0867,-2457.9248,13.4276,91.5492},
{2423.6230,-1243.4207,23.7425,178.2408},
{1631.9427,-1012.8997,23.5781,338.2425},
{314.8827,-1809.3593,4.1555,180.7877},
{1358.8263,-1750.3478,13.0563,181.6825},
{-2160.3284,-409.0518,35.0156,133.7493},
{-1991.9716,253.8751,34.8516,255.2966},
{-2477.8835,741.6272,34.6953,179.6847},
{-2643.8464,1358.9806,6.8387,86.4725},
{-1660.4048,1211.3013,20.7862,317.6182},
{-1657.6849,1213.6091,6.8891,282.8674},
{-1617.1693,673.7289,6.8175,180.8516},
{-2947.7537,503.5475,2.2099,179.0314},
{1095.0120,1613.4869,12.4142,339.4486},
{1603.8391,1840.3539,10.6898,179.1630},
{1945.2069,2199.5847,10.6918,91.6500},
{2078.2585,2479.4644,10.4455,1.3884},
{2502.6233,2131.6287,10.5163,90.9122},
{2074.2383,2181.0540,10.5919,193.6889},
{2148.9429,1408.6769,10.5930,176.9892},
{2038.1877,1022.8527,10.5142,179.5875},
{2681.4807,826.2079,10.7274,327.5371}
};

/*new Float:8ball[2][3]
{
{1839.8109,-1856.4781,13.3828},
{2005.7815,2298.7546,10.8203}
};*/

forward StartNewGame();
forward ResetCam(playerid);
forward CarHealth();
forward checkpointUpdate();
forward Boom();
//------------------------------------------------------------------------------
public OnFilterScriptInit()
{
	print("=====================================");
	print("|                                   |");
	print("|      Gone in 60 second v.1.0      |");
	print("|             by Eragon             |");
	print("|           -------------           |");
	print("|    http://eragon-studio.3dn.ru    |");
	print("|                                   |");
	print("=====================================");
	SetTimer("StartNewGame", 50000, 0);
	return 1;
}
//------------------------------------------------------------------------------
public OnFilterScriptExit()
{
    KillTimer (BoomTimer1);
	KillTimer (BoomTimer2);
    if(IsGameStarted == 1)
	{
	    for(new i; i<MAX_PLAYERS; i++)
	    {
    		DisablePlayerCheckpoint(i);
		}
	}
	if(IsTextdrawCreated == 1)
	{
		TextDrawHideForAll(ValueText);
		TextDrawDestroy(ValueText);
		IsTextdrawCreated = 0;
	}
	return 1;
}
//------------------------------------------------------------------------------
public OnPlayerConnect(playerid)
{
	if(IsGameStarted == 1)
	{
		if(PlayerIDCar == INVALID_PLAYER_ID)
		{
		    if(IsPlayerInCar == 1)
      		{
      		CheckpointStats[playerid]=1;
		    SetPlayerCheckpoint(playerid, FinishTempX, FinishTempY, FinishTempZ, 10);
			}
		}
	}
	return 1;
}
//------------------------------------------------------------------------------
public OnPlayerDisconnect(playerid, reason)
{
	if(playerid == PlayerIDCar)
	{
	    new str[128], pName[MAX_PLAYER_NAME];
	    GetPlayerName(playerid, pName, sizeof(pName));
	    format(str, 128, " %s (ID: %d) покинул автомобиль! Чтобы узнать местоположение автомобиля воспользуйтесь GPS ('/gps')", pName, playerid);
	    SendClientMessageToAll(COLOR_YELLOW, str);
		PlayerIDCar = INVALID_PLAYER_ID;
		IsPlayerInCar = 0;
        for(new i; i<MAX_PLAYERS; i++)
			{
			if(IsPlayerConnected(i))
				{
			    DisablePlayerCheckpoint(i);
				}
			}
	}
	return 1;
}
//------------------------------------------------------------------------------
public OnPlayerDeath(playerid, killerid, reason)
{
	if(playerid == PlayerIDCar)
	{
	    new str[128], pName[MAX_PLAYER_NAME];
	    GetPlayerName(playerid, pName, sizeof(pName));
	    format(str, 128, " %s (ID: %d) покинул автомобиль! Чтобы узнать местоположение автомобиля воспользуйтесь GPS ('/gps')", pName, playerid);
	    SendClientMessageToAll(COLOR_YELLOW, str);
		PlayerIDCar = INVALID_PLAYER_ID;
		IsPlayerInCar = 0;
        for(new i; i<MAX_PLAYERS; i++)
			{
			if(IsPlayerConnected(i))
				{
			    DisablePlayerCheckpoint(i);
				}
			}
	}
	return 1;
}
//------------------------------------------------------------------------------
public OnPlayerCommandText(playerid, cmdtext[])
{
	if(strcmp(cmdtext, "/gps", true)==0)
	{
	    if(IsGameStarted == 1)
	    {
	        GetVehiclePos(car,TempX,TempY,TempZ);
			SetPlayerCameraPos(playerid, TempX, TempY, (TempZ+60));
			SetPlayerCameraLookAt(playerid, TempX, TempY, TempZ);
		    TogglePlayerControllable(playerid, 0);
			SetTimerEx("ResetCam", 10000, 0, "i", playerid);
		}
		return 1;
	}
	if(strcmp(cmdtext, "/info", true)==0)
	{
	    if(IsGameStarted == 1)
	    {
	        SetPlayerPos(playerid,TempX, TempY, TempZ);
		}
		return 1;
	}
	return 0;
}
//------------------------------------------------------------------------------
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
	new plname[256], str[128];
	GetPlayerName(playerid, plname, 256);
	if (vehicleid == car)
	{ 
		PlayerIDCar = playerid;
		IsPlayerInCar = 1;
		format(str, 128, " %s (ID: %d) украл автомобиль! Не дайте ему доставить машину на красный маркер!", plname, playerid);
	    SendClientMessageToAll(COLOR_YELLOW, str);
	    SendClientMessageToAll(COLOR_WHITE, " Чтобы узнать местоположение автомобиля воспользуйтесь GPS ('/gps')");
	    SendClientMessage(playerid, COLOR_WHITE, " Как можно скорее доставь машину на красный маркер!");
		for(new i; i<MAX_PLAYERS; i++)
			{
			    if(IsPlayerConnected(i))
			    {
			    	SetPlayerCheckpoint(i, FinishTempX, FinishTempY, FinishTempZ, 10);
				}
			}
		if (RandomStatus == 0)
		    {
			new rand=random(2);
			RandomStatus = 1;
			if (rand == 1)
			    {
			    CheckpointStats[playerid]=2;
			    SendClientMessage(playerid, COLOR_YELLOW, " Машина заминированa! Быстрее езжай в ближайший 8ball и разминируй.");
			    BoomTimer2 = SetTimer("checkpointUpdate",1000,1);
			    BoomTimer1 = SetTimer("Boom",1000,1);
			    }
		    }
	}
    return 1;
}
//------------------------------------------------------------------------------
public Boom()
{
	if (BoomCount == 0)
	    {
		new pName[MAX_PLAYER_NAME], str[128];
		for(new i; i<MAX_PLAYERS; i++)
			{
				if(IsPlayerConnected(i))
					{
				    DisablePlayerCheckpoint(i);
					}
			}
		GetVehiclePos(car,TempX,TempY,TempZ);
		CreateExplosion(TempX,TempY,TempZ,1,20);
		SetPlayerHealth(PlayerIDCar,0);
		GetPlayerName(PlayerIDCar, pName, sizeof(pName));
		format(str,128, " Машина была заминирована и %s (ID: %d) не смог разминировать и погиб в аварии.",pName,PlayerIDCar);
    	SendClientMessageToAll(COLOR_WHITE,str);
		SendClientMessageToAll(COLOR_YELLOW, " Следущая миссия через 5 минут");
		SetTimer("StartNewGame", 300000, 0);
		IsGameStarted = 0;
		IsPlayerInCar = 0;
		DestroyVehicle(car);
		PlayerIDCar = INVALID_PLAYER_ID;
		RandomStatus = 0;
		KillTimer (BoomTimer1);
		KillTimer (Timer1);
		KillTimer (BoomTimer2);
		BoomCount = 60;
		CheckpointStats[PlayerIDCar]=1;
		}
	else
	    {
	    new str[128];
	    BoomCount = BoomCount - 1;
	    format (str,128,"%d", BoomCount);
	    GameTextForPlayer(PlayerIDCar,str,1000,1);
	    }
}
//------------------------------------------------------------------------------
public checkpointUpdate()
{
	if(IsPlayerConnected(PlayerIDCar))
	{
	new Float:x, Float:y, Float:z;
	new Float:sumC1, Float:sumC2;
	GetPlayerPos (PlayerIDCar,x,y,z);
	sumC1=(((1839.8109-x)*(1839.8109-x))+((-1856.4781-y)*(-1856.4781-y)));
	sumC2=(((2005.7815-x)*(2005.7815-x))+((2298.7546-y)*(2298.7546-y)));
	if (sumC1 > sumC2)
	    {
        DisablePlayerCheckpoint(PlayerIDCar);
		SetPlayerCheckpoint (PlayerIDCar,2005.7815,2298.7546,10.8203,10);
	    }
	else
	    {
	    DisablePlayerCheckpoint(PlayerIDCar);
		SetPlayerCheckpoint (PlayerIDCar,1839.8109,-1856.4781,13.3828,10);
	    }
    }
}
//------------------------------------------------------------------------------
public OnPlayerExitVehicle(playerid, vehicleid)
{
	if (vehicleid == car)
	{
		new str[128], pName[MAX_PLAYER_NAME];
	    GetPlayerName(playerid, pName, sizeof(pName));
	    format(str, 128, " %s (ID: %d) покинул автомобиль! Чтобы узнать местоположение автомобиля воспользуйтесь GPS ('/gps')", pName, playerid);
	    SendClientMessageToAll(COLOR_YELLOW, str);
		PlayerIDCar = INVALID_PLAYER_ID;
		IsPlayerInCar = 0;
		KillTimer (BoomTimer2);
        for(new i; i<MAX_PLAYERS; i++)
			{
			if(IsPlayerConnected(i))
				{
			    DisablePlayerCheckpoint(i);
				}
			}
	}
   return 1;
}
//------------------------------------------------------------------------------
public CarHealth()
{
	new Float:health;
	GetVehicleHealth(car,health);
	if ((health >= 0.0) && (health < 100.0))
	{
	new pName[MAX_PLAYER_NAME], str[128];
	GetVehiclePos(car,TempX,TempY,TempZ);
	CreateExplosion(TempX,TempY,TempZ,1,20);
	SetPlayerHealth(PlayerIDCar,0);
	GetPlayerName(PlayerIDCar, pName, sizeof(pName));
	format(str,128, " Машина изза сильных вопреждений взорвалась. Игрок %s (ID: %d) погиб в аварии.",pName,PlayerIDCar);
    SendClientMessageToAll(COLOR_WHITE,str);
	SendClientMessageToAll(COLOR_YELLOW, " Следущая миссия через 5 минут");
	SetTimer("StartNewGame", 300000, 0);
	IsGameStarted = 0;
	IsPlayerInCar = 0;
	DestroyVehicle(car);
	PlayerIDCar = INVALID_PLAYER_ID;
	RandomStatus = 0;
	KillTimer (BoomTimer1);
	KillTimer (BoomTimer2);
	KillTimer(Timer1);
	BoomCount = 60;
	for(new i; i<MAX_PLAYERS; i++)
			{
			if(IsPlayerConnected(i))
				{
			    DisablePlayerCheckpoint(i);
				}
			}
	}
}
//------------------------------------------------------------------------------
public OnPlayerEnterCheckpoint(playerid)
{
	if(playerid == PlayerIDCar)
	{
	    if (CheckpointStats[playerid] == 1)
	    {
	    PlayerPlaySound(playerid, 1054, 0, 0, 0);
	    new str[128], pName[MAX_PLAYER_NAME];
	    new Float:healthcar, statuscar;
    	for(new i; i<MAX_PLAYERS; i++)
			{
			if(IsPlayerConnected(i))
				{
			    DisablePlayerCheckpoint(i);
				}
			}
	    PlayerIDCar = INVALID_PLAYER_ID;
	    GetVehicleHealth(car,healthcar);
	    healthcar = 1000.0 - healthcar;
	    if ((healthcar > 900.0)&&(healthcar < 1000.0)) statuscar = 90000;
     	if ((healthcar > 800.0)&&(healthcar < 900.0)) statuscar = 80000;
	    if ((healthcar > 700.0)&&(healthcar < 800.0)) statuscar = 70000;
	    if ((healthcar > 600.0)&&(healthcar < 700.0)) statuscar = 60000;
	    if ((healthcar > 500.0)&&(healthcar < 600.0)) statuscar = 50000;
	    if ((healthcar > 400.0)&&(healthcar < 500.0)) statuscar = 40000;
	    if ((healthcar > 300.0)&&(healthcar < 400.0)) statuscar = 30000;
	    if ((healthcar > 200.0)&&(healthcar < 300.0)) statuscar = 20000;
	    if ((healthcar > 100.0)&&(healthcar < 200.0)) statuscar = 10000;
	    if ((healthcar > 0)&&(healthcar < 100.0)) statuscar = 0;
	    CarValue = CarValue - statuscar + BoomBonus*1000;
	    format(str,128, " Машина повреждена на %f едениц. С вашей доли вычтено %d $. Бонус за снятие бомбы %d $. Итого вы получаете %d $", healthcar, statuscar, BoomBonus, CarValue);
	    SendClientMessage(playerid,COLOR_WHITE, str);
		GetPlayerName(playerid, pName, sizeof(pName));
		format(str, 128, " %s (ID: %d) Отвёз(ла) машину в пункт назначения и получил(а) $%d", pName, playerid, CarValue);
		SendClientMessageToAll(COLOR_YELLOW, str);
		if (CarValue > 0) GivePlayerMoney(playerid, CarValue);
        format(str, 128, "~y~Congratulations! ~n~ You won ~n~~g~$%d", CarValue);
		GameTextForPlayer(playerid, str, 4000, 3);
		SendClientMessageToAll(0xFFD700AA, " Следущая миссия через 5 минут");
		SetTimer("StartNewGame", 300000, 0);
		IsGameStarted = 0;
		IsPlayerInCar = 0;
		RandomStatus = 0;
		BoomBonus = 0;
		KillTimer (BoomTimer1);
		KillTimer (BoomTimer2);
		KillTimer(Timer1);
		BoomCount = 60;
		DestroyVehicle(car);
		SendClientMessageToAll(COLOR_RED, " Script by Eragon (http://eragon-studio.3dn.ru)");
		if(IsTextdrawCreated == 1)
			{
			TextDrawDestroy(ValueText);
			IsTextdrawCreated = 0;
			}
		}
		if (CheckpointStats[playerid] == 2)
		{
		BoomBonus = 60 - BoomCount;
		KillTimer (BoomTimer1);
		KillTimer (BoomTimer2);
		BoomCount = 60;
		CheckpointStats[playerid]=1;
		SetPlayerCheckpoint(playerid, FinishTempX, FinishTempY, FinishTempZ, 10);
		SendClientMessage(playerid,COLOR_WHITE, " Машина разминирована. Пора доставить ее к пункту назначения и получить свои деньги!");
		}
	}
	return 1;
}
//------------------------------------------------------------------------------
public StartNewGame()
{
    IsGameStarted = 1;
    new str[128];
	new rand = random(sizeof(carfinish));
	new rand2 = random(sizeof(carset));
	new rand3 = random(sizeof(carmodel));
	FinishTempX = carfinish[rand][0];
	FinishTempY = carfinish[rand][1];
	FinishTempZ = carfinish[rand][2];
	BoomBonus = 0;
	CarValue = MinCarValue+random(MaxCarValue-MinCarValue);
	car = CreateVehicle(carmodel[rand3],carset[rand2][0],carset[rand2][1],carset[rand2][2],carset[rand2][3],0,0,999999);
	GetVehiclePos(car,TempX,TempY,TempZ);
	format(str, 128, " [SMS]: Угони машину и доставь в пункт назначения. Цена: %d. Чтобы найти машину пользуйся GPS ('/gps').", CarValue);
    SendClientMessageToAll(COLOR_YELLOW, str);
    Timer1 = SetTimer("CarHealth",1000,1);
	for(new i; i<MAX_PLAYERS; i++)
	{
	    if(IsPlayerConnected(i))
	    {
		    new randX = -100+random(200);
		    new randY = -100+random(200);
			SetPlayerCameraPos(i, TempX+randX, TempY+randY, (TempZ+50));
			SetPlayerCameraLookAt(i, TempX, TempY, TempZ);
		    TogglePlayerControllable(i, 0);
		    SetTimerEx("ResetCam", 8000, 0, "i", i);
		    SendClientMessage(i, COLOR_WHITE, " Чтобы запросить видео со спутника используйте свой GPS ('/gps')");
		    CheckpointStats[i]=1;
		}
	}
}
//------------------------------------------------------------------------------
public ResetCam(playerid)
{
	SetCameraBehindPlayer(playerid);
	TogglePlayerControllable(playerid, 1);
	TogglePlayerSpectating(playerid, 0);
}
