"And" bit comparison doesn't exist?

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Post Reply
User avatar
SurferIX
Posts: 79
Joined: 10 May 2011, 16:14
Contact:

"And" bit comparison doesn't exist?

Post 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
}
http://olivierpons.fr
Mon dernier framework hautes performances : http://www.papdevis.fr
krial057
Posts: 57
Joined: 13 Nov 2010, 20:15

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

Post 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
	}	
}
User avatar
SurferIX
Posts: 79
Joined: 10 May 2011, 16:14
Contact:

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

Post 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
http://olivierpons.fr
Mon dernier framework hautes performances : http://www.papdevis.fr
User avatar
Xymph
Posts: 1399
Joined: 15 Jun 2010, 20:35
Contact:

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

Post 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.
Developer of XASECO for TMF/TMN ESWC & XASECO2 for TM²: see XAseco.org
Find your way around the Mania community from the TMN ESWC hub, TMF hub, TM² hub, and SM hub
User avatar
SurferIX
Posts: 79
Joined: 10 May 2011, 16:14
Contact:

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

Post by SurferIX »

The directions are useless since it's only a flat square with nothing on it.
http://olivierpons.fr
Mon dernier framework hautes performances : http://www.papdevis.fr
Post Reply

Return to “ManiaScript”

Who is online

Users browsing this forum: No registered users and 4 guests