Quote:
Originally Posted by addons_zz
Please, have some mercy with the ordinary mortals. Add some nice info:
|
Ok.
PHP Code:
/**
* Executes action for each player appropriate for condition
*
* @param playerVar Variable name which stores player index
* @param condition Condition for player choosing
* @param action
*/
#define ForEachPlayer(%1,%2,%3) do {\
for (new %1 = 1; %1 <= MAX_PLAYERS; %1++) {\
if (%2) {\
%3;\
}\
}\
} while (is_linux_server() == 0xDEADBEEF)
Example:
PHP Code:
new name[MAX_NAME_LENGTH];
get_user_name(player, name, charsmax(name));
ForEachPlayer(x, x != player, client_print(x, print_chat, "You are not %s", name));
PHP Code:
/**
* Stores a filtered list of client indexes to an array.
*
* @param players Array to store indexes to
* @param num Variable to store number of indexes to
* @param playerVar Variable name which stores player index (for condition)
* @param condition Condition which filters players
*/
#define GetPlayers(%0,%1,%2,%3) do {\
%1 = 0;\
for (new %2 = 1; %2 <= MAX_PLAYERS; %2++) {\
if (%3) {\
%0[%1++] = %2;\
}\
}\
} while (is_linux_server() == 0xDEADBEEF)
Example:
PHP Code:
new zombies[MAX_PLAYERS], zombieCount;
GetPlayers(zombies, zombieCount, x, is_user_alive(x) && zp_get_user_zombie(x));
client_print(0, print_chat, "Zombie count is %d", zombieCount);
PHP Code:
/**
* Stores a filtered list of random client indexes to an array.
* Executes GetPlayers then limits and randomize that list
*
* @param players Array to store indexes to
* @param num Variable to store number of indexes to (and this variable sets limit of player count)
* @param playerVar Variable name which stores player index (for condition)
* @param condition Condition which filters players
*/
#define GetRandomPlayers(%0,%1,%2,%3) do {\
static _plrs[MAX_PLAYERS];\
new _plrCount;\
GetPlayers(_plrs, _plrCount, %2, %3);\
\
%1 = min(%1, _plrCount);\
for (new _i = 0; _i < %1; _i++) {\
new _rnd = random_num(0, _plrCount - 1);\
\
%0[_i] = _plrs[_rnd];\
_plrs[_rnd] = _plrs[_plrCount - 1];\
\
_plrCount--;\
}\
} while (is_linux_server() == 0xDEADBEEF)
Example:
PHP Code:
new humans[3], humanCount = sizeof(humans);
GetRandomPlayers(humans, humanCount, x, is_user_alive(x) && !zp_get_user_zombie(x));
for (new i = 0; i < humanCount; i++) {
client_print(humans[i], print_chat, "You're a randomly chosen player");
}
PHP Code:
/**
* Gets a random player appropriate for this condition
*
* @param player Variable to store chosen player index (will be 0 if can't found appropriate player)
* @param playerVar Variable name which stores player index (for condition)
* @param condition Condition for player choosing
*/
#define GetRandomPlayer(%0,%1,%2) do {\
static _players[MAX_PLAYERS];\
new _playerCount;\
GetPlayers(_players, _playerCount, %1, %2);\
%0 = _playerCount ? _players[random_num(0, _playerCount - 1)] : 0;\
} while (is_linux_server() == 0xDEADBEEF)
Example:
PHP Code:
new player;
GetRandomPlayer(player, x, is_user_alive(x) && cs_get_user_team(x) == CS_TEAM_CT);
client_print(player, print_chat, "You have received a bonus");
That's like lambda syntax.
__________________