You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

238 lines
6.2 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using SUISS.Core;
  5. using SUISS.Scheduling;
  6. using SUISSEngine;
  7. using UnityEngine;
  8. public class CIGVehicleManager : VehicleManager
  9. {
  10. protected override void Awake()
  11. {
  12. base.Awake();
  13. foreach (GameObject gameObject in this.vehiclePrefabs)
  14. {
  15. Vehicle component = gameObject.GetComponent<Vehicle>();
  16. if (component != null && component.requiredRoadType > 0)
  17. {
  18. this.RegisterVehicle(component, gameObject);
  19. }
  20. }
  21. }
  22. protected override void Start()
  23. {
  24. base.Start();
  25. this.SetPreferredCountsOnNextFrame();
  26. }
  27. public override void WillBeginBuildingRoads(int type)
  28. {
  29. base.WillBeginBuildingRoads(type);
  30. this._buildingRoads = true;
  31. }
  32. public override void DidBuildRoadAt(GridIndex index, Road road)
  33. {
  34. base.DidBuildRoadAt(index, road);
  35. if (!this._buildingRoads)
  36. {
  37. this.SetPreferredCountsOnNextFrame();
  38. }
  39. }
  40. public override void WillDestroyRoadAt(GridIndex index, Road road)
  41. {
  42. base.WillDestroyRoadAt(index, road);
  43. if (!this._buildingRoads)
  44. {
  45. this.SetPreferredCountsOnNextFrame();
  46. }
  47. }
  48. public override void DidFinishBuildingRoads(int type)
  49. {
  50. base.DidFinishBuildingRoads(type);
  51. this._buildingRoads = false;
  52. this.SetPreferredCountsOnNextFrame();
  53. }
  54. public override void DidBuildTileAt(GridIndex index, GridTile tile)
  55. {
  56. base.DidBuildTileAt(index, tile);
  57. if (this.IsTileInteresting(tile))
  58. {
  59. this.SetPreferredCountsOnNextFrame();
  60. }
  61. }
  62. public override void WillDestroyTileAt(GridIndex index, GridTile tile)
  63. {
  64. base.WillDestroyTileAt(index, tile);
  65. if (this.IsTileInteresting(tile))
  66. {
  67. this.SetPreferredCountsOnNextFrame();
  68. }
  69. }
  70. public void BuildingStateChanged(GridTile tile)
  71. {
  72. if (this.IsTileInteresting(tile))
  73. {
  74. this.SetPreferredCountsOnNextFrame();
  75. }
  76. }
  77. private VehicleManager.VehicleRegistration GetRegistration(int roadType, Vehicle.VehicleType vehicleType)
  78. {
  79. foreach (VehicleManager.VehicleRegistration vehicleRegistration in this._registrations)
  80. {
  81. if (vehicleRegistration.VehicleType == vehicleType && vehicleRegistration.RoadType == roadType)
  82. {
  83. return vehicleRegistration;
  84. }
  85. }
  86. return null;
  87. }
  88. private void RegisterVehicle(Vehicle vehicle, GameObject prefab)
  89. {
  90. VehicleManager.VehicleRegistration vehicleRegistration = this.GetRegistration(vehicle.requiredRoadType, vehicle.type);
  91. if (vehicleRegistration != null)
  92. {
  93. vehicleRegistration.AddPrefab(prefab);
  94. }
  95. else
  96. {
  97. VehicleManager.RoadStats statsForRoadType = base.GetStatsForRoadType(vehicle.requiredRoadType);
  98. vehicleRegistration = statsForRoadType.RegisterVehicle(vehicle.type, new GameObject[]
  99. {
  100. prefab
  101. });
  102. this._registrations.Add(vehicleRegistration);
  103. }
  104. }
  105. private void SetPreferredCountsOnNextFrame()
  106. {
  107. if (!this._willUpdateCounts)
  108. {
  109. this._willUpdateCounts = true;
  110. SingletonMonobehaviour<Scheduler>.Instance.ExecuteNextFrame(base.gameObject, new Action(this.SetPreferredCounts));
  111. }
  112. }
  113. private void SetPreferredCounts()
  114. {
  115. this._willUpdateCounts = false;
  116. IEnumerator enumerator = Enum.GetValues(typeof(CIGVehicleManager.RoadType)).GetEnumerator();
  117. try
  118. {
  119. while (enumerator.MoveNext())
  120. {
  121. object obj = enumerator.Current;
  122. CIGVehicleManager.RoadType roadType = (CIGVehicleManager.RoadType)obj;
  123. VehicleManager.RoadStats statsForRoadType = base.GetStatsForRoadType((int)roadType);
  124. int num = statsForRoadType.Area / 7;
  125. IEnumerator enumerator2 = Enum.GetValues(typeof(Vehicle.VehicleType)).GetEnumerator();
  126. try
  127. {
  128. while (enumerator2.MoveNext())
  129. {
  130. object obj2 = enumerator2.Current;
  131. Vehicle.VehicleType vehicleType = (Vehicle.VehicleType)obj2;
  132. if (vehicleType >= (Vehicle.VehicleType)0)
  133. {
  134. VehicleManager.VehicleRegistration registration = this.GetRegistration((int)roadType, vehicleType);
  135. if (registration != null)
  136. {
  137. int num2 = Mathf.Min(num, this.CountBuildingsForVehicleType(vehicleType));
  138. registration.PreferredCount = num2;
  139. num -= num2;
  140. }
  141. }
  142. }
  143. }
  144. finally
  145. {
  146. IDisposable disposable;
  147. if ((disposable = (enumerator2 as IDisposable)) != null)
  148. {
  149. disposable.Dispose();
  150. }
  151. }
  152. VehicleManager.VehicleRegistration registration2 = this.GetRegistration((int)roadType, Vehicle.VehicleType.Taxi);
  153. if (registration2 != null && num > 0)
  154. {
  155. int num3 = Mathf.Max(1, num / 8);
  156. registration2.PreferredCount = num3;
  157. num -= num3;
  158. }
  159. VehicleManager.VehicleRegistration registration3 = this.GetRegistration((int)roadType, Vehicle.VehicleType.Civilian);
  160. if (registration3 != null && num > 0)
  161. {
  162. registration3.PreferredCount = num;
  163. }
  164. else if (num > 0)
  165. {
  166. UnityEngine.Debug.LogWarning(string.Format("Unable to fill preferred counts for roadType {0}; have {1} vehicles leftover.", roadType, num));
  167. }
  168. }
  169. }
  170. finally
  171. {
  172. IDisposable disposable2;
  173. if ((disposable2 = (enumerator as IDisposable)) != null)
  174. {
  175. disposable2.Dispose();
  176. }
  177. }
  178. }
  179. private bool IsTileInteresting(GridTile tile)
  180. {
  181. CIGBuilding component = tile.GetComponent<CIGBuilding>();
  182. if (component != null && component.IsConstructed && component.spawnsVehicleType > (Vehicle.VehicleType)0)
  183. {
  184. foreach (VehicleManager.VehicleRegistration vehicleRegistration in this._registrations)
  185. {
  186. if (vehicleRegistration.VehicleType == component.spawnsVehicleType)
  187. {
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. return false;
  194. }
  195. private int CountBuildingsForVehicleType(Vehicle.VehicleType vehicleType)
  196. {
  197. int num = 0;
  198. foreach (CIGBuilding cigbuilding in UnityEngine.Object.FindObjectsOfType<CIGBuilding>())
  199. {
  200. if (cigbuilding.IsConstructed && cigbuilding.spawnsVehicleType == vehicleType)
  201. {
  202. num++;
  203. }
  204. }
  205. return num;
  206. }
  207. public GameObject[] vehiclePrefabs;
  208. private bool _buildingRoads;
  209. private bool _willUpdateCounts;
  210. private List<VehicleManager.VehicleRegistration> _registrations = new List<VehicleManager.VehicleRegistration>();
  211. public enum RoadType
  212. {
  213. Road = 1,
  214. Walkway,
  215. River = 4
  216. }
  217. }