/*
	PostNuke Wasteland Car System
	Date: 22.10.2011
	Author: NeoPhoenix	
*/

#if defined lib_cars_included
	#endinput
#else
	#define lib_cars_included
#endif

#define car:: car_
/*
	native car::DestroyVehicle(veh,bool:_save=true);
	native car::CreateVehicle(modelid,Float:x,Float:y,Float:z,Float:angle,color1,color2,respawn_delay,bool:engine_on=true,bool:doors_locked=false,bool:_save=true,vint=0,vvw=0);
	native car::OnVehicleSystemInit();
	native car::ShowPlayerCarMainDialog(playerid,veh,bool:in_car);
	native car::ActivateVehicleAlarm(vehicleid, time_s, bool:zusatz=false);
	native car::DisableVehicleAlarm(vehicleid,by_timer);
	native car::OnVehicleStreamIn(vehicleid, forplayerid);
	native car::OnCarUpdate(veh);
	native car::StartEngine(veh);
	native car::GetModelFueltype(model);
	native car::GetModelMaxTank(model);
	native car::GetModelVerbrauch(model);
*/

//Including 'pnwl_mysql' and player basics
#include "libraries/lib_core"

forward car_OnVehicleSpawn(vehicleid);
forward car_OnVehicleDeath(vehicleid,killerid);

#define MAX_VEHICLE_IDLE			(60*30)//Sekunden bis zum Respawn, sofern nicht benutzt
#define MAX_VEH_OBJECTS				(3)
#define encode_lights(%0,%1,%2,%3)	(%0|(%1<<1)|(%2<<2)|(%3<<3))
#define encode_fuel (%0,%1,%2,%3)	(%0|(%1<<1)|(%2<<2)|(%3<<3))

new Text:tcar_StatusHUL[4],
	Text:tcar_StatusENG[4],
	Text:tcar_StatusWHL[4],
	Text:tcar_StatusBAT[4],
	Text:tcar_FuelBackground,
	Text:tcar_Heading,
	PlayerText:tcar_PLAYER_Fuel[MAX_PLAYERS] = PlayerText:-1;

enum (+=1)
{
	dialog_CAR_IN_MAINRE = 1,
	dialog_CAR_IN_MAIN,
	dialog_CAR_IN_LIGHT,
	dialog_CAR_IN_ENGINE,
	dialog_CAR_IN_LOCK,
	dialog_CAR_OUT_MAIN,
	dialog_CAR_OUT_MAINRE,
}

car_ShowPlayerDialog(playerid, dialogid, dialogstyle, header[], info[], but1[], but2[])
{
	SetPVarInt(playerid,"car_dialog",1);
	ShowPlayerDialog(playerid,dialogid,dialogstyle,header,info,but1,but2);
	return 1;
}

enum cars_data
{
	bool:cCreated,
	bool:save_vehicle_data,
	cID,
	cOwner,
	bool:cIsDead,
	Float:cHealth,
	Float:cPos[4],
	Float:vtmpPos[3],
	cDamageStatus[4],
	cVINT[2],
	cVVW,
	cKeys[10],
	bool:cEngine,
	cLocked[2],
	bool:cLights,
	Float:cFuel,
	cFueltype,
	Float:cMaxTank,
	Float:cBatterie,
	Float:cGefahren,
	cColor[2],
	cAlarm[3],
	cOption,
	timer_Alarm,
	cPlate[32],
	Text3D:cLabel,
	cPaintjob,
	cLastUse,
	cItemsUnique,
	cAttachedObjects[MAX_VEH_OBJECTS]
}
new VehicleInfo[MAX_VEHICLES][cars_data];

#define IsVehicleConnected(%0) VehicleInfo[%0][cCreated]
#define ResetVehVars(%0) for(new enm; cars_data:enm<cars_data; enm++) VehicleInfo[%0][cars_data:enm] = 0
#define car_GetModelName(%0) VehicleNames[%0-400]

new VehicleNames[212][] =
{
	{"Landstalker"},{"Bravura"},{"Buffalo"},{"Linerunner"},{"Perrenial"},{"Sentinel"},{"Dumper"},
	{"Firetruck"},{"Trashmaster"},{"Stretch"},{"Manana"},{"Infernus"},{"Voodoo"},{"Pony"},{"Mule"},
	{"Cheetah"},{"Ambulance"},{"Leviathan"},{"Moonbeam"},{"Esperanto"},{"Taxi"},{"Washington"},
	{"Bobcat"},{"Mr Whoopee"},{"BF Injection"},{"Hunter"},{"Premier"},{"Enforcer"},{"Securicar"},
	{"Banshee"},{"Predator"},{"Bus"},{"Rhino"},{"Barracks"},{"Hotknife"},{"Trailer 1"},{"Previon"},
	{"Coach"},{"Cabbie"},{"Stallion"},{"Rumpo"},{"RC Bandit"},{"Romero"},{"Packer"},{"Monster"},
	{"Admiral"},{"Squalo"},{"Seasparrow"},{"Pizzaboy"},{"Tram"},{"Trailer 2"},{"Turismo"},
	{"Speeder"},{"Reefer"},{"Tropic"},{"Flatbed"},{"Yankee"},{"Caddy"},{"Solair"},{"Berkley's RC Van"},
	{"Skimmer"},{"PCJ-600"},{"Faggio"},{"Freeway"},{"RC Baron"},{"RC Raider"},{"Glendale"},{"Oceanic"},
	{"Sanchez"},{"Sparrow"},{"Patriot"},{"Quad"},{"Coastguard"},{"Dinghy"},{"Hermes"},{"Sabre"},
	{"Rustler"},{"ZR-350"},{"Walton"},{"Regina"},{"Comet"},{"BMX"},{"Burrito"},{"Camper"},{"Marquis"},
	{"Baggage"},{"Dozer"},{"Maverick"},{"News Chopper"},{"Rancher"},{"FBI Rancher"},{"Virgo"},{"Greenwood"},
	{"Jetmax"},{"Hotring"},{"Sandking"},{"Blista Compact"},{"Police Maverick"},{"Boxville"},{"Benson"},
	{"Mesa"},{"RC Goblin"},{"Hotring Racer A"},{"Hotring Racer B"},{"Bloodring Banger"},{"Rancher"},
	{"Super GT"},{"Elegant"},{"Journey"},{"Bike"},{"Mountain Bike"},{"Beagle"},{"Cropdust"},{"Stunt"},
	{"Tanker"}, {"Roadtrain"},{"Nebula"},{"Majestic"},{"Buccaneer"},{"Shamal"},{"Hydra"},{"FCR-900"},
	{"NRG-500"},{"HPV1000"},{"Cement Truck"},{"Tow Truck"},{"Fortune"},{"Cadrona"},{"FBI Truck"},
	{"Willard"},{"Forklift"},{"Tractor"},{"Combine"},{"Feltzer"},{"Remington"},{"Slamvan"},
	{"Blade"},{"Freight"},{"Streak"},{"Vortex"},{"Vincent"},{"Bullet"},{"Clover"},{"Sadler"},
	{"Firetruck LA"},{"Hustler"},{"Intruder"},{"Primo"},{"Cargobob"},{"Tampa"},{"Sunrise"},{"Merit"},
	{"Utility"},{"Nevada"},{"Yosemite"},{"Windsor"},{"Monster A"},{"Monster B"},{"Uranus"},{"Jester"},
	{"Sultan"},{"Stratum"},{"Elegy"},{"Raindance"},{"RC Tiger"},{"Flash"},{"Tahoma"},{"Savanna"},
	{"Bandito"},{"Freight Flat"},{"Streak Carriage"},{"Kart"},{"Mower"},{"Duneride"},{"Sweeper"},
	{"Broadway"},{"Tornado"},{"AT-400"},{"DFT-30"},{"Huntley"},{"Stafford"},{"BF-400"},{"Newsvan"},
	{"Tug"},{"Trailer 3"},{"Emperor"},{"Wayfarer"},{"Euros"},{"Hotdog"},{"Club"},{"Freight Carriage"},
	{"Trailer 3"},{"Andromada"},{"Dodo"},{"RC Cam"},{"Launch"},{"Police Car (LSPD)"},{"Police Car (SFPD)"},
	{"Police Car (LVPD)"},{"Police Ranger"},{"Picador"},{"S.W.A.T. Van"},{"Alpha"},{"Phoenix"},{"Glendale"},
	{"Sadler"},{"Luggage Trailer A"},{"Luggage Trailer B"},{"Stair Trailer"},{"Boxville"},{"Farm Plow"},
	{"Utility Trailer"}
};

