//Trucks
#define Linerunner 403
#define PetrolTanker 514
#define RoadTrain 515
//Trailers
#define Article1 435 //рефрижератор1
#define Article2 450 //открытый
#define Article3 591 //рефрижератор2
#define PetrolTrailer 584 //цистерна

forward strtok(const string[], &index, const seperator[] = " " );
forward IsVehicleTrailer(vehicleid);
forward GetVehicleWithinDistance(playerid, Float:x1, Float:y1, Float:z1, Float:dist, &veh);
forward IsPlayerInTruck(playerid);
forward PlayerToPoint(Float:radi, playerid, Float:x, Float:y, Float:z);
forward split(const strsrc[], strdest[][], delimiter);
forward GetTrailerModel(playerid);
forward GetTrailerID(playerid);

strtok(const string[], &index, const seperator[] = " " )
{
	new
		index2,
		result[ 30 ];
 
	index2 =  strfind(string, seperator, false, index);
 
 
	if(index2 == -1)
	{
		if(strlen(string) > index)
		{
			strmid(result, string, index, strlen(string), 30);
			index = strlen(string);
		}
		return result; // This string is empty, probably, if index came to an end
	}
	if(index2 > (index + 29))
	{
		index2 = index + 29;
		strmid(result, string, index, index2, 30);
		index = index2;
		return result;
	}
	strmid(result, string, index, index2, 30);
	index = index2 + 1;
	return result;
}

public IsVehicleTrailer(vehicleid)
	{
	new model = GetVehicleModel(vehicleid);
	switch(model){
		case Article1:return 1;
	    case Article2:return 1;
	    case Article3:return 1;
	    case PetrolTrailer:return 1;
		default: return 0;
	}
	return 1;
}

public GetVehicleWithinDistance( playerid, Float:x1, Float:y1, Float:z1, Float:dist, &veh)
{
	for(new i = 1; i < MAX_VEHICLES; i++){
		if(GetVehicleModel(i) > 0){
			if(GetPlayerVehicleID(playerid) != i ){
	        	new Float:x, Float:y, Float:z;
	        	new Float:x2, Float:y2, Float:z2;
				GetVehiclePos(i, x, y, z);
				x2 = x1 - x; y2 = y1 - y; z2 = z1 - z;
				new Float:vDist = (x2*x2+y2*y2+z2*z2);
				if( vDist < dist){veh = i;}
			}
		}
	}
}

public IsPlayerInTruck(playerid)
{
	if(IsPlayerInAnyVehicle(playerid))
	{
		new playerVehicle = GetPlayerVehicleID(playerid);
        new model = GetVehicleModel(playerVehicle);
		switch(model)
		{
			case Linerunner:return 1;
			case PetrolTanker:return 1;
			case RoadTrain:return 1;
			default:return 0;
		}			
   	}
	return 0;
}

public PlayerToPoint(Float:radi, playerid, Float:x, Float:y, Float:z)
{
    if(IsPlayerConnected(playerid))
	{
		new Float:oldposx, Float:oldposy, Float:oldposz;
		new Float:tempposx, Float:tempposy, Float:tempposz;
		GetPlayerPos(playerid, oldposx, oldposy, oldposz);
		tempposx = (oldposx -x);
		tempposy = (oldposy -y);
		tempposz = (oldposz -z);
		//printf("DEBUG: X:%f Y:%f Z:%f",posx,posy,posz);
		if (((tempposx < radi) && (tempposx > -radi)) && ((tempposy < radi) && (tempposy > -radi)) && ((tempposz < radi) && (tempposz > -radi)))
		{
			return 1;
		}
	}
	return 0;
}

public split(const strsrc[], strdest[][], delimiter)
{
	new i, li;
	new aNum;
	new len;
	while(i <= strlen(strsrc)){
	    if(strsrc[i]==delimiter || i==strlen(strsrc)){
	        len = strmid(strdest[aNum], strsrc, li, i, 128);
	        strdest[aNum][len] = 0;
	        li = i+1;
	        aNum++;
		}
		i++;
	}
	return 1;
}

public GetTrailerModel(playerid)
{
	if(IsPlayerConnected(playerid))
	{
        if(IsPlayerInAnyVehicle(playerid))
		{
			if(IsTrailerAttachedToVehicle(GetPlayerVehicleID(playerid)))
			{
				new trailermodel = GetVehicleModel(GetVehicleTrailer(GetPlayerVehicleID(playerid)));
				return trailermodel;
			}
		}
	}
	return 0;
}

public GetTrailerID(playerid)
{
	if(IsPlayerConnected(playerid))
	{
        if(IsPlayerInAnyVehicle(playerid))
		{
			if(IsTrailerAttachedToVehicle(GetPlayerVehicleID(playerid)))
			{
				new trailerID = GetVehicleTrailer(GetPlayerVehicleID(playerid));
				return trailerID;
			}
		}
	}
	return 0;
}