PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/NRaasStoryProgression/StoryProgressionSpace/OptionStore.cs

https://gitlab.com/forpdfsending/NRaas
C# | 613 lines | 472 code | 118 blank | 23 comment | 49 complexity | 0ac72112887f389d07a299dd9f7ca97e MD5 | raw file
  1. using NRaas.CommonSpace.Helpers;
  2. using NRaas.StoryProgressionSpace.Interfaces;
  3. using NRaas.StoryProgressionSpace.Managers;
  4. using NRaas.StoryProgressionSpace.Options;
  5. using NRaas.StoryProgressionSpace.Scenarios;
  6. using NRaas.StoryProgressionSpace.Scenarios.Sims;
  7. using NRaas.StoryProgressionSpace.Scoring;
  8. using NRaas.StoryProgressionSpace.SimDataElement;
  9. using Sims3.Gameplay.Actors;
  10. using Sims3.Gameplay.CAS;
  11. using Sims3.Gameplay.Core;
  12. using Sims3.Gameplay.UI;
  13. using Sims3.Gameplay.Utilities;
  14. using Sims3.SimIFace;
  15. using Sims3.UI;
  16. using System;
  17. using System.Collections.Generic;
  18. namespace NRaas.StoryProgressionSpace
  19. {
  20. // For legacy purposes, this class must always be at this level in the namespace
  21. [Persistable]
  22. public class OptionStore : GenericOptionBase
  23. {
  24. List<SimData> mSimData = new List<SimData>();
  25. List<HouseholdOptions> mHouseholdOptions = new List<HouseholdOptions>();
  26. List<LotOptions> mLotOptions = new List<LotOptions>();
  27. List<CasteOptions> mCasteOptions = new List<CasteOptions>();
  28. HouseholdOptions mImmigrantOptions = new HouseholdOptions();
  29. [Persistable(false)]
  30. ulong mNextCasteID = 0;
  31. [Persistable(false)]
  32. Dictionary<ulong, LotOptions> mLotOptionsLookup = null;
  33. [Persistable(false)]
  34. Dictionary<ulong, HouseholdOptions> mHouseholdOptionsLookup = null;
  35. [Persistable(false)]
  36. Dictionary<ulong, CasteOptions> mCasteOptionsLookup = null;
  37. [Persistable(false)]
  38. Dictionary<SimID, SimData> mSimLookup = null;
  39. public OptionStore() // required for persistence
  40. { }
  41. public override string Name
  42. {
  43. get { return "Town Options"; }
  44. }
  45. public bool HasCastes
  46. {
  47. get { return (Castes.Count > 0); }
  48. }
  49. public IEnumerable<CasteOptions> AllCastes
  50. {
  51. get { return Castes.Values; }
  52. }
  53. protected Dictionary<ulong, CasteOptions> Castes
  54. {
  55. get
  56. {
  57. if (mCasteOptionsLookup == null)
  58. {
  59. mCasteOptionsLookup = new Dictionary<ulong, CasteOptions>();
  60. int index = 0;
  61. while (index < mCasteOptions.Count)
  62. {
  63. CasteOptions oldOptions = mCasteOptions[index];
  64. if (!mCasteOptionsLookup.ContainsKey(oldOptions.ID))
  65. {
  66. mCasteOptionsLookup.Add(oldOptions.ID, oldOptions);
  67. index++;
  68. }
  69. else
  70. {
  71. mCasteOptions.RemoveAt(index);
  72. }
  73. }
  74. }
  75. return mCasteOptionsLookup;
  76. }
  77. }
  78. protected Dictionary<ulong, LotOptions> Lots
  79. {
  80. get
  81. {
  82. if (mLotOptionsLookup == null)
  83. {
  84. mLotOptionsLookup = new Dictionary<ulong, LotOptions>();
  85. int index = 0;
  86. while (index < mLotOptions.Count)
  87. {
  88. LotOptions oldOptions = mLotOptions[index];
  89. if ((LotManager.GetLot(oldOptions.LotId) == null) || (mLotOptionsLookup.ContainsKey(oldOptions.LotId)))
  90. {
  91. mLotOptions.RemoveAt(index);
  92. }
  93. else
  94. {
  95. mLotOptionsLookup.Add(oldOptions.LotId, oldOptions);
  96. index++;
  97. }
  98. }
  99. }
  100. return mLotOptionsLookup;
  101. }
  102. }
  103. protected Dictionary<ulong, HouseholdOptions> Households
  104. {
  105. get
  106. {
  107. if (mHouseholdOptionsLookup == null)
  108. {
  109. mHouseholdOptionsLookup = new Dictionary<ulong, HouseholdOptions>();
  110. int index = 0;
  111. while (index < mHouseholdOptions.Count)
  112. {
  113. HouseholdOptions oldOptions = mHouseholdOptions[index];
  114. if ((Household.Find(oldOptions.HouseholdId) == null) || (mHouseholdOptionsLookup.ContainsKey(oldOptions.HouseholdId)))
  115. {
  116. mHouseholdOptions.RemoveAt(index);
  117. }
  118. else
  119. {
  120. mHouseholdOptionsLookup.Add(oldOptions.HouseholdId, oldOptions);
  121. index++;
  122. }
  123. }
  124. }
  125. return mHouseholdOptionsLookup;
  126. }
  127. }
  128. protected Dictionary<SimID, SimData> Sims
  129. {
  130. get
  131. {
  132. if (mSimLookup == null)
  133. {
  134. mSimLookup = new Dictionary<SimID, SimData>();
  135. Dictionary<SimID, int> countLookup = new Dictionary<SimID, int>();
  136. int count = 0;
  137. int index = 0;
  138. while (index < mSimData.Count)
  139. {
  140. count++;
  141. SimData oldData = mSimData[index];
  142. if (oldData.SimDescription == null)
  143. {
  144. mSimData.RemoveAt(index);
  145. }
  146. else
  147. {
  148. if (mSimLookup.ContainsKey(oldData.SimID))
  149. {
  150. int priorCount = -1;
  151. if (countLookup.ContainsKey(oldData.SimID))
  152. {
  153. priorCount = countLookup[oldData.SimID];
  154. }
  155. mSimData.RemoveAt(index);
  156. }
  157. else
  158. {
  159. countLookup.Add(oldData.SimID, count);
  160. mSimLookup.Add(oldData.SimID, oldData);
  161. index++;
  162. }
  163. }
  164. }
  165. }
  166. return mSimLookup;
  167. }
  168. }
  169. public R GetValue<T, R>()
  170. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  171. {
  172. return GetInternalValue<T, R>();
  173. }
  174. public bool SetValue<T, R>(R value)
  175. where T : GenericOptionItem<R>, IGenericLevelOption, new()
  176. {
  177. return SetInternalValue<T, R>(value);
  178. }
  179. public TType AddValue<T, TType>(TType value)
  180. where T : OptionItem, IGenericAddOption<TType>, IGenericLevelOption, new()
  181. {
  182. return AddInternalValue<T, TType>(value);
  183. }
  184. public void RemoveValue<T, TType>(TType value)
  185. where T : OptionItem, IGenericRemoveOption<TType>, IGenericLevelOption, new()
  186. {
  187. RemoveInternalValue<T, TType>(value);
  188. }
  189. public bool HasValue<T, TType>(TType value)
  190. where T : OptionItem, IGenericHasOption<TType>, IGenericLevelOption, new()
  191. {
  192. return HasInternalValue<T, TType>(value);
  193. }
  194. public bool HasAnyValue<T, TType>()
  195. where T : OptionItem, IGenericHasOption<TType>, IGenericLevelOption, new()
  196. {
  197. return HasAnyInternalValue<T, TType>();
  198. }
  199. public override SimDescription SimDescription
  200. {
  201. get { return null; }
  202. }
  203. public override Household House
  204. {
  205. get { return null; }
  206. }
  207. public override Lot Lot
  208. {
  209. get { return null; }
  210. }
  211. public void ExportCastes(Persistence.Lookup settings)
  212. {
  213. settings.Add("Castes", mCasteOptions);
  214. }
  215. public void ImportCastes(Persistence.Lookup settings)
  216. {
  217. List<CasteOptions> casteOptions = mCasteOptions;
  218. mCasteOptions = settings.GetList<CasteOptions>("Castes");
  219. mCasteOptionsLookup = null;
  220. if ((mCasteOptions == null) || (mCasteOptions.Count == 0))
  221. {
  222. mCasteOptions = casteOptions;
  223. }
  224. else
  225. {
  226. mNextCasteID = 0;
  227. ValidateCasteOptions(Castes);
  228. }
  229. }
  230. public override void UpdateInheritors(IGenericLevelOption option)
  231. {
  232. base.UpdateInheritors(option);
  233. foreach (Household house in Household.sHouseholdList)
  234. {
  235. HouseholdOptions options = GetHouseOptions(house);
  236. if (!IsValidOption(option))
  237. {
  238. options.RemoveOption(option);
  239. }
  240. options.UpdateInheritors(option);
  241. }
  242. }
  243. public override bool IsValidOption(IGenericLevelOption option)
  244. {
  245. if (option is INotGlobalLevelOption) return false;
  246. if (option is IWriteHouseLevelOption) return true;
  247. return (option is IHouseLevelSimOption);
  248. }
  249. protected override void GetParentOptions(List<ParentItem> options, DefaultingLevel level)
  250. { }
  251. public void RemoveCaste(CasteOptions caste)
  252. {
  253. mCasteOptions.Remove(caste);
  254. Castes.Remove(caste.ID);
  255. ValidateCasteOptions(Castes);
  256. InvalidateCache();
  257. }
  258. public void RemoveSim(Sim sim)
  259. {
  260. RemoveSim(sim.SimDescription);
  261. }
  262. public void RemoveSim(SimDescription sim)
  263. {
  264. Sims.Remove(new SimID(sim));
  265. }
  266. public override void InvalidateCache()
  267. {
  268. base.InvalidateCache();
  269. foreach (SimData sim in mSimData)
  270. {
  271. sim.InvalidateCache();
  272. }
  273. foreach (HouseholdOptions house in mHouseholdOptions)
  274. {
  275. house.InvalidateCache();
  276. }
  277. foreach (LotOptions lot in mLotOptions)
  278. {
  279. lot.InvalidateCache();
  280. }
  281. }
  282. public SimData GetSim(Sim sim)
  283. {
  284. return GetSim(sim.SimDescription);
  285. }
  286. public SimData GetSim(SimDescription sim)
  287. {
  288. SimID id = new SimID(sim);
  289. SimData data;
  290. if (!Sims.TryGetValue(id, out data))
  291. {
  292. data = new SimData(sim);
  293. Sims.Add(id, data);
  294. mSimData.Add(data);
  295. }
  296. data.SetSimDescription(sim);
  297. return data;
  298. }
  299. public T GetSim<T>(Sim sim)
  300. where T : ElementalSimData, new()
  301. {
  302. return GetSim(sim).Get<T>();
  303. }
  304. public T GetSim<T>(SimDescription sim)
  305. where T : ElementalSimData, new()
  306. {
  307. return GetSim(sim).Get<T>();
  308. }
  309. public bool ShowOptions ()
  310. {
  311. return new Managers.Main.MasterListingOption(NRaas.StoryProgression.Main).Perform();
  312. }
  313. public bool ShowImmigrantOptions(Manager manager, string localizedTitle)
  314. {
  315. return mImmigrantOptions.ShowOptions(manager, localizedTitle);
  316. }
  317. public List<DefaultableOption> GetImmigrantOptions(Manager manager)
  318. {
  319. return mImmigrantOptions.GetOptions(manager, "Immigrant", false);
  320. }
  321. public void StampImmigrantHousehold(Household house)
  322. {
  323. GetHouseOptions(house).CopyOptions(mImmigrantOptions);
  324. }
  325. public void UpdateSimData(SimUpdateScenario scenario, ScenarioFrame frame)
  326. {
  327. // This list can change size during the loop, cannot use "foreach"
  328. int i=0;
  329. while (i < mSimData.Count)
  330. {
  331. scenario.Perform(mSimData[i], frame);
  332. i++;
  333. }
  334. }
  335. public override void ValidateCasteOptions(Dictionary<ulong, CasteOptions> castes)
  336. {
  337. base.ValidateCasteOptions(castes);
  338. foreach (SimData option in Sims.Values)
  339. {
  340. option.ValidateCasteOptions(castes);
  341. }
  342. foreach (CasteOptions option in Castes.Values)
  343. {
  344. option.ValidateCasteOptions(castes);
  345. }
  346. mImmigrantOptions.ValidateCasteOptions(castes);
  347. foreach (HouseholdOptions option in Households.Values)
  348. {
  349. option.ValidateCasteOptions(castes);
  350. }
  351. foreach (LotOptions option in Lots.Values)
  352. {
  353. option.ValidateCasteOptions(castes);
  354. }
  355. }
  356. public override void Restore()
  357. {
  358. base.Restore();
  359. foreach (SimData option in Sims.Values)
  360. {
  361. option.Restore();
  362. }
  363. foreach (CasteOptions option in Castes.Values)
  364. {
  365. option.Restore();
  366. }
  367. mImmigrantOptions.Restore();
  368. foreach (HouseholdOptions option in Households.Values)
  369. {
  370. option.Restore();
  371. }
  372. foreach (LotOptions option in Lots.Values)
  373. {
  374. option.Restore();
  375. }
  376. }
  377. protected override bool RetainAllOptions
  378. {
  379. get { return true; }
  380. }
  381. /*
  382. public void RemoveSim(ulong sim)
  383. {
  384. Sims.Remove(new SimID(sim));
  385. int index = 0;
  386. while (index < mSimData.Count)
  387. {
  388. SimData data = mSimData[index];
  389. if (SimID.Matches (data.SimID, sim))
  390. {
  391. mSimData.RemoveAt(index);
  392. }
  393. else
  394. {
  395. index++;
  396. }
  397. }
  398. }
  399. */
  400. public CasteOptions GetNewCasteOptions(string defaultName, string name, out bool created)
  401. {
  402. created = false;
  403. if (!string.IsNullOrEmpty(defaultName))
  404. {
  405. ManagerCaste.AddDefaultCaste(defaultName);
  406. foreach (CasteOptions option in AllCastes)
  407. {
  408. if (option.DefaultName == defaultName)
  409. {
  410. StoryProgression.Main.GetOption<ManagerCaste.CreatedCastesOption>().AddValue(defaultName);
  411. return option;
  412. }
  413. }
  414. if (StoryProgression.Main.GetValue<ManagerCaste.CreatedCastesOption, List<string>>().Contains(defaultName)) return null;
  415. }
  416. if (mNextCasteID == 0)
  417. {
  418. foreach (ulong id in Castes.Keys)
  419. {
  420. if (mNextCasteID < id)
  421. {
  422. mNextCasteID = id;
  423. }
  424. }
  425. }
  426. mNextCasteID++;
  427. CasteOptions options = GetCasteOptions(mNextCasteID);
  428. if (options == null) return null;
  429. options.SetValue<CasteNameOption, string>(name);
  430. if (!string.IsNullOrEmpty(defaultName))
  431. {
  432. options.SetValue<CasteDefaultNameOption, string>(defaultName);
  433. StoryProgression.Main.GetOption<ManagerCaste.CreatedCastesOption>().AddValue(defaultName);
  434. }
  435. created = true;
  436. return options;
  437. }
  438. public CasteOptions GetCasteOptions(ulong id)
  439. {
  440. CasteOptions result;
  441. if (!Castes.TryGetValue(id, out result))
  442. {
  443. result = new CasteOptions(id);
  444. mCasteOptions.Add(result);
  445. Castes.Add(id, result);
  446. }
  447. return result;
  448. }
  449. public LotOptions GetLotOptions(Lot lot)
  450. {
  451. if (lot == null)
  452. {
  453. return null;
  454. }
  455. else
  456. {
  457. LotOptions options;
  458. if (!Lots.TryGetValue(lot.LotId, out options))
  459. {
  460. options = new LotOptions(lot);
  461. mLotOptions.Add(options);
  462. Lots.Add(lot.LotId, options);
  463. }
  464. return options;
  465. }
  466. }
  467. public HouseholdOptions GetHouseOptions(Household house)
  468. {
  469. if (house == null)
  470. {
  471. return null;
  472. }
  473. else
  474. {
  475. HouseholdOptions options;
  476. if (!Households.TryGetValue(house.HouseholdId, out options))
  477. {
  478. options = new HouseholdOptions(house);
  479. mHouseholdOptions.Add(options);
  480. Households.Add(house.HouseholdId, options);
  481. }
  482. return options;
  483. }
  484. }
  485. public override string ToString()
  486. {
  487. Common.StringBuilder result = new Common.StringBuilder("<TownOptions>");
  488. result += base.ToString();
  489. result += Common.NewLine + "</TownOptions>";
  490. return result.ToString();
  491. }
  492. }
  493. }