Skip to content

Commit 36424e3

Browse files
committedDec 16, 2013
Improve worldedit.spiral and the WorldEdit GUI,
1 parent 3cffae4 commit 36424e3

File tree

3 files changed

+131
-72
lines changed

3 files changed

+131
-72
lines changed
 

‎worldedit/primitives.lua

+20-16
Original file line numberDiff line numberDiff line change
@@ -413,50 +413,54 @@ worldedit.spiral = function(pos, length, height, spacer, nodename)
413413
nodes[i] = ignore
414414
end
415415

416-
--
416+
--set up variables
417417
local node_id = minetest.get_content_id(nodename)
418418
local stride = {x=1, y=area.ystride, z=area.zstride}
419419
local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
420420
local i = offsetz * stride.z + offsety * stride.y + offsetx + 1
421421

422422
--add first column
423+
local count = height
423424
local column = i
424425
for y = 1, height do
425426
nodes[column] = node_id
426427
column = column + stride.y
427428
end
428429

429430
--add spiral segments
430-
local axis, other = "x", "z"
431-
local sign = 1
432-
local count = height
433-
for segment = 1, length / spacer - 1 do --go through each segment except the last
434-
for index = 1, segment * spacer do --fill segment
435-
i = i + stride[axis] * sign
431+
local strideaxis, strideother = stride.x, stride.z
432+
local sign = -1
433+
local segment_length = 0
434+
spacer = spacer + 1
435+
for segment = 1, math.floor(length / spacer) * 2 do --go through each segment except the last
436+
if segment % 2 == 1 then --change sign and length every other turn starting with the first
437+
sign = -sign
438+
segment_length = segment_length + spacer
439+
end
440+
for index = 1, segment_length do --fill segment
441+
i = i + strideaxis * sign --move along the direction of the segment
436442
local column = i
437443
for y = 1, height do --add column
438444
nodes[column] = node_id
439445
column = column + stride.y
440446
end
441-
count = count + height
442-
end
443-
axis, other = other, axis --swap axes
444-
if segment % 2 == 1 then --change sign every other turn
445-
sign = -sign
446447
end
448+
count = count + segment_length * height
449+
strideaxis, strideother = strideother, strideaxis --swap axes
447450
end
448451

449452
--add shorter final segment
450-
for index = 1, (math.floor(length / spacer) - 2) * spacer do
451-
i = i + stride[axis] * sign
453+
sign = -sign
454+
for index = 1, segment_length do
455+
i = i + strideaxis * sign
452456
local column = i
453457
for y = 1, height do --add column
454458
nodes[column] = node_id
455459
column = column + stride.y
456460
end
457-
count = count + height
458461
end
459-
print(minetest.serialize(nodes))
462+
count = count + segment_length * height
463+
460464
--update map nodes
461465
manip:set_data(nodes)
462466
manip:write_to_map()

‎worldedit_gui/functionality.lua

+108-55
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
--saved state for each player
22
local gui_nodename1 = {} --mapping of player names to node names (arbitrary strings may also appear as values)
33
local gui_nodename2 = {} --mapping of player names to node names (arbitrary strings may also appear as values)
4-
local gui_radius = {} --mapping of player names to radii (arbitrary strings may also appear as values)
54
local gui_axis = {} --mapping of player names to axes (one of 1, 2, 3, or 4, representing the axes in the `axis_indices` table below)
6-
local gui_length = {} --mapping of player names to lengths (arbitrary strings may also appear as values)
5+
local gui_distance1 = {} --mapping of player names to a distance, usually side length (arbitrary strings may also appear as values)
6+
local gui_distance2 = {} --mapping of player names to a distance, usually radius (arbitrary strings may also appear as values)
7+
local gui_distance3 = {} --mapping of player names to a distance, usually spacing (arbitrary strings may also appear as values)
78
local gui_formspec = {} --mapping of player names to formspecs (arbitrary strings may also appear as values)
89

10+
--set default values
11+
setmetatable(gui_nodename1, {__index = function () return "Cobblestone" end})
12+
setmetatable(gui_nodename2, {__index = function () return "Stone" end})
13+
setmetatable(gui_axis, {__index = function () return 4 end})
14+
setmetatable(gui_distance1, {__index = function () return "10" end})
15+
setmetatable(gui_distance2, {__index = function () return "5" end})
16+
setmetatable(gui_distance3, {__index = function () return "2" end})
17+
setmetatable(gui_formspec, {__index = function () return "" end})
18+
919
local axis_indices = {["X axis"]=1, ["Y axis"]=2, ["Z axis"]=3, ["Look direction"]=4}
1020
local axis_values = {"x", "y", "z", "?"}
21+
setmetatable(axis_indices, {__index = function () return 4 end})
22+
setmetatable(axis_values, {__index = function () return "?" end})
1123

