216 lines
6.0 KiB
Go
216 lines
6.0 KiB
Go
package game
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"sort"
|
|
"time"
|
|
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
)
|
|
|
|
// Candidates are produced in simulation event order. Only a successful store
|
|
// transaction may turn them into permanent awards and enable their effects.
|
|
type TitleCandidate struct {
|
|
CharacterID string
|
|
Award p.TitleAward
|
|
}
|
|
|
|
func (w *World) orderedCharacters() []*Character {
|
|
ids := make([]string, 0, len(w.Characters))
|
|
for id := range w.Characters {
|
|
ids = append(ids, id)
|
|
}
|
|
sort.Strings(ids)
|
|
characters := make([]*Character, 0, len(ids))
|
|
for _, id := range ids {
|
|
characters = append(characters, w.Characters[id])
|
|
}
|
|
return characters
|
|
}
|
|
|
|
func (w *World) restoreProgression(c *Character) error {
|
|
if c.StatsVersion < 0 || c.StatsVersion > 1 {
|
|
return errors.New("unsupported character stats version")
|
|
}
|
|
if c.View.Level == 0 {
|
|
c.View.Level = 1
|
|
}
|
|
if c.Progression == nil {
|
|
rules := *w.Balance.Progression
|
|
c.Progression = &rules
|
|
}
|
|
if c.StatsVersion == 0 {
|
|
rules := *c.Progression
|
|
if rules.StatPointsPerLevel < w.Balance.Progression.StatPointsPerLevel {
|
|
c.View.StatPoints += (c.View.Level - 1) * (w.Balance.Progression.StatPointsPerLevel - rules.StatPointsPerLevel)
|
|
}
|
|
// Preserve any older, more generous grant curve rather than deleting points.
|
|
if rules.StatPointsPerLevel < w.Balance.Progression.StatPointsPerLevel {
|
|
rules.StatPointsPerLevel = w.Balance.Progression.StatPointsPerLevel
|
|
}
|
|
rules.StaminaPerLevel = w.Balance.Progression.StaminaPerLevel
|
|
rules.VitalPointsPerChoice = w.Balance.Progression.VitalPointsPerChoice
|
|
c.Progression = &rules
|
|
c.View.PendingVitalChoices = c.View.Level - 1
|
|
c.StatsVersion = 1
|
|
w.dirty = true
|
|
}
|
|
if err := c.Progression.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if c.View.Level == 0 {
|
|
c.View.Level = 1
|
|
}
|
|
if c.View.Level < 1 || c.View.Level > c.Progression.MaxLevel || c.View.Experience < 0 || c.View.StatPoints < 0 {
|
|
return errors.New("invalid persisted character progression")
|
|
}
|
|
if e := w.migrateXPCurve(c); e != nil {
|
|
return e
|
|
}
|
|
c.View.ExperienceToNextLevel = c.nextLevelXP()
|
|
if (c.View.ExperienceToNextLevel == 0 && c.View.Experience != 0) || (c.View.ExperienceToNextLevel > 0 && c.View.Experience >= c.View.ExperienceToNextLevel) {
|
|
return errors.New("invalid persisted level XP")
|
|
}
|
|
if c.View.Titles == nil {
|
|
c.View.Titles = []p.TitleAward{}
|
|
}
|
|
for i := range c.View.Titles {
|
|
if c.View.Titles[i].Effects.Modifiers == nil {
|
|
c.View.Titles[i].Effects.Modifiers = []p.StatModifier{}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Character) nextLevelXP() int64 {
|
|
if c.View.Level >= c.Progression.MaxLevel {
|
|
return 0
|
|
}
|
|
return content.XPForLevel(c.View.Level + 1)
|
|
}
|
|
|
|
func (w *World) grantXP(c *Character, amount int64) {
|
|
if !w.AllowsXP() {
|
|
return
|
|
}
|
|
if amount <= 0 || c.View.Entity.State != "alive" || c.View.Level >= c.Progression.MaxLevel {
|
|
return
|
|
}
|
|
amount = int64(math.Min(1000000000, math.Floor(float64(amount)*c.View.Stats["xpGain"].Final/100)))
|
|
c.View.Experience += amount
|
|
before := c.View.Level
|
|
for c.View.Level < c.Progression.MaxLevel && c.View.Experience >= c.nextLevelXP() {
|
|
c.View.Experience -= c.nextLevelXP()
|
|
c.View.Level++
|
|
c.View.StatPoints += c.Progression.StatPointsPerLevel
|
|
c.View.PendingVitalChoices++
|
|
}
|
|
w.RefreshStats(c)
|
|
c.View.ExperienceToNextLevel = c.nextLevelXP()
|
|
if c.View.ExperienceToNextLevel == 0 {
|
|
c.View.Experience = 0
|
|
}
|
|
if c.View.Level > before {
|
|
c.Message = fmt.Sprintf("Level %d reached. %d stat points banked.", c.View.Level, c.View.StatPoints)
|
|
}
|
|
w.dirty = true
|
|
}
|
|
|
|
func (w *World) evaluateTitles(c *Character) {
|
|
metric := func(name string) float64 {
|
|
switch name {
|
|
case "level":
|
|
return float64(c.View.Level)
|
|
case "kills":
|
|
return float64(c.View.Kills)
|
|
case "worldKills":
|
|
return float64(c.WorldKills)
|
|
case "bossKills":
|
|
return float64(c.BossKills)
|
|
case "worldsSurvived":
|
|
return float64(c.WorldsSurvived)
|
|
case "deaths":
|
|
if c.View.Entity.State == "dead" {
|
|
return 1
|
|
}
|
|
return 0
|
|
default:
|
|
if value, ok := c.View.Stats[name]; ok {
|
|
return value.Final
|
|
}
|
|
return float64(c.View.Skills[name])
|
|
}
|
|
}
|
|
for _, title := range w.Balance.Titles {
|
|
owned := false
|
|
for _, award := range c.View.Titles {
|
|
if award.ID == title.ID {
|
|
owned = true
|
|
break
|
|
}
|
|
}
|
|
if owned {
|
|
continue
|
|
}
|
|
for _, pending := range w.PendingTitles {
|
|
if pending.CharacterID == c.View.Entity.ID && pending.Award.ID == title.ID {
|
|
owned = true
|
|
break
|
|
}
|
|
}
|
|
if owned {
|
|
continue
|
|
}
|
|
eligible := true
|
|
for _, requirement := range title.Requirements {
|
|
if metric(requirement.Metric) < float64(requirement.AtLeast) {
|
|
eligible = false
|
|
break
|
|
}
|
|
}
|
|
if !eligible {
|
|
continue
|
|
}
|
|
w.PendingTitles = append(w.PendingTitles, TitleCandidate{CharacterID: c.View.Entity.ID, Award: p.TitleAward{
|
|
ID: title.ID, Name: title.Name, Description: title.Description, Scope: title.Scope, WorldID: w.ID,
|
|
EarnedAt: w.now.UTC().Format(time.RFC3339Nano), Effects: p.TitleEffects{Damage: title.Effects.Damage, MoveSpeed: title.Effects.MoveSpeed, Modifiers: protocolModifiers(title.Effects.Modifiers)},
|
|
}})
|
|
w.dirty = true
|
|
}
|
|
}
|
|
|
|
func (c *Character) titleEffects() p.TitleEffects {
|
|
var effects p.TitleEffects
|
|
// One effect per permanent title ID, even if legacy data contains duplicates.
|
|
seen := map[string]bool{}
|
|
for _, title := range c.View.Titles {
|
|
if seen[title.ID] {
|
|
continue
|
|
}
|
|
seen[title.ID] = true
|
|
effects.Damage += title.Effects.Damage
|
|
effects.MoveSpeed += title.Effects.MoveSpeed
|
|
}
|
|
return effects
|
|
}
|
|
|
|
func (w *World) playerSpeed(c *Character) float64 {
|
|
return c.View.Stats["moveSpeed"].Final * w.EncumbranceFactor(c)
|
|
}
|
|
|
|
// Apply after all speed/capacity modifiers, so gear cannot override immobilization.
|
|
func (w *World) EncumbranceFactor(c *Character) float64 {
|
|
capacity, weight := c.View.Stats["carryCapacity"].Final, c.View.CarryWeight
|
|
if weight <= capacity {
|
|
return 1
|
|
}
|
|
if capacity <= 0 {
|
|
return 0
|
|
}
|
|
excess := (weight/capacity - 1) / (w.Balance.Inventory.StopWeightRatio - 1)
|
|
return math.Max(0, 1-math.Pow(excess, w.Balance.Inventory.SlowdownExponent))
|
|
}
|