|
| 1 | +package net.quarrymod.blockentity.machine.tier3; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Queue; |
| 5 | +import net.minecraft.util.math.BlockPos; |
| 6 | + |
| 7 | +public class MiningUtil { |
| 8 | + |
| 9 | + private MiningUtil() { |
| 10 | + // Hide constructor |
| 11 | + } |
| 12 | + |
| 13 | + /** |
| 14 | + * Creates an queue of positions that should be mined starting at the center of the position. |
| 15 | + * Positions are loaded in a circular pattern, for radius 2, this will be the loaded outcome. |
| 16 | + * 25 > 24 > 23 > 22 > 21 |
| 17 | + * V |
| 18 | + * 10 > 9 > 8 > 7 20 |
| 19 | + * ^ V V |
| 20 | + * 11 2 > [1] 6 19 |
| 21 | + * ^ ^ V V |
| 22 | + * 12 3 < 4 < 5 18 |
| 23 | + * ^ V |
| 24 | + * 13 < 14 < 15 < 16 < 17 |
| 25 | + * @param radius to use around the position |
| 26 | + * @param miningPosition the position the center of the shaft. |
| 27 | + * @return A list of circular positions to mine. |
| 28 | + */ |
| 29 | + public static Queue<BlockPos> createMiningPosition(int radius, BlockPos miningPosition) { |
| 30 | + LinkedList<BlockPos> minePositions = new LinkedList<>(); |
| 31 | + |
| 32 | + // Start from negative radius |
| 33 | + int k = -radius; |
| 34 | + int l = -radius; |
| 35 | + // Add 1 to prevent off-by-one errors. (including end) |
| 36 | + int m = radius + 1; |
| 37 | + int n = radius + 1; |
| 38 | + |
| 39 | + // |
| 40 | + // https://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/?ref=lbp |
| 41 | + // Explanation of the algorithm: |
| 42 | + // k - starting row index ( negative radius ) |
| 43 | + // m - ending row index ( positive radius ) |
| 44 | + // l - starting column index ( negative radius ) |
| 45 | + // n - ending column index ( positive radius ) |
| 46 | + // |
| 47 | + |
| 48 | + int i; |
| 49 | + while (k < m && l < n) { |
| 50 | + // Add the first row from the remaining rows |
| 51 | + for (i = l; i < n; ++i) { |
| 52 | + minePositions.addFirst(miningPosition.add(k, 0, i)); |
| 53 | + } |
| 54 | + k++; |
| 55 | + |
| 56 | + // Add the last column from the remaining columns |
| 57 | + for (i = k; i < m; ++i) { |
| 58 | + minePositions.addFirst(miningPosition.add(i, 0, n - 1)); |
| 59 | + } |
| 60 | + n--; |
| 61 | + |
| 62 | + // Add last remaining from the row */ |
| 63 | + if (k < m) { |
| 64 | + for (i = n - 1; i >= l; --i) { |
| 65 | + minePositions.addFirst(miningPosition.add((m - 1), 0, i)); |
| 66 | + } |
| 67 | + m--; |
| 68 | + } |
| 69 | + |
| 70 | + // Add the first column from the remaining columns */ |
| 71 | + if (l < n) { |
| 72 | + for (i = m - 1; i >= k; --i) { |
| 73 | + minePositions.addFirst(miningPosition.add(i, 0, l)); |
| 74 | + } |
| 75 | + l++; |
| 76 | + } |
| 77 | + } |
| 78 | + return minePositions; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +} |
0 commit comments