Page 1 of 1

"And" bit comparison doesn't exist?

Posted: 03 Sep 2011, 01:21
by SurferIX
Hi,

I'd like to do something like this but it seems the "and" isn't know in ManiaScript. Am I right, and if so, how could I do this:

Code: Select all

if (Var && 1) {
  // Bit Nortd actif => trace north
}

if (Var && 2) {
  // Bit East actif => trace east
}
if (Var && 4) {
  // Bit South actif => trace south
}
if (Var && 8) {
  // Bit West actif => trace West
}

Re: "And" bit comparison doesn't exist?

Posted: 03 Sep 2011, 16:17
by krial057
Hey,
In your script example, you are using the logical operators to do the conditions. (In ManaiScript, && is for logical condions).
Like you said, i couldn't find the bitwise operations too. However I think that you don't need them. Logical operations are sufficient for your example. Here is how I would do it:

Code: Select all

/**
 *Declare some Constants for the directions
 **/
#Const North 1
#Const East 2
#Const South 3
#Const West 4

main()
{
	declare Integer Var=East; //you could also write declare Integer Var=2;
	if(Var==South)
		DialogDoMessage("test");
}
You could also do it with functions:

Code: Select all

Boolean isNorth(Integer Var)
{
	return Var==1;
}
Boolean isEast(Integer Var)
{
	return Var==2;
}
Boolean isSouth(Integer Var)
{
	return Var==3;
}
Boolean isWest(Integer Var)
{
	return Var==4;
}
If you really want to use bitwise operations, wait for the official documentation(if they implement bitwise operations( or if they are alredy implemented)) you can read there how. If you really need bitwise oeprations now, and you don't care about the performance, you can use this functions:
(I DON'T RECOMMAND USING THEM)

Code: Select all

#Const IntegerBitSize 32
Integer XOR (Integer pA, Integer pB)
{
	declare Integer Result;
	declare a=pA;
	declare b=pB;
	Result = 0;
	for (i ,1, IntegerBitSize) {
	    Result += Result;
	    if (a < 0) 
		{
	        if (b >= 0)
	            Result += 1;
	    } 
		else if (b < 0) 
		{
	        Result += 1;
	    }
	    a += a;
	    b += b;
	}
	return Result;
}
Integer AND (Integer pA, Integer pB)
{
	declare Integer Result;
	declare a=pA;
	declare b=pB;
	Result = 0;
	for (i,1,IntegerBitSize) 
	{
		Result +=Result;
		if (a < 0) 
		{
			if (b < 0)
           	 Result += 1;
		}
	    a += a;
	    b += b;
	}
	return Result;
}

Integer OR (Integer pA, Integer pB)
{
	declare Integer Result;
	declare a=pA;
	declare b=pB;
	Result = 0;
	for (i,1,IntegerBitSize) 
	{
		Result += Result;
		if (a < 0)
		{
	        Result += 1;
	    } 
		else if (b < 0) 
		{
	        Result += 1;
	    }
	    a += a;
	    b += b;
	}
	return Result;
}


main()
{
	declare Integer Var=2;
	if (AND(Var,1)!=0) 
	{
	  // Bit Nortd actif => trace north
	}
	if (AND(Var,2)!=0) 
	{
	  // Bit East actif => trace east
	}
	if (AND(Var,4)!=0) 
	{
	  // Bit South actif => trace south
	}
	if (AND(Var,8)!=0) 
	{
	  // Bit West actif => trace West
	}	
}

Re: "And" bit comparison doesn't exist?

Posted: 04 Sep 2011, 13:51
by SurferIX
Thank you, I've answered in the French forum as well.

I've asked the question at Stackoverflow, and I got a nice answer here.

Here's the code that works:

Code: Select all

Void PlaceNESW(Integer X, Integer Y, Integer Z, Integer Val)
{
  PB("ArenaSimpleBase", X, Y, Z, ::CardinalDirections::North);
  declare Boolean ValBitN = ((Val / 1 ) % 2) > 0;
  declare Boolean ValBitE = ((Val / 2 ) % 2) > 0;
  declare Boolean ValBitS = ((Val / 4 ) % 2) > 0;
  declare Boolean ValBitW = ((Val / 8 ) % 2) > 0;
  if (ValBitN) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
  if (ValBitE) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
  if (ValBitS) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
  if (ValBitW) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
}
The goal is to put a square somewhere on the map, and if there's an opening on the north, put a square on the north, same for the west, south and east. But I need the function to be very short, and I needed to have maybe more than one opening... so there's nothing better than using bits (I think so).

I call the funtion like this:

Code: Select all

  PlaceNESW(25, CoordYBaseSol+2, 27, 15); // NESW
  PlaceNESW(25, CoordYBaseSol+2, 25, 1); // N
  PlaceNESW(25, CoordYBaseSol+2, 23, 10); // EW
Thank you very much for your answer, I've learned a lot, even though I didn't follow your suggestion. :oops:

Thanks again

Re: "And" bit comparison doesn't exist?

Posted: 04 Sep 2011, 14:32
by Xymph
SurferIX wrote:

Code: Select all

  if (ValBitN) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
  if (ValBitE) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
  if (ValBitS) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
  if (ValBitW) {
    PB("ArenaSimpleBase", X, Y, Z-1, ::CardinalDirections::North);
  }
Shouldn't the latter three directions be ::East, ::South and ::West ? Just wondering, I've barely looked into ManiaScript.

Re: "And" bit comparison doesn't exist?

Posted: 08 Sep 2011, 20:37
by SurferIX
The directions are useless since it's only a flat square with nothing on it.