local l_script_FirstAncestor_0 = script:FindFirstAncestor("TerrainEditor");
local l_deepCopy_0 = require(l_script_FirstAncestor_0.Packages.Framework).Util.deepCopy;
local v2 = require(l_script_FirstAncestor_0.Src.Util.getItems);
local v3 = require(l_script_FirstAncestor_0.Src.Types);
local l_AdditionalSettings_0 = v3.AdditionalSettings;
local l_Category_0 = v3.Category;
local l_EnumType_0 = v3.EnumType;
local l_ElevationSettings_0 = v3.ElevationSettings;
local l_MaterialUnit_0 = v3.MaterialUnit;
return {
    Get = function(v9, v10)
        return {
            {
                Id = l_Category_0.ElevationSettings,
                Label = v10:getText("Category", l_Category_0.ElevationSettings),
                Children = {
                    {
                        Id = l_ElevationSettings_0.Unit,
                        Label = v10:getText(l_Category_0.ElevationSettings, l_ElevationSettings_0.Unit),
                        Schema = {
                            Items = v2(v10, nil, l_EnumType_0.MaterialUnit),
                            Type = "Select"
                        },
                        Value = v9.Unit
                    },
                    {
                        Id = l_ElevationSettings_0.Elevation,
                        Label = "",
                        Layout = Enum.FillDirection.Vertical,
                        Schema = {
                            Type = l_ElevationSettings_0.Elevation
                        },
                        Value = v9
                    },
                    if not not v9.MaterialIndex and v9.MaterialIndex <= #v9.MaterialList then {
                        Id = l_ElevationSettings_0.Material,
                        Label = v10:getText(l_Category_0.ElevationSettings, l_ElevationSettings_0.Material),
                        Schema = {
                            AllowWater = false,
                            Dropdown = true,
                            Type = "Material"
                        },
                        Value = v9.MaterialList[v9.MaterialIndex].Material
                    } else nil,
                    if not not v9.MaterialIndex and v9.MaterialIndex <= #v9.MaterialList then {
                        Id = l_ElevationSettings_0.Slope,
                        Label = v10:getText(l_Category_0.ElevationSettings, l_ElevationSettings_0.Slope),
                        Schema = {
                            Type = "Slope"
                        },
                        Value = {
                            Min = v9.MaterialList[v9.MaterialIndex].MinSlope,
                            Max = v9.MaterialList[v9.MaterialIndex].MaxSlope
                        }
                    } else nil,
                    if not v9.FillMaterial then nil else {
                        Id = l_ElevationSettings_0.FillMaterial,
                        Label = v10:getText(l_Category_0.ElevationSettings, l_ElevationSettings_0.FillMaterial),
                        Schema = {
                            Dropdown = true,
                            Type = "Material"
                        },
                        Value = v9.FillMaterial
                    }
                },
                Expandable = false,
                Separator = true
            },
            {
                Id = l_Category_0.AdditionalSettings,
                Label = v10:getText("Category", l_Category_0.AdditionalSettings),
                Children = {
                    {
                        Id = l_AdditionalSettings_0.FallbackMaterial,
                        Label = v10:getText(l_Category_0.AdditionalSettings, l_AdditionalSettings_0.FallbackMaterial),
                        Schema = {
                            AllowWater = false,
                            Dropdown = true,
                            Type = "Material"
                        },
                        Value = v9.FallbackMaterial
                    }
                },
                Expandable = false,
                Separator = true
            }
        };
    end,
    Set = function(v11, v12, v13)
        assert(((v11[1] and v11[1].Children) and v11[2]) and v11[2].Children, "Cannot update from nothing.");
        local v14 = {};
        for _, v16 in v11[1].Children, nil, nil do
            v14[v16.Id] = v16.Value;
        end;
        for _, v18 in v11[2].Children, nil, nil do
            v14[v18.Id] = v18.Value;
        end;
        local v19 = l_deepCopy_0(v14[l_ElevationSettings_0.Elevation]);
        local v20 = v14[l_ElevationSettings_0.Unit];
        if v19.Unit ~= v20 then
            if v20 == l_MaterialUnit_0.Stud then
                for _, v22 in v19.MaterialList, nil, nil do
                    v22.MinHeight = v13 * v22.MinHeight;
                    v22.MaxHeight = v13 * v22.MaxHeight;
                end;
                v19.Min = 0;
                v19.Max = v13;
            elseif v20 == l_MaterialUnit_0.Percentage then
                for _, v24 in v19.MaterialList, nil, nil do
                    v24.MinHeight = math.clamp(v24.MinHeight, 0, v13) / v13;
                    v24.MaxHeight = math.clamp(v24.MaxHeight, 0, v13) / v13;
                end;
                v19.Min = 0;
                v19.Max = 1;
            end;
        end;
        if v12.MaterialIndex <= #v19.MaterialList and #v12.MaterialList == #v19.MaterialList then
            v19.Unit = v20;
            v19.FallbackMaterial = v14[l_AdditionalSettings_0.FallbackMaterial];
            v19.FillMaterial = v14[l_ElevationSettings_0.FillMaterial];
            local v25 = v14[l_ElevationSettings_0.Material];
            if v25 then
                v19.MaterialList[v12.MaterialIndex].Material = v25;
            end;
            local v26 = v14[l_ElevationSettings_0.Slope];
            if v26 then
                v19.MaterialList[v12.MaterialIndex].MinSlope = v26.Min;
                v19.MaterialList[v12.MaterialIndex].MaxSlope = v26.Max;
            end;
        end;
        return v19;
    end
};


local l_script_FirstAncestor_0 = script:FindFirstAncestor("TerrainEditor");
local v1 = require(l_script_FirstAncestor_0.Packages.Dash);
local l_Signal_0 = require(l_script_FirstAncestor_0.Packages.Framework).Util.Signal;
local v3 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorBrushPerformanceEnhancements)();
local v4 = {};
v4.__index = v4;
v4.new = function(v5)
    return (setmetatable({
        Started = l_Signal_0.new(),
        Paused = l_Signal_0.new(),
        Resumed = l_Signal_0.new(),
        Canceled = l_Signal_0.new(),
        Finished = l_Signal_0.new(),
        ProgressChanged = l_Signal_0.new(),
        _name = v5.Name,
        _description = v5.Description,
        _onStartFunc = v5.OnStart,
        _onStepFunc = v5.OnStep,
        _onPauseFunc = v5.OnPause,
        _onResumeFunc = v5.OnResume,
        _onCancelFunc = v5.OnCancel,
        _onFinishFunc = v5.OnFinish,
        _onErrorFunc = v5.OnError,
        _budget = v5.Budget,
        _timeBetweenSteps = v5.TimeBetweenSteps or 0,
        _timeBetweenPauseChecks = v5.TimeBetweenPauseChecks or 0.1,
        _options = {},
        _canCancel = v5.AllowCancel or false,
        _canPause = v5.AllowPause or false,
        _hasStarted = false,
        _isPaused = false,
        _wasCanceled = false,
        _hasFinished = false,
        _isRunning = false,
        _operationProgress = 0,
        _errorMessage = nil,
        _steps = 0,
        _startTime = 0,
        _endTime = 0,
        _yieldTime = 0
    }, v4));
end;
v4.destroy = function(v6)
    if not ((not v6._hasStarted or v6._hasFinished) or v6._wasCanceled) then
        v6:cancel();
    end;
end;
v4.start = function(v7, v8)
    assert(typeof(v8) == "table", "Options must be a table.");
    assert(not v7._hasStarted, "Cannot start an operation that was already started.");
    if v8 then
        v7._options = v8;
    end;
    v7._hasStarted = true;
    v7._isRunning = true;
    v7._startTime = os.clock();
    v7._cycles = v8.cycles;
    v7._Synchronous = v8.Synchronous;
    v7.Started:Fire();
    if not v7._cycles and not v7._Synchronous then
        task.spawn(function()
            v7:_runOperation();
        end);
        return ;
    else
        v7:_runOperation();
        return ;
    end;
end;
v4.pause = function(v9)
    if not (not v9._hasStarted or v9._hasFinished) and not v9._wasCanceled then
        assert(not v9._isPaused, "Operation already paused.");
        v9._isPaused = true;
        v9.Paused:Fire();
        return ;
    else
        return ;
    end;
end;
v4.resume = function(v10)
    if not (not v10._hasStarted or v10._hasFinished) and not v10._wasCanceled then
        assert(v10._isPaused, "Operation already running.");
        v10._isPaused = false;
        v10.Resumed:Fire();
        return ;
    else
        return ;
    end;
end;
v4.togglePause = function(v11)
    if not (not v11._hasStarted or v11._hasFinished) and not v11._wasCanceled then
        if not v11._isPaused then
            v11:pause();
            return ;
        else
            v11:resume();
            return ;
        end;
    else
        return ;
    end;
end;
v4.cancel = function(v12)
    if not (not v12._hasStarted or v12._hasFinished) and not v12._wasCanceled then
        v12._wasCanceled = true;
        v12.Canceled:Fire();
        return ;
    else
        return ;
    end;
end;
v4.getName = function(v13)
    return v13._name;
end;
v4.setName = function(v14, v15)
    v14._name = v15;
end;
v4.getDescription = function(v16)
    return v16._description;
end;
v4.setDescription = function(v17, v18)
    v17._description = v18;
end;
v4.getOptions = function(v19)
    return v19._options;
end;
v4.setOptions = function(v20, v21)
    v20._options = v21;
end;
v4.updateOptions = function(v22, v23)
    v22._options = v1.join(v22._options, v23);
end;
v4.updatePayload = function(v24, v25)
    v24._options.Payload = v25;
end;
v4.canCancel = function(v26)
    return v26._canCancel;
end;
v4.canPause = function(v27)
    return v27._canPause;
end;
v4.canResume = function(v28)
    return v28._canPause;
end;
v4.hasStarted = function(v29)
    return v29._hasStarted;
end;
v4.isPaused = function(v30)
    return v30._isPaused;
end;
v4.wasCanceled = function(v31)
    return v31._wasCanceled;
end;
v4.hasFinished = function(v32)
    return v32._hasFinished;
end;
v4.isRunning = function(v33)
    return v33._isRunning;
end;
v4.didError = function(v34)
    return v34._errorMessage ~= nil;
end;
v4.getErrorMessage = function(v35)
    return v35._errorMessage;
end;
v4.getProgress = function(v36)
    return v36._operationProgress;
end;
v4._setProgress = function(v37, v38)
    if v37._operationProgress ~= v38 then
        v37._operationProgress = v38;
        v37._runningTime = os.clock() - v37._startTime;
        v37.ProgressChanged:Fire(v38, if v38 > 0 then v37._runningTime / v38 - v37._runningTime else nil);
    end;
