Raised This Month: $51 Target: $400
 12% 

[SOLVED]Best way to randomly choose a random player from the enemy team?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Infractem
Member
Join Date: Dec 2016
Old 01-25-2017 , 06:58   [SOLVED]Best way to randomly choose a random player from the enemy team?
Reply With Quote #1

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!

Last edited by Infractem; 01-29-2017 at 02:13.
Infractem is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-25-2017 , 07:17   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #2

Search around a bit, this has been covered. Are you trying to avoid duplicates or any other conditions?
__________________
Bugsy is offline
Infractem
Member
Join Date: Dec 2016
Old 01-26-2017 , 09:11   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #3

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);
	}
}
Infractem is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-26-2017 , 09:37   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #4

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...

__________________

Last edited by OciXCrom; 01-26-2017 at 09:40.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-26-2017 , 10:01   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #5

Quote:
Originally Posted by OciXCrom View Post
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"
__________________
edon1337 is offline
Infractem
Member
Join Date: Dec 2016
Old 01-26-2017 , 10:07   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #6

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)

Last edited by Infractem; 01-26-2017 at 10:36.
Infractem is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-26-2017 , 10:43   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #7

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...

__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-26-2017 , 11:23   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #8

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;

__________________

Last edited by Bugsy; 01-26-2017 at 11:26.
Bugsy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-26-2017 , 14:45   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
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 ... ?
__________________
edon1337 is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 01-26-2017 , 15:24   Re: [Snippet request]Best way to randomly choose a random player from the enemy team?
Reply With Quote #10

enum numerates the variables starting from 0 if no other default value is assigned.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:58.


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