|
| 1 | +/* |
| 2 | + * Copyright 2025 Lambda |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.lambda.util.combat |
| 19 | + |
| 20 | +import com.lambda.context.SafeContext |
| 21 | +import com.lambda.util.BlockUtils.blockState |
| 22 | +import com.lambda.util.extension.fullHealth |
| 23 | +import com.lambda.util.math.flooredBlockPos |
| 24 | +import com.lambda.util.player.prediction.buildPlayerPrediction |
| 25 | +import net.minecraft.block.BedBlock |
| 26 | +import net.minecraft.block.CobwebBlock |
| 27 | +import net.minecraft.block.HayBlock |
| 28 | +import net.minecraft.block.HoneyBlock |
| 29 | +import net.minecraft.block.PointedDripstoneBlock |
| 30 | +import net.minecraft.block.PointedDripstoneBlock.THICKNESS |
| 31 | +import net.minecraft.block.PointedDripstoneBlock.VERTICAL_DIRECTION |
| 32 | +import net.minecraft.block.PowderSnowBlock |
| 33 | +import net.minecraft.block.SlimeBlock |
| 34 | +import net.minecraft.block.enums.Thickness |
| 35 | +import net.minecraft.client.world.ClientWorld |
| 36 | +import net.minecraft.entity.EquipmentSlot |
| 37 | +import net.minecraft.entity.LivingEntity |
| 38 | +import net.minecraft.entity.damage.DamageSource |
| 39 | +import net.minecraft.entity.effect.StatusEffects.FIRE_RESISTANCE |
| 40 | +import net.minecraft.entity.effect.StatusEffects.JUMP_BOOST |
| 41 | +import net.minecraft.entity.player.PlayerEntity |
| 42 | +import net.minecraft.registry.tag.DamageTypeTags.DAMAGES_HELMET |
| 43 | +import net.minecraft.registry.tag.DamageTypeTags.IS_FIRE |
| 44 | +import net.minecraft.registry.tag.DamageTypeTags.IS_FREEZING |
| 45 | +import net.minecraft.registry.tag.EntityTypeTags.FALL_DAMAGE_IMMUNE |
| 46 | +import net.minecraft.registry.tag.EntityTypeTags.FREEZE_HURTS_EXTRA_TYPES |
| 47 | +import net.minecraft.util.math.Direction |
| 48 | +import net.minecraft.world.Difficulty |
| 49 | +import net.minecraft.world.World |
| 50 | +import kotlin.math.ceil |
| 51 | +import kotlin.math.max |
| 52 | +import kotlin.math.min |
| 53 | + |
| 54 | +object DamageUtils { |
| 55 | + /** |
| 56 | + * Returns whether the player will fall from high enough for the impact to be deadly |
| 57 | + * |
| 58 | + * @param minHealth The minimum health (in half hearts) at which the fall is considered deadly |
| 59 | + */ |
| 60 | + fun SafeContext.isFallDeadly(minHealth: Double = 0.0) = |
| 61 | + player.fullHealth - fallDamage() <= minHealth |
| 62 | + |
| 63 | + /** |
| 64 | + * Calculates the fall damage for the player at the predicted position |
| 65 | + */ |
| 66 | + fun SafeContext.fallDamage(): Double { |
| 67 | + val prediction = buildPlayerPrediction() |
| 68 | + .skipUntil(60) { it.onGround } |
| 69 | + |
| 70 | + val predictedPos = prediction.position |
| 71 | + val fallDistance = player.y - predictedPos.y + player.fallDistance |
| 72 | + |
| 73 | + val state = blockState(predictedPos.flooredBlockPos) |
| 74 | + val block = state.block |
| 75 | + |
| 76 | + val distance = fallDistance + |
| 77 | + when (block) { |
| 78 | + is BedBlock -> 0.5 |
| 79 | + is PointedDripstoneBlock -> if (state.get(VERTICAL_DIRECTION) == Direction.UP && state.get(THICKNESS) == Thickness.TIP) 2.0 else 0.0 |
| 80 | + else -> 0.0 |
| 81 | + } |
| 82 | + |
| 83 | + val multiplier = when (block) { |
| 84 | + is HayBlock, is HoneyBlock -> 0.2 |
| 85 | + is PointedDripstoneBlock -> if (state.get(VERTICAL_DIRECTION) == Direction.UP && state.get(THICKNESS) == Thickness.TIP) 2.0 else 1.0 |
| 86 | + is PowderSnowBlock, is SlimeBlock, is CobwebBlock -> 0.0 |
| 87 | + else -> 1.0 |
| 88 | + } |
| 89 | + |
| 90 | + val source = player.damageSources.fall() |
| 91 | + return source.scale(world, player, player.fallDamage(distance, multiplier)) |
| 92 | + } |
| 93 | + |
| 94 | + fun LivingEntity.fallDamage(distance: Double, multiplier: Double): Double { |
| 95 | + if (type.isIn(FALL_DAMAGE_IMMUNE)) return 0.0 |
| 96 | + |
| 97 | + val modifier = getStatusEffect(JUMP_BOOST)?.amplifier?.plus(1.0) ?: 0.0 |
| 98 | + return max(0.0, ceil((distance - 3.0 - modifier) * multiplier)) |
| 99 | + } |
| 100 | + /** |
| 101 | + * Scales damage up or down based on the player resistances and other variables |
| 102 | + * |
| 103 | + * @param entity The entity to calculate the damage for |
| 104 | + * @param damage The damage to apply |
| 105 | + */ |
| 106 | + fun DamageSource.scale(world: ClientWorld, entity: LivingEntity, damage: Double): Double { |
| 107 | + if (damage.isNaN() || damage.isInfinite()) |
| 108 | + return Double.MAX_VALUE |
| 109 | + |
| 110 | + if (entity.isInvulnerableTo(this) || |
| 111 | + entity.isDead || |
| 112 | + entity.blockedByShield(this) || |
| 113 | + isIn(IS_FIRE) && entity.hasStatusEffect(FIRE_RESISTANCE)) return 0.0 |
| 114 | + |
| 115 | + if (isIn(IS_FREEZING) && entity.type.isIn(FREEZE_HURTS_EXTRA_TYPES)) |
| 116 | + return damage * 5 |
| 117 | + |
| 118 | + if (isIn(DAMAGES_HELMET) && !entity.getEquippedStack(EquipmentSlot.HEAD).isEmpty) |
| 119 | + return damage * 0.75 |
| 120 | + |
| 121 | + val appliedDamage = entity.applyArmorToDamage(this, |
| 122 | + entity.modifyAppliedDamage(this, damage.toFloat())).toDouble() |
| 123 | + |
| 124 | + return if (entity is PlayerEntity && isScaledWithDifficulty) |
| 125 | + world.scaleDamage(appliedDamage) else appliedDamage |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Scales the damage depending on the world difficulty |
| 130 | + * |
| 131 | + * Note: before using this function make sure to check if the damage scales with the given source |
| 132 | + */ |
| 133 | + fun World.scaleDamage(damage: Double): Double = |
| 134 | + when (difficulty) { |
| 135 | + Difficulty.PEACEFUL -> 0.0 |
| 136 | + Difficulty.EASY -> min(damage / 2 + 1, damage) |
| 137 | + Difficulty.HARD -> damage * 3 / 2 |
| 138 | + else -> damage |
| 139 | + } |
| 140 | +} |
0 commit comments