summaryrefslogtreecommitdiff
path: root/npc/functions/array.txt
blob: 48bafff3e843bcba0d6c74b494beea26e2b14bd2 (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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Evol Script
// Author: Gumi

// array_pad(<array>, <size>, <value>)
//     prepend or append <value> until the array is of <size> size
//     returns the amount added on success, or false (0) if nothing changed

function	script	array_pad	{
    .@index = getarrayindex(getarg(0)); // passed index
    .@count = getarraysize(getarg(0)) - .@index; // actual size
    .@size = getarg(1); // desired size
    .@absolute = (.@size >= 0 ? .@size : -(.@size)); // |size|
    .@delta = .@absolute - .@count; // amount to fill

    if (.@absolute <= .@count) {
        return false; // nothing to do
    }

    if (.@size < 0) {
        copyarray(getelementofarray(getarg(0), .@index + .@delta), getarg(0), .@count); // shift to the right
        cleararray(getarg(0), getarg(2), .@delta); // prepend
    } else {
        cleararray(getelementofarray(getarg(0), .@index + .@count), getarg(2), .@delta); // append
    }

    return .@delta;
}



// array_replace(<array>, <needle>, <replace>{, <neq>})
//     replace every occurence of <needle> with <replace>
//     returns the number of replaced elements

function	script	array_replace	{
    .@size = getarraysize(getarg(0));
    .@neq = getarg(3, false);
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
            (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
            set(getelementofarray(getarg(0), .@i), getarg(2));
            ++.@count;
        }
    }

    freeloop(false);
    return .@count;
}



// array_find(<array>, <needle>{, <neq>})
//     return the index of the first occurence of <needle> in <array>
//     if not found it returns -1

function	script	array_find	{
    .@size = getarraysize(getarg(0));
    .@neq = getarg(2, false);
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
            (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
            freeloop(false);
            return .@i;
        }
    }

    freeloop(false);
    return -1;
}



// array_rfind(<array>, <needle>{, <neq>})
//     return the index of the last occurence of <needle> in <array>
//     if not found it returns -1

function	script	array_rfind	{
    .@min = getarrayindex(getarg(0));
    .@neq = getarg(2, false);
    freeloop(true);

    for (.@i = (getarraysize(getarg(0)) - 1); .@i >= .@min; --.@i) {
        if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
            (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
            freeloop(false);
            return .@i;
        }
    }

    freeloop(false);
    return -1;
}



// array_exists(<array>, <needle>{, <neq>})
//     return true or false accordingly if <needle> is found in <array>

function	script	array_exists	{
    return array_find(getarg(0), getarg(1), getarg(2, false)) > -1;
}



// array_count(<array>, <needle>{, <neq>})
//     counts the number of occurrence of <needle> in the <array>

function	script	array_count	{
    .@size = getarraysize(getarg(0));
    .@neq = getarg(2, false);
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
            (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
            ++.@count;
        }
    }

    freeloop(false);
    return .@count;
}



// array_entries(<array>)
//     returns the number of non-empty entries

function	script	array_entries	{
    if (isstr(getarg(0)) == 1) {
        return array_count(getarg(0), "", true);
    }
    return array_count(getarg(0), 0, true);
}



// array_remove(<array>, <needle>{, <neq>})
//     removes every occurrence of <needle> in the <array> while shifting left

function	script	array_remove	{
    .@size = getarraysize(getarg(0));
    .@neq = getarg(2, false);
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        if ((.@neq && (getelementofarray(getarg(0), .@i) != getarg(1))) ||
            (!(.@neq) && (getelementofarray(getarg(0), .@i) == getarg(1)))) {
            deletearray(getelementofarray(getarg(0), .@i), 1); // shift left
            ++.@count; // increase the counter
            --.@size; // reduce the size
            --.@i; // step back
        }
    }

    freeloop(false);
    return .@count;
}



// array_reverse(<array>)
//     reverses the array

