public ulong getLiquidShowMap(adt_liquid_header h) { if (h.offsData2a != 0) { return(BitConverter.ToUInt64(_data, 8 + (int)h.offsData2a)); } else { return(0xFFFFFFFFFFFFFFFFu); } }
public float[] getLiquidHeightMap(adt_liquid_header h) { if (Convert.ToBoolean(h.formatFlags & 0x02)) { return(null); } if (h.offsData2b != 0) { int index = 8 + (int)h.offsData2b; float[] value = new float[_data.Length - index]; Buffer.BlockCopy(_data, index, value, 0, value.Length); return(value); } return(null); }
public byte[] getLiquidLightMap(adt_liquid_header h) { if (Convert.ToBoolean(h.formatFlags & 0x01)) { return(null); } if (h.offsData2b != 0) { int index = (int)(8 + h.offsData2b + (h.width + 1) * (h.height + 1) * 4); if (Convert.ToBoolean(h.formatFlags & 0x02)) { index = 8 + (int)h.offsData2b; } byte[] value = new byte[_data.Length - index]; Buffer.BlockCopy(_data, index, value, 0, value.Length); return(value); } return(null); }
public static bool ConvertADT(CASCHandler cascHandler, string inputPath, string outputPath, int cell_y, int cell_x, uint build) { ChunkedFile adt = new ChunkedFile(); if (!adt.loadFile(cascHandler, inputPath)) { return(false); } // Prepare map header map_fileheader map; map.mapMagic = MAP_MAGIC; map.versionMagic = MAP_VERSION_MAGIC; map.buildMagic = build; // Get area flags data for (var x = 0; x < ADT_CELLS_PER_GRID; ++x) { for (var y = 0; y < ADT_CELLS_PER_GRID; ++y) { area_ids[x][y] = 0; liquid_entry[x][y] = 0; liquid_flags[x][y] = 0; } } for (var x = 0; x < ADT_GRID_SIZE; ++x) { for (var y = 0; y < ADT_GRID_SIZE; ++y) { V8[x][y] = 0; liquid_show[x][y] = false; } } for (var x = 0; x < ADT_GRID_SIZE + 1; ++x) { for (var y = 0; y < ADT_GRID_SIZE + 1; ++y) { V9[x][y] = 0; } } for (var x = 0; x < ADT_CELLS_PER_GRID; ++x) { for (var y = 0; y < 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")) { adt_MCNK mcnk = fileChunk.As <adt_MCNK>(); // Area data area_ids[mcnk.iy][mcnk.ix] = (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 <= ADT_CELL_SIZE; y++) { int cy = (int)mcnk.iy * ADT_CELL_SIZE + y; for (int x = 0; x <= ADT_CELL_SIZE; x++) { int cx = (int)mcnk.ix * ADT_CELL_SIZE + x; V9[cy][cx] = mcnk.ypos; } } for (int y = 0; y < ADT_CELL_SIZE; y++) { int cy = (int)mcnk.iy * ADT_CELL_SIZE + y; for (int x = 0; x < ADT_CELL_SIZE; x++) { int cx = (int)mcnk.ix * ADT_CELL_SIZE + x; V8[cy][cx] = mcnk.ypos; } } // Get custom height FileChunk chunk = fileChunk.GetSubChunk("MCVT"); if (chunk != null) { adt_MCVT mcvt = chunk.As <adt_MCVT>(); // get V9 height map for (int y = 0; y <= ADT_CELL_SIZE; y++) { int cy = (int)mcnk.iy * ADT_CELL_SIZE + y; for (int x = 0; x <= ADT_CELL_SIZE; x++) { int cx = (int)mcnk.ix * ADT_CELL_SIZE + x; V9[cy][cx] += mcvt.height_map[y * (ADT_CELL_SIZE * 2 + 1) + x]; } } // get V8 height map for (int y = 0; y < ADT_CELL_SIZE; y++) { int cy = (int)mcnk.iy * ADT_CELL_SIZE + y; for (int x = 0; x < ADT_CELL_SIZE; x++) { int cx = (int)mcnk.ix * ADT_CELL_SIZE + x; V8[cy][cx] += mcvt.height_map[y * (ADT_CELL_SIZE * 2 + 1) + ADT_CELL_SIZE + 1 + x]; } } } // Liquid data if (mcnk.sizeMCLQ > 8) { FileChunk LiquidChunk = fileChunk.GetSubChunk("MCLQ"); if (LiquidChunk != null) { adt_MCLQ liquid = LiquidChunk.As <adt_MCLQ>(); int count = 0; for (int y = 0; y < ADT_CELL_SIZE; ++y) { int cy = (int)mcnk.iy * ADT_CELL_SIZE + y; for (int x = 0; x < ADT_CELL_SIZE; ++x) { int cx = (int)mcnk.ix * ADT_CELL_SIZE + x; if (liquid.flags[y][x] != 0x0F) { liquid_show[cy][cx] = true; if (Convert.ToBoolean(liquid.flags[y][x] & (1 << 7))) { liquid_flags[mcnk.iy][mcnk.ix] |= (byte)LiquidTypeMask.DarkWater; } ++count; } } } uint c_flag = mcnk.flags; if (Convert.ToBoolean(c_flag & (1 << 2))) { liquid_entry[mcnk.iy][mcnk.ix] = 1; liquid_flags[mcnk.iy][mcnk.ix] |= (byte)LiquidTypeMask.Water; // water } if (Convert.ToBoolean(c_flag & (1 << 3))) { liquid_entry[mcnk.iy][mcnk.ix] = 2; liquid_flags[mcnk.iy][mcnk.ix] |= (byte)LiquidTypeMask.Ocean; // ocean } if (Convert.ToBoolean(c_flag & (1 << 4))) { liquid_entry[mcnk.iy][mcnk.ix] = 3; liquid_flags[mcnk.iy][mcnk.ix] |= (byte)LiquidTypeMask.Magma; // magma/slime } if (count == 0 && liquid_flags[mcnk.iy][mcnk.ix] != 0) { Console.WriteLine("Wrong liquid detect in MCLQ chunk"); } for (int y = 0; y <= ADT_CELL_SIZE; ++y) { int cy = (int)mcnk.iy * ADT_CELL_SIZE + y; for (int x = 0; x <= ADT_CELL_SIZE; ++x) { int cx = (int)mcnk.ix * ADT_CELL_SIZE + x; liquid_height[cy][cx] = liquid.liquid[y][x].height; } } } } // Hole data if (!Convert.ToBoolean(mcnk.flags & 0x10000)) { uint hole = mcnk.holes; if (hole != 0) { if (TransformToHighRes((ushort)hole, holes[mcnk.iy][mcnk.ix])) { hasHoles = true; } } } else { Buffer.BlockCopy(mcnk.HighResHoles, 0, holes[mcnk.iy][mcnk.ix], 0, 8); if (BitConverter.ToUInt64(holes[mcnk.iy][mcnk.ix], 0) != 0) { hasHoles = true; } } } // Get liquid map for grid (in WOTLK used MH2O chunk) FileChunk chunkMH20 = adt.GetChunk("MH2O"); if (chunkMH20 != null) { adt_MH2O h2o = chunkMH20.As <adt_MH2O>(); for (int i = 0; i < ADT_CELLS_PER_GRID; i++) { for (int j = 0; j < ADT_CELLS_PER_GRID; j++) { adt_liquid_header?h = h2o.getLiquidData(i, j); if (!h.HasValue) { continue; } adt_liquid_header adtLiquidHeader = h.Value; int count = 0; ulong show = h2o.getLiquidShowMap(adtLiquidHeader); for (int y = 0; y < adtLiquidHeader.height; y++) { int cy = i * ADT_CELL_SIZE + y + adtLiquidHeader.yOffset; for (int x = 0; x < adtLiquidHeader.width; x++) { int cx = j * ADT_CELL_SIZE + x + adtLiquidHeader.xOffset; if (Convert.ToBoolean(show & 1)) { liquid_show[cy][cx] = true; ++count; } show >>= 1; } } liquid_entry[i][j] = adtLiquidHeader.liquidType; var liquidTypeRecord = Program.liquidTypeStorage[adtLiquidHeader.liquidType]; switch ((LiquidType)liquidTypeRecord.LiquidType) { case LiquidType.Water: liquid_flags[i][j] |= (byte)LiquidTypeMask.Water; break; case LiquidType.Ocean: liquid_flags[i][j] |= (byte)LiquidTypeMask.Ocean; break; case LiquidType.Magma: liquid_flags[i][j] |= (byte)LiquidTypeMask.Magma; break; case LiquidType.Slime: liquid_flags[i][j] |= (byte)LiquidTypeMask.Slime; break; default: Console.WriteLine($"\nCan't find Liquid type {adtLiquidHeader.liquidType} for map {inputPath}\nchunk {i},{j}\n"); break; } // Dark water detect if ((LiquidType)liquidTypeRecord.LiquidType == LiquidType.Ocean) { byte[] lm = h2o.getLiquidLightMap(adtLiquidHeader); if (lm == null) { liquid_flags[i][j] |= (byte)LiquidTypeMask.DarkWater; } } if (count == 0 && liquid_flags[i][j] != 0) { Console.WriteLine("Wrong liquid detect in MH2O chunk"); } float[] height = h2o.getLiquidHeightMap(adtLiquidHeader); int pos = 0; for (int y = 0; y <= adtLiquidHeader.height; y++) { int cy = i * ADT_CELL_SIZE + y + adtLiquidHeader.yOffset; for (int x = 0; x <= adtLiquidHeader.width; x++) { int cx = j * ADT_CELL_SIZE + x + adtLiquidHeader.xOffset; if (height != null) { liquid_height[cy][cx] = height[pos]; } else { liquid_height[cy][cx] = adtLiquidHeader.heightLevel1; } pos++; } } } } } FileChunk chunkMFBO = adt.GetChunk("MFBO"); if (chunkMFBO != null) { adt_MFBO mfbo = chunkMFBO.As <adt_MFBO>(); for (var i = 0; i < 3; ++i) { flight_box_max[i][0] = mfbo.max.coords[0 + i * 3]; flight_box_max[i][1] = mfbo.max.coords[1 + i * 3]; flight_box_max[i][2] = mfbo.max.coords[2 + i * 3]; flight_box_min[i][0] = mfbo.min.coords[0 + i * 3]; flight_box_min[i][1] = mfbo.min.coords[1 + i * 3]; flight_box_min[i][2] = mfbo.min.coords[2 + i * 3]; } hasFlightBox = true; } //============================================ // Try pack area data //============================================ bool fullAreaData = false; uint areaId = area_ids[0][0]; for (int y = 0; y < ADT_CELLS_PER_GRID; ++y) { for (int x = 0; x < ADT_CELLS_PER_GRID; ++x) { if (area_ids[y][x] != areaId) { fullAreaData = true; break; } } } map.areaMapOffset = 44; map.areaMapSize = 8; map_areaHeader areaHeader; areaHeader.fourcc = MAP_AREA_MAGIC; areaHeader.flags = 0; if (fullAreaData) { areaHeader.gridArea = 0; map.areaMapSize += 512; } else { areaHeader.flags |= 0x0001; areaHeader.gridArea = (ushort)areaId; } //============================================ // Try pack height data //============================================ float maxHeight = -20000; float minHeight = 20000; for (int y = 0; y < ADT_GRID_SIZE; y++) { for (int x = 0; x < ADT_GRID_SIZE; x++) { float h = V8[y][x]; if (maxHeight < h) { maxHeight = h; } if (minHeight > h) { minHeight = h; } } } for (int y = 0; y <= ADT_GRID_SIZE; y++) { for (int x = 0; x <= 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 < -500.0f) { for (int y = 0; y < ADT_GRID_SIZE; y++) { for (int x = 0; x < ADT_GRID_SIZE; x++) { if (V8[y][x] < -500.0f) { V8[y][x] = -500.0f; } } } for (int y = 0; y <= ADT_GRID_SIZE; y++) { for (int x = 0; x <= ADT_GRID_SIZE; x++) { if (V9[y][x] < -500.0f) { V9[y][x] = -500.0f; } } } if (minHeight < -500.0f) { minHeight = -500.0f; } if (maxHeight < -500.0f) { maxHeight = -500.0f; } } map.heightMapOffset = map.areaMapOffset + map.areaMapSize; map.heightMapSize = (uint)Marshal.SizeOf <map_heightHeader>(); map_heightHeader heightHeader; heightHeader.fourcc = MAP_HEIGHT_MAGIC; heightHeader.flags = 0; heightHeader.gridHeight = minHeight; heightHeader.gridMaxHeight = maxHeight; if (maxHeight == minHeight) { heightHeader.flags |= (uint)MapHeightFlags.NoHeight; } // Not need store if flat surface if ((maxHeight - minHeight) < 0.005f) { heightHeader.flags |= (uint)MapHeightFlags.NoHeight; } if (hasFlightBox) { heightHeader.flags |= (uint)MapHeightFlags.HasFlightBounds; map.heightMapSize += 18 + 18; } // Try store as packed in uint16 or uint8 values if (!Convert.ToBoolean(heightHeader.flags & (uint)MapHeightFlags.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 |= (uint)MapHeightFlags.AsInt8; step = 255 / diff; } else if (diff < 2048.0f) // As uint16 (max accuracy = CONF_float_to_int16_limit/65536) { heightHeader.flags |= (uint)MapHeightFlags.AsInt16; step = 65535 / diff; } } // Pack it to int values if need if (Convert.ToBoolean(heightHeader.flags & (uint)MapHeightFlags.AsInt8)) { for (int y = 0; y < ADT_GRID_SIZE; y++) { for (int x = 0; x < ADT_GRID_SIZE; x++) { uint8_V8[y][x] = (byte)((V8[y][x] - minHeight) * step + 0.5f); } } for (int y = 0; y <= ADT_GRID_SIZE; y++) { for (int x = 0; x <= ADT_GRID_SIZE; x++) { uint8_V9[y][x] = (byte)((V9[y][x] - minHeight) * step + 0.5f); } } map.heightMapSize += 16641 + 16384; } else if (Convert.ToBoolean(heightHeader.flags & (uint)MapHeightFlags.AsInt16)) { for (int y = 0; y < ADT_GRID_SIZE; y++) { for (int x = 0; x < ADT_GRID_SIZE; x++) { uint16_V8[y][x] = (ushort)((V8[y][x] - minHeight) * step + 0.5f); } } for (int y = 0; y <= ADT_GRID_SIZE; y++) { for (int x = 0; x <= 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 //============================================ byte type = liquid_flags[0][0]; bool fullType = false; for (int y = 0; y < ADT_CELLS_PER_GRID; y++) { for (int x = 0; x < ADT_CELLS_PER_GRID; x++) { if (liquid_flags[y][x] != type) { fullType = true; y = ADT_CELLS_PER_GRID; break; } } } map_liquidHeader mapLiquidHeader = new map_liquidHeader(); // no water data (if all grid have 0 liquid type) if (type == 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 < ADT_GRID_SIZE; y++) { for (int x = 0; x < ADT_GRID_SIZE; x++) { if (liquid_show[y][x]) { if (minX > x) { minX = x; } if (maxX < x) { maxX = x; } if (minY > y) { minY = y; } if (maxY < y) { maxY = y; } float h = liquid_height[y][x]; if (maxHeight < h) { maxHeight = h; } if (minHeight > h) { minHeight = h; } } else { liquid_height[y][x] = -500.0f; } } } map.liquidMapOffset = map.heightMapOffset + map.heightMapSize; map.liquidMapSize = (uint)Marshal.SizeOf <map_liquidHeader>(); mapLiquidHeader.fourcc = MAP_LIQUID_MAGIC; mapLiquidHeader.flags = 0; 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 |= 0x0002; } // Not need store if flat surface if ((maxHeight - minHeight) < 0.001f) { mapLiquidHeader.flags |= 0x0002; } if (!fullType) { mapLiquidHeader.flags |= 0x0001; } if (Convert.ToBoolean(mapLiquidHeader.flags & 0x0001)) { mapLiquidHeader.liquidType = type; } else { map.liquidMapSize += 512 + 256; } if (!Convert.ToBoolean(mapLiquidHeader.flags & 0x0002)) { map.liquidMapSize += (uint)(sizeof(float) * mapLiquidHeader.width * mapLiquidHeader.height); } } if (map.liquidMapOffset != 0) { map.holesOffset = map.liquidMapOffset + map.liquidMapSize; } else { map.holesOffset = map.heightMapOffset + map.heightMapSize; } if (hasHoles) { map.holesSize = 2048;// (uint)(1 * holes.Length); } else { map.holesSize = 0; } // Ok all data prepared - store it using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(outputPath, FileMode.Create, FileAccess.Write))) { binaryWriter.WriteStruct(map); // Store area data binaryWriter.WriteStruct(areaHeader); if (!Convert.ToBoolean(areaHeader.flags & 0x0001)) { for (var x = 0; x < area_ids.Length; ++x) { for (var y = 0; y < area_ids[x].Length; ++y) { binaryWriter.Write(area_ids[x][y]); } } } // Store height data binaryWriter.WriteStruct(heightHeader); if (!Convert.ToBoolean(heightHeader.flags & (uint)MapHeightFlags.NoHeight)) { if (Convert.ToBoolean(heightHeader.flags & (uint)MapHeightFlags.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 (Convert.ToBoolean(heightHeader.flags & (uint)MapHeightFlags.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 (Convert.ToBoolean(heightHeader.flags & (uint)MapHeightFlags.HasFlightBounds)) { for (var x = 0; x < 3; ++x) { for (var y = 0; y < 3; ++y) { binaryWriter.Write(flight_box_max[x][y]); } } for (var x = 0; x < 3; ++x) { for (var y = 0; y < 3; ++y) { binaryWriter.Write(flight_box_min[x][y]); } } } // Store liquid data if need if (map.liquidMapOffset != 0) { binaryWriter.WriteStruct(mapLiquidHeader); if (!Convert.ToBoolean(mapLiquidHeader.flags & 0x0001)) { for (var x = 0; x < liquid_entry.Length; ++x) { for (var y = 0; y < liquid_entry[x].Length; ++y) { binaryWriter.Write(liquid_entry[x][y]); } } for (var x = 0; x < liquid_flags.Length; ++x) { for (var y = 0; y < liquid_flags[x].Length; ++y) { binaryWriter.Write(liquid_flags[x][y]); } } } if (!Convert.ToBoolean(mapLiquidHeader.flags & 0x0002)) { for (int y = 0; y < mapLiquidHeader.height; y++) { for (int x = 0; x < mapLiquidHeader.width; x++) { binaryWriter.Write(liquid_height[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); }