Efficient Vehicle Looping
#16

Hey! Interesting question.

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
In my experience, this is the best way:

pawn Код:
for (new i = 0, j = GetVehiclePoolSize(); i <= j; i ++)
{
    if (!IsValidVehicle(i) || !IsVehicleStreamedIn(i, playerid))
        continue;

    // Check if a player is near a trunk, show a dialog, etc.
}
This is indeed a very good way, but can be improved to use maximally ONE function call to determine whether a vehicle is valid and streamed in for the player. Since if a vehicle is streamed in for someone, it is definitely valid, the IsValidVehicle check can be omitted as it is done internally anyways (this was not the case in SA-MP 0.1 with some natives and the PAWN programmer had to implement some extra sanity checks, though).
pawn Код:
for(new i = 0, j = GetVehiclePoolSize(); i != j; i++)
{
    if(!IsVehicleStreamedIn(i, playerid)) {
        continue;
    }

    // code
}
This is a situation similar to:
pawn Код:
if(IsPlayerInAnyVehicle(playerid))
{
    new vID = GetPlayerVehicleID(playerid);
    // code
}
which can be simplified to
pawn Код:
new vID;
if((vID = GetPlayerVehicleID(playerid)) != 0) {
    // code
}
------

Coming back to the original problem, the OP has already found a viable solution by running the code infrequently, so the algorithm won't be a problem even if it is slow.

If you desire to make the 3-second interval smaller and allow the player to quickly open-close the trunk, you could, for example, keep information about vehicles close to them somewhere and recalculate that data after some interval.
If you were to do it in PAWN, it would indeed be an array of MxN (M - number of players, N - number of vehicles) cells. I believe one cell is 4 bytes (32 bits), so the array would take up 400000 cells in standard situations (200 max players with 2000 vehicles) and up to 1562 kilobytes. This is of no importance in terms of the computer memory as the heap has times more space available, but it will also make your AMX large and slow down compilation.
So it is without doubt that a standard PAWN array will not do. Even though a char array provides some relief, it would still be a pain and not very well scalable. So you'd have to look into some dynamic method of storage.

From C++, I can think of std::bitset, which would very much be your friend in such situation. For 200 players, having a bitset for 2000 vehicles would take up 400000 bits or 48 kilobytes, which is far less.


Some of my data may be a little off, it has been a while since I last did programming in PAWN.
Reply


Messages In This Thread
Efficient Vehicle Looping - by MP2 - 05.05.2015, 08:56
Re: Efficient Vehicle Looping - by AIped - 05.05.2015, 09:08
Re: Efficient Vehicle Looping - by MicroD - 05.05.2015, 09:22
Re: Efficient Vehicle Looping - by Tamer - 05.05.2015, 09:43
Re: Efficient Vehicle Looping - by MP2 - 05.05.2015, 09:54
Re: Efficient Vehicle Looping - by Tamer - 05.05.2015, 10:00
Re: Efficient Vehicle Looping - by Misiur - 05.05.2015, 10:01
Re: Efficient Vehicle Looping - by Lordzy - 05.05.2015, 10:10
Re: Efficient Vehicle Looping - by MP2 - 05.05.2015, 10:29
Re: Efficient Vehicle Looping - by Konstantinos - 05.05.2015, 10:38
Re: Efficient Vehicle Looping - by MP2 - 05.05.2015, 10:51
Re: Efficient Vehicle Looping - by Kar - 05.05.2015, 21:28
Re: Efficient Vehicle Looping - by Emmet_ - 05.05.2015, 21:45
Re: Efficient Vehicle Looping - by Crayder - 05.05.2015, 21:59
Re: Efficient Vehicle Looping - by MP2 - 06.05.2015, 10:22
Re: Efficient Vehicle Looping - by AndreT - 06.05.2015, 11:51
Re: Efficient Vehicle Looping - by Mauzen - 06.05.2015, 13:31
Re: Efficient Vehicle Looping - by sammp - 06.05.2015, 14:57
Re: Efficient Vehicle Looping - by Tamer - 06.05.2015, 17:57
Re: Efficient Vehicle Looping - by AndreT - 06.05.2015, 19:31
Re: Efficient Vehicle Looping - by Marricio - 08.05.2015, 21:22
Re: Efficient Vehicle Looping - by Pottus - 09.05.2015, 01:39

Forum Jump:


Users browsing this thread: 1 Guest(s)