示例#1
0
        // Initializes for an existing map
        internal bool InitializeOpenMap(string filepathname, MapOptions options)
        {
            // Apply settings
            this.filename     = Path.GetFileName(filepathname);
            this.filepathname = filepathname;
            this.filepath     = Path.GetDirectoryName(filepathname);
            this.changed      = false;
            this.options      = options;

            General.WriteLogLine("Opening map \"" + this.filename + "\" with configuration \"" + options.ConfigFile + "\"");

            // Initiate graphics
            General.WriteLogLine("Initializing graphics device...");
            graphics = new D3DDevice(General.MainWindow.Display);
            if (!graphics.Initialize())
            {
                return(false);
            }

            // Create renderers
            renderer2d = new Renderer2D(graphics);
            renderer3d = new Renderer3D(graphics);

            // Load game configuration
            General.WriteLogLine("Loading game configuration...");
            configinfo = General.GetConfigurationInfo(options.ConfigFile);
            config     = new GameConfiguration(General.LoadGameConfiguration(options.ConfigFile));
            configinfo.ApplyDefaults(config);
            General.Editing.UpdateCurrentEditModes();

            // Create map data
            bool maprestored = false;             //mxd

            map = new MapSet();

            string mapname = Path.GetFileNameWithoutExtension(filepathname);

            if (!string.IsNullOrEmpty(mapname))
            {
                string hash       = MurmurHash2.Hash(mapname + File.GetLastWriteTime(filepathname)).ToString();
                string backuppath = Path.Combine(General.MapRestorePath, mapname + "." + hash + ".restore");

                // Backup exists and it's newer than the map itself?
                if (File.Exists(backuppath) && File.GetLastWriteTime(backuppath) > File.GetLastWriteTime(filepathname))
                {
                    if (General.ShowWarningMessage("Looks like your previous editing session has gone terribly wrong." + Environment.NewLine
                                                   + "Would you like to restore the map from the backup?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        General.WriteLogLine("Initializing map format interface " + config.FormatInterface + "...");
                        io = MapSetIO.Create(config.FormatInterface, this);
                        General.WriteLogLine("Restoring map from \"" + backuppath + "\"...");

#if DEBUG
                        // Restore map
                        map.Deserialize(new MemoryStream(File.ReadAllBytes(backuppath)));
#else
                        try
                        {
                            // Restore map
                            map.Deserialize(new MemoryStream(File.ReadAllBytes(backuppath)));

                            // Delete the backup
                            File.Delete(backuppath);
                        }
                        catch (Exception e)
                        {
                            General.ErrorLogger.Add(ErrorType.Error, "Unable to restore the map data structures from the backup. " + e.GetType().Name + ": " + e.Message);
                            General.ShowErrorMessage("Unable to restore the map data structures from the backup.", MessageBoxButtons.OK);
                            return(false);
                        }
#endif
                        maprestored = true;
                    }
                }
            }

            // Read the map from file
            if (!maprestored)
            {
                map.BeginAddRemove();

                General.WriteLogLine("Initializing map format interface " + config.FormatInterface + "...");
                io = MapSetIO.Create(config.FormatInterface, this);
                General.WriteLogLine("Reading map data structures from file...");

#if !DEBUG
                try
                {
#endif
                using (FileStream stream = File.OpenRead(filepathname))
                {
                    io.Read(map, stream);
                }
#if !DEBUG
            }
            catch (Exception e)
            {
                General.ErrorLogger.Add(ErrorType.Error, "Unable to read the map data structures with the specified configuration. " + e.GetType().Name + ": " + e.Message);
                General.ShowErrorMessage("Unable to read the map data structures with the specified configuration.", MessageBoxButtons.OK);
                return(false);
            }
#endif

                map.EndAddRemove();
            }


            // Load data manager
            General.WriteLogLine("Loading data resources...");
            data = new DataManager();
            data.Load(CreateResourcesList());

            // Remove unused sectors
            map.RemoveUnusedSectors(true);

            // Update structures
            options.ApplyGridSettings();
            map.UpdateConfiguration();
            //map.SnapAllToAccuracy();
            map.Update();
            thingsfilter.Update();

            // Bind any methods
            General.Actions.BindMethods(this);

            // Set defaults
            this.visualcamera = new VisualCamera();
            General.Editing.ChangeMode(configinfo.StartMode);
            renderer2d.SetViewMode((ViewMode)General.Settings.DefaultViewMode);

            // Center map in screen
            if (General.Editing.Mode is ClassicMode)
            {
                (General.Editing.Mode as ClassicMode).CenterInScreen();
            }

            // Success
            this.changed = maprestored;             //mxd
            General.WriteLogLine("Map loading done");
            General.MainWindow.UpdateTitle();       //mxd
            return(true);
        }
        private bool CreateGeometry(List <Vector3D> verts, List <Face> faces, int maxZ)
        {
            if (verts.Count < 3 || faces.Count == 0)
            {
                MessageBox.Show("Cannot import the model: failed to find any suitable polygons!",
                                "Terrain Importer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            MapSet map = General.Map.Map;

            // Capacity checks
            int totalverts   = map.Vertices.Count + verts.Count;
            int totalsides   = map.Sidedefs.Count + faces.Count * 6;
            int totallines   = map.Linedefs.Count + faces.Count * 3;
            int totalsectors = map.Sectors.Count + faces.Count;
            int totalthings  = 0;

            if (form.UseVertexHeights && !General.Map.UDMF)
            {
                // We'll use vertex height things in non-udmf maps, if such things are defined in Game Configuration
                totalthings = map.Things.Count + verts.Count;
            }

            if (totalverts > General.Map.FormatInterface.MaxVertices)
            {
                MessageBox.Show("Cannot import the model: resulting vertex count (" + totalverts
                                + ") is larger than map format's maximum (" + General.Map.FormatInterface.MaxVertices + ")!",
                                "Terrain Importer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (totalsides > General.Map.FormatInterface.MaxSidedefs)
            {
                MessageBox.Show("Cannot import the model: resulting sidedefs count (" + totalsides
                                + ") is larger than map format's maximum (" + General.Map.FormatInterface.MaxSidedefs + ")!",
                                "Terrain Importer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (totallines > General.Map.FormatInterface.MaxLinedefs)
            {
                MessageBox.Show("Cannot import the model: resulting sidedefs count (" + totallines
                                + ") is larger than map format's maximum (" + General.Map.FormatInterface.MaxLinedefs + ")!",
                                "Terrain Importer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (totalsectors > General.Map.FormatInterface.MaxSectors)
            {
                MessageBox.Show("Cannot import the model: resulting sidedefs count (" + totalsectors
                                + ") is larger than map format's maximum (" + General.Map.FormatInterface.MaxSectors + ")!",
                                "Terrain Importer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (totalthings > General.Map.FormatInterface.MaxThings)
            {
                MessageBox.Show("Cannot import the model: resulting things count (" + totalthings
                                + ") is larger than map format's maximum (" + General.Map.FormatInterface.MaxThings + ")!",
                                "Terrain Importer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            //make undo
            General.Map.UndoRedo.CreateUndo("Import Terrain");

            //prepare mapset
            List <Linedef> newlines = new List <Linedef>();

            map.BeginAddRemove();
            map.SetCapacity(totalverts, totallines, totalsides, totalsectors, totalthings);

            //terrain has many faces... let's create them
            Dictionary <Vector3D, Vertex> newverts = new Dictionary <Vector3D, Vertex>();

            foreach (Face face in faces)
            {
                // Create sector
                Sector s = map.CreateSector();
                s.Selected    = true;
                s.FloorHeight = (int)Math.Round((face.V1.z + face.V2.z + face.V3.z) / 3);
                s.CeilHeight  = maxZ;
                s.Brightness  = General.Settings.DefaultBrightness;                //todo: allow user to change this
                s.SetCeilTexture(General.Map.Config.SkyFlatName);
                s.SetFloorTexture(General.Map.Options.DefaultFloorTexture);        //todo: allow user to change this

                // And linedefs
                Linedef newline = GetLine(newverts, s, face.V1, face.V2);
                if (newline != null)
                {
                    newlines.Add(newline);
                }

                newline = GetLine(newverts, s, face.V2, face.V3);
                if (newline != null)
                {
                    newlines.Add(newline);
                }

                newline = GetLine(newverts, s, face.V3, face.V1);
                if (newline != null)
                {
                    newlines.Add(newline);
                }

                s.UpdateCache();
            }

            // Add slope things
            if (form.UseVertexHeights && !General.Map.UDMF)
            {
                foreach (Vector3D pos in newverts.Keys)
                {
                    Thing t = map.CreateThing();
                    General.Settings.ApplyDefaultThingSettings(t);
                    t.Type = VERTEX_HEIGHT_THING_TYPE;
                    t.Move(pos);
                    t.Selected = true;
                }
            }

            //update new lines
            foreach (Linedef l in newlines)
            {
                l.ApplySidedFlags();
            }

            map.EndAddRemove();
            return(true);
        }