From 41a457920de0451abb35e0f860b8edd4ce8ad53e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 2 Oct 2022 15:46:22 +0200 Subject: [PATCH] Optimize Laufbau Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 6 +- BauSystem_Main/src/BauSystem_de.properties | 6 +- .../features/slaves/laufbau/Laufbau.java | 53 +---- .../states/CreatingInnerBlocksState.java | 111 ---------- .../laufbau/states/CreatingLaufState.java | 200 ++++++++++++++++++ .../states/CreatingOuterBlocksState.java | 85 -------- .../laufbau/states/ExpandingTracesState.java | 82 ------- .../laufbau/states/ProcessingTracesState.java | 65 ++++-- .../states/ShrinkingOuterPointsState.java | 92 -------- .../laufbau/states/SplittingPointsState.java | 89 -------- 10 files changed, 265 insertions(+), 524 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingInnerBlocksState.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingLaufState.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingOuterBlocksState.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ExpandingTracesState.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ShrinkingOuterPointsState.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/SplittingPointsState.java diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index ca7b8228..5fe51591 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -1103,11 +1103,7 @@ LAUFBAU_NO_PERM = §cYou are not allowed to use the barrel building system here LAUFBAU_NO_WORLDEDIT = §cYou don't have a WorldEdit selection LAUFBAU_STATE_FILTERING_TRACES = Filtering traces LAUFBAU_STATE_PROCESSING_TRACES = Connnecting traces -LAUFBAU_STATE_EXPANDING_TRACES = Expanding traces -LAUFBAU_STATE_SPLITTING_POINTS = Splitting points -LAUFBAU_STATE_SHRINKING_POINTS = Shrinking points -LAUFBAU_STATE_CREATING_INNER_BLOCKS = Creating inner blocks -LAUFBAU_STATE_CREATING_OUTER_BLOCKS = Creating outer blocks +LAUFBAU_STATE_CREATE_LAUF = Create Barrel LAUFBAU_SIMPLE_PROGRESS = §e{0}§8: §e{1}§8/§e{2} §7Time left§8: §e{3} LAUFBAU_DONE = §aDone diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index be092402..439518b6 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -1081,11 +1081,7 @@ LAUFBAU_NO_PERM = §cDu darfst hier nicht das Laufbau System verwenden LAUFBAU_NO_WORLDEDIT = §cDu hast keine WorldEdit Selection LAUFBAU_STATE_FILTERING_TRACES = Traces filtern LAUFBAU_STATE_PROCESSING_TRACES = Traces verbinden -LAUFBAU_STATE_EXPANDING_TRACES = Traces aufblähen -LAUFBAU_STATE_SPLITTING_POINTS = Punkte trennen -LAUFBAU_STATE_SHRINKING_POINTS = Punkte ausdünnen -LAUFBAU_STATE_CREATING_INNER_BLOCKS = Innenraum füllen -LAUFBAU_STATE_CREATING_OUTER_BLOCKS = Mantel bauen +LAUFBAU_STATE_CREATE_LAUF = Lauf erstellen LAUFBAU_SIMPLE_PROGRESS = §e{0}§8: §e{1}§8/§e{2} §7Übrige Zeit §8: §e{3} LAUFBAU_DONE = §aZuende gebaut diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java index 6c22c4aa..d1e37613 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/Laufbau.java @@ -20,8 +20,11 @@ package de.steamwar.bausystem.features.slaves.laufbau; import com.sk89q.worldedit.EditSession; +import de.steamwar.bausystem.features.slaves.laufbau.states.FilteringTracesState; +import de.steamwar.bausystem.features.slaves.laufbau.states.LaufbauState; +import de.steamwar.bausystem.features.slaves.laufbau.states.CreatingLaufState; +import de.steamwar.bausystem.features.slaves.laufbau.states.ProcessingTracesState; import de.steamwar.bausystem.utils.WorldEditUtils; -import de.steamwar.bausystem.features.slaves.laufbau.states.*; import lombok.Getter; import org.bukkit.Location; import org.bukkit.World; @@ -37,15 +40,9 @@ public class Laufbau { private Location pos1; private Location pos2; - private boolean preferingBlastResistance; - private FilteringTracesState filteringTracesState = null; private ProcessingTracesState processingTracesState = null; - private ExpandingTracesState expandingTracesState = null; - private SplittingPointsState splittingPointsState = null; - private ShrinkingOuterPointsState shrinkingOuterPointsState = null; - private CreatingInnerBlocksState creatingInnerBlocksState = null; - private CreatingOuterBlocksState creatingOuterBlocksState = null; + private CreatingLaufState creatingLaufState = null; private List elements; @@ -57,8 +54,6 @@ public class Laufbau { this.pos1 = new Location(world, Math.min(pos1.getBlockX(), pos2.getBlockX()), Math.min(pos1.getBlockY(), pos2.getBlockY()), Math.min(pos1.getBlockZ(), pos2.getBlockZ())); this.pos2 = new Location(world, Math.max(pos1.getBlockX(), pos2.getBlockX()), Math.max(pos1.getBlockY(), pos2.getBlockY()), Math.max(pos1.getBlockZ(), pos2.getBlockZ())); - this.preferingBlastResistance = preferingBlastResistance; - filteringTracesState = new FilteringTracesState(world, this::inRegion); editSession = WorldEditUtils.getEditSession(world); @@ -86,20 +81,8 @@ public class Laufbau { } private LaufbauState getActive() { - if (creatingOuterBlocksState != null) { - return creatingOuterBlocksState; - } - if (creatingInnerBlocksState != null) { - return creatingInnerBlocksState; - } - if (shrinkingOuterPointsState != null) { - return shrinkingOuterPointsState; - } - if (splittingPointsState != null) { - return splittingPointsState; - } - if (expandingTracesState != null) { - return expandingTracesState; + if (creatingLaufState != null) { + return creatingLaufState; } if (processingTracesState != null) { return processingTracesState; @@ -108,30 +91,14 @@ public class Laufbau { } private void createNextState() { - if (creatingOuterBlocksState != null) { - return; - } - if (creatingInnerBlocksState != null) { - creatingOuterBlocksState = new CreatingOuterBlocksState(shrinkingOuterPointsState.getShrunkOuterPoints(), world, editSession); - return; - } - if (shrinkingOuterPointsState != null) { - creatingInnerBlocksState = new CreatingInnerBlocksState(splittingPointsState.getInnerPoints(), world, splittingPointsState.getIntersectedCuboid(), editSession, elements); - return; - } - if (splittingPointsState != null) { - shrinkingOuterPointsState = new ShrinkingOuterPointsState(splittingPointsState.getOuterPoints(), splittingPointsState.getInnerPoints()); - return; - } - if (expandingTracesState != null) { - splittingPointsState = new SplittingPointsState(expandingTracesState.getBlocks(), processingTracesState.getCuboidList()); + if (creatingLaufState != null) { return; } if (processingTracesState != null) { - expandingTracesState = new ExpandingTracesState(processingTracesState.getCuboidList(), world, this::inRegion); + creatingLaufState = new CreatingLaufState(processingTracesState.getBlocks(), processingTracesState.getCuboidList(), world, editSession, elements); return; } - processingTracesState = new ProcessingTracesState(filteringTracesState.getTntPositions()); + processingTracesState = new ProcessingTracesState(filteringTracesState.getTntPositions(), world, this::inRegion); } public String actionBarMessage(Player p) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingInnerBlocksState.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingInnerBlocksState.java deleted file mode 100644 index 6e6b3be1..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingInnerBlocksState.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.features.slaves.laufbau.states; - -import com.sk89q.worldedit.EditSession; -import com.sk89q.worldedit.MaxChangedBlocksException; -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.features.slaves.laufbau.BlockBoundingBox; -import de.steamwar.bausystem.features.slaves.laufbau.Cuboid; -import de.steamwar.bausystem.region.Point; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.Player; - -import java.text.SimpleDateFormat; -import java.time.Instant; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -public class CreatingInnerBlocksState implements LaufbauState { - - private long start = System.currentTimeMillis(); - - private List innerBlocks; - private int index = 0; - - private World world; - private Map> intersectedCuboid; - private EditSession editSession; - private List elements; - - public CreatingInnerBlocksState(List innerBlocks, World world, Map> intersectedCuboid, EditSession editSession, List elements) { - this.innerBlocks = innerBlocks; - this.world = world; - this.intersectedCuboid = intersectedCuboid; - this.editSession = editSession; - this.elements = elements; - } - - @Override - public String actionBarMessage(Player p) { - return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_CREATING_INNER_BLOCKS", p), index, innerBlocks.size(), eta(p, start, index, innerBlocks.size())); - } - - @Override - public boolean hasNext() { - return index < innerBlocks.size(); - } - - @Override - public void next() { - Point point = innerBlocks.get(index); - index++; - - List neededCuboids = intersectedCuboid.get(point); - List blockDataWithBoundingBoxList = new ArrayList<>(); - for (BlockBoundingBox blockDataWithBoundingBox : elements) { - List cuboids = blockDataWithBoundingBox.getCuboidList().stream().map(c -> c.add(point)).collect(Collectors.toList()); - - boolean isInCuboid = false; - for (Cuboid cuboid : neededCuboids) { - if (cuboids.stream().anyMatch(cuboid::intersects)) { - isInCuboid = true; - break; - } - } - if (!isInCuboid) { - blockDataWithBoundingBoxList.add(blockDataWithBoundingBox); - break; - } - } - if (blockDataWithBoundingBoxList.isEmpty()) { - return; - } - - BlockBoundingBox highest = blockDataWithBoundingBoxList.get(0); - Location location = new Location(world, point.getX(), point.getY(), point.getZ()); - if (!location.getBlock().getType().isAir()) { - return; - } - try { - editSession.setBlock(BukkitAdapter.asBlockVector(location), BukkitAdapter.adapt(highest.blockData)); - location.getBlock().setBlockData(highest.blockData, false); - if (highest.blockConsumer != null) highest.blockConsumer.accept(location.getBlock()); - } catch (MaxChangedBlocksException e) { - e.printStackTrace(); - // ignored - } - } -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingLaufState.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingLaufState.java new file mode 100644 index 00000000..be76c5a7 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingLaufState.java @@ -0,0 +1,200 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.slaves.laufbau.states; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.MaxChangedBlocksException; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockTypes; +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.slaves.laufbau.BlockBoundingBox; +import de.steamwar.bausystem.features.slaves.laufbau.Cuboid; +import de.steamwar.bausystem.region.Point; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class CreatingLaufState implements LaufbauState { + + private static final Cuboid ONE = new Cuboid(0, 0, 0, 1, 1, 1); + private static final Cuboid ABOVE_HALF = new Cuboid(0, 1, 0, 1, 0.5, 1); + private static final BaseBlock SHELL; + + static { + SHELL = BlockTypes.get("minecraft:end_stone").getDefaultState().toBaseBlock(); + } + + private long start = System.currentTimeMillis(); + + private List blocks; + private List cuboids; + private List outerPoints = new ArrayList<>(); + private Set innerPoints = new HashSet<>(); + private World world; + private EditSession editSession; + private List elements; + + private int operations = 0; + private int totalOperations; + + public CreatingLaufState(Set blocks, List cuboids, World world, EditSession editSession, List elements) { + this.blocks = new ArrayList<>(blocks); + this.cuboids = cuboids; + this.world = world; + this.editSession = editSession; + this.elements = elements; + + totalOperations = blocks.size() * 2; + } + + @Override + public String actionBarMessage(Player p) { + return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_CREATE_LAUF", p), operations, totalOperations, eta(p, start, operations, totalOperations)); + } + + @Override + public boolean hasNext() { + return !blocks.isEmpty() || !outerPoints.isEmpty(); + } + + @Override + public void next() { + operations++; + if (!blocks.isEmpty()) { + Point point = blocks.remove(0); + boolean isInCuboid = false; + Cuboid tempCuboid = ONE.add(point); + Cuboid aboveHalfTempCuboid = ABOVE_HALF.add(point); + List intersectedCuboids = new ArrayList<>(); + for (Cuboid cuboid : cuboids) { + if (cuboid.intersects(tempCuboid)) { + isInCuboid = true; + intersectedCuboids.add(cuboid); + } + if (cuboid.intersects(aboveHalfTempCuboid)) { + intersectedCuboids.add(cuboid); + } + } + if (!isInCuboid) { + if (hasInnerNeighbour(point)) { + operations++; + setShell(point); + } else { + outerPoints.add(point); + } + } else { + innerPoints.add(point); + operations++; + calcInnerBlock(point, intersectedCuboids); + } + } else { + Point point = outerPoints.remove(0); + setShell(point); + } + } + + private void calcInnerBlock(Point point, List neededCuboids) { + List blockDataWithBoundingBoxList = new ArrayList<>(); + for (BlockBoundingBox blockDataWithBoundingBox : elements) { + List cuboids = blockDataWithBoundingBox.getCuboidList().stream().map(c -> c.add(point)).collect(Collectors.toList()); + + boolean isInCuboid = false; + for (Cuboid cuboid : neededCuboids) { + if (cuboids.stream().anyMatch(cuboid::intersects)) { + isInCuboid = true; + break; + } + } + if (!isInCuboid) { + blockDataWithBoundingBoxList.add(blockDataWithBoundingBox); + break; + } + } + if (blockDataWithBoundingBoxList.isEmpty()) { + return; + } + + BlockBoundingBox highest = blockDataWithBoundingBoxList.get(0); + Location location = new Location(world, point.getX(), point.getY(), point.getZ()); + if (!location.getBlock().getType().isAir()) { + return; + } + try { + editSession.setBlock(BukkitAdapter.asBlockVector(location), BukkitAdapter.adapt(highest.blockData)); + location.getBlock().setBlockData(highest.blockData, false); + if (highest.blockConsumer != null) highest.blockConsumer.accept(location.getBlock()); + } catch (MaxChangedBlocksException e) { + e.printStackTrace(); + // ignored + } + } + + private boolean hasInnerNeighbour(Point point) { + Point p1 = new Point(point.getX() - 1, point.getY(), point.getZ()); + if (innerPoints.contains(p1)) { + return true; + } + Point p2 = new Point(point.getX() + 1, point.getY(), point.getZ()); + if (innerPoints.contains(p2)) { + return true; + } + Point p3 = new Point(point.getX(), point.getY() - 1, point.getZ()); + if (innerPoints.contains(p3)) { + return true; + } + Point p4 = new Point(point.getX(), point.getY() + 1, point.getZ()); + if (innerPoints.contains(p4)) { + return true; + } + Point p5 = new Point(point.getX(), point.getY(), point.getZ() - 1); + if (innerPoints.contains(p5)) { + return true; + } + Point p6 = new Point(point.getX(), point.getY(), point.getZ() + 1); + if (innerPoints.contains(p6)) { + return true; + } + return false; + } + + private void setShell(Point point) { + if (hasInnerNeighbour(point)) { + Location location = new Location(world, point.getX(), point.getY(), point.getZ()); + if (!location.getBlock().getType().isAir()) { + return; + } + try { + editSession.setBlock(BukkitAdapter.asBlockVector(location), SHELL); + location.getBlock().setType(Material.END_STONE, false); + } catch (MaxChangedBlocksException e) { + e.printStackTrace(); + // ingored + } + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingOuterBlocksState.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingOuterBlocksState.java deleted file mode 100644 index 991ca014..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/CreatingOuterBlocksState.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.features.slaves.laufbau.states; - -import com.sk89q.worldedit.EditSession; -import com.sk89q.worldedit.MaxChangedBlocksException; -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import com.sk89q.worldedit.world.block.BaseBlock; -import com.sk89q.worldedit.world.block.BlockTypes; -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.region.Point; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.entity.Player; - -import java.util.List; - -public class CreatingOuterBlocksState implements LaufbauState { - - private static final BaseBlock SHELL; - - static { - SHELL = BlockTypes.get("minecraft:end_stone").getDefaultState().toBaseBlock(); - } - - private long start = System.currentTimeMillis(); - - private List outerBlocks; - private int index = 0; - - private World world; - private EditSession editSession; - - public CreatingOuterBlocksState(List outerBlocks, World world, EditSession editSession) { - this.outerBlocks = outerBlocks; - this.world = world; - this.editSession = editSession; - } - - @Override - public String actionBarMessage(Player p) { - return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_CREATING_OUTER_BLOCKS", p), index, outerBlocks.size(), eta(p, start, index, outerBlocks.size())); - } - - @Override - public boolean hasNext() { - return index < outerBlocks.size(); - } - - @Override - public void next() { - Point point = outerBlocks.get(index); - index++; - - Location location = new Location(world, point.getX(), point.getY(), point.getZ()); - if (!location.getBlock().getType().isAir()) { - return; - } - try { - editSession.setBlock(BukkitAdapter.asBlockVector(location), SHELL); - location.getBlock().setType(Material.END_STONE, false); - } catch (MaxChangedBlocksException e) { - e.printStackTrace(); - // ingored - } - } -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ExpandingTracesState.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ExpandingTracesState.java deleted file mode 100644 index 33a9004d..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ExpandingTracesState.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.features.slaves.laufbau.states; - -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.features.slaves.laufbau.Cuboid; -import de.steamwar.bausystem.region.Point; -import lombok.Getter; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.Player; -import org.bukkit.util.Vector; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.function.BiPredicate; - -public class ExpandingTracesState implements LaufbauState { - - private long start = System.currentTimeMillis(); - - private List cuboidList; - private int totalCuboids; - - private World world; - private BiPredicate inRegionCheck; - - @Getter - private Set blocks = new HashSet<>(); - - public ExpandingTracesState(List cuboidList, World world, BiPredicate inRegionCheck) { - this.cuboidList = new ArrayList<>(cuboidList); - this.totalCuboids = cuboidList.size(); - this.world = world; - this.inRegionCheck = inRegionCheck; - } - - @Override - public String actionBarMessage(Player p) { - return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_EXPANDING_TRACES", p), totalCuboids - cuboidList.size(), totalCuboids, eta(p, start, totalCuboids - cuboidList.size(), totalCuboids)); - } - - @Override - public boolean hasNext() { - return !cuboidList.isEmpty(); - } - - @Override - public void next() { - Cuboid cuboid = cuboidList.remove(0); - for (double x = cuboid.getX() - 2; x < cuboid.getX() + cuboid.getDx() + 2; x++) { - for (double y = cuboid.getY() - 2; y < cuboid.getY() + cuboid.getDy() + 2; y++) { - for (double z = cuboid.getZ() - 2; z < cuboid.getZ() + cuboid.getDz() + 2; z++) { - Location location = new Location(world, x, y, z); - if (inRegionCheck.test(new Vector(location.getBlockX(), location.getBlockY(), location.getBlockZ()), 0)) { - Point point = new Point(location.getBlockX(), location.getBlockY(), location.getBlockZ()); - blocks.add(point); - } - } - } - } - } -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java index ead42c62..bde0c16f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ProcessingTracesState.java @@ -21,56 +21,97 @@ package de.steamwar.bausystem.features.slaves.laufbau.states; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.slaves.laufbau.Cuboid; +import de.steamwar.bausystem.features.slaves.laufbau.states.LaufbauState; import de.steamwar.bausystem.features.tracer.TNTPosition; +import de.steamwar.bausystem.region.Point; import lombok.Getter; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.util.Vector; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.function.BiPredicate; public class ProcessingTracesState implements LaufbauState { private long start = System.currentTimeMillis(); - private int totalTNTPositions; + private int totalCuboids; private List tntPositionList; + private World world; + private BiPredicate inRegionCheck; + + private List toExpand = new ArrayList<>(); + @Getter private List cuboidList = new ArrayList<>(); - public ProcessingTracesState(List tntPositionList) { + @Getter + private Set blocks = new HashSet<>(); + + public ProcessingTracesState(List tntPositionList, World world, BiPredicate inRegionCheck) { this.tntPositionList = tntPositionList; - this.totalTNTPositions = tntPositionList.size(); + this.totalCuboids = tntPositionList.stream().mapToInt(tntPosition -> tntPosition.getPreviousLocation() == null ? 1 : 3).sum(); + this.world = world; + this.inRegionCheck = inRegionCheck; } @Override public String actionBarMessage(Player p) { - return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_PROCESSING_TRACES", p), totalTNTPositions - tntPositionList.size(), totalTNTPositions, eta(p, start, totalTNTPositions - tntPositionList.size(), totalTNTPositions)); + return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_PROCESSING_TRACES", p), totalCuboids - cuboidList.size(), totalCuboids, eta(p, start, totalCuboids - cuboidList.size(), totalCuboids)); } @Override public boolean hasNext() { - return !tntPositionList.isEmpty(); + return !tntPositionList.isEmpty() || !toExpand.isEmpty(); } @Override public void next() { - TNTPosition tntPosition = tntPositionList.remove(0); + if (!toExpand.isEmpty()) { + Cuboid cuboid = toExpand.remove(0); + expandCuboid(cuboid); + } else { + TNTPosition tntPosition = tntPositionList.remove(0); + createCuboid(tntPosition); + } + } + + private void createCuboid(TNTPosition tntPosition) { Vector location = tntPosition.getLocation(); Vector previousLocation = tntPosition.getPreviousLocation(); if (previousLocation == null) { - cuboidList.add(new Cuboid(location.getX() - 0.49, location.getY(), location.getZ() - 0.49, 0.98, 0.98, 0.98)); + toExpand.add(new Cuboid(location.getX() - 0.49, location.getY(), location.getZ() - 0.49, 0.98, 0.98, 0.98)); } else { Vector movement = location.clone().subtract(previousLocation); - cuboidList.add(new Cuboid(previousLocation.getX() - 0.49, Math.min(previousLocation.getY(), location.getY()), previousLocation.getZ() - 0.49, 0.98, Math.abs(movement.getY()) + 0.98, 0.98)); + toExpand.add(new Cuboid(previousLocation.getX() - 0.49, Math.min(previousLocation.getY(), location.getY()), previousLocation.getZ() - 0.49, 0.98, Math.abs(movement.getY()) + 0.98, 0.98)); if (Math.abs(tntPosition.getUpdateVelocity().getX()) >= Math.abs(tntPosition.getUpdateVelocity().getZ())) { - cuboidList.add(new Cuboid(Math.min(previousLocation.getX(), location.getX()) - 0.49, location.getY(), previousLocation.getZ() - 0.49, Math.abs(movement.getX()) + 0.98, 0.98, 0.98)); - cuboidList.add(new Cuboid(location.getX() - 0.49, location.getY(), Math.min(previousLocation.getZ(), location.getZ()) - 0.49, 0.98, 0.98, Math.abs(movement.getZ()) + 0.98)); + toExpand.add(new Cuboid(Math.min(previousLocation.getX(), location.getX()) - 0.49, location.getY(), previousLocation.getZ() - 0.49, Math.abs(movement.getX()) + 0.98, 0.98, 0.98)); + toExpand.add(new Cuboid(location.getX() - 0.49, location.getY(), Math.min(previousLocation.getZ(), location.getZ()) - 0.49, 0.98, 0.98, Math.abs(movement.getZ()) + 0.98)); } else { - cuboidList.add(new Cuboid(previousLocation.getX() - 0.49, location.getY(), Math.min(previousLocation.getZ(), location.getZ()) - 0.49, 0.98, 0.98, Math.abs(movement.getZ()) + 0.98)); - cuboidList.add(new Cuboid(Math.min(previousLocation.getX(), location.getX()) - 0.49, location.getY(), location.getZ() - 0.49, Math.abs(movement.getX()) + 0.98, 0.98, 0.98)); + toExpand.add(new Cuboid(previousLocation.getX() - 0.49, location.getY(), Math.min(previousLocation.getZ(), location.getZ()) - 0.49, 0.98, 0.98, Math.abs(movement.getZ()) + 0.98)); + toExpand.add(new Cuboid(Math.min(previousLocation.getX(), location.getX()) - 0.49, location.getY(), location.getZ() - 0.49, Math.abs(movement.getX()) + 0.98, 0.98, 0.98)); + } + } + } + + private void expandCuboid(Cuboid cuboid) { + cuboidList.add(cuboid); + for (double x = cuboid.getX() - 2; x < cuboid.getX() + cuboid.getDx() + 2; x++) { + for (double y = cuboid.getY() - 2; y < cuboid.getY() + cuboid.getDy() + 2; y++) { + for (double z = cuboid.getZ() - 2; z < cuboid.getZ() + cuboid.getDz() + 2; z++) { + Location location = new Location(world, x, y, z); + if (inRegionCheck.test(new Vector(location.getBlockX(), location.getBlockY(), location.getBlockZ()), 0)) { + Point point = new Point(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + blocks.add(point); + } + } } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ShrinkingOuterPointsState.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ShrinkingOuterPointsState.java deleted file mode 100644 index 487b9991..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/ShrinkingOuterPointsState.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.features.slaves.laufbau.states; - -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.region.Point; -import lombok.Getter; -import org.bukkit.entity.Player; - -import java.util.ArrayList; -import java.util.List; - -public class ShrinkingOuterPointsState implements LaufbauState { - - private long start = System.currentTimeMillis(); - - private List outerPoints; - private List innerPoints; - private int index = 0; - - @Getter - private List shrunkOuterPoints = new ArrayList<>(); - - public ShrinkingOuterPointsState(List outerPoints, List innerPoints) { - this.outerPoints = outerPoints; - this.innerPoints = innerPoints; - } - - @Override - public String actionBarMessage(Player p) { - return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_SHRINKING_POINTS", p), index, outerPoints.size(), eta(p, start, index, outerPoints.size())); - } - - @Override - public boolean hasNext() { - return index < outerPoints.size(); - } - - @Override - public void next() { - Point point = outerPoints.get(index); - index++; - - Point p1 = new Point(point.getX() - 1, point.getY(), point.getZ()); - if (innerPoints.contains(p1)) { - shrunkOuterPoints.add(point); - return; - } - Point p2 = new Point(point.getX() + 1, point.getY(), point.getZ()); - if (innerPoints.contains(p2)) { - shrunkOuterPoints.add(point); - return; - } - Point p3 = new Point(point.getX(), point.getY() - 1, point.getZ()); - if (innerPoints.contains(p3)) { - shrunkOuterPoints.add(point); - return; - } - Point p4 = new Point(point.getX(), point.getY() + 1, point.getZ()); - if (innerPoints.contains(p4)) { - shrunkOuterPoints.add(point); - return; - } - Point p5 = new Point(point.getX(), point.getY(), point.getZ() - 1); - if (innerPoints.contains(p5)) { - shrunkOuterPoints.add(point); - return; - } - Point p6 = new Point(point.getX(), point.getY(), point.getZ() + 1); - if (innerPoints.contains(p6)) { - shrunkOuterPoints.add(point); - return; - } - } -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/SplittingPointsState.java b/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/SplittingPointsState.java deleted file mode 100644 index 2a61dd44..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/slaves/laufbau/states/SplittingPointsState.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.features.slaves.laufbau.states; - -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.features.slaves.laufbau.Cuboid; -import de.steamwar.bausystem.region.Point; -import lombok.Getter; -import org.bukkit.entity.Player; - -import java.util.*; - -public class SplittingPointsState implements LaufbauState { - - private static final Cuboid ONE = new Cuboid(0, 0, 0, 1, 1, 1); - private static final Cuboid ABOVE_HALF = new Cuboid(0, 1, 0, 1, 0.5, 1); - - private long start = System.currentTimeMillis(); - - private List blocks; - private int totalBlocks; - private List cuboidList; - - @Getter - private List innerPoints = new ArrayList<>(); - - @Getter - private List outerPoints = new ArrayList<>(); - - @Getter - private Map> intersectedCuboid = new HashMap<>(); - - public SplittingPointsState(Set blocks, List cuboidList) { - this.blocks = new ArrayList<>(blocks); - this.totalBlocks = blocks.size(); - this.cuboidList = cuboidList; - } - - @Override - public String actionBarMessage(Player p) { - return BauSystem.MESSAGE.parse("LAUFBAU_SIMPLE_PROGRESS", p, BauSystem.MESSAGE.parse("LAUFBAU_STATE_SPLITTING_POINTS", p), totalBlocks - blocks.size(), totalBlocks, eta(p, start, totalBlocks - blocks.size(), totalBlocks)); - } - - @Override - public boolean hasNext() { - return !blocks.isEmpty(); - } - - @Override - public void next() { - Point point = blocks.remove(0); - boolean isInCuboid = false; - Cuboid tempCuboid = ONE.add(point); - Cuboid aboveHalfTempCuboid = ABOVE_HALF.add(point); - List intersectedCuboids = new ArrayList<>(); - for (Cuboid cuboid : cuboidList) { - if (cuboid.intersects(tempCuboid)) { - isInCuboid = true; - intersectedCuboids.add(cuboid); - } - if (cuboid.intersects(aboveHalfTempCuboid)) { - intersectedCuboids.add(cuboid); - } - } - if (!isInCuboid) { - outerPoints.add(point); - } else { - innerPoints.add(point); - intersectedCuboid.put(point, intersectedCuboids); - } - } -}