Add ranged combat area effects and admin-authored mobs
This commit is contained in:
@@ -0,0 +1,324 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (w *World) mobIDs() []string {
|
||||
ids := make([]string, 0, len(w.Mobs))
|
||||
for id := range w.Mobs {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return ids
|
||||
}
|
||||
func ParseMobSpawn(value string) (int, float64, float64, error) {
|
||||
parts := strings.Split(value, ",")
|
||||
if len(parts) != 3 {
|
||||
return 0, 0, 0, errors.New("spawn requires count,x,z")
|
||||
}
|
||||
n, e1 := strconv.Atoi(parts[0])
|
||||
x, e2 := strconv.ParseFloat(parts[1], 64)
|
||||
z, e3 := strconv.ParseFloat(parts[2], 64)
|
||||
if e1 != nil || e2 != nil || e3 != nil || n < 1 || n > 32 || math.IsNaN(x) || math.IsNaN(z) || math.IsInf(x, 0) || math.IsInf(z, 0) || math.Abs(x) > 100000 || math.Abs(z) > 100000 {
|
||||
return 0, 0, 0, errors.New("choose 1-32 mobs and finite world coordinates")
|
||||
}
|
||||
return n, x, z, nil
|
||||
}
|
||||
func (w *World) mobDefinition(id string) *content.MobDefinition {
|
||||
for i := range w.Balance.Combat.Mobs {
|
||||
if w.Balance.Combat.Mobs[i].ID == id {
|
||||
return &w.Balance.Combat.Mobs[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (w *World) newMob(id string, d content.MobDefinition, pos p.Vec3, respawn float64) *Mob {
|
||||
scale := float64(d.Level) * (1 + w.Age()/86400*d.ScalePerDay)
|
||||
hp := d.HP * scale
|
||||
home := pos
|
||||
m := &Mob{Entity: p.Entity{ID: id, Kind: "mob", Model: stringPtr(d.Model), Name: d.Name, Position: pos, HP: hp, MaxHP: hp, State: "alive"}, Definition: &d, Home: &home, HomeX: ChunkCoord(pos.X, w.Balance.ChunkSize), HomeZ: ChunkCoord(pos.Z, w.Balance.ChunkSize), RespawnDelay: respawn, Power: d.Damage * scale}
|
||||
for _, a := range w.Balance.Combat.Attacks {
|
||||
if a.ID == d.AttackID {
|
||||
m.Attack = protocolAttack(a)
|
||||
}
|
||||
}
|
||||
for _, l := range w.Balance.Combat.Loot {
|
||||
if l.ID == d.LootID {
|
||||
m.Loot = l
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
func (w *World) SpawnMobs(definition, value string) error {
|
||||
if w.Phase == "won" || w.Phase == "lost" {
|
||||
return errors.New("world has ended")
|
||||
}
|
||||
n, x, z, err := ParseMobSpawn(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d := w.mobDefinition(definition)
|
||||
if d == nil {
|
||||
return errors.New("unknown mob definition")
|
||||
}
|
||||
if len(w.Mobs)+n > w.Balance.CombatRules.MaxMobs {
|
||||
return errors.New("world mob limit reached")
|
||||
}
|
||||
positions := []p.Vec3{}
|
||||
for i := 0; i < n; i++ {
|
||||
px, pz := x+float64(i%6)*2, z+float64(i/6)*2
|
||||
if math.Hypot(px, pz) >= w.SafeRadius() {
|
||||
return errors.New("spawn would leave the surviving world")
|
||||
}
|
||||
positions = append(positions, w.terrain.Position(px, pz))
|
||||
}
|
||||
for _, pos := range positions {
|
||||
id := w.combatID("mob")
|
||||
w.Mobs[id] = w.newMob(id, *d, pos, 0)
|
||||
}
|
||||
w.dirty = true
|
||||
return nil
|
||||
}
|
||||
func (w *World) ensureMob(ch p.Chunk) {
|
||||
id := "m:" + ch.Key
|
||||
if _, ok := w.Mobs[id]; ok || len(w.Mobs) >= w.Balance.CombatRules.MaxMobs {
|
||||
return
|
||||
}
|
||||
x, z := (float64(ch.X)+.55)*ch.Size, (float64(ch.Z)+.55)*ch.Size
|
||||
if math.Hypot(x, z) > w.Balance.WorldRadius {
|
||||
return
|
||||
}
|
||||
hp := w.Balance.MobHp * (1 + w.Age()/86400*w.Balance.MobScalePerDay)
|
||||
home := w.terrain.Position(x, z)
|
||||
w.Mobs[id] = &Mob{Entity: p.Entity{ID: id, Kind: "sentinel", Name: "Ash sentinel", Position: home, HP: hp, MaxHP: hp, State: "alive"}, HomeX: int(ch.X), HomeZ: int(ch.Z), Home: &home, RespawnDelay: w.Balance.MobRespawnSeconds}
|
||||
w.dirty = true
|
||||
}
|
||||
func (w *World) updateSpawns(active map[string]bool) {
|
||||
for _, rule := range w.Balance.Combat.Spawns {
|
||||
d := w.mobDefinition(rule.MobID)
|
||||
if d == nil {
|
||||
continue
|
||||
}
|
||||
for i := int64(0); i < rule.Count; i++ {
|
||||
id := fmt.Sprintf("spawn:%s:%d", rule.ID, i)
|
||||
if _, ok := w.Mobs[id]; ok {
|
||||
continue
|
||||
}
|
||||
if len(w.Mobs) >= w.Balance.CombatRules.MaxMobs {
|
||||
return
|
||||
}
|
||||
angle := float64(i) * 2.399963229728653
|
||||
radius := rule.Radius * math.Sqrt(float64(i+1)/float64(rule.Count))
|
||||
x, z := rule.X+math.Cos(angle)*radius, rule.Z+math.Sin(angle)*radius
|
||||
cx, cz := ChunkCoord(x, w.Balance.ChunkSize), ChunkCoord(z, w.Balance.ChunkSize)
|
||||
if !active[Key(cx, cz)] || math.Hypot(x, z) > w.SafeRadius() {
|
||||
continue
|
||||
}
|
||||
biomeID := w.mobBiome(cx, cz)
|
||||
if len(rule.Biomes) > 0 && !slices.Contains(rule.Biomes, biomeID) {
|
||||
continue
|
||||
}
|
||||
pos := w.terrain.Position(x, z)
|
||||
near := false
|
||||
for _, c := range w.Characters {
|
||||
if c.Online && c.View.Entity.State == "alive" && distance(pos, c.View.Entity.Position) < rule.MinPlayerDistance {
|
||||
near = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if near {
|
||||
continue
|
||||
}
|
||||
w.Mobs[id] = w.newMob(id, *d, pos, rule.Respawn)
|
||||
w.Mobs[id].MinPlayerDistance = rule.MinPlayerDistance
|
||||
w.dirty = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *World) mobBiome(x, z int) string {
|
||||
if layout, ok := w.ResourceLayouts[Key(x, z)]; ok {
|
||||
if layout.BiomeID != "" {
|
||||
return layout.BiomeID
|
||||
}
|
||||
// Older explored layouts persisted only the display name. Keep their
|
||||
// established biome instead of recalculating from new catalog weights.
|
||||
for _, biome := range w.Balance.Gathering.Biomes {
|
||||
if biome.Name == layout.Biome {
|
||||
return biome.ID
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return w.terrain.biome(x, z).ID
|
||||
}
|
||||
|
||||
// Local slope checks and deterministic side steps cross chunk edges. Future solid
|
||||
// structures can extend this one movement/line-of-sight boundary.
|
||||
func (w *World) mobStep(m *Mob, to p.Vec3, speed, dt float64) {
|
||||
from := m.Entity.Position
|
||||
d := distance(from, to)
|
||||
if d < .01 {
|
||||
return
|
||||
}
|
||||
step := math.Min(d, speed*dt)
|
||||
angle := math.Atan2(to.Z-from.Z, to.X-from.X)
|
||||
for _, offset := range []float64{0, .7, -.7, 1.3, -1.3} {
|
||||
x, z := from.X+math.Cos(angle+offset)*step, from.Z+math.Sin(angle+offset)*step
|
||||
if math.Hypot(x, z) >= w.SafeRadius() {
|
||||
continue
|
||||
}
|
||||
dest := w.terrain.Position(x, z)
|
||||
if math.Abs(dest.Y-from.Y) > w.Balance.CombatRules.MaxSlope*step {
|
||||
continue
|
||||
}
|
||||
m.Entity.Position = dest
|
||||
return
|
||||
}
|
||||
}
|
||||
func (w *World) updateMobs(active map[string]bool, dt float64) {
|
||||
w.updateSpawns(active)
|
||||
for _, id := range w.mobIDs() {
|
||||
m := w.Mobs[id]
|
||||
if m.Home == nil {
|
||||
pos := w.terrain.Position((float64(m.HomeX)+.55)*w.Balance.ChunkSize, (float64(m.HomeZ)+.55)*w.Balance.ChunkSize)
|
||||
m.Home = &pos
|
||||
m.RespawnDelay = w.Balance.MobRespawnSeconds
|
||||
}
|
||||
homeActive := active[Key(m.HomeX, m.HomeZ)]
|
||||
currentActive := active[Key(ChunkCoord(m.Entity.Position.X, w.Balance.ChunkSize), ChunkCoord(m.Entity.Position.Z, w.Balance.ChunkSize))]
|
||||
if m.Entity.HP <= 0 {
|
||||
if m.RespawnDelay == 0 {
|
||||
delete(w.Mobs, id)
|
||||
w.dirty = true
|
||||
continue
|
||||
}
|
||||
if !homeActive || w.Age() < m.RespawnAt {
|
||||
continue
|
||||
}
|
||||
blocked := false
|
||||
for _, c := range w.Characters {
|
||||
if c.Online && c.View.Entity.State == "alive" && distance(c.View.Entity.Position, *m.Home) < m.MinPlayerDistance {
|
||||
blocked = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if blocked {
|
||||
continue
|
||||
}
|
||||
hp := w.Balance.MobHp * (1 + w.Age()/86400*w.Balance.MobScalePerDay)
|
||||
if m.Definition != nil {
|
||||
d := m.Definition
|
||||
scale := float64(d.Level) * (1 + w.Age()/86400*d.ScalePerDay)
|
||||
hp = d.HP * scale
|
||||
m.Power = d.Damage * scale
|
||||
}
|
||||
m.Entity.HP = hp
|
||||
m.Entity.MaxHP = hp
|
||||
m.Entity.State = "alive"
|
||||
m.Entity.Position = *m.Home
|
||||
m.Target = ""
|
||||
w.dirty = true
|
||||
}
|
||||
if !homeActive && !currentActive {
|
||||
continue
|
||||
}
|
||||
speed, aggro, leash := w.Balance.MobSpeed, w.Balance.MobAggroRange, w.Balance.ChunkSize*2
|
||||
behavior := "aggressive"
|
||||
attack := p.CombatAttack{ID: "legacy_mob", Name: "Sentinel strike", Delivery: "melee", Effect: "damage", Stat: "meleeDamage", Range: w.Balance.MobAttackRange, Cooldown: w.Balance.MobCooldown, Multiplier: 1, Speed: 1, Interval: 1}
|
||||
power := w.Balance.MobDamage * (1 + w.Age()/86400*w.Balance.MobScalePerDay)
|
||||
if d := m.Definition; d != nil {
|
||||
speed, aggro, leash, behavior = d.Speed, d.AggroRange, d.LeashRange, d.Behavior
|
||||
attack = m.Attack
|
||||
power = m.Power
|
||||
}
|
||||
if distance(m.Entity.Position, *m.Home) > leash {
|
||||
m.Returning = true
|
||||
m.Target = ""
|
||||
}
|
||||
if m.Returning {
|
||||
if distance(m.Entity.Position, *m.Home) < 1 {
|
||||
m.Returning = false
|
||||
} else {
|
||||
w.mobStep(m, *m.Home, speed, dt)
|
||||
}
|
||||
continue
|
||||
}
|
||||
var target *Character
|
||||
if c := w.Characters[m.Target]; c != nil && c.Online && !c.Sheltered && c.View.Entity.State == "alive" && distance(c.View.Entity.Position, *m.Home) <= leash {
|
||||
target = c
|
||||
}
|
||||
if target == nil {
|
||||
m.Target = ""
|
||||
if behavior != "passive" {
|
||||
nearest := aggro
|
||||
for _, c := range w.orderedCharacters() {
|
||||
if !c.Online || c.Sheltered || c.View.Entity.State != "alive" {
|
||||
continue
|
||||
}
|
||||
d := distance(c.View.Entity.Position, m.Entity.Position)
|
||||
if d < nearest && distance(c.View.Entity.Position, *m.Home) <= leash {
|
||||
if _, blocked := w.terrainHit(body(m.Entity.Position), body(c.View.Entity.Position)); blocked {
|
||||
continue
|
||||
}
|
||||
target = c
|
||||
nearest = d
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if target == nil {
|
||||
if distance(m.Entity.Position, *m.Home) > 1 {
|
||||
w.mobStep(m, *m.Home, speed, dt)
|
||||
}
|
||||
continue
|
||||
}
|
||||
m.Target = target.View.Entity.ID
|
||||
if behavior == "guard" && distance(target.View.Entity.Position, *m.Home) > aggro {
|
||||
m.Target = ""
|
||||
w.mobStep(m, *m.Home, speed, dt)
|
||||
continue
|
||||
}
|
||||
aim := target.View.Entity.Position
|
||||
targetID := target.View.Entity.ID
|
||||
if attack.Effect == "heal" {
|
||||
aim = m.Entity.Position
|
||||
targetID = m.Entity.ID
|
||||
}
|
||||
if length3(body(m.Entity.Position), body(aim)) <= attack.Range {
|
||||
if w.Age()-m.LastAttack < attack.Cooldown {
|
||||
continue
|
||||
}
|
||||
if _, blocked := w.terrainHit(body(m.Entity.Position), body(aim)); blocked {
|
||||
w.mobStep(m, aim, speed, dt)
|
||||
continue
|
||||
}
|
||||
if attack.Delivery == "projectile" && len(w.Projectiles) >= w.Balance.CombatRules.MaxProjectiles {
|
||||
continue
|
||||
}
|
||||
if attack.Duration > 0 && len(w.Areas)+w.pendingAreas() >= w.Balance.CombatRules.MaxAreas {
|
||||
continue
|
||||
}
|
||||
shot := Projectile{ID: w.combatID("projectile"), Source: id, Player: false, Position: body(m.Entity.Position), Destination: body(aim), Attack: attack, Power: power * attack.Multiplier, Target: targetID}
|
||||
m.LastAttack = w.Age()
|
||||
if attack.Delivery == "projectile" {
|
||||
w.Projectiles[shot.ID] = &shot
|
||||
} else {
|
||||
shot.Position = body(aim)
|
||||
w.impact(shot, targetID)
|
||||
}
|
||||
w.dirty = true
|
||||
} else {
|
||||
w.mobStep(m, aim, speed, dt)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user