//This is called by the settings panel. It will serialize any new local settings the player sets in game.
        public static void SaveLocal(PloppableRICODefinition.Building newBuildingData)
        {
            Debug.Log("SaveLocal");

            if (File.Exists("LocalRICOSettings.xml") && newBuildingData != null)
            {
                PloppableRICODefinition localSettings = null;
                var newlocalSettings = new PloppableRICODefinition();

                var xmlSerializer = new XmlSerializer(typeof(PloppableRICODefinition));

                using (StreamReader streamReader = new System.IO.StreamReader("LocalRICOSettings.xml"))
                {
                    localSettings = xmlSerializer.Deserialize(streamReader) as PloppableRICODefinition;
                }

                foreach (var buildingDef in localSettings.Buildings)
                {
                    if (buildingDef.name != newBuildingData.name)
                    {
                        newlocalSettings.Buildings.Add(buildingDef);
                    }
                }

                //newBuildingData.name = newBuildingData.name;
                newlocalSettings.Buildings.Add(newBuildingData);

                using (TextWriter writer = new StreamWriter("LocalRICOSettings.xml"))
                {
                    xmlSerializer.Serialize(writer, newlocalSettings);
                }
            }
        }
        //Load local RICO settings.
        public void LocalSettings()
        {
            try
            {
                PloppableRICODefinition localSettings = null;

                var xmlSerializer = new XmlSerializer(typeof(PloppableRICODefinition));

                using (StreamReader streamReader = new System.IO.StreamReader("LocalRICOSettings.xml"))
                {
                    localSettings = xmlSerializer.Deserialize(streamReader) as PloppableRICODefinition;
                }

                foreach (var buildingDef in localSettings.Buildings)
                {
                    if (PrefabCollection <BuildingInfo> .FindLoaded(buildingDef.name) != null)
                    {
                        var buildingPrefab = PrefabCollection <BuildingInfo> .FindLoaded(buildingDef.name);

                        if (buildingPrefab != null)
                        {
                            //Add local RICO settings to dictionary
                            var local = new PloppableRICODefinition.Building();
                            local = buildingDef;
                            xmlData[buildingPrefab].local    = local;
                            xmlData[buildingPrefab].hasLocal = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
        }
        public static void CalculateWorkplaceCount(PloppableRICODefinition.Building ricoData, IWorkplaceLevelCalculator ai, Randomizer r, int width, int length, out int level0, out int level1, out int level2, out int level3)
        {
            SetWorkplaceLevels(out level0, out level1, out level2, out level3, 0, 0, 0, 0);
            PloppableRICODefinition.Building rc = ricoData;

            if (rc == null)
            {
                WorkplaceAIHelper.SetWorkplaceLevels(out level0, out level1, out level2, out level3, 10, 20, 30, 40);
            }
            else
            // reality mod is running and the xml file says ignore-reality="false"
            if (rc.useReality)
            {
                ai.CalculateBaseLevels(r, width, length, out level0, out level1, out level2, out level3);
            }
            else
            {
                if (rc.workplaceCount > 0)
                {
                    ai.CalculateLevels(r, width, length, out level0, out level1, out level2, out level3);
                }
                if (rc.workplaceDetailsEnabled)
                {
                    // this adds to the results of the usual workplaces calculation
                    WorkplaceAIHelper.SetWorkplaceLevels(out level0, out level1, out level2, out level3, rc.uneducated + level0, rc.educated + level1, rc.wellEducated + level2, rc.highEducated + level3);
                }
                // Comment that out and uncomment the following to ignore "workplaces"
                // and just use the details setting
                // WorkplaceAIHelper.SetWorkplaceLevels(out level0, out level1, out level2, out level3, m_ricoData);
            }
        }
Example #4
0
        public void SelectionChanged(BuildingData buildingData)
        {
            //When dropdowns are updated, this disables the event logic
            disableEvents = true;

            NoSettings();

            //If selected asset has local settings, update option UI elements with those settings.
            if (buildingData.hasLocal)
            {
                UpdateElements(buildingData.local.service);
                UpdateValues(buildingData.local);

                currentSelection = buildingData.local;
                disableEvents    = false;
                return;
            }
            else if (buildingData.hasAuthor)
            {
                UpdateElements(buildingData.author.service);
                currentSelection = buildingData.author;
                UpdateValues(buildingData.author);
                disableEvents = false;
                return;
            }
            else if (buildingData.hasMod)
            {
                UpdateElements(buildingData.mod.service);
                currentSelection = buildingData.mod;
                disableEvents    = false;
                return;
            }
            else
            {
                NoSettings();
            }

            disableEvents = false;
        }
        //Load RICO Settings from asset folders
        public void AssetSettings()
        {
            var ricoDefParseErrors = new HashSet <string>();
            var checkedPaths       = new List <string>();

            for (uint i = 0; i < PrefabCollection <BuildingInfo> .LoadedCount(); i++)
            {
                var prefab = PrefabCollection <BuildingInfo> .GetLoaded(i);

                if (prefab == null)
                {
                    continue;
                }

                // search for PloppableRICODefinition.xml
                var asset = PackageManager.FindAssetByName(prefab.name);
                if (asset == null || asset.package == null)
                {
                    continue;
                }

                var crpPath = asset.package.packagePath;
                if (crpPath == null)
                {
                    continue;
                }

                var ricoDefPath = Path.Combine(Path.GetDirectoryName(crpPath), "PloppableRICODefinition.xml");

                // skip files which were already parsed
                if (checkedPaths.Contains(ricoDefPath))
                {
                    continue;
                }
                checkedPaths.Add(ricoDefPath);

                if (!File.Exists(ricoDefPath))
                {
                    continue;
                }

                PloppableRICODefinition ricoDef = null;

                var xmlSerializer = new XmlSerializer(typeof(PloppableRICODefinition));
                try
                {
                    using (StreamReader streamReader = new System.IO.StreamReader(ricoDefPath))
                    {
                        ricoDef = xmlSerializer.Deserialize(streamReader) as PloppableRICODefinition;
                    }
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                    ricoDefParseErrors.Add(asset.package.packageName + " - " + e.Message);
                    continue;
                }

                if (ricoDef == null || ricoDef.Buildings == null || ricoDef.Buildings.Count == 0)
                {
                    ricoDefParseErrors.Add(asset.package.packageName + " - ricoDef is null or empty.");
                    continue;
                }

                foreach (var buildingDef in ricoDef.Buildings)
                {
                    if (buildingDef == null || buildingDef.name == null)
                    {
                        ricoDefParseErrors.Add(asset.package.packageName + " - Building name missing.");
                        continue;
                    }

                    var buildingPrefab = FindPrefab(buildingDef.name, asset.package.packageName);

                    if (buildingPrefab == null)
                    {
                        ricoDefParseErrors.Add(asset.package.packageName + " - Building with name " + buildingDef.name + " not loaded.");
                        continue;
                    }

                    try
                    {
                        UnityEngine.Debug.Log($"data index: {buildingPrefab.m_prefabDataIndex}");

                        if (buildingPrefab != null)
                        {
                            //Add asset author settings to dictionary.
                            var author = new PloppableRICODefinition.Building();
                            author = buildingDef;
                            xmlData[buildingPrefab].author    = author;
                            xmlData[buildingPrefab].hasAuthor = true;
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);
                        ricoDefParseErrors.Add(asset.package.packageName + " - " + e.Message);
                    }
                }
            }

            if (ricoDefParseErrors.Count > 0)
            {
                var errorMessage = "Error while parsing Ploppable RICO definition file(s). Contact the author of the assets. \n"
                                   + "List of errors:\n";
                foreach (var error in ricoDefParseErrors)
                {
                    errorMessage += error + '\n';
                }

                UIView.library.ShowModal <ExceptionPanel>("ExceptionPanel").SetMessage("Ploppable RICO", errorMessage, true);
            }
        }
Example #6
0
        public void UpdateValues(PloppableRICODefinition.Building buildingData)
        {
            //Updates the values in the RICO options panel to match the selected building.

            if (buildingData.service == "residential")
            {
                manualHomesEnabled.isChecked = buildingData.manualHomeEnabled;
                service.selectedIndex        = 1;
                if (buildingData.subService == "high")
                {
                    subService.selectedIndex = 0;
                }
                else
                {
                    subService.selectedIndex = 1;
                }
            }

            else if (buildingData.service == "industrial")
            {
                if (buildingData.manualWorkerEnabled)
                {
                    manualWorkersEnabled.isChecked = true;
                }
                service.selectedIndex = 2;
                //subService.items = IndustrialSub;
            }

            else if (buildingData.service == "office")
            {
                manualWorkersEnabled.isChecked = buildingData.manualWorkerEnabled;
                Debug.Log(buildingData.manualWorkerEnabled);
                service.selectedIndex    = 3;
                subService.selectedIndex = 0;
            }

            else if (buildingData.service == "commercial")
            {
                if (buildingData.manualWorkerEnabled)
                {
                    manualWorkersEnabled.isChecked = true;
                }
                service.selectedIndex = 4;
                //subService.items = ComSub;
            }

            else if (buildingData.service == "extractor")
            {
                if (buildingData.manualWorkerEnabled)
                {
                    manualWorkersEnabled.isChecked = true;
                }
                service.selectedIndex = 5;
                subService.items      = ExtractorSub;
            }

            if (buildingData.UICategory == "reslow")
            {
                uiCategory.selectedIndex = 0;
            }
            else if (buildingData.UICategory == "reshigh")
            {
                uiCategory.selectedIndex = 1;
            }
            else if (buildingData.UICategory == "comlow")
            {
                uiCategory.selectedIndex = 2;
            }
            else if (buildingData.UICategory == "comhigh")
            {
                uiCategory.selectedIndex = 3;
            }
            else if (buildingData.UICategory == "office")
            {
                uiCategory.selectedIndex = 4;
            }
            else if (buildingData.UICategory == "industrial")
            {
                uiCategory.selectedIndex = 5;
            }
            else if (buildingData.UICategory == "farming")
            {
                uiCategory.selectedIndex = 6;
            }
            else if (buildingData.UICategory == "oil")
            {
                uiCategory.selectedIndex = 7;
            }
            else if (buildingData.UICategory == "forest")
            {
                uiCategory.selectedIndex = 8;
            }
            else if (buildingData.UICategory == "ore")
            {
                uiCategory.selectedIndex = 9;
            }
            else if (buildingData.UICategory == "leisure")
            {
                uiCategory.selectedIndex = 10;
            }
            else if (buildingData.UICategory == "tourist")
            {
                uiCategory.selectedIndex = 11;
            }

            level.selectedIndex = (buildingData.level - 1);
            manual.text         = buildingData.workplaceCount.ToString();
            homes.text          = buildingData.homeCount.ToString();
            construction.text   = buildingData.constructionCost.ToString();

            ricoEnabled.isChecked             = buildingData.ricoEnabled;
            constructionCostEnabled.isChecked = buildingData.constructionCostEnabled;
        }
        public void ConvertPrefab(PloppableRICODefinition.Building buildingData, string name)
        {
            var prefab = PrefabCollection <BuildingInfo> .FindLoaded(name);

            if (prefab != null)
            {
                if (buildingData.service == "residential")
                {
                    var ai = prefab.gameObject.AddComponent <PloppableResidential>();
                    ai.m_ricoData         = buildingData;
                    ai.m_constructionCost = buildingData.constructionCost;
                    ai.m_homeCount        = buildingData.homeCount;
                    InitializePrefab(prefab, ai, Util.ucFirst(buildingData.subService) + " Residential - Level" + buildingData.level);
                }
                else if (buildingData.service == "office")
                {
                    var ai = prefab.gameObject.AddComponent <PloppableOffice>();
                    ai.m_ricoData         = buildingData;
                    ai.m_workplaceCount   = buildingData.workplaceCount;
                    ai.m_constructionCost = buildingData.constructionCost;
                    InitializePrefab(prefab, ai, "Office - Level" + buildingData.level);
                }
                else if (buildingData.service == "industrial")
                {
                    var ai = prefab.gameObject.AddComponent <PloppableIndustrial>();
                    ai.m_ricoData         = buildingData;
                    ai.m_workplaceCount   = buildingData.workplaceCount;
                    ai.m_constructionCost = buildingData.constructionCost;
                    ai.m_pollutionEnabled = buildingData.pollutionEnabled;

                    if (Util.industryServices.Contains(buildingData.subService))
                    {
                        InitializePrefab(prefab, ai, Util.ucFirst(buildingData.subService) + " - Processing");
                    }
                    else
                    {
                        InitializePrefab(prefab, ai, "Industrial - Level" + buildingData.level);
                    }
                }
                else if (buildingData.service == "extractor")
                {
                    var ai = prefab.gameObject.AddComponent <PloppableExtractor>();
                    ai.m_ricoData         = buildingData;
                    ai.m_workplaceCount   = buildingData.workplaceCount;
                    ai.m_constructionCost = buildingData.constructionCost;
                    ai.m_pollutionEnabled = buildingData.pollutionEnabled;

                    if (Util.industryServices.Contains(buildingData.subService))
                    {
                        InitializePrefab(prefab, ai, Util.ucFirst(buildingData.subService) + " - Extractor");
                    }
                }

                else if (buildingData.service == "commercial")
                {
                    string itemClass = "";
                    var    ai        = prefab.gameObject.AddComponent <PloppableCommercial>();
                    ai.m_ricoData         = buildingData;
                    ai.m_workplaceCount   = buildingData.workplaceCount;
                    ai.m_constructionCost = buildingData.constructionCost;

                    // high and low
                    if (Util.vanillaCommercialServices.Contains(buildingData.subService))
                    {
                        itemClass = Util.ucFirst(buildingData.subService) + " Commercial - Level" + buildingData.level;
                    }
                    else
                    if (Util.isADinstalled())
                    {
                        if (buildingData.subService == "tourist")
                        {
                            itemClass = "Tourist Commercial - Land";
                        }
                        else if (buildingData.subService == "leisure")
                        {
                            itemClass = "Leisure Commercial";
                        }
                        else
                        {
                            itemClass = "High Commercial - Level" + buildingData.level;
                        }
                    }
                    else
                    {
                        itemClass = "High Commercial - Level" + buildingData.level;
                    }

                    InitializePrefab(prefab, ai, itemClass);
                }
            }
        }
 public static void SetWorkplaceLevels(out int level0, out int level1, out int level2, out int level3, PloppableRICODefinition.Building ricoData)
 {
     SetWorkplaceLevels(out level0, out level1, out level2, out level3, new int[] { ricoData.uneducated, ricoData.educated, ricoData.wellEducated, ricoData.highEducated });
 }
        private void SetupControls()
        {
            save      = UIUtils.CreateButton(this);
            save.text = "Save";

            addLocal      = UIUtils.CreateButton(this);
            addLocal.text = "Add Local";

            addLocal.eventClick += (c, p) =>
            {
                if (currentSelection.hasLocal == false)
                {
                    var newlocal = new PloppableRICODefinition.Building();
                    currentSelection.hasLocal   = true;
                    currentSelection.local      = newlocal;
                    currentSelection.local.name = currentSelection.name;
                }
            };

            removeLocal      = UIUtils.CreateButton(this);
            removeLocal.text = "Remove Local";

            save.eventClick += (c, p) =>
            {
                //Serialize the new RICO settings.
                //XMLManager.SaveLocal(currentSelection.local);
                RICOSettingsPanel.instance.Save();

                if (File.Exists("LocalRICOSettings.xml") && currentSelection.local != null)
                {
                    PloppableRICODefinition localSettings;
                    var newlocalSettings = new PloppableRICODefinition();

                    var xmlSerializer = new XmlSerializer(typeof(PloppableRICODefinition));

                    using (StreamReader streamReader = new System.IO.StreamReader("LocalRICOSettings.xml"))
                    {
                        localSettings = xmlSerializer.Deserialize(streamReader) as PloppableRICODefinition;
                    }

                    foreach (var buildingDef in localSettings.Buildings)
                    {
                        if (buildingDef.name != currentSelection.local.name)
                        {
                            newlocalSettings.Buildings.Add(buildingDef);
                        }
                    }

                    //newBuildingData.name = newBuildingData.name;
                    newlocalSettings.Buildings.Add(currentSelection.local);

                    using (TextWriter writer = new StreamWriter("LocalRICOSettings.xml"))
                    {
                        xmlSerializer.Serialize(writer, newlocalSettings);
                    }
                }
            };

            reset      = UIUtils.CreateButton(this);
            reset.text = "Reset";
        }