AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   help with bit (https://forums.alliedmods.net/showthread.php?t=337294)

PawnBegg 04-12-2022 16:02

help with bit
 
Hello, i cant find info about ->
Code:

#define get_bit(%2,%1) (%1 & (1<<(%2&31)))
#define set_bit(%2,%1) (%1 |= (1<<(%2&31)))
#define rem_bit(%2,%1) (%1 &= ~(1 <<(%2&31)))

what these definitions mean ? Can some one help me to understand what does rem_bit mean in this code?
Code:

public csgo_reset_data()
{
        for (new i = 1; i <= MAX_PLAYERS; i++) rem_bit(i, loaded);

        nvault_prune(operations, 0, get_systime() + 1);
}

public client_disconnected(id)
        rem_bit(id, loaded);

Thanks.

zXCaptainXz 04-12-2022 17:07

Re: help with bit
 
Without getting into too much detail on how it works, you can compare the int "loaded" to a boolean array of size 32. So say you want to check if a player is connected, you can do it like this, note that the bool method is in comments

Code:

new Connected //new bool:Connected[33]

#define get_bit(%2,%1) (%1 & (1<<(%2&31)))
#define set_bit(%2,%1) (%1 |= (1<<(%2&31)))
#define rem_bit(%2,%1) (%1 &= ~(1 <<(%2&31)))

public client_connect(id)
{
        set_bit(id, Connected) // Connected[id] = true;
}

public client_disconnect(id)
{
        rem_bit(id, Connected) // Connected[id] = false;
}

public IsClientConnected(id)
{
        if(get_bit(id, Connected)) //if(Connected[id])
        {
                //Player is connected
        }
        else
        {
                //Player is not connected
        }
}


CrazY. 04-12-2022 22:29

Re: help with bit
 
Bugsy has some good explanation on that https://forums.alliedmods.net/showthread.php?t=139916

rem_bit is unseting whatever value is stored on the "loaded" variable out of the "i" variable, which doesn't make much sense, as far as I'm concerned it should be the inverse, ram_bit(loaded, i).

Bugsy 04-12-2022 23:34

Re: help with bit
 
These macros are designed to store booleans/bit-flags for each player, allowing you to pass their player ID and the bit-field variable. It's pretty straightforward.

Suppose you wanted to record each player that was slayed, clearing the flag if they are revived.
PHP Code:

#define get_bit(%2,%1) (%1 & (1<<(%2&31)))
#define set_bit(%2,%1) (%1 |= (1<<(%2&31)))
#define rem_bit(%2,%1) (%1 &= ~(1 <<(%2&31)))

new WasPlayerSlayed;

SlayFuncid )
{
    
//slay( id );

    
set_bitid WasPlayerSlayed );
}

RevivePlayerid )
{
    
//revive( id );

    
rem_bitid WasPlayerSlayed );
}

bool:WasPlayerSlayedAndNotRevivedid )
{
    return !!
get_bitid WasPlayerSlayed );


If csgo_reset_data() should set all bits in loaded to 0, just do loaded = 0

Please ask a more specific question if you need help, but the above posts should help you understand what each macro does.

PawnBegg 04-13-2022 04:19

Re: help with bit
 
THANKS YOU GUYS, a little bit clear now ;)


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

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