mirror of
				https://github.com/Uberi/Minetest-WorldEdit.git
				synced 2025-11-04 06:35:28 +01:00 
			
		
		
		
	fix spiral
This commit is contained in:
		
							
								
								
									
										10
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								README.md
									
									
									
									
									
								
							@@ -125,13 +125,13 @@ Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length>
 | 
			
		||||
    //cylinder z -12 3 mesecons:mesecon
 | 
			
		||||
    //cylinder ? 2 4 stone
 | 
			
		||||
    
 | 
			
		||||
### //spiral <size> <node>
 | 
			
		||||
### //spiral <width> <height> <spacer> <node>
 | 
			
		||||
 | 
			
		||||
Add spiral at WorldEdit position 1 with size <size>, composed of <node>.
 | 
			
		||||
Add spiral at WorldEdit position 1 with width <width>, height <height>, space between walls <spacer>, composed of <node>.
 | 
			
		||||
 | 
			
		||||
    //spiral 8 dirt
 | 
			
		||||
    //spiral 5 default:glass
 | 
			
		||||
    //spiral 2 stone
 | 
			
		||||
    //spiral 20 5 3 dirt
 | 
			
		||||
    //spiral 5 2 1 default:glass
 | 
			
		||||
    //spiral 7 1 5 stone
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### //copy x/y/z/? <amount>
 | 
			
		||||
 
 | 
			
		||||
@@ -238,21 +238,67 @@ worldedit.cylinder = function(pos, axis, length, radius, nodename)
 | 
			
		||||
	return count
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
--adds a spiral at `pos` with size `size`, composed of `nodename`, returning the number of nodes changed
 | 
			
		||||
worldedit.spiral = function(pos, size, nodename)
 | 
			
		||||
	local shift_x, shift_y
 | 
			
		||||
	sa = spiralt(size)
 | 
			
		||||
	shift_y = #sa -- "Height" of the Array
 | 
			
		||||
	local fe = sa[1]
 | 
			
		||||
	shift_x = #fe -- "Width" of the Array
 | 
			
		||||
	fe = nil
 | 
			
		||||
 | 
			
		||||
--adds a spiral at `pos` with width `width`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes changed
 | 
			
		||||
