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