Example #1
0
    void OnFixedUpdate()
    {
        if (!GamePresentationCache.Instance.Ready)
        {
            return;
        }

        SimulationWorld simWorld = (SimulationWorld)GamePresentationCache.Instance.SimWorld.EntityManager.World;

        _currentSimTick.Set(simWorld.LatestTickId);

        // 'not dirty' means no change. That means the simulation has NOT played a sim tick this past fixed update
        _offsettedSimTicks[_offsettedSimTicksIterator] = !_currentSimTick.IsDirty;

        _offsettedSimTicksIterator++;
        _offsettedSimTicksIterator %= _offsettedSimTicks.Length;

        if (_offsettedSimTicksIterator == 0)
        {
            _totalOffsettedSimTicks = 0;
            for (int i = 0; i < _offsettedSimTicks.Length; i++)
            {
                if (_offsettedSimTicks[i])
                {
                    _totalOffsettedSimTicks++;
                }
            }
        }

        _currentSimTick.ClearDirty();
    }
Example #2
0
        public MapResource(SimulationWorld world, ConfigurationSection sect)
        {
            string type = sect["Type"].ToLowerInvariant();

            switch (type)
            {
            case "petro":
                OilField oil = new OilField(world);
                oil.Parse(sect);

                Longitude = oil.Longitude;
                Latitude  = oil.Latitude;

                Type   = NaturalResourceType.Petro;
                Amount = oil.CurrentAmount;

                break;

            case "wood":
                Forest fores = new Forest(world);
                fores.Parse(sect);

                Longitude = fores.Longitude;
                Latitude  = fores.Latitude;

                Type   = NaturalResourceType.Wood;
                Amount = fores.CurrentAmount;
                Radius = fores.Radius;
                break;
            }
        }
        private void ReplaceSimWorld(SimulationWorld newWorld)
        {
            Log.Info($"Replacing sim world with new one (at tick {newWorld.GetLastTickIdFromEntity()})");
            if (SimulationWorld != null)
            {
                if (_inPlayerLoop)
                {
                    _tickSystem.RemoveFromPlayerLoop(SimulationWorld);
                    _inPlayerLoop = false;
                }
                SimulationWorld.Dispose();
                SimulationWorld = null;
            }

            SimulationWorld = newWorld;

            newWorld.Owner = this;

            _tickSystem.CreateSimSystemsAndAddToPlayerLoop(newWorld);
            _inPlayerLoop = true;

            SimWorldAccessor.SimWorld      = newWorld.GetInternalAccessor().SimWorld;
            SimWorldAccessor.EntityManager = newWorld.GetInternalAccessor().EntityManager;
            SimWorldAccessor.SomeSimSystem = newWorld.GetInternalAccessor().SomeSimSystem; // could be any system

            HasJustRepacked = true;
            ReplaceVersion++;
            WorldReplaced?.Invoke();
        }
Example #4
0
        private FuSMMachine machine;                //The FuSM Machine.

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="world">The world the entity lives in.</param>
        /// <param name="location">The location we want to spawn the entity at.</param>
        public FAIController(SimulationWorld world, Vector2 location) : base(world, location)
        {
            machine = new FuSMMachine();

            //Add states to the FuSM Machine
            machine.AddState(new Fire(this));
            machine.AddState(new Warn(this));
            machine.AddState(new Move(this));
        }
Example #5
0
        protected override double CountFitness(Team team, IList <IMovingAgent> sheep)
        {
            var world = new SimulationWorld(team, sheep, false);

            world.Work(parameters.TurnsOfHerding);
            var fitness = world.Sheep.Select(x => x.Position).SumOfDistancesFromCenter();

            return(fitness);
        }
Example #6
0
        private void InitSimWorld(Type simWorldType)
        {
            _simWorld = (SimulationWorld)Activator.CreateInstance(simWorldType);
            _simWorld.InitSimulationWorld();
            _simWorld.SetDebugDraw(_debugDraw);
            SetView();

            // Start the simulation timer.
            simTimer.Start();
        }
Example #7
0
        private void TerminateSimulation()
        {
            if (null == _simWorld)
            {   // Do nothing.
                return;
            }

            // Stop simulation.
            simTimer.Stop();
            _simWorld = null;
            // TODO: Explicit disposal?
        }
