summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgumi <git@gumi.ca>2020-08-10 11:44:41 -0400
committergumi <git@gumi.ca>2020-08-10 11:44:41 -0400
commit4c8112de3e1caf4e50c82f4ecb68295ecc5b31a4 (patch)
treead1790c94e6b023a007248e9f8169edfe5969118
parent08592a634e3786f761afae1432c85b85a86c838e (diff)
downloadserverdata-4c8112de3e1caf4e50c82f4ecb68295ecc5b31a4.tar.gz
serverdata-4c8112de3e1caf4e50c82f4ecb68295ecc5b31a4.tar.bz2
serverdata-4c8112de3e1caf4e50c82f4ecb68295ecc5b31a4.tar.xz
serverdata-4c8112de3e1caf4e50c82f4ecb68295ecc5b31a4.zip
allow to omit the shift parameter for bitwise operations
-rw-r--r--npc/functions/bitwise.txt27
1 files changed, 25 insertions, 2 deletions
diff --git a/npc/functions/bitwise.txt b/npc/functions/bitwise.txt
index 58ad8b5c..8cb6a426 100644
--- a/npc/functions/bitwise.txt
+++ b/npc/functions/bitwise.txt
@@ -1,12 +1,24 @@
/**
- * gets a bitmasked value in a variable
+ * 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 {
- return (getarg(0) & getarg(1)) >> getarg(2, 0);
+ .@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;
}
/**
@@ -19,5 +31,16 @@ function script bitwise_get {
* @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)));
}