77 lines
3.6 KiB
Java
77 lines
3.6 KiB
Java
package games.dmg.fantasystick.progression;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
|
|
/** Validated combat reward arithmetic and location policy; native classification lives at the adapter. */
|
|
public record CombatRewardRules(BigInteger passivePool, BigInteger ordinaryPool, BigInteger bossPool,
|
|
BigDecimal restrictedSpacing) {
|
|
public enum Target { PASSIVE, ORDINARY, BOSS, FANTASY_CHARACTER }
|
|
public static final Duration CONTRIBUTION_LIFETIME = Duration.ofMinutes(5);
|
|
|
|
public record KillPosition(UUID world, BigDecimal x, BigDecimal y, BigDecimal z) {
|
|
public KillPosition {
|
|
Objects.requireNonNull(world);
|
|
x = coordinate(x); y = coordinate(y); z = coordinate(z);
|
|
}
|
|
private static BigDecimal coordinate(BigDecimal value) {
|
|
Objects.requireNonNull(value);
|
|
if (!Double.isFinite(value.doubleValue())) throw new IllegalArgumentException("Kill coordinate is not finite");
|
|
return value;
|
|
}
|
|
}
|
|
|
|
public CombatRewardRules {
|
|
Objects.requireNonNull(passivePool); Objects.requireNonNull(ordinaryPool);
|
|
Objects.requireNonNull(bossPool); Objects.requireNonNull(restrictedSpacing);
|
|
if (passivePool.signum() < 0 || ordinaryPool.signum() < 0 || bossPool.signum() < 0
|
|
|| restrictedSpacing.signum() < 0 || !Double.isFinite(restrictedSpacing.doubleValue())) {
|
|
throw new IllegalArgumentException("Combat reward values must be finite and nonnegative");
|
|
}
|
|
}
|
|
|
|
public static CombatRewardRules defaults() {
|
|
return new CombatRewardRules(BigInteger.ONE, BigInteger.TEN, BigInteger.valueOf(100), BigDecimal.valueOf(3));
|
|
}
|
|
|
|
public BigInteger pool(Target target, int defeatedLevel) {
|
|
Objects.requireNonNull(target);
|
|
if (target == Target.FANTASY_CHARACTER) {
|
|
if (defeatedLevel < 1) throw new IllegalArgumentException("Fantasy character level must be positive");
|
|
return BigInteger.TEN.multiply(BigInteger.valueOf(defeatedLevel));
|
|
}
|
|
return switch (target) {
|
|
case PASSIVE -> passivePool;
|
|
case ORDINARY -> ordinaryPool;
|
|
case BOSS -> bossPool;
|
|
case FANTASY_CHARACTER -> throw new IllegalStateException("Handled fantasy character");
|
|
};
|
|
}
|
|
|
|
public BigInteger share(BigInteger pool, int contributors) {
|
|
Objects.requireNonNull(pool);
|
|
if (pool.signum() < 0 || contributors < 1) throw new IllegalArgumentException("Invalid reward share");
|
|
var count = BigInteger.valueOf(contributors);
|
|
return pool.add(count).subtract(BigInteger.ONE).divide(count);
|
|
}
|
|
|
|
public boolean contributes(Instant acceptedAt, Instant deathAt) {
|
|
Objects.requireNonNull(acceptedAt); Objects.requireNonNull(deathAt);
|
|
return !acceptedAt.isAfter(deathAt) && !deathAt.isAfter(acceptedAt.plus(CONTRIBUTION_LIFETIME));
|
|
}
|
|
|
|
public boolean spaced(KillPosition previous, KillPosition current) {
|
|
Objects.requireNonNull(previous); Objects.requireNonNull(current);
|
|
if (!previous.world().equals(current.world())) return true;
|
|
BigDecimal dx = current.x().subtract(previous.x());
|
|
BigDecimal dy = current.y().subtract(previous.y());
|
|
BigDecimal dz = current.z().subtract(previous.z());
|
|
BigDecimal squared = dx.multiply(dx).add(dy.multiply(dy)).add(dz.multiply(dz));
|
|
return squared.compareTo(restrictedSpacing.multiply(restrictedSpacing)) >= 0;
|
|
}
|
|
}
|