// This reads the SECTORS from WAD file
        // Returns a lookup table with indices
        private Dictionary <int, Sector> ReadSectors(MapSet map, int firstindex)
        {
            MemoryStream             mem;
            Dictionary <int, Sector> link;
            BinaryReader             reader;
            int    num, i, hfloor, hceil, bright, special, tag;
            string tfloor, tceil;
            Sector s;

            // Get the lump from wad file
            Lump lump = wad.FindLump("SECTORS", firstindex);

            if (lump == null)
            {
                throw new Exception("Could not find required lump SECTORS!");
            }

            // Prepare to read the items
            mem    = new MemoryStream(lump.Stream.ReadAllBytes());
            num    = (int)lump.Stream.Length / 26;
            reader = new BinaryReader(mem);

            // Create lookup table
            link = new Dictionary <int, Sector>(num);

            // Read items from the lump
            map.SetCapacity(0, 0, 0, map.Sectors.Count + num, 0);
            for (i = 0; i < num; i++)
            {
                // Read properties from stream
                hfloor  = reader.ReadInt16();
                hceil   = reader.ReadInt16();
                tfloor  = Lump.MakeNormalName(reader.ReadBytes(8), WAD.ENCODING);
                tceil   = Lump.MakeNormalName(reader.ReadBytes(8), WAD.ENCODING);
                bright  = reader.ReadInt16();
                special = reader.ReadUInt16();
                tag     = reader.ReadUInt16();

                // Create new item
                s = map.CreateSector();
                s.Update(hfloor, hceil, tfloor, tceil, special, tag, bright);

                // Add it to the lookup table
                link.Add(i, s);
            }

            // Done
            mem.Dispose();

            // Return lookup table
            return(link);
        }
        // This reads the LINEDEFS and SIDEDEFS from WAD file
        private void ReadLinedefs(MapSet map, int firstindex,
                                  Dictionary <int, Vertex> vertexlink, Dictionary <int, Sector> sectorlink)
        {
            int[] args = new int[Linedef.NUM_ARGS];

            // Get the linedefs lump from wad file
            Lump linedefslump = wad.FindLump("LINEDEFS", firstindex);

            if (linedefslump == null)
            {
                throw new Exception("Could not find required lump LINEDEFS!");
            }

            // Get the sidedefs lump from wad file
            Lump sidedefslump = wad.FindLump("SIDEDEFS", firstindex);

            if (sidedefslump == null)
            {
                throw new Exception("Could not find required lump SIDEDEFS!");
            }

            // Prepare to read the items
            MemoryStream linedefsmem = new MemoryStream(linedefslump.Stream.ReadAllBytes());
            MemoryStream sidedefsmem = new MemoryStream(sidedefslump.Stream.ReadAllBytes());
            int          num         = (int)linedefslump.Stream.Length / 16;
            int          numsides    = (int)sidedefslump.Stream.Length / 30;
            BinaryReader readline    = new BinaryReader(linedefsmem);
            BinaryReader readside    = new BinaryReader(sidedefsmem);

            // Read items from the lump
            map.SetCapacity(0, map.Linedefs.Count + num, map.Sidedefs.Count + numsides, 0, 0);
            for (int i = 0; i < num; i++)
            {
                // Read properties from stream
                int v1     = readline.ReadUInt16();
                int v2     = readline.ReadUInt16();
                int flags  = readline.ReadUInt16();
                int action = readline.ReadByte();
                args[0] = readline.ReadByte();
                args[1] = readline.ReadByte();
                args[2] = readline.ReadByte();
                args[3] = readline.ReadByte();
                args[4] = readline.ReadByte();
                int s1 = readline.ReadUInt16();
                int s2 = readline.ReadUInt16();

                // Make string flags
                Dictionary <string, bool> stringflags = new Dictionary <string, bool>(StringComparer.Ordinal);
                foreach (string f in manager.Config.SortedLinedefFlags)
                {
                    int fnum;
                    if (int.TryParse(f, out fnum))
                    {
                        stringflags[f] = ((flags & fnum) == fnum);
                    }
                }

                // Create new linedef
                if (vertexlink.ContainsKey(v1) && vertexlink.ContainsKey(v2))
                {
                    // Check if not zero-length
                    if (Vector2D.ManhattanDistance(vertexlink[v1].Position, vertexlink[v2].Position) > 0.0001f)
                    {
                        Linedef l = map.CreateLinedef(vertexlink[v1], vertexlink[v2]);
                        l.Update(stringflags, (flags & manager.Config.LinedefActivationsFilter), new List <int> {
                            0
                        }, action, args);
                        l.UpdateCache();

                        Sidedef s;
                        string  thigh, tmid, tlow;
                        int     offsetx, offsety, sc;

                        // Line has a front side?
                        if (s1 != ushort.MaxValue)
                        {
                            // Read front sidedef
                            sidedefsmem.Seek(s1 * 30, SeekOrigin.Begin);
                            if ((s1 * 30L) <= (sidedefsmem.Length - 30L))
                            {
                                offsetx = readside.ReadInt16();
                                offsety = readside.ReadInt16();
                                thigh   = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tlow    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tmid    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                sc      = readside.ReadUInt16();

                                // Create front sidedef
                                if (sectorlink.ContainsKey(sc))
                                {
                                    s = map.CreateSidedef(l, true, sectorlink[sc]);
                                    s.Update(offsetx, offsety, thigh, tmid, tlow);
                                }
                                else
                                {
                                    General.ErrorLogger.Add(ErrorType.Warning, "Sidedef " + s1 + " references invalid sector " + sc + ". Sidedef has been removed.");
                                }
                            }
                            else
                            {
                                General.ErrorLogger.Add(ErrorType.Warning, "Linedef " + i + " references invalid sidedef " + s1 + ". Sidedef has been removed.");
                            }
                        }

                        // Line has a back side?
                        if (s2 != ushort.MaxValue)
                        {
                            // Read back sidedef
                            sidedefsmem.Seek(s2 * 30, SeekOrigin.Begin);
                            if ((s2 * 30L) <= (sidedefsmem.Length - 30L))
                            {
                                offsetx = readside.ReadInt16();
                                offsety = readside.ReadInt16();
                                thigh   = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tlow    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tmid    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                sc      = readside.ReadUInt16();

                                // Create back sidedef
                                if (sectorlink.ContainsKey(sc))
                                {
                                    s = map.CreateSidedef(l, false, sectorlink[sc]);
                                    s.Update(offsetx, offsety, thigh, tmid, tlow);
                                }
                                else
                                {
                                    General.ErrorLogger.Add(ErrorType.Warning, "Sidedef " + s2 + " references invalid sector " + sc + ". Sidedef has been removed.");
                                }
                            }
                            else
                            {
                                General.ErrorLogger.Add(ErrorType.Warning, "Linedef " + i + " references invalid sidedef " + s2 + ". Sidedef has been removed.");
                            }
                        }
                    }
                    else
                    {
                        General.ErrorLogger.Add(ErrorType.Warning, "Linedef " + i + " is zero-length. Linedef has been removed.");
                    }
                }
                else
                {
                    General.ErrorLogger.Add(ErrorType.Warning, "Linedef " + i + " references one or more invalid vertices. Linedef has been removed.");
                }
            }

            // Done
            linedefsmem.Dispose();
            sidedefsmem.Dispose();
        }