worldedit.spiral = function(pos, width, height, spacer, nodename)
 | 
			
		||||
	-- spiral matrix - http://rosettacode.org/wiki/Spiral_matrix#Lua
 | 
			
		||||
	av, sn = math.abs, function(s) return s~=0 and s/av(s) or 0 end
 | 
			
		||||
	local function sindex(z, x) -- returns the value at (x, z) in a spiral that starts at 1 and goes outwards
 | 
			
		||||
		if z == -x and z >= x then return (2*z+1)^2 end
 | 
			
		||||
		local l = math.max(av(z), av(x))
 | 
			
		||||
		return (2*l-1)^2+4*l+2*l*sn(x+z)+sn(z^2-x^2)*(l-(av(z)==l and sn(z)*x or sn(x)*z)) -- OH GOD WHAT
 | 
			
		||||
	end
 | 
			
		||||
	local function spiralt(side)
 | 
			
		||||
		local ret, id, start, stop = {}, 0, math.floor((-side+1)/2), math.floor((side-1)/2)
 | 
			
		||||
		for i = 1, side do
 | 
			
		||||
			for j = 1, side do
 | 
			
		||||
				local id = side^2 - sindex(stop - i + 1,start + j - 1)
 | 
			
		||||
				ret[id] = {x=i,z=j}
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
		return ret
 | 
			
		||||
	end
 | 
			
		||||
	-- connect the joined parts
 | 
			
		||||
	local spiral = spiralt(width)
 | 
			
		||||
	height = tonumber(height)
 | 
			
		||||
	if height < 1 then height = 1 end
 | 
			
		||||
	spacer = tonumber(spacer)-1
 | 
			
		||||
	if spacer < 1 then spacer = 1 end
 | 
			
		||||
	local count = 0
 | 
			
		||||
	local node = {name=nodename}
 | 
			
		||||
	for x, v in ipairs(sa) do
 | 
			
		||||
		for y, z in ipairs(v) do
 | 
			
		||||
			minetest.env:add_node({x=pos.x - shift_x + x,y=pos.y - shift_y + y,z=pos.z + z}, node)
 | 
			
		||||
			count = count + 1
 | 
			
		||||
	local np,lp
 | 
			
		||||
	for y=0,height do
 | 
			
		||||
		lp = nil
 | 
			
		||||
		for _,v in ipairs(spiral) do
 | 
			
		||||
			np = {x=pos.x+v.x*spacer, y=pos.y+y, z=pos.z+v.z*spacer}
 | 
			
		||||
			if lp~=nil then
 | 
			
		||||
				if lp.x~=np.x then 
 | 
			
		||||
					if lp.x<np.x then 
 | 
			
		||||
						for i=lp.x+1,np.x do
 | 
			
		||||
							minetest.env:add_node({x=i, y=np.y, z=np.z}, node)
 | 
			
		||||
							count = count + 1
 | 
			
		||||
						end
 | 
			
		||||
					else
 | 
			
		||||
						for i=np.x,lp.x-1 do
 | 
			
		||||
							minetest.env:add_node({x=i, y=np.y, z=np.z}, node)
 | 
			
		||||
							count = count + 1
 | 
			
		||||
						end
 | 
			
		||||
					end
 | 
			
		||||
				end
 | 
			
		||||
				if lp.z~=np.z then 
 | 
			
		||||
					if lp.z<np.z then 
 | 
			
		||||
						for i=lp.z+1,np.z do
 | 
			
		||||
							minetest.env:add_node({x=np.x, y=np.y, z=i}, node)
 | 
			
		||||
							count = count + 1
 | 
			
		||||
						end
 | 
			
		||||
					else
 | 
			
		||||
						for i=np.z,lp.z-1 do
 | 
			
		||||
							minetest.env:add_node({x=np.x, y=np.y, z=i}, node)
 | 
			
		||||
							count = count + 1
 | 
			
		||||
						end
 | 
			
		||||
					end
 | 
			
		||||
				end
 | 
			
		||||
			end
 | 
			
		||||
			lp = np
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	return count
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								init.lua
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								init.lua
									
									
									
									
									
								
							@@ -272,8 +272,8 @@ minetest.register_chatcommand("/hollowcylinder", {
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
minetest.register_chatcommand("/spiral", {
 | 
			
		||||
	params = "<size> <node>",
 | 
			
		||||
	description = "Add spiral at WorldEdit position 1 with size <size>, composed of <node>",
 | 
			
		||||
	params = "<width> <height> <space> <node>",
 | 
			
		||||
	description = "Add spiral at WorldEdit position 1 with width <width>, height <height>, space between walls <space>, composed of <node>",
 | 
			
		||||
	privs = {worldedit=true},
 | 
			
		||||
	func = function(name, param)
 | 
			
		||||
		local pos = worldedit.pos1[name]
 | 
			
		||||
@@ -282,7 +282,7 @@ minetest.register_chatcommand("/spiral", {
 | 
			
		||||
			return
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		local found, _, size, nodename = param:find("(%d+)%s+([^%s]+)$")
 | 
			
		||||
		local found, _, width, height, space, nodename = param:find("(%d+)%s+(%d+)%s+(%d+)%s+([^%s]+)$")
 | 
			
		||||
		if found == nil then
 | 
			
		||||
			minetest.chat_send_player(name, "Invalid usage: " .. param)
 | 
			
		||||
			return
 | 
			
		||||
@@ -292,7 +292,7 @@ minetest.register_chatcommand("/spiral", {
 | 
			
		||||
			return
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		local count = worldedit.spiral(pos, tonumber(size), nodename)
 | 
			
		||||
		local count = worldedit.spiral(pos, tonumber(width), tonumber(height), tonumber(space), nodename)
 | 
			
		||||
		minetest.chat_send_player(name, count .. " nodes changed")
 | 
			
		||||
	end,
 | 
			
		||||
})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user