Skip to content

Instantly share code, notes, and snippets.

@kylepls
Created October 24, 2018 18:30
Show Gist options
  • Save kylepls/854ff46c2508825d7348696f2132b389 to your computer and use it in GitHub Desktop.
Save kylepls/854ff46c2508825d7348696f2132b389 to your computer and use it in GitHub Desktop.
package in.kyle.plotz2.util;
import in.kyle.plotz2.obj.player.PlotLocation;
import lombok.Data;
/**
* Created by Kyle on Jul 24, 2015
*/
public class PlotIdUtils {
public static int getBid(PlotLocation plotLocation) {
return getBid(new WorldPoint(plotLocation.getX(), plotLocation.getZ()));
}
public static int getBid(WorldPoint worldPoint) {
int x = worldPoint.getX();
int y = worldPoint.getZ();
int n = -1;
int m = Math.max(Math.abs(x), Math.abs(y));
if (x == m && y != -m) {
n = (4 * m) * (m - 1) + m + y + 1;
} else if (y == m) {
n = (4 * m) * (m - 1) + (3 * m) - x + 1;
} else if (x == -m) {
n = (4 * m) * (m - 1) + (5 * m) - y + 1;
} else if (y == -m) {
n = (4 * m) * (m - 1) + (7 * m) + x + 1;
}
return n;
}
public static WorldPoint getPoint(int tileNum) {
tileNum = tileNum - 1;
Conditions.isTrue(tileNum > -1, "Tile cannot be less than 1");
double intRoot = Math.floor(Math.sqrt(tileNum));
int x = (int) ((Math.round(intRoot / 2) * Math.pow(-1, intRoot + 1)) + (Math.pow(-1, intRoot + 1) * (((intRoot * (intRoot + 1)) -
tileNum) - Math.abs((intRoot * (intRoot + 1)) - tileNum)) / 2));
int y = (int) ((Math.round(intRoot / 2) * Math.pow(-1, intRoot)) + (Math.pow(-1, intRoot + 1) * (((intRoot * (intRoot + 1)) -
tileNum) + Math.abs((intRoot * (intRoot + 1)) - tileNum)) / 2));
return new WorldPoint(x, -y);
}
@Data
public static class WorldPoint {
private final int x;
private final int z;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment