Beispiel #1
0
        public void UnloadMapTile(uint tileX, uint tileY, VMapManager2 vm)
        {
            uint tileID = packTileID(tileX, tileY);

            if (!iLoadedTiles.ContainsKey(tileID))
            {
                Console.WriteLine("StaticMapTree.UnloadMapTile() : trying to unload non-loaded tile - Map:{iMapID} X:{tileX} Y:{tileY}");
                return;
            }

            var tile = iLoadedTiles.LookupByKey(tileID);

            if (tile) // file associated with tile
            {
                string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY);
                if (File.Exists(tilefile))
                {
                    using (BinaryReader binaryReader = new BinaryReader(File.Open(tilefile, FileMode.Open, FileAccess.Read)))
                    {
                        bool result = true;
                        if (binaryReader.ReadStringFromChars(8) != SharedConst.VMAP_MAGIC)
                        {
                            result = false;
                        }

                        uint numSpawns = binaryReader.ReadUInt32();
                        for (uint i = 0; i < numSpawns && result; ++i)
                        {
                            // read model spawns
                            ModelSpawn spawn;
                            result = ModelSpawn.readFromFile(binaryReader, out spawn);
                            if (result)
                            {
                                // release model instance
                                vm.releaseModelInstance(spawn.name);

                                // update tree
                                uint referencedNode = binaryReader.ReadUInt32();
                                if (!iLoadedSpawns.ContainsKey(referencedNode))
                                {
                                    Console.WriteLine($"StaticMapTree.UnloadMapTile() : trying to unload non-referenced model '{spawn.name}' (ID:{spawn.ID})");
                                }
                                else if (--iLoadedSpawns[referencedNode] == 0)
                                {
                                    iTreeValues[referencedNode].setUnloaded();
                                    iLoadedSpawns.Remove(referencedNode);
                                }
                            }
                        }
                    }
                }
            }
            iLoadedTiles.Remove(tileID);
            //TC_METRIC_EVENT("map_events", "UnloadMapTile", "Map: " + std::to_string(iMapID) + " TileX: " + std::to_string(tileX) + " TileY: " + std::to_string(tileY));
        }
Beispiel #2
0
        public bool InitMap(string fname, VMapManager2 vm)
        {
            //VMAP_DEBUG_LOG("maps", "StaticMapTree::InitMap() : initializing StaticMapTree '%s'", fname);
            bool   success  = false;
            string fullname = iBasePath + fname;

            if (!File.Exists(fullname))
            {
                return(false);
            }

            using (BinaryReader binaryReader = new BinaryReader(File.Open(fullname, FileMode.Open, FileAccess.Read)))
            {
                if (binaryReader.ReadStringFromChars(8) == SharedConst.VMAP_MAGIC)
                {
                    iIsTiled = binaryReader.ReadBoolean();
                    if (binaryReader.ReadStringFromChars(4) == "NODE" && iTree.readFromFile(binaryReader))
                    {
                        iNTreeValues = iTree.primCount();
                        iTreeValues  = new ModelInstance[iNTreeValues];
                        success      = binaryReader.ReadStringFromChars(4) == "GOBJ";
                    }
                }

                // global model spawns
                // only non-tiled maps have them, and if so exactly one (so far at least...)
                ModelSpawn spawn;
                if (!iIsTiled && ModelSpawn.readFromFile(binaryReader, out spawn))
                {
                    WorldModel model = vm.acquireModelInstance(iBasePath, spawn.name);
                    //VMAP_DEBUG_LOG("maps", "StaticMapTree::InitMap() : loading %s", spawn.name);
                    if (model != null)
                    {
                        // assume that global model always is the first and only tree value (could be improved...)
                        iTreeValues[0]   = new ModelInstance(spawn, model);
                        iLoadedSpawns[0] = 1;
                    }
                    else
                    {
                        success = false;
                        Console.WriteLine("StaticMapTree.InitMap() : could not acquire WorldModel pointer for '{spawn.name}'");
                    }
                }
            }
            return(success);
        }
