forked from Adeptus-Dominus/ChapterMaster
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscr_array_functions.gml
More file actions
85 lines (72 loc) · 1.92 KB
/
scr_array_functions.gml
File metadata and controls
85 lines (72 loc) · 1.92 KB
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
function array_iter_length(choice_array, offset, length){
if (length == 0){
if (offset==0){
length = array_length(choice_array);
}
else if (offset){
length = array_length(choice_array) - offset;
} else {
length = offset*-1;
}
}
return length;
}
function array_sum(choice_array,start_value=0, offset=0,length=0){
function arraysum(_prev, _curr, _index) {
return _prev + _curr;
}
length = array_iter_length(choice_array,offset,length);
return array_reduce(choice_array,arraysum,start_value,offset,length)
}
function array_set_value(choice_array, value){
for (i=0;i<array_length(choice_array);i++){
choice_array[@ i] = value;
}
}
function array_replace_value(choice_array, value, r_value){
for (i=0;i<array_length(choice_array);i++){
if (choice_array[i] == value ){
choice_array[@ i] = r_value;
}
}
}
function array_random_element(choice_array){
return choice_array[irandom(array_length(choice_array) - 1)];
}
function array_random_index(choice_array){
return irandom(array_length(choice_array) - 1);
}
function array_combine_strings(arr) {
var uniqueItems = [];
var itemCounts = [];
for (var i = 0; i < array_length(arr); i++) {
var item = arr[i];
if (!array_contains(uniqueItems, item)) {
array_push(uniqueItems, item);
array_push(itemCounts, 1);
} else {
var index = array_get_index(uniqueItems, item);
itemCounts[index]++;
}
}
var result = "";
var _array_length = array_length(uniqueItems);
for (var j = 0; j < _array_length; j++) {
var item = uniqueItems[j];
if (itemCounts[j] > 1) {
item = string_plural(item);
}
if (_array_length > 1) {
if (j == _array_length - 1) {
result += " and " + item;
} else if (j == _array_length - 2){
result += item;
} else {
result += item + ", ";
}
} else {
result = item;
}
}
return result;
}