001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.util;
020
021import com.plotsquared.core.location.Location;
022import com.sk89q.worldedit.math.BlockVector2;
023import org.checkerframework.checker.nullness.qual.NonNull;
024
025/**
026 * This cache is used for world generation and just saves a bit of calculation time when checking if something is in the plot area.
027 */
028public class ChunkUtil {
029
030    /**
031     * Cache of mapping x,y,z coordinates to the chunk array<br>
032     * - Used for efficient world generation<br>
033     */
034    private static final short[] x_loc;
035    private static final short[] y_loc;
036    private static final short[] z_loc;
037    private static final short[][][] CACHE_J;
038
039    static {
040        x_loc = new short[4096];
041        y_loc = new short[4096];
042        z_loc = new short[4096];
043        for (int j = 0; j < 4096; j++) {
044            int y = j >> 8;
045            int a = j - ((y & 0xF) << 8);
046            int z1 = a >> 4;
047            int x1 = a - (z1 << 4);
048            x_loc[j] = (short) x1;
049            y_loc[j] = (short) y;
050            z_loc[j] = (short) z1;
051        }
052        CACHE_J = new short[16][16][16];
053        for (int x = 0; x < 16; x++) {
054            for (int z = 0; z < 16; z++) {
055                for (int y = 0; y < 16; y++) {
056                    short j = (short) ((y & 0xF) << 8 | z << 4 | x);
057                    CACHE_J[y][x][z] = j;
058                }
059            }
060        }
061    }
062
063    private ChunkUtil() {
064    }
065
066    /**
067     * Get the J value for Chunk block storage from the chunk xyz coordinates.
068     * J is in the range 0 to 4095 where it represents a position in an array of 16x16x16 xyz (ChunkSection  Array[4096]).
069     *
070     * @param x Relative x coordinate
071     * @param y Relative y coordinate
072     * @param z Relative z coordinate
073     * @return J value for xyz position in Array[4096].
074     */
075    public static int getJ(int x, int y, int z) {
076        return CACHE_J[y & 15][x & 15][z & 15];
077    }
078
079    /**
080     * Gets the x coordinate for a specific J value for a ChunkSection 16x16x16 xyz Array[4096].
081     *
082     * @param j Position in the xyz Array[4096].
083     * @return x coordinate within the chunk
084     */
085    public static int getX(int j) {
086        return x_loc[j];
087    }
088
089    /**
090     * Gets the y coordinate for specific I and J values for a Chunk Nx16x16x16 layerxyz Array[N][4096].
091     *
092     * @param i Relative layer of the position in the layerxyz Array[16][4096]. May be negative.
093     * @param j Position in the xyz Array[4096].
094     * @return x coordinate within the chunk
095     */
096    public static int getY(int i, int j) {
097        return (i << 4) + y_loc[j];
098    }
099
100    /**
101     * Gets the z coordinate for a specific J value for a ChunkSection 16x16x16 xyz Array[4096].
102     *
103     * @param j Position in the xyz Array[4096].
104     * @return z coordinate within the chunk
105     */
106    public static int getZ(int j) {
107        return z_loc[j];
108    }
109
110    /**
111     * Returns true if the region pos1-pos2 contains the chunk
112     *
113     * @param pos1  Region minimum point
114     * @param pos2  Region maximum point
115     * @param chunk BlockVector2 of chunk coordinates
116     * @return {@code true} if the region pos1-pos2 contains the chunk
117     * @deprecated Unused internally. Scheduled for removal in next major release.
118     */
119    @Deprecated(forRemoval = true, since = "6.11.1")
120    public static boolean isWholeChunk(@NonNull Location pos1, @NonNull Location pos2, @NonNull BlockVector2 chunk) {
121        int x1 = pos1.getX();
122        int z1 = pos1.getZ();
123        int x2 = pos2.getX();
124        int z2 = pos2.getZ();
125        int cx = chunk.getX() << 4;
126        int cz = chunk.getZ() << 4;
127        return cx > x1 && cz > z1 && cx < x2 && cz < z2;
128    }
129
130}