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

Go through clients within X range of you


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SomePanns
Senior Member
Join Date: Dec 2013
Old 06-04-2016 , 14:05   Go through clients within X range of you
Reply With Quote #1

I'm trying to get all players that are within 500 radius of you and with less than 30 hp left.
Currently I'm struggling a bit with this as it doesn't really work as supposed.

What I want it to do as well, is to print a message to you and inform you if there's an injured player near you, and if there is but you later on leave that radius, another one prints saying there isn't (anymore).

This script works fine if there's just 1 injured player. However, if there is more than 1 injured player, it goes nuts. Simply because it stores absorigin of all injured players, and if you're within radius of one injured, but out of range of another injured, it will start to spam "injured is near/not near" messages in the chat.

This is all part of the code that you need (trust me), as it's a part of a big private project of mine.
This is a repeating timer that is called upon spawn of blue players.
Code:
public Action Thirst_Timer(Handle timer, int client) {
	if(GetClientTeam(client) == TEAM_BLUE) {
		float pos1[3];
		GetClientAbsOrigin(client, pos1);
		for(int i = 1; i <= MaxClients; i++){
			if(IsValidClient(i) && GetClientTeam(i) == TEAM_RED && GetClientHealth(i) < 30){
				float pos2[3];
				GetClientAbsOrigin(i, pos2);
				if(GetVectorDistance(pos1, pos2) <= 500){ // distance between targets is 500 radius or less (in range of injured)
					if(ThirstActive[client] == false) {
						ThirstActive[client] = true;
						PrintToChat(client, "Injured players nearby...");
					}
				}
				
				if(GetVectorDistance(pos1, pos2) > 500) { // out of ranged of injured
					if(ThirstActive[client] == true) {
						ThirstActive[client] = false;
						PrintToChat(client, "No injured players nearby...");
					}
				}
			}
		}
	}
}
SomePanns is offline
databomb
Veteran Member
Join Date: Jun 2009
Location: california
Old 06-04-2016 , 15:02   Re: Go through clients within X range of you
Reply With Quote #2

SomePanns, your entry condition for setting ThirsActive looks good but the exit condition should be checking everyone (you have it checking a single person).

