private void UpdateCeilingHeights()
        {
            for (int i = 0; i < sectorData.Count; i++)
            {
                SectorData sd = sectorData[i];

                if (sd.Triangular)
                {
                    for (int c = 0; c < 3; c++)
                    {
                        HeightOffsetVertexData vd = sd.Verts[c];
                        vd.JitterCeilingHeight = General.Random(-100, 100) / 100f;
                        sd.Verts[c]            = vd;
                    }
                }

                sd.JitterCeilingHeight = General.Random(-100, 100) / 100f;
                sectorData[i]          = sd;
            }
        }
        public JitterSectorsForm(string editingModeName)
        {
            this.editingModeName = editingModeName;

            InitializeComponent();

            //get selection
            List <Vertex> verts   = new List <Vertex>();
            List <Sector> sectors = new List <Sector>();

            if (editingModeName == "BaseVisualMode")
            {
                VisualMode            vm             = (VisualMode)General.Editing.Mode;
                List <VisualGeometry> visualGeometry = vm.GetSelectedSurfaces();
                visualSectors = new List <VisualSector>();

                //get selected visual and regular sectors
                foreach (VisualGeometry vg in visualGeometry)
                {
                    if (vg.GeometryType != VisualGeometryType.CEILING && vg.GeometryType != VisualGeometryType.FLOOR)
                    {
                        continue;
                    }

                    if (vg.Sector != null && vg.Sector.Sector != null)
                    {
                        foreach (Sidedef sd in vg.Sector.Sector.Sidedefs)
                        {
                            if (!verts.Contains(sd.Line.Start))
                            {
                                verts.Add(sd.Line.Start);
                            }
                            if (!verts.Contains(sd.Line.End))
                            {
                                verts.Add(sd.Line.End);
                            }
                        }

                        sectors.Add(vg.Sector.Sector);
                        visualSectors.Add(vg.Sector);
                    }
                }

                //also get visual sectors around selected ones (because they also may be affected)
                List <Vertex> affectedVerts = new List <Vertex>();

                foreach (Sector s in sectors)
                {
                    foreach (Sidedef sd in s.Sidedefs)
                    {
                        if (!affectedVerts.Contains(sd.Line.Start))
                        {
                            affectedVerts.Add(sd.Line.Start);
                        }
                        if (!affectedVerts.Contains(sd.Line.End))
                        {
                            affectedVerts.Add(sd.Line.End);
                        }
                    }
                }

                List <Sector> affectedSectors = new List <Sector>();
                foreach (Vertex v in affectedVerts)
                {
                    foreach (Linedef l in v.Linedefs)
                    {
                        if (l.Front != null && !sectors.Contains(l.Front.Sector) &&
                            !affectedSectors.Contains(l.Front.Sector) &&
                            vm.VisualSectorExists(l.Front.Sector))
                        {
                            visualSectors.Add(vm.GetVisualSector(l.Front.Sector));
                            affectedSectors.Add(l.Front.Sector);
                        }
                        if (l.Back != null && !sectors.Contains(l.Back.Sector) &&
                            !affectedSectors.Contains(l.Back.Sector) &&
                            vm.VisualSectorExists(l.Back.Sector))
                        {
                            visualSectors.Add(vm.GetVisualSector(l.Back.Sector));
                            affectedSectors.Add(l.Back.Sector);
                        }
                    }
                }

                visualVerts = new List <VisualVertexPair>();
                foreach (Vertex vert in affectedVerts)
                {
                    if (vm.VisualVertices.ContainsKey(vert))
                    {
                        visualVerts.Add(vm.VisualVertices[vert]);
                    }
                }
            }
            else if (editingModeName == "SectorsMode")
            {
                ICollection <Sector> list = General.Map.Map.GetSelectedSectors(true);

                foreach (Sector s in list)
                {
                    foreach (Sidedef sd in s.Sidedefs)
                    {
                        if (!verts.Contains(sd.Line.Start))
                        {
                            verts.Add(sd.Line.Start);
                        }
                        if (!verts.Contains(sd.Line.End))
                        {
                            verts.Add(sd.Line.End);
                        }
                    }
                    sectors.Add(s);
                }
            }

            if (verts.Count == 0 || sectors.Count == 0)
            {
                General.Interface.DisplayStatus(StatusType.Warning, "Unable to get sectors from selection!");
                return;
            }

            //create undo
            General.Map.UndoRedo.ClearAllRedos();
            General.Map.UndoRedo.CreateUndo("Randomize " + sectors.Count + (sectors.Count > 1 ? " sectors" : " sector"));

            //update window header
            this.Text = "Randomize " + sectors.Count + (sectors.Count > 1 ? " sectors" : " sector");

            //store intial properties
//process verts...
            Dictionary <Vertex, TranslationOffsetVertexData> data = new Dictionary <Vertex, TranslationOffsetVertexData>();

            foreach (Vertex v in verts)
            {
                TranslationOffsetVertexData vd = new TranslationOffsetVertexData();
                vd.Vertex          = v;
                vd.InitialPosition = v.Position;
                data.Add(v, vd);
            }

            foreach (Vertex v in verts)
            {
                if (v.Linedefs == null)
                {
                    continue;
                }

                //get nearest linedef
                Linedef closestLine = null;
                double  distance    = double.MaxValue;

                // Go for all linedefs in selection
                foreach (Linedef l in General.Map.Map.Linedefs)
                {
                    if (v.Linedefs.Contains(l))
                    {
                        continue;
                    }

                    // Calculate distance and check if closer than previous find
                    double d = l.SafeDistanceToSq(v.Position, true);
                    if (d < distance)
                    {
                        // This one is closer
                        closestLine = l;
                        distance    = d;
                    }
                }

                if (closestLine == null)
                {
                    continue;
                }

                double closestLineDistance = Vector2D.Distance(v.Position, closestLine.NearestOnLine(v.Position));

                //check SafeDistance of closest line
                if (data.ContainsKey(closestLine.Start) &&
                    data[closestLine.Start].SafeDistance > closestLineDistance)
                {
                    TranslationOffsetVertexData vd = data[closestLine.Start];
                    vd.SafeDistance         = (int)Math.Floor(closestLineDistance);
                    data[closestLine.Start] = vd;
                }
                if (data.ContainsKey(closestLine.End) &&
                    data[closestLine.End].SafeDistance > closestLineDistance)
                {
                    TranslationOffsetVertexData vd = data[closestLine.End];
                    vd.SafeDistance       = (int)Math.Floor(closestLineDistance);
                    data[closestLine.End] = vd;
                }

                //save SafeDistance
                int dist = (int)Math.Floor(closestLineDistance);
                if (data[v].SafeDistance == 0 || data[v].SafeDistance > dist)
                {
                    TranslationOffsetVertexData vd = data[v];
                    vd.SafeDistance = dist;
                    data[v]         = vd;
                }
            }

            //store properties
            vertexData = new TranslationOffsetVertexData[data.Values.Count];
            data.Values.CopyTo(vertexData, 0);

            for (int i = 0; i < data.Count; i++)
            {
                if (vertexData[i].SafeDistance > 0)
                {
                    vertexData[i].SafeDistance /= 2;
                }
                if (MaxSafeDistance < vertexData[i].SafeDistance)
                {
                    MaxSafeDistance = vertexData[i].SafeDistance;
                }
            }

//process sectors and linedes
            sectorData  = new List <SectorData>();
            sidedefData = new List <SidedefData>();

            foreach (Sector s in sectors)
            {
                SectorData sd = new SectorData();

                sd.Sector = s;
                sd.InitialCeilingHeight = s.CeilHeight;
                sd.InitialFloorHeight   = s.FloorHeight;
                sd.Triangular           = General.Map.UDMF && s.Sidedefs.Count == 3;
                if (sd.Triangular)
                {
                    Vertex[] sectorverts = GetSectorVerts(s);
                    sd.Verts = new HeightOffsetVertexData[sectorverts.Length];
                    for (int i = 0; i < sectorverts.Length; i++)
                    {
                        HeightOffsetVertexData vd = new HeightOffsetVertexData();
                        vd.Vertex               = sectorverts[i];
                        vd.ZFloor               = sectorverts[i].ZFloor;
                        vd.ZCeiling             = sectorverts[i].ZCeiling;
                        vd.InitialFloorHeight   = double.IsNaN(vd.ZFloor) ? GetHighestFloor(sectorverts[i]) : sectorverts[i].ZFloor;
                        vd.InitialCeilingHeight = double.IsNaN(vd.ZCeiling) ? GetLowestCeiling(sectorverts[i]) : sectorverts[i].ZCeiling;

                        sd.Verts[i] = vd;
                    }
                }
                sd.SafeDistance = (s.CeilHeight - s.FloorHeight) / 2;
                if (sd.SafeDistance > MaxSafeHeightDistance)
                {
                    MaxSafeHeightDistance = sd.SafeDistance;
                }
                sectorData.Add(sd);

                foreach (Sidedef side in s.Sidedefs)
                {
                    //store initial sidedef properties
                    SidedefData sdd = new SidedefData();

                    sdd.Side        = side;
                    sdd.LowTexture  = side.LowTexture;
                    sdd.HighTexture = side.HighTexture;
                    sdd.PegBottom   = side.Line.IsFlagSet(General.Map.Config.LowerUnpeggedFlag);
                    sdd.PegTop      = side.Line.IsFlagSet(General.Map.Config.UpperUnpeggedFlag);

                    if (side.Other != null && !sectors.Contains(side.Other.Sector))
                    {
                        sdd.UpdateTextureOnOtherSide = true;
                        sdd.OtherHighTexture         = side.Other.HighTexture;
                        sdd.OtherLowTexture          = side.Other.LowTexture;
                    }

                    sidedefData.Add(sdd);
                }
            }

            positionJitterAmmount.Maximum = MaxSafeDistance;
            floorHeightAmmount.Maximum    = MaxSafeHeightDistance;
            ceilingHeightAmmount.Maximum  = MaxSafeHeightDistance;

            //set editing settings
            cbKeepExistingTextures.Checked = keepExistingSideTextures;
            ceiloffsetmode.SelectedIndex   = storedceiloffsetmode;
            flooroffsetmode.SelectedIndex  = storedflooroffsetmode;

            //vertex heights can not be set in non-UDMF maps
            if (General.Map.UDMF)
            {
                cbUseFloorVertexHeights.Checked   = useFloorVertexHeights;
                cbUseCeilingVertexHeights.Checked = useCeilingVertexHeights;
            }
            else
            {
                useFloorVertexHeights           = false;
                cbUseFloorVertexHeights.Checked = false;
                cbUseFloorVertexHeights.Enabled = false;

                useCeilingVertexHeights           = false;
                cbUseCeilingVertexHeights.Checked = false;
                cbUseCeilingVertexHeights.Enabled = false;
            }

            //texture pickers
            textureLower.Initialize();
            textureUpper.Initialize();

            //We can't use floor/ceiling textures when MixTexturesFlats is disabled
            if (General.Map.Config.MixTexturesFlats)
            {
                textureLower.TextureName = General.Settings.DefaultFloorTexture;
                textureUpper.TextureName = General.Settings.DefaultCeilingTexture;
            }
            else
            {
                textureLower.TextureName = General.Settings.DefaultTexture;
                textureUpper.TextureName = General.Settings.DefaultTexture;
                cbUpperTexStyle.Items[1] = "Use default texture";
                cbLowerTexStyle.Items[1] = "Use default texture";
            }

            cbUpperTexStyle.SelectedIndex = 0;
            cbLowerTexStyle.SelectedIndex = 0;
            UpdateTextureSelectors();             //update interface

            //create random values
            UpdateAngles();
            UpdateFloorHeights();
            UpdateCeilingHeights();
        }