function	script	array_reverse	{
    .@index = getarrayindex(getarg(0));
    .@size = getarraysize(getarg(0));
    freeloop(true);

    for (.@i = .@index; .@i < ((.@size + .@index) / 2); ++.@i) {
        swap(getelementofarray(getarg(0), .@i), getelementofarray(getarg(0), .@size + .@index - 1 - .@i)); // a <> b
    }

    freeloop(false);
    return true;
}



// array_sum(<array>)
//     return the sum of every element of the array

function	script	array_sum	{
    .@size = getarraysize(getarg(0));
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        .@sum += getelementofarray(getarg(0), .@i);
    }

    freeloop(false);
    return .@sum;
}



// array_difference(<array>)
//     return the difference of every element of the array

function	script	array_difference	{
    .@size = getarraysize(getarg(0));
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        .@diff -= getelementofarray(getarg(0), .@i);
    }

    freeloop(false);
    return .@diff;
}



// array_avg(<array>)
//     return the average of every element of the array

function	script	array_avg	{
    .@size = getarraysize(getarg(0));
    if (!.@size) return 0;
    .@suma = array_sum(getarg(0));
    return .@suma / .@size;
}



// array_shift(<array>)
//     returns the first element of the array and removes it, while shifting left

function	script	array_shift	{
    if (isstr(getarg(0)) == 1) {
        .@val$ = getarg(0);
    } else {
        .@int = true;
        .@val = getarg(0);
    }

    deletearray(getarg(0), 1); // shift left

    return .@int ? .@val : .@val$;
}



// array_unshift(<array>, <value>)
//     adds <value> to the start of the array, while shifting right
//     returns the new size

function	script	array_unshift	{
    .@size = getarraysize(getarg(0)) + 1;
    array_pad(getarg(0), -(.@size - getarrayindex(getarg(0))), getarg(1));
    return .@size;
}



// array_pop(<array>)
//     returns the last element of the array and removes it

function	script	array_pop	{
    .@last = getarraysize(getarg(0)) - 1;

    if (isstr(getelementofarray(getarg(0), .@last)) == 1) {
        .@val$ = getelementofarray(getarg(0), .@last);
    } else {
        .@int = true;
        .@val = getelementofarray(getarg(0), .@last);
    }

    deletearray(getelementofarray(getarg(0), .@last), 1);

    return .@int ? .@val : .@val$;
}



// TODO: Rename to array_append >.<
// array_push(<array>, <value>)
//     adds <value> to the end of the array
//     returns the new size

function	script	array_push	{
    .@size = getarraysize(getarg(0));
    set(getelementofarray(getarg(0), .@size), getarg(1));
    return .@size + 1;
}



// array_shuffle(<array>)
//     shuffles the array

function	script	array_shuffle	{
    .@index = getarrayindex(getarg(0));
    .@size = getarraysize(getarg(0)) - .@index;
    freeloop(true);

    if (isstr(getarg(0)) == 1) {
        copyarray(.@tmp$[0], getarg(0), .@size);
        for (; .@size >= 1; --.@size) {
            set(getelementofarray(getarg(0), .@index + .@size - 1), array_shift(.@tmp$[rand(.@size)]));
        }
    } else {
        copyarray(.@tmp[0], getarg(0), .@size);
        for (; .@size >= 1; --.@size) {
            set(getelementofarray(getarg(0), .@index + .@size - 1), array_shift(.@tmp[rand(.@size)]));
        }
    }

    freeloop(false);
    return true;
}



// array_unique(<array>{, <threshold>})
//     allows entries to appear up to <threshold> in the array

function	script	array_unique	{
    .@size = getarraysize(getarg(0));
    .@max = getarg(1, 1);
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        .@count = 1;
        for (.@e = .@i + 1; .@e < .@size; ++.@e) {
            if (getelementofarray(getarg(0), .@i) == getelementofarray(getarg(0), .@e)) {
                if (++.@count >= .@max) {
                    deletearray(getelementofarray(getarg(0), .@e), 1);
                    ++.@removed; // increase counter
                    --.@size; // reduce size
                    --.@e; // step back
                }
            }
        }
    }

    freeloop(false);
    return .@removed;
}



