Int32Range.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //-----------------------------------------------------------------------------
  2. // Int32Range.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Text;
  9. using Microsoft.Xna.Framework.Content;
  10. namespace RolePlaying.Data
  11. {
  12. /// <summary>
  13. /// Defines a range of values, useful for generating values in that range.
  14. /// </summary>
  15. #if WINDOWS
  16. [Serializable]
  17. #endif
  18. public struct Int32Range
  19. {
  20. /// <summary>
  21. /// The minimum value for the range, inclusively.
  22. /// </summary>
  23. public int Minimum;
  24. /// <summary>
  25. /// The maximum value for the range, inclusively.
  26. /// </summary>
  27. public int Maximum;
  28. /// <summary>
  29. /// Calculate the average value returned by this range.
  30. /// </summary>
  31. public Int32 Average
  32. {
  33. get { return Minimum + Range / 2; }
  34. }
  35. /// <summary>
  36. /// Calculate the size of this range.
  37. /// </summary>
  38. public Int32 Range
  39. {
  40. get { return (Maximum - Minimum); }
  41. }
  42. /// <summary>
  43. /// Construct a new Int32Range object, from the given minimum and maximums.
  44. /// </summary>
  45. public Int32Range(Int32 minimum, Int32 maximum)
  46. {
  47. // check the parameters
  48. if (maximum > minimum)
  49. {
  50. throw new ArgumentException(
  51. "The minimum must be less than or equal to the maximum.");
  52. }
  53. // assign the parameters
  54. this.Minimum = minimum;
  55. this.Maximum = maximum;
  56. }
  57. /// <summary>
  58. /// Generate a random value between the minimum and maximum, inclusively.
  59. /// </summary>
  60. /// <param name="random">The Random object used to generate the value.</param>
  61. public Int32 GenerateValue(Random random)
  62. {
  63. // check the parameters
  64. Random usedRandom = random;
  65. if (usedRandom == null)
  66. {
  67. usedRandom = new Random();
  68. }
  69. return usedRandom.Next(Minimum, Maximum);
  70. }
  71. /// <summary>
  72. /// Add one range to another, piecewise, and return the result.
  73. /// </summary>
  74. public static Int32Range Add(Int32Range range1, Int32Range range2)
  75. {
  76. Int32Range outputRange;
  77. outputRange.Minimum = range1.Minimum + range2.Minimum;
  78. outputRange.Maximum = range1.Maximum + range2.Maximum;
  79. return outputRange;
  80. }
  81. /// <summary>
  82. /// Add one range to another, piecewise, and return the result.
  83. /// </summary>
  84. public static Int32Range operator +(Int32Range range1, Int32Range range2)
  85. {
  86. return Add(range1, range2);
  87. }
  88. /// <summary>
  89. /// Add an Int32 to both the minimum and maximum values of the range.
  90. /// </summary>
  91. public static Int32Range Add(Int32Range range, Int32 amount)
  92. {
  93. Int32Range outputRange = range;
  94. outputRange.Minimum += amount;
  95. outputRange.Maximum += amount;
  96. return outputRange;
  97. }
  98. /// <summary>
  99. /// Add an Int32 to both the minimum and maximum values of the range.
  100. /// </summary>
  101. public static Int32Range operator +(Int32Range range, Int32 amount)
  102. {
  103. return Add(range, amount);
  104. }
  105. /// <summary>
  106. /// Subtract one range from another, piecewise, and return the result.
  107. /// </summary>
  108. public static Int32Range Subtract(Int32Range range1, Int32Range range2)
  109. {
  110. Int32Range outputRange;
  111. outputRange.Minimum = range1.Minimum - range2.Minimum;
  112. outputRange.Maximum = range1.Maximum - range2.Maximum;
  113. return outputRange;
  114. }
  115. /// <summary>
  116. /// Subtract one range from another, piecewise, and return the result.
  117. /// </summary>
  118. public static Int32Range operator -(Int32Range range1, Int32Range range2)
  119. {
  120. return Subtract(range1, range2);
  121. }
  122. /// <summary>
  123. /// Subtract an Int32 from both the minimum and maximum values of the range.
  124. /// </summary>
  125. public static Int32Range Subtract(Int32Range range, Int32 amount)
  126. {
  127. Int32Range outputRange = range;
  128. outputRange.Minimum -= amount;
  129. outputRange.Maximum -= amount;
  130. return outputRange;
  131. }
  132. /// <summary>
  133. /// Subtract an Int32 from both the minimum and maximum values of the range.
  134. /// </summary>
  135. public static Int32Range operator -(Int32Range range, Int32 amount)
  136. {
  137. return Subtract(range, amount);
  138. }
  139. // Compound assignment (+=, etc.) operators use the overloaded binary operators,
  140. // so there is no need in this case to override them explicitly
  141. /// <summary>
  142. /// Builds a string that describes this object.
  143. /// </summary>
  144. public override string ToString()
  145. {
  146. StringBuilder sb = new StringBuilder("(");
  147. sb.Append(Minimum);
  148. sb.Append(',');
  149. sb.Append(Maximum);
  150. sb.Append(')');
  151. return sb.ToString();
  152. }
  153. /// <summary>
  154. /// Read an Int32Range object from the content pipeline.
  155. /// </summary>
  156. public class Int32RangeReader : ContentTypeReader<Int32Range>
  157. {
  158. protected override Int32Range Read(ContentReader input,
  159. Int32Range existingInstance)
  160. {
  161. Int32Range output = existingInstance;
  162. output.Minimum = input.ReadInt32();
  163. output.Maximum = input.ReadInt32();
  164. return output;
  165. }
  166. }
  167. }
  168. }