diff options
-rw-r--r-- | npc/functions/bitwise.txt | 67 | ||||
-rw-r--r-- | npc/functions/math.txt | 19 | ||||
-rw-r--r-- | npc/scripts.conf | 1 |
3 files changed, 68 insertions, 19 deletions
diff --git a/npc/functions/bitwise.txt b/npc/functions/bitwise.txt new file mode 100644 index 000000000..f7e595a27 --- /dev/null +++ b/npc/functions/bitwise.txt @@ -0,0 +1,67 @@ +// The Mana World Script +// Author: Gumi, Jesusalva +/** + * Gets a bitmasked value in from an integer. If the shift is omitted, it will + * be deduced from the mask. + * + * @arg 0 - the variable + * @arg 1 - mask + * @arg 2 - shift + */ +function script bitwise_get { + .@shift = getarg(2, 0); + + if (getargcount() < 3) { + // guess the shift from the mask: + for (.@shift = 0; .@shift < 32; ++.@shift) { + if ((getarg(1) & (1 << .@shift)) != 0) { + break; + } + } + } + + return (getarg(0) & getarg(1)) >> .@shift; +} + +/** + * sets a bitmasked value in a variable + * + * @arg 0 - the target variable + * @arg 1 - mask + * @arg 2 - shift + * @arg 3 - new value + * @return a reference to the variable + */ +function script bitwise_set { + if (getargcount() < 4) { + // guess the shift from the mask: + for (.@shift = 0; .@shift < 32; ++.@shift) { + if ((getarg(1) & (1 << .@shift)) != 0) { + break; + } + } + + return set(getarg(0), (getarg(0) & ~(getarg(1))) | (getarg(2, 0) << .@shift)); + } + + return set(getarg(0), (getarg(0) & ~(getarg(1))) | (getarg(3, 0) << getarg(2, 0))); +} + +// bitmask_count(<int>) +// returns the number of bits set in <int> (up to 4096?) +function script bitmask_count { + .@n = getarg(0); // Number evaluated + .@p=0; // Bits set/unset + .@s=0; // Stack and Check + .@m=0; // Memory + + // Loop only as needed + while (.@s < .@n) { + .@s=2**.@m; + if (.@n & .@s) + .@p++; + .@m++; + } + return .@p; +} + diff --git a/npc/functions/math.txt b/npc/functions/math.txt index 9c93fbb74..fc27bec40 100644 --- a/npc/functions/math.txt +++ b/npc/functions/math.txt @@ -102,22 +102,3 @@ function script ponderate_avg { return (.@h1+.@h2)/.@dd; } -// bitmask_count(<int>) -// returns the number of bits set in <int> (max. 4096) - -function script bitmask_count { - .@n = getarg(0); // Number evaluated - .@p=0; // Bits set/unset - .@s=0; // Stack and Check - .@m=0; // Memory - - // Loop only as needed - while (.@s < .@n) { - .@s=2**.@m; - if (.@n & .@s) - .@p++; - .@m++; - } - return .@p; -} - diff --git a/npc/scripts.conf b/npc/scripts.conf index 290624ea8..aad39c382 100644 --- a/npc/scripts.conf +++ b/npc/scripts.conf @@ -5,6 +5,7 @@ "npc/functions/string.txt", "npc/functions/main.txt", "npc/functions/array.txt", +"npc/functions/bitwise.txt", "npc/functions/math.txt", "npc/functions/util.txt", |