228 lines
13 KiB
Java
228 lines
13 KiB
Java
package games.dmg.fantasystick.ability;
|
|
|
|
import games.dmg.fantasystick.party.PartyRoster;
|
|
import games.dmg.fantasystick.progression.Stat;
|
|
import games.dmg.fantasystick.session.ControlInput;
|
|
import games.dmg.fantasystick.session.EquipmentSessions;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.time.Duration;
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.entity.Entity;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.potion.PotionEffect;
|
|
import org.bukkit.potion.PotionEffectType;
|
|
|
|
/** Dynamic owned party sphere for Rallying Cry movement and outgoing damage benefits. */
|
|
public final class RallyingCry implements AbilityEffect, AutoCloseable {
|
|
public record Settings(Duration cooldown, double radius, Duration duration,
|
|
int speedAmplifier, BigDecimal damageBonusPercent) {
|
|
public Settings {
|
|
Objects.requireNonNull(cooldown); Objects.requireNonNull(duration); Objects.requireNonNull(damageBonusPercent);
|
|
if (cooldown.isNegative() || cooldown.compareTo(Duration.ofDays(365)) > 0
|
|
|| !Double.isFinite(radius) || radius <= 0 || radius > 64
|
|
|| duration.isNegative() || duration.isZero() || duration.compareTo(Duration.ofDays(1)) > 0
|
|
|| speedAmplifier < 0 || speedAmplifier > 255
|
|
|| damageBonusPercent.signum() < 0 || damageBonusPercent.compareTo(BigDecimal.valueOf(100)) > 0) {
|
|
throw new IllegalArgumentException("Invalid Rallying Cry settings");
|
|
}
|
|
}
|
|
public static Settings defaults() {
|
|
return new Settings(Duration.ofSeconds(25), 8, Duration.ofSeconds(6), 0, BigDecimal.valueOf(20));
|
|
}
|
|
}
|
|
|
|
private final class Area implements AutoCloseable {
|
|
final long sequence; final AbilityExecution execution; final Player source; final Settings settings;
|
|
final Map<UUID, Benefit> recipients = new java.util.HashMap<>(); boolean closed;
|
|
Area(AbilityExecution execution, Settings settings) {
|
|
sequence = ++sequences; this.execution = execution; source = execution.input().player(); this.settings = settings;
|
|
}
|
|
void refresh() {
|
|
if (closed) return;
|
|
if (!sourceCurrent()) { breakArea(this); return; }
|
|
var eligible = candidates();
|
|
for (var entry : List.copyOf(recipients.entrySet())) {
|
|
if (!eligible.containsKey(entry.getKey())) entry.getValue().close();
|
|
}
|
|
for (var entry : eligible.entrySet()) {
|
|
if (!recipients.containsKey(entry.getKey())) {
|
|
var benefit = new Benefit(this, entry.getValue()); recipients.put(entry.getKey(), benefit);
|
|
}
|
|
}
|
|
}
|
|
boolean sourceCurrent() {
|
|
return execution.stillCurrent() && sessions.active(source.getUniqueId())
|
|
.filter(active -> active.equals(execution.input().session()) && sessions.current(active)).isPresent();
|
|
}
|
|
boolean eligible(Player player) {
|
|
if (!sourceCurrent()) return false;
|
|
var active = sessions.active(player.getUniqueId()).filter(sessions::current).orElse(null);
|
|
if (active == null || !inside(source, player, settings.radius())) return false;
|
|
if (player == source) return active.equals(execution.input().session());
|
|
var sourceParty = parties.partyOf(source.getUniqueId()).orElse(null);
|
|
var recipientParty = parties.partyOf(player.getUniqueId()).orElse(null);
|
|
return sourceParty != null && recipientParty != null && sourceParty.id().equals(recipientParty.id());
|
|
}
|
|
Map<UUID, Player> candidates() {
|
|
var result = new LinkedHashMap<UUID, Player>();
|
|
if (eligible(source)) result.put(source.getUniqueId(), source);
|
|
List<Entity> nearby;
|
|
try { nearby = source.getNearbyEntities(settings.radius(), settings.radius(), settings.radius()); }
|
|
catch (RuntimeException unavailable) { nearby = List.of(); }
|
|
int inspected = 0;
|
|
for (var entity : nearby) {
|
|
if (++inspected > 256 || result.size() == 64) break;
|
|
if (entity instanceof Player player && eligible(player)) result.putIfAbsent(player.getUniqueId(), player);
|
|
}
|
|
return result;
|
|
}
|
|
@Override public void close() {
|
|
if (closed) return; closed = true; areas.remove(sequence, this);
|
|
for (var benefit : List.copyOf(recipients.values())) benefit.close(); recipients.clear();
|
|
}
|
|
}
|
|
|
|
private final class Benefit implements AutoCloseable {
|
|
final Area area; final Player player; final AutoCloseable speed; final AutoCloseable bonus; boolean closed;
|
|
Benefit(Area area, Player player) {
|
|
this.area = area; this.player = player;
|
|
speed = speed(player, area.settings.speedAmplifier());
|
|
bonus = bonuses.grant(area.execution, player, area.settings.damageBonusPercent(), () -> !closed && area.eligible(player));
|
|
}
|
|
@Override public void close() {
|
|
if (closed) return; closed = true; area.recipients.remove(player.getUniqueId(), this);
|
|
closeResource(bonus); closeResource(speed);
|
|
}
|
|
}
|
|
|
|
private final class SpeedGrant implements AutoCloseable {
|
|
final SpeedState state; final long sequence; final int amplifier; boolean closed;
|
|
SpeedGrant(SpeedState state, int amplifier) { this.state = state; sequence = ++speedSequences; this.amplifier = amplifier; }
|
|
@Override public void close() { if (closed) return; closed = true; state.grants.remove(this); state.refresh(); }
|
|
}
|
|
private final class SpeedState {
|
|
final Player player; final List<SpeedGrant> grants = new ArrayList<>(); PotionEffect owned;
|
|
SpeedState(Player player) { this.player = player; }
|
|
void refresh() {
|
|
PotionEffect current;
|
|
try { current = player.getPotionEffect(PotionEffectType.SPEED); }
|
|
catch (RuntimeException unavailable) { return; }
|
|
if (owned != null && !same(current, owned)) owned = null;
|
|
SpeedGrant strongest = null;
|
|
for (var grant : List.copyOf(grants)) if (!grant.closed && (strongest == null
|
|
|| grant.amplifier > strongest.amplifier
|
|
|| grant.amplifier == strongest.amplifier && grant.sequence < strongest.sequence)) strongest = grant;
|
|
if (strongest == null) {
|
|
if (owned != null && same(current, owned) && !removeOwned()) return;
|
|
owned = null; speeds.remove(player.getUniqueId(), this); return;
|
|
}
|
|
if (owned != null && same(current, owned) && owned.getAmplifier() != strongest.amplifier) {
|
|
if (!removeOwned()) return;
|
|
current = null;
|
|
}
|
|
if (current != null && current.getAmplifier() >= strongest.amplifier) return;
|
|
var effect = new PotionEffect(PotionEffectType.SPEED, PotionEffect.INFINITE_DURATION,
|
|
strongest.amplifier, false, false, true);
|
|
try { if (player.addPotionEffect(effect, true)) owned = effect; }
|
|
catch (RuntimeException ignored) { owned = null; }
|
|
}
|
|
boolean removeOwned() {
|
|
try {
|
|
player.removePotionEffect(PotionEffectType.SPEED);
|
|
if (same(player.getPotionEffect(PotionEffectType.SPEED), owned)) return false;
|
|
} catch (RuntimeException failure) { return false; }
|
|
owned = null; return true;
|
|
}
|
|
}
|
|
|
|
private volatile Settings settings;
|
|
private final EquipmentSessions sessions;
|
|
private final PartyRoster parties;
|
|
private final OutgoingDamageBonus bonuses;
|
|
private final Map<Long, Area> areas = new java.util.HashMap<>();
|
|
private final Map<UUID, SpeedState> speeds = new java.util.HashMap<>();
|
|
private long sequences; private long speedSequences;
|
|
private org.bukkit.scheduler.BukkitTask sweepTask;
|
|
private boolean closed;
|
|
|
|
public RallyingCry(Settings settings, EquipmentSessions sessions, PartyRoster parties, OutgoingDamageBonus bonuses) {
|
|
this.settings = Objects.requireNonNull(settings); this.sessions = Objects.requireNonNull(sessions);
|
|
this.parties = Objects.requireNonNull(parties); this.bonuses = Objects.requireNonNull(bonuses);
|
|
}
|
|
public void configure(Settings settings) { this.settings = Objects.requireNonNull(settings); }
|
|
public void install(org.bukkit.plugin.Plugin plugin) {
|
|
Objects.requireNonNull(plugin);
|
|
if (closed || sweepTask != null) throw new IllegalStateException("Rallying Cry sweep already installed or closed");
|
|
sweepTask = plugin.getServer().getScheduler().runTaskTimer(plugin, this::sweep, 1, 1);
|
|
}
|
|
public void sweep() {
|
|
if (closed) return; int checked = 0;
|
|
for (var area : List.copyOf(areas.values())) { area.refresh(); if (++checked == 64) break; }
|
|
for (var state : List.copyOf(speeds.values())) state.refresh();
|
|
}
|
|
|
|
public AbilityDefinition definition() {
|
|
var current = settings;
|
|
return new AbilityDefinition("rallying_cry", "Rallying Cry", Material.GOAT_HORN,
|
|
List.of("Grant nearby active party members Speed " + (current.speedAmplifier() + 1)
|
|
+ " and " + decimal(current.damageBonusPercent()) + "% combat damage.",
|
|
"A " + decimal(BigDecimal.valueOf(current.radius())) + "-block sphere lasting " + seconds(current.duration()) + " seconds."),
|
|
"Use to rally yourself and nearby active party members; copies do not stack.",
|
|
Map.of(Stat.CHR, BigInteger.valueOf(16), Stat.CON, BigInteger.valueOf(12)), BigInteger.ONE, AbilityDefinition.Category.BUF,
|
|
AbilityDefinition.Activation.DELAYED, ControlInput.Gesture.USE, current.cooldown(),
|
|
Map.of("radius", BigDecimal.valueOf(current.radius()).stripTrailingZeros(),
|
|
"duration-seconds", BigDecimal.valueOf(current.duration().toNanos()).movePointLeft(9),
|
|
"speed-amplifier", BigDecimal.valueOf(current.speedAmplifier()),
|
|
"damage-bonus-percent", current.damageBonusPercent()), AbilityDefinition.TargetPolicy.AREA, this);
|
|
}
|
|
|
|
@Override public boolean accepts(ControlInput input) { return !closed && input.target() == null; }
|
|
@Override public void activate(AbilityExecution execution) {
|
|
if (closed || !execution.stillCurrent()) return;
|
|
var area = new Area(execution, from(execution.definition())); areas.put(area.sequence, area);
|
|
execution.own(area); area.refresh(); execution.later(area.settings.duration(), execution::finish);
|
|
}
|
|
|
|
private AutoCloseable speed(Player player, int amplifier) {
|
|
var state = speeds.computeIfAbsent(player.getUniqueId(), ignored -> new SpeedState(player));
|
|
var grant = new SpeedGrant(state, amplifier); state.grants.add(grant); state.refresh(); return grant;
|
|
}
|
|
private void breakArea(Area area) { area.close(); area.execution.finish(); }
|
|
private static boolean inside(Player source, Player target, double radius) {
|
|
try { var a = source.getLocation(); var b = target.getLocation();
|
|
return a.getWorld() == b.getWorld() && a.distanceSquared(b) <= radius * radius;
|
|
} catch (RuntimeException invalid) { return false; }
|
|
}
|
|
private static boolean same(PotionEffect first, PotionEffect second) {
|
|
return first != null && second != null && first.getType().equals(second.getType())
|
|
&& first.getAmplifier() == second.getAmplifier() && first.isInfinite() && second.isInfinite();
|
|
}
|
|
private static void closeResource(AutoCloseable resource) {
|
|
try { resource.close(); } catch (Exception impossible) { throw new IllegalStateException(impossible); }
|
|
}
|
|
private static Settings from(AbilityDefinition definition) {
|
|
var parameters = definition.parameters();
|
|
return new Settings(definition.cooldown(), parameters.get("radius").doubleValue(),
|
|
Duration.ofNanos(parameters.get("duration-seconds").movePointRight(9).longValueExact()),
|
|
parameters.get("speed-amplifier").intValueExact(), parameters.get("damage-bonus-percent"));
|
|
}
|
|
private static String decimal(BigDecimal value) { return value.stripTrailingZeros().toPlainString(); }
|
|
private static String seconds(Duration duration) {
|
|
return decimal(BigDecimal.valueOf(duration.toNanos()).movePointLeft(9));
|
|
}
|
|
|
|
@Override public void close() {
|
|
if (closed) return; closed = true; if (sweepTask != null) sweepTask.cancel();
|
|
for (var area : List.copyOf(areas.values())) area.execution.finish(); areas.clear();
|
|
for (var state : List.copyOf(speeds.values())) { state.grants.clear(); state.refresh(); }
|
|
speeds.clear();
|
|
}
|
|
}
|