Ejemplo n.º 1
0
        public bool convertWorld2()
        {
            bool success = readMapSpawns();

            if (!success)
            {
                return(false);
            }

            float invTileSize = 1.0f / 533.33333f;

            // export Map data
            foreach (var mapPair in mapData)
            {
                var mapSpawn = mapPair.Value;
                // build global map tree
                List <ModelSpawn> mapSpawns = new List <ModelSpawn>();

                Console.WriteLine($"Calculating model bounds for map {mapPair.Key}...");
                foreach (var entry in mapSpawn.UniqueEntries.Values)
                {
                    // M2 models don't have a bound set in WDT/ADT placement data, i still think they're not used for LoS at all on retail
                    if (Convert.ToBoolean(entry.flags & ModelFlags.M2))
                    {
                        if (!calculateTransformedBound(entry))
                        {
                            continue;
                        }
                    }

                    mapSpawns.Add(entry);
                    spawnedModelFiles.Add(entry.name);

                    var tileEntries = Convert.ToBoolean(entry.flags & ModelFlags.ParentSpawn) ? mapSpawn.ParentTileEntries : mapSpawn.TileEntries;

                    AxisAlignedBox bounds = entry.iBound;
                    Vector2        low    = new Vector2(bounds.Lo.X * invTileSize, bounds.Lo.Y * invTileSize);
                    Vector2        high   = new Vector2(bounds.Hi.X * invTileSize, bounds.Hi.Y * invTileSize);
                    for (uint x = (ushort)low.X; x <= (ushort)high.X; ++x)
                    {
                        for (uint y = (ushort)low.Y; y <= (ushort)high.Y; ++y)
                        {
                            tileEntries.Add(StaticMapTree.PackTileID(x, y), new TileSpawn(entry.ID, entry.flags));
                        }
                    }
                }

                Console.WriteLine($"Creating map tree for map {mapPair.Key}...");
                BIH pTree = new BIH();
                pTree.build(mapSpawns, BoundsTrait.GetBounds);

                // ===> possibly move this code to StaticMapTree class

                // write map tree file
                string mapfilename = $"{iDestDir}/{mapPair.Key:D4}.vmtree";
                using (BinaryWriter writer = new BinaryWriter(File.Open(mapfilename, FileMode.Create, FileAccess.Write)))
                {
                    //general info
                    writer.WriteString(SharedConst.VMAP_MAGIC);

                    // Nodes
                    writer.WriteString("NODE");
                    pTree.writeToFile(writer);

                    // spawn id to index map
                    writer.WriteString("SIDX");
                    writer.Write(mapSpawns.Count);
                    for (int i = 0; i < mapSpawns.Count; ++i)
                    {
                        writer.Write(mapSpawns[i].ID);
                        writer.Write(i);
                    }
                }

                // write map tile files, similar to ADT files, only with extra BIH tree node info
                foreach (var key in mapSpawn.TileEntries.Keys)
                {
                    var spawnList = mapSpawn.TileEntries[key];

                    uint x, y;
                    StaticMapTree.UnpackTileID(key, out x, out y);
                    string tilefilename = $"{iDestDir}/{mapPair.Key:D4}_{y:D2}_{x:D2}.vmtile";
                    using (BinaryWriter writer = new BinaryWriter(File.Open(tilefilename, FileMode.Create, FileAccess.Write)))
                    {
                        var parentTileEntries = mapPair.Value.ParentTileEntries[key];

                        int nSpawns = spawnList.Count + parentTileEntries.Count;

                        // file header
                        writer.WriteString(SharedConst.VMAP_MAGIC);
                        // write number of tile spawns
                        writer.Write(nSpawns);
                        // write tile spawns
                        foreach (var tileSpawn in spawnList)
                        {
                            ModelSpawn.WriteToFile(writer, mapPair.Value.UniqueEntries[tileSpawn.Id]);
                        }

                        foreach (var spawnItr in parentTileEntries)
                        {
                            ModelSpawn.WriteToFile(writer, mapPair.Value.UniqueEntries[spawnItr.Id]);
                        }
                    }
                }
            }

            // add an object models, listed in temp_gameobject_models file
            exportGameobjectModels();

            // export objects
            Console.WriteLine("Converting Model Files");
            foreach (var mfile in spawnedModelFiles)
            {
                Console.WriteLine($"Converting {mfile}");
                convertRawFile(mfile);
            }

            return(success);
        }
