mark.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. worldedit.marker1 = {}
  2. worldedit.marker2 = {}
  3. worldedit.marker_region = {}
  4. local init_sentinel = "new" .. tostring(math.random(99999))
  5. --marks worldedit region position 1
  6. worldedit.mark_pos1 = function(name, region_too)
  7. local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
  8. if worldedit.marker1[name] ~= nil then --marker already exists
  9. worldedit.marker1[name]:remove() --remove marker
  10. worldedit.marker1[name] = nil
  11. end
  12. if pos1 ~= nil then
  13. --make area stay loaded
  14. local manip = minetest.get_voxel_manip()
  15. manip:read_from_map(pos1, pos1)
  16. --add marker
  17. worldedit.marker1[name] = minetest.add_entity(pos1, "worldedit:pos1", init_sentinel)
  18. if worldedit.marker1[name] ~= nil then
  19. worldedit.marker1[name]:get_luaentity().player_name = name
  20. end
  21. end
  22. if region_too == nil or region_too then
  23. worldedit.mark_region(name)
  24. end
  25. end
  26. --marks worldedit region position 2
  27. worldedit.mark_pos2 = function(name, region_too)
  28. local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
  29. if worldedit.marker2[name] ~= nil then --marker already exists
  30. worldedit.marker2[name]:remove() --remove marker
  31. worldedit.marker2[name] = nil
  32. end
  33. if pos2 ~= nil then
  34. --make area stay loaded
  35. local manip = minetest.get_voxel_manip()
  36. manip:read_from_map(pos2, pos2)
  37. --add marker
  38. worldedit.marker2[name] = minetest.add_entity(pos2, "worldedit:pos2", init_sentinel)
  39. if worldedit.marker2[name] ~= nil then
  40. worldedit.marker2[name]:get_luaentity().player_name = name
  41. end
  42. end
  43. if region_too == nil or region_too then
  44. worldedit.mark_region(name)
  45. end
  46. end
  47. worldedit.mark_region = function(name)
  48. local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
  49. if worldedit.marker_region[name] ~= nil then --marker already exists
  50. for _, entity in ipairs(worldedit.marker_region[name]) do
  51. entity:remove()
  52. end
  53. worldedit.marker_region[name] = nil
  54. end
  55. if pos1 ~= nil and pos2 ~= nil then
  56. local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
  57. local vec = vector.subtract(pos2, pos1)
  58. local maxside = math.max(vec.x, math.max(vec.y, vec.z))
  59. local limit = tonumber(minetest.settings:get("active_object_send_range_blocks")) * 16
  60. if maxside > limit * 1.5 then
  61. -- The client likely won't be able to see the plane markers as intended anyway,
  62. -- thus don't place them and also don't load the area into memory
  63. return
  64. end
  65. local thickness = 0.2
  66. local sizex, sizey, sizez = (1 + pos2.x - pos1.x) / 2, (1 + pos2.y - pos1.y) / 2, (1 + pos2.z - pos1.z) / 2
  67. --make area stay loaded
  68. local manip = minetest.get_voxel_manip()
  69. manip:read_from_map(pos1, pos2)
  70. local markers = {}
  71. --XY plane markers
  72. for _, z in ipairs({pos1.z - 0.5, pos2.z + 0.5}) do
  73. local entpos = {x=pos1.x + sizex - 0.5, y=pos1.y + sizey - 0.5, z=z}
  74. local marker = minetest.add_entity(entpos, "worldedit:region_cube", init_sentinel)
  75. if marker ~= nil then
  76. marker:set_properties({
  77. visual_size={x=sizex * 2, y=sizey * 2},
  78. collisionbox = {-sizex, -sizey, -thickness, sizex, sizey, thickness},
  79. })
  80. marker:get_luaentity().player_name = name
  81. table.insert(markers, marker)
  82. end
  83. end
  84. --YZ plane markers
  85. for _, x in ipairs({pos1.x - 0.5, pos2.x + 0.5}) do
  86. local entpos = {x=x, y=pos1.y + sizey - 0.5, z=pos1.z + sizez - 0.5}
  87. local marker = minetest.add_entity(entpos, "worldedit:region_cube", init_sentinel)
  88. if marker ~= nil then
  89. marker:set_properties({
  90. visual_size={x=sizez * 2, y=sizey * 2},
  91. collisionbox = {-thickness, -sizey, -sizez, thickness, sizey, sizez},
  92. })
  93. marker:set_yaw(math.pi / 2)
  94. marker:get_luaentity().player_name = name
  95. table.insert(markers, marker)
  96. end
  97. end
  98. worldedit.marker_region[name] = markers
  99. end
  100. end
  101. --convenience function that calls everything
  102. worldedit.marker_update = function(name)
  103. worldedit.mark_pos1(name, false)
  104. worldedit.mark_pos2(name, false)
  105. worldedit.mark_region(name)
  106. end
  107. minetest.register_entity(":worldedit:pos1", {
  108. initial_properties = {
  109. visual = "cube",
  110. visual_size = {x=1.1, y=1.1},
  111. textures = {"worldedit_pos1.png", "worldedit_pos1.png",
  112. "worldedit_pos1.png", "worldedit_pos1.png",
  113. "worldedit_pos1.png", "worldedit_pos1.png"},
  114. collisionbox = {-0.55, -0.55, -0.55, 0.55, 0.55, 0.55},
  115. physical = false,
  116. static_save = false,
  117. },
  118. on_activate = function(self, staticdata, dtime_s)
  119. if staticdata ~= init_sentinel then
  120. -- we were loaded from before static_save = false was added
  121. self.object:remove()
  122. end
  123. end,
  124. on_punch = function(self, hitter)
  125. self.object:remove()
  126. worldedit.marker1[self.player_name] = nil
  127. end,
  128. on_blast = function(self, damage)
  129. return false, false, {} -- don't damage or knockback
  130. end,
  131. })
  132. minetest.register_entity(":worldedit:pos2", {
  133. initial_properties = {
  134. visual = "cube",
  135. visual_size = {x=1.1, y=1.1},
  136. textures = {"worldedit_pos2.png", "worldedit_pos2.png",
  137. "worldedit_pos2.png", "worldedit_pos2.png",
  138. "worldedit_pos2.png", "worldedit_pos2.png"},
  139. collisionbox = {-0.55, -0.55, -0.55, 0.55, 0.55, 0.55},
  140. physical = false,
  141. static_save = false,
  142. },
  143. on_activate = function(self, staticdata, dtime_s)
  144. if staticdata ~= init_sentinel then
  145. -- we were loaded from before static_save = false was added
  146. self.object:remove()
  147. end
  148. end,
  149. on_punch = function(self, hitter)
  150. self.object:remove()
  151. worldedit.marker2[self.player_name] = nil
  152. end,
  153. on_blast = function(self, damage)
  154. return false, false, {} -- don't damage or knockback
  155. end,
  156. })
  157. minetest.register_entity(":worldedit:region_cube", {
  158. initial_properties = {
  159. visual = "upright_sprite",
  160. textures = {"worldedit_cube.png"},
  161. visual_size = {x=10, y=10},
  162. physical = false,
  163. static_save = false,
  164. },
  165. on_activate = function(self, staticdata, dtime_s)
  166. if staticdata ~= init_sentinel then
  167. -- we were loaded from before static_save = false was added
  168. self.object:remove()
  169. end
  170. end,
  171. on_punch = function(self, hitter)
  172. local markers = worldedit.marker_region[self.player_name]
  173. if not markers then
  174. return
  175. end
  176. for _, entity in ipairs(markers) do
  177. entity:remove()
  178. end
  179. worldedit.marker_region[self.player_name] = nil
  180. end,
  181. on_blast = function(self, damage)
  182. return false, false, {} -- don't damage or knockback
  183. end,
  184. })