View Single Post
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