Add ranged combat area effects and admin-authored mobs

This commit is contained in:
dingo
2026-09-09 05:26:31 -04:00
parent 7a4c6c9e9b
commit 0b4eaa153c
29 changed files with 2220 additions and 181 deletions
+66 -131
View File
@@ -15,6 +15,7 @@ import (
)
type Character struct {
AttackReadyAt float64 `json:"attackReadyAt"`
JumpVelocity float64 `json:"jumpVelocity"`
GatherReadyAt float64 `json:"gatherReadyAt"`
@@ -46,6 +47,16 @@ type Corpse struct {
Items []p.ItemInstance `json:"items"`
}
type Mob struct {
Definition *content.MobDefinition `json:"definition,omitempty"`
Attack p.CombatAttack `json:"attack"`
Loot content.LootTable `json:"loot"`
Home *p.Vec3 `json:"home,omitempty"`
Power float64 `json:"power"`
Target string `json:"target"`
Returning bool `json:"returning"`
RespawnDelay float64 `json:"respawnDelay"`
MinPlayerDistance float64 `json:"minPlayerDistance"`
Entity p.Entity `json:"entity"`
HomeX int `json:"homeX"`
HomeZ int `json:"homeZ"`
@@ -53,11 +64,16 @@ type Mob struct {
RespawnAt float64 `json:"respawnAt"`
}
type ResourceLayout struct {
BiomeID string `json:"biomeId,omitempty"`
Biome string `json:"biome"`
Color *string `json:"color"`
Resources []p.Entity `json:"resources"`
}
type World struct {
Projectiles map[string]*Projectile `json:"projectiles,omitempty"`
Areas map[string]*CombatArea `json:"areas,omitempty"`
CombatSequence uint64 `json:"combatSequence"`
ResourceLayouts map[string]ResourceLayout `json:"resourceLayouts,omitempty"`
Version int `json:"version"`
@@ -159,6 +175,19 @@ func (w *World) Restore(now time.Time) error {
if w.ResourceLayouts == nil {
w.ResourceLayouts = map[string]ResourceLayout{}
}
if w.Balance.Combat == nil || w.Balance.CombatRules == nil {
b, e := content.Load()
if e != nil {
return e
}
if w.Balance.Combat == nil {
w.Balance.Combat = b.Combat
}
if w.Balance.CombatRules == nil {
w.Balance.CombatRules = b.CombatRules
}
w.dirty = true
}
if err := w.Balance.Validate(); err != nil {
return err
}
@@ -188,6 +217,9 @@ func (w *World) Restore(now time.Time) error {
c.MoveZ = 0
c.View.ACK = 0
}
if err := w.restoreCombat(); err != nil {
return err
}
return w.restoreCorpseEquipment()
}
func (w *World) Age() float64 {
@@ -324,6 +356,7 @@ func (w *World) Import(c *Character) error {
c.MoveZ = 0
c.View.ACK = 0
c.LastAttack = 0
c.AttackReadyAt = 0
c.LastGather = 0
c.GatherReadyAt = 0
c.JumpVelocity = 0
@@ -383,7 +416,7 @@ func (w *World) chunk(x, z int) p.Chunk {
if layout, ok := w.ResourceLayouts[key]; ok {
c.Biome, c.Color, c.Resources = layout.Biome, layout.Color, layout.Resources
} else {
w.ResourceLayouts[key] = ResourceLayout{Biome: c.Biome, Color: c.Color, Resources: c.Resources}
w.ResourceLayouts[key] = ResourceLayout{BiomeID: w.terrain.biome(x, z).ID, Biome: c.Biome, Color: c.Color, Resources: c.Resources}
w.dirty = true
}
w.chunks[key] = c
@@ -498,69 +531,27 @@ func (w *World) craft(c *Character, name string) error {
w.dirty = true
return nil
}
func (w *World) attack(c *Character, target string) error {
if w.Age()-c.LastAttack < w.Balance.AttackCooldown {
return errors.New("attack cooldown")
}
damage := c.View.Stats["meleeDamage"].Final
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")
}
if w.roll(c.View.Stats["criticalChance"].Final) {
damage *= w.Balance.CriticalMultiplier
}
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
// 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
survivor.Message = "The world is saved. Passage is open."
}
func (w *World) damageBoss(c *Character, damage float64) {
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
// 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
survivor.Message = "The world is saved. Passage is open."
}
}
} else {
m := w.Mobs[target]
if m == nil || m.Entity.HP <= 0 || distance(c.View.Entity.Position, m.Entity.Position) > w.Balance.AttackRange {
return errors.New("target out of reach")
}
if w.roll(c.View.Stats["criticalChance"].Final) {
damage *= w.Balance.CriticalMultiplier
}
m.Entity.HP = math.Max(0, m.Entity.HP-damage)
if m.Entity.HP == 0 {
m.Entity.State = "dead"
m.RespawnAt = w.Age() + w.Balance.MobRespawnSeconds
c.View.Kills++
c.WorldKills++
c.View.Inventory["herb"] += w.yieldAmount(c.View.Stats["dropRate"].Final)
for _, item := range w.Balance.Items {
if w.roll(item.DropChance * c.View.Stats["dropRate"].Final / 100) {
if err := w.grantItems(c, item.ID, 1); err != nil {
return err
}
}
}
w.RefreshStats(c)
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
}
func (w *World) kill(c *Character, reason string) {
if c.View.Entity.State != "alive" {
@@ -596,6 +587,8 @@ func (w *World) kill(c *Character, reason string) {
// Operator changes use the same simulation and persistence path as gameplay.
func (w *World) Control(op, target, value, reason string) error {
switch op {
case "spawn-mobs":
return w.SpawnMobs(target, value)
case "close-admissions", "open-admissions":
if w.Phase == "won" || w.Phase == "lost" {
return errors.New("world has ended")
@@ -718,6 +711,7 @@ func (w *World) Advance(now time.Time, dt float64) {
}
}
w.updateMobs(active, dt)
w.updateCombat(dt)
if w.Phase == "cataclysm" {
if w.Age()-w.BossLastAttack >= w.Balance.BossCooldown {
for _, c := range characters {
@@ -759,76 +753,6 @@ func (w *World) Advance(now time.Time, dt float64) {
}
}
}
func (w *World) ensureMob(ch p.Chunk) {
id := "m:" + ch.Key
if _, ok := w.Mobs[id]; ok {
return
}
x, z := (float64(ch.X)+0.55)*ch.Size, (float64(ch.Z)+0.55)*ch.Size
if math.Hypot(x, z) > w.Balance.WorldRadius {
return
}
hp := w.Balance.MobHp * (1 + w.Age()/86400*w.Balance.MobScalePerDay)
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) {
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) {
delete(w.Mobs, id)
}
continue
}
if m.Entity.HP <= 0 {
if w.Age() >= m.RespawnAt {
m.Entity.HP = w.Balance.MobHp * (1 + w.Age()/86400*w.Balance.MobScalePerDay)
m.Entity.MaxHP = m.Entity.HP
m.Entity.State = "alive"
}
continue
}
var target *Character
nearest := w.Balance.MobAggroRange
for _, c := range characters {
if !c.Online || c.View.Entity.State != "alive" || c.Sheltered {
continue
}
d := distance(c.View.Entity.Position, m.Entity.Position)
if d < nearest {
nearest = d
target = c
}
}
if target == nil {
continue
}
if nearest <= w.Balance.MobAttackRange {
if w.Age()-m.LastAttack >= w.Balance.MobCooldown {
w.damageCharacter(target, w.Balance.MobDamage*(1+w.Age()/86400*w.Balance.MobScalePerDay))
m.LastAttack = w.Age()
w.dirty = true
if target.View.Entity.HP <= 0 {
w.kill(target, "An ash sentinel ended this character's journey.")
}
}
} else {
pos, to := m.Entity.Position, target.View.Entity.Position
step := math.Min(nearest, w.Balance.MobSpeed*dt)
x, z := pos.X+(to.X-pos.X)/nearest*step, pos.Z+(to.Z-pos.Z)/nearest*step
if ChunkCoord(x, w.Balance.ChunkSize) == m.HomeX && ChunkCoord(z, w.Balance.ChunkSize) == m.HomeZ {
m.Entity.Position = w.terrain.Position(x, z)
}
}
}
}
func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
c := w.Characters[id]
s := p.Snapshot{Type: "snapshot", Version: 1, Tick: w.Tick, World: w.Info(), Self: c.View, Message: c.Message, Entities: []p.Entity{}, Chunks: []p.Chunk{}, ChunkKeys: []string{}, UnavailableResources: []string{}}
@@ -892,6 +816,17 @@ func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
if w.Phase == "cataclysm" && visible(w.terrain.Position(0, 0)) {
s.Entities = append(s.Entities, p.Entity{ID: "boss", Kind: "boss", Name: "The Harbinger", HP: w.BossHP, MaxHP: w.Balance.BossHp, Position: w.terrain.Position(0, 0), State: "alive"})
}
for _, shot := range w.Projectiles {
if visible(shot.Position) {
s.Entities = append(s.Entities, p.Entity{ID: shot.ID, Kind: "projectile", Name: shot.Attack.Name, Position: shot.Position, State: shot.Attack.Effect, HP: 1, MaxHP: 1})
}
}
for _, area := range w.Areas {
if visible(area.Position) {
radius := area.Attack.Radius
s.Entities = append(s.Entities, p.Entity{ID: area.ID, Kind: "area", Name: area.Attack.Name, Position: area.Position, State: area.Attack.Effect, Radius: &radius, HP: 1, MaxHP: 1})
}
}
sort.Slice(s.Entities, func(i, j int) bool { return s.Entities[i].ID < s.Entities[j].ID })
return s
}