cuboid.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. minetest.register_chatcommand("/outset", {
  2. params = "[h|v] <amount>",
  3. description = "outset the selection",
  4. privs = {worldedit=true},
  5. func = function(name, param)
  6. local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
  7. if find == nil then
  8. return false, "invalid usage: " .. param
  9. end
  10. local pos1 = worldedit.pos1[name]
  11. local pos2 = worldedit.pos2[name]
  12. if pos1 == nil or pos2 == nil then
  13. return false,
  14. "Undefined region. Region must be defined beforehand."
  15. end
  16. local hv_test = dir:find("[^hv]+")
  17. if hv_test ~= nil then
  18. return false, "Invalid direction."
  19. end
  20. if dir == "" or dir == "hv" or dir == "vh" then
  21. assert(worldedit.cuboid_volumetric_expand(name, amount))
  22. elseif dir == "h" then
  23. assert(worldedit.cuboid_linear_expand(name, 'x', 1, amount))
  24. assert(worldedit.cuboid_linear_expand(name, 'x', -1, amount))
  25. assert(worldedit.cuboid_linear_expand(name, 'z', 1, amount))
  26. assert(worldedit.cuboid_linear_expand(name, 'z', -1, amount))
  27. elseif dir == "v" then
  28. assert(worldedit.cuboid_linear_expand(name, 'y', 1, amount))
  29. assert(worldedit.cuboid_linear_expand(name, 'y', -1, amount))
  30. else
  31. return false, "Invalid number of arguments"
  32. end
  33. worldedit.marker_update(name)
  34. return true, "Region outset by " .. amount .. " blocks"
  35. end,
  36. }
  37. )
  38. minetest.register_chatcommand("/inset", {
  39. params = "[h|v] <amount>",
  40. description = "inset the selection",
  41. privs = {worldedit=true},
  42. func = function(name, param)
  43. local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
  44. if find == nil then
  45. return false, "invalid usage: " .. param
  46. end
  47. local pos1 = worldedit.pos1[name]
  48. local pos2 = worldedit.pos2[name]
  49. if pos1 == nil or pos2 == nil then
  50. return false,
  51. "Undefined region. Region must be defined beforehand."
  52. end
  53. local hv_test = dir:find("[^hv]+")
  54. if hv_test ~= nil then
  55. return false, "Invalid direction."
  56. end
  57. if dir == "" or dir == "vh" or dir == "hv" then
  58. assert(worldedit.cuboid_volumetric_expand(name, -amount))
  59. elseif dir == "h" then
  60. assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount))
  61. assert(worldedit.cuboid_linear_expand(name, 'x', -1, -amount))
  62. assert(worldedit.cuboid_linear_expand(name, 'z', 1, -amount))
  63. assert(worldedit.cuboid_linear_expand(name, 'z', -1, -amount))
  64. elseif dir == "v" then
  65. assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount))
  66. assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount))
  67. else
  68. return false, "Invalid number of arguments"
  69. end
  70. worldedit.marker_update(name)
  71. return true, "Region inset by " .. amount .. " blocks"
  72. end,
  73. }
  74. )
  75. minetest.register_chatcommand("/shift", {
  76. params = "[x|y|z|?|up|down|left|right|front|back] [+|-]<amount>",
  77. description = "Moves the selection region. Does not move contents.",
  78. privs = {worldedit=true},
  79. func = function(name, param)
  80. local pos1 = worldedit.pos1[name]
  81. local pos2 = worldedit.pos2[name]
  82. local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)")
  83. if find == nil then
  84. worldedit.player_notify(name, "invalid usage: " .. param)
  85. return
  86. end
  87. if pos1 == nil or pos2 == nil then
  88. worldedit.player_notify(name,
  89. "Undefined region. Region must be defined beforehand.")
  90. return
  91. end
  92. local axis, dir
  93. if direction == "x" or direction == "y" or direction == "z" then
  94. axis, dir = direction, 1
  95. elseif direction == "?" then
  96. axis, dir = worldedit.player_axis(name)
  97. else
  98. axis, dir = worldedit.translate_direction(name, direction)
  99. end
  100. if axis == nil or dir == nil then
  101. return false, "Invalid if looking straight up or down"
  102. end
  103. assert(worldedit.cuboid_shift(name, axis, amount * dir))
  104. worldedit.marker_update(name)
  105. return true, "Region shifted by " .. amount .. " nodes"
  106. end,
  107. }
  108. )
  109. minetest.register_chatcommand("/expand", {
  110. params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
  111. description = "expand the selection in one or two directions at once",
  112. privs = {worldedit=true},
  113. func = function(name, param)
  114. local find, _, sign, direction, amount,
  115. rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
  116. if find == nil then
  117. worldedit.player_notify(name, "invalid use: " .. param)
  118. return
  119. end
  120. if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
  121. worldedit.player_notify(name,
  122. "Undefined region. Region must be defined beforehand.")
  123. return
  124. end
  125. local absolute = direction:find("[xyz?]")
  126. local dir, axis
  127. if rev_amount == "" then
  128. rev_amount = 0
  129. end
  130. if absolute == nil then
  131. axis, dir = worldedit.translate_direction(name, direction)
  132. if axis == nil or dir == nil then
  133. return false, "Invalid if looking straight up or down"
  134. end
  135. else
  136. if direction == "?" then
  137. axis, dir = worldedit.player_axis(name)
  138. else
  139. axis = direction
  140. dir = 1
  141. end
  142. end
  143. if sign == "-" then
  144. dir = -dir
  145. end
  146. worldedit.cuboid_linear_expand(name, axis, dir, amount)
  147. worldedit.cuboid_linear_expand(name, axis, -dir, rev_amount)
  148. worldedit.marker_update(name)
  149. return true, "Region expanded by " .. (amount + rev_amount) .. " nodes"
  150. end,
  151. }
  152. )
  153. minetest.register_chatcommand("/contract", {
  154. params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
  155. description = "contract the selection in one or two directions at once",
  156. privs = {worldedit=true},
  157. func = function(name, param)
  158. local find, _, sign, direction, amount,
  159. rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
  160. if find == nil then
  161. worldedit.player_notify(name, "invalid use: " .. param)
  162. return
  163. end
  164. if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
  165. worldedit.player_notify(name,
  166. "Undefined region. Region must be defined beforehand.")
  167. return
  168. end
  169. local absolute = direction:find("[xyz?]")
  170. local dir, axis
  171. if rev_amount == "" then
  172. rev_amount = 0
  173. end
  174. if absolute == nil then
  175. axis, dir = worldedit.translate_direction(name, direction)
  176. if axis == nil or dir == nil then
  177. return false, "Invalid if looking straight up or down"
  178. end
  179. else
  180. if direction == "?" then
  181. axis, dir = worldedit.player_axis(name)
  182. else
  183. axis = direction
  184. dir = 1
  185. end
  186. end
  187. if sign == "-" then
  188. dir = -dir
  189. end
  190. worldedit.cuboid_linear_expand(name, axis, dir, -amount)
  191. worldedit.cuboid_linear_expand(name, axis, -dir, -rev_amount)
  192. worldedit.marker_update(name)
  193. return true, "Region contracted by " .. (amount + rev_amount) .. " nodes"
  194. end,
  195. }
  196. )