
--[[ 
24-slice script for Teardown By Resident Emil
This script takes a vox-brush and slices it in 24 parts so that it can be stretched to whatever size in the editor, while retaining the design of the model.

Parameters:
size:          The size of the script's bounding box.
shape:         The path to the voxel brush to use.
object:        Optional name of a sub-object in the Magica Voxel file.
slices:        The thickness of the slices, the order being; left, righ, front, back, top, and bottom.
snap:          Setting snap to 1 adjusts the size of the shape to fit the contents perfectly aligned according to the slices, otherwise you may get cut of shapes.
flip:          Setting flip to 1 mirrors the shape in the x axis. This is on by default as the engine imports Magica Voxel files flipped in the x axis.
--]]

sx, sy, sz = GetInt("size", 40, 18, 5)
model = GetString("model", "MOD/brush/house.vox", "brush vox")

-- Selecting a sub-object from the Magica Voxel file isn't supported in the public build yet. Sorry about that :)
--[[
object = GetString("object")

if object ~= "" then
	model = model .. ":" .. object
end
--]]

right, left, front, back, top, bottom = GetInt("L_R_F_B_T_B", 1, 1, 1, 1, 1, 1)

snap = GetInt("snap", 1)
flip = GetInt("flip", 1)

function init()
	shapeMat = CreateBrush(model)
	bx, by, bz = GetBrushSize(shapeMat)
	if right < bx and left < bx and front < bz and back < bz and top < by and bottom < by then
		centerWidthX = bx - left - right
		centerHeightY = by - top - bottom
		centerWidthZ = bz - front - back

		if flip > 0 then
			FlipBrush(shapeMat,"x")
		end

		if snap > 0 then
			sx = math.ceil( (sx- left - right) / centerWidthX ) * centerWidthX +  left + right
			sy = math.ceil( (sy-top-bottom) / centerHeightY ) * centerHeightY + top + bottom
			sz = math.ceil( (sz- front - back) / centerWidthZ ) * centerWidthZ + front + back
		end

		segmentWidthX = {left,sx-left-right,right}
		segmentWidthY = {bottom,sy-top-bottom,top}
		segmentWidthZ = {front,sz-front-back,back}
		shapeWidthX = {left,centerWidthX,right}
		shapeHeightY = {bottom,centerHeightY,top}
		shapeWidthZ = {front,centerWidthZ,back}
		segmentPosX = {0,left,sx-right}
		segmentPosY = {0,bottom,sy-top}
		segmentPosZ = {0,front,sz-back}
		segmentOffsetX = {0,left,bx-right}
		segmentOffsetY = {0,bottom,by-top}
		segmentOffsetZ = {0,front,bz-back}

		for z = 1,3 do
			for x = 1,3 do
				for y = 1,3 do
					--[[
						The following condition removes the center pieces of the shape. This is because the floors in a building would create shapes with a lot of air in them, which creates unnecessary strain on the engine.
						I suggest you create the floors manually once you are happy with the exterior shape, as I have done in the included example.
						I will see if this can be mitigated in a future version.
						As of now, _DO_NOT_ remove this condition unless you really really know what you are doing and have a model which does not create a lot of air in the resulting shapes.
					--]]
					if not(x == 2 --[[and y == 2--]] and z == 2) then 
						subpart(segmentPosX[x], segmentPosY[y], segmentPosZ[z], segmentWidthX[x], segmentWidthY[y], segmentWidthZ[z], segmentOffsetX[x], segmentOffsetY[y], segmentOffsetZ[z], shapeWidthX[x], shapeHeightY[y], shapeWidthZ[z])
					else
						
					end
				end
			end
		end
	else
		print("Slices are out of bounds")
	end
end

function subpart(x,y,z,w,h,d,ox,oy,oz,sw,sh,sd)
	TranslateBrush(shapeMat, ox,oy,oz) -- Transform brush to line up with new segment
	local cz = 0
	while (cz < d) do
		local cx = 0
		while (cx < w) do
			local cy = 0
			while (cy < h) do
				Vox(x+cx,y+cy,z+cz)
				Material(shapeMat)
				local lsd = sd
				local lsw = sw
				local lsh = sh
				if cz + lsd > d then
					lsd = d - cz
				end
				if cx + lsw > w then
					lsw = w - cx
				end
				if cy + lsh > h then
					lsh = h - cy
				end
				Box(0,0,0,lsw,lsh,lsd)
				cy = cy + sh
			end
			cx = cx + sw
		end
		cz = cz + sd
	end
	TranslateBrush(shapeMat, -ox,-oy,-oz) --Restore brush transform
end