car::UpdatePlayerTextdraws(playerid)
{
	if(!IsPlayerInAnyVehicle(playerid) || GetPlayerVehicleSeat(playerid) != 0) return 0;
	new veh				= GetPlayerVehicleID(playerid),
		Float:percentage= VehicleInfo[veh][cFuel]/VehicleInfo[veh][cMaxTank],
		Float:vstatus[4];
	vstatus = car::FetchVehicleDamageStatus(veh);
	if(percentage < 0.0)		percentage = 0.0;
	else if(percentage > 1.0)	percentage = 1.0;
	for(new i; i<4; i++)
	{
		TextDrawHideForPlayer(playerid, tcar_StatusENG[i]);
		TextDrawHideForPlayer(playerid, tcar_StatusHUL[i]);
		TextDrawHideForPlayer(playerid, tcar_StatusWHL[i]);
		TextDrawHideForPlayer(playerid, tcar_StatusBAT[i]);
	}
	
	//Engine
	if(vstatus[0] >= 90.0)		TextDrawShowForPlayer(playerid, tcar_StatusENG[0]);
	else if(vstatus[0] >= 50.0)	TextDrawShowForPlayer(playerid, tcar_StatusENG[1]);
	else if(vstatus[0] >= 40.0)	TextDrawShowForPlayer(playerid, tcar_StatusENG[2]);
	else						TextDrawShowForPlayer(playerid, tcar_StatusENG[3]);
	
	//Hull
	if(vstatus[1] >= 80.0)		TextDrawShowForPlayer(playerid, tcar_StatusHUL[0]);
	else if(vstatus[1] >= 50.0)	TextDrawShowForPlayer(playerid, tcar_StatusHUL[1]);
	else if(vstatus[1] >= 40.0)	TextDrawShowForPlayer(playerid, tcar_StatusHUL[2]);
	else						TextDrawShowForPlayer(playerid, tcar_StatusHUL[3]);
	
	//Wheels
	if(vstatus[2] >= 90.0)		TextDrawShowForPlayer(playerid, tcar_StatusWHL[0]);
	else if(vstatus[2] >= 75.0)	TextDrawShowForPlayer(playerid, tcar_StatusWHL[1]);
	else if(vstatus[2] >= 50.0)	TextDrawShowForPlayer(playerid, tcar_StatusWHL[2]);
	else						TextDrawShowForPlayer(playerid, tcar_StatusWHL[3]);
	
	//Battery
	if(vstatus[3] >= 90.0)		TextDrawShowForPlayer(playerid, tcar_StatusBAT[0]);
	else if(vstatus[3] >= 60.0)	TextDrawShowForPlayer(playerid, tcar_StatusBAT[1]);
	else if(vstatus[3] >= 30.0)	TextDrawShowForPlayer(playerid, tcar_StatusBAT[2]);
	else						TextDrawShowForPlayer(playerid, tcar_StatusBAT[3]);
	
	if(_:tcar_PLAYER_Fuel[playerid] != -1)
	{
		PlayerTextDrawHide(playerid, tcar_PLAYER_Fuel[playerid]);
		PlayerTextDrawDestroy(playerid, tcar_PLAYER_Fuel[playerid]);
		tcar_PLAYER_Fuel[playerid] = PlayerText:-1;
	}
	tcar_PLAYER_Fuel[playerid] = CreatePlayerTextDraw(playerid, 607.000000, 167.000000, "~f~");
	PlayerTextDrawAlignment(playerid, tcar_PLAYER_Fuel[playerid], 2);
	PlayerTextDrawBackgroundColor(playerid, tcar_PLAYER_Fuel[playerid], 255);
	PlayerTextDrawFont(playerid, tcar_PLAYER_Fuel[playerid], 1);
	PlayerTextDrawLetterSize(playerid, tcar_PLAYER_Fuel[playerid], 0.500000, -0.5-(5.7*percentage));
	PlayerTextDrawColor(playerid, tcar_PLAYER_Fuel[playerid], -1);
	PlayerTextDrawSetOutline(playerid, tcar_PLAYER_Fuel[playerid], 0);
	PlayerTextDrawSetProportional(playerid, tcar_PLAYER_Fuel[playerid], 1);
	PlayerTextDrawSetShadow(playerid, tcar_PLAYER_Fuel[playerid], 1);
	PlayerTextDrawUseBox(playerid, tcar_PLAYER_Fuel[playerid], 1);
	PlayerTextDrawBoxColor(playerid, tcar_PLAYER_Fuel[playerid], RGB_Verlauf(1.0-percentage)<<8|0xFF);
	PlayerTextDrawTextSize(playerid, tcar_PLAYER_Fuel[playerid], 0.000000, 6.000000);
	PlayerTextDrawShow(playerid, tcar_PLAYER_Fuel[playerid]);
	return 1;
}

car::DestroyVehicle(veh,bool:_save=true)
{
	if(IsVehicleConnected(veh) == false) return 0;
	for(new o; o<MAX_VEH_OBJECTS; o++)
	{
		if(IsValidObject(VehicleInfo[veh][cAttachedObjects][o]))
		{
			DestroyObject(VehicleInfo[veh][cAttachedObjects][o]);
		}
		VehicleInfo[veh][cAttachedObjects][o] = -1;
	}
	DestroyVehicle(veh);
	if(_save && VehicleInfo[veh][save_vehicle_data] == true)
	{
		car::item_store_DeleteEntry(veh);
		format(query,sizeof(query),"DELETE FROM pnwl_cars WHERE vid='%d'",VehicleInfo[veh][cID]);
		mysql_query(query);
		mysql_free_result();
	}
	
	ResetVehVars(veh);
	__SERVER[VEHICLES]--;
	
	if(veh == __SERVER[MAX_VEHICLE_ID])
	{
		new bool:changed=false;
		for(new vehl=veh; vehl>=0; vehl--)
		{
			if(IsVehicleConnected(vehl))
			{
				__SERVER[MAX_VEHICLE_ID] = vehl;
				changed = true;
				break;
			}
		}
		if(changed && __SERVER[MAX_VEHICLE_ID] == 0) __SERVER[MAX_VEHICLE_ID] = 1;
		else __SERVER[MAX_VEHICLE_ID] = 1;		
	}
	return __SERVER[MAX_VEHICLE_ID];
}

car::CreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay,bool:engine_on=false,bool:doors_locked=false,bool:_save=true,vint=0,vvw=0)
{
	if(modelid < 400 || modelid > 611) return -1;
	new veh = CreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay);
	ResetVehVars(veh);
	VehicleInfo[veh][cIsDead]			= false;
	VehicleInfo[veh][cHealth]			= 1000.0;
	VehicleInfo[veh][save_vehicle_data] = _save;
	VehicleInfo[veh][cColor][0]			= color1;
	VehicleInfo[veh][cColor][1]			= color2;
	VehicleInfo[veh][cCreated] 			= true;
	VehicleInfo[veh][cPos][0] 			= x;
	VehicleInfo[veh][cPos][1] 			= y;
	VehicleInfo[veh][cPos][2] 			= z;
	VehicleInfo[veh][cPos][3] 			= angle;
	VehicleInfo[veh][cVINT][0]			= vint;
	VehicleInfo[veh][cVVW]				= vvw;
	VehicleInfo[veh][cVINT][1]			= vint;
	VehicleInfo[veh][cEngine] 			= engine_on;
	VehicleInfo[veh][cLights]			= false;
	VehicleInfo[veh][cFueltype]			= car::GetModelFuelType(modelid);
	VehicleInfo[veh][cMaxTank]			= car::GetModelMaxTank(modelid);
	VehicleInfo[veh][cFuel]				= VehicleInfo[veh][cMaxTank];
	VehicleInfo[veh][cItemsUnique]		= 0;
	format(VehicleInfo[veh][cPlate],32,"PNWL v%d",veh);
	SetVehicleNumberPlate(veh,VehicleInfo[veh][cPlate]);
	new engine, lights, alarm, doors, bonnet, boot, objective;
	GetVehicleParamsEx(veh, engine, lights, alarm, doors, bonnet, boot, objective);
	SetVehicleParamsEx(veh, engine_on==true?1:0, lights, 0, doors_locked==true?1:0, bonnet, boot, objective);
	LinkVehicleToInterior(veh,vint);
	SetVehicleVirtualWorld(veh,vvw);
	if(doors_locked == true)
	{
		VehicleInfo[veh][cLocked][0] = 	1;
		VehicleInfo[veh][cLocked][1] = -1;
		for(new pid; pid<=__SERVER[HIGHEST_PLAYER_ID]; pid++)
		{
			SetVehicleParamsForPlayer(veh,pid,0,1);	
		}
	}
	__SERVER[VEHICLES]++;
	if(veh > __SERVER[MAX_VEHICLE_ID]) __SERVER[MAX_VEHICLE_ID] = veh;
	
	if(_save)
	{
		format(string,sizeof(string),"INSERT INTO pnwl_cars (isdead,model,status_reifen,status_lights,status_engine,x,y,z,rot,vint,vvw,fuel,fueltype,maxtank) VALUES ('0','%d','100.0','100.0','100.0','%0.2f','%0.2f','%0.2f','%0.2f','%d','%d','%0.2f','%d','%0.2f')",modelid,x,y,z,angle,vint,vvw,VehicleInfo[veh][cFuel],VehicleInfo[veh][cFueltype],VehicleInfo[veh][cMaxTank]);
		mysql_query(string);
		VehicleInfo[veh][cID] = mysql_insert_id();
		mysql_free_result();
		format(query,sizeof(query),"PNWL %d",VehicleInfo[veh][cID]);
		format(VehicleInfo[veh][cPlate],32,query);
		SetVehicleNumberPlate(veh,VehicleInfo[veh][cPlate]);
		format(string,sizeof(string),"DELETE FROM pnwl_car_items WHERE vid='%d'",VehicleInfo[veh][cID]);
		db_query_once(SQL_DB,string);
		car::OnCarUpdate(veh,true);
	}
	SetVehicleToRespawn(veh);
	return veh;
}

