21.08.2012, 19:52
What's the point running a loop if OnPlayerDeath is already the loop?
I wrote a fast include file for you:
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:
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.
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);
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;
}
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.

