Aller au contenu

Photo

Script finesse - How to test for Modulus (%)


  • Veuillez vous connecter pour répondre
20 réponses à ce sujet

#1
Morbane

Morbane
  • Members
  • 1 883 messages
I would like to script a test vs a random number but with the added modifier of being divisible by 2 (%2). Is there a way to script with some sort of algorithm for that?

In the end the test will only fire if the test vs %2 is TRUE.

Thanks

#2
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
int nRoll = d100(); // Roll a 100 sided die (percentage roll)
if(nRoll <= 2) // If the roll is equal to 2 or less
{
// Do Stuff
}

#3
Morbane

Morbane
  • Members
  • 1 883 messages
Thats not really what I meant - I am looking to generate a random number like 0 - 7, if the roll is even do something, if the roll is odd do something else.

#4
_Knightmare_

_Knightmare_
  • Members
  • 643 messages
Ah I get it, sorry. You can do it with either a if/else or a switch/case if there are differences on what each number might do (beyond odd/even). There is no function that I know of that determines if a mathmatical/roll/random result is odd or even:

int nRand = Random(7); // Choose a random number between 0 and 7

if((nRand == 0) || (nRand == 2) || (nRand == 4) || (nRand == 6))
{
// Do Stuff if roll is 0 or an even number
}
else // It is not 0, it is an odd number
{
// Do something else
}

Modifié par _Knightmare_, 09 mai 2011 - 10:21 .


#5
MasterChanger

MasterChanger
  • Members
  • 686 messages

Morbane wrote...

I would like to script a test vs a random number but with the added modifier of being divisible by 2 (%2). Is there a way to script with some sort of algorithm for that?

In the end the test will only fire if the test vs %2 is TRUE.

Thanks


I don't know what the remainder operator is for nwscript, but there is one in C. You would test if the remainder when divided by 2 equals 0.

#6
kevL

kevL
  • Members
  • 4 061 messages
i wonder if this actually works:

string sResult;
 
// determine if number is odd or even using modulus
if (nNumber % 2)
{
sResult = "Odd";
}
else
{
sResult = "Even";
}


- from nwn.wikia.com

#7
Dann-J

Dann-J
  • Members
  • 3 161 messages
You could  test (x / 2) against absolute(x / 2). If they are equal, then the number is even. If they aren't equal, then the number is odd. For instance;

int iX = Random(7)+1;
float fX = IntToFloat(iX);
float fX2 = fX / 2;
if (fX2 == abs(fX2)// number is even
[do something]
else // number is odd
[do something else]

Modifié par DannJ, 09 mai 2011 - 11:56 .


#8
Kaldor Silverwand

Kaldor Silverwand
  • Members
  • 1 592 messages
A function that returns true if even:

// return true if number is even (note: 0 is considered even by this function)
int IsEven (int nValue)
{
return (!(nValue%2));
}


If you need a conversation script that does the check:

// gc_bb_is_even
// return true if number is even (note: 0 is considered even by this function)
int StartingConditional(int nValue)
{
return (!(nValue%2));
}

This return line will also work:
return ( nValue / 2 == (1 + nValue) / 2 );

Modifié par Kaldor Silverwand, 10 mai 2011 - 02:22 .


#9
kevL

kevL
  • Members
  • 4 061 messages

Kaldor Silverwand wrote...

This return line will also work:
return ( nValue / 2 == (1 + nValue) / 2 );

sneaky sneaky ..

#10
Morbane

Morbane
  • Members
  • 1 883 messages
I tried to use the "%" to test for modulo 2 or (%2) - the error was trapped at the "%" - I forget the exact message. So I am wondering, is it supposed to work in NWScript? Or not? Because I have recently installed Skywing's Advanced Script Compiler http://social.biowar...4/index/7324714 - so if that is causing me grief I might have to just not do the modulo operation because I like the ASC. I was just trying to be fancy.

Just the same I like the options you guys posted - I will test them and see what happens.

Thanks Kaldor and Dan :)

Modifié par Morbane, 10 mai 2011 - 07:12 .


#11
GhostOfGod

GhostOfGod
  • Members
  • 863 messages
Yes Modulo works in NWScript.

www.nwnlexicon.com/compiled/primer.arithmaticandstringoperators.html

Modifié par GhostOfGod, 10 mai 2011 - 07:35 .


#12
kevL

kevL
  • Members
  • 4 061 messages
