use_script("utils.lua");

-- Generates and exports bricks sprites.
-- `material` is the material of the bricks.
-- `mortar` is the material to use for mortar; optional.
function export_bricks(material, mortar)
    local MATERIAL_KEY = color_from_rgb(0, 255, 0);
    local MORTAR_KEY = color_from_rgb(0, 0, 255);

    local MORTAL_DEFAULT = {
        color = color_from_rgb(0x44, 0x44, 0x44),
        bias = color_from_rgb(0, 0, 8),
    };

    mortar = mortar or MORTAL_DEFAULT;
    
    local templ = vsp_from_assets("templates/bricks.vsp");
    
    for i = 0, 15 do
        local localTempl = vsp_clone(templ);
        vsp_pluck_sides(localTempl, i | (1 << 4) | (1 << 5), MORTAR_KEY);

        local color_values = {};
        color_values[MATERIAL_KEY] = material.color;
        color_values[MORTAR_KEY] = mortar.color;
        
        local bias_values = {};
        bias_values[MATERIAL_KEY] = material.bias;
        bias_values[MORTAR_KEY] = mortar.bias;

        local lightspec_values = {};
        lightspec_values[MATERIAL_KEY] = material.lightspec or 0;
        lightspec_values[MORTAR_KEY] = 0;

        local refract_values = {};
        refract_values[MATERIAL_KEY] = material.refract or 0;
        refract_values[MORTAR_KEY] = 0;
        
        local colormap = vsp_clone(localTempl);
        vsp_color_remap(colormap, color_values);
        
        local biasmap = vsp_clone(localTempl);
        vsp_color_remap(biasmap, bias_values);

        local lightspecmap = vsp_clone(localTempl);
        vsp_color_remap(lightspecmap, lightspec_values);

        local refractmap = localTempl;
        vsp_color_remap(refractmap, refract_values);
        
        export_frame(tobin(i, 4), {
            colormap = colormap,
            biasmap = biasmap,
            lightspecmap = lightspecmap,
            refractmap = refractmap,
        });
    end
end