示例#1
0
文件: Map.cs 项目: IllidanS4/AlbLib
        /// <summary>
        /// Loads map from stream and assigns an id.
        /// </summary>
        /// <param name="id">
        /// Id of map.
        /// </param>
        /// <param name="stream">
        /// Source stream.
        /// </param>
        public Map(int id, Stream stream)
        {
            Id = id;

            BinaryReader reader = new BinaryReader(stream, TextCore.DefaultEncoding);

            //Basic info
            Flags = reader.ReadByte();
            NumNPC = reader.ReadByte();
            Type = (MapType)reader.ReadByte();
            if(Type != MapType.Map2D && Type != MapType.Map3D)throw new InvalidDataException("Unknown map format.");
            BackgroundMusic = reader.ReadByte();
            byte width = reader.ReadByte();
            byte height = reader.ReadByte();
            tilesid = reader.ReadByte();
            ComGFX = reader.ReadByte();
            Palette = reader.ReadByte();
            AnimRate = reader.ReadByte();

            //NPCs
            int npcstruct;
            if(NumNPC == 0)
            {
                npcstruct = 32;
            }else if(NumNPC == 64)
            {
                npcstruct = 96;
            }else{
                npcstruct = NumNPC;
            }
            NPCs = new NPC[npcstruct];
            for(int i = 0; i < npcstruct; i++)
            {
                NPCs[i] = new NPC(stream);
            }

            if(Type == MapType.Map2D)
            {
                //Tile data
                tiledata = new Tile[width,height];
                for(byte y = 0; y < height; y++)
                for(byte x = 0; x < width; x++)
                {
                    tiledata[x,y] = new Tile(x,y,stream);
                }
            }else if(Type == MapType.Map3D)
            {
                //Block data
                blockdata = new Block[width,height];
                for(byte y = 0; y < height; y++)
                for(byte x = 0; x < width; x++)
                {
                    blockdata[x,y] = new Block(x,y,stream);
                }
            }

            try{
                short autoevents = reader.ReadInt16();
                AutoEvents = new EventHeader[autoevents];
                for(int i = 0; i < autoevents; i++)
                {
                    AutoEvents[i] = new EventHeader(reader);
                }

                TileEvents = new EventHeader[height][];
                for(int y = 0; y < height; y++)
                {
                    short events = reader.ReadInt16();
                    TileEvents[y] = new EventHeader[events];
                    for(int i = 0; i < events; i++)
                    {
                        EventHeader e = TileEvents[y][i] = new EventHeader(reader);
                        if(Type == MapType.Map2D)
                            TileData[e.XPos,y].Event = e;
                        else if(Type == MapType.Map3D)
                            BlockData[e.XPos,y].Event = e;
                    }
                }

                short numevents = reader.ReadInt16();
                Events = new Event[numevents];
                for(int i = 0; i < numevents; i++)
                {
                    Events[i] = new Event(reader);
                }

                //NPC positions
                foreach(NPC npc in UsedNPCs)
                {
                    if((npc.Movement & 3) != 0)
                    {
                        npc.Positions = new Position[1];
                        npc.Positions[0] = new Position((byte)(reader.ReadByte()-1), (byte)(reader.ReadByte()-1));
                    }else{
                        npc.Positions = new Position[1152];
                        for(int i = 0; i < 1152; i++)
                        {
                            npc.Positions[i] = new Position((byte)(reader.ReadByte()-1), (byte)(reader.ReadByte()-1));
                        }
                    }
                }

                //Active events
                int aesize;
                if(Type == MapType.Map2D)
                {
                    aesize = 250;
                }else{
                //Goto-points
                    short numgotop = reader.ReadInt16();
                    gotopoints = new GotoPoint[numgotop];
                    for(int i = 0; i < numgotop; i++)
                    {
                        GotoPoint gp = gotopoints[i] = new GotoPoint(reader);
                        if(gp.X < width && gp.Y < height)
                            BlockData[gp.X,gp.Y].GotoPoint = gp;
                    }
                //Automap gfx remapping structure
                    automap = reader.ReadBytes(64);
                //Active events
                    aesize = 64;
                }
                ActiveEvents = new short[aesize];
                for(int i = 0; i < aesize; i++)
                {
                    ActiveEvents[i] = reader.ReadInt16();
                }
            }catch(EndOfStreamException)
            {
                corrupted = true;
            }
        }