end;
v4.getStepsTaken = function(v39)
    if v39._steps == 0 then
        return 1;
    else
        return v39._steps;
    end;
end;
v4.getCurrentTimeTaken = function(v40)
    return os.clock() - v40._startTime;
end;
v4.getTimeTaken = function(v41)
    if v41._hasFinished then
        return v41._endTime - v41._startTime;
    else
        return 0;
    end;
end;
v4.getYieldTime = function(v42)
    return v42._yieldTime;
end;
v4.getWorkTime = function(v43)
    if v43._hasFinished then
        return v43:getTimeTaken() - v43:getYieldTime();
    else
        return 0;
    end;
end;
v4.addTimeStatistic = function(v44, v45)
    if v45 then
        if v44._time then
            v44._time = v44._time + v45;
            v44._timeTotals = v44._timeTotals + 1;
            return ;
        else
            v44._time = v45;
            v44._timeTotals = 1;
            return ;
        end;
    else
        return ;
    end;
end;
v4.skipBudget = function(v46)
    v46._skipBudget = true;
end;
v4.getTimeStatistic = function(v47)
    if not v47._time or not v47._timeTotals then
        return nil;
    else
        return v47._time / v47._timeTotals;
    end;
end;
v4._yield = function(v48, ...)
    if not v48._cycles and not v48._Synchronous then
        local v49 = os.clock();
        task.wait(...);
        v48._yieldTime = v48._yieldTime + (os.clock() - v49);
        return ;
    else
        return ;
    end;
end;
v4._handleError = function(v50, v51)
    if not v50:didError() then
        v50._errorMessage = tostring(v51);
        if not v50._onErrorFunc then
            error(v50._errorMessage);
            return ;
        else
            v50:_runCallback(v50._onErrorFunc);
            return ;
        end;
    else
        return ;
    end;
end;
v4._runCallback = function(v52, v53)
    if v53 then
        local v54 = nil;
        local l_status_0, l_result_0 = pcall(function()
            v54 = table.pack(v53(v52._options, v52));
        end);
        if not l_status_0 then
            v52:_handleError(l_result_0);
            return false;
        else
            return true, unpack(v54);
        end;
    else
        return false;
    end;
end;
v4._runOperation = function(v57)
    v57:_runCallback(v57._onStartFunc);
    v57:_yield(v57._timeBetweenSteps);
    local v58 = false;
    local v59 = os.clock();
    while not v57:didError() do
        if not v57._wasCanceled then
            if not v57._isPaused then
                if not (not (not v58 or not v57._onResumeFunc) and not v57:_runCallback(v57._onResumeFunc)) then
                    v58 = false;
                    local v60, v61, v62 = v57:_runCallback(v57._onStepFunc);
                    v57._steps = v57._steps + 1;
                    if v60 then
                        if v62 then
                            v57:_setProgress(v62);
                        end;
                        if v57._cycles then
                            v57._cycles = v57._cycles - 1;
                        end;
                        if v61 and v57._cycles ~= 0 then
                            if not ((v57._budget and v57._budget >= os.clock() - v59) and (not v3 or not v57._skipBudget)) then
                                v57:_yield(v57._timeBetweenSteps);
                                v57._skipBudget = false;
                                v59 = os.clock();
                            end;
                        else
                            break;
                        end;
                    else
                        break;
                    end;
                else
                    break;
                end;
            elseif not (not (v58 or not v57._onPauseFunc) and not v57:_runCallback(v57._onPauseFunc)) then
                v58 = true;
                v57:_yield(v57._timeBetweenPauseChecks);
            else
                break;
            end;
        else
            v57:_runCallback(v57._onCancelFunc);
            break;
        end;
    end;
    v57._hasFinished = true;
    v57._isRunning = false;
    v57._endTime = os.clock();
    v57:_runCallback(v57._onFinishFunc);
    v57.Finished:Fire();
end;
return v4;


