diff --git a/sources/Types/Boxes/BoxAPI.uc b/sources/Types/Boxes/BoxAPI.uc index 3143bcf..b4ae913 100644 --- a/sources/Types/Boxes/BoxAPI.uc +++ b/sources/Types/Boxes/BoxAPI.uc @@ -136,6 +136,35 @@ public final function IntArrayBox IntArray(array arrayValue) return box; } +/** + * Creates initialized box that stores an `Vector` value. + * + * @param value Value to store in the box. + * @return `VectorBox`, containing `value`. + */ +public final function VectorBox Vector(optional Vector value) +{ + local VectorBox box; + box = VectorBox(_.memory.Allocate(class'VectorBox')); + box.Initialize(value); + return box; +} + +/** + * Creates initialized box that stores an array of `Vector` values. + * Initializes it with a given array. + * + * @param arrayValue Initial array value to store in the box. + * @return `VectorArrayBox`, containing `arrayValue`. + */ +public final function VectorArrayBox VectorArray(array arrayValue) +{ + local VectorArrayBox box; + box = VectorArrayBox(_.memory.Allocate(class'VectorArrayBox')); + box.Initialize(arrayValue); + return box; +} + defaultproperties { } \ No newline at end of file diff --git a/sources/Types/Boxes/Native/VectorArrayBox.uc b/sources/Types/Boxes/Native/VectorArrayBox.uc new file mode 100644 index 0000000..bb8f782 --- /dev/null +++ b/sources/Types/Boxes/Native/VectorArrayBox.uc @@ -0,0 +1,179 @@ +/** + * This file either is or was auto-generated from the template for + * array box. + * Copyright 2022 Anton Tarasenko + *------------------------------------------------------------------------------ + * This file is part of Acedia. + * + * Acedia is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License, or + * (at your option) any later version. + * + * Acedia is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Acedia. If not, see . + */ +class VectorArrayBox extends ArrayBox; + +var protected array< Vector > value; + +// Compares an element from the array to the passed `item`. +// Does not check boundary conditions, so make sure passed index is valid. +// +// `0` means values are equal; +// `-1` means value at `index1` is strictly smaller; +// `1` means value at `index1` is strictly larger. +// ^ Only these 3 values can be returned. +private function int _compareTo(int index, Vector item) +{ + if (value[index] == item) { + return 0; + } + if (value[index].x < item.x) { + return -1; + } + if (value[index].y < item.y) { + return -1; + } + if (value[index].z < item.z) { + return -1; + } + return 1; +} + +/** + * Returns stored value. + * + * @return Value, stored in this box. + */ +public final function array< Vector > Get() +{ + return value; +} + +/** + * Initialized box value. Can only be called once. + * + * @param boxValue Value to store in this box. + * @return Reference to the caller `VectorArrayBox` to allow for method + * chaining. + */ +public final function VectorArrayBox Initialize(array< Vector > boxValue) +{ + if (IsInitialized()) return self; + value = boxValue; + MarkInitialized(); + return self; +} + +/** + * Returns current length of stored array. + * Cannot fail. + * + * @return Returns length of the stored array. Guaranteed to be non-negative. + */ +public final function int GetLength() +{ + return value.length; +} + +/** + * Returns item at `index`. If index is invalid, returns `defaultValue`. + * + * @param index Index of an item that array has to return. + * @param defaultValue Value that will be returned if either `index < 0` + * or `index >= self.GetLength()`. + * @return Either value at `index` in the caller array or `defaultValue` if + * passed `index` is invalid. + */ +public final function Vector GetItem( + int index, + optional Vector defaultValue) +{ + if (index < 0) return defaultValue; + if (index >= value.length) return defaultValue; + return value[index]; +} + +/** + * Finds first occurrence of `item` in caller `FloatArrayBox` and returns + * it's index. + * + * @param item Item to find in array. + * @return Index of first occurrence of `item` in caller `FloatArrayBox`. + * `-1` if `item` is not found. + */ +public final function int Find(Vector item) +{ + local int i; + for (i = 0; i < value.length; i += 1) + { + if (_compareTo(i, item) == 0) { + return i; + } + } + return -1; +} + +public function bool IsEqual(Object other) +{ + local int i; + local VectorArrayBox otherBox; + local array otherValue; + otherBox = VectorArrayBox(other); + if (otherBox == none) { + return false; + } + otherValue = otherBox.value; + if (value.length != otherValue.length) { + return false; + } + for (i = 0; i < value.length; i += 1) + { + if (value[i] != otherValue[i]) { + return false; + } + } + return true; +} + +protected function int CalculateHashCode() +{ + local int i; + local int result; + result = 28492904; + for (i = 0; i < value.length; i += 1) { + result = CombineHash(result, CalculateVectorHashCode(value[i])); + } + return result; +} + +private final function int CalculateVectorHashCode(Vector someVector) +{ + local int hashCode; + hashCode = CalculateFloatHashCode(someVector.x); + hashCode = CombineHash(hashCode, CalculateFloatHashCode(someVector.y)); + return CombineHash(hashCode, CalculateFloatHashCode(someVector.z)); +} + +private final function int CalculateFloatHashCode(float someFloat) +{ + local int integerPart, fractionalPart; + integerPart = Ceil(someFloat) - 1; + if (someFloat - integerPart != 0) { + fractionalPart = Ceil(1 / (someFloat - integerPart)); + } + else { + fractionalPart = -26422645; + } + return CombineHash(integerPart, fractionalPart); +} + +defaultproperties +{ +} diff --git a/sources/Types/Boxes/Native/VectorBox.uc b/sources/Types/Boxes/Native/VectorBox.uc new file mode 100644 index 0000000..75e1c3e --- /dev/null +++ b/sources/Types/Boxes/Native/VectorBox.uc @@ -0,0 +1,117 @@ +/** + * This file either is or was auto-generated from the template for + * value box. + * Copyright 2022 Anton Tarasenko + *------------------------------------------------------------------------------ + * This file is part of Acedia. + * + * Acedia is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License, or + * (at your option) any later version. + * + * Acedia is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Acedia. If not, see . + */ +class VectorBox extends ValueBox; + +var protected Vector value; + +protected function Finalizer() +{ + value = Vect(0.0, 0.0, 0.0); +} + +/** + * Returns stored value. + * + * @return Value, stored in this reference. + */ +public final function Vector Get() +{ + return value; +} + +/** + * Returns x-coordinate of the stored value. + * + * @return Value, stored in this reference. + */ +public final function float GetX() +{ + return value.x; +} + +/** + * Returns y-coordinate of the stored value. + * + * @return Value, stored in this reference. + */ +public final function float GetY() +{ + return value.y; +} + +/** + * Returns z-coordinate of the stored value. + * + * @return Value, stored in this reference. + */ +public final function float GetZ() +{ + return value.z; +} + +/** + * Initialized box value. Can only be called once. + * + * @param boxValue Value to store in this reference. + * @return Reference to the caller `VectorBox` to allow for method chaining. + */ +public final function VectorBox Initialize(Vector boxValue) +{ + if (IsInitialized()) return self; + value = boxValue; + MarkInitialized(); + return self; +} + +public function bool IsEqual(Object other) +{ + local VectorBox otherBox; + otherBox = VectorBox(other); + if (otherBox == none) { + return false; + } + return value == otherBox.value; +} + +protected function int CalculateHashCode() +{ + local int hashCode; + hashCode = CalculateFloatHashCode(value.x); + hashCode = CombineHash(hashCode, CalculateFloatHashCode(value.y)); + return CombineHash(hashCode, CalculateFloatHashCode(value.z)); +} + +private final function int CalculateFloatHashCode(float someFloat) +{ + local int integerPart, fractionalPart; + integerPart = Ceil(someFloat) - 1; + if (someFloat - integerPart != 0) { + fractionalPart = Ceil(1 / (someFloat - integerPart)); + } + else { + fractionalPart = -26422645; + } + return CombineHash(integerPart, fractionalPart); +} + +defaultproperties +{ +} \ No newline at end of file diff --git a/sources/Types/Refs/Native/VectorArrayRef.uc b/sources/Types/Refs/Native/VectorArrayRef.uc new file mode 100644 index 0000000..d2e0866 Binary files /dev/null and b/sources/Types/Refs/Native/VectorArrayRef.uc differ diff --git a/sources/Types/Refs/Native/VectorRef.uc b/sources/Types/Refs/Native/VectorRef.uc new file mode 100644 index 0000000..bfc2cd0 Binary files /dev/null and b/sources/Types/Refs/Native/VectorRef.uc differ diff --git a/sources/Types/Refs/RefAPI.uc b/sources/Types/Refs/RefAPI.uc index d271940..82ee688 100644 --- a/sources/Types/Refs/RefAPI.uc +++ b/sources/Types/Refs/RefAPI.uc @@ -169,6 +169,35 @@ public final function IntArrayRef IntArray(array arrayValue) return ref; } +/** + * Creates reference object to store an `Vector` value. + * + * @param value Initial value to store in reference. + * @return `VectorRef`, containing `value`. + */ +public final function VectorRef Vector(optional Vector value) +{ + local VectorRef ref; + ref = VectorRef(_.memory.Allocate(class'VectorRef')); + ref.Set(value); + return ref; +} + +/** + * Creates reference object to store an array of `Vector` values. + * Initializes it with a given array. + * + * @param arrayValue Initial array value to store in reference. + * @return `VectorArrayRef`, containing `arrayValue`. + */ +public final function VectorArrayRef VectorArray(array arrayValue) +{ + local VectorArrayRef ref; + ref = VectorArrayRef(_.memory.Allocate(class'VectorArrayRef')); + ref.Set(arrayValue); + return ref; +} + /** * Creates reference object to store an array of `int` values. * Initializes it with an empty array.