cuboid.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. -- Expands or contracts the cuboid in all axes by amount (positive or negative)
  2. worldedit.cuboid_volumetric_expand = function(name, amount)
  3. local pos1 = worldedit.pos1[name]
  4. local pos2 = worldedit.pos2[name]
  5. if pos1 == nil or pos2 == nil then
  6. return false, "Undefined cuboid"
  7. end
  8. local delta1 = vector.new()
  9. local delta2 = vector.new()
  10. local delta_dir1
  11. local delta_dir2
  12. delta1 = vector.add(delta1, amount)
  13. delta2 = vector.add(delta2, amount)
  14. delta_dir1, delta_dir2 = worldedit.get_expansion_directions(pos1, pos2)
  15. delta1 = vector.multiply(delta1, delta_dir1)
  16. delta2 = vector.multiply(delta2, delta_dir2)
  17. worldedit.pos1[name] = vector.add(pos1, delta1)
  18. worldedit.pos2[name] = vector.add(pos2, delta2)
  19. return true
  20. end
  21. -- Expands or contracts the cuboid in a single axis by amount (positive or negative)
  22. worldedit.cuboid_linear_expand = function(name, axis, direction, amount)
  23. local pos1 = worldedit.pos1[name]
  24. local pos2 = worldedit.pos2[name]
  25. if pos1 == nil or pos2 == nil then
  26. return false, "undefined cuboid"
  27. end
  28. if direction ~= 1 and direction ~= -1 then
  29. return false, "invalid marker"
  30. end
  31. local marker = worldedit.marker_get_closest_to_axis(name, axis, direction)
  32. local deltavect = vector.new()
  33. if axis == 'x' then
  34. deltavect.x = amount * direction
  35. elseif axis == 'y' then
  36. deltavect.y = amount * direction
  37. elseif axis == 'z' then
  38. deltavect.z = amount * direction
  39. else
  40. return false, "invalid axis"
  41. end
  42. worldedit.marker_move(name, marker, deltavect)
  43. return true
  44. end
  45. -- Shifts the cuboid by '+-amount' in axis 'axis'
  46. worldedit.cuboid_shift = function(name, axis, amount)
  47. local pos1 = worldedit.pos1[name]
  48. local pos2 = worldedit.pos2[name]
  49. if pos1 == nil or pos2 == nil then
  50. return false, "undefined cuboid"
  51. end
  52. if axis == 'x' then
  53. worldedit.pos1[name].x = pos1.x + amount
  54. worldedit.pos2[name].x = pos2.x + amount
  55. elseif axis == 'y' then
  56. worldedit.pos1[name].y = pos1.y + amount
  57. worldedit.pos2[name].y = pos2.y + amount
  58. elseif axis == 'z' then
  59. worldedit.pos1[name].z = pos1.z + amount
  60. worldedit.pos2[name].z = pos2.z + amount
  61. else
  62. return false, "invalid axis"
  63. end
  64. return true
  65. end
  66. -- Moves the location of a single marker by adding deltavector
  67. worldedit.marker_move = function(name, marker, deltavector)
  68. if marker ~= 1 and marker ~= 2 then
  69. return false
  70. end
  71. if marker == 1 then
  72. local pos = worldedit.pos1[name]
  73. worldedit.pos1[name] = vector.add(deltavector, pos)
  74. else
  75. local pos = worldedit.pos2[name]
  76. worldedit.pos2[name] = vector.add(deltavector, pos)
  77. end
  78. return true
  79. end
  80. -- Returns two vectors with the directions for volumetric expansion
  81. worldedit.get_expansion_directions = function(mark1, mark2)
  82. if mark1 == nil or mark2 == nil then
  83. return
  84. end
  85. local dir1 = vector.new()
  86. local dir2 = vector.new()
  87. if mark1.x < mark2.x then
  88. dir1.x = -1
  89. dir2.x = 1
  90. else
  91. dir1.x = 1
  92. dir2.x = -1
  93. end
  94. if mark1.y < mark2.y then
  95. dir1.y = -1
  96. dir2.y = 1
  97. else
  98. dir1.y = 1
  99. dir2.y = -1
  100. end
  101. if mark1.z < mark2.z then
  102. dir1.z = -1
  103. dir2.z = 1
  104. else
  105. dir1.z = 1
  106. dir2.z = -1
  107. end
  108. return dir1, dir2
  109. end
  110. -- Return the marker that is closest to the player
  111. worldedit.marker_get_closest_to_player = function(name)
  112. local playerpos = minetest.get_player_by_name(name):get_pos()
  113. local dist1 = vector.distance(playerpos, worldedit.pos1[name])
  114. local dist2 = vector.distance(playerpos, worldedit.pos2[name])
  115. if dist1 < dist2 then
  116. return 1
  117. else
  118. return 2
  119. end
  120. end
  121. -- Returns the closest marker to the specified axis and direction
  122. worldedit.marker_get_closest_to_axis = function(name, axis, direction)
  123. local pos1 = vector.new()
  124. local pos2 = vector.new()
  125. if direction ~= 1 and direction ~= -1 then
  126. return nil
  127. end
  128. if axis == 'x' then
  129. pos1.x = worldedit.pos1[name].x * direction
  130. pos2.x = worldedit.pos2[name].x * direction
  131. if pos1.x > pos2.x then
  132. return 1
  133. else
  134. return 2
  135. end
  136. elseif axis == 'y' then
  137. pos1.y = worldedit.pos1[name].y * direction
  138. pos2.y = worldedit.pos2[name].y * direction
  139. if pos1.y > pos2.y then
  140. return 1
  141. else
  142. return 2
  143. end
  144. elseif axis == 'z' then
  145. pos1.z = worldedit.pos1[name].z * direction
  146. pos2.z = worldedit.pos2[name].z * direction
  147. if pos1.z > pos2.z then
  148. return 1
  149. else
  150. return 2
  151. end
  152. else
  153. return nil
  154. end
  155. end
  156. -- Translates up, down, left, right, front, back to their corresponding axes and
  157. -- directions according to faced direction
  158. worldedit.translate_direction = function(name, direction)
  159. local axis, dir = worldedit.player_axis(name)
  160. local resaxis, resdir
  161. if direction == "up" then
  162. return 'y', 1
  163. end
  164. if direction == "down" then
  165. return 'y', -1
  166. end
  167. if direction == "front" then
  168. if axis == "y" then
  169. resaxis = nil
  170. resdir = nil
  171. else
  172. resaxis = axis
  173. resdir = dir
  174. end
  175. end
  176. if direction == "back" then
  177. if axis == "y" then
  178. resaxis = nil
  179. resdir = nil
  180. else
  181. resaxis = axis
  182. resdir = -dir
  183. end
  184. end
  185. if direction == "left" then
  186. if axis == 'x' then
  187. resaxis = 'z'
  188. resdir = dir
  189. elseif axis == 'z' then
  190. resaxis = 'x'
  191. resdir = -dir
  192. end
  193. end
  194. if direction == "right" then
  195. if axis == 'x' then
  196. resaxis = 'z'
  197. resdir = -dir
  198. elseif axis == 'z' then
  199. resaxis = 'x'
  200. resdir = dir
  201. end
  202. end
  203. return resaxis, resdir
  204. end