Scripting help needed, char type variable.

Started by Jim Reed, Sat 01/08/2009 19:22:57

Previous topic - Next topic

Jim Reed

Ahem...I'm making a custom object move function that takes into consideration 8 collision points and 8 directions. (N, NE, E, SE, S, SW, W, NW)

Now, I would like to update the status of the collision points, using the GetWalkableAreaAt function, so if a collision point is on walkable area 0 the int that represents the collision point should read 1, and when on walkable area 1, it should read 0.
After all 8 points statuses have been checked, the function should, determine the object position by using 8 arrays (one for each direction) of [256] possible combinations, which are provided by a .txt file and loaded into the arrays beforehand.
Now I now I could declare 8 ints and check them one by one against the list to determine the outcome.

But, as that seems big, I was thinking, if I could make the 8 collision statuses into one byte or something. It would save me a lot of typing. I'm not very good at explaing things or scripting but as I see it:
Cp1=1
Cp2=0
Cp3=1
Cp4=0
Cp5=0
Cp6=0
Cp7=1
Cp8=0
...could easily be:
10100010
So if I used a Char would it be easier?

Pseudo code example:

Get the colission point's statuses
Turn them into a char
Get the direction wanted
Loop through the wanted direction array untill the status of the collision is the one I need
return the value under that index

Now how would go about scripting such a function? How do I go about making the 8 colision point's statuses into a char? Is there an easier way of doing this?
Can anyone help me, please?

Sorry for the bad grammar.

Pumaman

Well, << is the left shift operator so to set a bit to 1 you could do:

theChar = theChar | (1 << walkableAreaNumber);

However playing with bits like this can get a bit complicated and is easily prone to error.

Jim Reed

#2
Hmm, that's a bit over my head for now =D

I figure I can do:
Code: ags
int r=(cp1*10000000) + (cp2*1000000) + (cp3*100000) + (cp4*10000) + (cp5*1000) + (cp6*100) + (cp7*10) + (cp8*1); 


to go from:
Code: ags
cp1=1
cp2=0
cp3=1
cp4=0
cp5=0
cp6=0
cp7=1
cp8=0


...to:
10100010

...but how am I going to turn that to a value between 0-255?

EDIT: sorry Ryan, it still spells differently.

Ryan Timothy B

Use  [ code ]    and   [ /code ]    to display your coded text, without the spaces.

monkey0506

Quote from: Jim Reed on Sat 01/08/2009 22:07:35I figure I can do:
Code: ags
intare=(cp1*10000000) + (cp2*1000000) + (cp3*100000) + (cp4*10000) + (cp5*1000) + (cp6*100) + (cp7*10) + (cp8*1); 

Instead of that, why not:

Code: ags
int rr = (cp1 * 128) + (cp2 * 64) + (cp3 * 32) + (cp4 * 16) + (cp5 * 8) + (cp6 * 4) + (cp7 * 2) + cp8;


Do you specifically need to have the binary representation in the integer? Either way this would work. Or if you set "cp" as an array you could set up a while loop:

Code: ags
// top of script
bool cp[8]; // since each digit is just 0 or 1 then bool is the simplest

// ...
cp[0] = 1;
cp[1] = 0;
cp[2] = 1;
cp[3] = 0;
cp[4] = 0;
cp[5] = 0;
cp[6] = 1;
cp[7] = 0;

// ...
int pow(int base, int exponent) {
  return FloatToInt(Maths.RaiseToPower(IntToFloat(base), IntToFloat(exponent)));
}

int get_cp_int() {
  int i = 0;
  int j = 7;
  int rr = 0;
  while (i < 8) {
    rr += (cp[i] * pow(2, j));
    i++;
    j--;
  }
  return rr;
}


You could even do it in a function (like I did) so you can then just do:

Code: ags
int rr = get_cp_int();


At any time to convert the binary digits into the decimal value.

Since you're only using up to the 2^7 digit of course the highest value you'll get is 255 so you could technically store the result in a char.

Jim Reed

OK monkey_05_06, I can't understand what the second code does. It's because my math sucks. And my scripting sucks, too. BUT! I was thinking how to solve this problem yesterday at work, and I came to the conclusion that I can script it succsesfully if I can convert the 8 collision point's statuses to an combination from 0 to 255.

Code: ags

int rr = (cp1 * 128) + (cp2 * 64) + (cp3 * 32) + (cp4 * 16) + (cp5 * 8) + (cp6 * 4) + (cp7 * 2) + cp8;

And this does it, pretty neatly. Thank you monkey_05_06!

SMF spam blocked by CleanTalk