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
021/**
022 * This cache is used for world generation and just saves a bit of calculation time when checking if something is in the plot area.
023 */
024public class ChunkUtil {
025
026    /**
027     * Cache of mapping x,y,z coordinates to the chunk array<br>
028     * - Used for efficient world generation<br>
029     */
030    private static final short[] x_loc;
031    private static final short[] y_loc;
032    private static final short[] z_loc;
033    private static final short[][][] CACHE_J;
034
035    static {
036        x_loc = new short[4096];
037        y_loc = new short[4096];
038        z_loc = new short[4096];
039        for (int j = 0; j < 4096; j++) {
040            int y = j >> 8;
041            int a = j - ((y & 0xF) << 8);
042            int z1 = a >> 4;
043            int x1 = a - (z1 << 4);
044            x_loc[j] = (short) x1;
045            y_loc[j] = (short) y;
046            z_loc[j] = (short) z1;
047        }
048        CACHE_J = new short[16][16][16];
049        for (int x = 0; x < 16; x++) {
050            for (int z = 0; z < 16; z++) {
051                for (int y = 0; y < 16; y++) {
052                    short j = (short) ((y & 0xF) << 8 | z << 4 | x);
053                    CACHE_J[y][x][z] = j;
054                }
055            }
056        }
057    }
058
059    private ChunkUtil() {
060    }
061
062    /**
063     * Get the J value for Chunk block storage from the chunk xyz coordinates.
064     * J is in the range 0 to 4095 where it represents a position in an array of 16x16x16 xyz (ChunkSection  Array[4096]).
065     *
066     * @param x Relative x coordinate
067     * @param y Relative y coordinate
068     * @param z Relative z coordinate
069     * @return J value for xyz position in Array[4096].
070     */
071    public static int getJ(int x, int y, int z) {
072        return CACHE_J[y & 15][x & 15][z & 15];
073    }
074
075    /**
076     * Gets the x coordinate for a specific J value for a ChunkSection 16x16x16 xyz Array[4096].
077     *
078     * @param j Position in the xyz Array[4096].
079     * @return x coordinate within the chunk
080     */
081    public static int getX(int j) {
082        return x_loc[j];
083    }
084
085    /**
086     * Gets the y coordinate for specific I and J values for a Chunk Nx16x16x16 layerxyz Array[N][4096].
087     *
088     * @param i Relative layer of the position in the layerxyz Array[16][4096]. May be negative.
089     * @param j Position in the xyz Array[4096].
090     * @return x coordinate within the chunk
091     */
092    public static int getY(int i, int j) {
093        return (i << 4) + y_loc[j];
094    }
095
096    /**
097     * Gets the z coordinate for a specific J value for a ChunkSection 16x16x16 xyz Array[4096].
098     *
099     * @param j Position in the xyz Array[4096].
100     * @return z coordinate within the chunk
101     */
102    public static int getZ(int j) {
103        return z_loc[j];
104    }
105
106}