6 Commits

Author SHA1 Message Date
7971350ad9 Update SimulatorPreviewTNT
All checks were successful
SteamWarCI Build successful
2025-07-08 20:46:02 +02:00
71409c9023 Update SimulatorPreviewTNT
All checks were successful
SteamWarCI Build successful
2025-07-04 16:53:58 +02:00
1e37fbe558 Update SimulatorPreviewTNT
All checks were successful
SteamWarCI Build successful
2025-07-04 16:09:09 +02:00
1fe8c79f51 Add some of the collision code
All checks were successful
SteamWarCI Build successful
2025-07-04 09:16:49 +02:00
0edf4d73bd Update SimulatorPreviewTNT
All checks were successful
SteamWarCI Build successful
2025-07-03 21:50:39 +02:00
c6dfbb5f6f Add SimulatorPreview and SimulatorPreviewTNT
All checks were successful
SteamWarCI Build successful
2025-07-03 21:48:44 +02:00
9 changed files with 580 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.simulator.data.Simulator; import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.simulator.execute.SimulatorExecutor; import de.steamwar.bausystem.features.simulator.execute.SimulatorExecutor;
import de.steamwar.bausystem.features.simulator.preview.SimulatorPreviewCalculator;
import de.steamwar.command.PreviousArguments; import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand; import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper; import de.steamwar.command.TypeMapper;
@ -30,6 +31,7 @@ import de.steamwar.command.TypeValidator;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance; import de.steamwar.linkage.LinkedInstance;
import de.steamwar.linkage.MinVersion; import de.steamwar.linkage.MinVersion;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -90,6 +92,20 @@ public class SimulatorCommand extends SWCommand {
} }
} }
@Register(value = "test")
public void test(Player player) {
for (int i = 0; i < 1; i++) {
SimulatorPreviewCalculator data = new SimulatorPreviewCalculator(null);
data.spawnTNT(0, 166, 0);
data.calculate(simulatorPreviewResult -> {
simulatorPreviewResult.show(player);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
simulatorPreviewResult.hide(player);
}, 100);
});
}
}
@ClassMapper(value = Simulator.class, local = true) @ClassMapper(value = Simulator.class, local = true)
public TypeMapper<Simulator> allSimulators() { public TypeMapper<Simulator> allSimulators() {
return new TypeMapper<>() { return new TypeMapper<>() {

View File

@ -0,0 +1,23 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.preview;
public class SimulatorPreview {
}

View File

@ -0,0 +1,76 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.preview;
import lombok.AllArgsConstructor;
import lombok.ToString;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.VoxelShape;
import java.util.List;
import java.util.stream.Collectors;
@AllArgsConstructor
@ToString
public class SimulatorPreviewAABB {
public double minX;
public double minY;
public double minZ;
public double maxX;
public double maxY;
public double maxZ;
public SimulatorPreviewAABB copy() {
return new SimulatorPreviewAABB(minX, minY, minZ, maxX, maxY, maxZ);
}
public void expandTowards(double vx, double vy, double vz) {
if (vx < 0) {
minX += vx;
} else {
maxX += vx;
}
if (vy < 0) {
minY += vy;
} else {
maxY += vy;
}
if (vz < 0) {
minZ += vz;
} else {
maxZ += vz;
}
}
public void add(double vx, double vy, double vz) {
minX += vx;
minY += vy;
minZ += vz;
}
public boolean intersects(double x, double y, double z, BoundingBox boundingBox) {
return minX - (x + boundingBox.getMaxX()) < -1.0E-7 &&
maxX - (x + boundingBox.getMinX()) > 1.0E-7 &&
minY - (y + boundingBox.getMaxY()) < -1.0E-7 &&
maxY - (y + boundingBox.getMinY()) > 1.0E-7 &&
minZ - (z + boundingBox.getMaxZ()) < -1.0E-7 &&
maxZ - (z + boundingBox.getMinZ()) > 1.0E-7;
}
}

View File

@ -0,0 +1,114 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.preview;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.simulator.data.Simulator;
import de.steamwar.bausystem.features.tracer.TNTPoint;
import de.steamwar.bausystem.features.tracer.Trace;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.util.Consumer;
import org.bukkit.util.Vector;
import org.bukkit.util.VoxelShape;
import java.util.*;
@RequiredArgsConstructor
public class SimulatorPreviewCalculator {
private static final World WORLD = Bukkit.getWorlds().get(0);
public final Simulator simulator;
public final List<SimulatorPreviewTNT> tnts = new ArrayList<>();
private int ticks = 0;
private final Set<SimulatorPreviewPos> air = new HashSet<>();
private final Map<SimulatorPreviewPos, BlockData> datas = new HashMap<>();
private final Map<SimulatorPreviewPos, VoxelShape> shapes = new HashMap<>();
public BlockData getBlockData(int x, int y, int z) {
SimulatorPreviewPos pos = new SimulatorPreviewPos(x, y, z);
if (air.contains(pos)) return null;
return datas.computeIfAbsent(pos, __ -> {
BlockData blockData = WORLD.getBlockData(x, y, z);
if (blockData.getMaterial().isAir()) {
air.add(pos);
return null;
}
return blockData;
});
}
public VoxelShape getBlockShape(int x, int y, int z) {
SimulatorPreviewPos pos = new SimulatorPreviewPos(x, y, z);
if (air.contains(pos)) return null;
return shapes.computeIfAbsent(pos, __ -> {
Block block = WORLD.getBlockAt(x, y, z);
if (block.getType().isAir()) {
air.add(pos);
return null;
}
return block.getCollisionShape();
});
}
public void setAir(int x, int y, int z) {
SimulatorPreviewPos pos = new SimulatorPreviewPos(x, y, z);
air.add(pos);
datas.remove(pos);
shapes.remove(pos);
}
public void spawnTNT(double x, double y, double z) {
tnts.add(new SimulatorPreviewTNT(this, x, y, z));
}
public void calculate(Consumer<SimulatorPreviewResult> result) {
List<TNTPoint> tntPoints = new ArrayList<>();
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), bukkitTask -> {
long time = System.currentTimeMillis();
while (!tnts.isEmpty()) {
if (System.currentTimeMillis() - time > 40) {
return;
}
tick(tntPoints);
}
bukkitTask.cancel();
System.out.println(tntPoints);
result.accept(new SimulatorPreviewResult(new Trace(null, tntPoints), air));
}, 1, 1);
}
private void tick(List<TNTPoint> tntPoints) {
System.out.println("TICK");
tnts.removeIf(simulatorPreviewTNT -> {
tntPoints.add(new TNTPoint(0, false, false, true, false, false, ticks, simulatorPreviewTNT.fuse, new Location(WORLD, simulatorPreviewTNT.x + 0.49, simulatorPreviewTNT.y, simulatorPreviewTNT.z + 0.49), new Vector(simulatorPreviewTNT.vx, simulatorPreviewTNT.vy, simulatorPreviewTNT.vz), tntPoints));
simulatorPreviewTNT.tick();
if (!simulatorPreviewTNT.removed) return false;
tntPoints.add(new TNTPoint(0, true, false, true, false, false, ticks, simulatorPreviewTNT.fuse, new Location(WORLD, simulatorPreviewTNT.x + 0.49, simulatorPreviewTNT.y, simulatorPreviewTNT.z + 0.49), new Vector(simulatorPreviewTNT.vx, simulatorPreviewTNT.vy, simulatorPreviewTNT.vz), tntPoints));
return true;
});
ticks++;
}
}

View File

@ -0,0 +1,31 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.preview;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@EqualsAndHashCode
final class SimulatorPreviewPos {
public final int x;
public final int y;
public final int z;
}

View File

@ -0,0 +1,61 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.preview;
import de.steamwar.bausystem.features.tracer.Trace;
import de.steamwar.bausystem.features.tracer.rendering.BundleFilter;
import de.steamwar.bausystem.features.tracer.rendering.PlayerTraceShowData;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import java.util.Set;
@Getter
public class SimulatorPreviewResult {
private static final World WORLD = Bukkit.getWorlds().get(0);
private static final BlockData AIR = Material.AIR.createBlockData();
private final Trace trace;
private final Set<SimulatorPreviewPos> air;
public SimulatorPreviewResult(Trace trace, Set<SimulatorPreviewPos> air) {
this.trace = trace;
this.air = air;
}
public void show(Player player) {
trace.render(player, new PlayerTraceShowData(BundleFilter.DEFAULT));
air.forEach(simulatorPreviewPos -> {
player.sendBlockChange(new Location(WORLD, simulatorPreviewPos.x, simulatorPreviewPos.y, simulatorPreviewPos.z, 0, 0), AIR);
});
}
public void hide(Player player) {
trace.hide(player);
air.forEach(simulatorPreviewPos -> {
player.sendBlockChange(new Location(WORLD, simulatorPreviewPos.x, simulatorPreviewPos.y, simulatorPreviewPos.z, 0, 0), WORLD.getBlockData(simulatorPreviewPos.x, simulatorPreviewPos.y, simulatorPreviewPos.z));
});
}
}

View File

@ -0,0 +1,228 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.preview;
import org.bukkit.Axis;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.bukkit.util.VoxelShape;
import java.util.Random;
public class SimulatorPreviewTNT {
private static final double size = 0.98;
private static final Random random = new Random();
private final SimulatorPreviewCalculator data;
private boolean gravity = true;
int fuse = 80;
double x;
double y;
double z;
double vx;
double vy;
double vz;
private SimulatorPreviewVec collide = new SimulatorPreviewVec();
private boolean onGround;
boolean removed = false;
public SimulatorPreviewTNT(SimulatorPreviewCalculator data, double x, double y, double z) {
this.data = data;
this.x = x;
this.y = y;
this.z = z;
double d = random.nextDouble() * Math.PI * 2;
this.vx = -Math.sin(d) * 0.02;
this.vy = 0.2;
this.vz = -Math.cos(d) * 0.02;
}
public void tick() {
System.out.println(x + " " + y + " " + z);
// TODO: Issue in movement still!
// System.out.println(x + " " + y + " " + z);
// Gravity
if (gravity) {
vy -= 0.04;
}
// Movement
move();
// TODO: EffectsFromOtherBlocks
// Velocity * 0.98
vx *= 0.98;
vy *= 0.98;
vz *= 0.98;
// OnGround Velocity * 0.7 -0.5 0.7
if (onGround) {
vx *= 0.7;
vy *= -0.5;
vz *= 0.7;
}
// Decrease Fuse
fuse--;
if (fuse <= 0) {
// Explode on zero Fuse
explode();
} else {
// TODO: or do water Movement!
}
}
public void move() {
collide();
double collisionLengthSquared = collide.lengthSquared();
if (collisionLengthSquared > 1.0E-7 || new Vector(vx, vy, vz).lengthSquared() - collisionLengthSquared < 1.0E-7) {
x += collide.x;
y += collide.y;
z += collide.z;
}
boolean xCollision = !equal(vx, collide.x);
boolean zCollision = !equal(vz, collide.z);
boolean horizontalCollision = xCollision || zCollision;
if (Math.abs(vy) > 0.0F) {
boolean verticalCollision = vy != collide.y;
onGround = verticalCollision && vy < (double) 0.0F;
}
if (horizontalCollision) {
if (xCollision) vx = 0;
if (zCollision) vz = 0;
}
vx = collide.x;
vy = collide.y;
vz = collide.z;
// TODO: Get Block -> updateEntityMovementAfterFallOn!
// TODO: Get BlockSpeedFactor multiply
}
public static boolean equal(double first, double second) {
return Math.abs(second - first) < (double) 1.0E-5F;
}
public void collide() {
boolean xZero = vx == 0;
boolean zZero = vz == 0;
if (xZero && vy == 0 && zZero) {
collide.x = 0;
collide.y = 0;
collide.z = 0;
return;
}
SimulatorPreviewAABB box = new SimulatorPreviewAABB(x, y, z, x + size, y + size, z + size);
double vx = this.vx;
double vy = this.vy;
double vz = this.vz;
if (vy != 0) {
vy = iterateBlocks(box.copy(), Axis.Y, vy);
if (vy != 0) box.add(0, vy, 0);
}
boolean xSmaller = Math.abs(vx) < Math.abs(vz);
if (xSmaller && vz != 0) {
vz = iterateBlocks(box.copy(), Axis.Z, vz);
if (vz != 0) box.add(0, 0, vz);
}
if (vx != 0) {
vx = iterateBlocks(box.copy(), Axis.X, vx);
if (vx != 0) box.add(vx, 0, 0);
}
if (!xSmaller && vz != 0) {
vz = iterateBlocks(box.copy(), Axis.Z, vz);
if (vz != 0) box.add(0, 0, vz);
}
collide.x = vx;
collide.y = vy;
collide.z = vz;
}
private double iterateBlocks(SimulatorPreviewAABB box, Axis axis, double v) {
switch (axis) {
case X -> box.expandTowards(v, 0, 0);
case Y -> box.expandTowards(0, v, 0);
case Z -> box.expandTowards(0, 0, v);
}
boolean negative = v < 0;
for (int x = floor(box.minX - 1.0E-7) - 1; x < floor(box.maxX + 1.0E-7) + 1; x++) {
for (int y = floor(box.minY - 1.0E-7) - 1; y < floor(box.maxY + 1.0E-7) + 1; y++) {
for (int z = floor(box.minZ - 1.0E-7) - 1; z < floor(box.maxZ + 1.0E-7) + 1; z++) {
VoxelShape shape = data.getBlockShape(x, y, z);
if (shape == null) continue;
for (BoundingBox other : shape.getBoundingBoxes()) {
switch (axis) {
case X -> {
if (box.intersects(x, y, z, other)) {
if (negative) {
v = Math.max(v, x + other.getMaxX() - this.x);
} else {
v = Math.min(v, x + other.getMinX() - this.x - size);
}
}
}
case Y -> {
if (box.intersects(x, y, z, other)) {
if (negative) {
v = Math.max(v, y + other.getMaxY() - this.y);
} else {
v = Math.min(v, y + other.getMinY() - this.y - size);
}
}
}
case Z -> {
if (box.intersects(x, y, z, other)) {
if (negative) {
v = Math.max(v, z + other.getMaxZ() - this.z);
} else {
v = Math.min(v, z + other.getMinZ() - this.z - size);
}
}
}
}
}
}
}
}
return v;
}
public static int floor(double value) {
int i = (int) value;
return value < (double) i ? i - 1 : i;
}
public void explode() {
removed = true;
// TODO: Implement
}
}

View File

@ -0,0 +1,30 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.features.simulator.preview;
public class SimulatorPreviewVec {
public double x;
public double y;
public double z;
public double lengthSquared() {
return x * x + y * y + z * z;
}
}

View File

@ -25,23 +25,18 @@ import de.steamwar.bausystem.region.utils.RegionType;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.TNTPrimed; import org.bukkit.entity.TNTPrimed;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
/** /**
* Recording of a tnt at a specific tick * Recording of a tnt at a specific tick
*/ */
@AllArgsConstructor(access = AccessLevel.PACKAGE) @AllArgsConstructor
@Getter @Getter
public class TNTPoint{ public class TNTPoint{
/** /**