using System; using System.Collections.Generic; using System.Globalization; using CIG3.ExtensionMethods; using CIGEnums; using SUISS.Core; using SUISS.Scheduling; using SUISSEngine; using UnityEngine; public class CIGSceneryManager : MonoBehaviour { private void OnGridDeserialized() { if (++this.initializationCounter == 2) { SingletonMonobehaviour.Instance.ExecuteNextFrame(base.gameObject, delegate() { this.PlaceInitialScenery(); }); } } private void OnDeserialized() { if (++this.initializationCounter == 2) { SingletonMonobehaviour.Instance.ExecuteNextFrame(base.gameObject, delegate() { this.PlaceInitialScenery(); }); } } private DateTime SceneryTimestamp { get { return this._sceneryTimestamp; } set { this._sceneryTimestamp = value; this.serializing.Serialize(); } } private bool InitialSceneryPlaced { get { return this._initialSceneryPlaced; } set { this._initialSceneryPlaced = value; this.serializing.Serialize(); } } private void OnSerialize(Dictionary dict) { dict["SceneryTimestamp"] = this._sceneryTimestamp.ToString("yyyyMMddHHmmss"); dict["InitialSceneryIsPlaced"] = this._initialSceneryPlaced; } private void OnDeserialize(Dictionary dict) { if (dict.ContainsKey("SceneryTimestamp")) { CultureInfo invariantCulture = CultureInfo.InvariantCulture; this._sceneryTimestamp = DateTime.ParseExact((string)dict["SceneryTimestamp"], "yyyyMMddHHmmss", invariantCulture); } if (dict.ContainsKey("InitialSceneryIsPlaced")) { this._initialSceneryPlaced = (bool)dict["InitialSceneryIsPlaced"]; } } protected void LoadSceneryPrefabs() { bool[] availableSurfaceTypes = this.grid.AvailableSurfaceTypes; bool flag = false; for (int i = 1; i < availableSurfaceTypes.Length; i++) { if (availableSurfaceTypes[i] && ((SurfaceType)i).IsLand()) { flag = true; break; } } List additionalPrefabs = SingletonMonobehaviour.Instance.AdditionalPrefabs; int count = SingletonMonobehaviour.Instance.AdditionalPrefabs.Count; for (int j = 0; j < count; j++) { GameObject gameObject = additionalPrefabs[j]; CIGScenery component = gameObject.GetComponent(); if (component != null) { if (!component.island.IsValid() || component.island == this.cityIsland.island) { GridTile tile = component.tile; int requiredGridType = tile.requiredGridType; if ((requiredGridType == 10 && flag) || (requiredGridType < availableSurfaceTypes.Length && availableSurfaceTypes[requiredGridType])) { this.sceneryPrefabs.Add(gameObject); } } } } } private void PlaceInitialScenery() { if (this.InitialSceneryPlaced) { return; } this.LoadSceneryPrefabs(); List list = new List(this.expansions.blockSizeU * this.expansions.blockSizeV); foreach (CIGExpansionDefinition cigexpansionDefinition in this.expansions.blockDefinitions) { list.Clear(); int num = cigexpansionDefinition.sceneryItems; for (int j = 0; j < this.expansions.blockSizeV; j++) { for (int k = 0; k < this.expansions.blockSizeU; k++) { GridIndex gridIndex = new GridIndex(k + this.expansions.blockSizeU * cigexpansionDefinition.u, j + this.expansions.blockSizeV * cigexpansionDefinition.v); GridElement gridElement = this.grid[gridIndex]; if (this.HavePrefabForType(gridElement.Type)) { if (gridElement.Tile != null) { if (gridElement.Tile.GetComponent() != null) { num--; } } else { list.Add(gridIndex); } } } } list.Shuffle(); int num2 = 0; while (num2 < list.Count && num2 < cigexpansionDefinition.sceneryItems) { GridIndex index = list[num2]; GridElement gridElement2 = this.grid[index]; int type = gridElement2.Type; GameObject prefabForType = this.GetPrefabForType(type); GridTile component = prefabForType.GetComponent(); bool mirrored = component.canMirror && UnityEngine.Random.value >= 0.5f; this.island.builder.BuildAt(prefabForType, index, mirrored, true); num2++; } } this._initialSceneryPlaced = true; this.SceneryTimestamp = DateTime.UtcNow; } private bool HavePrefabForType(int type) { return this.GetPrefabForType(type) != null; } private GameObject GetPrefabForType(int type) { if (!this.availablePrefabs.ContainsKey(type)) { List list = new List(); foreach (GameObject gameObject in this.sceneryPrefabs) { CIGScenery component = gameObject.GetComponent(); GridTile tile = component.tile; bool flag = false; if (this.exclusiveElementType != (SurfaceType)type && tile.requiredGridType == 10) { if (((SurfaceType)type).IsLand()) { flag = true; } } else if (tile.requiredGridType == type) { flag = true; } if (flag) { if (component.multiple) { list = new List { gameObject }; break; } list.Add(gameObject); } } this.availablePrefabs[type] = list; } int count = this.availablePrefabs[type].Count; if (count == 0) { return null; } return this.availablePrefabs[type][UnityEngine.Random.Range(0, count)]; } [SelfReference] public IsometricIsland island; [SelfReference] public CityIsland cityIsland; [ChildReference] public IsometricGrid grid; [SelfReference] public Serializing serializing; [SelfReference] public CIGExpansions expansions; public SurfaceType exclusiveElementType = (SurfaceType)(-1); public const string SceneryTimestampKey = "SceneryTimestamp"; public const string InitialSceneryPlacedKey = "InitialSceneryIsPlaced"; protected List sceneryPrefabs = new List(); protected int initializationCounter; private Dictionary> availablePrefabs = new Dictionary>(); private DateTime _sceneryTimestamp = DateTime.MinValue; private bool _initialSceneryPlaced; }