use_script("utils.lua");

-- Rotate a sprite so that the west side becomes the specified side.
function rotate_sprite(vsp, side)
    local S = 7  -- size-1 (e.g., 8x8x8)

    local function remap_func(x, y, z)
        local axis = side >> 1
        local orient = side & 1
        local nx, ny, nz = x, y, z

        if axis == 0 then
            -- target axis: X (west/east)
            if orient == 0 then
                -- west (-X): identity
            else
                -- east (+X): 180° rotation around Z (horizontal flip)
                nx = S - x
                ny = S - y
            end

        elseif axis == 1 then
            -- target axis: Y (south/north)
            if orient == 0 then
                -- south (-Y): +90° rotation around Z (west → south)
                nx = S - y
                ny = x
            else
                -- north (+Y): -90° rotation around Z (west → north)
                nx = y
                ny = S - x
            end

        elseif axis == 2 then
            -- target axis: Z (down/up)
            if orient == 0 then
                -- down (-Z): -90° rotation around Y (west → down)
                nx = S - z
                nz = x
            else
                -- up (+Z): +90° rotation around Y (west → up)
                nx = z
                nz = S - x
            end
        end

        return nx, ny, nz
    end

    return remap_voxel_positions(vsp, remap_func)
end