299 lines
10 KiB
Go
299 lines
10 KiB
Go
package game
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"slices"
|
|
)
|
|
|
|
func stringPtr(s string) *string {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
return &s
|
|
}
|
|
func stringValue(s *string) string {
|
|
if s == nil {
|
|
return ""
|
|
}
|
|
return *s
|
|
}
|
|
|
|
func (w *World) restoreGathering(c *Character) error {
|
|
for _, xp := range c.View.Skills {
|
|
if xp < 0 || xp > 1000000000000 {
|
|
return errors.New("invalid persisted skill XP")
|
|
}
|
|
}
|
|
if math.IsNaN(c.JumpVelocity) || math.IsInf(c.JumpVelocity, 0) || math.IsNaN(c.GatherReadyAt) || math.IsInf(c.GatherReadyAt, 0) || c.GatherReadyAt < 0 || len(c.View.GatheringEffects) > 128 {
|
|
return errors.New("invalid persisted gathering/movement state")
|
|
}
|
|
seen := map[string]bool{}
|
|
live := []p.TimedGatheringEffect{}
|
|
for _, e := range c.View.GatheringEffects {
|
|
if e.Source == "" || len(e.Source) > 100 || seen[e.Source] || len(e.Name) > 160 || math.IsNaN(e.ExpiresAt) || math.IsInf(e.ExpiresAt, 0) || e.ExpiresAt < 0 {
|
|
return errors.New("invalid persisted gathering effect")
|
|
}
|
|
seen[e.Source] = true
|
|
mods := []content.StatModifier{}
|
|
for _, m := range e.Modifiers {
|
|
if !content.GatheringStat(m.Stat) {
|
|
return errors.New("unsupported timed gathering modifier")
|
|
}
|
|
mods = append(mods, content.StatModifier{Stat: m.Stat, Skill: stringValue(m.Skill), Flat: m.Flat, Percent: m.Percent})
|
|
}
|
|
if err := content.ValidateModifiers(mods); err != nil {
|
|
return err
|
|
}
|
|
if e.ExpiresAt > float64(w.now.UnixMilli())/1000 {
|
|
live = append(live, e)
|
|
}
|
|
}
|
|
if len(live) != len(c.View.GatheringEffects) {
|
|
w.dirty = true
|
|
}
|
|
c.View.GatheringEffects = live
|
|
return nil
|
|
}
|
|
|
|
func (w *World) gatheringModifiers(c *Character) []p.StatModifier {
|
|
mods := []p.StatModifier{}
|
|
seen := map[string]bool{}
|
|
for _, t := range c.View.Titles {
|
|
if !seen[t.ID] {
|
|
mods = append(mods, t.Effects.Modifiers...)
|
|
seen[t.ID] = true
|
|
}
|
|
}
|
|
equipped := map[string]bool{}
|
|
for _, id := range c.View.Equipment {
|
|
equipped[id] = true
|
|
}
|
|
for _, i := range c.View.Items {
|
|
if equipped[i.ID] {
|
|
mods = append(mods, i.Modifiers...)
|
|
}
|
|
}
|
|
now := float64(w.now.UnixMilli()) / 1000
|
|
for _, e := range c.View.GatheringEffects {
|
|
if e.ExpiresAt > now {
|
|
mods = append(mods, e.Modifiers...)
|
|
}
|
|
}
|
|
return mods
|
|
}
|
|
func (w *World) refreshGathering(c *Character) {
|
|
if w.Balance.Gathering == nil {
|
|
return
|
|
}
|
|
if c.View.Skills == nil {
|
|
c.View.Skills = map[string]int64{}
|
|
}
|
|
c.View.Gathering = map[string]p.SkillProgress{}
|
|
mods := w.gatheringModifiers(c)
|
|
value := func(skill, key string, base, min, max float64) p.StatValue {
|
|
v := p.StatValue{Base: base}
|
|
for _, m := range mods {
|
|
if m.Stat == key && (stringValue(m.Skill) == "" || stringValue(m.Skill) == skill) {
|
|
v.Flat += m.Flat
|
|
v.Percent += m.Percent
|
|
}
|
|
}
|
|
v.Final = math.Max(min, math.Min(max, (v.Base+v.Flat)*math.Max(0, 1+v.Percent/100)))
|
|
return v
|
|
}
|
|
for _, s := range w.Balance.Gathering.Skills {
|
|
xp := max(0, c.View.Skills[s.ID])
|
|
remaining := xp
|
|
level := int64(1)
|
|
for level < s.MaxLevel {
|
|
cost := s.BaseXP + (level-1)*s.XPPerLevel
|
|
if remaining < cost {
|
|
break
|
|
}
|
|
remaining -= cost
|
|
level++
|
|
}
|
|
next := s.BaseXP + (level-1)*s.XPPerLevel - remaining
|
|
if level == s.MaxLevel {
|
|
next = 0
|
|
}
|
|
lv := value(s.ID, "gatheringLevel", float64(level), 1, 10000)
|
|
c.View.Gathering[s.ID] = p.SkillProgress{Name: s.Name, XP: xp, Level: level, XPToNextLevel: next, EffectiveLevel: int64(math.Floor(lv.Final)), LevelValue: lv, Yield: value(s.ID, "gatheringYield", c.View.Stats["gatherRate"].Final, 0, 100000), Speed: value(s.ID, "gatheringSpeed", 100, 10, 1000), Experience: value(s.ID, "gatheringXP", c.View.Stats["xpGain"].Final, 0, 100000)}
|
|
}
|
|
c.View.StackNames = map[string]string{}
|
|
for _, r := range w.Balance.Gathering.Resources {
|
|
if r.Output == "self" && c.View.Inventory[r.ID] > 0 {
|
|
c.View.StackNames[r.ID] = r.Name
|
|
}
|
|
}
|
|
c.View.Consumables = []p.GatheringConsumable{}
|
|
for _, v := range w.Balance.Gathering.Consumables {
|
|
if c.View.Inventory[v.ID] > 0 {
|
|
c.View.StackNames[v.ID] = v.Name
|
|
c.View.Consumables = append(c.View.Consumables, p.GatheringConsumable{ID: v.ID, Name: v.Name, Description: v.Description, Seconds: v.Seconds, Modifiers: protocolModifiers(v.Modifiers)})
|
|
}
|
|
}
|
|
}
|
|
func (w *World) resource(id string) *content.ResourceDefinition {
|
|
for i := range w.Balance.Gathering.Resources {
|
|
if w.Balance.Gathering.Resources[i].ID == id {
|
|
return &w.Balance.Gathering.Resources[i]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (w *World) gatherRisk(c *Character, r content.ResourceDefinition) p.GatheringRisk {
|
|
s := c.View.Gathering[r.SkillID]
|
|
gap := max(int64(0), r.RequiredLevel-s.EffectiveLevel)
|
|
rules := w.Balance.GatheringRules
|
|
risk := p.GatheringRisk{Allowed: gap <= 20, Seconds: r.Seconds * 100 / math.Max(10, s.Speed.Final)}
|
|
if gap > 0 {
|
|
risk.Failure = math.Min(rules.FailureMax, rules.FailureBase+float64(gap)*rules.FailurePerLevel)
|
|
risk.Destruction = math.Min(100, rules.DestroyBase+float64(gap)*rules.DestroyPerLevel)
|
|
risk.Negative = rules.NegativeChance
|
|
risk.Damage = float64(gap) * rules.DamagePerLevel
|
|
}
|
|
return risk
|
|
}
|
|
func (w *World) gather(c *Character, target string) error {
|
|
if until := w.Depleted[target]; until > w.Age() {
|
|
return errors.New("resource depleted")
|
|
}
|
|
w.refreshGathering(c)
|
|
for _, ch := range w.neighbors(c.View.Entity.Position) {
|
|
for _, e := range ch.Resources {
|
|
if e.ID != target {
|
|
continue
|
|
}
|
|
if distance(c.View.Entity.Position, e.Position) > w.Balance.GatherRange || math.Abs(c.View.Entity.Position.Y-e.Position.Y) > w.Balance.GatherRange {
|
|
return errors.New("resource out of reach")
|
|
}
|
|
id := stringValue(e.ResourceID)
|
|
if id == "" {
|
|
id = e.Kind
|
|
}
|
|
r := w.resource(id)
|
|
if r == nil {
|
|
return errors.New("not gatherable")
|
|
}
|
|
risk := w.gatherRisk(c, *r)
|
|
if !risk.Allowed {
|
|
return errors.New("resource requires more than 20 levels above your effective gathering level")
|
|
}
|
|
if w.Age() < c.GatherReadyAt || w.Age()-c.LastGather < risk.Seconds {
|
|
return errors.New("gathering cooldown")
|
|
}
|
|
c.LastGather = w.Age()
|
|
c.GatherReadyAt = w.Age() + risk.Seconds
|
|
c.View.GatherAttempt++
|
|
w.dirty = true
|
|
if w.roll(risk.Failure) {
|
|
destroyed := w.roll(risk.Destruction)
|
|
if destroyed {
|
|
w.Depleted[target] = w.Age() + r.RespawnSeconds
|
|
}
|
|
negative := w.roll(risk.Negative)
|
|
if negative {
|
|
c.View.Entity.HP = math.Max(0, c.View.Entity.HP-risk.Damage)
|
|
w.applyGatheringEffect(c, p.TimedGatheringEffect{Source: "fatigue:" + r.SkillID, Name: "Gathering fatigue: " + c.View.Gathering[r.SkillID].Name, ExpiresAt: float64(w.now.UnixMilli())/1000 + w.Balance.GatheringRules.FatigueSeconds, Modifiers: []p.StatModifier{{Stat: "gatheringSpeed", Skill: stringPtr(r.SkillID), Percent: -w.Balance.GatheringRules.FatiguePercent}}})
|
|
}
|
|
c.Message = "Gathering failed"
|
|
if destroyed {
|
|
c.Message += "; resource destroyed until respawn"
|
|
}
|
|
if negative {
|
|
c.Message += fmt.Sprintf("; %.0f damage and gathering fatigue", risk.Damage)
|
|
}
|
|
if c.View.Entity.HP <= 0 {
|
|
w.kill(c, "A dangerous gathering attempt ended this character's journey.")
|
|
}
|
|
w.RefreshStats(c)
|
|
return nil
|
|
}
|
|
output := r.Output
|
|
if output == "self" {
|
|
output = r.ID
|
|
}
|
|
n := w.yieldAmount(float64(r.Amount) * c.View.Gathering[r.SkillID].Yield.Final)
|
|
if n > 0 {
|
|
if err := w.addInventoryItems(&c.View.Inventory, &c.View.Items, output, n); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
xp := int64(math.Min(1000000000, float64(r.XP)*c.View.Gathering[r.SkillID].Experience.Final/100))
|
|
c.View.Skills[r.SkillID] = min(int64(1000000000000), c.View.Skills[r.SkillID]+xp)
|
|
// Keep the legacy aggregate for existing title predicates, never as a level.
|
|
c.View.Skills["gathering"] = min(int64(1000000000000), c.View.Skills["gathering"]+w.Balance.SkillXpPerAction)
|
|
w.Depleted[target] = w.Age() + r.RespawnSeconds
|
|
label := output
|
|
if r.Output == "self" {
|
|
label = r.Name
|
|
}
|
|
c.Message = fmt.Sprintf("Gathered %d %s; +%d %s XP", n, label, xp, c.View.Gathering[r.SkillID].Name)
|
|
w.RefreshStats(c)
|
|
w.grantXP(c, w.Balance.Progression.GatherXP)
|
|
w.evaluateTitles(c)
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("unknown resource")
|
|
}
|
|
func (w *World) applyGatheringEffect(c *Character, e p.TimedGatheringEffect) {
|
|
c.View.GatheringEffects = slices.DeleteFunc(c.View.GatheringEffects, func(v p.TimedGatheringEffect) bool {
|
|
return v.Source == e.Source || v.ExpiresAt <= float64(w.now.UnixMilli())/1000
|
|
})
|
|
c.View.GatheringEffects = append(c.View.GatheringEffects, e)
|
|
}
|
|
func (w *World) consumeGathering(c *Character, id string) error {
|
|
if c.View.Inventory[id] < 1 {
|
|
return errors.New("consumable not in inventory")
|
|
}
|
|
for _, v := range w.Balance.Gathering.Consumables {
|
|
if v.ID == id {
|
|
if len(c.View.GatheringEffects) >= 64 && !slices.ContainsFunc(c.View.GatheringEffects, func(e p.TimedGatheringEffect) bool { return e.Source == id }) {
|
|
return errors.New("too many active gathering effects")
|
|
}
|
|
c.View.Inventory[id]--
|
|
w.applyGatheringEffect(c, p.TimedGatheringEffect{Source: id, Name: v.Name, ExpiresAt: float64(w.now.UnixMilli())/1000 + v.Seconds, Modifiers: protocolModifiers(v.Modifiers)})
|
|
w.RefreshStats(c)
|
|
c.Message = "Used " + v.Name
|
|
w.dirty = true
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("unknown gathering consumable")
|
|
}
|
|
func (w *World) jump(c *Character) error {
|
|
ground := w.terrain.Height(c.View.Entity.Position.X, c.View.Entity.Position.Z)
|
|
if c.JumpVelocity != 0 || c.View.Entity.Position.Y > ground+0.05 {
|
|
return errors.New("already airborne")
|
|
}
|
|
c.JumpVelocity = w.Balance.GatheringRules.JumpSpeed
|
|
w.dirty = true
|
|
return nil
|
|
}
|
|
func (w *World) moveCharacter(c *Character, dt float64) {
|
|
pos := c.View.Entity.Position
|
|
speed := w.playerSpeed(c)
|
|
x, z := pos.X+c.MoveX*speed*dt, pos.Z+c.MoveZ*speed*dt
|
|
if math.Hypot(x, z) >= w.Balance.WorldRadius {
|
|
x, z = pos.X, pos.Z
|
|
}
|
|
ground := w.terrain.Height(x, z)
|
|
if c.JumpVelocity != 0 || pos.Y > w.terrain.Height(pos.X, pos.Z)+0.05 {
|
|
y := pos.Y + c.JumpVelocity*dt - w.Balance.GatheringRules.Gravity*dt*dt/2
|
|
c.JumpVelocity -= w.Balance.GatheringRules.Gravity * dt
|
|
if y <= ground {
|
|
y = ground
|
|
c.JumpVelocity = 0
|
|
}
|
|
c.View.Entity.Position = p.Vec3{X: x, Y: y, Z: z}
|
|
} else {
|
|
c.View.Entity.Position = w.terrain.Position(x, z)
|
|
}
|
|
}
|