/*
         * Handles creation and removal of animals
         *
         * Note: Just because a animal has been removed visually, it does not mean
         * it is removed as far as the game is concerned. The animal is only truly removed
         * when the frame covers the animal's id, and that's when we will remove the
         * animal from our records.
         */
        public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
        {
            if (_terminated) return;

            if (!_helper.AnimalMonitorSpinnable) return;

            if (!_settings.Enable._AnimalMonitor) return;

            try
            {
                if (!_initialized)
                {
                    _data = Data.Instance;

                    _mapping = new AnimalPrefabMapping();

                    _buildingsAnimals = new Dictionary<ushort, HashSet<ushort>>();

                    _paused = false;

                    _instance = Singleton<CitizenManager>.instance;
                    _capacity = _instance.m_instances.m_buffer.Length;

                    _id = (ushort)_capacity;

                    for (int i = 0; i < _capacity; i++)
                        UpdateAnimal((ushort)i);

                    _initialized = true;
                    _helper.AnimalMonitorSpun = true;
                    _helper.AnimalMonitor = this;

                    _helper.NotifyPlayer("Animal monitor initialized");
                }
                else if (_data.BuildingsUpdated.Length > 0)
                {
                    _data._AnimalsUpdated.Clear();
                    _data._AnimalsRemoved.Clear();

                    foreach (ushort building in _data._BuildingsUpdated)
                    {
                        ushort id = Singleton<BuildingManager>.instance.m_buildings.m_buffer[building].m_targetCitizens;

                        while (id != 0)
                        {
                            if (UpdateAnimal(id))
                            {
                                _data._AnimalsUpdated.Add(id);
                                AddBuildingsAnimal(building, id);
                            }
                            else
                            {
                                _data._AnimalsRemoved.Add(id);

                                if (_data._Animals.Contains(id))
                                    RemoveAnimal(id);
                            }

                            id = _instance.m_instances.m_buffer[(int)id].m_nextTargetInstance;
                        }

                        CheckBuildingsAnimals(building);
                    }

                    foreach (ushort building in _data._BuildingsRemoved)
                        RemoveBuildingsAnimals(building);
                }

                OutputDebugLog();
            }
            catch (Exception e)
            {
                string error = "Animal monitor failed to initialize\r\n";
                error += String.Format("Error: {0}\r\n", e.Message);
                error += "\r\n";
                error += "==== STACK TRACE ====\r\n";
                error += e.StackTrace;

                _helper.Log(error);

                _terminated = true;
            }

            base.OnUpdate(realTimeDelta, simulationTimeDelta);
        }
Esempio n. 2
0
        /*
         * Handles creation and removal of animals
         *
         * Note: Just because a animal has been removed visually, it does not mean
         * it is removed as far as the game is concerned. The animal is only truly removed
         * when the frame covers the animal's id, and that's when we will remove the
         * animal from our records.
         */
        public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
        {
            if (_terminated)
            {
                return;
            }

            if (!_helper.AnimalMonitorSpinnable)
            {
                return;
            }

            if (!_settings.Enable._AnimalMonitor)
            {
                return;
            }

            try
            {
                if (!_initialized)
                {
                    _data = Data.Instance;

                    _mapping = new AnimalPrefabMapping();

                    _buildingsAnimals = new Dictionary <ushort, HashSet <ushort> >();

                    _paused = false;

                    _instance = Singleton <CitizenManager> .instance;
                    _capacity = _instance.m_instances.m_buffer.Length;

                    _id = (ushort)_capacity;

                    for (int i = 0; i < _capacity; i++)
                    {
                        UpdateAnimal((ushort)i);
                    }

                    _initialized = true;
                    _helper.AnimalMonitorSpun = true;
                    _helper.AnimalMonitor     = this;

                    _helper.Log("Animal monitor initialized");
                }
                else if (_data.BuildingsUpdated.Length > 0)
                {
                    _data._AnimalsUpdated.Clear();
                    _data._AnimalsRemoved.Clear();

                    foreach (ushort building in _data._BuildingsUpdated)
                    {
                        ushort id = Singleton <BuildingManager> .instance.m_buildings.m_buffer[building].m_targetCitizens;

                        while (id != 0)
                        {
                            if (UpdateAnimal(id))
                            {
                                _data._AnimalsUpdated.Add(id);
                                AddBuildingsAnimal(building, id);
                            }
                            else
                            {
                                _data._AnimalsRemoved.Add(id);

                                if (_data._Animals.Contains(id))
                                {
                                    RemoveAnimal(id);
                                }
                            }

                            id = _instance.m_instances.m_buffer[(int)id].m_nextTargetInstance;
                        }

                        CheckBuildingsAnimals(building);
                    }

                    foreach (ushort building in _data._BuildingsRemoved)
                    {
                        RemoveBuildingsAnimals(building);
                    }
                }

                OutputDebugLog();
            }
            catch (Exception e)
            {
                string error = "Animal monitor failed to initialize\r\n";
                error += String.Format("Error: {0}\r\n", e.Message);
                error += "\r\n";
                error += "==== STACK TRACE ====\r\n";
                error += e.StackTrace;

                _helper.Log(error);

                _terminated = true;
            }

            base.OnUpdate(realTimeDelta, simulationTimeDelta);
        }