--[[
9-slice script for Teardown By Resident Emil
This script takes a vox-brush and slices it in nine parts; upper-left, upper, upper-right, right, bottom-right, bottom, bottom-left, left and center, making the vox-brush stretchable while retaining it's correct border
The brush is expected to be aligned on the x and z axes in MagicaVoxel, with y being the depth.

Parameters:
size:          The size of the brush. Use 's' with the object selected in the editor to change it size with the axis gizmos.
shape:         The path to the voxel brush to use
slices:        The borders of the slicing, the order being; top, right, bottom, and left.
snap:          Setting snap to 1 adjusts the size of the shape to fit the contents perfectly aligned according to the slices
flip:          Setting flip to 1 mirrors the shape in the x axis
--]]

sx, sy, sz = GetInt("size", 40, 18, 5)
brush = GetString("shape", "LEVEL/9slicewall.vox", "brush vox")

top, right, bottom, left = GetInt("slices", 2, 2, 2, 2)
snap = GetInt("snap", 0)
flip = GetInt("flip", 0)
centerWidth = 0
centerHeight = 0

function init()
	local shapeMat = CreateBrush(brush)
	--local debugMat = CreateMaterial("plaster", 0,1,0)
	local bx, by, _bz = GetBrushSize(shapeMat)
	if flip > 0 then
		FlipBrush(shapeMat,"x")
		TranslateBrush(shapeMat, 1,0,0)
	end
	centerWidth = bx - left - right
	centerHeight = by - top - bottom
	if snap > 0 then
		sx = math.ceil( (sx-left-right) / centerWidth ) * centerWidth + left + right
		sy = math.ceil( (sy-top-bottom) / centerHeight ) * centerHeight + top + bottom
	end
	
	Vox(0,0,0)
	Material(shapeMat)



	-- ################
	-- # LEFT         # 
	-- ################

	--Upper left corner
	-------------------
	Vox(0,sy-top,0)
	TranslateBrush(shapeMat, 0,-top,0)
	Box(0, 0, 0,  left, top, sz)
	TranslateBrush(shapeMat, 0,top,0) --Restore brush transform

	--Lower left corner
	-------------------
	Vox(0,0,0)
	Box(0, 0, 0,  left, bottom, sz)
	
	--Left
	------
	Vox(0,bottom,0)
	TranslateBrush(shapeMat, 0,bottom,0)
	local jcnt = 0
	for j = 0, sy-top, centerHeight do
		local ey = j+centerHeight
		if ey > sy-top-bottom then
			ey = sy-top-bottom
		end
		Box(0, j , 0, left, ey, sz)
		TranslateBrush(shapeMat, 0,-centerHeight,0)
		jcnt = jcnt + 1
	end
	TranslateBrush(shapeMat, 0,jcnt * centerHeight,0) --Restore brush transform
	TranslateBrush(shapeMat, 0,-bottom,0) --Restore brush transform
	



	-- ################
	-- # MIDDLE       # 
	-- ################
	local xcnt = 0
	local ex = 0
	local ey = 0
	TranslateBrush(shapeMat, left,0,0)
	for i = 0, sx-right, centerWidth do

		ex = i+centerWidth
		if ex > sx-left-right then
			ex = sx-left-right
		end

		-- Lower
		--------
		Vox(left,0,0)
		Box(i, 0, 0, ex, bottom, sz)
		
		-- Center
		---------
		Vox(left,bottom,0)
		TranslateBrush(shapeMat, 0,bottom,0)
		jcnt = 0
		for j = 0, sy-top, centerHeight do
			ey = j+centerHeight
			if ey > sy-top-bottom then
				ey = sy-top-bottom
			end
			Box(i, j, 0, ex,ey, sz)
			TranslateBrush(shapeMat, 0,-centerHeight,0)
			jcnt = jcnt  + 1
		end
		TranslateBrush(shapeMat, 0,jcnt*centerHeight-bottom,0) --Restore brush transform

		
		
		-- Upper 
		--------
		TranslateBrush(shapeMat, 0,-top,0)
		Vox(left,sy-top,0)
		Box(i, 0, 0,  ex, top, sz)
		TranslateBrush(shapeMat, 0,top,0) --Restore brush transform
		xcnt = xcnt + 1
		TranslateBrush(shapeMat, -centerWidth,0,0) 
	end

	
	TranslateBrush(shapeMat, -left,0,0) --Restore brush transform
	TranslateBrush(shapeMat, centerWidth*xcnt,0,0) --Restore brush transform
	


	-- ################
	-- # RIGHT        # 
	-- ################
	TranslateBrush(shapeMat, centerWidth+right,0,0)

	--Lower right corner
	--------------------
	Vox(sx-right,0,0)
	--Material(debugMat)
	--Box(0, 0, 0, 30,30,30)
	--Material(shapeMat)
	Box(0, 0, 0,  right, bottom, sz)

	--Right
	-------
	Vox(sx-right,bottom,0)
	TranslateBrush(shapeMat, 0,bottom,0)
	jcnt = 0
	for j = 0, sy-top, centerHeight do
		ey = j+centerHeight
		if ey > sy-top-bottom then
			ey = sy-top-bottom
		end
		Box(0, j, 0,  right, ey, sz)
		TranslateBrush(shapeMat, 0,-centerHeight,0)
		jcnt = jcnt + 1
	end
	TranslateBrush(shapeMat, 0,jcnt*centerHeight-bottom,0) --Restore brush transform

	--Upper right corner
	--------------------
	Vox(sx-right,sy-top,0)
	TranslateBrush(shapeMat, 0,-top,0)
	Box(0, 0, 0,  right, top, sz)
	TranslateBrush(shapeMat, 0,top,0) --Restore brush transform
	TranslateBrush(shapeMat, right,0,0) --Restore brush transform	
end