utils.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License v2.5
  3. --
  4. -- Copyright (c) 2013-2016, Esoteric Software
  5. -- All rights reserved.
  6. --
  7. -- You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. -- non-transferable license to use, install, execute, and perform the Spine
  9. -- Runtimes software and derivative works solely for personal or internal
  10. -- use. Without the written permission of Esoteric Software (see Section 2 of
  11. -- the Spine Software License Agreement), you may not (a) modify, translate,
  12. -- adapt, or develop new applications using the Spine Runtimes or otherwise
  13. -- create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. -- delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. -- or other intellectual property or proprietary rights notices on or in the
  16. -- Software, including any copy thereof. Redistributions in binary or source
  17. -- form must include this license and terms.
  18. --
  19. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. -- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. -- EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. -- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. -- USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. -- IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. -- POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local utils = {}
  31. local math_sqrt = math.sqrt
  32. local math_random = math.random
  33. utils.degRad = math.pi / 180
  34. function tablePrint (tt, indent, done)
  35. done = done or {}
  36. for key, value in pairs(tt) do
  37. local spaces = string.rep (" ", indent)
  38. if type(value) == "table" and not done [value] then
  39. done [value] = true
  40. print(spaces .. "{")
  41. utils.print(value, indent + 2, done)
  42. print(spaces .. "}")
  43. else
  44. io.write(spaces .. tostring(key) .. " = ")
  45. utils.print(value, indent + 2, done)
  46. end
  47. end
  48. end
  49. function utils.print (value, indent, done)
  50. indent = indent or 0
  51. if "nil" == type(value) then
  52. print(tostring(nil))
  53. elseif "table" == type(value) then
  54. local spaces = string.rep (" ", indent)
  55. print(spaces .. "{")
  56. tablePrint(value, indent + 2)
  57. print(spaces .. "}")
  58. elseif "string" == type(value) then
  59. print("\"" .. value .. "\"")
  60. else
  61. print(tostring(value))
  62. end
  63. end
  64. function utils.indexOf (haystack, needle)
  65. for i,value in ipairs(haystack) do
  66. if value == needle then return i end
  67. end
  68. return nil
  69. end
  70. function utils.copy (from, to)
  71. if not to then to = {} end
  72. for k,v in pairs(from) do
  73. to[k] = v
  74. end
  75. return to
  76. end
  77. function utils.newNumberArray (size)
  78. local a = {}
  79. local i = 1
  80. while i <= size do
  81. a[i] = 0
  82. i = i + 1
  83. end
  84. return a
  85. end
  86. function utils.newNumberArrayZero (size)
  87. local a = {}
  88. local i = 0
  89. while i < size do
  90. a[i] = 0
  91. i = i + 1
  92. end
  93. return a
  94. end
  95. function utils.setArraySize (array, size)
  96. if #array == size then return array end
  97. if #array < size then
  98. local i = #array + 1
  99. while i <= size do
  100. array[i] = 0
  101. i = i + 1
  102. end
  103. else
  104. local originalSize = #array
  105. local i = originalSize
  106. while i > size do
  107. array[i] = nil -- dirty trick to appease # without realloc
  108. i = i - 1
  109. end
  110. end
  111. return array
  112. end
  113. function utils.arrayCopy (src, srcOffset, dst, dstOffset, size)
  114. local n = srcOffset + size
  115. while srcOffset < n do
  116. dst[dstOffset] = src[srcOffset]
  117. dstOffset = dstOffset + 1
  118. srcOffset = srcOffset + 1
  119. end
  120. end
  121. function utils.clamp (value, min, max)
  122. if value < min then return min end
  123. if value > max then return max end
  124. return value
  125. end
  126. function utils.signum (value)
  127. if value < 0 then
  128. return -1
  129. elseif value > 0 then
  130. return 1
  131. else
  132. return 0
  133. end
  134. end
  135. -- Implements Java float modulo
  136. function utils.mod(a, b)
  137. if b < 0 then b = -b end
  138. if a < 0 then
  139. return -(-a % b)
  140. else
  141. return a % b
  142. end
  143. end
  144. function utils.randomTriangular(min, max)
  145. return utils.randomTriangularWith(min, max, (min + max) * 0.5)
  146. end
  147. function utils.randomTriangularWith(min, max, mode)
  148. local u = math.random()
  149. local d = max - min
  150. if (u <= (mode - min) / d) then return min + math_sqrt(u * d * (mode - min)) end
  151. return max - math_sqrt((1 - u) * d * (max - mode))
  152. end
  153. return utils