Example #8
0
        private static void Cmd_SimLoadFromMemory(string fileNameToReadFrom)
        {
            if (s_commandInstance._ongoingCmdOperation != null && s_commandInstance._ongoingCmdOperation.IsRunning)
            {
                Log.Warning("Cannot load sim because another operation is ongoing");
                return;
            }

            SimulationWorld newWorld = s_commandInstance._simulationWorldSystem.CreateNewReplacementWorld();

            s_commandInstance._ongoingCmdOperation = new LoadSimulationFromMemoryOperation(newWorld);
            Cmd_PostSimLoad_Internal(locationTxt: "memory", newWorld);
        }
        void IWorldOwner.OnEndEntitiesInjectionFromGameObjectConversion()
        {
            _ongoingInjections--;

            if (_ongoingInjections == 0)
            {
                var changeDetectionBegin = SimulationWorld.GetExistingSystem <ChangeDetectionSystemBegin>();
                if (changeDetectionBegin != null)
                {
                    changeDetectionBegin.ResetSample();
                }
            }
        }
        protected override void OnDestroy()
        {
            base.OnDestroy();

            if (SimulationWorld.IsCreated)
            {
                SimulationWorld.Dispose();
            }

            SimWorldAccessor.SimWorld      = null;
            SimWorldAccessor.EntityManager = default;
            SimWorldAccessor.SomeSimSystem = null;

            SimulationWorld = null;
        }
Example #11
0
        private static void Cmd_PostSimLoad_Internal(string locationTxt, SimulationWorld newWorld)
        {
            s_commandInstance._tickSystem.PauseSimulation("load_cmd");

            s_commandInstance._ongoingCmdOperation.OnTerminateCallback = (op) => s_commandInstance._tickSystem.UnpauseSimulation("load_cmd");

            s_commandInstance._ongoingCmdOperation.OnSucceedCallback = (op) =>
            {
                DebugScreenMessage.DisplayMessage($"Loaded sim from {locationTxt}. {op.Message}");

                s_commandInstance._simulationWorldSystem.RequestReplaceSimWorld(newWorld);
            };

            s_commandInstance._ongoingCmdOperation.OnFailCallback = (op) =>
            {
                DebugScreenMessage.DisplayMessage($"Could not load sim from {locationTxt}. {op.Message}");
            };

            s_commandInstance._ongoingCmdOperation.Execute();
        }
Example #12
0
        private static void Cmd_SimLoadFromFile(string fileName)
        {
            if (s_commandInstance._ongoingCmdOperation != null && s_commandInstance._ongoingCmdOperation.IsRunning)
            {
                Log.Warning("Cannot load sim because another operation is ongoing");
                return;
            }

            if (!fileName.EndsWith(".txt"))
            {
                fileName += ".txt";
            }

            SimulationWorld newWorld = s_commandInstance._simulationWorldSystem.CreateNewReplacementWorld();

            s_commandInstance._ongoingCmdOperation = new LoadSimulationFromDiskOperation(
                $"{Application.persistentDataPath}/{fileName}",
                newWorld);

            Cmd_PostSimLoad_Internal(locationTxt: $"file {fileName}", newWorld);
        }
        void IWorldOwner.OnBeginEntitiesInjectionFromGameObjectConversion(List <Scene> comingFromScenes)
        {
            foreach (var scene in comingFromScenes)
            {
                if (!_incomingEntityInjections.Contains(scene.name))
                {
                    Log.Error($"Unexpected entities coming from {scene.name} are being injected into the simulation. " +
                              $"This should not happen");
                }
            }

            if (_ongoingInjections == 0)
            {
                var changeDetectionEnd = SimulationWorld.GetExistingSystem <ChangeDetectionSystemEnd>();
                if (changeDetectionEnd != null)
                {
                    changeDetectionEnd.ForceEndSample();
                }
            }
            _ongoingInjections++;
        }
