Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1.     public struct DDA {
  2.         int n, ix;
  3.         int[] start, end;
  4.         int[] delta, step, counter;
  5.         public int[] position;
  6.  
  7.         public DDA(int n, int[] start, int[] end) {
  8.             this.n = n;
  9.             if (start.Length != n || end.Length != n) {
  10.                 throw new ArgumentException();
  11.             }
  12.             this.start = start;
  13.             this.end = end;
  14.             position = new int[n];
  15.             delta = new int[n];
  16.             step = new int[n];
  17.             counter = new int[n];
  18.             ix = 0;
  19.             Setup();
  20.         }
  21.  
  22.         private void Setup() {
  23.             for (int i = 0; i < n; i++) {
  24.                 position[i] = start[i];
  25.                 delta[i] = end[i] - start[i];
  26.                 step[i] = 0;
  27.  
  28.                 if (delta[i] > 0) {
  29.                     step[i] = 1;
  30.                 }
  31.  
  32.                 if (delta[i] < 0) {
  33.                     step[i] = -1;
  34.                     delta[i] = -delta[i];
  35.                 }
  36.  
  37.                 if (delta[ix] < delta[i]) {
  38.                     ix = i;
  39.                 }
  40.             }
  41.  
  42.             for (int i = 0; i < n; i++) {
  43.                 counter[i] = delta[ix];
  44.             }
  45.         }
  46.  
  47.         public bool Update() {
  48.             for (int i = 0; i < n; i++) {
  49.                 counter[i] -= delta[i];
  50.                 if (counter[i] <= 0) {
  51.                     counter[i] += delta[ix];
  52.                     position[i] += step[i];
  53.                 }
  54.             }
  55.             return position[ix] != end[ix] + step[ix];
  56.         }
  57.     }
  58.  
  59.         public (Block, Box?) GetLookAtBlock(Vector3 pos, Vector3 look) {
  60.             try {
  61.                 int[] start = new[] {
  62.                     SandboxMath.RoundDown(pos.X),
  63.                     SandboxMath.RoundDown(pos.Y),
  64.                     SandboxMath.RoundDown(pos.Z)
  65.                 };
  66.  
  67.                 look = look.Normalized() * 4;
  68.  
  69.                 int[] end = new[] {
  70.                     SandboxMath.RoundDown(pos.X + look.X),
  71.                     SandboxMath.RoundDown(pos.Y + look.Y),
  72.                     SandboxMath.RoundDown(pos.Z + look.Z)
  73.                 };
  74.  
  75.                 DDA dda = new DDA(3, start, end);
  76.  
  77.                 while (dda.Update()) {
  78.                     Block block = GetBlock(dda.position[0], dda.position[1], dda.position[2]);
  79.                     if (block != 0) {
  80.                         return (
  81.                             block,
  82.                             new Box(
  83.                                 new Vector3(dda.position[0], dda.position[1], dda.position[2]),
  84.                                 new Vector3(dda.position[0] + 1, dda.position[1] + 1, dda.position[2] + 1)
  85.                             )
  86.                         );
  87.                     }
  88.                 }
  89.                 return (0, null);
  90.             } catch (IndexOutOfRangeException) {
  91.                 return (0, null);
  92.             }
  93.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement