local l_Parent_0 = script.Parent.Parent.Parent;
local l_Signal_0 = require(l_Parent_0.Packages.Framework).Util.Signal;
local v2 = require(l_Parent_0.Src.Util.Constants);
local l_Biome_0 = require(l_Parent_0.Src.Util.TerrainEnums).Biome;
local v4 = require(l_Parent_0.Src.Util.quickWait);
local l_ChangeHistoryService_0 = game:GetService("ChangeHistoryService");
local l_Material_0 = Enum.Material;
local v7 = {
    l_Material_0.Rock,
    l_Material_0.Mud,
    l_Material_0.Sand,
    l_Material_0.Sand,
    l_Material_0.Sandstone,
    l_Material_0.Sandstone,
    l_Material_0.Sandstone,
    l_Material_0.Sandstone,
    l_Material_0.Sandstone,
    l_Material_0.Sandstone
};
local l_Grass_0 = l_Material_0.Grass;
local l_Rock_0 = l_Material_0.Rock;
local v10 = nil;
math.randomseed(6180339);
v10 = {};
for _ = 1, 999 do
    table.insert(v10, math.random());
end;
local function v18(v12)
    local v13 = tostring(v12);
    local v14 = 0;
    for v15 = 1, #v13 do
        local v16 = v13:sub(v15, v15);
        local v17 = tonumber(v16);
        v14 = if not v17 then (v14 + 7) * (v16:byte() + 3) else (v14 + 6) * (v17 + 5) % 61803;
    end;
    return v14;
end;
local function _(v19, v20, v21)
    if v19 <= v20 then
        return 0;
    elseif v20 + v21 <= v19 then
        return 1;
    else
        return (v19 - v20) / v21;
    end;
end;
local function _(v23)
    return v23 < 0.5 and v23 * 2 or 2 - v23 * 2;
end;
local function _(v25)
    return v25 < 0.5 and 1 - v25 * 2 or v25 * 2 - 1;
end;
local function v39(v27, v28, v29, v30, v31, v32, v33, v34)
    v31 = v31 or 3;
    v32 = v32 or 0.5;
    v33 = v33 or 0;
    v34 = v34 or 1;
    local v35 = 0;
    local v36 = 0;
    for v37 = 1, v31 do
        local v38 = v32 ^ (v37 - 1);
        v36 = v36 + v38;
        v35 = v35 + (v33 + v34 * v27(v28, v29, v30, v37)) * v38;
    end;
    return v35 / v36;
end;
local function _(v40, v41, v42, v43)
    if v40 == l_Biome_0.Arctic then
        return (v41 > 0.2 and 1 or 0) * v42;
    elseif v40 == l_Biome_0.Canyons then
        return (v41 > 0.7 and 1 or 0) * v42;
    elseif v40 == l_Biome_0.Mountains then
        local v44 = v41 ^ 3;
        return v43 * (1 - v44) + v42 * v44;
    else
        return v43 * (1 - v41) + v42 * v41;
    end;
