Search in sources :

Example 1 with ProtectedPolygonalRegion

use of com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion in project AreaShop by NLthijs48.

the class GeneralRegion method calculateVolume.

/**
 * Calculate the volume of the region (could be expensive for polygon regions).
 * @return Number of blocks in the region
 */
private long calculateVolume() {
    // Use own calculation for polygon regions, as WorldGuard does not implement it and returns 0
    ProtectedRegion region = getRegion();
    if (region instanceof ProtectedPolygonalRegion) {
        BlockVector min = region.getMinimumPoint();
        BlockVector max = region.getMaximumPoint();
        // Exact, but slow algorithm
        if (getWidth() * getDepth() < 100) {
            long surface = 0;
            for (int x = min.getBlockX(); x <= max.getBlockX(); x++) {
                for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
                    if (region.contains(x, min.getBlockY(), z)) {
                        surface++;
                    }
                }
            }
            return surface * getHeight();
        } else // Estimate, but quick algorithm
        {
            List<BlockVector2D> points = region.getPoints();
            int numPoints = points.size();
            if (numPoints < 3) {
                return 0;
            }
            double area = 0;
            int x1, x2, z1, z2;
            for (int i = 0; i <= numPoints - 2; i++) {
                x1 = points.get(i).getBlockX();
                z1 = points.get(i).getBlockZ();
                x2 = points.get(i + 1).getBlockX();
                z2 = points.get(i + 1).getBlockZ();
                area = area + ((z1 + z2) * (x1 - x2));
            }
            x1 = points.get(numPoints - 1).getBlockX();
            z1 = points.get(numPoints - 1).getBlockZ();
            x2 = points.get(0).getBlockX();
            z2 = points.get(0).getBlockZ();
            area = area + ((z1 + z2) * (x1 - x2));
            area = Math.ceil(Math.abs(area) / 2);
            return (long) (area * getHeight());
        }
    } else {
        return region.volume();
    }
}
Also used : BlockVector2D(com.sk89q.worldedit.BlockVector2D) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) BlockVector(com.sk89q.worldedit.BlockVector)

Aggregations

BlockVector (com.sk89q.worldedit.BlockVector)1 BlockVector2D (com.sk89q.worldedit.BlockVector2D)1 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)1 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)1