Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using Vintagestory.API.MathTools;
  4.  
  5. namespace Vintagestory.Essentials
  6. {
  7. [Flags]
  8. public enum ProperNeighbor : byte
  9. {
  10. None = 0,
  11.  
  12. // FACINGS
  13. East = 0b110000, // +X
  14. West = 0b100000, // -X
  15. Up = 0b001100, // +Y
  16. Down = 0b001000, // -Y
  17. South = 0b000011, // +Z
  18. North = 0b000010, // -Z
  19.  
  20. // CARDINALS
  21. SouthEast = South | East, // +X +Z
  22. SouthWest = South | West, // -X +Z
  23. NorthEast = North | East, // +X -Z
  24. NorthWest = North | West, // -X -Z
  25.  
  26. // ALL_AXIS_PLANES
  27. UpEast = Up | East , // +X +Y
  28. UpWest = Up | West , // -X +Y
  29. UpSouth = Up | South, // +Z +Y
  30. UpNorth = Up | North, // -Z +Y
  31.  
  32. DownEast = Down | East , // +X -Y
  33. DownWest = Down | West , // -X -Y
  34. DownSouth = Down | South, // +Z -Y
  35. DownNorth = Down | North, // -Z -Y
  36.  
  37. // ALL
  38. UpSouthEast = Up | South | East, // +X +Y +Z
  39. UpSouthWest = Up | South | West, // -X +Y +Z
  40. UpNorthEast = Up | North | East, // +X +Y -Z
  41. UpNorthWest = Up | North | West, // -X +Y -Z
  42.  
  43. DownSouthEast = Down | South | East, // +X -Y +Z
  44. DownSouthWest = Down | South | West, // -X -Y +Z
  45. DownNorthEast = Down | North | East, // +X -Y -Z
  46. DownNorthWest = Down | North | West, // -X -Y -Z
  47. }
  48.  
  49. public static class ProperNeighborHelper
  50. {
  51. public static readonly ImmutableSet<ProperNeighbor> HORIZONTALS =
  52. new ImmutableSet<ProperNeighbor>(
  53. ProperNeighbor.East, ProperNeighbor.West,
  54. ProperNeighbor.South, ProperNeighbor.North);
  55.  
  56. public static readonly ImmutableSet<ProperNeighbor> VERTICALS =
  57. new ImmutableSet<ProperNeighbor>(ProperNeighbor.Up, ProperNeighbor.Down);
  58.  
  59. public static readonly ImmutableSet<ProperNeighbor> FACINGS =
  60. new ImmutableSet<ProperNeighbor>(HORIZONTALS, VERTICALS);
  61.  
  62. public static readonly ImmutableSet<ProperNeighbor> CARDINALS =
  63. new ImmutableSet<ProperNeighbor>(HORIZONTALS,
  64. ProperNeighbor.SouthEast, ProperNeighbor.SouthWest,
  65. ProperNeighbor.NorthEast, ProperNeighbor.NorthWest);
  66.  
  67. public static readonly ImmutableSet<ProperNeighbor> ALL_AXIS_PLANES =
  68. new ImmutableSet<ProperNeighbor>(FACINGS,
  69. ProperNeighbor.SouthEast, ProperNeighbor.SouthWest,
  70. ProperNeighbor.NorthEast, ProperNeighbor.NorthWest,
  71. ProperNeighbor.UpEast , ProperNeighbor.UpWest ,
  72. ProperNeighbor.UpSouth , ProperNeighbor.UpNorth ,
  73. ProperNeighbor.DownEast , ProperNeighbor.DownWest ,
  74. ProperNeighbor.DownSouth, ProperNeighbor.DownNorth);
  75.  
  76. public static readonly ImmutableSet<ProperNeighbor> ALL =
  77. new ImmutableSet<ProperNeighbor>(ALL_AXIS_PLANES,
  78. ProperNeighbor.UpSouthEast, ProperNeighbor.UpSouthWest,
  79. ProperNeighbor.UpNorthEast, ProperNeighbor.UpNorthWest,
  80. ProperNeighbor.DownSouthEast, ProperNeighbor.DownSouthWest,
  81. ProperNeighbor.DownNorthEast, ProperNeighbor.DownNorthWest);
  82.  
  83.  
  84. public static ProperNeighbor fromAxis(EnumAxis axis, int v)
  85. {
  86. if ((v < -1) || (v > 1))
  87. throw new ArgumentOutOfRangeException(nameof(v), $"v must be within (-1, 1), is {v}");
  88. switch (axis) {
  89. case EnumAxis.X: return (v > 0) ? ProperNeighbor.East : ProperNeighbor.West;
  90. case EnumAxis.Y: return (v > 0) ? ProperNeighbor.Up : ProperNeighbor.Down;
  91. case EnumAxis.Z: return (v > 0) ? ProperNeighbor.South : ProperNeighbor.North;
  92. default: return ProperNeighbor.None;
  93. }
  94. }
  95.  
  96. public static EnumAxis getAxis(this ProperNeighbor self)
  97. {
  98. switch (self) {
  99. case ProperNeighbor.East : return EnumAxis.X;
  100. case ProperNeighbor.West : return EnumAxis.X;
  101. case ProperNeighbor.Up : return EnumAxis.Y;
  102. case ProperNeighbor.Down : return EnumAxis.Y;
  103. case ProperNeighbor.South : return EnumAxis.Z;
  104. case ProperNeighbor.North : return EnumAxis.Z;
  105. default: throw new ArgumentException(nameof(self), $"{self} is not one of FACINGS");
  106. }
  107. }
  108.  
  109. public static ProperNeighbor fromOffset(int x, int y, int z)
  110. {
  111. ProperNeighbor neighbor = ProperNeighbor.None;
  112. if (x != 0) {
  113. if (x == 1) neighbor |= ProperNeighbor.East;
  114. else if (x == -1) neighbor |= ProperNeighbor.West;
  115. else throw new ArgumentOutOfRangeException(nameof(x), $"x must be within (-1, 1), is {x}");
  116. }
  117. if (y != 0) {
  118. if (y == 1) neighbor |= ProperNeighbor.Up;
  119. else if (y == -1) neighbor |= ProperNeighbor.Down;
  120. else throw new ArgumentOutOfRangeException(nameof(y), $"y must be within (-1, 1), is {y}");
  121. }
  122. if (z != 0) {
  123. if (z == 1) neighbor |= ProperNeighbor.South;
  124. else if (z == -1) neighbor |= ProperNeighbor.North;
  125. else throw new ArgumentOutOfRangeException(nameof(z), $"z must be within (-1, 1), is {z}");
  126. }
  127. return neighbor;
  128.  
  129. // Alternative implementation:
  130. // if ((x < -1) || (x > 1)) throw new ArgumentOutOfRangeException(nameof(x), "x must be within (-1, 1)");
  131. // if ((y < -1) || (y > 1)) throw new ArgumentOutOfRangeException(nameof(y), "y must be within (-1, 1)");
  132. // if ((z < -1) || (z > 1)) throw new ArgumentOutOfRangeException(nameof(z), "z must be within (-1, 1)");
  133. // return ((x != 0) ? ((x > 0) ? ProperNeighbor.East : ProperNeighbor.West ) : 0)
  134. // | ((y != 0) ? ((y > 0) ? ProperNeighbor.Up : ProperNeighbor.Down ) : 0)
  135. // | ((z != 0) ? ((z > 0) ? ProperNeighbor.South : ProperNeighbor.North) : 0);
  136. }
  137.  
  138.  
  139. private const int X_SET_BIT = 0b100000, X_VALUE_BIT = 0b010000;
  140. private const int Y_SET_BIT = 0b001000, Y_VALUE_BIT = 0b000100;
  141. private const int Z_SET_BIT = 0b000010, Z_VALUE_BIT = 0b000001;
  142. public static void getOffset(this ProperNeighbor self,
  143. out int x, out int y, out int z)
  144. {
  145. x = (((int)self & X_SET_BIT) != 0) ? ((((int)self & X_VALUE_BIT) != 0) ? 1 : -1) : 0;
  146. y = (((int)self & Y_SET_BIT) != 0) ? ((((int)self & Y_VALUE_BIT) != 0) ? 1 : -1) : 0;
  147. z = (((int)self & Z_SET_BIT) != 0) ? ((((int)self & Z_VALUE_BIT) != 0) ? 1 : -1) : 0;
  148. }
  149.  
  150. public static ProperPos toProperPos(this ProperNeighbor self)
  151. {
  152. self.getOffset(out var x, out var y, out var z);
  153. return new ProperPos(x, y, z);
  154. }
  155. public static Vec3i toVec3i(this ProperNeighbor self)
  156. {
  157. self.getOffset(out var x, out var y, out var z);
  158. return new Vec3i(x, y, z);
  159. }
  160.  
  161.  
  162. public static bool isNone(this ProperNeighbor self)
  163. => (self == ProperNeighbor.None);
  164.  
  165. public static bool isHorizontal(this ProperNeighbor self)
  166. => HORIZONTALS.Contains(self);
  167. public static bool isVertical(this ProperNeighbor self)
  168. => VERTICALS.Contains(self);
  169. public static bool isCardinal(this ProperNeighbor self)
  170. => CARDINALS.Contains(self);
  171. public static bool isFacing(this ProperNeighbor self)
  172. => FACINGS.Contains(self);
  173. public static bool isValid(this ProperNeighbor self)
  174. => ALL.Contains(self);
  175.  
  176.  
  177. public static string ToShortString(this ProperNeighbor self)
  178. {
  179. if (!self.isValid()) return "-";
  180. var sb = new StringBuilder(3);
  181. foreach (var chr in self.ToString())
  182. if ((chr >= 'A') && (chr <= 'Z')) // ASCII IsUpper
  183. sb.Append(chr + 0x20); // ASCII ToUpper
  184. return sb.ToString();
  185. }
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement