2023-01-09 03:14:20 +01:00
|
|
|
function array_create_from_list(list) {
|
2023-02-14 07:37:13 +01:00
|
|
|
if(list == undefined) return [];
|
|
|
|
if(!ds_exists(list, ds_type_list)) return [];
|
|
|
|
|
2023-01-09 03:14:20 +01:00
|
|
|
var arr = array_create(ds_list_size(list));
|
|
|
|
for( var i = 0; i < ds_list_size(list); i++ )
|
|
|
|
arr[i] = list[| i];
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2023-06-10 13:59:45 +02:00
|
|
|
function array_safe_set(arr, index, value, fill = 0) {
|
|
|
|
if(!is_array(arr)) return arr;
|
|
|
|
if(index < 0) return arr;
|
|
|
|
if(index >= array_length(arr)) {
|
|
|
|
var i = array_length(arr);
|
|
|
|
for(; i <= index; i++ )
|
|
|
|
arr[i] = fill;
|
|
|
|
arr[index] = value;
|
|
|
|
return arr;
|
|
|
|
}
|
2022-12-21 02:30:23 +01:00
|
|
|
|
|
|
|
array_set(arr, index, value);
|
2023-06-10 13:59:45 +02:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function array_resize_fill(arr, size, fill = 0) {
|
|
|
|
if(size < array_length(arr)) {
|
|
|
|
array_resize(arr, size);
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i = array_length(arr);
|
|
|
|
for(; i < size; i++)
|
|
|
|
arr[i] = fill;
|
|
|
|
return arr;
|
2022-12-21 02:30:23 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 07:37:13 +01:00
|
|
|
enum ARRAY_OVERFLOW {
|
|
|
|
_default,
|
|
|
|
loop
|
|
|
|
}
|
2023-03-13 10:45:56 +01:00
|
|
|
|
2023-02-14 07:37:13 +01:00
|
|
|
function array_safe_get(arr, index, def = 0, overflow = ARRAY_OVERFLOW._default) {
|
2023-04-16 16:24:17 +02:00
|
|
|
gml_pragma("forceinline");
|
2023-02-14 07:37:13 +01:00
|
|
|
if(!is_array(arr)) return def;
|
|
|
|
|
|
|
|
if(overflow == ARRAY_OVERFLOW.loop) {
|
2023-03-07 14:29:47 +01:00
|
|
|
var len = array_length(arr);
|
2023-02-14 07:37:13 +01:00
|
|
|
if(index < 0)
|
2023-03-07 14:29:47 +01:00
|
|
|
index = len - safe_mod(abs(index), len);
|
|
|
|
index = safe_mod(index, len);
|
2023-02-14 07:37:13 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 08:11:55 +01:00
|
|
|
if(index < 0) return def;
|
2022-09-27 06:37:28 +02:00
|
|
|
if(index >= array_length(arr)) return def;
|
2022-01-13 05:24:03 +01:00
|
|
|
return arr[index];
|
2022-09-27 06:37:28 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 11:09:15 +02:00
|
|
|
function array_push_create(arr, val) {
|
|
|
|
if(!is_array(arr)) return [ val ];
|
|
|
|
array_push(arr, val);
|
|
|
|
return arr;
|
|
|
|
}
|
2023-03-13 10:45:56 +01:00
|
|
|
|
2022-09-27 06:37:28 +02:00
|
|
|
function array_exists(arr, val) {
|
|
|
|
for( var i = 0; i < array_length(arr); i++ ) {
|
2023-04-11 20:29:20 +02:00
|
|
|
if(isEqual(arr[i], val)) return true;
|
2022-09-27 06:37:28 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-28 06:58:28 +02:00
|
|
|
function array_empty(arr) {
|
|
|
|
return array_length(arr) == 0;
|
|
|
|
}
|
|
|
|
|
2022-09-27 06:37:28 +02:00
|
|
|
function array_find(arr, val) {
|
|
|
|
for( var i = 0; i < array_length(arr); i++ ) {
|
2023-04-11 20:29:20 +02:00
|
|
|
if(isEqual(arr[i], val)) return i;
|
2022-09-27 06:37:28 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function array_remove(arr, val) {
|
|
|
|
if(!array_exists(arr, val)) return;
|
|
|
|
var ind = array_find(arr, val);
|
|
|
|
array_delete(arr, ind, 1);
|
2022-12-12 09:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function array_push_unique(arr, val) {
|
|
|
|
if(array_exists(arr, val)) return;
|
|
|
|
array_push(arr, val);
|
2022-12-13 14:11:39 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 11:31:33 +01:00
|
|
|
|
|
|
|
function array_insert_unique(arr, ind, val) {
|
|
|
|
if(array_exists(arr, val)) return;
|
|
|
|
array_insert(arr, ind, val);
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:11:39 +01:00
|
|
|
function array_append(arr, arr0) {
|
|
|
|
for( var i = 0; i < array_length(arr0); i++ )
|
|
|
|
array_push(arr, arr0[i]);
|
2023-02-14 07:37:13 +01:00
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function array_shuffle(arr) {
|
|
|
|
var r = array_length(arr) - 1;
|
|
|
|
for(var i = 0; i < r; i += 1) {
|
|
|
|
var j = irandom_range(i,r);
|
|
|
|
temp = arr[i];
|
|
|
|
arr[i] = arr[j];
|
|
|
|
arr[j] = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
2022-12-13 14:11:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function array_merge() {
|
|
|
|
var arr = [];
|
|
|
|
for( var i = 0; i < argument_count; i++ ) {
|
|
|
|
array_append(arr, argument[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
2022-12-16 09:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function array_clone(arr) {
|
2023-02-14 13:44:46 +01:00
|
|
|
if(!is_array(arr))
|
|
|
|
return arr;
|
|
|
|
|
|
|
|
var _res = [];
|
|
|
|
for( var i = 0; i < array_length(arr); i++ )
|
|
|
|
_res[i] = array_clone(arr[i]);
|
2022-12-16 09:18:09 +01:00
|
|
|
return _res;
|
2023-01-09 03:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function array_min(arr) {
|
|
|
|
if(array_length(arr) == 0) return 0;
|
|
|
|
|
|
|
|
var mn = arr[0];
|
|
|
|
for( var i = 0; i < array_length(arr); i++ )
|
|
|
|
mn = min(mn, arr[i]);
|
|
|
|
return mn;
|
|
|
|
}
|
|
|
|
|
|
|
|
function array_max(arr) {
|
|
|
|
if(array_length(arr) == 0) return 0;
|
|
|
|
|
|
|
|
var mx = arr[0];
|
|
|
|
for( var i = 0; i < array_length(arr); i++ )
|
|
|
|
mx = max(mx, arr[i]);
|
|
|
|
return mx;
|
2023-01-25 06:49:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function array_get_dimension(arr) {
|
|
|
|
return is_array(arr)? array_length(arr) : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function array_shape(arr, first = true, isSurface = false) {
|
|
|
|
if(!is_array(arr)) {
|
|
|
|
if(isSurface && is_surface(arr))
|
|
|
|
return (first? "" : " x ") + string(surface_get_width(arr)) + " x " + string(surface_get_height(arr)) + " px";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
var dim = string(array_length(arr));
|
|
|
|
|
|
|
|
if(array_length(arr))
|
|
|
|
dim += array_shape(arr[0], false, isSurface);
|
|
|
|
|
|
|
|
return (first? "" : " x ") + dim;
|
2023-02-14 13:44:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function array_spread(arr, _arr = []) {
|
|
|
|
if(!is_array(arr)) {
|
|
|
|
array_push(_arr, arr);
|
|
|
|
return _arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( var i = 0; i < array_length(arr); i++ )
|
|
|
|
array_spread(arr[i], _arr);
|
|
|
|
|
|
|
|
return _arr;
|
2022-01-13 05:24:03 +01:00
|
|
|
}
|