local l_script_FirstAncestor_0 = script:FindFirstAncestor("TerrainEditor");
local v1 = require(l_script_FirstAncestor_0.Src.Resources.Constants);
local l_Util_0 = l_script_FirstAncestor_0.Src.Util;
local v3 = require(l_Util_0.AnalyticsHelper);
local v4 = require(l_Util_0.Operations.BaseOperation);
local v5 = require(l_Util_0.CalculateAutoMaterial);
local v6 = require(l_Util_0.CalculateBrushOccupancy);
local v7 = require(l_Util_0.CalculateWaterLevel);
local v8 = require(l_Util_0.ClampVoxelBoundaries);
local v9 = require(l_Util_0.ConvertForPivot);
local v10 = require(l_Util_0.getDraggedPositions);
local v11 = require(l_Util_0.DEPRECATED_CalculateBrushPower);
local v12 = require(l_Util_0.DEPRECATED_CalculateAutoMaterial);
local _ = require(l_script_FirstAncestor_0.Src.Util.Operations.SculptOperation);
local v14 = require(l_script_FirstAncestor_0.Src.Util.Operations.SmoothOperation);
local v15 = require(l_script_FirstAncestor_0.Src.Types);
local l_BrushMode_0 = v15.BrushMode;
local l_BrushShape_0 = v15.BrushShape;
local l_BrushSettings_0 = v15.BrushSettings;
local l_Category_0 = v15.Category;
local l_MaterialSettings_0 = v15.MaterialSettings;
local v21 = require(l_script_FirstAncestor_0.Src.Util.DebugFlags);
local v22 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorWaterAutofill);
local v23 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorBrushPerformanceEnhancements);
local v24 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorTimeStatistic);
return function(v25, v26)
    return v4.new({
        Budget = 0.005,
        Name = v25,
        OnFinish = function(v27, _)
            assert(v27.State, "Tried to finish without starting first.");
            local l_State_0 = v27.State;
            v26.ChangeHistoryService:SetWaypoint("Draw");
            if l_State_0.Smooth then
                l_State_0.Smooth:cancel();
            end;
        end,
        OnStart = function(v30, _)
            local v32 = v30.Payload[l_Category_0.BrushSettings];
            local v33 = v32[l_BrushSettings_0.BrushSize];
            v30.State = {
                Position = v9(v32[l_BrushSettings_0.PivotPosition], v32[l_BrushSettings_0.State].Position, if v32[l_BrushSettings_0.BrushShape] == l_BrushShape_0.Sphere then v33.Size else v33.Height * v1.VoxelResolution)
            };
        end,
        OnStep = if not v23() then function(v34, v35)
            assert(v34.State, "Tried to step without starting first.");
            local l_State_1 = v34.State;
            local v37 = v34.Payload[l_Category_0.BrushSettings];
            local v38 = v37[l_BrushSettings_0.BrushSize];
            local l_Height_0 = v38.Height;
            local l_Size_0 = v38.Size;
            local v41 = v37[l_BrushSettings_0.BrushMode];
            local v42 = v37[l_BrushSettings_0.BrushShape];
            local v43 = v37[l_BrushSettings_0.State];
            local v44 = v37[l_BrushSettings_0.IgnoreWater];
            local v45 = if not v22() then nil else v37[l_BrushSettings_0.WaterAutofill];
            if not v37[l_BrushSettings_0.TemporarySmooth] then
                if l_State_1.Smooth then
                    l_State_1.Smooth:cancel();
                    l_State_1.Smooth = nil;
                    l_State_1.Position = v43.Position;
                end;
                local v46 = v34.Payload[l_Category_0.MaterialSettings];
                local v47 = v46[l_MaterialSettings_0.AutoMaterial];
                local v48 = v46[l_MaterialSettings_0.SourceMaterial];
                local v49 = if v42 == l_BrushShape_0.Sphere then l_Size_0 else l_Height_0 * v1.VoxelResolution;
                local v50 = l_Size_0 * v1.VoxelResolution;
                local v51 = v50 * 0.5;
                if v41 == l_BrushMode_0.Add then
                    l_Size_0 = l_Size_0 - 1;
                    v49 = v49 - 2;
                    v50 = v50 - 2;
                    v51 = v51 - 1;
                else
                    v48 = Enum.Material.Air;
                end;
                local v52 = v10(l_State_1.Position, v9(v37[l_BrushSettings_0.PivotPosition], v43.Position, v49), v51);
                l_State_1.Position = v52[#v52];
                for _, v54 in v52, nil, nil do
                    local v55 = nil;
                    if v24() then
                        v55 = os.clock();
                    end;
                    local v56, v57 = v8(v54, v51, v49);
                    local v58;
                    if v41 ~= l_BrushMode_0.Add then
                        v58 = false;
                        if v41 == l_BrushMode_0.Subtract then
                            v58 = not v45;
                            if not v58 then
                                goto label0;
                            end;
                        else
                            goto label0;
                        end;
                    end;
                    v58 = not v47 and not v44;
                    ::label0::;
                    if not v58 then
                        local v59 = Region3.new(v56, v57);
                        local v60 = (v57.X - v56.X) * 0.5;
                        local v61 = (v57 - v56) / v1.VoxelResolution;
                        local v62, v63 = v26.Terrain:ReadVoxels(v59, v1.VoxelResolution);
                        v62.Size = nil;
                        v63.Size = nil;
                        local v64 = nil;
                        local v65 = nil;
                        if v41 == l_BrushMode_0.Subtract then
                            local v66, v67 = v7(v62, v63);
                            v64 = v66;
                            v65 = v67;
                        end;
                        local v68 = {};
                        local v69 = {};
                        local v70 = {};
                        local v71 = {};
                        for v72, v73 in v63, nil, nil do
                            local v74 = (v56.X + (v72 - 0.5) * v1.VoxelResolution) - v54.X;
                            v68[v72] = {};
                            v69[v72] = {};
                            v70[v72] = {};
                            v71[v72] = {};
                            for v75, v76 in v73, nil, nil do
                                local v77 = (v56.Y + (v75 - 0.5) * v1.VoxelResolution) - v54.Y;
                                v68[v72][v75] = {};
                                v69[v72][v75] = {};
                                v70[v72][v75] = {};
                                v71[v72][v75] = {};
                                for v78, v79 in v76, nil, nil do
                                    local v80 = Vector3.new(v72, v75, v78);
                                    local v81 = v62[v72][v75][v78];
                                    local v82 = Vector3.new(v74, v77, (v56.Z + (v78 - 0.5) * v1.VoxelResolution) - v54.Z);
                                    v70[v72][v75][v78] = v81;
                                    v71[v72][v75][v78] = v79;
                                    local v83, _ = v11(v82, l_Size_0, v42, v60);
                                    if v41 == l_BrushMode_0.Add and v79 < v83 then
                                        if v44 and v81 == Enum.Material.Water then
                                            v69[v72][v75][v78] = v79;
                                            v68[v72][v75][v78] = v81;
                                        elseif not v47 then
                                            v69[v72][v75][v78] = v83;
                                            v68[v72][v75][v78] = v48;
                                        else
                                            v69[v72][v75][v78] = v83;
                                            v68[v72][v75][v78] = if not v47 then v81 else v12(v62, v80, v61) or v81;
                                        end;
                                    elseif (not (v41 == l_BrushMode_0.Add and v83 > 0) or v47) or not v44 then
                                        if v41 == l_BrushMode_0.Subtract and (not v44 or v81 ~= Enum.Material.Water) then
                                            local v85 = if not not v45 and v75 <= v64 then Enum.Material.Water else Enum.Material.Air;
                                            if v1.MinimumOccupancy <= v83 then
                                                v69[v72][v75][v78] = v85 == Enum.Material.Air and 0 or if v75 == v64 then v65 else 1;
                                                v68[v72][v75][v78] = v85;
                                                if v83 == 1 then
                                                    v70[v72][v75][v78] = Enum.Material.Air;
                                                    v71[v72][v75][v78] = 0;
                                                end;
                                            else
                                                v69[v72][v75][v78] = v79;
                                                v68[v72][v75][v78] = v81;
                                            end;
                                        else
                                            v69[v72][v75][v78] = v79;
                                            v68[v72][v75][v78] = v81;
                                        end;
                                    elseif v81 == Enum.Material.Water then
                                        v69[v72][v75][v78] = v79;
                                        v68[v72][v75][v78] = v81;
                                    else
                                        v69[v72][v75][v78] = v83;
                                        v68[v72][v75][v78] = v48;
                                    end;
                                end;
                            end;
                        end;
                        if v24() then
                            v35:addTimeStatistic(v3.NormalizeBrushTimer(v55, l_Size_0, l_Height_0));
                        end;
                        if v41 == l_BrushMode_0.Subtract then
                            v26.Terrain:WriteVoxels(v59, v1.VoxelResolution, v70, v71);
                        end;
                        v26.Terrain:WriteVoxels(v59, v1.VoxelResolution, v68, v69);
                    else
                        if v42 == l_BrushShape_0.Sphere then
                            v26.Terrain:FillBall(v54, v51, v48);
                        elseif v42 == l_BrushShape_0.Cube then
                            v26.Terrain:FillBlock(CFrame.new(v54), Vector3.new(v50, v49, v50), v48);
                        elseif v42 == l_BrushShape_0.Cylinder then
                            if (v57 - v56).X <= 2 * v1.VoxelResolution then
                                v26.Terrain:FillBall(v54, v51, v48);
                            else
                                v26.Terrain:FillCylinder(CFrame.new(v54), v49, v51, v48);
                            end;
                        end;
                        if v24() then
                            v35:addTimeStatistic(v3.NormalizeBrushTimer(v55, l_Size_0, l_Height_0));
                        end;
                    end;
                end;
                return true, 0;
            else
                if l_State_1.Smooth then
                    l_State_1.Smooth:updatePayload(v34.Payload);
                else
                    local v86 = v14("Smooth", v26);
                    v86:start({
                        Payload = v34.Payload
                    });
                    l_State_1.Smooth = v86;
                end;
                return true, 0;
            end;
        end else function(v87, v88)
            assert(v87.State, "Tried to step without starting first.");
            if v21.ProfileTools() then
                debug.profilebegin("Draw");
            end;
            local l_State_2 = v87.State;
            local v90 = v87.Payload[l_Category_0.BrushSettings];
            local v91 = v90[l_BrushSettings_0.BrushSize];
            local l_Height_1 = v91.Height;
            local l_Size_1 = v91.Size;
            local v94 = v90[l_BrushSettings_0.BrushMode];
            local v95 = v90[l_BrushSettings_0.BrushShape];
            local v96 = v90[l_BrushSettings_0.State];
            local v97 = v90[l_BrushSettings_0.IgnoreWater];
            local v98 = if not v22() then nil else v90[l_BrushSettings_0.WaterAutofill];
            if not v90[l_BrushSettings_0.TemporarySmooth] then
                if l_State_2.Smooth then
                    l_State_2.Smooth:cancel();
                    l_State_2.Smooth = nil;
                    l_State_2.Position = v96.Position;
                end;
                local v99 = v87.Payload[l_Category_0.MaterialSettings];
                local v100 = v99[l_MaterialSettings_0.AutoMaterial];
                local v101 = v99[l_MaterialSettings_0.SourceMaterial];
                local v102 = if v95 == l_BrushShape_0.Sphere then l_Size_1 else l_Height_1 * v1.VoxelResolution;
                local v103 = l_Size_1 * v1.VoxelResolution;
                local v104 = v103 * 0.5;
                if v94 == l_BrushMode_0.Add then
                    l_Size_1 = l_Size_1 - 1;
                    v102 = v102 - 2;
                    v103 = v103 - 2;
                    v104 = v104 - 1;
                else
                    v101 = Enum.Material.Air;
                end;
                local v105 = v10(l_State_2.Position, v9(v90[l_BrushSettings_0.PivotPosition], v96.Position, v102), v104);
                l_State_2.Position = v105[#v105];
                for _, v107 in v105, nil, nil do
                    if not (v107 == l_State_2.LastPosition) or v100 then
                        l_State_2.LastPosition = v107;
                        local v108, v109 = v8(v107, v104, v102);
                        local v110 = nil;
                        if v24() then
                            v110 = os.clock();
                        end;
                        local v111;
                        if v94 ~= l_BrushMode_0.Add then
                            v111 = false;
                            if v94 == l_BrushMode_0.Subtract then
                                v111 = not v98;
                                if v111 then
                                    v111 = not v97;
                                    if not v111 then
                                        goto label1;
                                    end;
                                else
                                    goto label1;
                                end;
                            else
                                goto label1;
                            end;
                        end;
                        v111 = not v100;
                        ::label1::;
                        if not v111 then
                            local v112 = Region3.new(v108, v109);
                            local v113 = (v109.X - v108.X) * 0.5;
                            local v114 = (v109 - v108) / v1.VoxelResolution;
                            local v115, v116 = v26.Terrain:ReadVoxels(v112, v1.VoxelResolution);
                            v115.Size = nil;
                            v116.Size = nil;
                            local v117 = nil;
                            local v118 = nil;
                            if v94 == l_BrushMode_0.Subtract then
                                local v119, v120 = v7(v115, v116);
                                v117 = v119;
                                v118 = v120;
                            end;
                            local v121 = #v115;
                            local v122 = #v115[1];
                            local _ = #v115[1][1];
                            local v124 = table.create(v121);
                            local v125 = table.create(v121);
                            local v126 = table.create(v121);
                            local l_VoxelResolution_0 = v1.VoxelResolution;
                            local v128 = false;
                            if v95 == l_BrushShape_0.Sphere then
                                v128 = l_Size_1 > 2;
                            end;
                            local v129;
                            if v95 == l_BrushShape_0.Cylinder then
                                v129 = true;
                                if not (l_Size_1 <= 2) then
                                    goto label2;
                                end;
                            end;
                            v129 = v128;
                            ::label2::;
                            local v130 = (v108.X - l_VoxelResolution_0 * 0.5) - v107.X;
                            local v131 = (v108.Y - l_VoxelResolution_0 * 0.5) - v107.Y;
                            local v132 = (v108.Z - l_VoxelResolution_0 * 0.5) - v107.Z;
                            local v133 = nil;
                            local v134 = nil;
                            local v135 = v94 == l_BrushMode_0.Add;
                            local v136 = v94 == l_BrushMode_0.Subtract;
                            local v137 = nil;
                            if v24() then
                                v137 = os.clock();
                            end;
                            for v138, v139 in v116, nil, nil do
                                v133 = if not v129 then nil else (v138 * l_VoxelResolution_0 + v130) ^ 2;
                                local v140 = v115[v138];
                                local v141 = table.create(v122);
                                local v142 = table.create(v122);
                                local v143 = table.create(v122);
                                v124[v138] = v141;
                                v125[v138] = v142;
                                v126[v138] = v143;
                                for v144, v145 in v139, nil, nil do
                                    v134 = if not v128 then nil else v133 + (v144 * l_VoxelResolution_0 + v131) ^ 2;
                                    local v146 = v140[v144];
                                    local v147 = table.clone(v146);
                                    local v148 = table.clone(v146);
                                    local v149 = table.clone(v145);
                                    v141[v144] = v147;
                                    v142[v144] = v148;
                                    v143[v144] = v149;
                                    for v150, v151 in v145, nil, nil do
                                        local v152 = v146[v150];
                                        local v153 = 1;
                                        if v129 then
                                            local v154 = (v150 * l_VoxelResolution_0 + v132) ^ 2;
                                            v153 = v6(if not v128 then math.sqrt(v133 + v154) else math.sqrt(v134 + v154), v113);
                                        end;
                                        if v135 and v151 < v153 then
                                            v145[v150] = v153;
                                            if v100 then
                                                v147[v150] = v5(v138, v144, v150, v115, v114) or v152;
                                            else
                                                v147[v150] = v152;
                                            end;
                                        elseif v136 and (not v97 or v152 ~= Enum.Material.Water) then
                                            local v155 = if not not v98 and v144 <= v117 then Enum.Material.Water else Enum.Material.Air;
                                            if v1.MinimumOccupancy <= v153 then
                                                v145[v150] = v155 == Enum.Material.Air and 0 or if v144 == v117 then v118 else 1;
                                                v147[v150] = v155;
                                                if v153 == 1 then
                                                    v148[v150] = Enum.Material.Air;
                                                    v149[v150] = 0;
                                                end;
                                            end;
                                        end;
                                    end;
                                end;
                            end;
                            if v24() then
                                v88:addTimeStatistic(v3.NormalizeBrushTimer(v137, v91.Size, v91.Height));
                            end;
                            if v94 == l_BrushMode_0.Subtract then
                                v26.Terrain:WriteVoxels(v112, v1.VoxelResolution, v125, v126);
                            end;
                            v26.Terrain:WriteVoxels(v112, v1.VoxelResolution, v124, v116);
                        else
                            if v95 == l_BrushShape_0.Sphere then
                                v26.Terrain:FillBall(v107, v104, v101);
                            elseif v95 == l_BrushShape_0.Cube then
                                v26.Terrain:FillBlock(CFrame.new(v107), Vector3.new(v103, v102, v103), v101);
                            elseif v95 == l_BrushShape_0.Cylinder then
                                if (v109 - v108).X <= 2 * v1.VoxelResolution then
                                    v26.Terrain:FillBall(v107, v104, v101);
                                else
                                    v26.Terrain:FillCylinder(CFrame.new(v107), v102, v104, v101);
                                end;
                            end;
                            if v24() then
                                v88:addTimeStatistic(v3.NormalizeBrushTimer(v110, v91.Size, v91.Height));
                            end;
                        end;
                    else
                        v88:skipBudget();
                        if v21.ProfileTools() then
                            debug.profileend();
                        end;
                        return true, 0;
                    end;
                end;
                if v21.ProfileTools() then
                    debug.profileend();
                end;
                return true, 0;
            else
                if l_State_2.Smooth then
                    l_State_2.Smooth:updatePayload(v87.Payload);
                else
                    local v156 = v14("Smooth", v26);
                    v156:start({
                        Payload = v87.Payload
                    });
                    l_State_2.Smooth = v156;
                end;
                if v21.ProfileTools() then
                    debug.profileend();
                end;
                return true, 0;
            end;
        end
    });
end;


local l_script_FirstAncestor_0 = script:FindFirstAncestor("TerrainEditor");
local v1 = require(l_script_FirstAncestor_0.Src.Util.Operations.BaseOperation);
local l_Util_0 = l_script_FirstAncestor_0.Src.Util;
local v3 = require(l_Util_0.AnalyticsHelper);
local v4 = require(l_Util_0.ConvertTransformToRegion);
local v5 = require(l_Util_0.CreateSubregions);
local v6 = require(l_script_FirstAncestor_0.Src.Types);
local l_Category_0 = v6.Category;
local l_FillMode_0 = v6.FillMode;
local l_MaterialSettings_0 = v6.MaterialSettings;
local l_SelectionSettings_0 = v6.SelectionSettings;
local v11 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorTimeStatistic);
local v12 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorIndexingBugFix);
return function(v13, v14, v15)
    return v1.new({
        AllowPause = true,
        AllowCancel = true,
        Description = v14,
        Name = v13,
        OnCancel = function(v16, _)
            assert(v16.State, "Tried to cancel without starting first.");
            local l_State_0 = v16.State;
            v15.Terrain:PasteRegion(l_State_0.BackupTerrainRegion, l_State_0.BackupRegion.Min, true);
        end,
        OnFinish = function(v19, v20)
            if not ((not v11() or not v19.State) or not v19.State.StartTime) then
                v20:addTimeStatistic(v3.NormalizeRegionTimer(v19.State.StartTime, v19.Payload[l_Category_0.SelectionSettings][l_SelectionSettings_0.Size]));
            end;
            v15.ChangeHistoryService:SetWaypoint("FillAction");
        end,
        OnStart = function(v21, _)
            local l_Terrain_0 = v15.Terrain;
            local v24 = v21.Payload[l_Category_0.SelectionSettings];
            local v25 = v24[l_SelectionSettings_0.Transform];
            local v26 = v24[l_SelectionSettings_0.Size];
            local v27 = v5(v25, v26);
            local v28 = v4(v25, v26);
            v21.State = {
                BackupRegion = v28,
                BackupTerrainRegion = l_Terrain_0:CopyRegion(v28),
                CurrentIndex = 1,
                Regions = v27,
                StartTime = os.clock()
            };
        end,
        OnStep = function(v29, _)
            assert(v29.State, "Tried to step without starting first.");
            local l_State_1 = v29.State;
            local l_Terrain_1 = v15.Terrain;
            local v33 = v29.Payload[l_Category_0.MaterialSettings];
            local v34 = v29.Payload[l_Category_0.SelectionSettings];
            local v35 = v33[l_MaterialSettings_0.FillMode];
            local v36 = v34[l_SelectionSettings_0.Size];
            local v37 = v33[l_MaterialSettings_0.SourceMaterial];
            local v38 = v33[l_MaterialSettings_0.TargetMaterial];
            local v39 = v34[l_SelectionSettings_0.Transform];
            local v40 = l_State_1.Regions[l_State_1.CurrentIndex];
            if not v12() or v40 then
                if v35 == l_FillMode_0.Replace then
                    l_Terrain_1:ReplaceMaterialInTransformSubregion(v39, v36, v37, v38, v40);
                else
                    l_Terrain_1:SetMaterialInTransformSubregion(v39, v36, v37, v40);
                end;
                if l_State_1.CurrentIndex == #l_State_1.Regions then
                    return false, 0;
                else
                    l_State_1.CurrentIndex = l_State_1.CurrentIndex + 1;
                    return true, l_State_1.CurrentIndex / #l_State_1.Regions;
                end;
            else
                return false, 0;
            end;
        end
    });
