mark.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. worldedit.marker1 = {}
  2. worldedit.marker2 = {}
  3. worldedit.marker_region = {}
  4. --marks worldedit region position 1
  5. worldedit.mark_pos1 = function(name)
  6. local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
  7. if pos1 ~= nil then
  8. --make area stay loaded
  9. local manip = minetest.get_voxel_manip()
  10. manip:read_from_map(pos1, pos1)
  11. end
  12. if worldedit.marker1[name] ~= nil then --marker already exists
  13. worldedit.marker1[name]:remove() --remove marker
  14. worldedit.marker1[name] = nil
  15. end
  16. if pos1 ~= nil then
  17. --add marker
  18. worldedit.marker1[name] = minetest.add_entity(pos1, "worldedit:pos1")
  19. if worldedit.marker1[name] ~= nil then
  20. worldedit.marker1[name]:get_luaentity().player_name = name
  21. end
  22. end
  23. worldedit.mark_region(name)
  24. end
  25. --marks worldedit region position 2
  26. worldedit.mark_pos2 = function(name)
  27. local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
  28. if pos2 ~= nil then
  29. --make area stay loaded
  30. local manip = minetest.get_voxel_manip()
  31. manip:read_from_map(pos2, pos2)
  32. end
  33. if worldedit.marker2[name] ~= nil then --marker already exists
  34. worldedit.marker2[name]:remove() --remove marker
  35. worldedit.marker2[name] = nil
  36. end
  37. if pos2 ~= nil then
  38. --add marker
  39. worldedit.marker2[name] = minetest.add_entity(pos2, "worldedit:pos2")
  40. if worldedit.marker2[name] ~= nil then
  41. worldedit.marker2[name]:get_luaentity().player_name = name
  42. end
  43. end
  44. worldedit.mark_region(name)
  45. end
  46. worldedit.mark_region = function(name)
  47. local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
  48. if worldedit.marker_region[name] ~= nil then --marker already exists
  49. --wip: make the area stay loaded somehow
  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.setting_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 marker = minetest.add_entity({x=pos1.x + sizex - 0.5, y=pos1.y + sizey - 0.5, z=z}, "worldedit:region_cube")
  74. if marker ~= nil then
  75. marker:set_properties({
  76. visual_size={x=sizex * 2, y=sizey * 2},
  77. collisionbox = {-sizex, -sizey, -thickness, sizex, sizey, thickness},
  78. })
  79. marker:get_luaentity().player_name = name
  80. table.insert(markers, marker)
  81. end
  82. end
  83. --YZ plane markers
  84. for _, x in ipairs({pos1.x - 0.5, pos2.x + 0.5}) do
  85. local marker = minetest.add_entity({x=x, y=pos1.y + sizey - 0.5, z=pos1.z + sizez - 0.5}, "worldedit:region_cube")
  86. if marker ~= nil then
  87. marker:set_properties({
  88. visual_size={x=sizez * 2, y=sizey * 2},
  89. collisionbox = {-thickness, -sizey, -sizez, thickness, sizey, sizez},
  90. })
  91. marker:setyaw(math.pi / 2)
  92. marker:get_luaentity().player_name = name
  93. table.insert(markers, marker)
  94. end
  95. end
  96. worldedit.marker_region[name] = markers
  97. end
  98. end
  99. minetest.register_entity(":worldedit:pos1", {
  100. initial_properties = {
  101. visual = "cube",
  102. visual_size = {x=1.1, y=1.1},
  103. textures = {"worldedit_pos1.png", "worldedit_pos1.png",
  104. "worldedit_pos1.png", "worldedit_pos1.png",
  105. "worldedit_pos1.png", "worldedit_pos1.png"},
  106. collisionbox = {-0.55, -0.55, -0.55, 0.55, 0.55, 0.55},
  107. physical = false,
  108. },
  109. on_step = function(self, dtime)
  110. if worldedit.marker1[self.player_name] == nil then
  111. self.object:remove()
  112. end
  113. end,
  114. on_punch = function(self, hitter)
  115. self.object:remove()
  116. worldedit.marker1[self.player_name] = nil
  117. end,
  118. })
  119. minetest.register_entity(":worldedit:pos2", {
  120. initial_properties = {
  121. visual = "cube",
  122. visual_size = {x=1.1, y=1.1},
  123. textures = {"worldedit_pos2.png", "worldedit_pos2.png",
  124. "worldedit_pos2.png", "worldedit_pos2.png",
  125. "worldedit_pos2.png", "worldedit_pos2.png"},
  126. collisionbox = {-0.55, -0.55, -0.55, 0.55, 0.55, 0.55},
  127. physical = false,
  128. },
  129. on_step = function(self, dtime)
  130. if worldedit.marker2[self.player_name] == nil then
  131. self.object:remove()
  132. end
  133. end,
  134. on_punch = function(self, hitter)
  135. self.object:remove()
  136. worldedit.marker2[self.player_name] = nil
  137. end,
  138. })
  139. minetest.register_entity(":worldedit:region_cube", {
  140. initial_properties = {
  141. visual = "upright_sprite",
  142. visual_size = {x=1.1, y=1.1},
  143. textures = {"worldedit_cube.png"},
  144. visual_size = {x=10, y=10},
  145. physical = false,
  146. },
  147. on_step = function(self, dtime)
  148. if worldedit.marker_region[self.player_name] == nil then
  149. self.object:remove()
  150. return
  151. end
  152. end,
  153. on_punch = function(self, hitter)
  154. local markers = worldedit.marker_region[self.player_name]
  155. if not markers then
  156. return
  157. end
  158. for _, entity in ipairs(markers) do
  159. entity:remove()
  160. end
  161. worldedit.marker_region[self.player_name] = nil
  162. end,
  163. })