AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   flags/math help [2, 4, 8, 16, 32, etc] (https://forums.alliedmods.net/showthread.php?t=65061)

sgtbane 12-31-2007 01:54

flags/math help [2, 4, 8, 16, 32, etc]
 
ok, this has been bothering me for a while so im just gonna ask on here. The reason I want this is not exactly directly amxx related, but related to many forms of code.
In AMXX there is the player flags. a-u, and I am assuming they use the values 1,2,4,8,16,32,64,128,256,512,1024,2048, etc...
(If this is not the case then please help me anyways because as I said i have been wondering for a while.)

when you have something like this you can have a variety of different options selected and store it as a single value because no combination of them will ever equal another.

so how do you read it?
if i had the number 69, how do i know that its 64, 4, and 1? There has to be a mathematical formula to figure it out but i can't get it.
again I know that this is not directly connected with AMXX although I am sure it is used in some way or form, and I know when writing I have often wondered how it is done.

If anyone can help thankyou very much. If this is removed since it does not apply to this "Support / Help - Find help for general AMXX issues. This does not include support for individual 3rd party plugins." then thanks anyways..

Brad 12-31-2007 20:46

Re: help?
 
The more serious issue, that this thread would be deleted regarding, is your choice of "help?" as a thread topic. Please fix that before responding again.

Generally you wouldn't be asking which numbers is 69 comprised of. Instead you'd be asking, is x in y? You'd do that in either an if/endif or switch/case statement.

sgtbane 01-02-2008 17:20

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
thanks. i changed the title
also i discovered a method to get it done, but i was wondering if there is a simpler way, since with this you have to count down backwards through them till you find the one you want. Just wondering if there is a more direct way.

anyways this is what I am doing:
Code:

    Dim tmpcntInt As Integer, tmpInt As Integer, max As Long, i As Integer
    tmpInt = ReadInteger(ptrPlayer + 3088, pid)
    tmpcntInt = tmpInt
    max = 268435456
    For i = 0 To 28
        If tmpcntInt >= max And tmpcntInt <= (max * 2 - 1) Then
            tmpcntInt = tmpcntInt - max
            StorMove(28 - i) = True
        Else
            StorMove(28 - i) = False
        End If
        max = (max / 2)
    Next i

thats in VB 6.0 o.0... i know its old, please spare me the nagging :P
anyways I am getting the value from memory via pointer+offset, then making a copy of it to make it easyer to make changes to the value later.

as you can see I had to hard code the max value of what I wanted to find (in this case I went up to 28 flags, and at 28 it happens to equal what the hard coded max value is.
the boolean: StorMove(i) is where I store the flag values.

if there is a simpler method please let me know. or if this is how its supposed to be done then please also let me know.

Sylwester 01-02-2008 18:34

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
Have you ever heard about binary numeral system?

To convert your decimal number to binary number use this:
PHP Code:

//pseudocode
number your_value
0
while (number 0){
    
flags[i] = number mod 2
    number 
number div 2
    i
++
}
//now you must invert flags table 

To convert binary number to decimal number use this:
PHP Code:

//pseudocode
number 0
for (new ii<=maxi++)
    
number number flags[i


Lee 01-02-2008 22:16

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
Rather than..

Code:

If StarMove(n) Then doStuff
..which requires your above snippet, simply use..

Code:

Const ONE As Integer = 1
Const TWO As Integer = 2
Const FOUR As Integer = 4
Const EIGHT As Integer = 8
Const SIXTEEN As Integer = 16
Const THIRTY_TWO As Integer = 32
Const SIXTY_FOUR As Integer = 64

Dim bitPattern as Integer = 69

If bitPattern And SIXTY_FOUR Then
    Rem bitPattern must 'contain' 64
End If

Why Microsoft considered bit shifting operators unnecessary syntactic sugar until VB.NET 2003 (i.e. they still did in VB.NET 2002) is a mystery to me.

Roach 01-03-2008 22:29

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
Just bumping this down to scripting help...since its kinda odd being in General AMXx issues.

alien 01-04-2008 06:00

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
I'll just copy & paste what I'm using in my plugin.
First you'll define flags you want to store:

Code:

#define FLAG_LOGGINGIN                1 << 0
#define FLAG_USER                1 << 1
#define FLAG_CLIMBING                1 << 2
#define FLAG_SCOUT                1 << 3
#define FLAG_STORECP                1 << 4
#define FLAG_BHOPSCRIPT                1 << 5
#define FLAG_LONGJUMP                1 << 6
#define FLAG_CHECKPOINT                1 << 7
#define FLAG_CHEATWALLS                1 << 8
#define FLAG_CHEATDEATH                1 << 9
#define FLAG_CHEATGRAVITY        1 << 10
#define FLAG_CHEATSPEED                1 << 11
#define FLAG_NORESPAWN                1 << 12
#define FLAG_RESPAWNING                1 << 13
#define FLAG_LJSTATS                1 << 14
#define FLAG_LJMARKS                1 << 15
#define FLAG_VOTEDMAP                1 << 16
#define FLAG_SLOT                1 << 17
#define FLAG_RTV                1 << 18
#define FLAG_CHEATHOOK                1 << 19

Then you have two functions:

Code:
stock bool:get_flag(flagset, flag)   {     return bool:(flagset & flag);   } stock set_flag(&flagset, flag, bool:status)   {     if (status)       flagset |= flag;     else       flagset &= ~flag;     return;   }

You can use it like this:

Code:
set_flag(some_integer, FLAG_BHOPSCRIPT, true); if (get_flag(some_integer, FLAG_BHOPSCRIPT))   {     // This will be executed, because FLAG_BHOPSCRIPT is true.   }

Hope it helps. It's also very fast this way.

purple_pixie 01-07-2008 10:35

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
Just one teeny point ...

This is a little much effort:
Code:

#define FLAG_LOGGINGIN                1 << 0
#define FLAG_USER                1 << 1
#define FLAG_CLIMBING                1 << 2
#define FLAG_SCOUT                1 << 3
...

When you could just do:
Code:

enum <<=1
{
  FLAG_LOGGINGIN=1,
  FLAG_USER,
  FLAG_CLIMBING,
  FLAG_SCOUT,
...
}


Lee 01-07-2008 12:43

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
You can't use either in VB6. :)

purple_pixie 01-08-2008 05:49

Re: flags/math help [2, 4, 8, 16, 32, etc]
 
Ah. I forgot that this post in Scripting Help was actually aboot VB ...

I would have posted something relevant, but if I see any more VB I shall either cry or shoot something.

It's such an ugly language. Euch.


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

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