Add biome revisions readable content and visible projectile outcomes
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"aeternalis/server/content"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBiomeResourceRevisionPreservesExploredChunks(t *testing.T) {
|
||||
w, _ := fixture(t)
|
||||
g := w.Balance.Gathering
|
||||
g.Biomes = []content.BiomeDefinition{{ID: "valley", Name: "Valley", Color: "#445566", Weight: 1, Revision: 1, Resources: []string{"tree"}}}
|
||||
before := w.chunk(2, 2)
|
||||
if len(before.Resources) == 0 {
|
||||
t.Fatal("existing tree could not be reused in a new biome")
|
||||
}
|
||||
for _, r := range before.Resources {
|
||||
if stringValue(r.ResourceID) != "tree" {
|
||||
t.Fatal("unselected resource generated")
|
||||
}
|
||||
}
|
||||
g.Biomes[0].Revision = 2
|
||||
g.Biomes[0].Resources = []string{}
|
||||
delete(w.chunks, before.Key)
|
||||
after := w.chunk(2, 2)
|
||||
if !reflect.DeepEqual(before, after) {
|
||||
t.Fatal("biome revision rewrote an explored chunk")
|
||||
}
|
||||
if len(w.chunk(3, 3).Resources) != 0 {
|
||||
t.Fatal("new chunk ignored empty resource selection")
|
||||
}
|
||||
// Saved geometry/resource identity survives restart independently of the active revision.
|
||||
raw, err := json.Marshal(w)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var restored World
|
||||
if err = json.Unmarshal(raw, &restored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Restore validates all resource biome references; use the normal default list,
|
||||
// while keeping the explored layout under test.
|
||||
b, _ := content.Load()
|
||||
restored.Balance.Gathering = b.Gathering
|
||||
if err = restored.Restore(w.now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(before, restored.chunk(2, 2)) {
|
||||
t.Fatal("restart rewrote explored layout")
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,11 @@ func (t *Terrain) Chunk(x, z int) p.Chunk {
|
||||
}
|
||||
c.Resources = []p.Entity{}
|
||||
for _, r := range t.B.Gathering.Resources {
|
||||
if !slices.Contains(r.Biomes, biome.ID) {
|
||||
allowed := slices.Contains(r.Biomes, biome.ID)
|
||||
if biome.Resources != nil {
|
||||
allowed = slices.Contains(biome.Resources, r.ID)
|
||||
}
|
||||
if !allowed {
|
||||
continue
|
||||
}
|
||||
hash := fnv.New64a()
|
||||
|
||||
@@ -27,12 +27,22 @@ type CombatArea struct {
|
||||
}
|
||||
|
||||
func protocolAttack(a content.CombatAttack) p.CombatAttack {
|
||||
return p.CombatAttack{ID: a.ID, Name: a.Name, Description: a.Description, Delivery: a.Delivery, Effect: a.Effect, Stat: a.Stat, Multiplier: a.Multiplier, Range: a.Range, Cooldown: a.Cooldown, Stamina: a.Stamina, Speed: a.Speed, Radius: a.Radius, Duration: a.Duration, Interval: a.Interval}
|
||||
return p.CombatAttack{ID: a.ID, Name: a.Name, Description: a.Description, Delivery: a.Delivery, Effect: a.Effect, Stat: a.Stat, Multiplier: a.Multiplier, Range: a.Range, Cooldown: a.Cooldown, Stamina: a.Stamina, Speed: a.Speed, DexteritySpeed: &a.DexteritySpeed, StrengthSpeed: &a.StrengthSpeed, Radius: a.Radius, Duration: a.Duration, Interval: a.Interval}
|
||||
}
|
||||
func (w *World) playerAttack(c *Character) p.CombatAttack {
|
||||
for _, i := range c.View.Items {
|
||||
if i.ID == c.View.Equipment["weapon"] && i.Attack != nil {
|
||||
return *i.Attack
|
||||
a := *i.Attack
|
||||
if a.Delivery == "projectile" {
|
||||
if a.DexteritySpeed != nil {
|
||||
a.Speed += c.View.Stats["dexterity"].Final * *a.DexteritySpeed
|
||||
}
|
||||
if a.StrengthSpeed != nil {
|
||||
a.Speed += c.View.Stats["strength"].Final * *a.StrengthSpeed
|
||||
}
|
||||
a.Speed = math.Max(1, math.Min(100, a.Speed))
|
||||
}
|
||||
return a
|
||||
}
|
||||
}
|
||||
return p.CombatAttack{ID: "basic_melee", Name: "Melee attack", Delivery: "melee", Effect: "damage", Stat: "meleeDamage", Multiplier: 1, Range: w.Balance.AttackRange, Cooldown: w.Balance.AttackCooldown, Speed: 1, Interval: 1}
|
||||
@@ -137,6 +147,7 @@ func (w *World) attack(c *Character, target string) error {
|
||||
shot := Projectile{ID: w.combatID("projectile"), Source: c.View.Entity.ID, Player: true, Target: target, Position: from, Destination: to, Attack: a, Power: power}
|
||||
if a.Delivery == "projectile" {
|
||||
w.Projectiles[shot.ID] = &shot
|
||||
w.projectileVisual(shot, "launch", "")
|
||||
} else {
|
||||
shot.Position = to
|
||||
w.impact(shot, target)
|
||||
@@ -254,6 +265,7 @@ func (w *World) updateCombat(dt float64) {
|
||||
for _, id := range ids {
|
||||
shot := w.Projectiles[id]
|
||||
if !w.sourceActive(shot.Source, shot.Player) {
|
||||
w.projectileVisual(*shot, "impact", "cancelled")
|
||||
delete(w.Projectiles, id)
|
||||
w.dirty = true
|
||||
continue
|
||||
@@ -302,6 +314,13 @@ func (w *World) updateCombat(dt float64) {
|
||||
copy.Position = hitPoint
|
||||
}
|
||||
delete(w.Projectiles, id)
|
||||
outcome := "miss"
|
||||
if hit != "" {
|
||||
outcome = "hit"
|
||||
} else if blocked {
|
||||
outcome = "terrain"
|
||||
}
|
||||
w.projectileVisual(copy, "impact", outcome)
|
||||
w.impact(copy, hit)
|
||||
} else {
|
||||
shot.Position = end
|
||||
@@ -326,6 +345,16 @@ func (w *World) updateCombat(dt float64) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Short, bounded event history lets a launch and impact survive between network
|
||||
// snapshots. This is presentation data; damage is resolved only by updateCombat.
|
||||
func (w *World) projectileVisual(shot Projectile, phase, outcome string) {
|
||||
w.ProjectileVisuals = append(w.ProjectileVisuals, p.ProjectileVisual{ID: w.combatID("visual"), ProjectileID: shot.ID, Phase: phase, Position: shot.Position, Destination: shot.Destination, Speed: shot.Attack.Speed, Outcome: outcome, At: w.Age()})
|
||||
if len(w.ProjectileVisuals) > 1024 {
|
||||
w.ProjectileVisuals = w.ProjectileVisuals[len(w.ProjectileVisuals)-1024:]
|
||||
}
|
||||
w.dirty = true
|
||||
}
|
||||
func (w *World) mobKilled(c *Character, m *Mob) {
|
||||
if m.Entity.State == "dead" {
|
||||
return
|
||||
|
||||
@@ -250,3 +250,32 @@ func TestRegionalSpawnsRespectExploredBiomeAndPlayerDistance(t *testing.T) {
|
||||
t.Fatal("regional slot was missing or duplicated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectileSpeedSampleAndShortFlightEvents(t *testing.T) {
|
||||
w, c, m := combatFixture(t)
|
||||
dex, str := float64(2), float64(1)
|
||||
weapon := &c.View.Items[len(c.View.Items)-1]
|
||||
weapon.Attack.DexteritySpeed = &dex
|
||||
weapon.Attack.StrengthSpeed = &str
|
||||
c.View.Stats["dexterity"] = p.StatValue{Final: 5}
|
||||
c.View.Stats["strength"] = p.StatValue{Final: 3}
|
||||
m.Entity.Position = w.terrain.Position(2, 0)
|
||||
if err := w.attack(c, m.Entity.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(w.ProjectileVisuals) != 1 || w.ProjectileVisuals[0].Phase != "launch" || w.ProjectileVisuals[0].Speed != 48 {
|
||||
t.Fatal("wrong launch speed/event")
|
||||
}
|
||||
c.View.Stats["dexterity"] = p.StatValue{Final: 100}
|
||||
w.updateCombat(.1)
|
||||
if len(w.Projectiles) != 0 || len(w.ProjectileVisuals) != 2 || w.ProjectileVisuals[1].Outcome != "hit" {
|
||||
t.Fatal("short flight lost its hit")
|
||||
}
|
||||
if w.ProjectileVisuals[1].Speed != 48 {
|
||||
t.Fatal("stat change altered in-flight speed")
|
||||
}
|
||||
snapshot := w.Snapshot(c.View.Entity.ID, map[string]bool{})
|
||||
if len(snapshot.ProjectileVisuals) != 2 {
|
||||
t.Fatal("flight between snapshots became invisible")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,6 +312,7 @@ func (w *World) updateMobs(active map[string]bool, dt float64) {
|
||||
m.LastAttack = w.Age()
|
||||
if attack.Delivery == "projectile" {
|
||||
w.Projectiles[shot.ID] = &shot
|
||||
w.projectileVisual(shot, "launch", "")
|
||||
} else {
|
||||
shot.Position = body(aim)
|
||||
w.impact(shot, targetID)
|
||||
|
||||
@@ -70,9 +70,10 @@ type ResourceLayout struct {
|
||||
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"`
|
||||
ProjectileVisuals []p.ProjectileVisual `json:"projectileVisuals,omitempty"`
|
||||
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"`
|
||||
|
||||
@@ -636,6 +637,13 @@ func (w *World) Advance(now time.Time, dt float64) {
|
||||
playedDT := math.Max(0, dt)
|
||||
w.now = now
|
||||
w.Tick++
|
||||
kept := w.ProjectileVisuals[:0]
|
||||
for _, v := range w.ProjectileVisuals {
|
||||
if w.Age()-v.At <= 2 {
|
||||
kept = append(kept, v)
|
||||
}
|
||||
}
|
||||
w.ProjectileVisuals = kept
|
||||
dt = math.Max(0, math.Min(dt, 0.25))
|
||||
if w.Phase == "won" || w.Phase == "lost" {
|
||||
return
|
||||
@@ -798,6 +806,12 @@ func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
|
||||
visible := func(pos p.Vec3) bool {
|
||||
return allowed[Key(ChunkCoord(pos.X, w.Balance.ChunkSize), ChunkCoord(pos.Z, w.Balance.ChunkSize))]
|
||||
}
|
||||
s.ProjectileVisuals = []p.ProjectileVisual{}
|
||||
for _, v := range w.ProjectileVisuals {
|
||||
if w.Age()-v.At <= 2 && (visible(v.Position) || visible(v.Destination)) {
|
||||
s.ProjectileVisuals = append(s.ProjectileVisuals, v)
|
||||
}
|
||||
}
|
||||
for _, other := range w.Characters {
|
||||
if other.View.Entity.ID != id && other.Online && other.View.Entity.State != "dead" && visible(other.View.Entity.Position) {
|
||||
s.Entities = append(s.Entities, other.View.Entity)
|
||||
|
||||
Reference in New Issue
Block a user