[Include] Extended functions on Vehicles / Seats
#1

Created this around 4 hours ago. There isn't really much to explain about it. Just around 8 functions that would should be useful to some people.


Function list:


pawn Код:
GetVehicleDriver(vehicleid) // Get's the driver of your current vehicle.
GetVehiclePassenger(vehicleid) // Get's the passenger seat player. (Only, the pasenger next to driver)
GetVehiclePassengers(vehicleid, &seat0 = INVALID_SEAT, &seat1 = INVALID_SEAT, &seat2 = INVALID_SEAT, &seat3 = INVALID_SEAT, &seat4 = INVALID_SEAT) // Collects the ID of everyone inside the vehicle.
GetUnusedSeat(vehicleid) // Returns the ID of a seat avaiable inside a specific vehicle
IsSeatUsed(vehicleid, seatid) // Simply checks if the vehicle seat is occupied.
GetLastPlayer(vehicleid) // Get's the ID of a player who last used this vehicle (Passenger / Driver)
GetDriverHP(vehicleid) // Self explanatory..
GetEveryoneHP(vehicle) // Get's the health of everyone inside the vehicle (In one, kinda useless.. but I needed it..)
Example:

pawn Код:
COMMAND:getdriver(playerid)
{
    if(IsPlayerInAnyVehicle(playerid))
    {
        new vehicleid = GetPlayerVehicleID(playerid);
        new iStr [ 128 ] ;
        if(GetVehicleDriver(vehicleid) != -1) format(iStr, sizeof(iStr), " Driver: %s", PlayerName(GetVehicleDriver(vehicleid)));
        else format(iStr, sizeof(iStr), " Driver: None");
        SendClientMessage(playerid, -1, iStr);
    }
    else return SendClientError(playerid, "No..");
    return 1;
}
Downloads:


Pastebin

Alt:

pawn Код:
/*****************************************************************************************************
    All by d3ll
******************************************************************************************************/


//native IsValidVehicle(vehicleid);

#undef MAX_VEHICLES
#define MAX_VEHICLES 2000

new LastPlayerToEnter[MAX_VEHICLES] = -1;

#define INVALID_SEAT 128
#define PlayerLoop(%1) for(new %1 = 0; %1 < MAX_PLAYERS; %1++) if(IsPlayerConnected(%1) && !IsPlayerNPC(%1))

stock GetVehicleDriver(vehicleid)
{
    if(IsValidVehicle(vehicleid) == 0) return -1;
    for(new p = 0; p < MAX_PLAYERS; p++)
    {
        if(IsPlayerConnected(p) != 1 || IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != vehicleid) continue;
        if(GetPlayerState(p) != PLAYER_STATE_DRIVER) continue;
        return p;
    }
    return -1;
}

stock GetVehiclePassenger(vehicleid)
{
    if(IsValidVehicle(vehicleid) == 0) return -1;
    for(new p = 0; p < MAX_PLAYERS; p++)
    {
        if(IsPlayerConnected(p) != 1 || IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != vehicleid) continue;
        if(GetPlayerVehicleSeat(p) != 1) continue;
        return p;
    }
    return -1;
}

stock GetVehiclePassengers(vehicleid, &seat0 = INVALID_SEAT, &seat1 = INVALID_SEAT, &seat2 = INVALID_SEAT, &seat3 = INVALID_SEAT, &seat4 = INVALID_SEAT)
{
    PlayerLoop(p)
    {
        if(IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != cehicleid) continue;
        if(GetPlayerVehicleSeat(p) == 128 || GetPlayerVehicleSeat(p) == -1) continue;
        if(GetPlayerVehicleSeat(p) == 0) seat0 = p;
        if(GetPlayerVehicleSeat(p) == 2) seat1 = p;
        if(GetPlayerVehicleSeat(p) == 3) seat2 = p;
        if(GetPlayerVehicleSeat(p) == 4) seat3 = p;
        if(GetPlayerVehicleSeat(p) >= 5) seat4 = p;
    }
}

