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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// 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;
}
/////////////////////////////////////////////////////////////////////////////////
// A Nibble can go up to 15. There are 7 nibbles.
// get_nibble(VAR, NIBBLEID)
function script get_nibble {
.@v=getarg(0);
switch (getarg(1)) {
case 0:
.@s=0; .@m=0xF; break;
case 1:
.@s=4; .@m=0xF0; break;
case 2:
.@s=8; .@m=0xF00; break;
case 3:
.@s=12; .@m=0xF000; break;
case 4:
.@s=16; .@m=0xF0000; break;
case 5:
.@s=20; .@m=0xF00000; break;
case 6:
.@s=24; .@m=0xF000000; break;
default:
Exception("Invalid Nibble: "+getarg(1), RB_DEFAULT, .@v);
}
return bitwise_get(.@v, .@m, .@s);
}
// A Byte can go up to 255. There are 3 bytes. The forth can go up to 127.
// get_nibble(VAR, BYTEID)
function script get_byte {
.@v=getarg(0);
switch (getarg(1)) {
case 0:
.@s=0; .@m=0xFF; break;
case 1:
.@s=8; .@m=0xFF00; break;
case 2:
.@s=16; .@m=0xFF0000; break;
case 3:
.@s=24; .@m=0x7F000000; break;
default:
Exception("Invalid Byte: "+getarg(1), RB_DEFAULT, .@v);
}
return bitwise_get(.@v, .@m, .@s);
}
// A Bitword can go up to 65535 and is fixed in position to handle Soul EXP.
// get_bitword(VAR)
function script get_bitword {
.@v=getarg(0);
return bitwise_get(.@v, 0xFFFF, 0);
}
/////////////////////////////////////////////////////////////////////////////////
// A Nibble can go up to 15. There are 7 nibbles.
// set_nibble(VAR, NIBBLEID, VAL)
function script set_nibble {
.@v=getarg(0);
switch (getarg(1)) {
case 0:
.@s=0; .@m=0xF; break;
case 1:
.@s=4; .@m=0xF0; break;
case 2:
.@s=8; .@m=0xF00; break;
case 3:
.@s=12; .@m=0xF000; break;
case 4:
.@s=16; .@m=0xF0000; break;
case 5:
.@s=20; .@m=0xF00000; break;
case 6:
.@s=24; .@m=0xF000000; break;
default:
Exception("Invalid SNibble: "+getarg(1), RB_DEFAULT);
}
return bitwise_set(getarg(0), .@m, .@s, getarg(2));
}
// A Byte can go up to 255. There are 3 bytes. The forth can go up to 127.
// set_nibble(VAR, BYTEID, VAL)
function script set_byte {
.@v=getarg(0);
switch (getarg(1)) {
case 0:
.@s=0; .@m=0xFF; break;
case 1:
.@s=8; .@m=0xFF00; break;
case 2:
.@s=16; .@m=0xFF0000; break;
case 3:
.@s=24; .@m=0x7F000000; break;
default:
Exception("Invalid SByte: "+getarg(1), RB_DEFAULT);
}
return bitwise_set(getarg(0), .@m, .@s, getarg(2));
}
// A Bitword can go up to 65535 and is fixed in position to handle Soul EXP.
// set_bitword(VAR, VAL)
function script set_bitword {
.@v=getarg(0);
return bitwise_set(getarg(0), 0xFFFF, 0, getarg(1));
}
|