| 1 | package thunder.hack.utility.world;
|
| 2 |
|
| 3 | import net.minecraft.core.*;
|
| 4 | import net.minecraft.core.registries.Registries;
|
| 5 | import net.minecraft.util.Mth;
|
| 6 | import net.minecraft.world.phys.*;
|
| 7 | import net.minecraft.world.phys.shapes.Shapes;
|
| 8 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 9 | import net.minecraft.world.Difficulty;
|
| 10 | import net.minecraft.world.damagesource.CombatRules;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.effect.MobEffects;
|
| 13 | import net.minecraft.world.entity.Entity;
|
| 14 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 18 | import net.minecraft.world.item.enchantment.Enchantments;
|
| 19 | import net.minecraft.world.level.BlockGetter;
|
| 20 | import net.minecraft.world.level.ClipContext;
|
| 21 | import net.minecraft.world.level.Explosion;
|
| 22 | import net.minecraft.world.level.block.Blocks;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import org.apache.commons.lang3.mutable.MutableInt;
|
| 25 | import thunder.hack.core.manager.client.ModuleManager;
|
| 26 | import thunder.hack.utility.math.PredictUtility;
|
| 27 |
|
| 28 | import java.util.Objects;
|
| 29 |
|
| 30 | import static thunder.hack.features.modules.Module.mc;
|
| 31 |
|
| 32 | public final class ExplosionUtility {
|
| 33 |
|
| 34 | public static boolean terrainIgnore = false;
|
| 35 | private static DamageSource getExplosionDamageSource() {
|
| 36 | return Explosion.getDefaultDamageSource(mc.level, mc.player);
|
| 37 | }
|
| 38 |
|
| 39 | /**
|
| 40 | * Calculate target damage based on crystal position and target. Uses AutoCrystal settings so use only in AutoCrystal
|
| 41 | *
|
| 42 | * @param crystalPos the position of the crystal whose damage is to be calculated
|
| 43 | * @param target the damage will be calculated on this entity
|
| 44 | * @param optimized use light calculate
|
| 45 | * @return damage value in Float format
|
| 46 | */
|
| 47 | public static float getAutoCrystalDamage(Vec3 crystalPos, Player target, int predictTicks, boolean optimized) {
|
| 48 | if (predictTicks == 0) return getExplosionDamage(crystalPos, target, optimized);
|
| 49 | else
|
| 50 | return getExplosionDamageWPredict(crystalPos, target, PredictUtility.predictBox(target, predictTicks), optimized);
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Calculate self damage based on crystal position and self extrapolation (predict).
|
| 55 | *
|
| 56 | * @param explosionPos the position of the explosion whose damage is to be calculated
|
| 57 | * @param predictTicks the number of game ticks for which the player's position should be predicted
|
| 58 | * @param optimized use light calculate
|
| 59 | * @return damage value in Float format
|
| 60 | */
|
| 61 | public static float getSelfExplosionDamage(Vec3 explosionPos, int predictTicks, boolean optimized) {
|
| 62 | return getAutoCrystalDamage(explosionPos, mc.player, predictTicks, optimized);
|
| 63 | }
|
| 64 |
|
| 65 | /**
|
| 66 | * Calculate target damage based on crystal position and target.
|
| 67 | *
|
| 68 | * @param explosionPos the position of the explosion whose damage is to be calculated
|
| 69 | * @param target the damage will be calculated on this entity
|
| 70 | * @param optimized use light calculate
|
| 71 | * @return damage value in Float format
|
| 72 | */
|
| 73 | public static float getExplosionDamage(Vec3 explosionPos, Player target, boolean optimized) {
|
| 74 | if (mc.level.getDifficulty() == Difficulty.PEACEFUL || target == null) return 0f;
|
| 75 |
|
| 76 | DamageSource damageSource = getExplosionDamageSource();
|
| 77 |
|
| 78 | if (!new AABB(Mth.floor(explosionPos.x - 11), Mth.floor(explosionPos.y - 11), Mth.floor(explosionPos.z - 11), Mth.floor(explosionPos.x + 13), Mth.floor(explosionPos.y + 13), Mth.floor(explosionPos.z + 13)).intersects(target.getBoundingBox()))
|
| 79 | return 0f;
|
| 80 |
|
| 81 | if (!target.isInvulnerable()) {
|
| 82 | double distExposure = (float) target.distanceToSqr(explosionPos) / 144.;
|
| 83 | if (distExposure <= 1.0) {
|
| 84 | terrainIgnore = ModuleManager.autoCrystal.ignoreTerrain.getValue();
|
| 85 | double exposure = getExposure(explosionPos, target.getBoundingBox(), optimized);
|
| 86 | terrainIgnore = false;
|
| 87 | double finalExposure = (1.0 - distExposure) * exposure;
|
| 88 |
|
| 89 | float toDamage = (float) Math.floor((finalExposure * finalExposure + finalExposure) / 2. * 7. * 12. + 1.);
|
| 90 |
|
| 91 | if (mc.level.getDifficulty() == Difficulty.EASY) toDamage = Math.min(toDamage / 2f + 1f, toDamage);
|
| 92 | else if (mc.level.getDifficulty() == Difficulty.HARD) toDamage = toDamage * 3f / 2f;
|
| 93 |
|
| 94 | toDamage = CombatRules.getDamageAfterAbsorb(target, toDamage, damageSource, target.getArmorValue(), (float) target.getAttribute(Attributes.ARMOR_TOUGHNESS).getValue());
|
| 95 |
|
| 96 | if (target.hasEffect(MobEffects.RESISTANCE)) {
|
| 97 | int resistance = 25 - (target.getEffect(MobEffects.RESISTANCE).getAmplifier() + 1) * 5;
|
| 98 | float resistance_1 = toDamage * resistance;
|
| 99 | toDamage = Math.max(resistance_1 / 25f, 0f);
|
| 100 | }
|
| 101 |
|
| 102 | if (toDamage <= 0f) toDamage = 0f;
|
| 103 | else {
|
| 104 | float protAmount = ModuleManager.autoCrystal.assumeBestArmor.getValue() ? 32f : getProtectionAmount(thunder.hack.utility.player.ArmorUtility.getArmorItems(target));
|
| 105 |
|
| 106 | if (protAmount > 0)
|
| 107 | toDamage = CombatRules.getDamageAfterMagicAbsorb(toDamage, protAmount);
|
| 108 | }
|
| 109 | return toDamage;
|
| 110 | }
|
| 111 | }
|
| 112 | return 0f;
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Calculate target damage based on crystal position, target and predicted copy of target.
|
| 117 | *
|
| 118 | * @param explosionPos the position of the explosion whose damage is to be calculated
|
| 119 | * @param target the damage will be calculated on this entity
|
| 120 | * @param predict predicted copy of target
|
| 121 | * @return damage value in Float format
|
| 122 | */
|
| 123 | public static float getExplosionDamageWPredict(Vec3 explosionPos, Player target, AABB predict, boolean optimized) {
|
| 124 | if (mc.level.getDifficulty() == Difficulty.PEACEFUL) return 0f;
|
| 125 |
|
| 126 | if (target == null || predict == null) return 0f;
|
| 127 |
|
| 128 | DamageSource damageSource = getExplosionDamageSource();
|
| 129 |
|
| 130 | if (!new AABB(Mth.floor(explosionPos.x - 11d), Mth.floor(explosionPos.y - 11d), Mth.floor(explosionPos.z - 11d), Mth.floor(explosionPos.x + 13d), Mth.floor(explosionPos.y + 13d), Mth.floor(explosionPos.z + 13d)).intersects(predict))
|
| 131 | return 0f;
|
| 132 |
|
| 133 | if (!target.isInvulnerable()) {
|
| 134 | double distExposure = predict.getCenter().add(0, -0.9, 0).distanceToSqr(explosionPos) / 144.;
|
| 135 | if (distExposure <= 1.0) {
|
| 136 | terrainIgnore = ModuleManager.autoCrystal.ignoreTerrain.getValue();
|
| 137 | double exposure = getExposure(explosionPos, predict, optimized);
|
| 138 | terrainIgnore = false;
|
| 139 | double finalExposure = (1.0 - distExposure) * exposure;
|
| 140 |
|
| 141 | float toDamage = (float) Math.floor((finalExposure * finalExposure + finalExposure) / 2.0 * 7.0 * 12d + 1.0);
|
| 142 |
|
| 143 | if (mc.level.getDifficulty() == Difficulty.EASY) toDamage = Math.min(toDamage / 2f + 1f, toDamage);
|
| 144 | else if (mc.level.getDifficulty() == Difficulty.HARD) toDamage = toDamage * 3f / 2f;
|
| 145 |
|
| 146 | toDamage = CombatRules.getDamageAfterAbsorb(target, toDamage, damageSource, target.getArmorValue(), (float) Objects.requireNonNull(target.getAttribute(Attributes.ARMOR_TOUGHNESS)).getValue());
|
| 147 |
|
| 148 | if (target.hasEffect(MobEffects.RESISTANCE)) {
|
| 149 | int resistance = 25 - (Objects.requireNonNull(target.getEffect(MobEffects.RESISTANCE)).getAmplifier() + 1) * 5;
|
| 150 | float resistance_1 = toDamage * resistance;
|
| 151 | toDamage = Math.max(resistance_1 / 25f, 0f);
|
| 152 | }
|
| 153 |
|
| 154 | if (toDamage <= 0f) toDamage = 0f;
|
| 155 | else {
|
| 156 | float protAmount = ModuleManager.autoCrystal.assumeBestArmor.getValue() ? 32f : getProtectionAmount(thunder.hack.utility.player.ArmorUtility.getArmorItems(target));
|
| 157 |
|
| 158 | if (protAmount > 0) toDamage = CombatRules.getDamageAfterMagicAbsorb(toDamage, protAmount);
|
| 159 | }
|
| 160 | return toDamage;
|
| 161 | }
|
| 162 | }
|
| 163 | return 0f;
|
| 164 | }
|
| 165 |
|
| 166 | /**
|
| 167 | * Returns the BlockHitResult of the block without considering other blocks
|
| 168 | *
|
| 169 | * @param context context with point of player's eyes and final aiming point
|
| 170 | * @param block position of block
|
| 171 | * @return BlockHitResult
|
| 172 | */
|
| 173 | public static BlockHitResult rayCastBlock(ClipContext context, BlockPos block) {
|
| 174 | return BlockGetter.traverseBlocks(context.getFrom(), context.getTo(), context, (raycastContext, blockPos) -> {
|
| 175 | BlockState blockState;
|
| 176 |
|
| 177 | if (!blockPos.equals(block)) blockState = Blocks.AIR.defaultBlockState();
|
| 178 | else blockState = Blocks.OBSIDIAN.defaultBlockState();
|
| 179 |
|
| 180 | Vec3 vec3d = raycastContext.getFrom();
|
| 181 | Vec3 vec3d2 = raycastContext.getTo();
|
| 182 | VoxelShape voxelShape = raycastContext.getBlockShape(blockState, mc.level, blockPos);
|
| 183 | BlockHitResult blockHitResult = mc.level.clipWithInteractionOverride(vec3d, vec3d2, blockPos, voxelShape, blockState);
|
| 184 | VoxelShape voxelShape2 = Shapes.empty();
|
| 185 | BlockHitResult blockHitResult2 = voxelShape2.clip(vec3d, vec3d2, blockPos);
|
| 186 |
|
| 187 | double d = blockHitResult == null ? Double.MAX_VALUE : raycastContext.getFrom().distanceToSqr(blockHitResult.getLocation());
|
| 188 | double e = blockHitResult2 == null ? Double.MAX_VALUE : raycastContext.getFrom().distanceToSqr(blockHitResult2.getLocation());
|
| 189 |
|
| 190 | return d <= e ? blockHitResult : blockHitResult2;
|
| 191 | }, (raycastContext) -> {
|
| 192 | Vec3 vec3d = raycastContext.getFrom().subtract(raycastContext.getTo());
|
| 193 | return BlockHitResult.miss(raycastContext.getTo(), Direction.getApproximateNearest(vec3d.x, vec3d.y, vec3d.z), BlockPos.containing(raycastContext.getTo()));
|
| 194 | });
|
| 195 | }
|
| 196 |
|
| 197 | /**
|
| 198 | * Calculate target damage based on explosion position, target and blockpos which, regardless of state, will be counted as obsidian
|
| 199 | *
|
| 200 | * @param explosionPos the position of the explosion whose damage is to be calculated
|
| 201 | * @param target the damage will be calculated on this entity
|
| 202 | * @param bp blockpos which, regardless of state, will be counted as obsidian
|
| 203 | * @return damage value in Float format
|
| 204 | */
|
| 205 | public static float getDamageOfGhostBlock(Vec3 explosionPos, Player target, BlockPos bp) {
|
| 206 |
|
| 207 | if (mc.level.getDifficulty() == Difficulty.PEACEFUL) return 0f;
|
| 208 |
|
| 209 | DamageSource damageSource = getExplosionDamageSource();
|
| 210 |
|
| 211 | double maxDist = 12;
|
| 212 | if (!new AABB(Mth.floor(explosionPos.x - maxDist - 1.0), Mth.floor(explosionPos.y - maxDist - 1.0), Mth.floor(explosionPos.z - maxDist - 1.0), Mth.floor(explosionPos.x + maxDist + 1.0), Mth.floor(explosionPos.y + maxDist + 1.0), Mth.floor(explosionPos.z + maxDist + 1.0)).intersects(target.getBoundingBox())) {
|
| 213 | return 0f;
|
| 214 | }
|
| 215 |
|
| 216 | if (!target.isInvulnerable()) {
|
| 217 | double distExposure = target.distanceToSqr(explosionPos) / 144.;
|
| 218 | if (distExposure <= 1.0) {
|
| 219 | terrainIgnore = ModuleManager.autoCrystal.ignoreTerrain.getValue();
|
| 220 | double exposure = getExposureGhost(explosionPos, target, bp);
|
| 221 | terrainIgnore = false;
|
| 222 | double finalExposure = (1.0 - distExposure) * exposure;
|
| 223 |
|
| 224 | float toDamage = (float) Math.floor((finalExposure * finalExposure + finalExposure) / 2.0 * 7.0 * maxDist + 1.0);
|
| 225 |
|
| 226 | if (mc.level.getDifficulty() == Difficulty.EASY) {
|
| 227 | toDamage = Math.min(toDamage / 2f + 1f, toDamage);
|
| 228 | } else if (mc.level.getDifficulty() == Difficulty.HARD) {
|
| 229 | toDamage = toDamage * 3f / 2f;
|
| 230 | }
|
| 231 |
|
| 232 | toDamage = CombatRules.getDamageAfterAbsorb(target, toDamage, damageSource, target.getArmorValue(), (float) target.getAttribute(Attributes.ARMOR_TOUGHNESS).getValue());
|
| 233 |
|
| 234 | if (target.hasEffect(MobEffects.RESISTANCE)) {
|
| 235 | int resistance = 25 - (target.getEffect(MobEffects.RESISTANCE).getAmplifier() + 1) * 5;
|
| 236 | float resistance_1 = toDamage * resistance;
|
| 237 | toDamage = Math.max(resistance_1 / 25f, 0f);
|
| 238 | }
|
| 239 |
|
| 240 | if (toDamage <= 0f) toDamage = 0f;
|
| 241 | else {
|
| 242 | float protAmount = ModuleManager.autoCrystal.assumeBestArmor.getValue() ? 32f : getProtectionAmount(thunder.hack.utility.player.ArmorUtility.getArmorItems(target));
|
| 243 |
|
| 244 | if (protAmount > 0) toDamage = CombatRules.getDamageAfterMagicAbsorb(toDamage, protAmount);
|
| 245 | }
|
| 246 | return toDamage;
|
| 247 | }
|
| 248 | }
|
| 249 | return 0f;
|
| 250 | }
|
| 251 |
|
| 252 | private static float getExposureGhost(Vec3 source, Entity entity, BlockPos pos) {
|
| 253 | AABB box = entity.getBoundingBox();
|
| 254 | double d = 1.0 / ((box.maxX - box.minX) * 2.0 + 1.0);
|
| 255 | double e = 1.0 / ((box.maxY - box.minY) * 2.0 + 1.0);
|
| 256 | double f = 1.0 / ((box.maxZ - box.minZ) * 2.0 + 1.0);
|
| 257 | double g = (1.0 - Math.floor(1.0 / d) * d) / 2.0;
|
| 258 | double h = (1.0 - Math.floor(1.0 / f) * f) / 2.0;
|
| 259 |
|
| 260 | if (d < 0.0 || e < 0.0 || f < 0.0) {
|
| 261 | return 0.0f;
|
| 262 | }
|
| 263 |
|
| 264 | int i = 0;
|
| 265 | int j = 0;
|
| 266 |
|
| 267 | for (double k = 0.0; k <= 1.0; k += d) {
|
| 268 | for (double l = 0.0; l <= 1.0; l += e) {
|
| 269 | for (double m = 0.0; m <= 1.0; m += f) {
|
| 270 | double n = Mth.lerp(k, box.minX, box.maxX);
|
| 271 | double o = Mth.lerp(l, box.minY, box.maxY);
|
| 272 | double p = Mth.lerp(m, box.minZ, box.maxZ);
|
| 273 | Vec3 vec3d = new Vec3(n + g, o, p + h);
|
| 274 | if (raycastGhost(new ClipContext(vec3d, source, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, entity), pos).getType() == HitResult.Type.MISS)
|
| 275 | ++i;
|
| 276 | ++j;
|
| 277 | }
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | return (float) i / (float) j;
|
| 282 | }
|
| 283 |
|
| 284 | public static float getExposure(Vec3 source, AABB box, boolean optimized) {
|
| 285 | if (!optimized) return getExposure(source, box);
|
| 286 |
|
| 287 | int miss = 0;
|
| 288 | int hit = 0;
|
| 289 |
|
| 290 | for (int k = 0; k <= 1; k += 1) {
|
| 291 | for (int l = 0; l <= 1; l += 1) {
|
| 292 | for (int m = 0; m <= 1; m += 1) {
|
| 293 | double n = Mth.lerp(k, box.minX, box.maxX);
|
| 294 | double o = Mth.lerp(l, box.minY, box.maxY);
|
| 295 | double p = Mth.lerp(m, box.minZ, box.maxZ);
|
| 296 | Vec3 vec3d = new Vec3(n, o, p);
|
| 297 | if (raycast(vec3d, source, ModuleManager.autoCrystal.ignoreTerrain.getValue()) == HitResult.Type.MISS)
|
| 298 | ++miss;
|
| 299 | ++hit;
|
| 300 | }
|
| 301 | }
|
| 302 | }
|
| 303 | return (float) miss / (float) hit;
|
| 304 | }
|
| 305 |
|
| 306 | public static float getExposure(Vec3 source, AABB box) {
|
| 307 | double d = 0.4545454446934474;
|
| 308 | double e = 0.21739130885479366;
|
| 309 | double f = 0.4545454446934474;
|
| 310 |
|
| 311 | int i = 0;
|
| 312 | int j = 0;
|
| 313 |
|
| 314 | for (double k = 0.0; k <= 1.0; k += d)
|
| 315 | for (double l = 0.0; l <= 1.0; l += e)
|
| 316 | for (double m = 0.0; m <= 1.0; m += f) {
|
| 317 | double n = Mth.lerp(k, box.minX, box.maxX);
|
| 318 | double o = Mth.lerp(l, box.minY, box.maxY);
|
| 319 | double p = Mth.lerp(m, box.minZ, box.maxZ);
|
| 320 | Vec3 vec3d = new Vec3(n + 0.045454555306552624, o, p + 0.045454555306552624);
|
| 321 | if (raycast(vec3d, source, ModuleManager.autoCrystal.ignoreTerrain.getValue()) == HitResult.Type.MISS)
|
| 322 | ++i;
|
| 323 | ++j;
|
| 324 | }
|
| 325 |
|
| 326 | return (float) i / (float) j;
|
| 327 | }
|
| 328 |
|
| 329 | private static BlockHitResult raycastGhost(ClipContext context, BlockPos bPos) {
|
| 330 | return BlockGetter.traverseBlocks(context.getFrom(), context.getTo(), context, (innerContext, pos) -> {
|
| 331 | Vec3 vec3d = innerContext.getFrom();
|
| 332 | Vec3 vec3d2 = innerContext.getTo();
|
| 333 |
|
| 334 | BlockState blockState;
|
| 335 |
|
| 336 | if (!pos.equals(bPos)) blockState = mc.level.getBlockState(bPos);
|
| 337 | else blockState = Blocks.OBSIDIAN.defaultBlockState();
|
| 338 |
|
| 339 | VoxelShape voxelShape = innerContext.getBlockShape(blockState, mc.level, pos);
|
| 340 | BlockHitResult blockHitResult = mc.level.clipWithInteractionOverride(vec3d, vec3d2, pos, voxelShape, blockState);
|
| 341 | BlockHitResult blockHitResult2 = Shapes.empty().clip(vec3d, vec3d2, pos);
|
| 342 | double d = blockHitResult == null ? Double.MAX_VALUE : innerContext.getFrom().distanceToSqr(blockHitResult.getLocation());
|
| 343 | double e = blockHitResult2 == null ? Double.MAX_VALUE : innerContext.getFrom().distanceToSqr(blockHitResult2.getLocation());
|
| 344 | return d <= e ? blockHitResult : blockHitResult2;
|
| 345 | }, innerContext -> {
|
| 346 | Vec3 vec3d = innerContext.getFrom().subtract(innerContext.getTo());
|
| 347 | return BlockHitResult.miss(innerContext.getTo(), Direction.getApproximateNearest(vec3d.x, vec3d.y, vec3d.z), BlockPos.containing(innerContext.getTo()));
|
| 348 | });
|
| 349 | }
|
| 350 |
|
| 351 | public static HitResult.Type raycast(Vec3 start, Vec3 end, boolean ignoreTerrain) {
|
| 352 | return BlockGetter.traverseBlocks(start, end, null, (innerContext, blockPos) -> {
|
| 353 | BlockState blockState = mc.level.getBlockState(blockPos);
|
| 354 | if (blockState.getBlock().getExplosionResistance() < 600 && ignoreTerrain) return null;
|
| 355 | BlockHitResult hitResult = blockState.getCollisionShape(mc.level, blockPos).clip(start, end, blockPos);
|
| 356 | return hitResult == null ? null : hitResult.getType();
|
| 357 | }, (innerContext) -> HitResult.Type.MISS);
|
| 358 | }
|
| 359 |
|
| 360 |
|
| 361 | public static int getProtectionAmount(Iterable<ItemStack> equipment) {
|
| 362 | MutableInt mutableInt = new MutableInt();
|
| 363 | equipment.forEach(i -> mutableInt.add(getProtectionAmount(i)));
|
| 364 | return mutableInt.intValue();
|
| 365 | }
|
| 366 |
|
| 367 | public static int getProtectionAmount(ItemStack stack) {
|
| 368 | var enchantments = mc.level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
|
| 369 | int modifierBlast = EnchantmentHelper.getItemEnchantmentLevel(enchantments.getOrThrow(Enchantments.BLAST_PROTECTION), stack);
|
| 370 | int modifier = EnchantmentHelper.getItemEnchantmentLevel(enchantments.getOrThrow(Enchantments.PROTECTION), stack);
|
| 371 | return modifierBlast * 2 + modifier;
|
| 372 | }
|
| 373 | }
|
| 374 |
|