|
| 1 | +package com.lambda.util.combat |
| 2 | + |
| 3 | +import com.lambda.context.SafeContext |
| 4 | +import com.lambda.util.math.VecUtils.minus |
| 5 | +import com.lambda.util.math.VecUtils.times |
| 6 | +import com.lambda.util.world.EntityUtils.getFastEntities |
| 7 | +import net.minecraft.enchantment.ProtectionEnchantment |
| 8 | +import net.minecraft.entity.LivingEntity |
| 9 | +import net.minecraft.util.math.BlockPos |
| 10 | +import net.minecraft.util.math.Vec3d |
| 11 | +import net.minecraft.world.explosion.Explosion |
| 12 | +import kotlin.math.max |
| 13 | + |
| 14 | +object Explosion { |
| 15 | + /** |
| 16 | + * Calculates the damage dealt by an explosion to a living entity. |
| 17 | + * @param source The source of the explosion. |
| 18 | + * @param entity The entity to calculate the damage for. |
| 19 | + * @return The damage dealt by the explosion. |
| 20 | + */ |
| 21 | + fun SafeContext.damage(source: Explosion, entity: LivingEntity) = |
| 22 | + damage(source.position, entity, source.power.toDouble()) |
| 23 | + |
| 24 | + /** |
| 25 | + * Calculates the damage dealt by an explosion to a living entity. |
| 26 | + * @param position The position of the explosion. |
| 27 | + * @param entity The entity to calculate the damage for. |
| 28 | + * @param power The strength of the explosion above 0. |
| 29 | + * @return The damage dealt by the explosion. |
| 30 | + */ |
| 31 | + fun SafeContext.damage(position: Vec3d, entity: LivingEntity, power: Double): Double { |
| 32 | + val distance = entity.pos.distanceTo(position) |
| 33 | + |
| 34 | + val impact = (1.0 - distance / (power * 2.0)) * |
| 35 | + Explosion.getExposure(position, entity) * |
| 36 | + 0.4 |
| 37 | + |
| 38 | + val damage = world.difficulty.id * 3 * |
| 39 | + power * |
| 40 | + (impact * impact + impact) + 1 |
| 41 | + |
| 42 | + return Damage.mask(entity, damage, Explosion.createDamageSource(world, null)) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Calculates the velocity of entities in the explosion. |
| 47 | + * @param explosion The explosion to calculate the velocity for. |
| 48 | + * @return The velocity of the entities. |
| 49 | + */ |
| 50 | + fun SafeContext.velocity(explosion: Explosion) = |
| 51 | + getFastEntities<LivingEntity>(explosion.position, explosion.power * 2.0) |
| 52 | + .associateWith { entity -> velocity(entity, explosion) } |
| 53 | + |
| 54 | + /** |
| 55 | + * Calculates the velocity of a living entity affected by an explosion. |
| 56 | + * @param entity The entity to calculate the velocity for. |
| 57 | + * @param explosion The explosion to calculate the velocity for. |
| 58 | + * @return The velocity of the entity. |
| 59 | + */ |
| 60 | + fun SafeContext.velocity(entity: LivingEntity, explosion: Explosion) = |
| 61 | + velocity(entity, explosion.position, explosion.power.toDouble()) |
| 62 | + |
| 63 | + /** |
| 64 | + * Calculates the velocity of a living entity affected by an explosion. |
| 65 | + * @param entity The entity to calculate the velocity for. |
| 66 | + * @param position The position of the explosion. |
| 67 | + * @param power The strength of the explosion. |
| 68 | + * @return The velocity of the entity. |
| 69 | + */ |
| 70 | + fun SafeContext.velocity(entity: LivingEntity, position: Vec3d, power: Double): Vec3d { |
| 71 | + val distance = entity.pos.distanceTo(position) |
| 72 | + |
| 73 | + val size = power * 2.0 |
| 74 | + val vel = ProtectionEnchantment.transformExplosionKnockback( |
| 75 | + entity, |
| 76 | + (1.0 - distance / size) * Explosion.getExposure(position, entity) |
| 77 | + ) |
| 78 | + |
| 79 | + val diff = entity.eyePos - position |
| 80 | + return diff.normalize() * vel |
| 81 | + } |
| 82 | + |
| 83 | + fun SafeContext.destruction(source: Explosion): List<Vec3d> { |
| 84 | + val affected = mutableListOf<Vec3d>() |
| 85 | + |
| 86 | + repeat(16) { x -> |
| 87 | + repeat(16) { y -> |
| 88 | + repeat(16) { z -> |
| 89 | + if (x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15) { |
| 90 | + val vec = Vec3d(x / 30.0 - 1.0, y / 30.0 - 1.0, z / 30.0 - 1.0) |
| 91 | + val len = vec.length() |
| 92 | + |
| 93 | + val dx = vec.x / len |
| 94 | + val dy = vec.y / len |
| 95 | + val dz = vec.z / len |
| 96 | + |
| 97 | + var explosionX = source.position.x |
| 98 | + var explosionY = source.position.y |
| 99 | + var explosionZ = source.position.z |
| 100 | + |
| 101 | + var intensity = source.power * (0.7 + world.random.nextDouble() * 0.6) |
| 102 | + |
| 103 | + while (intensity > 0) { |
| 104 | + val blockPos = BlockPos.ofFloored(explosionX, explosionY, explosionZ) |
| 105 | + val block = world.getBlockState(blockPos) |
| 106 | + val fluid = world.getFluidState(blockPos) |
| 107 | + if (!world.isInBuildLimit(blockPos)) { |
| 108 | + break |
| 109 | + } |
| 110 | + |
| 111 | + val resistance = max(block.block.blastResistance, fluid.blastResistance) |
| 112 | + intensity -= (resistance + 0.3) * 0.3 |
| 113 | + |
| 114 | + if (intensity > 0 && source.behavior.canDestroyBlock(source, world, blockPos, block, intensity.toFloat())) { |
| 115 | + affected.add(Vec3d(explosionX, explosionY, explosionZ)) |
| 116 | + } |
| 117 | + |
| 118 | + explosionX += dx * 0.3 |
| 119 | + explosionY += dy * 0.3 |
| 120 | + explosionZ += dz * 0.3 |
| 121 | + |
| 122 | + intensity -= 0.225 |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return affected |
| 130 | + } |
| 131 | +} |
0 commit comments