From faf62a6ee743459bf6619b4e6add6971d5b6e5eb Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 16 Apr 2023 13:00:17 +0200 Subject: [PATCH] Add basic Explosion Signed-off-by: yoyosource --- .../features/simulator/Explosion.java | 102 ++++++++++++++++++ .../bausystem/features/simulator/TNT.java | 3 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 BauSystem_19/src/de/steamwar/bausystem/features/simulator/Explosion.java diff --git a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/Explosion.java b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/Explosion.java new file mode 100644 index 00000000..64d20126 --- /dev/null +++ b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/Explosion.java @@ -0,0 +1,102 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.simulator; + +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Waterlogged; +import org.bukkit.util.Vector; + +import java.util.HashSet; +import java.util.List; +import java.util.Random; +import java.util.Set; + +public class Explosion { + + private static final Random RANDOM = new Random(); + private static final float POWER = 4.0F; + + private final TNT tnt; + private final double x; + private final double y; + private final double z; + + private final List entities; + + public Explosion(TNT tnt, double x, double y, double z, List entities) { + this.tnt = tnt; + this.x = x; + this.y = y; + this.z = z; + this.entities = entities; + } + + public void calculate() { + Set affectedBlocks = new HashSet<>(); + for (int i = 0; i < 16; ++i) { + for (int j = 0; j < 16; ++j) { + for (int k = 0; k < 16; ++k) { + if (i == 0 || i == 15 || j == 0 || j == 15 || k == 0 || k == 15) { + double d = i / 15.0F * 2.0F - 1.0F; + double e = j / 15.0F * 2.0F - 1.0F; + double f = k / 15.0F * 2.0F - 1.0F; + double g = Math.sqrt(d * d + e * e + f * f); + d /= g; + e /= g; + f /= g; + + float h = POWER * (0.7F + RANDOM.nextFloat() * 0.6F); + double m = x; + double n = y; + double o = z; + + for (; h > 0.0F; h -= 0.22500001F) { + int x = TNT.floor(m); + int y = TNT.floor(n); + int z = TNT.floor(o); + + BlockData blockData = Simulator19.getBlockData(x, y, z); + if (blockData != null) { + h -= (blockData.getMaterial().getBlastResistance() + 0.3F) * 0.3F; + } + if (blockData instanceof Waterlogged) { + Waterlogged waterlogged = (Waterlogged) blockData; + if (waterlogged.isWaterlogged()) { + h = 0.0F; + } + } + + if (h > 0.0F) { + affectedBlocks.add(new Vector(x, y, z)); + } + + m += d * 0.30000001192092896; + n += e * 0.30000001192092896; + o += f * 0.30000001192092896; + } + } + } + } + } + + // TODO: Add velocity change of other tnt entities + // System.out.println(affectedBlocks.size()); + } +} diff --git a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/TNT.java b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/TNT.java index bbc9c137..bb8d70e6 100644 --- a/BauSystem_19/src/de/steamwar/bausystem/features/simulator/TNT.java +++ b/BauSystem_19/src/de/steamwar/bausystem/features/simulator/TNT.java @@ -81,7 +81,8 @@ public class TNT { this.fuse--; if (this.fuse <= 0) { - // TODO: Add explosion + Explosion explosion = new Explosion(this, x, y + 0.98 * 0.0625, z); + explosion.calculate(); return true; } return false;