Example #14
0
        public MapCity(SimulationWorld world, ConfigurationSection sect)
        {
            City city = new City(world);
            city.Parse(sect);

            LinkableCity = city.LinkableCityName;
            FarmCount = city.FarmLandCount;
            ProblemEnvironment = city.ProblemEnvironment;
            ProblemEducation = city.ProblemEducation;
            ProblemDisease = city.ProblemDisease;
            ProblemChild = city.ProblemChild;
            ProblemGender = city.ProblemGender;
            ProblemHunger = city.ProblemHunger;
            ProblemMaternal = city.ProblemMaternal;

            Name = city.Name;
            Size = city.Size;

            Longitude = city.Longitude;
            Latitude = city.Latitude;

            StartUp = city.StartUp;
        }
Example #15
0
        public MapCity(SimulationWorld world, ConfigurationSection sect)
        {
            City city = new City(world);

            city.Parse(sect);

            LinkableCity       = city.LinkableCityName;
            FarmCount          = city.FarmLandCount;
            ProblemEnvironment = city.ProblemEnvironment;
            ProblemEducation   = city.ProblemEducation;
            ProblemDisease     = city.ProblemDisease;
            ProblemChild       = city.ProblemChild;
            ProblemGender      = city.ProblemGender;
            ProblemHunger      = city.ProblemHunger;
            ProblemMaternal    = city.ProblemMaternal;

            Name = city.Name;
            Size = city.Size;

            Longitude = city.Longitude;
            Latitude  = city.Latitude;

            StartUp = city.StartUp;
        }
Example #16
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SimulationWorld sim = new SimulationWorld();

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string dir = folderBrowserDialog1.SelectedPath;

                Configuration config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "cities.xml")));

                foreach (KeyValuePair<string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;
                    MapCity city = new MapCity(sim, sect);

                    MapObject obj = new MapObject();
                    obj.Longitude = city.Longitude;
                    obj.Latitude = city.Latitude;
                    obj.Tag = city;
                    obj.Type = ObjectType.City;
                    obj.StringDisplay = city.Name;
                    obj.SectionName = sect.Name;
                    
                    cityTable.Add(sect.Name, city);

                    objectList.Add(obj);
                }

                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "sceneObjects.xml")));
                foreach (KeyValuePair<string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;

                    MapSceneObject sceObj = new MapSceneObject(sect);
                    MapObject obj = new MapObject();
                    obj.Longitude = sect.GetSingle("Longitude");
                    obj.Latitude = sect.GetSingle("Latitude");
                    obj.Type = ObjectType.Scene;
                    obj.Tag = sceObj;
                    obj.StringDisplay = sceObj.Model;
                    obj.Radius = sceObj.Radius;
                    obj.SectionName = sect.Name;
                    objectList.Add(obj);
                }

                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "resources.xml")));

                foreach (KeyValuePair<string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;

                    MapResource res = new MapResource(sim, sect);

                    MapObject obj = new MapObject();

                    obj.Longitude = res.Longitude;
                    obj.Latitude = res.Latitude;
                    obj.Tag = res;
                    if (res.Type == NaturalResourceType.Wood)
                    {
                        obj.Type = ObjectType.ResWood;
                    }
                    else if (res.Type == NaturalResourceType.Petro)
                    {
                        obj.Type = ObjectType.ResOil;
                    }
                    obj.StringDisplay = (obj.Type == ObjectType.ResOil ? "O" : "W") + res.Amount.ToString();
                    obj.Radius = res.Radius;
                    obj.SectionName = sect.Name;
                    objectList.Add(obj);
                }

                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "soundObjects.xml")));
                foreach (KeyValuePair<string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;

                    MapSoundObject sndObj = new MapSoundObject(sect);

                    MapObject obj = new MapObject();
                    obj.Longitude = sect.GetSingle("Longitude");
                    obj.Latitude = sect.GetSingle("Latitude");

                    obj.Type = ObjectType.Sound;

                    obj.Tag = sndObj;
                    obj.StringDisplay = sndObj.SFXName;
                    obj.Radius = sndObj.Radius;
                    obj.SectionName = sect.Name;
                    objectList.Add(obj);
                  
                    
                }


                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "soundEffect.xml")));
                foreach (KeyValuePair<string, ConfigurationSection> s in config)
                {
                    comboBox2.Items.Add(s.Key);
                }
            }
            pictureBox1.Refresh();
        }
