117 lines
5.8 KiB
Java
117 lines
5.8 KiB
Java
package games.dmg.fantasystick.ability;
|
|
|
|
import games.dmg.fantasystick.progression.Stat;
|
|
import games.dmg.fantasystick.session.ControlInput;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.time.Duration;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.damage.DamageSource;
|
|
import org.bukkit.damage.DamageType;
|
|
import org.bukkit.entity.LivingEntity;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
|
|
/** Great Sword's charge-scaled direct melee damage, replacing the contained native weapon hit. */
|
|
public final class GreatSword implements AbilityEffect, Listener, AutoCloseable {
|
|
public record Settings(BigInteger baseDamage, Duration recovery) {
|
|
public Settings {
|
|
Objects.requireNonNull(baseDamage); Objects.requireNonNull(recovery);
|
|
if (baseDamage.signum() < 0 || recovery.isNegative()
|
|
|| recovery.compareTo(Duration.ofDays(365)) > 0) {
|
|
throw new IllegalArgumentException("Invalid Great Sword settings");
|
|
}
|
|
}
|
|
public static Settings defaults() { return new Settings(BigInteger.valueOf(7), Duration.ofMillis(1200)); }
|
|
}
|
|
|
|
private static final class Attempt {
|
|
final AbilityExecution execution;
|
|
final LivingEntity target;
|
|
boolean reported;
|
|
Attempt(AbilityExecution execution, LivingEntity target) {
|
|
this.execution = execution; this.target = target;
|
|
}
|
|
}
|
|
|
|
private final Settings settings;
|
|
private Attempt damaging;
|
|
private boolean closed;
|
|
|
|
public GreatSword(Settings settings) { this.settings = Objects.requireNonNull(settings); }
|
|
|
|
public AbilityDefinition definition() {
|
|
return new AbilityDefinition("great_sword", "Great Sword", Material.WOODEN_SWORD,
|
|
List.of("Charge a contained melee strike.",
|
|
"Fully charged: " + settings.baseDamage() + " + STR modifier damage."),
|
|
"Attack with the equipped sword; partial charge follows the native attack curve.",
|
|
Map.of(Stat.STR, BigInteger.valueOf(12)), BigInteger.ONE,
|
|
AbilityDefinition.Activation.IMMEDIATE, ControlInput.Gesture.ATTACK, settings.recovery(),
|
|
Map.of("base-damage", new BigDecimal(settings.baseDamage())),
|
|
AbilityDefinition.TargetPolicy.HOSTILE, this);
|
|
}
|
|
|
|
public static BigDecimal damage(BigInteger strength, float charge) {
|
|
return damage(BigInteger.valueOf(7), strength, charge);
|
|
}
|
|
|
|
static BigDecimal damage(BigInteger base, BigInteger strength, float charge) {
|
|
Objects.requireNonNull(base); Objects.requireNonNull(strength);
|
|
if (base.signum() < 0 || strength.signum() < 0 || !Float.isFinite(charge) || charge < 0 || charge > 1) {
|
|
throw new IllegalArgumentException("Invalid Great Sword damage input");
|
|
}
|
|
BigInteger delta = strength.subtract(BigInteger.TEN);
|
|
BigInteger[] divided = delta.divideAndRemainder(BigInteger.TWO);
|
|
BigInteger modifier = divided[0];
|
|
if (delta.signum() < 0 && divided[1].signum() != 0) modifier = modifier.subtract(BigInteger.ONE);
|
|
BigDecimal full = new BigDecimal(base.add(modifier));
|
|
BigDecimal nativeCharge = new BigDecimal(Float.toString(charge));
|
|
BigDecimal curve = new BigDecimal("0.2").add(new BigDecimal("0.8").multiply(nativeCharge.multiply(nativeCharge)));
|
|
return full.multiply(curve).stripTrailingZeros();
|
|
}
|
|
|
|
@Override public boolean accepts(ControlInput input) {
|
|
return !closed && input.target() instanceof LivingEntity;
|
|
}
|
|
|
|
@Override public void activate(AbilityExecution execution) {
|
|
if (closed || !execution.stillCurrent() || !(execution.input().target() instanceof LivingEntity target)) return;
|
|
BigDecimal amount = damage(settings.baseDamage(), execution.character().stats().get(Stat.STR),
|
|
execution.input().attackStrength());
|
|
double nativeDamage = amount.compareTo(BigDecimal.valueOf(Double.MAX_VALUE)) > 0
|
|
? Double.MAX_VALUE : amount.doubleValue();
|
|
if (!Double.isFinite(nativeDamage) || nativeDamage <= 0) {
|
|
throw new IllegalStateException("Great Sword damage is outside the native finite range");
|
|
}
|
|
var player = execution.input().player();
|
|
var source = DamageSource.builder(DamageType.PLAYER_ATTACK).withCausingEntity(player)
|
|
.withDirectEntity(player).build();
|
|
var attempt = new Attempt(execution, target);
|
|
damaging = attempt;
|
|
try { target.damage(nativeDamage, source); }
|
|
finally { if (damaging == attempt) damaging = null; }
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
public void acceptedDamage(EntityDamageByEntityEvent event) {
|
|
var attempt = damaging;
|
|
if (closed || attempt == null || attempt.reported || event.isCancelled()
|
|
|| event.getDamager() != attempt.execution.input().player() || event.getEntity() != attempt.target
|
|
|| (event.getDamageSource().getCausingEntity() != null
|
|
&& event.getDamageSource().getCausingEntity() != attempt.execution.input().player())
|
|
|| (event.getDamageSource().getDirectEntity() != null
|
|
&& event.getDamageSource().getDirectEntity() != attempt.execution.input().player())
|
|
|| event.getFinalDamage() <= 0 || !Double.isFinite(event.getFinalDamage())) return;
|
|
attempt.reported = true;
|
|
attempt.execution.report(AbilityReport.Type.DAMAGE, event.getEntity(),
|
|
BigDecimal.valueOf(event.getFinalDamage()).stripTrailingZeros());
|
|
}
|
|
|
|
@Override public void close() { closed = true; damaging = null; }
|
|
}
|