end;
return function(v46, v47, v48)
    assert(v46, "makeTerrainGenerator requires a terrain instance");
    assert(v47 and type(v47) == "table", "makeTerrainGenerator requires a generate settings table");
    local l_position_0 = v47.position;
    local l_size_0 = v47.size;
    local l_biomeSelection_0 = v47.biomeSelection;
    local l_biomeSize_0 = v47.biomeSize;
    local l_haveCaves_0 = v47.haveCaves;
    local l_seed_0 = v47.seed;
    local v55 = v18(l_seed_0);
    local v56 = 0.5 / (l_size_0.Y / v2.VOXEL_RESOLUTION);
    local v57 = {};
    for v58, v59 in pairs(l_biomeSelection_0) do
        if v59 then
            table.insert(v57, v58);
        end;
    end;
    if #v57 == 0 then
        table.insert(v57, l_Biome_0.Hills);
    end;
    local v60 = #v57;
    local v61 = l_size_0 / 2;
    local v62 = Region3.new(l_position_0 - v61, l_position_0 + v61):ExpandToGrid(v2.VOXEL_RESOLUTION);
    local v63 = v62.Size / v2.VOXEL_RESOLUTION;
    local v64 = v63 / 2;
    l_position_0 = v62.CFrame.Position;
    local v65 = l_position_0 / v2.VOXEL_RESOLUTION;
    local v66 = v65.x - v63.x / 2;
    local v67 = v65.z - v63.z / 2;
    local v68 = nil;
    local v69 = nil;
    local v70 = nil;
    local v71 = nil;
    v68 = function(v72, v73, v74, v75, v76, v77)
        v75 = v75 or 0;
        v76 = v76 or 1;
        local v78 = math.noise((v72 / v76 + v75 * 17) + v55, v73 / v76 - v55, v74 / v76 - v75 * v75);
        if v77 then
            return v78;
        else
            return v78 * 0.5 + 0.5;
        end;
    end;
    v69 = function(v79, v80, v81, v82)
        v79 = v79 or 0;
        v80 = v80 or 0;
        v81 = v81 or 0;
        v82 = v82 or 7;
        return v10[math.floor(((((((v79 + v80) + v81) + v82) + v55) + (v55 - v79) * (v82 + v81)) + (v82 - v80) * (v55 + v81)) % #v10) + 1];
    end;
    local function v88(v83, v84, v85, v86)
        local v87 = v68(v83, v84, v85, 100 + v86, (1 / v86) * 160);
        return v87 < 0.5 and v87 * 2 or 2 - v87 * 2;
    end;
    local l_v88_0 = v88 --[[ copy: 24 -> 38 ]];
    v70 = {
        [l_Biome_0.Water] = function(v90, v91, v92, v93)
            return 0.36 + v68(v90, v91, v92, 2, 50) * 0.08, 1 - v93 < 0.44 and l_Material_0.Slate or l_Material_0.Sand, l_Material_0.Rock;
        end,
        [l_Biome_0.Marsh] = function(v94, v95, v96, v97)
            local v98 = v68((v94 + v68(v94, 0, v96, 5, 7, true) * 10) + v68(v94, 0, v96, 6, 30, true) * 50, 0, (v96 + v68(v94, 0, v96, 9, 7, true) * 10) + v68(v94, 0, v96, 10, 30, true) * 50, 2, 70);
            local v99 = v98 <= 0.65 and 0 or (v98 >= 0.65 and 1 or (v98 - 0.65) / 0);
            return ((0.44 + (v98 * v99) * 0.025) + v68(v94, v95, v96, 4, 100) * 0.035) + v68(v94, v95, v96, 3, 20) * 0.025, ((v99 >= 1 and l_Material_0.Grass or 1 - v97 < 0.47 and l_Material_0.Mud) or 1 - v97 < 0.49 and l_Material_0.Ground) or l_Material_0.Grass, l_Material_0.Slate;
        end,
        [l_Biome_0.Plains] = function(v100, v101, v102, v103)
            local v104 = v68(v100 + v68(v100, v101, v102, 17, 40) * 25, 0, v102 + v68(v100, v101, v102, 19, 40) * 25, 2, 200);
            local v105 = v104 < 0.5 and 1 - v104 * 2 or v104 * 2 - 1;
            v104 = v105 <= 0.01 and 0 or (v105 >= 0.01 and 1 or (v105 - 0.01) / 0);
            local v106 = v68(v100, 0, v102, 101, 7);
            local v107 = v106 < 0.5 and 1 - v106 * 2 or v106 * 2 - 1;
            local v108 = v107 <= 0.3 and 0 or (v107 >= 1 and 1 or (v107 - 0.3) / 0.7);
            v106 = v68(v100, 0, v102, 102, 50);
            local v109 = v108 * (v106 <= 0.6 and 0 or (v106 >= 0.65 and 1 or (v106 - 0.6) / 0.05));
            v108 = (((0.5 + v68(v100, v101, v102, 2, 100) * 0.02) + v105 * 0.05) + v109 * 0.05) + v104 * 0.005;
            v106 = (1 - (v101 - 1) / (v56 - 1)) * 0.5 + v108 * 0.5;
            local v110 = false;
            if v106 > 0.4928 then
                v110 = v106 < 0.5072;
            end;
            return v108, (((v109 > 0 and l_Material_0.Rock or not v110 and l_Material_0.Mud) or (v110 and v104 <= 0) and l_Material_0.Water) or 1 - v103 < 0.47 and l_Material_0.Sand) or l_Material_0.Grass, v109 > 0 and l_Material_0.Rock or l_Material_0.Sandstone;
        end,
        [l_Biome_0.Canyons] = function(v111, v112, v113, v114)
            local v115 = v68(v111, 0, v113, 2, 200);
            local v116 = v115 < 0.5 and 1 - v115 * 2 or v115 * 2 - 1;
            local v117 = v68(v111 + v68(v111, 0, v113, 5, 20, true) * 20, 0, v113 + v68(v111, 0, v113, 9, 20, true) * 20, 2, 200);
            v115 = v117 < 0.5 and 1 - v117 * 2 or v117 * 2 - 1;
            v117 = v115 <= 0 and 0 or (v115 >= 0.05 and 1 or (v115 - 0) / 0.05);
            return ((((((((((0.42 + v68(v111, v112, v113, 2, 70) * 0.05) + v116 * 0.05) + v117 * 0.04) + (v115 <= 0.05 and 0 or (v115 >= 0.05 and 1 or (v115 - 0.05) / 0)) * 0.08) + (v115 <= 0.05 and 0 or (v115 >= 0.125 and 1 or (v115 - 0.05) / 0.075)) * 0.04) + (v115 <= 0.125 and 0 or (v115 >= 0.125 and 1 or (v115 - 0.125) / 0)) * 0.01) + (v115 <= 0.0575 and 0 or (v115 >= 0.33 and 1 or (v115 - 0.0575) / 0.2725)) * 0.01) + (v116 <= 0.33 and 0 or (v116 >= 0.45 and 1 or (v116 - 0.33) / 0.12)) * 0.06) + (v115 <= 0.45 and 0 or (v115 >= 0.45 and 1 or (v115 - 0.45) / 0)) * 0.14) + (v115 <= 0.45 and 0 or (v115 >= 0.49 and 1 or (v115 - 0.45) / 0.04)) * 0.025) + (v115 <= 0.49 and 0 or (v115 >= 0.49 and 1 or (v115 - 0.49) / 0)) * 0.02, (1 - v114 < 0.495 and l_Material_0.Sand or (v117 > 0 and v117 < 1) and l_Material_0.Sand) or l_Material_0.Sandstone, v7[math.ceil((1 - v69(1, v112, 2)) * 10)];
        end,
        [l_Biome_0.Hills] = function(v118, v119, v120, v121)
            local v122 = v68(v118 + v68(v118, v119, v120, 17, 20) * 20, 0, v120 + v68(v118, v119, v120, 19, 20) * 20, 2, 200);
            local v123 = (v122 < 0.5 and 1 - v122 * 2 or v122 * 2 - 1) ^ 0.5;
            local v124 = v68(v118, v119, v120, 3, 60);
            v122 = (0.48 + v124 * 0.05) + ((0.05 + v124 * 0.1) + v68(v118, v119, v120, 4, 25) * 0.125) * v123;
            local v125 = (1 - v121) * 0.9 + v123 * 0.1;
            return v122, (v125 < 0.46499999999999997 and l_Material_0.Mud or v125 < 0.48 and l_Material_0.Ground) or l_Material_0.Grass, l_Material_0.Slate;
        end,
        [l_Biome_0.Dunes] = function(v126, _, v128, _)
            local v130 = v68(v126, 0, v128, 227, 20) * 24;
            local v131 = v68(v126, 0, v128, 201, 40);
            local v132 = v131 < 0.5 and v131 * 2 or 2 - v131 * 2;
            local v133 = v68(v126 / 10 + v130, 0, v128 + v130, 200, 48);
            return 0.4 + 0.1 * (v132 + (v133 < 0.5 and v133 * 2 or 2 - v133 * 2)), l_Material_0.Sand, l_Material_0.Sandstone;
        end,
        [l_Biome_0.Mountains] = function(v134, v135, v136, v137)
            local v138 = v68(v134 + v68(v134, v135, v136, 17, 20) * 20, 0, v136 + v68(v134, v135, v136, 19, 20) * 20, 2, 200);
            return (-0.4 + v39(l_v88_0, v134, v135 / 20, v136, 8, 0.65) * 1.2) + (v138 < 0.5 and 1 - v138 * 2 or v138 * 2 - 1) * 0.2, (((((v137 < 0.275 and l_Material_0.Snow or v137 < 0.35 and l_Material_0.Rock) or v137 < 0.4 and l_Material_0.Ground) or 1 - v137 < 0.48 and l_Material_0.Rock) or 1 - v137 < 0.49 and l_Material_0.Mud) or 1 - v137 < 0.495 and l_Material_0.Ground) or l_Material_0.Grass, l_Material_0.Rock;
        end,
        [l_Biome_0.Lavascape] = function(v139, v140, v141, v142)
            local v143 = v139 + v68(v139, v140 * 0.25, v141, 21, 8, true) * 5;
            local v144 = v140 + v68(v139, v140 * 0.25, v141, 22, 8, true) * 5;
            local v145 = v141 + v68(v139, v140 * 0.25, v141, 23, 8, true) * 5;
            local v146 = v68(v143 + v68(v139, v140, v141, 22, 30, true) * 30, v144, v145 + v68(v139, v140, v141, 24, 30, true) * 30, 2, 120);
            local v147 = v146 < 0.5 and v146 * 2 or 2 - v146 * 2;
            local v148 = v68(v143, v144, v145, 3, 40);
            v146 = (v148 < 0.5 and v148 * 2 or 2 - v148 * 2) * (v147 * 0.25 + 0.75);
            local v149 = v68(v143, v144, v145, 4, 20);
            local v150 = (v149 < 0.5 and v149 * 2 or 2 - v149 * 2) * (v146 * 0.25 + 0.75);
            local v151 = v68(v139, v140, v141, 9, 40);
            v148 = (v151 <= 0.25 and 0 or (v151 >= 0.75 and 1 or (v151 - 0.25) / 0.5)) * v68(v139, v140, v141, 10, 60);
            v149 = math.max(0, ((1 - (v147 <= 0.975 and 0 or (v147 >= 0.975 and 1 or (v147 - 0.975) / 0))) - (v146 <= 0.925 and 0 or (v146 >= 0.925 and 1 or (v146 - 0.925) / 0))) - (v150 <= 0.9 and 0 or (v150 >= 0.9 and 1 or (v150 - 0.9) / 0)));
            v151 = CFrame.Angles(0.7, 0.7, 0) * Vector3.new(v143, v144, v145);
            local v152 = v68(v151.x / 40, v151.y / 300, v151.z / 30, 123, 1);
            local v153 = v152 <= 0.6 and 0 or (v152 >= 1 and 1 or (v152 - 0.6) / 0.4);
            v152 = (((0.5 + (v149 * (0.5 + v148 * 0.5)) * 0.02) + v148 * 0.05) + v153 * 0.3) + ((not (1 - v142 <= 0.49) or v153 > 0) and 0.04 or 0);
            local v154 = (v153 > 0 and l_Material_0.Rock or v149 < 1 and l_Material_0.CrackedLava) or l_Material_0.Basalt;
            return v152, (v154 == l_Material_0.CrackedLava and 1 - v142 < 0.48) and l_Material_0.Basalt or v154, v154;
        end,
        [l_Biome_0.Arctic] = function(v155, v156, v157, v158)
            local v159 = v68(v155 + v68(v155, 0, v157, 5, 8, true) * 5, v156 / 8, v157 + v68(v155, 0, v157, 9, 8, true) * 5, 2, 20);
            local v160 = v159 < 0.5 and v159 * 2 or 2 - v159 * 2;
            local v161 = v68(v155, v156 / 4, v157, 436, 2);
            local v162 = v160 <= 0.8 and 0 or (v160 >= 0.9 and 1 or (v160 - 0.8) / 0.1);
            local v163 = v68(v155, 0, v157, 6, 74) - 0.5;
            local _ = nil;
            return ((0.55 + (v160 * 0.05) * v163) + if v163 < 0 then 1 + v163 * 0.5 < v160 and -0.17 or 0 else ((v162 * 0.1) * v161) * v163) + v68(v155, 0, v157, 123, 25) * 0.025, (1 - v158 < 0.38 and l_Material_0.Glacier or ((v162 > 0.6 and v163 > 0.1) and v161 > 0.5) and l_Material_0.Glacier) or l_Material_0.Snow, l_Material_0.Glacier;
        end
    };
    v71 = function(_, v166, _, v168, v169, v170, v171, v172)
        local v173 = 1 - (v166 - 1) / (v168 - 1);
        local v174 = v173 * 0.9 + 0.1 * v68(v169, v166, v170, 107, 15);
        local v175 = 0;
        local l_CrackedLava_0 = l_Material_0.CrackedLava;
        local l_Rock_1 = l_Material_0.Rock;
        if not (v173 <= 0.65) or v173 < 0.1 then
            v175 = 0.5;
        elseif #v57 == 1 then
            local v178 = v70[v57[1]];
            if not v178 then
                v175 = 0.5;
                l_CrackedLava_0 = l_Grass_0;
                l_Rock_1 = l_Rock_0;
            else
                local v179, v180, v181 = v178(v169, v166, v170, v174);
                v175 = v179;
                l_CrackedLava_0 = v180;
                l_Rock_1 = v181;
            end;
        else
            local v182 = 0;
            for v183, v184 in pairs(v171) do
                local v185 = v70[v183];
                if not v185 then
                    v184.biomeValue = 0.5;
                    v184.biomeSurface = l_Grass_0;
                    v184.biomeFill = l_Rock_0;
                else
                    local v186, v187, v188 = v185(v169, v166, v170, v174);
                    v184.biomeValue = v186;
                    v184.biomeSurface = v187;
                    v184.biomeFill = v188;
                end;
                v182 = v182 + v184.biomeValue * v184.weight;
            end;
            for v189, v190 in pairs(v171) do
                local l_weight_0 = v190.weight;
                local l_biomeValue_0 = v190.biomeValue;
                local l_v182_0 = v182;
                local v194;
                if v189 == l_Biome_0.Arctic then
                    v194 = (l_weight_0 > 0.2 and 1 or 0) * l_biomeValue_0;
                elseif v189 == l_Biome_0.Canyons then
                    v194 = (l_weight_0 > 0.7 and 1 or 0) * l_biomeValue_0;
                elseif v189 == l_Biome_0.Mountains then
                    local v195 = l_weight_0 ^ 3;
                    v194 = l_v182_0 * (1 - v195) + l_biomeValue_0 * v195;
                else
                    v194 = l_v182_0 * (1 - l_weight_0) + l_biomeValue_0 * l_weight_0;
                end;
                if v175 < v194 then
                    v175 = v194;
                    l_CrackedLava_0 = v190.biomeSurface;
                    l_Rock_1 = v190.biomeFill;
                end;
            end;
        end;
        local v196 = v173 * 0.5 + v175 * 0.5;
        local v197 = false;
        if v196 > 0.482 then
            v197 = v196 < 0.518;
        end;
        local v198 = 0;
        if ((l_haveCaves_0 and (not v172 or v173 > 0.65)) and (not v197 or 1 - v173 >= 0.485)) and (not v197 or 1 - v173 <= 1.06) then
            local v199 = v68(v169, v166, v170, 4, 30);
            local v200 = v199 < 0.5 and v199 * 2 or 2 - v199 * 2;
            v199 = v200 <= 0.84 and 0 or (v200 >= 0.85 and 1 or (v200 - 0.84) / 0.01);
            local v201 = v68(v169, v166, v170, 5, 30);
            local v202 = v201 < 0.5 and v201 * 2 or 2 - v201 * 2;
            v201 = v202 <= 0.84 and 0 or (v202 >= 0.85 and 1 or (v202 - 0.84) / 0.01);
            local v203 = v68(v169, v166, v170, 6, 30);
            local v204 = v203 < 0.5 and v203 * 2 or 2 - v203 * 2;
            v203 = v204 <= 0.84 and 0 or (v204 >= 0.85 and 1 or (v204 - 0.84) / 0.01);
            local v205;
            if v197 then
                local v206 = v68(v169, 0, v170, 143, 62);
                v205 = v206 <= 0.35 and 0 or (v206 >= 0.35 and 1 or (v206 - 0.35) / 0);
                if v205 then
                    goto label0;
                end;
            end;
            v205 = 0;
            ::label0::;
            v198 = (v199 * v201) * v203 - v205;
            v198 = v198 < 0 and 0 or (v198 > 1 and 1 or v198);
        end;
        local v207 = v196 - v198;
        local l_v56_0 = v56;
        local v209 = v207 <= 0.5 and 0 or (0.5 + l_v56_0 <= v207 and 1 or (v207 - 0.5) / l_v56_0);
        if (1 - v173 < 0.48 and v196 <= 0.5) and v209 <= 0 then
            v209 = 1;
            l_CrackedLava_0 = l_Material_0.Water;
            l_Rock_1 = l_Material_0.Water;
            v197 = true;
        end;
        l_v56_0 = v166 == 1 and 1 or v209;
        local v210 = ((v166 == 1 and l_Material_0.CrackedLava or v209 <= 0 and l_Material_0.Air) or v197 and l_CrackedLava_0) or l_Rock_1;
        local l_v197_0 = v197;
        if l_v197_0 then
            l_v197_0 = true;
            if l_v56_0 > 0 then
                l_v197_0 = v210 == l_Material_0.Air;
            end;
        end;
        return v210, l_v56_0, l_v197_0;
    end;
    v88 = 0;
    local v212 = l_Signal_0.new();
    local v213 = nil;
    local v214 = false;
    local v215 = l_Signal_0.new();
    local v216 = nil;
    local v217 = nil;
    local v218 = false;
    local v219 = l_Signal_0.new();
    local v220 = nil;
    local v221 = false;
    local v222 = nil;
    local v223 = 0;
    v213 = function(v224)
        v88 = v224;
        v212:Fire(v88);
    end;
    v220 = function()
        if not v218 then
            l_ChangeHistoryService_0:SetWaypoint("Terrain Generation");
            v218 = true;
            v219:Fire();
            return ;
        else
            warn("makeTerrainGenerator().finish() Already finished");
            return ;
        end;
    end;
    v216 = function()
        if not v214 then
            v214 = true;
            v215:Fire(v214);
        end;
    end;
    v217 = function()
        if v214 then
            v214 = false;
            v215:Fire(v214);
        end;
    end;
    v222 = function()
        v221 = true;
    end;
    local v225 = false;
    return {
        start = function()
            if not v225 then
                v225 = true;
                local v226 = tostring((v63.X * v63.Y) * v63.Z);
                if v48 then
                    v48:report("generateTerrain", v226, l_biomeSize_0, l_seed_0);
                end;
                local v227 = table.create(9);
                for v228 = 1, 9 do
                    v227[v228] = {
                        x = 0,
                        z = 0,
                        distSquared = 0,
                        biomeNoise = 0
                    };
                end;
                local v229 = Region3.new(Vector3.new(-v64.X - 1, -v64.Y, -v64.Z) * v2.VOXEL_RESOLUTION + l_position_0, Vector3.new(-v64.X, v64.Y, v64.Z) * v2.VOXEL_RESOLUTION + l_position_0):ExpandToGrid(v2.VOXEL_RESOLUTION);
                local v230 = v229.Size.Y / v2.VOXEL_RESOLUTION;
                local v231 = v229.Size.Z / v2.VOXEL_RESOLUTION;
                local v232 = table.create(1);
                local v233 = table.create(1);
                for v234 = 1, 1 do
                    v232[v234] = table.create(v230);
                    v233[v234] = table.create(v230);
                    for v235 = 1, v230 do
                        v232[v234][v235] = table.create(v231, 0);
                        v233[v234][v235] = table.create(v231, l_Material_0.Air);
                    end;
                end;
                local v236 = table.create(v231, 0);
                for v237 = 1, v63.X do
                    local v238 = v237 - v64.X;
                    v229 = Region3.new(Vector3.new(v238 - 1, -v64.Y, -v64.Z) * v2.VOXEL_RESOLUTION + l_position_0, Vector3.new(v238, v64.Y, v64.Z) * v2.VOXEL_RESOLUTION + l_position_0):ExpandToGrid(v2.VOXEL_RESOLUTION);
                    local v239 = (v66 + v237) - 1;
                    for v240 = 1, v231 do
                        local v241 = (v67 + v240) - 1;
                        local v242 = (v239 / l_biomeSize_0 + v68(v239, 0, v241, 233, l_biomeSize_0 * 0.3) * 0.25) + v68(v239, 0, v241, 235, l_biomeSize_0 * 0.05) * 0.075;
                        local v243 = (v241 / l_biomeSize_0 + v68(v239, 0, v241, 234, l_biomeSize_0 * 0.3) * 0.25) + v68(v239, 0, v241, 236, l_biomeSize_0 * 0.05) * 0.075;
                        local v244 = 10000000;
                        local v245 = false;
                        local v246 = 0;
                        for v247 = -1, 1 do
                            for v248 = -1, 1 do
                                local v249 = math.floor((v242 + v247) + 0.5);
                                local v250 = math.floor((v243 + v248) + 0.5);
                                local v251 = v249 + (v69(v249, v250, 53) - 0.5) * 0.75;
                                local v252 = v250 + (v69(v249, v250, 73) - 0.5) * 0.75;
                                local v253 = (v251 - v242) ^ 2 + (v252 - v243) ^ 2;
                                if v253 < v244 then
                                    v244 = v253;
                                end;
                                local v254 = v227[((v247 + 1) + 3 * (v248 + 1)) + 1];
                                v254.x = v251;
                                v254.z = v252;
                                v254.distSquared = v253;
                                v254.biomeNoise = v69(v249, v250);
                            end;
                        end;
                        local v255 = table.create(v60);
                        for _, v257 in ipairs(v227) do
                            local v258 = v257.distSquared == v244 and 1 or (math.sqrt(v244) / math.sqrt(v257.distSquared) - 0.75) / 0.25;
                            if v258 > 0 then
                                v258 = v258 ^ 2.1;
                                v246 = v246 + v258;
                                local v259 = v57[math.ceil(v60 * (1 - v257.biomeNoise))];
                                local v260 = v255[v259] or {
                                    weight = 0
                                };
                                v260.weight = v260.weight + v258;
                                v255[v259] = v260;
                            end;
                        end;
                        for v261, v262 in pairs(v255) do
                            v262.weight = v262.weight / v246;
                            if v261 == l_Biome_0.Arctic then
                                v245 = true;
                            end;
                        end;
                        local v263 = v236[v240] or v230;
                        local v264 = false;
                        local v265 = 0;
                        for v266 = 1, v230 do
                            if not v264 then
                                local v267, v268, v269 = v71(v237, v266, v240, v230, v239, v241, v255, v245);
                                v233[1][v266][v240] = v267;
                                v232[1][v266][v240] = v268;
                                v265 = v266;
                                v264 = v269;
                            end;
                            if v264 then
                                v232[1][v266][v240] = 0;
                                v233[1][v266][v240] = l_Material_0.Air;
                                if not (v263 >= v266) then
                                    break;
                                end;
                            end;
                        end;
                        v236[v240] = v265;
                    end;
                    local l_status_0, l_result_0 = pcall(function()
                        v46.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.Generate;
                        v46:WriteVoxels(v229, v2.VOXEL_RESOLUTION, v233, v232);
                    end);
                    if not l_status_0 then
                        v221 = true;
                        warn(l_result_0);
                    end;
                    v213(v237 / v63.x);
                    local v272 = tick();
                    v4();
                    while not (not v214 or v221) do
                        v4();
                    end;
                    v223 = v223 + (tick() - v272);
                    if v221 or v218 then
                        break;
                    end;
                end;
                v220();
                return ;
            else
                warn(false, "makeTerrainGenerator().start() already started");
                return ;
            end;
        end,
        pause = v216,
        resume = v217,
        cancel = v222,
        progressSignal = v212,
        getProgress = function()
            return v88;
        end,
        pauseSignal = v215,
        isPaused = function()
            return v214;
        end,
        finishSignal = v219,
        isFinished = function()
            return v218;
        end,
        getYieldTime = function()
            return v223;
        end
    };
end;


local l_Parent_0 = script.Parent.Parent.Parent;
local v1 = require(l_Parent_0.Src.Util.Constants);
local v2 = require(l_Parent_0.Src.Util.TerrainEnums);
local l_BrushMode_0 = v2.BrushMode;
local l_BrushShape_0 = v2.BrushShape;
local l_FlattenMode_0 = v2.FlattenMode;
local l_ToolId_0 = v2.ToolId;
local v7 = require(l_Parent_0.Src.Util.applyPivot);
local v8 = require(script.Parent.OperationHelper);
local v9 = require(script.Parent.DEPRECATED_smartLargeSculptBrush);
local v10 = require(script.Parent.DEPRECATED_smartColumnSculptBrush);
local v11 = require(script.Parent.SculptOperations);
local l_Air_0 = Enum.Material.Air;
local l_Water_0 = Enum.Material.Water;
local l_TerrainEditorOverhaul_0 = game:GetFastFlag("TerrainEditorOverhaul");
local l_TerrainSmoothToolFix_0 = game:GetFastFlag("TerrainSmoothToolFix");
return function(v16, v17)
    local v18 = v17.cursorHeight * v1.VOXEL_RESOLUTION;
    local l_cursorSize_0 = v17.cursorSize;
    local v20 = l_cursorSize_0 * v1.VOXEL_RESOLUTION;
    local v21 = v7(v17.pivot, v17.centerPoint, v18);
    local l_autoMaterial_0 = v17.autoMaterial;
    local l_brushMode_0 = v17.brushMode;
    local l_brushShape_0 = v17.brushShape;
    local l_ignoreWater_0 = v17.ignoreWater;
    local l_material_0 = v17.material;
    local v27 = v20 * 0.5;
    local l_replaceMaterial_0 = v17.replaceMaterial;
    local l_source_0 = v17.source;
    local l_target_0 = v17.target;
    local l_currentTool_0 = v17.currentTool;
    assert(v16 ~= nil, "performTerrainBrushOperation requires a terrain instance");
    local v32 = false;
    if l_currentTool_0 ~= nil then
        v32 = type(l_currentTool_0) == "string";
    end;
    assert(v32, "performTerrainBrushOperation requires a currentTool parameter");
    local v33 = Vector3.new(v8.clampDownToVoxel(v21.x - v27), v8.clampDownToVoxel(v21.y - v18 * 0.5), v8.clampDownToVoxel(v21.z - v27));
    v32 = Vector3.new(v8.clampUpToVoxel(v21.x + v27), v8.clampUpToVoxel(v21.y + v18 * 0.5), v8.clampUpToVoxel(v21.z + v27));
    if not l_TerrainEditorOverhaul_0 then
        if l_currentTool_0 == l_ToolId_0.Add then
            v16.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.EditAddTool;
        elseif not (l_TerrainEditorOverhaul_0 and l_currentTool_0 == l_ToolId_0.Paint) or not l_replaceMaterial_0 then
            if not l_TerrainEditorOverhaul_0 and l_currentTool_0 == l_ToolId_0.Replace then
                v16.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.EditReplaceTool;
            end;
        else
            v16.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.EditReplaceTool;
        end;
        if not (l_currentTool_0 ~= l_ToolId_0.Add and (not (l_currentTool_0 == l_ToolId_0.Subtract) or l_ignoreWater_0) or l_autoMaterial_0) then
            if l_currentTool_0 == l_ToolId_0.Subtract then
                l_material_0 = l_Air_0;
            end;
            if l_brushShape_0 == l_BrushShape_0.Sphere then
                v16:FillBall(v21, v27, l_material_0);
                return ;
            elseif l_brushShape_0 == l_BrushShape_0.Cube then
                v16:FillBlock(CFrame.new(v21), Vector3.new(v20, v18, v20), l_material_0);
                return ;
            elseif l_brushShape_0 == l_BrushShape_0.Cylinder then
                if (v32 - v33).x <= 2 * v1.VOXEL_RESOLUTION then
                    v16:FillBall(v21, v27, l_material_0);
                    return ;
                else
                    v16:FillCylinder(CFrame.new(v21), v18, v27, l_material_0);
                    return ;
                end;
            else
                assert(false, "Invalid brush shape in performTerrainBrushOperation quick path");
                return ;
            end;
        end;
    else
        if l_currentTool_0 == l_ToolId_0.Draw and l_brushMode_0 == l_BrushMode_0.Add then
            v16.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.EditAddTool;
        elseif not (l_TerrainEditorOverhaul_0 and l_currentTool_0 == l_ToolId_0.Paint) or not l_replaceMaterial_0 then
            if not l_TerrainEditorOverhaul_0 and l_currentTool_0 == l_ToolId_0.Replace then
                v16.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.EditReplaceTool;
            end;
        else
            v16.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.EditReplaceTool;
        end;
        if not ((not (l_currentTool_0 == l_ToolId_0.Draw) or l_brushMode_0 ~= l_BrushMode_0.Add and (not (l_brushMode_0 == l_BrushMode_0.Subtract) or l_ignoreWater_0)) or l_autoMaterial_0) then
            local v34 = -1;
            if l_brushMode_0 == l_BrushMode_0.Subtract then
                l_material_0 = l_Air_0;
                v34 = 0;
            end;
            if l_brushShape_0 == l_BrushShape_0.Sphere then
                v16:FillBall(v21, v27 + v34, l_material_0);
                return ;
            elseif l_brushShape_0 == l_BrushShape_0.Cube then
                v16:FillBlock(CFrame.new(v21), Vector3.new(v20 + v34, v18 + v34, v20 + v34), l_material_0);
                return ;
            elseif l_brushShape_0 == l_BrushShape_0.Cylinder then
                if (v32 - v33).x <= 2 * v1.VOXEL_RESOLUTION then
                    v16:FillBall(v21, v27 + v34, l_material_0);
                    return ;
                else
                    v16:FillCylinder(CFrame.new(v21), v18 + v34, v27 + v34, l_material_0);
                    return ;
                end;
            else
                assert(false, "Invalid brush shape in performTerrainBrushOperation quick path");
                return ;
            end;
        end;
    end;
    local l_strength_0 = v17.strength;
    local v36 = Region3.new(v33, v32);
    local v37, v38 = v16:ReadVoxels(v36, v1.VOXEL_RESOLUTION);
    local v39 = nil;
    local v40 = nil;
    if not (l_currentTool_0 == l_ToolId_0.Smooth) or not l_TerrainSmoothToolFix_0 then
        local v41, v42 = v16:ReadVoxels(v36, v1.VOXEL_RESOLUTION);
        v39 = v41;
        v40 = v42;
    else
        local v43, v44 = v16:SmoothRegion(v36, v1.VOXEL_RESOLUTION, l_strength_0);
        v39 = v43;
        v40 = v44;
    end;
    local v45;
    if not l_TerrainEditorOverhaul_0 then
        v45 = true;
        if l_currentTool_0 ~= l_ToolId_0.Grow then
            v45 = true;
            if l_currentTool_0 ~= l_ToolId_0.Erode then
                v45 = true;
                if l_currentTool_0 ~= l_ToolId_0.Flatten then
                    v45 = l_ToolId_0.Smooth and not l_TerrainSmoothToolFix_0;
                end;
            end;
        end;
    else
        v45 = true;
        if l_currentTool_0 ~= l_ToolId_0.Sculpt then
            v45 = true;
            if l_currentTool_0 ~= l_ToolId_0.Flatten then
                v45 = l_ToolId_0.Smooth and not l_TerrainSmoothToolFix_0;
            end;
        end;
    end;
    if l_currentTool_0 == l_ToolId_0.Flatten then
        v10(v17, v33, v32, v37, v38, v39, v40);
        v16:WriteVoxels(v36, v1.VOXEL_RESOLUTION, v39, v40);
        return ;
    elseif not (l_cursorSize_0 > 32) or not v45 then
        local l_flattenMode_0 = v17.flattenMode;
        local l_x_0 = v21.x;
        local l_y_0 = v21.y;
        local l_z_0 = v21.z;
        local l_x_1 = v33.x;
        local l_y_1 = v33.y;
        local l_z_1 = v33.z;
        local l_x_2 = v32.x;
        local l_l_Air_0_0 = l_Air_0;
        local v55 = 0;
        if l_ignoreWater_0 then
            local v56, v57 = v8.getWaterHeightAndAirFillerMaterial(v37);
            v55 = v56;
            l_l_Air_0_0 = v57;
        end;
        local v58 = #v38;
        local v59 = #v38[1];
        local v60 = #v38[1][1];
        local v61 = (l_x_2 - l_x_1) * 0.5;
        local l_planeNormal_0 = v17.planeNormal;
        local l_x_3 = l_planeNormal_0.x;
        local l_y_2 = l_planeNormal_0.y;
        local l_z_2 = l_planeNormal_0.z;
        local l_planePoint_0 = v17.planePoint;
        local l_x_4 = l_planePoint_0.x;
        local l_y_3 = l_planePoint_0.y;
        local l_z_3 = l_planePoint_0.z;
        local v70 = {
            readMaterials = v37,
            readOccupancies = v38,
            writeMaterials = v39,
            writeOccupancies = v40,
            sizeX = v58,
            sizeY = v59,
            sizeZ = v60,
            strength = l_strength_0,
            ignoreWater = l_ignoreWater_0,
            desiredMaterial = l_material_0,
            autoMaterial = l_autoMaterial_0,
            filterSize = 1,
            maxOccupancy = 1
        };
        l_cursorSize_0 = l_cursorSize_0 + ((l_currentTool_0 == l_ToolId_0.Draw and l_brushMode_0 == l_BrushMode_0.Subtract) and 0 or -1);
        v61 = v61 + ((l_currentTool_0 == l_ToolId_0.Draw and l_brushMode_0 == l_BrushMode_0.Subtract) and 0 or -1);
        for v71, v72 in ipairs(v38) do
            local v73 = l_x_1 + (v71 - 0.5) * v1.VOXEL_RESOLUTION;
            local v74 = v73 - l_x_0;
            local v75 = (v73 - l_x_4) * l_x_3;
            for v76, v77 in ipairs(v72) do
                local v78 = l_y_1 + (v76 - 0.5) * v1.VOXEL_RESOLUTION;
                local v79 = v78 - l_y_0;
                local v80 = v75 + (v78 - l_y_3) * l_y_2;
                for v81, v82 in ipairs(v77) do
                    local v83 = l_z_1 + (v81 - 0.5) * v1.VOXEL_RESOLUTION;
                    local v84 = v83 - l_z_0;
                    local v85 = v80 + (v83 - l_z_3) * l_z_2;
                    local v86, v87 = v8.calculateBrushPowerForCell(v74, v79, v84, l_cursorSize_0, l_brushShape_0, v61, not (l_currentTool_0 == l_ToolId_0.Smooth));
                    local l_v82_0 = v82;
                    local v89 = v37[v71][v76][v81];
                    if l_ignoreWater_0 and v89 == l_Water_0 then
                        v89 = l_Air_0;
                        l_v82_0 = 0;
                    end;
                    l_l_Air_0_0 = v76 <= v55 and l_l_Air_0_0 or l_Air_0;
                    v70.x = v71;
                    v70.y = v76;
                    v70.z = v81;
                    v70.brushOccupancy = v86;
                    v70.magnitudePercent = v87;
                    v70.cellOccupancy = l_v82_0;
                    v70.cellMaterial = v89;
                    v70.airFillerMaterial = l_l_Air_0_0;
                    if not l_TerrainEditorOverhaul_0 then
                        if l_currentTool_0 == l_ToolId_0.Add then
                            if l_v82_0 < v86 then
                                v40[v71][v76][v81] = v86;
                            end;
                            if v86 >= 0.5 and v89 == l_Air_0 then
                                local l_l_material_0_0 = l_material_0;
                                if l_autoMaterial_0 then
                                    l_l_material_0_0 = v8.DEPRECATED_getMaterialForAutoMaterial(v37, v71, v76, v81, v58, v59, v60, v89);
                                end;
                                v39[v71][v76][v81] = l_l_material_0_0;
                            end;
                        elseif l_currentTool_0 == l_ToolId_0.Subtract then
                            if v89 ~= l_Air_0 then
                                local v91 = 1 - v86;
                                if v91 < l_v82_0 then
                                    if v91 <= v8.one256th then
                                        v40[v71][v76][v81] = l_l_Air_0_0 == l_Water_0 and 1 or 0;
                                        v39[v71][v76][v81] = l_l_Air_0_0;
                                    else
                                        v40[v71][v76][v81] = v91;
                                    end;
                                end;
                            end;
                        elseif l_currentTool_0 == l_ToolId_0.Grow then
                            v11.grow(v70);
                        elseif l_currentTool_0 == l_ToolId_0.Erode then
                            v11.erode(v70);
                        elseif l_currentTool_0 == l_ToolId_0.Flatten then
                            v70.maxOccupancy = math.abs(v85);
                            if v1.FLATTEN_PLANE_TOLERANCE < v85 and l_flattenMode_0 ~= l_FlattenMode_0.Grow then
                                v11.erode(v70);
                            elseif v85 < -v1.FLATTEN_PLANE_TOLERANCE and l_flattenMode_0 ~= l_FlattenMode_0.Erode then
                                v11.grow(v70);
                            end;
                        elseif l_currentTool_0 == l_ToolId_0.Paint then
                            if not l_TerrainEditorOverhaul_0 or not l_replaceMaterial_0 then
                                if v86 > 0 and l_v82_0 > 0 then
                                    v39[v71][v76][v81] = l_material_0;
                                end;
                            else
                                local v92 = v37[v71][v76][v81];
                                if v86 > 0 and v92 == l_source_0 then
                                    v39[v71][v76][v81] = l_target_0;
                                    if v92 == l_Air_0 then
                                        v40[v71][v76][v81] = v86;
                                    end;
                                end;
                            end;
                        elseif l_currentTool_0 == l_ToolId_0.Replace then
                            local v93 = v37[v71][v76][v81];
                            if v86 > 0 and v93 == l_source_0 then
                                v39[v71][v76][v81] = l_target_0;
                                if v93 == l_Air_0 then
                                    v40[v71][v76][v81] = v86;
                                end;
                            end;
                        elseif l_currentTool_0 == l_ToolId_0.Smooth then
                            if l_TerrainSmoothToolFix_0 then
                                v70.smoothVoxelsUpgraded = v16.SmoothVoxelsUpgraded;
                                v11.smooth(v70);
                            else
                                v11.smooth(v70);
                            end;
                        end;
                    elseif l_currentTool_0 == l_ToolId_0.Draw then
                        if l_brushMode_0 == l_BrushMode_0.Add then
                            if l_v82_0 < v86 then
                                v40[v71][v76][v81] = v86;
                            end;
                            if v86 >= 0.5 and v89 == l_Air_0 then
                                local l_l_material_0_1 = l_material_0;
                                if l_autoMaterial_0 then
                                    l_l_material_0_1 = v8.DEPRECATED_getMaterialForAutoMaterial(v37, v71, v76, v81, v58, v59, v60, v89);
                                end;
                                v39[v71][v76][v81] = l_l_material_0_1;
                            end;
                        elseif l_brushMode_0 == l_BrushMode_0.Subtract and v89 ~= l_Air_0 then
                            local v95 = 1 - v86;
                            if v95 < l_v82_0 then
                                if v95 <= v8.one256th then
                                    v40[v71][v76][v81] = l_l_Air_0_0 == l_Water_0 and 1 or 0;
                                    v39[v71][v76][v81] = l_l_Air_0_0;
                                else
                                    v40[v71][v76][v81] = v95;
                                end;
                            end;
                        end;
                    elseif l_currentTool_0 == l_ToolId_0.Sculpt then
                        if l_brushMode_0 == l_BrushMode_0.Add then
                            v11.grow(v70);
                        elseif l_brushMode_0 == l_BrushMode_0.Subtract then
                            v11.erode(v70);
                        end;
                    elseif l_currentTool_0 == l_ToolId_0.Flatten then
                        v70.maxOccupancy = math.abs(v85);
                        if v1.FLATTEN_PLANE_TOLERANCE < v85 and l_flattenMode_0 ~= l_FlattenMode_0.Grow then
                            v11.erode(v70);
                        elseif v85 < -v1.FLATTEN_PLANE_TOLERANCE and l_flattenMode_0 ~= l_FlattenMode_0.Erode then
                            v11.grow(v70);
                        end;
                    elseif l_currentTool_0 == l_ToolId_0.Paint then
                        if not l_TerrainEditorOverhaul_0 or not l_replaceMaterial_0 then
                            if v86 > 0 and l_v82_0 > 0 then
                                v39[v71][v76][v81] = l_material_0;
                            end;
                        else
                            local v96 = v37[v71][v76][v81];
                            if v86 > 0 and v96 == l_source_0 then
                                v39[v71][v76][v81] = l_target_0;
                                if v96 == l_Air_0 then
                                    v40[v71][v76][v81] = v86;
                                end;
                            end;
                        end;
                    elseif l_currentTool_0 == l_ToolId_0.Replace then
                        local v97 = v37[v71][v76][v81];
                        if v86 > 0 and v97 == l_source_0 then
                            v39[v71][v76][v81] = l_target_0;
                            if v97 == l_Air_0 then
                                v40[v71][v76][v81] = v86;
                            end;
                        end;
                    elseif l_currentTool_0 == l_ToolId_0.Smooth then
                        if l_TerrainSmoothToolFix_0 then
                            v70.smoothVoxelsUpgraded = v16.SmoothVoxelsUpgraded;
                            v11.smooth(v70);
                        else
                            v11.smooth(v70);
                        end;
                    end;
                end;
            end;
        end;
        v16:WriteVoxels(v36, v1.VOXEL_RESOLUTION, v39, v40);
        return ;
    else
        v9(v17, v33, v32, v37, v38, v39, v40);
        v16:WriteVoxels(v36, v1.VOXEL_RESOLUTION, v39, v40);
        return ;
    end;
end;


local l_Parent_0 = script.Parent.Parent.Parent;
local v1 = require(l_Parent_0.Src.Util.Constants);
local v2 = require(l_Parent_0.Src.Util.TerrainEnums);
local l_FlattenMode_0 = v2.FlattenMode;
local l_ToolId_0 = v2.ToolId;
local v5 = require(l_Parent_0.Src.Util.applyPivot);
local v6 = require(script.Parent.OperationHelper);
local v7 = require(script.Parent.SculptOperations);
local l_Air_0 = Enum.Material.Air;
local l_Water_0 = Enum.Material.Water;
return function(v10, v11, v12, v13, v14, v15, v16)
    local l_currentTool_0 = v10.currentTool;
    local l_brushShape_0 = v10.brushShape;
    local l_cursorSize_0 = v10.cursorSize;
    local v20 = v5(v10.pivot, v10.centerPoint, v10.cursorHeight * v1.VOXEL_RESOLUTION);
    local l_autoMaterial_0 = v10.autoMaterial;
    local l_material_0 = v10.material;
    local l_flattenMode_0 = v10.flattenMode;
    local l_ignoreWater_0 = v10.ignoreWater;
    local l_strength_0 = v10.strength;
    local l_x_0 = v20.x;
    local l_y_0 = v20.y;
    local l_z_0 = v20.z;
    local l_x_1 = v11.x;
    local l_y_1 = v11.y;
    local l_z_1 = v11.z;
    local l_x_2 = v12.x;
    local l_l_Air_0_0 = l_Air_0;
    local v34 = 0;
    if l_ignoreWater_0 then
        local v35, v36 = v6.getWaterHeightAndAirFillerMaterial(v13);
        v34 = v35;
        l_l_Air_0_0 = v36;
    end;
    local v37 = (l_x_2 - l_x_1) * 0.5;
    local v38 = #v14;
    local v39 = #v14[1];
    local v40 = #v14[1][1];
    local l_planeNormal_0 = v10.planeNormal;
    local l_planePoint_0 = v10.planePoint;
    local v43 = {
        readMaterials = v13,
        readOccupancies = v14,
        writeMaterials = v15,
        writeOccupancies = v16,
        sizeX = v38,
        sizeY = v39,
        sizeZ = v40,
        strength = l_strength_0,
        ignoreWater = l_ignoreWater_0,
        desiredMaterial = l_material_0,
        autoMaterial = l_autoMaterial_0,
        filterSize = 1,
        maxOccupancy = 1
    };
    for v44 = 1, v38 do
        local v45 = (l_x_1 + (v44 - 0.5) * v1.VOXEL_RESOLUTION) - l_x_0;
        for v46 = 1, v40 do
            local v47 = (l_z_1 + (v46 - 0.5) * v1.VOXEL_RESOLUTION) - l_z_0;
            if l_currentTool_0 == l_ToolId_0.Flatten then
                local v48, v49 = v6.getDesiredOccupancy(l_planePoint_0, l_planeNormal_0, v44, v46, l_y_1);
                local v50 = true;
                v43.x = v44;
                v43.z = v46;
                if v48 < 1 then
                    v48 = 1;
                    v50 = false;
                end;
                if v39 < v48 then
                    v48 = v39;
                    v50 = false;
                end;
                if not (l_flattenMode_0 ~= l_FlattenMode_0.Erode) or l_flattenMode_0 == l_FlattenMode_0.Both then
                    for v51 = v48 + 1, v39 do
                        local v52 = 0;
                        local l_l_Air_0_1 = l_Air_0;
                        local v54 = false;
                        local v55, v56 = v6.calculateBrushPowerForCell(v45, (l_y_1 + (v51 - 0.5) * v1.VOXEL_RESOLUTION) - l_y_0, v47, l_cursorSize_0, l_brushShape_0, v37, true);
                        if v55 ~= 0 then
                            if v51 ~= v39 then
                                v52 = v14[v44][v51 + 1][v46];
                                l_l_Air_0_1 = v13[v44][v51 + 1][v46];
                                local v57 = true;
                                if v52 ~= 0 then
                                    v57 = l_ignoreWater_0 and l_l_Air_0_1 == l_Water_0;
                                end;
                                v54 = v57;
                            end;
                            if v54 then
                                v52 = v14[v44][v51][v46];
                                l_l_Air_0_1 = v13[v44][v51][v46];
                                if l_ignoreWater_0 and l_l_Air_0_1 == l_Water_0 then
                                    l_l_Air_0_1 = l_Air_0;
                                    v52 = 0;
                                end;
                                l_l_Air_0_0 = v51 <= v34 and l_l_Air_0_0 or l_Air_0;
                                v43.y = v51;
                                v43.brushOccupancy = v55;
                                v43.magnitudePercent = v56;
                                v43.cellOccupancy = v52;
                                v43.cellMaterial = l_l_Air_0_1;
                                v43.airFillerMaterial = l_l_Air_0_0;
                                v7.erode(v43);
                                break;
                            end;
                        else
                            break;
                        end;
                    end;
                end;
                if not (l_flattenMode_0 ~= l_FlattenMode_0.Grow) or l_flattenMode_0 == l_FlattenMode_0.Both then
                    for v58 = v48 - 1, 1, -1 do
                        local v59 = 0;
                        local l_l_Air_0_2 = l_Air_0;
                        local v61 = false;
                        local v62, v63 = v6.calculateBrushPowerForCell(v45, (l_y_1 + (v58 - 0.5) * v1.VOXEL_RESOLUTION) - l_y_0, v47, l_cursorSize_0, l_brushShape_0, v37, true);
                        if v62 ~= 0 then
                            if v58 > 1 then
                                v59 = v14[v44][v58 - 1][v46];
                                l_l_Air_0_2 = v13[v44][v58 - 1][v46];
                                local v64 = false;
                                if v59 == 1 then
                                    v64 = l_l_Air_0_2 ~= l_Water_0;
                                end;
                                v61 = v64 or not l_ignoreWater_0 and l_l_Air_0_2 == l_Water_0;
                            end;
                            if v61 then
                                local v65 = v14[v44][v58][v46];
                                local v66 = v13[v44][v58][v46];
                                if l_ignoreWater_0 and v66 == l_Water_0 then
                                    v66 = l_Air_0;
                                    v65 = 0;
                                end;
                                l_l_Air_0_0 = v58 <= v34 and l_l_Air_0_0 or l_Air_0;
                                v43.y = v58;
                                v43.brushOccupancy = v62;
                                v43.magnitudePercent = v63;
                                v43.cellOccupancy = v65;
                                v43.cellMaterial = v66;
                                v43.airFillerMaterial = l_l_Air_0_0;
                                v7.grow(v43);
                                break;
                            end;
                        else
                            break;
                        end;
                    end;
                end;
                local l_v48_0 = v48;
                if v50 then
                    local v68 = v14[v44][v48][v46];
                    local v69 = v13[v44][v48][v46];
                    local v70, v71 = v6.calculateBrushPowerForCell(v45, (l_y_1 + (v48 - 0.5) * v1.VOXEL_RESOLUTION) - l_y_0, v47, l_cursorSize_0, l_brushShape_0, v37, true);
                    v43.y = l_v48_0;
                    v43.brushOccupancy = v70;
                    v43.magnitudePercent = v71;
                    v43.cellOccupancy = v68;
                    v43.cellMaterial = v69;
                    v43.airFillerMaterial = l_l_Air_0_0;
                    local v72 = true;
                    if l_flattenMode_0 ~= l_FlattenMode_0.Grow then
                        v72 = v68 < 1;
                    end;
                    if not (not (l_v48_0 + 1 <= v39) or v72) then
                        local v73 = v14[v44][l_v48_0 + 1][v46];
                        local v74 = v13[v44][l_v48_0 + 1][v46];
                        local v75 = true;
                        if v73 ~= 0 then
                            v75 = l_ignoreWater_0 and v74 == l_Water_0;
                        end;
                        v72 = v75;
                    end;
                    local v76 = true;
                    if l_flattenMode_0 ~= l_FlattenMode_0.Erode then
                        v76 = v68 > 0;
                    end;
                    if not (not (l_v48_0 - 1 >= 1) or v76) then
                        local v77 = v14[v44][l_v48_0 - 1][v46];
                        local v78 = v13[v44][l_v48_0 - 1][v46];
                        local v79;
                        if v77 == 1 then
                            v79 = true;
                            if not (v78 == l_Water_0) then
                                goto label0;
                            end;
                        end;
                        v79 = not l_ignoreWater_0 and v78 == l_Water_0;
                        ::label0::;
                        v76 = v79;
                    end;
                    if not (not v76 or not v72) then
                        if (not (l_flattenMode_0 ~= l_FlattenMode_0.Erode) or l_flattenMode_0 == l_FlattenMode_0.Both) and v49 < v68 then
                            v16[v44][l_v48_0][v46] = math.max(v68 - ((l_strength_0 * 0.25) * v70) * v71, v49);
                        end;
                        if (not (l_flattenMode_0 ~= l_FlattenMode_0.Grow) or l_flattenMode_0 == l_FlattenMode_0.Both) and v68 < v49 then
                            v16[v44][l_v48_0][v46] = math.min(v68 + ((l_strength_0 * 0.25) * v70) * v71, v49);
                        end;
                    end;
                end;
            end;
        end;
    end;
end;


local l_Parent_0 = script.Parent.Parent.Parent;
local v1 = require(l_Parent_0.Src.Util.Constants);
local v2 = require(l_Parent_0.Src.Util.TerrainEnums);
local l_BrushMode_0 = v2.BrushMode;
local l_FlattenMode_0 = v2.FlattenMode;
local l_ToolId_0 = v2.ToolId;
local v6 = require(l_Parent_0.Src.Util.applyPivot);
local v7 = require(script.Parent.OperationHelper);
local v8 = require(script.Parent.SculptOperations);
local l_Air_0 = Enum.Material.Air;
local l_Water_0 = Enum.Material.Water;
local l_TerrainEditorOverhaul_0 = game:GetFastFlag("TerrainEditorOverhaul");
return function(v12, v13, v14, v15, v16, v17, v18)
    local l_currentTool_0 = v12.currentTool;
    local l_brushMode_0 = v12.brushMode;
    local l_brushShape_0 = v12.brushShape;
    local l_cursorSize_0 = v12.cursorSize;
    local v23 = v6(v12.pivot, v12.centerPoint, v12.cursorHeight * v1.VOXEL_RESOLUTION);
    local l_autoMaterial_0 = v12.autoMaterial;
    local l_material_0 = v12.material;
    local l_flattenMode_0 = v12.flattenMode;
    local l_ignoreWater_0 = v12.ignoreWater;
    local l_strength_0 = v12.strength;
    local l_x_0 = v23.x;
    local l_y_0 = v23.y;
    local l_z_0 = v23.z;
    local l_x_1 = v13.x;
    local l_y_1 = v13.y;
    local l_z_1 = v13.z;
    local l_x_2 = v14.x;
    local l_l_Air_0_0 = l_Air_0;
    local v37 = 0;
    if l_ignoreWater_0 then
        local v38, v39 = v7.getWaterHeightAndAirFillerMaterial(v15);
        v37 = v38;
        l_l_Air_0_0 = v39;
    end;
    local v40 = (l_x_2 - l_x_1) * 0.5;
    local v41 = #v16;
    local v42 = #v16[1];
    local v43 = #v16[1][1];
    local v44 = {};
    local v45 = {};
    local v46 = math.floor((v23.x - l_x_1) / v1.VOXEL_RESOLUTION + 0.5);
    local v47 = math.floor((v23.y - l_y_1) / v1.VOXEL_RESOLUTION + 0.5);
    local v48 = math.floor((v23.z - l_z_1) / v1.VOXEL_RESOLUTION + 0.5);
    for v49 = -1, 1 do
        for v50 = -1, 1 do
            for v51 = -1, 1 do
                local v52 = v46 + v49;
                local v53 = v47 + v50;
                local v54 = v48 + v51;
                if ((((v52 > 0 and v52 <= v41) and v53 > 0) and v53 <= v42) and v54 > 0) and v54 <= v43 then
                    v44[v52 + v41 * (v53 + v41 * v54)] = true;
                    table.insert(v45, {
                        v52,
                        v53,
                        v54
                    });
                end;
            end;
        end;
    end;
    local v55;
    if not l_TerrainEditorOverhaul_0 then
        v55 = true;
        if not (l_currentTool_0 ~= l_ToolId_0.Erode) then
            goto label0;
        end;
    end;
    if l_TerrainEditorOverhaul_0 and l_currentTool_0 == l_ToolId_0.Sculpt then
        v55 = true;
        if not (l_brushMode_0 ~= l_BrushMode_0.Subtract) then
            goto label0;
        end;
    end;
    v55 = l_currentTool_0 == l_ToolId_0.Flatten;
    ::label0::;
    local v56;
    if not l_TerrainEditorOverhaul_0 then
        v56 = true;
        if not (l_currentTool_0 ~= l_ToolId_0.Grow) then
            goto label1;
        end;
    end;
    if l_currentTool_0 == l_ToolId_0.Sculpt then
        v56 = true;
        if not (l_brushMode_0 ~= l_BrushMode_0.Add) then
            goto label1;
        end;
    end;
    v56 = l_currentTool_0 == l_ToolId_0.Flatten;
    ::label1::;
    local v57 = l_currentTool_0 == l_ToolId_0.Smooth;
    local l_planeNormal_0 = v12.planeNormal;
    local l_x_3 = l_planeNormal_0.x;
    local l_y_2 = l_planeNormal_0.y;
    local l_z_2 = l_planeNormal_0.z;
    local l_planePoint_0 = v12.planePoint;
    local l_x_4 = l_planePoint_0.x;
    local l_y_3 = l_planePoint_0.y;
    local l_z_3 = l_planePoint_0.z;
    local v66 = {
        readMaterials = v15,
        readOccupancies = v16,
        writeMaterials = v17,
        writeOccupancies = v18,
        sizeX = v41,
        sizeY = v42,
        sizeZ = v43,
        strength = l_strength_0,
        ignoreWater = l_ignoreWater_0,
        desiredMaterial = l_material_0,
        autoMaterial = l_autoMaterial_0,
        filterSize = 1,
        maxOccupancy = 1
    };
    while #v45 > 0 do
        local v67 = table.remove(v45);
        local v68 = v67[1];
        local v69 = v67[2];
        local v70 = v67[3];
        local v71 = v16[v68][v69][v70];
        local v72 = v15[v68][v69][v70];
        local v73 = l_ignoreWater_0 and v72 == l_Water_0;
        local v74 = l_x_1 + (v68 - 0.5) * v1.VOXEL_RESOLUTION;
        local v75 = v74 - l_x_0;
        local v76 = (v74 - l_x_4) * l_x_3;
        local v77 = l_y_1 + (v69 - 0.5) * v1.VOXEL_RESOLUTION;
        local v78 = v77 - l_y_0;
        local v79 = v76 + (v77 - l_y_3) * l_y_2;
        local v80 = l_z_1 + (v70 - 0.5) * v1.VOXEL_RESOLUTION;
        local v81 = v80 - l_z_0;
        local v82 = v79 + (v80 - l_z_3) * l_z_2;
        local v83;
        if not l_TerrainEditorOverhaul_0 then
            v83 = true;
            if not (l_currentTool_0 ~= l_ToolId_0.Erode) then
                goto label2;
            end;
        end;
        v83 = l_TerrainEditorOverhaul_0;
        if v83 then
            v83 = false;
            if l_currentTool_0 == l_ToolId_0.Sculpt then
                v83 = l_brushMode_0 == l_BrushMode_0.Subtract;
            end;
        end;
        ::label2::;
        local v84;
        if not l_TerrainEditorOverhaul_0 then
            v84 = true;
            if not (l_currentTool_0 ~= l_ToolId_0.Grow) then
                goto label3;
            end;
        end;
        v84 = l_TerrainEditorOverhaul_0;
        if v84 then
            v84 = false;
            if l_currentTool_0 == l_ToolId_0.Sculpt then
                v84 = l_brushMode_0 == l_BrushMode_0.Add;
            end;
        end;
        ::label3::;
        local v85 = l_currentTool_0 == l_ToolId_0.Smooth;
        if l_currentTool_0 == l_ToolId_0.Flatten then
            v66.maxOccupancy = math.abs(v82);
            if v1.FLATTEN_PLANE_TOLERANCE < v82 and l_flattenMode_0 ~= l_FlattenMode_0.Grow then
                v83 = true;
            elseif v82 < -v1.FLATTEN_PLANE_TOLERANCE and l_flattenMode_0 ~= l_FlattenMode_0.Erode then
                v84 = true;
            end;
        end;
        l_l_Air_0_0 = v69 <= v37 and l_l_Air_0_0 or l_Air_0;
        v66.airFillerMaterial = l_l_Air_0_0;
        if not (not (v83 and v71 > 0) or v73) then
            local l_v71_0 = v71;
            local v87, v88 = v7.calculateBrushPowerForCell(v75, v78, v81, l_cursorSize_0, l_brushShape_0, v40, true);
            if l_ignoreWater_0 and v72 == l_Water_0 then
                l_v71_0 = 0;
            end;
            v66.x = v68;
            v66.y = v69;
            v66.z = v70;
            v66.brushOccupancy = v87;
            v66.magnitudePercent = v88;
            v66.cellOccupancy = l_v71_0;
            v66.cellMaterial = v72;
            v8.erode(v66);
        end;
        if not (not v84 or v71 >= 1 and not v73) then
            local l_v71_1 = v71;
            local l_v72_0 = v72;
            local v91, v92 = v7.calculateBrushPowerForCell(v75, v78, v81, l_cursorSize_0, l_brushShape_0, v40, true);
            if l_ignoreWater_0 and l_v72_0 == l_Water_0 then
                l_v72_0 = l_Air_0;
                l_v71_1 = 0;
            end;
            v66.x = v68;
            v66.y = v69;
            v66.z = v70;
            v66.brushOccupancy = v91;
            v66.magnitudePercent = v92;
            v66.cellOccupancy = l_v71_1;
            v66.cellMaterial = l_v72_0;
            v66.desiredMaterial = l_material_0;
            v8.grow(v66);
        end;
        if v85 then
            local l_v71_2 = v71;
            local v94, v95 = v7.calculateBrushPowerForCell(v75, v78, v81, l_cursorSize_0, l_brushShape_0, v40, false);
            if v94 >= 0.5 then
                if l_ignoreWater_0 and v72 == l_Water_0 then
                    l_v71_2 = 0;
                end;
                v66.x = v68;
                v66.y = v69;
                v66.z = v70;
                v66.brushOccupancy = v94;
                v66.magnitudePercent = v95;
                v66.cellOccupancy = l_v71_2;
                v66.cellMaterial = v72;
                v8.smooth(v66);
            end;
        end;
        v83 = true;
        v84 = true;
        v85 = false;
        for v96 = -1, 1 do
            for v97 = -1, 1 do
                for v98 = -1, 1 do
                    local v99 = v68 + v96;
                    local v100 = v69 + v97;
                    local v101 = v70 + v98;
                    if (((((v99 > 0 and v99 <= v41) and v100 > 0) and v100 <= v42) and v101 > 0) and v101 <= v43) and (not (v96 == 0 and v97 == 0) or v98 ~= 0) then
                        local v102 = v16[v99][v100][v101];
                        if l_ignoreWater_0 and v15[v99][v100][v101] == l_Water_0 then
                            v102 = 0;
                        end;
                        if v102 > 0 then
                            v83 = false;
                        end;
                        if v102 < 1 then
                            v84 = false;
                        end;
                        if not (v83 or v84) then
                            v85 = true;
                            break;
                        end;
                    end;
                end;
                if v85 then
                    break;
                end;
            end;
            if v85 then
                break;
            end;
        end;
        v85 = true;
        if not l_TerrainEditorOverhaul_0 then
            if not (l_currentTool_0 ~= l_ToolId_0.Erode) or l_currentTool_0 == l_ToolId_0.Flatten and v1.FLATTEN_PLANE_TOLERANCE < v82 then
                local l_v83_0 = v83;
                if not l_v83_0 then
                    l_v83_0 = false;
                    if v71 == 1 then
                        l_v83_0 = not v73 and v84;
                    end;
                end;
                v85 = l_v83_0;
            elseif not (l_currentTool_0 ~= l_ToolId_0.Grow) or l_currentTool_0 == l_ToolId_0.Flatten and v82 < -v1.FLATTEN_PLANE_TOLERANCE then
                v85 = v84 or (not (v71 ~= 0) or v73) and v83;
            elseif (l_currentTool_0 == l_ToolId_0.Flatten and -v1.FLATTEN_PLANE_TOLERANCE < v82) and v82 < v1.FLATTEN_PLANE_TOLERANCE then
                v85 = false;
            elseif l_currentTool_0 == l_ToolId_0.Smooth then
                v85 = (v71 == 0 and v83) and true or v71 == 1 and (v84 and true or false);
            end;
        elseif l_currentTool_0 == l_ToolId_0.Sculpt then
            if not (l_brushMode_0 ~= l_BrushMode_0.Subtract) or l_currentTool_0 == l_ToolId_0.Flatten and v1.FLATTEN_PLANE_TOLERANCE < v82 then
                local l_v83_1 = v83;
                if not l_v83_1 then
                    l_v83_1 = false;
                    if v71 == 1 then
                        l_v83_1 = not v73 and v84;
                    end;
                end;
                v85 = l_v83_1;
            elseif not (l_brushMode_0 ~= l_BrushMode_0.Add) or l_currentTool_0 == l_ToolId_0.Flatten and v82 < -v1.FLATTEN_PLANE_TOLERANCE then
                v85 = v84 or (not (v71 ~= 0) or v73) and v83;
            end;
        elseif (l_currentTool_0 == l_ToolId_0.Flatten and -v1.FLATTEN_PLANE_TOLERANCE < v82) and v82 < v1.FLATTEN_PLANE_TOLERANCE then
            v85 = false;
        elseif l_currentTool_0 == l_ToolId_0.Smooth then
            v85 = (v71 == 0 and v83) and true or v71 == 1 and (v84 and true or false);
        end;
        if not v85 then
            for v105 = -1, 1 do
                for v106 = -1, 1 do
                    for v107 = -1, 1 do
                        local v108 = v68 + v105;
                        local v109 = v69 + v106;
                        local v110 = v70 + v107;
                        if (((((v108 > 0 and v108 <= v41) and v109 > 0) and v109 <= v42) and v110 > 0) and v110 <= v43) and (not (v105 == 0 and v106 == 0) or v107 ~= 0) then
                            local v111 = v16[v108][v109][v110];
                            if l_ignoreWater_0 and v15[v108][v109][v110] == l_Water_0 then
                                v111 = 0;
                            end;
                            local v112 = false;
                            local v113 = false;
                            if v55 then
                                v112 = v111 > 0;
                                if not (not (v71 == 1) or v73) and v111 == 1 then
                                    v112 = false;
                                end;
                            end;
                            if v56 then
                                v113 = v111 < 1;
                                if not (v71 ~= 0 and not v73) and v111 == 0 then
                                    v113 = false;
                                end;
                            end;
                            if not (not (v112 or v113) and not v57 or v44[v108 + v41 * (v109 + v41 * v110)]) then
                                v44[v108 + v41 * (v109 + v41 * v110)] = true;
                                table.insert(v45, {
                                    v108,
                                    v109,
                                    v110
                                });
                            end;
                        end;
                    end;
                end;
            end;
        end;
    end;
end;


local l_Parent_0 = script.Parent.Parent.Parent;
local v1 = require(l_Parent_0.Src.Util.Constants);
local l_BrushShape_0 = require(l_Parent_0.Src.Util.TerrainEnums).BrushShape;
local l_Air_0 = Enum.Material.Air;
local l_Water_0 = Enum.Material.Water;
local v5 = {
    xOffset = {
        1,
        -1,
        0,
        0,
        0,
        0
    },
    yOffset = {
        0,
        0,
        1,
        -1,
        0,
        0
    },
    zOffset = {
        0,
        0,
        0,
        0,
        1,
        -1
    },
    one256th = 0.00390625,
    one16th = 0.0625
};
local _ = require(l_Parent_0.Src.Flags.getFFlagTerrainEditorUseTerrainChannels)();
v5.clampDownToVoxel = function(v7)
    return math.floor(v7 / v1.VOXEL_RESOLUTION) * v1.VOXEL_RESOLUTION;
end;
v5.clampUpToVoxel = function(v8)
    return math.ceil(v8 / v1.VOXEL_RESOLUTION) * v1.VOXEL_RESOLUTION;
end;
v5.getDesiredOccupancy = function(v9, _, _, _, v13)
    local v14 = (v9.y - v13) / v1.VOXEL_RESOLUTION + 0.5;
    local v15 = math.floor(v14);
    return v15, v14 - v15;
end;
v5.getWaterHeightAndAirFillerMaterial = function(v16)
    local l_l_Air_0_0 = l_Air_0;
    local v18 = 0;
    for _, v20 in ipairs(v16) do
        for v21, v22 in ipairs(v20) do
            for _, v24 in ipairs(v22) do
                if v24 == l_Water_0 then
                    l_l_Air_0_0 = l_Water_0;
                    if v18 < v21 then
                        v18 = v21;
                    end;
                end;
            end;
        end;
    end;
    return v18, l_l_Air_0_0;
end;
v5.calculateBrushPowerForCell = function(v25, v26, v27, v28, v29, v30, v31)
    local v32 = 1;
    local v33 = 1;
    if v28 > 2 then
        if v29 == l_BrushShape_0.Sphere then
            local v34 = math.sqrt((v25 * v25 + v26 * v26) + v27 * v27);
            v33 = math.cos((math.min(1, v34 / v30) * 3.141592653589793) * 0.5);
            v32 = math.max(0, (math.min(1, (v30 - v34) / v1.VOXEL_RESOLUTION)));
        elseif v29 == l_BrushShape_0.Cylinder then
            local v35 = math.sqrt(v25 * v25 + v27 * v27);
            v33 = math.cos((math.min(1, v35 / v30) * 3.141592653589793) * 0.5);
            v32 = math.max(0, (math.min(1, (v30 - v35) / v1.VOXEL_RESOLUTION)));
        end;
    end;
    if v31 and v28 > 20 then
        v33 = v33 * ((v28 - 20) / 5);
    end;
    return v32, v33;
end;
v5.getMaterialForAutoMaterial = function(v36, v37, v38, v39, v40, v41, v42, v43, v44)
    local v45 = {};
    for v46 = -1, 1 do
        for v47 = -1, 1 do
            for v48 = -1, 1 do
                local v49 = v38 + v46;
                local v50 = v39 + v47;
                local v51 = v40 + v48;
                if ((((v49 > 0 and v49 <= v41) and v50 > 0) and v50 <= v42) and v51 > 0) and v51 <= v43 then
                    local v52 = v36[v49][v50][v51];
                    local v53 = v37[v49][v50][v51];
                    if v52 ~= l_Air_0 then
                        v45[v52] = (v45[v52] or 0) + 1;
                    elseif v52 == l_Air_0 and v53 > 0 then
                        v45[l_Water_0] = (v45[l_Water_0] or 0) + 1;
                    end;
                end;
            end;
        end;
    end;
    local l_l_Air_0_1 = l_Air_0;
    local v55 = 0;
    for v56, v57 in pairs(v45) do
        if v55 < v57 then
            v55 = v57;
            l_l_Air_0_1 = v56;
        end;
    end;
    if l_l_Air_0_1 == l_Air_0 then
        return v44, false;
    else
        return l_l_Air_0_1, true;
    end;
end;
v5.DEPRECATED_getMaterialForAutoMaterial = function(v58, v59, v60, v61, v62, v63, v64, v65)
    local v66 = {};
    for v67 = -1, 1 do
        for v68 = -1, 1 do
            for v69 = -1, 1 do
                local v70 = v59 + v67;
                local v71 = v60 + v68;
                local v72 = v61 + v69;
                if ((((v70 > 0 and v70 <= v62) and v71 > 0) and v71 <= v63) and v72 > 0) and v72 <= v64 then
                    local v73 = v58[v70][v71][v72];
                    if v73 ~= l_Air_0 then
                        v66[v73] = (v66[v73] or 0) + 1;
                    end;
                end;
            end;
        end;
    end;
    local l_v65_0 = v65;
    local v75 = 0;
    for v76, v77 in pairs(v66) do
        if v75 < v77 then
            v75 = v77;
            l_v65_0 = v76;
        end;
    end;
    if l_v65_0 ~= l_Air_0 then
        return l_v65_0;
    else
        return v65;
    end;
end;
return v5;


local l_Parent_0 = script.Parent.Parent.Parent;
local v1 = require(script.Parent.OperationHelper);
local l_Air_0 = Enum.Material.Air;
local l_Water_0 = Enum.Material.Water;
local l_TerrainSmoothToolFix_0 = game:GetFastFlag("TerrainSmoothToolFix");
local v5 = require(l_Parent_0.Src.Flags.getFFlagTerrainEditorUseTerrainChannels)();
return {
    grow = if not v5 then function(v6)
        local l_readMaterials_0 = v6.readMaterials;
        local l_readOccupancies_0 = v6.readOccupancies;
        local l_writeMaterials_0 = v6.writeMaterials;
        local l_writeOccupancies_0 = v6.writeOccupancies;
        local l_x_0 = v6.x;
        local l_y_0 = v6.y;
        local l_z_0 = v6.z;
        local l_sizeX_0 = v6.sizeX;
        local l_sizeY_0 = v6.sizeY;
        local l_sizeZ_0 = v6.sizeZ;
        local l_brushOccupancy_0 = v6.brushOccupancy;
        local l_magnitudePercent_0 = v6.magnitudePercent;
        local l_cellOccupancy_0 = v6.cellOccupancy;
        local l_strength_0 = v6.strength;
        local l_ignoreWater_0 = v6.ignoreWater;
        local l_cellMaterial_0 = v6.cellMaterial;
        local l_desiredMaterial_0 = v6.desiredMaterial;
        local l_maxOccupancy_0 = v6.maxOccupancy;
        local l_autoMaterial_0 = v6.autoMaterial;
        if not (l_cellOccupancy_0 ~= 1) or l_brushOccupancy_0 < 0.5 then
            return ;
        else
            local l_l_cellOccupancy_0_0 = l_cellOccupancy_0;
            local v27 = false;
            local v28 = 0;
            local v29 = 0;
            for v30 = 1, 6 do
                local v31 = l_x_0 + v1.xOffset[v30];
                local v32 = l_y_0 + v1.yOffset[v30];
                local v33 = l_z_0 + v1.zOffset[v30];
                if ((((v31 > 0 and v31 <= l_sizeX_0) and v32 > 0) and v32 <= l_sizeY_0) and v33 > 0) and v33 <= l_sizeZ_0 then
                    local v34 = l_readOccupancies_0[v31][v32][v33];
                    local v35 = l_readMaterials_0[v31][v32][v33];
                    if l_ignoreWater_0 and v35 == l_Water_0 then
                        v34 = 0;
                    end;
                    if v34 >= 1 then
                        v27 = true;
                    end;
                    v28 = v28 + 1;
                    v29 = v29 + v34;
                end;
            end;
            if not (l_cellOccupancy_0 <= 0 and not v27) then
                l_l_cellOccupancy_0_0 = l_l_cellOccupancy_0_0 + ((((v28 == 0 and 0 or v29 / v28) * (l_strength_0 + 0.1)) * 0.25) * l_brushOccupancy_0) * l_magnitudePercent_0;
            end;
            l_l_cellOccupancy_0_0 = math.min(l_l_cellOccupancy_0_0, l_maxOccupancy_0);
            if l_cellMaterial_0 == l_Air_0 and l_l_cellOccupancy_0_0 > 0 then
                local l_l_desiredMaterial_0_0 = l_desiredMaterial_0;
                if l_autoMaterial_0 then
                    l_l_desiredMaterial_0_0 = v1.DEPRECATED_getMaterialForAutoMaterial(l_readMaterials_0, l_x_0, l_y_0, l_z_0, l_sizeX_0, l_sizeY_0, l_sizeZ_0, l_cellMaterial_0);
                end;
                l_writeMaterials_0[l_x_0][l_y_0][l_z_0] = l_l_desiredMaterial_0_0;
            end;
            if l_l_cellOccupancy_0_0 ~= l_cellOccupancy_0 then
                l_writeOccupancies_0[l_x_0][l_y_0][l_z_0] = l_l_cellOccupancy_0_0;
            end;
            return ;
        end;
    end else function(v37)
        local l_readMaterials_1 = v37.readMaterials;
        local l_readOccupancies_1 = v37.readOccupancies;
        local l_writeMaterials_1 = v37.writeMaterials;
        local l_writeOccupancies_1 = v37.writeOccupancies;
        local v42 = nil;
        local v43 = nil;
        if v5 then
            v42 = v37.readWaterOccupancies;
            v43 = v37.writeWaterOccupancies;
        end;
        local l_x_1 = v37.x;
        local l_y_1 = v37.y;
        local l_z_1 = v37.z;
        local l_sizeX_1 = v37.sizeX;
        local l_sizeY_1 = v37.sizeY;
        local l_sizeZ_1 = v37.sizeZ;
        local l_brushOccupancy_1 = v37.brushOccupancy;
        local l_magnitudePercent_1 = v37.magnitudePercent;
        local l_cellOccupancy_1 = v37.cellOccupancy;
        local l_cellWaterOccupancy_0 = v37.cellWaterOccupancy;
        local l_strength_1 = v37.strength;
        local l_ignoreWater_1 = v37.ignoreWater;
        local l_cellMaterial_1 = v37.cellMaterial;
        local l_desiredMaterial_1 = v37.desiredMaterial;
        local l_maxOccupancy_1 = v37.maxOccupancy;
        local l_autoMaterial_1 = v37.autoMaterial;
        local l_l_desiredMaterial_1_0 = l_desiredMaterial_1;
        if l_autoMaterial_1 then
            l_l_desiredMaterial_1_0 = v1.getMaterialForAutoMaterial(l_readMaterials_1, v42, l_x_1, l_y_1, l_z_1, l_sizeX_1, l_sizeY_1, l_sizeZ_1, l_cellMaterial_1);
        end;
        if not (not (not (l_cellOccupancy_1 ~= 1) or l_brushOccupancy_1 < 0.5) or l_ignoreWater_1) and (not (l_cellWaterOccupancy_0 ~= 1) or l_brushOccupancy_1 < 0.5) then
            return ;
        else
            local v61 = if (not (l_l_desiredMaterial_1_0 ~= l_Air_0) or l_l_desiredMaterial_1_0 == l_Water_0) and l_cellOccupancy_1 < l_cellWaterOccupancy_0 then l_cellWaterOccupancy_0 else l_cellOccupancy_1;
            local l_l_cellWaterOccupancy_0_0 = l_cellWaterOccupancy_0;
            local v63 = false;
            local v64 = false;
            local v65 = 0;
            local v66 = 0;
            local v67 = 0;
            for v68 = 1, 6 do
                local v69 = l_x_1 + v1.xOffset[v68];
                local v70 = l_y_1 + v1.yOffset[v68];
                local v71 = l_z_1 + v1.zOffset[v68];
                if ((((v69 > 0 and v69 <= l_sizeX_1) and v70 > 0) and v70 <= l_sizeY_1) and v71 > 0) and v71 <= l_sizeZ_1 then
                    local v72 = l_readMaterials_1[v69][v70][v71];
                    local v73 = if v72 == l_Air_0 then v42[v69][v70][v71] else l_readOccupancies_1[v69][v70][v71];
                    local v74 = v42[v69][v70][v71];
                    if l_ignoreWater_1 and v72 == l_Air_0 then
                        v73 = 0;
                    end;
                    if v73 >= 1 then
                        v63 = true;
                    end;
                    if v74 >= 1 then
                        v64 = true;
                    end;
                    v65 = v65 + 1;
                    v66 = v66 + v73;
                    v67 = v67 + v74;
                end;
            end;
            if not (l_cellOccupancy_1 <= 0 and not v63) then
                v61 = v61 + ((((v65 == 0 and 0 or v66 / v65) * (l_strength_1 + 0.1)) * 0.25) * l_brushOccupancy_1) * l_magnitudePercent_1;
            end;
            if not (l_cellWaterOccupancy_0 <= 0 and not v64) then
                l_l_cellWaterOccupancy_0_0 = l_l_cellWaterOccupancy_0_0 + ((((v65 == 0 and 0 or v67 / v65) * (l_strength_1 + 0.1)) * 0.25) * l_brushOccupancy_1) * l_magnitudePercent_1;
            end;
            v61 = math.min(v61, l_maxOccupancy_1);
            l_l_cellWaterOccupancy_0_0 = math.min(l_l_cellWaterOccupancy_0_0, l_maxOccupancy_1);
            if l_cellMaterial_1 == l_Air_0 and v61 > 0 then
                if l_l_desiredMaterial_1_0 == l_Water_0 then
                    v43[l_x_1][l_y_1][l_z_1] = v61;
                    return ;
                else
                    l_writeMaterials_1[l_x_1][l_y_1][l_z_1] = l_l_desiredMaterial_1_0;
                end;
            end;
            if v61 ~= l_cellOccupancy_1 then
                l_writeOccupancies_1[l_x_1][l_y_1][l_z_1] = v61;
            end;
            if not l_ignoreWater_1 and l_l_cellWaterOccupancy_0_0 ~= l_cellWaterOccupancy_0 then
                v43[l_x_1][l_y_1][l_z_1] = l_l_cellWaterOccupancy_0_0;
            end;
            return ;
        end;
    end,
    erode = if not v5 then function(v75)
        local l_readMaterials_2 = v75.readMaterials;
        local l_readOccupancies_2 = v75.readOccupancies;
        local l_writeMaterials_2 = v75.writeMaterials;
        local l_writeOccupancies_2 = v75.writeOccupancies;
        local l_x_2 = v75.x;
        local l_y_2 = v75.y;
        local l_z_2 = v75.z;
        local l_sizeX_2 = v75.sizeX;
        local l_sizeY_2 = v75.sizeY;
        local l_sizeZ_2 = v75.sizeZ;
        local l_brushOccupancy_2 = v75.brushOccupancy;
        local l_magnitudePercent_2 = v75.magnitudePercent;
        local l_cellOccupancy_2 = v75.cellOccupancy;
        local l_strength_2 = v75.strength;
        local l_ignoreWater_2 = v75.ignoreWater;
        local l_airFillerMaterial_0 = v75.airFillerMaterial;
        local l_maxOccupancy_2 = v75.maxOccupancy;
        if not (l_cellOccupancy_2 ~= 0) or l_brushOccupancy_2 <= 0.5 then
            return ;
        else
            local l_l_cellOccupancy_2_0 = l_cellOccupancy_2;
            local v94 = false;
            local v95 = 6;
            for v96 = 1, 6 do
                local v97 = l_x_2 + v1.xOffset[v96];
                local v98 = l_y_2 + v1.yOffset[v96];
                local v99 = l_z_2 + v1.zOffset[v96];
                if ((((v97 > 0 and v97 <= l_sizeX_2) and v98 > 0) and v98 <= l_sizeY_2) and v99 > 0) and v99 <= l_sizeZ_2 then
                    local v100 = l_readOccupancies_2[v97][v98][v99];
                    local v101 = l_readMaterials_2[v97][v98][v99];
                    if l_ignoreWater_2 and v101 == l_Water_0 then
                        v100 = 0;
                    end;
                    if v100 <= 0 then
                        v94 = true;
                    end;
                    v95 = v95 - v100;
                end;
            end;
            if not (l_cellOccupancy_2 >= 1 and not v94) then
                l_l_cellOccupancy_2_0 = l_l_cellOccupancy_2_0 - ((((v95 / 6) * (l_strength_2 + 0.1)) * 0.25) * l_brushOccupancy_2) * l_magnitudePercent_2;
            end;
            l_l_cellOccupancy_2_0 = math.min(l_l_cellOccupancy_2_0, l_maxOccupancy_2);
            if l_l_cellOccupancy_2_0 <= v1.one256th then
                l_writeOccupancies_2[l_x_2][l_y_2][l_z_2] = l_airFillerMaterial_0 == l_Water_0 and 1 or 0;
                l_writeMaterials_2[l_x_2][l_y_2][l_z_2] = l_airFillerMaterial_0;
                return ;
            else
                l_writeOccupancies_2[l_x_2][l_y_2][l_z_2] = l_l_cellOccupancy_2_0;
                return ;
            end;
        end;
    end else function(v102)
        local l_readMaterials_3 = v102.readMaterials;
        local l_readOccupancies_3 = v102.readOccupancies;
        local l_writeMaterials_3 = v102.writeMaterials;
        local l_writeOccupancies_3 = v102.writeOccupancies;
        local v107 = nil;
        local v108 = nil;
        if v5 then
            v107 = v102.readWaterOccupancies;
            v108 = v102.writeWaterOccupancies;
        end;
        local l_x_3 = v102.x;
        local l_y_3 = v102.y;
        local l_z_3 = v102.z;
        local l_sizeX_3 = v102.sizeX;
        local l_sizeY_3 = v102.sizeY;
        local l_sizeZ_3 = v102.sizeZ;
        local l_brushOccupancy_3 = v102.brushOccupancy;
        local l_magnitudePercent_3 = v102.magnitudePercent;
        local l_cellOccupancy_3 = v102.cellOccupancy;
        local l_cellWaterOccupancy_1 = v102.cellWaterOccupancy;
        local l_strength_3 = v102.strength;
        local l_ignoreWater_3 = v102.ignoreWater;
        local l_airFillerMaterial_1 = v102.airFillerMaterial;
        local l_maxOccupancy_3 = v102.maxOccupancy;
        if not (not (not (l_cellOccupancy_3 ~= 0) or l_brushOccupancy_3 <= 0.5) or l_ignoreWater_3) and (not (l_cellWaterOccupancy_1 ~= 0) or l_brushOccupancy_3 <= 0.5) then
            return ;
        else
            local l_l_cellOccupancy_3_0 = l_cellOccupancy_3;
            local l_l_cellWaterOccupancy_1_0 = l_cellWaterOccupancy_1;
            local v125 = false;
            local v126 = false;
            local v127 = 6;
            local v128 = 6;
            for v129 = 1, 6 do
                local v130 = l_x_3 + v1.xOffset[v129];
                local v131 = l_y_3 + v1.yOffset[v129];
                local v132 = l_z_3 + v1.zOffset[v129];
                if ((((v130 > 0 and v130 <= l_sizeX_3) and v131 > 0) and v131 <= l_sizeY_3) and v132 > 0) and v132 <= l_sizeZ_3 then
                    local v133 = l_readMaterials_3[v130][v131][v132];
                    local v134 = if v133 == l_Air_0 then v107[v130][v131][v132] else l_readOccupancies_3[v130][v131][v132];
                    local v135 = v107[v130][v131][v132];
                    local v136 = false;
                    if v133 == l_Air_0 then
                        v136 = v135 > 0;
                    end;
                    if not (not l_ignoreWater_3 or not v136) then
                        v134 = 0;
                    end;
                    if v135 <= 0 then
                        v126 = true;
                    end;
                    if v134 <= 0 then
                        v125 = true;
                    end;
                    v127 = v127 - v134;
                    v128 = v128 - v135;
                end;
            end;
            if not (l_cellOccupancy_3 >= 1 and not v125) then
                l_l_cellOccupancy_3_0 = l_l_cellOccupancy_3_0 - ((((v127 / 6) * (l_strength_3 + 0.1)) * 0.25) * l_brushOccupancy_3) * l_magnitudePercent_3;
            end;
            if not (l_cellWaterOccupancy_1 >= 1 and not v126) then
                l_l_cellWaterOccupancy_1_0 = l_l_cellWaterOccupancy_1_0 - ((((v128 / 6) * (l_strength_3 + 0.1)) * 0.25) * l_brushOccupancy_3) * l_magnitudePercent_3;
            end;
            l_l_cellOccupancy_3_0 = math.min(l_l_cellOccupancy_3_0, l_maxOccupancy_3);
            l_l_cellWaterOccupancy_1_0 = math.min(l_l_cellWaterOccupancy_1_0, l_maxOccupancy_3);
            if l_l_cellOccupancy_3_0 <= v1.one256th then
                l_writeOccupancies_3[l_x_3][l_y_3][l_z_3] = l_airFillerMaterial_1 == l_Water_0 and 1 or 0;
                l_writeMaterials_3[l_x_3][l_y_3][l_z_3] = l_airFillerMaterial_1;
            else
                l_writeOccupancies_3[l_x_3][l_y_3][l_z_3] = l_l_cellOccupancy_3_0;
            end;
            if not l_ignoreWater_3 and l_l_cellWaterOccupancy_1_0 ~= l_cellWaterOccupancy_1 then
                v108[l_x_3][l_y_3][l_z_3] = l_l_cellWaterOccupancy_1_0;
            end;
            return ;
        end;
    end,
    smooth = if not l_TerrainSmoothToolFix_0 then function(v137)
        local l_readMaterials_4 = v137.readMaterials;
        local l_readOccupancies_4 = v137.readOccupancies;
        local l_writeMaterials_4 = v137.writeMaterials;
        local l_writeOccupancies_4 = v137.writeOccupancies;
        local l_x_4 = v137.x;
        local l_y_4 = v137.y;
        local l_z_4 = v137.z;
        local l_sizeX_4 = v137.sizeX;
        local l_sizeY_4 = v137.sizeY;
        local l_sizeZ_4 = v137.sizeZ;
        local l_brushOccupancy_4 = v137.brushOccupancy;
        local l_magnitudePercent_4 = v137.magnitudePercent;
        local l_cellOccupancy_4 = v137.cellOccupancy;
        local l_cellMaterial_2 = v137.cellMaterial;
        local l_strength_4 = v137.strength;
        local l_filterSize_0 = v137.filterSize;
        local l_airFillerMaterial_2 = v137.airFillerMaterial;
        local l_ignoreWater_4 = v137.ignoreWater;
        if not (l_sizeX_4 > 2 and l_sizeZ_4 > 2) or l_sizeY_4 <= 2 then
            return ;
        elseif l_brushOccupancy_4 < 0.5 then
            return ;
        else
            local v156 = 0;
            local v157 = 0;
            local v158 = false;
            local v159 = false;
            local v160 = l_readMaterials_4[l_x_4][l_y_4][l_z_4];
            local v161 = true;
            if v160 ~= l_Air_0 then
                v161 = l_cellOccupancy_4 <= 0;
            end;
            for v162 = -l_filterSize_0, l_filterSize_0, l_filterSize_0 do
                for v163 = -l_filterSize_0, l_filterSize_0, l_filterSize_0 do
                    for v164 = -l_filterSize_0, l_filterSize_0, l_filterSize_0 do
                        local v165 = l_x_4 + v162;
                        local v166 = l_y_4 + v163;
                        local v167 = l_z_4 + v164;
                        if ((((v165 > 0 and v165 <= l_sizeX_4) and v166 > 0) and v166 <= l_sizeY_4) and v167 > 0) and v167 <= l_sizeZ_4 then
                            local v168 = l_readOccupancies_4[v165][v166][v167];
                            local v169 = 1 - math.sqrt((v162 * v162 + v163 * v163) + v164 * v164) / (l_filterSize_0 * 2);
                            local v170 = l_readMaterials_4[v165][v166][v167];
                            if l_ignoreWater_4 and v170 == l_Water_0 then
                                v168 = 0;
                            end;
                            if v168 >= 1 then
                                v158 = true;
                            end;
                            if v168 <= 0 then
                                v159 = true;
                            end;
                            v168 = v168 * 1.5 - 0.25;
                            v157 = v157 + 1 * v169;
                            v156 = v156 + v168 * v169;
                        end;
                    end;
                end;
            end;
            local v171 = ((((v156 / (v157 > 0 and v157 or l_cellOccupancy_4 * 1.5 - 0.25) - l_cellOccupancy_4) * (l_strength_4 + 0.1)) * 0.5) * l_brushOccupancy_4) * l_magnitudePercent_4;
            if not v158 and v171 > 0 then
                v171 = 0;
            elseif not v159 and v171 < 0 then
                v171 = 0;
            end;
            local v172 = math.max(0, (math.min(1, l_cellOccupancy_4 + v171)));
            if v172 ~= l_cellOccupancy_4 then
                if v161 and v172 > 0 then
                    l_writeMaterials_4[l_x_4][l_y_4][l_z_4] = v1.DEPRECATED_getMaterialForAutoMaterial(l_readMaterials_4, l_x_4, l_y_4, l_z_4, l_sizeX_4, l_sizeY_4, l_sizeZ_4, l_cellMaterial_2);
                elseif v172 <= 0 then
                    l_writeMaterials_4[l_x_4][l_y_4][l_z_4] = l_airFillerMaterial_2;
                end;
                l_writeOccupancies_4[l_x_4][l_y_4][l_z_4] = v172;
            end;
            return ;
        end;
    end else function(v173)
        local l_readMaterials_5 = v173.readMaterials;
        local l_readOccupancies_5 = v173.readOccupancies;
        local l_writeMaterials_5 = v173.writeMaterials;
        local l_writeOccupancies_5 = v173.writeOccupancies;
        local l_x_5 = v173.x;
        local l_y_5 = v173.y;
        local l_z_5 = v173.z;
        local _ = v173.sizeX;
        local _ = v173.sizeY;
        local _ = v173.sizeZ;
        local l_brushOccupancy_5 = v173.brushOccupancy;
        local l_magnitudePercent_5 = v173.magnitudePercent;
        local _ = v173.cellOccupancy;
        local _ = v173.cellMaterial;
        local l_strength_5 = v173.strength;
        local _ = v173.filterSize;
        local l_airFillerMaterial_3 = v173.airFillerMaterial;
        local l_ignoreWater_5 = v173.ignoreWater;
        local v192 = if not v173.smoothVoxelsUpgraded then v1.one256th else v1.one16th;
        if l_ignoreWater_5 and l_readMaterials_5[l_x_5][l_y_5][l_z_5] == Enum.Material.Water then
            l_writeOccupancies_5[l_x_5][l_y_5][l_z_5] = l_readOccupancies_5[l_x_5][l_y_5][l_z_5];
            l_writeMaterials_5[l_x_5][l_y_5][l_z_5] = l_readMaterials_5[l_x_5][l_y_5][l_z_5];
            return ;
        else
            local v193 = l_writeMaterials_5[l_x_5][l_y_5][l_z_5];
            local v194 = true;
            if v193 ~= l_Air_0 then
                v194 = l_writeOccupancies_5[l_x_5][l_y_5][l_z_5] <= 0;
            end;
            local v195 = math.max(0, (math.min(1, l_readOccupancies_5[l_x_5][l_y_5][l_z_5] + (((l_writeOccupancies_5[l_x_5][l_y_5][l_z_5] - l_readOccupancies_5[l_x_5][l_y_5][l_z_5]) * l_strength_5) * l_brushOccupancy_5) * l_magnitudePercent_5)));
            if v195 ~= l_writeOccupancies_5[l_x_5][l_y_5][l_z_5] then
                if v194 and v192 <= v195 then
                    l_writeMaterials_5[l_x_5][l_y_5][l_z_5] = l_readMaterials_5[l_x_5][l_y_5][l_z_5];
                elseif v195 <= 0 then
                    l_writeMaterials_5[l_x_5][l_y_5][l_z_5] = l_airFillerMaterial_3;
                end;
                l_writeOccupancies_5[l_x_5][l_y_5][l_z_5] = v195;
            end;
            return ;
        end;
    end
};