예제 #1
0
        /// <summary>
        /// Sets the customised number of workers for a given prefab.
        /// If a record doesn't already exist, a new one will be created.
        /// </summary>
        /// <param name="prefab">The prefab (BuildingInfo) to set</param>
        /// <param name="workers">The updated worker count</param>
        public static void SetWorker(BuildingInfo prefab, int workers)
        {
            // Update or add entry to configuration file cache.
            if (DataStore.workerCache.ContainsKey(prefab.name))
            {
                // Prefab already has a record; update.
                DataStore.workerCache[prefab.name] = workers;
            }
            else
            {
                // Prefab doesn't already have a record; create.
                DataStore.workerCache.Add(prefab.name, workers);
            }

            // Save the updated configuration file.
            XMLUtilsWG.WriteToXML();

            // Get current building hash (for updating prefab dictionary).
            var prefabHash = prefab.gameObject.GetHashCode();

            // Calculate employment breakdown.
            int[] array = CommercialBuildingAIMod.GetArray(prefab, (int)prefab.GetClassLevel());
            PrefabEmployStruct output = new PrefabEmployStruct();

            AI_Utils.CalculateprefabWorkerVisit(prefab.GetWidth(), prefab.GetLength(), ref prefab, 4, ref array, out output);

            // Update entry in 'live' settings.
            if (DataStore.prefabWorkerVisit.ContainsKey(prefabHash))
            {
                // Prefab already has a record; update.
                DataStore.prefabWorkerVisit[prefabHash] = output;
            }
            else
            {
                // Prefab doesn't already have a record; create.
                DataStore.prefabWorkerVisit.Add(prefabHash, output);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="width"></param>
        /// <param name="length"></param>
        /// <param name="item"></param>
        /// <param name="minWorkers"></param>
        /// <param name="array"></param>
        /// <param name="output"></param>
        internal static void CalculateprefabWorkerVisit(int width, int length, ref BuildingInfo item, int minWorkers, ref int[] array, out PrefabEmployStruct output)
        {
            // Prefabs are tied to a level

            int value  = 0;
            int num    = array[DataStore.PEOPLE];
            int level0 = array[DataStore.WORK_LVL0];
            int level1 = array[DataStore.WORK_LVL1];
            int level2 = array[DataStore.WORK_LVL2];
            int level3 = array[DataStore.WORK_LVL3];
            int num2   = level0 + level1 + level2 + level3;

            if (num > 0 && num2 > 0)
            {
                Vector3 v          = item.m_size;
                int     floorSpace = CalcBase(width, length, ref array, v);
                int     floorCount = Mathf.Max(1, Mathf.FloorToInt(v.y / array[DataStore.LEVEL_HEIGHT])) + array[DataStore.DENSIFICATION];
                value = (floorSpace * floorCount) / array[DataStore.PEOPLE];
                int outValue = 0;

                if ((array[DataStore.CALC_METHOD] == 0)) // Plot only will ignore any over ride or bonus
                {
                    // Check over ride
                    string name = item.gameObject.name;
                    if (DataStore.workerCache.TryGetValue(name, out outValue))
                    {
                        value = outValue;
                    }
                    else if (DataStore.bonusWorkerCache.TryGetValue(name, out outValue))
                    {
                        value = value + outValue;
                        DataStore.workerCache.Add(name, value);
                        DataStore.bonusWorkerCache.Remove(name);
                    }
                    else if (DataStore.printEmploymentNames)
                    {
                        try
                        {
                            DataStore.workerPrintOutCache.Add(item.gameObject.name, value);
                        }
                        catch (ArgumentException)
                        {
                            // Don't care
                        }
                    }
                }

                num = Mathf.Max(minWorkers, value);

                output.level3 = (num * level3) / num2;
                output.level2 = (num * level2) / num2;
                output.level1 = (num * level1) / num2;
                output.level0 = Mathf.Max(0, num - output.level3 - output.level2 - output.level1);  // Whatever is left
            }
            else
            {
                output.level0 = output.level1 = output.level2 = output.level3 = 1;  // Allocate 1 for every level, to stop div by 0
            }

            // Set the visitors here since we're calculating
            if (num != 0)
            {
                value = Mathf.Max(200, width * length * array[DataStore.VISIT]) / 100;
            }
            output.visitors = value;
        } // end calculateprefabWorkerVisit