Exemple #3
0
        // This reads the LINEDEFS and SIDEDEFS from WAD file
        private void ReadLinedefs(MapSet map, int firstindex,
                                  Dictionary <int, Vertex> vertexlink, Dictionary <int, Sector> sectorlink)
        {
            MemoryStream linedefsmem, sidedefsmem;
            BinaryReader readline, readside;
            Lump         linedefslump, sidedefslump;
            int          num, numsides, i, offsetx, offsety, v1, v2;
            int          s1, s2, flags, action, tag, sc;
            Dictionary <string, bool> stringflags;
            string  thigh, tmid, tlow;
            Linedef l;
            Sidedef s;

            // Get the linedefs lump from wad file
            linedefslump = wad.FindLump("LINEDEFS", firstindex);
            if (linedefslump == null)
            {
                throw new Exception("Could not find required lump LINEDEFS!");
            }

            // Get the sidedefs lump from wad file
            sidedefslump = wad.FindLump("SIDEDEFS", firstindex);
            if (sidedefslump == null)
            {
                throw new Exception("Could not find required lump SIDEDEFS!");
            }

            // Prepare to read the items
            linedefsmem = new MemoryStream(linedefslump.Stream.ReadAllBytes());
            sidedefsmem = new MemoryStream(sidedefslump.Stream.ReadAllBytes());
            num         = (int)linedefslump.Stream.Length / 14;
            numsides    = (int)sidedefslump.Stream.Length / 30;
            readline    = new BinaryReader(linedefsmem);
            readside    = new BinaryReader(sidedefsmem);

            // Read items from the lump

            // ano - heuristic to detect sidedef compression
            if (num > numsides)
            {
                // ano - set sidedefs to some larger amount than
                // numsides before resizing back down
                // because in the case of sidedef compression the
                // array will have to be resized a lot
                map.SetCapacity(0, map.Linedefs.Count + num, map.Sidedefs.Count + (num * 2), 0, 0);
            }
            else
            {
                // ano - + 32 as extra leniency for some amount of sidedef
                // compression that goes undetected by the prev check
                // note this wont be resized back down but 32 is a neglible amount
                map.SetCapacity(0, map.Linedefs.Count + num, map.Sidedefs.Count + num + 32, 0, 0);
            }
            for (i = 0; i < num; i++)
            {
                // Read properties from stream
                v1     = readline.ReadUInt16();
                v2     = readline.ReadUInt16();
                flags  = readline.ReadUInt16();
                action = readline.ReadUInt16();
                tag    = readline.ReadUInt16();
                s1     = readline.ReadUInt16();
                s2     = readline.ReadUInt16();

                // Make string flags
                stringflags = new Dictionary <string, bool>();
                foreach (string f in manager.Config.SortedLinedefFlags)
                {
                    int fnum;
                    if (int.TryParse(f, out fnum))
                    {
                        stringflags[f] = ((flags & fnum) == fnum);
                    }
                }

                // Create new linedef
                if (vertexlink.ContainsKey(v1) && vertexlink.ContainsKey(v2))
                {
                    // Check if not zero-length
                    if (Vector2D.ManhattanDistance(vertexlink[v1].Position, vertexlink[v2].Position) > 0.0001f)
                    {
                        l = map.CreateLinedef(vertexlink[v1], vertexlink[v2]);
                        l.Update(stringflags, 0, tag, action, new int[Linedef.NUM_ARGS]);
                        l.UpdateCache();

                        // Line has a front side?
                        if (s1 != ushort.MaxValue)
                        {
                            // Read front sidedef
                            if ((s1 * 30L) <= (sidedefsmem.Length - 30L))
                            {
                                sidedefsmem.Seek(s1 * 30, SeekOrigin.Begin);
                                offsetx = readside.ReadInt16();
                                offsety = readside.ReadInt16();
                                thigh   = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tlow    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tmid    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                sc      = readside.ReadUInt16();

                                // Create front sidedef
                                if (sectorlink.ContainsKey(sc))
                                {
                                    s = map.CreateSidedef(l, true, sectorlink[sc]);
                                    s.Update(offsetx, offsety, thigh, tmid, tlow);
                                }
                                else
                                {
                                    General.ErrorLogger.Add(ErrorType.Warning, "Sidedef " + s1 + " references invalid sector " + sc + ". Sidedef has been removed.");
                                }
                            }
                            else
                            {
                                General.ErrorLogger.Add(ErrorType.Warning, "Linedef references invalid sidedef " + s1 + ". Sidedef has been removed.");
                            }
                        }

                        // Line has a back side?
                        if (s2 != ushort.MaxValue)
                        {
                            // Read back sidedef
                            if ((s2 * 30L) <= (sidedefsmem.Length - 30L))
                            {
                                sidedefsmem.Seek(s2 * 30, SeekOrigin.Begin);
                                offsetx = readside.ReadInt16();
                                offsety = readside.ReadInt16();
                                thigh   = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tlow    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                tmid    = Lump.MakeNormalName(readside.ReadBytes(8), WAD.ENCODING);
                                sc      = readside.ReadUInt16();

                                // Create back sidedef
                                if (sectorlink.ContainsKey(sc))
                                {
                                    s = map.CreateSidedef(l, false, sectorlink[sc]);
                                    s.Update(offsetx, offsety, thigh, tmid, tlow);
                                }
                                else
                                {
                                    General.ErrorLogger.Add(ErrorType.Warning, "Sidedef " + s2 + " references invalid sector " + sc + ". Sidedef has been removed.");
                                }
                            }
                            else
                            {
                                General.ErrorLogger.Add(ErrorType.Warning, "Linedef " + i + " references invalid sidedef " + s2 + ". Sidedef has been removed.");
                            }
                        }
                    }
                    else
                    {
                        General.ErrorLogger.Add(ErrorType.Warning, "Linedef " + i + " is zero-length. Linedef has been removed.");
                    }
                }
                else
                {
                    General.ErrorLogger.Add(ErrorType.Warning, "Linedef " + i + " references one or more invalid vertices. Linedef has been removed.");
                }
            }
            if (num > numsides)
            {
                // ano - resize the arrays back down
                map.SetCapacity(0, map.Linedefs.Count, map.Sidedefs.Count, 0, 0);
            }

            // Done
            linedefsmem.Dispose();
            sidedefsmem.Dispose();
        }