car::OnVehicleSystemInit()
{
	__SERVER[MAX_VEHICLE_ID] = 1;
	format(query,sizeof(query),"SELECT * FROM pnwl_cars LIMIT %d",MAX_VEHICLES);
	mysql_query(query);
	mysql_store_result();
	printf("LOADING VEHICLES. COUNT: %d",mysql_num_rows());
	new Float:v_pos[3],f[2],veh,Float:rot,engine,lights,alarm,doors,bonnet,boot,objective,vint,vvw;
	
	for(new i; i<MAX_VEHICLES; i++)
	{
		for(new o; o<MAX_VEH_OBJECTS; o++)
		{
			VehicleInfo[i][cAttachedObjects][o] = -1;
		}
	}
	
	if(mysql_num_rows() >= 1)
	{
	    while(mysql_retrieve_row())
	    {
	        mysql_fetch_field_row(string,"x");
	        v_pos[0] = floatstr(string);
	        mysql_fetch_field_row(string,"y");
	        v_pos[1] = floatstr(string);				
	        mysql_fetch_field_row(string,"z");
	        v_pos[2] = floatstr(string);
	        mysql_fetch_field_row(string,"rot");
	        rot = floatstr(string);

	        mysql_fetch_field_row(string,"f1");
	        f[0] = strval(string);
	        mysql_fetch_field_row(string,"f2");
	        f[1] = strval(string);
			
	        mysql_fetch_field_row(string,"vint");
	        vint = strval(string);
	        mysql_fetch_field_row(string,"vvw");
	        vvw = strval(string);
	
			mysql_fetch_field_row(string,"model");
			veh = car::CreateVehicle(strval(string),v_pos[0],v_pos[1],v_pos[2],rot,f[0],f[1],-1,_,_,false,vint,vvw);//Nicht speichern
			
			mysql_fetch_field_row(string,"isdead");
			VehicleInfo[veh][cIsDead] = strval(string)==1?true:false;
			if(VehicleInfo[veh][cIsDead]) VehicleInfo[veh][cHealth] = 260.0;
			else
			{
				mysql_fetch_field_row(string,"engine_hp");
				VehicleInfo[veh][cHealth] = floatstr(string);
				if(VehicleInfo[veh][cHealth] < 260.0)		VehicleInfo[veh][cHealth] = 260.0;
				else if(VehicleInfo[veh][cHealth] > 1000.0)	VehicleInfo[veh][cHealth] = 1000.0;
				SetVehicleHealth(veh,VehicleInfo[veh][cHealth]);
			}

			mysql_fetch_field_row(string,"vid");
			VehicleInfo[veh][cID] = strval(string);
			
			VehicleInfo[veh][cPos][0] 			= v_pos[0];
			VehicleInfo[veh][cPos][1] 			= v_pos[1];
			VehicleInfo[veh][cPos][2]			= v_pos[2];
			VehicleInfo[veh][cPos][3]			= rot;
			VehicleInfo[veh][cVINT][0]			= vint;
			VehicleInfo[veh][cVINT][1]			= vint;
			VehicleInfo[veh][cVVW]				= vvw;
			
			mysql_fetch_field_row(string,"locked_position");
			VehicleInfo[veh][save_vehicle_data] = strval(string)==1?false:true;
			VehicleInfo[veh][cPlate][0] = 0;
			mysql_fetch_field_row(VehicleInfo[veh][cPlate],"nplate");
			if(!VehicleInfo[veh][cPlate][0]) format(VehicleInfo[veh][cPlate],32,"PNWL - %d",VehicleInfo[veh][cID]);
			SetVehicleNumberPlate(veh,VehicleInfo[veh][cPlate]);
			
			mysql_fetch_field_row(string,"owner");
			VehicleInfo[veh][cOwner] = strval(string);		
			mysql_fetch_field_row(string,"engine");
			VehicleInfo[veh][cEngine] = strval(string)==1?true:false;
			mysql_fetch_field_row(string,"locked");
			VehicleInfo[veh][cLocked][0] = strval(string);
			mysql_fetch_field_row(string,"locktype");
			VehicleInfo[veh][cLocked][1] = strval(string);
			mysql_fetch_field_row(string,"batterie");
			VehicleInfo[veh][cBatterie] = floatstr(string);
			mysql_fetch_field_row(string,"gefahren");
			VehicleInfo[veh][cGefahren] = floatstr(string);
			mysql_fetch_field_row(string,"fuel");
			VehicleInfo[veh][cFuel] = floatstr(string);
			mysql_fetch_field_row(string,"fueltype");
			VehicleInfo[veh][cFueltype] = strval(string);
			mysql_fetch_field_row(string,"maxtank");
			VehicleInfo[veh][cMaxTank] = floatstr(string);			
			mysql_fetch_field_row(string,"alarm");
			VehicleInfo[veh][cAlarm][1] = strval(string);
			mysql_fetch_field_row(string,"alarmon");
			VehicleInfo[veh][cAlarm][0] = strval(string);
			mysql_fetch_field_row(string,"lightson");
			VehicleInfo[veh][cLights] = strval(string)==1?true:false;	
			
			mysql_fetch_field_row(string,"owner");
			VehicleInfo[veh][cOwner] = strval(string);
			
			VehicleInfo[veh][cColor][0] = f[0];
			VehicleInfo[veh][cColor][1] = f[1];
			VehicleInfo[veh][cLastUse]	= gettime(_,_,_);
			VehicleInfo[veh][cCreated]	= true;
			
			SetVehicleToRespawn(veh);

			ChangeVehicleColor(veh,f[0],f[1]);
			mysql_fetch_field_row(string,"paintjob");
			if(strval(string) >= 1)
			{
				ChangeVehiclePaintjob(veh,strval(string)-1);
				VehicleInfo[veh][cPaintjob] = strval(string);
			}
			
			GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
	        mysql_fetch_field_row(string,"panels");
	        VehicleInfo[veh][cDamageStatus][0] = strval(string);
	        mysql_fetch_field_row(string,"doors");
	        VehicleInfo[veh][cDamageStatus][1] = strval(string);
	        mysql_fetch_field_row(string,"lights");
	        VehicleInfo[veh][cDamageStatus][2] = strval(string);
	        mysql_fetch_field_row(string,"tires");
	        VehicleInfo[veh][cDamageStatus][3] = strval(string);
			SetVehicleParamsEx(veh,VehicleInfo[veh][cEngine]==true?1:0,VehicleInfo[veh][cLights]==true?1:0,0,doors,bonnet,boot,objective);
			UpdateVehicleDamageStatus(veh,VehicleInfo[veh][cDamageStatus][0],VehicleInfo[veh][cDamageStatus][1],VehicleInfo[veh][cDamageStatus][2],VehicleInfo[veh][cDamageStatus][3]);
		}
	}
	mysql_free_result();
	return 1;
}

car::OnVehicleStreamIn(vehicleid, forplayerid)
{
	if(PlayerInfo[forplayerid][PLAYER_ID] == VehicleInfo[vehicleid][cOwner]) SetVehicleParamsForPlayer(vehicleid,forplayerid,0,0);
	else SetVehicleParamsForPlayer(vehicleid,forplayerid,0,VehicleInfo[vehicleid][cLocked][0]>=1?1:0);
	return 1;
}

forward Float:car::FetchVehicleDamageStatus(vehicleid);
stock Float:car::FetchVehicleDamageStatus(vehicleid)
{
	new Float:arr[4] = {100.0,...};//Engine, Hull, Wheels, Battery
	GetVehicleHealth(vehicleid,arr[0]);
	arr[0] /= 10.0;
	arr[3] = VehicleInfo[vehicleid][cBatterie];
	
	GetVehicleDamageStatus(vehicleid,VehicleInfo[vehicleid][cDamageStatus][0],VehicleInfo[vehicleid][cDamageStatus][1],VehicleInfo[vehicleid][cDamageStatus][2],VehicleInfo[vehicleid][cDamageStatus][3]);
	//panels, doors, lights, tires
	
	//tires
	if(VehicleInfo[vehicleid][cDamageStatus][3] & 0b0001) arr[2] -= 25.0;
	if(VehicleInfo[vehicleid][cDamageStatus][3] & 0b0010) arr[2] -= 25.0;
	if(VehicleInfo[vehicleid][cDamageStatus][3] & 0b0100) arr[2] -= 25.0;
	if(VehicleInfo[vehicleid][cDamageStatus][3] & 0b1000) arr[2] -= 25.0;
	
	for(new i; i<4; i++)
	{
		if(arr[i] < 0.0)	arr[i] = 0.0;
		if(arr[i] > 100.0)	arr[i] = 100.0;
	}
	return arr;
}

stock car::ActivateVehicleAlarm(vehicleid, time_s, bool:zusatz=false)
{
	if(time_s <= 0) time_s = 1;
	new engine,lights,alarm,doors,bonnet,boot,objective;
	GetVehicleParamsEx(vehicleid,engine,lights,alarm,doors,bonnet,boot,objective);
	SetVehicleParamsEx(vehicleid,engine,lights,1,doors,bonnet,boot,objective);
	KillTimer(VehicleInfo[vehicleid][timer_Alarm]);
	VehicleInfo[vehicleid][cAlarm][2] = time_s;
	VehicleInfo[vehicleid][cOption] = 0;
	if(zusatz == true) VehicleInfo[vehicleid][cOption] += (1<<0);
	if(zusatz == true && VehicleInfo[vehicleid][cAlarm][1] >= 1)
	{
		if(VehicleInfo[vehicleid][cAlarm][1] >= 3)
		{//3D Text Label wird erstellt
			new Float:v_pos[3];
			GetVehiclePos(vehicleid,v_pos[0],v_pos[1],v_pos[2]);
			format(string,sizeof(string),"ALARM %d",time_s);
			VehicleInfo[vehicleid][cLabel] = Create3DTextLabel(string,ROT,v_pos[0],v_pos[1],v_pos[2],15.0,0,0);
			Attach3DTextLabelToVehicle(VehicleInfo[vehicleid][cLabel], vehicleid, 0.0, 0.0, 0.5);
			VehicleInfo[vehicleid][cOption] += (1<<0);
		}
		if(VehicleInfo[vehicleid][cAlarm][1] >= 4)
		{//Owner erhält eine Nachricht
			for(new playerid; playerid<=__SERVER[HIGHEST_PLAYER_ID]; playerid++)
			{
				if(PlayerInfo[playerid][PLAYER_ID] == VehicleInfo[vehicleid][cOwner])
				{
					format(string,sizeof(string),"ACHTUNG: Es wurde soeben versucht dein Fahrzeug mit dem Kennzeichen \"%s\" aufzubrechen/kurzzuschließen.",VehicleInfo[vehicleid][cPlate]);
					SendClientMessage(playerid,ROT,string);
					break;
				}
			}
		}
	}
	VehicleInfo[vehicleid][timer_Alarm] = SetTimerEx("car::DisableVehicleAlarm", 1000, false, "dd", vehicleid, 1);
	return 1;
}
 
forward car::DisableVehicleAlarm(vehicleid,by_timer);
public car::DisableVehicleAlarm(vehicleid,by_timer)
{
	if(by_timer == 0 || (by_timer == 1 && (--VehicleInfo[vehicleid][cAlarm][2] == 0)))
	{
		new engine, lights, alarm, doors, bonnet, boot, objective;
		GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
		SetVehicleParamsEx(vehicleid, engine, lights, 0, doors, bonnet, boot, objective);
		if(VehicleInfo[vehicleid][cOption] & 1 && VehicleInfo[vehicleid][cOption] & 2)
		{
			Delete3DTextLabel(VehicleInfo[vehicleid][cLabel]);
		}
		VehicleInfo[vehicleid][cOption] = 0;
		return 1;
	}
	else if(by_timer == 1 && VehicleInfo[vehicleid][cAlarm][2] >= 1)
	{
		if(VehicleInfo[vehicleid][cAlarm][1] >= 3)
		{
			format(string,sizeof(string),"ALARM %d",VehicleInfo[vehicleid][cAlarm][2]);
			Update3DTextLabelText(VehicleInfo[vehicleid][cLabel],ROT,string);
			new engine,lights,alarm,doors,bonnet,boot,objective;
			GetVehicleParamsEx(vehicleid,engine,lights,alarm,doors,bonnet,boot,objective);
			SetVehicleParamsEx(vehicleid,engine,lights,1,doors,bonnet,boot,objective);
		}
		VehicleInfo[vehicleid][timer_Alarm] = SetTimerEx("car::DisableVehicleAlarm", 1000, false, "dd", vehicleid,1);
	}
	return 1;
}

