]> git.lizzy.rs Git - worldedit.git/commitdiff
Improve worldedit.spiral and the WorldEdit GUI,
authorUberi <azhang9@gmail.com>
Mon, 16 Dec 2013 23:55:56 +0000 (18:55 -0500)
committerUberi <azhang9@gmail.com>
Mon, 16 Dec 2013 23:55:56 +0000 (18:55 -0500)
worldedit/primitives.lua
worldedit_gui/functionality.lua
worldedit_gui/init.lua

index 8246bb91c369c517bc325e8610c7378d42c67eaa..1baa29e1730aceeb483f88121af7af342d506f19 100644 (file)
@@ -413,13 +413,14 @@ worldedit.spiral = function(pos, length, height, spacer, nodename)
                nodes[i] = ignore\r
        end\r
 \r
-       --\r
+       --set up variables\r
        local node_id = minetest.get_content_id(nodename)\r
        local stride = {x=1, y=area.ystride, z=area.zstride}\r
        local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z\r
        local i = offsetz * stride.z + offsety * stride.y + offsetx + 1\r
 \r
        --add first column\r
+       local count = height\r
        local column = i\r
        for y = 1, height do\r
                nodes[column] = node_id\r
@@ -427,36 +428,39 @@ worldedit.spiral = function(pos, length, height, spacer, nodename)
        end\r
 \r
        --add spiral segments\r
-       local axis, other = "x", "z"\r
-       local sign = 1\r
-       local count = height\r
-       for segment = 1, length / spacer - 1 do --go through each segment except the last\r
-               for index = 1, segment * spacer do --fill segment\r
-                       i = i + stride[axis] * sign\r
+       local strideaxis, strideother = stride.x, stride.z\r
+       local sign = -1\r
+       local segment_length = 0\r
+       spacer = spacer + 1\r
+       for segment = 1, math.floor(length / spacer) * 2 do --go through each segment except the last\r
+               if segment % 2 == 1 then --change sign and length every other turn starting with the first\r
+                       sign = -sign\r
+                       segment_length = segment_length + spacer\r
+               end\r
+               for index = 1, segment_length do --fill segment\r
+                       i = i + strideaxis * sign --move along the direction of the segment\r
                        local column = i\r
                        for y = 1, height do --add column\r
                                nodes[column] = node_id\r
                                column = column + stride.y\r
                        end\r
-                       count = count + height\r
-               end\r
-               axis, other = other, axis --swap axes\r
-               if segment % 2 == 1 then --change sign every other turn\r
-                       sign = -sign\r
                end\r
+               count = count + segment_length * height\r
+               strideaxis, strideother = strideother, strideaxis --swap axes\r
        end\r
 \r
        --add shorter final segment\r
-       for index = 1, (math.floor(length / spacer) - 2) * spacer do\r
-               i = i + stride[axis] * sign\r
+       sign = -sign\r
+       for index = 1, segment_length do\r
+               i = i + strideaxis * sign\r
                local column = i\r
                for y = 1, height do --add column\r
                        nodes[column] = node_id\r
                        column = column + stride.y\r
                end\r
-               count = count + height\r
        end\r
-print(minetest.serialize(nodes))\r
+       count = count + segment_length * height\r
+\r
        --update map nodes\r
        manip:set_data(nodes)\r
        manip:write_to_map()\r
index 29405eac2cd92e676cb1c7c74c3538addde67d54..a12faffef0ac158812a3a325d5dafb2a14681f0b 100644 (file)
@@ -1,13 +1,25 @@
 --saved state for each player\r
 local gui_nodename1 = {} --mapping of player names to node names (arbitrary strings may also appear as values)\r
 local gui_nodename2 = {} --mapping of player names to node names (arbitrary strings may also appear as values)\r
-local gui_radius = {} --mapping of player names to radii (arbitrary strings may also appear as values)\r
 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)\r
-local gui_length = {} --mapping of player names to lengths (arbitrary strings may also appear as values)\r
+local gui_distance1 = {} --mapping of player names to a distance, usually side length (arbitrary strings may also appear as values)\r
+local gui_distance2 = {} --mapping of player names to a distance, usually radius (arbitrary strings may also appear as values)\r
+local gui_distance3 = {} --mapping of player names to a distance, usually spacing (arbitrary strings may also appear as values)\r
 local gui_formspec = {} --mapping of player names to formspecs (arbitrary strings may also appear as values)\r
 \r
