Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public struct DDA {
- int n, ix;
- int[] start, end;
- int[] delta, step, counter;
- public int[] position;
- public DDA(int n, int[] start, int[] end) {
- this.n = n;
- if (start.Length != n || end.Length != n) {
- throw new ArgumentException();
- }
- this.start = start;
- this.end = end;
- position = new int[n];
- delta = new int[n];
- step = new int[n];
- counter = new int[n];
- ix = 0;
- Setup();
- }
- private void Setup() {
- for (int i = 0; i < n; i++) {
- position[i] = start[i];
- delta[i] = end[i] - start[i];
- step[i] = 0;
- if (delta[i] > 0) {
- step[i] = 1;
- }
- if (delta[i] < 0) {
- step[i] = -1;
- delta[i] = -delta[i];
- }
- if (delta[ix] < delta[i]) {
- ix = i;
- }
- }
- for (int i = 0; i < n; i++) {
- counter[i] = delta[ix];
- }
- }
- public bool Update() {
- for (int i = 0; i < n; i++) {
- counter[i] -= delta[i];
- if (counter[i] <= 0) {
- counter[i] += delta[ix];
- position[i] += step[i];
- }
- }
- return position[ix] != end[ix] + step[ix];
- }
- }
- public (Block, Box?) GetLookAtBlock(Vector3 pos, Vector3 look) {
- try {
- int[] start = new[] {
- SandboxMath.RoundDown(pos.X),
- SandboxMath.RoundDown(pos.Y),
- SandboxMath.RoundDown(pos.Z)
- };
- look = look.Normalized() * 4;
- int[] end = new[] {
- SandboxMath.RoundDown(pos.X + look.X),
- SandboxMath.RoundDown(pos.Y + look.Y),
- SandboxMath.RoundDown(pos.Z + look.Z)
- };
- DDA dda = new DDA(3, start, end);
- while (dda.Update()) {
- Block block = GetBlock(dda.position[0], dda.position[1], dda.position[2]);
- if (block != 0) {
- return (
- block,
- new Box(
- new Vector3(dda.position[0], dda.position[1], dda.position[2]),
- new Vector3(dda.position[0] + 1, dda.position[1] + 1, dda.position[2] + 1)
- )
- );
- }
- }
- return (0, null);
- } catch (IndexOutOfRangeException) {
- return (0, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement