AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED]Best way to randomly choose a random player from the enemy team? (https://forums.alliedmods.net/showthread.php?t=293185)

Infractem 01-25-2017 06:58

[SOLVED]Best way to randomly choose a random player from the enemy team?
 
Hi. I would like to request a code snippet where you can randomly choose a random enemy player. I am trying to create a "smite" like plugin where a random enemy player gets smitten. Thanks!

Bugsy 01-25-2017 07:17

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
Search around a bit, this has been covered. Are you trying to avoid duplicates or any other conditions?

Infractem 01-26-2017 09:11

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
I made this code using the other parts of the plugin I am editing right now(cypis' killstreak plugin)
The thing is this bombs EVERY enemy on the map, I only need a single random enemy to get bombed. Can you help me with this one, please?

Code:

public CreatePredator2(id)
{
        new num, players[32];
        get_players(players, num, "gh");
        for(new a = 0; a < num; a++)
        {
                new i = players[a];
                if(get_user_team(id) != get_user_team(i))
                        client_cmd(i, "spk sound/mw/predator_enemy.wav");
                else
                        client_cmd(i, "spk sound/mw/predator_friend.wav");
        }
        set_task(5.0, "predator_continue", id)
        predator[id] = false;
}

public predator_continue(id)
{
        new num, players[32], PlayerCoords[3];
        get_players(players, num, "gh")
        for(new a=0; a<num; a++)
        {     
                if(!is_user_alive(players[a]) || get_user_team(players[a]) == get_user_team(id))
                        continue;
                get_user_origin(players[a], PlayerCoords);
               
                new Float:LocVec[3];
                IVecFVec(PlayerCoords, LocVec);
                create_ent(id, "bomb", "models/p_hegrenade.mdl", 2, 10, LocVec);
        }
}


OciXCrom 01-26-2017 09:37

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
PHP Code:

get_random_enemy(id)
{
    new 
iPlayers[32], iPnum
    get_players
(iPlayersiPnum"ae"get_user_team(id) == "TERRORIST" "CT")
    
    if(
iPnum)
        return 
iPlayers[random(iPnum)]


PHP Code:

new iPlayer get_random_enemy(id)

if(
iPlayer)
{
    
// Your code here...



edon1337 01-26-2017 10:01

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
Quote:

Originally Posted by OciXCrom (Post 2490051)
PHP Code:

get_random_enemy(id)
{
    new 
iPlayers[32], iPnum
    get_players
(iPlayersiPnum"ae"get_user_team(id) == "TERRORIST" "CT")
    
    if(
iPnum)
        return 
iPlayers[random(iPnum)]


PHP Code:

new iPlayer get_random_enemy(id)

if(
iPlayer)
{
    
// Your code here...



PHP Code:

get_players(iPlayersiPnum"ae"get_user_team(id) == "CT" "TERRORIST"


Infractem 01-26-2017 10:07

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
Thanks guys, I'm gonna try this out.

EDIT: Working fine now, except compiler tells me that iPlayers

//this line
return iPlayers[random(iPnum)]

should return a value. How do I fix that?(Sorry extra noob scripter here)

OciXCrom 01-26-2017 10:43

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
PHP Code:

get_random_enemy(id)
{
    new 
iPlayers[32], iPnum
    get_players
(iPlayersiPnum"ae"get_user_team(id) == "CT" "TERRORIST")
    
    if(
iPnum)
        return 
iPlayers[random(iPnum)]
    
    return 
0


PHP Code:

new iPlayer get_random_enemy(id)

if(
iPlayer)
{
    
// Your code here...



Bugsy 01-26-2017 11:23

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
Best to avoid using magic numbers. I'd do the below or use cstrike for CS_TEAM_T.
PHP Code:

enum Teams
{
    
Team_Unassigned,
    
Team_T,
    
Team_CT,
    
Team_Spectator
}

get_random_enemyid )
{
    new 
iPlayers[32], iPnum
    get_players
(iPlayersiPnum"ae"Teams:get_user_teamid ) == Team_T "CT" "TERRORIST")
    
    return 
iPnum iPlayersrandomiPnum ) ] : 0;



edon1337 01-26-2017 14:45

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
Quote:

Originally Posted by Bugsy (Post 2490075)
Best to avoid using magic numbers. I'd do the below or use cstrike for CS_TEAM_T.
PHP Code:

enum Teams
{
    
Team_Unassigned,
    
Team_T,
    
Team_CT,
    
Team_Spectator
}

get_random_enemyid )
{
    new 
iPlayers[32], iPnum
    get_players
(iPlayersiPnum"ae"Teams:get_user_teamid ) == Team_T "CT" "TERRORIST")
    
    return 
iPnum iPlayersrandomiPnum ) ] : 0;



I'd like to ask, does get_user_team consider Team_Unassigned as 0, Team_T as 1 ... ?

OciXCrom 01-26-2017 15:24

Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
 
enum numerates the variables starting from 0 if no other default value is assigned.


All times are GMT -4. The time now is 21:03.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.