/*ВСЁ, ЧТО СВЯЗАНО С ТРАНСПОРТОМ, НАХОДИТСЯ В ЭТОЙ БИБЛИОТЕКЕ*/
//Форварды
forward ModCar(carid);//Установить на машину тюнинг
forward EngineStart(playerid,carid);//Завести двигатель автомобиля
forward IsAOwnableVehicle(carid);//Машина из файла
forward LoadCars();//Загружает автомобили
forward SaveCars();//Сохраняет автомобили
forward LockCar(carid);//Закрыть тачку для всех
forward UnLockCar(carid);//Открыть тачку для всех
forward IsAPlane(carid);//Самолет
forward IsAMoped(carid);//Мопед
forward IsAMoto(carid);//Мотоцикл
forward TruckCar(carid);//Грузовик
forward IsABoat(carid);//Лодка
forward IsABycicle(carid);//Велосипед
forward LoadTrunk();//Загруить багажники
forward SaveTrunk();//Сохранить багажники
//Для заправок
forward IsTruck(carid);
forward IsSportCar(carid);
forward IsEliteCar(carid);
forward IsStandartCar(carid);
//Функции
stock GetVehicleSpeed(carid)//Возвращает скорость транспортного средства
{
    new Float:Vx,Float:Vy,Float:Vz,Float:rtn;
    GetVehicleVelocity(carid,Vx,Vy,Vz);
  	rtn = floatsqroot(Vx*Vx+Vy*Vy+Vz*Vz);
    return floatround(rtn * 100);
}
stock GetMaxVehicleID()//Узнать наибольшее ИД автомобиля
{
	for(new i = 1; i < MAX_VEHICLES; i ++)
	{
	    if(GetVehicleModel(i) < 400 || GetVehicleModel(i) > 611)
	    {
	    	return i-1;
	    }
	}
	return MAX_VEHICLES;
}
stock GetVFreeLetterSlot(carid)
{
	for(new i = 0; i < MAX_VEHICLE_LETTERS; i ++)
	{
	    if(CarLetters[carid][i] == 0)
	    return i;
	}
	return -1;
}
public ModCar(carid)
{
	if(CarInfo[carid][cSpoiler] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cSpoiler]);
	}
	if(CarInfo[carid][cHood] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cHood]);
	}
	if(CarInfo[carid][cSideskirt] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cSideskirt]);
	}
	if(CarInfo[carid][cNitro] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cNitro]);
	}
	if(CarInfo[carid][cLamps] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cLamps]);
	}
	if(CarInfo[carid][cExhaust] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cExhaust]);
	}
	if(CarInfo[carid][cWheels] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cWheels]);
	}
	if(CarInfo[carid][cRoof] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cRoof]);
	}
	if(CarInfo[carid][cStereo] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cStereo]);
	}
	if(CarInfo[carid][cHydraulics] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cHydraulics]);
	}
	if(CarInfo[carid][cBullbar] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cBullbar]);
	}
	if(CarInfo[carid][cRearBullbars] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cRearBullbars]);
	}
	if(CarInfo[carid][cFrontSign] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cFrontSign]);
	}
	if(CarInfo[carid][cFrontBullbars] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cFrontBullbars]);
	}
	if(CarInfo[carid][cFrontBumper] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cFrontBumper]);
	}
	if(CarInfo[carid][cRearBumper] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cRearBumper]);
	}
	if(CarInfo[carid][cVents] != 0)
	{
	    AddVehicleComponent(carid+1+SIMPLECARS,CarInfo[carid][cVents]);
	}
	if(CarInfo[carid][cPaintJob] != -1)
	{
	    ChangeVehiclePaintjob(carid+1+SIMPLECARS,CarInfo[carid][cPaintJob]);
	}
	if(CarInfo[carid][cLock] == 1)
	{
		for(new i = 0; i < MAX_PLAYERS; i++)
		{
			if(IsPlayerConnected(i))
			{
				SetVehicleParamsForPlayer(carid,i,0,1);
			}
		}
	}
	return 1;
}
public EngineStart(playerid, carid)
{
	if(IsPlayerConnected(playerid))
	{
		if(IsPlayerInVehicle(playerid, carid))
		{
		    new sendername[MAX_PLAYER_NAME];
		    new string[64];
		    GetPlayerName(playerid, sendername, sizeof(sendername));
		    new chance = random(10);
		    if(chance >= 0 && chance <= 8)
		    {
		    	Engine[carid] = 1;
		    	TogglePlayerControllable(playerid, true);
		    	if(PlayerLang[playerid] == 1)
		    	SendClientMessage(playerid, COLOR_GREY, "Вы успешно запустили двигатель.");
		    	else
		    	SendClientMessage(playerid, COLOR_GREY, "You have succesfully started an engine.");
		    	format(string, sizeof(string), "* %s заводит двигатель.", sendername);
		    //	ProxDetector(30.0, playerid, string, COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE,COLOR_PURPLE);
		    	return 1;
	  		}
	  		else
	  		{
	  		    if(PlayerLang[playerid] == 1)
	  			SendClientMessage(playerid, COLOR_GREY, "Вам не удалось запустить двигатель.");
	  			else
	  			SendClientMessage(playerid, COLOR_GREY, "You failed to start an engine");
	 		}
  		}
  	}
  	return 1;
}
public IsAOwnableVehicle(carid)
{
//	if(carid > SIMPLECARS && carid <= (OWNABLECARS + SIMPLECARS))
	if(CarInfo[carid][cId] >=400 && CarInfo[carid][cId] <=611)//Проверим, будет ли работать это
	{
	    return 1;
	}
	return 0;
}
public LoadCars()
{
    new arrCoords[40][32];
    new strFromFile2[256];
    new File: file = fopen("vehicle.cfg", io_read);
	if (file)
	{
	    new idx = SIMPLECARS+1;
		while (idx <= OWNABLECARS+SIMPLECARS)
		{
			fread(file, strFromFile2);
			split(strFromFile2, arrCoords, '|');
	    	CarInfo[idx][cId] = strval(arrCoords[0]);
	    	CarInfo[idx][cSTCar_x] = strval(arrCoords[1]);
	    	CarInfo[idx][cSTCar_y] = strval(arrCoords[2]);
	    	CarInfo[idx][cSTCar_z] = strval(arrCoords[3]);
	    	CarInfo[idx][cSTCar_a] = strval(arrCoords[4]);
         	CarInfo[idx][cPARKCar_x] = strval(arrCoords[5]);
	    	CarInfo[idx][cPARKCar_y] = strval(arrCoords[6]);
	    	CarInfo[idx][cPARKCar_z] = strval(arrCoords[7]);
	    	CarInfo[idx][cPARKCar_a] = strval(arrCoords[8]);
         	CarInfo[idx][cColor1] = strval(arrCoords[9]);
	    	CarInfo[idx][cColor2] = strval(arrCoords[10]);
	    	strmid(CarInfo[idx][cOwner], arrCoords[11], 0, strlen(arrCoords[11]), 255);
	    	CarInfo[idx][cTeh] = strval(arrCoords[12]);
	    	CarInfo[idx][cLock] = strval(arrCoords[13]);
	    	CarInfo[idx][cCost] = strval(arrCoords[14]);
	    	CarInfo[idx][cAlarm] = strval(arrCoords[15]);
	    	CarInfo[idx][cPolis] = strval(arrCoords[16]);
   			//Ниже - тюнинг
         	CarInfo[idx][cSpoiler] = strval(arrCoords[17]);
	    	CarInfo[idx][cHood] = strval(arrCoords[18]);
	    	CarInfo[idx][cSideskirt] = strval(arrCoords[19]);
	    	CarInfo[idx][cNitro] = strval(arrCoords[20]);
         	CarInfo[idx][cLamps] = strval(arrCoords[21]);
	    	CarInfo[idx][cExhaust] = strval(arrCoords[22]);
	    	CarInfo[idx][cWheels] = strval(arrCoords[23]);
	    	CarInfo[idx][cRoof] = strval(arrCoords[24]);
        	CarInfo[idx][cStereo] = strval(arrCoords[25]);
	    	CarInfo[idx][cHydraulics] = strval(arrCoords[26]);
	    	CarInfo[idx][cBullbar] = strval(arrCoords[27]);
	    	CarInfo[idx][cRearBullbars] = strval(arrCoords[28]);
	    	CarInfo[idx][cFrontSign] = strval(arrCoords[29]);
	    	CarInfo[idx][cFrontBullbars] = strval(arrCoords[30]);
	    	CarInfo[idx][cFrontBumper] = strval(arrCoords[31]);
	    	CarInfo[idx][cRearBumper] = strval(arrCoords[32]);
	    	CarInfo[idx][cVents] = strval(arrCoords[33]);
	    	CarInfo[idx][cPaintJob] = strval(arrCoords[34]);
			
	    	CarInfo[idx][cKey] = strval(arrCoords[35]);
			CarInfo[idx][cImmob] = strval(arrCoords[36]);
			CarInfo[idx][cProbeg] = strval(arrCoords[37]);
			CarInfo[idx][cZagloh] = strval(arrCoords[38]);
			CarInfo[idx][cRepaired] = strval(arrCoords[39]);
			idx++;
		}
		fclose(file);
	}
	file = fopen("vehicleletters.cfg", io_read);
	new tempdata[MAX_VEHICLE_LETTERS][128];
	if (file)
	{
	    new idx = SIMPLECARS+1;
		while (idx <= OWNABLECARS+SIMPLECARS)
		{
			fread(file, strFromFile2);
			if(strlen(strFromFile2) > 0)
			{
				split(strFromFile2, tempdata, '|');
				for(new i = 0; i < MAX_VEHICLE_LETTERS; i++)
				{
					CarLetters[idx][i] = strval(tempdata[i]);
				}
			}
			idx++;
		}
		fclose(file);
	}
	return 1;
}

