package game import ( "aeternalis/server/content" p "aeternalis/server/internal/protocol" "encoding/json" "math" "reflect" "testing" "time" ) func itemRequest(c *Character, op, item string) p.ItemActionRequest { return p.ItemActionRequest{ID: ID(), CharacterID: c.View.Entity.ID, Op: op, Item: item, Quantity: 1} } func TestDestroyConsumesCraftedStackWithoutLosingRemainingProvenance(t *testing.T) { w, c := fixture(t) provenance := p.CraftProvenance{CharacterID: c.View.Entity.ID, CrafterName: c.View.Entity.Name, WorldID: w.ID, RecipeID: "recipe_bandage", RecipeRevision: 1, Profession: "alchemist", Level: 1} c.View.Inventory["bandage"] = 3 c.View.CraftedStacks = []p.CraftedStack{{ID: ID(), Item: "bandage", Quantity: 3, Provenance: provenance}} req := itemRequest(c, "destroy", "bandage") req.Quantity = 2 for _, mode := range []string{"offline", "sheltered", "dead", "capped"} { c.Online = true c.Sheltered = false c.View.Entity.State = "alive" c.View.RemainingSeconds = 100 switch mode { case "offline": c.Online = false case "sheltered": c.Sheltered = true case "dead": c.View.Entity.State = "dead" case "capped": c.View.RemainingSeconds = 0 } if _, err := w.ItemAction(c, req); err == nil || c.View.Inventory["bandage"] != 3 { t.Fatalf("accepted %s destruction", mode) } } c.Online = true c.Sheltered = false c.View.Entity.State = "alive" c.View.RemainingSeconds = 100 if _, err := w.ItemAction(c, req); err != nil { t.Fatal(err) } if c.View.Inventory["bandage"] != 1 || len(c.View.CraftedStacks) != 1 || c.View.CraftedStacks[0].Quantity != 1 || c.View.CraftedStacks[0].Provenance != provenance { t.Fatal("destroy corrupted retained crafted stack") } req = itemRequest(c, "destroy", "bandage") req.Quantity = 2 c.ItemReadyAt = 0 if _, err := w.ItemAction(c, req); err == nil || c.View.Inventory["bandage"] != 1 { t.Fatal("destroyed more than owned") } w.Balance.Inventory = nil raw, _ := json.Marshal(w) var old World _ = json.Unmarshal(raw, &old) if err := old.Restore(w.now); err != nil || old.Balance.Inventory == nil || old.Balance.Inventory.StopWeightRatio != 2 { t.Fatal("old snapshot missing inventory migration", err) } } func TestEncumbranceMovementRecoveryAndVictory(t *testing.T) { w, c := fixture(t) c.View.Inventory = map[string]int64{} c.View.Items = nil w.RefreshStats(c) capacity := c.View.Stats["carryCapacity"].Final for _, tt := range []struct{ ratio, factor float64 }{{0, 1}, {1, 1}, {1.1, .99}, {1.5, .75}, {2, 0}, {3, 0}} { item := testGear("weight", "helmet") item.Weight = capacity * tt.ratio c.View.Items = []p.ItemInstance{item} w.RefreshStats(c) if math.Abs(w.playerSpeed(c)/c.View.Stats["moveSpeed"].Final-tt.factor) > 1e-8 || math.Abs(*c.View.MovementFactor-tt.factor) > 1e-8 { t.Fatalf("wrong movement at %.1fx capacity", tt.ratio) } } before := c.View.Entity.Position c.MoveX = 1 w.moveCharacter(c, .1) if c.View.Entity.Position.X != before.X || w.jump(c) == nil { t.Fatal("immobilized player moved or jumped") } w.Kind = "hub" w.Phase = "hub" house := ID() pos := p.Vec3{} c.View.HouseID = &house c.View.HousePosition = &pos c.HouseRoom = &p.HouseScene{ID: house, Size: 20} c.LastMove = w.now w.moveHouse(c, .1) if c.View.HousePosition.X != 0 || w.houseIntent(c, p.Intent{Op: "jump"}) == nil { t.Fatal("house bypassed encumbrance") } c.View.HouseID = nil c.View.HousePosition = nil c.HouseRoom = nil w.Kind = "survival" w.Phase = "won" c.View.Entity.State = "eligible" if err := w.Intent(c.View.Entity.ID, p.Intent{Seq: 1, Op: "drop", Target: "weight"}); err != nil { t.Fatal("winner cannot shed weight to reach portal", err) } if w.EncumbranceFactor(c) != 1 { t.Fatal("dropping weight did not restore movement") } } func TestItemActionsRejectWithoutConsumptionAndHealTargets(t *testing.T) { w, c := fixture(t) c.View.Inventory["bandage"] = 5 other, err := w.Join("other", "Other") if err != nil { t.Fatal(err) } _ = w.Connect(other.View.Entity.ID) other.View.Entity.Position = c.View.Entity.Position other.View.Entity.HP = 1 req := itemRequest(c, "use", "bandage") req.Target = other.View.Entity.ID for _, mode := range []string{"far", "dead", "sheltered", "house", "full"} { other.View.Entity.Position = c.View.Entity.Position other.View.Entity.State = "alive" other.Sheltered = false other.View.HouseID = nil other.View.Entity.HP = 1 switch mode { case "far": other.View.Entity.Position.X += 100 case "dead": other.View.Entity.State = "dead" case "sheltered": other.Sheltered = true case "house": other.View.HouseID = stringPtr(ID()) case "full": other.View.Entity.HP = other.View.Entity.MaxHP } if _, err = w.ItemAction(c, req); err == nil || c.View.Inventory["bandage"] != 5 { t.Fatalf("accepted invalid healing target %s", mode) } } other.View.Entity.HP = 1 if _, err = w.ItemAction(c, req); err != nil || other.View.Entity.HP != math.Min(other.View.Entity.MaxHP, 1+w.Balance.BandageHealing) || c.View.Inventory["bandage"] != 4 { t.Fatal("targeted healing failed", err) } if _, err = w.ItemAction(c, req); err == nil { t.Fatal("item cooldown bypassed") } w.now = w.now.Add(2 * time.Second) mob := &Mob{Entity: p.Entity{ID: "heal-target", Kind: "mob", State: "alive", HP: 1, MaxHP: 100, Position: c.View.Entity.Position}} w.Mobs[mob.Entity.ID] = mob req.Target = mob.Entity.ID if _, err = w.ItemAction(c, req); err != nil || mob.Entity.HP <= 1 { t.Fatal("mob healing failed", err) } w.now = w.now.Add(2 * time.Second) key := testGear("key", "bag") key.HouseID = stringPtr(ID()) c.View.Items = append(c.View.Items, key) if _, err = w.ItemAction(c, itemRequest(c, "destroy", key.ID)); err == nil || ownedItem(c, key.ID) == nil { t.Fatal("generic destruction bypassed key timer") } } func TestBreakdownOriginalRecipeBatchAndDiscovery(t *testing.T) { w, c := fixture(t) c.View.Inventory = map[string]int64{} old := content.CraftRecipe{ID: "original", Revision: 1, Name: "Old spear", Profession: "weapon_smithing", Level: 1, XP: 20, Inputs: map[string]int64{"wood": 100, "ore": 20}, Outputs: map[string]int64{"spear": 2}} current := old current.Revision = 2 current.Inputs = map[string]int64{"herb": 100} w.Balance.Crafting.RecipeHistory = []content.CraftRecipe{old} w.Balance.Crafting.Recipes = []content.CraftRecipe{current} item := testGear("old-spear", "weapon") item.DefinitionID = "spear" item.Provenance = &p.CraftProvenance{CharacterID: c.View.Entity.ID, CrafterName: c.View.Entity.Name, WorldID: w.ID, Profession: old.Profession, Level: 1, RecipeID: old.ID, RecipeRevision: 1} c.View.Items = []p.ItemInstance{item} c.View.DiscoveredRecipes = map[string]p.KnownRecipe{} // Find a deterministic success for the actual 0.01% boundary, not a modified rule. for w.randomUnit() >= .0001 { } w.RandomCounter-- result, err := w.ItemAction(c, itemRequest(c, "breakdown", item.ID)) if err != nil || c.View.Inventory["wood"] != 12 || c.View.Inventory["ore"] != 2 || c.View.Inventory["herb"] != 0 || len(c.View.Items) != 0 { t.Fatal("breakdown did not use original recipe divided by output batch", result, err) } if result.Recipe == nil || result.Recipe.Recipe.Revision != 1 || c.View.Crafting["weapon_smithing"].XP != 0 || c.View.Experience != 0 { t.Fatal("wrong discovery or unexpected experience") } w.now = w.now.Add(2 * time.Second) c.View.Items = []p.ItemInstance{item} w.Balance.Crafting.RecipeHistory = nil if _, err = w.ItemAction(c, itemRequest(c, "breakdown", item.ID)); err == nil || len(c.View.Items) != 1 { t.Fatal("missing historical recipe substituted current revision") } // In a hub, high crafting skill raises recovery but never awards XP. w.Kind = "hub" w.Phase = "hub" w.Balance.Crafting.RecipeHistory = []content.CraftRecipe{old} cp := c.View.Crafting["weapon_smithing"] cp.XP = 1000000 c.View.Crafting[cp.ID] = cp result, err = w.ItemAction(c, itemRequest(c, "breakdown", item.ID)) if err != nil || result.Returned["wood"] <= 12 || result.Returned["wood"] >= 38 || c.View.Crafting[cp.ID].XP != cp.XP { t.Fatal("higher skill recovery or hub XP changed", result, err) } } func TestConsumableCoatingOwnershipExpiryAndRestore(t *testing.T) { w, c := fixture(t) definition := content.GatheringConsumable{ID: "poison", Name: "Weapon poison", Seconds: 10, UseEffect: &content.ItemUseEffect{Kind: "weapon-coating", Amount: 7}} w.Balance.Gathering.Consumables = append(w.Balance.Gathering.Consumables, definition) c.View.Inventory[definition.ID] = 2 weapon := testGear("coated", "weapon") c.View.Items = append(c.View.Items, weapon) req := itemRequest(c, "use", definition.ID) req.Target = "not-owned" if _, err := w.ItemAction(c, req); err == nil || c.View.Inventory[definition.ID] != 2 { t.Fatal("coated unowned weapon") } req.Target = weapon.ID if _, err := w.ItemAction(c, req); err != nil { t.Fatal(err) } coating := ownedItem(c, weapon.ID).Coating if coating == nil || coating.Damage != 7 || c.View.Inventory[definition.ID] != 1 { t.Fatal("coating was not copied onto exact item") } if err := w.consumeGathering(c, definition.ID); err == nil { t.Fatal("legacy consume bypassed target requirement") } 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(ownedItem(restored.Characters[c.View.Entity.ID], weapon.ID).Coating, coating) { t.Fatal("restore lost coating") } _ = w.equip(c, weapon.ID, "weapon") w.Balance.CriticalMultiplier = 1 mob := &Mob{Entity: p.Entity{ID: "damage-target", Kind: "mob", State: "alive", HP: 1000, MaxHP: 1000, Position: c.View.Entity.Position}} w.Mobs[mob.Entity.ID] = mob w.now = w.now.Add(2 * time.Second) if err := w.attack(c, mob.Entity.ID); err != nil { t.Fatal(err) } coatedDamage := 1000 - mob.Entity.HP w.now = w.now.Add(20 * time.Second) mob.Entity.HP = 1000 if err := w.attack(c, mob.Entity.ID); err != nil { t.Fatal(err) } normalDamage := 1000 - mob.Entity.HP if math.Abs(coatedDamage-normalDamage-7) > 1e-8 { t.Fatal("coating damage did not expire", coatedDamage, normalDamage) } }