How can I optimize this code? -
FacuScript - 19.08.2019
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 i = 0; i < MAX_PLAYERS; i++) //ALL PLAYERS CONNECTED
{
if(IsPlayerConnected(i) && !IsPlayerNPC(i)) //IF IT IS CONNECTED AND IT IS NOT A BOOT
{
if(IsPlayerInRangeOfPoint(i, 100.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(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);
}
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
Re: [HELP] How can I optimize this code? -
RogueDrifter - 20.08.2019
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:
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
Re: [HELP] How can I optimize this code? -
Calisthenics - 20.08.2019
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);
}
}
}