feat(progression): model character points experience and health
This commit is contained in:
1 parent
05f9627cf9
commit
421b302664
4 files changed
+288
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package games.dmg.fantasystick.progression;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Server-independent progression; no disk or Minecraft XP access.
|
||||
* Native adapters must still validate live sessions, reward eligibility and persistence ownership.
|
||||
* This core is not yet registered with crafting, the book or a native session.
|
||||
*/
|
||||
public final class CharacterProgress {
|
||||
public record Snapshot(UUID characterId, Map<Stat, BigInteger> stats, int level,
|
||||
BigInteger experience, BigInteger unspentPoints, BigInteger revision,
|
||||
BigDecimal currentHealth, BigDecimal maximumHealth, String xpDisplay) { }
|
||||
|
||||
private final UUID characterId;
|
||||
private final ProgressionRules rules;
|
||||
private final EnumMap<Stat, BigInteger> stats = new EnumMap<>(Stat.class);
|
||||
private int level = 1;
|
||||
private BigInteger experience = BigInteger.ZERO;
|
||||
private BigInteger points;
|
||||
private BigInteger revision = BigInteger.ZERO;
|
||||
private BigDecimal health = BigDecimal.TEN;
|
||||
|
||||
public CharacterProgress(UUID characterId, ProgressionRules rules) {
|
||||
this.characterId = Objects.requireNonNull(characterId);
|
||||
this.rules = Objects.requireNonNull(rules);
|
||||
this.points = rules.startingPoints();
|
||||
for (Stat stat : Stat.values()) stats.put(stat, BigInteger.ZERO);
|
||||
}
|
||||
|
||||
public synchronized Snapshot snapshot() {
|
||||
return new Snapshot(characterId, Map.copyOf(stats), level, experience, points, revision,
|
||||
health, maximumHealth(), level == rules.maximumLevel() ? "MAX"
|
||||
: experience.remainder(rules.xpPerLevel()) + "/" + rules.xpPerLevel());
|
||||
}
|
||||
|
||||
public enum Purchase { APPLIED, WRONG_CHARACTER, STALE_REVISION, NO_POINTS, NOT_ALIVE }
|
||||
|
||||
public synchronized Purchase buyStat(UUID expectedCharacter, BigInteger expectedRevision, Stat stat) {
|
||||
Objects.requireNonNull(stat);
|
||||
if (!characterId.equals(expectedCharacter)) return Purchase.WRONG_CHARACTER;
|
||||
if (!revision.equals(expectedRevision)) return Purchase.STALE_REVISION;
|
||||
if (health.signum() == 0) return Purchase.NOT_ALIVE;
|
||||
if (points.signum() == 0) return Purchase.NO_POINTS;
|
||||
stats.put(stat, stats.get(stat).add(BigInteger.ONE));
|
||||
points = points.subtract(BigInteger.ONE);
|
||||
if (stat == Stat.CON) health = health.add(BigDecimal.ONE);
|
||||
revision = revision.add(BigInteger.ONE);
|
||||
return Purchase.APPLIED;
|
||||
}
|
||||
|
||||
/** Records a health result supplied by a future native session adapter, not a player-facing heal. */
|
||||
public synchronized void recordHealth(BigDecimal remaining) {
|
||||
Objects.requireNonNull(remaining);
|
||||
if (remaining.signum() < 0 || remaining.compareTo(maximumHealth()) > 0) {
|
||||
throw new IllegalArgumentException("Health must be inside the character's pool");
|
||||
}
|
||||
health = remaining;
|
||||
revision = revision.add(BigInteger.ONE);
|
||||
}
|
||||
|
||||
public synchronized int awardExperience(BigInteger reward) {
|
||||
Objects.requireNonNull(reward);
|
||||
if (reward.signum() < 0) throw new IllegalArgumentException("Experience reward must not be negative");
|
||||
BigInteger cap = rules.xpPerLevel().multiply(BigInteger.valueOf(rules.maximumLevel() - 1L));
|
||||
BigInteger nextExperience = experience.add(reward).min(cap);
|
||||
if (experience.equals(nextExperience)) return 0;
|
||||
int nextLevel = nextExperience.divide(rules.xpPerLevel()).intValueExact() + 1;
|
||||
int gained = nextLevel - level;
|
||||
points = points.add(BigInteger.valueOf(gained));
|
||||
level = nextLevel;
|
||||
experience = nextExperience;
|
||||
revision = revision.add(BigInteger.ONE);
|
||||
return gained;
|
||||
}
|
||||
|
||||
private BigDecimal maximumHealth() {
|
||||
return new BigDecimal(stats.get(Stat.CON).add(BigInteger.TEN));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package games.dmg.fantasystick.progression;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Objects;
|
||||
|
||||
public record ProgressionRules(BigInteger startingPoints, BigInteger xpPerLevel, int maximumLevel) {
|
||||
public ProgressionRules {
|
||||
Objects.requireNonNull(startingPoints);
|
||||
Objects.requireNonNull(xpPerLevel);
|
||||
if (startingPoints.signum() < 0 || xpPerLevel.signum() <= 0 || maximumLevel < 1) {
|
||||
throw new IllegalArgumentException("Invalid progression rules");
|
||||
}
|
||||
}
|
||||
|
||||
public static ProgressionRules defaults() {
|
||||
return new ProgressionRules(BigInteger.valueOf(40), BigInteger.valueOf(100), 30);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package games.dmg.fantasystick.progression;
|
||||
|
||||
public enum Stat {
|
||||
STR, DEX, CON, INT, WIS, CHR
|
||||
}
|
||||
Reference in new issue
Block a user