From 3171fbd953bfadb002fada5f7b8be4586620a994 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sun, 18 Jun 2017 09:50:25 +1000 Subject: [PATCH] Cache angle mask + tweak updater --- .../boydti/fawe/object/mask/AngleMask.java | 50 +++++++++++++++++-- .../java/com/boydti/fawe/util/Updater.java | 5 +- .../com/sk89q/worldedit/extent/Extent.java | 2 +- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/object/mask/AngleMask.java b/core/src/main/java/com/boydti/fawe/object/mask/AngleMask.java index 8139fbbb..97852513 100644 --- a/core/src/main/java/com/boydti/fawe/object/mask/AngleMask.java +++ b/core/src/main/java/com/boydti/fawe/object/mask/AngleMask.java @@ -6,6 +6,7 @@ import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.function.mask.Mask2D; import com.sk89q.worldedit.function.mask.SolidBlockMask; +import java.util.Arrays; import javax.annotation.Nullable; public class AngleMask extends SolidBlockMask implements ResettableMask { @@ -30,6 +31,47 @@ public class AngleMask extends SolidBlockMask implements ResettableMask { @Override public void reset() { mutable = new MutableBlockVector(); + cacheBotX = Integer.MIN_VALUE; + cacheBotZ = Integer.MIN_VALUE; + if (cacheHeights != null) { + Arrays.fill(cacheHeights, (byte) 0); + } + } + + private transient int cacheCenX; + private transient int cacheCenZ; + private transient int cacheBotX = Integer.MIN_VALUE; + private transient int cacheBotZ = Integer.MIN_VALUE; + private transient int cacheCenterZ; + private transient byte[] cacheHeights = null; + private transient int lastY = 0; + + public int getHeight(int x, int y, int z) { + try { + int rx = x - cacheBotX; + int rz = z - cacheBotZ; + int index = rx + (rz << 8); + if (index < 0 || index >= 65536) { + cacheBotX = x - 16; + cacheBotZ = z - 16; + rx = x - cacheBotX; + rz = z - cacheBotZ; + index = rx + (rz << 8); + if (cacheHeights == null) { + cacheHeights = new byte[65536]; + } else { + Arrays.fill(cacheHeights, (byte) 0); + } + } + lastY = cacheHeights[index] & 0xFF; + if (lastY == 0) { + cacheHeights[index] = (byte) (lastY = getExtent().getNearestSurfaceTerrainBlock(x, z, lastY, 0, maxY)); + } + return lastY; + } catch (Throwable e) { + e.printStackTrace(); + throw e; + } } @Override @@ -47,13 +89,13 @@ public class AngleMask extends SolidBlockMask implements ResettableMask { } double slope; boolean aboveMin; - slope = Math.abs(getExtent().getNearestSurfaceTerrainBlock(x + 1, z, y, 0, maxY) - getExtent().getNearestSurfaceTerrainBlock(x - 1, z, y, 0, maxY)) * ADJACENT_MOD; + slope = Math.abs(getHeight(x + 1, y, z) - getHeight(x - 1, y, z)) * ADJACENT_MOD; if (slope >= min && max >= Math.max(maxY - y, y)) { return true; } - slope = Math.max(slope, Math.abs(getExtent().getNearestSurfaceTerrainBlock(x, z + 1, y, 0, maxY) - getExtent().getNearestSurfaceTerrainBlock(x, z - 1, y, 0, maxY)) * ADJACENT_MOD); - slope = Math.max(slope, Math.abs(getExtent().getNearestSurfaceTerrainBlock(x + 1, z + 1, y, 0, maxY) - getExtent().getNearestSurfaceTerrainBlock(x - 1, z - 1, y, 0, maxY)) * DIAGONAL_MOD); - slope = Math.max(slope, Math.abs(getExtent().getNearestSurfaceTerrainBlock(x - 1, z + 1, y, 0, maxY) - getExtent().getNearestSurfaceTerrainBlock(x + 1, z - 1, y, 0, maxY)) * DIAGONAL_MOD); + slope = Math.max(slope, Math.abs(getHeight(x, y, z + 1) - getHeight(x, y, z - 1)) * ADJACENT_MOD); + slope = Math.max(slope, Math.abs(getHeight(x + 1, y, z + 1) - getHeight(x - 1, y, z - 1)) * DIAGONAL_MOD); + slope = Math.max(slope, Math.abs(getHeight(x - 1, y, z + 1) - getHeight(x + 1, y, z - 1)) * DIAGONAL_MOD); return (slope >= min && slope <= max); } diff --git a/core/src/main/java/com/boydti/fawe/util/Updater.java b/core/src/main/java/com/boydti/fawe/util/Updater.java index ae536a36..cda8a00b 100644 --- a/core/src/main/java/com/boydti/fawe/util/Updater.java +++ b/core/src/main/java/com/boydti/fawe/util/Updater.java @@ -46,7 +46,9 @@ public class Updater { URL download = new URL(downloadUrl.replaceAll("%platform%", platform).replaceAll("%version%", versionString)); try (ReadableByteChannel rbc = Channels.newChannel(download.openStream())) { File jarFile = MainUtil.getJarFile(); - File outFile = new File(jarFile.getParent(), "update" + File.separator + jarFile.getName()); + + File finalFile = new File(jarFile.getParent(), "update" + File.separator + jarFile.getName()); + File outFile = new File(jarFile.getParent(), "update" + File.separator + jarFile.getName().replace(".jar", ".part")); boolean exists = outFile.exists(); if (exists) { outFile.delete(); @@ -59,6 +61,7 @@ public class Updater { try (FileOutputStream fos = new FileOutputStream(outFile)) { fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } + outFile.renameTo(finalFile); Fawe.debug("Updated FAWE to " + versionString); MainUtil.sendAdmin("&7Restart to update FAWE with these changes: &c/fawe changelog &7or&c " + "http://empcraft.com/fawe/cl?" + Integer.toHexString(currentVersion.hash)); } diff --git a/core/src/main/java/com/sk89q/worldedit/extent/Extent.java b/core/src/main/java/com/sk89q/worldedit/extent/Extent.java index 0cb8089b..b3592439 100644 --- a/core/src/main/java/com/sk89q/worldedit/extent/Extent.java +++ b/core/src/main/java/com/sk89q/worldedit/extent/Extent.java @@ -138,7 +138,7 @@ public interface Extent extends InputExtent, OutputExtent { } } } - return maxY >= 255 ? maxY : -1; + return maxY == 255 && minY == 0 ? maxY : -1; } default public void addCaves(Region region) throws WorldEditException {