public static void GenerateSnapshot(string snapshotPath, string[] workers)
 {
     Console.WriteLine($"Generating snapshot file {snapshotPath}");
     using (var snapshotOutput = new SnapshotOutputStream(snapshotPath))
     {
         for (var i = 0; i < workers.Length; i++)
         {
             var entityId   = new EntityId(i + 1);
             var workerType = workers[i];
             var entity     = createEntity(workerType);
             try
             {
                 snapshotOutput.WriteEntity(entityId, entity);
             }
             catch (StreamInvalidDataException ex)
             {
                 throw new SystemException("StreamInvalidDataException saving entity: " + ex.Message);
             }
             catch (StreamBadStateException ex)
             {
                 throw new SystemException("StreamBadStateException saving entity: " + ex.Message);
             }
         }
     }
 }
Exemple #2
0
        public static void GenerateSnapshot(string snapshotPath, string[] workers)
        {
            Console.WriteLine(String.Format("Generating snapshot file {0}", snapshotPath));
            Assembly.Load("GeneratedCode");
            var entityid = 0;

            using (var snapshotOutput = new SnapshotOutputStream(snapshotPath))
            {
                for (var i = 0; i < 1000; i++)
                {
                    for (int j = 0; j < 1000; j++)
                    {
                        Console.WriteLine(String.Format("hellu"));
                        var entityId   = new EntityId(++entityid);
                        var workerType = workers[0];
                        var entity     = createEntity(workerType, new Coordinates(i, 0, j));
                        var error      = snapshotOutput.WriteEntity(entityId, entity);
                        if (error.HasValue)
                        {
                            throw new System.SystemException("error saving: " + error.Value);
                        }
                    }
                }
            }
        }
Exemple #3
0
        private static void WriteSnapshot(Dictionary <EntityId, Entity> entities, string snapshotName)
        {
            Debug.Log("Writing snapshot to: " + snapshotName);
            var outputStream = new SnapshotOutputStream(snapshotName);

            foreach (var entry in entities)
            {
                var error = outputStream.WriteEntity(entry.Key, entry.Value);
                if (error.HasValue)
                {
                    Debug.Log(error.Value);
                }
            }

            outputStream.Dispose();
        }
Exemple #4
0
 public void SaveSnapshot()
 {
     File.Delete(snapshotPath);
     using (SnapshotOutputStream stream = new SnapshotOutputStream(snapshotPath))
     {
         foreach (var kvp in snapshotEntities)
         {
             var error = stream.WriteEntity(kvp.Key, kvp.Value);
             if (error.HasValue)
             {
                 Debug.LogErrorFormat("Failed to generate initial world snapshot: {0}", error.Value);
                 return;
             }
         }
     }
     Debug.LogFormat("Successfully generated initial world snapshot at {0}", snapshotPath);
 }
        private static void SaveSnapshot(IDictionary <EntityId, Entity> snapshotEntities)
        {
            File.Delete(SimulationSettings.DefaultSnapshotPath);

            using (SnapshotOutputStream stream = new SnapshotOutputStream(SimulationSettings.DefaultSnapshotPath))
            {
                foreach (var pair in snapshotEntities)
                {
                    var error = stream.WriteEntity(pair.Key, pair.Value);
                    if (error.HasValue)
                    {
                        Debug.LogErrorFormat("Failed to generate initial world snapshot: {0}", error.Value);
                        return;
                    }
                }
            }
        }
Exemple #6
0
 public static void GenerateSnapshot(string snapshotPath, string[] workers)
 {
     Console.WriteLine(String.Format("Generating snapshot file {0}", snapshotPath));
     using (var snapshotOutput = new SnapshotOutputStream(snapshotPath))
     {
         for (var i = 0; i < workers.Length; i++)
         {
             var entityId   = new EntityId(i + 1);
             var workerType = workers[i];
             var entity     = createEntity(workerType);
             var error      = snapshotOutput.WriteEntity(entityId, entity);
             if (error.HasValue)
             {
                 throw new System.SystemException("error saving: " + error.Value);
             }
         }
     }
 }