public SaveCars()
{
	new idx = SIMPLECARS+1;
	new File: file2;
	while (idx <= GetMaxVehicleID())
	{
	    if(CarInfo[idx][cId] > 399 && CarInfo[idx][cId] < 612)
	    {
			new coordsstring[256];
			format(coordsstring, sizeof(coordsstring), "%d|%f|%f|%f|%f|%f|%f|%f|%f|%d|%d|%s|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d\n",
			CarInfo[idx][cId],
	 		CarInfo[idx][cSTCar_x],
	  		CarInfo[idx][cSTCar_y],
	   		CarInfo[idx][cSTCar_z],
	    	CarInfo[idx][cSTCar_a],
	    	CarInfo[idx][cPARKCar_x],
	    	CarInfo[idx][cPARKCar_y],
	    	CarInfo[idx][cPARKCar_z],
	    	CarInfo[idx][cPARKCar_a],
	      	CarInfo[idx][cColor1],
	    	CarInfo[idx][cColor2],
	    	CarInfo[idx][cOwner],
	     	CarInfo[idx][cTeh],
	    	CarInfo[idx][cLock],
			CarInfo[idx][cCost],
			CarInfo[idx][cAlarm],
			CarInfo[idx][cPolis],
			//Ниже - тюнинг
			CarInfo[idx][cSpoiler],
	 		CarInfo[idx][cHood],
	  		CarInfo[idx][cSideskirt],
	   		CarInfo[idx][cNitro],
	    	CarInfo[idx][cLamps],
	    	CarInfo[idx][cExhaust],
	    	CarInfo[idx][cWheels],
	    	CarInfo[idx][cRoof],
	    	CarInfo[idx][cStereo],
	      	CarInfo[idx][cHydraulics],
	    	CarInfo[idx][cBullbar],
	    	CarInfo[idx][cRearBullbars],
	     	CarInfo[idx][cFrontSign],
	    	CarInfo[idx][cFrontBullbars],
			CarInfo[idx][cFrontBumper],
			CarInfo[idx][cRearBumper],
			CarInfo[idx][cVents],
	  		CarInfo[idx][cPaintJob],
			CarInfo[idx][cKey],
			CarInfo[idx][cImmob],
			CarInfo[idx][cProbeg]);
			if(idx == SIMPLECARS+1)	file2 = fopen("vehicle.cfg", io_write);
			else	file2 = fopen("vehicle.cfg", io_append);
			fwrite(file2, coordsstring);
			fclose(file2);
			coordsstring = "";
   			for(new i = 0; i < MAX_VEHICLE_LETTERS; i++)
			{
				if(i != 0)
				format(coordsstring,sizeof(coordsstring),"%s|%d\n",coordsstring,CarLetters[idx][i]);
				else
				format(coordsstring,sizeof(coordsstring),"%d\n",CarLetters[idx][i]);
			}
			if(idx == SIMPLECARS+1)	file2 = fopen("vehicleletters.cfg", io_write);
			else	file2 = fopen("vehicleletters.cfg", io_append);
			fwrite(file2, coordsstring);
			fclose(file2);
		}
		idx++;
	}
	return 1;
}
public LockCar(carid)
{
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i))
		{
			SetVehicleParamsForPlayer(carid,i,0,1);
		}
	}
	return 1;
}
public UnLockCar(carid)
{
	for(new i = 0; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i))
		{
			if(!IsAPlane(carid))
			{
				SetVehicleParamsForPlayer(carid,i,0,0);
			}
		}
	}
	return 1;
}
public LoadTrunk()
{
    new arrCoords[13][64];
    new strFromFile2[256];
    new File: file = fopen("trunk.cfg", io_read);
	if (file)
	{
	    new idx = 1;
		while (idx < MAX_VEHICLES)
		{
			fread(file, strFromFile2);
			split(strFromFile2, arrCoords, '|');
	    	TrunkInfo[idx][tMoney] = strval(arrCoords[0]);
	    	TrunkInfo[idx][tDrugs] = strval(arrCoords[1]);
	    	TrunkInfo[idx][tGun1] = strval(arrCoords[2]);
	    	TrunkInfo[idx][tGun2] = strval(arrCoords[3]);
	    	TrunkInfo[idx][tGun3] = strval(arrCoords[4]);
         	TrunkInfo[idx][tGun4] = strval(arrCoords[5]);
	    	TrunkInfo[idx][tGun5] = strval(arrCoords[6]);
	    	TrunkInfo[idx][tAmmo1] = strval(arrCoords[7]);
	    	TrunkInfo[idx][tAmmo2] = strval(arrCoords[8]);
         	TrunkInfo[idx][tAmmo3] = strval(arrCoords[9]);
	    	TrunkInfo[idx][tAmmo4] = strval(arrCoords[10]);
	    	TrunkInfo[idx][tAmmo5] = strval(arrCoords[11]);
	    	TrunkInfo[idx][tOpened] = strval(arrCoords[12]);
 			idx++;
		}
		fclose(file);
	}
	return 1;
}
public SaveTrunk()
{
	new idx = 1;
	new File: file2;
	while (idx < MAX_VEHICLES)
	{
		new coordsstring[256];
		format(coordsstring, sizeof(coordsstring), "%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d\n",
		TrunkInfo[idx][tMoney],
 		TrunkInfo[idx][tDrugs],
  		TrunkInfo[idx][tGun1],
   		TrunkInfo[idx][tGun2],
    	TrunkInfo[idx][tGun3],
    	TrunkInfo[idx][tGun4],
    	TrunkInfo[idx][tGun5],
    	TrunkInfo[idx][tAmmo1],
    	TrunkInfo[idx][tAmmo2],
      	TrunkInfo[idx][tAmmo3],
    	TrunkInfo[idx][tAmmo4],
    	TrunkInfo[idx][tAmmo5],
     	TrunkInfo[idx][tOpened]);
		if(idx == 1)
		{
			file2 = fopen("trunk.cfg", io_write);
		}
		else
		{
			file2 = fopen("trunk.cfg", io_append);
		}
		fwrite(file2, coordsstring);
		idx++;
		fclose(file2);
	}
	return 1;
}
public IsAPlane(carid)
{
	new planeid = GetVehicleModel(carid);
	if(planeid==460||planeid==476||planeid==511||planeid==512||planeid==513||planeid==519||planeid==520||planeid==539||planeid==553||planeid==577||planeid==592||planeid==593)
	{
	    return 1;
 	}
	else if(planeid==417||planeid==425||planeid==447||planeid==469||planeid==487||planeid==488||planeid==497||planeid==548||planeid==563)
	{
	    return 1;
 	}
	return 0;
}

