220 lines
11 KiB
Java
220 lines
11 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.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
import java.util.function.Consumer;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.player.PlayerMoveEvent;
|
|
import org.bukkit.event.player.PlayerVelocityEvent;
|
|
import org.bukkit.util.Vector;
|
|
|
|
/** Quickstep's grounded, collision-respecting, owned horizontal velocity adapter. */
|
|
public final class Quickstep implements AbilityEffect, Listener, AutoCloseable {
|
|
public record Settings(Duration cooldown, double distance, double speedPerTick) {
|
|
public Settings {
|
|
Objects.requireNonNull(cooldown);
|
|
if (cooldown.isNegative() || cooldown.compareTo(Duration.ofDays(365)) > 0
|
|
|| !Double.isFinite(distance) || distance <= 0 || distance > 64
|
|
|| !Double.isFinite(speedPerTick) || speedPerTick < 0.01 || speedPerTick > 8) {
|
|
throw new IllegalArgumentException("Invalid Quickstep settings");
|
|
}
|
|
}
|
|
public static Settings defaults() { return new Settings(Duration.ofSeconds(5), 4, 1); }
|
|
}
|
|
|
|
private final class State implements AutoCloseable {
|
|
final AbilityExecution execution;
|
|
final Player player;
|
|
final Location origin;
|
|
final Vector horizontal;
|
|
final Settings settings;
|
|
final long started = System.nanoTime();
|
|
final long boundTicks;
|
|
Location lastObserved;
|
|
boolean owned = true;
|
|
boolean closed;
|
|
State(AbilityExecution execution, Player player, Location origin, Vector horizontal,
|
|
Settings settings, long boundTicks) {
|
|
this.execution = execution; this.player = player; this.origin = origin;
|
|
this.horizontal = horizontal; this.settings = settings; this.boundTicks = boundTicks;
|
|
}
|
|
void finish(String outcome, Location observed) {
|
|
lastObserved = observed == null ? null : observed.clone();
|
|
execution.resolve(outcome);
|
|
execution.finish();
|
|
}
|
|
boolean stillOwns(Vector current) {
|
|
var horizontalCurrent = new Vector(current.getX(), 0, current.getZ());
|
|
double magnitude = horizontalCurrent.length();
|
|
if (magnitude < 1.0e-6) return true;
|
|
return magnitude <= horizontal.length() * 1.05 + 1.0e-6
|
|
&& horizontalCurrent.clone().normalize().dot(horizontal.clone().normalize()) >= 0.99;
|
|
}
|
|
@Override public void close() {
|
|
if (closed) return;
|
|
closed = true; dashes.remove(player.getUniqueId(), this);
|
|
try {
|
|
var to = lastObserved == null ? player.getLocation() : lastObserved;
|
|
if (to != null) {
|
|
var evidence = new java.util.LinkedHashMap<String, Object>();
|
|
evidence.put("to_location", locationData(to));
|
|
evidence.put("elapsed_ms", Math.max(0, (System.nanoTime() - started) / 1_000_000L));
|
|
evidence.put("bound_ticks", boundTicks);
|
|
if (to.getWorld() != null && to.getWorld().equals(origin.getWorld())) {
|
|
double dx = to.getX() - origin.getX(), dz = to.getZ() - origin.getZ();
|
|
evidence.put("horizontal_distance", Math.hypot(dx, dz));
|
|
} else evidence.put("horizontal_distance_status", "not_comparable_cross_world");
|
|
execution.resolutionEvidence(evidence);
|
|
}
|
|
} catch (RuntimeException ignored) { /* Auditing cannot change native motion or cleanup. */ }
|
|
if (!owned) return;
|
|
Vector current = player.getVelocity();
|
|
if (!stillOwns(current)) return;
|
|
player.setVelocity(new Vector(0, current.getY(), 0));
|
|
}
|
|
}
|
|
|
|
private volatile Settings settings;
|
|
private final Consumer<PlayerVelocityEvent> velocityEvents;
|
|
private final Map<UUID, State> dashes = new HashMap<>();
|
|
private boolean closed;
|
|
|
|
public Quickstep(Settings settings, Consumer<PlayerVelocityEvent> velocityEvents) {
|
|
this.settings = Objects.requireNonNull(settings); this.velocityEvents = Objects.requireNonNull(velocityEvents);
|
|
}
|
|
public void configure(Settings settings) { this.settings = Objects.requireNonNull(settings); }
|
|
|
|
public AbilityDefinition definition() {
|
|
var current = settings;
|
|
return new AbilityDefinition("quickstep", "Quickstep", Material.RABBIT_FOOT,
|
|
List.of("Dash forward with native horizontal movement.",
|
|
"Travel up to " + decimal(current.distance()) + " blocks while obstacles remain authoritative."),
|
|
"Use while grounded, on foot, and not flying or gliding.",
|
|
Map.of(Stat.DEX, BigInteger.valueOf(12)), BigInteger.ONE, AbilityDefinition.Category.BUF,
|
|
AbilityDefinition.Activation.DELAYED, ControlInput.Gesture.USE, current.cooldown(),
|
|
Map.of("distance", BigDecimal.valueOf(current.distance()).stripTrailingZeros(),
|
|
"speed", BigDecimal.valueOf(current.speedPerTick()).stripTrailingZeros()),
|
|
AbilityDefinition.TargetPolicy.SELF, this);
|
|
}
|
|
|
|
@Override public boolean accepts(ControlInput input) {
|
|
var player = input.player();
|
|
if (!player.isOnGround() || player.isFlying() || player.isGliding() || player.isInsideVehicle()) return false;
|
|
Vector direction = player.getEyeLocation().getDirection(); direction.setY(0);
|
|
return finite(direction) && direction.lengthSquared() > 1.0e-12;
|
|
}
|
|
|
|
@Override public void activate(AbilityExecution execution) {
|
|
if (closed || !execution.stillCurrent()) return;
|
|
var current = from(execution.definition());
|
|
var player = execution.input().player();
|
|
var origin = player.getLocation().clone();
|
|
try { execution.castEvidence(Map.of("from_location", locationData(origin))); }
|
|
catch (RuntimeException ignored) { /* Capturing an origin cannot affect admission. */ }
|
|
if (!accepts(execution.input())) {
|
|
rejectedVelocity(execution, origin, "native_recheck_refused"); return;
|
|
}
|
|
Vector direction = player.getEyeLocation().getDirection(); direction.setY(0).normalize();
|
|
Vector before = player.getVelocity();
|
|
Vector horizontal = direction.clone().multiply(current.speedPerTick());
|
|
var proposed = new Vector(horizontal.getX(), before.getY(), horizontal.getZ());
|
|
try { execution.castEvidence(Map.of("intended_direction", vectorData(direction),
|
|
"native_velocity_status", "pending_native_decision")); }
|
|
catch (RuntimeException ignored) { /* Capturing intent cannot affect native velocity. */ }
|
|
var event = new PlayerVelocityEvent(player, proposed.clone());
|
|
velocityEvents.accept(event);
|
|
if (event.isCancelled() || !finite(event.getVelocity())) {
|
|
rejectedVelocity(execution, origin, "native_velocity_cancelled_or_invalid"); return;
|
|
}
|
|
Vector accepted = event.getVelocity().clone(); accepted.setY(before.getY());
|
|
Vector acceptedHorizontal = new Vector(accepted.getX(), 0, accepted.getZ());
|
|
if (acceptedHorizontal.lengthSquared() < 1.0e-12) {
|
|
rejectedVelocity(execution, origin, "native_velocity_without_horizontal_motion"); return;
|
|
}
|
|
try { execution.castEvidence(Map.of("accepted_velocity", vectorData(accepted),
|
|
"native_velocity_status", "accepted")); }
|
|
catch (RuntimeException ignored) { /* Capturing accepted velocity cannot affect movement. */ }
|
|
long ticks = Math.max(1, (long) Math.ceil(current.distance() / acceptedHorizontal.length()));
|
|
var state = new State(execution, player, origin, acceptedHorizontal, current, ticks + 1);
|
|
var previous = dashes.put(player.getUniqueId(), state);
|
|
if (previous != null) previous.finish("replaced", previous.player.getLocation());
|
|
execution.own(state);
|
|
player.setVelocity(accepted);
|
|
execution.later(Duration.ofMillis(Math.multiplyExact(ticks + 1, 50)), () -> {
|
|
execution.resolve("completed", Map.of("reason", "elapsed_bound")); execution.finish();
|
|
});
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
|
public void moved(PlayerMoveEvent event) {
|
|
var state = dashes.get(event.getPlayer().getUniqueId());
|
|
if (state == null || state.player != event.getPlayer()) return;
|
|
var to = event.getTo();
|
|
if (to == null || !state.execution.stillCurrent()) {
|
|
state.finish("interrupted", to); return;
|
|
}
|
|
state.lastObserved = to.clone();
|
|
if (to.getWorld() == null || !to.getWorld().equals(state.origin.getWorld())) {
|
|
state.finish("interrupted", to); return;
|
|
}
|
|
Vector current = state.player.getVelocity();
|
|
if (!state.stillOwns(current)) {
|
|
state.owned = false; state.finish("replaced", to); return;
|
|
}
|
|
double dx = to.getX() - state.origin.getX(); double dz = to.getZ() - state.origin.getZ();
|
|
if (dx * dx + dz * dz >= state.settings.distance() * state.settings.distance()) {
|
|
state.finish("completed", to);
|
|
} else if (new Vector(current.getX(), 0, current.getZ()).lengthSquared() < 1.0e-12) {
|
|
state.finish("obstructed", to);
|
|
}
|
|
}
|
|
|
|
private static void rejectedVelocity(AbilityExecution execution, Location origin, String reason) {
|
|
try { execution.castEvidence(Map.of("native_velocity_status", "cancelled"));
|
|
execution.resolve("cancelled", Map.of("reason", reason,
|
|
"to_location", locationData(origin), "horizontal_distance", 0)); }
|
|
catch (RuntimeException ignored) { /* Native rejection is not contingent on audit capture. */ }
|
|
execution.finish();
|
|
}
|
|
|
|
private static Map<String, Object> vectorData(Vector vector) {
|
|
return Map.of("x", vector.getX(), "y", vector.getY(), "z", vector.getZ());
|
|
}
|
|
|
|
private static Map<String, Object> locationData(Location location) {
|
|
if (location == null || location.getWorld() == null) return Map.of("status", "unavailable");
|
|
return Map.of("world_id", location.getWorld().getUID().toString(),
|
|
"world_name", location.getWorld().getName(), "x", location.getX(), "y", location.getY(),
|
|
"z", location.getZ(), "captured_at", java.time.Instant.now().toString());
|
|
}
|
|
|
|
private static boolean finite(Vector vector) {
|
|
return Double.isFinite(vector.getX()) && Double.isFinite(vector.getY()) && Double.isFinite(vector.getZ());
|
|
}
|
|
private static Settings from(AbilityDefinition definition) {
|
|
return new Settings(definition.cooldown(), definition.parameters().get("distance").doubleValue(),
|
|
definition.parameters().get("speed").doubleValue());
|
|
}
|
|
private static BigDecimal decimal(double value) { return BigDecimal.valueOf(value).stripTrailingZeros(); }
|
|
|
|
@Override public void close() {
|
|
if (closed) return;
|
|
closed = true;
|
|
for (var state : List.copyOf(dashes.values())) state.execution.finish();
|
|
dashes.clear();
|
|
}
|
|
}
|