Add character levels and permanent player titles
This commit is contained in:
@@ -14,22 +14,26 @@ import (
|
||||
)
|
||||
|
||||
type Character struct {
|
||||
View p.Character `json:"view"`
|
||||
Account string `json:"account"`
|
||||
Day string `json:"day"`
|
||||
DailySeconds float64 `json:"dailySeconds"`
|
||||
ReturnPosition *p.Vec3 `json:"returnPosition,omitempty"`
|
||||
Sheltered bool `json:"sheltered"`
|
||||
LastAttack float64 `json:"lastAttack"`
|
||||
LastGather float64 `json:"lastGather"`
|
||||
Online bool `json:"-"`
|
||||
MoveX float64 `json:"-"`
|
||||
MoveZ float64 `json:"-"`
|
||||
LastMove time.Time `json:"-"`
|
||||
Message string `json:"-"`
|
||||
BornAt time.Time `json:"bornAt"`
|
||||
DiedAt *time.Time `json:"diedAt,omitempty"`
|
||||
DeathReason string `json:"deathReason,omitempty"`
|
||||
View p.Character `json:"view"`
|
||||
Account string `json:"account"`
|
||||
Day string `json:"day"`
|
||||
DailySeconds float64 `json:"dailySeconds"`
|
||||
ReturnPosition *p.Vec3 `json:"returnPosition,omitempty"`
|
||||
Sheltered bool `json:"sheltered"`
|
||||
LastAttack float64 `json:"lastAttack"`
|
||||
LastGather float64 `json:"lastGather"`
|
||||
Online bool `json:"-"`
|
||||
MoveX float64 `json:"-"`
|
||||
MoveZ float64 `json:"-"`
|
||||
LastMove time.Time `json:"-"`
|
||||
Message string `json:"-"`
|
||||
BornAt time.Time `json:"bornAt"`
|
||||
DiedAt *time.Time `json:"diedAt,omitempty"`
|
||||
DeathReason string `json:"deathReason,omitempty"`
|
||||
Progression *content.Progression `json:"progression,omitempty"`
|
||||
WorldKills int64 `json:"worldKills"`
|
||||
BossKills int64 `json:"bossKills"`
|
||||
WorldsSurvived int64 `json:"worldsSurvived"`
|
||||
}
|
||||
type Corpse struct {
|
||||
Entity p.Entity `json:"entity"`
|
||||
@@ -58,6 +62,7 @@ type World struct {
|
||||
Depleted map[string]float64 `json:"depleted"`
|
||||
Mobs map[string]*Mob `json:"mobs"`
|
||||
Balance content.Balance `json:"balance"`
|
||||
PendingTitles []TitleCandidate `json:"-"`
|
||||
terrain *Terrain
|
||||
chunks map[string]p.Chunk
|
||||
now time.Time
|
||||
@@ -80,6 +85,19 @@ func (w *World) Restore(now time.Time) error {
|
||||
if w.Version != GeneratorVersion {
|
||||
return errors.New("unsupported persisted generator version")
|
||||
}
|
||||
// Snapshots predating progression receive the YAML defaults once. Existing
|
||||
// world tuning and character lives remain intact; old actions grant no backfill XP.
|
||||
if w.Balance.Progression == nil {
|
||||
defaults, err := content.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Balance.Progression = defaults.Progression
|
||||
if w.Balance.Titles == nil {
|
||||
w.Balance.Titles = defaults.Titles
|
||||
}
|
||||
w.dirty = true
|
||||
}
|
||||
if err := w.Balance.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -89,7 +107,11 @@ func (w *World) Restore(now time.Time) error {
|
||||
w.terrain = NewTerrain(w.Seed, w.Balance)
|
||||
w.chunks = map[string]p.Chunk{}
|
||||
w.now = now
|
||||
w.PendingTitles = nil
|
||||
for _, c := range w.Characters {
|
||||
if err := w.restoreProgression(c); err != nil {
|
||||
return err
|
||||
}
|
||||
c.Online = false
|
||||
c.MoveX = 0
|
||||
c.MoveZ = 0
|
||||
@@ -177,11 +199,15 @@ func (w *World) Join(account, name string) (*Character, error) {
|
||||
return nil, err
|
||||
}
|
||||
c := &Character{Account: account, Day: w.now.UTC().Format(time.DateOnly), View: p.Character{Entity: p.Entity{ID: ID(), Kind: "player", Name: name, Position: pos, HP: w.Balance.PlayerHp, MaxHP: w.Balance.PlayerHp, State: "alive"}, Inventory: map[string]int64{}, Skills: map[string]int64{}, RemainingSeconds: w.Balance.DailyPlaySeconds}}
|
||||
if err := w.restoreProgression(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, v := range w.Balance.StartingKit {
|
||||
c.View.Inventory[k] = v
|
||||
}
|
||||
w.Characters[c.View.Entity.ID] = c
|
||||
c.BornAt = w.now
|
||||
w.evaluateTitles(c)
|
||||
w.dirty = true
|
||||
return c, nil
|
||||
}
|
||||
@@ -192,6 +218,9 @@ func (w *World) Import(c *Character) error {
|
||||
if c.View.Entity.State != "eligible" || c.View.Entity.HP <= 0 {
|
||||
return errors.New("character is not a living survivor")
|
||||
}
|
||||
if err := w.restoreProgression(c); err != nil {
|
||||
return err
|
||||
}
|
||||
pos, err := w.spawn("")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -207,6 +236,7 @@ func (w *World) Import(c *Character) error {
|
||||
c.ReturnPosition = nil
|
||||
c.Sheltered = false
|
||||
c.View.HasKeystone = false
|
||||
c.WorldKills = 0
|
||||
w.resetDay(c)
|
||||
w.Characters[c.View.Entity.ID] = c
|
||||
w.dirty = true
|
||||
@@ -356,6 +386,8 @@ func (w *World) gather(c *Character, target string) error {
|
||||
w.Depleted[target] = w.Age() + w.Balance.ResourceRespawnSeconds
|
||||
w.dirty = true
|
||||
c.Message = "Gathered " + item
|
||||
w.grantXP(c, w.Balance.Progression.GatherXP)
|
||||
w.evaluateTitles(c)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -379,6 +411,8 @@ func (w *World) craft(c *Character, name string) error {
|
||||
}
|
||||
c.View.Skills["crafting"] += w.Balance.SkillXpPerAction
|
||||
c.Message = "Crafted " + name
|
||||
w.grantXP(c, w.Balance.Progression.CraftXP)
|
||||
w.evaluateTitles(c)
|
||||
w.dirty = true
|
||||
return nil
|
||||
}
|
||||
@@ -390,17 +424,24 @@ func (w *World) attack(c *Character, target string) error {
|
||||
if c.View.Inventory["spear"] > 0 {
|
||||
damage = w.Balance.SpearDamage
|
||||
}
|
||||
damage = math.Max(0, damage+c.titleEffects().Damage)
|
||||
if target == "boss" {
|
||||
if w.Phase != "cataclysm" || distance(c.View.Entity.Position, w.terrain.Position(0, 0)) > w.Balance.AttackRange+4 {
|
||||
return errors.New("boss out of reach")
|
||||
}
|
||||
w.BossHP = math.Max(0, w.BossHP-damage)
|
||||
if w.BossHP == 0 {
|
||||
c.BossKills++
|
||||
w.grantXP(c, w.Balance.Progression.BossKillXP)
|
||||
w.evaluateTitles(c)
|
||||
w.Phase = "won"
|
||||
ended := w.now
|
||||
w.EndedAt = &ended
|
||||
for _, survivor := range w.Characters {
|
||||
// Stable order for simultaneous survivor milestone candidates.
|
||||
for _, survivor := range w.orderedCharacters() {
|
||||
if survivor.View.Entity.State == "alive" {
|
||||
survivor.WorldsSurvived++
|
||||
w.evaluateTitles(survivor)
|
||||
survivor.View.Entity.State = "eligible"
|
||||
survivor.MoveX = 0
|
||||
survivor.MoveZ = 0
|
||||
@@ -418,11 +459,14 @@ func (w *World) attack(c *Character, target string) error {
|
||||
m.Entity.State = "dead"
|
||||
m.RespawnAt = w.Age() + w.Balance.MobRespawnSeconds
|
||||
c.View.Kills++
|
||||
c.WorldKills++
|
||||
c.View.Inventory["herb"]++
|
||||
w.grantXP(c, w.Balance.Progression.MobKillXP)
|
||||
}
|
||||
}
|
||||
c.LastAttack = w.Age()
|
||||
c.View.Skills["combat"] += w.Balance.SkillXpPerAction
|
||||
w.evaluateTitles(c)
|
||||
w.dirty = true
|
||||
return nil
|
||||
}
|
||||
@@ -447,6 +491,7 @@ func (w *World) kill(c *Character, reason string) {
|
||||
died := w.now
|
||||
c.DiedAt = &died
|
||||
c.DeathReason = reason
|
||||
w.evaluateTitles(c)
|
||||
w.dirty = true
|
||||
}
|
||||
|
||||
@@ -464,7 +509,7 @@ func (w *World) Control(op, target, value, reason string) error {
|
||||
if w.Phase == "won" || w.Phase == "lost" {
|
||||
return errors.New("world has already ended")
|
||||
}
|
||||
for _, c := range w.Characters {
|
||||
for _, c := range w.orderedCharacters() {
|
||||
w.kill(c, "World ended by an operator: "+reason)
|
||||
}
|
||||
w.Phase = "lost"
|
||||
@@ -508,7 +553,8 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
w.dirty = true
|
||||
}
|
||||
active := map[string]bool{}
|
||||
for _, c := range w.Characters {
|
||||
characters := w.orderedCharacters()
|
||||
for _, c := range characters {
|
||||
if c.View.Entity.State != "alive" {
|
||||
continue
|
||||
}
|
||||
@@ -541,7 +587,8 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
c.MoveZ = 0
|
||||
}
|
||||
pos := c.View.Entity.Position
|
||||
nx, nz := pos.X+c.MoveX*w.Balance.PlayerSpeed*dt, pos.Z+c.MoveZ*w.Balance.PlayerSpeed*dt
|
||||
speed := w.playerSpeed(c)
|
||||
nx, nz := pos.X+c.MoveX*speed*dt, pos.Z+c.MoveZ*speed*dt
|
||||
if math.Hypot(nx, nz) < w.Balance.WorldRadius {
|
||||
c.View.Entity.Position = w.terrain.Position(nx, nz)
|
||||
}
|
||||
@@ -561,7 +608,7 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
w.updateMobs(active, dt)
|
||||
if w.Phase == "cataclysm" {
|
||||
if w.Age()-w.BossLastAttack >= w.Balance.BossCooldown {
|
||||
for _, c := range w.Characters {
|
||||
for _, c := range characters {
|
||||
if c.Online && !c.Sheltered && c.View.Entity.State == "alive" && distance(c.View.Entity.Position, w.terrain.Position(0, 0)) <= w.Balance.BossRange {
|
||||
c.View.Entity.HP -= w.Balance.BossDamage
|
||||
w.dirty = true
|
||||
@@ -579,7 +626,7 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
}
|
||||
}
|
||||
if w.SafeRadius() <= 0 || (len(w.Characters) > 0 && alive == 0) {
|
||||
for _, c := range w.Characters {
|
||||
for _, c := range characters {
|
||||
w.kill(c, "The world has been destroyed.")
|
||||
}
|
||||
w.Phase = "lost"
|
||||
@@ -613,7 +660,14 @@ func (w *World) ensureMob(ch p.Chunk) {
|
||||
w.Mobs[id] = &Mob{Entity: p.Entity{ID: id, Kind: "sentinel", Name: "Ash sentinel", Position: w.terrain.Position(x, z), HP: hp, MaxHP: hp, State: "alive"}, HomeX: int(ch.X), HomeZ: int(ch.Z)}
|
||||
}
|
||||
func (w *World) updateMobs(active map[string]bool, dt float64) {
|
||||
for id, m := range w.Mobs {
|
||||
mobIDs := make([]string, 0, len(w.Mobs))
|
||||
for id := range w.Mobs {
|
||||
mobIDs = append(mobIDs, id)
|
||||
}
|
||||
sort.Strings(mobIDs)
|
||||
characters := w.orderedCharacters()
|
||||
for _, id := range mobIDs {
|
||||
m := w.Mobs[id]
|
||||
key := Key(m.HomeX, m.HomeZ)
|
||||
if !active[key] {
|
||||
if m.Entity.HP == m.Entity.MaxHP || (m.Entity.HP <= 0 && w.Age() >= m.RespawnAt) {
|
||||
@@ -631,7 +685,7 @@ func (w *World) updateMobs(active map[string]bool, dt float64) {
|
||||
}
|
||||
var target *Character
|
||||
nearest := w.Balance.MobAggroRange
|
||||
for _, c := range w.Characters {
|
||||
for _, c := range characters {
|
||||
if !c.Online || c.View.Entity.State != "alive" || c.Sheltered {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user