States - Take 2

10-12-2011

O.K.  it seems I covered a lot of information in the last posting and because of the DAMN Blogging software I am using cutting the last quarter of the previous post I never got to finish what I wanted to say.  I am not going to continue with examples of the XOR operator at this time.  Instead I am going to discuss why the use of Binary is preferred over other methods.

Take for example our monster with the 8 previously defined states: Awake, Hungry, Enraged, Poisoned, Trapped, Crippled, Confused and Scared.  Why not just setup a string with zero's and one's in it like "00000110" and store the string in memory.  The quick answer is because it is not efficient in two ways.

First, each "character" is represented by one byte (8 bits). Zero is represented by %00110000 (remember % means binary number) and a One is represented by %00110001.  So, that 8 state string would take up 64 bits. If you have a hundred creatures thent hat would be 6,400 bits taken up (6Kb).  If we use a Binary number then the 8 states take up only 8 bits and storage of the states of 100 creature would take up only 800 bits (0.78Kb).   Big difference.  If we are doing a multiplayer game and we have hundreds of creatures over hundreds of instances our memory utilization becomes quite signifcant and we will introduce lag into the game as the system must dump portions of memory to disk.

Second, determining each state introduces additional programming loops that would slow the game down even that much more.  In order to determine the one state of Crippled the following code would be needed:

[+] Code Snippet

IF VAL(MID$(current_mob_state,6,1))
;mob is crippled if Val<>0
ENDIF

For checking a single state all we added was the code to pull the single character located in the sixth position and convert it to a number.  Couple of extra commands in a single line of code but lots of extra code when compiled.  In order to check every state we might end up with something more like this:

[+] Code Snippet

FOR i = 1 to number_of_mobs
     current_mob_state=monster_states[i]
     FOR j = 1 to 8
          mob_state[i,j]=VAL(MID$(current_mob_state,j,1))
     NEXT j
NEXT I

Can you see all the memory you would be using up, along with all of the additional, and unnecessary program loops?  It really adds up over time and will be seen by the end user in the form of a slow game.

O.K.  I guess that is enough for today so that I don't exceed the limits of this simple blogging software.

Next, I will go back and offer the XOR examples.

Take care

Add a Comment