Skip to content

Commit c13259e

Browse files
committed
Refactor: Better explosion utils
1 parent cee341d commit c13259e

File tree

4 files changed

+134
-91
lines changed

4 files changed

+134
-91
lines changed

common/src/main/kotlin/com/lambda/util/combat/CrystalUtils.kt

Lines changed: 0 additions & 87 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/util/combat/DamageUtils.kt renamed to common/src/main/kotlin/com/lambda/util/combat/Damage.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.lambda.util.combat
22

3-
import com.lambda.context.SafeContext
43
import net.minecraft.enchantment.EnchantmentHelper
54
import net.minecraft.entity.LivingEntity
65
import net.minecraft.entity.damage.DamageSource
@@ -9,13 +8,13 @@ import net.minecraft.registry.tag.DamageTypeTags
98
import kotlin.math.max
109
import kotlin.math.min
1110

12-
object DamageUtils {
11+
object Damage {
1312
/**
1413
* @param entity The entity to calculate the damage for
1514
* @param damage The damage to apply
1615
* @return The damage dealt by the explosion
1716
*/
18-
fun SafeContext.applyProtection(entity: LivingEntity, damage: Double, source: DamageSource): Double {
17+
fun mask(entity: LivingEntity, damage: Double, source: DamageSource): Double {
1918
val resistanceAmplifier = entity.getStatusEffect(StatusEffects.RESISTANCE)?.amplifier ?: -1
2019

2120
if (source.isIn(DamageTypeTags.BYPASSES_EFFECTS)) return damage
@@ -31,5 +30,4 @@ object DamageUtils {
3130

3231
return damage
3332
}
34-
3533
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

common/src/main/resources/lambda.accesswidener

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ accessible method net/minecraft/text/Style <init> (Lnet/minecraft/text/TextColor
3232
# Other
3333
accessible field net/minecraft/client/world/ClientEntityManager cache Lnet/minecraft/world/entity/SectionedEntityCache;
3434
accessible field net/minecraft/world/entity/EntityTrackingSection collection Lnet/minecraft/util/collection/TypeFilterableList;
35+
accessible field net/minecraft/world/explosion/Explosion behavior Lnet/minecraft/world/explosion/ExplosionBehavior;

0 commit comments

Comments
 (0)