1224
local register_gui_chatcommand = function(identifier, name, command, callback)
1325
callback = callback or function(name, command) command(name, "") end
@@ -92,7 +104,7 @@ register_gui_chatcommand("worldedit_gui_volume", "Region Volume", "/volume")
92104
worldedit.register_gui_function("worldedit_gui_set", {
93105
name = "Set Nodes", privs = minetest.chatcommands["/set"].privs,
94106
get_formspec = function(name)
95-
local node = gui_nodename1[name] or "Cobblestone"
107+
local node = gui_nodename1[name]
96108
local nodename = worldedit.normalize_nodename(node)
97109
return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_set") ..
98110
string.format("field[0.5,1.5;4,0.8;worldedit_gui_set_node;Name;%s]", minetest.formspec_escape(node)) ..
@@ -104,14 +116,12 @@ worldedit.register_gui_function("worldedit_gui_set", {
104116
})
105117

106118
worldedit.register_gui_handler("worldedit_gui_set", function(name, fields)
107-
if fields.worldedit_gui_set_search then
108-
gui_nodename1[name] = tostring(fields.worldedit_gui_set_node)
109-
worldedit.show_page(name, "worldedit_gui_set")
110-
return true
111-
elseif fields.worldedit_gui_set_submit then
119+
if fields.worldedit_gui_set_search or fields.worldedit_gui_set_submit then
112120
gui_nodename1[name] = tostring(fields.worldedit_gui_set_node)
113121
worldedit.show_page(name, "worldedit_gui_set")
114-
minetest.chatcommands["/set"].func(name, gui_nodename1[name])
122+
if fields.worldedit_gui_set_submit then
123+
minetest.chatcommands["/set"].func(name, gui_nodename1[name])
124+
end
115125
return true
116126
end
117127
return false
@@ -120,10 +130,8 @@ end)
120130
worldedit.register_gui_function("worldedit_gui_replace", {
121131
name = "Replace Nodes", privs = minetest.chatcommands["/replace"].privs,
122132
get_formspec = function(name)
123-
local search = gui_nodename1[name] or "Cobblestone"
124-
local search_nodename = worldedit.normalize_nodename(search)
125-
local replace = gui_nodename2[name] or "Stone"
126-
local replace_nodename = worldedit.normalize_nodename(replace)
133+
local search, replace = gui_nodename1[name], gui_nodename2[name]
134+
local search_nodename, replace_nodename = worldedit.normalize_nodename(search), worldedit.normalize_nodename(replace)
127135
return "size[6.5,4]" .. worldedit.get_formspec_header("worldedit_gui_replace") ..
128136
string.format("field[0.5,1.5;4,0.8;worldedit_gui_replace_search;Name;%s]", minetest.formspec_escape(search)) ..
129137
"button[4,1.18;1.5,0.8;worldedit_gui_replace_search_search;Search]" ..
@@ -139,21 +147,14 @@ worldedit.register_gui_function("worldedit_gui_replace", {
139147
})
140148

141149
worldedit.register_gui_handler("worldedit_gui_replace", function(name, fields)
142-
if fields.worldedit_gui_replace_search_search then
143-
gui_nodename1[name] = tostring(fields.worldedit_gui_replace_search)
144-
worldedit.show_page(name, "worldedit_gui_replace")
145-
return true
146-
elseif fields.worldedit_gui_replace_replace_search then
147-
gui_nodename2[name] = tostring(fields.worldedit_gui_replace_replace)
148-
worldedit.show_page(name, "worldedit_gui_replace")
149-
return true
150-
elseif fields.worldedit_gui_replace_submit or fields.worldedit_gui_replace_submit_inverse then
150+
if fields.worldedit_gui_replace_search_search or fields.worldedit_gui_replace_replace_search
151+
or fields.worldedit_gui_replace_submit or fields.worldedit_gui_replace_submit_inverse then
151152
gui_nodename1[name] = tostring(fields.worldedit_gui_replace_search)
152153
gui_nodename2[name] = tostring(fields.worldedit_gui_replace_replace)
153154
worldedit.show_page(name, "worldedit_gui_replace")
154155
if fields.worldedit_gui_replace_submit then
155156
minetest.chatcommands["/replace"].func(name, string.format("%s %s", gui_nodename1[name], gui_nodename2[name]))
156-
else
157+
elseif fields.worldedit_gui_replace_submit_inverse then
157158
minetest.chatcommands["/replaceinverse"].func(name, string.format("%s %s", gui_nodename1[name], gui_nodename2[name]))
158159
end
159160
return true
@@ -164,8 +165,7 @@ end)
164165
worldedit.register_gui_function("worldedit_gui_sphere_dome", {
165166
name = "Sphere/Dome", privs = minetest.chatcommands["/sphere"].privs,
166167
get_formspec = function(name)
167-
local node = gui_nodename1[name] or "Cobblestone"
168-
local radius = gui_radius[name] or "5"
168+
local node, radius = gui_nodename1[name], gui_distance2[name]
169169
local nodename = worldedit.normalize_nodename(node)
170170
return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_sphere_dome") ..
171171
string.format("field[0.5,1.5;4,0.8;worldedit_gui_sphere_dome_node;Name;%s]", minetest.formspec_escape(node)) ..
@@ -181,23 +181,20 @@ worldedit.register_gui_function("worldedit_gui_sphere_dome", {
181181
})
182182

183183
worldedit.register_gui_handler("worldedit_gui_sphere_dome", function(name, fields)
184-
if fields.worldedit_gui_sphere_dome_search then
185-
gui_nodename1[name] = tostring(fields.worldedit_gui_sphere_dome_node)
186-
worldedit.show_page(name, "worldedit_gui_sphere_dome")
187-
return true
188-
elseif fields.worldedit_gui_sphere_dome_submit_hollow or fields.worldedit_gui_sphere_dome_submit_solid
184+
if fields.worldedit_gui_sphere_dome_search
185+
or fields.worldedit_gui_sphere_dome_submit_hollow or fields.worldedit_gui_sphere_dome_submit_solid
189186
or fields.worldedit_gui_sphere_dome_submit_hollow_dome or fields.worldedit_gui_sphere_dome_submit_solid_dome then
190187
gui_nodename1[name] = tostring(fields.worldedit_gui_sphere_dome_node)
191-
gui_radius[name] = tostring(fields.worldedit_gui_sphere_dome_radius)
188+
gui_distance2[name] = tostring(fields.worldedit_gui_sphere_dome_radius)
192189
worldedit.show_page(name, "worldedit_gui_sphere_dome")
193190
if fields.worldedit_gui_sphere_dome_submit_hollow then
194-
minetest.chatcommands["/hollowsphere"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))
191+
minetest.chatcommands["/hollowsphere"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))
195192
elseif fields.worldedit_gui_sphere_dome_submit_solid then
196-
minetest.chatcommands["/sphere"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))
193+
minetest.chatcommands["/sphere"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))
197194
elseif fields.worldedit_gui_sphere_dome_submit_hollow_dome then
198-
minetest.chatcommands["/hollowdome"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))
199-
else --fields.worldedit_gui_sphere_dome_submit_solid_dome
200-
minetest.chatcommands["/dome"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))
195+
minetest.chatcommands["/hollowdome"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))
196+
elseif fields.worldedit_gui_sphere_dome_submit_solid_dome then
197+
minetest.chatcommands["/dome"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))
201198
end
202199
return true
203200
end
@@ -207,10 +204,7 @@ end)
207204
worldedit.register_gui_function("worldedit_gui_cylinder", {
208205
name = "Cylinder", privs = minetest.chatcommands["/cylinder"].privs,
209206
get_formspec = function(name)
210-
local node = gui_nodename1[name] or "Cobblestone"
211-
local axis = gui_axis[name] or 4
212-
local length = gui_length[name] or "10"
213-
local radius = gui_radius[name] or "5"
207+
local node, axis, length, radius = gui_nodename1[name], gui_axis[name], gui_distance1[name], gui_distance2[name]
214208
local nodename = worldedit.normalize_nodename(node)
215209
return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_cylinder") ..
216210
string.format("field[0.5,1.5;4,0.8;worldedit_gui_cylinder_node;Name;%s]", minetest.formspec_escape(node)) ..
@@ -226,20 +220,79 @@ worldedit.register_gui_function("worldedit_gui_cylinder", {
226220
})
227221

228222
worldedit.register_gui_handler("worldedit_gui_cylinder", function(name, fields)
229-
if fields.worldedit_gui_cylinder_search then
230-
gui_nodename1[name] = fields.worldedit_gui_cylinder_node
231-
worldedit.show_page(name, "worldedit_gui_cylinder")
232-
return true
233-
elseif fields.worldedit_gui_cylinder_submit_hollow or fields.worldedit_gui_cylinder_submit_solid then
223+
if fields.worldedit_gui_cylinder_search
224+
or fields.worldedit_gui_cylinder_submit_hollow or fields.worldedit_gui_cylinder_submit_solid then
234225
gui_nodename1[name] = tostring(fields.worldedit_gui_cylinder_node)
235-
gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis] or 4
236-
gui_length[name] = tostring(fields.worldedit_gui_cylinder_length)
237-
gui_radius[name] = tostring(fields.worldedit_gui_cylinder_radius)
226+
gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis]
227+
gui_distance1[name] = tostring(fields.worldedit_gui_cylinder_length)
228+
gui_distance2[name] = tostring(fields.worldedit_gui_cylinder_radius)
238229
worldedit.show_page(name, "worldedit_gui_cylinder")
239230
if fields.worldedit_gui_cylinder_submit_hollow then
240-
minetest.chatcommands["/hollowcylinder"].func(name, string.format("%s %s %s %s", axis_values[gui_axis[name]], gui_length[name], gui_radius[name], gui_nodename1[name]))
241-
else --fields.worldedit_gui_cylinder_submit_solid
242-
minetest.chatcommands["/cylinder"].func(name, string.format("%s %s %s %s", axis_values[gui_axis[name]], gui_length[name], gui_radius[name], gui_nodename1[name]))
231+
minetest.chatcommands["/hollowcylinder"].func(name, string.format("%s %s %s %s", axis_values[gui_axis[name]], gui_distance1[name], gui_distance2[name], gui_nodename1[name]))
232+
elseif fields.worldedit_gui_cylinder_submit_solid then
233+
minetest.chatcommands["/cylinder"].func(name, string.format("%s %s %s %s", axis_values[gui_axis[name]], gui_distance1[name], gui_distance2[name], gui_nodename1[name]))
234+
end
235+
return true
236+
end
237+
return false
238+
end)
239+
240+
worldedit.register_gui_function("worldedit_gui_pyramid", {
241+
name = "Pyramid", privs = minetest.chatcommands["/pyramid"].privs,
242+
get_formspec = function(name)
243+
local node, axis, length = gui_nodename1[name], gui_axis[name], gui_distance1[name]
244+
local nodename = worldedit.normalize_nodename(node)
245+
return "size[6.5,4]" .. worldedit.get_formspec_header("worldedit_gui_pyramid") ..
246+
string.format("field[0.5,1.5;4,0.8;worldedit_gui_pyramid_node;Name;%s]", minetest.formspec_escape(node)) ..
247+
"button[4,1.18;1.5,0.8;worldedit_gui_pyramid_search;Search]" ..
248+
(nodename and string.format("item_image[5.5,1.1;1,1;%s]", nodename)
249+
or "image[5.5,1.1;1,1;unknown_node.png]") ..
250+
string.format("field[0.5,2.5;4,0.8;worldedit_gui_pyramid_length;Length;%s]", minetest.formspec_escape(length)) ..
251+
string.format("dropdown[4,2.18;2.5;worldedit_gui_pyramid_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
252+
"button_exit[0,3.5;3,0.8;worldedit_gui_pyramid_submit;Pyramid]"
253+
end,
254+
})
255+
256+
worldedit.register_gui_handler("worldedit_gui_pyramid", function(name, fields)
257+
if fields.worldedit_gui_pyramid_search or fields.worldedit_gui_pyramid_submit then
258+
gui_nodename1[name] = tostring(fields.worldedit_gui_pyramid_node)
259+
gui_axis[name] = axis_indices[fields.worldedit_gui_pyramid_axis]
260+
gui_distance1[name] = tostring(fields.worldedit_gui_pyramid_length)
261+
worldedit.show_page(name, "worldedit_gui_pyramid")
262+
if fields.worldedit_gui_pyramid_submit then
263+
minetest.chatcommands["/pyramid"].func(name, string.format("%s %s %s", axis_values[gui_axis[name]], gui_distance1[name], gui_nodename1[name]))
264+
end
265+
return true
266+
end
267+
return false
268+
end)
269+
270+
worldedit.register_gui_function("worldedit_gui_spiral", {
271+
name = "Spiral", privs = minetest.chatcommands["/spiral"].privs,
272+
get_formspec = function(name)
273+
local node, length, height, space = gui_nodename1[name], gui_distance1[name], gui_distance2[name], gui_distance3[name]
274+
local nodename = worldedit.normalize_nodename(node)
275+
return "size[6.5,6]" .. worldedit.get_formspec_header("worldedit_gui_spiral") ..
276+
string.format("field[0.5,1.5;4,0.8;worldedit_gui_spiral_node;Name;%s]", minetest.formspec_escape(node)) ..
277+
"button[4,1.18;1.5,0.8;worldedit_gui_spiral_search;Search]" ..
278+
(nodename and string.format("item_image[5.5,1.1;1,1;%s]", nodename)
279+
or "image[5.5,1.1;1,1;unknown_node.png]") ..
280+
string.format("field[0.5,2.5;4,0.8;worldedit_gui_spiral_length;Side Length;%s]", minetest.formspec_escape(length)) ..
281+
string.format("field[0.5,3.5;4,0.8;worldedit_gui_spiral_height;Height;%s]", minetest.formspec_escape(height)) ..
282+
string.format("field[0.5,4.5;4,0.8;worldedit_gui_spiral_space;Wall Spacing;%s]", minetest.formspec_escape(space)) ..
283+
"button_exit[0,5.5;3,0.8;worldedit_gui_spiral_submit;Spiral]"
284+
end,
285+
})
286+
287+
worldedit.register_gui_handler("worldedit_gui_spiral", function(name, fields)
288+
if fields.worldedit_gui_spiral_search or fields.worldedit_gui_spiral_submit then
289+
gui_nodename1[name] = fields.worldedit_gui_spiral_node
290+
gui_distance1[name] = tostring(fields.worldedit_gui_spiral_length)
291+
gui_distance2[name] = tostring(fields.worldedit_gui_spiral_height)
292+
gui_distance3[name] = tostring(fields.worldedit_gui_spiral_space)
293+
worldedit.show_page(name, "worldedit_gui_spiral")
294+
if fields.worldedit_gui_spiral_submit then
295+
minetest.chatcommands["/spiral"].func(name, string.format("%s %s %s %s", gui_distance1[name], gui_distance2[name], gui_distance3[name], gui_nodename1[name]))
243296
end
244297
return true
245298
end
@@ -250,7 +303,7 @@ worldedit.register_gui_function("worldedit_gui_copy_move", {
250303
name = "Copy/Move", privs = minetest.chatcommands["/move"].privs,
251304
get_formspec = function(name)
252305
local axis = gui_axis[name] or 4
253-
local amount = gui_length[name] or "10"
306+
local amount = gui_distance1[name] or "10"
254307
return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_copy_move") ..
255308
string.format("field[0.5,1.5;4,0.8;worldedit_gui_copy_move_amount;Amount;%s]", minetest.formspec_escape(amount)) ..
256309
string.format("dropdown[4,1.18;2.5;worldedit_gui_copy_move_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
@@ -262,12 +315,12 @@ worldedit.register_gui_function("worldedit_gui_copy_move", {
262315
worldedit.register_gui_handler("worldedit_gui_copy_move", function(name, fields)
263316
if fields.worldedit_gui_copy_move_copy or fields.worldedit_gui_copy_move_move then
264317
gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis] or 4
265-
gui_length[name] = tostring(fields.worldedit_gui_copy_move_amount)
318+
gui_distance1[name] = tostring(fields.worldedit_gui_copy_move_amount)
266319
worldedit.show_page(name, "worldedit_gui_copy_move")
267320
if fields.worldedit_gui_copy_move_copy then
268-
minetest.chatcommands["/copy"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_length[name]))
321+
minetest.chatcommands["/copy"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_distance1[name]))
269322
else --fields.worldedit_gui_copy_move_move
270-
minetest.chatcommands["/move"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_length[name]))
323+
minetest.chatcommands["/move"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_distance1[name]))
271324
end
272325
return true
273326
end

‎worldedit_gui/init.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
--wip: make back buttons images in all screens
21
--wip: support unified_inventory, it even seems to have some sort of API now
32
--wip: make it look good with image buttons and stuff
43

@@ -114,6 +113,9 @@ worldedit.register_gui_function("worldedit_gui", {
114113
end
115114
end
116115
end
116+
if index == 0 then --empty row
117+
y = y - height
118+
end
117119
return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
118120
(inventory_plus and "button[0,0;2,0.5;main;Back]" or "button_exit[0,0;2,0.5;main;Exit]") ..
119121
"label[2,0;WorldEdit GUI]" ..

0 commit comments

Comments
 (0)
Please sign in to comment.