Ejemplo n.º 2
0
        public bool convertWorld2()
        {
            bool success = readMapSpawns();

            if (!success)
            {
                return(false);
            }

            // export Map data
            foreach (var mapPair in mapData)
            {
                var mapSpawn = mapPair.Value;
                // build global map tree
                List <ModelSpawn> mapSpawns = new List <ModelSpawn>();

                Console.WriteLine($"Calculating model bounds for map {mapPair.Key}...");
                foreach (var entry in mapSpawn.UniqueEntries.Values)
                {
                    // M2 models don't have a bound set in WDT/ADT placement data, i still think they're not used for LoS at all on retail
                    if (Convert.ToBoolean(entry.flags & ModelFlags.M2))
                    {
                        if (!calculateTransformedBound(entry))
                        {
                            break;
                        }
                    }
                    else if (Convert.ToBoolean(entry.flags & ModelFlags.WorldSpawn)) // WMO maps and terrain maps use different origin, so we need to adapt :/
                    {
                        /// @todo remove extractor hack and uncomment below line:
                        //entry.second.iPos += Vector3(533.33333f*32, 533.33333f*32, 0.f);
                        entry.iBound = entry.iBound + new Vector3(533.33333f * 32, 533.33333f * 32, 0.0f);
                    }
                    mapSpawns.Add(entry);
                    spawnedModelFiles.Add(entry.name);
                }

                Console.WriteLine($"Creating map tree for map {mapPair.Key}...");
                BIH pTree = new BIH();

                try
                {
                    pTree.build(mapSpawns, BoundsTrait.getBounds);
                }
                catch (Exception e)
                {
                    Console.Write($"Exception {e.Message} when calling pTree.build");
                    return(false);
                }

                // ===> possibly move this code to StaticMapTree class
                Dictionary <uint, uint> modelNodeIdx = new Dictionary <uint, uint>();
                for (uint i = 0; i < mapSpawns.Count; ++i)
                {
                    modelNodeIdx.Add(mapSpawns[(int)i].ID, i);
                }

                // write map tree file
                string mapfilename = $"{iDestDir}/{mapPair.Key:D4}.vmtree";
                using (BinaryWriter writer = new BinaryWriter(File.Open(mapfilename, FileMode.Create, FileAccess.Write)))
                {
                    //general info
                    writer.WriteString(SharedConst.VMAP_MAGIC);
                    uint globalTileID = StaticMapTree.packTileID(65, 65);
                    var  globalRange  = mapSpawn.TileEntries.LookupByKey(globalTileID);
                    bool isTiled      = globalRange.Count == 0; // only maps without terrain (tiles) have global WMO
                    writer.Write(isTiled);

                    // Nodes
                    writer.WriteString("NODE");
                    pTree.writeToFile(writer);
                    // global map spawns (WDT), if any (most instances)
                    writer.WriteString("GOBJ");

                    foreach (var glob in globalRange)
                    {
                        ModelSpawn.writeToFile(writer, mapSpawn.UniqueEntries[glob]);
                    }
                }

                // write map tile files, similar to ADT files, only with extra BSP tree node info
                var tileEntries = mapSpawn.TileEntries;
                foreach (var key in tileEntries.Keys)
                {
                    ModelSpawn spawn = mapSpawn.UniqueEntries[tileEntries[key].First()];
                    if (Convert.ToBoolean(spawn.flags & ModelFlags.WorldSpawn)) // WDT spawn, saved as tile 65/65 currently...
                    {
                        continue;
                    }

                    string tilefilename = $"{iDestDir}/{mapPair.Key:D4}_";
                    uint   x, y;
                    StaticMapTree.unpackTileID(key, out x, out y);
                    tilefilename += $"{x:D2}_{y:D2}.vmtile";
                    using (BinaryWriter writer = new BinaryWriter(File.Open(tilefilename, FileMode.Create, FileAccess.Write)))
                    {
                        // file header
                        writer.WriteString(SharedConst.VMAP_MAGIC);
                        // write number of tile spawns
                        writer.Write(tileEntries[key].Count);
                        // write tile spawns
                        foreach (var nSpawn in tileEntries[key])
                        {
                            ModelSpawn spawn2 = mapSpawn.UniqueEntries[nSpawn];
                            ModelSpawn.writeToFile(writer, spawn2);
                            // MapTree nodes to update when loading tile:
                            var nIdx = modelNodeIdx[spawn2.ID];
                            writer.Write(nIdx);
                        }
                    }
                }
                // break; //test, extract only first map; TODO: remvoe this line
            }

            // add an object models, listed in temp_gameobject_models file
            exportGameobjectModels();
            // export objects
            Console.WriteLine("Converting Model Files");
            foreach (var mfile in spawnedModelFiles)
            {
                Console.WriteLine($"Converting {mfile}");
                convertRawFile(mfile);
            }

            return(success);
        }