Files

66 lines
1.5 KiB
Go

package content
import (
"fmt"
"go.yaml.in/yaml/v3"
"sync"
)
type XPCurve struct {
Base int64 `yaml:"base"`
Quadratic int64 `yaml:"quadratic"`
}
func (x XPCurve) Validate() error {
if x.Base != 100 || x.Quadratic != 10 {
return fmt.Errorf("XP curve must be 100 + 10(L-2)^2")
}
return nil
}
// This global gameplay rule applies to existing worlds and copied definitions too.
// Read its tuning once from the embedded balance instead of a character's legacy curve.
var currentXPCurve = sync.OnceValue(func() XPCurve {
var b struct {
XP XPCurve `yaml:"xpCurve"`
}
if e := yaml.Unmarshal(balance, &b); e != nil {
panic(e)
}
if e := b.XP.Validate(); e != nil {
panic(e)
}
return b.XP
})
func XPForLevel(target int64) int64 {
x := currentXPCurve()
n := max(int64(0), min(target, int64(10000))-2)
return x.Base + x.Quadratic*n*n
}
func RebaseLevelXP(xp, oldCost, newCost int64) int64 {
return min(newCost-1, int64(float64(xp)/float64(oldCost)*float64(newCost)))
}
// Rebase legacy total XP without taking away levels or granting migration rewards.
func RebaseSkillXP(s GatheringSkill, xp int64) int64 {
level := int64(1)
for level < s.MaxLevel {
cost := s.BaseXP + (level-1)*s.XPPerLevel
if xp < cost {
break
}
xp -= cost
level++
}
total := int64(0)
for l := int64(2); l <= level; l++ {
total += XPForLevel(l)
}
if level < s.MaxLevel {
total += RebaseLevelXP(xp, s.BaseXP+(level-1)*s.XPPerLevel, XPForLevel(level+1))
}
return total
}