// array_diff(<array1>, <array2>{, <array>...}, <array>)
//     compares array1 against one or more other arrays and fills the last array
//     with the values in array1 that are not present in any of the other arrays
//     returns the number of entries not matching

function	script	array_diff	{
    .@size = getarraysize(getarg(0));
    .@index = getarrayindex(getarg(0));
    freeloop(true);

    for (.@a = 1; .@a < (getargcount() - 1); ++.@a) {
        for (.@i = .@index; .@i < .@size; ++.@i) {
            if (!array_exists(getarg(.@a), getelementofarray(getarg(0), .@i))) {
                array_push(getarg(getargcount() - 1), getelementofarray(getarg(0), .@i));
                ++.@count;
            }
        }
    }

    freeloop(false);
    return .@count;
}



// array_filter(<array>, "<function>")
//     filters the array using a callback function

function	script	array_filter	{
    .@size = getarraysize(getarg(0));
    .@neq = getarg(2, false);
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        .@eq = callfunc(getarg(1), getelementofarray(getarg(0), .@i)) != false;
        if ((.@neq && .@eq) || (!(.@neq) && !(.@eq))) {
            deletearray(getelementofarray(getarg(0), .@i), 1); // shift left
            ++.@count; // increase the counter
            --.@size; // reduce the size
            --.@i; // step back
        }
    }

    freeloop(false);
    return .@count;
}

// array_highest(<array>)
//     Returns the index of the highest value in <array>
//     NOTE: Array must be an INT array!

function	script	array_highest	{
    .@size = getarraysize(getarg(0));
    .@win=0;
    .@idx=0;
    .@dw=false;
    freeloop(true);

    for (.@i = getarrayindex(getarg(0)); .@i < .@size; ++.@i) {
        if (getelementofarray(getarg(0), .@i) > .@win) {
            .@win=getelementofarray(getarg(0), .@i);
            .@idx=.@i;
            if (.@dw) {
                deletearray .@draw;
                .@dw=false;
            }
        } else if (getelementofarray(getarg(0), .@i) == .@win) {
            if (!.@dw)
                array_push(.@draw, .@idx);
            array_push(.@draw, .@i);
            .@dw=true;
        }
    }

    // Will we return .@idx or do we need to draw a loot?
    freeloop(false);
    if (.@dw)
        return any_of(.@draw);
    else
        return .@idx;
}

// relative_array_random(<array: 0, {[value, probability]..}>)
//     returns a random entry from the array, by relative probability
//     the first key of the array should be 0 and every entries are a tuple
//     of [value, probability]

function	script	relative_array_random	{
    .@is_str = getdatatype(getarg(0)) & DATATYPE_STR;
    .@total_prob = getelementofarray(getarg(0), 0);
    .@initial_index = getarrayindex(getarg(0));
    .@initial_index = .@initial_index ? .@initial_index : 1;
    freeloop(true);

    if (.@total_prob < 1 || getarg(1, false))
    {
        // first calculation, or forced re-calculation
        .@total_prob = 0;
        .@size = getarraysize(getarg(0));

        for (.@i = .@initial_index + 1; .@i < .@size; .@i += 2) {
            if (.@is_str) {
                .@total_prob += max(1, atoi(getelementofarray(getarg(0), .@i)));
            } else {
                .@total_prob += max(1, getelementofarray(getarg(0), .@i));
            }
        }

        // we cache on the first key
        set(getelementofarray(getarg(0), 0), .@total_prob);
    }

    .@target_sum = rand(0, .@total_prob);

    for (.@i = .@initial_index; .@sum < .@target_sum; .@i += 2) {
        if (.@is_str) {
            .@sum += atoi(getelementofarray(getarg(0), .@i + 1));
        } else {
            .@sum += getelementofarray(getarg(0), .@i + 1);
        }

        if (.@sum >= .@target_sum) {
            break;
        }
    }

    freeloop(false);
    return getelementofarray(getarg(0), .@i);
}