Exemple #7
0
        private static void SaveSnapshot(IDictionary <EntityId, Entity> snapshotEntities)
        {
            var snapshotPath = Application.dataPath + SimulationSettings.DefaultRelativeSnapshotPath;

            File.Delete(snapshotPath);
            using (SnapshotOutputStream stream = new SnapshotOutputStream(snapshotPath))
            {
                foreach (var kvp in snapshotEntities)
                {
                    var error = stream.WriteEntity(kvp.Key, kvp.Value);
                    if (error.HasValue)
                    {
                        Debug.LogErrorFormat("Failed to generate initial world snapshot: {0}", error.Value);
                        return;
                    }
                }
            }
            Debug.LogFormat("Successfully generated initial world snapshot at {0}", snapshotPath);
        }
Exemple #8
0
        //[MenuItem("Improbable/Snapshots/Generate Phase 0 DEV Snapshot")]
        //private static void GeneratePhase0SnapshotsDev()
        //{
        //var snapshotEntities = new Dictionary<EntityId, Entity>();
        //var currentEntityId = 1;
        //var numDrones = SimulationSettings.numDrones;
        //var squareSize = SimulationSettings.squareSize;

        ////snapshotEntities.Add(new EntityId(currentEntityId++), EntityTemplateFactory.CreatePlayerCreatorTemplate());
        //snapshotEntities.Add(
        //    new EntityId(currentEntityId++),
        //    EntityTemplateFactory.CreateControllerTemplate(
        //        new Coordinates(0, 0, 0),
        //        new Vector3d(-1000, 0, 1000),
        //        new Vector3d(1000, 0, -1000),
        //        new NFZTemplate[]{})
        //);

        //var numDrones = SimulationSettings.numDrones;
        //var squareSize = SimulationSettings.squareSize;

        //for (int i = 0; i < numDrones; i++)
        //{
        //    Coordinates spawn = new Coordinates(Random.Range(-squareSize, squareSize), 0, Random.Range(-squareSize, squareSize));
        //    Vector3f target = new Vector3f(Random.Range(-squareSize, squareSize), 0, Random.Range(-squareSize, squareSize));
        //    float speed = Random.Range(2, 10);
        //    float radius = Random.Range(0.5f, 2);
        //    snapshotEntities.Add(new EntityId(currentEntityId++), EntityTemplateFactory.CreateDroneTemplate(spawn, target, speed, radius));
        //}

        //SaveSnapshot(snapshotEntities, "phase0dev");
        //}

        //[MenuItem("Improbable/Snapshots/Generate Phase 0 DEPLOY Snapshot")]
        //private static void GeneratePhase0Snapshots()
        //{
        //    //snapshotEntities.Add(new EntityId(currentEntityId++), EntityTemplateFactory.CreatePlayerCreatorTemplate());
        //    //snapshotEntities.Add(new EntityId(currentEntityId++), EntityTemplateFactory.CreateServerNodeTemplate(new Coordinates(5, 0, 5)));

        //    //GenerateSinglePhase0Snapshot(10, 10);
        //    //GenerateSinglePhase0Snapshot(20, 20);
        //    //GenerateSinglePhase0Snapshot(50, 50);
        //    //GenerateSinglePhase0Snapshot(100, 50);
        //    //GenerateSinglePhase0Snapshot(100, 100);
        //    //GenerateSinglePhase0Snapshot(500, 100);
        //    GenerateSinglePhase0Snapshot(1000, 800);
        //}

        //private static void GenerateSinglePhase0Snapshot(int numDrones, int squareSize)
        //{
        //    var snapshotEntities = new Dictionary<EntityId, Entity>();
        //    var currentEntityId = 1;

        //    for (int i = 0; i < numDrones; i++)
        //    {
        //        Coordinates spawn = new Coordinates(Random.Range(-squareSize, squareSize), 0, Random.Range(-squareSize, squareSize));
        //        Vector3f target = new Vector3f(Random.Range(-squareSize, squareSize), 0, Random.Range(-squareSize, squareSize));
        //        float speed = Random.Range(2, 10);
        //        float radius = Random.Range(0.5f, 2);
        //        snapshotEntities.Add(new EntityId(currentEntityId++), EntityTemplateFactory.CreateDroneTemplate(spawn, target, new EntityId(1)));
        //    }

        //    //SaveSnapshot(snapshotEntities, "phase0_d" + numDrones + "s" + squareSize);
        //    SaveSnapshot(snapshotEntities, "phase0");
        //}

        public static void SaveSnapshot(IDictionary <EntityId, Entity> snapshotEntities, string snapshotName)
        {
            string snapshotPath = Application.dataPath + "/../../../snapshots/" + snapshotName + ".snapshot";

            File.Delete(snapshotPath);
            using (SnapshotOutputStream stream = new SnapshotOutputStream(snapshotPath))
            {
                foreach (var kvp in snapshotEntities)
                {
                    var error = stream.WriteEntity(kvp.Key, kvp.Value);
                    if (error.HasValue)
                    {
                        Debug.LogErrorFormat("Failed to generate initial world snapshot: {0}", error.Value);
                        return;
                    }
                }
            }

            Debug.LogFormat("Successfully generated initial world snapshot at {0}", snapshotPath);
        }
        const string workerType   = "life"; //Layer name not workerType?

        static int Main(string[] arguments)
        {
            Console.WriteLine(String.Format("Generating snapshot file {0}", snapshotPath));
            Assembly.Load("GeneratedCode");

            // https://docs.improbable.io/reference/13.3/csharpsdk/using/snapshots

            // Construct a SnapshotOutputStream to write a snapshot to a file at the string path.
            SnapshotOutputStream sos = new SnapshotOutputStream(snapshotPath);

            var id             = 0;
            var maxRowCount    = 50; //dimensionsInWorldUnits z is 100
            var maxColumnCount = 50; //dimensionsInWorldUnits x is 100

            // TODO: Update to account for center being at (0.0)

            //Create a grid of entities
            for (double y = 0; y < maxRowCount; y++)        //Starts at 0
            {
                for (double x = 0; x < maxColumnCount; x++) //starts at 0
                {
                    id++;                                   //starts at 1
                    var entityId = new EntityId(id);
                    Improbable.Collections.List <EntityId> nList = getNeighbors(x, maxColumnCount, y, maxRowCount, id);
                    bool currIsAlive = RandomBool();
                    bool prevIsAlive = RandomBool();
                    var  entity      = createEntity(workerType, x, y, currIsAlive, 0, prevIsAlive, 0, nList);
                    var  error       = sos.WriteEntity(entityId, entity);
                    if (error.HasValue)
                    {
                        throw new System.SystemException("error saving: " + error.Value);
                    }
                }
            }

            // Writes the end of snapshot header and releases the resources of the SnapshotOutputStream.
            sos.Dispose();

            return(0);
        }