car_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(GetPVarInt(playerid,"car_dialog") != 1) return 0;
	SetPVarInt(playerid,"car_dialog",0);

	switch(dialogid)
	{
		/*if(dialogid == dialog_CAR_ENGINE_START)
		{
			if(GetPVarInt(playerid,"motor_an") >= 1)
			{
				format(string,sizeof(string),"%d",GetPVarInt(playerid,"motor_an"));
				if(GetPVarInt(playerid,"motor_type") == 0) car_ShowPlayerDialog(playerid,dialog_CAR_ENGINE_START,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du versuchst den Motor {77AE35}anzustellen{F1F7DC}.",string,"");
				else car_ShowPlayerDialog(playerid,dialog_CAR_ENGINE_START,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du versuchst das Fahrzeug {77AE35}kurzzuschließen{F1F7DC}.",string,"");
			}
			return 1;
		}*/
		case dialog_CAR_IN_MAINRE:
		{
			ncom_auto(playerid);
			return 1;
		}
		case dialog_CAR_OUT_MAIN:
		{
			if(!response) return 1;			
			new veh = GetPVarInt(playerid,"interact_veh"),bool:valid=true;
			if(!IsVehicleConnected(veh)) valid = false;
			if(valid)
			{
				new Float:v_pos[3];
				GetVehiclePos(veh,v_pos[0],v_pos[1],v_pos[2]);
				if(!IsPlayerInRangeOfPoint(playerid,5.0,v_pos[0],v_pos[1],v_pos[2])) valid = false;
			}
			if(!valid)
			{//er ist nicht mehr am fahrzeug oder fahrzeug wurde zerstört
				ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{C85135}Fehler: Du befindest dich nicht am Auto.","Ok","");
				return 1;
			}
			switch(GetDialogOption(playerid,listitem))
			{
				case 0:
				{//Informationen
					new modell=GetVehicleModel(veh),kraftstoff[50];
					misc_FetchFuelTypes(kraftstoff,VehicleInfo[veh][cFueltype]);
					if(VehicleInfo[veh][cOwner] == 0) format(string,sizeof(string),"{F1F7DC}Besitzer: \t\t{77AE35}Keiner");
					else format(string,sizeof(string),"{F1F7DC}Besitzer: \t\t{C85135}%s",mysql_GetStringByInt("pnwl_user","username","userid",VehicleInfo[veh][cOwner]));
					format(string,sizeof(string),"%s\n{F1F7DC}Modell: \t%s (%d)",string,car::GetModelName(modell),modell);
					format(string,sizeof(string),"%s\n{F1F7DC}Tankgröße: \t%0.2f Liter",string,car::GetModelMaxTank(modell));
					format(string,sizeof(string),"%s\n{F1F7DC}Verbrauch: \t%0.2f Liter/100km",string,car::GetModelVerbrauch(modell));
					format(string,sizeof(string),"%s\n{F1F7DC}Kraftstoff: \t%s",string,kraftstoff);
					format(string,sizeof(string),"%s\n{F1F7DC}Kennzeichen: \t%s",string,VehicleInfo[veh][cPlate]);
					format(string,sizeof(string),"%s\n{F1F7DC}Status: \t%s",string,VehicleInfo[veh][cLocked][0]==1?("{C85135}abgeschlossen"):("{77AE35}geöffnet"));
					car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"Zurück","");
				}
				case 3:
				{//Auto abschließen (OWNER)
					if(VehicleInfo[veh][cOwner] != 0 && PlayerInfo[playerid][PLAYER_ID] != VehicleInfo[veh][cOwner])
					{
						ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{C85135}Fehler: Das Fahrzeug gehört nicht dir.","Ok","");
						return 1;
					}
					if(VehicleInfo[veh][cLocked][1] == -1)
					{
						car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Dieses Schloss kann nicht aufgeschlossen werden.","Zurück","");
						return 1;
					}
					if(++VehicleInfo[veh][cLocked][0] == 2) VehicleInfo[veh][cLocked][0] = 0;
					for(new pid; pid<=__SERVER[HIGHEST_PLAYER_ID]; pid++)
					{
						if(PlayerInfo[pid][PLAYER_ID] == VehicleInfo[veh][cOwner]) SetVehicleParamsForPlayer(veh,pid,0,0);
						else SetVehicleParamsForPlayer(veh,pid,0,VehicleInfo[veh][cLocked][0]>=1?1:0);	
					}
					format(string,sizeof(string),"{F1F7DC}Du hast das Schloss (Stufe {C85135}%d{F1F7DC}) %s{F1F7DC}.",VehicleInfo[veh][cLocked][1],VehicleInfo[veh][cLocked][0]==1?("{C85135}abgeschlossen"):("{77AE35}aufgeschlossen"));
					car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"Zurück","");
				}
				case 4:
				{//Alarm konfigurieren (OWNER)
					if(PlayerInfo[playerid][PLAYER_ID] != VehicleInfo[veh][cOwner])
					{
						ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{C85135}Fehler: Das Fahrzeug gehört nicht dir.","Ok","");
						return 1;
					}
					else
					{
						VehicleInfo[veh][cAlarm][0] = VehicleInfo[veh][cAlarm][0]==1?0:1;
						if(VehicleInfo[veh][cAlarm][0] == 1) format(string,sizeof(string),"{F1F7DC}Du hast den Alarm {77AE35}aktiviert{F1F7DC}.");
						else format(string,sizeof(string),"{F1F7DC}Du hast den Alarm {C85135}deaktiviert{F1F7DC}.");
						car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"Zurück","");
					}
				}
				case 6:
				{//Auto aufbrechen
					if(VehicleInfo[veh][cLocked][0] == 0)
					{//Ist schon auf
						car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Das Fahrzeug wurde schon geöffnet","Zurück","");
						return 1;
					}
					else
					{
						if(GetPVarInt(playerid,"next_lockpick") <= 0)
						{//Kann Schloss möglicherweise knacken
							if(VehicleInfo[veh][cLocked][1] == -1)
							{//Kann nicht geknackt werden
								car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Hinweis","{C85135}Hinweis: Dieses Schloss kann nicht geknackt werden.","","");
								return 1;
							}
							new next_pick = floatround(floatrandom(200.0-PlayerSkill[playerid][SkillGlueck]*2)+400.0);
							if(floatrandom((1/float(VehicleInfo[veh][cLocked][1])+(PlayerSkill[playerid][SkillGlueck]+PlayerSkill[playerid][SkillGeschick])/200)) >= 1.0)
							{//Erfolg
								VehicleInfo[veh][cLocked][0] = 0;//Auto öffnen
								format(string,sizeof(string),"{C85135}Du hast das Schloss des Fahrzeuges erfolgreich geknackt. Du kannst\nin %d Sekunden das nächste Schloss knacken",next_pick);
								car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"","");
							}
							else
							{//Kein Erfolg
								if(VehicleInfo[veh][cAlarm][1] != 0 && VehicleInfo[veh][cAlarm][1] >= 1)
								{
									car::ActivateVehicleAlarm(veh,floatround(float(5)*float(VehicleInfo[veh][cAlarm][1])),true);
									format(string,sizeof(string),"{C85135}ACHTUNG: Du konntest das Schloss nicht knacken, der Alarm wurde ausgelöst.\nDu kannst das nächste Schloss in %d Sekunden knacken",next_pick);
									ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"","");
								}
								else
								{
									format(string,sizeof(string),"{C85135}ACHTUNG: Du konntest das Schloss nicht knacken. Du kannst das\nnächste Schloss in %d Sekunden knacken",next_pick);
									car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"","");
								}
							}
							SetPVarInt(playerid,"next_lockpick",next_pick);
						}
						else
						{
							format(string,sizeof(string),"{C85135}Hinweis: Du kannst das nächste Schloss erst in in %d Sekunden knacken",GetPVarInt(playerid,"next_lockpick"));
							car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Hinweis",string,"","");
						}
					}
				}
			}
			return 1;
		}
		case dialog_CAR_IN_MAIN:
		{
			if(!response) return 1;
			if(IsPlayerInAnyVehicle(playerid) && GetPlayerVehicleSeat(playerid) == 0)
			{//Ist im Auto
				new veh = GetPlayerVehicleID(playerid);
				switch(GetDialogOption(playerid,listitem))
				{
					case 0:
					{//Informationen
						new modell=GetVehicleModel(veh),kraftstoff[50];
						misc_FetchFuelTypes(kraftstoff,VehicleInfo[veh][cFueltype]);
						if(VehicleInfo[veh][cOwner] == 0) format(string,sizeof(string),"{F1F7DC}Besitzer: \t\t{77AE35}Keiner");
						else format(string,sizeof(string),"{F1F7DC}Besitzer: \t\t{C85135}%s",mysql_GetStringByInt("pnwl_user","username","userid",VehicleInfo[veh][cOwner]));
						format(string,sizeof(string),"%s\n{F1F7DC}Modell: \t\t%s (%d)",string,car_GetModelName(modell),modell);
						format(string,sizeof(string),"%s\n{F1F7DC}Verbrauch: \t\t%0.2f Liter/100km",string,car::GetModelVerbrauch(modell));
						format(string,sizeof(string),"%s\n{F1F7DC}Tankgröße: \t\t%0.2f Liter",string,VehicleInfo[veh][cMaxTank]);
						format(string,sizeof(string),"%s\n{F1F7DC}Tank: \t\t\t%0.2f Liter",string,VehicleInfo[veh][cFuel]);
						format(string,sizeof(string),"%s\n{F1F7DC}Batterie: \t\t%0.2f Watt",string,VehicleInfo[veh][cBatterie]);
						format(string,sizeof(string),"%s\n{F1F7DC}Kraftstoff: \t\t%s",string,kraftstoff);
						format(string,sizeof(string),"%s\n{F1F7DC}Kennzeichen: \t\t%s",string,VehicleInfo[veh][cPlate]);
						format(string,sizeof(string),"%s\n{F1F7DC}Kilometerstand: \t%0.2f km",string,VehicleInfo[veh][cGefahren]);
						format(string,sizeof(string),"%s\n{F1F7DC}Status: \t\t%s",string,VehicleInfo[veh][cLocked][0]==1?("{C85135}abgeschlossen"):("{77AE35}geöffnet"));
						car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"Zurück","");
					}
					case 1:
					{//Motor einschalten
						if(VehicleInfo[veh][cEngine] == true) 
						{
							car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du hast den Motor {C85135}ausgeschaltet{F1F7DC}.","Zurück","");
							new engine,lights,alarm,doors,bonnet,boot,objective;
							GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
							SetVehicleParamsEx(veh,0,lights,alarm,doors,bonnet,boot,objective);
							VehicleInfo[veh][cEngine] = false;
						}
						else
						{
							if(VehicleInfo[veh][cFuel] <= 0.0)
							{
								car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Der Tank des Autos ist {C85135}leer{F1F7DC}.","Zurück","");
								return 1;
							}
							if(GetPVarInt(playerid,"motor_an") == 0)
							{
								SetPVarInt(playerid,"motor_an",4);
								SetPVarInt(playerid,"motor_type",0);
								format(string,sizeof(string),"Du versuchst den Motor ~g~anzuschalten~w~.~n~.....%d",GetPVarInt(playerid,"motor_an"));
								DisplayInfobox(playerid,"Motor ~g~anschalten",string);
							}
							//car_ShowPlayerDialog(playerid,dialog_CAR_ENGINE_START,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du versuchst den Motor {77AE35}anzustellen{F1F7DC}.","4","");
						}
					}
					case 2:
					{//Licht einschalten
						new engine, lights, alarm, doors, bonnet, boot, objective;
						GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
						if(VehicleInfo[veh][cLights] == false)
						{
							if(VehicleInfo[veh][cBatterie] <= 0.0)
							{
								car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Die Batterie des Autos ist {C85135}leer{F1F7DC}.","Zurück","");
								return 1;
							}
							VehicleInfo[veh][cLights] = true;
							SetVehicleParamsEx(veh,engine,VEHICLE_PARAMS_ON,alarm,doors,bonnet,boot,objective);
							car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du hast die Lichter {77AE35}angestellt{F1F7DC}.","Zurück","");
						}
						else//Lichter sind an
						{
							VehicleInfo[veh][cLights] = false;
							SetVehicleParamsEx(veh,engine,VEHICLE_PARAMS_OFF,alarm,doors,bonnet,boot,objective);
							car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du hast die Lichter {C85135}ausgestellt{F1F7DC}.","Zurück","");
						}
					}
					case 3:
					{//Türen abschließen
						if(VehicleInfo[veh][cLocked][1] == -1)
						{
							car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Dieses Schloss kann nicht aufgeschlossen werden.","Zurück","");
							return 1;
						}
						if(++VehicleInfo[veh][cLocked][0] == 2) VehicleInfo[veh][cLocked][0] = 0;
						for(new pid; pid<=__SERVER[HIGHEST_PLAYER_ID]; pid++)
						{
							if(PlayerInfo[pid][PLAYER_ID] == VehicleInfo[veh][cOwner]) SetVehicleParamsForPlayer(veh,pid,0,0);
							else SetVehicleParamsForPlayer(veh,pid,0,VehicleInfo[veh][cLocked][0]>=1?1:0);
						}
						format(string,sizeof(string),"{F1F7DC}Du hast das Schloss (Stufe {C85135}%d{F1F7DC}) %s{F1F7DC}.",VehicleInfo[veh][cLocked][1],VehicleInfo[veh][cLocked][0]==1?("{C85135}abgeschlossen"):("{77AE35}aufgeschlossen"));
						car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"Zurück","");
					}
					case 4:
					{//Alarm aktivieren
						VehicleInfo[veh][cAlarm][0] = VehicleInfo[veh][cAlarm][0]==1?0:1;
						if(VehicleInfo[veh][cAlarm][0] == 1) format(string,sizeof(string),"{F1F7DC}Du hast den Alarm {77AE35}aktiviert{F1F7DC}.");
						else format(string,sizeof(string),"{F1F7DC}Du hast den Alarm {C85135}deaktiviert{F1F7DC}.");
						car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto",string,"Zurück","");
					}
					case 5:
					{//Wagen parken
						car::OnCarUpdate(GetPlayerVehicleID(playerid),true);
						car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du hast den Wagen {77AE35}geparkt{F1F7DC}.","Zurück","");
					}
					case 6:
					{//Anhänger abkoppeln
						new trailer = GetVehicleTrailer(veh);
						if(!trailer) return ncom_auto(playerid);
						DetachTrailerFromVehicle(veh);
						car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du hast den Anhänger {C85135}abgekoppelt{F1F7DC}.","Zurück","");
					}
					case 7:
					{//Kurzschließen
						if(GetPVarInt(playerid,"next_lockpick") <= 0)
						{//Kann Schloss möglicherweise knacken
							if(GetPVarInt(playerid,"motor_an") == 0)
							{
								SetPVarInt(playerid,"motor_an",4);
								SetPVarInt(playerid,"motor_type",1);
								format(string,sizeof(string),"Du versuchst den Motor ~r~kurzzuschliessen~w~.~n~.....%d",GetPVarInt(playerid,"motor_an"));
								DisplayInfobox(playerid,"Motor ~g~kurzschliessen",string);
								//car_ShowPlayerDialog(playerid,dialog_CAR_ENGINE_START,DIALOG_STYLE_MSGBOX,"{F1F7DC}Auto","{F1F7DC}Du versuchst das Fahrzeug {77AE35}kurzzuschließen{F1F7DC}.","4","");
							}
						}
						else
						{
							format(string,sizeof(string),"{C85135}Hinweis: Du kannst das nächste Schloss erst in in %d Sekunden knacken",GetPVarInt(playerid,"next_lockpick"));
							car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAINRE,DIALOG_STYLE_MSGBOX,"{F1F7DC}Hinweis",string,"","");
						}
					}
				}
			}
			else
			{//Ist nicht mehr im Auto
			}
			return 1;
		}
	}
	//@pnwl_cars->pnwl_autohaus
	autohaus_EX_OnDialogResponse(playerid, dialogid, response, listitem, inputtext);
	return 1;
}