using the default compiler this went through fine:

// test_modulo

void main(int iInteger)
{
object oPC = GetFirstPC();
string sResult;

// determine if number is odd or even using modulus
if (iInteger %2) sResult = "Odd";
else sResult = "Even";

SendMessageToPC(oPC, sResult);
}


came up with the right answers inGame, too.

#13
Morbane

Morbane
  • Members
  • 1 883 messages
Grr.

I must go back and try it again, Mayhap I got an associated error that the error checker just flagged as the %?

#14
Morbane

Morbane
  • Members
  • 1 883 messages
This is the final script with modulus

void main()
{
if(!GetGlobalInt("kuo_trig")) return;

location lLoc = GetLocation(GetObjectByTag("ip_kuo_spawn"));

if(GetGlobalInt("kuo_count") >= 7) return;

int nVar = GetGlobalInt("kuo_count");

if(!nVar || nVar%2)
{
CreateObject(OBJECT_TYPE_CREATURE, "c_kuoa_toa", lLoc);

SetGlobalInt("kuo_count", nVar+1);
}
else
SetGlobalInt("kuo_count", nVar+1);
}

Essentially it spawns a kuoa-toa every other heartbeat - I didnt want them swarming out of their pool but to pile up slowly.
So - yippie! I works now.

Modifié par Morbane, 10 mai 2011 - 09:01 .


#15
SkywingvL

SkywingvL
  • Members
  • 351 messages

Morbane wrote...

I tried to use the "%" to test for modulo 2 or (%2) - the error was trapped at the "%" - I forget the exact message. So I am wondering, is it supposed to work in NWScript? Or not? Because I have recently installed Skywing's Advanced Script Compiler http://social.biowar...4/index/7324714 - so if that is causing me grief I might have to just not do the modulo operation because I like the ASC. I was just trying to be fancy.

Just the same I like the options you guys posted - I will test them and see what happens.

Thanks Kaldor and Dan :)


Yes, the modulus operator % is supported in NWScript.  Were you able to get this working?  The operator should be recognized in the compiler plugin, as with the standard script compiler.

#16
Morbane

Morbane
  • Members
  • 1 883 messages

SkywingvL wrote...

Morbane wrote...

I tried to use the "%" to test for modulo 2 or (%2) - the error was trapped at the "%" - I forget the exact message. So I am wondering, is it supposed to work in NWScript? Or not? Because I have recently installed Skywing's Advanced Script Compiler http://social.biowar...4/index/7324714 - so if that is causing me grief I might have to just not do the modulo operation because I like the ASC. I was just trying to be fancy.

Just the same I like the options you guys posted - I will test them and see what happens.

Thanks Kaldor and Dan :)


Yes, the modulus operator % is supported in NWScript.  Were you able to get this working?  The operator should be recognized in the compiler plugin, as with the standard script compiler.


All is well - I was getting an error but the flag must have been something different not the  % operator. I posted the script above of the working code.

#17
The Fred

The Fred
  • Members
  • 2 516 messages
Surely it would have been easier just to use plain chances? For example, saying "Is 1-7 even" is like saying "Is 1-7 less than 4?". So long as you're using an even chance (e.g. a d8), you could just preface everything with an "if(d2() == 2){return;}" or somesuch.

Of course, modulus does work just fine (I've used it myself, in the past) so there's no need, but it seems like you were overcomplicating things... (something I'm very familiar with myself).

#18
The Fred

The Fred
  • Members
  • 2 516 messages
Also, can you guys slow down a bit, I never get to answer anything around here!

#19
Kaldor Silverwand

Kaldor Silverwand
  • Members
  • 1 592 messages
I always try to make sure my bad advice is given as quickly as possible.

#20
Morbane

Morbane
  • Members
  • 1 883 messages
The script runs from an ipoint hb - so to slow down the spawn I decided to use modulus so it would only spawn on even results on a scale that progressed by one per hb until the max value was reached - so I thought modulus was the easier choice fewer lines of code to get the same results eg spawn every 12 seconds instead of every six.

#21
The Fred

The Fred
  • Members
  • 2 516 messages
You could do something like this:

int nSwitch = GetLocalInt(OBJECT_SELF, "<VarnameHere>");
if(nSwitch)
{
    //Do stuff
}

SetLocalInt(OBJECT_SELF, "<VarnameHere>", !nSwitch);

Modulus works just fine, though.