Beispiel #3
0
        public bool LoadMapTile(uint tileX, uint tileY, VMapManager2 vm)
        {
            if (_treeValues == null)
            {
                Console.WriteLine("StaticMapTree.LoadMapTile() : tree has not been initialized [{tileX}, {tileY}]");
                return(false);
            }

            string tilefile = _basePath + GetTileFileName(_mapId, tileX, tileY);

            if (File.Exists(tilefile))
            {
                using (BinaryReader binaryReader = new BinaryReader(File.Open(tilefile, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    if (binaryReader.ReadStringFromChars(8) != SharedConst.VMAP_MAGIC)
                    {
                        return(false);
                    }

                    uint numSpawns = binaryReader.ReadUInt32();
                    for (uint i = 0; i < numSpawns; ++i)
                    {
                        // read model spawns
                        ModelSpawn spawn;
                        var        result = ModelSpawn.ReadFromFile(binaryReader, out spawn);
                        if (result)
                        {
                            // acquire model instance
                            WorldModel model = vm.AcquireModelInstance(_basePath, spawn.name);
                            if (model == null)
                            {
                                Console.WriteLine($"StaticMapTree.LoadMapTile() : could not acquire WorldModel pointer [{tileX}, {tileY}]");
                            }

                            // update tree
                            if (_spawnIndices.ContainsKey(spawn.ID))
                            {
                                uint referencedVal = _spawnIndices[spawn.ID];
                                if (!_loadedSpawns.ContainsKey(referencedVal))
                                {
                                    if (referencedVal > _nTreeValues)
                                    {
                                        Console.WriteLine($"StaticMapTree.LoadMapTile() : invalid tree element ({referencedVal}/{_nTreeValues}) referenced in tile {tilefile}");
                                        continue;
                                    }

                                    _treeValues[referencedVal]   = new ModelInstance(spawn, model);
                                    _loadedSpawns[referencedVal] = 1;
                                }
                                else
                                {
                                    ++_loadedSpawns[referencedVal];
                                }
                            }
                        }
                    }
                    _loadedTiles[PackTileID(tileX, tileY)] = true;
                }
            }
            else
            {
                _loadedTiles[PackTileID(tileX, tileY)] = false;
            }

            //TC_METRIC_EVENT("map_events", "LoadMapTile", "Map: " + std::to_string(iMapID) + " TileX: " + std::to_string(tileX) + " TileY: " + std::to_string(tileY));
            return(true);
        }
Beispiel #4
0
        public bool LoadMapTile(uint tileX, uint tileY, VMapManager2 vm)
        {
            if (!iIsTiled)
            {
                // currently, core creates grids for all maps, whether it has terrain tiles or not
                // so we need "fake" tile loads to know when we can unload map geometry
                iLoadedTiles[packTileID(tileX, tileY)] = false;
                return(true);
            }
            if (iTreeValues == null)
            {
                Console.WriteLine("StaticMapTree.LoadMapTile() : tree has not been initialized [{tileX}, {tileY}]");
                return(false);
            }

            string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY);

            if (File.Exists(tilefile))
            {
                using (BinaryReader binaryReader = new BinaryReader(File.Open(tilefile, FileMode.Open, FileAccess.Read)))
                {
                    if (binaryReader.ReadStringFromChars(8) != SharedConst.VMAP_MAGIC)
                    {
                        return(false);
                    }

                    uint numSpawns = binaryReader.ReadUInt32();
                    for (uint i = 0; i < numSpawns; ++i)
                    {
                        // read model spawns
                        ModelSpawn spawn;
                        var        result = ModelSpawn.readFromFile(binaryReader, out spawn);
                        if (result)
                        {
                            // acquire model instance
                            WorldModel model = vm.acquireModelInstance(iBasePath, spawn.name);
                            if (model == null)
                            {
                                Console.WriteLine($"StaticMapTree.LoadMapTile() : could not acquire WorldModel pointer [{tileX}, {tileY}]");
                            }

                            // update tree
                            uint referencedVal = binaryReader.ReadUInt32();
                            if (!iLoadedSpawns.ContainsKey(referencedVal))
                            {
                                if (referencedVal > iNTreeValues)
                                {
                                    Console.WriteLine($"StaticMapTree.LoadMapTile() : invalid tree element ({referencedVal}/{iNTreeValues}) referenced in tile {tilefile}");
                                    continue;
                                }

                                iTreeValues[referencedVal]   = new ModelInstance(spawn, model);
                                iLoadedSpawns[referencedVal] = 1;
                            }
                            else
                            {
                                ++iLoadedSpawns[referencedVal];
                            }
                        }
                    }
                    iLoadedTiles[packTileID(tileX, tileY)] = true;
                }
            }
            else
            {
                iLoadedTiles[packTileID(tileX, tileY)] = false;
            }

            //TC_METRIC_EVENT("map_events", "LoadMapTile", "Map: " + std::to_string(iMapID) + " TileX: " + std::to_string(tileX) + " TileY: " + std::to_string(tileY));
            return(true);
        }