+--set default values\r
+setmetatable(gui_nodename1, {__index = function () return "Cobblestone" end})\r
+setmetatable(gui_nodename2, {__index = function () return "Stone" end})\r
+setmetatable(gui_axis, {__index = function () return 4 end})\r
+setmetatable(gui_distance1, {__index = function () return "10" end})\r
+setmetatable(gui_distance2, {__index = function () return "5" end})\r
+setmetatable(gui_distance3, {__index = function () return "2" end})\r
+setmetatable(gui_formspec, {__index = function () return "" end})\r
+\r
 local axis_indices = {["X axis"]=1, ["Y axis"]=2, ["Z axis"]=3, ["Look direction"]=4}\r
 local axis_values = {"x", "y", "z", "?"}\r
+setmetatable(axis_indices, {__index = function () return 4 end})\r
+setmetatable(axis_values, {__index = function () return "?" end})\r
 \r
 local register_gui_chatcommand = function(identifier, name, command, callback)\r
        callback = callback or function(name, command) command(name, "") end\r
@@ -92,7 +104,7 @@ register_gui_chatcommand("worldedit_gui_volume", "Region Volume", "/volume")
 worldedit.register_gui_function("worldedit_gui_set", {\r
        name = "Set Nodes", privs = minetest.chatcommands["/set"].privs,\r
        get_formspec = function(name)\r
-               local node = gui_nodename1[name] or "Cobblestone"\r
+               local node = gui_nodename1[name]\r
                local nodename = worldedit.normalize_nodename(node)\r
                return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_set") ..\r
                        string.format("field[0.5,1.5;4,0.8;worldedit_gui_set_node;Name;%s]", minetest.formspec_escape(node)) ..\r
@@ -104,14 +116,12 @@ worldedit.register_gui_function("worldedit_gui_set", {
 })\r
 \r
 worldedit.register_gui_handler("worldedit_gui_set", function(name, fields)\r
-       if fields.worldedit_gui_set_search then\r
-               gui_nodename1[name] = tostring(fields.worldedit_gui_set_node)\r
-               worldedit.show_page(name, "worldedit_gui_set")\r
-               return true\r
-       elseif fields.worldedit_gui_set_submit then\r
+       if fields.worldedit_gui_set_search or fields.worldedit_gui_set_submit then\r
                gui_nodename1[name] = tostring(fields.worldedit_gui_set_node)\r
                worldedit.show_page(name, "worldedit_gui_set")\r
-               minetest.chatcommands["/set"].func(name, gui_nodename1[name])\r
+               if fields.worldedit_gui_set_submit then\r
+                       minetest.chatcommands["/set"].func(name, gui_nodename1[name])\r
+               end\r
                return true\r
        end\r
        return false\r
@@ -120,10 +130,8 @@ end)
 worldedit.register_gui_function("worldedit_gui_replace", {\r
        name = "Replace Nodes", privs = minetest.chatcommands["/replace"].privs,\r
        get_formspec = function(name)\r
-               local search = gui_nodename1[name] or "Cobblestone"\r
-               local search_nodename = worldedit.normalize_nodename(search)\r
-               local replace = gui_nodename2[name] or "Stone"\r
-               local replace_nodename = worldedit.normalize_nodename(replace)\r
+               local search, replace = gui_nodename1[name], gui_nodename2[name]\r
+               local search_nodename, replace_nodename = worldedit.normalize_nodename(search), worldedit.normalize_nodename(replace)\r
                return "size[6.5,4]" .. worldedit.get_formspec_header("worldedit_gui_replace") ..\r
                        string.format("field[0.5,1.5;4,0.8;worldedit_gui_replace_search;Name;%s]", minetest.formspec_escape(search)) ..\r
                        "button[4,1.18;1.5,0.8;worldedit_gui_replace_search_search;Search]" ..\r
@@ -139,21 +147,14 @@ worldedit.register_gui_function("worldedit_gui_replace", {
 })\r
 \r
 worldedit.register_gui_handler("worldedit_gui_replace", function(name, fields)\r
-       if fields.worldedit_gui_replace_search_search then\r
-               gui_nodename1[name] = tostring(fields.worldedit_gui_replace_search)\r
-               worldedit.show_page(name, "worldedit_gui_replace")\r
-               return true\r
-       elseif fields.worldedit_gui_replace_replace_search then\r
-               gui_nodename2[name] = tostring(fields.worldedit_gui_replace_replace)\r
-               worldedit.show_page(name, "worldedit_gui_replace")\r
-               return true\r
-       elseif fields.worldedit_gui_replace_submit or fields.worldedit_gui_replace_submit_inverse then\r
+       if fields.worldedit_gui_replace_search_search or fields.worldedit_gui_replace_replace_search\r
+       or fields.worldedit_gui_replace_submit or fields.worldedit_gui_replace_submit_inverse then\r
                gui_nodename1[name] = tostring(fields.worldedit_gui_replace_search)\r
                gui_nodename2[name] = tostring(fields.worldedit_gui_replace_replace)\r
                worldedit.show_page(name, "worldedit_gui_replace")\r
                if fields.worldedit_gui_replace_submit then\r
                        minetest.chatcommands["/replace"].func(name, string.format("%s %s", gui_nodename1[name], gui_nodename2[name]))\r
-               else\r
+               elseif fields.worldedit_gui_replace_submit_inverse then\r
                        minetest.chatcommands["/replaceinverse"].func(name, string.format("%s %s", gui_nodename1[name], gui_nodename2[name]))\r
                end\r
                return true\r
@@ -164,8 +165,7 @@ end)
 worldedit.register_gui_function("worldedit_gui_sphere_dome", {\r
        name = "Sphere/Dome", privs = minetest.chatcommands["/sphere"].privs,\r
        get_formspec = function(name)\r
-               local node = gui_nodename1[name] or "Cobblestone"\r
-               local radius = gui_radius[name] or "5"\r
+               local node, radius = gui_nodename1[name], gui_distance2[name]\r
                local nodename = worldedit.normalize_nodename(node)\r
                return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_sphere_dome") ..\r
                        string.format("field[0.5,1.5;4,0.8;worldedit_gui_sphere_dome_node;Name;%s]", minetest.formspec_escape(node)) ..\r
@@ -181,23 +181,20 @@ worldedit.register_gui_function("worldedit_gui_sphere_dome", {
 })\r
 \r
 worldedit.register_gui_handler("worldedit_gui_sphere_dome", function(name, fields)\r
-       if fields.worldedit_gui_sphere_dome_search then\r
-               gui_nodename1[name] = tostring(fields.worldedit_gui_sphere_dome_node)\r
-               worldedit.show_page(name, "worldedit_gui_sphere_dome")\r
-               return true\r
-       elseif fields.worldedit_gui_sphere_dome_submit_hollow or fields.worldedit_gui_sphere_dome_submit_solid\r
+       if fields.worldedit_gui_sphere_dome_search\r
+       or fields.worldedit_gui_sphere_dome_submit_hollow or fields.worldedit_gui_sphere_dome_submit_solid\r
        or fields.worldedit_gui_sphere_dome_submit_hollow_dome or fields.worldedit_gui_sphere_dome_submit_solid_dome then\r
                gui_nodename1[name] = tostring(fields.worldedit_gui_sphere_dome_node)\r
-               gui_radius[name] = tostring(fields.worldedit_gui_sphere_dome_radius)\r
+               gui_distance2[name] = tostring(fields.worldedit_gui_sphere_dome_radius)\r
                worldedit.show_page(name, "worldedit_gui_sphere_dome")\r
                if fields.worldedit_gui_sphere_dome_submit_hollow then\r
-                       minetest.chatcommands["/hollowsphere"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
+                       minetest.chatcommands["/hollowsphere"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))\r
                elseif fields.worldedit_gui_sphere_dome_submit_solid then\r
-                       minetest.chatcommands["/sphere"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
+                       minetest.chatcommands["/sphere"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))\r
                elseif fields.worldedit_gui_sphere_dome_submit_hollow_dome then\r
-                       minetest.chatcommands["/hollowdome"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
-               else --fields.worldedit_gui_sphere_dome_submit_solid_dome\r
-                       minetest.chatcommands["/dome"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
+                       minetest.chatcommands["/hollowdome"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))\r
+               elseif fields.worldedit_gui_sphere_dome_submit_solid_dome then\r
+                       minetest.chatcommands["/dome"].func(name, string.format("%s %s", gui_distance2[name], gui_nodename1[name]))\r
                end\r
                return true\r
        end\r
@@ -207,10 +204,7 @@ end)
 worldedit.register_gui_function("worldedit_gui_cylinder", {\r
        name = "Cylinder", privs = minetest.chatcommands["/cylinder"].privs,\r
        get_formspec = function(name)\r
-               local node = gui_nodename1[name] or "Cobblestone"\r
-               local axis = gui_axis[name] or 4\r
-               local length = gui_length[name] or "10"\r
-               local radius = gui_radius[name] or "5"\r
+               local node, axis, length, radius = gui_nodename1[name], gui_axis[name], gui_distance1[name], gui_distance2[name]\r
                local nodename = worldedit.normalize_nodename(node)\r
                return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_cylinder") ..\r
                        string.format("field[0.5,1.5;4,0.8;worldedit_gui_cylinder_node;Name;%s]", minetest.formspec_escape(node)) ..\r
@@ -226,20 +220,79 @@ worldedit.register_gui_function("worldedit_gui_cylinder", {
 })\r
 \r
 worldedit.register_gui_handler("worldedit_gui_cylinder", function(name, fields)\r
-       if fields.worldedit_gui_cylinder_search then\r
-               gui_nodename1[name] = fields.worldedit_gui_cylinder_node\r
-               worldedit.show_page(name, "worldedit_gui_cylinder")\r
-               return true\r
-       elseif fields.worldedit_gui_cylinder_submit_hollow or fields.worldedit_gui_cylinder_submit_solid then\r
+       if fields.worldedit_gui_cylinder_search\r
+       or fields.worldedit_gui_cylinder_submit_hollow or fields.worldedit_gui_cylinder_submit_solid then\r
                gui_nodename1[name] = tostring(fields.worldedit_gui_cylinder_node)\r
-               gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis] or 4\r
-               gui_length[name] = tostring(fields.worldedit_gui_cylinder_length)\r
-               gui_radius[name] = tostring(fields.worldedit_gui_cylinder_radius)\r
+               gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis]\r
+               gui_distance1[name] = tostring(fields.worldedit_gui_cylinder_length)\r
+               gui_distance2[name] = tostring(fields.worldedit_gui_cylinder_radius)\r
                worldedit.show_page(name, "worldedit_gui_cylinder")\r
                if fields.worldedit_gui_cylinder_submit_hollow then\r
-                       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]))\r
-               else --fields.worldedit_gui_cylinder_submit_solid\r
-                       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]))\r
+                       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]))\r
+               elseif fields.worldedit_gui_cylinder_submit_solid then\r
+                       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]))\r
+               end\r
+               return true\r
+       end\r
+       return false\r
+end)\r
+\r
+worldedit.register_gui_function("worldedit_gui_pyramid", {\r
+       name = "Pyramid", privs = minetest.chatcommands["/pyramid"].privs,\r
+       get_formspec = function(name)\r
+               local node, axis, length = gui_nodename1[name], gui_axis[name], gui_distance1[name]\r
+               local nodename = worldedit.normalize_nodename(node)\r
+               return "size[6.5,4]" .. worldedit.get_formspec_header("worldedit_gui_pyramid") ..\r
+                       string.format("field[0.5,1.5;4,0.8;worldedit_gui_pyramid_node;Name;%s]", minetest.formspec_escape(node)) ..\r
+                       "button[4,1.18;1.5,0.8;worldedit_gui_pyramid_search;Search]" ..\r
+                       (nodename and string.format("item_image[5.5,1.1;1,1;%s]", nodename)\r
+                               or "image[5.5,1.1;1,1;unknown_node.png]") ..\r
+                       string.format("field[0.5,2.5;4,0.8;worldedit_gui_pyramid_length;Length;%s]", minetest.formspec_escape(length)) ..\r
+                       string.format("dropdown[4,2.18;2.5;worldedit_gui_pyramid_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..\r
+                       "button_exit[0,3.5;3,0.8;worldedit_gui_pyramid_submit;Pyramid]"\r
+       end,\r
+})\r
+\r
+worldedit.register_gui_handler("worldedit_gui_pyramid", function(name, fields)\r
+       if fields.worldedit_gui_pyramid_search or fields.worldedit_gui_pyramid_submit then\r
+               gui_nodename1[name] = tostring(fields.worldedit_gui_pyramid_node)\r
+               gui_axis[name] = axis_indices[fields.worldedit_gui_pyramid_axis]\r
+               gui_distance1[name] = tostring(fields.worldedit_gui_pyramid_length)\r
+               worldedit.show_page(name, "worldedit_gui_pyramid")\r
+               if fields.worldedit_gui_pyramid_submit then\r
+                       minetest.chatcommands["/pyramid"].func(name, string.format("%s %s %s", axis_values[gui_axis[name]], gui_distance1[name], gui_nodename1[name]))\r
+               end\r
+               return true\r
+       end\r
+       return false\r
+end)\r
+\r
+worldedit.register_gui_function("worldedit_gui_spiral", {\r
+       name = "Spiral", privs = minetest.chatcommands["/spiral"].privs,\r
+       get_formspec = function(name)\r
+               local node, length, height, space = gui_nodename1[name], gui_distance1[name], gui_distance2[name], gui_distance3[name]\r
+               local nodename = worldedit.normalize_nodename(node)\r
+               return "size[6.5,6]" .. worldedit.get_formspec_header("worldedit_gui_spiral") ..\r
+                       string.format("field[0.5,1.5;4,0.8;worldedit_gui_spiral_node;Name;%s]", minetest.formspec_escape(node)) ..\r
+                       "button[4,1.18;1.5,0.8;worldedit_gui_spiral_search;Search]" ..\r
+                       (nodename and string.format("item_image[5.5,1.1;1,1;%s]", nodename)\r
+                               or "image[5.5,1.1;1,1;unknown_node.png]") ..\r
+                       string.format("field[0.5,2.5;4,0.8;worldedit_gui_spiral_length;Side Length;%s]", minetest.formspec_escape(length)) ..\r
+                       string.format("field[0.5,3.5;4,0.8;worldedit_gui_spiral_height;Height;%s]", minetest.formspec_escape(height)) ..\r
+                       string.format("field[0.5,4.5;4,0.8;worldedit_gui_spiral_space;Wall Spacing;%s]", minetest.formspec_escape(space)) ..\r
+                       "button_exit[0,5.5;3,0.8;worldedit_gui_spiral_submit;Spiral]"\r
+       end,\r
+})\r
+\r
+worldedit.register_gui_handler("worldedit_gui_spiral", function(name, fields)\r
+       if fields.worldedit_gui_spiral_search or fields.worldedit_gui_spiral_submit then\r
+               gui_nodename1[name] = fields.worldedit_gui_spiral_node\r
+               gui_distance1[name] = tostring(fields.worldedit_gui_spiral_length)\r
+               gui_distance2[name] = tostring(fields.worldedit_gui_spiral_height)\r
+               gui_distance3[name] = tostring(fields.worldedit_gui_spiral_space)\r
+               worldedit.show_page(name, "worldedit_gui_spiral")\r
+               if fields.worldedit_gui_spiral_submit then\r
+                       minetest.chatcommands["/spiral"].func(name, string.format("%s %s %s %s", gui_distance1[name], gui_distance2[name], gui_distance3[name], gui_nodename1[name]))\r
                end\r
                return true\r
        end\r
@@ -250,7 +303,7 @@ worldedit.register_gui_function("worldedit_gui_copy_move", {
        name = "Copy/Move", privs = minetest.chatcommands["/move"].privs,\r
        get_formspec = function(name)\r
                local axis = gui_axis[name] or 4\r
-               local amount = gui_length[name] or "10"\r
+               local amount = gui_distance1[name] or "10"\r
                return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_copy_move") ..\r
                        string.format("field[0.5,1.5;4,0.8;worldedit_gui_copy_move_amount;Amount;%s]", minetest.formspec_escape(amount)) ..\r
                        string.format("dropdown[4,1.18;2.5;worldedit_gui_copy_move_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..\r
@@ -262,12 +315,12 @@ worldedit.register_gui_function("worldedit_gui_copy_move", {
 worldedit.register_gui_handler("worldedit_gui_copy_move", function(name, fields)\r
        if fields.worldedit_gui_copy_move_copy or fields.worldedit_gui_copy_move_move then\r
                gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis] or 4\r
-               gui_length[name] = tostring(fields.worldedit_gui_copy_move_amount)\r
+               gui_distance1[name] = tostring(fields.worldedit_gui_copy_move_amount)\r
                worldedit.show_page(name, "worldedit_gui_copy_move")\r
                if fields.worldedit_gui_copy_move_copy then\r
-                       minetest.chatcommands["/copy"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_length[name]))\r
+                       minetest.chatcommands["/copy"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_distance1[name]))\r
                else --fields.worldedit_gui_copy_move_move\r
-                       minetest.chatcommands["/move"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_length[name]))\r
+                       minetest.chatcommands["/move"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_distance1[name]))\r
                end\r
                return true\r
        end\r
index 8e3b3524b22bc4181e51911f97cacca4d12f6711..abcc134f1a7719ae43d0c9f885cc70cd5105191d 100644 (file)
@@ -1,4 +1,3 @@
---wip: make back buttons images in all screens
 --wip: support unified_inventory, it even seems to have some sort of API now
 --wip: make it look good with image buttons and stuff
 
@@ -114,6 +113,9 @@ worldedit.register_gui_function("worldedit_gui", {
                                end
                        end
                end
+               if index == 0 then --empty row
+                       y = y - height
+               end
                return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
                        (inventory_plus and "button[0,0;2,0.5;main;Back]" or "button_exit[0,0;2,0.5;main;Exit]") ..
                        "label[2,0;WorldEdit GUI]" ..