Beispiel #1
0
        private void Read()
        {
            foreach (var subChunk in SubData.Chunks)
            {
                switch (subChunk.Name)
                {
                case "MCVT":
                    MCVT = new MCVT(subChunk);

                    GenerateVertices();
                    GenerateIndices();
                    break;

                case "MCRD":
                    MCRD = new MCRD(subChunk);

                    GenerateDoodads();
                    break;

                case "MCRW":
                    MCRW = new MCRW(subChunk);

                    GenerateWMOs();
                    break;
                }
            }
        }
Beispiel #2
0
        public MCVT ReadMCVTSubChunk(BlizzHeader chunk, BinaryReader bin)
        {
            MCVT vtchunk = new MCVT();

            vtchunk.vertices = new float[145];
            for (int i = 0; i < 145; i++)
            {
                vtchunk.vertices[i] = bin.ReadSingle();
            }
            return(vtchunk);
        }
Beispiel #3
0
        private MCVT ReadMCVTSubChunk(BinaryReader bin)
        {
            MCVT vtchunk = new MCVT()
            {
                vertices = new float[145]
            };

            for (int i = 0; i < 145; i++)
            {
                vtchunk.vertices[i] = bin.ReadSingle();
            }
            return(vtchunk);
        }
Beispiel #4
0
        private void Read(ChunkData subData)
        {
            foreach (var subChunk in subData.Chunks)
            {
                switch (subChunk.Name)
                {
                case "MCNK":
                    break;     // Ignore

                case "MCVT":
                    MCVT = new MCVT(subChunk);
                    break;

                case "MCRD":
                    MCRD = new MCRD(subChunk);
                    break;

                case "MCRW":
                    MCRW = new MCRW(subChunk);
                    break;

                case "MCNR":
                    MCNR = new MCNR(subChunk);
                    break;

                case "MCCV":
                    MCCV = new MCCV(subChunk);
                    break;
                    //case "MCSH":
                    //    MCSH = new MCSH(subChunk);
                    //    break;
                    //case "MCLY":
                    //    MCLY = new MCLY(subChunk);
                    //    break;
                    //case "MCAL":
                    //    if (WDT == null)
                    //        Console.WriteLine($"Skipping MCAL Chunk in MCNK #{Index} because no WDT was provided!");
                    //    else
                    //        MCAL = new MCAL(this, WDT, subChunk);
                    //    break;
                    //default:
                    //    Console.WriteLine($"Skipped {subChunk.Name} MapChunk sub-chunk.");
                    //    break;
                }
            }
        }
