SA-MP Forums Archive
Heavy CPU usage... - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Heavy CPU usage... (/showthread.php?tid=402544)



Heavy CPU usage... - The__ - 27.12.2012

I keep having heavy CPU usage when players are logged in, using DOF2 saving system, my OnPlayerUpdate isn't so heavy...timers neither...if you'd like any part of the code, let me know.


Re: Heavy CPU usage... - The__ - 27.12.2012

shameless self bump :/.


Re: Heavy CPU usage... - Gh05t_ - 27.12.2012

How about some info that may actually be of use to us.


Re: Heavy CPU usage... - Hiddos - 27.12.2012

Well you could show us your OnPlayerUpdate to start with


Re: Heavy CPU usage... - The__ - 27.12.2012

Quote:
Originally Posted by The__
Посмотреть сообщение
I keep having heavy CPU usage when players are logged in, using DOF2 saving system, my OnPlayerUpdate isn't so heavy...timers neither...if you'd like any part of the code, let me know.
What else would you want to know ?
EDIT:
Copying my onplayerupdate, hold on.
EDIT2:
pawn Код:
public OnPlayerUpdate(playerid)
{
    if(!BoomboxStream[playerid])
    {
        for(new i=0; i<MAX_PLAYERS; i++)
        {
            if(IsPlayerConnected(i))
            {
                if(Boombox[i])
                {
                    if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        PlayAudioStreamForPlayer(playerid, BoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 30, 1);
                        BoomboxPlayer[playerid] = i;
                        BoomboxStream[playerid] = 1;
                        SendClientMessage(playerid, COLOR_GREY, " You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        }
    }
    else
    {
        new i = BoomboxPlayer[playerid];
        if(!IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            BoomboxStream[playerid] = 0;
            BoomboxPlayer[playerid] = -1;
            StopAudioStreamForPlayer(playerid);
            SendClientMessage(playerid, COLOR_GREY, " You have went far away from the boombox.");
        }
    }
    new Float:hp;
    GetPlayerHealth(playerid,hp);
    if(hp <= 30) SetPlayerDrunkLevel(playerid,1000);
    if(hp == 100) SetPlayerDrunkLevel(playerid,0);
    if(IsPlayerInAnyVehicle(playerid))
    {
        GetVehicleHealth(GetPlayerVehicleID(playerid), vhp);
        if(vhp < vhealth[playerid] || vhp > vhealth[playerid])
        {
            if(vhp < vhealth[playerid]) OnVehicleLoseHealth(playerid,GetPlayerVehicleID(playerid),floatround(vhealth[playerid]-vhp));
            vhealth[playerid] = vhp;
        }
        if(vhp == vhealth[playerid]) GetVehicleVelocity(GetPlayerVehicleID(playerid),velX[playerid],velY[playerid],velZ[playerid]);
    }
    if(SeatBelt[playerid] != 1)
    {
        if(GetPVarInt(playerid,"FallFromVeh") == 1)
        {
            new Float:vX, Float:vY, Float:vZ;
            GetPlayerVelocity(playerid,vX,vY,vZ);
            if(vZ == 0)
            {
                new Float:php;
                ClearAnimations(playerid);
                GetPlayerHealth(playerid, php);
                SetPlayerHealth(playerid, php-30);
                if(php-30 <= 0 || php <= 0)
                {
                    SetPlayerDrunkLevel(playerid,0);
                    SetPVarInt(playerid,"FallFromVeh",0);
                    return 1;
                }
                SetPlayerDrunkLevel(playerid,10000);
                SetTimerEx("Refix",5000,0,"i",playerid);
                SetTimerEx("Animation",500,0,"i",playerid);
                TogglePlayerControllable(playerid,0);
                ApplyAnimation(playerid, "PARACHUTE", "FALL_skyDive_DIE",4,0,0,0,1,0);
                SetPVarInt(playerid,"FallFromVeh",0);
            }
        }
    }
    if(GetPlayerMoney(playerid) != PCash[playerid]) {
        SetPlayerMoney(playerid,PCash[playerid]);
    }
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER && Speedlimit[playerid])
    {
        new a, b, c;
        GetPlayerKeys(playerid, a, b ,c);
        if(a == 8 && GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) > Speedlimit[playerid])
        {
            new newspeed = GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) - Speedlimit[playerid];
            ModifyVehicleSpeed(GetPlayerVehicleID(playerid), -newspeed);
        }
    }
    if(PayDayAuth[playerid] == 1)
    {
        PayDayAuth[playerid] = 0;
        PayDay(playerid);
    }

    return 1;
}



Re: Heavy CPU usage... - BlackBank - 27.12.2012

Quote:
Originally Posted by The__
Посмотреть сообщение
What else would you want to know ?
EDIT:
Copying my onplayerupdate, hold on.
EDIT2:
pawn Код:
public OnPlayerUpdate(playerid)
{
    if(!BoomboxStream[playerid])
    {
        for(new i=0; i<MAX_PLAYERS; i++)
        {
            if(IsPlayerConnected(i))
            {
                if(Boombox[i])
                {
                    if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        PlayAudioStreamForPlayer(playerid, BoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 30, 1);
                        BoomboxPlayer[playerid] = i;
                        BoomboxStream[playerid] = 1;
                        SendClientMessage(playerid, COLOR_GREY, " You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        }
    }
    else
    {
        new i = BoomboxPlayer[playerid];
        if(!IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            BoomboxStream[playerid] = 0;
            BoomboxPlayer[playerid] = -1;
            StopAudioStreamForPlayer(playerid);
            SendClientMessage(playerid, COLOR_GREY, " You have went far away from the boombox.");
        }
    }
    new Float:hp;
    GetPlayerHealth(playerid,hp);
    if(hp <= 30) SetPlayerDrunkLevel(playerid,1000);
    if(hp == 100) SetPlayerDrunkLevel(playerid,0);
    if(IsPlayerInAnyVehicle(playerid))
    {
        GetVehicleHealth(GetPlayerVehicleID(playerid), vhp);
        if(vhp < vhealth[playerid] || vhp > vhealth[playerid])
        {
            if(vhp < vhealth[playerid]) OnVehicleLoseHealth(playerid,GetPlayerVehicleID(playerid),floatround(vhealth[playerid]-vhp));
            vhealth[playerid] = vhp;
        }
        if(vhp == vhealth[playerid]) GetVehicleVelocity(GetPlayerVehicleID(playerid),velX[playerid],velY[playerid],velZ[playerid]);
    }
    if(SeatBelt[playerid] != 1)
    {
        if(GetPVarInt(playerid,"FallFromVeh") == 1)
        {
            new Float:vX, Float:vY, Float:vZ;
            GetPlayerVelocity(playerid,vX,vY,vZ);
            if(vZ == 0)
            {
                new Float:php;
                ClearAnimations(playerid);
                GetPlayerHealth(playerid, php);
                SetPlayerHealth(playerid, php-30);
                if(php-30 <= 0 || php <= 0)
                {
                    SetPlayerDrunkLevel(playerid,0);
                    SetPVarInt(playerid,"FallFromVeh",0);
                    return 1;
                }
                SetPlayerDrunkLevel(playerid,10000);
                SetTimerEx("Refix",5000,0,"i",playerid);
                SetTimerEx("Animation",500,0,"i",playerid);
                TogglePlayerControllable(playerid,0);
                ApplyAnimation(playerid, "PARACHUTE", "FALL_skyDive_DIE",4,0,0,0,1,0);
                SetPVarInt(playerid,"FallFromVeh",0);
            }
        }
    }
    if(GetPlayerMoney(playerid) != PCash[playerid]) {
        SetPlayerMoney(playerid,PCash[playerid]);
    }
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER && Speedlimit[playerid])
    {
        new a, b, c;
        GetPlayerKeys(playerid, a, b ,c);
        if(a == 8 && GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) > Speedlimit[playerid])
        {
            new newspeed = GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) - Speedlimit[playerid];
            ModifyVehicleSpeed(GetPlayerVehicleID(playerid), -newspeed);
        }
    }
    if(PayDayAuth[playerid] == 1)
    {
        PayDayAuth[playerid] = 0;
        PayDay(playerid);
    }

    return 1;
}
Remove this out of your OnPlayerUpdate code±
PHP код:
        for(new i=0i<MAX_PLAYERSi++)
        {
            if(
IsPlayerConnected(i))
            {
                if(
Boombox[i])
                {
                    if(
IsPlayerInRangeOfPoint(playerid30bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        
PlayAudioStreamForPlayer(playeridBoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 301);
                        
BoomboxPlayer[playerid] = i;
                        
BoomboxStream[playerid] = 1;
                        
SendClientMessage(playeridCOLOR_GREY" You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        } 
You are looping every time when OnPlayerUpdate is called on all the players...

If MAX_PLAYERS is 500 and there is one player online, it will loop everytime when OnPlayerUpdate is callled.

If there are more players, like 7 players, it will be like this: 7 * 500.
So the more the server needs to do.


Re: Heavy CPU usage... - The__ - 27.12.2012

Quote:
Originally Posted by BlackBank3
Посмотреть сообщение
...
How about this loop ?
pawn Код:
for(new i, j = GetMaxPlayers(); i < j; i++)



Re: Heavy CPU usage... - BlackBank - 27.12.2012

Quote:
Originally Posted by The__
Посмотреть сообщение
How about this loop ?
pawn Код:
for(new i, j = GetMaxPlayers(); i < j; i++)
I looked back, and this whole code doesn't even works well.
PHP код:
    if(!BoomboxStream[playerid])
    {
        for(new 
i=0i<MAX_PLAYERSi++)
        {
            if(
IsPlayerConnected(i))
            {
                if(
Boombox[i])
                {
                    if(
IsPlayerInRangeOfPoint(playerid30bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        
PlayAudioStreamForPlayer(playeridBoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 301);
                        
BoomboxPlayer[playerid] = i;
                        
BoomboxStream[playerid] = 1;
                        
SendClientMessage(playeridCOLOR_GREY" You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        }
    }
    else
    {
        new 
BoomboxPlayer[playerid];
        if(!
IsPlayerInRangeOfPoint(playerid30bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            
BoomboxStream[playerid] = 0;
            
BoomboxPlayer[playerid] = -1;
            
StopAudioStreamForPlayer(playerid);
            
SendClientMessage(playeridCOLOR_GREY" You have went far away from the boombox.");
        }
    } 



Re: Heavy CPU usage... - The__ - 27.12.2012

So to replace my loop with foreach ?
EDIT:
changed it to this
pawn Код:
if(!BoomboxStream[playerid])
    {
        foreach(new i : Player)
        {
            if(Boombox[i])
            {
                if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
                {
                PlayAudioStreamForPlayer(playerid, BoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 30, 1);
                BoomboxPlayer[playerid] = i;
                BoomboxStream[playerid] = 1;
                SendClientMessage(playerid, COLOR_GREY, " You are listening to music coming out of a nearby boombox.");
                }
            }
        }
    }
    else
    {
        new i = BoomboxPlayer[playerid];
        if(!IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            BoomboxStream[playerid] = 0;
            BoomboxPlayer[playerid] = -1;
            StopAudioStreamForPlayer(playerid);
            SendClientMessage(playerid, COLOR_GREY, " You have went far away from the boombox.");
        }
    }
I'm going to sleep now..so dicuss and try to help my fucking CPU usage :/...good night.


Re: Heavy CPU usage... - The__ - 28.12.2012

shameless bump.
EDIT:
I tried just moving around...