stock car::StartEngine(veh)
{
	if(!IsVehicleConnected(veh)) return 0;
	if(VehicleInfo[veh][cFuel] <= 0.0)
	{
		VehicleInfo[veh][cFuel] = 0.0;
		return 2;
	}
	VehicleInfo[veh][cEngine] = true;
	new engine, lights, alarm, doors, bonnet, boot, objective;
	GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
	SetVehicleParamsEx(veh,1,lights,alarm,doors,bonnet,boot,objective);
	return 1;
}

car::OnCarUpdate(veh,bool:pos_update=true)
{
	if(veh < 0) return -1;
	if(IsVehicleConnected(veh))
	{
		if(!VehicleInfo[veh][save_vehicle_data]) 	return -2;
		if(!VehicleInfo[veh][cID])					return -2;//Nicht in der DB
		new Float:v_pos[4],engine,lights,alarm,doors,bonnet,boot,objective,Float:health;
		GetVehiclePos(veh,v_pos[0],v_pos[1],v_pos[2]);
		GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
		GetVehicleDamageStatus(veh,VehicleInfo[veh][cDamageStatus][0],VehicleInfo[veh][cDamageStatus][1],VehicleInfo[veh][cDamageStatus][2],VehicleInfo[veh][cDamageStatus][3]);
		GetVehicleZAngle(veh,v_pos[3]);
		GetVehicleHealth(veh,health);
		VehicleInfo[veh][cVVW] = GetVehicleVirtualWorld(veh);
		if(health <= 255.0)	health = 255.0;
		if(health > 1000)	health = 1000.0;
		
		if(pos_update == true)
		{
			VehicleInfo[veh][cHealth] = health;
			VehicleInfo[veh][cPos][0] = v_pos[0];
			VehicleInfo[veh][cPos][1] = v_pos[1];
			VehicleInfo[veh][cPos][2] = v_pos[2];
			VehicleInfo[veh][cPos][3] = v_pos[3];

			format(string,sizeof(string),"\
			UPDATE pnwl_cars SET \
			x='%0.2f',\
			y='%0.2f',\
			z='%0.2f',\
			rot='%0.2f',\
			vint='%d',\
			vvw='%d',\
			f1='%d',\
			f2='%d'",\
			v_pos[0],\
			v_pos[1],\
			v_pos[2],\
			v_pos[3],\
			VehicleInfo[veh][cVINT][1],\
			VehicleInfo[veh][cVVW],\
			VehicleInfo[veh][cColor][0],\
			VehicleInfo[veh][cColor][1]);
			
			format(string,sizeof(string),"%s,\
			engine_hp='%0.2f',\
			panels='%d',\
			doors='%d',\
			lights='%d',\
			tires='%d' \
			WHERE vid='%d'\
			",\
			string,\
			health,\
			VehicleInfo[veh][cDamageStatus][0],\
			VehicleInfo[veh][cDamageStatus][1],\
			VehicleInfo[veh][cDamageStatus][2],\
			VehicleInfo[veh][cDamageStatus][3],\
			VehicleInfo[veh][cID]);
			
			mysql_query(string);
			mysql_free_result();
		}
		
		format(string,sizeof(string),"UPDATE pnwl_cars SET \
		alarm='%d',\
		alarmon='%d',\
		nplate='%s',\
		fuel='%0.2f',\
		maxtank='%0.2f',",
		VehicleInfo[veh][cAlarm][1],\
		VehicleInfo[veh][cAlarm][0],\
		VehicleInfo[veh][cPlate],\
		VehicleInfo[veh][cFuel],\
		VehicleInfo[veh][cMaxTank]);	
		format(string,sizeof(string),"%s\
		fueltype='%d',\
		owner='%d',\
		locked='%d',\
		locktype='%d',\
		engine='%d',\
		lightson='%d',\
		batterie='%0.2f',\
		gefahren='%0.2f',\
		paintjob='%d' \
		WHERE vid='%d'",\
		string,\
		VehicleInfo[veh][cFueltype],\
		VehicleInfo[veh][cOwner],\
		VehicleInfo[veh][cLocked][0],\
		VehicleInfo[veh][cLocked][1],\
		VehicleInfo[veh][cEngine]==true?1:0,\
		VehicleInfo[veh][cLights]==true?1:0,\
		VehicleInfo[veh][cBatterie],\
		VehicleInfo[veh][cGefahren],\
		VehicleInfo[veh][cPaintjob],\
		VehicleInfo[veh][cID]);
		mysql_query(string);
		mysql_free_result();
		return 1;
	}
	return 0;
}