PHP Code:
public Action Thirst_Timer(Handle timerint client) {
    if(
GetClientTeam(client) == TEAM_BLUE) {
        
float pos1[3];
        
bool bFound false;
        
GetClientAbsOrigin(clientpos1);
        for(
int i 1<= MaxClientsi++){
            if(
IsValidClient(i) && GetClientTeam(i) == TEAM_RED && GetClientHealth(i) < 30){
                
float pos2[3];
                
GetClientAbsOrigin(ipos2);
                if(
GetVectorDistance(pos1pos2) <= 500){ // distance between targets is 500 radius or less (in range of injured)
                    
bFound true;
                    if(
ThirstActive[client] == false) {
                        
ThirstActive[client] = true;
                        
PrintToChat(client"Injured players nearby...");
                    }
                }
            }
        }
        
// check exit condition for ThirstActive
    
}

__________________
databomb is offline
SomePanns
Senior Member
Join Date: Dec 2013
Old 06-04-2016 , 15:19   Re: Go through clients within X range of you
Reply With Quote #3

Quote:
Originally Posted by databomb View Post
the exit condition should be checking everyone (you have it checking a single person).
I appreciate your input, but can you elaborate on this one?
SomePanns is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 06-04-2016 , 15:39   Re: Go through clients within X range of you
Reply With Quote #4

How about going through each client, counting how many there are within the distance and with low hp, and after the loop is done, display that value?
Phil25 is offline
SomePanns
Senior Member
Join Date: Dec 2013
Old 06-04-2016 , 15:40   Re: Go through clients within X range of you
Reply With Quote #5

Quote:
Originally Posted by Phil25 View Post
How about going through each client, counting how many there are within the distance and with low hp, and after the loop is done, display that value?
That's what I wanted to do as I think that might work, but I have no idea how to go through each client that are in within distance.
SomePanns is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 06-04-2016 , 15:43   Re: Go through clients within X range of you
Reply With Quote #6

https://sm.alliedmods.net/api/index....d=show&id=875&
KyleS is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 06-04-2016 , 15:49   Re: Go through clients within X range of you
Reply With Quote #7

Would tracehull be more optimized than GetVectorDistance?
Phil25 is offline
SomePanns
Senior Member
Join Date: Dec 2013
Old 06-04-2016 , 16:01   Re: Go through clients within X range of you
Reply With Quote #8

This seems to do the trick, thanks for the input everyone!

PHP Code:
public Action Thirst_Timer(Handle timerint client) {
    if(
GetClientTeam(client) == TEAM_BLUE) {
        
float pos1[3];
        
bool bFound[MAXPLAYERS+1] = false;
        
GetClientAbsOrigin(clientpos1);
        for(
int i 1<= MaxClientsi++){
            if(
IsValidClient(i) && GetClientTeam(i) == TEAM_RED && GetClientHealth(i) < 30){
                
float pos2[3];
                
GetClientAbsOrigin(ipos2);
                if(
GetVectorDistance(pos1pos2) <= 500){ // distance between targets is 500 radius or less (in range of injured)
                    
bFound[client] = true;
                }
            }
        }
        
        if(
bFound[client] == true && ThirstActive[client] == false) {
            
ThirstActive[client] = true;
            
PrintToChat(client"Injured players nearby...");
        } 
        
        if(
bFound[client] == false && ThirstActive[client] == true) {
            
ThirstActive[client] = false;
                    
            
PrintToChat(client"No injured players nearby...");
        }
    }

SomePanns is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 06-04-2016 , 17:54   Re: Go through clients within X range of you
Reply With Quote #9

Here's how I would've wrote it (untested and yes, I have an allergy to indentation):
PHP Code:
public Action Thirst_Timer(Handle hTimerint client){

    if(
GetClientTeam(client) != TEAM_BLUE)
        return 
Plugin_Stop;        //Woops, wrong team, let's quit this thing (or Plugin_Continue, as he may change later and if this timer repeats)

    
float fClientPos[3], fOtherPos[3];
    
GetClientAbsOrigin(clientfClientPos);

    
int iCount 0;    //We're not interested in displaying the exact amount (are we?), so let's just try to collect at least two clients
    
for(int i 1<= MaxClientsi++){
    
        if(!
IsClientInGame(i))
            continue;
        
        if(
GetClientTeam(i) != TEAM_RED)
            continue;
        
        if(
GetClientHealth(i) > 30)
            continue;
        
        
GetClientAbsAngles(ifOtherPos[3]);
        if(
GetVectorDistance(fClientPosfOtherPos) <= 500)
            
iCount++; //Let's see if the loop finds somebody
        
        
if(iCount >= 2)
            break; 
//We know there are two injured players, this is enough info, because we will know whether to display "player" or "players"
    
    
}

    switch(
iCount){
    
        case 
0:{    //No one found
    
            
if(ThirstActive[client]){
            
                
PrintToChat(client"No injured players nearby...");
                
ThirstActive[client] = false;
            
            }
        
        }
        
        case 
1:{    //1 guy found
        
            
if(!ThirstActive[client]){
            
                
PrintToChat(client"Injured player nearby...");
                
ThirstActive[client] = true;
            
            }
        
        }
        
        case 
2:{    //2 or more guys found
        
            
if(!ThirstActive[client]){
            
                
PrintToChat(client"Injured players nearby...");
                
ThirstActive[client] = true;
            
            }
        
        }
    
    }

    return 
Plugin_Whatever;


Unless you need a whole bFound array for something, you can just use one variable, iCount in my example.
Phil25 is offline
databomb
Veteran Member
Join Date: Jun 2009
Location: california
Old 06-04-2016 , 20:32   Re: Go through clients within X range of you
Reply With Quote #10

Quote:
Originally Posted by SomePanns View Post
This seems to do the trick, thanks for the input everyone!
What is the purpose of bFound for any other index except 'client'? It appears you only need one value per timer callback.
__________________
databomb is offline
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 13:23.


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