public IsAMoped(carid)
{
	new mopedid = GetVehicleModel(carid);
	{
	    if(mopedid==448||mopedid==462)
	    {
			return 1;
 		}
	}
	return 0;
}
public IsAMoto(carid)
{
	new motoid = GetVehicleModel(carid);
	{
	    if(motoid==461||motoid==463||motoid==468||motoid==471||motoid==521||motoid==522||motoid==523||motoid==581||motoid==586)
	    {
			return 1;
 		}
	}
	return 0;
}

public TruckCar(carid)
{
	new truck = GetVehicleModel(carid);
	{
	    if(truck==403||truck==433||truck==407||truck==427||truck==443||truck==455||truck==514||truck==515||truck==525||truck==528||truck==544||truck==573||truck==578||truck==456||truck==440)
	    {
			return 1;
 		}
	}
	return 0;
}
public IsABoat(carid)
{
	new boatid = GetVehicleModel(carid);
	{
	    if(boatid==430||boatid==446||boatid==452||boatid==453||boatid==454||boatid==472||boatid==473||boatid==484||boatid==493||boatid==595)
	    {
			return 1;
 		}
	}
	return 0;
}
public IsABycicle(carid)
{
	new carmodel = GetVehicleModel(carid);
	{
	    if(carmodel == 481 || carmodel == 509 || carmodel == 510 )
	    {
			return 1;
 		}
	}
	return 0;
}
//Для заправок
public IsTruck(carid)
{
	new model = GetVehicleModel(carid);
	if(model==403||model== 406||model== 407||model== 408||model== 414||model== 416||model== 422||model== 423||model== 427||model== 428
	||model== 431||model== 432||model== 433||model== 437||model== 440||model== 443||model==455||model== 456||model== 440||model== 470||
	model== 485||model== 498||model== 499||model== 508||model== 514||model== 515||model== 524||model== 525||model== 528||model== 531||
	model== 532||model== 537||model== 538||model== 544||model== 552||model== 554||model== 573||model== 578||model== 582||model== 583||
	model== 588||model== 599||model== 601||model== 609) return 1;
	return 0;
}
public IsSportCar(carid)
{
	new model = GetVehicleModel(carid);
	if(model== 411||model== 415||model== 429||model== 444
	||model== 451||model== 461||model== 477||model== 494
	||model== 495||model== 502||model== 503||model== 504
	||model== 506||model== 521||model== 522||model== 541
	||model== 556||model== 557||model== 581) return 1;
	return 0;
}
public IsEliteCar(carid)
{
	new model = GetVehicleModel(carid);
	if(model==409||model== 402||model== 405||model== 426||model== 434||model== 436||model== 445||model== 458
	||model== 468||model== 471||model== 480||model== 496||model== 507||model== 516||model== 523||model== 527
	||model== 529||model== 533||model== 535||model== 540||model== 545||model== 546||model== 547||model== 550
	||model== 551||model== 555||model== 558||model== 559||model== 560||model== 561||model== 562||model== 565
	||model== 568||model== 571||model== 580||model== 585||model== 586||model== 587||model== 589||model== 596
	||model== 597||model== 602||model== 603) return 1;
	return 0;
}
public IsStandartCar(carid)
{
	new model = GetVehicleModel(carid);
	if(model ==400||model == 401||model == 404||model == 410||model == 412||model == 413||model == 418
	||model == 419||model == 420||model == 421||model == 424||model == 438||model == 439||model == 442
	||model == 448||model == 459||model == 462||model == 463||model == 466||model == 467||model == 474
	||model == 475||model == 478||model == 479||model == 482||model == 483||model == 489||model == 490
	||model == 491||model == 492||model == 500||model == 505||model == 517||model == 518||model == 526
	||model == 534||model == 536||model == 542||model == 543||model == 549||model == 566||model == 567
	||model == 575||model == 576||model == 579||model == 598||model == 600||model == 604||model == 605) return 1;
	return 0;
}
stock GetVehicleMaxGas(carid)//Сколько максимум бензина влезет в эту тачку
{
	if(IsTruck(carid)) return MAX_TRUCK_GAS;
	if(IsSportCar(carid)) return MAX_SPORT_GAS;
	if(IsEliteCar(carid)) return MAX_ELITE_GAS;
	return MAX_OTHER_GAS;
}
