Checking if there is someone alive.
#1

I want to create a loop to check how many persons are alive, and if there is one person alive, the server will send a message to everyone "There is only one person alive". Can anyone help me with it.
Reply
#2

new bool:Alive[MAX_PLAYERS] = {true,...};

OnPlayerConnect:

Alive[playerid]=true;

OnPlayerDisconnect

Alive[playerid] = false;

OnPlayerDeath

Alive[playerid] = false;


Where u want to check

new alives = 0;
for ( new i = 0 , j = GetMaxPlayers( ) ; i < j ; ++ i ) {
if( ! IsPlayerConnected( i ) ) continue;
if( Alive[ i ] == false ) continue;
alives ++;
}

if ( alives == 1 ) {
SendClientMessageToAll( -1 , "one is alive" );
}
Reply
#3

pawn Код:
//
new players_alive;
for(new i; i < MAX_PLAYERS; i++)
{
    new Float:player_health;
    GetPlayerHealth(i, player_health);
    if(player_health > 0 || GetPlayerState(i) != PLAYER_STATE_WASTED)// Change it if needed.
    {
        players_alive++;
    }
}

if(players_alive == 1) SendClientMessageToAll(-1,  "There is only one person alive");
else if(players_alive > 1) SendClientMessageToAll(-1,  "There is players alive");
//
Reply
#4

a new way may help you:

pawn Код:
CMD:checkplayers(playerid, params[])
{
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        new Float:hp, str[185], pname[MAX_PLAYER_NAME];
        GetPlayerName(playerid, pname, MAX_PLAYER_NAME);
        if(GetPlayerHealth(playerid, hp) <= 0) return 1;
        format(str, 185, "%s is still alive with health: %d", pname, GetPlayerHealth(playerid, hp));
        SendClientMessage(playerid, 1, str);
    }
    return 1;
}
Reply
#5

Quote:
Originally Posted by ReVo_
Посмотреть сообщение
new bool:Alive[MAX_PLAYERS] = {true,...};

Where u want to check

new alives = 0;
for ( new i = 0 , j = GetMaxPlayers( ) ; i < j ; ++ i ) {
if( ! IsPlayerConnected( i ) ) continue;
if( Alive[ i ] == false ) continue;
alives ++;
}

if ( alives == 1 ) {
SendClientMessageToAll( -1 , "one is alive" );
}
I didn't quite get this part, where actually is the best part to add this code?
Reply
#6

What's the point running a loop if OnPlayerDeath is already the loop?

I wrote a fast include file for you:
pawn Код:
static alive_global[MAX_PLAYERS], alive_player[MAX_PLAYERS], alive_count;

forward LastManStanding(playerid);

stock GetAliveMax()
    return alive_count;

stock GetAlivePlayer(slot)
    return (0 <= slot < sizeof(alive_global)) ? (alive_global[slot]) : (INVALID_PLAYER_ID);

public OnPlayerSpawn(playerid)
{
    alive_global[alive_count] = playerid;
    alive_player[playerid] = alive_count;
    alive_count++;

    return CallLocalFunction("flms_OnPlayerSpawn", "i", playerid);
}

#if defined _ALS_OnPlayerSpawn
    #undef OnPlayerSpawn
#else
    #define _ALS_OnPlayerSpawn
#endif
#define OnPlayerSpawn flms_OnPlayerSpawn
forward flms_OnPlayerSpawn(playerid);

public OnPlayerDeath(playerid, killerid, reason)
{
    alive_count--;

    if(alive_player[playerid] != alive_count)
    {
        for(new i = alive_player[playerid]; i < alive_count; i++)
        {
            alive_global[i] = alive_global[i + 1];
            alive_player[alive_global[i + 1]] = i;
        }
    }

    if(alive_count == 1)
    {
        CallLocalFunction("LastManStanding", "i", alive_global[0]);
    }

    return CallLocalFunction("flms_OnPlayerDeath", "iii", playerid, killerid, reason);
}

#if defined _ALS_OnPlayerDeath
    #undef OnPlayerDeath
#else
    #define _ALS_OnPlayerDeath
#endif
#define OnPlayerDeath flms_OnPlayerDeath
forward flms_OnPlayerDeath(playerid, killerid, reason);

public OnPlayerDisconnect(playerid, reason)
{
    alive_count--;

    if(alive_player[playerid] != alive_count)
    {
        for(new i = alive_player[playerid]; i < alive_count; i++)
        {
            alive_global[i] = alive_global[i + 1];
            alive_player[alive_global[i + 1]] = i;
        }
    }

    return CallLocalFunction("flms_OnPlayerDisconnect", "ii", playerid, reason);
}

#if defined _ALS_OnPlayerDisconnect
    #undef OnPlayerDisconnect
#else
    #define _ALS_OnPlayerDisconnect
#endif
#define OnPlayerDisconnect flms_OnPlayerDisconnect
forward flms_OnPlayerDisconnect(playerid, reason);
CALLBACK: LastManStanding(playerid)
playerid = the player who's last one alive.

FUNCTION: GetAliveMax()
returns the amount of players alive.

FUNCTION: GetAlivePlayer(slot)
slot = the slot in the array. 0 <= slot < GetAliveMax()
returns the player in the given slot.

Example scripts:
pawn Код:
public LastManStanding(playerid)
{
    new string[MAX_PLAYER_NAME + 32];
    GetPlayerName(alive_global[0], string, sizeof(string));

    format(string, sizeof(string), "%s is the last man standing.", string);
    SendClientMessageToAll(0xFFFFFFFF, string);
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    new name[MAX_PLAYER_NAME + 32];
    GetPlayerName(playerid, name, sizeof(name));

    format(name, sizeof(name), "%s died.", name);
    SendClientMessageToAll(0xFFFFFFFF, name);

    SendClientMessageToAll(0xFFFFFFFF, "The following players are still alive:");

    for(new i; i < GetAliveMax(); i++)
    {
        GetPlayerName(GetAlivePlayer(i), name, sizeof(name));
        SendClientMessageToAll(0xFFFFFFFF, name);
    }

    return 1;
}
With fast debugging it should work.

In an actual server this will require that the OnPlayerSpawn callback doesn't get called after player dies. Well, it should work, but the callback will be more like LastManOnline.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)