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.
 
 
 

384 lines
9.1 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CIG.Translation;
  5. using CIGEnums;
  6. using SUISS.Core;
  7. using SUISS.Scheduling;
  8. using SUISSEngine;
  9. using UnityEngine;
  10. public class CIGResidentialBuilding : CIGBuilding
  11. {
  12. public int MaxPeople
  13. {
  14. get
  15. {
  16. return this.currentMaxPeople;
  17. }
  18. protected set
  19. {
  20. int num = value - this.currentMaxPeople;
  21. this.currentMaxPeople = value;
  22. if (num != 0)
  23. {
  24. base.IslandState.AddHousing(num);
  25. }
  26. }
  27. }
  28. public int People
  29. {
  30. get
  31. {
  32. return this.currentPeople;
  33. }
  34. protected set
  35. {
  36. int num = value - this.currentPeople;
  37. this.currentPeople = value;
  38. if (num != 0)
  39. {
  40. base.IslandState.AddPopulation(num);
  41. }
  42. }
  43. }
  44. private IEnumerator ResidentialBehaviour()
  45. {
  46. bool changed = true;
  47. for (;;)
  48. {
  49. yield return Timing.time + ((!changed) ? this.UnchangedWaitTime : this.ChangedWaitTime);
  50. changed = this.UpdatePeople();
  51. }
  52. yield break;
  53. }
  54. protected override void OnDestroy()
  55. {
  56. if (SingletonMonobehaviour<Scheduler>.IsAvailable)
  57. {
  58. this.StopResidentialBehavior();
  59. }
  60. base.OnDestroy();
  61. }
  62. protected virtual double UnchangedWaitTime
  63. {
  64. get
  65. {
  66. return (double)UnityEngine.Random.Range(2f, 30f);
  67. }
  68. }
  69. protected virtual double ChangedWaitTime
  70. {
  71. get
  72. {
  73. return (double)UnityEngine.Random.Range(1f, 3f);
  74. }
  75. }
  76. protected bool UpdatePeople()
  77. {
  78. int num = this.People;
  79. if (this.checkForRoad && !base.CheckForRoad())
  80. {
  81. num--;
  82. }
  83. else if (base.IslandState.Population > base.IslandState.AvailableHappiness)
  84. {
  85. num--;
  86. }
  87. else if (base.IslandState.Population < base.IslandState.AvailableHappiness)
  88. {
  89. num++;
  90. }
  91. num = Math.Max(Math.Min(num, this.MaxPeople), 0);
  92. if (num != this.People)
  93. {
  94. this.People = num;
  95. this.serializing.Serialize();
  96. return true;
  97. }
  98. return false;
  99. }
  100. public override bool InfoRequiresFrequentRefresh
  101. {
  102. get
  103. {
  104. return (base.state != BuildingState.Preview && base.state == BuildingState.Normal && !base.IsUpgrading) || base.InfoRequiresFrequentRefresh;
  105. }
  106. }
  107. public override bool CanSpeedup
  108. {
  109. get
  110. {
  111. return (base.state == BuildingState.Preview || base.state != BuildingState.Normal || base.IsUpgrading) && base.CanSpeedup;
  112. }
  113. }
  114. public override ILocalizedString InfoText()
  115. {
  116. ILocalizedString localizedString;
  117. if (base.state == BuildingState.Preview)
  118. {
  119. localizedString = Localization.Concat(new ILocalizedString[]
  120. {
  121. base.InfoText(),
  122. Localization.LiteralNewLineString,
  123. Localization.Key("people"),
  124. Localization.LiteralSemiColonSpaceString,
  125. Localization.Integer(this.maxPeopleBaseValue),
  126. Localization.LiteralNewLineString,
  127. Localization.Key("construction_time"),
  128. Localization.LiteralSemiColonSpaceString,
  129. Localization.TimeSpan(TimeSpan.FromSeconds((double)this.constructionTime), false)
  130. });
  131. if (this.maxFacilities > 0)
  132. {
  133. int num = SingletonMonobehaviour<CIGGameStats>.Instance.NumberOf(base.CachedName);
  134. if (this.PurchasePrice.Contains("Cash"))
  135. {
  136. localizedString = Localization.Concat(new ILocalizedString[]
  137. {
  138. localizedString,
  139. Localization.LiteralNewLineString,
  140. Localization.Format(Localization.Key("building_max_facilities"), new ILocalizedString[]
  141. {
  142. Localization.Integer(this.maxFacilities)
  143. })
  144. });
  145. }
  146. else if (num > 0)
  147. {
  148. localizedString = Localization.Concat(new ILocalizedString[]
  149. {
  150. localizedString,
  151. Localization.LiteralNewLineString,
  152. Localization.Format(Localization.Key("building_costs_gold_max_facilities"), new ILocalizedString[]
  153. {
  154. Localization.Integer(num)
  155. })
  156. });
  157. }
  158. }
  159. return localizedString;
  160. }
  161. if (base.state != BuildingState.Normal || base.IsUpgrading)
  162. {
  163. return base.InfoText();
  164. }
  165. if (this.checkForRoad && !base.CheckForRoad())
  166. {
  167. return Localization.Key("building_needs_road");
  168. }
  169. localizedString = Localization.Concat(new ILocalizedString[]
  170. {
  171. Localization.Format(Localization.Key("residential_people"), new ILocalizedString[]
  172. {
  173. Localization.Integer(this.People),
  174. Localization.Integer(this.MaxPeople)
  175. }),
  176. Localization.LiteralNewLineString,
  177. Localization.Format(Localization.Key("building_current_upgrade_level"), new ILocalizedString[]
  178. {
  179. Localization.Integer(base.CurrentLevel),
  180. Localization.Integer(base.GetMaxLevel())
  181. })
  182. });
  183. if (this.CanUpgrade)
  184. {
  185. localizedString = Localization.Concat(new ILocalizedString[]
  186. {
  187. localizedString,
  188. Localization.LiteralNewLineString,
  189. Localization.Format(Localization.Key("building_upgrade_option"), new ILocalizedString[]
  190. {
  191. Localization.Integer(base.CurrentLevel + 1),
  192. this.UpgradeCost.LocalizedString()
  193. }),
  194. Localization.LiteralNewLineString,
  195. Localization.Format(Localization.Key("building_upgrade_option_residential"), new ILocalizedString[]
  196. {
  197. Localization.Integer(this.UpgradePeople(base.CurrentLevel + 1))
  198. })
  199. });
  200. }
  201. return localizedString;
  202. }
  203. public override List<BuildingProperty> ShownProperties
  204. {
  205. get
  206. {
  207. List<BuildingProperty> shownProperties = base.ShownProperties;
  208. shownProperties.Add(BuildingProperty.People);
  209. return shownProperties;
  210. }
  211. }
  212. public override Currencies UpgradeCost
  213. {
  214. get
  215. {
  216. Currencies result;
  217. try
  218. {
  219. decimal num = this.baseUpgradeCost.GetValue("Cash");
  220. int num2 = (!this.activatable) ? 0 : -1;
  221. num *= this._upgradeCostFactor[base.CurrentLevel + 1 + num2] * 0.01m;
  222. result = new Currencies("Cash", Math.Ceiling(num));
  223. }
  224. catch (Exception ex)
  225. {
  226. throw new Exception(string.Format("[{0}|{2}]: {1}", base.name, ex.Message, this.stringReference), ex);
  227. }
  228. return result;
  229. }
  230. }
  231. public override Currencies PurchasePrice
  232. {
  233. get
  234. {
  235. Currencies purchasePrice = base.PurchasePrice;
  236. if (purchasePrice.Contains("Cash"))
  237. {
  238. return purchasePrice * SingletonMonobehaviour<CIGWebService>.Instance.Multipliers.ResidentialBuildingCostCashMultiplier;
  239. }
  240. return purchasePrice * SingletonMonobehaviour<CIGWebService>.Instance.Multipliers.ResidentialBuildingCostGoldMultiplier;
  241. }
  242. }
  243. public int UpgradePeople(int toLevel)
  244. {
  245. if (base.GetMaxLevel() != 0)
  246. {
  247. int val = Math.Max(0, toLevel - base.CurrentLevel);
  248. return Math.Max(val, this.maxPeopleBaseValue + toLevel * this.maxPeopleBaseValue / 5 - this.currentMaxPeople);
  249. }
  250. return 0;
  251. }
  252. private void StartResidentialBehavior()
  253. {
  254. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._behaviourRoutine = this.ResidentialBehaviour(), base.gameObject);
  255. }
  256. private void StopResidentialBehavior()
  257. {
  258. if (this._behaviourRoutine != null)
  259. {
  260. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._behaviourRoutine);
  261. this._behaviourRoutine = null;
  262. }
  263. }
  264. protected override void OnSerialize(Dictionary<string, object> values)
  265. {
  266. base.OnSerialize(values);
  267. values["people"] = this.People;
  268. values["maxPeople"] = this.MaxPeople;
  269. if (this._maxPeopleWhenDemolishStarted != 0)
  270. {
  271. values["maxPeopleWhenDemolishStarted"] = this._maxPeopleWhenDemolishStarted;
  272. }
  273. }
  274. protected override void OnDeserialize(Dictionary<string, object> values)
  275. {
  276. base.OnDeserialize(values);
  277. if (values.ContainsKey("people"))
  278. {
  279. this.currentPeople = (int)values["people"];
  280. }
  281. if (values.ContainsKey("maxPeople"))
  282. {
  283. this.currentMaxPeople = (int)values["maxPeople"];
  284. }
  285. if (values.ContainsKey("maxPeopleWhenDemolishStarted"))
  286. {
  287. this._maxPeopleWhenDemolishStarted = (int)values["maxPeopleWhenDemolishStarted"];
  288. }
  289. }
  290. protected override void OnDeserialized()
  291. {
  292. base.OnDeserialized();
  293. if (base.state == BuildingState.Normal)
  294. {
  295. this.StartResidentialBehavior();
  296. }
  297. }
  298. protected override void OnConstructionCompleted()
  299. {
  300. base.OnConstructionCompleted();
  301. this.MaxPeople = this.maxPeopleBaseValue;
  302. this.StartResidentialBehavior();
  303. }
  304. protected override void OnDemolishStarted()
  305. {
  306. base.OnDemolishStarted();
  307. this.StopResidentialBehavior();
  308. this._maxPeopleWhenDemolishStarted = this.MaxPeople;
  309. int num = 0;
  310. this.People = num;
  311. this.MaxPeople = num;
  312. }
  313. protected override void OnDemolishCancelled()
  314. {
  315. base.OnDemolishCancelled();
  316. this.MaxPeople = this._maxPeopleWhenDemolishStarted;
  317. this._maxPeopleWhenDemolishStarted = 0;
  318. this.StartResidentialBehavior();
  319. }
  320. protected override void OnUpgradeCompleted(double upgradedTime)
  321. {
  322. base.OnUpgradeCompleted(upgradedTime);
  323. this.MaxPeople += this.UpgradePeople(base.CurrentLevel);
  324. }
  325. public int maxPeopleBaseValue;
  326. private int currentMaxPeople;
  327. private int currentPeople;
  328. private int _maxPeopleWhenDemolishStarted;
  329. private IEnumerator _behaviourRoutine;
  330. private readonly int[] _upgradeCostFactor = new int[]
  331. {
  332. 100,
  333. 20,
  334. 28,
  335. 39,
  336. 55,
  337. 77,
  338. 108,
  339. 151,
  340. 211,
  341. 295,
  342. 413,
  343. 496,
  344. 595,
  345. 714,
  346. 857,
  347. 1028,
  348. 1234,
  349. 1481,
  350. 1777,
  351. 2132,
  352. 2558
  353. };
  354. }