Exemple #1
0
        public override void ProcessPacket(PlanetDataRequest packet, NebulaConnection conn)
        {
            if (IsClient)
            {
                return;
            }

            Dictionary <int, byte[]> planetDataToReturn = new Dictionary <int, byte[]>();

            foreach (int planetId in packet.PlanetIDs)
            {
                PlanetData planet = GameMain.galaxy.PlanetById(planetId);
                Log.Info($"Returning terrain for {planet.name}");

                // NOTE: The following has been picked-n-mixed from "PlanetModelingManager.PlanetComputeThreadMain()"
                // This method is **costly** - do not run it more than is required!
                // It generates the planet on the host and then sends it to the client

                PlanetAlgorithm planetAlgorithm = PlanetModelingManager.Algorithm(planet);

                if (planet.data == null)
                {
                    planet.data    = new PlanetRawData(planet.precision);
                    planet.modData = planet.data.InitModData(planet.modData);
                    planet.data.CalcVerts();
                    planet.aux = new PlanetAuxData(planet);
                    planetAlgorithm.GenerateTerrain(planet.mod_x, planet.mod_y);
                    planetAlgorithm.CalcWaterPercent();

                    //Load planet meshes and register callback to unload unneccessary stuff
                    planet.wanted    = true;
                    planet.onLoaded += OnActivePlanetLoaded;
                    PlanetModelingManager.modPlanetReqList.Enqueue(planet);

                    if (planet.type != EPlanetType.Gas)
                    {
                        planetAlgorithm.GenerateVegetables();
                        planetAlgorithm.GenerateVeins(false);
                    }
                }

                using (BinaryUtils.Writer writer = new BinaryUtils.Writer())
                {
                    planet.ExportRuntime(writer.BinaryWriter);
                    planetDataToReturn.Add(planetId, writer.CloseAndGetBytes());
                }
            }

            conn.SendPacket(new PlanetDataResponse(planetDataToReturn));
        }
        private static byte[] PlanetCompute(int planetId) 
        {
            PlanetData planet = GameMain.galaxy.PlanetById(planetId);
            HighStopwatch highStopwatch = new HighStopwatch();
            highStopwatch.Begin();

            // NOTE: The following has been picked-n-mixed from "PlanetModelingManager.PlanetComputeThreadMain()"
            // This method is **costly** - do not run it more than is required!
            // It generates the planet on the host and then sends it to the client

            PlanetAlgorithm planetAlgorithm = PlanetModelingManager.Algorithm(planet);

            if (planet.data == null)
            {
                planet.data = new PlanetRawData(planet.precision);
                planet.modData = planet.data.InitModData(planet.modData);
                planet.data.CalcVerts();
                planet.aux = new PlanetAuxData(planet);
                planetAlgorithm.GenerateTerrain(planet.mod_x, planet.mod_y);
                planetAlgorithm.CalcWaterPercent();

                //Load planet meshes and register callback to unload unneccessary stuff
                planet.wanted = true;
                planet.onLoaded += OnActivePlanetLoaded;
                PlanetModelingManager.modPlanetReqList.Enqueue(planet);

                if (planet.type != EPlanetType.Gas)
                {
                    planetAlgorithm.GenerateVegetables();
                    planetAlgorithm.GenerateVeins(false);
                }
            }

            byte[] data;
            using (BinaryUtils.Writer writer = new BinaryUtils.Writer())
            {
                planet.ExportRuntime(writer.BinaryWriter);
                data = writer.CloseAndGetBytes();
            }
            Log.Info($"Returning terrain for {planet.name} (id:{planet.id} time:{highStopwatch.duration:F4}s)");
            return data;
        }