summaryrefslogtreecommitdiff
path: root/npc/functions/math.txt
blob: 3c2c935d938c9b47a4ce1935d690749575f74416 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Evol functions.
// Authors:
//    4144
//    Reid
// Description:
//    Math functions


// abs(<int>)
//    returns the absolute value of the passed integer

function	script	abs	{
    .@n = getarg(0);
    return .@n >= 0 ? .@n : -.@n;
}



// lognbaselvl({<multiplicator>{, <min value>}})
//     returns BaseLevel * logn (BaseLevel * alpha).

function	script	lognbaselvl	{
    .@alpha = getarg(0, 1);
    .@min = getarg(1, 1);
    .@ret = 0;
    .@pc_level = BaseLevel * .@alpha;

    while (.@pc_level >>= 1)
    {
        ++.@ret;
    }
    .@ret *= BaseLevel;

    if (.@ret <= .@min)
    {
        .@ret = .@min;
    }

    return .@ret;
}

// log2(<int>)
//    returns the log base 2 of the passed integer, up to 20 (2**20=1.048.576) (round down always)

function	script	log2	{
    .@v=abs(getarg(0));
    .@ok=0;
    .@i=0;

    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;
        }
    }
    freeloop(false);

    return .@i;
}