server/npc_levels_test.go (5976B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "bytes" 6 _ "embed" 7 "encoding/binary" 8 "encoding/json" 9 "slices" 10 "strings" 11 "testing" 12 "time" 13 ) 14 15 //go:embed testdata/npc-level-population-assignments.json 16 var npcLevelPopulationAssignments []byte 17 18 func TestNPCLevelPopulationAssignmentsRetainHandoffEvidence(t *testing.T) { 19 var fixture struct { 20 Assignments map[string]struct { 21 Variants []struct { 22 Alternatives []string 23 Groups []string 24 } 25 } 26 } 27 if err := json.Unmarshal(npcLevelPopulationAssignments, &fixture); err != nil { 28 t.Fatal(err) 29 } 30 groups, placements := 0, 0 31 32 for _, scenario := range []struct { 33 code, name, confidence string 34 monster uint32 35 }{{"SR01", "Tiny Stalker", "medium", 17708}, {"DZRA", "Rollerrat", "low", 17687}} { 36 mapping := gamedata.NPCNames.Templates[scenario.code] 37 if mapping.Name != scenario.name || mapping.MonsterData != scenario.monster || mapping.Confidence != scenario.confidence || gamedata.NPCNames.Sources[mapping.Source] != "tools/data/npc_assignments.json" { 38 t.Fatal("selected identity lost its source or confidence", scenario.code, mapping) 39 } 40 for _, variant := range fixture.Assignments[scenario.code].Variants { 41 for _, alternative := range variant.Alternatives { 42 if !strings.Contains(mapping.Comment, alternative) { 43 t.Fatal("selected identity discarded a documented alternative", scenario.code, alternative) 44 } 45 } 46 for _, id := range variant.Groups { 47 group := gamedata.NPCGroups[id] 48 if group == nil || group.TemplateCode != scenario.code || group.Name != scenario.name || group.MonsterData != scenario.monster || group.AppearanceStatus != "best_effort" { 49 t.Fatal("population retained a Leet stand-in instead of its selected assignment", id, group) 50 } 51 groups++ 52 placements += len(group.Positions) 53 } 54 } 55 } 56 if groups != 10 || placements != 82 { 57 t.Fatal("handoff scope lost groups or placements", groups, placements) 58 } 59 } 60 61 func TestNPCLevelTwoUsesInterpolatedBiteForBothObservers(t *testing.T) { 62 data := testNPCData() 63 data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "inferred_species" 64 data.Playfields[0].Districts[0].Groups[0].Levels = [2]uint16{2, 2} 65 useNPCData(t, data) 66 world := newTestServer(t) 67 player, observer := world.newCharacter(), world.newCharacter() 68 first, second := &combatTestConn{}, &combatTestConn{} 69 world.clients[player.id] = &worldClient{id: player.id, conn: first, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 70 world.clients[observer.id] = &worldClient{id: observer.id, conn: second, target: true, ready: true, players: map[uint32]bool{player.id: true}, npcs: map[uint32]bool{1000000: true}} 71 npc := world.characters[1000000] 72 73 if err := world.handleCombat(world.clients[player.id], attackPacket(player.id, npc.id)[20:]); err != nil { 74 t.Fatal(err) 75 } 76 start := player.nextAttack 77 world.advanceCombat(start) 78 world.advanceCombat(start.Add(1750*time.Millisecond - time.Nanosecond)) 79 if player.health != 34 || npc.nextAttack != start.Add(1750*time.Millisecond) { 80 t.Fatal("level-2 bite did not retain its authored attack phase", player.health, npc.nextAttack.Sub(start)) 81 } 82 world.advanceCombat(start.Add(1750 * time.Millisecond)) 83 world.advanceCombat(start.Add(5250 * time.Millisecond)) 84 85 var damage []uint32 86 for index, packet := range first.packets { 87 if index >= len(second.packets) || !bytes.Equal(packet[16:], second.packets[index][16:]) { 88 t.Fatal("observers received different level-2 combat results") 89 } 90 if binary.BigEndian.Uint32(packet[16:20]) == 0x46002f16 && binary.BigEndian.Uint32(packet[24:28]) == npc.id { 91 damage = append(damage, binary.BigEndian.Uint32(packet[29:33])) 92 } 93 } 94 if !slices.Equal(damage, []uint32{9, 9}) || player.health != 16 || npc.maximumHealth() != 20 || npc.nextAttack != start.Add(8750*time.Millisecond) { 95 t.Fatal("level-2 profile did not use QL2 damage, selected resources and catalog timing", damage, player.health, npc.maximumHealth(), npc.nextAttack.Sub(start)) 96 } 97 if itemStat(gamedata.ItemTemplates[120910], 285) != 8 || itemStat(gamedata.ItemTemplates[120911], 285) != 406 { 98 t.Fatal("interpolation changed a shared catalog endpoint") 99 } 100 } 101 102 func TestNPCLevelTwoDeathRetainsProductionRewardsAcrossColdPickup(t *testing.T) { 103 world, _ := newKillExperienceWorld(t, "unarmed") 104 target := world.characters[1000000] 105 target.level, target.combatProfile = 2, gamedata.NPCCombatProfiles[17655][2] 106 // Select Monster Parts without the independent equipment drop. 107 world.combatRoll = func(limit uint32) uint32 { 108 if limit == 100 { 109 return 10 110 } 111 return 0 112 } 113 if err := world.flushWorld(); err != nil { 114 t.Fatal(err) 115 } 116 identity := world.nextItem + 1 117 118 attackForExperience(t, world, "unarmed") 119 world = newTestInventoryServer(t) 120 client := &worldClient{id: 1, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 121 world.clients[1] = client 122 if err := world.handleLootUse(client, lootUseRequest(1)); err != nil { 123 t.Fatal(err) 124 } 125 if err := world.handleInventory(client, lootTakeRequest()); err != nil { 126 t.Fatal(err) 127 } 128 if err := world.handleLootUse(client, lootUseRequest(1)); err != nil { 129 t.Fatal(err) 130 } 131 cold := newTestInventoryServer(t) 132 133 expected := []ownedItem{{ID: identity, Type: 51005, Template: 42640, Quality: 1, Slot: 64, Count: 1}} 134 for _, current := range []*server{world, cold} { 135 if !slices.Equal(current.characters[1].inventory.Items, expected) || current.characters[1].inventory.Credits != 1 || current.characters[1].experience != 100 || current.nextItem != identity { 136 t.Fatal("level-2 kill, cold pickup or repeated open lost or repeated rewards", current.characters[1].inventory, current.characters[1].experience) 137 } 138 if current.characters[1000000].combatProfile == nil || current.characters[1000000].level != 2 || len(current.characters[1000000].corpse.items) != 0 { 139 t.Fatal("cold state lost the exact-level profile or retained its collected item") 140 } 141 } 142 }