House.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using MobiusEditor.Utility;
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Globalization;
  20. using System.Linq;
  21. namespace MobiusEditor.Model
  22. {
  23. public struct AlliesMask : IEnumerable<int>
  24. {
  25. public int Value { get; private set; }
  26. public AlliesMask(int value)
  27. {
  28. Value = value;
  29. }
  30. public void Set(int id)
  31. {
  32. if ((id < 0) || (id > 31))
  33. {
  34. throw new ArgumentOutOfRangeException();
  35. }
  36. Value |= (1 << id);
  37. }
  38. public void Clear(int id)
  39. {
  40. if ((id < 0) || (id > 31))
  41. {
  42. throw new ArgumentOutOfRangeException();
  43. }
  44. Value &= ~(1 << id);
  45. }
  46. public override bool Equals(object obj)
  47. {
  48. return Value.Equals(obj);
  49. }
  50. public override int GetHashCode()
  51. {
  52. return Value.GetHashCode();
  53. }
  54. public override string ToString()
  55. {
  56. return Value.ToString();
  57. }
  58. public IEnumerator<int> GetEnumerator()
  59. {
  60. for (int i = 0, mask = 1; i < 32; ++i, mask <<= 1)
  61. {
  62. if ((Value & mask) != 0)
  63. {
  64. yield return i;
  65. }
  66. }
  67. }
  68. IEnumerator IEnumerable.GetEnumerator()
  69. {
  70. return GetEnumerator();
  71. }
  72. }
  73. public class AlliesMaskTypeConverter : TypeConverter
  74. {
  75. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  76. {
  77. return (context is MapContext) && (sourceType == typeof(string));
  78. }
  79. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  80. {
  81. return (context is MapContext) && (destinationType == typeof(string));
  82. }
  83. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  84. {
  85. if (!(value is AlliesMask) || !CanConvertTo(context, destinationType))
  86. {
  87. return null;
  88. }
  89. var map = (context as MapContext).Map;
  90. var alliesMask = (AlliesMask)value;
  91. var allies = new List<string>(map.Houses.Length);
  92. foreach (var id in alliesMask)
  93. {
  94. if (map.Houses.Where(h => h.Type.Equals((sbyte)id)).FirstOrDefault() is House house)
  95. {
  96. allies.Add(house.Type.Name);
  97. }
  98. }
  99. return string.Join(",", allies);
  100. }
  101. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  102. {
  103. if (!CanConvertFrom(context, value?.GetType()))
  104. {
  105. return null;
  106. }
  107. var map = (context as MapContext).Instance as Map;
  108. var alliesMask = new AlliesMask(0);
  109. var allies = (value as string).Split(',');
  110. foreach (var ally in allies)
  111. {
  112. if (map.Houses.Where(h => h.Type.Equals(ally)).FirstOrDefault() is House house)
  113. {
  114. alliesMask.Set(house.Type.ID);
  115. }
  116. }
  117. return alliesMask;
  118. }
  119. }
  120. public class House
  121. {
  122. public readonly HouseType Type;
  123. [NonSerializedINIKey]
  124. [DefaultValue(true)]
  125. public bool Enabled { get; set; }
  126. [TypeConverter(typeof(AlliesMaskTypeConverter))]
  127. public AlliesMask Allies { get; set; }
  128. [DefaultValue(150)]
  129. public int MaxBuilding { get; set; }
  130. [DefaultValue(150)]
  131. public int MaxUnit { get; set; }
  132. [DefaultValue("North")]
  133. public string Edge { get; set; }
  134. [DefaultValue(0)]
  135. public int Credits { get; set; } = 0;
  136. public House(HouseType type)
  137. {
  138. Type = type;
  139. Allies = new AlliesMask(1 << Type.ID);
  140. }
  141. }
  142. }