Esempio n. 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;
        }
Esempio n. 3
0
            private static void exportStar(PlanetData planet)
            {
                UnityEngine.Debug.Log("-- planet: " + planet.id);
                PlanetAlgorithm planetAlgorithm = PlanetModelingManager.Algorithm(planet);

                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();
                if (planet.type != EPlanetType.Gas)
                {
                    planetAlgorithm.GenerateVegetables();
                }
                if (planet.type != EPlanetType.Gas)
                {
                    planetAlgorithm.GenerateVeins(false);
                }
                Monitor.Enter(streamWriter);
                try
                {
                    if (!firstPlanet)
                    {
                        streamWriter.Write(",");
                    }
                    else
                    {
                        firstPlanet = false;
                    }
                    streamWriter.Write("\"" + planet.name + "\":{" +
                                       "\"seed\":" + planet.seed +
                                       ",\"index\":" + planet.index +
                                       ",\"id\":" + planet.id +
                                       ",\"orbitAround\":" + planet.orbitAround +
                                       ",\"number\":" + planet.number +
                                       ",\"orbitIndex\":" + planet.orbitIndex +
                                       ",\"name\":\"" + planet.name + "\"" +
                                       ",\"orbitRadius\":" + planet.orbitRadius +
                                       ",\"orbitInclination\":" + planet.orbitInclination +
                                       ",\"orbitLongitude\":" + planet.orbitLongitude +
                                       ",\"orbitalPeriod\":" + planet.orbitalPeriod +
                                       ",\"orbitPhase\":" + planet.orbitPhase +
                                       ",\"obliquity\":" + planet.obliquity +
                                       ",\"rotationPeriod\":" + planet.rotationPeriod +
                                       ",\"rotationPhase\":" + planet.rotationPhase +
                                       ",\"radius\":" + planet.radius +
                                       ",\"scale\":" + planet.scale +
                                       ",\"sunDistance\":" + planet.sunDistance +
                                       ",\"habitableBias\":" + planet.habitableBias +
                                       ",\"temperatureBias\":" + planet.temperatureBias +
                                       ",\"ionHeight\":" + planet.ionHeight +
                                       ",\"windStrength\":" + planet.windStrength +
                                       ",\"luminosity\":" + planet.luminosity +
                                       ",\"landPercent\":" + planet.landPercent +
                                       ",\"mod_x\":" + planet.mod_x +
                                       ",\"mod_y\":" + planet.mod_y +
                                       ",\"type\":" + (int)planet.type +
                                       ",\"singularity\":" + (int)planet.singularity +
                                       ",\"theme\":" + planet.theme +
                                       ",\"algoId\":" + planet.algoId +
                                       ",\"waterHeight\":" + planet.waterHeight);
                    if (planet.waterItemId > 0)
                    {
                        ItemProto water = LDB.items.Select(planet.waterItemId);
                        streamWriter.Write(
                            ",\"waterItem\":\"" + water.name + "\""
                            );
                    }
                    if (planet.type == EPlanetType.Gas)
                    {
                        streamWriter.Write(",\"gasTotalHeat\":" + planet.gasTotalHeat);
                        streamWriter.Write(",\"gas\":{"); // open gas[]
                        bool firstvein = true;
                        for (int k = 0; k < planet.gasItems.Length; k++)
                        {
                            ItemProto gas = LDB.items.Select(planet.gasItems[k]);
                            if (firstvein == false)
                            {
                                streamWriter.Write(",");
                            }
                            else
                            {
                                firstvein = false;
                            }
                            streamWriter.Write("\"" + gas.name + "\":{" +
                                               "\"gasName\":\"" + gas.name + "\"" +
                                               ",\"gasItem\":" + planet.gasItems[k] +
                                               ",\"gasSpeed\":\"" + planet.gasSpeeds[k] + "\"" +
                                               ",\"gasHeatValue\":\"" + planet.gasHeatValues[k] + "\"}");
                        }
                        streamWriter.Write("}"); // close gas[]
                    }
                    else
                    {
                        streamWriter.Write(",\"vein\":{"); // open vein[]
                        bool firstvein = true;
                        for (int k = 0; k < planet.veinAmounts.Length; k++)
                        {
                            if (planet.veinAmounts[k] == 0)
                            {
                                continue;
                            }
                            if (firstvein == false)
                            {
                                streamWriter.Write(",");
                            }
                            else
                            {
                                firstvein = false;
                            }
                            streamWriter.Write("\"" + (EVeinType)k + "\":" + planet.veinAmounts[k]);
                        }
                        streamWriter.Write("}");                                                                                                                // close vein[]
                    }
                    streamWriter.Write(",\"uPosition\":{\"x\":" + planet.uPosition.x + ",\"y\":" + planet.uPosition.y + ",\"z\":" + planet.uPosition.z + "}}"); // close planet
                }
                finally { Monitor.Exit(streamWriter); }
                planet.Unload();
            }