SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 19/08/2019, 11:32 PM   #1
FacuScript
Little Clucker
 
Join Date: Feb 2019
Posts: 6
Reputation: 0
Question How can I optimize this code?

Let nothing forgive my level of English.

Hi, I have a problem, the problem is that my CPU collapses completely, the CPU where I host my server, after I implement a code ..

I summarize briefly, add 17 NCP, create a couple of variables to activate an event, when the event is activated those players who are close to the position X, Y, Z get them the positions, then the 17 NCPs point and shoot with the updated position of the player, every 1 second ..

When I go alone to the place, the server is washed but it does not collapse, but when 2 or 3 players go in the same place, it is like the code, takes the positions of those 2 or 3 players and applies it to the 17 NCP , if we take accounts would be 17 x 3 = 51 queries per second ..


My server has 1GB RAM, I know very little, but I am also sure that the code can be optimized so that this does not happen.

I show you briefly

PHP Code:
if(eventobarco == 0// TIME FOR START OF EVENT, LOCATED WITHIN CALLBACK
    
{
    
Update3DTextLabelText(barcoevento,COLOR_WHITE,"{00CC00}Estado: ACTIVO\n{FFFFFF}Faltan 10 MIN para finalizar");
    
barcoeventoss 1;
    
tiemporestante 10;
    
SetTimer("tiempodeevento",60000,false); //TIMER COMMAND FOR THE UPDATE OF TIME
    
SetTimer("Shoot",1000,false); //HERE IS THE IMPORTANT, CALL THE CALLBACK SHOOT IN 1 SECOND

    
}
    return 
1
In The Calback Shoot

PHP Code:
public Shoot(playerid)
{
new 
Float:X,Float:Y,Float:Z;
for(new 
0MAX_PLAYERSi++) //ALL PLAYERS CONNECTED
{
if(
IsPlayerConnected(i) && !IsPlayerNPC(i)) //IF IT IS CONNECTED AND IT IS NOT A BOOT
{
if(
IsPlayerInRangeOfPoint(i100.0, -1456.6010,1500.7106,6.9688)) // AND IT'S ON THIS RADIO
{
if(
barcoeventoss == 1//AND THE EVENT IS ACTIVATED
{
GetPlayerPos(i,X,Y,Z);  // OBTAINS THE POSITIONS OF ALL WHO ARE WITHIN THE RADIO
//APPLY THIS AT 17 NCP
FCNPC_AimAtPlayer(barco1playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco2playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco3playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco4playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco5playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco6playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco7playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco8playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco9playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco10,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco11,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco12,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco13,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco14,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco15,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco16,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco17,playeridtrue, -1true0.00.00.5);
}
SetTimer("Shoot",1500,false); // SEND A TIMER OF 1 SECOND AND THE FUNCTION IS REPEATED 
My question is the following, how can I optimize that code? For example, that instead of obtaining the positions of all the players within the radius, it takes only one at random, of all those inside, and that the CPU usage is reduced, how can I optimize this so that the VPS don't collapse me

Include that I am using

PHP Code:
#include <FCNPC> 

could you help me? thank you
FacuScript is offline   Reply With Quote
Old 20/08/2019, 12:44 AM   #2
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,193
Reputation: 420
Default Re: [HELP] How can I optimize this code?

pawn Code:
FCNPC_AimAtPlayer(barco1, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco2, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco3, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco4, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco5, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco6, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco7, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco8, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco9, playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco10,playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco11,playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco12,playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco13,playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco14,playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco15,playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco16,playerid, true, -1, true, 0.0, 0.0, 0.5);
FCNPC_AimAtPlayer(barco17,playerid, true, -1, true, 0.0, 0.0, 0.5);

This can be shortened down by creating one variable with an array,

for example, instead of doing barco1,2,3,3 etc
you can do this:

pawn Code:
new barco[17];

Then do
pawn Code:
for(new i; i < 17; i++)
{
    FCNPC_AimAtPlayer(barco[i], playerid, true, -1, true, 0.0, 0.0, 0.5);
}

it's exactly the same thing basically. Then use later on your script barco[0] and barco[1] etc instead of creating 17 variables
__________________
Quote:
Originally Posted by Andy
My anti cheat is still relevant *puts shades on* https://github.com/RogueDrifter/Anti_cheat_pack

[Github]:Link [Gists]:Link [Forum]:Link [Server]:Link [Discord]:Link

RogueDrifter is offline   Reply With Quote
Old 20/08/2019, 08:30 AM   #3
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,226
Reputation: 205
Default Re: [HELP] How can I optimize this code?

Create a dynamic area (you need streamer plugin):
pawn Code:
// global variable
new Area_Barco;

// OnGameModeInit
Area_Barco = CreateDynamicSphere(-1456.6010, 1500.7106, 6.9688, 100.0);

// OnGameModeExit
DestroyDynamicArea(Area_Barco);

Create an iterator (you need YSI 5) to store who player is in area:
pawn Code:
// global
new Iterator: Barco_Players<MAX_PLAYERS>;

public OnPlayerEnterDynamicArea(playerid, areaid)
{
    // entered barco area + the event has started + player is spawned (spectators can call OnPlayerEnterDynamicArea) + not a bot
    if (areaid == Area_Barco && barcoeventoss == 1 && IsPlayerSpawned(playerid) && !IsPlayerNPC(playerid))
    {
        Iter_Add(Barco_Players, playerid);
    }
    return 1;
}

public OnPlayerEnterDynamicArea(playerid, areaid)
{
    if (areaid == Area_Barco && barcoeventoss == 1 && IsPlayerSpawned(playerid) && !IsPlayerNPC(playerid))
    {
        Iter_Remove(Barco_Players, playerid);
    }
    return 1;
}

IsPlayerSpawned(playerid)
{
    new player_state = GetPlayerState(playerid);
   
    return player_state != PLAYER_STATE_NONE && player_state != PLAYER_STATE_WASTED && player_state != PLAYER_STATE_SPECTATING;
}

When you start the event, store the timerid so you can kill it later.
pawn Code:
// global
new Timer_Barco = -1;

// when event is being started
if(eventobarco == 0) // TIME FOR START OF EVENT, LOCATED WITHIN CALLBACK
{
    Update3DTextLabelText(barcoevento,COLOR_WHITE,"{00CC00}Estado: ACTIVO\n{FFFFFF}Faltan 10 MIN para finalizar");
    barcoeventoss = 1;
    tiemporestante = 10;
    SetTimer("tiempodeevento",60000,false); //TIMER COMMAND FOR THE UPDATE OF TIME
    Timer_Barco = SetTimer("Shoot",1000,true); //HERE IS THE IMPORTANT, CALL THE CALLBACK SHOOT IN 1 SECOND
}

// when event is finished
... // reset variables
KillTimer(Timer_Barco);
Timer_Barco = -1;
In this callback below, we will choose a random player for NPCs to aim at or you can set a random player for each NPC to make it more realistic.

All NPCs aiming at one player:
pawn Code:
forward Shoot();
public Shoot()
{
    new victim = Iter_Random(Barco_Players);

    if (victim != INVALID_ITERATOR_SLOT) // iterator is not empty. another way is to check if Iter_Count(Barco_Players) is > 0
    {
        for (new i; i < sizeof (barco); i++)
        {
            FCNPC_AimAtPlayer(barco[i], victim, true, -1, true, 0.0, 0.0, 0.5);
        }
    }
}
Each NPC aiming at random player:
pawn Code:
forward Shoot();
public Shoot()
{
    if (Iter_Count(Barco_Players) > 0)
    {
        for (new i; i < sizeof (barco); i++)
        {
            FCNPC_AimAtPlayer(barco[i], Iter_Random(Barco_Players), true, -1, true, 0.0, 0.0, 0.5);
        }
    }
}
Calisthenics is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optimize code DeadNudock Scripting Help 4 25/06/2015 11:57 PM
optimize code JoshNudock Scripting Help 4 22/06/2015 07:38 PM
Optimize code Face9000 Scripting Help 4 05/01/2014 10:43 PM
OPTIMIZE CODE!!!!!!!! Nadim Scripting Help 2 01/11/2013 03:43 PM


All times are GMT. The time now is 08:55 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.