Exemple #10
0
        /// <summary>
        ///     Writes the snapshot out to a file.
        /// </summary>
        /// <param name="path">The file path.</param>
        public void WriteToFile(string path)
        {
            var parameters = new SnapshotParameters
            {
                DefaultComponentVtable = new PassthroughComponentVtable()
            };

            using (var outputStream = new SnapshotOutputStream(path, parameters))
            {
                foreach (var entry in entities)
                {
                    try
                    {
                        outputStream.WriteEntity(entry.Key, entry.Value);
                    }
                    catch (System.IO.InvalidDataException e)
                    {
                        Debug.LogError(e.Message);
                    }
                }
            }
        }
Exemple #11
0
        private static void SaveSnapshot(IDictionary <EntityId, Entity> snapshotEntities)
        {
            File.Delete(SimulationSettings.DefaultSnapshotPath);
            SnapshotOutputStream stream = new SnapshotOutputStream(SimulationSettings.DefaultSnapshotPath);

            foreach (EntityId key in snapshotEntities.Keys)
            {
                Entity entity = snapshotEntities[key];

                var maybeError = stream.WriteEntity(key, entity);

                if (maybeError.HasValue)
                {
                    Debug.LogErrorFormat("Failed to generate initial world snapshot: {0}", maybeError.Value);
                    return;
                }
                else
                {
                    Debug.LogFormat("Successfully generated initial world snapshot at {0}", SimulationSettings.DefaultSnapshotPath);
                }
            }
        }
Exemple #12
0
        public EntityOutputStream(String fname)
        {
            snapshotOutputStream = new SnapshotOutputStream(fname);

            entityTypeCounts = new Dictionary <string, uint>();
        }