18.08.2012, 22:32
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.
//
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");
//
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;
}
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" ); } |
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);
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;
}