Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dark_radeon

storing 8 different states (need some Clarification...)

This topic is 5644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m in the implementation phase of this little text RPG that i''m working on and well, i''ve decided to build a few townsman that will have a total of 8 different things that can trigger them to perform a certain event. For instance, If they''ve been hurt-they do something, if they''re low in health-PerformAction(), if they''re hungry-PerformAction()....Is this making sense so far?(I hope so...)Well here''s the question. Since the person has 8 different states, I was thinking that I could use a char. char townManState; and I was planning to map each state to a bit where I test if each bit is either 1 or 0..(I hope this is still making sense to you)...Now first off, is what i''m saying possible? or would i have to create separate data types (8 data types) to moniter those 2 separate states. And if it is possible, does anyone have any suggestions on how to approach this implementation, or does anyone have any sites that can be a guide when working with bits in this since. I know i would be using the |, & and maybe ^ operator to test each bit as the towns people''s state changes...The question is how do i truly go about doing it. I know how to bit mask, bit shift and all of that, but i need a bit of a guideline to go about implementing this feature correctly -Zeke''''s Signature

Share this post


Link to post
Share on other sites
Advertisement
Hey.

I''m not a total expert in c++, but couldn''t you create an enum type? If your townsman''s only able to have one state at a time an enum is totally ok I think.

Share this post


Link to post
Share on other sites
Well i know i could do something like this

enum TownmanState
{
isHealthLow, isHungry, isWalking, isHappy....
};

all the way up to 8 different states, but I wanted to know is there any kind of way of simply using the 1 and 0''s in a char, besides if i create an enum with 8 different values in it, won''t the size be bigger? I was thinking that the values in the enum get converted to integers ? so wouldn''t each state variable in the enum be 4 bytes each so 8*4=32 so that''d be 32 bytes? i was thinking of that while i could just use a 1 byte char to check the 8 different states...Or am i asking too much and using the enum is the way to go?...any suggestions, critiques would help me...any little pointers that you guys might have would be very helpful


-Zeke''''s Signature

Share this post


Link to post
Share on other sites
bitwise operations


i think they go liek this i have them in my code somewhere


if (yourbyte & 128)
{
//first bit set
}

if (yourbyte & 64)
{
//second bit set
}


to set them i think you do seomthing like


yourbyte = yourbyte & 128 //this may just toggle it im not sure

do a google on bitwise operation

Share this post


Link to post
Share on other sites
Firstly you need some constants one for each state

Hungry = 00000001
LowHealth = 00000010

and so on til you have one contant for each bit in the byte (to do it in C++ use hex and so on).

then you test like this

if(State & Hungry)
{
// Hungry code
}

and so on. Basically this means if the bit that shows you are hungry is 1, then do the hungry code. This works because the only bit in the hungry constant that is 1 is the bit that you use to store the hungry stae. Easy!

To make the villager hungry you do this

State = State | Hungry

That will or the two together and therefor set the right bit.

To cler you need some more constants, working on the two I have shown we would have this:

ClearHunger = 11111110
ClearLowHealth = 11111101

As you can see, you just invert the bits. then you simply

State = State & ClearHunger

I think you can work out how this works by now

To assign the constants in code remember your hex

Variable = 0x1 == 00000001
Variable = 0x3 == 00000010
Variable = 0x4 == 00000100
Variable = 0x8 == 00001000
Variable = 0x10 == 00010000
Variable = 0x20 == 00100000
Variable = 0x40 == 01000000
Variable = 0x80 == 10000000

Variable = 0xFE == 11111110
Variable = 0xFD == 11111101
Variable = 0xFB == 11111011
Variable = 0xF7 == 11110111
Variable = 0xEF == 11101111
Variable = 0xDF == 11011111
Variable = 0xBF == 10111111
Variable = 0x7F == 01111111

Might want to double check those as I did it in a hurry.

You can also use bit fields in C++ a quick search for those on google will help you (as I have to go out now and cant type it up for you).

Goodluck

ps Use enums instead if I were you, they are quicker and are les shassel. You are very unlikely to run out of space on a modern pc, ofcourse if you are programming for GBa or something well maybe there is a need.




Deadpan Studios

Share this post


Link to post
Share on other sites
typedef unsigned char PlayerState;

const PlayerState STATE_NULL = 0x00;
const PlayerState STATE_A = 0x01;
const PlayerState STATE_B = 0x02;
const PlayerState STATE_C = 0x04;
const PlayerState STATE_D = 0x08;
const PlayerState STATE_E = 0x10;
const PlayerState STATE_F = 0x20;
const PlayerState STATE_G = 0x40;
const PlayerState STATE_H = 0x80;

bool isInState(PlayerState value, PlayerState which)
{
return (value & which) != 0;
}

void setState(PlayerState& value, PlayerState which)
{
value = which; // see note later

}

void Player::whatever()
{
setState(this->state, STATE_H);

if (isInState(this->state, STATE_F))
whatever_();
else
whateverElse();
}


The setState above does a simple assignment so that only one state can be active at a time. You can have many states if you want by using the | operator (bitwise or). For example:

void setState(PlayerState& value, PlayerState which)
{
value |= which;
}

void removeState(PlayerState& value, PlayerState which)
{
value = value & ~which; // or value &= ~which in shorthand

}

void toggleState(PlayerState& value, PlayerState which)
{
value = value ^ which; // or value ^= which

}


EDIT: here's a link to an article about bit fiddling here at gdnet.


[edited by - Alimonster on July 3, 2003 9:51:40 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by dark_radeon
I wanted to know is there any kind of way of simply using the 1 and 0''s in a char, besides if i create an enum with 8 different values in it, won''t the size be bigger?

Yes. So what? Are you working on a pocket calculator?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah, use a long to give yourself room for expansion. Longs are what the processor''s optimized to work with, anyway.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Longs are what the processor''s optimized to work with, anyway.


Actually, that''s ints. And it''s the other way around. An int is supposed to be the optimum datatype for a processor (in case of the 386 and better, that''s 32 bits). A long has at least as many bits as an int. So, although on MSVC and most other Win32 compilers, the long and int are both 32bit, there''s nothing to keep a long from being, say 64 bit or even more (or even 40 bits -- however weird that may be).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!