end;


local l_script_FirstAncestor_0 = script:FindFirstAncestor("TerrainEditor");
local v1 = require(l_script_FirstAncestor_0.Src.Resources.Constants);
local l_Util_0 = l_script_FirstAncestor_0.Src.Util;
local v3 = require(l_Util_0.AnalyticsHelper);
local v4 = require(l_Util_0.Operations.BaseOperation);
local _ = require(l_Util_0.CalculateAutoMaterial);
local v6 = require(l_Util_0.CalculateBrushOccupancy);
local v7 = require(l_Util_0.CalculateMagnitudePercent);
local v8 = require(l_Util_0.CalculateWaterLevel);
local v9 = require(l_Util_0.ClampVoxelBoundaries);
local v10 = require(l_Util_0.ConvertForPivot);
local v11 = require(l_Util_0.DebugFlags);
local v12 = require(l_Util_0.getDraggedPositions);
local v13 = require(l_Util_0.getPlaneIntersection);
local v14 = require(l_Util_0.DEPRECATED_CalculateAutoMaterial);
local v15 = require(l_Util_0.DEPRECATED_CalculateBrushPower);
local v16 = require(l_script_FirstAncestor_0.Src.Types);
local l_BrushShape_0 = v16.BrushShape;
local l_BrushSettings_0 = v16.BrushSettings;
local l_Category_0 = v16.Category;
local l_FlattenMode_0 = v16.FlattenMode;
local l_FlattenPlane_0 = v16.FlattenPlane;
local v22 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorWaterAutofill);
local v23 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorTimeStatistic);
local v24 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorBrushPerformanceEnhancements);
local _ = v24();
local v26 = v1.VoxelResolution * 0.5;
local l_VoxelResolution_0 = v1.VoxelResolution;
return function(v28, v29)
    return v4.new({
        Budget = 0.005,
        Name = v28,
        OnFinish = function()
            v29.ChangeHistoryService:SetWaypoint("Flatten");
        end,
        OnStart = function(v30, _)
            local v32 = v30.Payload[l_Category_0.BrushSettings];
            local v33 = v32[l_BrushSettings_0.BrushSize];
            local v34 = if v32[l_BrushSettings_0.FlattenPlane] == l_FlattenPlane_0.Auto then v10(v32[l_BrushSettings_0.PivotPosition], v32[l_BrushSettings_0.State].Position, if v32[l_BrushSettings_0.BrushShape] == l_BrushShape_0.Sphere then v33.Size else v33.Height * v1.VoxelResolution) else Vector3.new(0, v32[l_BrushSettings_0.FixedYPlane], 0);
            v30.State = {
                Plane = Vector3.yAxis,
                PlanePosition = v34,
                Position = v34
            };
        end,
        OnStep = if not v24() then function(v35, v36)
            assert(v35.State, "Tried to step without starting first.");
            local l_State_0 = v35.State;
            local v38 = v35.Payload[l_Category_0.BrushSettings];
            local v39 = v38[l_BrushSettings_0.BrushSize];
            local l_Height_0 = v39.Height;
            local l_Size_0 = v39.Size;
            local v42 = v38[l_BrushSettings_0.BrushShape];
            local v43 = v38[l_BrushSettings_0.FlattenMode];
            local v44 = v38[l_BrushSettings_0.IgnoreWater];
            local v45 = v38[l_BrushSettings_0.PullTerrain];
            local v46 = v38[l_BrushSettings_0.Strength];
            local v47 = if not v22 then nil else v38[l_BrushSettings_0.WaterAutofill];
            local v48 = v38[l_BrushSettings_0.State];
            local v49 = true;
            if v43 ~= l_FlattenMode_0.Grow then
                v49 = v43 == l_FlattenMode_0.Both;
            end;
            local v50 = true;
            if v43 ~= l_FlattenMode_0.Erode then
                v50 = v43 == l_FlattenMode_0.Both;
            end;
            local v51 = if v42 == l_BrushShape_0.Sphere then l_Size_0 else l_Height_0 * v1.VoxelResolution;
            local v52 = (l_Size_0 * v1.VoxelResolution) * 0.5;
            local l_Plane_0 = l_State_0.Plane;
            local l_PlanePosition_0 = l_State_0.PlanePosition;
            local v55 = math.abs(l_Plane_0.X);
            local v56 = math.abs(l_Plane_0.Y);
            local v57 = math.abs(l_Plane_0.Z);
            local _ = v42 == l_BrushShape_0.Sphere;
            local _ = v42 == l_BrushShape_0.Cube;
            local v60 = if v57 <= v56 and v55 <= v56 then Vector3.new(0, l_Plane_0.Y > 0 and 1 or -1, 0) else if v56 <= v57 and v55 <= v57 then Vector3.new(0, 0, l_Plane_0.Z > 0 and 1 or -1) else Vector3.new(l_Plane_0.X > 0 and 1 or -1, 0, 0);
            local v61 = v10(v38[l_BrushSettings_0.PivotPosition], v48.Position, v51);
            local v62 = v12(l_State_0.Position, v61, v52);
            l_State_0.Position = v62[#v62];
            local v63, v64 = v9(v61, v52, v51);
            for _, v66 in v62, nil, nil do
                local v67 = Region3.new(v63, v64);
                local v68 = (v64.X - v63.X) * 0.5;
                local v69, v70 = v29.Terrain:ReadVoxels(v67, v1.VoxelResolution);
                v70.Size = nil;
                v69.Size = nil;
                local v71, v72 = v29.Terrain:ReadVoxels(v67, v1.VoxelResolution);
                local v73 = nil;
                if v23() then
                    v73 = os.clock();
                end;
                local v74 = #v69;
                local v75 = #v69[1];
                local v76 = #v69[1][1];
                local v77 = Vector3.new(v74, v75, v76);
                local v78 = nil;
                local v79 = nil;
                local v80 = {};
                local v81 = {};
                if v47 then
                    local v82, v83 = v8(v69, v70);
                    v78 = v82;
                    v79 = v83;
                    v82, v83 = v29.Terrain:ReadVoxels(v67, v1.VoxelResolution);
                    v80 = v82;
                    v81 = v83;
                end;
                local function v98(v84, v85, v86, v87, v88)
                    local v89, _ = v15(v85, l_Size_0, v42, v68);
                    local v91 = v69[v84.X][v84.Y][v84.Z];
                    local v92 = v70[v84.X][v84.Y][v84.Z];
                    if v89 > 0 and v92 > 0 then
                        v88 = true;
                    end;
                    if v88 then
                        if v89 > 0 then
                            if v26 < v87 and v92 < 1 then
                                local v93 = v92 + v86;
                                local v94 = v14(v69, v84, v77, v47) or v91;
                                v72[v84.X][v84.Y][v84.Z] = v94 == Enum.Material.Air and 0 or if v93 <= 1 then v93 else 1;
                                v71[v84.X][v84.Y][v84.Z] = v94 or v91;
                                v86 = v93 - 1;
                                if v86 > 0 then
                                    return v86, v88;
                                else
                                    return 0, v88;
                                end;
                            elseif v87 <= v26 and -v26 < v87 then
                                local v95 = (v87 + v26) / v1.VoxelResolution;
                                local v96 = v92 + v86;
                                local v97 = if v91 == Enum.Material.Air then v14(v69, v84, v77, v47) or v91 else v91;
                                v72[v84.X][v84.Y][v84.Z] = v97 == Enum.Material.Air and 0 or if v96 <= v95 then v96 else v95;
                                v71[v84.X][v84.Y][v84.Z] = v97 or v91;
                                return 0, v88, v92 < v72[v84.X][v84.Y][v84.Z];
                            end;
                        end;
                        return v86, v88;
                    else
                        return v86, v88;
                    end;
                end;
                do
                    local l_v78_0, l_v79_0, l_v80_0, l_v81_0 = v78, v79, v80, v81;
                    local function v115(v103, v104, v105, v106, v107)
                        local v108, _ = v15(v104, l_Size_0, v42, v68);
                        local v110 = v70[v103.X][v103.Y][v103.Z];
                        local v111 = v69[v103.X][v103.Y][v103.Z];
                        if v44 and v111 == Enum.Material.Water then
                            return v105, v107;
                        else
                            local v112;
                            if v108 > 0 then
                                v112 = true;
                                if v110 >= 1 then
                                    if v47 then
                                        v112 = true;
                                        if not (v111 ~= Enum.Material.Water) then
                                            goto label0;
                                        end;
                                    end;
                                else
                                    goto label0;
                                end;
                            end;
                            v112 = v107;
                            ::label0::;
                            v107 = v112;
                            if v107 then
                                if v108 > 0 then
                                    if ((v47 and v103.Y <= l_v78_0) and v110 == 0) and v26 < v106 then
                                        v72[v103.X][v103.Y][v103.Z] = if v103.Y == l_v78_0 then l_v79_0 else 1;
                                        v71[v103.X][v103.Y][v103.Z] = Enum.Material.Water;
                                    elseif (v26 < v106 and v110 > 0) and (not v47 or v111 ~= Enum.Material.Water) then
                                        v112 = v110 - v105;
                                        if v47 and v103.Y <= l_v78_0 then
                                            l_v81_0[v103.X][v103.Y][v103.Z] = if v112 >= 0 then v112 else 0;
                                            l_v80_0[v103.X][v103.Y][v103.Z] = if v112 <= 0 then Enum.Material.Air else v111;
                                            v72[v103.X][v103.Y][v103.Z] = if v103.Y == l_v78_0 then l_v79_0 else 1;
                                            v71[v103.X][v103.Y][v103.Z] = Enum.Material.Water;
                                        else
                                            v72[v103.X][v103.Y][v103.Z] = if v112 >= 0 then v112 else 0;
                                            v71[v103.X][v103.Y][v103.Z] = if v112 <= 0 then Enum.Material.Air else v111;
                                        end;
                                        v105 = v105 - v110;
                                        if v105 > 0 then
                                            return v105, v107;
                                        else
                                            return 0, v107;
                                        end;
                                    elseif (v106 <= v26 and -v26 < v106) and (not v47 or v111 ~= Enum.Material.Water) then
                                        v112 = 1 - (v106 + v26) / v1.VoxelResolution;
                                        local v113 = v110 - v105;
                                        if v47 and v103.Y <= l_v78_0 then
                                            if v112 <= v113 then
                                                v72[v103.X][v103.Y][v103.Z] = if v103.Y == l_v78_0 then l_v79_0 else 1;
                                                v71[v103.X][v103.Y][v103.Z] = Enum.Material.Water;
                                                l_v81_0[v103.X][v103.Y][v103.Z] = v113;
                                                l_v80_0[v103.X][v103.Y][v103.Z] = v111;
                                            else
                                                v72[v103.X][v103.Y][v103.Z] = if v103.Y == l_v78_0 then l_v79_0 else 1;
                                                v71[v103.X][v103.Y][v103.Z] = Enum.Material.Water;
                                                l_v81_0[v103.X][v103.Y][v103.Z] = v112;
                                                l_v80_0[v103.X][v103.Y][v103.Z] = v111;
                                            end;
                                        else
                                            local v114 = if v112 <= v113 then v113 else v112;
                                            v72[v103.X][v103.Y][v103.Z] = v114;
                                            v71[v103.X][v103.Y][v103.Z] = if v114 <= 0 then Enum.Material.Air else v111;
                                        end;
                                        return 0, v107;
                                    end;
                                end;
                                return v105, v107;
                            else
                                return v105, v107;
                            end;
                        end;
                    end;
                    local function _(v116, v117)
                        return v116 * v63 + v116 * (v117 * v1.VoxelResolution);
                    end;
                    local function v158(v119, v120, v121)
                        local l_Direction_0 = v119.Direction;
                        local v123 = v119.Increment or 1;
                        local l_Size_1 = v119.Size;
                        local l_Direction_1 = v120.Direction;
                        local l_Size_2 = v120.Size;
                        local l_Direction_2 = v121.Direction;
                        local l_Size_3 = v121.Size;
                        local v129 = if v123 < 0 then l_Size_1 else 1;
                        local v130 = v123 < 0 and 1 or l_Size_1;
                        local v131 = l_Direction_0 * v63 + l_Direction_0 * (v129 * v1.VoxelResolution);
                        local v132 = l_Direction_0 * v63 + l_Direction_0 * (v130 * v1.VoxelResolution);
                        local v133 = l_Plane_0 * Vector3.new(-1, -1, -1);
                        local v134 = l_Direction_0 * v123;
                        local v135 = v134 * Vector3.new(-1, -1, -1);
                        for v136 = 1, l_Size_2 do
                            local v137 = l_Direction_1 * v63 + l_Direction_1 * (v136 * v1.VoxelResolution);
                            local v138 = v136 * l_Direction_1;
                            for v139 = 1, l_Size_3 do
                                local v140 = (l_Direction_2 * v63 + l_Direction_2 * (v139 * v1.VoxelResolution)) + v137;
                                local v141 = v139 * l_Direction_2;
                                local l_v46_0 = v46;
                                local l_v46_1 = v46;
                                local v144 = not v45;
                                local v145 = not v45;
                                local _, v147 = v13(v131 + v140, v134, l_PlanePosition_0, v133);
                                local _, v149 = v13(v132 + v140, v135, l_PlanePosition_0, l_Plane_0);
                                v147 = math.round(v147 * 256) / 256;
                                v149 = math.round(v149 * 256) / 256;
                                local v150 = false;
                                for v151 = v129, v130, v123 do
                                    if (v49 and -v1.VoxelResolution / 2 < v147) and l_v46_0 ~= 0 then
                                        local v152, v153, v154 = v98((l_Direction_0 * v151 + v138) + v141, ((l_Direction_0 * v63 + l_Direction_0 * (v151 * v1.VoxelResolution)) + v140) - v66, l_v46_0, v147, v144);
                                        l_v46_0 = v152;
                                        v144 = v153;
                                        v150 = v154;
                                        v147 = v147 - v1.VoxelResolution;
                                    end;
                                end;
                                if not v150 then
                                    for v155 = v130, v129, -v123 do
                                        if (v50 and -v1.VoxelResolution / 2 < v149) and l_v46_1 ~= 0 then
                                            local v156, v157 = v115((l_Direction_0 * v155 + v138) + v141, ((l_Direction_0 * v63 + l_Direction_0 * (v155 * v1.VoxelResolution)) + v140) - v66, l_v46_1, v149, v145);
                                            l_v46_1 = v156;
                                            v145 = v157;
                                            v149 = v149 - v1.VoxelResolution;
                                        end;
                                    end;
                                end;
                            end;
                        end;
                    end;
                    if v60.X ~= 0 then
                        v158({
                            Direction = Vector3.xAxis,
                            Increment = v60.X,
                            Size = v74
                        }, {
                            Direction = Vector3.yAxis,
                            Size = v75
                        }, {
                            Direction = Vector3.zAxis,
                            Size = v76
                        });
                    elseif v60.Y ~= 0 then
                        v158({
                            Direction = Vector3.yAxis,
                            Increment = v60.Y,
                            Size = v75
                        }, {
                            Direction = Vector3.xAxis,
                            Size = v74
                        }, {
                            Direction = Vector3.zAxis,
                            Size = v76
                        });
                    elseif v60.Z ~= 0 then
                        v158({
                            Direction = Vector3.zAxis,
                            Increment = v60.Z,
                            Size = v76
                        }, {
                            Direction = Vector3.xAxis,
                            Size = v74
                        }, {
                            Direction = Vector3.yAxis,
                            Size = v75
                        });
                    end;
                    if v23() then
                        v36:addTimeStatistic(v3.NormalizeBrushTimer(v73, l_Size_0, l_Height_0));
                    end;
                    if v47 then
                        v29.Terrain:WriteVoxels(v67, v1.VoxelResolution, l_v80_0, l_v81_0);
                    end;
                    v29.Terrain:WriteVoxels(v67, v1.VoxelResolution, v71, v72);
                end;
            end;
            return true, 0;
        end else function(v159, v160)
            assert(v159.State, "Tried to step without starting first.");
            if v11.ProfileTools() then
                debug.profilebegin("Flatten");
            end;
            local l_State_1 = v159.State;
            local v162 = v159.Payload[l_Category_0.BrushSettings];
            local v163 = v162[l_BrushSettings_0.BrushSize];
            local l_Height_1 = v163.Height;
            local l_Size_4 = v163.Size;
            local v166 = v162[l_BrushSettings_0.BrushShape];
            local v167 = v162[l_BrushSettings_0.FlattenMode];
            local v168 = v162[l_BrushSettings_0.IgnoreWater];
            local _ = v162[l_BrushSettings_0.PullTerrain];
            local v170 = v162[l_BrushSettings_0.Strength];
            local v171 = v162[l_BrushSettings_0.State];
            local v172 = true;
            if v167 ~= l_FlattenMode_0.Grow then
                v172 = v167 == l_FlattenMode_0.Both;
            end;
            local v173 = true;
            if v167 ~= l_FlattenMode_0.Erode then
                v173 = v167 == l_FlattenMode_0.Both;
            end;
            local v174 = if v166 == l_BrushShape_0.Sphere then l_Size_4 else l_Height_1 * v1.VoxelResolution;
            local v175 = (l_Size_4 * v1.VoxelResolution) * 0.5;
            local l_Plane_1 = l_State_1.Plane;
            local l_PlanePosition_1 = l_State_1.PlanePosition;
            local v178 = math.abs(l_Plane_1.X);
            local v179 = math.abs(l_Plane_1.Y);
            local v180 = math.abs(l_Plane_1.Z);
            local _ = v166 == l_BrushShape_0.Sphere;
            local _ = v166 == l_BrushShape_0.Cube;
            local _ = if v180 <= v179 and v178 <= v179 then Vector3.new(0, l_Plane_1.Y > 0 and 1 or -1, 0) else if v179 <= v180 and v178 <= v180 then Vector3.new(0, 0, l_Plane_1.Z > 0 and 1 or -1) else Vector3.new(l_Plane_1.X > 0 and 1 or -1, 0, 0);
            local v184 = v10(v162[l_BrushSettings_0.PivotPosition], v171.Position, v174);
            local v185 = v12(l_State_1.Position, v184, v175);
            l_State_1.Position = v185[#v185];
            local v186, v187 = v9(v184, v175, v174);
            for _, v189 in v185, nil, nil do
                local v190 = Region3.new(v186, v187);
                local v191 = (v187.X - v186.X) * 0.5;
                local v192, v193 = v29.Terrain:ReadVoxels(v190, v1.VoxelResolution);
                v193.Size = nil;
                v192.Size = nil;
                local v194, v195 = v29.Terrain:ReadVoxels(v190, v1.VoxelResolution);
                local v196 = #v192;
                local v197 = #v192[1];
                local v198 = #v192[1][1];
                local _ = Vector3.new(v196, v197, v198);
                local v200 = (l_PlanePosition_1.Y - v189.Y) / v1.VoxelResolution;
                local v201 = v189.Y / v1.VoxelResolution;
                local v202 = math.clamp(math.floor((v200 + v197 / 2) + (v201 - math.floor(v201))), 1, v197);
                local v203 = math.floor((((l_PlanePosition_1.Y + 2) % v1.VoxelResolution) / v1.VoxelResolution) * 256) / 256;
                local v204 = false;
                if v166 == l_BrushShape_0.Sphere then
                    v204 = l_Size_4 > 2;
                end;
                local v205;
                if v166 == l_BrushShape_0.Cylinder then
                    v205 = true;
                    if not (l_Size_4 <= 2) then
                        goto label1;
                    end;
                end;
                v205 = v204;
                ::label1::;
                local v206 = (v186.X - l_VoxelResolution_0 * 0.5) - v189.X;
                local v207 = (v186.Y - l_VoxelResolution_0 * 0.5) - v189.Y;
                local v208 = (v186.Z - l_VoxelResolution_0 * 0.5) - v189.Z;
                local v209 = nil;
                local v210 = nil;
                local v211 = nil;
                local v212 = nil;
                if v23() then
                    v212 = os.clock();
                end;
                for v213 = 1, v196 do
                    if v205 then
                        v209 = (v213 * v1.VoxelResolution + v206) ^ 2;
                    end;
                    local v214 = v193[v213];
                    local v215 = v192[v213];
                    local v216 = v195[v213];
                    local v217 = v194[v213];
                    for v218 = 1, v198 do
                        local v219 = 1;
                        if v205 then
                            v210 = (v218 * v1.VoxelResolution + v208) ^ 2;
                            v211 = math.sqrt(v209 + v210);
                            v219 = v7(v211, v191);
                        end;
                        local v220 = (v219 * v170) * 0.1;
                        local v221 = (v219 * v170) * 0.1;
                        local v222 = false;
                        local v223 = false;
                        if v172 then
                            for v224 = 1, v202 do
                                local v225 = v214[v224][v218];
                                local v226 = v215[v224][v218];
                                local v227 = 1;
                                if v205 then
                                    v227 = v6(if not v204 then v211 else math.sqrt((v209 + v210) + (v224 * v1.VoxelResolution + v207) ^ 2), v191);
                                end;
                                if v227 > 0 and (not (v225 == 0) or v223) then
                                    v223 = true;
                                    if v225 ~= 1 then
                                        local v228 = v225 + v220;
                                        if v228 > 1 then
                                            v220 = v228 - 1;
                                            v228 = 1;
                                        else
                                            v220 = 0;
                                        end;
                                        if v224 == v202 and v203 < v228 then
                                            v228 = v203;
                                        end;
                                        if v225 == 0 and v224 > 1 then
                                            v226 = v192[v213][v224 - 1][v218];
                                        end;
                                        if not (not (not v168 or v226 ~= Enum.Material.Water) or not v226) and v225 ~= v228 then
                                            v216[v224][v218] = v228;
                                            v217[v224][v218] = v226;
                                            v222 = true;
                                            if not (v220 ~= 0) then
                                                break;
                                            end;
                                        else
                                            break;
                                        end;
                                    end;
                                end;
                            end;
                        end;
                        exitedTerrain = false;
                        if v173 then
                            for v229 = v197, v202, -1 do
                                local v230 = v214[v229][v218];
                                local v231 = v215[v229][v218];
                                local v232 = 1;
                                if v205 then
                                    v232 = v6(if not v204 then v211 else math.sqrt((v209 + v210) + (v229 * v1.VoxelResolution + v207) ^ 2), v191);
                                end;
                                if v232 > 0 and (not (v230 == 1) or exitedTerrain) then
                                    exitedTerrain = true;
                                    if v230 ~= 0 then
                                        local v233 = v230 - v221;
                                        if v233 < 0 then
                                            v221 = -v233;
                                            v233 = 0;
                                        else
                                            v221 = 0;
                                        end;
                                        if v229 == v202 and v233 < v203 then
                                            v233 = v203;
                                        end;
                                        if v233 == 0 then
                                            v231 = Enum.Material.Air;
                                        end;
                                        if not v168 or v231 ~= Enum.Material.Water then
                                            v216[v229][v218] = v233;
                                            v217[v229][v218] = v231;
                                            if not (v221 ~= 0) then
                                                break;
                                            end;
                                        else
                                            break;
                                        end;
                                    end;
                                end;
                            end;
                        end;
                    end;
                end;
                if v23() then
                    v160:addTimeStatistic(v3.NormalizeBrushTimer(v212, l_Size_4, l_Height_1));
                end;
                v29.Terrain:WriteVoxels(v190, v1.VoxelResolution, v194, v195);
            end;
            if v11.ProfileTools() then
                debug.profileend();
            end;
            return true, 0;
        end
    });
end;


local l_script_FirstAncestor_0 = script:FindFirstAncestor("TerrainEditor");
local l_Generation_0 = l_script_FirstAncestor_0.Src.Util.Generation;
local v2 = require(l_Generation_0.ComputeMasterSeed);
local v3 = require(l_Generation_0.NoisySeed);
local v4 = require(l_Generation_0.Perlin);
local v5 = require(l_Generation_0.ProcessVoxel);
local v6 = require(l_Generation_0.ProcessPerlin);
local v7 = require(l_script_FirstAncestor_0.Src.Resources.Constants);
local l_VoxelResolution_0 = v7.VoxelResolution;
local l_RegionNormalization_0 = v7.RegionNormalization;
local l_Util_0 = l_script_FirstAncestor_0.Src.Util;
local v11 = require(l_Util_0.AnalyticsHelper);
local v12 = require(l_Util_0.Operations.BaseOperation);
local v13 = require(l_script_FirstAncestor_0.Src.Types);
local l_Biome_0 = v13.Biome;
local l_BiomeSettings_0 = v13.BiomeSettings;
local l_Category_0 = v13.Category;
local l_SelectionSettings_0 = v13.SelectionSettings;
local v18 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorTimeStatistic);
local v19 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorErrorHandling);
local v20 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorIndexingBugFix);
return function(v21, v22, v23)
    local v24 = 0;
    local v25 = 0;
    return v12.new({
        AllowPause = true,
        AllowCancel = true,
        Description = v22,
        Name = v21,
        OnFinish = function(v26, v27)
            if v18() then
                local v28 = v26.Payload[l_Category_0.SelectionSettings][l_SelectionSettings_0.Size];
                if (l_RegionNormalization_0 <= v28.X and l_RegionNormalization_0 <= v28.Y) and l_RegionNormalization_0 <= v28.Z then
                    v27:addTimeStatistic(v11.NormalizeRegionTimer(v26.State.StartTime, v28));
                end;
            end;
            v23.ChangeHistoryService:SetWaypoint("GenerateAction");
        end,
        OnStart = function(v29, _)
            local v31 = v2((tostring(v29.Payload[l_Category_0.BiomeSettings][l_BiomeSettings_0.Seed])));
            local v32 = v4(v31);
            local v33 = v3(v31);
            local l_Position_0 = v29.Payload[l_Category_0.SelectionSettings][l_SelectionSettings_0.Transform].Position;
            local v35 = v29.Payload[l_Category_0.SelectionSettings][l_SelectionSettings_0.Size];
            local v36 = v35 / 2;
            local v37 = Region3.new(l_Position_0 - v36, l_Position_0 + v36):ExpandToGrid(l_VoxelResolution_0);
            l_Position_0 = v37.CFrame.Position;
            local v38 = v37.Size / l_VoxelResolution_0;
            local v39 = v38 / 2;
            local v40 = l_Position_0 / l_VoxelResolution_0;
            local v41 = Region3.new((-v39 + Vector3.new(-1, 0, 0)) * l_VoxelResolution_0 + l_Position_0, (v39 * Vector3.new(-1, 1, 1)) * l_VoxelResolution_0 + l_Position_0):ExpandToGrid(l_VoxelResolution_0);
            local v42 = v41.Size.Y / l_VoxelResolution_0;
            local v43 = v41.Size.Z / l_VoxelResolution_0;
            local v44 = if not v19() then nil else math.floor(65536 / v42);
            local v45 = {
                table.create(v42)
            };
            local v46 = {
                table.create(v42)
            };
            if not v20() then
                for v47 = 1, v42 do
                    v45[1][v47] = table.create(if not v19() then v43 else v44, 0);
                    v46[1][v47] = table.create(if not v19() then v43 else v44, Enum.Material.Air);
                end;
            else
                for v48 = 1, v42 do
                    local v49 = if v43 < v44 then v43 else v44;
                    v45[1][v48] = table.create(v49, 0);
                    v46[1][v48] = table.create(v49, Enum.Material.Air);
                end;
            end;
            local v50 = table.create(9);
            for v51 = 1, 9 do
                v50[v51] = {
                    DistSquared = 0,
                    BiomeNoise = 0
                };
            end;
            local v52 = v29.Payload[l_Category_0.BiomeSettings][l_BiomeSettings_0.BiomeBlending];
            local v53 = {};
            for v54, v55 in v29.Payload[l_Category_0.BiomeSettings][l_BiomeSettings_0.Biomes], nil, nil do
                if v55 then
                    table.insert(v53, v54);
                end;
            end;
            v29.State = {
                Biomes = v53,
                BiomePoints = v50,
                BiomeBlending = v52,
                BiomeBlendingInverse = 1 - v52,
                BiomeSize = v29.Payload[l_Category_0.BiomeSettings][l_BiomeSettings_0.BiomeSize],
                GetPerlin = v32,
                GetNoise = v33,
                MaterialMap = v46,
                MaxWidth = v44,
                OccupancyMap = v45,
                Position = l_Position_0,
                PreviousColumnHeights = {},
                ProcessVoxel = v5(v32, v33, {
                    Biomes = v53,
                    DefaultBiomeFill = Enum.Material.Rock,
                    DefaultBiomeSurface = Enum.Material.Grass,
                    DefaultBiomeValue = 0.5,
                    HasCaves = v29.Payload[l_Category_0.BiomeSettings][l_BiomeSettings_0.Caves],
                    MapHeight = 0.5 / (v35.Y / l_VoxelResolution_0),
                    SurfaceThickness = 0.018,
                    WaterLevel = 0.48
                }),
                SliceY = v42,
                SliceZ = v43,
                StartTime = os.clock(),
                VoxelCorner = v40 - v38 / 2,
                VoxelExtents = v39,
                VoxelSize = v38,
                X = 1
            };
        end,
        OnStep = if not v19() then function(v56, _)
            local l_State_0 = v56.State;
            local l_Biomes_0 = l_State_0.Biomes;
            local l_BiomePoints_0 = l_State_0.BiomePoints;
            local l_BiomeSize_0 = l_State_0.BiomeSize;
            local l_BiomeBlending_0 = l_State_0.BiomeBlending;
            local l_BiomeBlendingInverse_0 = l_State_0.BiomeBlendingInverse;
            local l_GetPerlin_0 = l_State_0.GetPerlin;
            local l_GetNoise_0 = l_State_0.GetNoise;
            local l_MaterialMap_0 = l_State_0.MaterialMap;
            local l_OccupancyMap_0 = l_State_0.OccupancyMap;
            local l_Position_1 = l_State_0.Position;
            local l_PreviousColumnHeights_0 = l_State_0.PreviousColumnHeights;
            local l_ProcessVoxel_0 = l_State_0.ProcessVoxel;
            local l_SliceY_0 = l_State_0.SliceY;
            local l_SliceZ_0 = l_State_0.SliceZ;
            local l_VoxelCorner_0 = l_State_0.VoxelCorner;
            local l_VoxelExtents_0 = l_State_0.VoxelExtents;
            local l_VoxelSize_0 = l_State_0.VoxelSize;
            local l_X_0 = l_State_0.X;
            local v77 = l_X_0 - l_VoxelExtents_0.X;
            local v78 = Region3.new(Vector3.new(v77 - 1, -l_VoxelExtents_0.Y, -l_VoxelExtents_0.Z) * l_VoxelResolution_0 + l_Position_1, Vector3.new(v77, l_VoxelExtents_0.Y, l_VoxelExtents_0.Z) * l_VoxelResolution_0 + l_Position_1):ExpandToGrid(l_VoxelResolution_0);
            for v79 = 1, l_SliceZ_0 do
                local v80 = (l_VoxelCorner_0 * Vector3.new(1, 0, 1) + Vector3.new(l_X_0 - 1, 0, 0)) + Vector3.new(0, 0, v79 - 1);
                local v81 = (v80.X / l_BiomeSize_0 + v6(l_GetPerlin_0(v80, 233, l_BiomeSize_0 * 0.3)) * 0.25) + v6(l_GetPerlin_0(v80, 235, l_BiomeSize_0 * 0.05)) * 0.075;
                local v82 = (v80.Z / l_BiomeSize_0 + v6(l_GetPerlin_0(v80, 234, l_BiomeSize_0 * 0.3)) * 0.25) + v6(l_GetPerlin_0(v80, 236, l_BiomeSize_0 * 0.05)) * 0.075;
                local v83 = 10000000;
                local v84 = false;
                local v85 = 0;
                for v86 = -1, 1 do
                    for v87 = -1, 1 do
                        local v88 = math.floor((v81 + v86) + 0.5);
                        local v89 = math.floor((v82 + v87) + 0.5);
                        local v90 = ((v88 + (l_GetNoise_0((Vector3.new(v88, v89, 53))) - 0.5) * 0.75) - v81) ^ 2 + ((v89 + (l_GetNoise_0((Vector3.new(v88, v89, 73))) - 0.5) * 0.75) - v82) ^ 2;
                        if v90 < v83 then
                            v83 = v90;
                        end;
                        local v91 = l_BiomePoints_0[((v86 + 1) + 3 * (v87 + 1)) + 1];
                        v91.DistSquared = v90;
                        v91.BiomeNoise = l_GetNoise_0((Vector3.new(v88, v89, 0)));
                    end;
                end;
                local v92 = table.create(#l_Biomes_0);
                for _, v94 in l_BiomePoints_0, nil, nil do
                    local v95 = v94.DistSquared == v83 and 1 or (math.sqrt(v83) / math.sqrt(v94.DistSquared) - l_BiomeBlendingInverse_0) / l_BiomeBlending_0;
                    if v95 > 0 then
                        v95 = v95 ^ 2.1;
                        v85 = v85 + v95;
                        local v96 = l_Biomes_0[math.ceil(#l_Biomes_0 * (1 - v94.BiomeNoise))];
                        local v97 = v92[v96] or {
                            Weight = 0
                        };
                        v97.Weight = v97.Weight + v95;
                        v92[v96] = v97;
                    end;
                end;
                for v98, v99 in pairs(v92) do
                    v99.Weight = v99.Weight / v85;
                    if v98 == l_Biome_0.Arctic then
                        v84 = true;
                    end;
                end;
                local v100 = l_PreviousColumnHeights_0[v79] or l_SliceY_0;
                local v101 = false;
                local v102 = 0;
                for v103 = 1, l_SliceY_0 do
                    if not v101 then
                        local v104, v105, v106 = l_ProcessVoxel_0(Vector3.new(l_X_0, v103, v79), Vector3.new(v80.X, l_SliceY_0, v80.Z), {
                            WeightPoints = v92,
                            HasCaves = not v84
                        });
                        v24 = v24 + 1;
                        l_MaterialMap_0[1][v103][v79] = v104;
                        l_OccupancyMap_0[1][v103][v79] = v105;
                        v102 = v103;
                        v101 = v106;
                    end;
                    if v101 then
                        v25 = v25 + 1;
                        l_OccupancyMap_0[1][v103][v79] = 0;
                        l_MaterialMap_0[1][v103][v79] = Enum.Material.Air;
                        if not (v100 >= v103) then
                            break;
                        end;
                    end;
                end;
                l_PreviousColumnHeights_0[v79] = v102;
            end;
            v23.Terrain.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.Generate;
            v23.Terrain:WriteVoxels(v78, l_VoxelResolution_0, l_MaterialMap_0, l_OccupancyMap_0);
            local l_State_1 = v56.State;
            l_State_1.X = l_State_1.X + 1;
            l_State_1 = l_X_0 / l_VoxelSize_0.x;
            return l_State_1 < 1, l_State_1;
        end else function(v108, _)
            local l_State_2 = v108.State;
            local l_Biomes_1 = l_State_2.Biomes;
            local l_BiomePoints_1 = l_State_2.BiomePoints;
            local l_BiomeSize_1 = l_State_2.BiomeSize;
            local l_BiomeBlending_1 = l_State_2.BiomeBlending;
            local l_BiomeBlendingInverse_1 = l_State_2.BiomeBlendingInverse;
            local l_GetPerlin_1 = l_State_2.GetPerlin;
            local l_GetNoise_1 = l_State_2.GetNoise;
            local l_MaterialMap_1 = l_State_2.MaterialMap;
            local l_MaxWidth_0 = l_State_2.MaxWidth;
            local l_OccupancyMap_1 = l_State_2.OccupancyMap;
            local l_Position_2 = l_State_2.Position;
            local l_PreviousColumnHeights_1 = l_State_2.PreviousColumnHeights;
            local l_ProcessVoxel_1 = l_State_2.ProcessVoxel;
            local l_SliceY_1 = l_State_2.SliceY;
            local l_SliceZ_1 = l_State_2.SliceZ;
            local l_VoxelCorner_1 = l_State_2.VoxelCorner;
            local l_VoxelExtents_1 = l_State_2.VoxelExtents;
            local l_VoxelSize_1 = l_State_2.VoxelSize;
            local l_X_1 = l_State_2.X;
            local v130 = l_X_1 - l_VoxelExtents_1.X;
            local l_l_SliceZ_1_0 = l_SliceZ_1;
            local v132 = -l_VoxelExtents_1.Z;
            local v133 = {};
            if not v20() then
                while l_l_SliceZ_1_0 > 0 do
                    table.insert(v133, Region3.new(Vector3.new(v130 - 1, -l_VoxelExtents_1.Y, v132) * l_VoxelResolution_0 + l_Position_2, Vector3.new(v130, l_VoxelExtents_1.Y, v132 + l_MaxWidth_0) * l_VoxelResolution_0 + l_Position_2):ExpandToGrid(l_VoxelResolution_0));
                    l_l_SliceZ_1_0 = l_l_SliceZ_1_0 - l_MaxWidth_0;
                    v132 = v132 + l_MaxWidth_0;
                end;
            else
                while l_l_SliceZ_1_0 > 0 do
                    table.insert(v133, Region3.new(Vector3.new(v130 - 1, -l_VoxelExtents_1.Y, v132) * l_VoxelResolution_0 + l_Position_2, Vector3.new(v130, l_VoxelExtents_1.Y, v132 + if l_l_SliceZ_1_0 < l_MaxWidth_0 then l_l_SliceZ_1_0 else l_MaxWidth_0) * l_VoxelResolution_0 + l_Position_2):ExpandToGrid(l_VoxelResolution_0));
                    l_l_SliceZ_1_0 = l_l_SliceZ_1_0 - l_MaxWidth_0;
                    v132 = v132 + l_MaxWidth_0;
                end;
                if #v133 == 0 then
                    return false, 1;
                end;
            end;
            local v134 = 1;
            for v135 = 1, l_SliceZ_1 do
                local v136 = math.floor((v135 - 1) / l_MaxWidth_0) + 1;
                if v136 ~= v134 then
                    v23.Terrain:WriteVoxels(v133[v134], l_VoxelResolution_0, l_MaterialMap_1, l_OccupancyMap_1);
                    if v136 == #v133 then
                        l_OccupancyMap_1 = {
                            table.create(l_SliceY_1)
                        };
                        l_MaterialMap_1 = {
                            table.create(l_SliceY_1)
                        };
                        for v137 = 1, l_SliceY_1 do
                            l_OccupancyMap_1[1][v137] = table.create(l_MaxWidth_0, 0);
                            l_MaterialMap_1[1][v137] = table.create(l_MaxWidth_0, Enum.Material.Air);
                        end;
                    end;
                    v134 = v136;
                end;
                local v138 = v135 - (v134 - 1) * l_MaxWidth_0;
                local v139 = (l_VoxelCorner_1 * Vector3.new(1, 0, 1) + Vector3.new(l_X_1 - 1, 0, 0)) + Vector3.new(0, 0, v135 - 1);
                local v140 = (v139.X / l_BiomeSize_1 + v6(l_GetPerlin_1(v139, 233, l_BiomeSize_1 * 0.3)) * 0.25) + v6(l_GetPerlin_1(v139, 235, l_BiomeSize_1 * 0.05)) * 0.075;
                local v141 = (v139.Z / l_BiomeSize_1 + v6(l_GetPerlin_1(v139, 234, l_BiomeSize_1 * 0.3)) * 0.25) + v6(l_GetPerlin_1(v139, 236, l_BiomeSize_1 * 0.05)) * 0.075;
                local v142 = 10000000;
                local v143 = false;
                local v144 = 0;
                for v145 = -1, 1 do
                    for v146 = -1, 1 do
                        local v147 = math.floor((v140 + v145) + 0.5);
                        local v148 = math.floor((v141 + v146) + 0.5);
                        local v149 = ((v147 + (l_GetNoise_1((Vector3.new(v147, v148, 53))) - 0.5) * 0.75) - v140) ^ 2 + ((v148 + (l_GetNoise_1((Vector3.new(v147, v148, 73))) - 0.5) * 0.75) - v141) ^ 2;
                        if v149 < v142 then
                            v142 = v149;
                        end;
                        local v150 = l_BiomePoints_1[((v145 + 1) + 3 * (v146 + 1)) + 1];
                        v150.DistSquared = v149;
                        v150.BiomeNoise = l_GetNoise_1((Vector3.new(v147, v148, 0)));
                    end;
                end;
                local v151 = table.create(#l_Biomes_1);
                for _, v153 in l_BiomePoints_1, nil, nil do
                    local v154 = v153.DistSquared == v142 and 1 or (math.sqrt(v142) / math.sqrt(v153.DistSquared) - l_BiomeBlendingInverse_1) / l_BiomeBlending_1;
                    if v154 > 0 then
                        v154 = v154 ^ 2.1;
                        v144 = v144 + v154;
                        local v155 = l_Biomes_1[math.ceil(#l_Biomes_1 * (1 - v153.BiomeNoise))];
                        local v156 = v151[v155] or {
                            Weight = 0
                        };
                        v156.Weight = v156.Weight + v154;
                        v151[v155] = v156;
                    end;
                end;
                for v157, v158 in pairs(v151) do
                    v158.Weight = v158.Weight / v144;
                    if v157 == l_Biome_0.Arctic then
                        v143 = true;
                    end;
                end;
                local v159 = l_PreviousColumnHeights_1[v135] or l_SliceY_1;
                local v160 = false;
                local v161 = 0;
                for v162 = 1, l_SliceY_1 do
                    if not v160 then
                        local v163, v164, v165 = l_ProcessVoxel_1(Vector3.new(l_X_1, v162, v135), Vector3.new(v139.X, l_SliceY_1, v139.Z), {
                            WeightPoints = v151,
                            HasCaves = not v143
                        });
                        v24 = v24 + 1;
                        l_MaterialMap_1[1][v162][v138] = v163;
                        l_OccupancyMap_1[1][v162][v138] = v164;
                        v161 = v162;
                        v160 = v165;
                    end;
                    if v160 then
                        v25 = v25 + 1;
                        l_OccupancyMap_1[1][v162][v138] = 0;
                        l_MaterialMap_1[1][v162][v138] = Enum.Material.Air;
                        if not (v159 >= v162) then
                            break;
                        end;
                    end;
                end;
                l_PreviousColumnHeights_1[v135] = v161;
            end;
            v23.Terrain.LastUsedModificationMethod = Enum.TerrainAcquisitionMethod.Generate;
            v23.Terrain:WriteVoxels(v133[v134], l_VoxelResolution_0, l_MaterialMap_1, l_OccupancyMap_1);
            local l_State_3 = v108.State;
            l_State_3.X = l_State_3.X + 1;
            l_State_3 = l_X_1 / l_VoxelSize_1.x;
            return l_State_3 < 1, l_State_3;
        end
    });
end;


local l_script_FirstAncestor_0 = script:FindFirstAncestor("TerrainEditor");
local v1 = require(l_script_FirstAncestor_0.Src.Util.Operations.BaseOperation);
local l_HeightmapImporterService_0 = game:GetService("HeightmapImporterService");
local v3 = require(l_script_FirstAncestor_0.Src.Types);
local l_Category_0 = v3.Category;
local l_HeightmapSettings_0 = v3.HeightmapSettings;
local l_SelectionSettings_0 = v3.SelectionSettings;
local v7 = require(l_script_FirstAncestor_0.Src.Resources.Constants);
local l_RegionNormalization_0 = v7.RegionNormalization;
local l_MaxImportVolume_0 = v7.MaxImportVolume;
local l_Util_0 = l_script_FirstAncestor_0.Src.Util;
local v11 = require(l_Util_0.AnalyticsHelper);
local v12 = require(l_Util_0.ConvertTransformToRegion);
local v13 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorTimeStatistic);
local v14 = require(l_script_FirstAncestor_0.Src.Flags.getFFlagTerrainEditorHeightmapFixes);
local function _(v15)
    return (Vector3.new(v15.X * 4, v15.Y * 4, v15.Z * 4));
end;
return function(v17, v18, v19)
    return v1.new({
        AllowPause = true,
        AllowCancel = true,
        Description = v18,
        Name = v17,
        OnCancel = function(_, _)
            l_HeightmapImporterService_0:CancelImportHeightmap();
        end,
        OnFinish = function(v22, v23)
            local l_State_0 = v22.State;
            if l_State_0.ProgressUpdateConnection then
                l_State_0.ProgressUpdateConnection:Disconnect();
                l_State_0.ProgressUpdateConnection = nil;
                l_State_0.Progress = 0;
            end;
            if v13() then
                local v25 = v22.Payload[l_Category_0.SelectionSettings][l_SelectionSettings_0.Size];
                if (l_RegionNormalization_0 <= v25.X and l_RegionNormalization_0 <= v25.Y) and l_RegionNormalization_0 <= v25.Z then
                    v23:addTimeStatistic(v11.NormalizeRegionTimer(l_State_0.StartTime, v25));
                end;
            end;
            v19.ChangeHistoryService:SetWaypoint("ImportAction");
        end,
        OnPause = function(_, _)
            l_HeightmapImporterService_0:SetImportHeightmapPaused(true);
        end,
        OnResume = function(_, _)
            l_HeightmapImporterService_0:SetImportHeightmapPaused(false);
        end,
        OnStart = function(v30, v31)
            local function v40()
                local v32 = v30.Payload[l_Category_0.SelectionSettings];
                local v33 = v30.Payload[l_Category_0.HeightmapSettings];
                local v34 = v12(v32[l_SelectionSettings_0.Transform], v32[l_SelectionSettings_0.Size], true);
                local l_Min_0 = v34.Min;
                local v36 = Vector3.new(l_Min_0.X * 4, l_Min_0.Y * 4, l_Min_0.Z * 4);
                local l_Max_0 = v34.Max;
                l_Min_0 = Vector3.new(l_Max_0.X * 4, l_Max_0.Y * 4, l_Max_0.Z * 4);
                if v14() then
                    l_Max_0 = l_Min_0 - v36;
                    if l_MaxImportVolume_0 < (l_Max_0.X * l_Max_0.Y) * l_Max_0.Z then
                        l_Min_0 = l_Min_0 - Vector3.one * 4;
                    end;
                end;
                l_Max_0 = Region3.new(v36, l_Min_0);
                if v33[l_HeightmapSettings_0.Heightmap].Image then
                    local l_TemporaryId_0 = v33[l_HeightmapSettings_0.Heightmap].Image.File:GetTemporaryId();
                    local l_Image_0 = v33[l_HeightmapSettings_0.Colormap].Image;
                    l_HeightmapImporterService_0:ImportHeightmap(l_Max_0, l_TemporaryId_0, not l_Image_0 and "" or l_Image_0.File:GetTemporaryId(), v33[l_HeightmapSettings_0.DefaultMaterial]);
                    v30.State.Progress = 1;
                    return ;
                else
                    v30.State.Progress = 1;
                    return ;
                end;
            end;
            v30.State = {
                Progress = 0,
                ProgressUpdateConnection = l_HeightmapImporterService_0.ProgressUpdate:Connect(function(v41, v42)
                    v31:setDescription(v42);
                    v30.State.Progress = v41;
                end),
                StartTime = os.clock()
            };
            if not v30.Synchronous then
                task.spawn(v40);
                return ;
            else
                v40();
                return ;
            end;
        end,
        OnStep = function(v43, _)
            local l_State_1 = v43.State;
            if l_State_1.Progress and l_State_1.Progress == 1 then
                return false, 1;
            else
                return true, l_State_1.Progress or 0;
            end;
        end
    });
end;