Beispiel #5
0
        public MCNK(char[] magic, byte[] size, byte[] content) : base(magic, size)
        {
            using (BinaryReader reader = new BinaryReader(new MemoryStream(content)))
            {
                #region read MCNKhead
                mcHeader.Flags       = reader.ReadUInt32();
                mcHeader.IndexX      = reader.ReadUInt32();
                mcHeader.IndexY      = reader.ReadUInt32();
                mcHeader.NLayers     = reader.ReadUInt32();
                mcHeader.NDoodadRefs = reader.ReadUInt32();

                offset offset = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCVT = offset;

                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCNR = offset;
                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCLY = offset;
                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCRF = offset;
                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCAL = offset;

                mcHeader.SizeAlpha = reader.ReadUInt32();

                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCSH = offset;

                mcHeader.SizeShadow  = reader.ReadUInt32();
                mcHeader.Areaid      = reader.ReadUInt32();
                mcHeader.NMapObjRefs = reader.ReadUInt32();
                mcHeader.Holes       = reader.ReadUInt32();

                mcHeader.GroundEffectsMap = new byte[16];
                for (int x = 0; x < 16; x++)
                {
                    mcHeader.GroundEffectsMap[x] = reader.ReadByte();
                }

                mcHeader.PredTex        = reader.ReadUInt32();
                mcHeader.NoEffectDoodad = reader.ReadUInt32();

                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCSE = offset;

                mcHeader.NSndEmitters = reader.ReadUInt32();

                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCLQ = offset;

                mcHeader.SizeLiquid = reader.ReadUInt32();

                mcHeader.Pos    = new float[3];
                mcHeader.Pos[0] = reader.ReadSingle();
                mcHeader.Pos[1] = reader.ReadSingle();
                mcHeader.Pos[2] = reader.ReadSingle();

                offset           = new offset();
                offset.address   = reader.ReadUInt32();
                mcHeader.OfsMCCV = offset;

                mcHeader.Props    = reader.ReadUInt32();
                mcHeader.EffectId = reader.ReadUInt32();
                #endregion

                while (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    byte[] ChunkMagic   = reader.ReadBytes(4);
                    byte[] ChunkSize    = reader.ReadBytes(4);
                    byte[] ChunkContent = reader.ReadBytes(BitConverter.ToInt32(ChunkSize, 0));

                    string ChunkMagicString = ADT.MagicBytesToString(ChunkMagic);

                    switch (ChunkMagicString)
                    {
                    case "MCVT":
                        mcvt = new MCVT(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;

                    case "MCCV":
                        mccv = new MCCV(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;

                    case "MCNR":
                        mcnr = new MCNR(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent, reader.ReadBytes(13));
                        break;

                    case "MCLY":
                        mcly = new MCLY(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;

                    case "MCRF":
                        mcrf = new MCRF(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;

                    case "MCAL":
                        mcal = new MCAL(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;

                    case "MCSE":
                        mcse = new MCSE(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;

                    case "MCSH":
                        mcsh = new MCSH(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;

                    case "MCLQ":
                        mclq = new MCLQ(ADT.MagicBytesToChars(ChunkMagic), ChunkSize, ChunkContent);
                        break;
                    }

                    //Logger.log(ChunkMagicString, Logger.Direction.LEVEL2, ChunkContent.Length.ToString() + " byte");
                }
            }

            //Logger.log("---", Logger.Direction.LEVEL2);
        }
Beispiel #6
0
		static MCVT ProcessHeights(BinaryReader fileReader, uint mcvtOffset)
		{
			fileReader.BaseStream.Position = mcvtOffset;
			var heights = new MCVT();
			var sig = fileReader.ReadUInt32();
			var size = fileReader.ReadUInt32();
			for (var i = 0; i < 145; i++)
			{
				heights.Heights[i] = fileReader.ReadSingle();
			}
			return heights;
		}
Beispiel #7
0
        public override byte[] GetChunkBytes()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    writer.Write((uint)Flags);
                    writer.WriteVector2UInt(Index);
                    writer.Write(Layers);
                    writer.Write(NumberDoodadRefs);
                    var positionOffsetMCVT = BaseStream.Position;
                    writer.Write(0);
                    var positionOffsetMCNR = BaseStream.Position;
                    writer.Write(0);
                    var positionOffsetMCLY = BaseStream.Position;
                    writer.Write(0);
                    var positionOffsetMCRF = BaseStream.Position;
                    writer.Write(0);
                    var positionOffsetMCAL = BaseStream.Position;
                    writer.Write(0);
                    writer.Write(SizeShadow);
                    var positionOffsetMCSH = BaseStream.Position;
                    writer.Write(0);
                    writer.Write(SizeShadow);
                    writer.Write(AreaId);
                    writer.Write(NumberMapObjectRefs);
                    writer.Write(Holes);
                    writer.Write(HolesPadding);
                    for (int i = 0; i < 16; i++)
                    {
                        writer.Write(ReallyLowQualityTextureingMap[i]);
                    }
                    writer.Write(PredTex);
                    writer.Write(NumberEffectDoodads);
                    var positionOffsetMCSE = BaseStream.Position;
                    writer.Write(0);
                    writer.Write(NumberSoundEmitters);
                    var positionOffsetMCLQ = BaseStream.Position;
                    writer.Write(0);
                    writer.Write(SizeLiquid);
                    writer.WriteVector3Float(Position);
                    var positionOffsetMCCV = BaseStream.Position;
                    writer.Write(0);
                    writer.Write(Unused1);
                    writer.Write(Unused2);

                    // Subchunks

                    var positionMCVT = BaseStream.Position;
                    BaseStream.Position = positionOffsetMCVT;
                    writer.Write(positionMCVT);
                    BaseStream.Position = positionMCVT;
                    writer.Write(MCVT.GetChunkHeaderBytes());
                    writer.Write(MCVT.GetChunkBytes());

                    var positionMCNR = BaseStream.Position;
                    BaseStream.Position = positionOffsetMCNR;
                    writer.Write(positionMCNR);
                    BaseStream.Position = positionMCNR;
                    writer.Write(MCNR.GetChunkHeaderBytes());
                    writer.Write(MCNR.GetChunkBytes());

                    var positionMCLY = BaseStream.Position;
                    BaseStream.Position = positionOffsetMCLY;
                    writer.Write(positionMCLY);
                    BaseStream.Position = positionMCLY;
                    writer.Write(MCLY.GetChunkHeaderBytes());
                    writer.Write(MCLY.GetChunkBytes());

                    var positionMCRF = BaseStream.Position;
                    BaseStream.Position = positionOffsetMCRF;
                    writer.Write(positionMCRF);
                    BaseStream.Position = positionMCRF;
                    writer.Write(MCRF.GetChunkHeaderBytes());
                    writer.Write(MCRF.GetChunkBytes());

                    // if WDT
                    var positionMCAL = BaseStream.Position;
                    BaseStream.Position = positionOffsetMCAL;
                    writer.Write(positionMCAL);
                    BaseStream.Position = positionMCAL;
                    writer.Write(MCAL.GetChunkHeaderBytes());
                    writer.Write(MCAL.GetChunkBytes());

                    if (SizeShadow > 8 && Flags.HasFlag(MCNKFlags.HasMCSH))
                    {
                        var positionMCSH = BaseStream.Position;
                        BaseStream.Position = positionOffsetMCSH;
                        writer.Write(positionMCSH);
                        BaseStream.Position = positionMCSH;
                        writer.Write(MCSH.GetChunkHeaderBytes());
                        writer.Write(MCSH.GetChunkBytes());
                    }

                    if (NumberSoundEmitters > 0)
                    {
                        var positionMCSE = BaseStream.Position;
                        BaseStream.Position = positionOffsetMCSE;
                        writer.Write(positionMCSE);
                        BaseStream.Position = positionMCSE;
                        writer.Write(MCSE.GetChunkHeaderBytes());
                        writer.Write(MCSE.GetChunkBytes());
                    }

                    if (SizeLiquid > 0)
                    {
                        var positionMCLQ = BaseStream.Position;
                        BaseStream.Position = positionOffsetMCLQ;
                        writer.Write(positionMCLQ);
                        BaseStream.Position = positionMCLQ;
                        writer.Write(MCLQ.GetChunkHeaderBytes());
                        writer.Write(MCLQ.GetChunkBytes());
                    }

                    var positionMCCV = BaseStream.Position;
                    BaseStream.Position = positionOffsetMCCV;
                    writer.Write(positionMCCV);
                    BaseStream.Position = positionMCCV;
                    writer.Write(MCCV.GetChunkHeaderBytes());
                    writer.Write(MCCV.GetChunkBytes());
                }
                return(stream.ToArray());
            }
        }
Beispiel #8
0
        public MCNK(byte[] chunkBytes, WDT.WDT wdt) : base(chunkBytes)
        {
            Flags               = (MCNKFlags)ReadUInt32();
            Index               = this.ReadVector2UInt();
            Layers              = ReadUInt32();
            NumberDoodadRefs    = ReadUInt32();
            OffsetMCVT          = ReadUInt32();
            OffsetMCNR          = ReadUInt32();
            OffsetMCLY          = ReadUInt32();
            OffsetMCRF          = ReadUInt32();
            OffsetMCAL          = ReadUInt32();
            SizeAlpha           = ReadUInt32();
            OffsetMCSH          = ReadUInt32();
            SizeShadow          = ReadUInt32();
            AreaId              = ReadUInt32();
            NumberMapObjectRefs = ReadUInt32();
            Holes               = ReadUInt16();
            HolesPadding        = ReadUInt16();
            // Maybe change this to two longs like in https://bitbucket.org/mugadr_m/kotlin-wow/src/378f3fdec7fff325f52560fc2cce64c946cf57ab/editor/src/main/kotlin/ch/cromon/wow/io/files/map/wotlk/MapChunk.kt?at=master&fileviewer=file-view-default#MapChunk.kt-37
            for (int i = 0; i < 16; i++)
            {
                ReallyLowQualityTextureingMap[i] = ReadByte();
            }
            PredTex             = ReadUInt32();
            NumberEffectDoodads = ReadUInt32();
            OffsetMCSE          = ReadUInt32();
            NumberSoundEmitters = ReadUInt32();
            OffsetMCLQ          = ReadUInt32();
            SizeLiquid          = ReadUInt32();
            Position            = this.ReadVector3Float();
            OffsetMCCV          = ReadUInt32();
            Unused1             = ReadUInt32();
            Unused2             = ReadUInt32();

            if (OffsetMCVT > 0)
            {
                BaseStream.Position = OffsetMCVT;
                MCVT = new MCVT(ReadBytes((int)MCVT.ChunkSize));
            }

            if (OffsetMCNR > 0)
            {
                BaseStream.Position = OffsetMCNR;
                MCNR = new MCNR(ReadBytes((int)MCNR.ChunkSize));
            }

            if (OffsetMCLY > 0)
            {
                BaseStream.Position = OffsetMCLY;
                MCLY = new MCLY(ReadBytes((int)MCLY.ChunkSize));
            }

            if (OffsetMCRF > 0)
            {
                BaseStream.Position = OffsetMCRF;
                MCRF = new MCRF(ReadBytes((int)MCRF.ChunkSize), this);
            }

            // TODO: && No WDT file?
            if (OffsetMCAL > 0 && wdt != null)
            {
                BaseStream.Position = OffsetMCAL;
                MCAL = new MCAL(ReadBytes((int)MCAL.ChunkSize), this, wdt);
            }

            if (OffsetMCSH > 0 && SizeShadow > 8 && Flags.HasFlag(MCNKFlags.HasMCSH))
            {
                BaseStream.Position = OffsetMCSH;
                MCSH = new MCSH(ReadBytes((int)MCSH.ChunkSize));
            }

            if (OffsetMCSE > 0 && NumberSoundEmitters > 0)
            {
                BaseStream.Position = OffsetMCSE;
                MCSE = new MCSE(ReadBytes((int)MCSE.ChunkSize), this);
            }

            if (OffsetMCLQ > 0 && SizeLiquid > 0)
            {
                BaseStream.Position = OffsetMCLQ;
                MCLQ = new MCLQ(ReadBytes((int)MCLQ.ChunkSize), this);
            }

            if (OffsetMCCV > 0)
            {
                BaseStream.Position = OffsetMCCV;
                MCCV = new MCCV(ReadBytes((int)MCCV.ChunkSize));
            }
            Close();
        }
Beispiel #9
0
        public static bool ConvertADT(ChunkedFile adt, string mapName, string outputPath, int gx, int gy, uint build, bool ignoreDeepWater)
        {
            // Prepare map header
            MapFileHeader map;

            map.mapMagic     = SharedConst.MAP_MAGIC;
            map.versionMagic = SharedConst.MAP_VERSION_MAGIC;
            map.buildMagic   = build;

            // Get area flags data
            for (var x = 0; x < SharedConst.ADT_CELLS_PER_GRID; ++x)
            {
                for (var y = 0; y < SharedConst.ADT_CELLS_PER_GRID; ++y)
                {
                    AreaIDs[x][y]       = 0;
                    LiquidEntries[x][y] = 0;
                    LiquidFlags[x][y]   = 0;
                }
            }

            for (var x = 0; x < SharedConst.ADT_GRID_SIZE; ++x)
            {
                for (var y = 0; y < SharedConst.ADT_GRID_SIZE; ++y)
                {
                    V8[x][y]         = 0;
                    LiquidSnow[x][y] = false;
                }
            }

            for (var x = 0; x < SharedConst.ADT_GRID_SIZE + 1; ++x)
            {
                for (var y = 0; y < SharedConst.ADT_GRID_SIZE + 1; ++y)
                {
                    V9[x][y] = 0;
                }
            }

            for (var x = 0; x < SharedConst.ADT_CELLS_PER_GRID; ++x)
            {
                for (var y = 0; y < SharedConst.ADT_CELLS_PER_GRID; ++y)
                {
                    for (var z = 0; z < 8; ++z)
                    {
                        Holes[x][y][z] = 0;
                    }
                }
            }

            bool hasHoles     = false;
            bool hasFlightBox = false;

            foreach (var fileChunk in adt.chunks.LookupByKey("MCNK"))
            {
                MCNK mcnk = fileChunk.As <MCNK>();

                // Area data
                AreaIDs[mcnk.IndexY][mcnk.IndexX] = (ushort)mcnk.AreaID;

                // Height
                // Height values for triangles stored in order:
                // 1     2     3     4     5     6     7     8     9
                //    10    11    12    13    14    15    16    17
                // 18    19    20    21    22    23    24    25    26
                //    27    28    29    30    31    32    33    34
                // . . . . . . . .
                // For better get height values merge it to V9 and V8 map
                // V9 height map:
                // 1     2     3     4     5     6     7     8     9
                // 18    19    20    21    22    23    24    25    26
                // . . . . . . . .
                // V8 height map:
                //    10    11    12    13    14    15    16    17
                //    27    28    29    30    31    32    33    34
                // . . . . . . . .

                // Set map height as grid height
                for (int y = 0; y <= SharedConst.ADT_CELL_SIZE; y++)
                {
                    int cy = (int)mcnk.IndexY * SharedConst.ADT_CELL_SIZE + y;
                    for (int x = 0; x <= SharedConst.ADT_CELL_SIZE; x++)
                    {
                        int cx = (int)mcnk.IndexX * SharedConst.ADT_CELL_SIZE + x;
                        V9[cy][cx] = mcnk.ypos;
                    }
                }

                for (int y = 0; y < SharedConst.ADT_CELL_SIZE; y++)
                {
                    int cy = (int)mcnk.IndexY * SharedConst.ADT_CELL_SIZE + y;
                    for (int x = 0; x < SharedConst.ADT_CELL_SIZE; x++)
                    {
                        int cx = (int)mcnk.IndexX * SharedConst.ADT_CELL_SIZE + x;
                        V8[cy][cx] = mcnk.ypos;
                    }
                }

                // Get custom height
                FileChunk chunk = fileChunk.GetSubChunk("MCVT");
                if (chunk != null)
                {
                    MCVT mcvt = chunk.As <MCVT>();

                    // get V9 height map
                    for (int y = 0; y <= SharedConst.ADT_CELL_SIZE; y++)
                    {
                        int cy = (int)mcnk.IndexY * SharedConst.ADT_CELL_SIZE + y;
                        for (int x = 0; x <= SharedConst.ADT_CELL_SIZE; x++)
                        {
                            int cx = (int)mcnk.IndexX * SharedConst.ADT_CELL_SIZE + x;
                            V9[cy][cx] += mcvt.HeightMap[y * (SharedConst.ADT_CELL_SIZE * 2 + 1) + x];
                        }
                    }
                    // get V8 height map
                    for (int y = 0; y < SharedConst.ADT_CELL_SIZE; y++)
                    {
                        int cy = (int)mcnk.IndexY * SharedConst.ADT_CELL_SIZE + y;
                        for (int x = 0; x < SharedConst.ADT_CELL_SIZE; x++)
                        {
                            int cx = (int)mcnk.IndexX * SharedConst.ADT_CELL_SIZE + x;
                            V8[cy][cx] += mcvt.HeightMap[y * (SharedConst.ADT_CELL_SIZE * 2 + 1) + SharedConst.ADT_CELL_SIZE + 1 + x];
                        }
                    }
                }

                // Liquid data
                if (mcnk.MCLQCount > 8)
                {
                    FileChunk lquidChunk = fileChunk.GetSubChunk("MCLQ");
                    if (lquidChunk != null)
                    {
                        MCLQ liquid = lquidChunk.As <MCLQ>();
                        int  count  = 0;
                        for (int y = 0; y < SharedConst.ADT_CELL_SIZE; ++y)
                        {
                            int cy = (int)mcnk.IndexY * SharedConst.ADT_CELL_SIZE + y;
                            for (int x = 0; x < SharedConst.ADT_CELL_SIZE; ++x)
                            {
                                int cx = (int)mcnk.IndexX * SharedConst.ADT_CELL_SIZE + x;
                                if (liquid.Flags[y][x] != 0x0F)
                                {
                                    LiquidSnow[cy][cx] = true;
                                    if (!ignoreDeepWater && Convert.ToBoolean(liquid.Flags[y][x] & (1 << 7)))
                                    {
                                        LiquidFlags[mcnk.IndexY][mcnk.IndexX] |= LiquidHeaderTypeFlags.DarkWater;
                                    }
                                    ++count;
                                }
                            }
                        }

                        if (mcnk.Flags.HasAnyFlag(MCNKFlags.LiquidRiver))
                        {
                            LiquidEntries[mcnk.IndexY][mcnk.IndexX] = 1;
                            LiquidFlags[mcnk.IndexY][mcnk.IndexX]  |= LiquidHeaderTypeFlags.Water;           // water
                        }
                        if (mcnk.Flags.HasAnyFlag(MCNKFlags.LiquidOcean))
                        {
                            LiquidEntries[mcnk.IndexY][mcnk.IndexX] = 2;
                            LiquidFlags[mcnk.IndexY][mcnk.IndexX]  |= LiquidHeaderTypeFlags.Ocean;           // ocean
                        }
                        if (mcnk.Flags.HasAnyFlag(MCNKFlags.LiquidMagma))
                        {
                            LiquidEntries[mcnk.IndexY][mcnk.IndexX] = 3;
                            LiquidFlags[mcnk.IndexY][mcnk.IndexX]  |= LiquidHeaderTypeFlags.Magma;           // magma
                        }
                        if (mcnk.Flags.HasAnyFlag(MCNKFlags.LiquidSlime))
                        {
                            LiquidEntries[mcnk.IndexY][mcnk.IndexX] = 4;
                            LiquidFlags[mcnk.IndexY][mcnk.IndexX]  |= LiquidHeaderTypeFlags.Slime;           // slime
                        }

                        if (count == 0 && LiquidFlags[mcnk.IndexY][mcnk.IndexX] != 0)
                        {
                            Console.WriteLine("Wrong liquid detect in MCLQ chunk");
                        }

                        for (int y = 0; y <= SharedConst.ADT_CELL_SIZE; ++y)
                        {
                            int cy = (int)mcnk.IndexY * SharedConst.ADT_CELL_SIZE + y;
                            for (int x = 0; x <= SharedConst.ADT_CELL_SIZE; ++x)
                            {
                                int cx = (int)mcnk.IndexX * SharedConst.ADT_CELL_SIZE + x;
                                LiquidHeight[cy][cx] = liquid.Liquid[y][x].Height;
                            }
                        }
                    }
                }

                // Hole data
                if (!mcnk.Flags.HasAnyFlag(MCNKFlags.HighResHoles))
                {
                    uint hole = mcnk.HolesLowRes;
                    if (hole != 0)
                    {
                        if (TransformToHighRes((ushort)hole, Holes[mcnk.IndexY][mcnk.IndexX]))
                        {
                            hasHoles = true;
                        }
                    }
                }
                else
                {
                    Buffer.BlockCopy(mcnk.HighResHoles, 0, Holes[mcnk.IndexY][mcnk.IndexX], 0, 8);
                    if (BitConverter.ToUInt64(Holes[mcnk.IndexY][mcnk.IndexX], 0) != 0)
                    {
                        hasHoles = true;
                    }
                }
            }

            // Get liquid map for grid (in WOTLK used MH2O chunk)
            FileChunk chunkMH2O = adt.GetChunk("MH2O");

            if (chunkMH2O != null)
            {
                MH2O h2o = chunkMH2O.As <MH2O>();
                for (int i = 0; i < SharedConst.ADT_CELLS_PER_GRID; i++)
                {
                    for (int j = 0; j < SharedConst.ADT_CELLS_PER_GRID; j++)
                    {
                        MH2OInstance?h = h2o.GetLiquidInstance(i, j);
                        if (!h.HasValue)
                        {
                            continue;
                        }

                        MH2OInstance       adtLiquidHeader = h.Value;
                        MH2OChunkAttribute?attrs           = h2o.GetLiquidAttributes(i, j);

                        int   count      = 0;
                        ulong existsMask = h2o.GetLiquidExistsBitmap(adtLiquidHeader);
                        for (int y = 0; y < adtLiquidHeader.GetHeight(); y++)
                        {
                            int cy = i * SharedConst.ADT_CELL_SIZE + y + adtLiquidHeader.GetOffsetY();
                            for (int x = 0; x < adtLiquidHeader.GetWidth(); x++)
                            {
                                int cx = j * SharedConst.ADT_CELL_SIZE + x + adtLiquidHeader.GetOffsetX();
                                if (Convert.ToBoolean(existsMask & 1))
                                {
                                    LiquidSnow[cy][cx] = true;
                                    ++count;
                                }
                                existsMask >>= 1;
                            }
                        }

                        LiquidEntries[i][j] = h2o.GetLiquidType(adtLiquidHeader);
                        if (LiquidEntries[i][j] != 0)
                        {
                            var liquidTypeRecord = LiquidTypeStorage[LiquidEntries[i][j]];
                            switch ((LiquidType)liquidTypeRecord.SoundBank)
                            {
                            case LiquidType.Water:
                                LiquidFlags[i][j] |= LiquidHeaderTypeFlags.Water;
                                break;

                            case LiquidType.Ocean:
                                LiquidFlags[i][j] |= LiquidHeaderTypeFlags.Ocean;
                                if (!ignoreDeepWater && attrs.Value.Deep != 0)
                                {
                                    LiquidFlags[i][j] |= LiquidHeaderTypeFlags.DarkWater;
                                }
                                break;

                            case LiquidType.Magma:
                                LiquidFlags[i][j] |= LiquidHeaderTypeFlags.Magma;
                                break;

                            case LiquidType.Slime:
                                LiquidFlags[i][j] |= LiquidHeaderTypeFlags.Slime;
                                break;

                            default:
                                Console.WriteLine($"\nCan't find Liquid type {adtLiquidHeader.LiquidType} for map {mapName}\nchunk {i},{j}\n");
                                break;
                            }

                            if (count == 0 && LiquidFlags[i][j] != 0)
                            {
                                Console.WriteLine("Wrong liquid detect in MH2O chunk");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"LiquidEntries is 0 for [{i}][{j}] ({i * j})");
                        }

                        int pos = 0;
                        for (int y = 0; y <= adtLiquidHeader.GetHeight(); y++)
                        {
                            int cy = i * SharedConst.ADT_CELL_SIZE + y + adtLiquidHeader.GetOffsetY();
                            for (int x = 0; x <= adtLiquidHeader.GetWidth(); x++)
                            {
                                int cx = j * SharedConst.ADT_CELL_SIZE + x + adtLiquidHeader.GetOffsetX();

                                LiquidHeight[cy][cx] = h2o.GetLiquidHeight(adtLiquidHeader, i, j, pos);

                                pos++;
                            }
                        }
                    }
                }
            }

            FileChunk chunkMFBO = adt.GetChunk("MFBO");

            if (chunkMFBO != null)
            {
                MFBO mfbo = chunkMFBO.As <MFBO>();
                for (var i = 0; i < 3; ++i)
                {
                    FlightBoxMax[i][0] = mfbo.Max.coords[0 + i * 3];
                    FlightBoxMax[i][1] = mfbo.Max.coords[1 + i * 3];
                    FlightBoxMax[i][2] = mfbo.Max.coords[2 + i * 3];

                    FlightBoxMin[i][0] = mfbo.Min.coords[0 + i * 3];
                    FlightBoxMin[i][1] = mfbo.Min.coords[1 + i * 3];
                    FlightBoxMin[i][2] = mfbo.Min.coords[2 + i * 3];
                }
                hasFlightBox = true;
            }

            //============================================
            // Try pack area data
            //============================================
            bool fullAreaData = false;
            uint areaId       = AreaIDs[0][0];

            for (int y = 0; y < SharedConst.ADT_CELLS_PER_GRID; ++y)
            {
                for (int x = 0; x < SharedConst.ADT_CELLS_PER_GRID; ++x)
                {
                    if (AreaIDs[y][x] != areaId)
                    {
                        fullAreaData = true;
                        break;
                    }
                }
            }

            map.areaMapOffset = 44;
            map.areaMapSize   = 8;

            MapAreaHeader areaHeader;

            areaHeader.fourcc = SharedConst.MAP_AREA_MAGIC;
            areaHeader.flags  = 0;
            if (fullAreaData)
            {
                areaHeader.gridArea = 0;
                map.areaMapSize    += 512;
            }
            else
            {
                areaHeader.flags   |= AreaHeaderFlags.NoArea;
                areaHeader.gridArea = (ushort)areaId;
            }

            //============================================
            // Try pack height data
            //============================================
            float maxHeight = -20000;
            float minHeight = 20000;

            for (int y = 0; y < SharedConst.ADT_GRID_SIZE; y++)
            {
                for (int x = 0; x < SharedConst.ADT_GRID_SIZE; x++)
                {
                    float h = V8[y][x];
                    if (maxHeight < h)
                    {
                        maxHeight = h;
                    }
                    if (minHeight > h)
                    {
                        minHeight = h;
                    }
                }
            }
            for (int y = 0; y <= SharedConst.ADT_GRID_SIZE; y++)
            {
                for (int x = 0; x <= SharedConst.ADT_GRID_SIZE; x++)
                {
                    float h = V9[y][x];
                    if (maxHeight < h)
                    {
                        maxHeight = h;
                    }
                    if (minHeight > h)
                    {
                        minHeight = h;
                    }
                }
            }

            // Check for allow limit minimum height (not store height in deep ochean - allow save some memory)
            if (minHeight < -2000.0f)
            {
                for (int y = 0; y < SharedConst.ADT_GRID_SIZE; y++)
                {
                    for (int x = 0; x < SharedConst.ADT_GRID_SIZE; x++)
                    {
                        if (V8[y][x] < -2000.0f)
                        {
                            V8[y][x] = -2000.0f;
                        }
                    }
                }
                for (int y = 0; y <= SharedConst.ADT_GRID_SIZE; y++)
                {
                    for (int x = 0; x <= SharedConst.ADT_GRID_SIZE; x++)
                    {
                        if (V9[y][x] < -2000.0f)
                        {
                            V9[y][x] = -2000.0f;
                        }
                    }
                }

                if (minHeight < -2000.0f)
                {
                    minHeight = -2000.0f;
                }
                if (maxHeight < -2000.0f)
                {
                    maxHeight = -2000.0f;
                }
            }

            map.heightMapOffset = map.areaMapOffset + map.areaMapSize;
            map.heightMapSize   = (uint)Marshal.SizeOf <MapHeightHeader>();

            MapHeightHeader heightHeader;

            heightHeader.fourcc        = SharedConst.MAP_HEIGHT_MAGIC;
            heightHeader.flags         = 0;
            heightHeader.gridHeight    = minHeight;
            heightHeader.gridMaxHeight = maxHeight;

            if (maxHeight == minHeight)
            {
                heightHeader.flags |= HeightHeaderFlags.NoHeight;
            }

            // Not need store if flat surface
            if ((maxHeight - minHeight) < 0.005f)
            {
                heightHeader.flags |= HeightHeaderFlags.NoHeight;
            }

            if (hasFlightBox)
            {
                heightHeader.flags |= HeightHeaderFlags.HasFlightBounds;
                map.heightMapSize  += 18 + 18;
            }

            // Try store as packed in uint16 or uint8 values
            if (!heightHeader.flags.HasFlag(HeightHeaderFlags.NoHeight))
            {
                float step = 0;
                // Try Store as uint values
                if (true)//CONF_allow_float_to_int
                {
                    float diff = maxHeight - minHeight;
                    if (diff < 2.0f)      // As uint8 (max accuracy = CONF_float_to_int8_limit/256)
                    {
                        heightHeader.flags |= HeightHeaderFlags.AsInt8;
                        step = 255 / diff;
                    }
                    else if (diff < 2048.0f)  // As uint16 (max accuracy = CONF_float_to_int16_limit/65536)
                    {
                        heightHeader.flags |= HeightHeaderFlags.AsInt16;
                        step = 65535 / diff;
                    }
                }

                // Pack it to int values if need
                if (heightHeader.flags.HasFlag(HeightHeaderFlags.AsInt8))
                {
                    for (int y = 0; y < SharedConst.ADT_GRID_SIZE; y++)
                    {
                        for (int x = 0; x < SharedConst.ADT_GRID_SIZE; x++)
                        {
                            UInt8_V8[y][x] = (byte)((V8[y][x] - minHeight) * step + 0.5f);
                        }
                    }
                    for (int y = 0; y <= SharedConst.ADT_GRID_SIZE; y++)
                    {
                        for (int x = 0; x <= SharedConst.ADT_GRID_SIZE; x++)
                        {
                            UInt8_V9[y][x] = (byte)((V9[y][x] - minHeight) * step + 0.5f);
                        }
                    }
                    map.heightMapSize += 16641 + 16384;
                }
                else if (heightHeader.flags.HasFlag(HeightHeaderFlags.AsInt16))
                {
                    for (int y = 0; y < SharedConst.ADT_GRID_SIZE; y++)
                    {
                        for (int x = 0; x < SharedConst.ADT_GRID_SIZE; x++)
                        {
                            UInt16_V8[y][x] = (ushort)((V8[y][x] - minHeight) * step + 0.5f);
                        }
                    }

                    for (int y = 0; y <= SharedConst.ADT_GRID_SIZE; y++)
                    {
                        for (int x = 0; x <= SharedConst.ADT_GRID_SIZE; x++)
                        {
                            UInt16_V9[y][x] = (ushort)((V9[y][x] - minHeight) * step + 0.5f);
                        }
                    }

                    map.heightMapSize += 33282 + 32768;
                }
                else
                {
                    map.heightMapSize += 66564 + 65536;
                }
            }

            //============================================
            // Pack liquid data
            //============================================
            ushort firstLiquidType = LiquidEntries[0][0];
            LiquidHeaderTypeFlags firstLiquidFlag = LiquidFlags[0][0];
            bool fullType = false;

            for (int y = 0; y < SharedConst.ADT_CELLS_PER_GRID; y++)
            {
                for (int x = 0; x < SharedConst.ADT_CELLS_PER_GRID; x++)
                {
                    if (LiquidEntries[y][x] != firstLiquidType || LiquidFlags[y][x] != firstLiquidFlag)
                    {
                        fullType = true;
                        y        = SharedConst.ADT_CELLS_PER_GRID;
                        break;
                    }
                }
            }

            MapLiquidHeader mapLiquidHeader = new();

            // no water data (if all grid have 0 liquid type)
            if (firstLiquidFlag == 0 && !fullType)
            {
                // No liquid data
                map.liquidMapOffset = 0;
                map.liquidMapSize   = 0;
            }
            else
            {
                int minX = 255, minY = 255;
                int maxX = 0, maxY = 0;
                maxHeight = -20000;
                minHeight = 20000;
                for (int y = 0; y < SharedConst.ADT_GRID_SIZE; y++)
                {
                    for (int x = 0; x < SharedConst.ADT_GRID_SIZE; x++)
                    {
                        if (LiquidSnow[y][x])
                        {
                            if (minX > x)
                            {
                                minX = x;
                            }
                            if (maxX < x)
                            {
                                maxX = x;
                            }
                            if (minY > y)
                            {
                                minY = y;
                            }
                            if (maxY < y)
                            {
                                maxY = y;
                            }
                            float h = LiquidHeight[y][x];
                            if (maxHeight < h)
                            {
                                maxHeight = h;
                            }
                            if (minHeight > h)
                            {
                                minHeight = h;
                            }
                        }
                        else
                        {
                            LiquidHeight[y][x] = -2000.0f;
                        }
                    }
                }
                map.liquidMapOffset = map.heightMapOffset + map.heightMapSize;
                map.liquidMapSize   = (uint)Marshal.SizeOf <MapLiquidHeader>();

                mapLiquidHeader.fourcc      = SharedConst.MAP_LIQUID_MAGIC;
                mapLiquidHeader.flags       = LiquidHeaderFlags.None;
                mapLiquidHeader.liquidType  = 0;
                mapLiquidHeader.offsetX     = (byte)minX;
                mapLiquidHeader.offsetY     = (byte)minY;
                mapLiquidHeader.width       = (byte)(maxX - minX + 1 + 1);
                mapLiquidHeader.height      = (byte)(maxY - minY + 1 + 1);
                mapLiquidHeader.liquidLevel = minHeight;

                if (maxHeight == minHeight)
                {
                    mapLiquidHeader.flags |= LiquidHeaderFlags.NoHeight;
                }

                // Not need store if flat surface
                if ((maxHeight - minHeight) < 0.001f)
                {
                    mapLiquidHeader.flags |= LiquidHeaderFlags.NoHeight;
                }

                if (!fullType)
                {
                    mapLiquidHeader.flags |= LiquidHeaderFlags.NoType;
                }

                if (mapLiquidHeader.flags.HasFlag(LiquidHeaderFlags.NoType))
                {
                    mapLiquidHeader.liquidFlags = firstLiquidFlag;
                    mapLiquidHeader.liquidType  = firstLiquidType;
                }
                else
                {
                    map.liquidMapSize += 512 + 256;
                }

                if (!mapLiquidHeader.flags.HasFlag(LiquidHeaderFlags.NoHeight))
                {
                    map.liquidMapSize += (uint)(sizeof(float) * mapLiquidHeader.width * mapLiquidHeader.height);
                }
            }

            if (hasHoles)
            {
                if (map.liquidMapOffset != 0)
                {
                    map.holesOffset = map.liquidMapOffset + map.liquidMapSize;
                }
                else
                {
                    map.holesOffset = map.heightMapOffset + map.heightMapSize;
                }

                map.holesSize = 2048;
            }
            else
            {
                map.holesOffset = 0;
                map.holesSize   = 0;
            }

            // Ok all data prepared - store it
            using (BinaryWriter binaryWriter = new(File.Open(outputPath, FileMode.Create, FileAccess.Write)))
            {
                binaryWriter.WriteStruct(map);
                // Store area data
                binaryWriter.WriteStruct(areaHeader);
                if (!areaHeader.flags.HasFlag(AreaHeaderFlags.NoArea))
                {
                    for (var x = 0; x < AreaIDs.Length; ++x)
                    {
                        for (var y = 0; y < AreaIDs[x].Length; ++y)
                        {
                            binaryWriter.Write(AreaIDs[x][y]);
                        }
                    }
                }

                // Store height data
                binaryWriter.WriteStruct(heightHeader);
                if (!heightHeader.flags.HasFlag(HeightHeaderFlags.NoHeight))
                {
                    if (heightHeader.flags.HasFlag(HeightHeaderFlags.AsInt16))
                    {
                        for (var x = 0; x < UInt16_V9.Length; ++x)
                        {
                            for (var y = 0; y < UInt16_V9[x].Length; ++y)
                            {
                                binaryWriter.Write(UInt16_V9[x][y]);
                            }
                        }

                        for (var x = 0; x < UInt16_V8.Length; ++x)
                        {
                            for (var y = 0; y < UInt16_V8[x].Length; ++y)
                            {
                                binaryWriter.Write(UInt16_V8[x][y]);
                            }
                        }
                    }
                    else if (heightHeader.flags.HasFlag(HeightHeaderFlags.AsInt8))
                    {
                        for (var x = 0; x < UInt8_V9.Length; ++x)
                        {
                            for (var y = 0; y < UInt8_V9[x].Length; ++y)
                            {
                                binaryWriter.Write(UInt8_V9[x][y]);
                            }
                        }

                        for (var x = 0; x < UInt8_V8.Length; ++x)
                        {
                            for (var y = 0; y < UInt8_V8[x].Length; ++y)
                            {
                                binaryWriter.Write(UInt8_V8[x][y]);
                            }
                        }
                    }
                    else
                    {
                        for (var x = 0; x < V9.Length; ++x)
                        {
                            for (var y = 0; y < V9[x].Length; ++y)
                            {
                                binaryWriter.Write(V9[x][y]);
                            }
                        }

                        for (var x = 0; x < V8.Length; ++x)
                        {
                            for (var y = 0; y < V8[x].Length; ++y)
                            {
                                binaryWriter.Write(V8[x][y]);
                            }
                        }
                    }
                }

                if (heightHeader.flags.HasFlag(HeightHeaderFlags.HasFlightBounds))
                {
                    for (var x = 0; x < 3; ++x)
                    {
                        for (var y = 0; y < 3; ++y)
                        {
                            binaryWriter.Write(FlightBoxMax[x][y]);
                        }
                    }

                    for (var x = 0; x < 3; ++x)
                    {
                        for (var y = 0; y < 3; ++y)
                        {
                            binaryWriter.Write(FlightBoxMin[x][y]);
                        }
                    }
                }

                // Store liquid data if need
                if (map.liquidMapOffset != 0)
                {
                    binaryWriter.WriteStruct(mapLiquidHeader);
                    if (!mapLiquidHeader.flags.HasFlag(LiquidHeaderFlags.NoType))
                    {
                        for (var x = 0; x < LiquidEntries.Length; ++x)
                        {
                            for (var y = 0; y < LiquidEntries[x].Length; ++y)
                            {
                                binaryWriter.Write(LiquidEntries[x][y]);
                            }
                        }

                        for (var x = 0; x < LiquidFlags.Length; ++x)
                        {
                            for (var y = 0; y < LiquidFlags[x].Length; ++y)
                            {
                                binaryWriter.Write((byte)LiquidFlags[x][y]);
                            }
                        }
                    }

                    if (!mapLiquidHeader.flags.HasFlag(LiquidHeaderFlags.NoHeight))
                    {
                        for (int y = 0; y < mapLiquidHeader.height; y++)
                        {
                            for (int x = 0; x < mapLiquidHeader.width; x++)
                            {
                                binaryWriter.Write(LiquidHeight[y + mapLiquidHeader.offsetY][x + mapLiquidHeader.offsetX]);
                            }
                        }
                    }
                }

                // store hole data
                if (hasHoles)
                {
                    for (var x = 0; x < Holes.Length; ++x)
                    {
                        for (var y = 0; y < Holes[x].Length; ++y)
                        {
                            for (var z = 0; z < Holes[x][y].Length; ++z)
                            {
                                binaryWriter.Write(Holes[x][y][z]);
                            }
                        }
                    }
                }
            }

            return(true);
        }