helpers.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ---@diagnostic disable: missing-return
  2. ---@meta
  3. ---Helper functions
  4. -------------------
  5. -- Returns a string which makes `obj` human-readable.
  6. ---@param obj any
  7. ---@param dumped string|nil Default: `{}`.
  8. ---@return string
  9. function dump(obj, dumped) end
  10. -- Returns a string which makes `obj` human-readable, handles reference loops.
  11. ---@param obj any
  12. ---@param name string|nil Default: `"_"`.
  13. ---@param dumped string|nil Default: `{}`.
  14. ---@return string
  15. function dump2(obj, name, dumped) end
  16. -- Get the hypotenuse of a triangle with legs x and y. Useful for distance
  17. -- calculation.
  18. ---@param x mt.Vector
  19. ---@param y mt.Vector
  20. ---@return number
  21. function math.hypot(x, y) end
  22. -- Get the sign of a number.
  23. --
  24. -- If the absolute value of `x` is within the `tolerance` or `x` is NaN, `0` is
  25. -- returned.
  26. ---@param x number
  27. ---@param tolerance number|nil Default: `0`
  28. ---@return -1|0|1
  29. function math.sign(x, tolerance) end
  30. ---@param x number
  31. ---@return number
  32. function math.factorial(x) end
  33. -- `"a,b":split","` returns `{"a", "b"}`.
  34. ---@param str string
  35. ---@param separator string|nil Default: `","`.
  36. ---@param include_empty boolean|nil Default: `false`.
  37. ---@param max_splits number|nil If it's negative, splits aren't limited. Default: `-1`.
  38. ---@param sep_is_pattern boolean|nil Is separator a plain string or a pattern? Default: `false`.
  39. ---@return string[]
  40. function string.split(
  41. str,
  42. separator,
  43. include_empty,
  44. max_splits,
  45. sep_is_pattern
  46. )
  47. end
  48. -- Returns the string without whitespace pre- and suffixes.
  49. -- - e.g. `"\n \t\tfoo bar\t ":trim()` returns `"foo bar"`
  50. ---@param str string
  51. ---@return string
  52. function string.trim(str) end
  53. -- Adds newlines to the string to keep it within the specified character limit
  54. --
  55. -- Note that the returned lines may be longer than the limit since it only
  56. -- splits at word borders.
  57. ---@param str string
  58. ---@param limit number Maximal amount of characters in one line.
  59. ---@param as_table boolean|nil If `true`, a table of lines instead of a string is returned, default: `false`.
  60. ---@return string|table
  61. function minetest.wrap_text(str, limit, as_table) end
  62. -- Convert a vector to human-readable string `"(X,Y,Z)"`.
  63. ---@param pos mt.Vector
  64. ---@param decimal_places number|nil If specified, the x, y and z values of the position are rounded to the given decimal place.
  65. ---@return string
  66. function minetest.pos_to_string(pos, decimal_places) end
  67. -- Convert a string like `"(X,Y,Z)"` to a vector.
  68. --
  69. -- If string can't be parsed to a position, nothing is returned.
  70. ---@return mt.Vector|nil
  71. function minetest.string_to_pos(string) end
  72. --[[
  73. Convert a string like `"(X1, Y1, Z1) (X2, Y2, Z2)"` to two vectors - box angles.
  74. - `relative_to`: Optional. If set to a position, each coordinate can use the
  75. tilde notation for relative positions.
  76. - Tilde notation: `"~"`: Relative coordinate `"~<number>"`: Relative coordinate
  77. plus `<number>`.
  78. - Example: `minetest.string_to_area("(1,2,3) (~5,~-5,~)", {x=10,y=10,z=10})`
  79. returns `{x=1,y=2,z=3}, {x=15,y=5,z=10}`.
  80. ]]
  81. ---@param str string
  82. ---@param relative_to mt.Vector|nil
  83. ---@return mt.Vector, mt.Vector
  84. function minetest.string_to_area(str, relative_to) end
  85. -- Escapes the characters `"["`, `"]"`, `"\"`, `","` and `";"`,
  86. -- which can not be used in formspecs.
  87. ---@param str string
  88. ---@return string
  89. function minetest.formspec_escape(str) end
  90. -- Returns true if passed 'y', 'yes', 'true' or a number that isn't zero.
  91. ---@param arg any
  92. ---@return boolean
  93. function minetest.is_yes(arg) end
  94. -- Returns true when the passed number represents NaN.
  95. ---@param arg any
  96. ---@return boolean
  97. function minetest.is_nan(arg) end
  98. -- Returns time with microsecond precision. May not return wall time.
  99. ---@return number
  100. function minetest.get_us_time() end
  101. -- Returns a deep copy of `t`.
  102. ---@generic T:table
  103. ---@param t T
  104. ---@return T
  105. function table.copy(t) end
  106. --[[
  107. Returns the smallest numerical index containing
  108. the value `val` in the table `list`. Non-numerical indexes are ignored. If
  109. `val` could not be found, `-1` is returned. `list` must not have negative
  110. indexes.
  111. ]]
  112. ---@param list any[]
  113. ---@param val any
  114. ---@return integer
  115. function table.indexof(list, val) end
  116. -- Appends all values in `other_table` to `table` - uses `#table + 1` to find
  117. -- new indexes.
  118. ---@param table table
  119. ---@param other_table table
  120. function table.insert_all(table, other_table) end
  121. -- Returns a table with keys and values swapped.
  122. --
  123. -- If multiple keys in `t` map to the same value, it is unspecified which value
  124. -- maps to that key.
  125. ---@param t table
  126. ---@return table swapped
  127. function table.key_value_swap(t) end
  128. -- Shuffles elements `from` to `to` in `table` in place.
  129. ---@param table table
  130. ---@param from number|nil Default: `1`
  131. ---@param to number|nil Default: `#table`
  132. ---@param random_func nil|fun(number, number?):number Default: `math.random`
  133. function table.shuffle(table, from, to, random_func) end
  134. -- Returns the exact position on the surface of a pointed node.
  135. ---@param placer mt.ObjectRef
  136. ---@param pointed_thing mt.PointedThing
  137. ---@return mt.Vector position
  138. function minetest.pointed_thing_to_face_pos(placer, pointed_thing) end
  139. --[[
  140. Simulates a tool being used once and returns the added wear, such that, if
  141. only this function is used to calculate wear, the tool will break exactly
  142. after `uses` times of uses.
  143. ]]
  144. ---@param uses number Number of times the tool can be used.
  145. ---@param initial_wear number|nil Initial wear the tool starts with (default: `0`).
  146. ---@return number
  147. function minetest.get_tool_wear_after_use(uses, initial_wear) end
  148. -- Simulates an item that digs a node.
  149. ---@param groups mt.ObjectGroups
  150. ---@param tool_capabilities mt.ToolCaps
  151. ---@param wear number|nil Amount of wear the tool starts with (default: `0`).
  152. ---@return mt.DigParams
  153. function minetest.get_dig_params(groups, tool_capabilities, wear) end
  154. ---@class mt.DigParams
  155. ---@field diggable boolean `true` if node can be dug, `false` otherwise.
  156. ---@field time number|nil Time it would take to dig the node.
  157. -- How much wear would be added to the tool (ignored for non-tools).
  158. ---@field wear number|nil
  159. --[[
  160. Groups are stored in a table, having the group names with keys and the group
  161. ratings as values. Group ratings are integer values within the range [-32767,
  162. 32767]. For example:
  163. -- Default dirt
  164. groups = {crumbly=3, soil=1}
  165. -- A more special dirt-kind of thing
  166. groups = {crumbly=2, soil=1, level=2, outerspace=1}
  167. Groups always have a rating associated with them. If there is no useful meaning
  168. for a rating for an enabled group, it shall be `1`.
  169. When not defined, the rating of a group defaults to `0`. Thus when you read
  170. groups, you must interpret `nil` and `0` as the same value, `0`.
  171. You can read the rating of a group for an item or a node by using
  172. minetest.get_item_group(itemname, groupname)
  173. ]]
  174. ---@alias mt.ObjectGroups table<string, number>
  175. -- Simulates an item that punches an object.
  176. ---@param groups mt.ObjectGroups
  177. ---@param tool_caps mt.ToolCaps
  178. ---@param last_punch_time number|nil Time in seconds since last punch action.
  179. ---@param wear number|nil Amount of wear the item starts with (default: `0`).
  180. ---@return mt.HitParams
  181. function minetest.get_hit_params(groups, tool_caps, last_punch_time, wear) end
  182. ---@class mt.HitParams
  183. ---@field hp number
  184. ---@field wear number