Raised This Month: $32 Target: $400
 8% 

GetPlayers()


Post New Thread Reply   
 
Thread Tools Display Modes
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 09-25-2016 , 09:31   Re: GetPlayers()
Reply With Quote #11

This is a horrible idea no matter how you look at it.

Code:
public function() {     new PlayerBitsum = GetPlayersBitsum();     new IsDedicatedServer = is_dedicated_server();     for ( new i = 0 ; i < 32 ; i++ ) {         if ( PlayerBitsum & (1<<i) )             client_print(i + IsDedicatedServer, print_chat, "Hello!");     } } public function(id) {     new PlayerBitsum = GetPlayersBitsum("a");     new IsDedicatedServer = is_dedicated_server();     if ( PlayerBitsum & id - IsDedicatedServer )         client_print(id, print_chat, "Hello!"); } stock GetPlayersBitsum(const flags[]="", const team[]="") {     new Players[32], PlayersNum, PlayersBitsum, IsDedicatedServer = is_dedicated_server();     get_players(Players, PlayersNum, flags, team);     for ( new i ; i < PlayersNum ; i++ )         PlayersBitsum |= 1 << Players[i] - IsDedicatedServer;     return PlayersBitsum; }
__________________
Black Rose is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-25-2016 , 12:38   Re: GetPlayers()
Reply With Quote #12

You can do PlayersBitsum |= 1 << ( Players[i] & 31 ) to handle index 32.
__________________
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 09-25-2016 , 13:40   Re: GetPlayers()
Reply With Quote #13

@Depresie: You still sleeping ?

@Black Rose: That's what i'm scared of, to create AGAIN the loop, it's stupid, i will just leave it ...

@Bugsy: That's an example how to handle the index 32 ( last player ) ... but no way to check all of them?

Like:

new szName[32];
get_user_name( PlayersBitsum, szName ..............

You know what i mean ..
__________________
Project: Among Us

Last edited by Craxor; 09-25-2016 at 13:54.
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-25-2016 , 13:59   Re: GetPlayers()
Reply With Quote #14

Quote:
Originally Posted by Craxor View Post
@Depresie: You still sleeping ?

@Black Rose: That's what i'm scared of, to create AGAIN the loop, it's stupid, i will just leave it ...

@Bugsy: That's an example how to handle the index 32 ( last player ) ... but no way to check all of them?

Like:

new szName[32];
get_user_name( PlayersBitsum, szName ..............

You know what i mean ..
No, I don't know what you mean. PlayersBitsum is not equal to a individual player indexes. It stores all player indexes using a single bit of memory to represent each slot that has a player connected; A bit has a value of 0 or 1. There are 32 bits of memory in a single cell/variable. Read the bitsum tut for more info. With that said, you cannot call any player natives/functions using this as the player index. What makes you think you can store multiple player names within al single string variable? That makes no logical sense.
__________________

Last edited by Bugsy; 09-25-2016 at 14:00.
Bugsy is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 09-25-2016 , 14:03   Re: GetPlayers()
Reply With Quote #15

Yes, i get it, after i test all the previous night this stock )

The only thing i can do with, is to test if a 'index' is inside of a bit-field, if that bit-field contain that specific index, and that's useless.

Maybe if i would understand in a deep way how bits works i can make a way to 'convert' these, but i im not realy good at matematics so i don't think is any chance... i can learn only in a practical way ...
__________________
Project: Among Us

Last edited by Craxor; 09-25-2016 at 14:03.
Craxor is offline
Send a message via ICQ to Craxor
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-25-2016 , 14:32   Re: GetPlayers()
Reply With Quote #16

Quote:
Originally Posted by Craxor View Post
Yes, i get it, after i test all the previous night this stock )

The only thing i can do with, is to test if a 'index' is inside of a bit-field, if that bit-field contain that specific index, and that's useless.

Maybe if i would understand in a deep way how bits works i can make a way to 'convert' these, but i im not realy good at matematics so i don't think is any chance... i can learn only in a practical way ...
Bits/bitfields are not specific to AMX-X so you can find info all over the web. I did write a relatively basic tutorial that may help you.

https://forums.alliedmods.net/showthread.php?t=139916
__________________
Bugsy is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 09-25-2016 , 15:08   Re: GetPlayers()
Reply With Quote #17

Quote:
Originally Posted by Bugsy View Post
You can do PlayersBitsum |= 1 << ( Players[i] & 31 ) to handle index 32.
You can just use 1 << Players[i] because (1 << 32) works as (1 << 0).
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-25-2016 , 15:21   Re: GetPlayers()
Reply With Quote #18

Quote:
Originally Posted by PRoSToTeM@ View Post
You can just use 1 << Players[i] because (1 << 32) works as (1 << 0).
The bit will fall off of the end. You cannot use the logic 'if BitSum == 0 then slot 32 is connected'.

Also (assuming a 32 bit):
1 << 32 = 0
1 << 0 = 1

Blue are valid bits within a cell (32). 1 << 32 goes to the 33rd bit, which doesn't exist in a 32-bit cell.
[0000 0001] [0000 0000] [0000 0000] [0000 0000] [0000 0000]
__________________
Bugsy is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 09-25-2016 , 15:57   Re: GetPlayers()
Reply With Quote #19

@Bugsy the shift count is given by the low-order five bits of the second operand (& 31).
Code:
    register_clcmd("say", "OnClientCommand_Say_BitShift");
}

public OnClientCommand_Say_BitShift(clientEntIndex) {
    new args[128];
    read_args(args, charsmax(args));
    remove_quotes(args);
    
    new cmd[32];
    new value[32];
    parse(args, cmd, charsmax(cmd), value, charsmax(value));
    
    if (!equal(cmd, "/bitshift")) {
        return;
    }
    
    new shiftCount = str_to_num(value);
    
    client_print(clientEntIndex, print_chat, "(1 << %d) = %d", shiftCount, (1 << shiftCount));
}
Prints:
Code:
(1 << 0) = 1
PRoSToC0der :  /bitshift 0

(1 << 32) = 1
PRoSToC0der :  /bitshift 32
__________________

Last edited by PRoSToTeM@; 09-25-2016 at 15:59.
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 09-25-2016 , 16:27   Re: GetPlayers()
Reply With Quote #20

Quote:
Originally Posted by https://en.wikipedia.org/wiki/Logical_shift
All currently relevant C standards (ISO/IEC 9899:1999 to 2011) leave a definition gap for cases where the number of shifts is equal to or bigger than the number of bits in the operands in a way that the result is undefined.
This means that shifting the bits farther than available bits is not defined. Thus, the behavior can be unpredictable. You should never rely on something that is not defined by a standard (unless you yourself are able to control how it is handled; in this case you can't control it) and is why you should do it the proper way with what Bugsy suggested.

In other words, just because it works empirically for now, doesn't mean that a future update won't break it.
__________________

Last edited by fysiks; 09-25-2016 at 16:30.
fysiks 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 21:35.


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