212 lines
7.8 KiB
Go
212 lines
7.8 KiB
Go
package game
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
"encoding/json"
|
|
"math"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func gatheringArrival(t *testing.T, w *World, c *Character) {
|
|
t.Helper()
|
|
w.now = w.CreatedAt.Add(time.Minute)
|
|
c.View.Entity.Position = w.terrain.Position(12, 12)
|
|
}
|
|
func TestGatheringSkillsModifiersAndConsumables(t *testing.T) {
|
|
w, c := fixture(t)
|
|
gatheringArrival(t, w, c)
|
|
// A scoped item changes effective level, yield, speed and XP through one path.
|
|
mods := []p.StatModifier{{Stat: "gatheringLevel", Skill: stringPtr("lumberjack"), Flat: 5}, {Stat: "gatheringYield", Skill: stringPtr("lumberjack"), Percent: 100}, {Stat: "gatheringSpeed", Skill: stringPtr("lumberjack"), Percent: 100}, {Stat: "gatheringXP", Skill: stringPtr("lumberjack"), Percent: 100}}
|
|
c.View.Items = append(c.View.Items, p.ItemInstance{ID: "test-ring", DefinitionID: "test-ring", Name: "Test ring", Slot: "ring", Requirements: p.ItemRequirements{Level: 1}, Modifiers: mods})
|
|
c.View.Equipment["ring1"] = "test-ring"
|
|
w.RefreshStats(c)
|
|
if c.View.Gathering["lumberjack"].Level != 1 || c.View.Gathering["lumberjack"].EffectiveLevel != 6 || c.View.Gathering["miner"].EffectiveLevel != 1 {
|
|
t.Fatal("level bonus leaked or granted earned levels")
|
|
}
|
|
if err := w.gather(c, "arrival:tree"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Inventory["wood"] != 2 || c.View.Skills["lumberjack"] != 4 || c.View.Skills["miner"] != 0 {
|
|
t.Fatal("wrong scoped yield or XP", c.View.Inventory, c.View.Skills)
|
|
}
|
|
if err := w.gather(c, "arrival:tree"); err == nil {
|
|
t.Fatal("depleted node rewarded twice")
|
|
}
|
|
delete(w.Depleted, "arrival:tree")
|
|
w.now = w.now.Add(200 * time.Millisecond)
|
|
if err := w.gather(c, "arrival:tree"); err == nil {
|
|
t.Fatal("gather speed bypassed cooldown")
|
|
}
|
|
w.now = w.now.Add(200 * time.Millisecond)
|
|
if err := w.gather(c, "arrival:tree"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := w.unequip(c, "ring1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Gathering["lumberjack"].EffectiveLevel != 1 || c.View.Skills["lumberjack"] != 8 {
|
|
t.Fatal("unequip changed earned XP")
|
|
}
|
|
c.View.Inventory["gathering_tonic"] = 2
|
|
if err := w.consumeGathering(c, "gathering_tonic"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
first := c.View.GatheringEffects[0].ExpiresAt
|
|
w.now = w.now.Add(time.Second)
|
|
if err := w.consumeGathering(c, "gathering_tonic"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(c.View.GatheringEffects) != 1 || c.View.GatheringEffects[0].ExpiresAt <= first || c.View.Gathering["miner"].EffectiveLevel != 6 || c.View.Inventory["gathering_tonic"] != 0 {
|
|
t.Fatal("consumption did not refresh one source")
|
|
}
|
|
if err := w.consumeGathering(c, "gathering_tonic"); err == nil {
|
|
t.Fatal("used missing consumable")
|
|
}
|
|
raw, _ := json.Marshal(w)
|
|
var restored World
|
|
if err := json.Unmarshal(raw, &restored); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := restored.Restore(w.now.Add(121 * time.Second)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rc := restored.Characters[c.View.Entity.ID]
|
|
if rc.View.Gathering["miner"].EffectiveLevel != 1 || rc.View.Skills["lumberjack"] != 8 {
|
|
t.Fatal("restart renewed expired bonus or lost progression")
|
|
}
|
|
}
|
|
func TestGatheringRiskCeilingFailureDeathAndReplacement(t *testing.T) {
|
|
w, c := fixture(t)
|
|
gatheringArrival(t, w, c)
|
|
r := w.resource("tree")
|
|
r.RequiredLevel = 22
|
|
before := c.View.GatherAttempt
|
|
if err := w.gather(c, "arrival:tree"); err == nil || c.View.GatherAttempt != before || len(w.Depleted) != 0 {
|
|
t.Fatal("accepted +21 or mutated rejection")
|
|
}
|
|
r.RequiredLevel = 21
|
|
risk := w.gatherRisk(c, *r)
|
|
if !risk.Allowed || risk.Failure < 95 || risk.Destruction < 90 || risk.Negative == 0 {
|
|
t.Fatal("+20 should be allowed but extremely risky", risk)
|
|
}
|
|
// Force each probabilistic outcome independently for deterministic lifecycle coverage.
|
|
rules := w.Balance.GatheringRules
|
|
rules.FailureBase = 100
|
|
rules.FailureMax = 100
|
|
rules.DestroyBase = 100
|
|
rules.NegativeChance = 100
|
|
if err := w.gather(c, "arrival:tree"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Skills["lumberjack"] != 0 || c.View.Inventory["wood"] != 0 || w.Depleted["arrival:tree"] <= w.Age() || len(c.View.GatheringEffects) != 1 || c.View.Entity.HP >= c.View.Entity.MaxHP {
|
|
t.Fatal("failure gave rewards or omitted consequences")
|
|
}
|
|
c.View.Entity.HP = 1
|
|
delete(w.Depleted, "arrival:tree")
|
|
w.now = w.now.Add(time.Minute)
|
|
if err := w.gather(c, "arrival:tree"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Entity.State != "dead" || len(w.Corpses) != 1 {
|
|
t.Fatal("lethal failure did not use permanent-death path")
|
|
}
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "gather", Seq: 1, Target: "arrival:tree"}); err == nil {
|
|
t.Fatal("dead character gathered")
|
|
}
|
|
replacement, err := w.Join(c.Account, "Replacement")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if replacement.View.Gathering["lumberjack"].Level != 1 || len(replacement.View.GatheringEffects) != 0 {
|
|
t.Fatal("replacement inherited progress or effects")
|
|
}
|
|
}
|
|
func TestGatheringProgressAndEffectsTravel(t *testing.T) {
|
|
w, c := fixture(t)
|
|
c.View.Skills["miner"] = 90
|
|
c.View.Inventory["gathering_tonic"] = 1
|
|
if err := w.consumeGathering(c, "gathering_tonic"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.DailySeconds = 123
|
|
c.View.Entity.State = "eligible"
|
|
expiry := c.View.GatheringEffects[0].ExpiresAt
|
|
b, _ := content.Load()
|
|
dest := New("next", 42, w.now, b)
|
|
if err := dest.Import(c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.View.Skills["miner"] != 90 || c.View.Gathering["miner"].Level != 1 || c.View.Gathering["miner"].EffectiveLevel != 6 || c.DailySeconds != 123 || c.View.GatheringEffects[0].ExpiresAt != expiry {
|
|
t.Fatal("travel reset XP, usage, or renewed effects")
|
|
}
|
|
if err := dest.Connect(c.View.Entity.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.DailySeconds = b.DailyPlaySeconds
|
|
dest.Advance(w.now.Add(time.Second), 1)
|
|
if c.Online || !c.Sheltered {
|
|
t.Fatal("daily cap bypassed")
|
|
}
|
|
if err := dest.Intent(c.View.Entity.ID, p.Intent{Op: "jump", Seq: 1}); err == nil {
|
|
t.Fatal("capped player acted")
|
|
}
|
|
}
|
|
func TestJumpAndPersistedResourceLayouts(t *testing.T) {
|
|
w, c := fixture(t)
|
|
ground := c.View.Entity.Position.Y
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "jump", Seq: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.Advance(w.now.Add(100*time.Millisecond), .1)
|
|
if c.View.Entity.Position.Y <= ground {
|
|
t.Fatal("jump did not rise")
|
|
}
|
|
if err := w.Intent(c.View.Entity.ID, p.Intent{Op: "jump", Seq: 2}); err == nil {
|
|
t.Fatal("air jump accepted")
|
|
}
|
|
for i := 0; i < 30; i++ {
|
|
w.Advance(w.now.Add(50*time.Millisecond), .05)
|
|
}
|
|
if math.Abs(c.View.Entity.Position.Y-ground) > 1e-8 || c.JumpVelocity != 0 {
|
|
t.Fatal("jump did not land on authoritative terrain")
|
|
}
|
|
ch := w.chunk(0, 0)
|
|
depleted := ch.Resources[0].ID
|
|
w.Depleted[depleted] = w.Age() + 100
|
|
w.Balance.Gathering.Resources = append(w.Balance.Gathering.Resources, content.ResourceDefinition{ID: "added", Name: "Added", SkillID: "miner", Model: "ore", Output: "ore", RequiredLevel: 1, Amount: 1, Seconds: 1, RespawnSeconds: 60, Density: 32, Biomes: []string{"forest", "meadow", "highlands"}})
|
|
raw, _ := json.Marshal(w)
|
|
var restored World
|
|
_ = json.Unmarshal(raw, &restored)
|
|
if err := restored.Restore(w.now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(ch, restored.chunk(0, 0)) || restored.Depleted[depleted] != w.Depleted[depleted] {
|
|
t.Fatal("catalog/restart rerolled explored nodes or depletion")
|
|
}
|
|
found := false
|
|
for _, r := range restored.chunk(5, 0).Resources {
|
|
if stringValue(r.ResourceID) == "added" {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("new resource missing from unexplored chunk")
|
|
}
|
|
old := NewTerrain(w.Seed, w.Balance)
|
|
old.Version = 1
|
|
restored.Version = 1
|
|
restored.ResourceLayouts = nil
|
|
if err := restored.Restore(w.now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if restored.terrain.Height(130, 75) != old.Height(130, 75) {
|
|
t.Fatal("legacy terrain changed")
|
|
}
|
|
if math.Abs(old.Height(130, 75)-w.terrain.Height(130, 75)) < .01 {
|
|
t.Fatal("new terrain is not deeper")
|
|
}
|
|
}
|