server/npc_profile_evidence_test.go (6332B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "testing" 7 "time" 8 ) 9 10 func TestNPCProfileSeparatesSpeciesEvidenceFromMatchingBodies(t *testing.T) { 11 for _, status := range []string{"inferred_species", "best_effort", "placeholder_creature", "placeholder_human"} { 12 t.Run(status, func(t *testing.T) { 13 data := testNPCData() 14 data.AppearanceStatuses["placeholder_human"] = gamedata.Evidence{Confidence: "placeholder"} 15 group := &data.Playfields[0].Districts[0].Groups[0] 16 group.Name, group.Levels, group.AppearanceStatus = "Leet", [2]uint16{1, 1}, status 17 useNPCData(t, data) 18 19 world := newTestServer(t) 20 if err := world.flushWorld(); err != nil { 21 t.Fatal(err) 22 } 23 cold := newTestInventoryServer(t) 24 25 for _, current := range []*server{world, cold} { 26 npc := current.characters[1000000] 27 if npc.monsterData != 17655 || npc.appearance != 229 || npc.name != "Leet" || npc.npcDefinitionID != "test/npc" { 28 t.Fatal("profile admission changed the visible placeholder or stable definition") 29 } 30 profile := status == "inferred_species" 31 if (npc.combatProfile != nil) != profile { 32 t.Fatal("visible body or name substituted for species evidence", status, npc.combatProfile) 33 } 34 packet := targetPacket(characterPacket(npc)) 35 end := int(binary.BigEndian.Uint16(packet[6:8])) 36 weapon := binary.BigEndian.Uint32(packet[end-16 : end-12]) 37 if profile && weapon != 120910 || !profile && weapon != 43712 { 38 t.Fatal("native initialization disagrees with profile eligibility", weapon) 39 } 40 } 41 }) 42 } 43 } 44 45 func TestNPCPlaceholderDeathDoesNotGenerateLeetRewards(t *testing.T) { 46 for _, status := range []string{"inferred_species", "placeholder_creature"} { 47 t.Run(status, func(t *testing.T) { 48 data := testNPCData() 49 group := &data.Playfields[0].Districts[0].Groups[0] 50 group.Levels, group.AppearanceStatus = [2]uint16{1, 1}, status 51 useNPCData(t, data) 52 world := newTestServer(t) 53 world.combatRoll = func(uint32) uint32 { return 0 } 54 player := world.newCharacter() 55 player.position = group.Positions[0] 56 if err := world.saveUser(player); err != nil { 57 t.Fatal(err) 58 } 59 world.characters[1000000].health = 1 60 if err := world.flushWorld(); err != nil { 61 t.Fatal(err) 62 } 63 client := &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 64 world.clients[player.id] = client 65 66 if err := world.handleCombat(client, attackPacket(player.id, 1000000)[20:]); err != nil { 67 t.Fatal(err) 68 } 69 world.advanceCombat(player.nextAttack) 70 cold := newTestInventoryServer(t) 71 72 for _, current := range []*server{world, cold} { 73 npc := current.characters[1000000] 74 if npc.health != 0 || npc.corpse == nil || npc.corpse.looter != player.id { 75 t.Fatal("profile rejection disabled the ordinary death or corpse contract") 76 } 77 if status == "inferred_species" { 78 if current.characters[player.id].experience != 100 || current.nextItem != 2 || len(npc.corpse.items) != 2 || npc.corpse.items[0].Template != 42640 || npc.corpse.items[1].Template != 70562 || npc.corpse.credits != 1 || len(current.npcRespawns) != 1 { 79 t.Fatal("a supported Leet lost its production rewards or lifecycle") 80 } 81 } else if current.characters[player.id].experience != 0 || current.nextItem != 0 || len(npc.corpse.items) != 0 || npc.corpse.credits != 0 || len(current.npcRespawns) != 0 || !npc.corpse.expiresAt.IsZero() { 82 t.Fatal("a placeholder acquired Leet XP, items, cash or lifecycle settings", npc.corpse) 83 } 84 } 85 }) 86 } 87 } 88 89 func TestNPCProfileColdLoadingRechecksDefinitionEvidence(t *testing.T) { 90 for _, scenario := range []string{"missing definition", "different monster", "placeholder evidence"} { 91 t.Run(scenario, func(t *testing.T) { 92 data := testNPCData() 93 group := &data.Playfields[0].Districts[0].Groups[0] 94 group.Levels, group.AppearanceStatus = [2]uint16{1, 1}, "inferred_species" 95 useNPCData(t, data) 96 world := newTestServer(t) 97 switch scenario { 98 case "missing definition": 99 world.characters[1000000].npcDefinitionID = "unresolved" 100 case "different monster": 101 group.MonsterData = 17627 102 case "placeholder evidence": 103 group.AppearanceStatus = "placeholder_creature" 104 } 105 if err := world.flushWorld(); err != nil { 106 t.Fatal(err) 107 } 108 109 cold := newTestInventoryServer(t) 110 111 if cold.characters[1000000].monsterData != 17655 || cold.characters[1000000].combatProfile != nil { 112 t.Fatal("cold loading inferred a species profile from saved rendering data", scenario) 113 } 114 }) 115 } 116 } 117 118 func TestNPCReplacementRechecksSpeciesEvidence(t *testing.T) { 119 for _, restart := range []bool{false, true} { 120 name := "live source" 121 if restart { 122 name = "cold source" 123 } 124 t.Run(name, func(t *testing.T) { 125 data := testNPCData() 126 group := &data.Playfields[0].Districts[0].Groups[0] 127 group.Levels, group.AppearanceStatus = [2]uint16{1, 1}, "inferred_species" 128 useNPCData(t, data) 129 world := newTestServer(t) 130 player := world.newCharacter() 131 player.position = group.Positions[0] 132 if err := world.saveUser(player); err != nil { 133 t.Fatal(err) 134 } 135 world.characters[1000000].health = 1 136 if err := world.flushWorld(); err != nil { 137 t.Fatal(err) 138 } 139 client := &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 140 world.clients[player.id] = client 141 if err := world.handleCombat(client, attackPacket(player.id, 1000000)[20:]); err != nil { 142 t.Fatal(err) 143 } 144 death := player.nextAttack 145 world.advanceCombat(death) 146 group.AppearanceStatus = "placeholder_creature" 147 if restart { 148 world = newTestInventoryServer(t) 149 } 150 151 world.advanceNPCRespawns(death.Add(100 * time.Second)) 152 cold := newTestInventoryServer(t) 153 154 for _, current := range []*server{world, cold} { 155 npc := current.characters[1000001] 156 if npc == nil || npc.combatProfile != nil || npc.monsterData != 17655 || npc.appearance != 229 || npc.npcDefinitionID != "test/npc" || npc.health != 100 { 157 t.Fatal("replacement copied an ineligible cached profile", npc) 158 } 159 if current.characters[player.id].experience != 100 || current.characters[1000000].corpse == nil { 160 t.Fatal("profile admission reversed an already committed kill") 161 } 162 } 163 }) 164 } 165 }