示例#2
0
        /// <summary>
        /// Loads map from stream and assigns an id.
        /// </summary>
        /// <param name="id">
        /// Id of map.
        /// </param>
        /// <param name="stream">
        /// Source stream.
        /// </param>
        public Map(int id, Stream stream)
        {
            Id = id;

            BinaryReader reader = new BinaryReader(stream, TextCore.DefaultEncoding);

            //Basic info
            Flags  = reader.ReadByte();
            NumNPC = reader.ReadByte();
            Type   = (MapType)reader.ReadByte();
            if (Type != MapType.Map2D && Type != MapType.Map3D)
            {
                throw new InvalidDataException("Unknown map format.");
            }
            BackgroundMusic = reader.ReadByte();
            byte width  = reader.ReadByte();
            byte height = reader.ReadByte();

            tilesid  = reader.ReadByte();
            ComGFX   = reader.ReadByte();
            Palette  = reader.ReadByte();
            AnimRate = reader.ReadByte();

            //NPCs
            int npcstruct;

            if (NumNPC == 0)
            {
                npcstruct = 32;
            }
            else if (NumNPC == 64)
            {
                npcstruct = 96;
            }
            else
            {
                npcstruct = NumNPC;
            }
            NPCs = new NPC[npcstruct];
            for (int i = 0; i < npcstruct; i++)
            {
                NPCs[i] = new NPC(stream);
            }

            if (Type == MapType.Map2D)
            {
                //Tile data
                tiledata = new Tile[width, height];
                for (byte y = 0; y < height; y++)
                {
                    for (byte x = 0; x < width; x++)
                    {
                        tiledata[x, y] = new Tile(x, y, stream);
                    }
                }
            }
            else if (Type == MapType.Map3D)
            {
                //Block data
                blockdata = new Block[width, height];
                for (byte y = 0; y < height; y++)
                {
                    for (byte x = 0; x < width; x++)
                    {
                        blockdata[x, y] = new Block(x, y, stream);
                    }
                }
            }

            try{
                short autoevents = reader.ReadInt16();
                AutoEvents = new EventHeader[autoevents];
                for (int i = 0; i < autoevents; i++)
                {
                    AutoEvents[i] = new EventHeader(reader);
                }

                //EventHeader[][] tileEvents = new EventHeader[height][];
                for (int y = 0; y < height; y++)
                {
                    short events = reader.ReadInt16();
                    //tileEvents[y] = new EventHeader[events];
                    for (int i = 0; i < events; i++)
                    {
                        EventHeader e = new EventHeader(reader);
                        if (Type == MapType.Map2D)
                        {
                            TileData[e.XPos, y].Event = e;
                        }
                        else if (Type == MapType.Map3D)
                        {
                            BlockData[e.XPos, y].Event = e;
                        }
                    }
                }

                short numevents = reader.ReadInt16();
                Events = new MapEvent[numevents];
                for (int i = 0; i < numevents; i++)
                {
                    Events[i] = new MapEvent(i, reader);
                }

                //NPC positions
                foreach (NPC npc in UsedNPCs)
                {
                    if ((npc.Movement & 3) != 0)
                    {
                        npc.Positions    = new Position[1];
                        npc.Positions[0] = new Position((byte)(reader.ReadByte() - 1), (byte)(reader.ReadByte() - 1));
                    }
                    else
                    {
                        npc.Positions = new Position[1152];
                        for (int i = 0; i < 1152; i++)
                        {
                            npc.Positions[i] = new Position((byte)(reader.ReadByte() - 1), (byte)(reader.ReadByte() - 1));
                        }
                    }
                }

                //Active events
                int aesize;
                if (Type == MapType.Map2D)
                {
                    aesize = 250;
                }
                else
                {
                    //Goto-points
                    short numgotop = reader.ReadInt16();
                    gotopoints = new GotoPoint[numgotop];
                    for (int i = 0; i < numgotop; i++)
                    {
                        GotoPoint gp = gotopoints[i] = new GotoPoint(reader);
                        if (gp.X < width && gp.Y < height)
                        {
                            BlockData[gp.X, gp.Y].GotoPoint = gp;
                        }
                    }
                    //Automap gfx remapping structure
                    automap = reader.ReadBytes(64);
                    //Active events
                    aesize = 64;
                }
                ActiveEvents = new short[aesize];
                for (int i = 0; i < aesize; i++)
                {
                    ActiveEvents[i] = reader.ReadInt16();
                }
            }catch (EndOfStreamException e)
            {
                Error = e;
            }
        }