What about this commands?
#1

Hi'.
I created this thread to talk about commands optimization.
I mean, post your command and let's other users to give them her opinion.
So, there is mine:
PHP Code:
new TimeRespawn;
CMD:respawncall(playerid)
{
    if(
pAccount[playerid][pAdmin] < ADMIN) return ErrorMsg(playerid_false);
    
TimeRespawn gettime()+20;
    
SetTimer("AllVehicleRespawn"5000false);
    
AdminMessage(playerid"Vous avez dйmarrй le respawn de tous les vйhicules.");
    
SendMessageToAdmins(RED"%s a dйmarrй le respawn de tous les vйhicules."GetName(playerid));
    
ServeurAnnonce(_"20 secondes avant le respawn de tous les vйhicules.");
    return 
1;
}
PUBLIC:
AllVehicleRespawn()
{
    if(
TimeRespawn gettime() > 13)
    {
        
ServeurAnnonce(_"15 secondes avant le respawn de tous les vйhicules.");
        
// 15 seconds before every vehicle's respawn.
        
SetTimer("AllVehicleRespawn"5000false);
        return 
1;
    }
    else if(
TimeRespawn gettime() > 9)
    {
        
ServeurAnnonce(_"10 secondes avant le respawn de tous les vйhicules.");
        
SetTimer("AllVehicleRespawn"5000false);
        return 
1;
    }
    else if(
TimeRespawn gettime() > 4)
    {
        
ServeurAnnonce(_"5 secondes avant le respawn de tous les vйhicules.");
        
SetTimer("AllVehicleRespawn"5000false);
        return 
1;
    }
    
ServeurAnnonce(_"Tous les vйhicules ont йtй respawn par un administrateur.");
    for(new 
iGetVehiclePoolSize()+1i++)
    {
        
SetVehicleToRespawn(i);
    }
    return 
1;

Any suggestions? Optimization? This code seem too much repetitive. :/
Reply
#2

First optimization is your loop, it's calling GetVehiclePoolSize at every iteration.
And a little (mistake or it's mean't to be like that?), it will respawn also used vehicles which will be annoying to players.
PHP Code:
for(new ij=GetVehiclePoolSize(); <= ji++) 
    { 
        if(
GetVehicleDriver(i) == INVALID_PLAYER_IDSetVehicleToRespawn(i); 
    } 
Reply
#3

Quote:
Originally Posted by Shinja
View Post
First optimization is your loop, it's calling GetVehiclePoolSize at every iteration.
And a little (mistake or it's mean't to be like that?), it will respawn also used vehicles which will be annoying to players.
PHP Code:
for(new ij=GetVehiclePoolSize(); <= ji++) 
    { 
        if(
GetVehicleDriver(i) == INVALID_PLAYER_IDSetVehicleToRespawn(i); 
    } 
Ye'p, I forget those 2 things.
This is better?
PHP Code:
PUBLIC:AllVehicleRespawn()
{
    if(
TimeRespawn gettime() > 13)
    {
        
ServeurAnnonce(_"15 secondes avant le respawn de tous les vйhicules.");
        
SetTimer("AllVehicleRespawn"5000false);
        return 
1;
    }
    else if(
TimeRespawn gettime() > 9)
    {
        
ServeurAnnonce(_"10 secondes avant le respawn de tous les vйhicules.");
        
SetTimer("AllVehicleRespawn"5000false);
        return 
1;
    }
    else if(
TimeRespawn gettime() > 4)
    {
        
ServeurAnnonce(_"5 secondes avant le respawn de tous les vйhicules.");
        
SetTimer("AllVehicleRespawn"5000false);
        return 
1;
    }
    
ServeurAnnonce(_"Tous les vйhicules ont йtй respawn par un administrateur.");
    for(new 
iGetVehiclePoolSize(); <= ji++)  
    {  
        if(
GetVehicleDriver(i) == INVALID_PLAYER_IDSetVehicleToRespawn(i);  
    }  
    return 
1;
}
GetVehicleDriver(vehicleid)
{
    foreach(new 
Player)
    {
        if(
GetPlayerVehicleID(i) == vehicleid) return i;
    }
    return 
INVALID_PLAYER_ID;

Reply
#4

Assuming ServeurAnnonce accepts arguments to format, I'd have the timer as:
pawn Code:
// command: /respawncall
SetTimerEx("AllVehicleRespawn", 5000, false, "i", 20);

// callback: AllVehicleRespawn(interval)
if ((interval -= 5))
{
    ServeurAnnonce(_, "%i secondes avant le respawn de tous les vйhicules.", interval);
    SetTimerEx("AllVehicleRespawn", 5000, false, "i", interval);
}
else
{
    // inform players and respawn vehicles..
}
And there are betters ways to check if the vehicle is not occupied because you keep calling player-loops. A method I liked is a vehicle-array and you loop through all players. Set the index (what GetPlayerVehicleID returns) to 1 and then respawn those which were set to 1.
Reply
#5

Quote:
Originally Posted by Konstantinos
View Post
And there are betters ways to check if the vehicle is not occupied because you keep calling player-loops. A method I liked is a vehicle-array and you loop through all players. Set the index (what GetPlayerVehicleID returns) to 1 and then respawn those which were set to 1.
This one will respawn every player's car no ? I don't get your idea but it's interesting.
Reply
#6

Sorry, my mistake "..those which were not set to 1".

I actually wrote a function yesterday that is supposed to respawn the vehicles which are not occupied (trailers that are attached to occupied vehicles won't respawn either). Just make sure that you are up-to-date to YSI for the "Vehicle" iterator:

pawn Code:
RespawnVehicles()
{
    new bool: occupied_vehicle[MAX_VEHICLES char], vehicleid;

    foreach(new i : Player)
    {
        if ((vehicleid = GetPlayerVehicleID(i)))
        {
            occupied_vehicle{vehicleid} = true;
            occupied_vehicle{GetVehicleTrailer(vehicleid)} = true;
        }
    }
    foreach(new v : Vehicle)
    {
        if (!occupied_vehicle{v}) SetVehicleToRespawn(v);
    }
}
Reply
#7

Quote:
Originally Posted by Konstantinos
View Post
Sorry, my mistake "..those which were not set to 1".

I actually wrote a function yesterday that is supposed to respawn the vehicles which are not occupied (trailers that are attached to occupied vehicles won't respawn either). Just make sure that you are up-to-date to YSI for the "Vehicle" iterator:

pawn Code:
RespawnVehicles()
{
    new bool: occupied_vehicle[MAX_VEHICLES char], vehicleid;

    foreach(new i : Player)
    {
        if ((vehicleid = GetPlayerVehicleID(i)))
        {
            occupied_vehicle{vehicleid} = true;
            occupied_vehicle{GetVehicleTrailer(vehicleid)} = true;
        }
    }
    foreach(new v : Vehicle)
    {
        if (!occupied_vehicle{v}) SetVehicleToRespawn(v);
    }
}
Alright.. I just tried to up-to-date my YSI and.. everything is broke. I've warning on every va_format and I still don't have Vehicle's Iterator.
If you can share me your include, it's would be good. u.u
Reply
#8

Weren't you using YSI 4 before? I'm not sure if changes have been done in y_va include since then but if you post few parts of the lines that give the errors/warnings, I can look into.

I use an edited version of foreach as standalone but this looks okay.
Reply
#9

Quote:
Originally Posted by Konstantinos
View Post
Weren't you using YSI 4 before? I'm not sure if changes have been done in y_va include since then but if you post few parts of the lines that give the errors/warnings, I can look into.

I use an edited version of foreach as standalone but this looks okay.
I don't know. I got an error message because I was missing : "Vehicle" iterator.
Expect the two last warnings, I had not those errors.
Reply
#10

Looking at the first warning, it shows the path pawno\include\y_scriptinit.inc which indicates that there are standalone includes of YSI library and probably conflict.

Delete any library related to YSI completely and re-download: http://forum.sa-mp.com/showpost.php?...99&postcount=2
Reply
#11

Alright, almost done :

I never get those warnings.
Reply
#12

Why don't you move everything from YSI-Includes-YSI.tl folder to pawno\include folder so it will be easier to just:
pawn Код:
#include "YSI\y_colours"
Anyway, those colors are defined already (in your script or in an include). CTRL + H and rename them to something else (replace all).
Reply
#13

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Why don't you move everything from YSI-Includes-YSI.tl folder to pawno\include folder so it will be easier to just:
pawn Код:
#include "YSI\y_colours"
Anyway, those colors are defined already (in your script or in an include). CTRL + H and rename them to something else (replace all).
Ye'p.
There is no define in y_colours include. '-'
PHP код:
#include "..\YSI_Internal\y_compilerpass"
#if AUTO_INCLUDE_GUARD
    #undef _inc_y_colours
#endif
#include "..\YSI_Server\y_colours" 
So I checked y_compilerpass, this is the same thing. No colors.
Reply
#14

My first though was that as well but are macros to create colors with RRGGBB(AA) and some others.

Something I realized afterwards is that you include foreach as standalone. Since you have YSI include y_iterate instead.

I don't know where those colors might be defined but replace for example "RED" with "C_RED" in that script and so on.
Reply
#15

[COLOR NAME] is defined in y_colours (YSI 4.0)
I don't get why you define your own colors, when you already have that included.

Just use X11_COLOR for integers, and COLOR for embedded.
Reply
#16

Quote:
Originally Posted by Stinged
Посмотреть сообщение
[COLOR NAME] is defined in y_colours (YSI 4.0)
I don't get why you define your own colors, when you already have that included.

Just use X11_COLOR for integers, and COLOR for embedded.
I didn't get this problem before.
I'll modify my colors. y.y
Reply
#17

Alright, foreach doesn't work anymore. Woa.
The code doesn't perform. He is like ignored.
EDIT:
Alriiiight! I changed the foreach include and the function!
Final version:
PHP код:
CMD:respawncall(playeridparams[])
{
    if(
pAccount[playerid][pAdmin] < ADMIN) return ErrorMsg(playerid_false);
    new 
time;
    if(
sscanf(params"I(20)"time)) return SCM(playeridLBLUE"/respawncall [temps avant respawn]");
    if(
RespawnAllVehicle != -1)
    {
        
ServeurAnnonce(_"Le respawn des vйhicules a йtй annulй par un administrateur.");
        
SendMessageToAdmins(ARED"%s a annulй le respawn des vйhicules"GetName(playerid));
        
KillTimer(RespawnAllVehicle);
        
RespawnAllVehicle = -1;
        return 
1;
    }
    
RespawnAllVehicle SetTimerEx("AllVehicleRespawn"5000false"i"time);
    
AdminMessage(playerid"Vous avez dйmarrй le respawn de tous les vйhicules.");
    
SendMessageToAdmins(ARED"%s a dйmarrй le respawn de tous les vйhicules."GetName(playerid));
    
ServeurAnnonce(_"%i secondes avant le respawn de tous les vйhicules."time);
    return 
1;
}
PUBLIC:
AllVehicleRespawn(interval)
{
    if((
interval -= 5) > 0)
    {
        
ServeurAnnonce(_"%i secondes avant le respawn de tous les vйhicules."interval);
        
RespawnAllVehicle SetTimerEx("AllVehicleRespawn"5000false"i"interval);
        return 
1;
    }
    
KillTimer(RespawnAllVehicle);
    
RespawnAllVehicle = -1;
    
ServeurAnnonce(_"Tous les vйhicules ont йtй respawn par un administrateur.");
    
RespawnVehicles();
    return 
1;
}
RespawnVehicles()
{
    new 
booloccupied_vehicle[MAX_VEHICLES char], vehicleid;
    foreach(new 
Player)
    {
        if ((
vehicleid GetPlayerVehicleID(i)) != 0)
        {
            
occupied_vehicle{vehicleid} = true;
            
occupied_vehicle{GetVehicleTrailer(vehicleid)} = true;
        }
    }
    foreach(new 
Vehicle)
    {
        if (!
occupied_vehicle{v}) SetVehicleToRespawn(v);
    }

Thanks a lot Konstantinos for your help and precious code!
I was wondering if I can use char for every array which can be divided by 4 ? Why nobody use this instead of normal array? This is optimization but it's interesting!
Reply
#18

Instead of creating a new thread, I'm asking here.
I created a function (no purpose) which is converting real number (in base 10) to a real number in binary. (ex: 78.347 (10) = 1001110.01011 (2))
My question is : how can I optimize it:
PHP код:
DecimalBase10toDecimalBase2(Float:number)
{
    new
        
Float:decimal - (floatround(numberfloatround_ceil) - number),
        
entier floatround(number decimal),
        
binary[20 EOS],
        
rest;
    for(new 
i!= 8i++)
    {
        
decimal *= 2.00;
        if(
decimal 1.00)
        {
            
decimal -= 1.00;
            
format(binarysizeof(binary), "%s1"binary);
        }
        else
            
format(binarysizeof(binary), "%s0"binary);
    }
    
format(binarysizeof(binary), ".%s"binary);
    do
    {
        
rest entier 2;
        
entier /= 2;
        if(
rest 0)
            
format(binarysizeof(binary), "1%s"binary);
        else
            
format(binarysizeof(binary), "0%s"binary);
    }
    while(
entier 0);
    return 
binary;

Reply
#19

Your "RespawnVehicles" function is alright, but it isn't something I would go with.

You can get the math plugin, and you will only need one loop that starts from 1 to the vehicle pool size (look for a function you would like to use). Or you could create your own function that tells you if a vehicle is occupied or not with an array since you know when a player enters and leaves a vehicle.

https://sampforum.blast.hk/showthread.php?tid=270508
Reply
#20

This cmd is enough optimized for me.
I asked for my previous code (in this post) if I can optimize it and how.
Quote:
Originally Posted by Dayrion
Посмотреть сообщение
Instead of creating a new thread, I'm asking here.
I created a function (no purpose) which is converting real number (in base 10) to a real number in binary. (ex: 78.347 (10) = 1001110.01011 (2))
My question is : how can I optimize it:
PHP код:
DecimalBase10toDecimalBase2(Float:number)
{
    new
        
Float:decimal - (floatround(numberfloatround_ceil) - number),
        
entier floatround(number decimal),
        
binary[20 EOS],
        
rest;
    for(new 
i!= 8i++)
    {
        
decimal *= 2.00;
        if(
decimal 1.00)
        {
            
decimal -= 1.00;
            
format(binarysizeof(binary), "%s1"binary);
        }
        else
            
format(binarysizeof(binary), "%s0"binary);
    }
    
format(binarysizeof(binary), ".%s"binary);
    do
    {
        
rest entier 2;
        
entier /= 2;
        if(
rest 0)
            
format(binarysizeof(binary), "1%s"binary);
        else
            
format(binarysizeof(binary), "0%s"binary);
    }
    while(
entier 0);
    return 
binary;

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)