summaryrefslogtreecommitdiff
path: root/npc
diff options
context:
space:
mode:
authorFedja Beader <fedja@protonmail.ch>2025-01-29 01:11:10 +0100
committerJesusalva Jesusalva <jesusalva@tmw2.org>2025-05-03 14:41:08 +0000
commit6b41e4b8d247aef07bea9b5826a84db9946770b9 (patch)
tree5bc188790d05159c68aac5c3be575b1050f95d27 /npc
parentdf8cce5728bdf0a3155e6412a39cd7544570a1db (diff)
downloadserverdata-6b41e4b8d247aef07bea9b5826a84db9946770b9.tar.gz
serverdata-6b41e4b8d247aef07bea9b5826a84db9946770b9.tar.bz2
serverdata-6b41e4b8d247aef07bea9b5826a84db9946770b9.tar.xz
serverdata-6b41e4b8d247aef07bea9b5826a84db9946770b9.zip
Simplify base 2 logarithm computation
Diffstat (limited to 'npc')
-rw-r--r--npc/functions/math.txt32
1 files changed, 9 insertions, 23 deletions
diff --git a/npc/functions/math.txt b/npc/functions/math.txt
index 22cf88159..d54558352 100644
--- a/npc/functions/math.txt
+++ b/npc/functions/math.txt
@@ -40,31 +40,17 @@ function script lognbaselvl {
}
// log2(<int>)
-// returns the log base 2 of the passed integer, up to 20 (2**20=1.048.576) (round down always)
-
+// returns the base 2 logarithm of the passed integer
+// Note: Negative values are first converted to positive.
+// For 0, -1 is returned instead
+// (as if passed value == 0.5, but that is not possible for integers).
function script log2 {
- .@v=abs(getarg(0));
- .@ok=0;
- .@i=0;
- if (.@v < 1)
- return -1;
-
- freeloop(true);
- while (!.@ok) {
- // exact match
- if (2**.@i == .@v) {
- .@ok=1;
- // inexact match, or limit exceeded
- } else if (2**.@i >= .@v || .@i > 20) {
- .@ok=1;
- .@i-=1; // round down
- // not yet
- } else {
- .@i+=1;
- }
+ .@v = abs(getarg(0));
+ .@i = -1;
+ while (.@v > 0) {
+ .@i += 1;
+ .@v >>= 1;
}
- freeloop(false);
-
return .@i;
}