stock GetUnusedSeat(vehicleid)
{
    new UsedSeats[6] = -1;
    PlauyerLoop(p)
    {
        if(IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != vehicleid) continue;
        if(GetPlayerVehicleSeat(p) == 128 || GetPlayerVehicleSeat(p) == -1) continue;
        for(new c = 0; c < sizeof(UsedSeats); c++)
        {
            if(GetPlayerVehicleSeat(p) == c) UsedSeats[c] = 1;
            if(UsedSeats[c] != 1) return c;
        }
    }
    return -1;
}

stock IsSeatUsed(vehicleid, seatid)
{
    PlayerLoop(p)
    {
        if(IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleSeat(p) == seatid && GetPlayerVehicleID(p) == vehicleid) return 1;
    }
    return 0;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(oldstate == PLAYER_STATE_ONFOOT && newstate == PLAYER_STATE_PASSENGER || newstate == PLAYER_STATE_DRIVER)
    {
        new vehicleid = GetPlayerVehicleID(playerid);
        LastPlayerToEnter[vehicleid] = playerid;
    }
    #if defined DLL_OnPlayerStateChange
        DLL_OnPlayerStateChange(playerid, newstate, oldstate);
    #endif
   
    return 1;
}

stock GetLastPlayer(vehicleid) return LastPlayerToEnter[vehicleid];

stock Float:GetDriverHP(vehicleid)
{
    if(IsValidVehicle(vehicleid) == 0) return -1;
    for(new p = 0; p < MAX_PLAYERS; p++)
    {
        if(IsPlayerConnected(p) != 1 || IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != vehicleid) continue;
        if(GetPlayerState(p) != PLAYER_STATE_DRIVER) continue;
        new Float:hP;
        GetPlayerHealth(p, hP);
        return hP;
    }
    return -1;
}

stock Float:GetEveryoneHP(vehicle)
{
    if(IsValidVehicle(vehicleid) == 0) return -1;
    new Float:hP = -1.0;
    for(new p = 0; p < MAX_PLAYERS; p++)
    {
        if(IsPlayerConnected(p) != 1 || IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != vehicleid) continue;
        if(GetPlayerState(p) != PLAYER_STATE_DRIVER) continue;
        new Float:pHP;
        GetPlayerHealth(p, pHP);
        hP = (hP + pHP);
    }
    return hP;
}

#if defined _ALS_OnPlayerStateChange
    #undef OnPlayerStateChange
#else
    #define _ALS_OnPlayerStateChange
#endif

#define OnPlayerStateChange DLL_OnPlayerStateChange

#if defined DLL_OnPlayerStateChange
    forward DLL_OnPlayerStateChange(playerid, newstate, oldstate);
#endif
I will update this, in near future. I did this simply for revision :/ Don't like it don't comment.
Reply
#2

Should've used foreach instead of regular loops and that particular define.

Cool sauce regardless.
Reply
#3

Some vehicles has more that 7 seats. at-400, bus, coach
Reply
#4

pawn Код:
stock Float:GetEveryoneHP(vehicle)
{
    if(IsValidVehicle(vehicleid) == 0) return -1;
    new Float:hP = 0.0; //This should be "0.0" instead of "-1.0"
    for(new p = 0; p < MAX_PLAYERS; p++)
    {
        if(IsPlayerConnected(p) != 1 || IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != vehicleid) continue;
        if(GetPlayerState(p) != PLAYER_STATE_DRIVER) continue;
        new Float:pHP;
        GetPlayerHealth(p, pHP);
        hP = (hP + pHP); //If hP is equal to "-1.0" it will deduct "1.0" from the first instance.
    }
    return hP;
}
EDIT:

pawn Код:
stock GetVehiclePassenger(vehicleid)
{
    if(IsValidVehicle(vehicleid) == 0) return -1;
    for(new p = 0; p < MAX_PLAYERS; p++)
    {
        if(IsPlayerConnected(p) != 1 || IsPlayerInAnyVehicle(p) != 1) continue;
        if(GetPlayerVehicleID(p) != vehicleid) continue;
        if(GetPlayerVehicleSeat(p) != 1) continue;
        return p;
    }
    return -1;
}
I think it will just return the id of the very first passenger in a car/bus. What if all the seats are occupied by passengers ?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)