13
0
geforkt von Mirrors/Paper

[Bleeding] Optimized locToBlock. Addresses BUKKIT-815

By: Wesley Wolfe <weswolf@aol.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2012-02-29 16:07:47 -06:00
Ursprung 43002d0cbb
Commit fa6a95bc3f
4 geänderte Dateien mit 22 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -1,6 +1,7 @@
package org.bukkit; package org.bukkit;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.util.NumberConversions;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
/** /**
@ -485,6 +486,6 @@ public class Location implements Cloneable {
* @return Block coordinate * @return Block coordinate
*/ */
public static int locToBlock(double loc) { public static int locToBlock(double loc) {
return (int) Math.floor(loc); return NumberConversions.floor(loc);
} }
} }

Datei anzeigen

@ -1,5 +1,7 @@
package org.bukkit.util; package org.bukkit.util;
import static org.bukkit.util.NumberConversions.*;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -66,7 +68,7 @@ public class BlockIterator implements Iterator<Block> {
double secondPosition = 0; double secondPosition = 0;
double thirdPosition = 0; double thirdPosition = 0;
Block startBlock = this.world.getBlockAt((int) Math.floor(startClone.getX()), (int) Math.floor(startClone.getY()), (int) Math.floor(startClone.getZ())); Block startBlock = this.world.getBlockAt(floor(startClone.getX()), floor(startClone.getY()), floor(startClone.getZ()));
if (getXLength(direction) > mainDirection) { if (getXLength(direction) > mainDirection) {
mainFace = getXFace(direction); mainFace = getXFace(direction);
@ -117,10 +119,10 @@ public class BlockIterator implements Iterator<Block> {
// Guarantee that the ray will pass though the start block. // Guarantee that the ray will pass though the start block.
// It is possible that it would miss due to rounding // It is possible that it would miss due to rounding
// This should only move the ray by 1 grid position // This should only move the ray by 1 grid position
secondError = (int) (Math.floor(secondd * gridSize)); secondError = floor(secondd * gridSize);
secondStep = (int) (Math.round(secondDirection / mainDirection * gridSize)); secondStep = round(secondDirection / mainDirection * gridSize);
thirdError = (int) (Math.floor(thirdd * gridSize)); thirdError = floor(thirdd * gridSize);
thirdStep = (int) (Math.round(thirdDirection / mainDirection * gridSize)); thirdStep = round(thirdDirection / mainDirection * gridSize);
if (secondError + secondStep <= 0) { if (secondError + secondStep <= 0) {
secondError = -secondStep + 1; secondError = -secondStep + 1;
@ -168,7 +170,7 @@ public class BlockIterator implements Iterator<Block> {
} }
// Calculate the number of planes passed to give max distance // Calculate the number of planes passed to give max distance
maxDistanceInt = (int) Math.round(maxDistance / (Math.sqrt(mainDirection * mainDirection + secondDirection * secondDirection + thirdDirection * thirdDirection) / mainDirection)); maxDistanceInt = round(maxDistance / (Math.sqrt(mainDirection * mainDirection + secondDirection * secondDirection + thirdDirection * thirdDirection) / mainDirection));
} }

Datei anzeigen

@ -6,6 +6,15 @@ package org.bukkit.util;
public final class NumberConversions { public final class NumberConversions {
private NumberConversions() {} private NumberConversions() {}
public static int floor(double num) {
final int floor = (int) num;
return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63);
}
public static int round(double num) {
return floor(num + 0.5d);
}
public static int toInt(Object object) { public static int toInt(Object object) {
if (object instanceof Number) { if (object instanceof Number) {
return ((Number) object).intValue(); return ((Number) object).intValue();

Datei anzeigen

@ -365,7 +365,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return block X * @return block X
*/ */
public int getBlockX() { public int getBlockX() {
return (int) Math.floor(x); return NumberConversions.floor(x);
} }
/** /**
@ -384,7 +384,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return block y * @return block y
*/ */
public int getBlockY() { public int getBlockY() {
return (int) Math.floor(y); return NumberConversions.floor(y);
} }
/** /**
@ -403,7 +403,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
* @return block z * @return block z
*/ */
public int getBlockZ() { public int getBlockZ() {
return (int) Math.floor(z); return NumberConversions.floor(z);
} }
/** /**