car::ShowPlayerCarMainDialog(playerid,veh,bool:in_car)
{
	new dlop = 0;
	if(in_car == true)
	{
		format(string,sizeof(string),"{F1F7DC}Informationen");
		PlayerOption[playerid][dialog][dlop] = 0;
		dlop++;			
		
		if(VehicleInfo[veh][cEngine] == true) 
		{
			format(string,sizeof(string),"%s\n{F1F7DC}Motor {C85135}ausschalten",string);
			PlayerOption[playerid][dialog][dlop] = 1;
		}
		else 
		{
			if(VehicleInfo[veh][cOwner] == 0 || VehicleInfo[veh][cOwner] == PlayerInfo[playerid][PLAYER_ID])
			{
				format(string,sizeof(string),"%s\n{F1F7DC}Motor {77AE35}einschalten",string);
				PlayerOption[playerid][dialog][dlop] = 1;
			}
			else
			{
				if(GetPVarInt(playerid,"next_lockpick") <= 0)
				{
					format(string,sizeof(string),"%s\n{F1F7DC}Motor {77AE35}kurzschließen (!!)",string);
					PlayerOption[playerid][dialog][dlop] = 7;
				}
			}
		}
		dlop++;

		if(VehicleInfo[veh][cLights] == false) format(string,sizeof(string),"%s\n{F1F7DC}Licht {77AE35}einschalten",string);
		else format(string,sizeof(string),"%s\n{F1F7DC}Licht {C85135}ausschalten",string);
		PlayerOption[playerid][dialog][dlop] = 2;
		dlop++;

		if(PlayerInfo[playerid][PLAYER_ID] == VehicleInfo[veh][cOwner]) 
		{		
			if(VehicleInfo[veh][cLocked][0] >= 1) format(string,sizeof(string),"%s\n{F1F7DC}Schloss (Stufe {C85135}%d{F1F7DC}) {77AE35}öffnen",string,VehicleInfo[veh][cLocked][1]);
			else format(string,sizeof(string),"%s\n{F1F7DC}Schloss (Stufe {C85135}%d{F1F7DC}) {C85135}abschließen",string,VehicleInfo[veh][cLocked][1]);
			PlayerOption[playerid][dialog][dlop] = 3;
			dlop++;

			if(VehicleInfo[veh][cAlarm][1] >= 1) 
			{
				if(VehicleInfo[veh][cAlarm][0] == 1) format(string,sizeof(string),"%s\n{F1F7DC}Alarm (Stufe {C85135}%d{F1F7DC}) {C85135}deaktivieren",string,VehicleInfo[veh][cAlarm][1]);
				else format(string,sizeof(string),"%s\n{F1F7DC}Alarm (Stufe %d{F1F7DC}) {77AE35}aktivieren",string,VehicleInfo[veh][cAlarm][1]);
				PlayerOption[playerid][dialog][dlop] = 4;
				dlop++;
			}
			
			format(string,sizeof(string),"%s\n{F1F7DC}Fahrzeug hier parken",string);
			PlayerOption[playerid][dialog][dlop] = 5;
			dlop++;
		}
		
		if(IsTrailerAttachedToVehicle(veh))
		{
			format(string,sizeof(string),"%s\n{F1F7DC}Anhänger abkoppeln",string);
			PlayerOption[playerid][dialog][dlop] = 6;
			dlop++;
		}
		
		format(query,sizeof(query),"{F1F7DC}Auto - %s",VehicleInfo[veh][cPlate]);
		car_ShowPlayerDialog(playerid,dialog_CAR_IN_MAIN,DIALOG_STYLE_LIST,query,string,"Ok","Beenden");	
	}
	else
	{
		format(string,sizeof(string),"{F1F7DC}Informationen");
		PlayerOption[playerid][dialog][dlop] = 0;
		dlop++;
		
		if(VehicleInfo[veh][cOwner] == 0 || PlayerInfo[playerid][PLAYER_ID] == VehicleInfo[veh][cOwner]) 
		{
			if(VehicleInfo[veh][cLocked][0] >= 1) format(string,sizeof(string),"%s\n{F1F7DC}Schloss (Stufe {C85135}%d{F1F7DC}) {77AE35}öffnen",string,VehicleInfo[veh][cLocked][1]);
			else format(string,sizeof(string),"%s\n{F1F7DC}Schloss (Stufe {C85135}%d{F1F7DC}) {C85135}abschließen",string,VehicleInfo[veh][cLocked][1]);
			PlayerOption[playerid][dialog][dlop] = 3;
			dlop++;

			if(VehicleInfo[veh][cAlarm][1] >= 1) 
			{
				if(VehicleInfo[veh][cAlarm][0] == 1) format(string,sizeof(string),"%s\n{F1F7DC}Alarm (Stufe {C85135}%d{F1F7DC}) {C85135}deaktivieren",string,VehicleInfo[veh][cAlarm][1]);
				else format(string,sizeof(string),"%s\n{F1F7DC}Alarm (Stufe %d{F1F7DC}) {77AE35}aktivieren",string,VehicleInfo[veh][cAlarm][1]);
				PlayerOption[playerid][dialog][dlop] = 4;
				dlop++;
			}
		}
		else
		{//Ist nicht der Owner, Fahrzeug ist nicht öffentlich
			if(VehicleInfo[veh][cLocked][0] >= 1) 
			{//Ist abgeschlossen
				format(string,sizeof(string),"%s\n{F1F7DC}Schloss (Stufe {C85135}%d{F1F7DC}) {C85135}knacken?",string,VehicleInfo[veh][cLocked][1]);
				PlayerOption[playerid][dialog][dlop] = 6;
				dlop++;
			}
		}		
		format(query,sizeof(query),"{F1F7DC}Auto - %s",VehicleInfo[veh][cPlate]);
		car_ShowPlayerDialog(playerid,dialog_CAR_OUT_MAIN,DIALOG_STYLE_LIST,query,string,"Ok","Beenden");	
	}
	return 1;
}

stock car::GetModelFuelType(model)
{
	/*	&1	Normal			(1<<0)
		&2	Super			(1<<1)
		&4	Super Plus		(1<<2)
		&8	Diesel			(1<<3)
		&16 Gas				(1<<4)*/
	switch(model)
	{
		case 400: /*Landstalker10*/ 	return (1<<1);
		case 401: /*Bravura6*/ 			return (1<<0);
		case 403: /*Linerunner20*/ 		return (1<<3);
		case 404: /*Perenniel7*/ 		return (1<<0);
		case 410: /*Manana6*/ 			return (1<<0);
		case 413: /*Pony10*/ 			return (1<<0);
		case 414: /*Mule10*/ 			return (1<<0);
		case 422: /*Bobcat8*/ 			return (1<<0);
		case 433: /*Barracks18*/ 		return (1<<3);
		case 434: /*Hotknife14*/ 		return (1<<2);
		case 439: /*Stallion9*/ 		return (1<<1);
		case 440: /*Rumpo9*/ 			return (1<<0);
		case 455: /*Flatbed17*/ 		return (1<<3);
		case 456: /*Yankee10*/ 			return (1<<0);
		case 466: /*Glendale8*/ 		return (1<<0);
		case 467: /*Oceanic7*/ 			return (1<<0);
		case 470: /*Patriot13*/ 		return (1<<1);
		case 475: /*Sabre8*/ 			return (1<<0);
		case 478: /*Walton8*/ 			return (1<<0);
		case 479: /*Regina8*/ 			return (1<<0);
		case 482: /*Burrito10*/ 		return (1<<0);
		case 483: /*Camper8*/ 			return (1<<0);
		case 489: /*Rancher10*/ 		return (1<<1);
		case 498: /*Boxville10*/ 		return (1<<0);
		case 499: /*Benson10*/ 			return (1<<0);
		case 500: /*Mesa9*/ 			return (1<<1);
		case 505: /*Rancher11*/ 		return (1<<1);
		case 508: /*Journey12*/ 		return (1<<0);
		case 514: /*Petrol Tanker20*/ 	return (1<<1);
		case 515: /*Roadtrain20*/ 		return (1<<1);
		case 518: /*Buccaneer9*/ 		return (1<<0);
		case 524: /*Cement Truck13*/ 	return (1<<0);
		case 525: /*Towtruck8*/ 		return (1<<0);
		case 526: /*Fortune6*/ 			return (1<<0);
		case 527: /*Cadrona6*/ 			return (1<<0);
		case 529: /*Willard7*/ 			return (1<<0);
		case 531: /*Tractor7*/ 			return (1<<0);
		case 542: /*Clover8*/ 			return (1<<0);
		case 543: /*Sadler8*/ 			return (1<<0);
		case 545: /*Hustler12*/ 		return (1<<2);
		case 547: /*Primo8*/ 			return (1<<0);
		case 549: /*Tampa9*/ 			return (1<<0);
		case 554: /*Yosemite12*/	 	return (1<<2);
		case 568: /*Bandito10*/ 		return (1<<1);
		case 573: /*Dune13*/ 			return (1<<2);
		case 576: /*Tornado8*/ 			return (1<<0);
		case 579: /*Huntley11*/ 		return (1<<1);
		case 580: /*Stafford9*/ 		return (1<<1);
		case 588: /*Hotdog8*/ 			return (1<<0);
		case 600: /*Picador8*/ 			return (1<<1);
		case 604: /*Glendale Shit7*/	return (1<<0);
		case 605: /*Sadler Shit7*/ 		return (1<<0);
		case 609: /*Boxville8*/ 		return (1<<0);
	}
	return (1<<0);
}

forward Float:car::GetModelMaxTank(model);
stock Float:car::GetModelMaxTank(model)
{
	switch(model)
	{
		case 400: /*Landstalker10*/ 	return 80.0;
		case 401: /*Bravura6*/ 			return 50.0;
		case 403: /*Linerunner20*/ 		return 250.0;
		case 404: /*Perenniel7*/ 		return 70.0;
		case 410: /*Manana6*/ 			return 50.0;
		case 413: /*Pony10*/ 			return 90.0;
		case 414: /*Mule10*/ 			return 90.0;
		case 422: /*Bobcat8*/ 			return 60.0;
		case 433: /*Barracks18*/ 		return 150.0;
		case 434: /*Hotknife14*/ 		return 60.0;
		case 439: /*Stallion9*/ 		return 60.0;
		case 440: /*Rumpo9*/ 			return 80.0;
		case 455: /*Flatbed17*/ 		return 150.0;
		case 456: /*Yankee10*/ 			return 80.0;
		case 466: /*Glendale8*/ 		return 60.0;
		case 467: /*Oceanic7*/ 			return 60.0;
		case 470: /*Patriot13*/ 		return 90.0;
		case 475: /*Sabre8*/ 			return 60.0;
		case 478: /*Walton8*/ 			return 60.0;
		case 479: /*Regina8*/ 			return 70.0;
		case 482: /*Burrito10*/ 		return 80.0;
		case 483: /*Camper8*/ 			return 80.0;
		case 489: /*Rancher10*/ 		return 80.0;
		case 498: /*Boxville10*/ 		return 80.0;
		case 499: /*Benson10*/ 			return 70.0;
		case 500: /*Mesa9*/ 			return 60.0;
		case 505: /*Rancher11*/ 		return 80.0;
		case 508: /*Journey12*/ 		return 90.0;
		case 514: /*Petrol Tanker20*/ 	return 250.0;
		case 515: /*Roadtrain20*/ 		return 250.0;
		case 518: /*Buccaneer9*/ 		return 60.0;
		case 524: /*Cement Truck13*/ 	return 150.0;
		case 525: /*Towtruck8*/ 		return 90.0;
		case 526: /*Fortune6*/ 			return 50.0;
		case 527: /*Cadrona6*/ 			return 50.0;
		case 529: /*Willard7*/ 			return 55.0;
		case 531: /*Tractor7*/ 			return 40.0;
		case 542: /*Clover8*/ 			return 60.0;
		case 543: /*Sadler8*/ 			return 60.0;
		case 545: /*Hustler12*/ 		return 75.0;
		case 547: /*Primo8*/ 			return 60.0;
		case 549: /*Tampa9*/ 			return 65.0;
		case 554: /*Yosemite12*/	 	return 90.0;
		case 568: /*Bandito10*/ 		return 40.0;
		case 573: /*Dune13*/ 			return 120.0;
		case 576: /*Tornado8*/ 			return 70.0;
		case 579: /*Huntley11*/ 		return 100.0;
		case 580: /*Stafford9*/ 		return 90.0;
		case 588: /*Hotdog8*/ 			return 80.0;
		case 600: /*Picador8*/ 			return 65.0;
		case 604: /*Glendale Shit7*/	return 60.0;
		case 605: /*Sadler Shit7*/ 		return 60.0;
		case 609: /*Boxville8*/ 		return 80.0;
	}
	return 70.0;
}

forward Float:car::GetModelVerbrauch(model);
stock Float:car::GetModelVerbrauch(model)
{
	switch(model)
	{
		case 400: return /*Landstalker*/	10.0;
		case 401: return /*Bravura*/		6.0;
		case 403: return /*Linerunner*/		20.0;
		case 404: return /*Perenniel*/		7.0;
		case 410: return /*Manana*/			6.0;
		case 413: return /*Pony*/			10.0;
		case 414: return /*Mule*/			10.0;
		case 422: return /*Bobcat*/			8.0;
		case 433: return /*Barracks*/		18.0;
		case 434: return /*Hotknife*/		14.0;
		case 439: return /*Stallion*/		9.0;
		case 440: return /*Rumpo*/			9.0;
		case 455: return /*Flatbed*/		17.0;
		case 456: return /*Yankee*/			10.0;
		case 466: return /*Glendale*/		8.0;
		case 467: return /*Oceanic*/		7.0;
		case 470: return /*Patriot*/		13.0;
		case 475: return /*Sabre*/			8.0;
		case 478: return /*Walton*/			8.0;
		case 479: return /*Regina*/			8.0;
		case 482: return /*Burrito*/		10.0;
		case 483: return /*Camper*/			8.0;
		case 489: return /*Rancher*/		10.0;
		case 498: return /*Boxville*/		10.0;
		case 499: return /*Benson*/			10.0;
		case 500: return /*Mesa*/			9.0;
		case 505: return /*Rancher*/		11.0;
		case 508: return /*Journey*/		12.0;
		case 514: return /*Petrol Tanker*/	20.0;
		case 515: return /*Roadtrain*/		20.0;
		case 518: return /*Buccaneer*/		9.0;
		case 524: return /*Cement Truck*/	13.0;
		case 525: return /*Towtruck*/		8.0;
		case 526: return /*Fortune*/		6.0;
		case 527: return /*Cadrona*/		6.0;
		case 529: return /*Willard*/		7.0;
		case 531: return /*Tractor*/		7.0;
		case 542: return /*Clover*/			8.0;
		case 543: return /*Sadler*/			8.0;
		case 545: return /*Hustler*/		12.0;
		case 547: return /*Primo*/			8.0;
		case 549: return /*Tampa*/			9.0;
		case 554: return /*Yosemite*/		12.0;
		case 568: return /*Bandito*/		10.0;
		case 573: return /*Dune*/			15.0;
		case 576: return /*Tornado*/		8.0;
		case 579: return /*Huntley*/		11.0;
		case 580: return /*Stafford*/		9.0;
		case 588: return /*Hotdog*/			8.0;
		case 600: return /*Picador*/		8.0;
		case 604: return /*Glendale Shit*/	7.0;
		case 605: return /*Sadler Shit*/	7.0;
		case 609: return /*Boxville*/		8.0;
	}
	return 8.0;
}

forward bool:car::IsVehicle(model);
stock bool:car::IsVehicle(model)
{
	if(car::IsCar(model) || car::IsBike(model)) return true;
	return false;
}

forward bool:car::IsCar(model);
stock bool:car::IsCar(model)
{
	switch(model)
	{
		case 425,
			430,
			435,
			446,447,448,
			450,
			452..465,
			468,469,
			471..473,
			476,
			481,
			487,488: return false;
		case 493,
			497,
			501,
			509..513,
			519,523,
			537,538,
			548,
			533,
			563,564: return false;
		case 569,570,
			557,
			584,
			586,
			590..595,
			606..608,
			610,611: return false;
	}
	return true;
}

forward bool:car::IsBike(model);
stock bool:car::IsBike(model)
{
	switch(model)
	{
		case 448,
			462,463,
			468,
			471,
			481,
			509,510,
			522,523,
			461,
			581,
			586: return true;
	}
	return false;
}

forward bool:car::IsTrailer(model);
stock bool:car::IsTrailer(model)
{
	switch(model)
	{
		case 435,
			450,
			569,
			580,
			584,
			590,
			591,
			607,
			608,
			610,
			611: return true;
	}
	return false;
}

forward bool:car::CanPullTrailer(model);
stock bool:car::CanPullTrailer(model)
{
	switch(model)
	{
		case 403,
			514,
			515,
			525,
			531,
			532,
			583: return true;
	}
	return false;
}

misc_FetchFuelTypes(dest[],kraftstoff,ds=sizeof(dest))
{
	format(dest,ds,"");
	new arten=0;
	if(kraftstoff&(1<<0))
	{
		format(dest,ds,"Normal");
		arten++;
	}
	if(kraftstoff&(1<<1))
	{
		if(arten == 0) 	format(dest,ds,"Super");
		else            format(dest,ds,"%s, Super",string);
		arten++;
	}
	if(kraftstoff&(1<<2))
	{
		if(arten == 0) 	format(dest,ds,"Super+");
		else            format(dest,ds,"%s, Super+",string);
		arten++;
	}
	if(kraftstoff&(1<<3))
	{
		if(arten == 0) 	format(dest,ds,"Diesel");
		else            format(dest,ds,"%s, Diesel",string);
		arten++;
	}
	if(kraftstoff&(1<<4))
	{
		if(arten == 0) 	format(dest,ds,"Gas");
		else            format(dest,ds,"%s, Gas",string);
		arten++;
	}
	if(!arten) format(dest,ds,"Unbekannt");
	return arten;
}