Example #17
0
        public MapResource(SimulationWorld world, ConfigurationSection sect)
        {
            string type = sect["Type"].ToLowerInvariant();

            switch (type)
            {
                case "petro":
                    OilField oil = new OilField(world);
                    oil.Parse(sect);

                    Longitude = oil.Longitude;
                    Latitude = oil.Latitude;

                    Type = NaturalResourceType.Petro;
                    Amount = oil.CurrentAmount;

                    break;
                case "wood":
                    Forest fores = new Forest(world);
                    fores.Parse(sect);

                    Longitude = fores.Longitude;
                    Latitude = fores.Latitude;

                    Type = NaturalResourceType.Wood;
                    Amount = fores.CurrentAmount;
                    Radius = fores.Radius;
                    break;
            }

        }
Example #18
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SimulationWorld sim = new SimulationWorld();

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string dir = folderBrowserDialog1.SelectedPath;

                Configuration config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "cities.xml")));

                foreach (KeyValuePair <string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;
                    MapCity city = new MapCity(sim, sect);

                    MapObject obj = new MapObject();
                    obj.Longitude     = city.Longitude;
                    obj.Latitude      = city.Latitude;
                    obj.Tag           = city;
                    obj.Type          = ObjectType.City;
                    obj.StringDisplay = city.Name;
                    obj.SectionName   = sect.Name;

                    cityTable.Add(sect.Name, city);

                    objectList.Add(obj);
                }

                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "sceneObjects.xml")));
                foreach (KeyValuePair <string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;

                    MapSceneObject sceObj = new MapSceneObject(sect);
                    MapObject      obj    = new MapObject();
                    obj.Longitude     = sect.GetSingle("Longitude");
                    obj.Latitude      = sect.GetSingle("Latitude");
                    obj.Type          = ObjectType.Scene;
                    obj.Tag           = sceObj;
                    obj.StringDisplay = sceObj.Model;
                    obj.Radius        = sceObj.Radius;
                    obj.SectionName   = sect.Name;
                    objectList.Add(obj);
                }

                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "resources.xml")));

                foreach (KeyValuePair <string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;

                    MapResource res = new MapResource(sim, sect);

                    MapObject obj = new MapObject();

                    obj.Longitude = res.Longitude;
                    obj.Latitude  = res.Latitude;
                    obj.Tag       = res;
                    if (res.Type == NaturalResourceType.Wood)
                    {
                        obj.Type = ObjectType.ResWood;
                    }
                    else if (res.Type == NaturalResourceType.Petro)
                    {
                        obj.Type = ObjectType.ResOil;
                    }
                    obj.StringDisplay = (obj.Type == ObjectType.ResOil ? "O" : "W") + res.Amount.ToString();
                    obj.Radius        = res.Radius;
                    obj.SectionName   = sect.Name;
                    objectList.Add(obj);
                }

                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "soundObjects.xml")));
                foreach (KeyValuePair <string, ConfigurationSection> s in config)
                {
                    ConfigurationSection sect = s.Value;

                    MapSoundObject sndObj = new MapSoundObject(sect);

                    MapObject obj = new MapObject();
                    obj.Longitude = sect.GetSingle("Longitude");
                    obj.Latitude  = sect.GetSingle("Latitude");

                    obj.Type = ObjectType.Sound;

                    obj.Tag           = sndObj;
                    obj.StringDisplay = sndObj.SFXName;
                    obj.Radius        = sndObj.Radius;
                    obj.SectionName   = sect.Name;
                    objectList.Add(obj);
                }


                config = ConfigurationManager.Instance.CreateInstance(new FileLocation(Path.Combine(dir, "soundEffect.xml")));
                foreach (KeyValuePair <string, ConfigurationSection> s in config)
                {
                    comboBox2.Items.Add(s.Key);
                }
            }
            pictureBox1.Refresh();
        }
Example #19
0
 /// <summary>
 /// Creates a new simulation context
 /// </summary>
 /// <param name="world">The world of the context</param>
 public SimulationContext(SimulationWorld world)
 {
     this.World = world;
 }
 public void RequestReplaceSimWorld(SimulationWorld newWorld)
 {
     _replaceWorld = newWorld;
 }