public car::OnVehicleSpawn(vehicleid)
{
	new model = GetVehicleModel(vehicleid);	
	SetVehiclePos(vehicleid,VehicleInfo[vehicleid][cPos][0],VehicleInfo[vehicleid][cPos][1],VehicleInfo[vehicleid][cPos][2]);
	SetVehicleZAngle(vehicleid,VehicleInfo[vehicleid][cPos][3]);
	GetVehiclePos(vehicleid,VehicleInfo[vehicleid][vtmpPos][0],VehicleInfo[vehicleid][vtmpPos][1],VehicleInfo[vehicleid][vtmpPos][2]);
	
	VehicleInfo[vehicleid][cVINT][1] 	= VehicleInfo[vehicleid][cVINT][0];
	LinkVehicleToInterior(vehicleid,VehicleInfo[vehicleid][cVINT][1]);
	SetVehicleVirtualWorld(vehicleid,VehicleInfo[vehicleid][cVVW]);
	VehicleInfo[vehicleid][cLastUse]	= gettime(_,_,_);

	if(!VehicleInfo[vehicleid][save_vehicle_data])
	{
		VehicleInfo[vehicleid][cMaxTank]	= car::GetModelMaxTank(model);
		VehicleInfo[vehicleid][cFuel] 		= VehicleInfo[vehicleid][cMaxTank];
		VehicleInfo[vehicleid][cEngine] 	= false;
		VehicleInfo[vehicleid][cLights] 	= false;
	}
	
	if(VehicleInfo[vehicleid][cFuel] <= 0.0) 		VehicleInfo[vehicleid][cEngine] 	= false;
	if(VehicleInfo[vehicleid][cBatterie] <= 0.0) 	VehicleInfo[vehicleid][cLights] 	= false;
	if(VehicleInfo[vehicleid][cFueltype] <= 0)		VehicleInfo[vehicleid][cFueltype]	= (1<<0);

	new engine, lights, alarm, doors, bonnet, boot, objective;
	GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
	SetVehicleParamsEx(vehicleid, VehicleInfo[vehicleid][cEngine]==true?1:0, VehicleInfo[vehicleid][cLights]==true?1:0, 0, doors, bonnet, boot, objective);
	
	if(VehicleInfo[vehicleid][cIsDead])
	{
		VehicleInfo[vehicleid][cEngine] = false;
		VehicleInfo[vehicleid][cLights] = false;
		VehicleInfo[vehicleid][cHealth]	= 260.0;
		SetVehicleParamsEx(vehicleid,0,0,0,0,0,0,0);
		UpdateVehicleDamageStatus(vehicleid,.panels=0,.doors=0b00000100000001000000010000000100,.lights=0b1111,.tires=0b1111);
		
		VehicleInfo[vehicleid][cAttachedObjects][0] = CreateObject(18691,0.0,0.0,-0.15,0.0,0.0,0.0,50.0);
		AttachObjectToVehicle(VehicleInfo[vehicleid][cAttachedObjects][0],vehicleid,0.0,0.0,0.0,0.0,0.0,0.0);
		VehicleInfo[vehicleid][cAttachedObjects][1] = CreateObject(18748,0.0,0.0,-0.15,0.0,0.0,0.0,50.0);
		AttachObjectToVehicle(VehicleInfo[vehicleid][cAttachedObjects][1],vehicleid,0.0,0.0,0.0,0.0,0.0,0.0);
	}
	if(VehicleInfo[vehicleid][cHealth] < 260.0)			VehicleInfo[vehicleid][cHealth] = 260.0;
	else if(VehicleInfo[vehicleid][cHealth] > 1000.0)	VehicleInfo[vehicleid][cHealth] = 1000.0;
	SetVehicleHealth(vehicleid,VehicleInfo[vehicleid][cHealth]);
	return 1;
}

public car_OnVehicleDeath(vehicleid,killerid)
{
	VehicleInfo[vehicleid][cEngine] = false;
	VehicleInfo[vehicleid][cLights] = false;
	for(new o; o<MAX_VEH_OBJECTS; o++)
	{
		if(IsValidObject(VehicleInfo[vehicleid][cAttachedObjects][o]))
		{
			DestroyObject(VehicleInfo[vehicleid][cAttachedObjects][o]);
		}
		VehicleInfo[vehicleid][cAttachedObjects][o] = -1;
	}
	if(!car::IsTrailer(GetVehicleModel(vehicleid)))
	{
		if(VehicleInfo[vehicleid][save_vehicle_data])
		{
			VehicleInfo[vehicleid][cIsDead]		= true;
			VehicleInfo[vehicleid][cHealth]		= 260.0;
			GetVehiclePos(vehicleid,VehicleInfo[vehicleid][cPos][0],VehicleInfo[vehicleid][cPos][1],VehicleInfo[vehicleid][cPos][2]);
			GetVehicleZAngle(vehicleid,VehicleInfo[vehicleid][cPos][3]);
			GetVehiclePos(vehicleid,VehicleInfo[vehicleid][vtmpPos][0],VehicleInfo[vehicleid][vtmpPos][1],VehicleInfo[vehicleid][vtmpPos][2]);
			VehicleInfo[vehicleid][cVVW]		= GetVehicleVirtualWorld(vehicleid);
			VehicleInfo[vehicleid][cFuel]		= 0.0;
			VehicleInfo[vehicleid][cBatterie]	= 0.0;
		}
	}
	SetVehicleToRespawn(vehicleid);
	return 1;
}

car::VehicleCronjob()
{
	new timestamp = gettime(_,_,_);
	for(new veh; veh<=__SERVER[MAX_VEHICLE_ID]; veh++)
	{
		if(IsVehicleConnected(veh))
		{
			if((timestamp-VehicleInfo[veh][cLastUse]) > MAX_VEHICLE_IDLE)
			{
				new bool:in_use = false;
				if(car::IsTrailer(GetVehicleModel(veh)))
				{
					for(new i; i<MAX_VEHICLES; i++)
					{
						if(!VehicleInfo[i][cCreated] || i == veh) continue;
						if(GetVehicleTrailer(i) == veh)
						{
							in_use = true;
							break;
						}
					}
				}
				else
				{
					for(new i; i<=__SERVER[HIGHEST_PLAYER_ID]; i++)
					{
						if(!IsPlayerConnected(i)) continue;
						if(GetPlayerVehicleID(i) == veh)
						{
							in_use = true;
							break;
						}
					}
				}
				if(!in_use)
				{
					car_OnVehicleSpawn(veh);
					continue;
				}
				VehicleInfo[veh][cLastUse] = timestamp;
			}
			
			if(VehicleInfo[veh][cEngine])
			{//Motor ist an
				new Float:tmppos[3];
				GetVehiclePos(veh,tmppos[0],tmppos[1],tmppos[2]);						
				new Float:dist = floatsqroot(
						floatpower((tmppos[0]-VehicleInfo[veh][vtmpPos][0]),2)+
						floatpower((tmppos[1]-VehicleInfo[veh][vtmpPos][1]),2)+
						floatpower((tmppos[2]-VehicleInfo[veh][vtmpPos][2]),2)
					);
				VehicleInfo[veh][vtmpPos][0] = tmppos[0];
				VehicleInfo[veh][vtmpPos][1] = tmppos[1];
				VehicleInfo[veh][vtmpPos][2] = tmppos[2];
					
				if(dist >= 3.0)
				{//Ist gefahren
					dist *= 1.0;//Umrechnung in Metern
					new model 			= GetVehicleModel(veh),
						Float:verbrauch = ((car::GetModelVerbrauch(model)/100000.0/*100km*/)*dist);
					VehicleInfo[veh][cGefahren] += (dist/1000.0);
					VehicleInfo[veh][cFuel] 	-= verbrauch;
					VehicleInfo[veh][cBatterie]	+= (dist/100.0);
					if(VehicleInfo[veh][cBatterie] >= 100.0) VehicleInfo[veh][cBatterie] = 100.0;
					if(VehicleInfo[veh][cFuel] <= 0.0)
					{
						VehicleInfo[veh][cFuel] = 0.0;
						new engine,lights,alarm,doors,bonnet,boot,objective;
						GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
						SetVehicleParamsEx(veh,0,lights,alarm,doors,bonnet,boot,objective);
						VehicleInfo[veh][cEngine] = false;
						for(new p; p<=__SERVER[HIGHEST_PLAYER_ID]; p++)
						{
							if(IsPlayerInAnyVehicle(p) && GetPlayerVehicleSeat(p) == 0 && GetPlayerVehicleID(p) == veh)
							{
								GameTextForPlayer(p,"~w~Der Tank ist ~r~leer",5000,1);
							}
						}
					}
				}
			}
			if(VehicleInfo[veh][cLights])
			{//Lichter sind an
				//Alle 10 Sekunden
				VehicleInfo[veh][cBatterie] -= 0.5;
				if(VehicleInfo[veh][cBatterie] <= 0.0)
				{
					VehicleInfo[veh][cLights] 	= false;
					VehicleInfo[veh][cBatterie]	= 0.0;
					new engine,lights,alarm,doors,bonnet,boot,objective;
					GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
					SetVehicleParamsEx(veh,engine,0,alarm,doors,bonnet,boot,objective);
					for(new p; p<=__SERVER[HIGHEST_PLAYER_ID]; p++)
					{
						if(IsPlayerInAnyVehicle(p) && GetPlayerVehicleSeat(p) == 0 && GetPlayerVehicleID(p) == veh)
						{
							GameTextForPlayer(p,"~w~Die Batterie ist ~r~leer",5000,1);
						}
					}
				}
			}
		}
	}
	return 1;
}

stock car::IsPlayerAtVehicleTrunk(playerid,vehicleid)
{//Kofferraum
	//if(!IsValidVehicle(vehicleid)) return 0;
	new Float:vx,
		Float:vy,
		Float:vz,
		Float:px,
		Float:py,
		Float:pz,
		Float:sx,
		Float:sy,
		Float:sz,
		Float:vr,
		Float:angle;
	GetVehiclePos(vehicleid,vx,vy,vz);
	GetVehicleModelInfo(GetVehicleModel(vehicleid),VEHICLE_MODEL_INFO_SIZE,sx,sy,sz);
	if(!IsPlayerInRangeOfPoint(playerid,vx,vx,vy+(sy/2.0),vz)) return 0;
	GetPlayerPos(playerid,px,py,pz);
	GetVehicleZAngle(vehicleid,vr);
	angle = absoluteangle(vr-GetAngleToPoint(vx,vy,px,py));
	if(155.0 < angle < 205.0) return 1;
	return 0;
}

stock car::IsPlayerAtVehicleBonnet(playerid,vehicleid)
{//Motorhaube
	//if(!IsValidVehicle(vehicleid)) return 0;
	new Float:vx,
		Float:vy,
		Float:vz,
		Float:px,
		Float:py,
		Float:pz,
		Float:sx,
		Float:sy,
		Float:sz,
		Float:vr,
		Float:angle;
	GetVehiclePos(vehicleid,vx,vy,vz);	
	GetVehicleModelInfo(GetVehicleModel(vehicleid),VEHICLE_MODEL_INFO_SIZE,sx,sy,sz);
	if(!IsPlayerInRangeOfPoint(playerid,vx,vx,vy-(sy/2.0),vz)) return 0;
	GetPlayerPos(playerid,px,py,pz);
	GetVehicleZAngle(vehicleid,vr);
	angle = absoluteangle(vr-GetAngleToPoint(vx, vy, px, py));
	if(-25.0 < angle < 25.0 || 335.0 < angle < 385.0) return 1;
	return 0;
}

#include "libraries/lib_EX_car_item_storage.inc"